OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/audio_output_proxy.h" | 5 #include "media/audio/audio_output_proxy.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "media/audio/audio_manager.h" | 9 #include "media/audio/audio_manager.h" |
10 #include "media/audio/audio_output_dispatcher.h" | 10 #include "media/audio/audio_output_dispatcher.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 | 13 |
14 AudioOutputProxy::AudioOutputProxy(AudioOutputDispatcher* dispatcher) | 14 AudioOutputProxy::AudioOutputProxy( |
15 : dispatcher_(dispatcher), | 15 base::WeakPtr<AudioOutputDispatcher> dispatcher) |
16 state_(kCreated), | 16 : dispatcher_(std::move(dispatcher)), state_(kCreated), volume_(1.0) { |
17 volume_(1.0) { | 17 DCHECK(dispatcher_); |
18 } | 18 } |
19 | 19 |
20 AudioOutputProxy::~AudioOutputProxy() { | 20 AudioOutputProxy::~AudioOutputProxy() { |
21 DCHECK(CalledOnValidThread()); | 21 DCHECK(CalledOnValidThread()); |
22 DCHECK(state_ == kCreated || state_ == kClosed) << "State is: " << state_; | 22 DCHECK(state_ == kCreated || state_ == kClosed) << "State is: " << state_; |
23 } | 23 } |
24 | 24 |
25 bool AudioOutputProxy::Open() { | 25 bool AudioOutputProxy::Open() { |
26 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
27 DCHECK_EQ(state_, kCreated); | 27 DCHECK_EQ(state_, kCreated); |
28 | 28 |
29 if (!dispatcher_->OpenStream()) { | 29 if (!dispatcher_ || !dispatcher_->OpenStream()) { |
30 state_ = kOpenError; | 30 state_ = kOpenError; |
31 return false; | 31 return false; |
32 } | 32 } |
33 | 33 |
34 state_ = kOpened; | 34 state_ = kOpened; |
35 return true; | 35 return true; |
36 } | 36 } |
37 | 37 |
38 void AudioOutputProxy::Start(AudioSourceCallback* callback) { | 38 void AudioOutputProxy::Start(AudioSourceCallback* callback) { |
39 DCHECK(CalledOnValidThread()); | 39 DCHECK(CalledOnValidThread()); |
40 | 40 |
41 // We need to support both states since the callback may not handle OnError() | 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() | 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. | 43 // calls to succeed after failing, so we allow it to be called again. |
44 DCHECK(state_ == kOpened || state_ == kStartError); | 44 DCHECK(state_ == kOpened || state_ == kStartError); |
45 | 45 |
46 if (!dispatcher_->StartStream(callback, this)) { | 46 if (!dispatcher_ || !dispatcher_->StartStream(callback, this)) { |
47 state_ = kStartError; | 47 state_ = kStartError; |
48 callback->OnError(this); | 48 callback->OnError(this); |
49 return; | 49 return; |
50 } | 50 } |
51 state_ = kPlaying; | 51 state_ = kPlaying; |
52 } | 52 } |
53 | 53 |
54 void AudioOutputProxy::Stop() { | 54 void AudioOutputProxy::Stop() { |
55 DCHECK(CalledOnValidThread()); | 55 DCHECK(CalledOnValidThread()); |
56 if (state_ != kPlaying) | 56 if (state_ != kPlaying) |
57 return; | 57 return; |
58 | 58 |
59 dispatcher_->StopStream(this); | 59 if (dispatcher_) |
| 60 dispatcher_->StopStream(this); |
60 state_ = kOpened; | 61 state_ = kOpened; |
61 } | 62 } |
62 | 63 |
63 void AudioOutputProxy::SetVolume(double volume) { | 64 void AudioOutputProxy::SetVolume(double volume) { |
64 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
65 volume_ = volume; | 66 volume_ = volume; |
66 dispatcher_->StreamVolumeSet(this, volume); | 67 |
| 68 if (dispatcher_) |
| 69 dispatcher_->StreamVolumeSet(this, volume); |
67 } | 70 } |
68 | 71 |
69 void AudioOutputProxy::GetVolume(double* volume) { | 72 void AudioOutputProxy::GetVolume(double* volume) { |
70 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
71 *volume = volume_; | 74 *volume = volume_; |
72 } | 75 } |
73 | 76 |
74 void AudioOutputProxy::Close() { | 77 void AudioOutputProxy::Close() { |
75 DCHECK(CalledOnValidThread()); | 78 DCHECK(CalledOnValidThread()); |
76 DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened || | 79 DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened || |
77 state_ == kStartError); | 80 state_ == kStartError); |
78 | 81 |
79 // kStartError means OpenStream() succeeded and the stream must be closed | 82 // kStartError means OpenStream() succeeded and the stream must be closed |
80 // before destruction. | 83 // before destruction. |
81 if (state_ != kCreated && state_ != kOpenError) | 84 if (state_ != kCreated && state_ != kOpenError && dispatcher_) |
82 dispatcher_->CloseStream(this); | 85 dispatcher_->CloseStream(this); |
83 | 86 |
84 state_ = kClosed; | 87 state_ = kClosed; |
85 | 88 |
86 // Delete the object now like is done in the Close() implementation of | 89 // Delete the object now like is done in the Close() implementation of |
87 // physical stream objects. If we delete the object via DeleteSoon, we | 90 // physical stream objects. If we delete the object via DeleteSoon, we |
88 // unnecessarily complicate the Shutdown procedure of the | 91 // unnecessarily complicate the Shutdown procedure of the |
89 // dispatcher+audio manager. | 92 // dispatcher+audio manager. |
90 delete this; | 93 delete this; |
91 } | 94 } |
92 | 95 |
93 } // namespace media | 96 } // namespace media |
OLD | NEW |