| 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 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 record_write_pos_ = 0; | 63 record_write_pos_ = 0; |
| 64 memset(record_cache_buffer_.get(), 0, required_record_buffer_size_bytes_); | 64 memset(record_cache_buffer_.get(), 0, required_record_buffer_size_bytes_); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { | 67 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { |
| 68 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { | 68 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { |
| 69 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], | 69 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
| 70 desired_frame_size_bytes_); | 70 desired_frame_size_bytes_); |
| 71 playout_cached_buffer_start_ += desired_frame_size_bytes_; | 71 playout_cached_buffer_start_ += desired_frame_size_bytes_; |
| 72 playout_cached_bytes_ -= desired_frame_size_bytes_; | 72 playout_cached_bytes_ -= desired_frame_size_bytes_; |
| 73 CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, | 73 RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, |
| 74 bytes_per_10_ms_); | 74 bytes_per_10_ms_); |
| 75 return; | 75 return; |
| 76 } | 76 } |
| 77 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], | 77 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
| 78 playout_cached_bytes_); | 78 playout_cached_bytes_); |
| 79 // Push another n*10ms of audio to |buffer|. n > 1 if | 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 | 80 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we |
| 81 // write the audio after the cached bytes copied earlier. | 81 // write the audio after the cached bytes copied earlier. |
| 82 int8_t* unwritten_buffer = &buffer[playout_cached_bytes_]; | 82 int8_t* unwritten_buffer = &buffer[playout_cached_bytes_]; |
| 83 int bytes_left = | 83 int bytes_left = |
| 84 static_cast<int>(desired_frame_size_bytes_ - playout_cached_bytes_); | 84 static_cast<int>(desired_frame_size_bytes_ - playout_cached_bytes_); |
| 85 // Ceiling of integer division: 1 + ((x - 1) / y) | 85 // Ceiling of integer division: 1 + ((x - 1) / y) |
| 86 size_t number_of_requests = 1 + (bytes_left - 1) / (bytes_per_10_ms_); | 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) { | 87 for (size_t i = 0; i < number_of_requests; ++i) { |
| 88 device_buffer_->RequestPlayoutData(samples_per_10_ms_); | 88 device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
| 89 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); | 89 int num_out = device_buffer_->GetPlayoutData(unwritten_buffer); |
| 90 if (static_cast<size_t>(num_out) != samples_per_10_ms_) { | 90 if (static_cast<size_t>(num_out) != samples_per_10_ms_) { |
| 91 CHECK_EQ(num_out, 0); | 91 RTC_CHECK_EQ(num_out, 0); |
| 92 playout_cached_bytes_ = 0; | 92 playout_cached_bytes_ = 0; |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 unwritten_buffer += bytes_per_10_ms_; | 95 unwritten_buffer += bytes_per_10_ms_; |
| 96 CHECK_GE(bytes_left, 0); | 96 RTC_CHECK_GE(bytes_left, 0); |
| 97 bytes_left -= static_cast<int>(bytes_per_10_ms_); | 97 bytes_left -= static_cast<int>(bytes_per_10_ms_); |
| 98 } | 98 } |
| 99 CHECK_LE(bytes_left, 0); | 99 RTC_CHECK_LE(bytes_left, 0); |
| 100 // Put the samples that were written to |buffer| but are not used in the | 100 // Put the samples that were written to |buffer| but are not used in the |
| 101 // cache. | 101 // cache. |
| 102 size_t cache_location = desired_frame_size_bytes_; | 102 size_t cache_location = desired_frame_size_bytes_; |
| 103 int8_t* cache_ptr = &buffer[cache_location]; | 103 int8_t* cache_ptr = &buffer[cache_location]; |
| 104 playout_cached_bytes_ = number_of_requests * bytes_per_10_ms_ - | 104 playout_cached_bytes_ = number_of_requests * bytes_per_10_ms_ - |
| 105 (desired_frame_size_bytes_ - playout_cached_bytes_); | 105 (desired_frame_size_bytes_ - playout_cached_bytes_); |
| 106 // If playout_cached_bytes_ is larger than the cache buffer, uninitialized | 106 // If playout_cached_bytes_ is larger than the cache buffer, uninitialized |
| 107 // memory will be read. | 107 // memory will be read. |
| 108 CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); | 108 RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); |
| 109 CHECK_EQ(static_cast<size_t>(-bytes_left), playout_cached_bytes_); | 109 RTC_CHECK_EQ(static_cast<size_t>(-bytes_left), playout_cached_bytes_); |
| 110 playout_cached_buffer_start_ = 0; | 110 playout_cached_buffer_start_ = 0; |
| 111 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); | 111 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, | 114 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, |
| 115 size_t size_in_bytes, | 115 size_t size_in_bytes, |
| 116 int playout_delay_ms, | 116 int playout_delay_ms, |
| 117 int record_delay_ms) { | 117 int record_delay_ms) { |
| 118 CHECK_EQ(size_in_bytes, desired_frame_size_bytes_); | 118 RTC_CHECK_EQ(size_in_bytes, desired_frame_size_bytes_); |
| 119 // Check if the temporary buffer can store the incoming buffer. If not, | 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 | 120 // move the remaining (old) bytes to the beginning of the temporary buffer |
| 121 // and start adding new samples after the old samples. | 121 // and start adding new samples after the old samples. |
| 122 if (record_write_pos_ + size_in_bytes > required_record_buffer_size_bytes_) { | 122 if (record_write_pos_ + size_in_bytes > required_record_buffer_size_bytes_) { |
| 123 if (record_cached_bytes_ > 0) { | 123 if (record_cached_bytes_ > 0) { |
| 124 memmove(record_cache_buffer_.get(), | 124 memmove(record_cache_buffer_.get(), |
| 125 record_cache_buffer_.get() + record_read_pos_, | 125 record_cache_buffer_.get() + record_read_pos_, |
| 126 record_cached_bytes_); | 126 record_cached_bytes_); |
| 127 } | 127 } |
| 128 record_write_pos_ = record_cached_bytes_; | 128 record_write_pos_ = record_cached_bytes_; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 141 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); | 141 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
| 142 device_buffer_->DeliverRecordedData(); | 142 device_buffer_->DeliverRecordedData(); |
| 143 // Read next chunk of 10ms data. | 143 // Read next chunk of 10ms data. |
| 144 record_read_pos_ += bytes_per_10_ms_; | 144 record_read_pos_ += bytes_per_10_ms_; |
| 145 // Reduce number of cached bytes with the consumed amount. | 145 // Reduce number of cached bytes with the consumed amount. |
| 146 record_cached_bytes_ -= bytes_per_10_ms_; | 146 record_cached_bytes_ -= bytes_per_10_ms_; |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 } // namespace webrtc | 150 } // namespace webrtc |
| OLD | NEW |