Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / audio_renderer_mixer.cc
blob984798d916d6ce59ace8ebd9244c23693eb9db4b
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
3 // found in the LICENSE file.
5 #include "media/base/audio_renderer_mixer.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
11 namespace media {
13 enum { kPauseDelaySeconds = 10 };
15 AudioRendererMixer::AudioRendererMixer(
16 const AudioParameters& input_params, const AudioParameters& output_params,
17 const scoped_refptr<AudioRendererSink>& sink)
18 : audio_sink_(sink),
19 audio_converter_(input_params, output_params, true),
20 pause_delay_(base::TimeDelta::FromSeconds(kPauseDelaySeconds)),
21 last_play_time_(base::TimeTicks::Now()),
22 // Initialize |playing_| to true since Start() results in an auto-play.
23 playing_(true) {
24 audio_sink_->Initialize(output_params, this);
25 audio_sink_->Start();
28 AudioRendererMixer::~AudioRendererMixer() {
29 // AudioRendererSinks must be stopped before being destructed.
30 audio_sink_->Stop();
32 // Ensure that all mixer inputs have removed themselves prior to destruction.
33 DCHECK(audio_converter_.empty());
34 DCHECK_EQ(error_callbacks_.size(), 0U);
37 void AudioRendererMixer::AddMixerInput(AudioConverter::InputCallback* input) {
38 base::AutoLock auto_lock(lock_);
39 if (!playing_) {
40 playing_ = true;
41 last_play_time_ = base::TimeTicks::Now();
42 audio_sink_->Play();
45 audio_converter_.AddInput(input);
48 void AudioRendererMixer::RemoveMixerInput(
49 AudioConverter::InputCallback* input) {
50 base::AutoLock auto_lock(lock_);
51 audio_converter_.RemoveInput(input);
54 void AudioRendererMixer::AddErrorCallback(const base::Closure& error_cb) {
55 base::AutoLock auto_lock(lock_);
56 error_callbacks_.push_back(error_cb);
59 void AudioRendererMixer::RemoveErrorCallback(const base::Closure& error_cb) {
60 base::AutoLock auto_lock(lock_);
61 for (ErrorCallbackList::iterator it = error_callbacks_.begin();
62 it != error_callbacks_.end();
63 ++it) {
64 if (it->Equals(error_cb)) {
65 error_callbacks_.erase(it);
66 return;
70 // An error callback should always exist when called.
71 NOTREACHED();
74 OutputDevice* AudioRendererMixer::GetOutputDevice() {
75 DVLOG(1) << __FUNCTION__;
76 base::AutoLock auto_lock(lock_);
77 return audio_sink_->GetOutputDevice();
80 int AudioRendererMixer::Render(AudioBus* audio_bus,
81 int audio_delay_milliseconds) {
82 base::AutoLock auto_lock(lock_);
84 // If there are no mixer inputs and we haven't seen one for a while, pause the
85 // sink to avoid wasting resources when media elements are present but remain
86 // in the pause state.
87 const base::TimeTicks now = base::TimeTicks::Now();
88 if (!audio_converter_.empty()) {
89 last_play_time_ = now;
90 } else if (now - last_play_time_ >= pause_delay_ && playing_) {
91 audio_sink_->Pause();
92 playing_ = false;
95 audio_converter_.ConvertWithDelay(
96 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds), audio_bus);
97 return audio_bus->frames();
100 void AudioRendererMixer::OnRenderError() {
101 // Call each mixer input and signal an error.
102 base::AutoLock auto_lock(lock_);
103 for (const auto& cb : error_callbacks_)
104 cb.Run();
107 } // namespace media