OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/audio_renderer_algorithm_base.h" | 5 #include "media/filters/audio_renderer_algorithm_base.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 // Audio at these speeds would sound better under a frequency domain algorithm. | 34 // Audio at these speeds would sound better under a frequency domain algorithm. |
35 static const float kMinPlaybackRate = 0.5f; | 35 static const float kMinPlaybackRate = 0.5f; |
36 static const float kMaxPlaybackRate = 4.0f; | 36 static const float kMaxPlaybackRate = 4.0f; |
37 | 37 |
38 AudioRendererAlgorithmBase::AudioRendererAlgorithmBase() | 38 AudioRendererAlgorithmBase::AudioRendererAlgorithmBase() |
39 : channels_(0), | 39 : channels_(0), |
40 samples_per_second_(0), | 40 samples_per_second_(0), |
41 bytes_per_channel_(0), | 41 bytes_per_channel_(0), |
42 playback_rate_(0.0f), | 42 playback_rate_(0.0f), |
43 audio_buffer_(0, kStartingBufferSizeInBytes), | 43 audio_buffer_(0, kStartingBufferSizeInBytes), |
44 crossfade_size_(0), | 44 bytes_in_crossfade_(0), |
45 bytes_per_frame_(0), | |
46 index_into_window_(0), | |
47 crossfade_frame_number_(0), | |
48 muted_(false), | |
49 needs_more_data_(false), | |
45 window_size_(0) { | 50 window_size_(0) { |
46 } | 51 } |
47 | 52 |
48 AudioRendererAlgorithmBase::~AudioRendererAlgorithmBase() {} | 53 AudioRendererAlgorithmBase::~AudioRendererAlgorithmBase() {} |
49 | 54 |
50 bool AudioRendererAlgorithmBase::ValidateConfig( | 55 bool AudioRendererAlgorithmBase::ValidateConfig( |
51 int channels, | 56 int channels, |
52 int samples_per_second, | 57 int samples_per_second, |
53 int bits_per_channel) { | 58 int bits_per_channel) { |
54 bool status = true; | 59 bool status = true; |
(...skipping 22 matching lines...) Expand all Loading... | |
77 int samples_per_second, | 82 int samples_per_second, |
78 int bits_per_channel, | 83 int bits_per_channel, |
79 float initial_playback_rate, | 84 float initial_playback_rate, |
80 const base::Closure& callback) { | 85 const base::Closure& callback) { |
81 DCHECK(!callback.is_null()); | 86 DCHECK(!callback.is_null()); |
82 DCHECK(ValidateConfig(channels, samples_per_second, bits_per_channel)); | 87 DCHECK(ValidateConfig(channels, samples_per_second, bits_per_channel)); |
83 | 88 |
84 channels_ = channels; | 89 channels_ = channels; |
85 samples_per_second_ = samples_per_second; | 90 samples_per_second_ = samples_per_second; |
86 bytes_per_channel_ = bits_per_channel / 8; | 91 bytes_per_channel_ = bits_per_channel / 8; |
92 bytes_per_frame_ = bytes_per_channel_ * channels_; | |
87 request_read_cb_ = callback; | 93 request_read_cb_ = callback; |
88 SetPlaybackRate(initial_playback_rate); | 94 SetPlaybackRate(initial_playback_rate); |
89 | 95 |
90 window_size_ = | 96 window_size_ = |
91 samples_per_second_ * bytes_per_channel_ * channels_ * kWindowDuration; | 97 samples_per_second_ * bytes_per_channel_ * channels_ * kWindowDuration; |
92 AlignToSampleBoundary(&window_size_); | 98 AlignToFrameBoundary(&window_size_); |
93 | 99 |
94 crossfade_size_ = | 100 bytes_in_crossfade_ = |
95 samples_per_second_ * bytes_per_channel_ * channels_ * kCrossfadeDuration; | 101 samples_per_second_ * bytes_per_channel_ * channels_ * kCrossfadeDuration; |
96 AlignToSampleBoundary(&crossfade_size_); | 102 AlignToFrameBoundary(&bytes_in_crossfade_); |
97 } | 103 |
98 | 104 crossfade_buffer_.reset(new uint8[bytes_in_crossfade_]); |
99 uint32 AudioRendererAlgorithmBase::FillBuffer(uint8* dest, uint32 length) { | 105 } |
100 if (IsQueueEmpty() || playback_rate_ == 0.0f) | 106 |
107 uint32 AudioRendererAlgorithmBase::FillBuffer( | |
108 uint8* dest, uint32 requested_frames) { | |
109 DCHECK_NE(bytes_per_frame_, 0u); | |
110 | |
111 if (playback_rate_ == 0.0f) | |
101 return 0; | 112 return 0; |
102 | 113 |
103 // Handle the simple case of normal playback. | 114 uint32 total_frames_rendered = 0; |
104 if (playback_rate_ == 1.0f) { | |
105 uint32 bytes_written = | |
106 CopyFromAudioBuffer(dest, std::min(length, bytes_buffered())); | |
107 AdvanceBufferPosition(bytes_written); | |
108 return bytes_written; | |
109 } | |
110 | |
111 // Output muted data when out of acceptable quality range. | |
112 if (playback_rate_ < kMinPlaybackRate || playback_rate_ > kMaxPlaybackRate) | |
113 return MuteBuffer(dest, length); | |
114 | |
115 uint32 input_step = window_size_; | |
116 uint32 output_step = window_size_; | |
117 | |
118 if (playback_rate_ > 1.0f) { | |
119 // Playback is faster than normal; need to squish output! | |
120 output_step = ceil(window_size_ / playback_rate_); | |
121 } else { | |
122 // Playback is slower than normal; need to stretch input! | |
123 input_step = ceil(window_size_ * playback_rate_); | |
124 } | |
125 | |
126 AlignToSampleBoundary(&input_step); | |
127 AlignToSampleBoundary(&output_step); | |
128 DCHECK_LE(crossfade_size_, input_step); | |
129 DCHECK_LE(crossfade_size_, output_step); | |
130 | |
131 uint32 bytes_written = 0; | |
132 uint32 bytes_left_to_output = length; | |
133 uint8* output_ptr = dest; | 115 uint8* output_ptr = dest; |
134 | 116 while (total_frames_rendered < requested_frames) { |
135 // TODO(vrk): The while loop and if test below are lame! We are requiring the | 117 if (index_into_window_ == window_size_) |
136 // client to provide us with enough data to output only complete crossfaded | 118 ResetWindow(); |
137 // windows. Instead, we should output as much data as we can, and add state to | 119 |
138 // keep track of what point in the crossfade we are at. | 120 bool rendered_frame = true; |
139 // This is also the cause of crbug.com/108239. | 121 if (playback_rate_ > 1.0) |
140 while (bytes_left_to_output >= output_step) { | 122 rendered_frame = OutputFasterPlayback(output_ptr); |
141 // If there is not enough data buffered to complete an iteration of the | 123 else if (playback_rate_ < 1.0) |
142 // loop, mute the remaining and break. | 124 rendered_frame = OutputSlowerPlayback(output_ptr); |
143 if (bytes_buffered() < window_size_) { | 125 else |
144 bytes_written += MuteBuffer(output_ptr, bytes_left_to_output); | 126 rendered_frame = OutputNormalPlayback(output_ptr); |
127 | |
128 if (!rendered_frame) { | |
129 needs_more_data_ = true; | |
145 break; | 130 break; |
146 } | 131 } |
147 | 132 |
148 // Copy |output_step| bytes into destination buffer. | 133 output_ptr += bytes_per_frame_; |
149 uint32 copied = CopyFromAudioBuffer(output_ptr, output_step); | 134 total_frames_rendered++; |
150 DCHECK_EQ(copied, output_step); | 135 } |
151 output_ptr += output_step; | 136 return total_frames_rendered; |
152 bytes_written += copied; | 137 } |
153 bytes_left_to_output -= copied; | 138 |
154 | 139 void AudioRendererAlgorithmBase::ResetWindow() { |
155 // Copy the |crossfade_size_| bytes leading up to the next window that will | 140 DCHECK_LE(index_into_window_, window_size_); |
156 // be played into an intermediate buffer. This will be used to crossfade | 141 index_into_window_ = 0; |
157 // from the current window to the next. | 142 crossfade_frame_number_ = 0; |
158 AdvanceBufferPosition(input_step - crossfade_size_); | 143 muted_ = false; |
159 scoped_array<uint8> next_window_intro(new uint8[crossfade_size_]); | 144 } |
160 uint32 bytes_copied = | 145 |
161 CopyFromAudioBuffer(next_window_intro.get(), crossfade_size_); | 146 bool AudioRendererAlgorithmBase::OutputFasterPlayback(uint8* dest) { |
162 DCHECK_EQ(bytes_copied, crossfade_size_); | 147 DCHECK_LT(index_into_window_, window_size_); |
163 AdvanceBufferPosition(crossfade_size_); | 148 DCHECK_GT(playback_rate_, 1.0); |
164 | 149 |
165 // Prepare pointers to end of the current window and the start of the next | 150 if (audio_buffer_.forward_bytes() < bytes_per_frame_) |
166 // window. | 151 return false; |
167 uint8* start_of_outro = output_ptr - crossfade_size_; | 152 |
168 const uint8* start_of_intro = next_window_intro.get(); | 153 if (playback_rate_ > kMaxPlaybackRate) |
169 | 154 muted_ = true; |
170 // Do crossfade! | 155 |
171 Crossfade(crossfade_size_, channels_, bytes_per_channel_, | 156 // The audio data is output in a series of windows. For sped-up playback, |
172 start_of_intro, start_of_outro); | 157 // the window is comprised of the following phases: |
173 } | 158 // |
174 | 159 // a) Output raw data. |
175 return bytes_written; | 160 // b) Save bytes for crossfade in |crossfade_buffer_|. |
176 } | 161 // c) Drop data. |
177 | 162 // d) Output crossfaded audio leading up to the next window. |
178 uint32 AudioRendererAlgorithmBase::MuteBuffer(uint8* dest, uint32 length) { | 163 // |
164 // The duration of each phase is computed below based on the |window_size_| | |
165 // and |playback_rate_|. | |
166 uint32 input_step = window_size_; | |
167 uint32 output_step = ceil(window_size_ / playback_rate_); | |
168 AlignToFrameBoundary(&output_step); | |
169 DCHECK_GT(input_step, output_step); | |
170 | |
171 uint32 bytes_to_crossfade = bytes_in_crossfade_; | |
172 if (muted_ || bytes_to_crossfade > output_step) | |
173 bytes_to_crossfade = 0; | |
174 | |
175 // This is the index of the end of phase a, beginning of phase b. | |
176 uint32 outtro_crossfade_begin = output_step - bytes_to_crossfade; | |
177 | |
178 // This is the index of the end of phase b, beginning of phase c. | |
179 uint32 outtro_crossfade_end = output_step; | |
180 | |
181 // This is the index of the end of phase c, beginning of phase d. | |
182 // This phase continues until |index_into_window_| reaches |window_size_|, at | |
183 // which point the window restarts. | |
184 uint32 intro_crossfade_begin = input_step - bytes_to_crossfade; | |
185 | |
186 // a) Output a raw frame if we haven't reached the crossfade section. | |
187 if (index_into_window_ < outtro_crossfade_begin) { | |
188 CopyWithAdvance(dest); | |
189 index_into_window_ += bytes_per_frame_; | |
190 return true; | |
191 } | |
192 | |
193 // b) Save outtro crossfade frames into intermediate buffer, but do not output | |
194 // anything to |dest|. | |
195 while (index_into_window_ < outtro_crossfade_end) { | |
196 if (audio_buffer_.forward_bytes() < bytes_per_frame_) | |
197 return false; | |
198 | |
199 // This phase only applies if there are bytes to crossfade. | |
200 DCHECK_GT(bytes_to_crossfade, 0u); | |
201 uint8* place_to_copy = crossfade_buffer_.get() + | |
202 (index_into_window_ - outtro_crossfade_begin); | |
203 CopyWithAdvance(place_to_copy); | |
204 index_into_window_ += bytes_per_frame_; | |
205 } | |
206 | |
207 // c) Drop frames until we reach the intro crossfade section. | |
208 while (index_into_window_ < intro_crossfade_begin) { | |
209 if (audio_buffer_.forward_bytes() < bytes_per_frame_) | |
210 return false; | |
211 | |
212 DropFrame(); | |
213 index_into_window_ += bytes_per_frame_; | |
214 } | |
215 | |
216 // Return if we have run out of data after Phase c). | |
217 if (audio_buffer_.forward_bytes() < bytes_per_frame_) | |
218 return false; | |
219 | |
220 // Phase d) doesn't apply if there are no bytes to crossfade. | |
221 if (bytes_to_crossfade == 0) { | |
222 DCHECK_EQ(index_into_window_, window_size_); | |
223 return false; | |
224 } | |
225 | |
226 // d) Crossfade and output a frame. | |
227 DCHECK_LT(index_into_window_, window_size_); | |
228 uint32 offset_into_buffer = index_into_window_ - intro_crossfade_begin; | |
229 memcpy(dest, crossfade_buffer_.get() + offset_into_buffer, | |
230 bytes_per_frame_); | |
231 scoped_array<uint8> intro_frame_ptr(new uint8[bytes_per_frame_]); | |
232 audio_buffer_.Read(intro_frame_ptr.get(), bytes_per_frame_); | |
233 OutputCrossfadedFrame(dest, intro_frame_ptr.get()); | |
234 index_into_window_ += bytes_per_frame_; | |
235 return true; | |
236 } | |
237 | |
238 bool AudioRendererAlgorithmBase::OutputSlowerPlayback(uint8* dest) { | |
239 DCHECK_LT(index_into_window_, window_size_); | |
240 DCHECK_LT(playback_rate_, 1.0); | |
179 DCHECK_NE(playback_rate_, 0.0); | 241 DCHECK_NE(playback_rate_, 0.0); |
180 // Note: This may not play at the speed requested as we can only consume as | 242 |
181 // much data as we have, and audio timestamps drive the pipeline clock. | 243 if (audio_buffer_.forward_bytes() < bytes_per_frame_) |
182 // | 244 return false; |
183 // Furthermore, we won't end up scaling the very last bit of audio, but | 245 |
184 // we're talking about <8ms of audio data. | 246 if (playback_rate_ < kMinPlaybackRate) |
185 | 247 muted_ = true; |
186 // Cap the |input_step| by the amount of bytes buffered. | 248 |
187 uint32 input_step = | 249 // The audio data is output in a series of windows. For slowed down playback, |
188 std::min(static_cast<uint32>(length * playback_rate_), bytes_buffered()); | 250 // the window is comprised of the following phases: |
189 uint32 output_step = input_step / playback_rate_; | 251 // |
190 AlignToSampleBoundary(&input_step); | 252 // a) Output raw data. |
191 AlignToSampleBoundary(&output_step); | 253 // b) Output and save bytes for crossfade in |crossfade_buffer_|. |
192 | 254 // c) Output* raw data. |
193 DCHECK_LE(output_step, length); | 255 // d) Output* crossfaded audio leading up to the next window. |
194 if (output_step > length) { | 256 // |
195 LOG(ERROR) << "OLA: output_step (" << output_step << ") calculated to " | 257 // * Phases c) and d) do not progress |audio_buffer_|'s cursor so that the |
196 << "be larger than destination length (" << length << ")"; | 258 // |audio_buffer_|'s cursor is in the correct place for the next window. |
197 output_step = length; | 259 // |
198 } | 260 // The duration of each phase is computed below based on the |window_size_| |
199 | 261 // and |playback_rate_|. |
200 memset(dest, 0, output_step); | 262 uint32 input_step = ceil(window_size_ * playback_rate_); |
201 AdvanceBufferPosition(input_step); | 263 AlignToFrameBoundary(&input_step); |
202 return output_step; | 264 uint32 output_step = window_size_; |
265 DCHECK_LT(input_step, output_step); | |
266 | |
267 uint32 bytes_to_crossfade = bytes_in_crossfade_; | |
268 if (muted_ || bytes_to_crossfade > input_step) | |
269 bytes_to_crossfade = 0; | |
270 | |
271 // This is the index of the end of phase a, beginning of phase b. | |
272 uint32 intro_crossfade_begin = input_step - bytes_to_crossfade; | |
273 | |
274 // This is the index of the end of phase b, beginning of phase c. | |
275 uint32 intro_crossfade_end = input_step; | |
276 | |
277 // This is the index of the end of phase c, beginning of phase d. | |
278 // This phase continues until |index_into_window_| reaches |window_size_|, at | |
279 // which point the window restarts. | |
280 uint32 outtro_crossfade_begin = output_step - bytes_to_crossfade; | |
281 | |
282 // a) Output a raw frame. | |
283 if (index_into_window_ < intro_crossfade_begin) { | |
284 CopyWithAdvance(dest); | |
285 index_into_window_ += bytes_per_frame_; | |
286 return true; | |
287 } | |
288 | |
289 // b) Save the raw frame for the intro crossfade section, then output the | |
290 // frame to |dest|. | |
291 if (index_into_window_ < intro_crossfade_end) { | |
292 uint32 offset = index_into_window_ - intro_crossfade_begin; | |
293 uint8* place_to_copy = crossfade_buffer_.get() + offset; | |
294 CopyWithoutAdvance(place_to_copy); | |
295 CopyWithAdvance(dest); | |
296 index_into_window_ += bytes_per_frame_; | |
297 return true; | |
298 } | |
299 | |
300 uint32 audio_buffer_offset = index_into_window_ - intro_crossfade_end; | |
301 | |
302 if (audio_buffer_.forward_bytes() < audio_buffer_offset + bytes_per_frame_) | |
303 return false; | |
304 | |
305 // c) Output a raw frame into |dest| without advancing the |audio_buffer_| | |
306 // cursor. See function-level comment. | |
307 DCHECK_GE(index_into_window_, intro_crossfade_end); | |
308 CopyWithoutAdvance(dest, audio_buffer_offset); | |
309 | |
310 // d) Crossfade the next frame of |crossfade_buffer_| into |dest| if we've | |
311 // reached the outtro crossfade section of the window. | |
312 if (index_into_window_ >= outtro_crossfade_begin) { | |
313 uint32 offset_into_crossfade_buffer = | |
314 index_into_window_ - outtro_crossfade_begin; | |
315 uint8* intro_frame_ptr = | |
316 crossfade_buffer_.get() + offset_into_crossfade_buffer; | |
317 OutputCrossfadedFrame(dest, intro_frame_ptr); | |
318 } | |
319 | |
320 index_into_window_ += bytes_per_frame_; | |
321 return true; | |
322 } | |
323 | |
324 bool AudioRendererAlgorithmBase::OutputNormalPlayback(uint8* dest) { | |
325 if (audio_buffer_.forward_bytes() >= bytes_per_frame_) { | |
326 CopyWithAdvance(dest); | |
327 index_into_window_ += bytes_per_frame_; | |
328 return true; | |
329 } | |
330 return false; | |
331 } | |
332 | |
333 void AudioRendererAlgorithmBase::CopyWithAdvance(uint8* dest) { | |
334 CopyWithoutAdvance(dest); | |
335 DropFrame(); | |
336 } | |
337 | |
338 void AudioRendererAlgorithmBase::CopyWithoutAdvance(uint8* dest) { | |
339 CopyWithoutAdvance(dest, 0); | |
340 } | |
341 | |
342 void AudioRendererAlgorithmBase::CopyWithoutAdvance( | |
343 uint8* dest, uint32 offset) { | |
344 if (muted_) { | |
345 memset(dest, 0, bytes_per_frame_); | |
346 return; | |
347 } | |
348 uint32 copied = audio_buffer_.Peek(dest, bytes_per_frame_, offset); | |
349 DCHECK_EQ(bytes_per_frame_, copied); | |
350 } | |
351 | |
352 void AudioRendererAlgorithmBase::DropFrame() { | |
353 audio_buffer_.Seek(bytes_per_frame_); | |
354 | |
355 if (!IsQueueFull()) | |
356 request_read_cb_.Run(); | |
357 } | |
358 | |
359 void AudioRendererAlgorithmBase::OutputCrossfadedFrame( | |
360 uint8* outtro, const uint8* intro) { | |
361 DCHECK_LE(index_into_window_, window_size_); | |
362 DCHECK(!muted_); | |
363 | |
364 switch (bytes_per_channel_) { | |
365 case 4: | |
366 CrossfadeFrame<int32>(outtro, intro); | |
367 break; | |
368 case 2: | |
369 CrossfadeFrame<int16>(outtro, intro); | |
370 break; | |
371 case 1: | |
372 CrossfadeFrame<int8>(outtro, intro); | |
acolwell GONE FROM CHROMIUM
2012/02/27 21:00:08
I think this needs to be uint8 to be consistent w/
vrk (LEFT CHROMIUM)
2012/02/28 23:40:32
Oh man, good catch!! Blah I was only testing with
| |
373 break; | |
374 default: | |
375 NOTREACHED() << "Unsupported audio bit depth in crossfade."; | |
376 } | |
377 } | |
378 | |
379 template <class Type> | |
380 void AudioRendererAlgorithmBase::CrossfadeFrame( | |
381 uint8* outtro_bytes, const uint8* intro_bytes) { | |
382 Type* outtro = reinterpret_cast<Type*>(outtro_bytes); | |
383 const Type* intro = reinterpret_cast<const Type*>(intro_bytes); | |
384 | |
385 uint32 frames_in_crossfade = bytes_in_crossfade_ / bytes_per_frame_; | |
386 float crossfade_ratio = | |
387 static_cast<float>(crossfade_frame_number_) / frames_in_crossfade; | |
388 for (int channel = 0; channel < channels_; ++channel) { | |
389 *outtro *= 1.0 - crossfade_ratio; | |
390 *outtro++ += (*intro++) * crossfade_ratio; | |
391 } | |
392 crossfade_frame_number_++; | |
203 } | 393 } |
204 | 394 |
205 void AudioRendererAlgorithmBase::SetPlaybackRate(float new_rate) { | 395 void AudioRendererAlgorithmBase::SetPlaybackRate(float new_rate) { |
206 DCHECK_GE(new_rate, 0.0); | 396 DCHECK_GE(new_rate, 0.0); |
207 playback_rate_ = new_rate; | 397 playback_rate_ = new_rate; |
208 } | 398 } |
209 | 399 |
210 void AudioRendererAlgorithmBase::AlignToSampleBoundary(uint32* value) { | 400 void AudioRendererAlgorithmBase::AlignToFrameBoundary(uint32* value) { |
211 (*value) -= ((*value) % (channels_ * bytes_per_channel_)); | 401 (*value) -= ((*value) % bytes_per_frame_); |
212 } | 402 } |
213 | 403 |
214 void AudioRendererAlgorithmBase::FlushBuffers() { | 404 void AudioRendererAlgorithmBase::FlushBuffers() { |
405 ResetWindow(); | |
406 | |
215 // Clear the queue of decoded packets (releasing the buffers). | 407 // Clear the queue of decoded packets (releasing the buffers). |
216 audio_buffer_.Clear(); | 408 audio_buffer_.Clear(); |
217 request_read_cb_.Run(); | 409 request_read_cb_.Run(); |
218 } | 410 } |
219 | 411 |
220 base::TimeDelta AudioRendererAlgorithmBase::GetTime() { | 412 base::TimeDelta AudioRendererAlgorithmBase::GetTime() { |
221 return audio_buffer_.current_time(); | 413 return audio_buffer_.current_time(); |
222 } | 414 } |
223 | 415 |
224 void AudioRendererAlgorithmBase::EnqueueBuffer(Buffer* buffer_in) { | 416 void AudioRendererAlgorithmBase::EnqueueBuffer(Buffer* buffer_in) { |
225 // If we're at end of stream, |buffer_in| contains no data. | 417 DCHECK(!buffer_in->IsEndOfStream()); |
226 if (!buffer_in->IsEndOfStream()) | 418 audio_buffer_.Append(buffer_in); |
227 audio_buffer_.Append(buffer_in); | 419 needs_more_data_ = false; |
228 | 420 |
229 // If we still don't have enough data, request more. | 421 // If we still don't have enough data, request more. |
230 if (!IsQueueFull()) | 422 if (!IsQueueFull()) |
231 request_read_cb_.Run(); | 423 request_read_cb_.Run(); |
232 } | 424 } |
233 | 425 |
426 bool AudioRendererAlgorithmBase::NeedsMoreData() { | |
427 return needs_more_data_ || IsQueueEmpty(); | |
428 } | |
429 | |
234 bool AudioRendererAlgorithmBase::IsQueueEmpty() { | 430 bool AudioRendererAlgorithmBase::IsQueueEmpty() { |
235 return audio_buffer_.forward_bytes() == 0; | 431 return audio_buffer_.forward_bytes() == 0; |
236 } | 432 } |
237 | 433 |
238 bool AudioRendererAlgorithmBase::IsQueueFull() { | 434 bool AudioRendererAlgorithmBase::IsQueueFull() { |
239 return audio_buffer_.forward_bytes() >= audio_buffer_.forward_capacity(); | 435 return audio_buffer_.forward_bytes() >= audio_buffer_.forward_capacity(); |
240 } | 436 } |
241 | 437 |
242 uint32 AudioRendererAlgorithmBase::QueueCapacity() { | 438 uint32 AudioRendererAlgorithmBase::QueueCapacity() { |
243 return audio_buffer_.forward_capacity(); | 439 return audio_buffer_.forward_capacity(); |
244 } | 440 } |
245 | 441 |
246 void AudioRendererAlgorithmBase::IncreaseQueueCapacity() { | 442 void AudioRendererAlgorithmBase::IncreaseQueueCapacity() { |
247 audio_buffer_.set_forward_capacity( | 443 audio_buffer_.set_forward_capacity( |
248 std::min(2 * audio_buffer_.forward_capacity(), kMaxBufferSizeInBytes)); | 444 std::min(2 * audio_buffer_.forward_capacity(), kMaxBufferSizeInBytes)); |
249 } | 445 } |
250 | 446 |
251 void AudioRendererAlgorithmBase::AdvanceBufferPosition(uint32 bytes) { | |
252 audio_buffer_.Seek(bytes); | |
253 | |
254 if (!IsQueueFull()) | |
255 request_read_cb_.Run(); | |
256 } | |
257 | |
258 uint32 AudioRendererAlgorithmBase::CopyFromAudioBuffer( | |
259 uint8* dest, uint32 bytes) { | |
260 return audio_buffer_.Peek(dest, bytes); | |
261 } | |
262 | |
263 } // namespace media | 447 } // namespace media |
OLD | NEW |