Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 | 23 |
| 24 static const int kMaxInputChannels = 2; | 24 static const int kMaxInputChannels = 2; |
| 25 | 25 |
| 26 // Implementation of AudioManager. | 26 // Implementation of AudioManager. |
| 27 bool AudioManagerLinux::HasAudioOutputDevices() { | 27 bool AudioManagerLinux::HasAudioOutputDevices() { |
| 28 // TODO(ajwong): Make this actually query audio devices. | 28 // TODO(ajwong): Make this actually query audio devices. |
| 29 return true; | 29 return true; |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool AudioManagerLinux::HasAudioInputDevices() { | 32 bool AudioManagerLinux::HasAudioInputDevices() { |
| 33 // TODO(satish): Make this actually query audio devices. | 33 if (!initialized()) { |
| 34 return true; | 34 return false; |
| 35 } | |
| 36 | |
| 37 // Constants specified by the ALSA API for device hints. | |
| 38 static const int kGetAllDevices = -1; | |
| 39 static const char kPcmInterfaceName[] = "pcm"; | |
| 40 bool has_device = false; | |
| 41 void** hints = NULL; | |
| 42 | |
| 43 // Use the same approach to find the devices as in | |
| 44 // AlsaPcmOutputStream::FindDeviceForChannels | |
| 45 // Get Alsa device hints. | |
| 46 int error = wrapper_->DeviceNameHint(kGetAllDevices, | |
| 47 kPcmInterfaceName, | |
| 48 &hints); | |
| 49 if (error == 0) { | |
| 50 has_device = HasAnyValidAudioInputDevice(hints); | |
| 51 } else { | |
| 52 LOG(ERROR) << "Unable to get device hints: " << wrapper_->StrError(error); | |
| 53 } | |
| 54 | |
| 55 // Destroy the hint now that we're done with it. | |
| 56 wrapper_->DeviceNameFreeHint(hints); | |
| 57 return has_device; | |
| 35 } | 58 } |
| 36 | 59 |
| 37 AudioOutputStream* AudioManagerLinux::MakeAudioOutputStream( | 60 AudioOutputStream* AudioManagerLinux::MakeAudioOutputStream( |
| 38 AudioParameters params) { | 61 AudioParameters params) { |
| 39 // Early return for testing hook. Do this before checking for | 62 // Early return for testing hook. Do this before checking for |
| 40 // |initialized_|. | 63 // |initialized_|. |
| 41 if (params.format == AudioParameters::AUDIO_MOCK) { | 64 if (params.format == AudioParameters::AUDIO_MOCK) { |
| 42 return FakeAudioOutputStream::MakeFakeStream(params); | 65 return FakeAudioOutputStream::MakeFakeStream(params); |
| 43 } | 66 } |
| 44 | 67 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 AlsaPcmInputStream* stream = new AlsaPcmInputStream( | 112 AlsaPcmInputStream* stream = new AlsaPcmInputStream( |
| 90 device_name, params, wrapper_.get()); | 113 device_name, params, wrapper_.get()); |
| 91 | 114 |
| 92 return stream; | 115 return stream; |
| 93 } | 116 } |
| 94 | 117 |
| 95 AudioManagerLinux::AudioManagerLinux() { | 118 AudioManagerLinux::AudioManagerLinux() { |
| 96 } | 119 } |
| 97 | 120 |
| 98 AudioManagerLinux::~AudioManagerLinux() { | 121 AudioManagerLinux::~AudioManagerLinux() { |
| 99 // Make sure we stop the thread first. If we let the default destructor to | 122 // 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 | 123 // destroy the members, we may destroy audio streams before stopping the |
| 101 // thread, resulting an unexpected behavior. | 124 // thread, resulting an unexpected behavior. |
| 102 // This way we make sure activities of the audio streams are all stopped | 125 // This way we make sure activities of the audio streams are all stopped |
| 103 // before we destroy them. | 126 // before we destroy them. |
| 104 audio_thread_.Stop(); | 127 audio_thread_.Stop(); |
| 105 | 128 |
| 106 // Free output dispatchers, closing all remaining open streams. | 129 // Free output dispatchers, closing all remaining open streams. |
| 107 output_dispatchers_.clear(); | 130 output_dispatchers_.clear(); |
| 108 | 131 |
| 109 active_streams_.clear(); | 132 active_streams_.clear(); |
| 110 } | 133 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 140 | 163 |
| 141 void AudioManagerLinux::ShowAudioInputSettings() { | 164 void AudioManagerLinux::ShowAudioInputSettings() { |
| 142 scoped_ptr<base::Environment> env(base::Environment::Create()); | 165 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 143 base::nix::DesktopEnvironment desktop = base::nix::GetDesktopEnvironment( | 166 base::nix::DesktopEnvironment desktop = base::nix::GetDesktopEnvironment( |
| 144 env.get()); | 167 env.get()); |
| 145 std::string command((desktop == base::nix::DESKTOP_ENVIRONMENT_GNOME) ? | 168 std::string command((desktop == base::nix::DESKTOP_ENVIRONMENT_GNOME) ? |
| 146 "gnome-volume-control" : "kmix"); | 169 "gnome-volume-control" : "kmix"); |
| 147 base::LaunchApp(CommandLine(FilePath(command)), false, false, NULL); | 170 base::LaunchApp(CommandLine(FilePath(command)), false, false, NULL); |
| 148 } | 171 } |
| 149 | 172 |
| 173 void AudioManagerLinux::GetAudioInputDeviceNames( | |
| 174 media::AudioDeviceNames* device_names) { | |
| 175 // TODO(xians): query a full list of valid devices. | |
| 176 if (HasAudioInputDevices()) { | |
| 177 // Add the default device to the list. | |
| 178 // We use index 0 to make up the unique_id to identify the | |
| 179 // default devices. | |
| 180 media::AudioDeviceName name; | |
| 181 name.device_name = AudioManagerBase::kDefaultDeviceName; | |
| 182 name.unique_id = std::string("0"); | |
| 183 device_names->push_back(name); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 bool AudioManagerLinux::HasAnyValidAudioInputDevice(void** hints) { | |
| 188 static const char kIoHintName[] = "IOID"; | |
| 189 static const char kNameHintName[] = "NAME"; | |
| 190 | |
| 191 // Since "default", "pulse" and "dmix" devices are virtual devices mapped to | |
| 192 // real devices, we remove them from the list to avoiding duplicate counting. | |
| 193 // In addition, note that we support no more than 2 channels for recording, | |
| 194 // hence surround devices are not stored in the list. Finally, output and | |
| 195 // null devices are not considered as valid devices for recording. | |
| 196 static const char kOutputDevice[] = "Output"; | |
| 197 static const char kNotWantedDefaultDevice[] = "default"; | |
| 198 static const char kNotWantedNullDevice[] = "null"; | |
| 199 static const char kNotWantedPulseDevice[] = "pulse"; | |
| 200 static const char kNotWantedDmixDevice[] = "dmix"; | |
| 201 static const char kNotWantedSurroundDevice[] = "surround"; | |
|
scherkus (not reviewing)
2011/05/27 19:10:37
you missed this comment.. thoughts?
"what about a
xians
2011/05/27 23:57:04
Sorry, I wrote my replay in somewhere else by mist
| |
| 202 | |
| 203 for (void** hint_iter = hints; *hint_iter != NULL; hint_iter++) { | |
| 204 // Only examine devices that are input capable.. Valid values are | |
| 205 // "Input", "Output", and NULL which means both input and output. | |
| 206 scoped_ptr_malloc<char> io(wrapper_->DeviceNameGetHint(*hint_iter, | |
| 207 kIoHintName)); | |
| 208 // Wrong device type, skip it. | |
| 209 if (io != NULL && | |
| 210 strncmp(kOutputDevice, io.get(), strlen(kOutputDevice)) == 0) | |
| 211 continue; | |
| 212 | |
| 213 scoped_ptr_malloc<char> hint_device_name( | |
| 214 wrapper_->DeviceNameGetHint(*hint_iter, kNameHintName)); | |
| 215 // Now check if if it is a valid device. | |
| 216 if (hint_device_name != NULL && | |
| 217 strncmp(kNotWantedDefaultDevice, | |
| 218 hint_device_name.get(), | |
| 219 strlen(kNotWantedDefaultDevice)) != 0 && | |
| 220 strncmp(kNotWantedNullDevice, | |
| 221 hint_device_name.get(), | |
| 222 strlen(kNotWantedNullDevice)) != 0 && | |
| 223 strncmp(kNotWantedPulseDevice, | |
| 224 hint_device_name.get(), | |
| 225 strlen(kNotWantedPulseDevice)) != 0 && | |
| 226 strncmp(kNotWantedDmixDevice, | |
| 227 hint_device_name.get(), | |
| 228 strlen(kNotWantedDmixDevice)) != 0 && | |
| 229 strncmp(kNotWantedSurroundDevice, | |
| 230 hint_device_name.get(), | |
| 231 strlen(kNotWantedSurroundDevice)) != 0) { | |
| 232 return true; | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 // Did not find a valid device. | |
| 237 return false; | |
| 238 } | |
| 239 | |
| 150 // static | 240 // static |
| 151 AudioManager* AudioManager::CreateAudioManager() { | 241 AudioManager* AudioManager::CreateAudioManager() { |
| 152 return new AudioManagerLinux(); | 242 return new AudioManagerLinux(); |
| 153 } | 243 } |
| OLD | NEW |