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

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

Issue 2043883005: Implementing AudioOutputDevice authorization timeout (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: guidou@'s comments addressed Created 4 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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <cmath> 10 #include <cmath>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/callback_helpers.h" 13 #include "base/callback_helpers.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/metrics/histogram_macros.h"
15 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
17 #include "base/trace_event/trace_event.h" 19 #include "base/trace_event/trace_event.h"
18 #include "build/build_config.h" 20 #include "build/build_config.h"
19 #include "media/audio/audio_device_description.h" 21 #include "media/audio/audio_device_description.h"
20 #include "media/audio/audio_output_controller.h" 22 #include "media/audio/audio_output_controller.h"
21 #include "media/base/limits.h" 23 #include "media/base/limits.h"
22 24
23 namespace media { 25 namespace media {
24 26
25 // Takes care of invoking the render callback on the audio thread. 27 // Takes care of invoking the render callback on the audio thread.
26 // An instance of this class is created for each capture stream in 28 // An instance of this class is created for each capture stream in
(...skipping 18 matching lines...) Expand all
45 uint64_t callback_num_; 47 uint64_t callback_num_;
46 48
47 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); 49 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback);
48 }; 50 };
49 51
50 AudioOutputDevice::AudioOutputDevice( 52 AudioOutputDevice::AudioOutputDevice(
51 std::unique_ptr<AudioOutputIPC> ipc, 53 std::unique_ptr<AudioOutputIPC> ipc,
52 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, 54 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
53 int session_id, 55 int session_id,
54 const std::string& device_id, 56 const std::string& device_id,
55 const url::Origin& security_origin) 57 const url::Origin& security_origin,
58 base::TimeDelta authorization_timeout)
56 : ScopedTaskRunnerObserver(io_task_runner), 59 : ScopedTaskRunnerObserver(io_task_runner),
57 callback_(NULL), 60 callback_(NULL),
58 ipc_(std::move(ipc)), 61 ipc_(std::move(ipc)),
59 state_(IDLE), 62 state_(IDLE),
60 start_on_authorized_(false), 63 start_on_authorized_(false),
61 play_on_start_(true), 64 play_on_start_(true),
62 session_id_(session_id), 65 session_id_(session_id),
63 device_id_(device_id), 66 device_id_(device_id),
64 security_origin_(security_origin), 67 security_origin_(security_origin),
65 stopping_hack_(false), 68 stopping_hack_(false),
66 did_receive_auth_(base::WaitableEvent::ResetPolicy::MANUAL, 69 did_receive_auth_(base::WaitableEvent::ResetPolicy::MANUAL,
67 base::WaitableEvent::InitialState::NOT_SIGNALED), 70 base::WaitableEvent::InitialState::NOT_SIGNALED),
68 device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) { 71 device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL),
72 auth_timeout_(authorization_timeout) {
69 CHECK(ipc_); 73 CHECK(ipc_);
70 74
71 // The correctness of the code depends on the relative values assigned in the 75 // The correctness of the code depends on the relative values assigned in the
72 // State enum. 76 // State enum.
73 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0"); 77 static_assert(IPC_CLOSED < IDLE, "invalid enum value assignment 0");
74 static_assert(IDLE < AUTHORIZING, "invalid enum value assignment 1"); 78 static_assert(IDLE < AUTHORIZING, "invalid enum value assignment 1");
75 static_assert(AUTHORIZING < AUTHORIZED, "invalid enum value assignment 2"); 79 static_assert(AUTHORIZING < AUTHORIZED, "invalid enum value assignment 2");
76 static_assert(AUTHORIZED < CREATING_STREAM, 80 static_assert(AUTHORIZED < CREATING_STREAM,
77 "invalid enum value assignment 3"); 81 "invalid enum value assignment 3");
78 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 4"); 82 static_assert(CREATING_STREAM < PAUSED, "invalid enum value assignment 4");
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 : device_id_, 153 : device_id_,
150 device_status_, output_params_); 154 device_status_, output_params_);
151 } 155 }
152 156
153 void AudioOutputDevice::RequestDeviceAuthorizationOnIOThread() { 157 void AudioOutputDevice::RequestDeviceAuthorizationOnIOThread() {
154 DCHECK(task_runner()->BelongsToCurrentThread()); 158 DCHECK(task_runner()->BelongsToCurrentThread());
155 DCHECK_EQ(state_, IDLE); 159 DCHECK_EQ(state_, IDLE);
156 state_ = AUTHORIZING; 160 state_ = AUTHORIZING;
157 ipc_->RequestDeviceAuthorization(this, session_id_, device_id_, 161 ipc_->RequestDeviceAuthorization(this, session_id_, device_id_,
158 security_origin_); 162 security_origin_);
163
164 // Create the timer on the thread it's used on. It's guaranteed to be
165 // deleted on the same thread since users must call Stop() before deleting
166 // AudioOutputDevice; see ShutDownOnIOThread().
167 auth_timeout_action_.reset(new base::OneShotTimer());
168 auth_timeout_action_->Start(
169 FROM_HERE, auth_timeout_,
170 base::Bind(&AudioOutputDevice::ProcessDeviceAuthorizationOnIOThread, this,
171 OUTPUT_DEVICE_STATUS_ERROR_INTERNAL, media::AudioParameters(),
DaleCurtis 2016/06/13 18:30:09 This is fine with me, but have you considered addi
o1ka 2016/06/14 10:33:05 Good idea! Done.
172 std::string(), true /* timed out */));
159 } 173 }
160 174
161 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { 175 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) {
162 DCHECK(task_runner()->BelongsToCurrentThread()); 176 DCHECK(task_runner()->BelongsToCurrentThread());
163 switch (state_) { 177 switch (state_) {
164 case IPC_CLOSED: 178 case IPC_CLOSED:
165 if (callback_) 179 if (callback_)
166 callback_->OnRenderError(); 180 callback_->OnRenderError();
167 break; 181 break;
168 182
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 void AudioOutputDevice::ShutDownOnIOThread() { 236 void AudioOutputDevice::ShutDownOnIOThread() {
223 DCHECK(task_runner()->BelongsToCurrentThread()); 237 DCHECK(task_runner()->BelongsToCurrentThread());
224 238
225 // Close the stream, if we haven't already. 239 // Close the stream, if we haven't already.
226 if (state_ >= AUTHORIZING) { 240 if (state_ >= AUTHORIZING) {
227 ipc_->CloseStream(); 241 ipc_->CloseStream();
228 state_ = IDLE; 242 state_ = IDLE;
229 } 243 }
230 start_on_authorized_ = false; 244 start_on_authorized_ = false;
231 245
246 // Destoy the timer on the thread it's used on.
247 auth_timeout_action_.reset(nullptr);
DaleCurtis 2016/06/13 18:30:09 reset(nullptr) is unnecessary. just .reset()
o1ka 2016/06/14 10:33:05 Done.
248
232 // We can run into an issue where ShutDownOnIOThread is called right after 249 // We can run into an issue where ShutDownOnIOThread is called right after
233 // OnStreamCreated is called in cases where Start/Stop are called before we 250 // OnStreamCreated is called in cases where Start/Stop are called before we
234 // get the OnStreamCreated callback. To handle that corner case, we call 251 // get the OnStreamCreated callback. To handle that corner case, we call
235 // Stop(). In most cases, the thread will already be stopped. 252 // Stop(). In most cases, the thread will already be stopped.
236 // 253 //
237 // Another situation is when the IO thread goes away before Stop() is called 254 // Another situation is when the IO thread goes away before Stop() is called
238 // in which case, we cannot use the message loop to close the thread handle 255 // in which case, we cannot use the message loop to close the thread handle
239 // and can't rely on the main thread existing either. 256 // and can't rely on the main thread existing either.
240 base::AutoLock auto_lock_(audio_thread_lock_); 257 base::AutoLock auto_lock_(audio_thread_lock_);
241 base::ThreadRestrictions::ScopedAllowIO allow_io; 258 base::ThreadRestrictions::ScopedAllowIO allow_io;
242 audio_thread_.Stop(NULL); 259 audio_thread_.Stop(NULL);
243 audio_callback_.reset(); 260 audio_callback_.reset();
244 stopping_hack_ = false; 261 stopping_hack_ = false;
245 } 262 }
246 263
247 void AudioOutputDevice::SetVolumeOnIOThread(double volume) { 264 void AudioOutputDevice::SetVolumeOnIOThread(double volume) {
248 DCHECK(task_runner()->BelongsToCurrentThread()); 265 DCHECK(task_runner()->BelongsToCurrentThread());
249 if (state_ >= CREATING_STREAM) 266 if (state_ >= CREATING_STREAM)
250 ipc_->SetVolume(volume); 267 ipc_->SetVolume(volume);
251 } 268 }
252 269
253 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) { 270 void AudioOutputDevice::ProcessDeviceAuthorizationOnIOThread(
271 OutputDeviceStatus device_status,
272 const media::AudioParameters& output_params,
273 const std::string& matched_device_id,
274 bool timed_out) {
254 DCHECK(task_runner()->BelongsToCurrentThread()); 275 DCHECK(task_runner()->BelongsToCurrentThread());
255 276
256 // Do nothing if the stream has been closed. 277 if (auth_timeout_action_) {
DaleCurtis 2016/06/13 18:30:09 Just auth_timeout_action_.reset() is sufficient.
o1ka 2016/06/14 10:33:05 Done.
257 if (state_ < CREATING_STREAM) 278 auth_timeout_action_->Stop();
279 auth_timeout_action_.reset(nullptr);
280 }
281
282 // Do nothing if late authorization is received after timeout.
283 if (state_ == IPC_CLOSED && !timed_out)
258 return; 284 return;
259 285
260 // TODO(miu): Clean-up inconsistent and incomplete handling here. 286 UMA_HISTOGRAM_BOOLEAN("Media.Audio.Render.OutputDeviceAuthorizationTimedOut",
261 // http://crbug.com/180640 287 timed_out);
262 switch (state) {
263 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING:
264 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED:
265 break;
266 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR:
267 DLOG(WARNING) << "AudioOutputDevice::OnStateChanged(ERROR)";
268 // Don't dereference the callback object if the audio thread
269 // is stopped or stopping. That could mean that the callback
270 // object has been deleted.
271 // TODO(tommi): Add an explicit contract for clearing the callback
272 // object. Possibly require calling Initialize again or provide
273 // a callback object via Start() and clear it in Stop().
274 if (!audio_thread_.IsStopped())
275 callback_->OnRenderError();
276 break;
277 default:
278 NOTREACHED();
279 break;
280 }
281 }
282 288
283 void AudioOutputDevice::OnDeviceAuthorized( 289 DCHECK(!timed_out || device_status == OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
284 OutputDeviceStatus device_status,
285 const media::AudioParameters& output_params,
286 const std::string& matched_device_id) {
287 DCHECK(task_runner()->BelongsToCurrentThread());
288 DCHECK_EQ(state_, AUTHORIZING); 290 DCHECK_EQ(state_, AUTHORIZING);
289 291
290 // It may happen that a second authorization is received as a result to a 292 // It may happen that a second authorization is received as a result to a
291 // call to Start() after Stop(). If the status for the second authorization 293 // call to Start() after Stop(). If the status for the second authorization
292 // differs from the first, it will not be reflected in |device_status_| 294 // differs from the first, it will not be reflected in |device_status_|
293 // to avoid a race. 295 // to avoid a race.
294 // This scenario is unlikely. If it occurs, the new value will be 296 // This scenario is unlikely. If it occurs, the new value will be
295 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice 297 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice
296 // will enter the IPC_CLOSED state anyway, which is the safe thing to do. 298 // will enter the IPC_CLOSED state anyway, which is the safe thing to do.
297 // This is preferable to holding a lock. 299 // This is preferable to holding a lock.
(...skipping 24 matching lines...) Expand all
322 } else { 324 } else {
323 // Closing IPC forces a Signal(), so no clients are locked waiting 325 // Closing IPC forces a Signal(), so no clients are locked waiting
324 // indefinitely after this method returns. 326 // indefinitely after this method returns.
325 ipc_->CloseStream(); 327 ipc_->CloseStream();
326 OnIPCClosed(); 328 OnIPCClosed();
327 if (callback_) 329 if (callback_)
328 callback_->OnRenderError(); 330 callback_->OnRenderError();
329 } 331 }
330 } 332 }
331 333
334 void AudioOutputDevice::OnStateChanged(AudioOutputIPCDelegateState state) {
335 DCHECK(task_runner()->BelongsToCurrentThread());
336
337 // Do nothing if the stream has been closed.
338 if (state_ < CREATING_STREAM)
339 return;
340
341 // TODO(miu): Clean-up inconsistent and incomplete handling here.
342 // http://crbug.com/180640
343 switch (state) {
344 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING:
345 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED:
346 break;
347 case AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR:
348 DLOG(WARNING) << "AudioOutputDevice::OnStateChanged(ERROR)";
349 // Don't dereference the callback object if the audio thread
350 // is stopped or stopping. That could mean that the callback
351 // object has been deleted.
352 // TODO(tommi): Add an explicit contract for clearing the callback
353 // object. Possibly require calling Initialize again or provide
354 // a callback object via Start() and clear it in Stop().
355 if (!audio_thread_.IsStopped())
356 callback_->OnRenderError();
357 break;
358 default:
359 NOTREACHED();
360 break;
361 }
362 }
363
364 void AudioOutputDevice::OnDeviceAuthorized(
365 OutputDeviceStatus device_status,
366 const media::AudioParameters& output_params,
367 const std::string& matched_device_id) {
368 ProcessDeviceAuthorizationOnIOThread(device_status, output_params,
369 matched_device_id, false);
370 }
371
332 void AudioOutputDevice::OnStreamCreated( 372 void AudioOutputDevice::OnStreamCreated(
333 base::SharedMemoryHandle handle, 373 base::SharedMemoryHandle handle,
334 base::SyncSocket::Handle socket_handle, 374 base::SyncSocket::Handle socket_handle,
335 int length) { 375 int length) {
336 DCHECK(task_runner()->BelongsToCurrentThread()); 376 DCHECK(task_runner()->BelongsToCurrentThread());
337 DCHECK(base::SharedMemory::IsHandleValid(handle)); 377 DCHECK(base::SharedMemory::IsHandleValid(handle));
338 #if defined(OS_WIN) 378 #if defined(OS_WIN)
339 DCHECK(socket_handle); 379 DCHECK(socket_handle);
340 #else 380 #else
341 DCHECK_GE(socket_handle, 0); 381 DCHECK_GE(socket_handle, 0);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 484
445 // Update the audio-delay measurement, inform about the number of skipped 485 // Update the audio-delay measurement, inform about the number of skipped
446 // frames, and ask client to render audio. Since |output_bus_| is wrapping 486 // frames, and ask client to render audio. Since |output_bus_| is wrapping
447 // the shared memory the Render() call is writing directly into the shared 487 // the shared memory the Render() call is writing directly into the shared
448 // memory. 488 // memory.
449 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), 489 render_callback_->Render(output_bus_.get(), std::round(frames_delayed),
450 frames_skipped); 490 frames_skipped);
451 } 491 }
452 492
453 } // namespace media 493 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698