| 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 |
| 7 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 8 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 9 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
| 10 #include "media/audio/audio_output_controller.h" | 12 #include "media/audio/audio_output_controller.h" |
| 11 #include "media/base/limits.h" | 13 #include "media/base/limits.h" |
| 12 | 14 |
| 13 namespace media { | 15 namespace media { |
| 14 | 16 |
| 15 // Takes care of invoking the render callback on the audio thread. | 17 // Takes care of invoking the render callback on the audio thread. |
| 16 // An instance of this class is created for each capture stream in | 18 // An instance of this class is created for each capture stream in |
| (...skipping 21 matching lines...) Expand all Loading... |
| 38 | 40 |
| 39 AudioOutputDevice::AudioOutputDevice( | 41 AudioOutputDevice::AudioOutputDevice( |
| 40 scoped_ptr<AudioOutputIPC> ipc, | 42 scoped_ptr<AudioOutputIPC> ipc, |
| 41 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | 43 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 42 : ScopedTaskRunnerObserver(io_task_runner), | 44 : ScopedTaskRunnerObserver(io_task_runner), |
| 43 callback_(NULL), | 45 callback_(NULL), |
| 44 ipc_(ipc.Pass()), | 46 ipc_(ipc.Pass()), |
| 45 state_(IDLE), | 47 state_(IDLE), |
| 46 play_on_start_(true), | 48 play_on_start_(true), |
| 47 session_id_(-1), | 49 session_id_(-1), |
| 48 stopping_hack_(false) { | 50 stopping_hack_(false), |
| 51 current_switch_request_id_(0) { |
| 49 CHECK(ipc_); | 52 CHECK(ipc_); |
| 50 | 53 |
| 51 // The correctness of the code depends on the relative values assigned in the | 54 // The correctness of the code depends on the relative values assigned in the |
| 52 // State enum. | 55 // State enum. |
| 53 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0"); | 56 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0"); |
| 54 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1"); | 57 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1"); |
| 55 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2"); | 58 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2"); |
| 56 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3"); | 59 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3"); |
| 57 } | 60 } |
| 58 | 61 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 return false; | 113 return false; |
| 111 | 114 |
| 112 if (!task_runner()->PostTask(FROM_HERE, | 115 if (!task_runner()->PostTask(FROM_HERE, |
| 113 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { | 116 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { |
| 114 return false; | 117 return false; |
| 115 } | 118 } |
| 116 | 119 |
| 117 return true; | 120 return true; |
| 118 } | 121 } |
| 119 | 122 |
| 123 void AudioOutputDevice::SwitchOutputDevice( |
| 124 const std::string& device_id, |
| 125 const GURL& security_origin, |
| 126 scoped_ptr<SwitchOutputDeviceCallbackRunner> callback_runner) { |
| 127 DVLOG(1) << __FUNCTION__ << "(" << device_id << ")"; |
| 128 task_runner()->PostTask( |
| 129 FROM_HERE, base::Bind(&AudioOutputDevice::SwitchOutputDeviceOnIOThread, |
| 130 this, device_id, security_origin, |
| 131 base::Passed(callback_runner.Pass()))); |
| 132 } |
| 133 |
| 120 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { | 134 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { |
| 121 DCHECK(task_runner()->BelongsToCurrentThread()); | 135 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 122 if (state_ == IDLE) { | 136 if (state_ == IDLE) { |
| 123 state_ = CREATING_STREAM; | 137 state_ = CREATING_STREAM; |
| 124 ipc_->CreateStream(this, params, session_id_); | 138 ipc_->CreateStream(this, params, session_id_); |
| 125 } | 139 } |
| 126 } | 140 } |
| 127 | 141 |
| 128 void AudioOutputDevice::PlayOnIOThread() { | 142 void AudioOutputDevice::PlayOnIOThread() { |
| 129 DCHECK(task_runner()->BelongsToCurrentThread()); | 143 DCHECK(task_runner()->BelongsToCurrentThread()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 audio_callback_.reset(); | 186 audio_callback_.reset(); |
| 173 stopping_hack_ = false; | 187 stopping_hack_ = false; |
| 174 } | 188 } |
| 175 | 189 |
| 176 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { | 190 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { |
| 177 DCHECK(task_runner()->BelongsToCurrentThread()); | 191 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 178 if (state_ >= CREATING_STREAM) | 192 if (state_ >= CREATING_STREAM) |
| 179 ipc_->SetVolume(volume); | 193 ipc_->SetVolume(volume); |
| 180 } | 194 } |
| 181 | 195 |
| 196 void AudioOutputDevice::SwitchOutputDeviceOnIOThread( |
| 197 const std::string& device_id, |
| 198 const GURL& security_origin, |
| 199 scoped_ptr<SwitchOutputDeviceCallbackRunner> callback_runner) { |
| 200 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 201 DVLOG(1) << __FUNCTION__ << "(" << device_id << "," << security_origin << ")"; |
| 202 if (state_ >= CREATING_STREAM) { |
| 203 SetCurrentSwitchRequest(callback_runner.Pass()); |
| 204 ipc_->SwitchOutputDevice(device_id, security_origin, |
| 205 current_switch_request_id_); |
| 206 } else { |
| 207 callback_runner->Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED); |
| 208 } |
| 209 } |
| 210 |
| 182 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { | 211 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { |
| 183 DCHECK(task_runner()->BelongsToCurrentThread()); | 212 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 184 | 213 |
| 185 // Do nothing if the stream has been closed. | 214 // Do nothing if the stream has been closed. |
| 186 if (state_ < CREATING_STREAM) | 215 if (state_ < CREATING_STREAM) |
| 187 return; | 216 return; |
| 188 | 217 |
| 189 // TODO(miu): Clean-up inconsistent and incomplete handling here. | 218 // TODO(miu): Clean-up inconsistent and incomplete handling here. |
| 190 // http://crbug.com/180640 | 219 // http://crbug.com/180640 |
| 191 switch (state) { | 220 switch (state) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 audio_thread_.Start( | 276 audio_thread_.Start( |
| 248 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); | 277 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); |
| 249 state_ = PAUSED; | 278 state_ = PAUSED; |
| 250 | 279 |
| 251 // We handle the case where Play() and/or Pause() may have been called | 280 // We handle the case where Play() and/or Pause() may have been called |
| 252 // multiple times before OnStreamCreated() gets called. | 281 // multiple times before OnStreamCreated() gets called. |
| 253 if (play_on_start_) | 282 if (play_on_start_) |
| 254 PlayOnIOThread(); | 283 PlayOnIOThread(); |
| 255 } | 284 } |
| 256 | 285 |
| 286 void AudioOutputDevice::SetCurrentSwitchRequest( |
| 287 scoped_ptr<SwitchOutputDeviceCallbackRunner> callback_runner) { |
| 288 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 289 DVLOG(1) << __FUNCTION__; |
| 290 // If there is a previous unresolved request, resolve it as obsolete |
| 291 if (current_switch_callback_runner_) { |
| 292 current_switch_callback_runner_->Run( |
| 293 SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE); |
| 294 } |
| 295 current_switch_callback_runner_ = callback_runner.Pass(); |
| 296 current_switch_request_id_++; |
| 297 } |
| 298 |
| 299 void AudioOutputDevice::OnOutputDeviceSwitched( |
| 300 int request_id, SwitchOutputDeviceResult result) { |
| 301 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 302 DCHECK(request_id <= current_switch_request_id_); |
| 303 DVLOG(1) << __FUNCTION__ |
| 304 << "(" << request_id << ", " << result << ")"; |
| 305 if (request_id != current_switch_request_id_) { |
| 306 return; |
| 307 } |
| 308 current_switch_callback_runner_->Run(result); |
| 309 current_switch_callback_runner_ = NULL; |
| 310 } |
| 311 |
| 257 void AudioOutputDevice::OnIPCClosed() { | 312 void AudioOutputDevice::OnIPCClosed() { |
| 258 DCHECK(task_runner()->BelongsToCurrentThread()); | 313 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 259 state_ = IPC_CLOSED; | 314 state_ = IPC_CLOSED; |
| 260 ipc_.reset(); | 315 ipc_.reset(); |
| 261 } | 316 } |
| 262 | 317 |
| 263 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { | 318 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { |
| 264 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; | 319 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; |
| 265 ShutDownOnIOThread(); | 320 ShutDownOnIOThread(); |
| 266 } | 321 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); | 359 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); |
| 305 } | 360 } |
| 306 | 361 |
| 307 // Update the audio-delay measurement then ask client to render audio. Since | 362 // 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 | 363 // |output_bus_| is wrapping the shared memory the Render() call is writing |
| 309 // directly into the shared memory. | 364 // directly into the shared memory. |
| 310 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); | 365 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); |
| 311 } | 366 } |
| 312 | 367 |
| 313 } // namespace media. | 368 } // namespace media. |
| OLD | NEW |