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/audio/audio_output_proxy.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "media/audio/audio_manager.h"
10 #include "media/audio/audio_output_dispatcher.h"
14 AudioOutputProxy::AudioOutputProxy(AudioOutputDispatcher* dispatcher)
15 : dispatcher_(dispatcher),
20 AudioOutputProxy::~AudioOutputProxy() {
21 DCHECK(CalledOnValidThread());
22 DCHECK(state_ == kCreated || state_ == kClosed) << "State is: " << state_;
25 bool AudioOutputProxy::Open() {
26 DCHECK(CalledOnValidThread());
27 DCHECK_EQ(state_, kCreated);
29 if (!dispatcher_->OpenStream()) {
38 void AudioOutputProxy::Start(AudioSourceCallback* callback) {
39 DCHECK(CalledOnValidThread());
41 // We need to support both states since the callback may not handle OnError()
42 // immediately (or at all). It's also possible for subsequent StartStream()
43 // calls to succeed after failing, so we allow it to be called again.
44 DCHECK(state_ == kOpened || state_ == kStartError);
46 if (!dispatcher_->StartStream(callback, this)) {
48 callback->OnError(this);
54 void AudioOutputProxy::Stop() {
55 DCHECK(CalledOnValidThread());
56 if (state_ != kPlaying)
59 dispatcher_->StopStream(this);
63 void AudioOutputProxy::SetVolume(double volume) {
64 DCHECK(CalledOnValidThread());
66 dispatcher_->StreamVolumeSet(this, volume);
69 void AudioOutputProxy::GetVolume(double* volume) {
70 DCHECK(CalledOnValidThread());
74 void AudioOutputProxy::Close() {
75 DCHECK(CalledOnValidThread());
76 DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened ||
77 state_ == kStartError);
79 // kStartError means OpenStream() succeeded and the stream must be closed
80 // before destruction.
81 if (state_ != kCreated && state_ != kOpenError)
82 dispatcher_->CloseStream(this);
86 // Delete the object now like is done in the Close() implementation of
87 // physical stream objects. If we delete the object via DeleteSoon, we
88 // unnecessarily complicate the Shutdown procedure of the
89 // dispatcher+audio manager.