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/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "media/audio/audio_device_description.h" | 19 #include "media/audio/audio_device_description.h" |
| 20 #include "media/audio/audio_output_controller.h" | 20 #include "media/audio/audio_output_controller.h" |
| 21 #include "media/base/limits.h" | 21 #include "media/base/limits.h" |
| 22 | 22 |
| 23 constexpr int kAuthorizationTimeoutMs = 1000; | |
|
o1ka
2016/06/08 15:46:06
Not sure about the value
o1ka
2016/06/09 12:02:04
Looks like renderer hang timeout is defined by con
DaleCurtis
2016/06/09 18:25:34
I don't think we want to block for anymore than 5
o1ka
2016/06/10 14:21:08
Ok. I'm not sure if we should wait for 5 seconds o
| |
| 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 |
| 27 // OnStreamCreated(). | 29 // OnStreamCreated(). |
| 28 class AudioOutputDevice::AudioThreadCallback | 30 class AudioOutputDevice::AudioThreadCallback |
| 29 : public AudioDeviceThread::Callback { | 31 : public AudioDeviceThread::Callback { |
| 30 public: | 32 public: |
| 31 AudioThreadCallback(const AudioParameters& audio_parameters, | 33 AudioThreadCallback(const AudioParameters& audio_parameters, |
| 32 base::SharedMemoryHandle memory, | 34 base::SharedMemoryHandle memory, |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 session_id_, device_id_) | 149 session_id_, device_id_) |
| 148 ? matched_device_id_ | 150 ? matched_device_id_ |
| 149 : device_id_, | 151 : device_id_, |
| 150 device_status_, output_params_); | 152 device_status_, output_params_); |
| 151 } | 153 } |
| 152 | 154 |
| 153 void AudioOutputDevice::RequestDeviceAuthorizationOnIOThread() { | 155 void AudioOutputDevice::RequestDeviceAuthorizationOnIOThread() { |
| 154 DCHECK(task_runner()->BelongsToCurrentThread()); | 156 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 155 DCHECK_EQ(state_, IDLE); | 157 DCHECK_EQ(state_, IDLE); |
| 156 state_ = AUTHORIZING; | 158 state_ = AUTHORIZING; |
| 159 | |
| 160 if (auth_timeout_action_) | |
|
DaleCurtis
2016/06/09 18:25:34
Instead, if you use a base::OneShotTimer here you
o1ka
2016/06/10 14:21:08
Thanks! Done.
| |
| 161 auth_timeout_action_->Cancel(); | |
| 162 | |
| 163 // Create the closure on the thread it's used on. It's guaranteed to be | |
| 164 // deleted on the same thread since users must call Stop() before deleting | |
| 165 // AudioOutputDevice. | |
| 166 auth_timeout_action_.reset(new base::CancelableClosure( | |
| 167 base::Bind(&AudioOutputDevice::ProcessDeviceAuthorizationOnIOThread, this, | |
| 168 OUTPUT_DEVICE_STATUS_ERROR_INTERNAL, media::AudioParameters(), | |
| 169 std::string(), false))); | |
| 170 | |
| 157 ipc_->RequestDeviceAuthorization(this, session_id_, device_id_, | 171 ipc_->RequestDeviceAuthorization(this, session_id_, device_id_, |
| 158 security_origin_); | 172 security_origin_); |
| 173 task_runner()->PostDelayedTask( | |
| 174 FROM_HERE, auth_timeout_action_->callback(), | |
| 175 base::TimeDelta::FromMilliseconds(kAuthorizationTimeoutMs)); | |
| 159 } | 176 } |
| 160 | 177 |
| 161 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { | 178 void AudioOutputDevice::CreateStreamOnIOThread(const AudioParameters& params) { |
| 162 DCHECK(task_runner()->BelongsToCurrentThread()); | 179 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 163 switch (state_) { | 180 switch (state_) { |
| 164 case IPC_CLOSED: | 181 case IPC_CLOSED: |
| 165 if (callback_) | 182 if (callback_) |
| 166 callback_->OnRenderError(); | 183 callback_->OnRenderError(); |
| 167 break; | 184 break; |
| 168 | 185 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 void AudioOutputDevice::ShutDownOnIOThread() { | 239 void AudioOutputDevice::ShutDownOnIOThread() { |
| 223 DCHECK(task_runner()->BelongsToCurrentThread()); | 240 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 224 | 241 |
| 225 // Close the stream, if we haven't already. | 242 // Close the stream, if we haven't already. |
| 226 if (state_ >= AUTHORIZING) { | 243 if (state_ >= AUTHORIZING) { |
| 227 ipc_->CloseStream(); | 244 ipc_->CloseStream(); |
| 228 state_ = IDLE; | 245 state_ = IDLE; |
| 229 } | 246 } |
| 230 start_on_authorized_ = false; | 247 start_on_authorized_ = false; |
| 231 | 248 |
| 249 if (auth_timeout_action_) { | |
|
DaleCurtis
2016/06/09 18:25:34
No need for this with the timer (or cancellable re
o1ka
2016/06/10 14:21:08
I guess we do need this, because both cancellable
| |
| 250 auth_timeout_action_->Cancel(); | |
| 251 auth_timeout_action_.reset(nullptr); | |
| 252 } | |
| 253 | |
| 232 // We can run into an issue where ShutDownOnIOThread is called right after | 254 // 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 | 255 // OnStreamCreated is called in cases where Start/Stop are called before we |
| 234 // get the OnStreamCreated callback. To handle that corner case, we call | 256 // get the OnStreamCreated callback. To handle that corner case, we call |
| 235 // Stop(). In most cases, the thread will already be stopped. | 257 // Stop(). In most cases, the thread will already be stopped. |
| 236 // | 258 // |
| 237 // Another situation is when the IO thread goes away before Stop() is called | 259 // 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 | 260 // 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. | 261 // and can't rely on the main thread existing either. |
| 240 base::AutoLock auto_lock_(audio_thread_lock_); | 262 base::AutoLock auto_lock_(audio_thread_lock_); |
| 241 base::ThreadRestrictions::ScopedAllowIO allow_io; | 263 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 default: | 299 default: |
| 278 NOTREACHED(); | 300 NOTREACHED(); |
| 279 break; | 301 break; |
| 280 } | 302 } |
| 281 } | 303 } |
| 282 | 304 |
| 283 void AudioOutputDevice::OnDeviceAuthorized( | 305 void AudioOutputDevice::OnDeviceAuthorized( |
| 284 OutputDeviceStatus device_status, | 306 OutputDeviceStatus device_status, |
| 285 const media::AudioParameters& output_params, | 307 const media::AudioParameters& output_params, |
| 286 const std::string& matched_device_id) { | 308 const std::string& matched_device_id) { |
| 309 ProcessDeviceAuthorizationOnIOThread(device_status, output_params, | |
| 310 matched_device_id, true); | |
| 311 } | |
| 312 | |
| 313 void AudioOutputDevice::ProcessDeviceAuthorizationOnIOThread( | |
|
o1ka
2016/06/08 15:46:06
Will move it to an appropriate place, now it's her
| |
| 314 OutputDeviceStatus device_status, | |
| 315 const media::AudioParameters& output_params, | |
| 316 const std::string& matched_device_id, | |
| 317 bool responce_received) { | |
|
Guido Urdaneta
2016/06/09 12:09:49
s/responce/response
or maybe use a more explicit
o1ka
2016/06/10 14:21:08
Done.
| |
| 287 DCHECK(task_runner()->BelongsToCurrentThread()); | 318 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 319 | |
| 320 if (responce_received) { | |
| 321 if (auth_timeout_action_) | |
|
DaleCurtis
2016/06/09 18:25:34
Would just be .Stop() with timer. You can always c
o1ka
2016/06/10 14:21:08
Done.
| |
| 322 auth_timeout_action_->Cancel(); | |
| 323 | |
| 324 if (state_ == IPC_CLOSED) { | |
| 325 // ProcessDeviceAuthorizationOnIOThread has already been called with | |
| 326 // |responce_received| set to false, i.e. authorization timed out, but now | |
| 327 // we received the authorization responce. Do nothing, since we can't | |
| 328 // upgrade device information after |did_receive_auth_| is signalled. | |
| 329 // TODO(olka): log UMA stat. | |
| 330 return; | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 if (!responce_received) { | |
|
Guido Urdaneta
2016/06/09 12:09:49
else?
o1ka
2016/06/09 12:38:28
Yes, I'll need "else" for UMA stats (those will be
o1ka
2016/06/10 14:21:08
Done.
| |
| 335 DCHECK_EQ(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL, device_status); | |
| 336 // TODO(olka): log UMA stat. | |
| 337 } | |
| 338 | |
| 288 DCHECK_EQ(state_, AUTHORIZING); | 339 DCHECK_EQ(state_, AUTHORIZING); |
| 289 | 340 |
| 290 // It may happen that a second authorization is received as a result to a | 341 // 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 | 342 // 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_| | 343 // differs from the first, it will not be reflected in |device_status_| |
| 293 // to avoid a race. | 344 // to avoid a race. |
| 294 // This scenario is unlikely. If it occurs, the new value will be | 345 // This scenario is unlikely. If it occurs, the new value will be |
| 295 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice | 346 // different from OUTPUT_DEVICE_STATUS_OK, so the AudioOutputDevice |
| 296 // will enter the IPC_CLOSED state anyway, which is the safe thing to do. | 347 // will enter the IPC_CLOSED state anyway, which is the safe thing to do. |
| 297 // This is preferable to holding a lock. | 348 // This is preferable to holding a lock. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 | 495 |
| 445 // Update the audio-delay measurement, inform about the number of skipped | 496 // Update the audio-delay measurement, inform about the number of skipped |
| 446 // frames, and ask client to render audio. Since |output_bus_| is wrapping | 497 // 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 | 498 // the shared memory the Render() call is writing directly into the shared |
| 448 // memory. | 499 // memory. |
| 449 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), | 500 render_callback_->Render(output_bus_.get(), std::round(frames_delayed), |
| 450 frames_skipped); | 501 frames_skipped); |
| 451 } | 502 } |
| 452 | 503 |
| 453 } // namespace media | 504 } // namespace media |
| OLD | NEW |