Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: media/audio/linux/audio_manager_linux.cc

Issue 7060011: Adding GetAudioInputDeviceNames to AudioManager, this function is supposed to do device enumerati... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/linux/audio_manager_linux.h" 5 #include "media/audio/linux/audio_manager_linux.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/environment.h" 8 #include "base/environment.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/nix/xdg_util.h" 10 #include "base/nix/xdg_util.h"
11 #include "base/process_util.h" 11 #include "base/process_util.h"
12 #include "base/stringprintf.h"
12 #include "media/audio/audio_output_dispatcher.h" 13 #include "media/audio/audio_output_dispatcher.h"
13 #include "media/audio/fake_audio_input_stream.h" 14 #include "media/audio/fake_audio_input_stream.h"
14 #include "media/audio/fake_audio_output_stream.h" 15 #include "media/audio/fake_audio_output_stream.h"
15 #include "media/audio/linux/alsa_input.h" 16 #include "media/audio/linux/alsa_input.h"
16 #include "media/audio/linux/alsa_output.h" 17 #include "media/audio/linux/alsa_output.h"
17 #include "media/audio/linux/alsa_wrapper.h" 18 #include "media/audio/linux/alsa_wrapper.h"
18 #include "media/base/limits.h" 19 #include "media/base/limits.h"
19 #include "media/base/media_switches.h" 20 #include "media/base/media_switches.h"
20 21
21 // Maximum number of output streams that can be open simultaneously. 22 // Maximum number of output streams that can be open simultaneously.
22 static const size_t kMaxOutputStreams = 50; 23 static const size_t kMaxOutputStreams = 50;
23 24
24 static const int kMaxInputChannels = 2; 25 static const int kMaxInputChannels = 2;
25 26
26 // Implementation of AudioManager. 27 // Implementation of AudioManager.
27 bool AudioManagerLinux::HasAudioOutputDevices() { 28 bool AudioManagerLinux::HasAudioOutputDevices() {
28 // TODO(ajwong): Make this actually query audio devices. 29 // TODO(ajwong): Make this actually query audio devices.
29 return true; 30 return true;
30 } 31 }
31 32
32 bool AudioManagerLinux::HasAudioInputDevices() { 33 bool AudioManagerLinux::HasAudioInputDevices() {
33 // TODO(satish): Make this actually query audio devices. 34 if (!initialized()) {
34 return true; 35 return false;
36 }
37
38 // Constants specified by the ALSA API for device hints.
39 static const int kGetAllDevices = -1;
40 static const char kPcmInterfaceName[] = "pcm";
41 bool has_device = false;
42 void **hints = NULL;
scherkus (not reviewing) 2011/05/26 06:13:12 pointers go with typename void ** --> void**
xians 2011/05/26 13:54:57 Done.
43
44 // Use the same approach to find the devices as in
45 // AlsaPcmOutputStream::FindDeviceForChannels
46 // Get Alsa device hints.
47 int error = wrapper_->DeviceNameHint(kGetAllDevices,
48 kPcmInterfaceName,
49 &hints);
50 if (error == 0) {
51 has_device = HasAnyValidAudioInputDevice(hints);
52 } else {
53 LOG(ERROR) << "Unable to get device hints: " << wrapper_->StrError(error);
54 }
55
56 // Destory the hint now that we're done with it.
scherkus (not reviewing) 2011/05/26 06:13:12 Destory -> Destroy
xians 2011/05/26 13:54:57 Done.
57 wrapper_->DeviceNameFreeHint(hints);
58 return has_device;
35 } 59 }
36 60
37 AudioOutputStream* AudioManagerLinux::MakeAudioOutputStream( 61 AudioOutputStream* AudioManagerLinux::MakeAudioOutputStream(
38 AudioParameters params) { 62 AudioParameters params) {
39 // Early return for testing hook. Do this before checking for 63 // Early return for testing hook. Do this before checking for
40 // |initialized_|. 64 // |initialized_|.
41 if (params.format == AudioParameters::AUDIO_MOCK) { 65 if (params.format == AudioParameters::AUDIO_MOCK) {
42 return FakeAudioOutputStream::MakeFakeStream(params); 66 return FakeAudioOutputStream::MakeFakeStream(params);
43 } 67 }
44 68
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 AlsaPcmInputStream* stream = new AlsaPcmInputStream( 113 AlsaPcmInputStream* stream = new AlsaPcmInputStream(
90 device_name, params, wrapper_.get()); 114 device_name, params, wrapper_.get());
91 115
92 return stream; 116 return stream;
93 } 117 }
94 118
95 AudioManagerLinux::AudioManagerLinux() { 119 AudioManagerLinux::AudioManagerLinux() {
96 } 120 }
97 121
98 AudioManagerLinux::~AudioManagerLinux() { 122 AudioManagerLinux::~AudioManagerLinux() {
99 // Make sure we stop the thread first. If we let the default destructor to 123 // Make sure we stop the thread first. If we allow the default destructor to
100 // destruct the members, we may destroy audio streams before stopping the 124 // destroy the members, we may destroy audio streams before stopping the
101 // thread, resulting an unexpected behavior. 125 // thread, resulting an unexpected behavior.
102 // This way we make sure activities of the audio streams are all stopped 126 // This way we make sure activities of the audio streams are all stopped
103 // before we destroy them. 127 // before we destroy them.
104 audio_thread_.Stop(); 128 audio_thread_.Stop();
105 129
106 // Free output dispatchers, closing all remaining open streams. 130 // Free output dispatchers, closing all remaining open streams.
107 output_dispatchers_.clear(); 131 output_dispatchers_.clear();
108 132
109 active_streams_.clear(); 133 active_streams_.clear();
110 } 134 }
(...skipping 29 matching lines...) Expand all
140 164
141 void AudioManagerLinux::ShowAudioInputSettings() { 165 void AudioManagerLinux::ShowAudioInputSettings() {
142 scoped_ptr<base::Environment> env(base::Environment::Create()); 166 scoped_ptr<base::Environment> env(base::Environment::Create());
143 base::nix::DesktopEnvironment desktop = base::nix::GetDesktopEnvironment( 167 base::nix::DesktopEnvironment desktop = base::nix::GetDesktopEnvironment(
144 env.get()); 168 env.get());
145 std::string command((desktop == base::nix::DESKTOP_ENVIRONMENT_GNOME) ? 169 std::string command((desktop == base::nix::DESKTOP_ENVIRONMENT_GNOME) ?
146 "gnome-volume-control" : "kmix"); 170 "gnome-volume-control" : "kmix");
147 base::LaunchApp(CommandLine(FilePath(command)), false, false, NULL); 171 base::LaunchApp(CommandLine(FilePath(command)), false, false, NULL);
148 } 172 }
149 173
174 void AudioManagerLinux::GetAudioInputDeviceNames(
175 media::AudioDeviceNames* device_names) {
176 // TODO(xians): query a full list of valid devices.
177 if (HasAudioInputDevices()) {
178 // Add the default device to the list.
179 // We use (device_name)_(index) to make up the unique_ids to identify the
180 // devices. For default device, the index is 0, so its unique_id is
181 // Default_0.
182 media::AudioDeviceName name;
183 name.device_name = AudioManagerBase::kDefaultDeviceName;
184 name.unique_id = StringPrintf("%s_0", AudioManagerBase::kDefaultDeviceName);
185 device_names->push_back(name);
186 }
187 }
188
189 bool AudioManagerLinux::HasAnyValidAudioInputDevice(void** hints) {
190 static const char kIoHintName[] = "IOID";
191 static const char kNameHintName[] = "NAME";
192
193 // Since "default", "pulse" and "dmix" devices are virtual devices mapped to
194 // real devices, we remove them from the list to avoiding duplicate counting.
195 // In addition, note that we support no more than 2 channels for recording,
196 // hence surround devices are not stored in the list. Finally, output and
197 // null devices are not considered as valid devices for recording.
198 static const char kOutputDevice[] = "Output";
199 static const char KNotWantedDefaultDevice[] = "default";
scherkus (not reviewing) 2011/05/26 06:13:12 nit: K -> k
xians 2011/05/26 13:54:57 Done.
200 static const char kNotWantedNullDevice[] = "null";
201 static const char kNotWantedPulseDevice[] = "pulse";
202 static const char kNotWantedDmixDevice[] = "dmix";
203 static const char kNotWantedSurroundDevice[] = "surround";
scherkus (not reviewing) 2011/05/26 06:13:12 what about adding these to an array and iterating
204
205 for (void** hint_iter = hints; *hint_iter != NULL; hint_iter++) {
206 // Only examine devices that are input capable.. Valid values are
207 // "Input", "Output", and NULL which means both input and output.
208 scoped_ptr_malloc<char> io(wrapper_->DeviceNameGetHint(*hint_iter,
scherkus (not reviewing) 2011/05/26 06:13:12 sanity check: DeviceNameGetHint mallocs() and we'r
xians 2011/05/26 13:54:57 yes, there is a "if (io != NULL.." just right bel
209 kIoHintName));
210 // Wrong device type, skip it.
211 if (io != NULL &&
212 strncmp(kOutputDevice, io.get(), strlen(kOutputDevice)) == 0)
213 continue;
214
215 scoped_ptr_malloc<char> hint_device_name(
216 wrapper_->DeviceNameGetHint(*hint_iter, kNameHintName));
217 // Now check if if it is a valid device.
218 if (hint_device_name != NULL &&
219 strncmp(KNotWantedDefaultDevice,
220 hint_device_name.get(),
221 strlen(KNotWantedDefaultDevice)) != 0 &&
222 strncmp(kNotWantedNullDevice,
223 hint_device_name.get(),
224 strlen(kNotWantedNullDevice)) != 0 &&
225 strncmp(kNotWantedPulseDevice,
226 hint_device_name.get(),
227 strlen(kNotWantedPulseDevice)) != 0 &&
228 strncmp(kNotWantedDmixDevice,
229 hint_device_name.get(),
230 strlen(kNotWantedDmixDevice)) != 0 &&
231 strncmp(kNotWantedSurroundDevice,
232 hint_device_name.get(),
233 strlen(kNotWantedSurroundDevice)) != 0) {
234 return true;
235 }
236 }
237
238 // Did not find a valid device
scherkus (not reviewing) 2011/05/26 06:13:12 comments end w/ period
xians 2011/05/26 13:54:57 Done.
239 return false;
240 }
241
150 // static 242 // static
151 AudioManager* AudioManager::CreateAudioManager() { 243 AudioManager* AudioManager::CreateAudioManager() {
152 return new AudioManagerLinux(); 244 return new AudioManagerLinux();
153 } 245 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698