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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | |
11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
12 #include "media/audio/audio_output_dispatcher_impl.h" | 13 #include "media/audio/audio_output_dispatcher_impl.h" |
13 #include "media/audio/audio_output_proxy.h" | 14 #include "media/audio/audio_output_proxy.h" |
14 #include "media/audio/audio_output_resampler.h" | 15 #include "media/audio/audio_output_resampler.h" |
15 #include "media/audio/fake_audio_input_stream.h" | 16 #include "media/audio/fake_audio_input_stream.h" |
16 #include "media/audio/fake_audio_output_stream.h" | 17 #include "media/audio/fake_audio_output_stream.h" |
17 #include "media/base/media_switches.h" | 18 #include "media/base/media_switches.h" |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 | 21 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 | 74 |
74 AudioManagerBase::AudioManagerBase(AudioLogFactory* audio_log_factory) | 75 AudioManagerBase::AudioManagerBase(AudioLogFactory* audio_log_factory) |
75 : max_num_output_streams_(kDefaultMaxOutputStreams), | 76 : max_num_output_streams_(kDefaultMaxOutputStreams), |
76 max_num_input_streams_(kDefaultMaxInputStreams), | 77 max_num_input_streams_(kDefaultMaxInputStreams), |
77 num_output_streams_(0), | 78 num_output_streams_(0), |
78 num_input_streams_(0), | 79 num_input_streams_(0), |
79 // TODO(dalecurtis): Switch this to an ObserverListThreadSafe, so we don't | 80 // TODO(dalecurtis): Switch this to an ObserverListThreadSafe, so we don't |
80 // block the UI thread when swapping devices. | 81 // block the UI thread when swapping devices. |
81 output_listeners_( | 82 output_listeners_( |
82 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 83 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
84 state_listeners_(ObserverList<StateChangeListener>::NOTIFY_EXISTING_ONLY), | |
83 audio_thread_("AudioThread"), | 85 audio_thread_("AudioThread"), |
84 audio_log_factory_(audio_log_factory) { | 86 audio_log_factory_(audio_log_factory) { |
85 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
86 audio_thread_.init_com_with_mta(true); | 88 audio_thread_.init_com_with_mta(true); |
87 #elif defined(OS_MACOSX) | 89 #elif defined(OS_MACOSX) |
88 // CoreAudio calls must occur on the main thread of the process, which in our | 90 // CoreAudio calls must occur on the main thread of the process, which in our |
89 // case is sadly the browser UI thread. Failure to execute calls on the right | 91 // case is sadly the browser UI thread. Failure to execute calls on the right |
90 // thread leads to crashes and odd behavior. See http://crbug.com/158170. | 92 // thread leads to crashes and odd behavior. See http://crbug.com/158170. |
91 // TODO(dalecurtis): We should require the message loop to be passed in. | 93 // TODO(dalecurtis): We should require the message loop to be passed in. |
92 if (base::MessageLoopForUI::IsCurrent()) { | 94 if (base::MessageLoopForUI::IsCurrent()) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 | 182 |
181 AudioInputStream* AudioManagerBase::MakeAudioInputStream( | 183 AudioInputStream* AudioManagerBase::MakeAudioInputStream( |
182 const AudioParameters& params, | 184 const AudioParameters& params, |
183 const std::string& device_id) { | 185 const std::string& device_id) { |
184 // TODO(miu): Fix ~20 call points across several unit test modules to call | 186 // TODO(miu): Fix ~20 call points across several unit test modules to call |
185 // this method on the audio thread, then uncomment the following: | 187 // this method on the audio thread, then uncomment the following: |
186 // DCHECK(task_runner_->BelongsToCurrentThread()); | 188 // DCHECK(task_runner_->BelongsToCurrentThread()); |
187 | 189 |
188 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || | 190 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || |
189 device_id.empty()) { | 191 device_id.empty()) { |
190 DLOG(ERROR) << "Audio parameters are invalid for device " << device_id; | 192 std::ostringstream oss; |
193 oss << "MakeAudioInputStream => invalid parameters"; | |
194 DLOG(ERROR) << oss; | |
195 NotifyAllStateChangeListeners(oss.str()); | |
DaleCurtis
2014/08/26 20:09:29
This seems wrong. Only the creating controller sho
henrika (OOO until Aug 14)
2014/08/27 13:54:38
Done.
| |
191 return NULL; | 196 return NULL; |
192 } | 197 } |
193 | 198 |
194 if (num_input_streams_ >= max_num_input_streams_) { | 199 if (num_input_streams_ >= max_num_input_streams_) { |
195 DLOG(ERROR) << "Number of opened input audio streams " | 200 std::ostringstream oss; |
196 << num_input_streams_ | 201 oss << "MakeAudioInputStream => " |
DaleCurtis
2014/08/26 20:09:29
Ditto. All this is doing is spamming all listeners
henrika (OOO until Aug 14)
2014/08/27 13:54:38
Done.
| |
197 << " exceed the max allowed number " << max_num_input_streams_; | 202 << "number of opened input audio streams " << num_input_streams_ |
203 << " exceed the max allowed number " << max_num_input_streams_; | |
204 DLOG(ERROR) << oss; | |
205 NotifyAllStateChangeListeners(oss.str()); | |
198 return NULL; | 206 return NULL; |
199 } | 207 } |
200 | 208 |
201 AudioInputStream* stream; | 209 AudioInputStream* stream; |
210 std::ostringstream oss; | |
211 oss << "MakeAudioInputStream("; | |
202 switch (params.format()) { | 212 switch (params.format()) { |
203 case AudioParameters::AUDIO_PCM_LINEAR: | 213 case AudioParameters::AUDIO_PCM_LINEAR: |
204 stream = MakeLinearInputStream(params, device_id); | 214 stream = MakeLinearInputStream(params, device_id); |
215 oss << "AUDIO_PCM_LINEAR)"; | |
205 break; | 216 break; |
206 case AudioParameters::AUDIO_PCM_LOW_LATENCY: | 217 case AudioParameters::AUDIO_PCM_LOW_LATENCY: |
207 stream = MakeLowLatencyInputStream(params, device_id); | 218 stream = MakeLowLatencyInputStream(params, device_id); |
219 oss << "AUDIO_PCM_LOW_LATENCY)"; | |
208 break; | 220 break; |
209 case AudioParameters::AUDIO_FAKE: | 221 case AudioParameters::AUDIO_FAKE: |
210 stream = FakeAudioInputStream::MakeFakeStream(this, params); | 222 stream = FakeAudioInputStream::MakeFakeStream(this, params); |
223 oss << "AUDIO_FAKE)"; | |
211 break; | 224 break; |
212 default: | 225 default: |
213 stream = NULL; | 226 stream = NULL; |
214 break; | 227 break; |
215 } | 228 } |
216 | 229 |
217 if (stream) { | 230 if (stream) { |
218 ++num_input_streams_; | 231 ++num_input_streams_; |
219 } | 232 } |
233 oss << " => num_input_streams = " << num_input_streams_; | |
234 NotifyAllStateChangeListeners(oss.str()); | |
220 | 235 |
221 return stream; | 236 return stream; |
222 } | 237 } |
223 | 238 |
224 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 239 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
225 const AudioParameters& params, | 240 const AudioParameters& params, |
226 const std::string& device_id) { | 241 const std::string& device_id) { |
227 DCHECK(task_runner_->BelongsToCurrentThread()); | 242 DCHECK(task_runner_->BelongsToCurrentThread()); |
228 | 243 |
229 // If the caller supplied an empty device id to select the default device, | 244 // If the caller supplied an empty device id to select the default device, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 // For example, pass the ownership to AudioManager so it can delete the | 323 // For example, pass the ownership to AudioManager so it can delete the |
309 // streams. | 324 // streams. |
310 --num_output_streams_; | 325 --num_output_streams_; |
311 delete stream; | 326 delete stream; |
312 } | 327 } |
313 | 328 |
314 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 329 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
315 DCHECK(stream); | 330 DCHECK(stream); |
316 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 331 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
317 --num_input_streams_; | 332 --num_input_streams_; |
333 std::string log_string = base::StringPrintf( | |
DaleCurtis
2014/08/26 20:09:29
Further, none of this is actually a state change,
henrika (OOO until Aug 14)
2014/08/27 13:54:38
Done.
| |
334 "ReleaseInputStream => num_input_streams = %d", num_input_streams_); | |
335 NotifyAllStateChangeListeners(log_string); | |
318 delete stream; | 336 delete stream; |
319 } | 337 } |
320 | 338 |
321 void AudioManagerBase::Shutdown() { | 339 void AudioManagerBase::Shutdown() { |
322 // Only true when we're sharing the UI message loop with the browser. The UI | 340 // Only true when we're sharing the UI message loop with the browser. The UI |
323 // loop is no longer running at this time and browser destruction is imminent. | 341 // loop is no longer running at this time and browser destruction is imminent. |
324 if (task_runner_->BelongsToCurrentThread()) { | 342 if (task_runner_->BelongsToCurrentThread()) { |
325 ShutdownOnAudioThread(); | 343 ShutdownOnAudioThread(); |
326 } else { | 344 } else { |
327 task_runner_->PostTask(FROM_HERE, base::Bind( | 345 task_runner_->PostTask(FROM_HERE, base::Bind( |
(...skipping 23 matching lines...) Expand all Loading... | |
351 DCHECK(task_runner_->BelongsToCurrentThread()); | 369 DCHECK(task_runner_->BelongsToCurrentThread()); |
352 output_listeners_.RemoveObserver(listener); | 370 output_listeners_.RemoveObserver(listener); |
353 } | 371 } |
354 | 372 |
355 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { | 373 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { |
356 DCHECK(task_runner_->BelongsToCurrentThread()); | 374 DCHECK(task_runner_->BelongsToCurrentThread()); |
357 DVLOG(1) << "Firing OnDeviceChange() notifications."; | 375 DVLOG(1) << "Firing OnDeviceChange() notifications."; |
358 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); | 376 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); |
359 } | 377 } |
360 | 378 |
379 void AudioManagerBase::AddStateChangeListener(StateChangeListener* listener) { | |
380 DCHECK(task_runner_->BelongsToCurrentThread()); | |
381 state_listeners_.AddObserver(listener); | |
382 } | |
383 | |
384 void AudioManagerBase::RemoveStateChangeListener( | |
385 StateChangeListener* listener) { | |
386 DCHECK(task_runner_->BelongsToCurrentThread()); | |
387 state_listeners_.RemoveObserver(listener); | |
388 } | |
389 | |
390 void AudioManagerBase::NotifyAllStateChangeListeners(const std::string& msg) { | |
391 DCHECK(task_runner_->BelongsToCurrentThread()); | |
392 std::string log_string("AMB::"); | |
393 log_string += msg; | |
394 FOR_EACH_OBSERVER( | |
395 StateChangeListener, state_listeners_, OnStateChange(log_string)); | |
396 } | |
397 | |
361 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { | 398 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { |
362 return GetPreferredOutputStreamParameters(GetDefaultOutputDeviceID(), | 399 return GetPreferredOutputStreamParameters(GetDefaultOutputDeviceID(), |
363 AudioParameters()); | 400 AudioParameters()); |
364 } | 401 } |
365 | 402 |
366 AudioParameters AudioManagerBase::GetOutputStreamParameters( | 403 AudioParameters AudioManagerBase::GetOutputStreamParameters( |
367 const std::string& device_id) { | 404 const std::string& device_id) { |
368 return GetPreferredOutputStreamParameters(device_id, | 405 return GetPreferredOutputStreamParameters(device_id, |
369 AudioParameters()); | 406 AudioParameters()); |
370 } | 407 } |
(...skipping 23 matching lines...) Expand all Loading... | |
394 | 431 |
395 return 0; | 432 return 0; |
396 } | 433 } |
397 | 434 |
398 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 435 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
399 AudioLogFactory::AudioComponent component) { | 436 AudioLogFactory::AudioComponent component) { |
400 return audio_log_factory_->CreateAudioLog(component); | 437 return audio_log_factory_->CreateAudioLog(component); |
401 } | 438 } |
402 | 439 |
403 } // namespace media | 440 } // namespace media |
OLD | NEW |