Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / audio_renderer_mixer_input.cc
blob4abec01d7f89819cb2481b68113f166e7b4bfb81
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"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "media/base/audio_renderer_mixer.h"
12 namespace media {
14 AudioRendererMixerInput::AudioRendererMixerInput(
15 const GetMixerCB& get_mixer_cb, const RemoveMixerCB& remove_mixer_cb)
16 : playing_(false),
17 initialized_(false),
18 volume_(1.0f),
19 get_mixer_cb_(get_mixer_cb),
20 remove_mixer_cb_(remove_mixer_cb),
21 mixer_(NULL),
22 callback_(NULL),
23 error_cb_(base::Bind(
24 &AudioRendererMixerInput::OnRenderError, base::Unretained(this))) {
27 AudioRendererMixerInput::~AudioRendererMixerInput() {
28 DCHECK(!playing_);
29 DCHECK(!mixer_);
32 void AudioRendererMixerInput::Initialize(
33 const AudioParameters& params,
34 AudioRendererSink::RenderCallback* callback) {
35 DCHECK(callback);
36 DCHECK(!initialized_);
38 params_ = params;
39 callback_ = callback;
40 initialized_ = true;
43 void AudioRendererMixerInput::Start() {
44 DCHECK(initialized_);
45 DCHECK(!playing_);
46 DCHECK(!mixer_);
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.
56 if (playing_) {
57 mixer_->RemoveMixerInput(this);
58 playing_ = false;
61 if (mixer_) {
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_);
67 mixer_ = NULL;
71 void AudioRendererMixerInput::Play() {
72 DCHECK(initialized_);
73 DCHECK(mixer_);
75 if (playing_)
76 return;
78 mixer_->AddMixerInput(this);
79 playing_ = true;
82 void AudioRendererMixerInput::Pause() {
83 DCHECK(initialized_);
84 DCHECK(mixer_);
86 if (!playing_)
87 return;
89 mixer_->RemoveMixerInput(this);
90 playing_ = false;
93 bool AudioRendererMixerInput::SetVolume(double volume) {
94 volume_ = volume;
95 return true;
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();
121 } // namespace media