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(AudioLogFactory* audio_log_factory) | 92 AudioManagerBase::AudioManagerBase( |
93 : max_num_output_streams_(kDefaultMaxOutputStreams), | 93 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 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), |
94 max_num_input_streams_(kDefaultMaxInputStreams), | 98 max_num_input_streams_(kDefaultMaxInputStreams), |
95 num_output_streams_(0), | 99 num_output_streams_(0), |
96 num_input_streams_(0), | 100 num_input_streams_(0), |
97 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we | 101 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we |
98 // don't | 102 // don't |
99 // block the UI thread when swapping devices. | 103 // block the UI thread when swapping devices. |
100 output_listeners_( | 104 output_listeners_( |
101 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 105 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
102 audio_log_factory_(audio_log_factory) { | 106 audio_log_factory_(audio_log_factory) {} |
103 } | |
104 | 107 |
105 AudioManagerBase::~AudioManagerBase() { | 108 AudioManagerBase::~AudioManagerBase() { |
106 // The platform specific AudioManager implementation must have already | 109 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
107 // stopped the audio thread. Otherwise, we may destroy audio streams before | 110 |
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_); | |
112 // All the output streams should have been deleted. | 111 // All the output streams should have been deleted. |
113 DCHECK_EQ(0, num_output_streams_); | 112 CHECK_EQ(0, num_output_streams_); |
114 // All the input streams should have been deleted. | 113 // All the input streams should have been deleted. |
115 DCHECK_EQ(0, num_input_streams_); | 114 CHECK_EQ(0, num_input_streams_); |
116 } | 115 } |
117 | 116 |
118 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { | 117 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { |
119 return base::string16(); | 118 return base::string16(); |
120 } | 119 } |
121 | 120 |
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 | |
138 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | 121 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
139 const AudioParameters& params, | 122 const AudioParameters& params, |
140 const std::string& device_id) { | 123 const std::string& device_id) { |
141 // TODO(miu): Fix ~50 call points across several unit test modules to call | 124 // TODO(miu): Fix ~50 call points across several unit test modules to call |
142 // this method on the audio thread, then uncomment the following: | 125 // this method on the audio thread, then uncomment the following: |
143 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 126 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
144 | 127 |
145 if (!params.IsValid()) { | 128 if (!params.IsValid()) { |
146 DLOG(ERROR) << "Audio parameters are invalid"; | 129 DLOG(ERROR) << "Audio parameters are invalid"; |
147 return NULL; | 130 return NULL; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 } | 305 } |
323 | 306 |
324 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 307 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
325 DCHECK(stream); | 308 DCHECK(stream); |
326 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 309 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
327 --num_input_streams_; | 310 --num_input_streams_; |
328 delete stream; | 311 delete stream; |
329 } | 312 } |
330 | 313 |
331 void AudioManagerBase::Shutdown() { | 314 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() { | |
351 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 315 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 316 // Close all output streams. |
352 while (!output_dispatchers_.empty()) { | 317 while (!output_dispatchers_.empty()) { |
353 output_dispatchers_.back()->dispatcher->Shutdown(); | 318 output_dispatchers_.back()->dispatcher->Shutdown(); |
354 output_dispatchers_.pop_back(); | 319 output_dispatchers_.pop_back(); |
355 } | 320 } |
356 } | 321 } |
357 | 322 |
358 void AudioManagerBase::AddOutputDeviceChangeListener( | 323 void AudioManagerBase::AddOutputDeviceChangeListener( |
359 AudioDeviceListener* listener) { | 324 AudioDeviceListener* listener) { |
360 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 325 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
361 output_listeners_.AddObserver(listener); | 326 output_listeners_.AddObserver(listener); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 | 374 |
410 return 0; | 375 return 0; |
411 } | 376 } |
412 | 377 |
413 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 378 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
414 AudioLogFactory::AudioComponent component) { | 379 AudioLogFactory::AudioComponent component) { |
415 return audio_log_factory_->CreateAudioLog(component); | 380 return audio_log_factory_->CreateAudioLog(component); |
416 } | 381 } |
417 | 382 |
418 } // namespace media | 383 } // namespace media |
OLD | NEW |