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 | |
| 17 | #include "webrtc/base/checks.h" |
| 18 | #include "webrtc/base/logging.h" |
| 19 | #include "webrtc/modules/audio_device/audio_device_buffer.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, |
| 24 | size_t desired_frame_size_bytes, |
| 25 | int sample_rate) |
| 26 | : device_buffer_(device_buffer), |
| 27 | desired_frame_size_bytes_(desired_frame_size_bytes), |
| 28 | sample_rate_(sample_rate), |
| 29 | samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)), |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame^] | 30 | bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)) { |
| 31 | LOG(INFO) << "desired_frame_size_bytes:" << desired_frame_size_bytes; |
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 | |
| 44 | void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame^] | 45 | const size_t num_bytes = desired_frame_size_bytes_; |
| 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. |
| 49 | while (playout_buffer_.size() < num_bytes) { |
| 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. |
| 53 | const size_t bytes_written = playout_buffer_.AppendData( |
| 54 | bytes_per_10_ms_, [&](rtc::ArrayView<int8_t> buf) { |
| 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. |
| 59 | return sizeof(int16_t) * samples_per_channel; |
| 60 | }); |
| 61 | RTC_DCHECK_EQ(bytes_per_10_ms_, bytes_written); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 62 | } |
henrika | b3ebc1a | 2017-02-27 05:14:17 -0800 | [diff] [blame^] | 63 | // Provide the requested number of bytes to the consumer. |
| 64 | memcpy(buffer, playout_buffer_.data(), num_bytes); |
| 65 | // Move remaining samples to start of buffer to prepare for next round. |
| 66 | memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes, |
| 67 | playout_buffer_.size() - num_bytes); |
| 68 | playout_buffer_.SetSize(playout_buffer_.size() - num_bytes); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, |
| 72 | size_t size_in_bytes, |
| 73 | int playout_delay_ms, |
| 74 | int record_delay_ms) { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 75 | // Always append new data and grow the buffer if needed. |
| 76 | record_buffer_.AppendData(buffer, size_in_bytes); |
| 77 | // Consume samples from buffer in chunks of 10ms until there is not |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 78 | // 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] | 79 | // the new size of the buffer. |
| 80 | while (record_buffer_.size() >= bytes_per_10_ms_) { |
| 81 | device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
| 82 | samples_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 83 | device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
| 84 | device_buffer_->DeliverRecordedData(); |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame] | 85 | memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, |
| 86 | record_buffer_.size() - bytes_per_10_ms_); |
| 87 | record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | } // namespace webrtc |