audio_output_proxy.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#include "media/audio/audio_output_proxy.h"
6
7#include "base/logging.h"
8#include "base/message_loop.h"
9#include "media/audio/audio_manager.h"
10#include "media/audio/audio_output_dispatcher.h"
11
12namespace media {
13
14AudioOutputProxy::AudioOutputProxy(AudioOutputDispatcher* dispatcher)
15    : dispatcher_(dispatcher),
16      state_(kCreated),
17      volume_(1.0) {
18}
19
20AudioOutputProxy::~AudioOutputProxy() {
21  DCHECK(CalledOnValidThread());
22  DCHECK(state_ == kCreated || state_ == kClosed) << "State is: " << state_;
23}
24
25bool AudioOutputProxy::Open() {
26  DCHECK(CalledOnValidThread());
27  DCHECK_EQ(state_, kCreated);
28
29  if (!dispatcher_->OpenStream()) {
30    state_ = kOpenError;
31    return false;
32  }
33
34  state_ = kOpened;
35  return true;
36}
37
38void AudioOutputProxy::Start(AudioSourceCallback* callback) {
39  DCHECK(CalledOnValidThread());
40  DCHECK_EQ(state_, kOpened);
41
42  if (!dispatcher_->StartStream(callback, this)) {
43    state_ = kStartError;
44    callback->OnError(this, 0);
45    return;
46  }
47  state_ = kPlaying;
48}
49
50void AudioOutputProxy::Stop() {
51  DCHECK(CalledOnValidThread());
52  if (state_ != kPlaying)
53    return;
54
55  dispatcher_->StopStream(this);
56  state_ = kOpened;
57}
58
59void AudioOutputProxy::SetVolume(double volume) {
60  DCHECK(CalledOnValidThread());
61  volume_ = volume;
62  dispatcher_->StreamVolumeSet(this, volume);
63}
64
65void AudioOutputProxy::GetVolume(double* volume) {
66  DCHECK(CalledOnValidThread());
67  *volume = volume_;
68}
69
70void AudioOutputProxy::Close() {
71  DCHECK(CalledOnValidThread());
72  DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened ||
73         state_ == kStartError);
74
75  // kStartError means OpenStream() succeeded and the stream must be closed
76  // before destruction.
77  if (state_ != kCreated && state_ != kOpenError)
78    dispatcher_->CloseStream(this);
79
80  state_ = kClosed;
81
82  // Delete the object now like is done in the Close() implementation of
83  // physical stream objects.  If we delete the object via DeleteSoon, we
84  // unnecessarily complicate the Shutdown procedure of the
85  // dispatcher+audio manager.
86  delete this;
87}
88
89}  // namespace media
90