| 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 return NULL; |
| 51 |
| 52 // Limit the number of audio streams opened. This is to prevent using |
| 53 // excessive resources for a large number of audio streams. More |
| 54 // importantly it prevents instability on certain systems. |
| 55 // See bug: http://crbug.com/30242. |
| 56 if (num_output_streams_ >= GetMaxAudioOutputStreamsAllowed()) { |
| 57 return NULL; |
| 58 } |
| 59 |
| 60 if (params.format == AudioParameters::AUDIO_MOCK) { |
| 61 return FakeAudioOutputStream::MakeFakeStream(params); |
| 62 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { |
| 63 num_output_streams_++; |
| 64 return MakeAudioLinearOutputStream(params); |
| 65 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { |
| 66 num_output_streams_++; |
| 67 return MakeAudioLowLatencyOutputStream(params); |
| 68 } |
| 69 |
| 70 return NULL; |
| 71 } |
| 72 |
| 73 AudioInputStream* AudioManagerBase::MakeAudioInputStream( |
| 74 const AudioParameters& params, const std::string& device_id) { |
| 75 if (!params.IsValid() || device_id.empty()) |
| 76 return NULL; |
| 77 |
| 78 if (params.format == AudioParameters::AUDIO_MOCK) { |
| 79 return FakeAudioInputStream::MakeFakeStream(params); |
| 80 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { |
| 81 return MakeAudioLinearInputStream(params, device_id); |
| 82 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { |
| 83 return MakeAudioLowLatencyInputStream(params, device_id); |
| 84 } |
| 85 |
| 86 return NULL; |
| 87 } |
| 88 |
| 42 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 89 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
| 43 const AudioParameters& params) { | 90 const AudioParameters& params) { |
| 44 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 91 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 45 | 92 |
| 46 scoped_refptr<AudioOutputDispatcher>& dispatcher = | 93 scoped_refptr<AudioOutputDispatcher>& dispatcher = |
| 47 output_dispatchers_[params]; | 94 output_dispatchers_[params]; |
| 48 if (!dispatcher) | 95 if (!dispatcher) |
| 49 dispatcher = new AudioOutputDispatcher( | 96 dispatcher = new AudioOutputDispatcher( |
| 50 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); | 97 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); |
| 51 return new AudioOutputProxy(dispatcher); | 98 return new AudioOutputProxy(dispatcher); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 64 | 111 |
| 65 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 112 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
| 66 base::AtomicRefCountInc(&num_active_input_streams_); | 113 base::AtomicRefCountInc(&num_active_input_streams_); |
| 67 } | 114 } |
| 68 | 115 |
| 69 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 116 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
| 70 DCHECK(IsRecordingInProcess()); | 117 DCHECK(IsRecordingInProcess()); |
| 71 base::AtomicRefCountDec(&num_active_input_streams_); | 118 base::AtomicRefCountDec(&num_active_input_streams_); |
| 72 } | 119 } |
| 73 | 120 |
| 121 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { |
| 122 DCHECK(stream); |
| 123 num_output_streams_--; |
| 124 delete stream; |
| 125 } |
| 126 |
| 127 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
| 128 DCHECK(stream); |
| 129 delete stream; |
| 130 } |
| 131 |
| 74 bool AudioManagerBase::IsRecordingInProcess() { | 132 bool AudioManagerBase::IsRecordingInProcess() { |
| 75 return !base::AtomicRefCountIsZero(&num_active_input_streams_); | 133 return !base::AtomicRefCountIsZero(&num_active_input_streams_); |
| 76 } | 134 } |
| 77 | 135 |
| 78 void AudioManagerBase::Shutdown() { | 136 void AudioManagerBase::Shutdown() { |
| 79 // To avoid running into deadlocks while we stop the thread, shut it down | 137 // To avoid running into deadlocks while we stop the thread, shut it down |
| 80 // via a local variable while not holding the audio thread lock. | 138 // via a local variable while not holding the audio thread lock. |
| 81 scoped_ptr<base::Thread> audio_thread; | 139 scoped_ptr<base::Thread> audio_thread; |
| 82 { | 140 { |
| 83 base::AutoLock lock(audio_thread_lock_); | 141 base::AutoLock lock(audio_thread_lock_); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 // both physical audio stream objects that belong to the dispatcher as | 172 // 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. | 173 // well as the message loop of the audio thread that will soon go away. |
| 116 // So, better crash now than later. | 174 // So, better crash now than later. |
| 117 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 175 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
| 118 dispatcher = NULL; | 176 dispatcher = NULL; |
| 119 } | 177 } |
| 120 } | 178 } |
| 121 | 179 |
| 122 output_dispatchers_.clear(); | 180 output_dispatchers_.clear(); |
| 123 } | 181 } |
| OLD | NEW |