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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } | 74 } |
75 | 75 |
76 private: | 76 private: |
77 const DispatcherParams* dispatcher_; | 77 const DispatcherParams* dispatcher_; |
78 }; | 78 }; |
79 | 79 |
80 static bool IsDefaultDeviceId(const std::string& device_id) { | 80 static bool IsDefaultDeviceId(const std::string& device_id) { |
81 return device_id.empty() || device_id == AudioManagerBase::kDefaultDeviceId; | 81 return device_id.empty() || device_id == AudioManagerBase::kDefaultDeviceId; |
82 } | 82 } |
83 | 83 |
84 AudioManagerBase::AudioManagerBase(AudioLogFactory* audio_log_factory) | 84 AudioManagerBase::AudioManagerBase( |
85 : max_num_output_streams_(kDefaultMaxOutputStreams), | 85 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 86 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 87 AudioLogFactory* audio_log_factory) |
| 88 : AudioManager(task_runner, worker_task_runner), |
| 89 max_num_output_streams_(kDefaultMaxOutputStreams), |
86 max_num_input_streams_(kDefaultMaxInputStreams), | 90 max_num_input_streams_(kDefaultMaxInputStreams), |
87 num_output_streams_(0), | 91 num_output_streams_(0), |
88 num_input_streams_(0), | 92 num_input_streams_(0), |
89 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we | 93 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we |
90 // don't | 94 // don't |
91 // block the UI thread when swapping devices. | 95 // block the UI thread when swapping devices. |
92 output_listeners_( | 96 output_listeners_( |
93 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 97 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
94 audio_log_factory_(audio_log_factory) { | 98 audio_log_factory_(audio_log_factory) {} |
95 } | |
96 | 99 |
97 AudioManagerBase::~AudioManagerBase() { | 100 AudioManagerBase::~AudioManagerBase() { |
98 // The platform specific AudioManager implementation must have already | |
99 // stopped the audio thread. Otherwise, we may destroy audio streams before | |
100 // stopping the thread, resulting an unexpected behavior. | |
101 // This way we make sure activities of the audio streams are all stopped | |
102 // before we destroy them. | |
103 CHECK(!audio_thread_); | |
104 // All the output streams should have been deleted. | 101 // All the output streams should have been deleted. |
105 DCHECK_EQ(0, num_output_streams_); | 102 DCHECK_EQ(0, num_output_streams_); |
106 // All the input streams should have been deleted. | 103 // All the input streams should have been deleted. |
107 DCHECK_EQ(0, num_input_streams_); | 104 DCHECK_EQ(0, num_input_streams_); |
108 } | 105 } |
109 | 106 |
110 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { | 107 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { |
111 return base::string16(); | 108 return base::string16(); |
112 } | 109 } |
113 | 110 |
114 scoped_refptr<base::SingleThreadTaskRunner> AudioManagerBase::GetTaskRunner() { | |
115 if (!audio_thread_) { | |
116 audio_thread_.reset(new base::Thread("AudioThread")); | |
117 #if defined(OS_WIN) | |
118 audio_thread_->init_com_with_mta(true); | |
119 #endif | |
120 CHECK(audio_thread_->Start()); | |
121 } | |
122 return audio_thread_->task_runner(); | |
123 } | |
124 | |
125 scoped_refptr<base::SingleThreadTaskRunner> | |
126 AudioManagerBase::GetWorkerTaskRunner() { | |
127 return GetTaskRunner(); | |
128 } | |
129 | |
130 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | 111 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
131 const AudioParameters& params, | 112 const AudioParameters& params, |
132 const std::string& device_id) { | 113 const std::string& device_id) { |
133 // TODO(miu): Fix ~50 call points across several unit test modules to call | 114 // TODO(miu): Fix ~50 call points across several unit test modules to call |
134 // this method on the audio thread, then uncomment the following: | 115 // this method on the audio thread, then uncomment the following: |
135 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 116 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
136 | 117 |
137 if (!params.IsValid()) { | 118 if (!params.IsValid()) { |
138 DLOG(ERROR) << "Audio parameters are invalid"; | 119 DLOG(ERROR) << "Audio parameters are invalid"; |
139 return NULL; | 120 return NULL; |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 } | 295 } |
315 | 296 |
316 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 297 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
317 DCHECK(stream); | 298 DCHECK(stream); |
318 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 299 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
319 --num_input_streams_; | 300 --num_input_streams_; |
320 delete stream; | 301 delete stream; |
321 } | 302 } |
322 | 303 |
323 void AudioManagerBase::Shutdown() { | 304 void AudioManagerBase::Shutdown() { |
324 // Only true when we're sharing the UI message loop with the browser. The UI | |
325 // loop is no longer running at this time and browser destruction is imminent. | |
326 auto task_runner = GetTaskRunner(); | |
327 if (task_runner->BelongsToCurrentThread()) { | |
328 ShutdownOnAudioThread(); | |
329 } else { | |
330 task_runner->PostTask(FROM_HERE, | |
331 base::Bind(&AudioManagerBase::ShutdownOnAudioThread, | |
332 base::Unretained(this))); | |
333 } | |
334 | |
335 // Stop() will wait for any posted messages to be processed first. | |
336 if (audio_thread_) { | |
337 audio_thread_->Stop(); | |
338 audio_thread_.reset(); | |
339 } | |
340 } | |
341 | |
342 void AudioManagerBase::ShutdownOnAudioThread() { | |
343 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 305 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
344 while (!output_dispatchers_.empty()) { | 306 while (!output_dispatchers_.empty()) { |
345 output_dispatchers_.back()->dispatcher->Shutdown(); | 307 output_dispatchers_.back()->dispatcher->Shutdown(); |
346 output_dispatchers_.pop_back(); | 308 output_dispatchers_.pop_back(); |
347 } | 309 } |
348 } | 310 } |
349 | 311 |
350 void AudioManagerBase::AddOutputDeviceChangeListener( | 312 void AudioManagerBase::AddOutputDeviceChangeListener( |
351 AudioDeviceListener* listener) { | 313 AudioDeviceListener* listener) { |
352 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); | 314 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 | 363 |
402 return 0; | 364 return 0; |
403 } | 365 } |
404 | 366 |
405 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 367 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
406 AudioLogFactory::AudioComponent component) { | 368 AudioLogFactory::AudioComponent component) { |
407 return audio_log_factory_->CreateAudioLog(component); | 369 return audio_log_factory_->CreateAudioLog(component); |
408 } | 370 } |
409 | 371 |
410 } // namespace media | 372 } // namespace media |
OLD | NEW |