| 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.h" | 8 #include "base/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(AudioOutputDispatcher* dispatcher) |
| 15 : dispatcher_(dispatcher), | 15 : dispatcher_(dispatcher), |
| 16 state_(kCreated), | 16 state_(kCreated), |
| 17 volume_(1.0) { | 17 volume_(1.0) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 AudioOutputProxy::~AudioOutputProxy() { | 20 AudioOutputProxy::~AudioOutputProxy() { |
| 21 DCHECK(CalledOnValidThread()); | 21 DCHECK(CalledOnValidThread()); |
| 22 CHECK(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 CHECK_EQ(state_, kCreated); | 27 DCHECK_EQ(state_, kCreated); |
| 28 | 28 |
| 29 if (!dispatcher_->OpenStream()) { | 29 if (!dispatcher_->OpenStream()) { |
| 30 state_ = kError; | 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 CHECK_EQ(state_, kOpened); | 40 DCHECK_EQ(state_, kOpened); |
| 41 | 41 |
| 42 if (!dispatcher_->StartStream(callback, this)) { | 42 if (!dispatcher_->StartStream(callback, this)) { |
| 43 state_ = kError; | 43 state_ = kStartError; |
| 44 callback->OnError(this, 0); | 44 callback->OnError(this, 0); |
| 45 return; | 45 return; |
| 46 } | 46 } |
| 47 state_ = kPlaying; | 47 state_ = kPlaying; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void AudioOutputProxy::Stop() { | 50 void AudioOutputProxy::Stop() { |
| 51 DCHECK(CalledOnValidThread()); | 51 DCHECK(CalledOnValidThread()); |
| 52 if (state_ != kPlaying) | 52 if (state_ != kPlaying) |
| 53 return; | 53 return; |
| 54 | 54 |
| 55 dispatcher_->StopStream(this); | 55 dispatcher_->StopStream(this); |
| 56 state_ = kOpened; | 56 state_ = kOpened; |
| 57 } | 57 } |
| 58 | 58 |
| 59 void AudioOutputProxy::SetVolume(double volume) { | 59 void AudioOutputProxy::SetVolume(double volume) { |
| 60 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
| 61 volume_ = volume; | 61 volume_ = volume; |
| 62 dispatcher_->StreamVolumeSet(this, volume); | 62 dispatcher_->StreamVolumeSet(this, volume); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void AudioOutputProxy::GetVolume(double* volume) { | 65 void AudioOutputProxy::GetVolume(double* volume) { |
| 66 DCHECK(CalledOnValidThread()); | 66 DCHECK(CalledOnValidThread()); |
| 67 *volume = volume_; | 67 *volume = volume_; |
| 68 } | 68 } |
| 69 | 69 |
| 70 void AudioOutputProxy::Close() { | 70 void AudioOutputProxy::Close() { |
| 71 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
| 72 DCHECK(state_ == kCreated || state_ == kError || state_ == kOpened); | 72 DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened || |
| 73 state_ == kStartError); |
| 73 | 74 |
| 74 if (state_ != kCreated) | 75 // kStartError means OpenStream() succeeded and the stream must be closed |
| 76 // before destruction. |
| 77 if (state_ != kCreated && state_ != kOpenError) |
| 75 dispatcher_->CloseStream(this); | 78 dispatcher_->CloseStream(this); |
| 76 | 79 |
| 77 state_ = kClosed; | 80 state_ = kClosed; |
| 78 | 81 |
| 79 // Delete the object now like is done in the Close() implementation of | 82 // Delete the object now like is done in the Close() implementation of |
| 80 // physical stream objects. If we delete the object via DeleteSoon, we | 83 // physical stream objects. If we delete the object via DeleteSoon, we |
| 81 // unnecessarily complicate the Shutdown procedure of the | 84 // unnecessarily complicate the Shutdown procedure of the |
| 82 // dispatcher+audio manager. | 85 // dispatcher+audio manager. |
| 83 delete this; | 86 delete this; |
| 84 } | 87 } |
| 85 | 88 |
| 86 } // namespace media | 89 } // namespace media |
| OLD | NEW |