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

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: Dale's comments addressed: code simplified, comments fixed. 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
« 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 <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::OnDeviceAuthorized, this,
171 OUTPUT_DEVICE_STATUS_ERROR_TIMED_OUT, media::AudioParameters(),
172 std::string()));
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();
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;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 NOTREACHED(); 295 NOTREACHED();
279 break; 296 break;
280 } 297 }
281 } 298 }
282 299
283 void AudioOutputDevice::OnDeviceAuthorized( 300 void AudioOutputDevice::OnDeviceAuthorized(
284 OutputDeviceStatus device_status, 301 OutputDeviceStatus device_status,
285 const media::AudioParameters& output_params, 302 const media::AudioParameters& output_params,
286 const std::string& matched_device_id) { 303 const std::string& matched_device_id) {
287 DCHECK(task_runner()->BelongsToCurrentThread()); 304 DCHECK(task_runner()->BelongsToCurrentThread());
305
306 auth_timeout_action_.reset();
307
308 // Do nothing if late authorization is received after timeout.
309 if (state_ == IPC_CLOSED)
310 return;
311
312 UMA_HISTOGRAM_BOOLEAN("Media.Audio.Render.OutputDeviceAuthorizationTimedOut",
313 device_status == OUTPUT_DEVICE_STATUS_ERROR_TIMED_OUT);
314
288 DCHECK_EQ(state_, AUTHORIZING); 315 DCHECK_EQ(state_, AUTHORIZING);
289 316
290 // It may happen that a second authorization is received as a result to a 317 // 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 318 // 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_| 319 // differs from the first, it will not be reflected in |device_status_|
293 // to avoid a race. 320 // to avoid a race.
294 // This scenario is unlikely. If it occurs, the new value will be 321 // This scenario is unlikely. If it occurs, the new value will be
295 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice 322 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice
296 // will enter the IPC_CLOSED state anyway, which is the safe thing to do. 323 // will enter the IPC_CLOSED state anyway, which is the safe thing to do.
297 // This is preferable to holding a lock. 324 // This is preferable to holding a lock.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 471
445 // Update the audio-delay measurement, inform about the number of skipped 472 // Update the audio-delay measurement, inform about the number of skipped
446 // frames, and ask client to render audio. Since |output_bus_| is wrapping 473 // 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 474 // the shared memory the Render() call is writing directly into the shared
448 // memory. 475 // memory.
449 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), 476 render_callback_->Render(output_bus_.get(), std::round(frames_delayed),
450 frames_skipped); 477 frames_skipped);
451 } 478 }
452 479
453 } // namespace media 480 } // 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