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 | |
| 11 | #include "webrtc/modules/audio_device/fine_audio_buffer.h" |
| 12 | |
| 13 | #include <memory.h> |
| 14 | #include <stdio.h> |
| 15 | #include <algorithm> |
| 16 | |
Henrik Kjellander | dca1e09 | 2017-07-01 16:42:22 +0200 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/base/logging.h" |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 19 | #include "webrtc/modules/audio_device/audio_device_buffer.h" |
| 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) { |
| 32 | 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 | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 45 | void FineAudioBuffer::GetPlayoutData(rtc::ArrayView<int8_t> audio_buffer) { |
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 | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 49 | const size_t num_bytes = audio_buffer.size(); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 50 | while (playout_buffer_.size() < num_bytes) { |
| 51 | // Get 10ms decoded audio from WebRTC. |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 52 | device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 53 | // Append |bytes_per_10_ms_| elements to the end of the buffer. |
| 54 | const size_t bytes_written = playout_buffer_.AppendData( |
| 55 | bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) { |
| 56 | const size_t samples_per_channel = |
| 57 | device_buffer_->GetPlayoutData(buf.data()); |
| 58 | // TODO(henrika): this class is only used on mobile devices and is |
| 59 | // currently limited to mono. Modifications are needed for stereo. |
| 60 | return sizeof(int16_t) * samples_per_channel; |
| 61 | }); |
| 62 | RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 63 | } |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 64 | // Provide the requested number of bytes to the consumer. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 65 | memcpy(audio_buffer.data(), playout_buffer_.data(), num_bytes); |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame] | 66 | // Move remaining samples to start of buffer to prepare for next round. |
| 67 | memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes, |
| 68 | playout_buffer_.size() - num_bytes); |
| 69 | playout_buffer_.SetSize(playout_buffer_.size() - num_bytes); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 70 | } |
| 71 | |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 72 | void FineAudioBuffer::DeliverRecordedData( |
| 73 | rtc::ArrayView<const int8_t> audio_buffer, |
| 74 | int playout_delay_ms, |
| 75 | int record_delay_ms) { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 76 | // Always append new data and grow the buffer if needed. |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 77 | record_buffer_.AppendData(audio_buffer.data(), audio_buffer.size()); |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 78 | // Consume samples from buffer in chunks of 10ms until there is not |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 79 | // 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] | 80 | // the new size of the buffer. |
| 81 | while (record_buffer_.size() >= bytes_per_10_ms_) { |
| 82 | device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
| 83 | samples_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 84 | device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
| 85 | device_buffer_->DeliverRecordedData(); |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 86 | memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, |
| 87 | record_buffer_.size() - bytes_per_10_ms_); |
| 88 | record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | } // namespace webrtc |