| 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/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/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "media/audio/audio_output_dispatcher.h" | 13 #include "media/audio/audio_output_dispatcher.h" |
| 14 #include "media/audio/audio_util.h" | 14 #include "media/audio/audio_util.h" |
| 15 #if defined(USE_CRAS) | 15 #if defined(USE_CRAS) |
| 16 #include "media/audio/cras/audio_manager_cras.h" | 16 #include "media/audio/cras/audio_manager_cras.h" |
| 17 #endif | 17 #endif |
| 18 #include "media/audio/linux/alsa_input.h" | 18 #include "media/audio/linux/alsa_input.h" |
| 19 #include "media/audio/linux/alsa_output.h" | 19 #include "media/audio/linux/alsa_output.h" |
| 20 #include "media/audio/linux/alsa_wrapper.h" | 20 #include "media/audio/linux/alsa_wrapper.h" |
| 21 #if defined(USE_PULSEAUDIO) | 21 #if defined(USE_PULSEAUDIO) |
| 22 #include "media/audio/pulse/pulse_output.h" | 22 #include "media/audio/pulse/audio_manager_pulse.h" |
| 23 #endif | 23 #endif |
| 24 #include "media/base/limits.h" | 24 #include "media/base/limits.h" |
| 25 #include "media/base/media_switches.h" | 25 #include "media/base/media_switches.h" |
| 26 | 26 |
| 27 namespace media { | 27 namespace media { |
| 28 | 28 |
| 29 // Maximum number of output streams that can be open simultaneously. | 29 // Maximum number of output streams that can be open simultaneously. |
| 30 static const int kMaxOutputStreams = 50; | 30 static const int kMaxOutputStreams = 50; |
| 31 | 31 |
| 32 // Since "default", "pulse" and "dmix" devices are virtual devices mapped to | 32 // Since "default", "pulse" and "dmix" devices are virtual devices mapped to |
| 33 // real devices, we remove them from the list to avoiding duplicate counting. | 33 // real devices, we remove them from the list to avoiding duplicate counting. |
| 34 // In addition, note that we support no more than 2 channels for recording, | 34 // In addition, note that we support no more than 2 channels for recording, |
| 35 // hence surround devices are not stored in the list. | 35 // hence surround devices are not stored in the list. |
| 36 static const char* kInvalidAudioInputDevices[] = { | 36 static const char* kInvalidAudioInputDevices[] = { |
| 37 "default", | 37 "default", |
| 38 "null", | 38 "null", |
| 39 "pulse", | 39 "pulse", |
| 40 "dmix", | 40 "dmix", |
| 41 "surround", | 41 "surround", |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 // Implementation of AudioManager. | 44 // static |
| 45 bool AudioManagerLinux::HasAudioOutputDevices() { | 45 void AudioManagerLinux::ShowLinuxAudioInputSettings() { |
| 46 return HasAnyAlsaAudioDevice(kStreamPlayback); | |
| 47 } | |
| 48 | |
| 49 bool AudioManagerLinux::HasAudioInputDevices() { | |
| 50 return HasAnyAlsaAudioDevice(kStreamCapture); | |
| 51 } | |
| 52 | |
| 53 AudioManagerLinux::AudioManagerLinux() | |
| 54 : wrapper_(new AlsaWrapper()) { | |
| 55 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | |
| 56 } | |
| 57 | |
| 58 AudioManagerLinux::~AudioManagerLinux() { | |
| 59 Shutdown(); | |
| 60 } | |
| 61 | |
| 62 void AudioManagerLinux::ShowAudioInputSettings() { | |
| 63 scoped_ptr<base::Environment> env(base::Environment::Create()); | 46 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 64 CommandLine command_line(CommandLine::NO_PROGRAM); | 47 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 65 switch (base::nix::GetDesktopEnvironment(env.get())) { | 48 switch (base::nix::GetDesktopEnvironment(env.get())) { |
| 66 case base::nix::DESKTOP_ENVIRONMENT_GNOME: | 49 case base::nix::DESKTOP_ENVIRONMENT_GNOME: |
| 67 command_line.SetProgram(base::FilePath("gnome-volume-control")); | 50 command_line.SetProgram(base::FilePath("gnome-volume-control")); |
| 68 break; | 51 break; |
| 69 case base::nix::DESKTOP_ENVIRONMENT_KDE3: | 52 case base::nix::DESKTOP_ENVIRONMENT_KDE3: |
| 70 case base::nix::DESKTOP_ENVIRONMENT_KDE4: | 53 case base::nix::DESKTOP_ENVIRONMENT_KDE4: |
| 71 command_line.SetProgram(base::FilePath("kmix")); | 54 command_line.SetProgram(base::FilePath("kmix")); |
| 72 break; | 55 break; |
| 73 case base::nix::DESKTOP_ENVIRONMENT_UNITY: | 56 case base::nix::DESKTOP_ENVIRONMENT_UNITY: |
| 74 command_line.SetProgram(base::FilePath("gnome-control-center")); | 57 command_line.SetProgram(base::FilePath("gnome-control-center")); |
| 75 command_line.AppendArg("sound"); | 58 command_line.AppendArg("sound"); |
| 76 command_line.AppendArg("input"); | 59 command_line.AppendArg("input"); |
| 77 break; | 60 break; |
| 78 default: | 61 default: |
| 79 LOG(ERROR) << "Failed to show audio input settings: we don't know " | 62 LOG(ERROR) << "Failed to show audio input settings: we don't know " |
| 80 << "what command to use for your desktop environment."; | 63 << "what command to use for your desktop environment."; |
| 81 return; | 64 return; |
| 82 } | 65 } |
| 83 base::LaunchProcess(command_line, base::LaunchOptions(), NULL); | 66 base::LaunchProcess(command_line, base::LaunchOptions(), NULL); |
| 84 } | 67 } |
| 85 | 68 |
| 69 // Implementation of AudioManager. |
| 70 bool AudioManagerLinux::HasAudioOutputDevices() { |
| 71 return HasAnyAlsaAudioDevice(kStreamPlayback); |
| 72 } |
| 73 |
| 74 bool AudioManagerLinux::HasAudioInputDevices() { |
| 75 return HasAnyAlsaAudioDevice(kStreamCapture); |
| 76 } |
| 77 |
| 78 AudioManagerLinux::AudioManagerLinux() |
| 79 : wrapper_(new AlsaWrapper()) { |
| 80 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
| 81 } |
| 82 |
| 83 AudioManagerLinux::~AudioManagerLinux() { |
| 84 Shutdown(); |
| 85 } |
| 86 |
| 87 void AudioManagerLinux::ShowAudioInputSettings() { |
| 88 ShowLinuxAudioInputSettings(); |
| 89 } |
| 90 |
| 86 void AudioManagerLinux::GetAudioInputDeviceNames( | 91 void AudioManagerLinux::GetAudioInputDeviceNames( |
| 87 media::AudioDeviceNames* device_names) { | 92 media::AudioDeviceNames* device_names) { |
| 88 DCHECK(device_names->empty()); | 93 DCHECK(device_names->empty()); |
| 89 GetAlsaAudioInputDevices(device_names); | 94 GetAlsaAudioInputDevices(device_names); |
| 90 } | 95 } |
| 91 | 96 |
| 92 void AudioManagerLinux::GetAlsaAudioInputDevices( | 97 void AudioManagerLinux::GetAlsaAudioInputDevices( |
| 93 media::AudioDeviceNames* device_names) { | 98 media::AudioDeviceNames* device_names) { |
| 94 // Constants specified by the ALSA API for device hints. | 99 // Constants specified by the ALSA API for device hints. |
| 95 static const char kPcmInterfaceName[] = "pcm"; | 100 static const char kPcmInterfaceName[] = "pcm"; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 246 } |
| 242 | 247 |
| 243 AudioInputStream* AudioManagerLinux::MakeLowLatencyInputStream( | 248 AudioInputStream* AudioManagerLinux::MakeLowLatencyInputStream( |
| 244 const AudioParameters& params, const std::string& device_id) { | 249 const AudioParameters& params, const std::string& device_id) { |
| 245 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | 250 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 246 return MakeInputStream(params, device_id); | 251 return MakeInputStream(params, device_id); |
| 247 } | 252 } |
| 248 | 253 |
| 249 AudioOutputStream* AudioManagerLinux::MakeOutputStream( | 254 AudioOutputStream* AudioManagerLinux::MakeOutputStream( |
| 250 const AudioParameters& params) { | 255 const AudioParameters& params) { |
| 251 #if defined(USE_PULSEAUDIO) | |
| 252 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUsePulseAudio)) { | |
| 253 return new PulseAudioOutputStream(params, this); | |
| 254 } | |
| 255 #endif | |
| 256 | |
| 257 std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice; | 256 std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice; |
| 258 if (CommandLine::ForCurrentProcess()->HasSwitch( | 257 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 259 switches::kAlsaOutputDevice)) { | 258 switches::kAlsaOutputDevice)) { |
| 260 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 259 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 261 switches::kAlsaOutputDevice); | 260 switches::kAlsaOutputDevice); |
| 262 } | 261 } |
| 263 return new AlsaPcmOutputStream(device_name, params, wrapper_.get(), this); | 262 return new AlsaPcmOutputStream(device_name, params, wrapper_.get(), this); |
| 264 } | 263 } |
| 265 | 264 |
| 266 AudioInputStream* AudioManagerLinux::MakeInputStream( | 265 AudioInputStream* AudioManagerLinux::MakeInputStream( |
| 267 const AudioParameters& params, const std::string& device_id) { | 266 const AudioParameters& params, const std::string& device_id) { |
| 268 std::string device_name = (device_id == AudioManagerBase::kDefaultDeviceId) ? | 267 std::string device_name = (device_id == AudioManagerBase::kDefaultDeviceId) ? |
| 269 AlsaPcmInputStream::kAutoSelectDevice : device_id; | 268 AlsaPcmInputStream::kAutoSelectDevice : device_id; |
| 270 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaInputDevice)) { | 269 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaInputDevice)) { |
| 271 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 270 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 272 switches::kAlsaInputDevice); | 271 switches::kAlsaInputDevice); |
| 273 } | 272 } |
| 274 | 273 |
| 275 return new AlsaPcmInputStream(this, device_name, params, wrapper_.get()); | 274 return new AlsaPcmInputStream(this, device_name, params, wrapper_.get()); |
| 276 } | 275 } |
| 277 | 276 |
| 278 AudioManager* CreateAudioManager() { | 277 AudioManager* CreateAudioManager() { |
| 279 #if defined(USE_CRAS) | 278 #if defined(USE_CRAS) |
| 280 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseCras)) { | 279 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUseCras)) { |
| 281 return new AudioManagerCras(); | 280 return new AudioManagerCras(); |
| 282 } | 281 } |
| 283 #endif | 282 #endif |
| 284 | 283 |
| 284 #if defined(USE_PULSEAUDIO) |
| 285 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUsePulseAudio)) { |
| 286 AudioManager* manager = AudioManagerPulse::Create(); |
| 287 if (manager) |
| 288 return manager; |
| 289 } |
| 290 #endif |
| 291 |
| 285 return new AudioManagerLinux(); | 292 return new AudioManagerLinux(); |
| 286 } | 293 } |
| 287 | 294 |
| 288 AudioParameters AudioManagerLinux::GetPreferredLowLatencyOutputStreamParameters( | 295 AudioParameters AudioManagerLinux::GetPreferredLowLatencyOutputStreamParameters( |
| 289 const AudioParameters& input_params) { | 296 const AudioParameters& input_params) { |
| 290 // Since Linux doesn't actually have a low latency path the hardware buffer | 297 // Since Linux doesn't actually have a low latency path the hardware buffer |
| 291 // size is quite large in order to prevent glitches with general usage. Some | 298 // size is quite large in order to prevent glitches with general usage. Some |
| 292 // clients, such as WebRTC, have a more limited use case and work acceptably | 299 // clients, such as WebRTC, have a more limited use case and work acceptably |
| 293 // with a smaller buffer size. The check below allows clients which want to | 300 // with a smaller buffer size. The check below allows clients which want to |
| 294 // try a smaller buffer size on Linux to do so. | 301 // try a smaller buffer size on Linux to do so. |
| 295 int buffer_size = GetAudioHardwareBufferSize(); | 302 int buffer_size = GetAudioHardwareBufferSize(); |
| 296 if (input_params.frames_per_buffer() < buffer_size) | 303 if (input_params.frames_per_buffer() < buffer_size) |
| 297 buffer_size = input_params.frames_per_buffer(); | 304 buffer_size = input_params.frames_per_buffer(); |
| 298 | 305 |
| 299 // TODO(dalecurtis): This should include bits per channel and channel layout | 306 // TODO(dalecurtis): This should include bits per channel and channel layout |
| 300 // eventually. | 307 // eventually. |
| 301 return AudioParameters( | 308 return AudioParameters( |
| 302 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), | 309 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), |
| 303 input_params.sample_rate(), 16, buffer_size); | 310 input_params.sample_rate(), 16, buffer_size); |
| 304 } | 311 } |
| 305 | 312 |
| 306 } // namespace media | 313 } // namespace media |
| OLD | NEW |