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