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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_device/audio_device_buffer.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/logging.h" |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/numerics/safe_conversions.h" |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 20 | FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* audio_device_buffer) |
| 21 | : audio_device_buffer_(audio_device_buffer), |
| 22 | playout_samples_per_channel_10ms_(rtc::dchecked_cast<size_t>( |
| 23 | audio_device_buffer->PlayoutSampleRate() * 10 / 1000)), |
| 24 | record_samples_per_channel_10ms_(rtc::dchecked_cast<size_t>( |
| 25 | audio_device_buffer->RecordingSampleRate() * 10 / 1000)), |
| 26 | playout_channels_(audio_device_buffer->PlayoutChannels()), |
| 27 | record_channels_(audio_device_buffer->RecordingChannels()) { |
| 28 | RTC_DCHECK(audio_device_buffer_); |
| 29 | if (IsReadyForPlayout()) { |
| 30 | RTC_DLOG(INFO) << "playout_samples_per_channel_10ms: " |
| 31 | << playout_samples_per_channel_10ms_; |
| 32 | RTC_DLOG(INFO) << "playout_channels: " << playout_channels_; |
| 33 | } |
| 34 | if (IsReadyForRecord()) { |
| 35 | RTC_DLOG(INFO) << "record_samples_per_channel_10ms: " |
| 36 | << record_samples_per_channel_10ms_; |
| 37 | RTC_DLOG(INFO) << "record_channels: " << record_channels_; |
| 38 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | FineAudioBuffer::~FineAudioBuffer() {} |
| 42 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 43 | void FineAudioBuffer::ResetPlayout() { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 44 | playout_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void FineAudioBuffer::ResetRecord() { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 48 | record_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 49 | } |
| 50 | |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 51 | bool FineAudioBuffer::IsReadyForPlayout() const { |
| 52 | return playout_samples_per_channel_10ms_ > 0 && playout_channels_ > 0; |
| 53 | } |
| 54 | |
| 55 | bool FineAudioBuffer::IsReadyForRecord() const { |
| 56 | return record_samples_per_channel_10ms_ > 0 && record_channels_ > 0; |
| 57 | } |
| 58 | |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 59 | void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int16_t> audio_buffer, |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 60 | int playout_delay_ms) { |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 61 | RTC_DCHECK(IsReadyForPlayout()); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 62 | // Ask WebRTC for new data in chunks of 10ms until we have enough to |
| 63 | // fulfill the request. It is possible that the buffer already contains |
| 64 | // enough samples from the last round. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 65 | while (playout_buffer_.size() < audio_buffer.size()) { |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 66 | // Get 10ms decoded audio from WebRTC. The ADB knows about number of |
| 67 | // channels; hence we can ask for number of samples per channel here. |
henrika | ec9c745 | 2018-06-08 16:10:03 +0200 | [diff] [blame] | 68 | if (audio_device_buffer_->RequestPlayoutData( |
| 69 | playout_samples_per_channel_10ms_) == |
| 70 | static_cast<int32_t>(playout_samples_per_channel_10ms_)) { |
| 71 | // Append 10ms to the end of the local buffer taking number of channels |
| 72 | // into account. |
| 73 | const size_t num_elements_10ms = |
| 74 | playout_channels_ * playout_samples_per_channel_10ms_; |
| 75 | const size_t written_elements = playout_buffer_.AppendData( |
| 76 | num_elements_10ms, [&](rtc::ArrayView<int16_t> buf) { |
| 77 | const size_t samples_per_channel_10ms = |
| 78 | audio_device_buffer_->GetPlayoutData(buf.data()); |
| 79 | return playout_channels_ * samples_per_channel_10ms; |
| 80 | }); |
| 81 | RTC_DCHECK_EQ(num_elements_10ms, written_elements); |
| 82 | } else { |
| 83 | // Provide silence if AudioDeviceBuffer::RequestPlayoutData() fails. |
| 84 | // Can e.g. happen when an AudioTransport has not been registered. |
| 85 | const size_t num_bytes = audio_buffer.size() * sizeof(int16_t); |
| 86 | std::memset(audio_buffer.data(), 0, num_bytes); |
| 87 | return; |
| 88 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 89 | } |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 90 | |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 91 | // Provide the requested number of bytes to the consumer. |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 92 | const size_t num_bytes = audio_buffer.size() * sizeof(int16_t); |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 93 | memcpy(audio_buffer.data(), playout_buffer_.data(), num_bytes); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 94 | // Move remaining samples to start of buffer to prepare for next round. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 95 | memmove(playout_buffer_.data(), playout_buffer_.data() + audio_buffer.size(), |
| 96 | (playout_buffer_.size() - audio_buffer.size()) * sizeof(int16_t)); |
| 97 | playout_buffer_.SetSize(playout_buffer_.size() - audio_buffer.size()); |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 98 | // Cache playout latency for usage in DeliverRecordedData(); |
| 99 | playout_delay_ms_ = playout_delay_ms; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 100 | } |
| 101 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 102 | void FineAudioBuffer::DeliverRecordedData( |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 103 | rtc::ArrayView<const int16_t> audio_buffer, |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 104 | int record_delay_ms) { |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 105 | RTC_DCHECK(IsReadyForRecord()); |
| 106 | // Always append new data and grow the buffer when needed. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 107 | record_buffer_.AppendData(audio_buffer.data(), audio_buffer.size()); |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 108 | // Consume samples from buffer in chunks of 10ms until there is not |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 109 | // enough data left. The number of remaining samples in the cache is given by |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 110 | // the new size of the internal |record_buffer_|. |
| 111 | const size_t num_elements_10ms = |
| 112 | record_channels_ * record_samples_per_channel_10ms_; |
| 113 | while (record_buffer_.size() >= num_elements_10ms) { |
| 114 | audio_device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
| 115 | record_samples_per_channel_10ms_); |
| 116 | audio_device_buffer_->SetVQEData(playout_delay_ms_, record_delay_ms); |
| 117 | audio_device_buffer_->DeliverRecordedData(); |
| 118 | memmove(record_buffer_.data(), record_buffer_.data() + num_elements_10ms, |
| 119 | (record_buffer_.size() - num_elements_10ms) * sizeof(int16_t)); |
| 120 | record_buffer_.SetSize(record_buffer_.size() - num_elements_10ms); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace webrtc |