| 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> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 18 #include "base/metrics/sparse_histogram.h" | 18 #include "base/metrics/sparse_histogram.h" |
| 19 #include "base/numerics/safe_conversions.h" | 19 #include "base/numerics/safe_conversions.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "media/audio/audio_debug_recording_helper.h" |
| 23 #include "media/audio/audio_output_proxy.h" | 25 #include "media/audio/audio_output_proxy.h" |
| 24 #include "media/audio/sample_rates.h" | 26 #include "media/audio/sample_rates.h" |
| 25 #include "media/base/audio_converter.h" | 27 #include "media/base/audio_converter.h" |
| 26 #include "media/base/audio_timestamp_helper.h" | 28 #include "media/base/audio_timestamp_helper.h" |
| 27 #include "media/base/limits.h" | 29 #include "media/base/limits.h" |
| 28 | 30 |
| 29 namespace media { | 31 namespace media { |
| 30 | 32 |
| 33 namespace { |
| 34 |
| 35 // Running id to append to debug recording filename. |
| 36 int g_next_debug_recording_filename_id = 1; |
| 37 |
| 38 #if defined(OS_WIN) |
| 39 #define IntToStringType base::IntToString16 |
| 40 #else |
| 41 #define IntToStringType base::IntToString |
| 42 #endif |
| 43 |
| 44 // Adds debug recording filename running id as an extension. |
| 45 base::FilePath AddDebugRecordingFilenameId(const base::FilePath& file_name) { |
| 46 return file_name.AddExtension( |
| 47 IntToStringType(g_next_debug_recording_filename_id++)); |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 31 class OnMoreDataConverter | 52 class OnMoreDataConverter |
| 32 : public AudioOutputStream::AudioSourceCallback, | 53 : public AudioOutputStream::AudioSourceCallback, |
| 33 public AudioConverter::InputCallback { | 54 public AudioConverter::InputCallback { |
| 34 public: | 55 public: |
| 35 OnMoreDataConverter(const AudioParameters& input_params, | 56 OnMoreDataConverter( |
| 36 const AudioParameters& output_params); | 57 const AudioParameters& input_params, |
| 58 const AudioParameters& output_params, |
| 59 std::unique_ptr<AudioDebugRecordingHelper> debug_recording_helper); |
| 37 ~OnMoreDataConverter() override; | 60 ~OnMoreDataConverter() override; |
| 38 | 61 |
| 39 // AudioSourceCallback interface. | 62 // AudioSourceCallback interface. |
| 40 int OnMoreData(base::TimeDelta delay, | 63 int OnMoreData(base::TimeDelta delay, |
| 41 base::TimeTicks delay_timestamp, | 64 base::TimeTicks delay_timestamp, |
| 42 int prior_frames_skipped, | 65 int prior_frames_skipped, |
| 43 AudioBus* dest) override; | 66 AudioBus* dest) override; |
| 44 void OnError(AudioOutputStream* stream) override; | 67 void OnError(AudioOutputStream* stream) override; |
| 45 | 68 |
| 46 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 69 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| 47 // called before Start(). | 70 // called before Start(). |
| 48 void Start(AudioOutputStream::AudioSourceCallback* callback); | 71 void Start(AudioOutputStream::AudioSourceCallback* callback); |
| 49 | 72 |
| 50 // Clears |source_callback_| and flushes the resampler. | 73 // Clears |source_callback_| and flushes the resampler. |
| 51 void Stop(); | 74 void Stop(); |
| 52 | 75 |
| 76 // Controls debug recording. |
| 77 void EnableDebugRecording(const base::FilePath& file_name); |
| 78 void DisableDebugRecording(); |
| 79 |
| 53 bool started() const { return source_callback_ != nullptr; } | 80 bool started() const { return source_callback_ != nullptr; } |
| 54 | 81 |
| 55 bool error_occurred() const { return error_occurred_; } | 82 bool error_occurred() const { return error_occurred_; } |
| 56 | 83 |
| 57 private: | 84 private: |
| 58 // AudioConverter::InputCallback implementation. | 85 // AudioConverter::InputCallback implementation. |
| 59 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; | 86 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
| 60 | 87 |
| 61 // Ratio of input bytes to output bytes used to correct playback delay with | 88 // Ratio of input bytes to output bytes used to correct playback delay with |
| 62 // regard to buffering and resampling. | 89 // regard to buffering and resampling. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 77 AudioConverter audio_converter_; | 104 AudioConverter audio_converter_; |
| 78 | 105 |
| 79 // True if OnError() was ever called. Should only be read if the underlying | 106 // True if OnError() was ever called. Should only be read if the underlying |
| 80 // stream has been stopped. | 107 // stream has been stopped. |
| 81 bool error_occurred_; | 108 bool error_occurred_; |
| 82 | 109 |
| 83 // Information about input and output buffer sizes to be traced. | 110 // Information about input and output buffer sizes to be traced. |
| 84 const int input_buffer_size_; | 111 const int input_buffer_size_; |
| 85 const int output_buffer_size_; | 112 const int output_buffer_size_; |
| 86 | 113 |
| 114 // Used for audio debug recordings. |
| 115 std::unique_ptr<AudioDebugRecordingHelper> debug_recording_helper_; |
| 116 AudioParameters output_params_; |
| 117 |
| 87 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 118 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
| 88 }; | 119 }; |
| 89 | 120 |
| 90 // Record UMA statistics for hardware output configuration. | 121 // Record UMA statistics for hardware output configuration. |
| 91 static void RecordStats(const AudioParameters& output_params) { | 122 static void RecordStats(const AudioParameters& output_params) { |
| 92 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py | 123 // 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 | 124 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION |
| 94 // to report a discrete value. | 125 // to report a discrete value. |
| 95 UMA_HISTOGRAM_ENUMERATION( | 126 UMA_HISTOGRAM_ENUMERATION( |
| 96 "Media.HardwareAudioBitsPerChannel", | 127 "Media.HardwareAudioBitsPerChannel", |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 Initialize(); | 245 Initialize(); |
| 215 #endif | 246 #endif |
| 216 } | 247 } |
| 217 | 248 |
| 218 AudioOutputResampler::AudioOutputResampler(AudioManager* audio_manager, | 249 AudioOutputResampler::AudioOutputResampler(AudioManager* audio_manager, |
| 219 const AudioParameters& input_params, | 250 const AudioParameters& input_params, |
| 220 const AudioParameters& output_params, | 251 const AudioParameters& output_params, |
| 221 const std::string& output_device_id, | 252 const std::string& output_device_id, |
| 222 const base::TimeDelta& close_delay) | 253 const base::TimeDelta& close_delay) |
| 223 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), | 254 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), |
| 255 audio_manager_(audio_manager), |
| 224 close_delay_(close_delay), | 256 close_delay_(close_delay), |
| 225 output_params_(output_params), | 257 output_params_(output_params), |
| 226 original_output_params_(output_params), | 258 original_output_params_(output_params), |
| 227 streams_opened_(false), | 259 streams_opened_(false), |
| 228 reinitialize_timer_(FROM_HERE, | 260 reinitialize_timer_(FROM_HERE, |
| 229 close_delay_, | 261 close_delay_, |
| 230 base::Bind(&AudioOutputResampler::Reinitialize, | 262 base::Bind(&AudioOutputResampler::Reinitialize, |
| 231 base::Unretained(this)), | 263 base::Unretained(this)), |
| 232 false), | 264 false), |
| 233 weak_factory_(this) { | 265 weak_factory_(this) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 } | 366 } |
| 335 | 367 |
| 336 bool AudioOutputResampler::StartStream( | 368 bool AudioOutputResampler::StartStream( |
| 337 AudioOutputStream::AudioSourceCallback* callback, | 369 AudioOutputStream::AudioSourceCallback* callback, |
| 338 AudioOutputProxy* stream_proxy) { | 370 AudioOutputProxy* stream_proxy) { |
| 339 DCHECK(task_runner_->BelongsToCurrentThread()); | 371 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 340 | 372 |
| 341 OnMoreDataConverter* resampler_callback = nullptr; | 373 OnMoreDataConverter* resampler_callback = nullptr; |
| 342 CallbackMap::iterator it = callbacks_.find(stream_proxy); | 374 CallbackMap::iterator it = callbacks_.find(stream_proxy); |
| 343 if (it == callbacks_.end()) { | 375 if (it == callbacks_.end()) { |
| 344 resampler_callback = new OnMoreDataConverter(params_, output_params_); | 376 resampler_callback = new OnMoreDataConverter( |
| 377 params_, output_params_, |
| 378 base::MakeUnique<AudioDebugRecordingHelper>( |
| 379 audio_manager_, audio_manager_->GetTaskRunner())); |
| 345 callbacks_[stream_proxy] = | 380 callbacks_[stream_proxy] = |
| 346 base::WrapUnique<OnMoreDataConverter>(resampler_callback); | 381 base::WrapUnique<OnMoreDataConverter>(resampler_callback); |
| 382 |
| 383 // If debug recording is enabled, enable it on the new OnMoreDataConverter. |
| 384 if (!debug_recording_file_name_.empty()) |
| 385 resampler_callback->EnableDebugRecording( |
| 386 AddDebugRecordingFilenameId(debug_recording_file_name_)); |
| 347 } else { | 387 } else { |
| 348 resampler_callback = it->second.get(); | 388 resampler_callback = it->second.get(); |
| 349 } | 389 } |
| 350 | 390 |
| 351 resampler_callback->Start(callback); | 391 resampler_callback->Start(callback); |
| 352 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); | 392 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); |
| 353 if (!result) | 393 if (!result) |
| 354 resampler_callback->Stop(); | 394 resampler_callback->Stop(); |
| 355 return result; | 395 return result; |
| 356 } | 396 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 380 | 420 |
| 381 // Start the reinitialization timer if there are no active proxies and we're | 421 // 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 | 422 // not using the originally requested output parameters. This allows us to |
| 383 // recover from transient output creation errors. | 423 // recover from transient output creation errors. |
| 384 if (!dispatcher_->HasOutputProxies() && callbacks_.empty() && | 424 if (!dispatcher_->HasOutputProxies() && callbacks_.empty() && |
| 385 !output_params_.Equals(original_output_params_)) { | 425 !output_params_.Equals(original_output_params_)) { |
| 386 reinitialize_timer_.Reset(); | 426 reinitialize_timer_.Reset(); |
| 387 } | 427 } |
| 388 } | 428 } |
| 389 | 429 |
| 430 void AudioOutputResampler::EnableDebugRecording( |
| 431 const base::FilePath& file_name) { |
| 432 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 433 for (const auto& item : callbacks_) |
| 434 item.second->EnableDebugRecording(AddDebugRecordingFilenameId(file_name)); |
| 435 debug_recording_file_name_ = file_name; |
| 436 } |
| 437 |
| 438 void AudioOutputResampler::DisableDebugRecording() { |
| 439 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 440 for (const auto& item : callbacks_) |
| 441 item.second->DisableDebugRecording(); |
| 442 debug_recording_file_name_.clear(); |
| 443 } |
| 444 |
| 390 void AudioOutputResampler::StopStreamInternal( | 445 void AudioOutputResampler::StopStreamInternal( |
| 391 const CallbackMap::value_type& item) { | 446 const CallbackMap::value_type& item) { |
| 392 AudioOutputProxy* stream_proxy = item.first; | 447 AudioOutputProxy* stream_proxy = item.first; |
| 393 OnMoreDataConverter* callback = item.second.get(); | 448 OnMoreDataConverter* callback = item.second.get(); |
| 394 DCHECK(callback->started()); | 449 DCHECK(callback->started()); |
| 395 | 450 |
| 396 // Stop the underlying physical stream. | 451 // Stop the underlying physical stream. |
| 397 dispatcher_->StopStream(stream_proxy); | 452 dispatcher_->StopStream(stream_proxy); |
| 398 | 453 |
| 399 // Now that StopStream() has completed the underlying physical stream should | 454 // Now that StopStream() has completed the underlying physical stream should |
| 400 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the | 455 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the |
| 401 // OnMoreDataConverter. | 456 // OnMoreDataConverter. |
| 402 callback->Stop(); | 457 callback->Stop(); |
| 403 | 458 |
| 404 // Destroy idle streams if any errors occurred during output; this ensures | 459 // 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() | 460 // bad streams will not be reused. Note: Errors may occur during the Stop() |
| 406 // call above. | 461 // call above. |
| 407 if (callback->error_occurred()) | 462 if (callback->error_occurred()) |
| 408 dispatcher_->CloseAllIdleStreams(); | 463 dispatcher_->CloseAllIdleStreams(); |
| 409 } | 464 } |
| 410 | 465 |
| 411 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 466 OnMoreDataConverter::OnMoreDataConverter( |
| 412 const AudioParameters& output_params) | 467 const AudioParameters& input_params, |
| 468 const AudioParameters& output_params, |
| 469 std::unique_ptr<AudioDebugRecordingHelper> debug_recording_helper) |
| 413 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 470 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
| 414 output_params.GetBytesPerSecond()), | 471 output_params.GetBytesPerSecond()), |
| 415 source_callback_(nullptr), | 472 source_callback_(nullptr), |
| 416 input_samples_per_second_(input_params.sample_rate()), | 473 input_samples_per_second_(input_params.sample_rate()), |
| 417 audio_converter_(input_params, output_params, false), | 474 audio_converter_(input_params, output_params, false), |
| 418 error_occurred_(false), | 475 error_occurred_(false), |
| 419 input_buffer_size_(input_params.frames_per_buffer()), | 476 input_buffer_size_(input_params.frames_per_buffer()), |
| 420 output_buffer_size_(output_params.frames_per_buffer()) { | 477 output_buffer_size_(output_params.frames_per_buffer()), |
| 478 debug_recording_helper_(std::move(debug_recording_helper)), |
| 479 output_params_(output_params) { |
| 421 RecordRebufferingStats(input_params, output_params); | 480 RecordRebufferingStats(input_params, output_params); |
| 422 } | 481 } |
| 423 | 482 |
| 424 OnMoreDataConverter::~OnMoreDataConverter() { | 483 OnMoreDataConverter::~OnMoreDataConverter() { |
| 425 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 484 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
| 426 // calling back into OnMoreData() after destruction. | 485 // calling back into OnMoreData() after destruction. |
| 427 CHECK(!source_callback_); | 486 CHECK(!source_callback_); |
| 428 } | 487 } |
| 429 | 488 |
| 430 void OnMoreDataConverter::Start( | 489 void OnMoreDataConverter::Start( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 447 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, | 506 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, |
| 448 base::TimeTicks delay_timestamp, | 507 base::TimeTicks delay_timestamp, |
| 449 int /* prior_frames_skipped */, | 508 int /* prior_frames_skipped */, |
| 450 AudioBus* dest) { | 509 AudioBus* dest) { |
| 451 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", | 510 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", |
| 452 input_buffer_size_, "output buffer size", output_buffer_size_); | 511 input_buffer_size_, "output buffer size", output_buffer_size_); |
| 453 current_delay_ = delay; | 512 current_delay_ = delay; |
| 454 current_delay_timestamp_ = delay_timestamp; | 513 current_delay_timestamp_ = delay_timestamp; |
| 455 audio_converter_.Convert(dest); | 514 audio_converter_.Convert(dest); |
| 456 | 515 |
| 516 debug_recording_helper_->MaybeWrite(dest); |
| 517 |
| 457 // Always return the full number of frames requested, ProvideInput() | 518 // Always return the full number of frames requested, ProvideInput() |
| 458 // will pad with silence if it wasn't able to acquire enough data. | 519 // will pad with silence if it wasn't able to acquire enough data. |
| 459 return dest->frames(); | 520 return dest->frames(); |
| 460 } | 521 } |
| 461 | 522 |
| 462 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 523 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
| 463 uint32_t frames_delayed) { | 524 uint32_t frames_delayed) { |
| 464 base::TimeDelta new_delay = | 525 base::TimeDelta new_delay = |
| 465 current_delay_ + AudioTimestampHelper::FramesToTime( | 526 current_delay_ + AudioTimestampHelper::FramesToTime( |
| 466 frames_delayed, input_samples_per_second_); | 527 frames_delayed, input_samples_per_second_); |
| 467 // Retrieve data from the original callback. | 528 // Retrieve data from the original callback. |
| 468 const int frames = source_callback_->OnMoreData( | 529 const int frames = source_callback_->OnMoreData( |
| 469 new_delay, current_delay_timestamp_, 0, dest); | 530 new_delay, current_delay_timestamp_, 0, dest); |
| 470 | 531 |
| 471 // Zero any unfilled frames if anything was filled, otherwise we'll just | 532 // Zero any unfilled frames if anything was filled, otherwise we'll just |
| 472 // return a volume of zero and let AudioConverter drop the output. | 533 // return a volume of zero and let AudioConverter drop the output. |
| 473 if (frames > 0 && frames < dest->frames()) | 534 if (frames > 0 && frames < dest->frames()) |
| 474 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 535 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 475 return frames > 0 ? 1 : 0; | 536 return frames > 0 ? 1 : 0; |
| 476 } | 537 } |
| 477 | 538 |
| 478 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 539 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
| 479 error_occurred_ = true; | 540 error_occurred_ = true; |
| 480 source_callback_->OnError(stream); | 541 source_callback_->OnError(stream); |
| 481 } | 542 } |
| 482 | 543 |
| 544 void OnMoreDataConverter::EnableDebugRecording( |
| 545 const base::FilePath& file_name) { |
| 546 debug_recording_helper_->EnableDebugRecording(output_params_, file_name); |
| 547 } |
| 548 |
| 549 void OnMoreDataConverter::DisableDebugRecording() { |
| 550 debug_recording_helper_->DisableDebugRecording(); |
| 551 } |
| 552 |
| 483 } // namespace media | 553 } // namespace media |
| OLD | NEW |