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.
27 #include <pulse/pulseaudio.h>
30 #include "libaf/af_format.h"
32 #include "audio_out.h"
33 #include "input/input.h"
35 #define PULSE_CLIENT_NAME "mplayer2"
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
;
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);
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
)) {
77 case PA_STREAM_FAILED
:
78 case PA_STREAM_TERMINATED
:
79 pa_threaded_mainloop_signal(priv
->mainloop
, 0);
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
)
116 pa_threaded_mainloop_unlock(priv
->mainloop
);
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
{
131 pa_sample_format_t pa_format
;
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
));
154 pa_threaded_mainloop_stop(priv
->mainloop
);
157 pa_stream_disconnect(priv
->stream
);
158 pa_stream_unref(priv
->stream
);
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
;
181 const char *version
= pa_get_library_version();
183 struct priv
*priv
= talloc_zero(ao
, struct priv
);
187 devarg
= strdup(ao_subdevice
);
188 sink
= strchr(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
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
;
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");
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");
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");
244 pa_context_set_state_callback(priv
->context
, context_state_cb
, ao
);
246 if (pa_context_connect(priv
->context
, host
, 0, NULL
) < 0)
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
,
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
= {
270 .tlength
= pa_usec_to_bytes(1000000, &ss
),
275 if (pa_stream_connect_playback(priv
->stream
, sink
, &bufattr
,
276 PA_STREAM_INTERPOLATE_TIMING
277 | PA_STREAM_AUTO_TIMING_UPDATE
, NULL
,
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
);
295 pa_threaded_mainloop_unlock(priv
->mainloop
);
299 GENERIC_ERR_MSG(priv
->context
, "Init failed");
305 static void cork(struct ao
*ao
, bool pause
)
307 struct priv
*priv
= ao
->priv
;
308 pa_threaded_mainloop_lock(priv
->mainloop
);
310 if (!waitop(priv
, pa_stream_cork(priv
->stream
, pause
, success_cb
, ao
)) ||
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");
325 pa_threaded_mainloop_unlock(priv
->mainloop
);
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
);
335 if (!waitop(priv
, pa_stream_flush(priv
->stream
, success_cb
, ao
)) ||
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
)
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
)
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
);
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");
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
;
396 GENERIC_ERR_MSG(priv
->context
, "Failed to get sink input info");
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
;
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
,
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
;
423 vol
->left
= priv
->volume
.values
[0] * 100 / PA_VOLUME_NORM
;
424 vol
->right
= priv
->volume
.values
[1] * 100 / PA_VOLUME_NORM
;
428 case AOCONTROL_SET_VOLUME
: {
429 const ao_control_vol_t
*vol
= arg
;
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);
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
);
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
);
457 return CONTROL_UNKNOWN
;
461 const struct ao_driver audio_out_pulse
= {
463 .info
= &(const struct ao_info
) {
464 "PulseAudio audio output",
466 "Lennart Poettering",
473 .get_space
= get_space
,
475 .get_delay
= get_delay
,