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.h" | 16 #include "base/metrics/histogram.h" |
14 #include "base/numerics/safe_conversions.h" | 17 #include "base/numerics/safe_conversions.h" |
15 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
16 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
17 #include "build/build_config.h" | 20 #include "build/build_config.h" |
18 #include "media/audio/audio_output_proxy.h" | 21 #include "media/audio/audio_output_proxy.h" |
19 #include "media/audio/sample_rates.h" | 22 #include "media/audio/sample_rates.h" |
20 #include "media/base/audio_converter.h" | 23 #include "media/base/audio_converter.h" |
21 #include "media/base/limits.h" | 24 #include "media/base/limits.h" |
22 | 25 |
23 namespace media { | 26 namespace media { |
24 | 27 |
25 class OnMoreDataConverter | 28 class OnMoreDataConverter |
26 : public AudioOutputStream::AudioSourceCallback, | 29 : public AudioOutputStream::AudioSourceCallback, |
27 public AudioConverter::InputCallback { | 30 public AudioConverter::InputCallback { |
28 public: | 31 public: |
29 OnMoreDataConverter(const AudioParameters& input_params, | 32 OnMoreDataConverter(const AudioParameters& input_params, |
30 const AudioParameters& output_params); | 33 const AudioParameters& output_params); |
31 ~OnMoreDataConverter() override; | 34 ~OnMoreDataConverter() override; |
32 | 35 |
33 // AudioSourceCallback interface. | 36 // AudioSourceCallback interface. |
34 int OnMoreData(AudioBus* dest, | 37 int OnMoreData(base::TimeTicks target_playout_time, |
35 uint32_t total_bytes_delay, | 38 int prior_frames_skipped, |
36 uint32_t frames_skipped) override; | 39 AudioBus* dest) override; |
37 void OnError(AudioOutputStream* stream) override; | 40 void OnError(AudioOutputStream* stream) override; |
38 | 41 |
39 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 42 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
40 // called before Start(). | 43 // called before Start(). |
41 void Start(AudioOutputStream::AudioSourceCallback* callback); | 44 void Start(AudioOutputStream::AudioSourceCallback* callback); |
42 | 45 |
43 // Clears |source_callback_| and flushes the resampler. | 46 // Clears |source_callback_| and flushes the resampler. |
44 void Stop(); | 47 void Stop(); |
45 | 48 |
46 bool started() const { return source_callback_ != nullptr; } | 49 bool started() const { return source_callback_ != nullptr; } |
47 | 50 |
48 bool error_occurred() const { return error_occurred_; } | 51 bool error_occurred() const { return error_occurred_; } |
49 | 52 |
50 private: | 53 private: |
51 // AudioConverter::InputCallback implementation. | 54 // AudioConverter::InputCallback implementation. |
52 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; | 55 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
53 | 56 |
54 // Ratio of input bytes to output bytes used to correct playback delay with | 57 // Ratio of input bytes to output bytes used to correct playback delay with |
55 // regard to buffering and resampling. | 58 // regard to buffering and resampling. |
56 const double io_ratio_; | 59 const double io_ratio_; |
57 | 60 |
58 // Source callback. | 61 // Source callback. |
59 AudioOutputStream::AudioSourceCallback* source_callback_; | 62 AudioOutputStream::AudioSourceCallback* source_callback_; |
60 | 63 |
61 // Last |total_bytes_delay| received via OnMoreData(), used to correct | 64 // Last |target_playout_time| received via OnMoreData() and passed on to |
62 // playback delay by ProvideInput() and passed on to |source_callback_|. | 65 // |source_callback_|. |
63 uint32_t current_total_bytes_delay_; | 66 base::TimeTicks target_playout_time_; |
64 | 67 |
65 const int input_bytes_per_frame_; | 68 const int input_bytes_per_frame_; |
66 | 69 |
67 // Handles resampling, buffering, and channel mixing between input and output | 70 // Handles resampling, buffering, and channel mixing between input and output |
68 // parameters. | 71 // parameters. |
69 AudioConverter audio_converter_; | 72 AudioConverter audio_converter_; |
70 | 73 |
71 // True if OnError() was ever called. Should only be read if the underlying | 74 // True if OnError() was ever called. Should only be read if the underlying |
72 // stream has been stopped. | 75 // stream has been stopped. |
73 bool error_occurred_; | 76 bool error_occurred_; |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
366 // side mixer. | 369 // side mixer. |
367 audio_converter_.AddInput(this); | 370 audio_converter_.AddInput(this); |
368 } | 371 } |
369 | 372 |
370 void OnMoreDataConverter::Stop() { | 373 void OnMoreDataConverter::Stop() { |
371 CHECK(source_callback_); | 374 CHECK(source_callback_); |
372 source_callback_ = nullptr; | 375 source_callback_ = nullptr; |
373 audio_converter_.RemoveInput(this); | 376 audio_converter_.RemoveInput(this); |
374 } | 377 } |
375 | 378 |
376 int OnMoreDataConverter::OnMoreData(AudioBus* dest, | 379 int OnMoreDataConverter::OnMoreData(base::TimeTicks target_playout_time, |
377 uint32_t total_bytes_delay, | 380 int /* prior_frames_skipped */, |
378 uint32_t frames_skipped) { | 381 AudioBus* dest) { |
379 current_total_bytes_delay_ = total_bytes_delay; | 382 target_playout_time_ = target_playout_time; |
380 audio_converter_.Convert(dest); | 383 audio_converter_.Convert(dest); |
381 | 384 |
382 // Always return the full number of frames requested, ProvideInput() | 385 // Always return the full number of frames requested, ProvideInput() |
383 // will pad with silence if it wasn't able to acquire enough data. | 386 // will pad with silence if it wasn't able to acquire enough data. |
384 return dest->frames(); | 387 return dest->frames(); |
385 } | 388 } |
386 | 389 |
387 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 390 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
388 uint32_t frames_delayed) { | 391 uint32_t frames_delayed) { |
389 // Adjust playback delay to include |frames_delayed|. | |
390 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | |
chcunningham
2016/07/29 01:21:09
Why is it ok for you to just drop frames_delayed?
jameswest
2016/08/26 02:08:47
Done.
| |
391 // AudioBus is just float data. Use TimeDelta instead. | |
392 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( | |
393 io_ratio_ * | |
394 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); | |
395 | |
396 // Retrieve data from the original callback. | 392 // Retrieve data from the original callback. |
397 const int frames = | 393 const int frames = |
398 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | 394 source_callback_->OnMoreData(target_playout_time_, 0, dest); |
399 | 395 |
400 // Zero any unfilled frames if anything was filled, otherwise we'll just | 396 // Zero any unfilled frames if anything was filled, otherwise we'll just |
401 // return a volume of zero and let AudioConverter drop the output. | 397 // return a volume of zero and let AudioConverter drop the output. |
402 if (frames > 0 && frames < dest->frames()) | 398 if (frames > 0 && frames < dest->frames()) |
403 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 399 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
404 return frames > 0 ? 1 : 0; | 400 return frames > 0 ? 1 : 0; |
405 } | 401 } |
406 | 402 |
407 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 403 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
408 error_occurred_ = true; | 404 error_occurred_ = true; |
409 source_callback_->OnError(stream); | 405 source_callback_->OnError(stream); |
410 } | 406 } |
411 | 407 |
412 } // namespace media | 408 } // namespace media |
OLD | NEW |