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"
8 #include "base/bind_helpers.h"
9 #include "base/logging.h"
13 enum { kPauseDelaySeconds
= 10 };
15 AudioRendererMixer::AudioRendererMixer(
16 const AudioParameters
& input_params
, const AudioParameters
& output_params
,
17 const scoped_refptr
<AudioRendererSink
>& 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.
24 audio_sink_
->Initialize(output_params
, this);
28 AudioRendererMixer::~AudioRendererMixer() {
29 // AudioRendererSinks must be stopped before being destructed.
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_
);
41 last_play_time_
= base::TimeTicks::Now();
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();
64 if (it
->Equals(error_cb
)) {
65 error_callbacks_
.erase(it
);
70 // An error callback should always exist when called.
74 void AudioRendererMixer::SwitchOutputDevice(
75 const std::string
& device_id
,
76 const GURL
& security_origin
,
77 const SwitchOutputDeviceCB
& callback
) {
78 DVLOG(1) << __FUNCTION__
<< "(" << device_id
<< ", " << security_origin
80 base::AutoLock
auto_lock(lock_
);
81 audio_sink_
->SwitchOutputDevice(device_id
, security_origin
, callback
);
84 int AudioRendererMixer::Render(AudioBus
* audio_bus
,
85 int audio_delay_milliseconds
) {
86 base::AutoLock
auto_lock(lock_
);
88 // If there are no mixer inputs and we haven't seen one for a while, pause the
89 // sink to avoid wasting resources when media elements are present but remain
90 // in the pause state.
91 const base::TimeTicks now
= base::TimeTicks::Now();
92 if (!audio_converter_
.empty()) {
93 last_play_time_
= now
;
94 } else if (now
- last_play_time_
>= pause_delay_
&& playing_
) {
99 audio_converter_
.ConvertWithDelay(
100 base::TimeDelta::FromMilliseconds(audio_delay_milliseconds
), audio_bus
);
101 return audio_bus
->frames();
104 void AudioRendererMixer::OnRenderError() {
105 // Call each mixer input and signal an error.
106 base::AutoLock
auto_lock(lock_
);
107 for (const auto& cb
: error_callbacks_
)