Chromium Code Reviews| 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/audio/audio_output_resampler.h" | 5 #include "media/audio/audio_output_resampler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 if (!source_callback_) { | 252 if (!source_callback_) { |
| 253 dest->Zero(); | 253 dest->Zero(); |
| 254 return dest->frames(); | 254 return dest->frames(); |
| 255 } | 255 } |
| 256 | 256 |
| 257 current_buffers_state_ = buffers_state; | 257 current_buffers_state_ = buffers_state; |
| 258 | 258 |
| 259 if (!resampler_.get() && !audio_fifo_.get()) { | 259 if (!resampler_.get() && !audio_fifo_.get()) { |
| 260 // We have no internal buffers, so clear any outstanding audio data. | 260 // We have no internal buffers, so clear any outstanding audio data. |
| 261 outstanding_audio_bytes_ = 0; | 261 outstanding_audio_bytes_ = 0; |
| 262 SourceCallback_Locked(dest); | 262 SourceIOCallback_Locked(source, dest); |
| 263 return dest->frames(); | 263 return dest->frames(); |
| 264 } | 264 } |
| 265 | 265 |
| 266 if (resampler_.get()) | 266 if (resampler_.get()) |
| 267 resampler_->Resample(dest, dest->frames()); | 267 resampler_->Resample(dest, dest->frames()); |
| 268 else | 268 else |
| 269 ProvideInput(dest); | 269 ProvideInput(dest); |
| 270 | 270 |
| 271 // Calculate how much data is left in the internal FIFO and resampler buffers. | 271 // Calculate how much data is left in the internal FIFO and resampler buffers. |
| 272 outstanding_audio_bytes_ -= | 272 outstanding_audio_bytes_ -= |
| 273 dest->frames() * output_params_.GetBytesPerFrame(); | 273 dest->frames() * output_params_.GetBytesPerFrame(); |
| 274 | 274 |
| 275 // Due to rounding errors while multiplying against |io_ratio_|, | 275 // Due to rounding errors while multiplying against |io_ratio_|, |
| 276 // |outstanding_audio_bytes_| might (rarely) slip below zero. | 276 // |outstanding_audio_bytes_| might (rarely) slip below zero. |
| 277 if (outstanding_audio_bytes_ < 0) { | 277 if (outstanding_audio_bytes_ < 0) { |
| 278 DLOG(ERROR) << "Outstanding audio bytes went negative! Value: " | 278 DLOG(ERROR) << "Outstanding audio bytes went negative! Value: " |
| 279 << outstanding_audio_bytes_; | 279 << outstanding_audio_bytes_; |
| 280 outstanding_audio_bytes_ = 0; | 280 outstanding_audio_bytes_ = 0; |
| 281 } | 281 } |
| 282 | 282 |
| 283 // Always return the full number of frames requested, ProvideInput() will pad | 283 // Always return the full number of frames requested, ProvideInput() will pad |
| 284 // with silence if it wasn't able to acquire enough data. | 284 // with silence if it wasn't able to acquire enough data. |
| 285 return dest->frames(); | 285 return dest->frames(); |
| 286 } | 286 } |
| 287 | 287 |
| 288 void AudioOutputResampler::SourceCallback_Locked(AudioBus* audio_bus) { | 288 void AudioOutputResampler::SourceCallback_Locked(AudioBus* dest) { |
| 289 SourceIOCallback_Locked(NULL, dest); | |
|
DaleCurtis
2012/09/17 21:17:36
As mentioned on chat, I feel like this is just ask
Chris Rogers
2012/09/17 22:00:42
How?
| |
| 290 } | |
| 291 | |
| 292 void AudioOutputResampler::SourceIOCallback_Locked( | |
| 293 AudioBus* source, AudioBus* dest) { | |
| 289 source_lock_.AssertAcquired(); | 294 source_lock_.AssertAcquired(); |
| 290 | 295 |
| 291 // Adjust playback delay to include the state of the internal buffers used by | 296 // Adjust playback delay to include the state of the internal buffers used by |
| 292 // the resampler and/or the FIFO. Since the sample rate and bits per channel | 297 // the resampler and/or the FIFO. Since the sample rate and bits per channel |
| 293 // may be different, we need to scale this value appropriately. | 298 // may be different, we need to scale this value appropriately. |
| 294 AudioBuffersState new_buffers_state; | 299 AudioBuffersState new_buffers_state; |
| 295 new_buffers_state.pending_bytes = io_ratio_ * | 300 new_buffers_state.pending_bytes = io_ratio_ * |
| 296 (current_buffers_state_.total_bytes() + outstanding_audio_bytes_); | 301 (current_buffers_state_.total_bytes() + outstanding_audio_bytes_); |
| 297 | 302 |
| 298 // Retrieve data from the original callback. Zero any unfilled frames. | 303 // Retrieve data from the original callback. Zero any unfilled frames. |
| 299 int frames = source_callback_->OnMoreData(audio_bus, new_buffers_state); | 304 int frames = source_callback_->OnMoreIOData(source, dest, new_buffers_state); |
| 300 if (frames < audio_bus->frames()) | 305 if (frames < dest->frames()) |
| 301 audio_bus->ZeroFramesPartial(frames, audio_bus->frames() - frames); | 306 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 302 | 307 |
| 303 // Scale the number of frames we got back in terms of input bytes to output | 308 // Scale the number of frames we got back in terms of input bytes to output |
| 304 // bytes accordingly. | 309 // bytes accordingly. |
| 305 outstanding_audio_bytes_ += | 310 outstanding_audio_bytes_ += |
| 306 (audio_bus->frames() * params_.GetBytesPerFrame()) / io_ratio_; | 311 (dest->frames() * params_.GetBytesPerFrame()) / io_ratio_; |
| 307 } | 312 } |
| 308 | 313 |
| 309 void AudioOutputResampler::ProvideInput(AudioBus* audio_bus) { | 314 void AudioOutputResampler::ProvideInput(AudioBus* audio_bus) { |
| 310 audio_fifo_->Consume(audio_bus, audio_bus->frames()); | 315 audio_fifo_->Consume(audio_bus, audio_bus->frames()); |
| 311 } | 316 } |
| 312 | 317 |
| 313 void AudioOutputResampler::OnError(AudioOutputStream* stream, int code) { | 318 void AudioOutputResampler::OnError(AudioOutputStream* stream, int code) { |
| 314 base::AutoLock auto_lock(source_lock_); | 319 base::AutoLock auto_lock(source_lock_); |
| 315 if (source_callback_) | 320 if (source_callback_) |
| 316 source_callback_->OnError(stream, code); | 321 source_callback_->OnError(stream, code); |
| 317 } | 322 } |
| 318 | 323 |
| 319 void AudioOutputResampler::WaitTillDataReady() { | 324 void AudioOutputResampler::WaitTillDataReady() { |
| 320 base::AutoLock auto_lock(source_lock_); | 325 base::AutoLock auto_lock(source_lock_); |
| 321 if (source_callback_ && !outstanding_audio_bytes_) | 326 if (source_callback_ && !outstanding_audio_bytes_) |
| 322 source_callback_->WaitTillDataReady(); | 327 source_callback_->WaitTillDataReady(); |
| 323 } | 328 } |
| 324 | 329 |
| 325 } // namespace media | 330 } // namespace media |
| OLD | NEW |