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 "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 // Clears |source_callback_| and flushes the resampler. | 43 // Clears |source_callback_| and flushes the resampler. |
44 void Stop(); | 44 void Stop(); |
45 | 45 |
46 bool started() const { return source_callback_ != nullptr; } | 46 bool started() const { return source_callback_ != nullptr; } |
47 | 47 |
48 bool error_occurred() const { return error_occurred_; } | 48 bool error_occurred() const { return error_occurred_; } |
49 | 49 |
50 private: | 50 private: |
51 // AudioConverter::InputCallback implementation. | 51 // AudioConverter::InputCallback implementation. |
52 double ProvideInput(AudioBus* audio_bus, | 52 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; |
53 base::TimeDelta buffer_delay) override; | |
54 | 53 |
55 // Ratio of input bytes to output bytes used to correct playback delay with | 54 // Ratio of input bytes to output bytes used to correct playback delay with |
56 // regard to buffering and resampling. | 55 // regard to buffering and resampling. |
57 const double io_ratio_; | 56 const double io_ratio_; |
58 | 57 |
59 // Source callback. | 58 // Source callback. |
60 AudioOutputStream::AudioSourceCallback* source_callback_; | 59 AudioOutputStream::AudioSourceCallback* source_callback_; |
61 | 60 |
62 // Last |total_bytes_delay| received via OnMoreData(), used to correct | 61 // Last |total_bytes_delay| received via OnMoreData(), used to correct |
63 // playback delay by ProvideInput() and passed on to |source_callback_|. | 62 // playback delay by ProvideInput() and passed on to |source_callback_|. |
64 uint32_t current_total_bytes_delay_; | 63 uint32_t current_total_bytes_delay_; |
65 | 64 |
66 const int input_bytes_per_second_; | 65 const int input_bytes_per_frame_; |
67 | 66 |
68 // Handles resampling, buffering, and channel mixing between input and output | 67 // Handles resampling, buffering, and channel mixing between input and output |
69 // parameters. | 68 // parameters. |
70 AudioConverter audio_converter_; | 69 AudioConverter audio_converter_; |
71 | 70 |
72 // True if OnError() was ever called. Should only be read if the underlying | 71 // True if OnError() was ever called. Should only be read if the underlying |
73 // stream has been stopped. | 72 // stream has been stopped. |
74 bool error_occurred_; | 73 bool error_occurred_; |
75 | 74 |
76 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 75 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 339 |
341 dispatcher_->Shutdown(); | 340 dispatcher_->Shutdown(); |
342 DCHECK(callbacks_.empty()); | 341 DCHECK(callbacks_.empty()); |
343 } | 342 } |
344 | 343 |
345 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 344 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
346 const AudioParameters& output_params) | 345 const AudioParameters& output_params) |
347 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 346 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
348 output_params.GetBytesPerSecond()), | 347 output_params.GetBytesPerSecond()), |
349 source_callback_(nullptr), | 348 source_callback_(nullptr), |
350 input_bytes_per_second_(input_params.GetBytesPerSecond()), | 349 input_bytes_per_frame_(input_params.GetBytesPerFrame()), |
351 audio_converter_(input_params, output_params, false), | 350 audio_converter_(input_params, output_params, false), |
352 error_occurred_(false) {} | 351 error_occurred_(false) {} |
353 | 352 |
354 OnMoreDataConverter::~OnMoreDataConverter() { | 353 OnMoreDataConverter::~OnMoreDataConverter() { |
355 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 354 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
356 // calling back into OnMoreData() after destruction. | 355 // calling back into OnMoreData() after destruction. |
357 CHECK(!source_callback_); | 356 CHECK(!source_callback_); |
358 } | 357 } |
359 | 358 |
360 void OnMoreDataConverter::Start( | 359 void OnMoreDataConverter::Start( |
(...skipping 18 matching lines...) Expand all Loading... |
379 uint32_t frames_skipped) { | 378 uint32_t frames_skipped) { |
380 current_total_bytes_delay_ = total_bytes_delay; | 379 current_total_bytes_delay_ = total_bytes_delay; |
381 audio_converter_.Convert(dest); | 380 audio_converter_.Convert(dest); |
382 | 381 |
383 // Always return the full number of frames requested, ProvideInput() | 382 // Always return the full number of frames requested, ProvideInput() |
384 // will pad with silence if it wasn't able to acquire enough data. | 383 // will pad with silence if it wasn't able to acquire enough data. |
385 return dest->frames(); | 384 return dest->frames(); |
386 } | 385 } |
387 | 386 |
388 double OnMoreDataConverter::ProvideInput(AudioBus* dest, | 387 double OnMoreDataConverter::ProvideInput(AudioBus* dest, |
389 base::TimeDelta buffer_delay) { | 388 uint32_t frames_delayed) { |
390 // Adjust playback delay to include |buffer_delay|. | 389 // Adjust playback delay to include |frames_delayed|. |
391 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 390 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since |
392 // AudioBus is just float data. Use TimeDelta instead. | 391 // AudioBus is just float data. Use TimeDelta instead. |
393 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( | 392 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( |
394 io_ratio_ * (current_total_bytes_delay_ + | 393 io_ratio_ * |
395 buffer_delay.InSecondsF() * input_bytes_per_second_)); | 394 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); |
396 | 395 |
397 // Retrieve data from the original callback. | 396 // Retrieve data from the original callback. |
398 const int frames = | 397 const int frames = |
399 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | 398 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); |
400 | 399 |
401 // Zero any unfilled frames if anything was filled, otherwise we'll just | 400 // Zero any unfilled frames if anything was filled, otherwise we'll just |
402 // return a volume of zero and let AudioConverter drop the output. | 401 // return a volume of zero and let AudioConverter drop the output. |
403 if (frames > 0 && frames < dest->frames()) | 402 if (frames > 0 && frames < dest->frames()) |
404 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 403 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
405 return frames > 0 ? 1 : 0; | 404 return frames > 0 ? 1 : 0; |
406 } | 405 } |
407 | 406 |
408 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 407 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
409 error_occurred_ = true; | 408 error_occurred_ = true; |
410 source_callback_->OnError(stream); | 409 source_callback_->OnError(stream); |
411 } | 410 } |
412 | 411 |
413 } // namespace media | 412 } // namespace media |
OLD | NEW |