OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 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/restartable_audio_output_device_impl.h" |
| 6 |
| 7 #include "media/audio/audio_output_device.h" |
| 8 |
| 9 namespace media { |
| 10 |
| 11 RestartableAudioOutputDeviceImpl::RestartableAudioOutputDeviceImpl( |
| 12 const GetSinkCB& get_sink_cb, |
| 13 const GetHWParamsCB& get_hw_params_cb) |
| 14 : state_(kStopped), |
| 15 volume_(1.0f), |
| 16 render_cb_(nullptr), |
| 17 get_sink_cb_(get_sink_cb), |
| 18 get_hw_params_cb_(get_hw_params_cb), |
| 19 // Creating output device now, since it may be needed to get output |
| 20 // parameters before the device is started. |
| 21 sink_(get_sink_cb_.Run()) {} |
| 22 |
| 23 void RestartableAudioOutputDeviceImpl::Initialize(const AudioParameters& params, |
| 24 RenderCallback* callback) { |
| 25 DCHECK_EQ(state_, kStopped) << "Calling Initialize() on a running device."; |
| 26 DCHECK(callback); |
| 27 |
| 28 params_ = params; |
| 29 render_cb_ = callback; |
| 30 } |
| 31 |
| 32 RestartableAudioOutputDeviceImpl::~RestartableAudioOutputDeviceImpl() { |
| 33 DCHECK_EQ(state_, kStopped) << "Stop() has not been called."; |
| 34 if (sink_) |
| 35 sink_->Stop(); // AudioOutputDevice must be stopped. |
| 36 } |
| 37 |
| 38 void RestartableAudioOutputDeviceImpl::Start() { |
| 39 DCHECK_EQ(state_, kStopped) << "Already started."; |
| 40 DCHECK(render_cb_) << "Initialize() has not been called."; |
| 41 |
| 42 state_ = kStarted; |
| 43 |
| 44 if (!sink_) { |
| 45 // Restarting the device after it was stopped. |
| 46 sink_ = get_sink_cb_.Run(); // Restarting the device after it was stopped. |
| 47 } // Otherwise the sink is created in the constructor. |
| 48 |
| 49 if (sink_->GetDeviceStatus() == OUTPUT_DEVICE_STATUS_OK) |
| 50 sink_->Initialize(params_, render_cb_); |
| 51 } |
| 52 |
| 53 void RestartableAudioOutputDeviceImpl::Stop() { |
| 54 if (sink_) { |
| 55 sink_->Stop(); |
| 56 sink_ = nullptr; // AudioOutputDevice cannot be restarted. |
| 57 } |
| 58 state_ = kStopped; |
| 59 } |
| 60 |
| 61 void RestartableAudioOutputDeviceImpl::Play() { |
| 62 if ((state_ == kPlaying) || (state_ == kStopped)) |
| 63 return; |
| 64 |
| 65 if (state_ == kPaused) |
| 66 sink_->Play(); |
| 67 else { // kStarted, it's the first time Play() is called. |
| 68 sink_->Start(); // AudioOutputDevice plays on start. |
| 69 sink_->SetVolume(volume_); |
| 70 } |
| 71 state_ = kPlaying; |
| 72 } |
| 73 |
| 74 void RestartableAudioOutputDeviceImpl::Pause() { |
| 75 if (state_ != kPlaying) |
| 76 return; |
| 77 |
| 78 sink_->Pause(); |
| 79 state_ = kPaused; |
| 80 } |
| 81 |
| 82 bool RestartableAudioOutputDeviceImpl::SetVolume(double volume) { |
| 83 volume_ = volume; |
| 84 if (sink_ && ((state_ == kPaused) || (state_ == kPlaying))) |
| 85 return sink_->SetVolume(volume_); // sink_->Start() is already called. |
| 86 return true; |
| 87 } |
| 88 |
| 89 OutputDevice* RestartableAudioOutputDeviceImpl::GetOutputDevice() { |
| 90 return this; |
| 91 } |
| 92 |
| 93 void RestartableAudioOutputDeviceImpl::SwitchOutputDevice( |
| 94 const std::string& device_id, |
| 95 const url::Origin& security_origin, |
| 96 const SwitchOutputDeviceCB& callback) { |
| 97 NOTREACHED(); |
| 98 } |
| 99 |
| 100 AudioParameters RestartableAudioOutputDeviceImpl::GetOutputParameters() { |
| 101 if (sink_) |
| 102 return sink_->GetOutputParameters(); |
| 103 |
| 104 return get_hw_params_cb_.Run(); |
| 105 } |
| 106 |
| 107 OutputDeviceStatus RestartableAudioOutputDeviceImpl::GetDeviceStatus() { |
| 108 if (sink_) |
| 109 return sink_->GetDeviceStatus(); |
| 110 |
| 111 // If there is no sink because the device was stopped: |
| 112 if (state_ == kStopped) |
| 113 return OUTPUT_DEVICE_STATUS_OK; |
| 114 |
| 115 return OUTPUT_DEVICE_STATUS_ERROR_INTERNAL; |
| 116 } |
| 117 |
| 118 } // namespace media |
OLD | NEW |