demux_lavf: try harder to make up a frame rate
[mplayer.git] / libao2 / ao_pulse.c
blobed6e08286a461abfc3b24d6880eab4e9ae304f98
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 <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
27 #include <pulse/pulseaudio.h>
29 #include "config.h"
30 #include "libaf/af_format.h"
31 #include "mp_msg.h"
32 #include "audio_out.h"
33 #include "input/input.h"
35 #define PULSE_CLIENT_NAME "mplayer2"
38 struct priv {
39 // PulseAudio playback stream object
40 struct pa_stream *stream;
42 // PulseAudio connection context
43 struct pa_context *context;
45 // Main event loop object
46 struct pa_threaded_mainloop *mainloop;
48 // temporary during control()
49 struct pa_sink_input_info pi;
51 bool broken_pause;
52 int retval;
53 bool did_reset;
56 #define GENERIC_ERR_MSG(ctx, str) \
57 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] "str": %s\n", \
58 pa_strerror(pa_context_errno(ctx)))
60 static void context_state_cb(pa_context *c, void *userdata)
62 struct ao *ao = userdata;
63 struct priv *priv = ao->priv;
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(priv->mainloop, 0);
69 break;
73 static void stream_state_cb(pa_stream *s, void *userdata)
75 struct ao *ao = userdata;
76 struct priv *priv = ao->priv;
77 switch (pa_stream_get_state(s)) {
78 case PA_STREAM_READY:
79 case PA_STREAM_FAILED:
80 case PA_STREAM_TERMINATED:
81 pa_threaded_mainloop_signal(priv->mainloop, 0);
82 break;
86 static void stream_request_cb(pa_stream *s, size_t length, void *userdata)
88 struct ao *ao = userdata;
89 struct priv *priv = ao->priv;
90 mp_input_wakeup(ao->input_ctx);
91 pa_threaded_mainloop_signal(priv->mainloop, 0);
94 static void stream_latency_update_cb(pa_stream *s, void *userdata)
96 struct ao *ao = userdata;
97 struct priv *priv = ao->priv;
98 pa_threaded_mainloop_signal(priv->mainloop, 0);
101 static void success_cb(pa_stream *s, int success, void *userdata)
103 struct ao *ao = userdata;
104 struct priv *priv = ao->priv;
105 priv->retval = success;
106 pa_threaded_mainloop_signal(priv->mainloop, 0);
110 * \brief waits for a pulseaudio operation to finish, frees it and
111 * unlocks the mainloop
112 * \param op operation to wait for
113 * \return 1 if operation has finished normally (DONE state), 0 otherwise
115 static int waitop(struct priv *priv, pa_operation *op)
117 if (!op) {
118 pa_threaded_mainloop_unlock(priv->mainloop);
119 return 0;
121 pa_operation_state_t state = pa_operation_get_state(op);
122 while (state == PA_OPERATION_RUNNING) {
123 pa_threaded_mainloop_wait(priv->mainloop);
124 state = pa_operation_get_state(op);
126 pa_operation_unref(op);
127 pa_threaded_mainloop_unlock(priv->mainloop);
128 return state == PA_OPERATION_DONE;
131 static const struct format_map {
132 int mp_format;
133 pa_sample_format_t pa_format;
134 } format_maps[] = {
135 {AF_FORMAT_S16_LE, PA_SAMPLE_S16LE},
136 {AF_FORMAT_S16_BE, PA_SAMPLE_S16BE},
137 {AF_FORMAT_S32_LE, PA_SAMPLE_S32LE},
138 {AF_FORMAT_S32_BE, PA_SAMPLE_S32BE},
139 {AF_FORMAT_FLOAT_LE, PA_SAMPLE_FLOAT32LE},
140 {AF_FORMAT_FLOAT_BE, PA_SAMPLE_FLOAT32BE},
141 {AF_FORMAT_U8, PA_SAMPLE_U8},
142 {AF_FORMAT_MU_LAW, PA_SAMPLE_ULAW},
143 {AF_FORMAT_A_LAW, PA_SAMPLE_ALAW},
144 {AF_FORMAT_UNKNOWN, 0}
147 static void uninit(struct ao *ao, bool cut_audio)
149 struct priv *priv = ao->priv;
150 if (priv->stream && !cut_audio) {
151 pa_threaded_mainloop_lock(priv->mainloop);
152 waitop(priv, pa_stream_drain(priv->stream, success_cb, ao));
155 if (priv->mainloop)
156 pa_threaded_mainloop_stop(priv->mainloop);
158 if (priv->stream) {
159 pa_stream_disconnect(priv->stream);
160 pa_stream_unref(priv->stream);
161 priv->stream = NULL;
164 if (priv->context) {
165 pa_context_disconnect(priv->context);
166 pa_context_unref(priv->context);
167 priv->context = NULL;
170 if (priv->mainloop) {
171 pa_threaded_mainloop_free(priv->mainloop);
172 priv->mainloop = NULL;
176 static int init(struct ao *ao, char *params)
178 struct pa_sample_spec ss;
179 struct pa_channel_map map;
180 char *devarg = NULL;
181 char *host = NULL;
182 char *sink = NULL;
183 const char *version = pa_get_library_version();
185 struct priv *priv = talloc_zero(ao, struct priv);
186 ao->priv = priv;
188 if (params) {
189 devarg = strdup(ao_subdevice);
190 sink = strchr(devarg, ':');
191 if (sink)
192 *sink++ = 0;
193 if (devarg[0])
194 host = devarg;
197 priv->broken_pause = false;
198 /* not sure which versions are affected, assume 0.9.11* to 0.9.14*
199 * known bad: 0.9.14, 0.9.13
200 * known good: 0.9.9, 0.9.10, 0.9.15
201 * To test: pause, wait ca. 5 seconds, framestep and see if MPlayer
202 * hangs somewhen. */
203 if (strncmp(version, "0.9.1", 5) == 0 && version[5] >= '1'
204 && version[5] <= '4') {
205 mp_msg(MSGT_AO, MSGL_WARN,
206 "[pulse] working around probably broken pause functionality,\n"
207 " see http://www.pulseaudio.org/ticket/440\n");
208 priv->broken_pause = true;
211 ss.channels = ao->channels;
212 ss.rate = ao->samplerate;
214 const struct format_map *fmt_map = format_maps;
215 while (fmt_map->mp_format != ao->format) {
216 if (fmt_map->mp_format == AF_FORMAT_UNKNOWN) {
217 mp_msg(MSGT_AO, MSGL_V,
218 "AO: [pulse] Unsupported format, using default\n");
219 fmt_map = format_maps;
220 break;
222 fmt_map++;
224 ao->format = fmt_map->mp_format;
225 ss.format = fmt_map->pa_format;
227 if (!pa_sample_spec_valid(&ss)) {
228 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Invalid sample spec\n");
229 goto fail;
232 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
233 ao->bps = pa_bytes_per_second(&ss);
235 if (!(priv->mainloop = pa_threaded_mainloop_new())) {
236 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate main loop\n");
237 goto fail;
240 if (!(priv->context = pa_context_new(pa_threaded_mainloop_get_api(
241 priv->mainloop), PULSE_CLIENT_NAME))) {
242 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate context\n");
243 goto fail;
246 pa_context_set_state_callback(priv->context, context_state_cb, ao);
248 if (pa_context_connect(priv->context, host, 0, NULL) < 0)
249 goto fail;
251 pa_threaded_mainloop_lock(priv->mainloop);
253 if (pa_threaded_mainloop_start(priv->mainloop) < 0)
254 goto unlock_and_fail;
256 /* Wait until the context is ready */
257 pa_threaded_mainloop_wait(priv->mainloop);
259 if (pa_context_get_state(priv->context) != PA_CONTEXT_READY)
260 goto unlock_and_fail;
262 if (!(priv->stream = pa_stream_new(priv->context, "audio stream", &ss,
263 &map)))
264 goto unlock_and_fail;
266 pa_stream_set_state_callback(priv->stream, stream_state_cb, ao);
267 pa_stream_set_write_callback(priv->stream, stream_request_cb, ao);
268 pa_stream_set_latency_update_callback(priv->stream,
269 stream_latency_update_cb, ao);
270 pa_buffer_attr bufattr = {
271 .maxlength = -1,
272 .tlength = pa_usec_to_bytes(1000000, &ss),
273 .prebuf = -1,
274 .minreq = -1,
275 .fragsize = -1,
277 if (pa_stream_connect_playback(priv->stream, sink, &bufattr,
278 PA_STREAM_INTERPOLATE_TIMING
279 | PA_STREAM_AUTO_TIMING_UPDATE, NULL,
280 NULL) < 0)
281 goto unlock_and_fail;
283 /* Wait until the stream is ready */
284 pa_threaded_mainloop_wait(priv->mainloop);
286 if (pa_stream_get_state(priv->stream) != PA_STREAM_READY)
287 goto unlock_and_fail;
289 pa_threaded_mainloop_unlock(priv->mainloop);
291 free(devarg);
292 return 0;
294 unlock_and_fail:
296 if (priv->mainloop)
297 pa_threaded_mainloop_unlock(priv->mainloop);
299 fail:
300 if (priv->context)
301 GENERIC_ERR_MSG(priv->context, "Init failed");
302 free(devarg);
303 uninit(ao, true);
304 return -1;
307 static void cork(struct ao *ao, bool pause)
309 struct priv *priv = ao->priv;
310 pa_threaded_mainloop_lock(priv->mainloop);
311 priv->retval = 0;
312 if (!waitop(priv, pa_stream_cork(priv->stream, pause, success_cb, ao)) ||
313 !priv->retval)
314 GENERIC_ERR_MSG(priv->context, "pa_stream_cork() failed");
317 // Play the specified data to the pulseaudio server
318 static int play(struct ao *ao, void *data, int len, int flags)
320 struct priv *priv = ao->priv;
321 /* For some reason Pulseaudio behaves worse if this is done after
322 * the write - rapidly repeated seeks result in bogus increasing
323 * reported latency. */
324 if (priv->did_reset)
325 cork(ao, false);
326 pa_threaded_mainloop_lock(priv->mainloop);
327 if (pa_stream_write(priv->stream, data, len, NULL, 0,
328 PA_SEEK_RELATIVE) < 0) {
329 GENERIC_ERR_MSG(priv->context, "pa_stream_write() failed");
330 len = -1;
332 if (priv->did_reset) {
333 priv->did_reset = false;
334 if (!waitop(priv, pa_stream_update_timing_info(priv->stream,
335 success_cb, ao))
336 || !priv->retval)
337 GENERIC_ERR_MSG(priv->context, "pa_stream_UPP() failed");
338 } else
339 pa_threaded_mainloop_unlock(priv->mainloop);
340 return len;
343 // Reset the audio stream, i.e. flush the playback buffer on the server side
344 static void reset(struct ao *ao)
346 // pa_stream_flush() works badly if not corked
347 cork(ao, true);
348 struct priv *priv = ao->priv;
349 pa_threaded_mainloop_lock(priv->mainloop);
350 priv->retval = 0;
351 if (!waitop(priv, pa_stream_flush(priv->stream, success_cb, ao)) ||
352 !priv->retval)
353 GENERIC_ERR_MSG(priv->context, "pa_stream_flush() failed");
354 priv->did_reset = true;
357 // Pause the audio stream by corking it on the server
358 static void pause(struct ao *ao)
360 cork(ao, true);
363 // Resume the audio stream by uncorking it on the server
364 static void resume(struct ao *ao)
366 struct priv *priv = ao->priv;
367 if (priv->did_reset)
368 return;
369 /* Without this, certain versions will cause an infinite hang because
370 * pa_stream_writable_size returns 0 always.
371 * Note that this workaround causes A-V desync after pause. */
372 if (priv->broken_pause)
373 reset(ao);
374 cork(ao, false);
377 // Return number of bytes that may be written to the server without blocking
378 static int get_space(struct ao *ao)
380 struct priv *priv = ao->priv;
381 pa_threaded_mainloop_lock(priv->mainloop);
382 size_t space = pa_stream_writable_size(priv->stream);
383 pa_threaded_mainloop_unlock(priv->mainloop);
384 return space;
387 // Return the current latency in seconds
388 static float get_delay(struct ao *ao)
390 struct priv *priv = ao->priv;
391 pa_usec_t latency = (pa_usec_t) -1;
392 pa_threaded_mainloop_lock(priv->mainloop);
393 while (pa_stream_get_latency(priv->stream, &latency, NULL) < 0) {
394 if (pa_context_errno(priv->context) != PA_ERR_NODATA) {
395 GENERIC_ERR_MSG(priv->context, "pa_stream_get_latency() failed");
396 break;
398 /* Wait until latency data is available again */
399 pa_threaded_mainloop_wait(priv->mainloop);
401 pa_threaded_mainloop_unlock(priv->mainloop);
402 return latency == (pa_usec_t) -1 ? 0 : latency / 1000000.0;
405 /* A callback function that is called when the
406 * pa_context_get_sink_input_info() operation completes. Saves the
407 * volume field of the specified structure to the global variable volume.
409 static void info_func(struct pa_context *c, const struct pa_sink_input_info *i,
410 int is_last, void *userdata)
412 struct ao *ao = userdata;
413 struct priv *priv = ao->priv;
414 if (is_last < 0) {
415 GENERIC_ERR_MSG(priv->context, "Failed to get sink input info");
416 return;
418 if (!i)
419 return;
420 priv->pi = *i;
421 pa_threaded_mainloop_signal(priv->mainloop, 0);
424 static int control(struct ao *ao, enum aocontrol cmd, void *arg)
426 struct priv *priv = ao->priv;
427 switch (cmd) {
428 case AOCONTROL_GET_MUTE:
429 case AOCONTROL_GET_VOLUME: {
430 uint32_t devidx = pa_stream_get_index(priv->stream);
431 pa_threaded_mainloop_lock(priv->mainloop);
432 if (!waitop(priv, pa_context_get_sink_input_info(priv->context, devidx,
433 info_func, ao))) {
434 GENERIC_ERR_MSG(priv->context,
435 "pa_stream_get_sink_input_info() failed");
436 return CONTROL_ERROR;
438 // Warning: some information in pi might be unaccessible, because
439 // we naively copied the struct, without updating pointers etc.
440 // Pointers might point to invalid data, accessors might fail.
441 if (cmd == AOCONTROL_GET_VOLUME) {
442 ao_control_vol_t *vol = arg;
443 if (priv->pi.volume.channels != 2)
444 vol->left = vol->right =
445 pa_cvolume_avg(&priv->pi.volume) * 100 / PA_VOLUME_NORM;
446 else {
447 vol->left = priv->pi.volume.values[0] * 100 / PA_VOLUME_NORM;
448 vol->right = priv->pi.volume.values[1] * 100 / PA_VOLUME_NORM;
450 } else if (cmd == AOCONTROL_GET_MUTE) {
451 bool *mute = arg;
452 *mute = priv->pi.mute;
454 return CONTROL_OK;
457 case AOCONTROL_SET_MUTE:
458 case AOCONTROL_SET_VOLUME: {
459 pa_operation *o;
461 pa_threaded_mainloop_lock(priv->mainloop);
462 uint32_t stream_index = pa_stream_get_index(priv->stream);
463 if (cmd == AOCONTROL_SET_VOLUME) {
464 const ao_control_vol_t *vol = arg;
465 struct pa_cvolume volume;
467 pa_cvolume_reset(&volume, ao->channels);
468 if (volume.channels != 2)
469 pa_cvolume_set(&volume, volume.channels,
470 vol->left * PA_VOLUME_NORM / 100);
471 else {
472 volume.values[0] = vol->left * PA_VOLUME_NORM / 100;
473 volume.values[1] = vol->right * PA_VOLUME_NORM / 100;
475 o = pa_context_set_sink_input_volume(priv->context, stream_index,
476 &volume, NULL, NULL);
477 if (!o) {
478 pa_threaded_mainloop_unlock(priv->mainloop);
479 GENERIC_ERR_MSG(priv->context,
480 "pa_context_set_sink_input_volume() failed");
481 return CONTROL_ERROR;
483 } else if (cmd == AOCONTROL_SET_MUTE) {
484 const bool *mute = arg;
485 o = pa_context_set_sink_input_mute(priv->context, stream_index,
486 *mute, NULL, NULL);
487 if (!o) {
488 pa_threaded_mainloop_unlock(priv->mainloop);
489 GENERIC_ERR_MSG(priv->context,
490 "pa_context_set_sink_input_mute() failed");
491 return CONTROL_ERROR;
493 } else
494 abort();
495 /* We don't wait for completion here */
496 pa_operation_unref(o);
497 pa_threaded_mainloop_unlock(priv->mainloop);
498 return CONTROL_OK;
500 default:
501 return CONTROL_UNKNOWN;
505 const struct ao_driver audio_out_pulse = {
506 .is_new = true,
507 .info = &(const struct ao_info) {
508 "PulseAudio audio output",
509 "pulse",
510 "Lennart Poettering",
513 .control = control,
514 .init = init,
515 .uninit = uninit,
516 .reset = reset,
517 .get_space = get_space,
518 .play = play,
519 .get_delay = get_delay,
520 .pause = pause,
521 .resume = resume,