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_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_debug_recording_helper.h" | |
| 16 #include "media/audio/audio_device_description.h" | 17 #include "media/audio/audio_device_description.h" |
| 17 #include "media/audio/audio_output_dispatcher_impl.h" | 18 #include "media/audio/audio_output_dispatcher_impl.h" |
| 18 #include "media/audio/audio_output_proxy.h" | 19 #include "media/audio/audio_output_proxy.h" |
| 19 #include "media/audio/audio_output_resampler.h" | 20 #include "media/audio/audio_output_resampler.h" |
| 20 #include "media/audio/fake_audio_input_stream.h" | 21 #include "media/audio/fake_audio_input_stream.h" |
| 21 #include "media/audio/fake_audio_output_stream.h" | 22 #include "media/audio/fake_audio_output_stream.h" |
| 22 #include "media/base/media_switches.h" | 23 #include "media/base/media_switches.h" |
| 23 | 24 |
| 24 namespace media { | 25 namespace media { |
| 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 #if defined(OS_WIN) | |
| 41 #define IntToStringType base::IntToString16 | |
| 42 #else | |
| 43 #define IntToStringType base::IntToString | |
| 44 #endif | |
| 45 | |
| 46 // Helper function. | |
| 47 base::FilePath GetOutputDebugRecordingFileNameWithExtensions( | |
| 48 const base::FilePath& file_name, | |
| 49 int id) { | |
| 50 return file_name.AddExtension("output").AddExtension(IntToStringType(id)); | |
| 51 } | |
| 52 | |
| 39 } // namespace | 53 } // namespace |
| 40 | 54 |
| 41 struct AudioManagerBase::DispatcherParams { | 55 struct AudioManagerBase::DispatcherParams { |
| 42 DispatcherParams(const AudioParameters& input, | 56 DispatcherParams(const AudioParameters& input, |
| 43 const AudioParameters& output, | 57 const AudioParameters& output, |
| 44 const std::string& output_device_id) | 58 const std::string& output_device_id) |
| 45 : input_params(input), | 59 : input_params(input), |
| 46 output_params(output), | 60 output_params(output), |
| 47 output_device_id(output_device_id) {} | 61 output_device_id(output_device_id) {} |
| 48 ~DispatcherParams() {} | 62 ~DispatcherParams() {} |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 71 dispatcher_->output_device_id == dispatcher_in->output_device_id); | 85 dispatcher_->output_device_id == dispatcher_in->output_device_id); |
| 72 } | 86 } |
| 73 | 87 |
| 74 private: | 88 private: |
| 75 const DispatcherParams* dispatcher_; | 89 const DispatcherParams* dispatcher_; |
| 76 }; | 90 }; |
| 77 | 91 |
| 78 AudioManagerBase::AudioManagerBase( | 92 AudioManagerBase::AudioManagerBase( |
| 79 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 93 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | 94 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 81 AudioLogFactory* audio_log_factory) | 95 AudioLogFactory* audio_log_factory, |
| 96 CreateAudioFileWriterCallback create_audio_file_writer_callback) | |
| 82 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), | 97 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), |
| 83 max_num_output_streams_(kDefaultMaxOutputStreams), | 98 max_num_output_streams_(kDefaultMaxOutputStreams), |
| 84 max_num_input_streams_(kDefaultMaxInputStreams), | 99 max_num_input_streams_(kDefaultMaxInputStreams), |
| 85 num_output_streams_(0), | 100 num_output_streams_(0), |
| 86 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we | 101 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we |
| 87 // don't | 102 // don't block the UI thread when swapping devices. |
| 88 // block the UI thread when swapping devices. | |
| 89 output_listeners_( | 103 output_listeners_( |
| 90 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 104 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
| 91 audio_log_factory_(audio_log_factory) {} | 105 audio_log_factory_(audio_log_factory), |
| 106 create_audio_file_writer_callback_( | |
| 107 std::move(create_audio_file_writer_callback)) { | |
| 108 DCHECK(create_audio_file_writer_callback_); | |
| 109 } | |
| 92 | 110 |
| 93 AudioManagerBase::~AudioManagerBase() { | 111 AudioManagerBase::~AudioManagerBase() { |
| 94 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 112 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 95 | 113 |
| 96 // All the output streams should have been deleted. | 114 // All the output streams should have been deleted. |
| 97 CHECK_EQ(0, num_output_streams_); | 115 CHECK_EQ(0, num_output_streams_); |
| 98 // All the input streams should have been deleted. | 116 // All the input streams should have been deleted. |
| 99 CHECK(input_streams_.empty()); | 117 CHECK(input_streams_.empty()); |
| 100 } | 118 } |
| 101 | 119 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 CompareByParams(dispatcher_params)); | 296 CompareByParams(dispatcher_params)); |
| 279 if (it != output_dispatchers_.end()) { | 297 if (it != output_dispatchers_.end()) { |
| 280 delete dispatcher_params; | 298 delete dispatcher_params; |
| 281 return (*it)->dispatcher->CreateStreamProxy(); | 299 return (*it)->dispatcher->CreateStreamProxy(); |
| 282 } | 300 } |
| 283 | 301 |
| 284 const base::TimeDelta kCloseDelay = | 302 const base::TimeDelta kCloseDelay = |
| 285 base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds); | 303 base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds); |
| 286 std::unique_ptr<AudioOutputDispatcher> dispatcher; | 304 std::unique_ptr<AudioOutputDispatcher> dispatcher; |
| 287 if (output_params.format() != AudioParameters::AUDIO_FAKE) { | 305 if (output_params.format() != AudioParameters::AUDIO_FAKE) { |
| 288 dispatcher = base::MakeUnique<AudioOutputResampler>( | 306 std::unique_ptr<AudioOutputResampler> resampler = |
| 289 this, params, output_params, output_device_id, kCloseDelay); | 307 base::MakeUnique<AudioOutputResampler>(this, params, output_params, |
| 308 output_device_id, kCloseDelay); | |
| 309 | |
| 310 resampler->SetDebugRecordingCallbacks( | |
| 311 base::Bind(&AudioManagerBase::RegisterDebugRecordingSource, | |
| 312 base::Unretained(this)), | |
| 313 base::Bind(&AudioManagerBase::UnregisterDebugRecordingSource, | |
| 314 base::Unretained(this)), | |
| 315 base::Bind(&AudioManagerBase::OnDebugRecordingData, | |
| 316 base::Unretained(this))); | |
| 317 | |
| 318 dispatcher = std::move(resampler); | |
| 290 } else { | 319 } else { |
| 291 dispatcher = base::MakeUnique<AudioOutputDispatcherImpl>( | 320 dispatcher = base::MakeUnique<AudioOutputDispatcherImpl>( |
| 292 this, output_params, output_device_id, kCloseDelay); | 321 this, output_params, output_device_id, kCloseDelay); |
| 293 } | 322 } |
| 294 | 323 |
| 295 dispatcher_params->dispatcher = std::move(dispatcher); | 324 dispatcher_params->dispatcher = std::move(dispatcher); |
| 296 output_dispatchers_.push_back(dispatcher_params); | 325 output_dispatchers_.push_back(dispatcher_params); |
| 297 return dispatcher_params->dispatcher->CreateStreamProxy(); | 326 return dispatcher_params->dispatcher->CreateStreamProxy(); |
| 298 } | 327 } |
| 299 | 328 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 426 return buffer_size; | 455 return buffer_size; |
| 427 | 456 |
| 428 return 0; | 457 return 0; |
| 429 } | 458 } |
| 430 | 459 |
| 431 std::unique_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 460 std::unique_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
| 432 AudioLogFactory::AudioComponent component) { | 461 AudioLogFactory::AudioComponent component) { |
| 433 return audio_log_factory_->CreateAudioLog(component); | 462 return audio_log_factory_->CreateAudioLog(component); |
| 434 } | 463 } |
| 435 | 464 |
| 465 void AudioManagerBase::EnableOutputDebugRecording( | |
| 466 const base::FilePath& base_file_name) { | |
| 467 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 468 for (const auto& it : debug_recording_helpers_) { | |
| 469 it.second->EnableDebugRecording( | |
| 470 GetOutputDebugRecordingFileNameWithExtensions(base_file_name, | |
| 471 it.first)); | |
| 472 } | |
| 473 debug_recording_base_file_name_ = base_file_name; | |
| 474 } | |
| 475 | |
| 476 void AudioManagerBase::DisableOutputDebugRecording() { | |
| 477 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 478 for (const auto& it : debug_recording_helpers_) | |
| 479 it.second->DisableDebugRecording(); | |
| 480 debug_recording_base_file_name_.clear(); | |
| 481 } | |
| 482 | |
| 483 void AudioManagerBase::RegisterDebugRecordingSource( | |
|
o1ka
2017/01/25 17:47:00
If keeping this logic, could you move it into a he
Henrik Grunell
2017/01/26 10:25:09
Yep, agree.
| |
| 484 int id, | |
| 485 const AudioParameters& params) { | |
| 486 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 487 DCHECK(debug_recording_helpers_.find(id) == debug_recording_helpers_.end()); | |
| 488 debug_recording_helpers_[id] = base::MakeUnique<AudioDebugRecordingHelper>( | |
| 489 params, create_audio_file_writer_callback_, GetTaskRunner()); | |
| 490 if (!debug_recording_base_file_name_.empty()) { | |
| 491 debug_recording_helpers_[id]->EnableDebugRecording( | |
| 492 GetOutputDebugRecordingFileNameWithExtensions( | |
| 493 debug_recording_base_file_name_, id)); | |
| 494 } | |
| 495 } | |
| 496 | |
| 497 void AudioManagerBase::UnregisterDebugRecordingSource(int id) { | |
| 498 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | |
| 499 auto it = debug_recording_helpers_.find(id); | |
| 500 DCHECK(it != debug_recording_helpers_.end()); | |
| 501 it->second->DisableDebugRecording(); | |
| 502 debug_recording_helpers_.erase(id); | |
| 503 } | |
| 504 | |
| 505 void AudioManagerBase::OnDebugRecordingData(int id, AudioBus* data) { | |
| 506 // TODO BEFORE COMMIT: Comment on why accessing |debug_recording_helpers_| | |
| 507 // here is safe. | |
|
o1ka
2017/01/25 17:47:00
This does not looks like a good separation of conc
Henrik Grunell
2017/01/26 10:25:09
Yes, will do.
| |
| 508 auto it = debug_recording_helpers_.find(id); | |
| 509 DCHECK(it != debug_recording_helpers_.end()); | |
| 510 it->second->MaybeWrite(data); | |
| 511 } | |
| 512 | |
| 436 void AudioManagerBase::SetMaxStreamCountForTesting(int max_input, | 513 void AudioManagerBase::SetMaxStreamCountForTesting(int max_input, |
| 437 int max_output) { | 514 int max_output) { |
| 438 max_num_output_streams_ = max_output; | 515 max_num_output_streams_ = max_output; |
| 439 max_num_input_streams_ = max_input; | 516 max_num_input_streams_ = max_input; |
| 440 } | 517 } |
| 441 | 518 |
| 442 } // namespace media | 519 } // namespace media |
| OLD | NEW |