| 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 15 matching lines...) Expand all Loading... |
| 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 class OnMoreDataConverter | 31 class OnMoreDataConverter |
| 32 : public AudioOutputStream::AudioSourceCallback, | 32 : public AudioOutputStream::AudioSourceCallback, |
| 33 public AudioConverter::InputCallback { | 33 public AudioConverter::InputCallback { |
| 34 public: | 34 public: |
| 35 OnMoreDataConverter(const AudioParameters& input_params, | 35 OnMoreDataConverter(const AudioParameters& input_params, |
| 36 const AudioParameters& output_params); | 36 const AudioParameters& output_params, |
| 37 std::unique_ptr<AudioDebugRecorder> debug_recorder); |
| 37 ~OnMoreDataConverter() override; | 38 ~OnMoreDataConverter() override; |
| 38 | 39 |
| 39 // AudioSourceCallback interface. | 40 // AudioSourceCallback interface. |
| 40 int OnMoreData(base::TimeDelta delay, | 41 int OnMoreData(base::TimeDelta delay, |
| 41 base::TimeTicks delay_timestamp, | 42 base::TimeTicks delay_timestamp, |
| 42 int prior_frames_skipped, | 43 int prior_frames_skipped, |
| 43 AudioBus* dest) override; | 44 AudioBus* dest) override; |
| 44 void OnError(AudioOutputStream* stream) override; | 45 void OnError(AudioOutputStream* stream) override; |
| 45 | 46 |
| 46 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 47 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
| (...skipping 30 matching lines...) Expand all Loading... |
| 77 AudioConverter audio_converter_; | 78 AudioConverter audio_converter_; |
| 78 | 79 |
| 79 // True if OnError() was ever called. Should only be read if the underlying | 80 // True if OnError() was ever called. Should only be read if the underlying |
| 80 // stream has been stopped. | 81 // stream has been stopped. |
| 81 bool error_occurred_; | 82 bool error_occurred_; |
| 82 | 83 |
| 83 // Information about input and output buffer sizes to be traced. | 84 // Information about input and output buffer sizes to be traced. |
| 84 const int input_buffer_size_; | 85 const int input_buffer_size_; |
| 85 const int output_buffer_size_; | 86 const int output_buffer_size_; |
| 86 | 87 |
| 88 // For audio debug recordings. |
| 89 std::unique_ptr<AudioDebugRecorder> debug_recorder_; |
| 90 |
| 87 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 91 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
| 88 }; | 92 }; |
| 89 | 93 |
| 90 // Record UMA statistics for hardware output configuration. | 94 // Record UMA statistics for hardware output configuration. |
| 91 static void RecordStats(const AudioParameters& output_params) { | 95 static void RecordStats(const AudioParameters& output_params) { |
| 92 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py | 96 // 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 | 97 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION |
| 94 // to report a discrete value. | 98 // to report a discrete value. |
| 95 UMA_HISTOGRAM_ENUMERATION( | 99 UMA_HISTOGRAM_ENUMERATION( |
| 96 "Media.HardwareAudioBitsPerChannel", | 100 "Media.HardwareAudioBitsPerChannel", |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 | 212 |
| 209 output_params_ = AudioParameters( | 213 output_params_ = AudioParameters( |
| 210 AudioParameters::AUDIO_PCM_LINEAR, params_.channel_layout(), | 214 AudioParameters::AUDIO_PCM_LINEAR, params_.channel_layout(), |
| 211 params_.sample_rate(), params_.bits_per_sample(), | 215 params_.sample_rate(), params_.bits_per_sample(), |
| 212 frames_per_buffer); | 216 frames_per_buffer); |
| 213 device_id_ = ""; | 217 device_id_ = ""; |
| 214 Initialize(); | 218 Initialize(); |
| 215 #endif | 219 #endif |
| 216 } | 220 } |
| 217 | 221 |
| 218 AudioOutputResampler::AudioOutputResampler(AudioManager* audio_manager, | 222 AudioOutputResampler::AudioOutputResampler( |
| 219 const AudioParameters& input_params, | 223 AudioManager* audio_manager, |
| 220 const AudioParameters& output_params, | 224 const AudioParameters& input_params, |
| 221 const std::string& output_device_id, | 225 const AudioParameters& output_params, |
| 222 const base::TimeDelta& close_delay) | 226 const std::string& output_device_id, |
| 227 base::TimeDelta close_delay, |
| 228 const RegisterDebugRecordingSourceCallback& |
| 229 register_debug_recording_source_callback) |
| 223 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), | 230 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), |
| 224 close_delay_(close_delay), | 231 close_delay_(close_delay), |
| 225 output_params_(output_params), | 232 output_params_(output_params), |
| 226 original_output_params_(output_params), | 233 original_output_params_(output_params), |
| 227 streams_opened_(false), | 234 streams_opened_(false), |
| 228 reinitialize_timer_(FROM_HERE, | 235 reinitialize_timer_(FROM_HERE, |
| 229 close_delay_, | 236 close_delay_, |
| 230 base::Bind(&AudioOutputResampler::Reinitialize, | 237 base::Bind(&AudioOutputResampler::Reinitialize, |
| 231 base::Unretained(this)), | 238 base::Unretained(this)), |
| 232 false), | 239 false), |
| 240 register_debug_recording_source_callback_( |
| 241 register_debug_recording_source_callback), |
| 233 weak_factory_(this) { | 242 weak_factory_(this) { |
| 234 DCHECK(input_params.IsValid()); | 243 DCHECK(input_params.IsValid()); |
| 235 DCHECK(output_params.IsValid()); | 244 DCHECK(output_params.IsValid()); |
| 236 DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY); | 245 DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY); |
| 246 DCHECK(register_debug_recording_source_callback_); |
| 237 | 247 |
| 238 // Record UMA statistics for the hardware configuration. | 248 // Record UMA statistics for the hardware configuration. |
| 239 RecordStats(output_params); | 249 RecordStats(output_params); |
| 240 | 250 |
| 241 Initialize(); | 251 Initialize(); |
| 242 } | 252 } |
| 243 | 253 |
| 244 AudioOutputResampler::~AudioOutputResampler() { | 254 AudioOutputResampler::~AudioOutputResampler() { |
| 245 for (const auto& item : callbacks_) { | 255 for (const auto& item : callbacks_) { |
| 246 if (item.second->started()) | 256 if (item.second->started()) |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 } | 344 } |
| 335 | 345 |
| 336 bool AudioOutputResampler::StartStream( | 346 bool AudioOutputResampler::StartStream( |
| 337 AudioOutputStream::AudioSourceCallback* callback, | 347 AudioOutputStream::AudioSourceCallback* callback, |
| 338 AudioOutputProxy* stream_proxy) { | 348 AudioOutputProxy* stream_proxy) { |
| 339 DCHECK(task_runner_->BelongsToCurrentThread()); | 349 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 340 | 350 |
| 341 OnMoreDataConverter* resampler_callback = nullptr; | 351 OnMoreDataConverter* resampler_callback = nullptr; |
| 342 CallbackMap::iterator it = callbacks_.find(stream_proxy); | 352 CallbackMap::iterator it = callbacks_.find(stream_proxy); |
| 343 if (it == callbacks_.end()) { | 353 if (it == callbacks_.end()) { |
| 344 resampler_callback = new OnMoreDataConverter(params_, output_params_); | 354 // If a register callback has been given, register and pass the returned |
| 355 // recoder to the converter. Data is fed to same recorder for the lifetime |
| 356 // of the converter, which is until the stream is closed. |
| 357 resampler_callback = new OnMoreDataConverter( |
| 358 params_, output_params_, |
| 359 register_debug_recording_source_callback_.Run(output_params_)); |
| 345 callbacks_[stream_proxy] = | 360 callbacks_[stream_proxy] = |
| 346 base::WrapUnique<OnMoreDataConverter>(resampler_callback); | 361 base::WrapUnique<OnMoreDataConverter>(resampler_callback); |
| 347 } else { | 362 } else { |
| 348 resampler_callback = it->second.get(); | 363 resampler_callback = it->second.get(); |
| 349 } | 364 } |
| 350 | 365 |
| 351 resampler_callback->Start(callback); | 366 resampler_callback->Start(callback); |
| 352 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); | 367 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); |
| 353 if (!result) | 368 if (!result) |
| 354 resampler_callback->Stop(); | 369 resampler_callback->Stop(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 // OnMoreDataConverter. | 416 // OnMoreDataConverter. |
| 402 callback->Stop(); | 417 callback->Stop(); |
| 403 | 418 |
| 404 // Destroy idle streams if any errors occurred during output; this ensures | 419 // 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() | 420 // bad streams will not be reused. Note: Errors may occur during the Stop() |
| 406 // call above. | 421 // call above. |
| 407 if (callback->error_occurred()) | 422 if (callback->error_occurred()) |
| 408 dispatcher_->CloseAllIdleStreams(); | 423 dispatcher_->CloseAllIdleStreams(); |
| 409 } | 424 } |
| 410 | 425 |
| 411 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 426 OnMoreDataConverter::OnMoreDataConverter( |
| 412 const AudioParameters& output_params) | 427 const AudioParameters& input_params, |
| 428 const AudioParameters& output_params, |
| 429 std::unique_ptr<AudioDebugRecorder> debug_recorder) |
| 413 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 430 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
| 414 output_params.GetBytesPerSecond()), | 431 output_params.GetBytesPerSecond()), |
| 415 source_callback_(nullptr), | 432 source_callback_(nullptr), |
| 416 input_samples_per_second_(input_params.sample_rate()), | 433 input_samples_per_second_(input_params.sample_rate()), |
| 417 audio_converter_(input_params, output_params, false), | 434 audio_converter_(input_params, output_params, false), |
| 418 error_occurred_(false), | 435 error_occurred_(false), |
| 419 input_buffer_size_(input_params.frames_per_buffer()), | 436 input_buffer_size_(input_params.frames_per_buffer()), |
| 420 output_buffer_size_(output_params.frames_per_buffer()) { | 437 output_buffer_size_(output_params.frames_per_buffer()), |
| 438 debug_recorder_(std::move(debug_recorder)) { |
| 421 RecordRebufferingStats(input_params, output_params); | 439 RecordRebufferingStats(input_params, output_params); |
| 422 } | 440 } |
| 423 | 441 |
| 424 OnMoreDataConverter::~OnMoreDataConverter() { | 442 OnMoreDataConverter::~OnMoreDataConverter() { |
| 425 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 443 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
| 426 // calling back into OnMoreData() after destruction. | 444 // calling back into OnMoreData() after destruction. |
| 427 CHECK(!source_callback_); | 445 CHECK(!source_callback_); |
| 428 } | 446 } |
| 429 | 447 |
| 430 void OnMoreDataConverter::Start( | 448 void OnMoreDataConverter::Start( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 447 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, | 465 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, |
| 448 base::TimeTicks delay_timestamp, | 466 base::TimeTicks delay_timestamp, |
| 449 int /* prior_frames_skipped */, | 467 int /* prior_frames_skipped */, |
| 450 AudioBus* dest) { | 468 AudioBus* dest) { |
| 451 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", | 469 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", |
| 452 input_buffer_size_, "output buffer size", output_buffer_size_); | 470 input_buffer_size_, "output buffer size", output_buffer_size_); |
| 453 current_delay_ = delay; | 471 current_delay_ = delay; |
| 454 current_delay_timestamp_ = delay_timestamp; | 472 current_delay_timestamp_ = delay_timestamp; |
| 455 audio_converter_.Convert(dest); | 473 audio_converter_.Convert(dest); |
| 456 | 474 |
| 475 if (debug_recorder_) |
| 476 debug_recorder_->OnData(dest); |
| 477 |
| 457 // Always return the full number of frames requested, ProvideInput() | 478 // Always return the full number of frames requested, ProvideInput() |
| 458 // will pad with silence if it wasn't able to acquire enough data. | 479 // will pad with silence if it wasn't able to acquire enough data. |
| 459 return dest->frames(); | 480 return dest->frames(); |
| 460 } | 481 } |
| 461 | 482 |
| 462 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 483 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
| 463 uint32_t frames_delayed) { | 484 uint32_t frames_delayed) { |
| 464 base::TimeDelta new_delay = | 485 base::TimeDelta new_delay = |
| 465 current_delay_ + AudioTimestampHelper::FramesToTime( | 486 current_delay_ + AudioTimestampHelper::FramesToTime( |
| 466 frames_delayed, input_samples_per_second_); | 487 frames_delayed, input_samples_per_second_); |
| 467 // Retrieve data from the original callback. | 488 // Retrieve data from the original callback. |
| 468 const int frames = source_callback_->OnMoreData( | 489 const int frames = source_callback_->OnMoreData( |
| 469 new_delay, current_delay_timestamp_, 0, dest); | 490 new_delay, current_delay_timestamp_, 0, dest); |
| 470 | 491 |
| 471 // Zero any unfilled frames if anything was filled, otherwise we'll just | 492 // Zero any unfilled frames if anything was filled, otherwise we'll just |
| 472 // return a volume of zero and let AudioConverter drop the output. | 493 // return a volume of zero and let AudioConverter drop the output. |
| 473 if (frames > 0 && frames < dest->frames()) | 494 if (frames > 0 && frames < dest->frames()) |
| 474 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 495 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
| 475 return frames > 0 ? 1 : 0; | 496 return frames > 0 ? 1 : 0; |
| 476 } | 497 } |
| 477 | 498 |
| 478 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 499 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
| 479 error_occurred_ = true; | 500 error_occurred_ = true; |
| 480 source_callback_->OnError(stream); | 501 source_callback_->OnError(stream); |
| 481 } | 502 } |
| 482 | 503 |
| 483 } // namespace media | 504 } // namespace media |
| OLD | NEW |