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_controller.h" | 5 #include "media/audio/audio_output_controller.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 | 112 |
113 void AudioOutputController::DoCreate(bool is_for_device_change) { | 113 void AudioOutputController::DoCreate(bool is_for_device_change) { |
114 DCHECK(message_loop_->BelongsToCurrentThread()); | 114 DCHECK(message_loop_->BelongsToCurrentThread()); |
115 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CreateTime"); | 115 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CreateTime"); |
116 TRACE_EVENT0("audio", "AudioOutputController::DoCreate"); | 116 TRACE_EVENT0("audio", "AudioOutputController::DoCreate"); |
117 | 117 |
118 // Close() can be called before DoCreate() is executed. | 118 // Close() can be called before DoCreate() is executed. |
119 if (state_ == kClosed) | 119 if (state_ == kClosed) |
120 return; | 120 return; |
121 | 121 |
122 DoStopCloseAndClearStream(); // Calls RemoveOutputDeviceChangeListener(). | 122 // Calls RemoveOutputDeviceChangeListener(). |
123 DoStopCloseAndClearStream(is_for_device_change); | |
123 DCHECK_EQ(kEmpty, state_); | 124 DCHECK_EQ(kEmpty, state_); |
124 | 125 |
125 stream_ = diverting_to_stream_ ? | 126 stream_ = diverting_to_stream_ ? |
126 diverting_to_stream_ : | 127 diverting_to_stream_ : |
127 audio_manager_->MakeAudioOutputStreamProxy(params_, output_device_id_); | 128 audio_manager_->MakeAudioOutputStreamProxy(params_, output_device_id_); |
128 if (!stream_) { | 129 if (!stream_) { |
129 state_ = kError; | 130 state_ = kError; |
130 handler_->OnError(); | 131 handler_->OnError(); |
131 return; | 132 return; |
132 } | 133 } |
133 | 134 |
134 if (!stream_->Open()) { | 135 if (!stream_->Open()) { |
135 DoStopCloseAndClearStream(); | 136 DoStopCloseAndClearStream(false); |
136 state_ = kError; | 137 state_ = kError; |
137 handler_->OnError(); | 138 handler_->OnError(); |
138 return; | 139 return; |
139 } | 140 } |
140 | 141 |
141 // Everything started okay, so re-register for state change callbacks if | 142 // Everything started okay, so re-register for state change callbacks if |
142 // stream_ was created via AudioManager. | 143 // stream_ was created via AudioManager. |
143 if (stream_ != diverting_to_stream_) | 144 if (stream_ != diverting_to_stream_) |
144 audio_manager_->AddOutputDeviceChangeListener(this); | 145 audio_manager_->AddOutputDeviceChangeListener(this); |
145 | 146 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 | 222 |
222 handler_->OnPaused(); | 223 handler_->OnPaused(); |
223 } | 224 } |
224 | 225 |
225 void AudioOutputController::DoClose() { | 226 void AudioOutputController::DoClose() { |
226 DCHECK(message_loop_->BelongsToCurrentThread()); | 227 DCHECK(message_loop_->BelongsToCurrentThread()); |
227 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime"); | 228 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime"); |
228 TRACE_EVENT0("audio", "AudioOutputController::DoClose"); | 229 TRACE_EVENT0("audio", "AudioOutputController::DoClose"); |
229 | 230 |
230 if (state_ != kClosed) { | 231 if (state_ != kClosed) { |
231 DoStopCloseAndClearStream(); | 232 DoStopCloseAndClearStream(false); |
232 sync_reader_->Close(); | 233 sync_reader_->Close(); |
233 state_ = kClosed; | 234 state_ = kClosed; |
234 } | 235 } |
235 } | 236 } |
236 | 237 |
237 void AudioOutputController::DoSetVolume(double volume) { | 238 void AudioOutputController::DoSetVolume(double volume) { |
238 DCHECK(message_loop_->BelongsToCurrentThread()); | 239 DCHECK(message_loop_->BelongsToCurrentThread()); |
239 | 240 |
240 // Saves the volume to a member first. We may not be able to set the volume | 241 // Saves the volume to a member first. We may not be able to set the volume |
241 // right away but when the stream is created we'll set the volume. | 242 // right away but when the stream is created we'll set the volume. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 // thread starts, its safe to compare and then increment. | 295 // thread starts, its safe to compare and then increment. |
295 if (base::AtomicRefCountIsZero(&on_more_io_data_called_)) | 296 if (base::AtomicRefCountIsZero(&on_more_io_data_called_)) |
296 base::AtomicRefCountInc(&on_more_io_data_called_); | 297 base::AtomicRefCountInc(&on_more_io_data_called_); |
297 | 298 |
298 sync_reader_->Read(dest); | 299 sync_reader_->Read(dest); |
299 | 300 |
300 const int frames = dest->frames(); | 301 const int frames = dest->frames(); |
301 sync_reader_->UpdatePendingBytes( | 302 sync_reader_->UpdatePendingBytes( |
302 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); | 303 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); |
303 | 304 |
305 const int64_t kMicrosecondsPerSecond = 1000000; | |
miu
2016/05/02 20:06:13
Delete this line, and just use base::Time::kMicros
qiangchen
2016/05/03 16:58:23
Done.
| |
306 const base::TimeTicks reference_time = | |
miu
2016/05/02 20:06:13
Please wrap all these lines of code to avoid the c
qiangchen
2016/05/03 16:58:23
Done.
| |
307 base::TimeTicks::Now() + base::TimeDelta::FromMicroseconds( | |
308 kMicrosecondsPerSecond * total_bytes_delay / | |
309 params_.GetBytesPerSecond()); | |
310 for (auto& target : duplicating_targets_) | |
miu
2016/05/02 20:06:13
The "auto&" doesn't help readability here. Please
qiangchen
2016/05/03 16:58:23
Done.
| |
311 target->OnData(*dest, reference_time); | |
312 | |
304 if (will_monitor_audio_levels()) | 313 if (will_monitor_audio_levels()) |
305 power_monitor_.Scan(*dest, frames); | 314 power_monitor_.Scan(*dest, frames); |
306 | 315 |
307 return frames; | 316 return frames; |
308 } | 317 } |
309 | 318 |
310 void AudioOutputController::OnError(AudioOutputStream* stream) { | 319 void AudioOutputController::OnError(AudioOutputStream* stream) { |
311 { | 320 { |
312 base::AutoLock auto_lock(error_lock_); | 321 base::AutoLock auto_lock(error_lock_); |
313 if (ignore_errors_during_stop_close_) | 322 if (ignore_errors_during_stop_close_) |
314 return; | 323 return; |
315 } | 324 } |
316 | 325 |
317 // Handle error on the audio controller thread. | 326 // Handle error on the audio controller thread. |
318 message_loop_->PostTask(FROM_HERE, base::Bind( | 327 message_loop_->PostTask(FROM_HERE, base::Bind( |
319 &AudioOutputController::DoReportError, this)); | 328 &AudioOutputController::DoReportError, this)); |
320 } | 329 } |
321 | 330 |
322 void AudioOutputController::DoStopCloseAndClearStream() { | 331 void AudioOutputController::DoStopCloseAndClearStream( |
332 bool is_for_device_change) { | |
323 DCHECK(message_loop_->BelongsToCurrentThread()); | 333 DCHECK(message_loop_->BelongsToCurrentThread()); |
324 | 334 |
325 // Allow calling unconditionally and bail if we don't have a stream_ to close. | 335 // Allow calling unconditionally and bail if we don't have a stream_ to close. |
326 if (stream_) { | 336 if (stream_) { |
327 { | 337 { |
328 base::AutoLock auto_lock(error_lock_); | 338 base::AutoLock auto_lock(error_lock_); |
329 ignore_errors_during_stop_close_ = true; | 339 ignore_errors_during_stop_close_ = true; |
330 } | 340 } |
331 | 341 |
332 // De-register from state change callbacks if stream_ was created via | 342 // De-register from state change callbacks if stream_ was created via |
333 // AudioManager. | 343 // AudioManager. |
334 if (stream_ != diverting_to_stream_) | 344 if (stream_ != diverting_to_stream_) |
335 audio_manager_->RemoveOutputDeviceChangeListener(this); | 345 audio_manager_->RemoveOutputDeviceChangeListener(this); |
336 | 346 |
337 StopStream(); | 347 StopStream(); |
338 stream_->Close(); | 348 stream_->Close(); |
349 | |
350 // Only close duplicating streams when this is really a close class | |
351 // For device change call, duplicating streams should remain unchanged. | |
352 if (!is_for_device_change) { | |
miu
2016/05/02 20:06:13
Looks like you don't need this at all. Let the gua
qiangchen
2016/05/03 16:58:23
Done.
| |
353 for (auto& target : duplicating_targets_) | |
354 target->Close(); | |
355 } | |
356 | |
339 if (stream_ == diverting_to_stream_) | 357 if (stream_ == diverting_to_stream_) |
340 diverting_to_stream_ = NULL; | 358 diverting_to_stream_ = NULL; |
341 stream_ = NULL; | 359 stream_ = NULL; |
342 | 360 |
343 // Since the stream is no longer running, no lock is necessary. | 361 // Since the stream is no longer running, no lock is necessary. |
344 ignore_errors_during_stop_close_ = false; | 362 ignore_errors_during_stop_close_ = false; |
345 } | 363 } |
346 | 364 |
347 state_ = kEmpty; | 365 state_ = kEmpty; |
348 } | 366 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 message_loop_->PostTask( | 403 message_loop_->PostTask( |
386 FROM_HERE, | 404 FROM_HERE, |
387 base::Bind(&AudioOutputController::DoStartDiverting, this, to_stream)); | 405 base::Bind(&AudioOutputController::DoStartDiverting, this, to_stream)); |
388 } | 406 } |
389 | 407 |
390 void AudioOutputController::StopDiverting() { | 408 void AudioOutputController::StopDiverting() { |
391 message_loop_->PostTask( | 409 message_loop_->PostTask( |
392 FROM_HERE, base::Bind(&AudioOutputController::DoStopDiverting, this)); | 410 FROM_HERE, base::Bind(&AudioOutputController::DoStopDiverting, this)); |
393 } | 411 } |
394 | 412 |
413 void AudioOutputController::StartDuplicating(AudioPushSink* sink) { | |
414 message_loop_->PostTask( | |
415 FROM_HERE, | |
416 base::Bind(&AudioOutputController::DoStartDuplicating, this, sink)); | |
417 } | |
418 | |
419 void AudioOutputController::StopDuplicating(AudioPushSink* sink) { | |
420 message_loop_->PostTask( | |
421 FROM_HERE, | |
422 base::Bind(&AudioOutputController::DoStopDuplicating, this, sink)); | |
423 } | |
424 | |
395 void AudioOutputController::DoStartDiverting(AudioOutputStream* to_stream) { | 425 void AudioOutputController::DoStartDiverting(AudioOutputStream* to_stream) { |
396 DCHECK(message_loop_->BelongsToCurrentThread()); | 426 DCHECK(message_loop_->BelongsToCurrentThread()); |
397 | 427 |
398 if (state_ == kClosed) | 428 if (state_ == kClosed) |
399 return; | 429 return; |
400 | 430 |
401 DCHECK(!diverting_to_stream_); | 431 DCHECK(!diverting_to_stream_); |
402 diverting_to_stream_ = to_stream; | 432 diverting_to_stream_ = to_stream; |
403 // Note: OnDeviceChange() will engage the "re-create" process, which will | 433 // Note: OnDeviceChange() will engage the "re-create" process, which will |
404 // detect and use the alternate AudioOutputStream rather than create a new one | 434 // detect and use the alternate AudioOutputStream rather than create a new one |
405 // via AudioManager. | 435 // via AudioManager. |
406 OnDeviceChange(); | 436 OnDeviceChange(); |
407 } | 437 } |
408 | 438 |
409 void AudioOutputController::DoStopDiverting() { | 439 void AudioOutputController::DoStopDiverting() { |
410 DCHECK(message_loop_->BelongsToCurrentThread()); | 440 DCHECK(message_loop_->BelongsToCurrentThread()); |
411 | 441 |
412 if (state_ == kClosed) | 442 if (state_ == kClosed) |
413 return; | 443 return; |
414 | 444 |
415 // Note: OnDeviceChange() will cause the existing stream (the consumer of the | 445 // Note: OnDeviceChange() will cause the existing stream (the consumer of the |
416 // diverted audio data) to be closed, and diverting_to_stream_ will be set | 446 // diverted audio data) to be closed, and diverting_to_stream_ will be set |
417 // back to NULL. | 447 // back to NULL. |
418 OnDeviceChange(); | 448 OnDeviceChange(); |
419 DCHECK(!diverting_to_stream_); | 449 DCHECK(!diverting_to_stream_); |
420 } | 450 } |
421 | 451 |
452 void AudioOutputController::DoStartDuplicating(AudioPushSink* to_stream) { | |
453 DCHECK(message_loop_->BelongsToCurrentThread()); | |
454 if (state_ == kClosed) | |
455 return; | |
456 | |
457 // Already serving the stream. | |
458 if (duplicating_targets_.find(to_stream) != duplicating_targets_.end()) | |
459 return; | |
460 | |
461 duplicating_targets_.insert(to_stream); | |
462 } | |
463 | |
464 void AudioOutputController::DoStopDuplicating(AudioPushSink* to_stream) { | |
465 DCHECK(message_loop_->BelongsToCurrentThread()); | |
466 | |
467 if (state_ == kClosed) | |
468 return; | |
miu
2016/05/02 20:06:13
You should probably still call to_stream->Close()
qiangchen
2016/05/03 16:58:23
Done.
| |
469 | |
470 // No such stream. | |
471 if (duplicating_targets_.find(to_stream) == duplicating_targets_.end()) | |
472 return; | |
473 | |
474 duplicating_targets_.erase(to_stream); | |
475 to_stream->Close(); | |
476 } | |
477 | |
422 std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() { | 478 std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() { |
423 DCHECK(will_monitor_audio_levels()); | 479 DCHECK(will_monitor_audio_levels()); |
424 return power_monitor_.ReadCurrentPowerAndClip(); | 480 return power_monitor_.ReadCurrentPowerAndClip(); |
425 } | 481 } |
426 | 482 |
427 void AudioOutputController::WedgeCheck() { | 483 void AudioOutputController::WedgeCheck() { |
428 DCHECK(message_loop_->BelongsToCurrentThread()); | 484 DCHECK(message_loop_->BelongsToCurrentThread()); |
429 | 485 |
430 // If we should be playing and we haven't, that's a wedge. | 486 // If we should be playing and we haven't, that's a wedge. |
431 if (state_ == kPlaying) { | 487 if (state_ == kPlaying) { |
432 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", | 488 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", |
433 base::AtomicRefCountIsOne(&on_more_io_data_called_)); | 489 base::AtomicRefCountIsOne(&on_more_io_data_called_)); |
434 } | 490 } |
435 } | 491 } |
436 | 492 |
437 } // namespace media | 493 } // namespace media |
OLD | NEW |