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 14 matching lines...) Expand all Loading... |
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 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( |
36 const AudioParameters& output_params); | 36 const AudioParameters& input_params, |
| 37 const AudioParameters& output_params, |
| 38 AudioDebugRecorderWrapperUniquePtr debug_recorder_wrapper); |
37 ~OnMoreDataConverter() override; | 39 ~OnMoreDataConverter() override; |
38 | 40 |
39 // AudioSourceCallback interface. | 41 // AudioSourceCallback interface. |
40 int OnMoreData(base::TimeDelta delay, | 42 int OnMoreData(base::TimeDelta delay, |
41 base::TimeTicks delay_timestamp, | 43 base::TimeTicks delay_timestamp, |
42 int prior_frames_skipped, | 44 int prior_frames_skipped, |
43 AudioBus* dest) override; | 45 AudioBus* dest) override; |
44 void OnError(AudioOutputStream* stream) override; | 46 void OnError(AudioOutputStream* stream) override; |
45 | 47 |
46 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 48 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
47 // called before Start(). | 49 // called before Start(). |
48 void Start(AudioOutputStream::AudioSourceCallback* callback); | 50 void Start(AudioOutputStream::AudioSourceCallback* callback); |
49 | 51 |
50 // Clears |source_callback_| and flushes the resampler. | 52 // Clears |source_callback_| and flushes the resampler. |
51 void Stop(); | 53 void Stop(); |
52 | 54 |
53 bool started() const { return source_callback_ != nullptr; } | 55 bool started() const { return source_callback_ != nullptr; } |
54 | 56 |
55 bool error_occurred() const { return error_occurred_; } | 57 bool error_occurred() const { return error_occurred_; } |
56 | 58 |
| 59 const AudioParameters& output_params() const { return output_params_; } |
| 60 |
57 private: | 61 private: |
58 // AudioConverter::InputCallback implementation. | 62 // AudioConverter::InputCallback implementation. |
59 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; | 63 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
60 | 64 |
61 // Ratio of input bytes to output bytes used to correct playback delay with | 65 // Ratio of input bytes to output bytes used to correct playback delay with |
62 // regard to buffering and resampling. | 66 // regard to buffering and resampling. |
63 const double io_ratio_; | 67 const double io_ratio_; |
64 | 68 |
65 // Source callback. | 69 // Source callback. |
66 AudioOutputStream::AudioSourceCallback* source_callback_; | 70 AudioOutputStream::AudioSourceCallback* source_callback_; |
67 | 71 |
68 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to | 72 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to |
69 // correct playback delay in ProvideInput() before calling |source_callback_|. | 73 // correct playback delay in ProvideInput() before calling |source_callback_|. |
70 base::TimeDelta current_delay_; | 74 base::TimeDelta current_delay_; |
71 base::TimeTicks current_delay_timestamp_; | 75 base::TimeTicks current_delay_timestamp_; |
72 | 76 |
73 const int input_samples_per_second_; | 77 const int input_samples_per_second_; |
74 | 78 |
75 // Handles resampling, buffering, and channel mixing between input and output | 79 // Handles resampling, buffering, and channel mixing between input and output |
76 // parameters. | 80 // parameters. |
77 AudioConverter audio_converter_; | 81 AudioConverter audio_converter_; |
78 | 82 |
79 // True if OnError() was ever called. Should only be read if the underlying | 83 // True if OnError() was ever called. Should only be read if the underlying |
80 // stream has been stopped. | 84 // stream has been stopped. |
81 bool error_occurred_; | 85 bool error_occurred_; |
82 | 86 |
83 // Information about input and output buffer sizes to be traced. | 87 // Information about input buffer sizes to be traced. |
84 const int input_buffer_size_; | 88 const int input_buffer_size_; |
85 const int output_buffer_size_; | 89 |
| 90 // Output parameters used for buffer size tracing and for users to get. |
| 91 const AudioParameters output_params_; |
| 92 |
| 93 // Used for audio debug recordings. See comment on SetDebugRecordingCallback() |
| 94 // above. |id_| is a unique running id used in the callback. |
| 95 AudioDebugRecorderWrapperUniquePtr debug_recorder_wrapper_; |
86 | 96 |
87 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 97 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
88 }; | 98 }; |
89 | 99 |
90 // Record UMA statistics for hardware output configuration. | 100 // Record UMA statistics for hardware output configuration. |
91 static void RecordStats(const AudioParameters& output_params) { | 101 static void RecordStats(const AudioParameters& output_params) { |
92 // Note the 'PRESUBMIT_IGNORE_UMA_MAX's below, these silence the PRESUBMIT.py | 102 // 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 | 103 // check for uma enum max usage, since we're abusing UMA_HISTOGRAM_ENUMERATION |
94 // to report a discrete value. | 104 // to report a discrete value. |
95 UMA_HISTOGRAM_ENUMERATION( | 105 UMA_HISTOGRAM_ENUMERATION( |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 218 |
209 output_params_ = AudioParameters( | 219 output_params_ = AudioParameters( |
210 AudioParameters::AUDIO_PCM_LINEAR, params_.channel_layout(), | 220 AudioParameters::AUDIO_PCM_LINEAR, params_.channel_layout(), |
211 params_.sample_rate(), params_.bits_per_sample(), | 221 params_.sample_rate(), params_.bits_per_sample(), |
212 frames_per_buffer); | 222 frames_per_buffer); |
213 device_id_ = ""; | 223 device_id_ = ""; |
214 Initialize(); | 224 Initialize(); |
215 #endif | 225 #endif |
216 } | 226 } |
217 | 227 |
218 AudioOutputResampler::AudioOutputResampler(AudioManager* audio_manager, | 228 AudioOutputResampler::AudioOutputResampler( |
219 const AudioParameters& input_params, | 229 AudioManager* audio_manager, |
220 const AudioParameters& output_params, | 230 const AudioParameters& input_params, |
221 const std::string& output_device_id, | 231 const AudioParameters& output_params, |
222 const base::TimeDelta& close_delay) | 232 const std::string& output_device_id, |
| 233 base::TimeDelta close_delay, |
| 234 const RegisterDebugRecordingSourceCallback& |
| 235 register_debug_recording_source_callback) |
223 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), | 236 : AudioOutputDispatcher(audio_manager, input_params, output_device_id), |
224 close_delay_(close_delay), | 237 close_delay_(close_delay), |
225 output_params_(output_params), | 238 output_params_(output_params), |
226 original_output_params_(output_params), | 239 original_output_params_(output_params), |
227 streams_opened_(false), | 240 streams_opened_(false), |
228 reinitialize_timer_(FROM_HERE, | 241 reinitialize_timer_(FROM_HERE, |
229 close_delay_, | 242 close_delay_, |
230 base::Bind(&AudioOutputResampler::Reinitialize, | 243 base::Bind(&AudioOutputResampler::Reinitialize, |
231 base::Unretained(this)), | 244 base::Unretained(this)), |
232 false), | 245 false), |
| 246 register_debug_recording_source_callback_( |
| 247 register_debug_recording_source_callback), |
233 weak_factory_(this) { | 248 weak_factory_(this) { |
234 DCHECK(input_params.IsValid()); | 249 DCHECK(input_params.IsValid()); |
235 DCHECK(output_params.IsValid()); | 250 DCHECK(output_params.IsValid()); |
236 DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY); | 251 DCHECK_EQ(output_params_.format(), AudioParameters::AUDIO_PCM_LOW_LATENCY); |
237 | 252 |
238 // Record UMA statistics for the hardware configuration. | 253 // Record UMA statistics for the hardware configuration. |
239 RecordStats(output_params); | 254 RecordStats(output_params); |
240 | 255 |
241 Initialize(); | 256 Initialize(); |
242 } | 257 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 } | 349 } |
335 | 350 |
336 bool AudioOutputResampler::StartStream( | 351 bool AudioOutputResampler::StartStream( |
337 AudioOutputStream::AudioSourceCallback* callback, | 352 AudioOutputStream::AudioSourceCallback* callback, |
338 AudioOutputProxy* stream_proxy) { | 353 AudioOutputProxy* stream_proxy) { |
339 DCHECK(task_runner_->BelongsToCurrentThread()); | 354 DCHECK(task_runner_->BelongsToCurrentThread()); |
340 | 355 |
341 OnMoreDataConverter* resampler_callback = nullptr; | 356 OnMoreDataConverter* resampler_callback = nullptr; |
342 CallbackMap::iterator it = callbacks_.find(stream_proxy); | 357 CallbackMap::iterator it = callbacks_.find(stream_proxy); |
343 if (it == callbacks_.end()) { | 358 if (it == callbacks_.end()) { |
344 resampler_callback = new OnMoreDataConverter(params_, output_params_); | 359 AudioDebugRecorderWrapperUniquePtr recorder_wrapper = |
| 360 register_debug_recording_source_callback_.Run(output_params_); |
| 361 resampler_callback = new OnMoreDataConverter(params_, output_params_, |
| 362 std::move(recorder_wrapper)); |
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 // Now that StopStream() has completed the underlying physical stream should | 417 // Now that StopStream() has completed the underlying physical stream should |
400 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the | 418 // be stopped and no longer calling OnMoreData(), making it safe to Stop() the |
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(); |
| 427 |
| 428 // TODO BEFORE COMMIT: I think we need to clear the recorder wrapper here and |
| 429 // register again at start. Seems like converters are re-used. |
409 } | 430 } |
410 | 431 |
411 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 432 OnMoreDataConverter::OnMoreDataConverter( |
412 const AudioParameters& output_params) | 433 const AudioParameters& input_params, |
| 434 const AudioParameters& output_params, |
| 435 AudioDebugRecorderWrapperUniquePtr debug_recorder_wrapper) |
413 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 436 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
414 output_params.GetBytesPerSecond()), | 437 output_params.GetBytesPerSecond()), |
415 source_callback_(nullptr), | 438 source_callback_(nullptr), |
416 input_samples_per_second_(input_params.sample_rate()), | 439 input_samples_per_second_(input_params.sample_rate()), |
417 audio_converter_(input_params, output_params, false), | 440 audio_converter_(input_params, output_params, false), |
418 error_occurred_(false), | 441 error_occurred_(false), |
419 input_buffer_size_(input_params.frames_per_buffer()), | 442 input_buffer_size_(input_params.frames_per_buffer()), |
420 output_buffer_size_(output_params.frames_per_buffer()) { | 443 output_params_(output_params), |
| 444 debug_recorder_wrapper_(std::move(debug_recorder_wrapper)) { |
421 RecordRebufferingStats(input_params, output_params); | 445 RecordRebufferingStats(input_params, output_params); |
422 } | 446 } |
423 | 447 |
424 OnMoreDataConverter::~OnMoreDataConverter() { | 448 OnMoreDataConverter::~OnMoreDataConverter() { |
425 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 449 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
426 // calling back into OnMoreData() after destruction. | 450 // calling back into OnMoreData() after destruction. |
427 CHECK(!source_callback_); | 451 CHECK(!source_callback_); |
428 } | 452 } |
429 | 453 |
430 void OnMoreDataConverter::Start( | 454 void OnMoreDataConverter::Start( |
(...skipping 11 matching lines...) Expand all Loading... |
442 CHECK(source_callback_); | 466 CHECK(source_callback_); |
443 source_callback_ = nullptr; | 467 source_callback_ = nullptr; |
444 audio_converter_.RemoveInput(this); | 468 audio_converter_.RemoveInput(this); |
445 } | 469 } |
446 | 470 |
447 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, | 471 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, |
448 base::TimeTicks delay_timestamp, | 472 base::TimeTicks delay_timestamp, |
449 int /* prior_frames_skipped */, | 473 int /* prior_frames_skipped */, |
450 AudioBus* dest) { | 474 AudioBus* dest) { |
451 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", | 475 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", |
452 input_buffer_size_, "output buffer size", output_buffer_size_); | 476 input_buffer_size_, "output buffer size", |
| 477 output_params_.frames_per_buffer()); |
453 current_delay_ = delay; | 478 current_delay_ = delay; |
454 current_delay_timestamp_ = delay_timestamp; | 479 current_delay_timestamp_ = delay_timestamp; |
455 audio_converter_.Convert(dest); | 480 audio_converter_.Convert(dest); |
456 | 481 |
| 482 debug_recorder_wrapper_->recorder->OnData(dest); |
| 483 |
457 // Always return the full number of frames requested, ProvideInput() | 484 // Always return the full number of frames requested, ProvideInput() |
458 // will pad with silence if it wasn't able to acquire enough data. | 485 // will pad with silence if it wasn't able to acquire enough data. |
459 return dest->frames(); | 486 return dest->frames(); |
460 } | 487 } |
461 | 488 |
462 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 489 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
463 uint32_t frames_delayed) { | 490 uint32_t frames_delayed) { |
464 base::TimeDelta new_delay = | 491 base::TimeDelta new_delay = |
465 current_delay_ + AudioTimestampHelper::FramesToTime( | 492 current_delay_ + AudioTimestampHelper::FramesToTime( |
466 frames_delayed, input_samples_per_second_); | 493 frames_delayed, input_samples_per_second_); |
467 // Retrieve data from the original callback. | 494 // Retrieve data from the original callback. |
468 const int frames = source_callback_->OnMoreData( | 495 const int frames = source_callback_->OnMoreData( |
469 new_delay, current_delay_timestamp_, 0, dest); | 496 new_delay, current_delay_timestamp_, 0, dest); |
470 | 497 |
471 // Zero any unfilled frames if anything was filled, otherwise we'll just | 498 // Zero any unfilled frames if anything was filled, otherwise we'll just |
472 // return a volume of zero and let AudioConverter drop the output. | 499 // return a volume of zero and let AudioConverter drop the output. |
473 if (frames > 0 && frames < dest->frames()) | 500 if (frames > 0 && frames < dest->frames()) |
474 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 501 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
475 return frames > 0 ? 1 : 0; | 502 return frames > 0 ? 1 : 0; |
476 } | 503 } |
477 | 504 |
478 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 505 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
479 error_occurred_ = true; | 506 error_occurred_ = true; |
480 source_callback_->OnError(stream); | 507 source_callback_->OnError(stream); |
481 } | 508 } |
482 | 509 |
483 } // namespace media | 510 } // namespace media |
OLD | NEW |