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

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

Powered by Google App Engine
This is Rietveld 408576698