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 max_num_output_streams_(0), | |
scherkus (not reviewing)
2012/03/06 22:31:51
so if implementors fail to set something all audio
tommi (sloooow) - chröme
2012/03/07 07:44:55
ugh, nope, that's not what I asked for. Shijing,
no longer working on chromium
2012/03/07 09:57:08
Thanks, Done.
no longer working on chromium
2012/03/07 09:57:08
Sorry, I missed the comments.
| |
23 num_output_streams_(0) { | |
20 } | 24 } |
21 | 25 |
22 AudioManagerBase::~AudioManagerBase() { | 26 AudioManagerBase::~AudioManagerBase() { |
23 Shutdown(); | 27 Shutdown(); |
28 // All the output streams should have been deleted. | |
29 DCHECK_EQ(0, num_output_streams_); | |
24 } | 30 } |
25 | 31 |
26 void AudioManagerBase::Init() { | 32 void AudioManagerBase::Init() { |
27 base::AutoLock lock(audio_thread_lock_); | 33 base::AutoLock lock(audio_thread_lock_); |
28 DCHECK(!audio_thread_.get()); | 34 DCHECK(!audio_thread_.get()); |
29 audio_thread_.reset(new base::Thread("AudioThread")); | 35 audio_thread_.reset(new base::Thread("AudioThread")); |
30 CHECK(audio_thread_->Start()); | 36 CHECK(audio_thread_->Start()); |
31 } | 37 } |
32 | 38 |
33 string16 AudioManagerBase::GetAudioInputDeviceModel() { | 39 string16 AudioManagerBase::GetAudioInputDeviceModel() { |
34 return string16(); | 40 return string16(); |
35 } | 41 } |
36 | 42 |
37 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { | 43 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
38 base::AutoLock lock(audio_thread_lock_); | 44 base::AutoLock lock(audio_thread_lock_); |
39 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; | 45 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; |
40 } | 46 } |
41 | 47 |
48 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | |
49 const AudioParameters& params) { | |
50 if (!params.IsValid()) { | |
51 DLOG(ERROR) << "Audio parameters are invalid"; | |
52 return NULL; | |
53 } | |
54 | |
55 // Limit the number of audio streams opened. This is to prevent using | |
56 // excessive resources for a large number of audio streams. More | |
57 // importantly it prevents instability on certain systems. | |
58 // See bug: http://crbug.com/30242. | |
59 if (num_output_streams_ >= max_num_output_streams_) { | |
60 return NULL; | |
scherkus (not reviewing)
2012/03/06 22:31:51
should we LOG(WARNING) here? otherwise the result
tommi (sloooow) - chröme
2012/03/07 07:44:55
+1 even LOG(ERROR)
no longer working on chromium
2012/03/07 09:57:08
Good point. Done
no longer working on chromium
2012/03/07 09:57:08
Done.
| |
61 } | |
62 | |
63 AudioOutputStream* stream = NULL; | |
64 if (params.format == AudioParameters::AUDIO_MOCK) { | |
65 stream = FakeAudioOutputStream::MakeFakeStream(params); | |
66 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | |
67 num_output_streams_++; | |
68 stream = MakeLinearOutputStream(params); | |
69 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | |
70 num_output_streams_++; | |
71 stream = MakeLowLatencyOutputStream(params); | |
72 } | |
73 | |
74 return stream; | |
75 } | |
76 | |
77 AudioInputStream* AudioManagerBase::MakeAudioInputStream( | |
78 const AudioParameters& params, const std::string& device_id) { | |
79 if (!params.IsValid() || device_id.empty()) { | |
80 DLOG(ERROR) << "Audio parameters are invalid for device " << device_id; | |
81 return NULL; | |
82 } | |
83 | |
84 AudioInputStream* stream = NULL; | |
85 if (params.format == AudioParameters::AUDIO_MOCK) { | |
86 stream = FakeAudioInputStream::MakeFakeStream(params); | |
87 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | |
88 stream = MakeLinearInputStream(params, device_id); | |
89 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | |
90 stream = MakeLowLatencyInputStream(params, device_id); | |
91 } | |
92 | |
93 return stream; | |
94 } | |
95 | |
42 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 96 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
43 const AudioParameters& params) { | 97 const AudioParameters& params) { |
44 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); | 98 DCHECK(GetMessageLoop()->BelongsToCurrentThread()); |
45 | 99 |
46 scoped_refptr<AudioOutputDispatcher>& dispatcher = | 100 scoped_refptr<AudioOutputDispatcher>& dispatcher = |
47 output_dispatchers_[params]; | 101 output_dispatchers_[params]; |
48 if (!dispatcher) | 102 if (!dispatcher) |
49 dispatcher = new AudioOutputDispatcher( | 103 dispatcher = new AudioOutputDispatcher( |
50 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); | 104 this, params, base::TimeDelta::FromSeconds(kStreamCloseDelaySeconds)); |
51 return new AudioOutputProxy(dispatcher); | 105 return new AudioOutputProxy(dispatcher); |
52 } | 106 } |
53 | 107 |
54 bool AudioManagerBase::CanShowAudioInputSettings() { | 108 bool AudioManagerBase::CanShowAudioInputSettings() { |
55 return false; | 109 return false; |
56 } | 110 } |
57 | 111 |
58 void AudioManagerBase::ShowAudioInputSettings() { | 112 void AudioManagerBase::ShowAudioInputSettings() { |
59 } | 113 } |
60 | 114 |
61 void AudioManagerBase::GetAudioInputDeviceNames( | 115 void AudioManagerBase::GetAudioInputDeviceNames( |
62 media::AudioDeviceNames* device_names) { | 116 media::AudioDeviceNames* device_names) { |
63 } | 117 } |
64 | 118 |
119 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { | |
scherkus (not reviewing)
2012/03/06 22:31:51
I'd really like us to see this re-done at some poi
| |
120 DCHECK(stream); | |
121 num_output_streams_--; | |
122 delete stream; | |
123 } | |
124 | |
125 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | |
126 DCHECK(stream); | |
127 delete stream; | |
128 } | |
129 | |
65 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 130 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
66 base::AtomicRefCountInc(&num_active_input_streams_); | 131 base::AtomicRefCountInc(&num_active_input_streams_); |
67 } | 132 } |
68 | 133 |
69 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 134 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
70 DCHECK(IsRecordingInProcess()); | 135 DCHECK(IsRecordingInProcess()); |
71 base::AtomicRefCountDec(&num_active_input_streams_); | 136 base::AtomicRefCountDec(&num_active_input_streams_); |
72 } | 137 } |
73 | 138 |
74 bool AudioManagerBase::IsRecordingInProcess() { | 139 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 | 179 // 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. | 180 // well as the message loop of the audio thread that will soon go away. |
116 // So, better crash now than later. | 181 // So, better crash now than later. |
117 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 182 CHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
118 dispatcher = NULL; | 183 dispatcher = NULL; |
119 } | 184 } |
120 } | 185 } |
121 | 186 |
122 output_dispatchers_.clear(); | 187 output_dispatchers_.clear(); |
123 } | 188 } |
OLD | NEW |