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 11 matching lines...) Expand all Loading... | |
22 | 22 |
23 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, | 23 FineAudioBuffer::FineAudioBuffer(AudioDeviceBuffer* device_buffer, |
24 size_t desired_frame_size_bytes, | 24 size_t desired_frame_size_bytes, |
25 int sample_rate) | 25 int sample_rate) |
26 : device_buffer_(device_buffer), | 26 : device_buffer_(device_buffer), |
27 desired_frame_size_bytes_(desired_frame_size_bytes), | 27 desired_frame_size_bytes_(desired_frame_size_bytes), |
28 sample_rate_(sample_rate), | 28 sample_rate_(sample_rate), |
29 samples_per_10_ms_(static_cast<size_t>(sample_rate_ * 10 / 1000)), | 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)), | 30 bytes_per_10_ms_(samples_per_10_ms_ * sizeof(int16_t)), |
31 playout_cached_buffer_start_(0), | 31 playout_cached_buffer_start_(0), |
32 playout_cached_bytes_(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_]); | 33 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 } | 34 } |
44 | 35 |
45 FineAudioBuffer::~FineAudioBuffer() {} | 36 FineAudioBuffer::~FineAudioBuffer() {} |
46 | 37 |
47 size_t FineAudioBuffer::RequiredPlayoutBufferSizeBytes() { | 38 size_t FineAudioBuffer::RequiredPlayoutBufferSizeBytes() { |
48 // 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 |
49 // 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 |
50 // 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. |
51 return desired_frame_size_bytes_ + bytes_per_10_ms_; | 42 return desired_frame_size_bytes_ + bytes_per_10_ms_; |
52 } | 43 } |
53 | 44 |
54 void FineAudioBuffer::ResetPlayout() { | 45 void FineAudioBuffer::ResetPlayout() { |
55 playout_cached_buffer_start_ = 0; | 46 playout_cached_buffer_start_ = 0; |
56 playout_cached_bytes_ = 0; | 47 playout_cached_bytes_ = 0; |
57 memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_); | 48 memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_); |
58 } | 49 } |
59 | 50 |
60 void FineAudioBuffer::ResetRecord() { | 51 void FineAudioBuffer::ResetRecord() { |
61 record_cached_bytes_ = 0; | 52 LOG(INFO) << "ResetRecord"; |
62 record_read_pos_ = 0; | 53 record_buffer_.Clear(); |
63 record_write_pos_ = 0; | |
64 memset(record_cache_buffer_.get(), 0, required_record_buffer_size_bytes_); | |
65 } | 54 } |
66 | 55 |
67 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { | 56 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { |
68 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { | 57 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { |
69 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], | 58 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], |
70 desired_frame_size_bytes_); | 59 desired_frame_size_bytes_); |
71 playout_cached_buffer_start_ += desired_frame_size_bytes_; | 60 playout_cached_buffer_start_ += desired_frame_size_bytes_; |
72 playout_cached_bytes_ -= desired_frame_size_bytes_; | 61 playout_cached_bytes_ -= desired_frame_size_bytes_; |
73 RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, | 62 RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, |
74 bytes_per_10_ms_); | 63 bytes_per_10_ms_); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); | 97 RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); |
109 RTC_CHECK_EQ(-bytes_left, playout_cached_bytes_); | 98 RTC_CHECK_EQ(-bytes_left, playout_cached_bytes_); |
110 playout_cached_buffer_start_ = 0; | 99 playout_cached_buffer_start_ = 0; |
111 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); | 100 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); |
112 } | 101 } |
113 | 102 |
114 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, | 103 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, |
115 size_t size_in_bytes, | 104 size_t size_in_bytes, |
116 int playout_delay_ms, | 105 int playout_delay_ms, |
117 int record_delay_ms) { | 106 int record_delay_ms) { |
118 // Check if the temporary buffer can store the incoming buffer. If not, | 107 // Always append new data and grow the buffer if needed. |
119 // move the remaining (old) bytes to the beginning of the temporary buffer | 108 record_buffer_.AppendData(buffer, size_in_bytes); |
120 // and start adding new samples after the old samples. | 109 // Consume samples from buffer in chunks of 10ms until there is not |
121 if (record_write_pos_ + size_in_bytes > required_record_buffer_size_bytes_) { | |
122 if (record_cached_bytes_ > 0) { | |
123 memmove(record_cache_buffer_.get(), | |
124 record_cache_buffer_.get() + record_read_pos_, | |
125 record_cached_bytes_); | |
126 } | |
127 record_write_pos_ = record_cached_bytes_; | |
128 record_read_pos_ = 0; | |
129 } | |
130 // Add recorded samples to a temporary buffer. | |
131 memcpy(record_cache_buffer_.get() + record_write_pos_, buffer, size_in_bytes); | |
132 record_write_pos_ += size_in_bytes; | |
133 record_cached_bytes_ += size_in_bytes; | |
134 // Consume samples in temporary buffer in chunks of 10ms until there is not | |
135 // enough data left. The number of remaining bytes in the cache is given by | 110 // enough data left. The number of remaining bytes in the cache is given by |
136 // |record_cached_bytes_| after this while loop is done. | 111 // the new size of the buffer. |
137 while (record_cached_bytes_ >= bytes_per_10_ms_) { | 112 while (record_buffer_.size() >= bytes_per_10_ms_) { |
138 device_buffer_->SetRecordedBuffer( | 113 RTC_DCHECK_LE(bytes_per_10_ms_, record_buffer_.size()); |
kwiberg-webrtc
2017/02/23 08:08:24
You just checked this on the line before. :-)
henrika_webrtc
2017/02/23 08:39:11
Man ;-) So, I guess we are fine without a CHECK wi
| |
139 record_cache_buffer_.get() + record_read_pos_, samples_per_10_ms_); | 114 device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
115 samples_per_10_ms_); | |
140 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); | 116 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
141 device_buffer_->DeliverRecordedData(); | 117 device_buffer_->DeliverRecordedData(); |
142 // Read next chunk of 10ms data. | 118 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, |
143 record_read_pos_ += bytes_per_10_ms_; | 119 record_buffer_.size() - bytes_per_10_ms_); |
144 // Reduce number of cached bytes with the consumed amount. | 120 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); |
145 record_cached_bytes_ -= bytes_per_10_ms_; | |
146 } | 121 } |
kwiberg-webrtc
2017/02/23 08:08:24
Hmm. You repeatedly take a bite from the front, th
henrika_webrtc
2017/02/23 08:39:11
Thanks but a double-loop is rare. I have not been
| |
147 } | 122 } |
148 | 123 |
149 } // namespace webrtc | 124 } // namespace webrtc |
OLD | NEW |