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); |
237 | 246 |
238 // Record UMA statistics for the hardware configuration. | 247 // Record UMA statistics for the hardware configuration. |
239 RecordStats(output_params); | 248 RecordStats(output_params); |
240 | 249 |
241 Initialize(); | 250 Initialize(); |
242 } | 251 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
334 } | 343 } |
335 | 344 |
336 bool AudioOutputResampler::StartStream( | 345 bool AudioOutputResampler::StartStream( |
337 AudioOutputStream::AudioSourceCallback* callback, | 346 AudioOutputStream::AudioSourceCallback* callback, |
338 AudioOutputProxy* stream_proxy) { | 347 AudioOutputProxy* stream_proxy) { |
339 DCHECK(task_runner_->BelongsToCurrentThread()); | 348 DCHECK(task_runner_->BelongsToCurrentThread()); |
340 | 349 |
341 OnMoreDataConverter* resampler_callback = nullptr; | 350 OnMoreDataConverter* resampler_callback = nullptr; |
342 CallbackMap::iterator it = callbacks_.find(stream_proxy); | 351 CallbackMap::iterator it = callbacks_.find(stream_proxy); |
343 if (it == callbacks_.end()) { | 352 if (it == callbacks_.end()) { |
344 resampler_callback = new OnMoreDataConverter(params_, output_params_); | 353 // If a register callback has been given, register and pass the returned |
354 // recoder to the converter. Data is fed to same recorder for the lifetime | |
355 // of the converter, which is until the stream is closed. | |
356 std::unique_ptr<AudioDebugRecorder> debug_recorder; | |
357 if (register_debug_recording_source_callback_) { | |
358 debug_recorder = | |
359 register_debug_recording_source_callback_.Run(output_params_); | |
360 } | |
361 resampler_callback = new OnMoreDataConverter(params_, output_params_, | |
362 std::move(debug_recorder)); | |
o1ka
2017/02/09 13:04:03
Just
OnMoreDataConverter(params_, output_params_,
Henrik Grunell
2017/02/10 09:00:56
Sounds good, changed to always running the callbac
| |
345 callbacks_[stream_proxy] = | 363 callbacks_[stream_proxy] = |
346 base::WrapUnique<OnMoreDataConverter>(resampler_callback); | 364 base::WrapUnique<OnMoreDataConverter>(resampler_callback); |
347 } else { | 365 } else { |
348 resampler_callback = it->second.get(); | 366 resampler_callback = it->second.get(); |
349 } | 367 } |
350 | 368 |
351 resampler_callback->Start(callback); | 369 resampler_callback->Start(callback); |
352 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); | 370 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); |
353 if (!result) | 371 if (!result) |
354 resampler_callback->Stop(); | 372 resampler_callback->Stop(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 // OnMoreDataConverter. | 419 // OnMoreDataConverter. |
402 callback->Stop(); | 420 callback->Stop(); |
403 | 421 |
404 // Destroy idle streams if any errors occurred during output; this ensures | 422 // 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() | 423 // bad streams will not be reused. Note: Errors may occur during the Stop() |
406 // call above. | 424 // call above. |
407 if (callback->error_occurred()) | 425 if (callback->error_occurred()) |
408 dispatcher_->CloseAllIdleStreams(); | 426 dispatcher_->CloseAllIdleStreams(); |
409 } | 427 } |
410 | 428 |
411 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 429 OnMoreDataConverter::OnMoreDataConverter( |
412 const AudioParameters& output_params) | 430 const AudioParameters& input_params, |
431 const AudioParameters& output_params, | |
432 std::unique_ptr<AudioDebugRecorder> debug_recorder) | |
413 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 433 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
414 output_params.GetBytesPerSecond()), | 434 output_params.GetBytesPerSecond()), |
415 source_callback_(nullptr), | 435 source_callback_(nullptr), |
416 input_samples_per_second_(input_params.sample_rate()), | 436 input_samples_per_second_(input_params.sample_rate()), |
417 audio_converter_(input_params, output_params, false), | 437 audio_converter_(input_params, output_params, false), |
418 error_occurred_(false), | 438 error_occurred_(false), |
419 input_buffer_size_(input_params.frames_per_buffer()), | 439 input_buffer_size_(input_params.frames_per_buffer()), |
420 output_buffer_size_(output_params.frames_per_buffer()) { | 440 output_buffer_size_(output_params.frames_per_buffer()), |
441 debug_recorder_(std::move(debug_recorder)) { | |
421 RecordRebufferingStats(input_params, output_params); | 442 RecordRebufferingStats(input_params, output_params); |
422 } | 443 } |
423 | 444 |
424 OnMoreDataConverter::~OnMoreDataConverter() { | 445 OnMoreDataConverter::~OnMoreDataConverter() { |
425 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 446 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
426 // calling back into OnMoreData() after destruction. | 447 // calling back into OnMoreData() after destruction. |
427 CHECK(!source_callback_); | 448 CHECK(!source_callback_); |
428 } | 449 } |
429 | 450 |
430 void OnMoreDataConverter::Start( | 451 void OnMoreDataConverter::Start( |
(...skipping 16 matching lines...) Expand all Loading... | |
447 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, | 468 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, |
448 base::TimeTicks delay_timestamp, | 469 base::TimeTicks delay_timestamp, |
449 int /* prior_frames_skipped */, | 470 int /* prior_frames_skipped */, |
450 AudioBus* dest) { | 471 AudioBus* dest) { |
451 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", | 472 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", |
452 input_buffer_size_, "output buffer size", output_buffer_size_); | 473 input_buffer_size_, "output buffer size", output_buffer_size_); |
453 current_delay_ = delay; | 474 current_delay_ = delay; |
454 current_delay_timestamp_ = delay_timestamp; | 475 current_delay_timestamp_ = delay_timestamp; |
455 audio_converter_.Convert(dest); | 476 audio_converter_.Convert(dest); |
456 | 477 |
478 if (debug_recorder_) | |
479 debug_recorder_->OnData(dest); | |
480 | |
457 // Always return the full number of frames requested, ProvideInput() | 481 // Always return the full number of frames requested, ProvideInput() |
458 // will pad with silence if it wasn't able to acquire enough data. | 482 // will pad with silence if it wasn't able to acquire enough data. |
459 return dest->frames(); | 483 return dest->frames(); |
460 } | 484 } |
461 | 485 |
462 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 486 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
463 uint32_t frames_delayed) { | 487 uint32_t frames_delayed) { |
464 base::TimeDelta new_delay = | 488 base::TimeDelta new_delay = |
465 current_delay_ + AudioTimestampHelper::FramesToTime( | 489 current_delay_ + AudioTimestampHelper::FramesToTime( |
466 frames_delayed, input_samples_per_second_); | 490 frames_delayed, input_samples_per_second_); |
467 // Retrieve data from the original callback. | 491 // Retrieve data from the original callback. |
468 const int frames = source_callback_->OnMoreData( | 492 const int frames = source_callback_->OnMoreData( |
469 new_delay, current_delay_timestamp_, 0, dest); | 493 new_delay, current_delay_timestamp_, 0, dest); |
470 | 494 |
471 // Zero any unfilled frames if anything was filled, otherwise we'll just | 495 // Zero any unfilled frames if anything was filled, otherwise we'll just |
472 // return a volume of zero and let AudioConverter drop the output. | 496 // return a volume of zero and let AudioConverter drop the output. |
473 if (frames > 0 && frames < dest->frames()) | 497 if (frames > 0 && frames < dest->frames()) |
474 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 498 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
475 return frames > 0 ? 1 : 0; | 499 return frames > 0 ? 1 : 0; |
476 } | 500 } |
477 | 501 |
478 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 502 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
479 error_occurred_ = true; | 503 error_occurred_ = true; |
480 source_callback_->OnError(stream); | 504 source_callback_->OnError(stream); |
481 } | 505 } |
482 | 506 |
483 } // namespace media | 507 } // namespace media |
OLD | NEW |