| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> |
| 10 #include <string> |
| 11 |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 11 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 15 #include "base/macros.h" |
| 13 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 14 #include "base/numerics/safe_conversions.h" | 17 #include "base/numerics/safe_conversions.h" |
| 15 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 16 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
| 17 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 18 #include "media/audio/audio_output_proxy.h" | 21 #include "media/audio/audio_output_proxy.h" |
| 19 #include "media/audio/sample_rates.h" | 22 #include "media/audio/sample_rates.h" |
| 20 #include "media/base/audio_converter.h" | 23 #include "media/base/audio_converter.h" |
| 21 #include "media/base/limits.h" | 24 #include "media/base/limits.h" |
| 22 | 25 |
| 23 namespace media { | 26 namespace media { |
| 24 | 27 |
| 25 class OnMoreDataConverter | 28 class OnMoreDataConverter |
| 26 : public AudioOutputStream::AudioSourceCallback, | 29 : public AudioOutputStream::AudioSourceCallback, |
| 27 public AudioConverter::InputCallback { | 30 public AudioConverter::InputCallback { |
| 28 public: | 31 public: |
| 29 OnMoreDataConverter(const AudioParameters& input_params, | 32 OnMoreDataConverter(const AudioParameters& input_params, |
| 30 const AudioParameters& output_params); | 33 const AudioParameters& output_params); |
| 31 ~OnMoreDataConverter() override; | 34 ~OnMoreDataConverter() override; |
| 32 | 35 |
| 33 // AudioSourceCallback interface. | 36 // AudioSourceCallback interface. |
| 34 int OnMoreData(AudioBus* dest, | 37 int OnMoreData(AudioBus* dest, |
| 35 uint32_t total_bytes_delay, | 38 uint32_t total_bytes_delay, |
| 39 base::TimeDelta delay_timestamp, |
| 36 uint32_t frames_skipped) override; | 40 uint32_t frames_skipped) override; |
| 37 void OnError(AudioOutputStream* stream) override; | 41 void OnError(AudioOutputStream* stream) override; |
| 38 | 42 |
| 39 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 43 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| 40 // called before Start(). | 44 // called before Start(). |
| 41 void Start(AudioOutputStream::AudioSourceCallback* callback); | 45 void Start(AudioOutputStream::AudioSourceCallback* callback); |
| 42 | 46 |
| 43 // Clears |source_callback_| and flushes the resampler. | 47 // Clears |source_callback_| and flushes the resampler. |
| 44 void Stop(); | 48 void Stop(); |
| 45 | 49 |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 } | 372 } |
| 369 | 373 |
| 370 void OnMoreDataConverter::Stop() { | 374 void OnMoreDataConverter::Stop() { |
| 371 CHECK(source_callback_); | 375 CHECK(source_callback_); |
| 372 source_callback_ = nullptr; | 376 source_callback_ = nullptr; |
| 373 audio_converter_.RemoveInput(this); | 377 audio_converter_.RemoveInput(this); |
| 374 } | 378 } |
| 375 | 379 |
| 376 int OnMoreDataConverter::OnMoreData(AudioBus* dest, | 380 int OnMoreDataConverter::OnMoreData(AudioBus* dest, |
| 377 uint32_t total_bytes_delay, | 381 uint32_t total_bytes_delay, |
| 382 base::TimeDelta delay_timestamp, |
| 378 uint32_t frames_skipped) { | 383 uint32_t frames_skipped) { |
| 379 current_total_bytes_delay_ = total_bytes_delay; | 384 current_total_bytes_delay_ = total_bytes_delay; |
| 380 audio_converter_.Convert(dest); | 385 audio_converter_.Convert(dest); |
| 381 | 386 |
| 382 // Always return the full number of frames requested, ProvideInput() | 387 // Always return the full number of frames requested, ProvideInput() |
| 383 // will pad with silence if it wasn't able to acquire enough data. | 388 // will pad with silence if it wasn't able to acquire enough data. |
| 384 return dest->frames(); | 389 return dest->frames(); |
| 385 } | 390 } |
| 386 | 391 |
| 387 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 392 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
| 388 uint32_t frames_delayed) { | 393 uint32_t frames_delayed) { |
| 389 // Adjust playback delay to include |frames_delayed|. | 394 // Adjust playback delay to include |frames_delayed|. |
| 390 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 395 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since |
| 391 // AudioBus is just float data. Use TimeDelta instead. | 396 // AudioBus is just float data. Use TimeDelta instead. |
| 392 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( | 397 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( |
| 393 io_ratio_ * | 398 io_ratio_ * |
| 394 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); | 399 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); |
| 395 | 400 |
| 396 // Retrieve data from the original callback. | 401 // Retrieve data from the original callback. |
| 397 const int frames = | 402 const int frames = source_callback_->OnMoreData(dest, new_total_bytes_delay, |
| 398 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | 403 base::TimeDelta(), 0); |
| 399 | 404 |
| 400 // Zero any unfilled frames if anything was filled, otherwise we'll just | 405 // Zero any unfilled frames if anything was filled, otherwise we'll just |
| 401 // return a volume of zero and let AudioConverter drop the output. | 406 // return a volume of zero and let AudioConverter drop the output. |
| 402 if (frames > 0 && frames < dest->frames()) | 407 if (frames > 0 && frames < dest->frames()) |
| 403 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 408 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 404 return frames > 0 ? 1 : 0; | 409 return frames > 0 ? 1 : 0; |
| 405 } | 410 } |
| 406 | 411 |
| 407 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 412 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
| 408 error_occurred_ = true; | 413 error_occurred_ = true; |
| 409 source_callback_->OnError(stream); | 414 source_callback_->OnError(stream); |
| 410 } | 415 } |
| 411 | 416 |
| 412 } // namespace media | 417 } // namespace media |
| OLD | NEW |