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>
33 #include <pulse/pulseaudio.h>
35 static int Open ( vlc_object_t * );
36 static void Close ( vlc_object_t * );
39 set_shortname( "PulseAudio" )
40 set_description( N_("Pulseaudio audio output") )
41 set_capability( "audio output", 160 )
42 set_category( CAT_AUDIO )
43 set_subcategory( SUBCAT_AUDIO_AOUT )
44 add_shortcut( "pulseaudio", "pa" )
45 set_callbacks( Open, Close )
48 /* TODO: single static mainloop */
52 pa_stream *stream; /**< PulseAudio playback stream object */
53 pa_context *context; /**< PulseAudio connection context */
54 pa_threaded_mainloop *mainloop; /**< PulseAudio event loop */
55 pa_volume_t base_volume; /**< 0dB reference volume */
56 pa_cvolume cvolume; /**< actual sink input volume */
57 //uint32_t byterate; /**< bytes per second */
61 static void context_state_cb(pa_context *c, void *userdata)
63 pa_threaded_mainloop *mainloop = userdata;
65 switch (pa_context_get_state(c)) {
66 case PA_CONTEXT_READY:
67 case PA_CONTEXT_FAILED:
68 case PA_CONTEXT_TERMINATED:
69 pa_threaded_mainloop_signal(mainloop, 0);
75 static bool context_wait(pa_threaded_mainloop *mainloop, pa_context *context)
77 pa_context_state_t state;
79 while ((state = pa_context_get_state(context)) != PA_CONTEXT_READY) {
80 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
82 pa_threaded_mainloop_wait(mainloop);
87 static void error(aout_instance_t *aout, const char *msg, pa_context *context)
89 msg_Err(aout, "%s: %s", msg, pa_strerror(pa_context_errno(context)));
93 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
96 aout_instance_t *aout = userdata;
97 aout_sys_t *sys = aout->output.p_sys;
103 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
104 * amplification and maximum hardware amplification.
105 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
106 * Thus we need to use the sink base_volume as a multiplier,
107 * if and only if flat volume is active for our current sink. */
108 if (i->flags & PA_SINK_FLAT_VOLUME)
109 sys->base_volume = i->base_volume;
111 sys->base_volume = PA_VOLUME_NORM;
112 msg_Dbg(aout, "base volume: %f", pa_sw_volume_to_linear(sys->base_volume));
116 static void stream_state_cb(pa_stream *s, void *userdata)
118 pa_threaded_mainloop *mainloop = userdata;
120 switch (pa_stream_get_state(s)) {
121 case PA_STREAM_READY:
122 case PA_STREAM_FAILED:
123 case PA_STREAM_TERMINATED:
124 pa_threaded_mainloop_signal(mainloop, 0);
130 static void stream_moved_cb(pa_stream *s, void *userdata)
132 aout_instance_t *aout = userdata;
133 aout_sys_t *sys = aout->output.p_sys;
135 uint32_t idx = pa_stream_get_device_index(s);
137 msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
138 pa_stream_get_device_name(s));
139 op = pa_context_get_sink_info_by_index(sys->context, idx,
141 if (likely(op != NULL))
142 pa_operation_unref(op);
145 static void stream_overflow_cb(pa_stream *s, void *userdata)
147 aout_instance_t *aout = userdata;
149 msg_Err(aout, "overflow");
153 static void stream_started_cb(pa_stream *s, void *userdata)
155 aout_instance_t *aout = userdata;
157 msg_Dbg(aout, "started");
161 static void stream_suspended_cb(pa_stream *s, void *userdata)
163 aout_instance_t *aout = userdata;
165 msg_Dbg(aout, "suspended");
169 static void stream_underflow_cb(pa_stream *s, void *userdata)
171 aout_instance_t *aout = userdata;
173 msg_Dbg(aout, "underflow");
177 static int stream_wait(pa_threaded_mainloop *mainloop, pa_stream *stream)
179 pa_stream_state_t state;
181 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
182 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
184 pa_threaded_mainloop_wait(mainloop);
189 /* Memory free callback. The block_t address is in front of the data. */
190 static void data_free(void *data)
192 block_t **pp = data, *block;
194 memcpy(&block, pp - 1, sizeof (block));
195 block_Release(block);
198 static void *data_convert(block_t **pp)
200 block_t *block = *pp;
201 /* In most cases, there is enough head room, and this is really cheap: */
202 block = block_Realloc(block, sizeof (block), block->i_buffer);
204 if (unlikely(block == NULL))
207 memcpy(block->p_buffer, &block, sizeof (block));
208 block->p_buffer += sizeof (block);
209 block->i_buffer -= sizeof (block);
210 return block->p_buffer;
213 /*****************************************************************************
214 * Play: play a sound samples buffer
215 *****************************************************************************/
216 static void Play(aout_instance_t *aout)
218 aout_sys_t *sys = aout->output.p_sys;
219 pa_stream *s = sys->stream;
221 /* Note: The core already holds the output FIFO lock at this point.
222 * Therefore we must not under any circumstances (try to) acquire the
223 * output FIFO lock while the PulseAudio threaded main loop lock is held
224 * (including from PulseAudio stream callbacks). Otherwise lock inversion
225 * will take place, and sooner or later a deadlock. */
226 pa_threaded_mainloop_lock(sys->mainloop);
228 if (pa_stream_is_corked(sys->stream) > 0) {
229 pa_operation *op = pa_stream_cork(s, 0, NULL, NULL);
231 pa_operation_unref(op);
232 msg_Dbg(aout, "uncorking");
236 /* This function should be called by the LibVLC core a header of time,
237 * but not more than AOUT_MAX_PREPARE. The PulseAudio latency should be
238 * shorter than that (though it might not be the case with some evil piece
239 * of audio output hardware). So we may need to trigger playback early,
240 * (that is to say, short cut the PulseAudio prebuffering). Otherwise,
241 * audio and video may be out of synchronization. */
244 if (pa_stream_get_latency(s, &latency, &negative) < 0) {
245 /* Especially at start of stream, latency may not be known (yet). */
246 if (pa_context_errno(sys->context) != PA_ERR_NODATA)
247 error(aout, "cannot determine latency", sys->context);
249 mtime_t gap = aout_FifoFirstDate(aout, &aout->output.fifo) - mdate()
252 if (gap > AOUT_PTS_TOLERANCE)
253 msg_Dbg(aout, "buffer too early (%"PRId64" us)", gap);
254 else if (gap < -AOUT_PTS_TOLERANCE)
255 msg_Err(aout, "buffer too late (%"PRId64" us)", -gap);
258 #if 0 /* Fault injector to test underrun recovery */
259 static unsigned u = 0;
260 if ((++u % 500) == 0) {
261 msg_Err(aout, "fault injection");
262 msleep(CLOCK_FREQ*2);
266 /* This function is called exactly once per block in the output FIFO, so
267 * this for-loop is not necessary.
268 * If this function is changed to not always dequeue blocks, be sure to
269 * limit the queue size to a reasonable limit to avoid huge leaks. */
271 block_t *block = aout_FifoPop(aout, &aout->output.fifo);
275 const void *ptr = data_convert(&block);
276 if (unlikely(ptr == NULL))
279 size_t len = block->i_buffer;
280 //mtime_t pts = block->i_pts, duration = block->i_length;
282 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0)
284 block_Release(block);
285 msg_Err(aout, "cannot write: %s",
286 pa_strerror(pa_context_errno(sys->context)));
290 pa_threaded_mainloop_unlock(sys->mainloop);
293 static int VolumeSet(aout_instance_t *aout, audio_volume_t vol, bool mute)
295 aout_sys_t *sys = aout->output.p_sys;
296 pa_threaded_mainloop *mainloop = sys->mainloop;
299 uint32_t idx = pa_stream_get_index(sys->stream);
300 pa_volume_t volume = pa_sw_volume_from_linear(vol / (float)AOUT_VOLUME_DEFAULT);
303 /* TODO: do not ruin the channel balance (if set outside VLC) */
304 /* TODO: notify UI about volume changes by other PulseAudio clients */
305 pa_cvolume_set(&sys->cvolume, sys->cvolume.channels, volume);
306 pa_sw_cvolume_multiply_scalar(&cvolume, &sys->cvolume, sys->base_volume);
307 assert(pa_cvolume_valid(&cvolume));
309 pa_threaded_mainloop_lock(mainloop);
310 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
311 if (likely(op != NULL))
312 pa_operation_unref(op);
313 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
314 if (likely(op != NULL))
315 pa_operation_unref(op);
316 pa_threaded_mainloop_unlock(mainloop);
321 /*****************************************************************************
322 * Open: open the audio device
323 *****************************************************************************/
324 static int Open(vlc_object_t *obj)
326 aout_instance_t *aout = (aout_instance_t *)obj;
328 /* Sample format specification */
329 struct pa_sample_spec ss;
331 switch(aout->output.output.i_format)
334 aout->output.output.i_format = VLC_CODEC_F32B;
336 ss.format = PA_SAMPLE_FLOAT32BE;
339 aout->output.output.i_format = VLC_CODEC_F32L;
341 ss.format = PA_SAMPLE_FLOAT32LE;
344 aout->output.output.i_format = VLC_CODEC_FL32;
345 ss.format = PA_SAMPLE_FLOAT32NE;
348 ss.format = PA_SAMPLE_S32BE;
351 ss.format = PA_SAMPLE_S32LE;
354 ss.format = PA_SAMPLE_S24BE;
357 ss.format = PA_SAMPLE_S24LE;
360 ss.format = PA_SAMPLE_S16BE;
363 ss.format = PA_SAMPLE_S16LE;
366 aout->output.output.i_format = VLC_CODEC_U8;
368 ss.format = PA_SAMPLE_U8;
373 aout->output.output.i_format = VLC_CODEC_FL32;
374 ss.format = PA_SAMPLE_FLOAT32NE;
378 aout->output.output.i_format = VLC_CODEC_S16N;
379 ss.format = PA_SAMPLE_S16NE;
384 ss.rate = aout->output.output.i_rate;
385 ss.channels = aout_FormatNbChannels(&aout->output.output);
386 if (!pa_sample_spec_valid(&ss)) {
387 msg_Err(aout, "unsupported sample specification");
391 /* Channel mapping (order defined in vlc_aout.h) */
392 struct pa_channel_map map;
395 if (aout->output.output.i_physical_channels & AOUT_CHAN_LEFT)
396 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
397 if (aout->output.output.i_physical_channels & AOUT_CHAN_RIGHT)
398 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
399 if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
400 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
401 if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
402 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
403 if (aout->output.output.i_physical_channels & AOUT_CHAN_REARLEFT)
404 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
405 if (aout->output.output.i_physical_channels & AOUT_CHAN_REARRIGHT)
406 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
407 if (aout->output.output.i_physical_channels & AOUT_CHAN_REARCENTER)
408 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
409 if (aout->output.output.i_physical_channels & AOUT_CHAN_CENTER)
411 if (ss.channels == 1)
412 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
414 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
416 if (aout->output.output.i_physical_channels & AOUT_CHAN_LFE)
417 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
419 for (unsigned i = 0; map.channels < ss.channels; i++) {
420 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
421 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
424 if (!pa_channel_map_valid(&map)) {
425 msg_Err(aout, "unsupported channel map");
428 const char *name = pa_channel_map_to_pretty_name(&map);
429 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
432 /* Stream parameters */
433 const pa_stream_flags_t flags = PA_STREAM_INTERPOLATE_TIMING
434 | PA_STREAM_AUTO_TIMING_UPDATE
435 | PA_STREAM_ADJUST_LATENCY
436 | PA_STREAM_START_CORKED;
438 const uint32_t byterate = pa_bytes_per_second(&ss);
439 struct pa_buffer_attr attr;
440 /* no point in larger buffers on PA side than VLC */
442 attr.tlength = byterate * AOUT_MAX_ADVANCE_TIME / CLOCK_FREQ;
443 attr.prebuf = byterate * AOUT_MAX_PREPARE_TIME / CLOCK_FREQ;
445 attr.fragsize = 0; /* not used for output */
447 /* Allocate structures */
448 aout_sys_t *sys = malloc(sizeof(*sys));
449 if (unlikely(sys == NULL))
451 aout->output.p_sys = sys;
454 //sys->byterate = byterate;
457 sys->base_volume = PA_VOLUME_NORM;
458 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
460 /* Allocate threaded main loop */
461 pa_threaded_mainloop *mainloop = pa_threaded_mainloop_new();
462 if (unlikely(mainloop == NULL)) {
466 sys->mainloop = mainloop;
468 if (pa_threaded_mainloop_start(mainloop) < 0) {
469 pa_threaded_mainloop_free(mainloop);
473 pa_threaded_mainloop_lock(mainloop);
475 /* Connect to PulseAudio server */
476 char *user_agent = var_InheritString(aout, "user-agent");
477 pa_context *ctx = pa_context_new(pa_threaded_mainloop_get_api(mainloop),
480 if (unlikely(ctx == NULL))
484 pa_context_set_state_callback(ctx, context_state_cb, mainloop);
485 if (pa_context_connect(ctx, NULL, 0, NULL) < 0
486 || context_wait(mainloop, ctx)) {
487 error(aout, "cannot connect to server", ctx);
491 /* Create a playback stream */
492 pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
494 error(aout, "cannot create stream", ctx);
498 pa_stream_set_state_callback(s, stream_state_cb, mainloop);
499 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
500 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
501 pa_stream_set_started_callback(s, stream_started_cb, aout);
502 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
503 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
505 if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
506 || stream_wait(mainloop, s)) {
507 error(aout, "cannot connect stream", ctx);
510 stream_moved_cb(s, aout);
512 const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
513 msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
514 "prebuf=%u, minreq=%u",
515 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
517 aout->output.i_nb_samples = pba->minreq / pa_frame_size(&ss);
518 pa_threaded_mainloop_unlock(mainloop);
520 aout->output.pf_play = Play;
521 aout->output.pf_volume_set = VolumeSet;
525 pa_threaded_mainloop_unlock(mainloop);
530 /*****************************************************************************
531 * Close: close the audio device
532 *****************************************************************************/
533 static void Close (vlc_object_t *obj)
535 aout_instance_t *aout = (aout_instance_t *)obj;
536 aout_sys_t *sys = aout->output.p_sys;
537 pa_threaded_mainloop *mainloop = sys->mainloop;
538 pa_context *ctx = sys->context;
539 pa_stream *s = sys->stream;
541 pa_threaded_mainloop_lock(mainloop);
545 op = pa_stream_flush(s, NULL, NULL);
547 pa_operation_unref(op);
548 op = pa_stream_drain(s, NULL, NULL);
550 pa_operation_unref(op);
551 pa_stream_disconnect(s);
555 pa_context_unref(ctx);
556 pa_threaded_mainloop_unlock(mainloop);
557 pa_threaded_mainloop_free(mainloop);