1 /*****************************************************************************
2 * pulse.c : Pulseaudio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
5 * Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
7 * Authors: Martin Hamrle <hamrle @ post . cz>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
31 #include <vlc_aout_intf.h>
34 #include <pulse/pulseaudio.h>
36 static int Open ( vlc_object_t * );
37 static void Close ( vlc_object_t * );
40 set_shortname( "PulseAudio" )
41 set_description( N_("Pulseaudio audio output") )
42 set_capability( "audio output", 160 )
43 set_category( CAT_AUDIO )
44 set_subcategory( SUBCAT_AUDIO_AOUT )
45 add_shortcut( "pulseaudio", "pa" )
46 set_callbacks( Open, Close )
49 /* TODO: single static mainloop */
52 * Be careful what you do when the PulseAudio mainloop is held, which is to say
53 * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
54 * In particular, a VLC variable callback cannot be triggered nor deleted with
55 * the PulseAudio mainloop lock held, if the callback acquires the lock. */
59 pa_stream *stream; /**< PulseAudio playback stream object */
60 pa_context *context; /**< PulseAudio connection context */
61 pa_threaded_mainloop *mainloop; /**< PulseAudio event loop */
62 pa_volume_t base_volume; /**< 0dB reference volume */
63 pa_cvolume cvolume; /**< actual sink input volume */
64 //uint32_t byterate; /**< bytes per second */
68 static void context_state_cb(pa_context *c, void *userdata)
70 pa_threaded_mainloop *mainloop = userdata;
72 switch (pa_context_get_state(c)) {
73 case PA_CONTEXT_READY:
74 case PA_CONTEXT_FAILED:
75 case PA_CONTEXT_TERMINATED:
76 pa_threaded_mainloop_signal(mainloop, 0);
82 static bool context_wait(pa_threaded_mainloop *mainloop, pa_context *context)
84 pa_context_state_t state;
86 while ((state = pa_context_get_state(context)) != PA_CONTEXT_READY) {
87 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
89 pa_threaded_mainloop_wait(mainloop);
94 static void error(aout_instance_t *aout, const char *msg, pa_context *context)
96 msg_Err(aout, "%s: %s", msg, pa_strerror(pa_context_errno(context)));
100 static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
103 aout_instance_t *aout = userdata;
104 vlc_value_t val, text;
110 msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
112 val.i_int = i->index;
113 text.psz_string = (char *)i->description;
114 var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
117 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
120 aout_instance_t *aout = userdata;
121 aout_sys_t *sys = aout->output.p_sys;
127 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
128 * amplification and maximum hardware amplification.
129 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
130 * Thus we need to use the sink base_volume as a multiplier,
131 * if and only if flat volume is active for our current sink. */
132 if (i->flags & PA_SINK_FLAT_VOLUME)
133 sys->base_volume = i->base_volume;
135 sys->base_volume = PA_VOLUME_NORM;
136 msg_Dbg(aout, "base volume: %f", pa_sw_volume_to_linear(sys->base_volume));
140 static void stream_state_cb(pa_stream *s, void *userdata)
142 pa_threaded_mainloop *mainloop = userdata;
144 switch (pa_stream_get_state(s)) {
145 case PA_STREAM_READY:
146 case PA_STREAM_FAILED:
147 case PA_STREAM_TERMINATED:
148 pa_threaded_mainloop_signal(mainloop, 0);
154 static void stream_moved_cb(pa_stream *s, void *userdata)
156 aout_instance_t *aout = userdata;
157 aout_sys_t *sys = aout->output.p_sys;
159 uint32_t idx = pa_stream_get_device_index(s);
161 msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
162 pa_stream_get_device_name(s));
163 op = pa_context_get_sink_info_by_index(sys->context, idx,
165 if (likely(op != NULL))
166 pa_operation_unref(op);
168 /* Update the variable if someone else moved our stream */
169 var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
170 &(vlc_value_t){ .i_int = idx }, NULL);
173 static void stream_overflow_cb(pa_stream *s, void *userdata)
175 aout_instance_t *aout = userdata;
177 msg_Err(aout, "overflow");
181 static void stream_started_cb(pa_stream *s, void *userdata)
183 aout_instance_t *aout = userdata;
185 msg_Dbg(aout, "started");
189 static void stream_suspended_cb(pa_stream *s, void *userdata)
191 aout_instance_t *aout = userdata;
193 msg_Dbg(aout, "suspended");
197 static void stream_underflow_cb(pa_stream *s, void *userdata)
199 aout_instance_t *aout = userdata;
201 msg_Dbg(aout, "underflow");
205 static int stream_wait(pa_threaded_mainloop *mainloop, pa_stream *stream)
207 pa_stream_state_t state;
209 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
210 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
212 pa_threaded_mainloop_wait(mainloop);
217 /* Memory free callback. The block_t address is in front of the data. */
218 static void data_free(void *data)
220 block_t **pp = data, *block;
222 memcpy(&block, pp - 1, sizeof (block));
223 block_Release(block);
226 static void *data_convert(block_t **pp)
228 block_t *block = *pp;
229 /* In most cases, there is enough head room, and this is really cheap: */
230 block = block_Realloc(block, sizeof (block), block->i_buffer);
232 if (unlikely(block == NULL))
235 memcpy(block->p_buffer, &block, sizeof (block));
236 block->p_buffer += sizeof (block);
237 block->i_buffer -= sizeof (block);
238 return block->p_buffer;
241 /*****************************************************************************
242 * Play: play a sound samples buffer
243 *****************************************************************************/
244 static void Play(aout_instance_t *aout)
246 aout_sys_t *sys = aout->output.p_sys;
247 pa_stream *s = sys->stream;
249 /* Note: The core already holds the output FIFO lock at this point.
250 * Therefore we must not under any circumstances (try to) acquire the
251 * output FIFO lock while the PulseAudio threaded main loop lock is held
252 * (including from PulseAudio stream callbacks). Otherwise lock inversion
253 * will take place, and sooner or later a deadlock. */
254 pa_threaded_mainloop_lock(sys->mainloop);
256 if (pa_stream_is_corked(s) > 0) {
257 pa_operation *op = pa_stream_cork(s, 0, NULL, NULL);
259 pa_operation_unref(op);
260 msg_Dbg(aout, "uncorking");
264 /* This function should be called by the LibVLC core a header of time,
265 * but not more than AOUT_MAX_PREPARE. The PulseAudio latency should be
266 * shorter than that (though it might not be the case with some evil piece
267 * of audio output hardware). So we may need to trigger playback early,
268 * (that is to say, short cut the PulseAudio prebuffering). Otherwise,
269 * audio and video may be out of synchronization. */
272 if (pa_stream_get_latency(s, &latency, &negative) < 0) {
273 /* Especially at start of stream, latency may not be known (yet). */
274 if (pa_context_errno(sys->context) != PA_ERR_NODATA)
275 error(aout, "cannot determine latency", sys->context);
277 mtime_t gap = aout_FifoFirstDate(&aout->output.fifo) - mdate()
280 if (gap > AOUT_PTS_TOLERANCE)
281 msg_Dbg(aout, "buffer too early (%"PRId64" us)", gap);
282 else if (gap < -AOUT_PTS_TOLERANCE)
283 msg_Err(aout, "buffer too late (%"PRId64" us)", -gap);
286 #if 0 /* Fault injector to test underrun recovery */
287 static unsigned u = 0;
288 if ((++u % 500) == 0) {
289 msg_Err(aout, "fault injection");
290 msleep(CLOCK_FREQ*2);
294 /* This function is called exactly once per block in the output FIFO, so
295 * this for-loop is not necessary.
296 * If this function is changed to not always dequeue blocks, be sure to
297 * limit the queue size to a reasonable limit to avoid huge leaks. */
299 block_t *block = aout_FifoPop(&aout->output.fifo);
303 const void *ptr = data_convert(&block);
304 if (unlikely(ptr == NULL))
307 size_t len = block->i_buffer;
308 //mtime_t pts = block->i_pts, duration = block->i_length;
310 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0)
312 error(aout, "cannot write", sys->context);
313 block_Release(block);
317 pa_threaded_mainloop_unlock(sys->mainloop);
320 static void Pause(aout_instance_t *aout, bool b_paused, mtime_t i_date)
322 aout_sys_t *sys = aout->output.p_sys;
323 pa_stream *s = sys->stream;
325 /* Note: The core already holds the output FIFO lock at this point.
326 * Therefore we must not under any circumstances (try to) acquire the
327 * output FIFO lock while the PulseAudio threaded main loop lock is held
328 * (including from PulseAudio stream callbacks). Otherwise lock inversion
329 * will take place, and sooner or later a deadlock. */
330 pa_threaded_mainloop_lock(sys->mainloop);
332 pa_operation *op = pa_stream_cork(s, b_paused ? 1 : 0, NULL, NULL);
334 pa_operation_unref(op);
336 pa_threaded_mainloop_unlock(sys->mainloop);
339 static int VolumeSet(aout_instance_t *aout, audio_volume_t vol, bool mute)
341 aout_sys_t *sys = aout->output.p_sys;
342 pa_threaded_mainloop *mainloop = sys->mainloop;
345 uint32_t idx = pa_stream_get_index(sys->stream);
346 pa_volume_t volume = pa_sw_volume_from_linear(vol / (float)AOUT_VOLUME_DEFAULT);
349 /* TODO: do not ruin the channel balance (if set outside VLC) */
350 /* TODO: notify UI about volume changes by other PulseAudio clients */
351 pa_cvolume_set(&sys->cvolume, sys->cvolume.channels, volume);
352 pa_sw_cvolume_multiply_scalar(&cvolume, &sys->cvolume, sys->base_volume);
353 assert(pa_cvolume_valid(&cvolume));
355 pa_threaded_mainloop_lock(mainloop);
356 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
357 if (likely(op != NULL))
358 pa_operation_unref(op);
359 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
360 if (likely(op != NULL))
361 pa_operation_unref(op);
362 pa_threaded_mainloop_unlock(mainloop);
367 static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
368 vlc_value_t val, void *userdata)
370 aout_instance_t *aout = (aout_instance_t *)obj;
371 aout_sys_t *sys = aout->output.p_sys;
372 pa_stream *s = userdata;
374 uint32_t idx = pa_stream_get_index(s);
375 uint32_t sink_idx = val.i_int;
377 (void) varname; (void) old;
379 pa_threaded_mainloop_lock(sys->mainloop);
380 op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
382 if (likely(op != NULL)) {
383 pa_operation_unref(op);
384 msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
386 error(aout, "cannot move sink", sys->context);
387 pa_threaded_mainloop_unlock(sys->mainloop);
389 return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
393 /*****************************************************************************
394 * Open: open the audio device
395 *****************************************************************************/
396 static int Open(vlc_object_t *obj)
398 aout_instance_t *aout = (aout_instance_t *)obj;
401 /* Sample format specification */
402 struct pa_sample_spec ss;
404 switch(aout->output.output.i_format)
407 aout->output.output.i_format = VLC_CODEC_F32B;
409 ss.format = PA_SAMPLE_FLOAT32BE;
412 aout->output.output.i_format = VLC_CODEC_F32L;
414 ss.format = PA_SAMPLE_FLOAT32LE;
417 aout->output.output.i_format = VLC_CODEC_FL32;
418 ss.format = PA_SAMPLE_FLOAT32NE;
421 ss.format = PA_SAMPLE_S32BE;
424 ss.format = PA_SAMPLE_S32LE;
427 ss.format = PA_SAMPLE_S24BE;
430 ss.format = PA_SAMPLE_S24LE;
433 ss.format = PA_SAMPLE_S16BE;
436 ss.format = PA_SAMPLE_S16LE;
439 aout->output.output.i_format = VLC_CODEC_U8;
441 ss.format = PA_SAMPLE_U8;
446 aout->output.output.i_format = VLC_CODEC_FL32;
447 ss.format = PA_SAMPLE_FLOAT32NE;
451 aout->output.output.i_format = VLC_CODEC_S16N;
452 ss.format = PA_SAMPLE_S16NE;
457 ss.rate = aout->output.output.i_rate;
458 ss.channels = aout_FormatNbChannels(&aout->output.output);
459 if (!pa_sample_spec_valid(&ss)) {
460 msg_Err(aout, "unsupported sample specification");
464 /* Channel mapping (order defined in vlc_aout.h) */
465 struct pa_channel_map map;
468 if (aout->output.output.i_physical_channels & AOUT_CHAN_LEFT)
469 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
470 if (aout->output.output.i_physical_channels & AOUT_CHAN_RIGHT)
471 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
472 if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
473 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
474 if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
475 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
476 if (aout->output.output.i_physical_channels & AOUT_CHAN_REARLEFT)
477 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
478 if (aout->output.output.i_physical_channels & AOUT_CHAN_REARRIGHT)
479 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
480 if (aout->output.output.i_physical_channels & AOUT_CHAN_REARCENTER)
481 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
482 if (aout->output.output.i_physical_channels & AOUT_CHAN_CENTER)
484 if (ss.channels == 1)
485 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
487 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
489 if (aout->output.output.i_physical_channels & AOUT_CHAN_LFE)
490 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
492 for (unsigned i = 0; map.channels < ss.channels; i++) {
493 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
494 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
497 if (!pa_channel_map_valid(&map)) {
498 msg_Err(aout, "unsupported channel map");
501 const char *name = pa_channel_map_to_pretty_name(&map);
502 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
505 /* Stream parameters */
506 const pa_stream_flags_t flags = PA_STREAM_INTERPOLATE_TIMING
507 | PA_STREAM_AUTO_TIMING_UPDATE
508 | PA_STREAM_ADJUST_LATENCY
509 | PA_STREAM_START_CORKED;
511 const uint32_t byterate = pa_bytes_per_second(&ss);
512 struct pa_buffer_attr attr;
513 /* no point in larger buffers on PA side than VLC */
515 attr.tlength = byterate * AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ;
516 attr.prebuf = byterate * AOUT_MAX_PREPARE_TIME / CLOCK_FREQ;
518 attr.fragsize = 0; /* not used for output */
520 /* Allocate structures */
521 aout_sys_t *sys = malloc(sizeof(*sys));
522 if (unlikely(sys == NULL))
524 aout->output.p_sys = sys;
527 //sys->byterate = byterate;
530 sys->base_volume = PA_VOLUME_NORM;
531 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
533 /* Allocate threaded main loop */
534 pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new();
535 if (unlikely(mainloop == NULL)) {
539 sys->mainloop = mainloop;
541 if (pa_threaded_mainloop_start(mainloop) < 0) {
542 pa_threaded_mainloop_free(mainloop);
546 pa_threaded_mainloop_lock(mainloop);
548 /* Connect to PulseAudio server */
549 char *user_agent = var_InheritString(aout, "user-agent");
550 pa_context *ctx = pa_context_new(pa_threaded_mainloop_get_api(mainloop),
553 if (unlikely(ctx == NULL))
557 pa_context_set_state_callback(ctx, context_state_cb, mainloop);
558 if (pa_context_connect(ctx, NULL, 0, NULL) < 0
559 || context_wait(mainloop, ctx)) {
560 error(aout, "cannot connect to server", ctx);
564 /* Create a playback stream */
565 pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
567 error(aout, "cannot create stream", ctx);
571 pa_stream_set_state_callback(s, stream_state_cb, mainloop);
572 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
573 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
574 pa_stream_set_started_callback(s, stream_started_cb, aout);
575 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
576 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
578 if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
579 || stream_wait(mainloop, s)) {
580 error(aout, "cannot connect stream", ctx);
584 const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
585 msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
586 "prebuf=%u, minreq=%u",
587 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
589 aout->output.i_nb_samples = pba->minreq / pa_frame_size(&ss);
591 var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
592 var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
593 &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
595 var_AddCallback (aout, "audio-device", StreamMove, s);
596 op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
597 /* We may need to wait for completion... once LibVLC supports this */
599 pa_operation_unref(op);
600 stream_moved_cb(s, aout);
601 pa_threaded_mainloop_unlock(mainloop);
603 aout->output.pf_play = Play;
604 aout->output.pf_pause = Pause;
605 aout->output.pf_volume_set = VolumeSet;
609 pa_threaded_mainloop_unlock(mainloop);
614 /*****************************************************************************
615 * Close: close the audio device
616 *****************************************************************************/
617 static void Close (vlc_object_t *obj)
619 aout_instance_t *aout = (aout_instance_t *)obj;
620 aout_sys_t *sys = aout->output.p_sys;
621 pa_threaded_mainloop *mainloop = sys->mainloop;
622 pa_context *ctx = sys->context;
623 pa_stream *s = sys->stream;
626 /* The callback takes mainloop lock, so it CANNOT be held here! */
627 var_DelCallback (aout, "audio-device", StreamMove, s);
628 var_Destroy (aout, "audio-device");
631 pa_threaded_mainloop_lock(mainloop);
635 op = pa_stream_flush(s, NULL, NULL);
637 pa_operation_unref(op);
638 op = pa_stream_drain(s, NULL, NULL);
640 pa_operation_unref(op);
641 pa_stream_disconnect(s);
645 pa_context_unref(ctx);
646 pa_threaded_mainloop_unlock(mainloop);
647 pa_threaded_mainloop_free(mainloop);