Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/audio/pulse/audio_manager_pulse.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/environment.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/nix/xdg_util.h" | |
| 11 #include "base/process_util.h" | |
| 12 #include "base/stl_util.h" | |
| 13 #include "media/audio/audio_util.h" | |
| 14 #include "media/audio/pulse/pulse_input.h" | |
| 15 #include "media/audio/pulse/pulse_output.h" | |
| 16 #include "media/audio/pulse/pulse_util.h" | |
| 17 #include "media/audio/pulse/pulse_wrapper.h" | |
| 18 | |
| 19 namespace media { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Maximum number of output streams that can be open simultaneously. | |
| 24 static const int kMaxOutputStreams = 50; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 // static | |
| 29 AudioManager* AudioManagerPulse::Create() { | |
| 30 scoped_ptr<AudioManagerPulse> ret(new AudioManagerPulse()); | |
| 31 if (ret->Init()) | |
| 32 return ret.release(); | |
| 33 | |
| 34 DVLOG(1) << "PulseAudio is not available on the OS"; | |
| 35 return NULL; | |
| 36 } | |
| 37 | |
| 38 AudioManagerPulse::AudioManagerPulse() | |
| 39 : input_mainloop_(NULL), | |
| 40 input_context_(NULL), | |
| 41 devices_(NULL), | |
| 42 native_input_sample_rate_(0) { | |
| 43 SetMaxOutputStreamsAllowed(kMaxOutputStreams); | |
| 44 } | |
| 45 | |
| 46 AudioManagerPulse::~AudioManagerPulse() { | |
| 47 Terminate(); | |
| 48 Shutdown(); | |
| 49 } | |
| 50 | |
| 51 // Implementation of AudioManager. | |
| 52 bool AudioManagerPulse::HasAudioOutputDevices() { | |
| 53 // TODO(xians): implement this function. | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 bool AudioManagerPulse::HasAudioInputDevices() { | |
| 58 // TODO(xians): implement this function. | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 bool AudioManagerPulse::CanShowAudioInputSettings() { | |
| 63 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 64 | |
| 65 switch (base::nix::GetDesktopEnvironment(env.get())) { | |
| 66 case base::nix::DESKTOP_ENVIRONMENT_GNOME: | |
| 67 case base::nix::DESKTOP_ENVIRONMENT_KDE3: | |
| 68 case base::nix::DESKTOP_ENVIRONMENT_KDE4: | |
| 69 case base::nix::DESKTOP_ENVIRONMENT_UNITY: | |
| 70 return true; | |
| 71 case base::nix::DESKTOP_ENVIRONMENT_OTHER: | |
| 72 case base::nix::DESKTOP_ENVIRONMENT_XFCE: | |
| 73 return false; | |
| 74 } | |
| 75 // Unless GetDesktopEnvironment() badly misbehaves, this should never happen. | |
| 76 NOTREACHED(); | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 void AudioManagerPulse::ShowAudioInputSettings() { | |
| 81 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 82 CommandLine command_line(CommandLine::NO_PROGRAM); | |
| 83 switch (base::nix::GetDesktopEnvironment(env.get())) { | |
| 84 case base::nix::DESKTOP_ENVIRONMENT_GNOME: | |
| 85 command_line.SetProgram(base::FilePath("gnome-volume-control")); | |
| 86 break; | |
| 87 case base::nix::DESKTOP_ENVIRONMENT_KDE3: | |
| 88 case base::nix::DESKTOP_ENVIRONMENT_KDE4: | |
| 89 command_line.SetProgram(base::FilePath("kmix")); | |
| 90 break; | |
| 91 case base::nix::DESKTOP_ENVIRONMENT_UNITY: | |
| 92 command_line.SetProgram(base::FilePath("gnome-control-center")); | |
| 93 command_line.AppendArg("sound"); | |
| 94 command_line.AppendArg("input"); | |
| 95 break; | |
| 96 default: | |
| 97 LOG(ERROR) << "Failed to show audio input settings: we don't know " | |
| 98 << "what command to use for your desktop environment."; | |
| 99 return; | |
| 100 } | |
| 101 base::LaunchProcess(command_line, base::LaunchOptions(), NULL); | |
| 102 } | |
| 103 | |
| 104 void AudioManagerPulse::GetAudioInputDeviceNames( | |
| 105 media::AudioDeviceNames* device_names) { | |
| 106 DCHECK(device_names->empty()); | |
| 107 DCHECK(input_mainloop_); | |
| 108 DCHECK(input_context_); | |
| 109 devices_ = device_names; | |
| 110 AutoPulseLock auto_lock(wrapper_.get(), input_mainloop_); | |
| 111 pa_operation* operation = wrapper_->pa_context_get_source_info_list_( | |
| 112 input_context_, DevicesInfoCallback, this); | |
| 113 WaitForOperationCompletion(wrapper_.get(), input_mainloop_, operation); | |
| 114 | |
| 115 if (!device_names->empty()) { | |
| 116 device_names->push_front( | |
| 117 AudioDeviceName(AudioManagerBase::kDefaultDeviceName, | |
| 118 AudioManagerBase::kDefaultDeviceId)); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 AudioOutputStream* AudioManagerPulse::MakeLinearOutputStream( | |
| 123 const AudioParameters& params) { | |
| 124 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | |
| 125 return MakeOutputStream(params); | |
| 126 } | |
| 127 | |
| 128 AudioOutputStream* AudioManagerPulse::MakeLowLatencyOutputStream( | |
| 129 const AudioParameters& params) { | |
| 130 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | |
| 131 return MakeOutputStream(params); | |
| 132 } | |
| 133 | |
| 134 AudioInputStream* AudioManagerPulse::MakeLinearInputStream( | |
| 135 const AudioParameters& params, const std::string& device_id) { | |
| 136 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | |
| 137 return MakeInputStream(params, device_id); | |
| 138 } | |
| 139 | |
| 140 AudioInputStream* AudioManagerPulse::MakeLowLatencyInputStream( | |
| 141 const AudioParameters& params, const std::string& device_id) { | |
| 142 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); | |
| 143 return MakeInputStream(params, device_id); | |
| 144 } | |
| 145 | |
| 146 AudioOutputStream* AudioManagerPulse::MakeOutputStream( | |
| 147 const AudioParameters& params) { | |
| 148 return new PulseAudioOutputStream(wrapper_.get(), params, this); | |
| 149 } | |
| 150 | |
| 151 AudioInputStream* AudioManagerPulse::MakeInputStream( | |
| 152 const AudioParameters& params, const std::string& device_id) { | |
| 153 return new PulseAudioInputStream(wrapper_.get(), this, device_id, params, | |
| 154 input_mainloop_, input_context_); | |
| 155 } | |
| 156 | |
| 157 AudioParameters AudioManagerPulse::GetPreferredLowLatencyOutputStreamParameters( | |
| 158 const AudioParameters& input_params) { | |
| 159 // TODO(xians): figure out the optimized buffer size for the Pulse IO. | |
| 160 int buffer_size = GetAudioHardwareBufferSize(); | |
| 161 if (input_params.frames_per_buffer() < buffer_size) | |
| 162 buffer_size = input_params.frames_per_buffer(); | |
| 163 | |
| 164 // TODO(dalecurtis): This should include bits per channel and channel layout | |
| 165 // eventually. | |
| 166 return AudioParameters( | |
| 167 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), | |
| 168 input_params.sample_rate(), 16, buffer_size); | |
|
DaleCurtis
2013/02/15 00:28:51
We can land it this way, but I'm pretty sure we'll
no longer working on chromium
2013/02/15 16:07:33
Yes, we definitely need to use the native sample r
| |
| 169 } | |
| 170 | |
| 171 int AudioManagerPulse::GetNativeSampleRate() { | |
|
DaleCurtis
2013/02/15 00:28:51
We'll want to plumb this into AudioUtil for before
no longer working on chromium
2013/02/15 16:07:33
Great, FYI, we need to access some member variable
| |
| 172 DCHECK(input_mainloop_); | |
| 173 DCHECK(input_context_); | |
| 174 AutoPulseLock auto_lock(wrapper_.get(), input_mainloop_); | |
| 175 pa_operation* operation = wrapper_->pa_context_get_server_info_( | |
| 176 input_context_, SamplerateInfoCallback, this); | |
| 177 WaitForOperationCompletion(wrapper_.get(), input_mainloop_, operation); | |
| 178 | |
| 179 return native_input_sample_rate_; | |
| 180 } | |
| 181 | |
| 182 bool AudioManagerPulse::Init() { | |
| 183 DCHECK(!input_mainloop_); | |
| 184 | |
| 185 wrapper_.reset(PulseWrapper::Create()); | |
| 186 if (!wrapper_) { | |
| 187 DLOG(WARNING) << "Failed on loading the Pulse library and symbols"; | |
| 188 return false; | |
| 189 } | |
| 190 DLOG(WARNING) << "after loading the Pulse library and symbols"; | |
| 191 // Create a mainloop API and connect to the default server. | |
| 192 // The mainloop is the internal asynchronous API event loop. | |
| 193 input_mainloop_ = wrapper_->pa_threaded_mainloop_new_(); | |
| 194 if (!input_mainloop_) | |
| 195 return false; | |
| 196 DLOG(WARNING) << "after 2"; | |
| 197 // Start the threaded mainloop. | |
| 198 if (wrapper_->pa_threaded_mainloop_start_(input_mainloop_)) { | |
| 199 Terminate(); | |
| 200 return false; | |
| 201 } | |
| 202 | |
| 203 // Lock the event loop object, effectively blocking the event loop thread | |
| 204 // from processing events. This is necessary. | |
| 205 AutoPulseLock auto_lock(wrapper_.get(), input_mainloop_); | |
| 206 | |
| 207 pa_mainloop_api* pa_mainloop_api = | |
| 208 wrapper_->pa_threaded_mainloop_get_api_(input_mainloop_); | |
| 209 input_context_ = wrapper_->pa_context_new_(pa_mainloop_api, "Chrome input"); | |
| 210 DCHECK(input_context_) << "Failed to create PA context"; | |
| 211 if (!input_context_) { | |
| 212 return false; | |
| 213 } | |
| 214 | |
| 215 wrapper_->pa_context_set_state_callback_( | |
| 216 input_context_, &ContextStateCallback, this); | |
| 217 if (wrapper_->pa_context_connect_(input_context_, NULL, | |
| 218 PA_CONTEXT_NOAUTOSPAWN, NULL)) { | |
| 219 DLOG(ERROR) << "Failed to connect to the context"; | |
| 220 return false; | |
| 221 } | |
| 222 | |
| 223 // Wait until |input_context_| is ready. pa_threaded_mainloop_wait() must be | |
| 224 // called after pa_context_get_state() in case the context is already ready, | |
| 225 // otherwise pa_threaded_mainloop_wait() will hang indefinitely. | |
| 226 while (true) { | |
| 227 pa_context_state_t context_state = | |
| 228 wrapper_->pa_context_get_state_(input_context_); | |
| 229 if (!PA_CONTEXT_IS_GOOD(context_state)) { | |
| 230 return false; | |
| 231 } | |
| 232 if (context_state == PA_CONTEXT_READY) | |
| 233 break; | |
| 234 wrapper_->pa_threaded_mainloop_wait_(input_mainloop_); | |
| 235 } | |
| 236 | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 void AudioManagerPulse::Terminate() { | |
| 241 if (!input_mainloop_) { | |
| 242 DCHECK(!input_context_); | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 { | |
| 247 AutoPulseLock auto_lock(wrapper_.get(), input_mainloop_); | |
| 248 if (input_context_) { | |
| 249 // Clear our state callback. | |
| 250 wrapper_->pa_context_set_state_callback_(input_context_, NULL, NULL); | |
| 251 wrapper_->pa_context_disconnect_(input_context_); | |
| 252 wrapper_->pa_context_unref_(input_context_); | |
| 253 input_context_ = NULL; | |
| 254 } | |
| 255 } | |
| 256 | |
| 257 wrapper_->pa_threaded_mainloop_stop_(input_mainloop_); | |
| 258 wrapper_->pa_threaded_mainloop_free_(input_mainloop_); | |
| 259 input_mainloop_ = NULL; | |
| 260 } | |
| 261 | |
| 262 // static, |pa_context| state changed cb. | |
| 263 void AudioManagerPulse::ContextStateCallback(pa_context* context, | |
| 264 void* user_data) { | |
| 265 AudioManagerPulse* manager = reinterpret_cast<AudioManagerPulse*>(user_data); | |
| 266 manager->wrapper_->pa_threaded_mainloop_signal_(manager->input_mainloop_, 0); | |
| 267 } | |
| 268 | |
| 269 void AudioManagerPulse::DevicesInfoCallback(pa_context* context, | |
| 270 const pa_source_info* info, | |
| 271 int error, void *user_data) { | |
| 272 AudioManagerPulse* manager = reinterpret_cast<AudioManagerPulse*>(user_data); | |
| 273 | |
| 274 if (error) { | |
| 275 // Signal the pulse object that it is done. | |
| 276 manager->wrapper_->pa_threaded_mainloop_signal_( | |
| 277 manager->input_mainloop_, 0); | |
| 278 return; | |
| 279 } | |
| 280 | |
| 281 // Exclude the output devices. | |
| 282 if (info->monitor_of_sink == PA_INVALID_INDEX) { | |
| 283 manager->devices_->push_back(media::AudioDeviceName(info->description, | |
| 284 info->name)); | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 void AudioManagerPulse::SamplerateInfoCallback(pa_context* context, | |
| 289 const pa_server_info* info, | |
| 290 void* user_data) { | |
| 291 AudioManagerPulse* manager = reinterpret_cast<AudioManagerPulse*>(user_data); | |
| 292 | |
| 293 manager->native_input_sample_rate_ = info->sample_spec.rate; | |
| 294 manager->wrapper_->pa_threaded_mainloop_signal_(manager->input_mainloop_, 0); | |
| 295 } | |
| 296 | |
| 297 } // namespace media | |
| OLD | NEW |