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