Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| 26 constexpr int kAuthorizationTimeoutMs = 4000; | |
|
o1ka
2016/06/10 14:21:09
Since content::kHungRendererDelayMs is obviously i
| |
| 27 | |
| 23 namespace media { | 28 namespace media { |
| 24 | 29 |
| 25 // Takes care of invoking the render callback on the audio thread. | 30 // Takes care of invoking the render callback on the audio thread. |
| 26 // An instance of this class is created for each capture stream in | 31 // An instance of this class is created for each capture stream in |
| 27 // OnStreamCreated(). | 32 // OnStreamCreated(). |
| 28 class AudioOutputDevice::AudioThreadCallback | 33 class AudioOutputDevice::AudioThreadCallback |
| 29 : public AudioDeviceThread::Callback { | 34 : public AudioDeviceThread::Callback { |
| 30 public: | 35 public: |
| 31 AudioThreadCallback(const AudioParameters& audio_parameters, | 36 AudioThreadCallback(const AudioParameters& audio_parameters, |
| 32 base::SharedMemoryHandle memory, | 37 base::SharedMemoryHandle memory, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 session_id_, device_id_) | 152 session_id_, device_id_) |
| 148 ? matched_device_id_ | 153 ? matched_device_id_ |
| 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; |
| 162 | |
| 157 ipc_->RequestDeviceAuthorization(this, session_id_, device_id_, | 163 ipc_->RequestDeviceAuthorization(this, session_id_, device_id_, |
| 158 security_origin_); | 164 security_origin_); |
| 165 | |
| 166 // Create the timer on the thread it's used on. It's guaranteed to be | |
| 167 // deleted on the same thread since users must call Stop() before deleting | |
| 168 // AudioOutputDevice; see ShutDownOnIOThread(). | |
| 169 auth_timeout_action_.reset(new base::OneShotTimer()); | |
| 170 auth_timeout_action_->Start( | |
| 171 FROM_HERE, base::TimeDelta::FromMilliseconds(kAuthorizationTimeoutMs), | |
| 172 base::Bind(&AudioOutputDevice::ProcessDeviceAuthorizationOnIOThread, this, | |
| 173 OUTPUT_DEVICE_STATUS_ERROR_INTERNAL, media::AudioParameters(), | |
| 174 std::string(), true /* timed out */)); | |
| 159 } | 175 } |
| 160 | 176 |
| 161 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { | 177 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { |
| 162 DCHECK(task_runner()->BelongsToCurrentThread()); | 178 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 163 switch (state_) { | 179 switch (state_) { |
| 164 case IPC_CLOSED: | 180 case IPC_CLOSED: |
| 165 if (callback_) | 181 if (callback_) |
| 166 callback_->OnRenderError(); | 182 callback_->OnRenderError(); |
| 167 break; | 183 break; |
| 168 | 184 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 void AudioOutputDevice::ShutDownOnIOThread() { | 238 void AudioOutputDevice::ShutDownOnIOThread() { |
| 223 DCHECK(task_runner()->BelongsToCurrentThread()); | 239 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 224 | 240 |
| 225 // Close the stream, if we haven't already. | 241 // Close the stream, if we haven't already. |
| 226 if (state_ >= AUTHORIZING) { | 242 if (state_ >= AUTHORIZING) { |
| 227 ipc_->CloseStream(); | 243 ipc_->CloseStream(); |
| 228 state_ = IDLE; | 244 state_ = IDLE; |
| 229 } | 245 } |
| 230 start_on_authorized_ = false; | 246 start_on_authorized_ = false; |
| 231 | 247 |
| 248 // Destoy the timer on the thread it's used on. | |
| 249 auth_timeout_action_.reset(nullptr); | |
| 250 | |
| 232 // We can run into an issue where ShutDownOnIOThread is called right after | 251 // 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 | 252 // OnStreamCreated is called in cases where Start/Stop are called before we |
| 234 // get the OnStreamCreated callback. To handle that corner case, we call | 253 // get the OnStreamCreated callback. To handle that corner case, we call |
| 235 // Stop(). In most cases, the thread will already be stopped. | 254 // Stop(). In most cases, the thread will already be stopped. |
| 236 // | 255 // |
| 237 // Another situation is when the IO thread goes away before Stop() is called | 256 // 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 | 257 // 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. | 258 // and can't rely on the main thread existing either. |
| 240 base::AutoLock auto_lock_(audio_thread_lock_); | 259 base::AutoLock auto_lock_(audio_thread_lock_); |
| 241 base::ThreadRestrictions::ScopedAllowIO allow_io; | 260 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 default: | 296 default: |
| 278 NOTREACHED(); | 297 NOTREACHED(); |
| 279 break; | 298 break; |
| 280 } | 299 } |
| 281 } | 300 } |
| 282 | 301 |
| 283 void AudioOutputDevice::OnDeviceAuthorized( | 302 void AudioOutputDevice::OnDeviceAuthorized( |
| 284 OutputDeviceStatus device_status, | 303 OutputDeviceStatus device_status, |
| 285 const media::AudioParameters& output_params, | 304 const media::AudioParameters& output_params, |
| 286 const std::string& matched_device_id) { | 305 const std::string& matched_device_id) { |
| 306 ProcessDeviceAuthorizationOnIOThread(device_status, output_params, | |
| 307 matched_device_id, false); | |
| 308 } | |
| 309 | |
| 310 void AudioOutputDevice::ProcessDeviceAuthorizationOnIOThread( | |
| 311 OutputDeviceStatus device_status, | |
| 312 const media::AudioParameters& output_params, | |
| 313 const std::string& matched_device_id, | |
| 314 bool timed_out) { | |
| 287 DCHECK(task_runner()->BelongsToCurrentThread()); | 315 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 316 | |
| 317 if (auth_timeout_action_) | |
| 318 auth_timeout_action_->Stop(); | |
| 319 | |
| 320 if (timed_out) { | |
| 321 DCHECK_EQ(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL, device_status); | |
| 322 UMA_HISTOGRAM_BOOLEAN( | |
| 323 "Media.Audio.Render.OutputDeviceAuthorizationTimedOut", true); | |
| 324 auth_delay_.reset(new base::ElapsedTimer()); | |
| 325 } else { | |
| 326 if (state_ == IPC_CLOSED) { | |
| 327 // ProcessDeviceAuthorizationOnIOThread has already been called with | |
| 328 // |timed_out| set to true, i.e. authorization timed out, but now we | |
| 329 // received a late authorization response. Do nothing, since we can't | |
| 330 // upgrade device information after |did_receive_auth_| is signalled. | |
|
DaleCurtis
2016/06/10 18:29:18
signaled.
o1ka
2016/06/13 13:22:12
Acknowledged.
| |
| 331 if (auth_delay_) { | |
| 332 UMA_HISTOGRAM_COUNTS( | |
| 333 "Media.Audio.Render.OutputDeviceAuthorizationTimeoutExceededMs", | |
|
o1ka
2016/06/10 14:21:09
I'm not sure if we want to have this metric. Since
DaleCurtis
2016/06/10 18:29:18
Yeah, I don't think this is useful.
o1ka
2016/06/13 13:22:12
Removed.
| |
| 334 auth_delay_->Elapsed().InMilliseconds()); | |
| 335 auth_delay_.reset(nullptr); | |
| 336 } | |
| 337 return; | |
| 338 } else { | |
| 339 UMA_HISTOGRAM_BOOLEAN( | |
|
DaleCurtis
2016/06/10 18:29:18
Just have this in one spot outside the conditional
o1ka
2016/06/13 13:22:13
Done.
| |
| 340 "Media.Audio.Render.OutputDeviceAuthorizationTimedOut", false); | |
| 341 } | |
| 342 } | |
| 343 | |
| 288 DCHECK_EQ(state_, AUTHORIZING); | 344 DCHECK_EQ(state_, AUTHORIZING); |
| 289 | 345 |
| 290 // It may happen that a second authorization is received as a result to a | 346 // 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 | 347 // 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_| | 348 // differs from the first, it will not be reflected in |device_status_| |
| 293 // to avoid a race. | 349 // to avoid a race. |
| 294 // This scenario is unlikely. If it occurs, the new value will be | 350 // This scenario is unlikely. If it occurs, the new value will be |
| 295 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice | 351 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice |
| 296 // will enter the IPC_CLOSED state anyway, which is the safe thing to do. | 352 // will enter the IPC_CLOSED state anyway, which is the safe thing to do. |
| 297 // This is preferable to holding a lock. | 353 // This is preferable to holding a lock. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 | 500 |
| 445 // Update the audio-delay measurement, inform about the number of skipped | 501 // Update the audio-delay measurement, inform about the number of skipped |
| 446 // frames, and ask client to render audio. Since |output_bus_| is wrapping | 502 // 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 | 503 // the shared memory the Render() call is writing directly into the shared |
| 448 // memory. | 504 // memory. |
| 449 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), | 505 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), |
| 450 frames_skipped); | 506 frames_skipped); |
| 451 } | 507 } |
| 452 | 508 |
| 453 } // namespace media | 509 } // namespace media |
| OLD | NEW |