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/logging.h"
9 #include "media/base/audio_renderer_mixer.h"
13 AudioRendererMixerInput::AudioRendererMixerInput(
14 const GetMixerCB
& get_mixer_cb
, const RemoveMixerCB
& remove_mixer_cb
)
18 get_mixer_cb_(get_mixer_cb
),
19 remove_mixer_cb_(remove_mixer_cb
),
23 &AudioRendererMixerInput::OnRenderError
, base::Unretained(this))) {
26 AudioRendererMixerInput::~AudioRendererMixerInput() {
31 void AudioRendererMixerInput::Initialize(
32 const AudioParameters
& params
,
33 AudioRendererSink::RenderCallback
* callback
) {
35 DCHECK(!initialized_
);
42 void AudioRendererMixerInput::Start() {
46 mixer_
= get_mixer_cb_
.Run(params_
);
48 // Note: OnRenderError() may be called immediately after this call returns.
49 mixer_
->AddErrorCallback(error_cb_
);
52 void AudioRendererMixerInput::Stop() {
53 // Stop() may be called at any time, if Pause() hasn't been called we need to
54 // remove our mixer input before shutdown.
56 mixer_
->RemoveMixerInput(this);
61 // TODO(dalecurtis): This is required so that |callback_| isn't called after
62 // Stop() by an error event since it may outlive this ref-counted object. We
63 // should instead have sane ownership semantics: http://crbug.com/151051
64 mixer_
->RemoveErrorCallback(error_cb_
);
65 remove_mixer_cb_
.Run(params_
);
70 void AudioRendererMixerInput::Play() {
77 mixer_
->AddMixerInput(this);
81 void AudioRendererMixerInput::Pause() {
88 mixer_
->RemoveMixerInput(this);
92 bool AudioRendererMixerInput::SetVolume(double volume
) {
97 double AudioRendererMixerInput::ProvideInput(AudioBus
* audio_bus
,
98 base::TimeDelta buffer_delay
) {
99 int frames_filled
= callback_
->Render(
100 audio_bus
, static_cast<int>(buffer_delay
.InMillisecondsF() + 0.5));
102 // AudioConverter expects unfilled frames to be zeroed.
103 if (frames_filled
< audio_bus
->frames()) {
104 audio_bus
->ZeroFramesPartial(
105 frames_filled
, audio_bus
->frames() - frames_filled
);
108 return frames_filled
> 0 ? volume_
: 0;
111 void AudioRendererMixerInput::OnRenderError() {
112 callback_
->OnRenderError();