| 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 |
| 17 // Default maximum number of output streams that can be open simultaneously |
| 18 // for all platforms. |
| 19 static const int kDefaultMaxOutputStreams = 15; |
| 20 |
| 21 static const int kMaxInputChannels = 2; |
| 22 |
| 15 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; | 23 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; |
| 16 const char AudioManagerBase::kDefaultDeviceId[] = "default"; | 24 const char AudioManagerBase::kDefaultDeviceId[] = "default"; |
| 17 | 25 |
| 18 AudioManagerBase::AudioManagerBase() | 26 AudioManagerBase::AudioManagerBase() |
| 19 : num_active_input_streams_(0) { | 27 : num_active_input_streams_(0), |
| 28 max_num_output_streams_(kDefaultMaxOutputStreams), |
| 29 num_output_streams_(0) { |
| 20 } | 30 } |
| 21 | 31 |
| 22 AudioManagerBase::~AudioManagerBase() { | 32 AudioManagerBase::~AudioManagerBase() { |
| 23 Shutdown(); | 33 Shutdown(); |
| 34 // All the output streams should have been deleted. |
| 35 DCHECK_EQ(0, num_output_streams_); |
| 24 } | 36 } |
| 25 | 37 |
| 26 void AudioManagerBase::Init() { | 38 void AudioManagerBase::Init() { |
| 27 base::AutoLock lock(audio_thread_lock_); | 39 base::AutoLock lock(audio_thread_lock_); |
| 28 DCHECK(!audio_thread_.get()); | 40 DCHECK(!audio_thread_.get()); |
| 29 audio_thread_.reset(new base::Thread("AudioThread")); | 41 audio_thread_.reset(new base::Thread("AudioThread")); |
| 30 CHECK(audio_thread_->Start()); | 42 CHECK(audio_thread_->Start()); |
| 31 } | 43 } |
| 32 | 44 |
| 33 string16 AudioManagerBase::GetAudioInputDeviceModel() { | 45 string16 AudioManagerBase::GetAudioInputDeviceModel() { |
| 34 return string16(); | 46 return string16(); |
| 35 } | 47 } |
| 36 | 48 |
| 37 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { | 49 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
| 38 base::AutoLock lock(audio_thread_lock_); | 50 base::AutoLock lock(audio_thread_lock_); |
| 39 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; | 51 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; |
| 40 } | 52 } |
| 41 | 53 |
| 54 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
| 55 const AudioParameters& params) { |
| 56 if (!params.IsValid()) { |
| 57 DLOG(ERROR) << "Audio parameters are invalid"; |
| 58 return NULL; |
| 59 } |
| 60 |
| 61 // Limit the number of audio streams opened. This is to prevent using |
| 62 // excessive resources for a large number of audio streams. More |
| 63 // importantly it prevents instability on certain systems. |
| 64 // See bug: http://crbug.com/30242. |
| 65 if (num_output_streams_ >= max_num_output_streams_) { |
| 66 DLOG(ERROR) << "Number of opened audio streams " << num_output_streams_ |
| 67 << " exceed the max allowed number " << max_num_output_streams_; |
| 68 return NULL; |
| 69 } |
| 70 |
| 71 AudioOutputStream* stream = NULL; |
| 72 if (params.format == AudioParameters::AUDIO_MOCK) { |
| 73 stream = FakeAudioOutputStream::MakeFakeStream(params); |
| 74 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { |
| 75 num_output_streams_++; |
| 76 stream = MakeLinearOutputStream(params); |
| 77 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { |
| 78 num_output_streams_++; |
| 79 stream = MakeLowLatencyOutputStream(params); |
| 80 } |
| 81 |
| 82 return stream; |
| 83 } |
| 84 |
| 85 AudioInputStream* AudioManagerBase::MakeAudioInputStream( |
| 86 const AudioParameters& params, const std::string& device_id) { |
| 87 if (!params.IsValid() || (params.channels > kMaxInputChannels) || |
| 88 device_id.empty()) { |
| 89 DLOG(ERROR) << "Audio parameters are invalid for device " << device_id; |
| 90 return NULL; |
| 91 } |
| 92 |
| 93 AudioInputStream* stream = NULL; |
| 94 if (params.format == AudioParameters::AUDIO_MOCK) { |
| 95 stream = FakeAudioInputStream::MakeFakeStream(params); |
| 96 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { |
| 97 stream = MakeLinearInputStream(params, device_id); |
| 98 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { |
| 99 stream = MakeLowLatencyInputStream(params, device_id); |
| 100 } |
| 101 |
| 102 return stream; |
| 103 } |
| 104 |
| 42 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 105 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
| 43 const AudioParameters& params) { | 106 const AudioParameters& params) { |
| 44 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 107 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
| 45 | 108 |
| 46 scoped_refptr<AudioOutputDispatcher>& dispatcher = | 109 scoped_refptr<AudioOutputDispatcher>& dispatcher = |
| 47 output_dispatchers_[params]; | 110 output_dispatchers_[params]; |
| 48 if (!dispatcher) | 111 if (!dispatcher) |
| 49 dispatcher = new AudioOutputDispatcher( | 112 dispatcher = new AudioOutputDispatcher( |
| 50 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); | 113 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); |
| 51 return new AudioOutputProxy(dispatcher); | 114 return new AudioOutputProxy(dispatcher); |
| 52 } | 115 } |
| 53 | 116 |
| 54 bool AudioManagerBase::CanShowAudioInputSettings() { | 117 bool AudioManagerBase::CanShowAudioInputSettings() { |
| 55 return false; | 118 return false; |
| 56 } | 119 } |
| 57 | 120 |
| 58 void AudioManagerBase::ShowAudioInputSettings() { | 121 void AudioManagerBase::ShowAudioInputSettings() { |
| 59 } | 122 } |
| 60 | 123 |
| 61 void AudioManagerBase::GetAudioInputDeviceNames( | 124 void AudioManagerBase::GetAudioInputDeviceNames( |
| 62 media::AudioDeviceNames* device_names) { | 125 media::AudioDeviceNames* device_names) { |
| 63 } | 126 } |
| 64 | 127 |
| 128 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { |
| 129 DCHECK(stream); |
| 130 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream. |
| 131 // For example, pass the ownership to AudioManager so it can delete the |
| 132 // streams. |
| 133 num_output_streams_--; |
| 134 delete stream; |
| 135 } |
| 136 |
| 137 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
| 138 DCHECK(stream); |
| 139 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
| 140 delete stream; |
| 141 } |
| 142 |
| 65 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 143 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
| 66 base::AtomicRefCountInc(&num_active_input_streams_); | 144 base::AtomicRefCountInc(&num_active_input_streams_); |
| 67 } | 145 } |
| 68 | 146 |
| 69 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 147 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
| 70 DCHECK(IsRecordingInProcess()); | 148 DCHECK(IsRecordingInProcess()); |
| 71 base::AtomicRefCountDec(&num_active_input_streams_); | 149 base::AtomicRefCountDec(&num_active_input_streams_); |
| 72 } | 150 } |
| 73 | 151 |
| 74 bool AudioManagerBase::IsRecordingInProcess() { | 152 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 | 192 // 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. | 193 // well as the message loop of the audio thread that will soon go away. |
| 116 // So, better crash now than later. | 194 // So, better crash now than later. |
| 117 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 195 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
| 118 dispatcher = NULL; | 196 dispatcher = NULL; |
| 119 } | 197 } |
| 120 } | 198 } |
| 121 | 199 |
| 122 output_dispatchers_.clear(); | 200 output_dispatchers_.clear(); |
| 123 } | 201 } |
| OLD | NEW |