Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / media / base / audio_renderer_mixer_input.cc
blobffdcfa875f70d62387dcafc4afd1aad3b1fcc179
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/logging.h"
9 #include "media/base/audio_renderer_mixer.h"
11 namespace media {
13 AudioRendererMixerInput::AudioRendererMixerInput(
14 const GetMixerCB& get_mixer_cb, const RemoveMixerCB& remove_mixer_cb)
15 : playing_(false),
16 initialized_(false),
17 volume_(1.0f),
18 get_mixer_cb_(get_mixer_cb),
19 remove_mixer_cb_(remove_mixer_cb),
20 mixer_(NULL),
21 callback_(NULL),
22 error_cb_(base::Bind(
23 &AudioRendererMixerInput::OnRenderError, base::Unretained(this))) {
26 AudioRendererMixerInput::~AudioRendererMixerInput() {
27 // Mixer is no longer safe to use after |remove_mixer_cb_| has been called.
28 if (initialized_)
29 remove_mixer_cb_.Run(params_);
32 void AudioRendererMixerInput::Initialize(
33 const AudioParameters& params,
34 AudioRendererSink::RenderCallback* callback) {
35 DCHECK(!initialized_);
36 params_ = params;
37 mixer_ = get_mixer_cb_.Run(params_);
38 callback_ = callback;
39 initialized_ = true;
42 void AudioRendererMixerInput::Start() {
43 DCHECK(initialized_);
44 DCHECK(!playing_);
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.
50 if (!playing_)
51 return;
53 mixer_->RemoveMixerInput(this);
54 playing_ = false;
57 void AudioRendererMixerInput::Play() {
58 DCHECK(initialized_);
60 if (playing_)
61 return;
63 mixer_->AddMixerInput(this, error_cb_);
64 playing_ = true;
67 void AudioRendererMixerInput::Pause() {
68 DCHECK(initialized_);
70 if (!playing_)
71 return;
73 mixer_->RemoveMixerInput(this);
74 playing_ = false;
77 bool AudioRendererMixerInput::SetVolume(double volume) {
78 volume_ = volume;
79 return true;
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();
100 } // namespace media