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() {
27 // Mixer is no longer safe to use after |remove_mixer_cb_| has been called.
29 remove_mixer_cb_
.Run(params_
);
32 void AudioRendererMixerInput::Initialize(
33 const AudioParameters
& params
,
34 AudioRendererSink::RenderCallback
* callback
) {
35 DCHECK(!initialized_
);
37 mixer_
= get_mixer_cb_
.Run(params_
);
42 void AudioRendererMixerInput::Start() {
47 void AudioRendererMixerInput::Stop() {
48 // Stop() may be called at any time, if Pause() hasn't been called we need to
49 // remove our mixer input before shutdown.
53 mixer_
->RemoveMixerInput(this);
57 void AudioRendererMixerInput::Play() {
63 mixer_
->AddMixerInput(this, error_cb_
);
67 void AudioRendererMixerInput::Pause() {
73 mixer_
->RemoveMixerInput(this);
77 bool AudioRendererMixerInput::SetVolume(double volume
) {
82 double AudioRendererMixerInput::ProvideInput(AudioBus
* audio_bus
,
83 base::TimeDelta buffer_delay
) {
84 int frames_filled
= callback_
->Render(
85 audio_bus
, static_cast<int>(buffer_delay
.InMillisecondsF() + 0.5));
87 // AudioConverter expects unfilled frames to be zeroed.
88 if (frames_filled
< audio_bus
->frames()) {
89 audio_bus
->ZeroFramesPartial(
90 frames_filled
, audio_bus
->frames() - frames_filled
);
93 return frames_filled
> 0 ? volume_
: 0;
96 void AudioRendererMixerInput::OnRenderError() {
97 callback_
->OnRenderError();