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_input.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "media/base/audio_renderer_mixer.h"
14 AudioRendererMixerInput::AudioRendererMixerInput(
15 const GetMixerCB
& get_mixer_cb
, const RemoveMixerCB
& remove_mixer_cb
)
19 get_mixer_cb_(get_mixer_cb
),
20 remove_mixer_cb_(remove_mixer_cb
),
24 &AudioRendererMixerInput::OnRenderError
, base::Unretained(this))) {
27 AudioRendererMixerInput::~AudioRendererMixerInput() {
32 void AudioRendererMixerInput::Initialize(
33 const AudioParameters
& params
,
34 AudioRendererSink::RenderCallback
* callback
) {
36 DCHECK(!initialized_
);
43 void AudioRendererMixerInput::Start() {
47 mixer_
= get_mixer_cb_
.Run(params_
);
49 // Note: OnRenderError() may be called immediately after this call returns.
50 mixer_
->AddErrorCallback(error_cb_
);
53 void AudioRendererMixerInput::Stop() {
54 // Stop() may be called at any time, if Pause() hasn't been called we need to
55 // remove our mixer input before shutdown.
57 mixer_
->RemoveMixerInput(this);
62 // TODO(dalecurtis): This is required so that |callback_| isn't called after
63 // Stop() by an error event since it may outlive this ref-counted object. We
64 // should instead have sane ownership semantics: http://crbug.com/151051
65 mixer_
->RemoveErrorCallback(error_cb_
);
66 remove_mixer_cb_
.Run(params_
);
71 void AudioRendererMixerInput::Play() {
78 mixer_
->AddMixerInput(this);
82 void AudioRendererMixerInput::Pause() {
89 mixer_
->RemoveMixerInput(this);
93 bool AudioRendererMixerInput::SetVolume(double volume
) {
98 OutputDevice
* AudioRendererMixerInput::GetOutputDevice() {
99 DVLOG(1) << __FUNCTION__
;
100 return mixer_
->GetOutputDevice();
103 double AudioRendererMixerInput::ProvideInput(AudioBus
* audio_bus
,
104 base::TimeDelta buffer_delay
) {
105 int frames_filled
= callback_
->Render(
106 audio_bus
, static_cast<int>(buffer_delay
.InMillisecondsF() + 0.5));
108 // AudioConverter expects unfilled frames to be zeroed.
109 if (frames_filled
< audio_bus
->frames()) {
110 audio_bus
->ZeroFramesPartial(
111 frames_filled
, audio_bus
->frames() - frames_filled
);
114 return frames_filled
> 0 ? volume_
: 0;
117 void AudioRendererMixerInput::OnRenderError() {
118 callback_
->OnRenderError();