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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "media/audio/audio_output_proxy.h" | 23 #include "media/audio/audio_output_proxy.h" |
| 24 #include "media/audio/sample_rates.h" | 24 #include "media/audio/sample_rates.h" |
| 25 #include "media/base/audio_converter.h" | 25 #include "media/base/audio_converter.h" |
| 26 #include "media/base/audio_timestamp_helper.h" | 26 #include "media/base/audio_timestamp_helper.h" |
| 27 #include "media/base/limits.h" | 27 #include "media/base/limits.h" |
| 28 | 28 |
| 29 namespace media { | 29 namespace media { |
| 30 | 30 |
| 31 namespace { | |
| 32 // Global running id for OnMoreDataConverters. Used for debug recording. | |
| 33 int g_next_stream_id = 1; | |
| 34 } // namespace | |
| 35 | |
| 31 class OnMoreDataConverter | 36 class OnMoreDataConverter |
| 32 : public AudioOutputStream::AudioSourceCallback, | 37 : public AudioOutputStream::AudioSourceCallback, |
| 33 public AudioConverter::InputCallback { | 38 public AudioConverter::InputCallback { |
| 34 public: | 39 public: |
| 35 OnMoreDataConverter(const AudioParameters& input_params, | 40 OnMoreDataConverter(const AudioParameters& input_params, |
| 36 const AudioParameters& output_params); | 41 const AudioParameters& output_params); |
| 37 ~OnMoreDataConverter() override; | 42 ~OnMoreDataConverter() override; |
| 38 | 43 |
| 39 // AudioSourceCallback interface. | 44 // AudioSourceCallback interface. |
| 40 int OnMoreData(base::TimeDelta delay, | 45 int OnMoreData(base::TimeDelta delay, |
| 41 base::TimeTicks delay_timestamp, | 46 base::TimeTicks delay_timestamp, |
| 42 int prior_frames_skipped, | 47 int prior_frames_skipped, |
| 43 AudioBus* dest) override; | 48 AudioBus* dest) override; |
| 44 void OnError(AudioOutputStream* stream) override; | 49 void OnError(AudioOutputStream* stream) override; |
| 45 | 50 |
| 46 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 51 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| 47 // called before Start(). | 52 // called before Start(). |
| 48 void Start(AudioOutputStream::AudioSourceCallback* callback); | 53 void Start(AudioOutputStream::AudioSourceCallback* callback); |
| 49 | 54 |
| 50 // Clears |source_callback_| and flushes the resampler. | 55 // Clears |source_callback_| and flushes the resampler. |
| 51 void Stop(); | 56 void Stop(); |
| 52 | 57 |
| 58 // Sets the debug recording callback which is used to pass data after | |
| 59 // resampling/buffering. Must be called before Start(). | |
| 60 void SetDebugRecordingCallback( | |
| 61 const AudioOutputResampler::DebugRecordingCallback& | |
| 62 debug_recording_callback); | |
| 63 | |
| 53 bool started() const { return source_callback_ != nullptr; } | 64 bool started() const { return source_callback_ != nullptr; } |
| 54 | 65 |
| 55 bool error_occurred() const { return error_occurred_; } | 66 bool error_occurred() const { return error_occurred_; } |
| 56 | 67 |
| 68 int id() const { return id_; } | |
| 69 | |
| 70 const AudioParameters& output_params() const { return output_params_; } | |
| 71 | |
| 57 private: | 72 private: |
| 58 // AudioConverter::InputCallback implementation. | 73 // AudioConverter::InputCallback implementation. |
| 59 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; | 74 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
| 60 | 75 |
| 61 // Ratio of input bytes to output bytes used to correct playback delay with | 76 // Ratio of input bytes to output bytes used to correct playback delay with |
| 62 // regard to buffering and resampling. | 77 // regard to buffering and resampling. |
| 63 const double io_ratio_; | 78 const double io_ratio_; |
| 64 | 79 |
| 65 // Source callback. | 80 // Source callback. |
| 66 AudioOutputStream::AudioSourceCallback* source_callback_; | 81 AudioOutputStream::AudioSourceCallback* source_callback_; |
| 67 | 82 |
| 68 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to | 83 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to |
| 69 // correct playback delay in ProvideInput() before calling |source_callback_|. | 84 // correct playback delay in ProvideInput() before calling |source_callback_|. |
| 70 base::TimeDelta current_delay_; | 85 base::TimeDelta current_delay_; |
| 71 base::TimeTicks current_delay_timestamp_; | 86 base::TimeTicks current_delay_timestamp_; |
| 72 | 87 |
| 73 const int input_samples_per_second_; | 88 const int input_samples_per_second_; |
| 74 | 89 |
| 75 // Handles resampling, buffering, and channel mixing between input and output | 90 // Handles resampling, buffering, and channel mixing between input and output |
| 76 // parameters. | 91 // parameters. |
| 77 AudioConverter audio_converter_; | 92 AudioConverter audio_converter_; |
| 78 | 93 |
| 79 // True if OnError() was ever called. Should only be read if the underlying | 94 // True if OnError() was ever called. Should only be read if the underlying |
| 80 // stream has been stopped. | 95 // stream has been stopped. |
| 81 bool error_occurred_; | 96 bool error_occurred_; |
| 82 | 97 |
| 83 // Information about input and output buffer sizes to be traced. | 98 // Information about input buffer sizes to be traced. |
| 84 const int input_buffer_size_; | 99 const int input_buffer_size_; |
| 85 const int output_buffer_size_; | 100 |
| 101 // Output parameters used for buffer size tracing and for users to get. | |
| 102 const AudioParameters output_params_; | |
| 103 | |
| 104 // Used for audio debug recordings. See comment on SetDebugRecordingCallback() | |
| 105 // above. |id_| is a unique running id used in the callback. | |
| 106 AudioOutputResampler::DebugRecordingCallback debug_recording_callback_; | |
| 107 const int id_; | |
| 86 | 108 |
| 87 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 109 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
| 88 }; | 110 }; |
| 89 | 111 |
| 90 // Record UMA statistics for hardware output configuration. | 112 // Record UMA statistics for hardware output configuration. |
| 91 static void RecordStats(const AudioParameters& output_params) { | 113 static void RecordStats(const AudioParameters& output_params) { |
| 92 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py | 114 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py |
| 93 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION | 115 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION |
| 94 // to report a discrete value. | 116 // to report a discrete value. |
| 95 UMA_HISTOGRAM_ENUMERATION( | 117 UMA_HISTOGRAM_ENUMERATION( |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 AudioOutputStream::AudioSourceCallback* callback, | 359 AudioOutputStream::AudioSourceCallback* callback, |
| 338 AudioOutputProxy* stream_proxy) { | 360 AudioOutputProxy* stream_proxy) { |
| 339 DCHECK(task_runner_->BelongsToCurrentThread()); | 361 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 340 | 362 |
| 341 OnMoreDataConverter* resampler_callback = nullptr; | 363 OnMoreDataConverter* resampler_callback = nullptr; |
| 342 CallbackMap::iterator it = callbacks_.find(stream_proxy); | 364 CallbackMap::iterator it = callbacks_.find(stream_proxy); |
| 343 if (it == callbacks_.end()) { | 365 if (it == callbacks_.end()) { |
| 344 resampler_callback = new OnMoreDataConverter(params_, output_params_); | 366 resampler_callback = new OnMoreDataConverter(params_, output_params_); |
| 345 callbacks_[stream_proxy] = | 367 callbacks_[stream_proxy] = |
| 346 base::WrapUnique<OnMoreDataConverter>(resampler_callback); | 368 base::WrapUnique<OnMoreDataConverter>(resampler_callback); |
| 369 if (register_debug_recording_source_callback_) | |
| 370 register_debug_recording_source_callback_.Run(resampler_callback->id(), | |
| 371 output_params_); | |
| 372 resampler_callback->SetDebugRecordingCallback(debug_recording_callback_); | |
|
o1ka
2017/01/25 17:47:00
Sorry I can't process it :) Why can't you still ca
Henrik Grunell
2017/01/26 10:25:09
This only sets the callback to forward the data. T
| |
| 347 } else { | 373 } else { |
| 348 resampler_callback = it->second.get(); | 374 resampler_callback = it->second.get(); |
| 349 } | 375 } |
| 350 | 376 |
| 351 resampler_callback->Start(callback); | 377 resampler_callback->Start(callback); |
| 352 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); | 378 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); |
| 353 if (!result) | 379 if (!result) |
| 354 resampler_callback->Stop(); | 380 resampler_callback->Stop(); |
| 355 return result; | 381 return result; |
| 356 } | 382 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 380 | 406 |
| 381 // Start the reinitialization timer if there are no active proxies and we're | 407 // Start the reinitialization timer if there are no active proxies and we're |
| 382 // not using the originally requested output parameters. This allows us to | 408 // not using the originally requested output parameters. This allows us to |
| 383 // recover from transient output creation errors. | 409 // recover from transient output creation errors. |
| 384 if (!dispatcher_->HasOutputProxies() && callbacks_.empty() && | 410 if (!dispatcher_->HasOutputProxies() && callbacks_.empty() && |
| 385 !output_params_.Equals(original_output_params_)) { | 411 !output_params_.Equals(original_output_params_)) { |
| 386 reinitialize_timer_.Reset(); | 412 reinitialize_timer_.Reset(); |
| 387 } | 413 } |
| 388 } | 414 } |
| 389 | 415 |
| 416 void AudioOutputResampler::SetDebugRecordingCallbacks( | |
| 417 const RegisterDebugRecordingSourceCallback& | |
| 418 register_debug_recording_source_callback, | |
| 419 const UnregisterDebugRecordingSourceCallback& | |
| 420 unregister_debug_recording_source_callback, | |
| 421 const DebugRecordingCallback& debug_recording_callback) { | |
| 422 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 423 | |
| 424 register_debug_recording_source_callback_ = | |
| 425 register_debug_recording_source_callback; | |
| 426 unregister_debug_recording_source_callback_ = | |
| 427 unregister_debug_recording_source_callback; | |
| 428 debug_recording_callback_ = debug_recording_callback; | |
| 429 | |
| 430 for (const auto& it : callbacks_) { | |
| 431 register_debug_recording_source_callback.Run(it.second->id(), | |
|
o1ka
2017/01/25 17:47:00
Way too many callbacks :( the code is unreadable.
Henrik Grunell
2017/01/26 10:25:09
Yes, exactly what I thought myself later yesterday
| |
| 432 it.second->output_params()); | |
| 433 it.second->SetDebugRecordingCallback(debug_recording_callback); | |
| 434 } | |
| 435 } | |
| 436 | |
| 390 void AudioOutputResampler::StopStreamInternal( | 437 void AudioOutputResampler::StopStreamInternal( |
| 391 const CallbackMap::value_type& item) { | 438 const CallbackMap::value_type& item) { |
| 392 AudioOutputProxy* stream_proxy = item.first; | 439 AudioOutputProxy* stream_proxy = item.first; |
| 393 OnMoreDataConverter* callback = item.second.get(); | 440 OnMoreDataConverter* callback = item.second.get(); |
| 394 DCHECK(callback->started()); | 441 DCHECK(callback->started()); |
| 395 | 442 |
| 396 // Stop the underlying physical stream. | 443 // Stop the underlying physical stream. |
| 397 dispatcher_->StopStream(stream_proxy); | 444 dispatcher_->StopStream(stream_proxy); |
| 398 | 445 |
| 399 // Now that StopStream() has completed the underlying physical stream should | 446 // Now that StopStream() has completed the underlying physical stream should |
| 400 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the | 447 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the |
| 401 // OnMoreDataConverter. | 448 // OnMoreDataConverter. |
| 402 callback->Stop(); | 449 callback->Stop(); |
| 403 | 450 |
| 404 // Destroy idle streams if any errors occurred during output; this ensures | 451 // Destroy idle streams if any errors occurred during output; this ensures |
| 405 // bad streams will not be reused. Note: Errors may occur during the Stop() | 452 // bad streams will not be reused. Note: Errors may occur during the Stop() |
| 406 // call above. | 453 // call above. |
| 407 if (callback->error_occurred()) | 454 if (callback->error_occurred()) |
| 408 dispatcher_->CloseAllIdleStreams(); | 455 dispatcher_->CloseAllIdleStreams(); |
| 456 | |
| 457 if (unregister_debug_recording_source_callback_) | |
| 458 unregister_debug_recording_source_callback_.Run(callback->id()); | |
| 409 } | 459 } |
| 410 | 460 |
| 411 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 461 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
| 412 const AudioParameters& output_params) | 462 const AudioParameters& output_params) |
| 413 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 463 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
| 414 output_params.GetBytesPerSecond()), | 464 output_params.GetBytesPerSecond()), |
| 415 source_callback_(nullptr), | 465 source_callback_(nullptr), |
| 416 input_samples_per_second_(input_params.sample_rate()), | 466 input_samples_per_second_(input_params.sample_rate()), |
| 417 audio_converter_(input_params, output_params, false), | 467 audio_converter_(input_params, output_params, false), |
| 418 error_occurred_(false), | 468 error_occurred_(false), |
| 419 input_buffer_size_(input_params.frames_per_buffer()), | 469 input_buffer_size_(input_params.frames_per_buffer()), |
| 420 output_buffer_size_(output_params.frames_per_buffer()) { | 470 output_params_(output_params), |
| 471 id_(g_next_stream_id++) { | |
| 421 RecordRebufferingStats(input_params, output_params); | 472 RecordRebufferingStats(input_params, output_params); |
| 422 } | 473 } |
| 423 | 474 |
| 424 OnMoreDataConverter::~OnMoreDataConverter() { | 475 OnMoreDataConverter::~OnMoreDataConverter() { |
| 425 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 476 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
| 426 // calling back into OnMoreData() after destruction. | 477 // calling back into OnMoreData() after destruction. |
| 427 CHECK(!source_callback_); | 478 CHECK(!source_callback_); |
| 428 } | 479 } |
| 429 | 480 |
| 430 void OnMoreDataConverter::Start( | 481 void OnMoreDataConverter::Start( |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 442 CHECK(source_callback_); | 493 CHECK(source_callback_); |
| 443 source_callback_ = nullptr; | 494 source_callback_ = nullptr; |
| 444 audio_converter_.RemoveInput(this); | 495 audio_converter_.RemoveInput(this); |
| 445 } | 496 } |
| 446 | 497 |
| 447 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, | 498 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, |
| 448 base::TimeTicks delay_timestamp, | 499 base::TimeTicks delay_timestamp, |
| 449 int /* prior_frames_skipped */, | 500 int /* prior_frames_skipped */, |
| 450 AudioBus* dest) { | 501 AudioBus* dest) { |
| 451 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", | 502 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", |
| 452 input_buffer_size_, "output buffer size", output_buffer_size_); | 503 input_buffer_size_, "output buffer size", |
| 504 output_params_.frames_per_buffer()); | |
| 453 current_delay_ = delay; | 505 current_delay_ = delay; |
| 454 current_delay_timestamp_ = delay_timestamp; | 506 current_delay_timestamp_ = delay_timestamp; |
| 455 audio_converter_.Convert(dest); | 507 audio_converter_.Convert(dest); |
| 456 | 508 |
| 509 if (debug_recording_callback_) | |
| 510 debug_recording_callback_.Run(id_, dest); | |
| 511 | |
| 457 // Always return the full number of frames requested, ProvideInput() | 512 // Always return the full number of frames requested, ProvideInput() |
| 458 // will pad with silence if it wasn't able to acquire enough data. | 513 // will pad with silence if it wasn't able to acquire enough data. |
| 459 return dest->frames(); | 514 return dest->frames(); |
| 460 } | 515 } |
| 461 | 516 |
| 462 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 517 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
| 463 uint32_t frames_delayed) { | 518 uint32_t frames_delayed) { |
| 464 base::TimeDelta new_delay = | 519 base::TimeDelta new_delay = |
| 465 current_delay_ + AudioTimestampHelper::FramesToTime( | 520 current_delay_ + AudioTimestampHelper::FramesToTime( |
| 466 frames_delayed, input_samples_per_second_); | 521 frames_delayed, input_samples_per_second_); |
| 467 // Retrieve data from the original callback. | 522 // Retrieve data from the original callback. |
| 468 const int frames = source_callback_->OnMoreData( | 523 const int frames = source_callback_->OnMoreData( |
| 469 new_delay, current_delay_timestamp_, 0, dest); | 524 new_delay, current_delay_timestamp_, 0, dest); |
| 470 | 525 |
| 471 // Zero any unfilled frames if anything was filled, otherwise we'll just | 526 // Zero any unfilled frames if anything was filled, otherwise we'll just |
| 472 // return a volume of zero and let AudioConverter drop the output. | 527 // return a volume of zero and let AudioConverter drop the output. |
| 473 if (frames > 0 && frames < dest->frames()) | 528 if (frames > 0 && frames < dest->frames()) |
| 474 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 529 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 475 return frames > 0 ? 1 : 0; | 530 return frames > 0 ? 1 : 0; |
| 476 } | 531 } |
| 477 | 532 |
| 478 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 533 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
| 479 error_occurred_ = true; | 534 error_occurred_ = true; |
| 480 source_callback_->OnError(stream); | 535 source_callback_->OnError(stream); |
| 481 } | 536 } |
| 482 | 537 |
| 538 void OnMoreDataConverter::SetDebugRecordingCallback( | |
| 539 const AudioOutputResampler::DebugRecordingCallback& | |
| 540 debug_recording_callback) { | |
| 541 CHECK(!source_callback_); | |
| 542 debug_recording_callback_ = debug_recording_callback; | |
| 543 } | |
| 544 | |
| 483 } // namespace media | 545 } // namespace media |
| OLD | NEW |