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)), |
| 30 | bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)), |
| 31 | playout_cached_buffer_start_(0), |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame^] | 32 | playout_cached_bytes_(0) { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 33 | playout_cache_buffer_.reset(new int8_t[bytes_per_10_ms_]); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | FineAudioBuffer::~FineAudioBuffer() {} |
| 37 | |
| 38 | size_t FineAudioBuffer::RequiredPlayoutBufferSizeBytes() { |
| 39 | // It is possible that we store the desired frame size - 1 samples. Since new |
| 40 | // audio frames are pulled in chunks of 10ms we will need a buffer that can |
| 41 | // hold desired_frame_size - 1 + 10ms of data. We omit the - 1. |
| 42 | return desired_frame_size_bytes_ + bytes_per_10_ms_; |
| 43 | } |
| 44 | |
| 45 | void FineAudioBuffer::ResetPlayout() { |
| 46 | playout_cached_buffer_start_ = 0; |
| 47 | playout_cached_bytes_ = 0; |
| 48 | memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_); |
| 49 | } |
| 50 | |
| 51 | void FineAudioBuffer::ResetRecord() { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame^] | 52 | record_buffer_.Clear(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { |
| 56 | if (desired_frame_size_bytes_ <= playout_cached_bytes_) { |
| 57 | memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
| 58 | desired_frame_size_bytes_); |
| 59 | playout_cached_buffer_start_ += desired_frame_size_bytes_; |
| 60 | playout_cached_bytes_ -= desired_frame_size_bytes_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 61 | RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, |
| 62 | bytes_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
| 66 | playout_cached_bytes_); |
| 67 | // Push another n*10ms of audio to |buffer|. n > 1 if |
| 68 | // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we |
| 69 | // write the audio after the cached bytes copied earlier. |
| 70 | int8_t* unwritten_buffer = &buffer[playout_cached_bytes_]; |
| 71 | int bytes_left = |
| 72 | static_cast<int>(desired_frame_size_bytes_ - playout_cached_bytes_); |
| 73 | // Ceiling of integer division: 1 + ((x - 1) / y) |
| 74 | size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_); |
| 75 | for (size_t i = 0; i < number_of_requests; ++i) { |
| 76 | device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
| 77 | int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); |
| 78 | if (static_cast<size_t>(num_out) != samples_per_10_ms_) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 79 | RTC_CHECK_EQ(num_out, 0); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 80 | playout_cached_bytes_ = 0; |
| 81 | return; |
| 82 | } |
| 83 | unwritten_buffer += bytes_per_10_ms_; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 84 | RTC_CHECK_GE(bytes_left, 0); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 85 | bytes_left -= static_cast<int>(bytes_per_10_ms_); |
| 86 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 87 | RTC_CHECK_LE(bytes_left, 0); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 88 | // Put the samples that were written to |buffer| but are not used in the |
| 89 | // cache. |
| 90 | size_t cache_location = desired_frame_size_bytes_; |
| 91 | int8_t* cache_ptr = &buffer[cache_location]; |
| 92 | playout_cached_bytes_ = number_of_requests * bytes_per_10_ms_ - |
| 93 | (desired_frame_size_bytes_ - playout_cached_bytes_); |
| 94 | // If playout_cached_bytes_ is larger than the cache buffer, uninitialized |
| 95 | // memory will be read. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 96 | RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 97 | RTC_CHECK_EQ(-bytes_left, playout_cached_bytes_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 98 | playout_cached_buffer_start_ = 0; |
| 99 | memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); |
| 100 | } |
| 101 | |
| 102 | void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, |
| 103 | size_t size_in_bytes, |
| 104 | int playout_delay_ms, |
| 105 | int record_delay_ms) { |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame^] | 106 | // Always append new data and grow the buffer if needed. |
| 107 | record_buffer_.AppendData(buffer, size_in_bytes); |
| 108 | // Consume samples from buffer in chunks of 10ms until there is not |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 109 | // 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^] | 110 | // the new size of the buffer. |
| 111 | while (record_buffer_.size() >= bytes_per_10_ms_) { |
| 112 | device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
| 113 | samples_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 114 | device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
| 115 | device_buffer_->DeliverRecordedData(); |
henrika | f166e1b | 2017-02-23 02:44:55 -0800 | [diff] [blame^] | 116 | memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, |
| 117 | record_buffer_.size() - bytes_per_10_ms_); |
| 118 | record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | } // namespace webrtc |