| 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/android/fine_audio_buffer.h" | 11 #include "webrtc/modules/audio_device/android/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/modules/audio_device/audio_device_buffer.h" | 18 #include "webrtc/modules/audio_device/audio_device_buffer.h" |
| 19 | 19 |
| 20 namespace webrtc { | 20 namespace webrtc { |
| 21 | 21 |
| 22 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, | 22 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, |
| 23 int desired_frame_size_bytes, | 23 size_t desired_frame_size_bytes, |
| 24 int sample_rate) | 24 int sample_rate) |
| 25 : device_buffer_(device_buffer), | 25 : device_buffer_(device_buffer), |
| 26 desired_frame_size_bytes_(desired_frame_size_bytes), | 26 desired_frame_size_bytes_(desired_frame_size_bytes), |
| 27 sample_rate_(sample_rate), | 27 sample_rate_(sample_rate), |
| 28 samples_per_10_ms_(sample_rate_ * 10 / 1000), | 28 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)), |
| 29 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)), | 29 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)), |
| 30 cached_buffer_start_(0), | 30 cached_buffer_start_(0), |
| 31 cached_bytes_(0) { | 31 cached_bytes_(0) { |
| 32 cache_buffer_.reset(new int8_t[bytes_per_10_ms_]); | 32 cache_buffer_.reset(new int8_t[bytes_per_10_ms_]); |
| 33 } | 33 } |
| 34 | 34 |
| 35 FineAudioBuffer::~FineAudioBuffer() { | 35 FineAudioBuffer::~FineAudioBuffer() { |
| 36 } | 36 } |
| 37 | 37 |
| 38 int FineAudioBuffer::RequiredBufferSizeBytes() { | 38 size_t FineAudioBuffer::RequiredBufferSizeBytes() { |
| 39 // It is possible that we store the desired frame size - 1 samples. Since new | 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 | 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. | 41 // hold desired_frame_size - 1 + 10ms of data. We omit the - 1. |
| 42 return desired_frame_size_bytes_ + bytes_per_10_ms_; | 42 return desired_frame_size_bytes_ + bytes_per_10_ms_; |
| 43 } | 43 } |
| 44 | 44 |
| 45 void FineAudioBuffer::GetBufferData(int8_t* buffer) { | 45 void FineAudioBuffer::GetBufferData(int8_t* buffer) { |
| 46 if (desired_frame_size_bytes_ <= cached_bytes_) { | 46 if (desired_frame_size_bytes_ <= cached_bytes_) { |
| 47 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], | 47 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], |
| 48 desired_frame_size_bytes_); | 48 desired_frame_size_bytes_); |
| 49 cached_buffer_start_ += desired_frame_size_bytes_; | 49 cached_buffer_start_ += desired_frame_size_bytes_; |
| 50 cached_bytes_ -= desired_frame_size_bytes_; | 50 cached_bytes_ -= desired_frame_size_bytes_; |
| 51 CHECK_LT(cached_buffer_start_ + cached_bytes_, bytes_per_10_ms_); | 51 CHECK_LT(cached_buffer_start_ + cached_bytes_, bytes_per_10_ms_); |
| 52 return; | 52 return; |
| 53 } | 53 } |
| 54 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], cached_bytes_); | 54 memcpy(buffer, &cache_buffer_.get()[cached_buffer_start_], cached_bytes_); |
| 55 // Push another n*10ms of audio to |buffer|. n > 1 if | 55 // Push another n*10ms of audio to |buffer|. n > 1 if |
| 56 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we | 56 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we |
| 57 // write the audio after the cached bytes copied earlier. | 57 // write the audio after the cached bytes copied earlier. |
| 58 int8_t* unwritten_buffer = &buffer[cached_bytes_]; | 58 int8_t* unwritten_buffer = &buffer[cached_bytes_]; |
| 59 int bytes_left = desired_frame_size_bytes_ - cached_bytes_; | 59 int bytes_left = static_cast<int>(desired_frame_size_bytes_ - cached_bytes_); |
| 60 // Ceiling of integer division: 1 + ((x - 1) / y) | 60 // Ceiling of integer division: 1 + ((x - 1) / y) |
| 61 int number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_); | 61 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_); |
| 62 for (int i = 0; i < number_of_requests; ++i) { | 62 for (size_t i = 0; i < number_of_requests; ++i) { |
| 63 device_buffer_->RequestPlayoutData(samples_per_10_ms_); | 63 device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
| 64 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); | 64 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); |
| 65 if (num_out != samples_per_10_ms_) { | 65 if (static_cast<size_t>(num_out) != samples_per_10_ms_) { |
| 66 CHECK_EQ(num_out, 0); | 66 CHECK_EQ(num_out, 0); |
| 67 cached_bytes_ = 0; | 67 cached_bytes_ = 0; |
| 68 return; | 68 return; |
| 69 } | 69 } |
| 70 unwritten_buffer += bytes_per_10_ms_; | 70 unwritten_buffer += bytes_per_10_ms_; |
| 71 CHECK_GE(bytes_left, 0); | 71 CHECK_GE(bytes_left, 0); |
| 72 bytes_left -= bytes_per_10_ms_; | 72 bytes_left -= bytes_per_10_ms_; |
| 73 } | 73 } |
| 74 CHECK_LE(bytes_left, 0); | 74 CHECK_LE(bytes_left, 0); |
| 75 // Put the samples that were written to |buffer| but are not used in the | 75 // Put the samples that were written to |buffer| but are not used in the |
| 76 // cache. | 76 // cache. |
| 77 int cache_location = desired_frame_size_bytes_; | 77 size_t cache_location = desired_frame_size_bytes_; |
| 78 int8_t* cache_ptr = &buffer[cache_location]; | 78 int8_t* cache_ptr = &buffer[cache_location]; |
| 79 cached_bytes_ = number_of_requests * bytes_per_10_ms_ - | 79 cached_bytes_ = number_of_requests * bytes_per_10_ms_ - |
| 80 (desired_frame_size_bytes_ - cached_bytes_); | 80 (desired_frame_size_bytes_ - cached_bytes_); |
| 81 // If cached_bytes_ is larger than the cache buffer, uninitialized memory | 81 // If cached_bytes_ is larger than the cache buffer, uninitialized memory |
| 82 // will be read. | 82 // will be read. |
| 83 CHECK_LE(cached_bytes_, bytes_per_10_ms_); | 83 CHECK_LE(cached_bytes_, bytes_per_10_ms_); |
| 84 CHECK_EQ(-bytes_left, cached_bytes_); | 84 CHECK_EQ(static_cast<size_t>(-bytes_left), cached_bytes_); |
| 85 cached_buffer_start_ = 0; | 85 cached_buffer_start_ = 0; |
| 86 memcpy(cache_buffer_.get(), cache_ptr, cached_bytes_); | 86 memcpy(cache_buffer_.get(), cache_ptr, cached_bytes_); |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace webrtc | 89 } // namespace webrtc |
| OLD | NEW |