Chromium Code Reviews| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 return stream; | 180 return stream; |
| 179 } | 181 } |
| 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 |
| 190 std::string log_string = base::StringPrintf( | |
| 191 "MakeAudioInputStream(device_id=%s)", device_id.c_str()); | |
|
Henrik Grunell
2014/08/22 16:14:43
Is this id something that can identify someone?
no longer working on chromium
2014/08/25 08:20:05
as I pointed out, we can't log the device_id due t
henrika (OOO until Aug 14)
2014/08/25 12:47:45
Removed.
But Shijing, we do log it at other place
| |
| 192 NotifyAllStateChangeListeners(log_string); | |
| 193 | |
| 188 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || | 194 if (!params.IsValid() || (params.channels() > kMaxInputChannels) || |
| 189 device_id.empty()) { | 195 device_id.empty()) { |
| 190 DLOG(ERROR) << "Audio parameters are invalid for device " << device_id; | 196 DLOG(ERROR) << "Audio parameters are invalid for device " << device_id; |
| 191 return NULL; | 197 return NULL; |
| 192 } | 198 } |
| 193 | 199 |
| 194 if (num_input_streams_ >= max_num_input_streams_) { | 200 if (num_input_streams_ >= max_num_input_streams_) { |
| 195 DLOG(ERROR) << "Number of opened input audio streams " | 201 DLOG(ERROR) << "Number of opened input audio streams " |
| 196 << num_input_streams_ | 202 << num_input_streams_ |
| 197 << " exceed the max allowed number " << max_num_input_streams_; | 203 << " exceed the max allowed number " << max_num_input_streams_; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 209 case AudioParameters::AUDIO_FAKE: | 215 case AudioParameters::AUDIO_FAKE: |
| 210 stream = FakeAudioInputStream::MakeFakeStream(this, params); | 216 stream = FakeAudioInputStream::MakeFakeStream(this, params); |
| 211 break; | 217 break; |
| 212 default: | 218 default: |
| 213 stream = NULL; | 219 stream = NULL; |
| 214 break; | 220 break; |
| 215 } | 221 } |
| 216 | 222 |
| 217 if (stream) { | 223 if (stream) { |
| 218 ++num_input_streams_; | 224 ++num_input_streams_; |
| 225 log_string = base::StringPrintf( | |
| 226 "MakeAudioInputStream => num_input_streams = %d", num_input_streams_); | |
|
Henrik Grunell
2014/08/22 16:14:43
Do we need the above if we have this? What about t
henrika (OOO until Aug 14)
2014/08/25 12:47:45
Thanks. Improved.
| |
| 227 NotifyAllStateChangeListeners(log_string); | |
| 219 } | 228 } |
| 220 | 229 |
| 221 return stream; | 230 return stream; |
| 222 } | 231 } |
| 223 | 232 |
| 224 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 233 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
| 225 const AudioParameters& params, | 234 const AudioParameters& params, |
| 226 const std::string& device_id) { | 235 const std::string& device_id) { |
| 227 DCHECK(task_runner_->BelongsToCurrentThread()); | 236 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 228 | 237 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 // For example, pass the ownership to AudioManager so it can delete the | 317 // For example, pass the ownership to AudioManager so it can delete the |
| 309 // streams. | 318 // streams. |
| 310 --num_output_streams_; | 319 --num_output_streams_; |
| 311 delete stream; | 320 delete stream; |
| 312 } | 321 } |
| 313 | 322 |
| 314 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 323 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
| 315 DCHECK(stream); | 324 DCHECK(stream); |
| 316 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 325 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
| 317 --num_input_streams_; | 326 --num_input_streams_; |
| 327 std::string log_string = base::StringPrintf( | |
| 328 "ReleaseInputStream => num_input_streams = %d", | |
| 329 num_input_streams_); | |
| 330 NotifyAllStateChangeListeners(log_string); | |
| 318 delete stream; | 331 delete stream; |
| 319 } | 332 } |
| 320 | 333 |
| 321 void AudioManagerBase::Shutdown() { | 334 void AudioManagerBase::Shutdown() { |
| 322 // Only true when we're sharing the UI message loop with the browser. The UI | 335 // 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. | 336 // loop is no longer running at this time and browser destruction is imminent. |
| 324 if (task_runner_->BelongsToCurrentThread()) { | 337 if (task_runner_->BelongsToCurrentThread()) { |
| 325 ShutdownOnAudioThread(); | 338 ShutdownOnAudioThread(); |
| 326 } else { | 339 } else { |
| 327 task_runner_->PostTask(FROM_HERE, base::Bind( | 340 task_runner_->PostTask(FROM_HERE, base::Bind( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 351 DCHECK(task_runner_->BelongsToCurrentThread()); | 364 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 352 output_listeners_.RemoveObserver(listener); | 365 output_listeners_.RemoveObserver(listener); |
| 353 } | 366 } |
| 354 | 367 |
| 355 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { | 368 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { |
| 356 DCHECK(task_runner_->BelongsToCurrentThread()); | 369 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 357 DVLOG(1) << "Firing OnDeviceChange() notifications."; | 370 DVLOG(1) << "Firing OnDeviceChange() notifications."; |
| 358 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); | 371 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); |
| 359 } | 372 } |
| 360 | 373 |
| 374 void AudioManagerBase::AddStateChangeListener(StateChangeListener* listener) { | |
| 375 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 376 state_listeners_.AddObserver(listener); | |
| 377 } | |
| 378 | |
| 379 void AudioManagerBase::RemoveStateChangeListener( | |
| 380 StateChangeListener* listener) { | |
| 381 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 382 state_listeners_.RemoveObserver(listener); | |
| 383 } | |
| 384 | |
| 385 void AudioManagerBase::NotifyAllStateChangeListeners(const std::string& msg) { | |
| 386 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 387 std::string log_string("AMB::"); | |
| 388 log_string += msg; | |
| 389 FOR_EACH_OBSERVER( | |
| 390 StateChangeListener, state_listeners_, OnStateChange(log_string)); | |
| 391 } | |
| 392 | |
| 361 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { | 393 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { |
| 362 return GetPreferredOutputStreamParameters(GetDefaultOutputDeviceID(), | 394 return GetPreferredOutputStreamParameters(GetDefaultOutputDeviceID(), |
| 363 AudioParameters()); | 395 AudioParameters()); |
| 364 } | 396 } |
| 365 | 397 |
| 366 AudioParameters AudioManagerBase::GetOutputStreamParameters( | 398 AudioParameters AudioManagerBase::GetOutputStreamParameters( |
| 367 const std::string& device_id) { | 399 const std::string& device_id) { |
| 368 return GetPreferredOutputStreamParameters(device_id, | 400 return GetPreferredOutputStreamParameters(device_id, |
| 369 AudioParameters()); | 401 AudioParameters()); |
| 370 } | 402 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 394 | 426 |
| 395 return 0; | 427 return 0; |
| 396 } | 428 } |
| 397 | 429 |
| 398 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( | 430 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( |
| 399 AudioLogFactory::AudioComponent component) { | 431 AudioLogFactory::AudioComponent component) { |
| 400 return audio_log_factory_->CreateAudioLog(component); | 432 return audio_log_factory_->CreateAudioLog(component); |
| 401 } | 433 } |
| 402 | 434 |
| 403 } // namespace media | 435 } // namespace media |
| OLD | NEW |