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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 return device_id.empty() || device_id == AudioManagerBase::kDefaultDeviceId; | 82 return device_id.empty() || device_id == AudioManagerBase::kDefaultDeviceId; |
83 } | 83 } |
84 | 84 |
85 // static | 85 // static |
86 bool AudioManagerBase::UseSessionIdToSelectDevice( | 86 bool AudioManagerBase::UseSessionIdToSelectDevice( |
87 int session_id, | 87 int session_id, |
88 const std::string& device_id) { | 88 const std::string& device_id) { |
89 return session_id && device_id.empty(); | 89 return session_id && device_id.empty(); |
90 } | 90 } |
91 | 91 |
92 AudioManagerBase::AudioManagerBase( | 92 AudioManagerBase::AudioManagerBase(AudioLogFactory* audio_log_factory) |
93 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 93 : max_num_output_streams_(kDefaultMaxOutputStreams), |
94 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
95 AudioLogFactory* audio_log_factory) | |
96 : AudioManager(std::move(task_runner), std::move(worker_task_runner)), | |
97 max_num_output_streams_(kDefaultMaxOutputStreams), | |
98 max_num_input_streams_(kDefaultMaxInputStreams), | 94 max_num_input_streams_(kDefaultMaxInputStreams), |
99 num_output_streams_(0), | 95 num_output_streams_(0), |
100 num_input_streams_(0), | 96 num_input_streams_(0), |
101 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we | 97 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we |
102 // don't | 98 // don't |
103 // block the UI thread when swapping devices. | 99 // block the UI thread when swapping devices. |
104 output_listeners_( | 100 output_listeners_( |
105 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 101 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
106 audio_log_factory_(audio_log_factory) {} | 102 audio_log_factory_(audio_log_factory) { |
| 103 } |
107 | 104 |
108 AudioManagerBase::~AudioManagerBase() { | 105 AudioManagerBase::~AudioManagerBase() { |
109 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 106 // The platform specific AudioManager implementation must have already |
110 | 107 // stopped the audio thread. Otherwise, we may destroy audio streams before |
| 108 // stopping the thread, resulting an unexpected behavior. |
| 109 // This way we make sure activities of the audio streams are all stopped |
| 110 // before we destroy them. |
| 111 CHECK(!audio_thread_); |
111 // All the output streams should have been deleted. | 112 // All the output streams should have been deleted. |
112 CHECK_EQ(0, num_output_streams_); | 113 DCHECK_EQ(0, num_output_streams_); |
113 // All the input streams should have been deleted. | 114 // All the input streams should have been deleted. |
114 CHECK_EQ(0, num_input_streams_); | 115 DCHECK_EQ(0, num_input_streams_); |
115 } | 116 } |
116 | 117 |
117 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { | 118 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { |
118 return base::string16(); | 119 return base::string16(); |
119 } | 120 } |
120 | 121 |
| 122 scoped_refptr<base::SingleThreadTaskRunner> AudioManagerBase::GetTaskRunner() { |
| 123 if (!audio_thread_) { |
| 124 audio_thread_.reset(new base::Thread("AudioThread")); |
| 125 #if defined(OS_WIN) |
| 126 audio_thread_->init_com_with_mta(true); |
| 127 #endif |
| 128 CHECK(audio_thread_->Start()); |
| 129 } |
| 130 return audio_thread_->task_runner(); |
| 131 } |
| 132 |
| 133 scoped_refptr<base::SingleThreadTaskRunner> |
| 134 AudioManagerBase::GetWorkerTaskRunner() { |
| 135 return GetTaskRunner(); |
| 136 } |
| 137 |
121 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | 138 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
122 const AudioParameters& params, | 139 const AudioParameters& params, |
123 const std::string& device_id) { | 140 const std::string& device_id) { |
124 // TODO(miu): Fix ~50 call points across several unit test modules to call | 141 // TODO(miu): Fix ~50 call points across several unit test modules to call |
125 // this method on the audio thread, then uncomment the following: | 142 // this method on the audio thread, then uncomment the following: |
126 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 143 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
127 | 144 |
128 if (!params.IsValid()) { | 145 if (!params.IsValid()) { |
129 DLOG(ERROR) << "Audio parameters are invalid"; | 146 DLOG(ERROR) << "Audio parameters are invalid"; |
130 return NULL; | 147 return NULL; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 } | 322 } |
306 | 323 |
307 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 324 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
308 DCHECK(stream); | 325 DCHECK(stream); |
309 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 326 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
310 --num_input_streams_; | 327 --num_input_streams_; |
311 delete stream; | 328 delete stream; |
312 } | 329 } |
313 | 330 |
314 void AudioManagerBase::Shutdown() { | 331 void AudioManagerBase::Shutdown() { |
| 332 // Only true when we're sharing the UI message loop with the browser. The UI |
| 333 // loop is no longer running at this time and browser destruction is imminent. |
| 334 auto task_runner = GetTaskRunner(); |
| 335 if (task_runner->BelongsToCurrentThread()) { |
| 336 ShutdownOnAudioThread(); |
| 337 } else { |
| 338 task_runner->PostTask(FROM_HERE, |
| 339 base::Bind(&AudioManagerBase::ShutdownOnAudioThread, |
| 340 base::Unretained(this))); |
| 341 } |
| 342 |
| 343 // Stop() will wait for any posted messages to be processed first. |
| 344 if (audio_thread_) { |
| 345 audio_thread_->Stop(); |
| 346 audio_thread_.reset(); |
| 347 } |
| 348 } |
| 349 |
| 350 void AudioManagerBase::ShutdownOnAudioThread() { |
315 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 351 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
316 // Close all output streams. | |
317 while (!output_dispatchers_.empty()) { | 352 while (!output_dispatchers_.empty()) { |
318 output_dispatchers_.back()->dispatcher->Shutdown(); | 353 output_dispatchers_.back()->dispatcher->Shutdown(); |
319 output_dispatchers_.pop_back(); | 354 output_dispatchers_.pop_back(); |
320 } | 355 } |
321 } | 356 } |
322 | 357 |
323 void AudioManagerBase::AddOutputDeviceChangeListener( | 358 void AudioManagerBase::AddOutputDeviceChangeListener( |
324 AudioDeviceListener* listener) { | 359 AudioDeviceListener* listener) { |
325 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 360 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
326 output_listeners_.AddObserver(listener); | 361 output_listeners_.AddObserver(listener); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 | 409 |
375 return 0; | 410 return 0; |
376 } | 411 } |
377 | 412 |
378 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 413 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
379 AudioLogFactory::AudioComponent component) { | 414 AudioLogFactory::AudioComponent component) { |
380 return audio_log_factory_->CreateAudioLog(component); | 415 return audio_log_factory_->CreateAudioLog(component); |
381 } | 416 } |
382 | 417 |
383 } // namespace media | 418 } // namespace media |
OLD | NEW |