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_samples_per_second_; |
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_; |
74 | 77 |
75 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); | 78 DISALLOW_COPY_AND_ASSIGN(OnMoreDataConverter); |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
339 | 342 |
340 dispatcher_->Shutdown(); | 343 dispatcher_->Shutdown(); |
341 DCHECK(callbacks_.empty()); | 344 DCHECK(callbacks_.empty()); |
342 } | 345 } |
343 | 346 |
344 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, | 347 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, |
345 const AudioParameters& output_params) | 348 const AudioParameters& output_params) |
346 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / | 349 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / |
347 output_params.GetBytesPerSecond()), | 350 output_params.GetBytesPerSecond()), |
348 source_callback_(nullptr), | 351 source_callback_(nullptr), |
349 input_bytes_per_frame_(input_params.GetBytesPerFrame()), | 352 input_samples_per_second_(input_params.sample_rate()), |
350 audio_converter_(input_params, output_params, false), | 353 audio_converter_(input_params, output_params, false), |
351 error_occurred_(false) {} | 354 error_occurred_(false) {} |
352 | 355 |
353 OnMoreDataConverter::~OnMoreDataConverter() { | 356 OnMoreDataConverter::~OnMoreDataConverter() { |
354 // Ensure Stop() has been called so we don't end up with an AudioOutputStream | 357 // Ensure Stop() has been called so we don't end up with an AudioOutputStream |
355 // calling back into OnMoreData() after destruction. | 358 // calling back into OnMoreData() after destruction. |
356 CHECK(!source_callback_); | 359 CHECK(!source_callback_); |
357 } | 360 } |
358 | 361 |
359 void OnMoreDataConverter::Start( | 362 void OnMoreDataConverter::Start( |
360 AudioOutputStream::AudioSourceCallback* callback) { | 363 AudioOutputStream::AudioSourceCallback* callback) { |
361 CHECK(!source_callback_); | 364 CHECK(!source_callback_); |
362 source_callback_ = callback; | 365 source_callback_ = callback; |
363 | 366 |
364 // While AudioConverter can handle multiple inputs, we're using it only with | 367 // While AudioConverter can handle multiple inputs, we're using it only with |
365 // a single input currently. Eventually this may be the basis for a browser | 368 // a single input currently. Eventually this may be the basis for a browser |
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|. | 392 base::TimeTicks new_target_playout_time = |
chcunningham1
2016/08/27 00:35:00
This new math looks good to me, but I'd like to ha
miu
2016/08/31 23:26:54
Actually, Hubbe originally wrote this code. I'll a
DaleCurtis
2016/09/01 18:46:13
Looking at the code that does seem correct.
James West
2016/09/13 07:40:50
Acknowledged.
| |
390 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since | 393 target_playout_time_ + |
391 // AudioBus is just float data. Use TimeDelta instead. | 394 base::TimeDelta::FromMicroseconds(frames_delayed * |
392 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>( | 395 base::Time::kMicrosecondsPerSecond / |
393 io_ratio_ * | 396 input_samples_per_second_); |
394 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_)); | |
395 | |
396 // Retrieve data from the original callback. | 397 // Retrieve data from the original callback. |
397 const int frames = | 398 const int frames = |
398 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); | 399 source_callback_->OnMoreData(new_target_playout_time, 0, dest); |
399 | 400 |
400 // Zero any unfilled frames if anything was filled, otherwise we'll just | 401 // Zero any unfilled frames if anything was filled, otherwise we'll just |
401 // return a volume of zero and let AudioConverter drop the output. | 402 // return a volume of zero and let AudioConverter drop the output. |
402 if (frames > 0 && frames < dest->frames()) | 403 if (frames > 0 && frames < dest->frames()) |
403 dest->ZeroFramesPartial(frames, dest->frames() - frames); | 404 dest->ZeroFramesPartial(frames, dest->frames() - frames); |
404 return frames > 0 ? 1 : 0; | 405 return frames > 0 ? 1 : 0; |
405 } | 406 } |
406 | 407 |
407 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { | 408 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { |
408 error_occurred_ = true; | 409 error_occurred_ = true; |
409 source_callback_->OnError(stream); | 410 source_callback_->OnError(stream); |
410 } | 411 } |
411 | 412 |
412 } // namespace media | 413 } // namespace media |
OLD | NEW |