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

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

Issue 1184473002: Add support for the audio-output-device switching IPC mechanism to the renderer lower layers (media… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Change error return code for ClocklessAudioSink to NOT_SUPPORTED 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
« no previous file with comments | « media/audio/audio_output_device.h ('k') | media/audio/audio_output_device_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <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
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
59 void AudioOutputDevice::InitializeWithSessionId(const AudioParameters& params, 62 void AudioOutputDevice::InitializeWithSessionId(const AudioParameters& params,
60 RenderCallback* callback, 63 RenderCallback* callback,
61 int session_id) { 64 int session_id) {
62 DCHECK(!callback_) << "Calling InitializeWithSessionId() twice?"; 65 DCHECK(!callback_) << "Calling InitializeWithSessionId() twice?";
63 DCHECK(params.IsValid()); 66 DCHECK(params.IsValid());
64 audio_parameters_ = params; 67 audio_parameters_ = params;
65 callback_ = callback; 68 callback_ = callback;
66 session_id_ = session_id; 69 session_id_ = session_id;
67 } 70 }
68 71
69 void AudioOutputDevice::Initialize(const AudioParameters& params, 72 void AudioOutputDevice::Initialize(const AudioParameters& params,
70 RenderCallback* callback) { 73 RenderCallback* callback) {
71 InitializeWithSessionId(params, callback, 0); 74 InitializeWithSessionId(params, callback, 0);
72 } 75 }
73 76
74 AudioOutputDevice::~AudioOutputDevice() { 77 AudioOutputDevice::~AudioOutputDevice() {
75 // The current design requires that the user calls Stop() before deleting 78 // The current design requires that the user calls Stop() before deleting
76 // this class. 79 // this class.
77 DCHECK(audio_thread_.IsStopped()); 80 DCHECK(audio_thread_.IsStopped());
81
82 // The following makes it possible for |current_switch_callback_| to release
83 // its bound parameters in the correct thread instead of implicitly releasing
84 // them in the thread where this destructor runs.
85 if (current_switch_callback_) {
86 current_switch_callback_->Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE);
DaleCurtis 2015/06/15 16:58:30 you can use base::ResetAndReturn() to achieve this
87 current_switch_callback_ = NULL;
88 }
78 } 89 }
79 90
80 void AudioOutputDevice::Start() { 91 void AudioOutputDevice::Start() {
81 DCHECK(callback_) << "Initialize hasn't been called"; 92 DCHECK(callback_) << "Initialize hasn't been called";
82 task_runner()->PostTask(FROM_HERE, 93 task_runner()->PostTask(FROM_HERE,
83 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this, 94 base::Bind(&AudioOutputDevice::CreateStreamOnIOThread, this,
84 audio_parameters_)); 95 audio_parameters_));
85 } 96 }
86 97
87 void AudioOutputDevice::Stop() { 98 void AudioOutputDevice::Stop() {
(...skipping 22 matching lines...) Expand all
110 return false; 121 return false;
111 122
112 if (!task_runner()->PostTask(FROM_HERE, 123 if (!task_runner()->PostTask(FROM_HERE,
113 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) { 124 base::Bind(&AudioOutputDevice::SetVolumeOnIOThread, this, volume))) {
114 return false; 125 return false;
115 } 126 }
116 127
117 return true; 128 return true;
118 } 129 }
119 130
131 void AudioOutputDevice::SwitchOutputDevice(
132 const std::string& device_id,
133 const GURL& security_origin,
134 const SwitchOutputDeviceCB& callback) {
135 DVLOG(1) << __FUNCTION__ << "(" << device_id << ")";
136 task_runner()->PostTask(
137 FROM_HERE, base::Bind(&AudioOutputDevice::SwitchOutputDeviceOnIOThread,
138 this, device_id, security_origin, callback));
139 }
140
120 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { 141 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) {
121 DCHECK(task_runner()->BelongsToCurrentThread()); 142 DCHECK(task_runner()->BelongsToCurrentThread());
122 if (state_ == IDLE) { 143 if (state_ == IDLE) {
123 state_ = CREATING_STREAM; 144 state_ = CREATING_STREAM;
124 ipc_->CreateStream(this, params, session_id_); 145 ipc_->CreateStream(this, params, session_id_);
125 } 146 }
126 } 147 }
127 148
128 void AudioOutputDevice::PlayOnIOThread() { 149 void AudioOutputDevice::PlayOnIOThread() {
129 DCHECK(task_runner()->BelongsToCurrentThread()); 150 DCHECK(task_runner()->BelongsToCurrentThread());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 audio_callback_.reset(); 193 audio_callback_.reset();
173 stopping_hack_ = false; 194 stopping_hack_ = false;
174 } 195 }
175 196
176 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { 197 void AudioOutputDevice::SetVolumeOnIOThread(double volume) {
177 DCHECK(task_runner()->BelongsToCurrentThread()); 198 DCHECK(task_runner()->BelongsToCurrentThread());
178 if (state_ >= CREATING_STREAM) 199 if (state_ >= CREATING_STREAM)
179 ipc_->SetVolume(volume); 200 ipc_->SetVolume(volume);
180 } 201 }
181 202
203 void AudioOutputDevice::SwitchOutputDeviceOnIOThread(
204 const std::string& device_id,
205 const GURL& security_origin,
206 const SwitchOutputDeviceCB& callback) {
207 DCHECK(task_runner()->BelongsToCurrentThread());
208 DVLOG(1) << __FUNCTION__ << "(" << device_id << "," << security_origin << ")";
209 if (state_ >= CREATING_STREAM) {
210 SetCurrentSwitchRequest(callback);
211 ipc_->SwitchOutputDevice(device_id, security_origin,
212 current_switch_request_id_);
213 } else {
214 callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED);
215 }
216 }
217
182 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) { 218 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) {
183 DCHECK(task_runner()->BelongsToCurrentThread()); 219 DCHECK(task_runner()->BelongsToCurrentThread());
184 220
185 // Do nothing if the stream has been closed. 221 // Do nothing if the stream has been closed.
186 if (state_ < CREATING_STREAM) 222 if (state_ < CREATING_STREAM)
187 return; 223 return;
188 224
189 // TODO(miu): Clean-up inconsistent and incomplete handling here. 225 // TODO(miu): Clean-up inconsistent and incomplete handling here.
190 // http://crbug.com/180640 226 // http://crbug.com/180640
191 switch (state) { 227 switch (state) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 audio_thread_.Start( 283 audio_thread_.Start(
248 audio_callback_.get(), socket_handle, "AudioOutputDevice", true); 284 audio_callback_.get(), socket_handle, "AudioOutputDevice", true);
249 state_ = PAUSED; 285 state_ = PAUSED;
250 286
251 // We handle the case where Play() and/or Pause() may have been called 287 // We handle the case where Play() and/or Pause() may have been called
252 // multiple times before OnStreamCreated() gets called. 288 // multiple times before OnStreamCreated() gets called.
253 if (play_on_start_) 289 if (play_on_start_)
254 PlayOnIOThread(); 290 PlayOnIOThread();
255 } 291 }
256 292
293 void AudioOutputDevice::SetCurrentSwitchRequest(
294 const SwitchOutputDeviceCB& callback) {
295 DCHECK(task_runner()->BelongsToCurrentThread());
296 DVLOG(1) << __FUNCTION__;
297 // If there is a previous unresolved request, resolve it as obsolete
298 if (current_switch_callback_) {
299 current_switch_callback_->Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE);
300 }
301 current_switch_callback_.reset(new SwitchOutputDeviceCB(callback));
302 current_switch_request_id_++;
303 }
304
305 void AudioOutputDevice::OnOutputDeviceSwitched(
306 int request_id,
307 SwitchOutputDeviceResult result) {
308 DCHECK(task_runner()->BelongsToCurrentThread());
309 DCHECK(request_id <= current_switch_request_id_);
310 DVLOG(1) << __FUNCTION__
311 << "(" << request_id << ", " << result << ")";
312 if (request_id != current_switch_request_id_) {
313 return;
314 }
315 current_switch_callback_->Run(result);
316 current_switch_callback_ = NULL;
317 }
318
257 void AudioOutputDevice::OnIPCClosed() { 319 void AudioOutputDevice::OnIPCClosed() {
258 DCHECK(task_runner()->BelongsToCurrentThread()); 320 DCHECK(task_runner()->BelongsToCurrentThread());
259 state_ = IPC_CLOSED; 321 state_ = IPC_CLOSED;
260 ipc_.reset(); 322 ipc_.reset();
261 } 323 }
262 324
263 void AudioOutputDevice::WillDestroyCurrentMessageLoop() { 325 void AudioOutputDevice::WillDestroyCurrentMessageLoop() {
264 LOG(ERROR) << "IO loop going away before the audio device has been stopped"; 326 LOG(ERROR) << "IO loop going away before the audio device has been stopped";
265 ShutDownOnIOThread(); 327 ShutDownOnIOThread();
266 } 328 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this); 366 TRACE_EVENT_ASYNC_END0("audio", "StartingPlayback", this);
305 } 367 }
306 368
307 // Update the audio-delay measurement then ask client to render audio. Since 369 // 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 370 // |output_bus_| is wrapping the shared memory the Render() call is writing
309 // directly into the shared memory. 371 // directly into the shared memory.
310 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds); 372 render_callback_->Render(output_bus_.get(), audio_delay_milliseconds);
311 } 373 }
312 374
313 } // namespace media. 375 } // namespace media.
OLDNEW
« no previous file with comments | « media/audio/audio_output_device.h ('k') | media/audio/audio_output_device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698