| 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_device.h" | 5 #include "media/audio/audio_output_device.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/callback_helpers.h" | |
| 10 #include "base/threading/thread_restrictions.h" | 7 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 12 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 13 #include "media/audio/audio_output_controller.h" | 10 #include "media/audio/audio_output_controller.h" |
| 14 #include "media/base/limits.h" | 11 #include "media/base/limits.h" |
| 15 | 12 |
| 16 namespace media { | 13 namespace media { |
| 17 | 14 |
| 18 // Takes care of invoking the render callback on the audio thread. | 15 // Takes care of invoking the render callback on the audio thread. |
| 19 // An instance of this class is created for each capture stream in | 16 // An instance of this class is created for each capture stream in |
| (...skipping 21 matching lines...) Expand all Loading... |
| 41 | 38 |
| 42 AudioOutputDevice::AudioOutputDevice( | 39 AudioOutputDevice::AudioOutputDevice( |
| 43 scoped_ptr<AudioOutputIPC> ipc, | 40 scoped_ptr<AudioOutputIPC> ipc, |
| 44 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 45 : ScopedTaskRunnerObserver(io_task_runner), | 42 : ScopedTaskRunnerObserver(io_task_runner), |
| 46 callback_(NULL), | 43 callback_(NULL), |
| 47 ipc_(ipc.Pass()), | 44 ipc_(ipc.Pass()), |
| 48 state_(IDLE), | 45 state_(IDLE), |
| 49 play_on_start_(true), | 46 play_on_start_(true), |
| 50 session_id_(-1), | 47 session_id_(-1), |
| 51 stopping_hack_(false), | 48 stopping_hack_(false) { |
| 52 current_switch_request_id_(0) { | |
| 53 CHECK(ipc_); | 49 CHECK(ipc_); |
| 54 | 50 |
| 55 // The correctness of the code depends on the relative values assigned in the | 51 // The correctness of the code depends on the relative values assigned in the |
| 56 // State enum. | 52 // State enum. |
| 57 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0"); | 53 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0"); |
| 58 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1"); | 54 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1"); |
| 59 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2"); | 55 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2"); |
| 60 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3"); | 56 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3"); |
| 61 } | 57 } |
| 62 | 58 |
| 63 void AudioOutputDevice::InitializeWithSessionId(const AudioParameters& params, | 59 void AudioOutputDevice::InitializeWithSessionId(const AudioParameters& params, |
| 64 RenderCallback* callback, | 60 RenderCallback* callback, |
| 65 int session_id) { | 61 int session_id) { |
| 66 DCHECK(!callback_) << "Calling InitializeWithSessionId() twice?"; | 62 DCHECK(!callback_) << "Calling InitializeWithSessionId() twice?"; |
| 67 DCHECK(params.IsValid()); | 63 DCHECK(params.IsValid()); |
| 68 audio_parameters_ = params; | 64 audio_parameters_ = params; |
| 69 callback_ = callback; | 65 callback_ = callback; |
| 70 session_id_ = session_id; | 66 session_id_ = session_id; |
| 71 } | 67 } |
| 72 | 68 |
| 73 void AudioOutputDevice::Initialize(const AudioParameters& params, | 69 void AudioOutputDevice::Initialize(const AudioParameters& params, |
| 74 RenderCallback* callback) { | 70 RenderCallback* callback) { |
| 75 InitializeWithSessionId(params, callback, 0); | 71 InitializeWithSessionId(params, callback, 0); |
| 76 } | 72 } |
| 77 | 73 |
| 78 AudioOutputDevice::~AudioOutputDevice() { | 74 AudioOutputDevice::~AudioOutputDevice() { |
| 79 // The current design requires that the user calls Stop() before deleting | 75 // The current design requires that the user calls Stop() before deleting |
| 80 // this class. | 76 // this class. |
| 81 DCHECK(audio_thread_.IsStopped()); | 77 DCHECK(audio_thread_.IsStopped()); |
| 82 | |
| 83 // The following makes it possible for |current_switch_callback_| to release | |
| 84 // its bound parameters in the correct thread instead of implicitly releasing | |
| 85 // them in the thread where this destructor runs. | |
| 86 if (!current_switch_callback_.is_null()) { | |
| 87 base::ResetAndReturn(¤t_switch_callback_).Run( | |
| 88 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE); | |
| 89 } | |
| 90 } | 78 } |
| 91 | 79 |
| 92 void AudioOutputDevice::Start() { | 80 void AudioOutputDevice::Start() { |
| 93 DCHECK(callback_) << "Initialize hasn't been called"; | 81 DCHECK(callback_) << "Initialize hasn't been called"; |
| 94 task_runner()->PostTask(FROM_HERE, | 82 task_runner()->PostTask(FROM_HERE, |
| 95 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, | 83 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, |
| 96 audio_parameters_)); | 84 audio_parameters_)); |
| 97 } | 85 } |
| 98 | 86 |
| 99 void AudioOutputDevice::Stop() { | 87 void AudioOutputDevice::Stop() { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 122 return false; | 110 return false; |
| 123 | 111 |
| 124 if (!task_runner()->PostTask(FROM_HERE, | 112 if (!task_runner()->PostTask(FROM_HERE, |
| 125 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { | 113 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { |
| 126 return false; | 114 return false; |
| 127 } | 115 } |
| 128 | 116 |
| 129 return true; | 117 return true; |
| 130 } | 118 } |
| 131 | 119 |
| 132 void AudioOutputDevice::SwitchOutputDevice( | |
| 133 const std::string& device_id, | |
| 134 const GURL& security_origin, | |
| 135 const SwitchOutputDeviceCB& callback) { | |
| 136 DVLOG(1) << __FUNCTION__ << "(" << device_id << ")"; | |
| 137 task_runner()->PostTask( | |
| 138 FROM_HERE, base::Bind(&AudioOutputDevice::SwitchOutputDeviceOnIOThread, | |
| 139 this, device_id, security_origin, callback)); | |
| 140 } | |
| 141 | |
| 142 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { | 120 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { |
| 143 DCHECK(task_runner()->BelongsToCurrentThread()); | 121 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 144 if (state_ == IDLE) { | 122 if (state_ == IDLE) { |
| 145 state_ = CREATING_STREAM; | 123 state_ = CREATING_STREAM; |
| 146 ipc_->CreateStream(this, params, session_id_); | 124 ipc_->CreateStream(this, params, session_id_); |
| 147 } | 125 } |
| 148 } | 126 } |
| 149 | 127 |
| 150 void AudioOutputDevice::PlayOnIOThread() { | 128 void AudioOutputDevice::PlayOnIOThread() { |
| 151 DCHECK(task_runner()->BelongsToCurrentThread()); | 129 DCHECK(task_runner()->BelongsToCurrentThread()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 audio_callback_.reset(); | 172 audio_callback_.reset(); |
| 195 stopping_hack_ = false; | 173 stopping_hack_ = false; |
| 196 } | 174 } |
| 197 | 175 |
| 198 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { | 176 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { |
| 199 DCHECK(task_runner()->BelongsToCurrentThread()); | 177 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 200 if (state_ >= CREATING_STREAM) | 178 if (state_ >= CREATING_STREAM) |
| 201 ipc_->SetVolume(volume); | 179 ipc_->SetVolume(volume); |
| 202 } | 180 } |
| 203 | 181 |
| 204 void AudioOutputDevice::SwitchOutputDeviceOnIOThread( | |
| 205 const std::string& device_id, | |
| 206 const GURL& security_origin, | |
| 207 const SwitchOutputDeviceCB& callback) { | |
| 208 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 209 DVLOG(1) << __FUNCTION__ << "(" << device_id << "," << security_origin << ")"; | |
| 210 if (state_ >= CREATING_STREAM) { | |
| 211 SetCurrentSwitchRequest(callback); | |
| 212 ipc_->SwitchOutputDevice(device_id, security_origin, | |
| 213 current_switch_request_id_); | |
| 214 } else { | |
| 215 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) { | 182 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) { |
| 220 DCHECK(task_runner()->BelongsToCurrentThread()); | 183 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 221 | 184 |
| 222 // Do nothing if the stream has been closed. | 185 // Do nothing if the stream has been closed. |
| 223 if (state_ < CREATING_STREAM) | 186 if (state_ < CREATING_STREAM) |
| 224 return; | 187 return; |
| 225 | 188 |
| 226 // TODO(miu): Clean-up inconsistent and incomplete handling here. | 189 // TODO(miu): Clean-up inconsistent and incomplete handling here. |
| 227 // http://crbug.com/180640 | 190 // http://crbug.com/180640 |
| 228 switch (state) { | 191 switch (state) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 audio_thread_.Start( | 247 audio_thread_.Start( |
| 285 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); | 248 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); |
| 286 state_ = PAUSED; | 249 state_ = PAUSED; |
| 287 | 250 |
| 288 // We handle the case where Play() and/or Pause() may have been called | 251 // We handle the case where Play() and/or Pause() may have been called |
| 289 // multiple times before OnStreamCreated() gets called. | 252 // multiple times before OnStreamCreated() gets called. |
| 290 if (play_on_start_) | 253 if (play_on_start_) |
| 291 PlayOnIOThread(); | 254 PlayOnIOThread(); |
| 292 } | 255 } |
| 293 | 256 |
| 294 void AudioOutputDevice::SetCurrentSwitchRequest( | |
| 295 const SwitchOutputDeviceCB& callback) { | |
| 296 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 297 DVLOG(1) << __FUNCTION__; | |
| 298 // If there is a previous unresolved request, resolve it as obsolete | |
| 299 if (!current_switch_callback_.is_null()) { | |
| 300 base::ResetAndReturn(¤t_switch_callback_).Run( | |
| 301 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE); | |
| 302 } | |
| 303 current_switch_callback_ = callback; | |
| 304 current_switch_request_id_++; | |
| 305 } | |
| 306 | |
| 307 void AudioOutputDevice::OnOutputDeviceSwitched( | |
| 308 int request_id, | |
| 309 SwitchOutputDeviceResult result) { | |
| 310 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 311 DCHECK(request_id <= current_switch_request_id_); | |
| 312 DVLOG(1) << __FUNCTION__ | |
| 313 << "(" << request_id << ", " << result << ")"; | |
| 314 if (request_id != current_switch_request_id_) { | |
| 315 return; | |
| 316 } | |
| 317 base::ResetAndReturn(¤t_switch_callback_).Run(result); | |
| 318 } | |
| 319 | |
| 320 void AudioOutputDevice::OnIPCClosed() { | 257 void AudioOutputDevice::OnIPCClosed() { |
| 321 DCHECK(task_runner()->BelongsToCurrentThread()); | 258 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 322 state_ = IPC_CLOSED; | 259 state_ = IPC_CLOSED; |
| 323 ipc_.reset(); | 260 ipc_.reset(); |
| 324 } | 261 } |
| 325 | 262 |
| 326 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { | 263 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { |
| 327 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; | 264 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; |
| 328 ShutDownOnIOThread(); | 265 ShutDownOnIOThread(); |
| 329 } | 266 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); | 304 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); |
| 368 } | 305 } |
| 369 | 306 |
| 370 // Update the audio-delay measurement then ask client to render audio. Since | 307 // Update the audio-delay measurement then ask client to render audio. Since |
| 371 // |output_bus_| is wrapping the shared memory the Render() call is writing | 308 // |output_bus_| is wrapping the shared memory the Render() call is writing |
| 372 // directly into the shared memory. | 309 // directly into the shared memory. |
| 373 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); | 310 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); |
| 374 } | 311 } |
| 375 | 312 |
| 376 } // namespace media. | 313 } // namespace media. |
| OLD | NEW |