| 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 DoStopCloseAndClearStream( |
| 123 is_for_device_change); // Calls RemoveOutputDeviceChangeListener(). |
| 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& item : duplicator_) { |
| 174 item->Start(); |
| 175 } |
| 172 | 176 |
| 173 // For UMA tracking purposes, start the wedge detection timer. This allows us | 177 // 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. | 178 // to record statistics about the number of wedged playbacks in the field. |
| 175 // | 179 // |
| 176 // WedgeCheck() will look to see if |on_more_io_data_called_| is true after | 180 // 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 | 181 // 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 | 182 // large enough that the value isn't queried while OnMoreDataIO() is setting |
| 179 // it. | 183 // it. |
| 180 // | 184 // |
| 181 // Timer self-manages its lifetime and WedgeCheck() will only record the UMA | 185 // Timer self-manages its lifetime and WedgeCheck() will only record the UMA |
| 182 // statistic if state is still kPlaying. Additional Start() calls will | 186 // statistic if state is still kPlaying. Additional Start() calls will |
| 183 // invalidate the previous timer. | 187 // invalidate the previous timer. |
| 184 wedge_timer_.reset(new base::OneShotTimer()); | 188 wedge_timer_.reset(new base::OneShotTimer()); |
| 185 wedge_timer_->Start( | 189 wedge_timer_->Start( |
| 186 FROM_HERE, TimeDelta::FromSeconds(5), this, | 190 FROM_HERE, TimeDelta::FromSeconds(5), this, |
| 187 &AudioOutputController::WedgeCheck); | 191 &AudioOutputController::WedgeCheck); |
| 188 | 192 |
| 189 handler_->OnPlaying(); | 193 handler_->OnPlaying(); |
| 190 } | 194 } |
| 191 | 195 |
| 192 void AudioOutputController::StopStream() { | 196 void AudioOutputController::StopStream() { |
| 193 DCHECK(message_loop_->BelongsToCurrentThread()); | 197 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 194 | 198 |
| 195 if (state_ == kPlaying) { | 199 if (state_ == kPlaying) { |
| 196 wedge_timer_.reset(); | 200 wedge_timer_.reset(); |
| 197 stream_->Stop(); | 201 stream_->Stop(); |
| 198 | 202 |
| 203 for (auto& item : duplicator_) { |
| 204 item->Stop(); |
| 205 } |
| 199 // A stopped stream is silent, and power_montior_.Scan() is no longer being | 206 // A stopped stream is silent, and power_montior_.Scan() is no longer being |
| 200 // called; so we must reset the power monitor. | 207 // called; so we must reset the power monitor. |
| 201 power_monitor_.Reset(); | 208 power_monitor_.Reset(); |
| 202 | 209 |
| 203 state_ = kPaused; | 210 state_ = kPaused; |
| 204 } | 211 } |
| 205 } | 212 } |
| 206 | 213 |
| 207 void AudioOutputController::DoPause() { | 214 void AudioOutputController::DoPause() { |
| 208 DCHECK(message_loop_->BelongsToCurrentThread()); | 215 DCHECK(message_loop_->BelongsToCurrentThread()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 221 | 228 |
| 222 handler_->OnPaused(); | 229 handler_->OnPaused(); |
| 223 } | 230 } |
| 224 | 231 |
| 225 void AudioOutputController::DoClose() { | 232 void AudioOutputController::DoClose() { |
| 226 DCHECK(message_loop_->BelongsToCurrentThread()); | 233 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 227 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime"); | 234 SCOPED_UMA_HISTOGRAM_TIMER("Media.AudioOutputController.CloseTime"); |
| 228 TRACE_EVENT0("audio", "AudioOutputController::DoClose"); | 235 TRACE_EVENT0("audio", "AudioOutputController::DoClose"); |
| 229 | 236 |
| 230 if (state_ != kClosed) { | 237 if (state_ != kClosed) { |
| 231 DoStopCloseAndClearStream(); | 238 DoStopCloseAndClearStream(false); |
| 232 sync_reader_->Close(); | 239 sync_reader_->Close(); |
| 233 state_ = kClosed; | 240 state_ = kClosed; |
| 234 } | 241 } |
| 235 } | 242 } |
| 236 | 243 |
| 237 void AudioOutputController::DoSetVolume(double volume) { | 244 void AudioOutputController::DoSetVolume(double volume) { |
| 238 DCHECK(message_loop_->BelongsToCurrentThread()); | 245 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 239 | 246 |
| 240 // Saves the volume to a member first. We may not be able to set the volume | 247 // 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. | 248 // right away but when the stream is created we'll set the volume. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 304 |
| 298 sync_reader_->Read(dest); | 305 sync_reader_->Read(dest); |
| 299 | 306 |
| 300 const int frames = dest->frames(); | 307 const int frames = dest->frames(); |
| 301 sync_reader_->UpdatePendingBytes( | 308 sync_reader_->UpdatePendingBytes( |
| 302 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); | 309 total_bytes_delay + frames * params_.GetBytesPerFrame(), frames_skipped); |
| 303 | 310 |
| 304 if (will_monitor_audio_levels()) | 311 if (will_monitor_audio_levels()) |
| 305 power_monitor_.Scan(*dest, frames); | 312 power_monitor_.Scan(*dest, frames); |
| 306 | 313 |
| 314 for (auto sink : duplicator_) |
| 315 sink->OnData(nullptr, dest, total_bytes_delay, 1); |
| 316 |
| 307 return frames; | 317 return frames; |
| 308 } | 318 } |
| 309 | 319 |
| 310 void AudioOutputController::OnError(AudioOutputStream* stream) { | 320 void AudioOutputController::OnError(AudioOutputStream* stream) { |
| 311 { | 321 { |
| 312 base::AutoLock auto_lock(error_lock_); | 322 base::AutoLock auto_lock(error_lock_); |
| 313 if (ignore_errors_during_stop_close_) | 323 if (ignore_errors_during_stop_close_) |
| 314 return; | 324 return; |
| 315 } | 325 } |
| 316 | 326 |
| 317 // Handle error on the audio controller thread. | 327 // Handle error on the audio controller thread. |
| 318 message_loop_->PostTask(FROM_HERE, base::Bind( | 328 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 319 &AudioOutputController::DoReportError, this)); | 329 &AudioOutputController::DoReportError, this)); |
| 320 } | 330 } |
| 321 | 331 |
| 322 void AudioOutputController::DoStopCloseAndClearStream() { | 332 void AudioOutputController::DoStopCloseAndClearStream(bool isForDeviceChange) { |
| 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 if (!isForDeviceChange) { |
| 350 for (auto& item : duplicator_) { |
| 351 item->Close(); |
| 352 } |
| 353 } |
| 339 if (stream_ == diverting_to_stream_) | 354 if (stream_ == diverting_to_stream_) |
| 340 diverting_to_stream_ = NULL; | 355 diverting_to_stream_ = NULL; |
| 341 stream_ = NULL; | 356 stream_ = NULL; |
| 342 | 357 |
| 343 // Since the stream is no longer running, no lock is necessary. | 358 // Since the stream is no longer running, no lock is necessary. |
| 344 ignore_errors_during_stop_close_ = false; | 359 ignore_errors_during_stop_close_ = false; |
| 345 } | 360 } |
| 346 | 361 |
| 347 state_ = kEmpty; | 362 state_ = kEmpty; |
| 348 } | 363 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 message_loop_->PostTask( | 400 message_loop_->PostTask( |
| 386 FROM_HERE, | 401 FROM_HERE, |
| 387 base::Bind(&AudioOutputController::DoStartDiverting, this, to_stream)); | 402 base::Bind(&AudioOutputController::DoStartDiverting, this, to_stream)); |
| 388 } | 403 } |
| 389 | 404 |
| 390 void AudioOutputController::StopDiverting() { | 405 void AudioOutputController::StopDiverting() { |
| 391 message_loop_->PostTask( | 406 message_loop_->PostTask( |
| 392 FROM_HERE, base::Bind(&AudioOutputController::DoStopDiverting, this)); | 407 FROM_HERE, base::Bind(&AudioOutputController::DoStopDiverting, this)); |
| 393 } | 408 } |
| 394 | 409 |
| 410 void AudioOutputController::StartDuplicating(AudioPushSink* to_stream) { |
| 411 message_loop_->PostTask( |
| 412 FROM_HERE, |
| 413 base::Bind(&AudioOutputController::DoStartDuplicating, this, to_stream)); |
| 414 } |
| 415 |
| 416 void AudioOutputController::StopDuplicating(AudioPushSink* to_stream) { |
| 417 message_loop_->PostTask( |
| 418 FROM_HERE, |
| 419 base::Bind(&AudioOutputController::DoStopDuplicating, this, to_stream)); |
| 420 } |
| 421 |
| 395 void AudioOutputController::DoStartDiverting(AudioOutputStream* to_stream) { | 422 void AudioOutputController::DoStartDiverting(AudioOutputStream* to_stream) { |
| 396 DCHECK(message_loop_->BelongsToCurrentThread()); | 423 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 397 | 424 |
| 398 if (state_ == kClosed) | 425 if (state_ == kClosed) |
| 399 return; | 426 return; |
| 400 | 427 |
| 401 DCHECK(!diverting_to_stream_); | 428 DCHECK(!diverting_to_stream_); |
| 402 diverting_to_stream_ = to_stream; | 429 diverting_to_stream_ = to_stream; |
| 403 // Note: OnDeviceChange() will engage the "re-create" process, which will | 430 // Note: OnDeviceChange() will engage the "re-create" process, which will |
| 404 // detect and use the alternate AudioOutputStream rather than create a new one | 431 // detect and use the alternate AudioOutputStream rather than create a new one |
| 405 // via AudioManager. | 432 // via AudioManager. |
| 406 OnDeviceChange(); | 433 OnDeviceChange(); |
| 407 } | 434 } |
| 408 | 435 |
| 409 void AudioOutputController::DoStopDiverting() { | 436 void AudioOutputController::DoStopDiverting() { |
| 410 DCHECK(message_loop_->BelongsToCurrentThread()); | 437 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 411 | 438 |
| 412 if (state_ == kClosed) | 439 if (state_ == kClosed) |
| 413 return; | 440 return; |
| 414 | 441 |
| 415 // Note: OnDeviceChange() will cause the existing stream (the consumer of the | 442 // 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 | 443 // diverted audio data) to be closed, and diverting_to_stream_ will be set |
| 417 // back to NULL. | 444 // back to NULL. |
| 418 OnDeviceChange(); | 445 OnDeviceChange(); |
| 419 DCHECK(!diverting_to_stream_); | 446 DCHECK(!diverting_to_stream_); |
| 420 } | 447 } |
| 421 | 448 |
| 449 void AudioOutputController::DoStartDuplicating(AudioPushSink* to_stream) { |
| 450 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 451 printf("StartDup\n"); |
| 452 if (state_ == kClosed) |
| 453 return; |
| 454 |
| 455 // Already serving the stream. |
| 456 if (duplicator_.find(to_stream) != duplicator_.end()) |
| 457 return; |
| 458 |
| 459 duplicator_.insert(to_stream); |
| 460 if (state_ == kPlaying) |
| 461 to_stream->Start(); |
| 462 } |
| 463 |
| 464 void AudioOutputController::DoStopDuplicating(AudioPushSink* to_stream) { |
| 465 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 466 |
| 467 if (state_ == kClosed) |
| 468 return; |
| 469 |
| 470 // No such stream. |
| 471 if (duplicator_.find(to_stream) == duplicator_.end()) |
| 472 return; |
| 473 |
| 474 to_stream->Stop(); |
| 475 to_stream->Close(); |
| 476 duplicator_.erase(to_stream); |
| 477 } |
| 478 |
| 422 std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() { | 479 std::pair<float, bool> AudioOutputController::ReadCurrentPowerAndClip() { |
| 423 DCHECK(will_monitor_audio_levels()); | 480 DCHECK(will_monitor_audio_levels()); |
| 424 return power_monitor_.ReadCurrentPowerAndClip(); | 481 return power_monitor_.ReadCurrentPowerAndClip(); |
| 425 } | 482 } |
| 426 | 483 |
| 427 void AudioOutputController::WedgeCheck() { | 484 void AudioOutputController::WedgeCheck() { |
| 428 DCHECK(message_loop_->BelongsToCurrentThread()); | 485 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 429 | 486 |
| 430 // If we should be playing and we haven't, that's a wedge. | 487 // If we should be playing and we haven't, that's a wedge. |
| 431 if (state_ == kPlaying) { | 488 if (state_ == kPlaying) { |
| 432 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", | 489 UMA_HISTOGRAM_BOOLEAN("Media.AudioOutputControllerPlaybackStartupSuccess", |
| 433 base::AtomicRefCountIsOne(&on_more_io_data_called_)); | 490 base::AtomicRefCountIsOne(&on_more_io_data_called_)); |
| 434 } | 491 } |
| 435 } | 492 } |
| 436 | 493 |
| 437 } // namespace media | 494 } // namespace media |
| OLD | NEW |