OLD | NEW |
(Empty) | |
| 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), |
| 32 playout_cached_bytes_(0), |
| 33 // Allocate extra space on the recording side to reduce the number of |
| 34 // memmove() calls. |
| 35 required_record_buffer_size_bytes_( |
| 36 5 * (desired_frame_size_bytes + bytes_per_10_ms_)), |
| 37 record_cached_bytes_(0), |
| 38 record_read_pos_(0), |
| 39 record_write_pos_(0) { |
| 40 playout_cache_buffer_.reset(new int8_t[bytes_per_10_ms_]); |
| 41 record_cache_buffer_.reset(new int8_t[required_record_buffer_size_bytes_]); |
| 42 memset(record_cache_buffer_.get(), 0, required_record_buffer_size_bytes_); |
| 43 } |
| 44 |
| 45 FineAudioBuffer::~FineAudioBuffer() {} |
| 46 |
| 47 size_t FineAudioBuffer::RequiredPlayoutBufferSizeBytes() { |
| 48 // It is possible that we store the desired frame size - 1 samples. Since new |
| 49 // audio frames are pulled in chunks of 10ms we will need a buffer that can |
| 50 // hold desired_frame_size - 1 + 10ms of data. We omit the - 1. |
| 51 return desired_frame_size_bytes_ + bytes_per_10_ms_; |
| 52 } |
| 53 |
| 54 void FineAudioBuffer::ResetPlayout() { |
| 55 playout_cached_buffer_start_ = 0; |
| 56 playout_cached_bytes_ = 0; |
| 57 memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_); |
| 58 } |
| 59 |
| 60 void FineAudioBuffer::ResetRecord() { |
| 61 record_cached_bytes_ = 0; |
| 62 record_read_pos_ = 0; |
| 63 record_write_pos_ = 0; |
| 64 memset(record_cache_buffer_.get(), 0, required_record_buffer_size_bytes_); |
| 65 } |
| 66 |
| 67 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { |
| 68 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { |
| 69 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
| 70 desired_frame_size_bytes_); |
| 71 playout_cached_buffer_start_ += desired_frame_size_bytes_; |
| 72 playout_cached_bytes_ -= desired_frame_size_bytes_; |
| 73 CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, |
| 74 bytes_per_10_ms_); |
| 75 return; |
| 76 } |
| 77 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
| 78 playout_cached_bytes_); |
| 79 // Push another n*10ms of audio to |buffer|. n > 1 if |
| 80 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we |
| 81 // write the audio after the cached bytes copied earlier. |
| 82 int8_t* unwritten_buffer = &buffer[playout_cached_bytes_]; |
| 83 int bytes_left = |
| 84 static_cast<int>(desired_frame_size_bytes_ - playout_cached_bytes_); |
| 85 // Ceiling of integer division: 1 + ((x - 1) / y) |
| 86 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_); |
| 87 for (size_t i = 0; i < number_of_requests; ++i) { |
| 88 device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
| 89 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); |
| 90 if (static_cast<size_t>(num_out) != samples_per_10_ms_) { |
| 91 CHECK_EQ(num_out, 0); |
| 92 playout_cached_bytes_ = 0; |
| 93 return; |
| 94 } |
| 95 unwritten_buffer += bytes_per_10_ms_; |
| 96 CHECK_GE(bytes_left, 0); |
| 97 bytes_left -= static_cast<int>(bytes_per_10_ms_); |
| 98 } |
| 99 CHECK_LE(bytes_left, 0); |
| 100 // Put the samples that were written to |buffer| but are not used in the |
| 101 // cache. |
| 102 size_t cache_location = desired_frame_size_bytes_; |
| 103 int8_t* cache_ptr = &buffer[cache_location]; |
| 104 playout_cached_bytes_ = number_of_requests * bytes_per_10_ms_ - |
| 105 (desired_frame_size_bytes_ - playout_cached_bytes_); |
| 106 // If playout_cached_bytes_ is larger than the cache buffer, uninitialized |
| 107 // memory will be read. |
| 108 CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); |
| 109 CHECK_EQ(static_cast<size_t>(-bytes_left), playout_cached_bytes_); |
| 110 playout_cached_buffer_start_ = 0; |
| 111 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); |
| 112 } |
| 113 |
| 114 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, |
| 115 size_t size_in_bytes, |
| 116 int playout_delay_ms, |
| 117 int record_delay_ms) { |
| 118 CHECK_EQ(size_in_bytes, desired_frame_size_bytes_); |
| 119 // Check if the temporary buffer can store the incoming buffer. If not, |
| 120 // move the remaining (old) bytes to the beginning of the temporary buffer |
| 121 // and start adding new samples after the old samples. |
| 122 if (record_write_pos_ + size_in_bytes > required_record_buffer_size_bytes_) { |
| 123 if (record_cached_bytes_ > 0) { |
| 124 memmove(record_cache_buffer_.get(), |
| 125 record_cache_buffer_.get() + record_read_pos_, |
| 126 record_cached_bytes_); |
| 127 } |
| 128 record_write_pos_ = record_cached_bytes_; |
| 129 record_read_pos_ = 0; |
| 130 } |
| 131 // Add recorded samples to a temporary buffer. |
| 132 memcpy(record_cache_buffer_.get() + record_write_pos_, buffer, size_in_bytes); |
| 133 record_write_pos_ += size_in_bytes; |
| 134 record_cached_bytes_ += size_in_bytes; |
| 135 // Consume samples in temporary buffer in chunks of 10ms until there is not |
| 136 // enough data left. The number of remaining bytes in the cache is given by |
| 137 // |record_cached_bytes_| after this while loop is done. |
| 138 while (record_cached_bytes_ >= bytes_per_10_ms_) { |
| 139 device_buffer_->SetRecordedBuffer( |
| 140 record_cache_buffer_.get() + record_read_pos_, samples_per_10_ms_); |
| 141 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
| 142 device_buffer_->DeliverRecordedData(); |
| 143 // Read next chunk of 10ms data. |
| 144 record_read_pos_ += bytes_per_10_ms_; |
| 145 // Reduce number of cached bytes with the consumed amount. |
| 146 record_cached_bytes_ -= bytes_per_10_ms_; |
| 147 } |
| 148 } |
| 149 |
| 150 } // namespace webrtc |
OLD | NEW |