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