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/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "media/audio/audio_output_dispatcher.h" | 10 #include "media/audio/audio_output_dispatcher.h" |
| 11 #include "media/audio/audio_output_proxy.h" | 11 #include "media/audio/audio_output_proxy.h" |
| 12 #include "media/audio/fake_audio_input_stream.h" | |
| 13 #include "media/audio/fake_audio_output_stream.h" | |
| 12 | 14 |
| 13 static const int kStreamCloseDelaySeconds = 5; | 15 static const int kStreamCloseDelaySeconds = 5; |
| 14 | 16 |
| 15 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; | 17 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; |
| 16 const char AudioManagerBase::kDefaultDeviceId[] = "default"; | 18 const char AudioManagerBase::kDefaultDeviceId[] = "default"; |
| 17 | 19 |
| 18 AudioManagerBase::AudioManagerBase() | 20 AudioManagerBase::AudioManagerBase() |
| 19 : num_active_input_streams_(0) { | 21 : num_active_input_streams_(0), |
| 22 num_output_streams_(0) { | |
| 20 } | 23 } |
| 21 | 24 |
| 22 AudioManagerBase::~AudioManagerBase() { | 25 AudioManagerBase::~AudioManagerBase() { |
| 23 Shutdown(); | 26 Shutdown(); |
| 27 // All the output streams should have been deleted. | |
| 28 DCHECK_EQ(0, num_output_streams_); | |
| 24 } | 29 } |
| 25 | 30 |
| 26 void AudioManagerBase::Init() { | 31 void AudioManagerBase::Init() { |
| 27 base::AutoLock lock(audio_thread_lock_); | 32 base::AutoLock lock(audio_thread_lock_); |
| 28 DCHECK(!audio_thread_.get()); | 33 DCHECK(!audio_thread_.get()); |
| 29 audio_thread_.reset(new base::Thread("AudioThread")); | 34 audio_thread_.reset(new base::Thread("AudioThread")); |
| 30 CHECK(audio_thread_->Start()); | 35 CHECK(audio_thread_->Start()); |
| 31 } | 36 } |
| 32 | 37 |
| 33 string16 AudioManagerBase::GetAudioInputDeviceModel() { | 38 string16 AudioManagerBase::GetAudioInputDeviceModel() { |
| 34 return string16(); | 39 return string16(); |
| 35 } | 40 } |
| 36 | 41 |
| 37 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { | 42 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
| 38 base::AutoLock lock(audio_thread_lock_); | 43 base::AutoLock lock(audio_thread_lock_); |
| 39 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; | 44 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; |
| 40 } | 45 } |
| 41 | 46 |
| 47 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | |
| 48 const AudioParameters& params) { | |
| 49 if (!params.IsValid()) { | |
| 50 DLOG(ERROR) << "Audio parameters are invalid"; | |
| 51 return NULL; | |
| 52 } | |
| 53 | |
| 54 // Limit the number of audio streams opened. This is to prevent using | |
| 55 // excessive resources for a large number of audio streams. More | |
| 56 // importantly it prevents instability on certain systems. | |
| 57 // See bug: http://crbug.com/30242. | |
| 58 if (num_output_streams_ >= GetMaxOutputStreamsAllowed()) { | |
| 59 return NULL; | |
| 60 } | |
| 61 | |
| 62 AudioOutputStream* stream = NULL; | |
| 63 if (params.format == AudioParameters::AUDIO_MOCK) { | |
| 64 stream = FakeAudioOutputStream::MakeFakeStream(params); | |
| 65 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | |
| 66 num_output_streams_++; | |
| 67 stream = MakeLinearOutputStream(params); | |
| 68 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | |
| 69 num_output_streams_++; | |
| 70 stream = MakeLowLatencyOutputStream(params); | |
| 71 } | |
| 72 | |
| 73 return stream; | |
| 74 } | |
| 75 | |
| 76 AudioInputStream* AudioManagerBase::MakeAudioInputStream( | |
| 77 const AudioParameters& params, const std::string& device_id) { | |
| 78 if (!params.IsValid() || device_id.empty()) { | |
| 79 DLOG(ERROR) << "Audio parameters are invalid for device " << device_id; | |
| 80 return NULL; | |
| 81 } | |
| 82 | |
| 83 AudioInputStream* stream = NULL; | |
| 84 if (params.format == AudioParameters::AUDIO_MOCK) { | |
| 85 stream = FakeAudioInputStream::MakeFakeStream(params); | |
| 86 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | |
| 87 stream = MakeLinearInputStream(params, device_id); | |
| 88 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | |
| 89 stream = MakeLowLatencyInputStream(params, device_id); | |
| 90 } | |
| 91 | |
| 92 return stream; | |
| 93 } | |
| 94 | |
| 42 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 95 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
| 43 const AudioParameters& params) { | 96 const AudioParameters& params) { |
| 44 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 97 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 45 | 98 |
| 46 scoped_refptr<AudioOutputDispatcher>& dispatcher = | 99 scoped_refptr<AudioOutputDispatcher>& dispatcher = |
| 47 output_dispatchers_[params]; | 100 output_dispatchers_[params]; |
| 48 if (!dispatcher) | 101 if (!dispatcher) |
| 49 dispatcher = new AudioOutputDispatcher( | 102 dispatcher = new AudioOutputDispatcher( |
| 50 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); | 103 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); |
| 51 return new AudioOutputProxy(dispatcher); | 104 return new AudioOutputProxy(dispatcher); |
| 52 } | 105 } |
| 53 | 106 |
| 54 bool AudioManagerBase::CanShowAudioInputSettings() { | 107 bool AudioManagerBase::CanShowAudioInputSettings() { |
| 55 return false; | 108 return false; |
| 56 } | 109 } |
| 57 | 110 |
| 58 void AudioManagerBase::ShowAudioInputSettings() { | 111 void AudioManagerBase::ShowAudioInputSettings() { |
| 59 } | 112 } |
| 60 | 113 |
| 61 void AudioManagerBase::GetAudioInputDeviceNames( | 114 void AudioManagerBase::GetAudioInputDeviceNames( |
| 62 media::AudioDeviceNames* device_names) { | 115 media::AudioDeviceNames* device_names) { |
| 63 } | 116 } |
| 64 | 117 |
| 118 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { | |
| 119 DCHECK(stream); | |
|
tommi (sloooow) - chröme
2012/03/06 16:16:04
Can you add
DCHECK(GetMessageLoop()->BelongsToCurr
no longer working on chromium
2012/03/06 17:38:41
Doing this will break quite some unittests, since
| |
| 120 num_output_streams_--; | |
| 121 delete stream; | |
| 122 } | |
| 123 | |
| 124 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | |
| 125 DCHECK(stream); | |
|
tommi (sloooow) - chröme
2012/03/06 16:16:04
DCHECK(GetMessageLoop()->BelongsToCurrentThread())
no longer working on chromium
2012/03/06 17:38:41
same question.
| |
| 126 delete stream; | |
| 127 } | |
| 128 | |
| 65 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 129 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
| 66 base::AtomicRefCountInc(&num_active_input_streams_); | 130 base::AtomicRefCountInc(&num_active_input_streams_); |
| 67 } | 131 } |
| 68 | 132 |
| 69 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 133 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
| 70 DCHECK(IsRecordingInProcess()); | 134 DCHECK(IsRecordingInProcess()); |
| 71 base::AtomicRefCountDec(&num_active_input_streams_); | 135 base::AtomicRefCountDec(&num_active_input_streams_); |
| 72 } | 136 } |
| 73 | 137 |
| 74 bool AudioManagerBase::IsRecordingInProcess() { | 138 bool AudioManagerBase::IsRecordingInProcess() { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 // both physical audio stream objects that belong to the dispatcher as | 178 // both physical audio stream objects that belong to the dispatcher as |
| 115 // well as the message loop of the audio thread that will soon go away. | 179 // well as the message loop of the audio thread that will soon go away. |
| 116 // So, better crash now than later. | 180 // So, better crash now than later. |
| 117 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 181 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
| 118 dispatcher = NULL; | 182 dispatcher = NULL; |
| 119 } | 183 } |
| 120 } | 184 } |
| 121 | 185 |
| 122 output_dispatchers_.clear(); | 186 output_dispatchers_.clear(); |
| 123 } | 187 } |
| OLD | NEW |