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_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 16 matching lines...) Expand all Loading... | |
| 162 // We can start from created or paused state. | 163 // We can start from created or paused state. |
| 163 if (state_ != kCreated && state_ != kPaused) | 164 if (state_ != kCreated && state_ != kPaused) |
| 164 return; | 165 return; |
| 165 | 166 |
| 166 // Ask for first packet. | 167 // Ask for first packet. |
| 167 sync_reader_->UpdatePendingBytes(0, 0); | 168 sync_reader_->UpdatePendingBytes(0, 0); |
| 168 | 169 |
| 169 state_ = kPlaying; | 170 state_ = kPlaying; |
| 170 | 171 |
| 171 stream_->Start(this); | 172 stream_->Start(this); |
| 173 for (auto& target : duplicating_targets_) | |
| 174 target->Start(); | |
| 172 | 175 |
| 173 // For UMA tracking purposes, start the wedge detection timer. This allows us | 176 // For UMA tracking purposes, start the wedge detection timer. This allows us |
| 174 // to record statistics about the number of wedged playbacks in the field. | 177 // to record statistics about the number of wedged playbacks in the field. |
| 175 // | 178 // |
| 176 // WedgeCheck() will look to see if |on_more_io_data_called_| is true after | 179 // WedgeCheck() will look to see if |on_more_io_data_called_| is true after |
| 177 // the timeout expires. Care must be taken to ensure the wedge check delay is | 180 // the timeout expires. Care must be taken to ensure the wedge check delay is |
| 178 // large enough that the value isn't queried while OnMoreDataIO() is setting | 181 // large enough that the value isn't queried while OnMoreDataIO() is setting |
| 179 // it. | 182 // it. |
| 180 // | 183 // |
| 181 // Timer self-manages its lifetime and WedgeCheck() will only record the UMA | 184 // Timer self-manages its lifetime and WedgeCheck() will only record the UMA |
| 182 // statistic if state is still kPlaying. Additional Start() calls will | 185 // statistic if state is still kPlaying. Additional Start() calls will |
| 183 // invalidate the previous timer. | 186 // invalidate the previous timer. |
| 184 wedge_timer_.reset(new base::OneShotTimer()); | 187 wedge_timer_.reset(new base::OneShotTimer()); |
| 185 wedge_timer_->Start( | 188 wedge_timer_->Start( |
| 186 FROM_HERE, TimeDelta::FromSeconds(5), this, | 189 FROM_HERE, TimeDelta::FromSeconds(5), this, |
| 187 &AudioOutputController::WedgeCheck); | 190 &AudioOutputController::WedgeCheck); |
| 188 | 191 |
| 189 handler_->OnPlaying(); | 192 handler_->OnPlaying(); |
| 190 } | 193 } |
| 191 | 194 |
| 192 void AudioOutputController::StopStream() { | 195 void AudioOutputController::StopStream() { |
| 193 DCHECK(message_loop_->BelongsToCurrentThread()); | 196 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 194 | 197 |
| 195 if (state_ == kPlaying) { | 198 if (state_ == kPlaying) { |
| 196 wedge_timer_.reset(); | 199 wedge_timer_.reset(); |
| 197 stream_->Stop(); | 200 stream_->Stop(); |
| 198 | 201 |
| 202 for (auto& target : duplicating_targets_) | |
| 203 target->Stop(); | |
| 204 | |
| 199 // A stopped stream is silent, and power_montior_.Scan() is no longer being | 205 // A stopped stream is silent, and power_montior_.Scan() is no longer being |
| 200 // called; so we must reset the power monitor. | 206 // called; so we must reset the power monitor. |
| 201 power_monitor_.Reset(); | 207 power_monitor_.Reset(); |
| 202 | 208 |
| 203 state_ = kPaused; | 209 state_ = kPaused; |
| 204 } | 210 } |
| 205 } | 211 } |
| 206 | 212 |
| 207 void AudioOutputController::DoPause() { | 213 void AudioOutputController::DoPause() { |
| 208 DCHECK(message_loop_->BelongsToCurrentThread()); | 214 DCHECK(message_loop_->BelongsToCurrentThread()); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 221 | 227 |
| 222 handler_->OnPaused(); | 228 handler_->OnPaused(); |
| 223 } | 229 } |
| 224 | 230 |
| 225 void AudioOutputController::DoClose() { | 231 void AudioOutputController::DoClose() { |
| 226 DCHECK(message_loop_->BelongsToCurrentThread()); | 232 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 227 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime"); | 233 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime"); |
| 228 TRACE_EVENT0("audio", "AudioOutputController::DoClose"); | 234 TRACE_EVENT0("audio", "AudioOutputController::DoClose"); |
| 229 | 235 |
| 230 if (state_ != kClosed) { | 236 if (state_ != kClosed) { |
| 231 DoStopCloseAndClearStream(); | 237 DoStopCloseAndClearStream(false); |
| 232 sync_reader_->Close(); | 238 sync_reader_->Close(); |
| 233 state_ = kClosed; | 239 state_ = kClosed; |
| 234 } | 240 } |
| 235 } | 241 } |
| 236 | 242 |
| 237 void AudioOutputController::DoSetVolume(double volume) { | 243 void AudioOutputController::DoSetVolume(double volume) { |
| 238 DCHECK(message_loop_->BelongsToCurrentThread()); | 244 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 239 | 245 |
| 240 // Saves the volume to a member first. We may not be able to set the volume | 246 // 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. | 247 // 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. | 300 // thread starts, its safe to compare and then increment. |
| 295 if (base::AtomicRefCountIsZero(&on_more_io_data_called_)) | 301 if (base::AtomicRefCountIsZero(&on_more_io_data_called_)) |
| 296 base::AtomicRefCountInc(&on_more_io_data_called_); | 302 base::AtomicRefCountInc(&on_more_io_data_called_); |
| 297 | 303 |
| 298 sync_reader_->Read(dest); | 304 sync_reader_->Read(dest); |
| 299 | 305 |
| 300 const int frames = dest->frames(); | 306 const int frames = dest->frames(); |
| 301 sync_reader_->UpdatePendingBytes( | 307 sync_reader_->UpdatePendingBytes( |
| 302 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); | 308 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); |
| 303 | 309 |
| 310 for (auto& target : duplicating_targets_) | |
| 311 target->OnData(dest); | |
|
miu
2016/04/21 00:15:25
Please pass the delay information here as well, id
qiangchen
2016/04/28 00:00:56
Done.
| |
| 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) { | |
| 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 if (state_ == kPlaying) | |
| 463 to_stream->Start(); | |
| 464 } | |
| 465 | |
| 466 void AudioOutputController::DoStopDuplicating(AudioPushSink* to_stream) { | |
| 467 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 468 | |
| 469 if (state_ == kClosed) | |
| 470 return; | |
| 471 | |
| 472 // No such stream. | |
| 473 if (duplicating_targets_.find(to_stream) == duplicating_targets_.end()) | |
| 474 return; | |
| 475 | |
| 476 duplicating_targets_.erase(to_stream); | |
| 477 to_stream->Close(); | |
| 478 } | |
| 479 | |
| 422 std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() { | 480 std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() { |
| 423 DCHECK(will_monitor_audio_levels()); | 481 DCHECK(will_monitor_audio_levels()); |
| 424 return power_monitor_.ReadCurrentPowerAndClip(); | 482 return power_monitor_.ReadCurrentPowerAndClip(); |
| 425 } | 483 } |
| 426 | 484 |
| 427 void AudioOutputController::WedgeCheck() { | 485 void AudioOutputController::WedgeCheck() { |
| 428 DCHECK(message_loop_->BelongsToCurrentThread()); | 486 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 429 | 487 |
| 430 // If we should be playing and we haven't, that's a wedge. | 488 // If we should be playing and we haven't, that's a wedge. |
| 431 if (state_ == kPlaying) { | 489 if (state_ == kPlaying) { |
| 432 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", | 490 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", |
| 433 base::AtomicRefCountIsOne(&on_more_io_data_called_)); | 491 base::AtomicRefCountIsOne(&on_more_io_data_called_)); |
| 434 } | 492 } |
| 435 } | 493 } |
| 436 | 494 |
| 437 } // namespace media | 495 } // namespace media |
| OLD | NEW |