henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_device/fine_audio_buffer.h" |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 12 | |
| 13 | #include <memory.h> |
| 14 | #include <stdio.h> |
| 15 | #include <algorithm> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_device/audio_device_buffer.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 24 | int sample_rate, |
| 25 | size_t capacity) |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 26 | : device_buffer_(device_buffer), |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 27 | sample_rate_(sample_rate), |
| 28 | samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)), |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 29 | playout_buffer_(0, capacity), |
| 30 | record_buffer_(0, capacity) { |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 31 | RTC_LOG(INFO) << "samples_per_10_ms_: " << samples_per_10_ms_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | FineAudioBuffer::~FineAudioBuffer() {} |
| 35 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 36 | void FineAudioBuffer::ResetPlayout() { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 37 | playout_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void FineAudioBuffer::ResetRecord() { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 41 | record_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 42 | } |
| 43 | |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 44 | void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer, |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 45 | int playout_delay_ms) { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 46 | // Ask WebRTC for new data in chunks of 10ms until we have enough to |
| 47 | // fulfill the request. It is possible that the buffer already contains |
| 48 | // enough samples from the last round. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 49 | while (playout_buffer_.size() < audio_buffer.size()) { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 50 | // Get 10ms decoded audio from WebRTC. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 51 | device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 52 | // Append |bytes_per_10_ms_| elements to the end of the buffer. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 53 | const size_t samples_written = playout_buffer_.AppendData( |
| 54 | samples_per_10_ms_, [&](rtc::ArrayView<int16_t> buf) { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 55 | const size_t samples_per_channel = |
| 56 | device_buffer_->GetPlayoutData(buf.data()); |
| 57 | // TODO(henrika): this class is only used on mobile devices and is |
| 58 | // currently limited to mono. Modifications are needed for stereo. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 59 | return samples_per_channel; |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 60 | }); |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 61 | RTC_DCHECK_EQ(samples_per_10_ms_, samples_written); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 62 | } |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 63 | |
| 64 | const size_t num_bytes = audio_buffer.size() * sizeof(int16_t); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 65 | // Provide the requested number of bytes to the consumer. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 66 | memcpy(audio_buffer.data(), playout_buffer_.data(), num_bytes); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 67 | // Move remaining samples to start of buffer to prepare for next round. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 68 | memmove(playout_buffer_.data(), playout_buffer_.data() + audio_buffer.size(), |
| 69 | (playout_buffer_.size() - audio_buffer.size()) * sizeof(int16_t)); |
| 70 | playout_buffer_.SetSize(playout_buffer_.size() - audio_buffer.size()); |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 71 | // Cache playout latency for usage in DeliverRecordedData(); |
| 72 | playout_delay_ms_ = playout_delay_ms; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 73 | } |
| 74 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 75 | void FineAudioBuffer::DeliverRecordedData( |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 76 | rtc::ArrayView<const int16_t> audio_buffer, |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 77 | int record_delay_ms) { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 78 | // Always append new data and grow the buffer if needed. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 79 | record_buffer_.AppendData(audio_buffer.data(), audio_buffer.size()); |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 80 | // Consume samples from buffer in chunks of 10ms until there is not |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 81 | // enough data left. The number of remaining samples in the cache is given by |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 82 | // the new size of the buffer. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 83 | while (record_buffer_.size() >= samples_per_10_ms_) { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 84 | device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
| 85 | samples_per_10_ms_); |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 86 | device_buffer_->SetVQEData(playout_delay_ms_, record_delay_ms); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 87 | device_buffer_->DeliverRecordedData(); |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame^] | 88 | memmove(record_buffer_.data(), record_buffer_.data() + samples_per_10_ms_, |
| 89 | (record_buffer_.size() - samples_per_10_ms_) * sizeof(int16_t)); |
| 90 | record_buffer_.SetSize(record_buffer_.size() - samples_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace webrtc |