Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Side by Side Diff: media/audio/audio_output_device.cc

Issue 1122393004: Add support for switching the audio output device for HTMLMediaElements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes to MediaPlayers so that they invoke callbacks in the correct threads. First complete implem… Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/thread_task_runner_handle.h"
8 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
9 #include "base/time/time.h" 10 #include "base/time/time.h"
10 #include "base/trace_event/trace_event.h" 11 #include "base/trace_event/trace_event.h"
11 #include "media/audio/audio_output_controller.h" 12 #include "media/audio/audio_output_controller.h"
12 #include "media/base/limits.h" 13 #include "media/base/limits.h"
13 14
14 namespace media { 15 namespace media {
15 16
16 // Takes care of invoking the render callback on the audio thread. 17 // Takes care of invoking the render callback on the audio thread.
17 // 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
39 40
40 AudioOutputDevice::AudioOutputDevice( 41 AudioOutputDevice::AudioOutputDevice(
41 scoped_ptr<AudioOutputIPC> ipc, 42 scoped_ptr<AudioOutputIPC> ipc,
42 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) 43 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
43 : ScopedTaskRunnerObserver(io_task_runner), 44 : ScopedTaskRunnerObserver(io_task_runner),
44 callback_(NULL), 45 callback_(NULL),
45 ipc_(ipc.Pass()), 46 ipc_(ipc.Pass()),
46 state_(IDLE), 47 state_(IDLE),
47 play_on_start_(true), 48 play_on_start_(true),
48 session_id_(-1), 49 session_id_(-1),
49 stopping_hack_(false) { 50 stopping_hack_(false),
51 next_switch_request_id_(0) {
50 CHECK(ipc_); 52 CHECK(ipc_);
51 53
52 // 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
53 // State enum. 55 // State enum.
54 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0"); 56 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0");
55 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1"); 57 static_assert(IDLE < CREATING_STREAM, "invalid enum value assignment 1");
56 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2"); 58 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 2");
57 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3"); 59 static_assert(PAUSED < PLAYING, "invalid enum value assignment 3");
58 } 60 }
59 61
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 return false; 113 return false;
112 114
113 if (!task_runner()->PostTask(FROM_HERE, 115 if (!task_runner()->PostTask(FROM_HERE,
114 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { 116 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) {
115 return false; 117 return false;
116 } 118 }
117 119
118 return true; 120 return true;
119 } 121 }
120 122
123 void AudioOutputDevice::SwitchOutputDevice(
124 const std::string& device_id,
125 const GURL& security_origin,
126 const base::Callback<void(int)>& callback) {
127 DVLOG(1) << __FUNCTION__ << "(" << device_id << ")";
128 task_runner()->PostTask(FROM_HERE,
129 base::Bind(&AudioOutputDevice::SwitchOutputDeviceOnIOThread,
130 this,
131 device_id,
132 security_origin,
133 callback));
134 }
135
121 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { 136 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) {
122 DCHECK(task_runner()->BelongsToCurrentThread()); 137 DCHECK(task_runner()->BelongsToCurrentThread());
123 if (state_ == IDLE) { 138 if (state_ == IDLE) {
124 state_ = CREATING_STREAM; 139 state_ = CREATING_STREAM;
125 ipc_->CreateStream(this, params, session_id_); 140 ipc_->CreateStream(this, params, session_id_);
126 } 141 }
127 } 142 }
128 143
129 void AudioOutputDevice::PlayOnIOThread() { 144 void AudioOutputDevice::PlayOnIOThread() {
130 DCHECK(task_runner()->BelongsToCurrentThread()); 145 DCHECK(task_runner()->BelongsToCurrentThread());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 audio_callback_.reset(); 188 audio_callback_.reset();
174 stopping_hack_ = false; 189 stopping_hack_ = false;
175 } 190 }
176 191
177 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { 192 void AudioOutputDevice::SetVolumeOnIOThread(double volume) {
178 DCHECK(task_runner()->BelongsToCurrentThread()); 193 DCHECK(task_runner()->BelongsToCurrentThread());
179 if (state_ >= CREATING_STREAM) 194 if (state_ >= CREATING_STREAM)
180 ipc_->SetVolume(volume); 195 ipc_->SetVolume(volume);
181 } 196 }
182 197
198 void AudioOutputDevice::SwitchOutputDeviceOnIOThread(
199 const std::string& device_id,
200 const GURL& security_origin,
201 const base::Callback<void(int)>& callback) {
202 DCHECK(task_runner()->BelongsToCurrentThread());
203 DVLOG(1) << __FUNCTION__ << "(" << device_id << "," << security_origin << ")";
204 if (state_ >= CREATING_STREAM) {
205 int request_id = AddSwitchRequest(callback);
206 ipc_->SwitchDevice(device_id, security_origin, request_id, callback);
207 }
208 }
209
183 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) { 210 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegate::State state) {
184 DCHECK(task_runner()->BelongsToCurrentThread()); 211 DCHECK(task_runner()->BelongsToCurrentThread());
185 212
186 // Do nothing if the stream has been closed. 213 // Do nothing if the stream has been closed.
187 if (state_ < CREATING_STREAM) 214 if (state_ < CREATING_STREAM)
188 return; 215 return;
189 216
190 // TODO(miu): Clean-up inconsistent and incomplete handling here. 217 // TODO(miu): Clean-up inconsistent and incomplete handling here.
191 // http://crbug.com/180640 218 // http://crbug.com/180640
192 switch (state) { 219 switch (state) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 audio_thread_.Start( 276 audio_thread_.Start(
250 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); 277 audio_callback_.get(), socket_handle, "AudioOutputDevice", true);
251 state_ = PAUSED; 278 state_ = PAUSED;
252 279
253 // 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
254 // multiple times before OnStreamCreated() gets called. 281 // multiple times before OnStreamCreated() gets called.
255 if (play_on_start_) 282 if (play_on_start_)
256 PlayOnIOThread(); 283 PlayOnIOThread();
257 } 284 }
258 285
286 int AudioOutputDevice::AddSwitchRequest(
287 const base::Callback<void(int)>& callback) {
288 DCHECK(task_runner()->BelongsToCurrentThread());
289 DVLOG(1) << __PRETTY_FUNCTION__;
290 int request_id = next_switch_request_id_++;
291 switch_requests_[request_id] = callback;
292 return request_id;
293 }
294
295 void AudioOutputDevice::OnDeviceSwitched(
296 int request_id, AudioOutputIPCDelegate::DeviceSwitchResult result) {
297 DCHECK(task_runner()->BelongsToCurrentThread());
298 DVLOG(1) << __PRETTY_FUNCTION__
299 << "(" << request_id << ", " << result << ")";
300 auto request = switch_requests_.find(request_id);
301 if (request == switch_requests_.end()) {
302 return;
303 }
304
305 request->second.Run(result);
306 switch_requests_.erase(request);
307 }
308
259 void AudioOutputDevice::OnIPCClosed() { 309 void AudioOutputDevice::OnIPCClosed() {
260 DCHECK(task_runner()->BelongsToCurrentThread()); 310 DCHECK(task_runner()->BelongsToCurrentThread());
261 state_ = IPC_CLOSED; 311 state_ = IPC_CLOSED;
262 ipc_.reset(); 312 ipc_.reset();
263 } 313 }
264 314
265 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { 315 void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
266 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; 316 LOG(ERROR) << "IO loop going away before the audio device has been stopped";
267 ShutDownOnIOThread(); 317 ShutDownOnIOThread();
268 } 318 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); 356 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this);
307 } 357 }
308 358
309 // Update the audio-delay measurement then ask client to render audio. Since 359 // Update the audio-delay measurement then ask client to render audio. Since
310 // |output_bus_| is wrapping the shared memory the Render() call is writing 360 // |output_bus_| is wrapping the shared memory the Render() call is writing
311 // directly into the shared memory. 361 // directly into the shared memory.
312 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); 362 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds);
313 } 363 }
314 364
315 } // namespace media. 365 } // namespace media.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698