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> | |
10 #include <string> | |
11 | |
9 #include "base/bind.h" | 12 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
11 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
12 #include "base/macros.h" | 15 #include "base/macros.h" |
13 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
14 #include "base/metrics/sparse_histogram.h" | 17 #include "base/metrics/sparse_histogram.h" |
15 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
16 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
17 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
18 #include "build/build_config.h" | 21 #include "build/build_config.h" |
19 #include "media/audio/audio_output_proxy.h" | 22 #include "media/audio/audio_output_proxy.h" |
20 #include "media/audio/sample_rates.h" | 23 #include "media/audio/sample_rates.h" |
21 #include "media/base/audio_converter.h" | 24 #include "media/base/audio_converter.h" |
22 #include "media/base/limits.h" | 25 #include "media/base/limits.h" |
23 | 26 |
24 namespace media { | 27 namespace media { |
25 | 28 |
26 class OnMoreDataConverter | 29 class OnMoreDataConverter |
27 : public AudioOutputStream::AudioSourceCallback, | 30 : public AudioOutputStream::AudioSourceCallback, |
28 public AudioConverter::InputCallback { | 31 public AudioConverter::InputCallback { |
29 public: | 32 public: |
30 OnMoreDataConverter(const AudioParameters& input_params, | 33 OnMoreDataConverter(const AudioParameters& input_params, |
31 const AudioParameters& output_params); | 34 const AudioParameters& output_params); |
32 ~OnMoreDataConverter() override; | 35 ~OnMoreDataConverter() override; |
33 | 36 |
34 // AudioSourceCallback interface. | 37 // AudioSourceCallback interface. |
35 int OnMoreData(AudioBus* dest, | 38 int OnMoreData(base::TimeDelta delay, |
36 uint32_t total_bytes_delay, | 39 base::TimeTicks delay_timestamp, |
37 uint32_t frames_skipped) override; | 40 int prior_frames_skipped, |
41 AudioBus* dest) override; | |
38 void OnError(AudioOutputStream* stream) override; | 42 void OnError(AudioOutputStream* stream) override; |
39 | 43 |
40 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 44 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
41 // called before Start(). | 45 // called before Start(). |
42 void Start(AudioOutputStream::AudioSourceCallback* callback); | 46 void Start(AudioOutputStream::AudioSourceCallback* callback); |
43 | 47 |
44 // Clears |source_callback_| and flushes the resampler. | 48 // Clears |source_callback_| and flushes the resampler. |
45 void Stop(); | 49 void Stop(); |
46 | 50 |
47 bool started() const { return source_callback_ != nullptr; } | 51 bool started() const { return source_callback_ != nullptr; } |
48 | 52 |
49 bool error_occurred() const { return error_occurred_; } | 53 bool error_occurred() const { return error_occurred_; } |
50 | 54 |
51 private: | 55 private: |
52 // AudioConverter::InputCallback implementation. | 56 // AudioConverter::InputCallback implementation. |
53 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; | 57 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
54 | 58 |
55 // Ratio of input bytes to output bytes used to correct playback delay with | 59 // Ratio of input bytes to output bytes used to correct playback delay with |
56 // regard to buffering and resampling. | 60 // regard to buffering and resampling. |
57 const double io_ratio_; | 61 const double io_ratio_; |
58 | 62 |
59 // Source callback. | 63 // Source callback. |
60 AudioOutputStream::AudioSourceCallback* source_callback_; | 64 AudioOutputStream::AudioSourceCallback* source_callback_; |
61 | 65 |
62 // Last |total_bytes_delay| received via OnMoreData(), used to correct | 66 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to |
63 // playback delay by ProvideInput() and passed on to |source_callback_|. | 67 // correct playback delay in ProvideInput() before calling |source_callback_|. |
64 uint32_t current_total_bytes_delay_; | 68 base::TimeDelta current_delay_; |
69 base::TimeTicks current_delay_timestamp_; | |
65 | 70 |
66 const int input_bytes_per_frame_; | 71 const int input_samples_per_second_; |
67 | 72 |
68 // Handles resampling, buffering, and channel mixing between input and output | 73 // Handles resampling, buffering, and channel mixing between input and output |
69 // parameters. | 74 // parameters. |
70 AudioConverter audio_converter_; | 75 AudioConverter audio_converter_; |
71 | 76 |
72 // True if OnError() was ever called. Should only be read if the underlying | 77 // True if OnError() was ever called. Should only be read if the underlying |
73 // stream has been stopped. | 78 // stream has been stopped. |
74 bool error_occurred_; | 79 bool error_occurred_; |
75 | 80 |
76 // Information about input and output buffer sizes to be traced. | 81 // Information about input and output buffer sizes to be traced. |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
394 | 399 |
395 dispatcher_->Shutdown(); | 400 dispatcher_->Shutdown(); |
396 DCHECK(callbacks_.empty()); | 401 DCHECK(callbacks_.empty()); |
397 } | 402 } |
398 | 403 |
399 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 404 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
400 const AudioParameters& output_params) | 405 const AudioParameters& output_params) |
401 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 406 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
402 output_params.GetBytesPerSecond()), | 407 output_params.GetBytesPerSecond()), |
403 source_callback_(nullptr), | 408 source_callback_(nullptr), |
404 input_bytes_per_frame_(input_params.GetBytesPerFrame()), | 409 input_samples_per_second_(input_params.sample_rate()), |
405 audio_converter_(input_params, output_params, false), | 410 audio_converter_(input_params, output_params, false), |
406 error_occurred_(false), | 411 error_occurred_(false), |
407 input_buffer_size_(input_params.frames_per_buffer()), | 412 input_buffer_size_(input_params.frames_per_buffer()), |
408 output_buffer_size_(output_params.frames_per_buffer()) { | 413 output_buffer_size_(output_params.frames_per_buffer()) { |
409 RecordRebufferingStats(input_params, output_params); | 414 RecordRebufferingStats(input_params, output_params); |
410 } | 415 } |
411 | 416 |
412 OnMoreDataConverter::~OnMoreDataConverter() { | 417 OnMoreDataConverter::~OnMoreDataConverter() { |
413 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 418 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
414 // calling back into OnMoreData() after destruction. | 419 // calling back into OnMoreData() after destruction. |
(...skipping 10 matching lines...) Expand all Loading... | |
425 // side mixer. | 430 // side mixer. |
426 audio_converter_.AddInput(this); | 431 audio_converter_.AddInput(this); |
427 } | 432 } |
428 | 433 |
429 void OnMoreDataConverter::Stop() { | 434 void OnMoreDataConverter::Stop() { |
430 CHECK(source_callback_); | 435 CHECK(source_callback_); |
431 source_callback_ = nullptr; | 436 source_callback_ = nullptr; |
432 audio_converter_.RemoveInput(this); | 437 audio_converter_.RemoveInput(this); |
433 } | 438 } |
434 | 439 |
435 int OnMoreDataConverter::OnMoreData(AudioBus* dest, | 440 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay, |
436 uint32_t total_bytes_delay, | 441 base::TimeTicks delay_timestamp, |
437 uint32_t frames_skipped) { | 442 int /* prior_frames_skipped */, |
443 AudioBus* dest) { | |
438 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", | 444 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", |
439 input_buffer_size_, "output buffer size", output_buffer_size_); | 445 input_buffer_size_, "output buffer size", output_buffer_size_); |
440 current_total_bytes_delay_ = total_bytes_delay; | 446 current_delay_ = delay; |
447 current_delay_timestamp_ = delay_timestamp; | |
441 audio_converter_.Convert(dest); | 448 audio_converter_.Convert(dest); |
442 | 449 |
443 // Always return the full number of frames requested, ProvideInput() | 450 // Always return the full number of frames requested, ProvideInput() |
444 // will pad with silence if it wasn't able to acquire enough data. | 451 // will pad with silence if it wasn't able to acquire enough data. |
445 return dest->frames(); | 452 return dest->frames(); |
446 } | 453 } |
447 | 454 |
448 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 455 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
449 uint32_t frames_delayed) { | 456 uint32_t frames_delayed) { |
450 // Adjust playback delay to include |frames_delayed|. | 457 base::TimeDelta new_delay = |
451 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 458 current_delay_ + base::TimeDelta::FromMicroseconds( |
452 // AudioBus is just float data. Use TimeDelta instead. | 459 frames_delayed * base::Time::kMicrosecondsPerSecond / |
chcunningham
2016/09/23 20:53:30
ditto about int division
jameswest
2016/09/29 00:52:24
Moved to utility function.
| |
453 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( | 460 input_samples_per_second_); |
454 io_ratio_ * | |
455 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); | |
456 | |
457 // Retrieve data from the original callback. | 461 // Retrieve data from the original callback. |
458 const int frames = | 462 const int frames = source_callback_->OnMoreData( |
459 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | 463 new_delay, current_delay_timestamp_, 0, dest); |
460 | 464 |
461 // Zero any unfilled frames if anything was filled, otherwise we'll just | 465 // Zero any unfilled frames if anything was filled, otherwise we'll just |
462 // return a volume of zero and let AudioConverter drop the output. | 466 // return a volume of zero and let AudioConverter drop the output. |
463 if (frames > 0 && frames < dest->frames()) | 467 if (frames > 0 && frames < dest->frames()) |
464 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 468 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
465 return frames > 0 ? 1 : 0; | 469 return frames > 0 ? 1 : 0; |
466 } | 470 } |
467 | 471 |
468 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 472 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
469 error_occurred_ = true; | 473 error_occurred_ = true; |
470 source_callback_->OnError(stream); | 474 source_callback_->OnError(stream); |
471 } | 475 } |
472 | 476 |
473 } // namespace media | 477 } // namespace media |
OLD | NEW |