Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: media/audio/audio_output_resampler.cc

Issue 2101303004: Pass delay and timestamp to AudioSourceCallback::OnMoreData. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Fix Mac CQ errors. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_macros.h" 16 #include "base/metrics/histogram_macros.h"
14 #include "base/metrics/sparse_histogram.h" 17 #include "base/metrics/sparse_histogram.h"
15 #include "base/numerics/safe_conversions.h" 18 #include "base/numerics/safe_conversions.h"
16 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
17 #include "base/trace_event/trace_event.h" 20 #include "base/trace_event/trace_event.h"
18 #include "build/build_config.h" 21 #include "build/build_config.h"
19 #include "media/audio/audio_output_proxy.h" 22 #include "media/audio/audio_output_proxy.h"
20 #include "media/audio/sample_rates.h" 23 #include "media/audio/sample_rates.h"
21 #include "media/base/audio_converter.h" 24 #include "media/base/audio_converter.h"
25 #include "media/base/audio_timestamp_helper.h"
22 #include "media/base/limits.h" 26 #include "media/base/limits.h"
23 27
24 namespace media { 28 namespace media {
25 29
26 class OnMoreDataConverter 30 class OnMoreDataConverter
27 : public AudioOutputStream::AudioSourceCallback, 31 : public AudioOutputStream::AudioSourceCallback,
28 public AudioConverter::InputCallback { 32 public AudioConverter::InputCallback {
29 public: 33 public:
30 OnMoreDataConverter(const AudioParameters& input_params, 34 OnMoreDataConverter(const AudioParameters& input_params,
31 const AudioParameters& output_params); 35 const AudioParameters& output_params);
32 ~OnMoreDataConverter() override; 36 ~OnMoreDataConverter() override;
33 37
34 // AudioSourceCallback interface. 38 // AudioSourceCallback interface.
35 int OnMoreData(AudioBus* dest, 39 int OnMoreData(base::TimeDelta delay,
36 uint32_t total_bytes_delay, 40 base::TimeTicks delay_timestamp,
37 uint32_t frames_skipped) override; 41 int prior_frames_skipped,
42 AudioBus* dest) override;
38 void OnError(AudioOutputStream* stream) override; 43 void OnError(AudioOutputStream* stream) override;
39 44
40 // Sets |source_callback_|. If this is not a new object, then Stop() must be 45 // Sets |source_callback_|. If this is not a new object, then Stop() must be
41 // called before Start(). 46 // called before Start().
42 void Start(AudioOutputStream::AudioSourceCallback* callback); 47 void Start(AudioOutputStream::AudioSourceCallback* callback);
43 48
44 // Clears |source_callback_| and flushes the resampler. 49 // Clears |source_callback_| and flushes the resampler.
45 void Stop(); 50 void Stop();
46 51
47 bool started() const { return source_callback_ != nullptr; } 52 bool started() const { return source_callback_ != nullptr; }
48 53
49 bool error_occurred() const { return error_occurred_; } 54 bool error_occurred() const { return error_occurred_; }
50 55
51 private: 56 private:
52 // AudioConverter::InputCallback implementation. 57 // AudioConverter::InputCallback implementation.
53 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override; 58 double ProvideInput(AudioBus* audio_bus, uint32_t frames_delayed) override;
54 59
55 // Ratio of input bytes to output bytes used to correct playback delay with 60 // Ratio of input bytes to output bytes used to correct playback delay with
56 // regard to buffering and resampling. 61 // regard to buffering and resampling.
57 const double io_ratio_; 62 const double io_ratio_;
58 63
59 // Source callback. 64 // Source callback.
60 AudioOutputStream::AudioSourceCallback* source_callback_; 65 AudioOutputStream::AudioSourceCallback* source_callback_;
61 66
62 // Last |total_bytes_delay| received via OnMoreData(), used to correct 67 // Last |delay| and |delay_timestamp| received via OnMoreData(). Used to
63 // playback delay by ProvideInput() and passed on to |source_callback_|. 68 // correct playback delay in ProvideInput() before calling |source_callback_|.
64 uint32_t current_total_bytes_delay_; 69 base::TimeDelta current_delay_;
70 base::TimeTicks current_delay_timestamp_;
65 71
66 const int input_bytes_per_frame_; 72 const int input_samples_per_second_;
67 73
68 // Handles resampling, buffering, and channel mixing between input and output 74 // Handles resampling, buffering, and channel mixing between input and output
69 // parameters. 75 // parameters.
70 AudioConverter audio_converter_; 76 AudioConverter audio_converter_;
71 77
72 // True if OnError() was ever called. Should only be read if the underlying 78 // True if OnError() was ever called. Should only be read if the underlying
73 // stream has been stopped. 79 // stream has been stopped.
74 bool error_occurred_; 80 bool error_occurred_;
75 81
76 // Information about input and output buffer sizes to be traced. 82 // Information about input and output buffer sizes to be traced.
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 400
395 dispatcher_->Shutdown(); 401 dispatcher_->Shutdown();
396 DCHECK(callbacks_.empty()); 402 DCHECK(callbacks_.empty());
397 } 403 }
398 404
399 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params, 405 OnMoreDataConverter::OnMoreDataConverter(const AudioParameters& input_params,
400 const AudioParameters& output_params) 406 const AudioParameters& output_params)
401 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) / 407 : io_ratio_(static_cast<double>(input_params.GetBytesPerSecond()) /
402 output_params.GetBytesPerSecond()), 408 output_params.GetBytesPerSecond()),
403 source_callback_(nullptr), 409 source_callback_(nullptr),
404 input_bytes_per_frame_(input_params.GetBytesPerFrame()), 410 input_samples_per_second_(input_params.sample_rate()),
405 audio_converter_(input_params, output_params, false), 411 audio_converter_(input_params, output_params, false),
406 error_occurred_(false), 412 error_occurred_(false),
407 input_buffer_size_(input_params.frames_per_buffer()), 413 input_buffer_size_(input_params.frames_per_buffer()),
408 output_buffer_size_(output_params.frames_per_buffer()) { 414 output_buffer_size_(output_params.frames_per_buffer()) {
409 RecordRebufferingStats(input_params, output_params); 415 RecordRebufferingStats(input_params, output_params);
410 } 416 }
411 417
412 OnMoreDataConverter::~OnMoreDataConverter() { 418 OnMoreDataConverter::~OnMoreDataConverter() {
413 // Ensure Stop() has been called so we don't end up with an AudioOutputStream 419 // Ensure Stop() has been called so we don't end up with an AudioOutputStream
414 // calling back into OnMoreData() after destruction. 420 // calling back into OnMoreData() after destruction.
(...skipping 10 matching lines...) Expand all
425 // side mixer. 431 // side mixer.
426 audio_converter_.AddInput(this); 432 audio_converter_.AddInput(this);
427 } 433 }
428 434
429 void OnMoreDataConverter::Stop() { 435 void OnMoreDataConverter::Stop() {
430 CHECK(source_callback_); 436 CHECK(source_callback_);
431 source_callback_ = nullptr; 437 source_callback_ = nullptr;
432 audio_converter_.RemoveInput(this); 438 audio_converter_.RemoveInput(this);
433 } 439 }
434 440
435 int OnMoreDataConverter::OnMoreData(AudioBus* dest, 441 int OnMoreDataConverter::OnMoreData(base::TimeDelta delay,
436 uint32_t total_bytes_delay, 442 base::TimeTicks delay_timestamp,
437 uint32_t frames_skipped) { 443 int /* prior_frames_skipped */,
444 AudioBus* dest) {
438 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size", 445 TRACE_EVENT2("audio", "OnMoreDataConverter::OnMoreData", "input buffer size",
439 input_buffer_size_, "output buffer size", output_buffer_size_); 446 input_buffer_size_, "output buffer size", output_buffer_size_);
440 current_total_bytes_delay_ = total_bytes_delay; 447 current_delay_ = delay;
448 current_delay_timestamp_ = delay_timestamp;
441 audio_converter_.Convert(dest); 449 audio_converter_.Convert(dest);
442 450
443 // Always return the full number of frames requested, ProvideInput() 451 // Always return the full number of frames requested, ProvideInput()
444 // will pad with silence if it wasn't able to acquire enough data. 452 // will pad with silence if it wasn't able to acquire enough data.
445 return dest->frames(); 453 return dest->frames();
446 } 454 }
447 455
448 double OnMoreDataConverter::ProvideInput(AudioBus* dest, 456 double OnMoreDataConverter::ProvideInput(AudioBus* dest,
449 uint32_t frames_delayed) { 457 uint32_t frames_delayed) {
450 // Adjust playback delay to include |frames_delayed|. 458 base::TimeDelta new_delay =
451 // TODO(dalecurtis): Stop passing bytes around, it doesn't make sense since 459 current_delay_ + AudioTimestampHelper::FramesToTime(
452 // AudioBus is just float data. Use TimeDelta instead. 460 frames_delayed, input_samples_per_second_);
453 uint32_t new_total_bytes_delay = base::saturated_cast<uint32_t>(
454 io_ratio_ *
455 (current_total_bytes_delay_ + frames_delayed * input_bytes_per_frame_));
456
457 // Retrieve data from the original callback. 461 // Retrieve data from the original callback.
458 const int frames = 462 const int frames = source_callback_->OnMoreData(
459 source_callback_->OnMoreData(dest, new_total_bytes_delay, 0); 463 new_delay, current_delay_timestamp_, 0, dest);
460 464
461 // Zero any unfilled frames if anything was filled, otherwise we'll just 465 // Zero any unfilled frames if anything was filled, otherwise we'll just
462 // return a volume of zero and let AudioConverter drop the output. 466 // return a volume of zero and let AudioConverter drop the output.
463 if (frames > 0 && frames < dest->frames()) 467 if (frames > 0 && frames < dest->frames())
464 dest->ZeroFramesPartial(frames, dest->frames() - frames); 468 dest->ZeroFramesPartial(frames, dest->frames() - frames);
465 return frames > 0 ? 1 : 0; 469 return frames > 0 ? 1 : 0;
466 } 470 }
467 471
468 void OnMoreDataConverter::OnError(AudioOutputStream* stream) { 472 void OnMoreDataConverter::OnError(AudioOutputStream* stream) {
469 error_occurred_ = true; 473 error_occurred_ = true;
470 source_callback_->OnError(stream); 474 source_callback_->OnError(stream);
471 } 475 }
472 476
473 } // namespace media 477 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698