| 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 "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 : public AudioOutputStream::AudioSourceCallback, | 26 : public AudioOutputStream::AudioSourceCallback, |
| 27 public AudioConverter::InputCallback { | 27 public AudioConverter::InputCallback { |
| 28 public: | 28 public: |
| 29 OnMoreDataConverter(const AudioParameters& input_params, | 29 OnMoreDataConverter(const AudioParameters& input_params, |
| 30 const AudioParameters& output_params); | 30 const AudioParameters& output_params); |
| 31 ~OnMoreDataConverter() override; | 31 ~OnMoreDataConverter() override; |
| 32 | 32 |
| 33 // AudioSourceCallback interface. | 33 // AudioSourceCallback interface. |
| 34 int OnMoreData(AudioBus* dest, | 34 int OnMoreData(AudioBus* dest, |
| 35 uint32_t total_bytes_delay, | 35 uint32_t total_bytes_delay, |
| 36 uint32_t frames_skipped) override; | 36 uint32_t frames_skipped, |
| 37 const AudioTimestamp& timestamp) override; |
| 37 void OnError(AudioOutputStream* stream) override; | 38 void OnError(AudioOutputStream* stream) override; |
| 38 | 39 |
| 39 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 40 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| 40 // called before Start(). | 41 // called before Start(). |
| 41 void Start(AudioOutputStream::AudioSourceCallback* callback); | 42 void Start(AudioOutputStream::AudioSourceCallback* callback); |
| 42 | 43 |
| 43 // Clears |source_callback_| and flushes the resampler. | 44 // Clears |source_callback_| and flushes the resampler. |
| 44 void Stop(); | 45 void Stop(); |
| 45 | 46 |
| 46 bool started() const { return source_callback_ != nullptr; } | 47 bool started() const { return source_callback_ != nullptr; } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 const int input_bytes_per_frame_; | 66 const int input_bytes_per_frame_; |
| 66 | 67 |
| 67 // Handles resampling, buffering, and channel mixing between input and output | 68 // Handles resampling, buffering, and channel mixing between input and output |
| 68 // parameters. | 69 // parameters. |
| 69 AudioConverter audio_converter_; | 70 AudioConverter audio_converter_; |
| 70 | 71 |
| 71 // True if OnError() was ever called. Should only be read if the underlying | 72 // True if OnError() was ever called. Should only be read if the underlying |
| 72 // stream has been stopped. | 73 // stream has been stopped. |
| 73 bool error_occurred_; | 74 bool error_occurred_; |
| 74 | 75 |
| 76 // Information about last recorded stream output position. |
| 77 AudioTimestamp output_timestamp_; |
| 78 |
| 75 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 79 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
| 76 }; | 80 }; |
| 77 | 81 |
| 78 // Record UMA statistics for hardware output configuration. | 82 // Record UMA statistics for hardware output configuration. |
| 79 static void RecordStats(const AudioParameters& output_params) { | 83 static void RecordStats(const AudioParameters& output_params) { |
| 80 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py | 84 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py |
| 81 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION | 85 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION |
| 82 // to report a discrete value. | 86 // to report a discrete value. |
| 83 UMA_HISTOGRAM_ENUMERATION( | 87 UMA_HISTOGRAM_ENUMERATION( |
| 84 "Media.HardwareAudioBitsPerChannel", | 88 "Media.HardwareAudioBitsPerChannel", |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 DCHECK(callbacks_.empty()); | 345 DCHECK(callbacks_.empty()); |
| 342 } | 346 } |
| 343 | 347 |
| 344 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 348 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
| 345 const AudioParameters& output_params) | 349 const AudioParameters& output_params) |
| 346 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 350 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
| 347 output_params.GetBytesPerSecond()), | 351 output_params.GetBytesPerSecond()), |
| 348 source_callback_(nullptr), | 352 source_callback_(nullptr), |
| 349 input_bytes_per_frame_(input_params.GetBytesPerFrame()), | 353 input_bytes_per_frame_(input_params.GetBytesPerFrame()), |
| 350 audio_converter_(input_params, output_params, false), | 354 audio_converter_(input_params, output_params, false), |
| 351 error_occurred_(false) {} | 355 error_occurred_(false), |
| 356 output_timestamp_() {} |
| 352 | 357 |
| 353 OnMoreDataConverter::~OnMoreDataConverter() { | 358 OnMoreDataConverter::~OnMoreDataConverter() { |
| 354 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 359 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
| 355 // calling back into OnMoreData() after destruction. | 360 // calling back into OnMoreData() after destruction. |
| 356 CHECK(!source_callback_); | 361 CHECK(!source_callback_); |
| 357 } | 362 } |
| 358 | 363 |
| 359 void OnMoreDataConverter::Start( | 364 void OnMoreDataConverter::Start( |
| 360 AudioOutputStream::AudioSourceCallback* callback) { | 365 AudioOutputStream::AudioSourceCallback* callback) { |
| 361 CHECK(!source_callback_); | 366 CHECK(!source_callback_); |
| 362 source_callback_ = callback; | 367 source_callback_ = callback; |
| 363 | 368 |
| 364 // While AudioConverter can handle multiple inputs, we're using it only with | 369 // While AudioConverter can handle multiple inputs, we're using it only with |
| 365 // a single input currently. Eventually this may be the basis for a browser | 370 // a single input currently. Eventually this may be the basis for a browser |
| 366 // side mixer. | 371 // side mixer. |
| 367 audio_converter_.AddInput(this); | 372 audio_converter_.AddInput(this); |
| 368 } | 373 } |
| 369 | 374 |
| 370 void OnMoreDataConverter::Stop() { | 375 void OnMoreDataConverter::Stop() { |
| 371 CHECK(source_callback_); | 376 CHECK(source_callback_); |
| 372 source_callback_ = nullptr; | 377 source_callback_ = nullptr; |
| 373 audio_converter_.RemoveInput(this); | 378 audio_converter_.RemoveInput(this); |
| 374 } | 379 } |
| 375 | 380 |
| 376 int OnMoreDataConverter::OnMoreData(AudioBus* dest, | 381 int OnMoreDataConverter::OnMoreData(AudioBus* dest, |
| 377 uint32_t total_bytes_delay, | 382 uint32_t total_bytes_delay, |
| 378 uint32_t frames_skipped) { | 383 uint32_t frames_skipped, |
| 384 const AudioTimestamp& timestamp) { |
| 385 output_timestamp_ = timestamp; |
| 379 current_total_bytes_delay_ = total_bytes_delay; | 386 current_total_bytes_delay_ = total_bytes_delay; |
| 380 audio_converter_.Convert(dest); | 387 audio_converter_.Convert(dest); |
| 381 | 388 |
| 382 // Always return the full number of frames requested, ProvideInput() | 389 // Always return the full number of frames requested, ProvideInput() |
| 383 // will pad with silence if it wasn't able to acquire enough data. | 390 // will pad with silence if it wasn't able to acquire enough data. |
| 384 return dest->frames(); | 391 return dest->frames(); |
| 385 } | 392 } |
| 386 | 393 |
| 387 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 394 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
| 388 uint32_t frames_delayed) { | 395 uint32_t frames_delayed) { |
| 389 // Adjust playback delay to include |frames_delayed|. | 396 // Adjust playback delay to include |frames_delayed|. |
| 390 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 397 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since |
| 391 // AudioBus is just float data. Use TimeDelta instead. | 398 // AudioBus is just float data. Use TimeDelta instead. |
| 392 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( | 399 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( |
| 393 io_ratio_ * | 400 io_ratio_ * |
| 394 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); | 401 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); |
| 395 | 402 |
| 396 // Retrieve data from the original callback. | 403 // Retrieve data from the original callback. |
| 397 const int frames = | 404 const int frames = source_callback_->OnMoreData(dest, new_total_bytes_delay, |
| 398 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | 405 0, output_timestamp_); |
| 399 | 406 |
| 400 // Zero any unfilled frames if anything was filled, otherwise we'll just | 407 // Zero any unfilled frames if anything was filled, otherwise we'll just |
| 401 // return a volume of zero and let AudioConverter drop the output. | 408 // return a volume of zero and let AudioConverter drop the output. |
| 402 if (frames > 0 && frames < dest->frames()) | 409 if (frames > 0 && frames < dest->frames()) |
| 403 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 410 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 404 return frames > 0 ? 1 : 0; | 411 return frames > 0 ? 1 : 0; |
| 405 } | 412 } |
| 406 | 413 |
| 407 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 414 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
| 408 error_occurred_ = true; | 415 error_occurred_ = true; |
| 409 source_callback_->OnError(stream); | 416 source_callback_->OnError(stream); |
| 410 } | 417 } |
| 411 | 418 |
| 412 } // namespace media | 419 } // namespace media |
| OLD | NEW |