| 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 #include "media/audio/linux/alsa_input.h" | 15 #include "media/audio/linux/alsa_input.h" |
| 16 #include "media/audio/linux/alsa_output.h" | 16 #include "media/audio/linux/alsa_output.h" |
| 17 #include "media/audio/linux/alsa_wrapper.h" | 17 #include "media/audio/linux/alsa_wrapper.h" |
| 18 #if defined(USE_PULSEAUDIO) | 18 #if defined(USE_PULSEAUDIO) |
| 19 #include "media/audio/pulse/pulse_output.h" | 19 #include "media/audio/pulse/audio_manager_pulse.h" |
| 20 #endif | 20 #endif |
| 21 #if defined(USE_CRAS) | 21 #if defined(USE_CRAS) |
| 22 #include "media/audio/linux/cras_input.h" | 22 #include "media/audio/linux/cras_input.h" |
| 23 #include "media/audio/linux/cras_output.h" | 23 #include "media/audio/linux/cras_output.h" |
| 24 #endif | 24 #endif |
| 25 #include "media/base/limits.h" | 25 #include "media/base/limits.h" |
| 26 #include "media/base/media_switches.h" | 26 #include "media/base/media_switches.h" |
| 27 | 27 |
| 28 namespace media { | 28 namespace media { |
| 29 | 29 |
| 30 // Maximum number of output streams that can be open simultaneously. | 30 // Maximum number of output streams that can be open simultaneously. |
| 31 static const int kMaxOutputStreams = 50; | 31 static const int kMaxOutputStreams = 50; |
| 32 | 32 |
| 33 // Since "default", "pulse" and "dmix" devices are virtual devices mapped to | 33 // Since "default", "pulse" and "dmix" devices are virtual devices mapped to |
| 34 // real devices, we remove them from the list to avoiding duplicate counting. | 34 // real devices, we remove them from the list to avoiding duplicate counting. |
| 35 // In addition, note that we support no more than 2 channels for recording, | 35 // In addition, note that we support no more than 2 channels for recording, |
| 36 // hence surround devices are not stored in the list. | 36 // hence surround devices are not stored in the list. |
| 37 static const char* kInvalidAudioInputDevices[] = { | 37 static const char* kInvalidAudioInputDevices[] = { |
| 38 "default", | 38 "default", |
| 39 "null", | 39 "null", |
| 40 "pulse", | 40 "pulse", |
| 41 "dmix", | 41 "dmix", |
| 42 "surround", | 42 "surround", |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 static const char kCrasAutomaticDeviceName[] = "Automatic"; | 45 static const char kCrasAutomaticDeviceName[] = "Automatic"; |
| 46 static const char kCrasAutomaticDeviceId[] = "automatic"; | 46 static const char kCrasAutomaticDeviceId[] = "automatic"; |
| 47 | 47 |
| 48 // static |
| 49 void AudioManagerLinux::ShowLinuxAudioInputSettings() { |
| 50 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 51 CommandLine command_line(CommandLine::NO_PROGRAM); |
| 52 switch (base::nix::GetDesktopEnvironment(env.get())) { |
| 53 case base::nix::DESKTOP_ENVIRONMENT_GNOME: |
| 54 command_line.SetProgram(base::FilePath("gnome-volume-control")); |
| 55 break; |
| 56 case base::nix::DESKTOP_ENVIRONMENT_KDE3: |
| 57 case base::nix::DESKTOP_ENVIRONMENT_KDE4: |
| 58 command_line.SetProgram(base::FilePath("kmix")); |
| 59 break; |
| 60 case base::nix::DESKTOP_ENVIRONMENT_UNITY: |
| 61 command_line.SetProgram(base::FilePath("gnome-control-center")); |
| 62 command_line.AppendArg("sound"); |
| 63 command_line.AppendArg("input"); |
| 64 break; |
| 65 default: |
| 66 LOG(ERROR) << "Failed to show audio input settings: we don't know " |
| 67 << "what command to use for your desktop environment."; |
| 68 return; |
| 69 } |
| 70 base::LaunchProcess(command_line, base::LaunchOptions(), NULL); |
| 71 } |
| 72 |
| 48 // Implementation of AudioManager. | 73 // Implementation of AudioManager. |
| 49 bool AudioManagerLinux::HasAudioOutputDevices() { | 74 bool AudioManagerLinux::HasAudioOutputDevices() { |
| 50 if (UseCras()) | 75 if (UseCras()) |
| 51 return true; | 76 return true; |
| 52 | 77 |
| 53 return HasAnyAlsaAudioDevice(kStreamPlayback); | 78 return HasAnyAlsaAudioDevice(kStreamPlayback); |
| 54 } | 79 } |
| 55 | 80 |
| 56 bool AudioManagerLinux::HasAudioInputDevices() { | 81 bool AudioManagerLinux::HasAudioInputDevices() { |
| 57 if (UseCras()) | 82 if (UseCras()) |
| 58 return true; | 83 return true; |
| 59 | 84 |
| 60 return HasAnyAlsaAudioDevice(kStreamCapture); | 85 return HasAnyAlsaAudioDevice(kStreamCapture); |
| 61 } | 86 } |
| 62 | 87 |
| 63 AudioManagerLinux::AudioManagerLinux() | 88 AudioManagerLinux::AudioManagerLinux() |
| 64 : wrapper_(new AlsaWrapper()) { | 89 : wrapper_(new AlsaWrapper()) { |
| 65 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | 90 SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
| 66 } | 91 } |
| 67 | 92 |
| 68 AudioManagerLinux::~AudioManagerLinux() { | 93 AudioManagerLinux::~AudioManagerLinux() { |
| 69 Shutdown(); | 94 Shutdown(); |
| 70 } | 95 } |
| 71 | 96 |
| 72 void AudioManagerLinux::ShowAudioInputSettings() { | 97 void AudioManagerLinux::ShowAudioInputSettings() { |
| 73 scoped_ptr<base::Environment> env(base::Environment::Create()); | 98 ShowLinuxAudioInputSettings(); |
| 74 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 75 switch (base::nix::GetDesktopEnvironment(env.get())) { | |
| 76 case base::nix::DESKTOP_ENVIRONMENT_GNOME: | |
| 77 command_line.SetProgram(base::FilePath("gnome-volume-control")); | |
| 78 break; | |
| 79 case base::nix::DESKTOP_ENVIRONMENT_KDE3: | |
| 80 case base::nix::DESKTOP_ENVIRONMENT_KDE4: | |
| 81 command_line.SetProgram(base::FilePath("kmix")); | |
| 82 break; | |
| 83 case base::nix::DESKTOP_ENVIRONMENT_UNITY: | |
| 84 command_line.SetProgram(base::FilePath("gnome-control-center")); | |
| 85 command_line.AppendArg("sound"); | |
| 86 command_line.AppendArg("input"); | |
| 87 break; | |
| 88 default: | |
| 89 LOG(ERROR) << "Failed to show audio input settings: we don't know " | |
| 90 << "what command to use for your desktop environment."; | |
| 91 return; | |
| 92 } | |
| 93 base::LaunchProcess(command_line, base::LaunchOptions(), NULL); | |
| 94 } | 99 } |
| 95 | 100 |
| 96 void AudioManagerLinux::GetAudioInputDeviceNames( | 101 void AudioManagerLinux::GetAudioInputDeviceNames( |
| 97 media::AudioDeviceNames* device_names) { | 102 media::AudioDeviceNames* device_names) { |
| 98 DCHECK(device_names->empty()); | 103 DCHECK(device_names->empty()); |
| 99 if (UseCras()) { | 104 if (UseCras()) { |
| 100 GetCrasAudioInputDevices(device_names); | 105 GetCrasAudioInputDevices(device_names); |
| 101 return; | 106 return; |
| 102 } | 107 } |
| 103 | 108 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 } | 283 } |
| 279 | 284 |
| 280 AudioOutputStream* AudioManagerLinux::MakeOutputStream( | 285 AudioOutputStream* AudioManagerLinux::MakeOutputStream( |
| 281 const AudioParameters& params) { | 286 const AudioParameters& params) { |
| 282 #if defined(USE_CRAS) | 287 #if defined(USE_CRAS) |
| 283 if (UseCras()) { | 288 if (UseCras()) { |
| 284 return new CrasOutputStream(params, this); | 289 return new CrasOutputStream(params, this); |
| 285 } | 290 } |
| 286 #endif | 291 #endif |
| 287 | 292 |
| 288 #if defined(USE_PULSEAUDIO) | |
| 289 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUsePulseAudio)) { | |
| 290 return new PulseAudioOutputStream(params, this); | |
| 291 } | |
| 292 #endif | |
| 293 | |
| 294 std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice; | 293 std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice; |
| 295 if (CommandLine::ForCurrentProcess()->HasSwitch( | 294 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 296 switches::kAlsaOutputDevice)) { | 295 switches::kAlsaOutputDevice)) { |
| 297 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 296 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 298 switches::kAlsaOutputDevice); | 297 switches::kAlsaOutputDevice); |
| 299 } | 298 } |
| 300 return new AlsaPcmOutputStream(device_name, params, wrapper_.get(), this); | 299 return new AlsaPcmOutputStream(device_name, params, wrapper_.get(), this); |
| 301 } | 300 } |
| 302 | 301 |
| 303 AudioInputStream* AudioManagerLinux::MakeInputStream( | 302 AudioInputStream* AudioManagerLinux::MakeInputStream( |
| 304 const AudioParameters& params, const std::string& device_id) { | 303 const AudioParameters& params, const std::string& device_id) { |
| 305 #if defined(USE_CRAS) | 304 #if defined(USE_CRAS) |
| 306 if (UseCras()) { | 305 if (UseCras()) { |
| 307 return new CrasInputStream(params, this); | 306 return new CrasInputStream(params, this); |
| 308 } | 307 } |
| 309 #endif | 308 #endif |
| 310 | 309 |
| 311 std::string device_name = (device_id == AudioManagerBase::kDefaultDeviceId) ? | 310 std::string device_name = (device_id == AudioManagerBase::kDefaultDeviceId) ? |
| 312 AlsaPcmInputStream::kAutoSelectDevice : device_id; | 311 AlsaPcmInputStream::kAutoSelectDevice : device_id; |
| 313 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaInputDevice)) { | 312 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaInputDevice)) { |
| 314 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 313 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 315 switches::kAlsaInputDevice); | 314 switches::kAlsaInputDevice); |
| 316 } | 315 } |
| 317 | 316 |
| 318 return new AlsaPcmInputStream(this, device_name, params, wrapper_.get()); | 317 return new AlsaPcmInputStream(this, device_name, params, wrapper_.get()); |
| 319 } | 318 } |
| 320 | 319 |
| 321 AudioManager* CreateAudioManager() { | 320 AudioManager* CreateAudioManager() { |
| 321 #if defined(USE_PULSEAUDIO) |
| 322 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUsePulseAudio)) { |
| 323 AudioManager* manager = AudioManagerPulse::Create(); |
| 324 if (manager) |
| 325 return manager; |
| 326 } |
| 327 #endif |
| 328 |
| 322 return new AudioManagerLinux(); | 329 return new AudioManagerLinux(); |
| 323 } | 330 } |
| 324 | 331 |
| 325 AudioParameters AudioManagerLinux::GetPreferredLowLatencyOutputStreamParameters( | 332 AudioParameters AudioManagerLinux::GetPreferredLowLatencyOutputStreamParameters( |
| 326 const AudioParameters& input_params) { | 333 const AudioParameters& input_params) { |
| 327 // Since Linux doesn't actually have a low latency path the hardware buffer | 334 // Since Linux doesn't actually have a low latency path the hardware buffer |
| 328 // size is quite large in order to prevent glitches with general usage. Some | 335 // size is quite large in order to prevent glitches with general usage. Some |
| 329 // clients, such as WebRTC, have a more limited use case and work acceptably | 336 // clients, such as WebRTC, have a more limited use case and work acceptably |
| 330 // with a smaller buffer size. The check below allows clients which want to | 337 // with a smaller buffer size. The check below allows clients which want to |
| 331 // try a smaller buffer size on Linux to do so. | 338 // try a smaller buffer size on Linux to do so. |
| 332 int buffer_size = GetAudioHardwareBufferSize(); | 339 int buffer_size = GetAudioHardwareBufferSize(); |
| 333 if (input_params.frames_per_buffer() < buffer_size) | 340 if (input_params.frames_per_buffer() < buffer_size) |
| 334 buffer_size = input_params.frames_per_buffer(); | 341 buffer_size = input_params.frames_per_buffer(); |
| 335 | 342 |
| 336 // TODO(dalecurtis): This should include bits per channel and channel layout | 343 // TODO(dalecurtis): This should include bits per channel and channel layout |
| 337 // eventually. | 344 // eventually. |
| 338 return AudioParameters( | 345 return AudioParameters( |
| 339 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), | 346 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), |
| 340 input_params.sample_rate(), 16, buffer_size); | 347 input_params.sample_rate(), 16, buffer_size); |
| 341 } | 348 } |
| 342 | 349 |
| 343 } // namespace media | 350 } // namespace media |
| OLD | NEW |