| 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 22 matching lines...) Expand all Loading... |
| 33 AudioBuffersState buffers_state) OVERRIDE; | 33 AudioBuffersState buffers_state) 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_ != nullptr; } |
| 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 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 230 } |
| 231 | 231 |
| 232 return false; | 232 return false; |
| 233 } | 233 } |
| 234 | 234 |
| 235 bool AudioOutputResampler::StartStream( | 235 bool AudioOutputResampler::StartStream( |
| 236 AudioOutputStream::AudioSourceCallback* callback, | 236 AudioOutputStream::AudioSourceCallback* callback, |
| 237 AudioOutputProxy* stream_proxy) { | 237 AudioOutputProxy* stream_proxy) { |
| 238 DCHECK(task_runner_->BelongsToCurrentThread()); | 238 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 239 | 239 |
| 240 OnMoreDataConverter* resampler_callback = NULL; | 240 OnMoreDataConverter* resampler_callback = nullptr; |
| 241 CallbackMap::iterator it = callbacks_.find(stream_proxy); | 241 CallbackMap::iterator it = callbacks_.find(stream_proxy); |
| 242 if (it == callbacks_.end()) { | 242 if (it == callbacks_.end()) { |
| 243 resampler_callback = new OnMoreDataConverter(params_, output_params_); | 243 resampler_callback = new OnMoreDataConverter(params_, output_params_); |
| 244 callbacks_[stream_proxy] = resampler_callback; | 244 callbacks_[stream_proxy] = resampler_callback; |
| 245 } else { | 245 } else { |
| 246 resampler_callback = it->second; | 246 resampler_callback = it->second; |
| 247 } | 247 } |
| 248 | 248 |
| 249 resampler_callback->Start(callback); | 249 resampler_callback->Start(callback); |
| 250 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); | 250 bool result = dispatcher_->StartStream(resampler_callback, stream_proxy); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 DCHECK(HasOneRef()) << "Only the AudioManager should hold a reference"; | 292 DCHECK(HasOneRef()) << "Only the AudioManager should hold a reference"; |
| 293 | 293 |
| 294 dispatcher_->Shutdown(); | 294 dispatcher_->Shutdown(); |
| 295 DCHECK(callbacks_.empty()); | 295 DCHECK(callbacks_.empty()); |
| 296 } | 296 } |
| 297 | 297 |
| 298 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 298 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
| 299 const AudioParameters& output_params) | 299 const AudioParameters& output_params) |
| 300 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 300 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
| 301 output_params.GetBytesPerSecond()), | 301 output_params.GetBytesPerSecond()), |
| 302 source_callback_(NULL), | 302 source_callback_(nullptr), |
| 303 input_bytes_per_second_(input_params.GetBytesPerSecond()), | 303 input_bytes_per_second_(input_params.GetBytesPerSecond()), |
| 304 audio_converter_(input_params, output_params, false) {} | 304 audio_converter_(input_params, output_params, false) {} |
| 305 | 305 |
| 306 OnMoreDataConverter::~OnMoreDataConverter() { | 306 OnMoreDataConverter::~OnMoreDataConverter() { |
| 307 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 307 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
| 308 // calling back into OnMoreData() after destruction. | 308 // calling back into OnMoreData() after destruction. |
| 309 CHECK(!source_callback_); | 309 CHECK(!source_callback_); |
| 310 } | 310 } |
| 311 | 311 |
| 312 void OnMoreDataConverter::Start( | 312 void OnMoreDataConverter::Start( |
| 313 AudioOutputStream::AudioSourceCallback* callback) { | 313 AudioOutputStream::AudioSourceCallback* callback) { |
| 314 CHECK(!source_callback_); | 314 CHECK(!source_callback_); |
| 315 source_callback_ = callback; | 315 source_callback_ = callback; |
| 316 | 316 |
| 317 // While AudioConverter can handle multiple inputs, we're using it only with | 317 // While AudioConverter can handle multiple inputs, we're using it only with |
| 318 // a single input currently. Eventually this may be the basis for a browser | 318 // a single input currently. Eventually this may be the basis for a browser |
| 319 // side mixer. | 319 // side mixer. |
| 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_ = nullptr; |
| 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 AudioBuffersState buffers_state) { |
| 331 current_buffers_state_ = buffers_state; | 331 current_buffers_state_ = buffers_state; |
| 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. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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 |