Close /dev/tty again on uninit.
[mplayer/glamo.git] / libao2 / ao_pulse.c
blobb2fdcbb1752918e4f9cf015025a96792188df4ee
1 /*
2 * PulseAudio audio output driver.
3 * Copyright (C) 2006 Lennart Poettering
4 * Copyright (C) 2007 Reimar Doeffinger
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <string.h>
25 #include <pulse/pulseaudio.h>
27 #include "config.h"
28 #include "libaf/af_format.h"
29 #include "mp_msg.h"
30 #include "audio_out.h"
31 #include "audio_out_internal.h"
33 #define PULSE_CLIENT_NAME "MPlayer"
35 /** General driver info */
36 static const ao_info_t info = {
37 "PulseAudio audio output",
38 "pulse",
39 "Lennart Poettering",
43 /** PulseAudio playback stream object */
44 static struct pa_stream *stream;
46 /** PulseAudio connection context */
47 static struct pa_context *context;
49 /** Main event loop object */
50 static struct pa_threaded_mainloop *mainloop;
52 /** A temporary variable to store the current volume */
53 static pa_cvolume volume;
55 static int broken_pause;
57 LIBAO_EXTERN(pulse)
59 #define GENERIC_ERR_MSG(ctx, str) \
60 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] "str": %s\n", \
61 pa_strerror(pa_context_errno(ctx)))
63 static void context_state_cb(pa_context *c, void *userdata) {
64 switch (pa_context_get_state(c)) {
65 case PA_CONTEXT_READY:
66 case PA_CONTEXT_TERMINATED:
67 case PA_CONTEXT_FAILED:
68 pa_threaded_mainloop_signal(mainloop, 0);
69 break;
73 static void stream_state_cb(pa_stream *s, void *userdata) {
74 switch (pa_stream_get_state(s)) {
75 case PA_STREAM_READY:
76 case PA_STREAM_FAILED:
77 case PA_STREAM_TERMINATED:
78 pa_threaded_mainloop_signal(mainloop, 0);
79 break;
83 static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
84 pa_threaded_mainloop_signal(mainloop, 0);
87 static void stream_latency_update_cb(pa_stream *s, void *userdata) {
88 pa_threaded_mainloop_signal(mainloop, 0);
91 static void success_cb(pa_stream *s, int success, void *userdata) {
92 if (userdata)
93 *(int *)userdata = success;
94 pa_threaded_mainloop_signal(mainloop, 0);
97 /**
98 * \brief waits for a pulseaudio operation to finish, frees it and
99 * unlocks the mainloop
100 * \param op operation to wait for
101 * \return 1 if operation has finished normally (DONE state), 0 otherwise
103 static int waitop(pa_operation *op) {
104 pa_operation_state_t state;
105 if (!op) {
106 pa_threaded_mainloop_unlock(mainloop);
107 return 0;
109 state = pa_operation_get_state(op);
110 while (state == PA_OPERATION_RUNNING) {
111 pa_threaded_mainloop_wait(mainloop);
112 state = pa_operation_get_state(op);
114 pa_operation_unref(op);
115 pa_threaded_mainloop_unlock(mainloop);
116 return state == PA_OPERATION_DONE;
119 static const struct format_map_s {
120 int mp_format;
121 pa_sample_format_t pa_format;
122 } format_maps[] = {
123 {AF_FORMAT_S16_LE, PA_SAMPLE_S16LE},
124 {AF_FORMAT_S16_BE, PA_SAMPLE_S16BE},
125 #ifdef PA_SAMPLE_S32NE
126 {AF_FORMAT_S32_LE, PA_SAMPLE_S32LE},
127 {AF_FORMAT_S32_BE, PA_SAMPLE_S32BE},
128 #endif
129 #ifdef PA_SAMPLE_FLOAT32NE
130 {AF_FORMAT_FLOAT_LE, PA_SAMPLE_FLOAT32LE},
131 {AF_FORMAT_FLOAT_BE, PA_SAMPLE_FLOAT32BE},
132 #endif
133 {AF_FORMAT_U8, PA_SAMPLE_U8},
134 {AF_FORMAT_MU_LAW, PA_SAMPLE_ULAW},
135 {AF_FORMAT_A_LAW, PA_SAMPLE_ALAW},
136 {AF_FORMAT_UNKNOWN, 0}
139 static int init(int rate_hz, int channels, int format, int flags) {
140 struct pa_sample_spec ss;
141 struct pa_channel_map map;
142 const struct format_map_s *fmt_map;
143 char *devarg = NULL;
144 char *host = NULL;
145 char *sink = NULL;
146 char *version = pa_get_library_version();
148 if (ao_subdevice) {
149 devarg = strdup(ao_subdevice);
150 sink = strchr(devarg, ':');
151 if (sink) *sink++ = 0;
152 if (devarg[0]) host = devarg;
155 broken_pause = 0;
156 // not sure which versions are affected, assume 0.9.11* to 0.9.14*
157 // known bad: 0.9.14, 0.9.13
158 // known good: 0.9.9, 0.9.10, 0.9.15
159 // to test: pause, wait ca. 5 seconds framestep and see if MPlayer hangs somewhen
160 if (strncmp(version, "0.9.1", 5) == 0 && version[5] >= '1' && version[5] <= '4') {
161 mp_msg(MSGT_AO, MSGL_WARN, "[pulse] working around probably broken pause functionality,\n"
162 " see http://www.pulseaudio.org/ticket/440\n");
163 broken_pause = 1;
166 ss.channels = channels;
167 ss.rate = rate_hz;
169 ao_data.samplerate = rate_hz;
170 ao_data.channels = channels;
172 fmt_map = format_maps;
173 while (fmt_map->mp_format != format) {
174 if (fmt_map->mp_format == AF_FORMAT_UNKNOWN) {
175 mp_msg(MSGT_AO, MSGL_V, "AO: [pulse] Unsupported format, using default\n");
176 fmt_map = format_maps;
177 break;
179 fmt_map++;
181 ao_data.format = fmt_map->mp_format;
182 ss.format = fmt_map->pa_format;
184 if (!pa_sample_spec_valid(&ss)) {
185 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Invalid sample spec\n");
186 goto fail;
189 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
190 ao_data.bps = pa_bytes_per_second(&ss);
192 pa_cvolume_reset(&volume, ss.channels);
194 if (!(mainloop = pa_threaded_mainloop_new())) {
195 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate main loop\n");
196 goto fail;
199 if (!(context = pa_context_new(pa_threaded_mainloop_get_api(mainloop), PULSE_CLIENT_NAME))) {
200 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate context\n");
201 goto fail;
204 pa_context_set_state_callback(context, context_state_cb, NULL);
206 if (pa_context_connect(context, host, 0, NULL) < 0)
207 goto fail;
209 pa_threaded_mainloop_lock(mainloop);
211 if (pa_threaded_mainloop_start(mainloop) < 0)
212 goto unlock_and_fail;
214 /* Wait until the context is ready */
215 pa_threaded_mainloop_wait(mainloop);
217 if (pa_context_get_state(context) != PA_CONTEXT_READY)
218 goto unlock_and_fail;
220 if (!(stream = pa_stream_new(context, "audio stream", &ss, &map)))
221 goto unlock_and_fail;
223 pa_stream_set_state_callback(stream, stream_state_cb, NULL);
224 pa_stream_set_write_callback(stream, stream_request_cb, NULL);
225 pa_stream_set_latency_update_callback(stream, stream_latency_update_cb, NULL);
227 if (pa_stream_connect_playback(stream, sink, NULL, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, &volume, NULL) < 0)
228 goto unlock_and_fail;
230 /* Wait until the stream is ready */
231 pa_threaded_mainloop_wait(mainloop);
233 if (pa_stream_get_state(stream) != PA_STREAM_READY)
234 goto unlock_and_fail;
236 pa_threaded_mainloop_unlock(mainloop);
238 free(devarg);
239 return 1;
241 unlock_and_fail:
243 if (mainloop)
244 pa_threaded_mainloop_unlock(mainloop);
246 fail:
247 if (context)
248 GENERIC_ERR_MSG(context, "Init failed");
249 free(devarg);
250 uninit(1);
251 return 0;
254 /** Destroy libao driver */
255 static void uninit(int immed) {
256 if (stream && !immed) {
257 pa_threaded_mainloop_lock(mainloop);
258 waitop(pa_stream_drain(stream, success_cb, NULL));
261 if (mainloop)
262 pa_threaded_mainloop_stop(mainloop);
264 if (stream) {
265 pa_stream_disconnect(stream);
266 pa_stream_unref(stream);
267 stream = NULL;
270 if (context) {
271 pa_context_disconnect(context);
272 pa_context_unref(context);
273 context = NULL;
276 if (mainloop) {
277 pa_threaded_mainloop_free(mainloop);
278 mainloop = NULL;
282 /** Play the specified data to the pulseaudio server */
283 static int play(void* data, int len, int flags) {
284 pa_threaded_mainloop_lock(mainloop);
285 if (pa_stream_write(stream, data, len, NULL, 0, PA_SEEK_RELATIVE) < 0) {
286 GENERIC_ERR_MSG(context, "pa_stream_write() failed");
287 len = -1;
289 pa_threaded_mainloop_unlock(mainloop);
290 return len;
293 static void cork(int b) {
294 int success = 0;
295 pa_threaded_mainloop_lock(mainloop);
296 if (!waitop(pa_stream_cork(stream, b, success_cb, &success)) ||
297 !success)
298 GENERIC_ERR_MSG(context, "pa_stream_cork() failed");
301 /** Pause the audio stream by corking it on the server */
302 static void audio_pause(void) {
303 cork(1);
306 /** Resume the audio stream by uncorking it on the server */
307 static void audio_resume(void) {
308 // without this, certain versions will cause an infinite hang because
309 // pa_stream_writable_size returns 0 always.
310 // Note that this workaround causes A-V desync after pause
311 if (broken_pause) reset();
312 cork(0);
315 /** Reset the audio stream, i.e. flush the playback buffer on the server side */
316 static void reset(void) {
317 int success = 0;
318 pa_threaded_mainloop_lock(mainloop);
319 if (!waitop(pa_stream_flush(stream, success_cb, &success)) ||
320 !success)
321 GENERIC_ERR_MSG(context, "pa_stream_flush() failed");
324 /** Return number of bytes that may be written to the server without blocking */
325 static int get_space(void) {
326 size_t l;
327 pa_threaded_mainloop_lock(mainloop);
328 l = pa_stream_writable_size(stream);
329 pa_threaded_mainloop_unlock(mainloop);
330 return l;
333 /** Return the current latency in seconds */
334 static float get_delay(void) {
335 pa_usec_t latency = (pa_usec_t) -1;
336 pa_threaded_mainloop_lock(mainloop);
337 while (pa_stream_get_latency(stream, &latency, NULL) < 0) {
338 if (pa_context_errno(context) != PA_ERR_NODATA) {
339 GENERIC_ERR_MSG(context, "pa_stream_get_latency() failed");
340 break;
342 /* Wait until latency data is available again */
343 pa_threaded_mainloop_wait(mainloop);
345 pa_threaded_mainloop_unlock(mainloop);
346 return latency == (pa_usec_t) -1 ? 0 : latency / 1000000.0;
349 /** A callback function that is called when the
350 * pa_context_get_sink_input_info() operation completes. Saves the
351 * volume field of the specified structure to the global variable volume. */
352 static void info_func(struct pa_context *c, const struct pa_sink_input_info *i, int is_last, void *userdata) {
353 if (is_last < 0) {
354 GENERIC_ERR_MSG(context, "Failed to get sink input info");
355 return;
357 if (!i)
358 return;
359 volume = i->volume;
360 pa_threaded_mainloop_signal(mainloop, 0);
363 static int control(int cmd, void *arg) {
364 switch (cmd) {
365 case AOCONTROL_GET_VOLUME: {
366 ao_control_vol_t *vol = arg;
367 uint32_t devidx = pa_stream_get_index(stream);
368 pa_threaded_mainloop_lock(mainloop);
369 if (!waitop(pa_context_get_sink_input_info(context, devidx, info_func, NULL))) {
370 GENERIC_ERR_MSG(context, "pa_stream_get_sink_input_info() failed");
371 return CONTROL_ERROR;
374 if (volume.channels != 2)
375 vol->left = vol->right = pa_cvolume_avg(&volume)*100/PA_VOLUME_NORM;
376 else {
377 vol->left = volume.values[0]*100/PA_VOLUME_NORM;
378 vol->right = volume.values[1]*100/PA_VOLUME_NORM;
381 return CONTROL_OK;
384 case AOCONTROL_SET_VOLUME: {
385 const ao_control_vol_t *vol = arg;
386 pa_operation *o;
388 if (volume.channels != 2)
389 pa_cvolume_set(&volume, volume.channels, (pa_volume_t)vol->left*PA_VOLUME_NORM/100);
390 else {
391 volume.values[0] = (pa_volume_t)vol->left*PA_VOLUME_NORM/100;
392 volume.values[1] = (pa_volume_t)vol->right*PA_VOLUME_NORM/100;
395 pa_threaded_mainloop_lock(mainloop);
396 o = pa_context_set_sink_input_volume(context, pa_stream_get_index(stream), &volume, NULL, NULL);
397 if (!o) {
398 pa_threaded_mainloop_unlock(mainloop);
399 GENERIC_ERR_MSG(context, "pa_context_set_sink_input_volume() failed");
400 return CONTROL_ERROR;
402 /* We don't wait for completion here */
403 pa_operation_unref(o);
404 pa_threaded_mainloop_unlock(mainloop);
405 return CONTROL_OK;
408 default:
409 return CONTROL_UNKNOWN;