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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/numerics/safe_conversions.h" |
11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
14 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
15 #include "media/audio/audio_output_dispatcher_impl.h" | 16 #include "media/audio/audio_output_dispatcher_impl.h" |
16 #include "media/audio/audio_output_proxy.h" | 17 #include "media/audio/audio_output_proxy.h" |
17 #include "media/audio/sample_rates.h" | 18 #include "media/audio/sample_rates.h" |
18 #include "media/base/audio_converter.h" | 19 #include "media/base/audio_converter.h" |
19 #include "media/base/limits.h" | 20 #include "media/base/limits.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 | 23 |
23 class OnMoreDataConverter | 24 class OnMoreDataConverter |
24 : public AudioOutputStream::AudioSourceCallback, | 25 : public AudioOutputStream::AudioSourceCallback, |
25 public AudioConverter::InputCallback { | 26 public AudioConverter::InputCallback { |
26 public: | 27 public: |
27 OnMoreDataConverter(const AudioParameters& input_params, | 28 OnMoreDataConverter(const AudioParameters& input_params, |
28 const AudioParameters& output_params); | 29 const AudioParameters& output_params); |
29 virtual ~OnMoreDataConverter(); | 30 virtual ~OnMoreDataConverter(); |
30 | 31 |
31 // AudioSourceCallback interface. | 32 // AudioSourceCallback interface. |
32 virtual int OnMoreData(AudioBus* dest, | 33 virtual int OnMoreData(AudioBus* dest, |
33 AudioBuffersState buffers_state) OVERRIDE; | 34 uint32 total_bytes_delay) OVERRIDE; |
34 virtual void OnError(AudioOutputStream* stream) OVERRIDE; | 35 virtual void OnError(AudioOutputStream* stream) OVERRIDE; |
35 | 36 |
36 // Sets |source_callback_|. If this is not a new object, then Stop() must be | 37 // Sets |source_callback_|. If this is not a new object, then Stop() must be |
37 // called before Start(). | 38 // called before Start(). |
38 void Start(AudioOutputStream::AudioSourceCallback* callback); | 39 void Start(AudioOutputStream::AudioSourceCallback* callback); |
39 | 40 |
40 // Clears |source_callback_| and flushes the resampler. | 41 // Clears |source_callback_| and flushes the resampler. |
41 void Stop(); | 42 void Stop(); |
42 | 43 |
43 bool started() { return source_callback_ != nullptr; } | 44 bool started() { return source_callback_ != nullptr; } |
44 | 45 |
45 private: | 46 private: |
46 // AudioConverter::InputCallback implementation. | 47 // AudioConverter::InputCallback implementation. |
47 virtual double ProvideInput(AudioBus* audio_bus, | 48 virtual double ProvideInput(AudioBus* audio_bus, |
48 base::TimeDelta buffer_delay) OVERRIDE; | 49 base::TimeDelta buffer_delay) OVERRIDE; |
49 | 50 |
50 // Ratio of input bytes to output bytes used to correct playback delay with | 51 // Ratio of input bytes to output bytes used to correct playback delay with |
51 // regard to buffering and resampling. | 52 // regard to buffering and resampling. |
52 const double io_ratio_; | 53 const double io_ratio_; |
53 | 54 |
54 // Source callback. | 55 // Source callback. |
55 AudioOutputStream::AudioSourceCallback* source_callback_; | 56 AudioOutputStream::AudioSourceCallback* source_callback_; |
56 | 57 |
57 // Last AudioBuffersState object received via OnMoreData(), used to correct | 58 // Last |total_bytes_delay| received via OnMoreData(), used to correct |
58 // playback delay by ProvideInput() and passed on to |source_callback_|. | 59 // playback delay by ProvideInput() and passed on to |source_callback_|. |
59 AudioBuffersState current_buffers_state_; | 60 uint32 current_total_bytes_delay_; |
60 | 61 |
61 const int input_bytes_per_second_; | 62 const int input_bytes_per_second_; |
62 | 63 |
63 // Handles resampling, buffering, and channel mixing between input and output | 64 // Handles resampling, buffering, and channel mixing between input and output |
64 // parameters. | 65 // parameters. |
65 AudioConverter audio_converter_; | 66 AudioConverter audio_converter_; |
66 | 67 |
67 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 68 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
68 }; | 69 }; |
69 | 70 |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 audio_converter_.AddInput(this); | 321 audio_converter_.AddInput(this); |
321 } | 322 } |
322 | 323 |
323 void OnMoreDataConverter::Stop() { | 324 void OnMoreDataConverter::Stop() { |
324 CHECK(source_callback_); | 325 CHECK(source_callback_); |
325 source_callback_ = nullptr; | 326 source_callback_ = nullptr; |
326 audio_converter_.RemoveInput(this); | 327 audio_converter_.RemoveInput(this); |
327 } | 328 } |
328 | 329 |
329 int OnMoreDataConverter::OnMoreData(AudioBus* dest, | 330 int OnMoreDataConverter::OnMoreData(AudioBus* dest, |
330 AudioBuffersState buffers_state) { | 331 uint32 total_bytes_delay) { |
331 current_buffers_state_ = buffers_state; | 332 current_total_bytes_delay_ = total_bytes_delay; |
332 audio_converter_.Convert(dest); | 333 audio_converter_.Convert(dest); |
333 | 334 |
334 // Always return the full number of frames requested, ProvideInput() | 335 // Always return the full number of frames requested, ProvideInput() |
335 // will pad with silence if it wasn't able to acquire enough data. | 336 // will pad with silence if it wasn't able to acquire enough data. |
336 return dest->frames(); | 337 return dest->frames(); |
337 } | 338 } |
338 | 339 |
339 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 340 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
340 base::TimeDelta buffer_delay) { | 341 base::TimeDelta buffer_delay) { |
341 // Adjust playback delay to include |buffer_delay|. | 342 // Adjust playback delay to include |buffer_delay|. |
342 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 343 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since |
343 // AudioBus is just float data. Use TimeDelta instead. | 344 // AudioBus is just float data. Use TimeDelta instead. |
344 AudioBuffersState new_buffers_state; | 345 uint32 new_total_bytes_delay = base::saturated_cast<uint32>( |
345 new_buffers_state.pending_bytes = | 346 io_ratio_ * (current_total_bytes_delay_ + |
346 io_ratio_ * (current_buffers_state_.total_bytes() + | 347 buffer_delay.InSecondsF() * input_bytes_per_second_)); |
347 buffer_delay.InSecondsF() * input_bytes_per_second_); | |
348 | 348 |
349 // Retrieve data from the original callback. | 349 // Retrieve data from the original callback. |
350 const int frames = source_callback_->OnMoreData(dest, new_buffers_state); | 350 const int frames = source_callback_->OnMoreData(dest, new_total_bytes_delay); |
351 | 351 |
352 // Zero any unfilled frames if anything was filled, otherwise we'll just | 352 // Zero any unfilled frames if anything was filled, otherwise we'll just |
353 // return a volume of zero and let AudioConverter drop the output. | 353 // return a volume of zero and let AudioConverter drop the output. |
354 if (frames > 0 && frames < dest->frames()) | 354 if (frames > 0 && frames < dest->frames()) |
355 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 355 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
356 return frames > 0 ? 1 : 0; | 356 return frames > 0 ? 1 : 0; |
357 } | 357 } |
358 | 358 |
359 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 359 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
360 source_callback_->OnError(stream); | 360 source_callback_->OnError(stream); |
361 } | 361 } |
362 | 362 |
363 } // namespace media | 363 } // namespace media |
OLD | NEW |