| 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( | 14 AudioOutputProxy::AudioOutputProxy( |
| 15 base::WeakPtr<AudioOutputDispatcher> dispatcher) | 15 base::WeakPtr<AudioOutputDispatcher> dispatcher) |
| 16 : dispatcher_(std::move(dispatcher)), state_(kCreated), volume_(1.0) { | 16 : dispatcher_(std::move(dispatcher)), state_(kCreated), volume_(1.0) { |
| 17 DCHECK(dispatcher_); | 17 DCHECK(dispatcher_); |
| 18 } | 18 } |
| 19 | 19 |
| 20 AudioOutputProxy::~AudioOutputProxy() { | 20 AudioOutputProxy::~AudioOutputProxy() { |
| 21 DCHECK(CalledOnValidThread()); | 21 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 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_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 27 DCHECK_EQ(state_, kCreated); | 27 DCHECK_EQ(state_, kCreated); |
| 28 | 28 |
| 29 if (!dispatcher_ || !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_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 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_ || !dispatcher_->StartStream(callback, this)) { | 46 if (!dispatcher_ || !dispatcher_->StartStream(callback, this)) { |
| 47 state_ = kStartError; | 47 state_ = kStartError; |
| 48 callback->OnError(); | 48 callback->OnError(); |
| 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_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 56 if (state_ != kPlaying) | 56 if (state_ != kPlaying) |
| 57 return; | 57 return; |
| 58 | 58 |
| 59 if (dispatcher_) | 59 if (dispatcher_) |
| 60 dispatcher_->StopStream(this); | 60 dispatcher_->StopStream(this); |
| 61 state_ = kOpened; | 61 state_ = kOpened; |
| 62 } | 62 } |
| 63 | 63 |
| 64 void AudioOutputProxy::SetVolume(double volume) { | 64 void AudioOutputProxy::SetVolume(double volume) { |
| 65 DCHECK(CalledOnValidThread()); | 65 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 66 volume_ = volume; | 66 volume_ = volume; |
| 67 | 67 |
| 68 if (dispatcher_) | 68 if (dispatcher_) |
| 69 dispatcher_->StreamVolumeSet(this, volume); | 69 dispatcher_->StreamVolumeSet(this, volume); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void AudioOutputProxy::GetVolume(double* volume) { | 72 void AudioOutputProxy::GetVolume(double* volume) { |
| 73 DCHECK(CalledOnValidThread()); | 73 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 74 *volume = volume_; | 74 *volume = volume_; |
| 75 } | 75 } |
| 76 | 76 |
| 77 void AudioOutputProxy::Close() { | 77 void AudioOutputProxy::Close() { |
| 78 DCHECK(CalledOnValidThread()); | 78 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 79 DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened || | 79 DCHECK(state_ == kCreated || state_ == kOpenError || state_ == kOpened || |
| 80 state_ == kStartError); | 80 state_ == kStartError); |
| 81 | 81 |
| 82 // kStartError means OpenStream() succeeded and the stream must be closed | 82 // kStartError means OpenStream() succeeded and the stream must be closed |
| 83 // before destruction. | 83 // before destruction. |
| 84 if (state_ != kCreated && state_ != kOpenError && dispatcher_) | 84 if (state_ != kCreated && state_ != kOpenError && dispatcher_) |
| 85 dispatcher_->CloseStream(this); | 85 dispatcher_->CloseStream(this); |
| 86 | 86 |
| 87 state_ = kClosed; | 87 state_ = kClosed; |
| 88 | 88 |
| 89 // 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 |
| 90 // physical stream objects. If we delete the object via DeleteSoon, we | 90 // physical stream objects. If we delete the object via DeleteSoon, we |
| 91 // unnecessarily complicate the Shutdown procedure of the | 91 // unnecessarily complicate the Shutdown procedure of the |
| 92 // dispatcher+audio manager. | 92 // dispatcher+audio manager. |
| 93 delete this; | 93 delete this; |
| 94 } | 94 } |
| 95 | 95 |
| 96 } // namespace media | 96 } // namespace media |
| OLD | NEW |