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