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 10 matching lines...) Expand all Loading... | |
21 namespace webrtc { | 21 namespace webrtc { |
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 buffer_10_ms_(new int8_t[bytes_per_10_ms_]) { |
32 playout_cached_bytes_(0) { | 32 LOG(INFO) << "desired_frame_size_bytes:" << desired_frame_size_bytes; |
33 playout_cache_buffer_.reset(new int8_t[bytes_per_10_ms_]); | |
34 } | 33 } |
35 | 34 |
36 FineAudioBuffer::~FineAudioBuffer() {} | 35 FineAudioBuffer::~FineAudioBuffer() {} |
37 | 36 |
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() { | 37 void FineAudioBuffer::ResetPlayout() { |
46 playout_cached_buffer_start_ = 0; | 38 playout_buffer_.Clear(); |
47 playout_cached_bytes_ = 0; | |
48 memset(playout_cache_buffer_.get(), 0, bytes_per_10_ms_); | |
49 } | 39 } |
50 | 40 |
51 void FineAudioBuffer::ResetRecord() { | 41 void FineAudioBuffer::ResetRecord() { |
52 record_buffer_.Clear(); | 42 record_buffer_.Clear(); |
53 } | 43 } |
54 | 44 |
55 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { | 45 void FineAudioBuffer::GetPlayoutData(int8_t* buffer) { |
56 if (desired_frame_size_bytes_ <= playout_cached_bytes_) { | 46 const size_t num_bytes = desired_frame_size_bytes_; |
57 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], | 47 // Ask WebRTC for new data in chunks of 10ms until we have enough to |
58 desired_frame_size_bytes_); | 48 // fulfill the request. It is possible that the buffer already contains |
59 playout_cached_buffer_start_ += desired_frame_size_bytes_; | 49 // enough samples from the last round. |
60 playout_cached_bytes_ -= desired_frame_size_bytes_; | 50 while (playout_buffer_.size() < num_bytes) { |
61 RTC_CHECK_LT(playout_cached_buffer_start_ + playout_cached_bytes_, | 51 // Get 10ms decoded audio from WebRTC. |
62 bytes_per_10_ms_); | 52 device_buffer_->RequestPlayoutData(samples_per_10_ms_); |
63 return; | 53 int samples_per_channel = |
kwiberg-webrtc
2017/02/27 09:45:35
const?
henrika_webrtc
2017/02/27 12:32:27
Done.
| |
54 device_buffer_->GetPlayoutData(buffer_10_ms_.get()); | |
55 RTC_DCHECK_EQ(samples_per_channel, samples_per_10_ms_); | |
56 // Append PCM audio samples to buffer. | |
57 playout_buffer_.AppendData(buffer_10_ms_.get(), bytes_per_10_ms_); | |
kwiberg-webrtc
2017/02/27 09:45:35
Hmm. The only use of buffer_10_ms_ is as temp stor
henrika_webrtc
2017/02/27 12:32:27
Correct. Only used as temp storage. Will try to im
| |
64 } | 58 } |
65 memcpy(buffer, &playout_cache_buffer_.get()[playout_cached_buffer_start_], | 59 // Provide the requested number of bytes to the consumer. |
66 playout_cached_bytes_); | 60 memcpy(buffer, playout_buffer_.data(), num_bytes); |
67 // Push another n*10ms of audio to |buffer|. n > 1 if | 61 // Move remaining samples to start of buffer to prepare for next round. |
68 // |desired_frame_size_bytes_| is greater than 10ms of audio. Note that we | 62 memmove(playout_buffer_.data(), playout_buffer_.data() + num_bytes, |
69 // write the audio after the cached bytes copied earlier. | 63 playout_buffer_.size() - num_bytes); |
70 int8_t* unwritten_buffer = &buffer[playout_cached_bytes_]; | 64 playout_buffer_.SetSize(playout_buffer_.size() - num_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_) { | |
79 RTC_CHECK_EQ(num_out, 0); | |
80 playout_cached_bytes_ = 0; | |
81 return; | |
82 } | |
83 unwritten_buffer += bytes_per_10_ms_; | |
84 RTC_CHECK_GE(bytes_left, 0); | |
85 bytes_left -= static_cast<int>(bytes_per_10_ms_); | |
86 } | |
87 RTC_CHECK_LE(bytes_left, 0); | |
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. | |
96 RTC_CHECK_LE(playout_cached_bytes_, bytes_per_10_ms_); | |
97 RTC_CHECK_EQ(-bytes_left, playout_cached_bytes_); | |
98 playout_cached_buffer_start_ = 0; | |
99 memcpy(playout_cache_buffer_.get(), cache_ptr, playout_cached_bytes_); | |
100 } | 65 } |
101 | 66 |
102 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, | 67 void FineAudioBuffer::DeliverRecordedData(const int8_t* buffer, |
103 size_t size_in_bytes, | 68 size_t size_in_bytes, |
104 int playout_delay_ms, | 69 int playout_delay_ms, |
105 int record_delay_ms) { | 70 int record_delay_ms) { |
106 // Always append new data and grow the buffer if needed. | 71 // Always append new data and grow the buffer if needed. |
107 record_buffer_.AppendData(buffer, size_in_bytes); | 72 record_buffer_.AppendData(buffer, size_in_bytes); |
108 // Consume samples from buffer in chunks of 10ms until there is not | 73 // Consume samples from buffer in chunks of 10ms until there is not |
109 // enough data left. The number of remaining bytes in the cache is given by | 74 // enough data left. The number of remaining bytes in the cache is given by |
110 // the new size of the buffer. | 75 // the new size of the buffer. |
111 while (record_buffer_.size() >= bytes_per_10_ms_) { | 76 while (record_buffer_.size() >= bytes_per_10_ms_) { |
112 device_buffer_->SetRecordedBuffer(record_buffer_.data(), | 77 device_buffer_->SetRecordedBuffer(record_buffer_.data(), |
113 samples_per_10_ms_); | 78 samples_per_10_ms_); |
114 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); | 79 device_buffer_->SetVQEData(playout_delay_ms, record_delay_ms, 0); |
115 device_buffer_->DeliverRecordedData(); | 80 device_buffer_->DeliverRecordedData(); |
116 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, | 81 memmove(record_buffer_.data(), record_buffer_.data() + bytes_per_10_ms_, |
117 record_buffer_.size() - bytes_per_10_ms_); | 82 record_buffer_.size() - bytes_per_10_ms_); |
118 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); | 83 record_buffer_.SetSize(record_buffer_.size() - bytes_per_10_ms_); |
119 } | 84 } |
120 } | 85 } |
121 | 86 |
122 } // namespace webrtc | 87 } // namespace webrtc |
OLD | NEW |