ao_pulse, core: make pulse thread wake up core for more data
[mplayer.git] / libao2 / ao_pulse.c
blobddaf35716c7a732b0cdd80978c7274de53eb6e32
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 struct pa_cvolume volume;
50 bool broken_pause;
51 int retval;
54 #define GENERIC_ERR_MSG(ctx, str) \
55 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] "str": %s\n", \
56 pa_strerror(pa_context_errno(ctx)))
58 static void context_state_cb(pa_context *c, void *userdata)
60 struct ao *ao = userdata;
61 struct priv *priv = ao->priv;
62 switch (pa_context_get_state(c)) {
63 case PA_CONTEXT_READY:
64 case PA_CONTEXT_TERMINATED:
65 case PA_CONTEXT_FAILED:
66 pa_threaded_mainloop_signal(priv->mainloop, 0);
67 break;
71 static void stream_state_cb(pa_stream *s, void *userdata)
73 struct ao *ao = userdata;
74 struct priv *priv = ao->priv;
75 switch (pa_stream_get_state(s)) {
76 case PA_STREAM_READY:
77 case PA_STREAM_FAILED:
78 case PA_STREAM_TERMINATED:
79 pa_threaded_mainloop_signal(priv->mainloop, 0);
80 break;
84 static void stream_request_cb(pa_stream *s, size_t length, void *userdata)
86 struct ao *ao = userdata;
87 struct priv *priv = ao->priv;
88 mp_input_wakeup(ao->input_ctx);
89 pa_threaded_mainloop_signal(priv->mainloop, 0);
92 static void stream_latency_update_cb(pa_stream *s, void *userdata)
94 struct ao *ao = userdata;
95 struct priv *priv = ao->priv;
96 pa_threaded_mainloop_signal(priv->mainloop, 0);
99 static void success_cb(pa_stream *s, int success, void *userdata)
101 struct ao *ao = userdata;
102 struct priv *priv = ao->priv;
103 priv->retval = success;
104 pa_threaded_mainloop_signal(priv->mainloop, 0);
108 * \brief waits for a pulseaudio operation to finish, frees it and
109 * unlocks the mainloop
110 * \param op operation to wait for
111 * \return 1 if operation has finished normally (DONE state), 0 otherwise
113 static int waitop(struct priv *priv, pa_operation *op)
115 if (!op) {
116 pa_threaded_mainloop_unlock(priv->mainloop);
117 return 0;
119 pa_operation_state_t state = pa_operation_get_state(op);
120 while (state == PA_OPERATION_RUNNING) {
121 pa_threaded_mainloop_wait(priv->mainloop);
122 state = pa_operation_get_state(op);
124 pa_operation_unref(op);
125 pa_threaded_mainloop_unlock(priv->mainloop);
126 return state == PA_OPERATION_DONE;
129 static const struct format_map {
130 int mp_format;
131 pa_sample_format_t pa_format;
132 } format_maps[] = {
133 {AF_FORMAT_S16_LE, PA_SAMPLE_S16LE},
134 {AF_FORMAT_S16_BE, PA_SAMPLE_S16BE},
135 {AF_FORMAT_S32_LE, PA_SAMPLE_S32LE},
136 {AF_FORMAT_S32_BE, PA_SAMPLE_S32BE},
137 {AF_FORMAT_FLOAT_LE, PA_SAMPLE_FLOAT32LE},
138 {AF_FORMAT_FLOAT_BE, PA_SAMPLE_FLOAT32BE},
139 {AF_FORMAT_U8, PA_SAMPLE_U8},
140 {AF_FORMAT_MU_LAW, PA_SAMPLE_ULAW},
141 {AF_FORMAT_A_LAW, PA_SAMPLE_ALAW},
142 {AF_FORMAT_UNKNOWN, 0}
145 static void uninit(struct ao *ao, bool cut_audio)
147 struct priv *priv = ao->priv;
148 if (priv->stream && !cut_audio) {
149 pa_threaded_mainloop_lock(priv->mainloop);
150 waitop(priv, pa_stream_drain(priv->stream, success_cb, ao));
153 if (priv->mainloop)
154 pa_threaded_mainloop_stop(priv->mainloop);
156 if (priv->stream) {
157 pa_stream_disconnect(priv->stream);
158 pa_stream_unref(priv->stream);
159 priv->stream = NULL;
162 if (priv->context) {
163 pa_context_disconnect(priv->context);
164 pa_context_unref(priv->context);
165 priv->context = NULL;
168 if (priv->mainloop) {
169 pa_threaded_mainloop_free(priv->mainloop);
170 priv->mainloop = NULL;
174 static int init(struct ao *ao, char *params)
176 struct pa_sample_spec ss;
177 struct pa_channel_map map;
178 char *devarg = NULL;
179 char *host = NULL;
180 char *sink = NULL;
181 const char *version = pa_get_library_version();
183 struct priv *priv = talloc_zero(ao, struct priv);
184 ao->priv = priv;
186 if (params) {
187 devarg = strdup(ao_subdevice);
188 sink = strchr(devarg, ':');
189 if (sink)
190 *sink++ = 0;
191 if (devarg[0])
192 host = devarg;
195 priv->broken_pause = false;
196 /* not sure which versions are affected, assume 0.9.11* to 0.9.14*
197 * known bad: 0.9.14, 0.9.13
198 * known good: 0.9.9, 0.9.10, 0.9.15
199 * To test: pause, wait ca. 5 seconds, framestep and see if MPlayer
200 * hangs somewhen. */
201 if (strncmp(version, "0.9.1", 5) == 0 && version[5] >= '1'
202 && version[5] <= '4') {
203 mp_msg(MSGT_AO, MSGL_WARN,
204 "[pulse] working around probably broken pause functionality,\n"
205 " see http://www.pulseaudio.org/ticket/440\n");
206 priv->broken_pause = true;
209 ss.channels = ao->channels;
210 ss.rate = ao->samplerate;
212 const struct format_map *fmt_map = format_maps;
213 while (fmt_map->mp_format != ao->format) {
214 if (fmt_map->mp_format == AF_FORMAT_UNKNOWN) {
215 mp_msg(MSGT_AO, MSGL_V,
216 "AO: [pulse] Unsupported format, using default\n");
217 fmt_map = format_maps;
218 break;
220 fmt_map++;
222 ao->format = fmt_map->mp_format;
223 ss.format = fmt_map->pa_format;
225 if (!pa_sample_spec_valid(&ss)) {
226 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Invalid sample spec\n");
227 goto fail;
230 pa_channel_map_init_auto(&map, ss.channels, PA_CHANNEL_MAP_ALSA);
231 ao->bps = pa_bytes_per_second(&ss);
233 if (!(priv->mainloop = pa_threaded_mainloop_new())) {
234 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate main loop\n");
235 goto fail;
238 if (!(priv->context = pa_context_new(pa_threaded_mainloop_get_api(
239 priv->mainloop), PULSE_CLIENT_NAME))) {
240 mp_msg(MSGT_AO, MSGL_ERR, "AO: [pulse] Failed to allocate context\n");
241 goto fail;
244 pa_context_set_state_callback(priv->context, context_state_cb, ao);
246 if (pa_context_connect(priv->context, host, 0, NULL) < 0)
247 goto fail;
249 pa_threaded_mainloop_lock(priv->mainloop);
251 if (pa_threaded_mainloop_start(priv->mainloop) < 0)
252 goto unlock_and_fail;
254 /* Wait until the context is ready */
255 pa_threaded_mainloop_wait(priv->mainloop);
257 if (pa_context_get_state(priv->context) != PA_CONTEXT_READY)
258 goto unlock_and_fail;
260 if (!(priv->stream = pa_stream_new(priv->context, "audio stream", &ss,
261 &map)))
262 goto unlock_and_fail;
264 pa_stream_set_state_callback(priv->stream, stream_state_cb, ao);
265 pa_stream_set_write_callback(priv->stream, stream_request_cb, ao);
266 pa_stream_set_latency_update_callback(priv->stream,
267 stream_latency_update_cb, ao);
268 pa_buffer_attr bufattr = {
269 .maxlength = -1,
270 .tlength = pa_usec_to_bytes(1000000, &ss),
271 .prebuf = -1,
272 .minreq = -1,
273 .fragsize = -1,
275 if (pa_stream_connect_playback(priv->stream, sink, &bufattr,
276 PA_STREAM_INTERPOLATE_TIMING
277 | PA_STREAM_AUTO_TIMING_UPDATE, NULL,
278 NULL) < 0)
279 goto unlock_and_fail;
281 /* Wait until the stream is ready */
282 pa_threaded_mainloop_wait(priv->mainloop);
284 if (pa_stream_get_state(priv->stream) != PA_STREAM_READY)
285 goto unlock_and_fail;
287 pa_threaded_mainloop_unlock(priv->mainloop);
289 free(devarg);
290 return 0;
292 unlock_and_fail:
294 if (priv->mainloop)
295 pa_threaded_mainloop_unlock(priv->mainloop);
297 fail:
298 if (priv->context)
299 GENERIC_ERR_MSG(priv->context, "Init failed");
300 free(devarg);
301 uninit(ao, true);
302 return -1;
305 static void cork(struct ao *ao, bool pause)
307 struct priv *priv = ao->priv;
308 pa_threaded_mainloop_lock(priv->mainloop);
309 priv->retval = 0;
310 if (!waitop(priv, pa_stream_cork(priv->stream, pause, success_cb, ao)) ||
311 !priv->retval)
312 GENERIC_ERR_MSG(priv->context, "pa_stream_cork() failed");
315 // Play the specified data to the pulseaudio server
316 static int play(struct ao *ao, void *data, int len, int flags)
318 struct priv *priv = ao->priv;
319 pa_threaded_mainloop_lock(priv->mainloop);
320 if (pa_stream_write(priv->stream, data, len, NULL, 0,
321 PA_SEEK_RELATIVE) < 0) {
322 GENERIC_ERR_MSG(priv->context, "pa_stream_write() failed");
323 len = -1;
325 pa_threaded_mainloop_unlock(priv->mainloop);
326 return len;
329 // Reset the audio stream, i.e. flush the playback buffer on the server side
330 static void reset(struct ao *ao)
332 struct priv *priv = ao->priv;
333 pa_threaded_mainloop_lock(priv->mainloop);
334 priv->retval = 0;
335 if (!waitop(priv, pa_stream_flush(priv->stream, success_cb, ao)) ||
336 !priv->retval)
337 GENERIC_ERR_MSG(priv->context, "pa_stream_flush() failed");
340 // Pause the audio stream by corking it on the server
341 static void pause(struct ao *ao)
343 cork(ao, true);
346 // Resume the audio stream by uncorking it on the server
347 static void resume(struct ao *ao)
349 struct priv *priv = ao->priv;
350 /* Without this, certain versions will cause an infinite hang because
351 * pa_stream_writable_size returns 0 always.
352 * Note that this workaround causes A-V desync after pause. */
353 if (priv->broken_pause)
354 reset(ao);
355 cork(ao, false);
358 // Return number of bytes that may be written to the server without blocking
359 static int get_space(struct ao *ao)
361 struct priv *priv = ao->priv;
362 pa_threaded_mainloop_lock(priv->mainloop);
363 size_t space = pa_stream_writable_size(priv->stream);
364 pa_threaded_mainloop_unlock(priv->mainloop);
365 return space;
368 // Return the current latency in seconds
369 static float get_delay(struct ao *ao)
371 struct priv *priv = ao->priv;
372 pa_usec_t latency = (pa_usec_t) -1;
373 pa_threaded_mainloop_lock(priv->mainloop);
374 while (pa_stream_get_latency(priv->stream, &latency, NULL) < 0) {
375 if (pa_context_errno(priv->context) != PA_ERR_NODATA) {
376 GENERIC_ERR_MSG(priv->context, "pa_stream_get_latency() failed");
377 break;
379 /* Wait until latency data is available again */
380 pa_threaded_mainloop_wait(priv->mainloop);
382 pa_threaded_mainloop_unlock(priv->mainloop);
383 return latency == (pa_usec_t) -1 ? 0 : latency / 1000000.0;
386 /* A callback function that is called when the
387 * pa_context_get_sink_input_info() operation completes. Saves the
388 * volume field of the specified structure to the global variable volume.
390 static void info_func(struct pa_context *c, const struct pa_sink_input_info *i,
391 int is_last, void *userdata)
393 struct ao *ao = userdata;
394 struct priv *priv = ao->priv;
395 if (is_last < 0) {
396 GENERIC_ERR_MSG(priv->context, "Failed to get sink input info");
397 return;
399 if (!i)
400 return;
401 priv->volume = i->volume;
402 pa_threaded_mainloop_signal(priv->mainloop, 0);
405 static int control(struct ao *ao, int cmd, void *arg)
407 struct priv *priv = ao->priv;
408 switch (cmd) {
409 case AOCONTROL_GET_VOLUME: {
410 ao_control_vol_t *vol = arg;
411 uint32_t devidx = pa_stream_get_index(priv->stream);
412 pa_threaded_mainloop_lock(priv->mainloop);
413 if (!waitop(priv, pa_context_get_sink_input_info(priv->context, devidx,
414 info_func, ao))) {
415 GENERIC_ERR_MSG(priv->context,
416 "pa_stream_get_sink_input_info() failed");
417 return CONTROL_ERROR;
419 if (priv->volume.channels != 2)
420 vol->left = vol->right =
421 pa_cvolume_avg(&priv->volume) * 100 / PA_VOLUME_NORM;
422 else {
423 vol->left = priv->volume.values[0] * 100 / PA_VOLUME_NORM;
424 vol->right = priv->volume.values[1] * 100 / PA_VOLUME_NORM;
426 return CONTROL_OK;
428 case AOCONTROL_SET_VOLUME: {
429 const ao_control_vol_t *vol = arg;
430 pa_operation *o;
431 struct pa_cvolume volume;
433 pa_cvolume_reset(&volume, ao->channels);
434 if (volume.channels != 2)
435 pa_cvolume_set(&volume, volume.channels,
436 (pa_volume_t)vol->left*PA_VOLUME_NORM/100);
437 else {
438 volume.values[0] = vol->left * PA_VOLUME_NORM / 100;
439 volume.values[1] = vol->right * PA_VOLUME_NORM / 100;
441 pa_threaded_mainloop_lock(priv->mainloop);
442 o = pa_context_set_sink_input_volume(priv->context,
443 pa_stream_get_index(priv->stream),
444 &volume, NULL, NULL);
445 if (!o) {
446 pa_threaded_mainloop_unlock(priv->mainloop);
447 GENERIC_ERR_MSG(priv->context,
448 "pa_context_set_sink_input_volume() failed");
449 return CONTROL_ERROR;
451 /* We don't wait for completion here */
452 pa_operation_unref(o);
453 pa_threaded_mainloop_unlock(priv->mainloop);
454 return CONTROL_OK;
456 default:
457 return CONTROL_UNKNOWN;
461 const struct ao_driver audio_out_pulse = {
462 .is_new = true,
463 .info = &(const struct ao_info) {
464 "PulseAudio audio output",
465 "pulse",
466 "Lennart Poettering",
469 .control = control,
470 .init = init,
471 .uninit = uninit,
472 .reset = reset,
473 .get_space = get_space,
474 .play = play,
475 .get_delay = get_delay,
476 .pause = pause,
477 .resume = resume,