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 | bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)), |
| 30 | playout_buffer_(0, capacity), |
| 31 | record_buffer_(0, capacity) { |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame^] | 32 | RTC_LOG(INFO) << "samples_per_10_ms_: " << samples_per_10_ms_; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | FineAudioBuffer::~FineAudioBuffer() {} |
| 36 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 37 | void FineAudioBuffer::ResetPlayout() { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 38 | playout_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void FineAudioBuffer::ResetRecord() { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 42 | record_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 43 | } |
| 44 | |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame^] | 45 | void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer, |
| 46 | int playout_delay_ms) { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 47 | // Ask WebRTC for new data in chunks of 10ms until we have enough to |
| 48 | // fulfill the request. It is possible that the buffer already contains |
| 49 | // enough samples from the last round. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 50 | const size_t num_bytes = audio_buffer.size(); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 51 | while (playout_buffer_.size() < num_bytes) { |
| 52 | // Get 10ms decoded audio from WebRTC. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 53 | device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 54 | // Append |bytes_per_10_ms_| elements to the end of the buffer. |
| 55 | const size_t bytes_written = playout_buffer_.AppendData( |
| 56 | bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) { |
| 57 | const size_t samples_per_channel = |
| 58 | device_buffer_->GetPlayoutData(buf.data()); |
| 59 | // TODO(henrika): this class is only used on mobile devices and is |
| 60 | // currently limited to mono. Modifications are needed for stereo. |
| 61 | return sizeof(int16_t) * samples_per_channel; |
| 62 | }); |
| 63 | RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 64 | } |
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. |
| 68 | memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes, |
| 69 | playout_buffer_.size() - num_bytes); |
| 70 | playout_buffer_.SetSize(playout_buffer_.size() - num_bytes); |
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( |
| 76 | rtc::ArrayView<const int8_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 | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 81 | // enough data left. The number of remaining bytes in the cache is given by |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 82 | // the new size of the buffer. |
| 83 | while (record_buffer_.size() >= bytes_per_10_ms_) { |
| 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 | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 88 | memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, |
| 89 | record_buffer_.size() - bytes_per_10_ms_); |
| 90 | record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace webrtc |