| 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_manager_base.h" | 5 #include "media/audio/audio_manager_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "media/audio/audio_device_description.h" | 16 #include "media/audio/audio_device_description.h" |
| 17 #include "media/audio/audio_output_dispatcher_impl.h" | 17 #include "media/audio/audio_output_dispatcher_impl.h" |
| 18 #include "media/audio/audio_output_proxy.h" | 18 #include "media/audio/audio_output_proxy.h" |
| 19 #include "media/audio/audio_output_resampler.h" | 19 #include "media/audio/audio_output_resampler.h" |
| 20 #include "media/audio/fake_audio_input_stream.h" | 20 #include "media/audio/fake_audio_input_stream.h" |
| 21 #include "media/audio/fake_audio_output_stream.h" | 21 #include "media/audio/fake_audio_output_stream.h" |
| 22 #include "media/base/media_switches.h" | 22 #include "media/base/media_switches.h" |
| 23 | 23 |
| 24 namespace media { | 24 namespace media { |
| 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 const int kStreamCloseDelaySeconds = 5; | 28 const int kStreamCloseDelaySeconds = 5; |
| 28 | 29 |
| 29 // Default maximum number of output streams that can be open simultaneously | 30 // Default maximum number of output streams that can be open simultaneously |
| 30 // for all platforms. | 31 // for all platforms. |
| 31 const int kDefaultMaxOutputStreams = 32; | 32 const int kDefaultMaxOutputStreams = 32; |
| 32 | 33 |
| 33 // Default maximum number of input streams that can be open simultaneously | 34 // Default maximum number of input streams that can be open simultaneously |
| 34 // for all platforms. | 35 // for all platforms. |
| 35 const int kDefaultMaxInputStreams = 32; | 36 const int kDefaultMaxInputStreams = 32; |
| 36 | 37 |
| 37 const int kMaxInputChannels = 3; | 38 const int kMaxInputChannels = 3; |
| 38 | 39 |
| 40 // Helper function to pass as callback when the audio debug recording is not |
| 41 // enabled. |
| 42 std::unique_ptr<AudioDebugRecorder> GetNullptrAudioDebugRecorder( |
| 43 const AudioParameters& params) { |
| 44 return nullptr; |
| 45 } |
| 46 |
| 39 } // namespace | 47 } // namespace |
| 40 | 48 |
| 41 struct AudioManagerBase::DispatcherParams { | 49 struct AudioManagerBase::DispatcherParams { |
| 42 DispatcherParams(const AudioParameters& input, | 50 DispatcherParams(const AudioParameters& input, |
| 43 const AudioParameters& output, | 51 const AudioParameters& output, |
| 44 const std::string& output_device_id) | 52 const std::string& output_device_id) |
| 45 : input_params(input), | 53 : input_params(input), |
| 46 output_params(output), | 54 output_params(output), |
| 47 output_device_id(output_device_id) {} | 55 output_device_id(output_device_id) {} |
| 48 ~DispatcherParams() {} | 56 ~DispatcherParams() {} |
| (...skipping 28 matching lines...) Expand all Loading... |
| 77 | 85 |
| 78 AudioManagerBase::AudioManagerBase( | 86 AudioManagerBase::AudioManagerBase( |
| 79 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 87 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | 88 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 81 AudioLogFactory* audio_log_factory) | 89 AudioLogFactory* audio_log_factory) |
| 82 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), | 90 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), |
| 83 max_num_output_streams_(kDefaultMaxOutputStreams), | 91 max_num_output_streams_(kDefaultMaxOutputStreams), |
| 84 max_num_input_streams_(kDefaultMaxInputStreams), | 92 max_num_input_streams_(kDefaultMaxInputStreams), |
| 85 num_output_streams_(0), | 93 num_output_streams_(0), |
| 86 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we | 94 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we |
| 87 // don't | 95 // don't block the UI thread when swapping devices. |
| 88 // block the UI thread when swapping devices. | |
| 89 output_listeners_( | 96 output_listeners_( |
| 90 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 97 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
| 91 audio_log_factory_(audio_log_factory) {} | 98 audio_log_factory_(audio_log_factory) {} |
| 92 | 99 |
| 93 AudioManagerBase::~AudioManagerBase() { | 100 AudioManagerBase::~AudioManagerBase() { |
| 94 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 101 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 95 | 102 |
| 96 // All the output streams should have been deleted. | 103 // All the output streams should have been deleted. |
| 97 CHECK_EQ(0, num_output_streams_); | 104 CHECK_EQ(0, num_output_streams_); |
| 98 // All the input streams should have been deleted. | 105 // All the input streams should have been deleted. |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 CompareByParams(dispatcher_params)); | 285 CompareByParams(dispatcher_params)); |
| 279 if (it != output_dispatchers_.end()) { | 286 if (it != output_dispatchers_.end()) { |
| 280 delete dispatcher_params; | 287 delete dispatcher_params; |
| 281 return (*it)->dispatcher->CreateStreamProxy(); | 288 return (*it)->dispatcher->CreateStreamProxy(); |
| 282 } | 289 } |
| 283 | 290 |
| 284 const base::TimeDelta kCloseDelay = | 291 const base::TimeDelta kCloseDelay = |
| 285 base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds); | 292 base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds); |
| 286 std::unique_ptr<AudioOutputDispatcher> dispatcher; | 293 std::unique_ptr<AudioOutputDispatcher> dispatcher; |
| 287 if (output_params.format() != AudioParameters::AUDIO_FAKE) { | 294 if (output_params.format() != AudioParameters::AUDIO_FAKE) { |
| 295 // Using unretained for |debug_recording_manager_| is safe since it |
| 296 // outlives the dispatchers (cleared in Shutdown()). |
| 288 dispatcher = base::MakeUnique<AudioOutputResampler>( | 297 dispatcher = base::MakeUnique<AudioOutputResampler>( |
| 289 this, params, output_params, output_device_id, kCloseDelay); | 298 this, params, output_params, output_device_id, kCloseDelay, |
| 299 debug_recording_manager_ |
| 300 ? base::BindRepeating( |
| 301 &AudioDebugRecordingManager::RegisterDebugRecordingSource, |
| 302 base::Unretained(debug_recording_manager_.get()), |
| 303 FILE_PATH_LITERAL("output")) |
| 304 : base::BindRepeating(&GetNullptrAudioDebugRecorder)); |
| 290 } else { | 305 } else { |
| 291 dispatcher = base::MakeUnique<AudioOutputDispatcherImpl>( | 306 dispatcher = base::MakeUnique<AudioOutputDispatcherImpl>( |
| 292 this, output_params, output_device_id, kCloseDelay); | 307 this, output_params, output_device_id, kCloseDelay); |
| 293 } | 308 } |
| 294 | 309 |
| 295 dispatcher_params->dispatcher = std::move(dispatcher); | 310 dispatcher_params->dispatcher = std::move(dispatcher); |
| 296 output_dispatchers_.push_back(dispatcher_params); | 311 output_dispatchers_.push_back(dispatcher_params); |
| 297 return dispatcher_params->dispatcher->CreateStreamProxy(); | 312 return dispatcher_params->dispatcher->CreateStreamProxy(); |
| 298 } | 313 } |
| 299 | 314 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 return buffer_size; | 441 return buffer_size; |
| 427 | 442 |
| 428 return 0; | 443 return 0; |
| 429 } | 444 } |
| 430 | 445 |
| 431 std::unique_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 446 std::unique_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
| 432 AudioLogFactory::AudioComponent component) { | 447 AudioLogFactory::AudioComponent component) { |
| 433 return audio_log_factory_->CreateAudioLog(component); | 448 return audio_log_factory_->CreateAudioLog(component); |
| 434 } | 449 } |
| 435 | 450 |
| 451 void AudioManagerBase::InitializeOutputDebugRecording( |
| 452 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
| 453 if (!GetTaskRunner()->BelongsToCurrentThread()) { |
| 454 // AudioManager is deleted on the audio thread, so it's safe to post |
| 455 // unretained. |
| 456 GetTaskRunner()->PostTask( |
| 457 FROM_HERE, |
| 458 base::Bind(&AudioManagerBase::InitializeOutputDebugRecording, |
| 459 base::Unretained(this), std::move(file_task_runner))); |
| 460 return; |
| 461 } |
| 462 |
| 463 DCHECK(!debug_recording_manager_); |
| 464 debug_recording_manager_ = CreateAudioDebugRecordingManager( |
| 465 GetTaskRunner(), std::move(file_task_runner)); |
| 466 } |
| 467 |
| 468 void AudioManagerBase::EnableOutputDebugRecording( |
| 469 const base::FilePath& base_file_name) { |
| 470 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 471 DCHECK(debug_recording_manager_) |
| 472 << "InitializeOutputDebugRecording() must be called before enabling"; |
| 473 debug_recording_manager_->EnableDebugRecording(base_file_name); |
| 474 } |
| 475 |
| 476 void AudioManagerBase::DisableOutputDebugRecording() { |
| 477 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 478 if (debug_recording_manager_) |
| 479 debug_recording_manager_->DisableDebugRecording(); |
| 480 } |
| 481 |
| 482 std::unique_ptr<AudioDebugRecordingManager> |
| 483 AudioManagerBase::CreateAudioDebugRecordingManager( |
| 484 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 485 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { |
| 486 return base::MakeUnique<AudioDebugRecordingManager>( |
| 487 std::move(task_runner), std::move(file_task_runner)); |
| 488 } |
| 489 |
| 436 void AudioManagerBase::SetMaxStreamCountForTesting(int max_input, | 490 void AudioManagerBase::SetMaxStreamCountForTesting(int max_input, |
| 437 int max_output) { | 491 int max_output) { |
| 438 max_num_output_streams_ = max_output; | 492 max_num_output_streams_ = max_output; |
| 439 max_num_input_streams_ = max_input; | 493 max_num_input_streams_ = max_input; |
| 440 } | 494 } |
| 441 | 495 |
| 442 } // namespace media | 496 } // namespace media |
| OLD | NEW |