| 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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 class OnMoreDataConverter | 22 class OnMoreDataConverter |
| 23 : public AudioOutputStream::AudioSourceCallback, | 23 : public AudioOutputStream::AudioSourceCallback, |
| 24 public AudioConverter::InputCallback { | 24 public AudioConverter::InputCallback { |
| 25 public: | 25 public: |
| 26 OnMoreDataConverter(const AudioParameters& input_params, | 26 OnMoreDataConverter(const AudioParameters& input_params, |
| 27 const AudioParameters& output_params); | 27 const AudioParameters& output_params); |
| 28 ~OnMoreDataConverter() override; | 28 ~OnMoreDataConverter() override; |
| 29 | 29 |
| 30 // AudioSourceCallback interface. | 30 // AudioSourceCallback interface. |
| 31 int OnMoreData(AudioBus* dest, | 31 int OnMoreData(AudioBus* dest, uint32 total_bytes_delay) override; |
| 32 uint32_t total_bytes_delay, | |
| 33 uint32_t frames_skipped) override; | |
| 34 void OnError(AudioOutputStream* stream) override; | 32 void OnError(AudioOutputStream* stream) override; |
| 35 | 33 |
| 36 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 34 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| 37 // called before Start(). | 35 // called before Start(). |
| 38 void Start(AudioOutputStream::AudioSourceCallback* callback); | 36 void Start(AudioOutputStream::AudioSourceCallback* callback); |
| 39 | 37 |
| 40 // Clears |source_callback_| and flushes the resampler. | 38 // Clears |source_callback_| and flushes the resampler. |
| 41 void Stop(); | 39 void Stop(); |
| 42 | 40 |
| 43 bool started() const { return source_callback_ != nullptr; } | 41 bool started() const { return source_callback_ != nullptr; } |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 audio_converter_.AddInput(this); | 363 audio_converter_.AddInput(this); |
| 366 } | 364 } |
| 367 | 365 |
| 368 void OnMoreDataConverter::Stop() { | 366 void OnMoreDataConverter::Stop() { |
| 369 CHECK(source_callback_); | 367 CHECK(source_callback_); |
| 370 source_callback_ = nullptr; | 368 source_callback_ = nullptr; |
| 371 audio_converter_.RemoveInput(this); | 369 audio_converter_.RemoveInput(this); |
| 372 } | 370 } |
| 373 | 371 |
| 374 int OnMoreDataConverter::OnMoreData(AudioBus* dest, | 372 int OnMoreDataConverter::OnMoreData(AudioBus* dest, |
| 375 uint32_t total_bytes_delay, | 373 uint32 total_bytes_delay) { |
| 376 uint32_t frames_skipped) { | |
| 377 current_total_bytes_delay_ = total_bytes_delay; | 374 current_total_bytes_delay_ = total_bytes_delay; |
| 378 audio_converter_.Convert(dest); | 375 audio_converter_.Convert(dest); |
| 379 | 376 |
| 380 // Always return the full number of frames requested, ProvideInput() | 377 // Always return the full number of frames requested, ProvideInput() |
| 381 // will pad with silence if it wasn't able to acquire enough data. | 378 // will pad with silence if it wasn't able to acquire enough data. |
| 382 return dest->frames(); | 379 return dest->frames(); |
| 383 } | 380 } |
| 384 | 381 |
| 385 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 382 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
| 386 base::TimeDelta buffer_delay) { | 383 base::TimeDelta buffer_delay) { |
| 387 // Adjust playback delay to include |buffer_delay|. | 384 // Adjust playback delay to include |buffer_delay|. |
| 388 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 385 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since |
| 389 // AudioBus is just float data. Use TimeDelta instead. | 386 // AudioBus is just float data. Use TimeDelta instead. |
| 390 uint32 new_total_bytes_delay = base::saturated_cast<uint32>( | 387 uint32 new_total_bytes_delay = base::saturated_cast<uint32>( |
| 391 io_ratio_ * (current_total_bytes_delay_ + | 388 io_ratio_ * (current_total_bytes_delay_ + |
| 392 buffer_delay.InSecondsF() * input_bytes_per_second_)); | 389 buffer_delay.InSecondsF() * input_bytes_per_second_)); |
| 393 | 390 |
| 394 // Retrieve data from the original callback. | 391 // Retrieve data from the original callback. |
| 395 const int frames = | 392 const int frames = source_callback_->OnMoreData(dest, new_total_bytes_delay); |
| 396 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | |
| 397 | 393 |
| 398 // Zero any unfilled frames if anything was filled, otherwise we'll just | 394 // Zero any unfilled frames if anything was filled, otherwise we'll just |
| 399 // return a volume of zero and let AudioConverter drop the output. | 395 // return a volume of zero and let AudioConverter drop the output. |
| 400 if (frames > 0 && frames < dest->frames()) | 396 if (frames > 0 && frames < dest->frames()) |
| 401 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 397 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 402 return frames > 0 ? 1 : 0; | 398 return frames > 0 ? 1 : 0; |
| 403 } | 399 } |
| 404 | 400 |
| 405 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 401 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
| 406 error_occurred_ = true; | 402 error_occurred_ = true; |
| 407 source_callback_->OnError(stream); | 403 source_callback_->OnError(stream); |
| 408 } | 404 } |
| 409 | 405 |
| 410 } // namespace media | 406 } // namespace media |
| OLD | NEW |