Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / audio / pulse / audio_manager_pulse.cc
blobf3d8832635b196a370aff6792b059a4d1e3d99ff
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.
5 #include "media/audio/pulse/audio_manager_pulse.h"
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/nix/xdg_util.h"
12 #include "base/stl_util.h"
13 #if defined(USE_ALSA)
14 #include "media/audio/alsa/audio_manager_alsa.h"
15 #endif
16 #include "media/audio/audio_parameters.h"
17 #include "media/audio/pulse/pulse_input.h"
18 #include "media/audio/pulse/pulse_output.h"
19 #include "media/audio/pulse/pulse_util.h"
20 #include "media/base/channel_layout.h"
22 #if defined(DLOPEN_PULSEAUDIO)
23 #include "media/audio/pulse/pulse_stubs.h"
25 using media_audio_pulse::kModulePulse;
26 using media_audio_pulse::InitializeStubs;
27 using media_audio_pulse::StubPathMap;
28 #endif // defined(DLOPEN_PULSEAUDIO)
30 namespace media {
32 using pulse::AutoPulseLock;
33 using pulse::WaitForOperationCompletion;
35 // Maximum number of output streams that can be open simultaneously.
36 static const int kMaxOutputStreams = 50;
38 // Define bounds for the output buffer size.
39 static const int kMinimumOutputBufferSize = 512;
40 static const int kMaximumOutputBufferSize = 8192;
42 // Default input buffer size.
43 static const int kDefaultInputBufferSize = 1024;
45 #if defined(DLOPEN_PULSEAUDIO)
46 static const base::FilePath::CharType kPulseLib[] =
47 FILE_PATH_LITERAL("libpulse.so.0");
48 #endif
50 // static
51 AudioManager* AudioManagerPulse::Create(AudioLogFactory* audio_log_factory) {
52 scoped_ptr<AudioManagerPulse> ret(new AudioManagerPulse(audio_log_factory));
53 if (ret->Init())
54 return ret.release();
56 DVLOG(1) << "PulseAudio is not available on the OS";
57 return NULL;
60 AudioManagerPulse::AudioManagerPulse(AudioLogFactory* audio_log_factory)
61 : AudioManagerBase(audio_log_factory),
62 input_mainloop_(NULL),
63 input_context_(NULL),
64 devices_(NULL),
65 native_input_sample_rate_(0) {
66 SetMaxOutputStreamsAllowed(kMaxOutputStreams);
69 AudioManagerPulse::~AudioManagerPulse() {
70 Shutdown();
72 // The Pulse objects are the last things to be destroyed since Shutdown()
73 // needs them.
74 DestroyPulse();
77 // Implementation of AudioManager.
78 bool AudioManagerPulse::HasAudioOutputDevices() {
79 AudioDeviceNames devices;
80 GetAudioOutputDeviceNames(&devices);
81 return !devices.empty();
84 bool AudioManagerPulse::HasAudioInputDevices() {
85 AudioDeviceNames devices;
86 GetAudioInputDeviceNames(&devices);
87 return !devices.empty();
90 void AudioManagerPulse::ShowAudioInputSettings() {
91 #if defined(USE_ALSA)
92 AudioManagerAlsa::ShowLinuxAudioInputSettings();
93 #endif
96 void AudioManagerPulse::GetAudioDeviceNames(
97 bool input, media::AudioDeviceNames* device_names) {
98 DCHECK(device_names->empty());
99 DCHECK(input_mainloop_);
100 DCHECK(input_context_);
101 AutoPulseLock auto_lock(input_mainloop_);
102 devices_ = device_names;
103 pa_operation* operation = NULL;
104 if (input) {
105 operation = pa_context_get_source_info_list(
106 input_context_, InputDevicesInfoCallback, this);
107 } else {
108 operation = pa_context_get_sink_info_list(
109 input_context_, OutputDevicesInfoCallback, this);
111 WaitForOperationCompletion(input_mainloop_, operation);
113 // Prepend the default device if the list is not empty.
114 if (!device_names->empty()) {
115 device_names->push_front(
116 AudioDeviceName(AudioManagerBase::kDefaultDeviceName,
117 AudioManagerBase::kDefaultDeviceId));
121 void AudioManagerPulse::GetAudioInputDeviceNames(
122 AudioDeviceNames* device_names) {
123 GetAudioDeviceNames(true, device_names);
126 void AudioManagerPulse::GetAudioOutputDeviceNames(
127 AudioDeviceNames* device_names) {
128 GetAudioDeviceNames(false, device_names);
131 AudioParameters AudioManagerPulse::GetInputStreamParameters(
132 const std::string& device_id) {
133 int user_buffer_size = GetUserBufferSize();
134 int buffer_size = user_buffer_size ?
135 user_buffer_size : kDefaultInputBufferSize;
137 // TODO(xians): add support for querying native channel layout for pulse.
138 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY,
139 CHANNEL_LAYOUT_STEREO, GetNativeSampleRate(), 16,
140 buffer_size);
143 AudioOutputStream* AudioManagerPulse::MakeLinearOutputStream(
144 const AudioParameters& params) {
145 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
146 return MakeOutputStream(params, AudioManagerBase::kDefaultDeviceId);
149 AudioOutputStream* AudioManagerPulse::MakeLowLatencyOutputStream(
150 const AudioParameters& params,
151 const std::string& device_id) {
152 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
153 return MakeOutputStream(
154 params,
155 device_id.empty() ? AudioManagerBase::kDefaultDeviceId : device_id);
158 AudioInputStream* AudioManagerPulse::MakeLinearInputStream(
159 const AudioParameters& params, const std::string& device_id) {
160 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format());
161 return MakeInputStream(params, device_id);
164 AudioInputStream* AudioManagerPulse::MakeLowLatencyInputStream(
165 const AudioParameters& params, const std::string& device_id) {
166 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format());
167 return MakeInputStream(params, device_id);
170 AudioParameters AudioManagerPulse::GetPreferredOutputStreamParameters(
171 const std::string& output_device_id,
172 const AudioParameters& input_params) {
173 // TODO(tommi): Support |output_device_id|.
174 VLOG_IF(0, !output_device_id.empty()) << "Not implemented!";
176 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
177 int buffer_size = kMinimumOutputBufferSize;
178 int bits_per_sample = 16;
179 int sample_rate = GetNativeSampleRate();
180 if (input_params.IsValid()) {
181 bits_per_sample = input_params.bits_per_sample();
182 channel_layout = input_params.channel_layout();
183 buffer_size =
184 std::min(kMaximumOutputBufferSize,
185 std::max(buffer_size, input_params.frames_per_buffer()));
188 int user_buffer_size = GetUserBufferSize();
189 if (user_buffer_size)
190 buffer_size = user_buffer_size;
192 return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout,
193 sample_rate, bits_per_sample, buffer_size);
196 AudioOutputStream* AudioManagerPulse::MakeOutputStream(
197 const AudioParameters& params,
198 const std::string& device_id) {
199 DCHECK(!device_id.empty());
200 return new PulseAudioOutputStream(params, device_id, this);
203 AudioInputStream* AudioManagerPulse::MakeInputStream(
204 const AudioParameters& params, const std::string& device_id) {
205 return new PulseAudioInputStream(this, device_id, params,
206 input_mainloop_, input_context_);
209 int AudioManagerPulse::GetNativeSampleRate() {
210 DCHECK(input_mainloop_);
211 DCHECK(input_context_);
212 AutoPulseLock auto_lock(input_mainloop_);
213 pa_operation* operation = pa_context_get_server_info(
214 input_context_, SampleRateInfoCallback, this);
215 WaitForOperationCompletion(input_mainloop_, operation);
217 return native_input_sample_rate_;
220 bool AudioManagerPulse::Init() {
221 DCHECK(!input_mainloop_);
223 #if defined(DLOPEN_PULSEAUDIO)
224 StubPathMap paths;
226 // Check if the pulse library is avialbale.
227 paths[kModulePulse].push_back(kPulseLib);
228 if (!InitializeStubs(paths)) {
229 VLOG(1) << "Failed on loading the Pulse library and symbols";
230 return false;
232 #endif // defined(DLOPEN_PULSEAUDIO)
234 // Create a mainloop API and connect to the default server.
235 // The mainloop is the internal asynchronous API event loop.
236 input_mainloop_ = pa_threaded_mainloop_new();
237 if (!input_mainloop_)
238 return false;
240 // Start the threaded mainloop.
241 if (pa_threaded_mainloop_start(input_mainloop_))
242 return false;
244 // Lock the event loop object, effectively blocking the event loop thread
245 // from processing events. This is necessary.
246 AutoPulseLock auto_lock(input_mainloop_);
248 pa_mainloop_api* pa_mainloop_api =
249 pa_threaded_mainloop_get_api(input_mainloop_);
250 input_context_ = pa_context_new(pa_mainloop_api, "Chrome input");
251 if (!input_context_)
252 return false;
254 pa_context_set_state_callback(input_context_, &pulse::ContextStateCallback,
255 input_mainloop_);
256 if (pa_context_connect(input_context_, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL)) {
257 VLOG(0) << "Failed to connect to the context. Error: "
258 << pa_strerror(pa_context_errno(input_context_));
259 return false;
262 // Wait until |input_context_| is ready. pa_threaded_mainloop_wait() must be
263 // called after pa_context_get_state() in case the context is already ready,
264 // otherwise pa_threaded_mainloop_wait() will hang indefinitely.
265 while (true) {
266 pa_context_state_t context_state = pa_context_get_state(input_context_);
267 if (!PA_CONTEXT_IS_GOOD(context_state))
268 return false;
269 if (context_state == PA_CONTEXT_READY)
270 break;
271 pa_threaded_mainloop_wait(input_mainloop_);
274 return true;
277 void AudioManagerPulse::DestroyPulse() {
278 if (!input_mainloop_) {
279 DCHECK(!input_context_);
280 return;
284 AutoPulseLock auto_lock(input_mainloop_);
285 if (input_context_) {
286 // Clear our state callback.
287 pa_context_set_state_callback(input_context_, NULL, NULL);
288 pa_context_disconnect(input_context_);
289 pa_context_unref(input_context_);
290 input_context_ = NULL;
294 pa_threaded_mainloop_stop(input_mainloop_);
295 pa_threaded_mainloop_free(input_mainloop_);
296 input_mainloop_ = NULL;
299 void AudioManagerPulse::InputDevicesInfoCallback(pa_context* context,
300 const pa_source_info* info,
301 int error, void *user_data) {
302 AudioManagerPulse* manager = reinterpret_cast<AudioManagerPulse*>(user_data);
304 if (error) {
305 // Signal the pulse object that it is done.
306 pa_threaded_mainloop_signal(manager->input_mainloop_, 0);
307 return;
310 // Exclude the output devices.
311 if (info->monitor_of_sink == PA_INVALID_INDEX) {
312 manager->devices_->push_back(AudioDeviceName(info->description,
313 info->name));
317 void AudioManagerPulse::OutputDevicesInfoCallback(pa_context* context,
318 const pa_sink_info* info,
319 int error, void *user_data) {
320 AudioManagerPulse* manager = reinterpret_cast<AudioManagerPulse*>(user_data);
322 if (error) {
323 // Signal the pulse object that it is done.
324 pa_threaded_mainloop_signal(manager->input_mainloop_, 0);
325 return;
328 manager->devices_->push_back(AudioDeviceName(info->description,
329 info->name));
332 void AudioManagerPulse::SampleRateInfoCallback(pa_context* context,
333 const pa_server_info* info,
334 void* user_data) {
335 AudioManagerPulse* manager = reinterpret_cast<AudioManagerPulse*>(user_data);
337 manager->native_input_sample_rate_ = info->sample_spec.rate;
338 pa_threaded_mainloop_signal(manager->input_mainloop_, 0);
341 } // namespace media