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" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 dispatcher_->output_device_id == dispatcher_in->output_device_id); | 71 dispatcher_->output_device_id == dispatcher_in->output_device_id); |
72 } | 72 } |
73 | 73 |
74 private: | 74 private: |
75 const DispatcherParams* dispatcher_; | 75 const DispatcherParams* dispatcher_; |
76 }; | 76 }; |
77 | 77 |
78 AudioManagerBase::AudioManagerBase( | 78 AudioManagerBase::AudioManagerBase( |
79 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 79 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
80 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | 80 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
81 AudioLogFactory* audio_log_factory) | 81 AudioLogFactory* audio_log_factory, |
| 82 CreateAudioFileWriterCallback create_audio_file_writer_callback) |
82 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), | 83 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), |
83 max_num_output_streams_(kDefaultMaxOutputStreams), | 84 max_num_output_streams_(kDefaultMaxOutputStreams), |
84 max_num_input_streams_(kDefaultMaxInputStreams), | 85 max_num_input_streams_(kDefaultMaxInputStreams), |
85 num_output_streams_(0), | 86 num_output_streams_(0), |
86 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we | 87 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we |
87 // don't | 88 // don't block the UI thread when swapping devices. |
88 // block the UI thread when swapping devices. | |
89 output_listeners_( | 89 output_listeners_( |
90 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 90 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
91 audio_log_factory_(audio_log_factory) {} | 91 audio_log_factory_(audio_log_factory), |
| 92 create_audio_file_writer_callback_( |
| 93 std::move(create_audio_file_writer_callback)) { |
| 94 CHECK(!create_audio_file_writer_callback_.Equals( |
| 95 CreateAudioFileWriterCallback())); |
| 96 } |
92 | 97 |
93 AudioManagerBase::~AudioManagerBase() { | 98 AudioManagerBase::~AudioManagerBase() { |
94 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 99 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
95 | 100 |
96 // All the output streams should have been deleted. | 101 // All the output streams should have been deleted. |
97 CHECK_EQ(0, num_output_streams_); | 102 CHECK_EQ(0, num_output_streams_); |
98 // All the input streams should have been deleted. | 103 // All the input streams should have been deleted. |
99 CHECK(input_streams_.empty()); | 104 CHECK(input_streams_.empty()); |
100 } | 105 } |
101 | 106 |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 CompareByParams(dispatcher_params)); | 283 CompareByParams(dispatcher_params)); |
279 if (it != output_dispatchers_.end()) { | 284 if (it != output_dispatchers_.end()) { |
280 delete dispatcher_params; | 285 delete dispatcher_params; |
281 return (*it)->dispatcher->CreateStreamProxy(); | 286 return (*it)->dispatcher->CreateStreamProxy(); |
282 } | 287 } |
283 | 288 |
284 const base::TimeDelta kCloseDelay = | 289 const base::TimeDelta kCloseDelay = |
285 base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds); | 290 base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds); |
286 std::unique_ptr<AudioOutputDispatcher> dispatcher; | 291 std::unique_ptr<AudioOutputDispatcher> dispatcher; |
287 if (output_params.format() != AudioParameters::AUDIO_FAKE) { | 292 if (output_params.format() != AudioParameters::AUDIO_FAKE) { |
288 dispatcher = base::MakeUnique<AudioOutputResampler>( | 293 std::unique_ptr<AudioOutputResampler> resampler = |
289 this, params, output_params, output_device_id, kCloseDelay); | 294 base::MakeUnique<AudioOutputResampler>(this, params, output_params, |
| 295 output_device_id, kCloseDelay); |
| 296 output_resamplers_.insert(resampler.get()); |
| 297 |
| 298 // Debug recording is enabled if the file name is non-empty. If so, also |
| 299 // enable it on the new resampler. |
| 300 if (!debug_recording_base_file_name_.empty()) { |
| 301 resampler->EnableDebugRecording(GetDebugRecordingFileNameWithExtension( |
| 302 debug_recording_base_file_name_)); |
| 303 } |
| 304 |
| 305 dispatcher = std::move(resampler); |
290 } else { | 306 } else { |
291 dispatcher = base::MakeUnique<AudioOutputDispatcherImpl>( | 307 dispatcher = base::MakeUnique<AudioOutputDispatcherImpl>( |
292 this, output_params, output_device_id, kCloseDelay); | 308 this, output_params, output_device_id, kCloseDelay); |
293 } | 309 } |
294 | 310 |
295 dispatcher_params->dispatcher = std::move(dispatcher); | 311 dispatcher_params->dispatcher = std::move(dispatcher); |
296 output_dispatchers_.push_back(dispatcher_params); | 312 output_dispatchers_.push_back(dispatcher_params); |
297 return dispatcher_params->dispatcher->CreateStreamProxy(); | 313 return dispatcher_params->dispatcher->CreateStreamProxy(); |
298 } | 314 } |
299 | 315 |
(...skipping 25 matching lines...) Expand all Loading... |
325 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 341 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
326 CHECK_EQ(1u, input_streams_.erase(stream)); | 342 CHECK_EQ(1u, input_streams_.erase(stream)); |
327 delete stream; | 343 delete stream; |
328 } | 344 } |
329 | 345 |
330 void AudioManagerBase::Shutdown() { | 346 void AudioManagerBase::Shutdown() { |
331 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 347 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
332 | 348 |
333 // Close all output streams. | 349 // Close all output streams. |
334 output_dispatchers_.clear(); | 350 output_dispatchers_.clear(); |
| 351 output_resamplers_.clear(); |
335 | 352 |
336 #if defined(OS_MACOSX) | 353 #if defined(OS_MACOSX) |
337 // On mac, AudioManager runs on the main thread, loop for which stops | 354 // On mac, AudioManager runs on the main thread, loop for which stops |
338 // processing task queue at this point. So even if tasks to close the | 355 // processing task queue at this point. So even if tasks to close the |
339 // streams are enqueued, they would not run leading to CHECKs getting hit | 356 // streams are enqueued, they would not run leading to CHECKs getting hit |
340 // in the destructor about open streams. Close them explicitly here. | 357 // in the destructor about open streams. Close them explicitly here. |
341 // crbug.com/608049. | 358 // crbug.com/608049. |
342 for (auto iter = input_streams_.begin(); iter != input_streams_.end();) { | 359 for (auto iter = input_streams_.begin(); iter != input_streams_.end();) { |
343 // Note: Closing the stream will invalidate the iterator. | 360 // Note: Closing the stream will invalidate the iterator. |
344 // Increment the iterator before closing the stream. | 361 // Increment the iterator before closing the stream. |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 return buffer_size; | 443 return buffer_size; |
427 | 444 |
428 return 0; | 445 return 0; |
429 } | 446 } |
430 | 447 |
431 std::unique_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 448 std::unique_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
432 AudioLogFactory::AudioComponent component) { | 449 AudioLogFactory::AudioComponent component) { |
433 return audio_log_factory_->CreateAudioLog(component); | 450 return audio_log_factory_->CreateAudioLog(component); |
434 } | 451 } |
435 | 452 |
| 453 std::unique_ptr<AudioFileWriter> AudioManagerBase::CreateAudioFileWriter( |
| 454 const AudioParameters& params) { |
| 455 return create_audio_file_writer_callback_.Run(params); |
| 456 } |
| 457 |
| 458 void AudioManagerBase::EnableOutputDebugRecording( |
| 459 const base::FilePath& base_file_name) { |
| 460 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 461 for (auto it : output_resamplers_) { |
| 462 it->EnableDebugRecording( |
| 463 GetDebugRecordingFileNameWithExtension(base_file_name)); |
| 464 } |
| 465 debug_recording_base_file_name_ = base_file_name; |
| 466 } |
| 467 |
| 468 void AudioManagerBase::DisableOutputDebugRecording() { |
| 469 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 470 for (auto it : output_resamplers_) |
| 471 it->DisableDebugRecording(); |
| 472 debug_recording_base_file_name_.clear(); |
| 473 } |
| 474 |
| 475 base::FilePath AudioManagerBase::GetDebugRecordingFileNameWithExtension( |
| 476 const base::FilePath& file_name) { |
| 477 return file_name.AddExtension("output"); |
| 478 } |
| 479 |
436 void AudioManagerBase::SetMaxStreamCountForTesting(int max_input, | 480 void AudioManagerBase::SetMaxStreamCountForTesting(int max_input, |
437 int max_output) { | 481 int max_output) { |
438 max_num_output_streams_ = max_output; | 482 max_num_output_streams_ = max_output; |
439 max_num_input_streams_ = max_input; | 483 max_num_input_streams_ = max_input; |
440 } | 484 } |
441 | 485 |
442 } // namespace media | 486 } // namespace media |
OLD | NEW |