1 /*****************************************************************************
2 * pulse.c : Pulseaudio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
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 it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
34 #include <pulse/pulseaudio.h>
36 #if !PA_CHECK_VERSION(0,9,22)
37 # include <vlc_xlib.h>
40 static int Open ( vlc_object_t * );
41 static void Close ( vlc_object_t * );
44 set_shortname( "PulseAudio" )
45 set_description( N_("Pulseaudio audio output") )
46 set_capability( "audio output", 160 )
47 set_category( CAT_AUDIO )
48 set_subcategory( SUBCAT_AUDIO_AOUT )
49 add_shortcut( "pulseaudio", "pa" )
50 set_callbacks( Open, Close )
54 * Be careful what you do when the PulseAudio mainloop is held, which is to say
55 * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
56 * In particular, a VLC variable callback cannot be triggered nor deleted with
57 * the PulseAudio mainloop lock held, if the callback acquires the lock. */
61 pa_stream *stream; /**< PulseAudio playback stream object */
62 pa_context *context; /**< PulseAudio connection context */
63 pa_threaded_mainloop *mainloop; /**< PulseAudio thread */
64 pa_time_event *trigger; /**< Deferred stream trigger */
65 pa_volume_t base_volume; /**< 0dB reference volume */
66 pa_cvolume cvolume; /**< actual sink input volume */
67 mtime_t first_pts; /**< Play time of buffer start */
68 mtime_t paused; /**< Time when (last) paused */
71 static void sink_list_cb(pa_context *, const pa_sink_info *, int, void *);
72 static void sink_input_info_cb(pa_context *, const pa_sink_input_info *,
76 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
77 uint32_t idx, void *userdata)
79 audio_output_t *aout = userdata;
80 aout_sys_t *sys = aout->sys;
83 switch (type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
85 case PA_SUBSCRIPTION_EVENT_SINK:
86 switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
88 case PA_SUBSCRIPTION_EVENT_NEW:
89 case PA_SUBSCRIPTION_EVENT_CHANGE:
90 op = pa_context_get_sink_info_by_index(ctx, idx, sink_list_cb, aout);
91 if (likely(op != NULL))
92 pa_operation_unref(op);
95 case PA_SUBSCRIPTION_EVENT_REMOVE:
96 var_Change(aout, "audio-device", VLC_VAR_DELCHOICE,
97 &(vlc_value_t){ .i_int = idx }, NULL);
102 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
103 if (sys->stream == NULL || idx != pa_stream_get_index(sys->stream))
104 break; /* only interested in our sink input */
106 /* Gee... PA will not provide the infos directly in the event. */
107 switch (type & PA_SUBSCRIPTION_EVENT_TYPE_MASK)
109 case PA_SUBSCRIPTION_EVENT_REMOVE:
110 msg_Err(aout, "sink input killed!");
114 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
116 if (likely(op != NULL))
117 pa_operation_unref(op);
122 default: /* unsubscribed facility?! */
129 static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
132 audio_output_t *aout = userdata;
133 aout_sys_t *sys = aout->sys;
134 vlc_value_t val, text;
140 msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
142 val.i_int = i->index;
143 text.psz_string = (char *)i->description;
144 /* FIXME: There is no way to replace a choice explicitly. */
145 var_Change(aout, "audio-device", VLC_VAR_DELCHOICE, &val, NULL);
146 var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
147 /* FIXME: var_Change() can change the variable value if we remove the
148 * current value from the choice list, or if we add a choice while there
149 * was none. So force the correct value back. */
150 if (sys->stream != NULL)
152 val.i_int = pa_stream_get_device_index(sys->stream);
153 var_Change(aout, "audio-device", VLC_VAR_SETVALUE, &val, NULL);
157 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
160 audio_output_t *aout = userdata;
161 aout_sys_t *sys = aout->sys;
167 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
168 * amplification and maximum hardware amplification.
169 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
170 * Thus we need to use the sink base_volume as a multiplier,
171 * if and only if flat volume is active for our current sink. */
172 if (i->flags & PA_SINK_FLAT_VOLUME)
173 sys->base_volume = i->base_volume;
175 sys->base_volume = PA_VOLUME_NORM;
176 msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
180 /*** Latency management and lip synchronization ***/
181 static void stream_start_now(pa_stream *s, audio_output_t *aout)
183 aout_sys_t *sys = aout->sys;
186 assert (sys->trigger == NULL);
188 op = pa_stream_cork(s, 0, NULL, NULL);
190 pa_operation_unref(op);
191 op = pa_stream_trigger(s, NULL, NULL);
192 if (likely(op != NULL))
193 pa_operation_unref(op);
196 static void stream_stop(pa_stream *s, audio_output_t *aout)
198 aout_sys_t *sys = aout->sys;
201 if (sys->trigger != NULL) {
202 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
206 op = pa_stream_cork(s, 1, NULL, NULL);
208 pa_operation_unref(op);
211 static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
212 const struct timeval *tv, void *userdata)
214 audio_output_t *aout = userdata;
215 aout_sys_t *sys = aout->sys;
217 assert (sys->trigger == e);
219 msg_Dbg(aout, "starting deferred");
220 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
222 stream_start_now(sys->stream, aout);
223 (void) api; (void) e; (void) tv;
227 * Starts or resumes the playback stream.
228 * Tries start playing back audio samples at the most accurate time
229 * in order to minimize desync and resampling during early playback.
230 * @note PulseAudio lock required.
232 static void stream_start(pa_stream *s, audio_output_t *aout)
234 aout_sys_t *sys = aout->sys;
237 assert (sys->first_pts != VLC_TS_INVALID);
239 if (sys->trigger != NULL) {
240 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
244 delta = vlc_pa_get_latency(aout, sys->context, s);
245 if (unlikely(delta == VLC_TS_INVALID)) {
246 msg_Dbg(aout, "cannot synchronize start");
247 delta = 0; /* screwed */
250 delta = (sys->first_pts - mdate()) - delta;
252 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
253 delta += pa_rtclock_now();
254 sys->trigger = pa_context_rttime_new(sys->context, delta,
255 stream_trigger_cb, aout);
257 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
258 stream_start_now(s, aout);
262 static void stream_latency_cb(pa_stream *s, void *userdata)
264 audio_output_t *aout = userdata;
265 aout_sys_t *sys = aout->sys;
267 if (sys->paused != VLC_TS_INVALID)
268 return; /* nothing to do while paused */
269 if (sys->first_pts == VLC_TS_INVALID)
270 return; /* nothing to do if buffers are (still) empty */
271 if (pa_stream_is_corked(s) > 0)
272 stream_start(s, aout);
276 /*** Stream helpers ***/
277 static void stream_state_cb(pa_stream *s, void *userdata)
279 pa_threaded_mainloop *mainloop = userdata;
281 switch (pa_stream_get_state(s)) {
282 case PA_STREAM_READY:
283 case PA_STREAM_FAILED:
284 case PA_STREAM_TERMINATED:
285 pa_threaded_mainloop_signal(mainloop, 0);
291 static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
293 audio_output_t *aout = userdata;
294 const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
296 msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
297 "prebuf=%u, minreq=%u",
298 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
301 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
304 audio_output_t *aout = userdata;
306 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
307 aout_PolicyReport(aout, true);
309 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
310 aout_PolicyReport(aout, false);
312 #if PA_CHECK_VERSION(1,0,0)
313 /* FIXME: expose aout_Restart() directly */
314 if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
315 vlc_value_t dummy = { .i_int = 0 };
317 msg_Dbg (aout, "format lost");
318 aout_ChannelsRestart (VLC_OBJECT(aout), "audio-device",
322 msg_Warn (aout, "unhandled stream event \"%s\"", name);
327 static void stream_moved_cb(pa_stream *s, void *userdata)
329 audio_output_t *aout = userdata;
330 aout_sys_t *sys = aout->sys;
332 uint32_t idx = pa_stream_get_device_index(s);
334 msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
335 pa_stream_get_device_name(s));
336 op = pa_context_get_sink_info_by_index(sys->context, idx,
338 if (likely(op != NULL))
339 pa_operation_unref(op);
341 /* Update the variable if someone else moved our stream */
342 var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
343 &(vlc_value_t){ .i_int = idx }, NULL);
345 /* Sink unknown as yet, create stub choice for it */
346 if (var_GetInteger(aout, "audio-device") != idx)
348 var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE,
349 &(vlc_value_t){ .i_int = idx },
350 &(vlc_value_t){ .psz_string = (char *)"?" });
351 var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
352 &(vlc_value_t){ .i_int = idx }, NULL);
356 static void stream_overflow_cb(pa_stream *s, void *userdata)
358 audio_output_t *aout = userdata;
359 aout_sys_t *sys = aout->sys;
362 msg_Err(aout, "overflow, flushing");
363 op = pa_stream_flush(s, NULL, NULL);
364 if (unlikely(op == NULL))
366 pa_operation_unref(op);
367 sys->first_pts = VLC_TS_INVALID;
370 static void stream_started_cb(pa_stream *s, void *userdata)
372 audio_output_t *aout = userdata;
374 msg_Dbg(aout, "started");
378 static void stream_suspended_cb(pa_stream *s, void *userdata)
380 audio_output_t *aout = userdata;
382 msg_Dbg(aout, "suspended");
386 static void stream_underflow_cb(pa_stream *s, void *userdata)
388 audio_output_t *aout = userdata;
390 msg_Dbg(aout, "underflow");
394 static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
396 pa_stream_state_t state;
398 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
399 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
401 pa_threaded_mainloop_wait(mainloop);
408 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
409 int eol, void *userdata)
411 audio_output_t *aout = userdata;
412 aout_sys_t *sys = aout->sys;
418 sys->cvolume = i->volume; /* cache volume for balance preservation */
420 pa_volume_t volume = pa_cvolume_max(&i->volume);
421 volume = pa_sw_volume_divide(volume, sys->base_volume);
422 aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
423 aout_MuteReport(aout, i->mute);
427 /*** VLC audio output callbacks ***/
429 static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
431 aout_sys_t *sys = aout->sys;
432 pa_stream *s = sys->stream;
434 if (pa_stream_is_corked(s) > 0)
435 return -1; /* latency is irrelevant if corked */
437 mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
438 if (delta == VLC_TS_INVALID)
445 /* Memory free callback. The block_t address is in front of the data. */
446 static void data_free(void *data)
448 block_t **pp = data, *block;
450 memcpy(&block, pp - 1, sizeof (block));
451 block_Release(block);
454 static void *data_convert(block_t **pp)
456 block_t *block = *pp;
457 /* In most cases, there is enough head room, and this is really cheap: */
458 block = block_Realloc(block, sizeof (block), block->i_buffer);
460 if (unlikely(block == NULL))
463 memcpy(block->p_buffer, &block, sizeof (block));
464 block->p_buffer += sizeof (block);
465 block->i_buffer -= sizeof (block);
466 return block->p_buffer;
470 * Queue one audio frame to the playback stream
472 static void Play(audio_output_t *aout, block_t *block)
474 aout_sys_t *sys = aout->sys;
475 pa_stream *s = sys->stream;
477 assert (sys->paused == VLC_TS_INVALID);
479 const void *ptr = data_convert(&block);
480 if (unlikely(ptr == NULL))
483 size_t len = block->i_buffer;
485 /* Note: The core already holds the output FIFO lock at this point.
486 * Therefore we must not under any circumstances (try to) acquire the
487 * output FIFO lock while the PulseAudio threaded main loop lock is held
488 * (including from PulseAudio stream callbacks). Otherwise lock inversion
489 * will take place, and sooner or later a deadlock. */
490 pa_threaded_mainloop_lock(sys->mainloop);
492 if (sys->first_pts == VLC_TS_INVALID)
493 sys->first_pts = block->i_pts;
495 if (pa_stream_is_corked(s) > 0)
496 stream_start(s, aout);
498 #if 0 /* Fault injector to test underrun recovery */
499 static volatile unsigned u = 0;
500 if ((++u % 1000) == 0) {
501 msg_Err(aout, "fault injection");
502 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
506 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
507 vlc_pa_error(aout, "cannot write", sys->context);
508 block_Release(block);
511 pa_threaded_mainloop_unlock(sys->mainloop);
515 * Cork or uncork the playback stream
517 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
519 aout_sys_t *sys = aout->sys;
520 pa_stream *s = sys->stream;
522 pa_threaded_mainloop_lock(sys->mainloop);
526 stream_stop(s, aout);
528 assert (sys->paused != VLC_TS_INVALID);
530 msg_Dbg(aout, "resuming after %"PRId64" us", date);
531 sys->paused = VLC_TS_INVALID;
533 if (sys->first_pts != VLC_TS_INVALID) {
534 sys->first_pts += date;
535 stream_start(s, aout);
539 pa_threaded_mainloop_unlock(sys->mainloop);
543 * Flush or drain the playback stream
545 static void Flush(audio_output_t *aout, bool wait)
547 aout_sys_t *sys = aout->sys;
548 pa_stream *s = sys->stream;
551 pa_threaded_mainloop_lock(sys->mainloop);
554 op = pa_stream_drain(s, NULL, NULL);
555 /* TODO: wait for drain completion*/
557 op = pa_stream_flush(s, NULL, NULL);
559 pa_operation_unref(op);
560 pa_threaded_mainloop_unlock(sys->mainloop);
563 static int VolumeSet(audio_output_t *aout, float vol)
565 aout_sys_t *sys = aout->sys;
566 if (sys->stream == NULL)
568 msg_Err (aout, "cannot change volume while not playing");
572 /* VLC provides the software volume so convert directly to PulseAudio
573 * software volume, pa_volume_t. This is not a linear amplification factor
574 * so do not use PulseAudio linear amplification! */
575 vol *= PA_VOLUME_NORM;
576 if (unlikely(vol >= PA_VOLUME_MAX))
578 pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
580 /* Preserve the balance (VLC does not support it). */
581 pa_cvolume cvolume = sys->cvolume;
582 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
583 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
584 assert(pa_cvolume_valid(&cvolume));
587 uint32_t idx = pa_stream_get_index(sys->stream);
588 pa_threaded_mainloop_lock(sys->mainloop);
589 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
591 if (likely(op != NULL))
592 pa_operation_unref(op);
593 pa_threaded_mainloop_unlock(sys->mainloop);
598 static int MuteSet(audio_output_t *aout, bool mute)
600 aout_sys_t *sys = aout->sys;
601 if (sys->stream == NULL)
603 msg_Err (aout, "cannot change volume while not playing");
608 uint32_t idx = pa_stream_get_index(sys->stream);
609 pa_threaded_mainloop_lock(sys->mainloop);
610 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
611 if (likely(op != NULL))
612 pa_operation_unref(op);
613 pa_threaded_mainloop_unlock(sys->mainloop);
618 static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
619 vlc_value_t val, void *userdata)
621 audio_output_t *aout = (audio_output_t *)obj;
622 aout_sys_t *sys = aout->sys;
623 pa_stream *s = userdata;
625 uint32_t idx = pa_stream_get_index(s);
626 uint32_t sink_idx = val.i_int;
628 (void) varname; (void) old;
630 pa_threaded_mainloop_lock(sys->mainloop);
631 op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
633 if (likely(op != NULL)) {
634 pa_operation_unref(op);
635 msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
637 vlc_pa_error(obj, "cannot move sink", sys->context);
638 pa_threaded_mainloop_unlock(sys->mainloop);
640 return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
643 static void Stop(audio_output_t *);
646 * Create a PulseAudio playback stream, a.k.a. a sink input.
648 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
650 aout_sys_t *sys = aout->sys;
652 /* Sample format specification */
653 struct pa_sample_spec ss;
654 #if PA_CHECK_VERSION(1,0,0)
655 pa_encoding_t encoding = PA_ENCODING_INVALID;
658 switch (fmt->i_format)
661 fmt->i_format = VLC_CODEC_F32B;
663 ss.format = PA_SAMPLE_FLOAT32BE;
666 fmt->i_format = VLC_CODEC_F32L;
668 ss.format = PA_SAMPLE_FLOAT32LE;
671 ss.format = PA_SAMPLE_S32BE;
674 ss.format = PA_SAMPLE_S32LE;
677 ss.format = PA_SAMPLE_S24BE;
680 ss.format = PA_SAMPLE_S24LE;
683 ss.format = PA_SAMPLE_S16BE;
686 ss.format = PA_SAMPLE_S16LE;
689 fmt->i_format = VLC_CODEC_U8;
691 ss.format = PA_SAMPLE_U8;
693 #if PA_CHECK_VERSION(1,0,0)
695 fmt->i_format = VLC_CODEC_SPDIFL;
696 encoding = PA_ENCODING_AC3_IEC61937;
697 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
699 /*case VLC_CODEC_EAC3:
700 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
701 encoding = PA_ENCODING_EAC3_IEC61937;
702 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
705 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
706 encoding = PA_ENCODING_MPEG_IEC61937;
707 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
710 fmt->i_format = VLC_CODEC_SPDIFL;
711 encoding = PA_ENCODING_DTS_IEC61937;
712 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
718 fmt->i_format = VLC_CODEC_FL32;
719 ss.format = PA_SAMPLE_FLOAT32NE;
723 fmt->i_format = VLC_CODEC_S16N;
724 ss.format = PA_SAMPLE_S16NE;
729 ss.rate = fmt->i_rate;
730 ss.channels = aout_FormatNbChannels(fmt);
731 if (!pa_sample_spec_valid(&ss)) {
732 msg_Err(aout, "unsupported sample specification");
736 /* Channel mapping (order defined in vlc_aout.h) */
737 struct pa_channel_map map;
740 if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
741 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
742 if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
743 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
744 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
745 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
746 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
747 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
748 if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
749 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
750 if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
751 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
752 if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
753 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
754 if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
756 if (ss.channels == 1)
757 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
759 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
761 if (fmt->i_physical_channels & AOUT_CHAN_LFE)
762 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
764 for (unsigned i = 0; map.channels < ss.channels; i++) {
765 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
766 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
769 if (!pa_channel_map_valid(&map)) {
770 msg_Err(aout, "unsupported channel map");
773 const char *name = pa_channel_map_to_name(&map);
774 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
777 /* Stream parameters */
778 const pa_stream_flags_t flags = PA_STREAM_START_CORKED
779 | PA_STREAM_INTERPOLATE_TIMING
780 | PA_STREAM_NOT_MONOTONIC
781 | PA_STREAM_AUTO_TIMING_UPDATE
782 | PA_STREAM_FIX_RATE;
784 struct pa_buffer_attr attr;
786 /* PulseAudio assumes that tlength bytes are available in the buffer. Thus
787 * we need to be conservative and set the minimum value that the VLC
788 * audio decoder thread warrants. Otherwise, PulseAudio buffers will
789 * underrun on hardware with large buffers. VLC keeps at least
790 * AOUT_MIN_PREPARE and at most AOUT_MAX_PREPARE worth of audio buffers.
791 * TODO? tlength could be adaptively increased to reduce wakeups. */
792 attr.tlength = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
793 attr.prebuf = 0; /* trigger manually */
795 attr.fragsize = 0; /* not used for output */
799 sys->first_pts = VLC_TS_INVALID;
800 sys->paused = VLC_TS_INVALID;
803 sys->base_volume = PA_VOLUME_NORM;
804 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
806 #if PA_CHECK_VERSION(1,0,0)
807 pa_format_info *formatv[2];
808 unsigned formatc = 0;
810 /* Favor digital pass-through if available*/
811 if (encoding != PA_ENCODING_INVALID) {
812 formatv[formatc] = pa_format_info_new();
813 formatv[formatc]->encoding = encoding;
814 pa_format_info_set_rate(formatv[formatc], ss.rate);
815 pa_format_info_set_channels(formatv[formatc], ss.channels);
816 pa_format_info_set_channel_map(formatv[formatc], &map);
820 /* Fallback to PCM */
821 formatv[formatc] = pa_format_info_new();
822 formatv[formatc]->encoding = PA_ENCODING_PCM;
823 pa_format_info_set_sample_format(formatv[formatc], ss.format);
824 pa_format_info_set_rate(formatv[formatc], ss.rate);
825 pa_format_info_set_channels(formatv[formatc], ss.channels);
826 pa_format_info_set_channel_map(formatv[formatc], &map);
829 /* Create a playback stream */
831 pa_proplist *props = pa_proplist_new();
832 if (likely(props != NULL))
833 /* TODO: set other stream properties */
834 pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
836 pa_threaded_mainloop_lock(sys->mainloop);
837 s = pa_stream_new_extended(sys->context, "audio stream", formatv, formatc,
839 if (likely(props != NULL))
840 pa_proplist_free(props);
842 for (unsigned i = 0; i < formatc; i++)
843 pa_format_info_free(formatv[i]);
845 pa_threaded_mainloop_lock(sys->mainloop);
846 pa_stream *s = pa_stream_new(sys->context, "audio stream", &ss, &map);
849 pa_threaded_mainloop_unlock(sys->mainloop);
850 vlc_pa_error(aout, "stream creation failure", sys->context);
854 pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
855 pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
856 pa_stream_set_event_callback(s, stream_event_cb, aout);
857 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
858 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
859 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
860 pa_stream_set_started_callback(s, stream_started_cb, aout);
861 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
862 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
864 if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
865 || stream_wait(s, sys->mainloop)) {
866 vlc_pa_error(aout, "stream connection failure", sys->context);
870 const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
871 #if PA_CHECK_VERSION(1,0,0)
872 if (encoding != PA_ENCODING_INVALID) {
873 const pa_format_info *info = pa_stream_get_format_info(s);
875 assert (info != NULL);
876 if (pa_format_info_is_pcm (info)) {
877 msg_Dbg(aout, "digital pass-through not available");
878 fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
880 msg_Dbg(aout, "digital pass-through enabled");
886 fmt->i_rate = spec->rate;
888 stream_buffer_attr_cb(s, aout);
889 stream_moved_cb(s, aout);
890 pa_threaded_mainloop_unlock(sys->mainloop);
891 var_AddCallback (aout, "audio-device", StreamMove, s);
896 pa_threaded_mainloop_unlock(sys->mainloop);
897 var_AddCallback (aout, "audio-device", StreamMove, s);
903 * Removes a PulseAudio playback stream
905 static void Stop(audio_output_t *aout)
907 aout_sys_t *sys = aout->sys;
908 pa_stream *s = sys->stream;
910 /* The callback takes mainloop lock, so it CANNOT be held here! */
911 var_DelCallback (aout, "audio-device", StreamMove, s);
913 pa_threaded_mainloop_lock(sys->mainloop);
914 if (unlikely(sys->trigger != NULL))
915 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
916 pa_stream_disconnect(s);
918 /* Clear all callbacks */
919 pa_stream_set_state_callback(s, NULL, NULL);
920 pa_stream_set_buffer_attr_callback(s, NULL, NULL);
921 pa_stream_set_event_callback(s, NULL, NULL);
922 pa_stream_set_latency_update_callback(s, NULL, NULL);
923 pa_stream_set_moved_callback(s, NULL, NULL);
924 pa_stream_set_overflow_callback(s, NULL, NULL);
925 pa_stream_set_started_callback(s, NULL, NULL);
926 pa_stream_set_suspended_callback(s, NULL, NULL);
927 pa_stream_set_underflow_callback(s, NULL, NULL);
931 pa_threaded_mainloop_unlock(sys->mainloop);
934 static int Open(vlc_object_t *obj)
936 audio_output_t *aout = (audio_output_t *)obj;
937 aout_sys_t *sys = malloc(sizeof (*sys));
940 #if !PA_CHECK_VERSION(0,9,22)
941 if (!vlc_xlib_init(obj))
944 if (unlikely(sys == NULL))
947 /* Allocate structures */
948 pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
960 aout->time_get = TimeGet;
964 aout->volume_set = VolumeSet;
965 aout->mute_set = MuteSet;
967 /* Devices (sinks) */
968 var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
969 var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
970 &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
973 pa_threaded_mainloop_lock(sys->mainloop);
974 op = pa_context_get_sink_info_list(sys->context, sink_list_cb, aout);
976 pa_operation_unref(op);
979 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
980 | PA_SUBSCRIPTION_MASK_SINK_INPUT;
981 pa_context_set_subscribe_callback(sys->context, context_cb, aout);
982 op = pa_context_subscribe(sys->context, mask, NULL, NULL);
983 if (likely(op != NULL))
984 pa_operation_unref(op);
985 pa_threaded_mainloop_unlock(sys->mainloop);
990 static void Close(vlc_object_t *obj)
992 audio_output_t *aout = (audio_output_t *)obj;
993 aout_sys_t *sys = aout->sys;
994 pa_context *ctx = sys->context;
996 pa_threaded_mainloop_lock(sys->mainloop);
997 pa_context_set_subscribe_callback(sys->context, NULL, NULL);
998 pa_threaded_mainloop_unlock(sys->mainloop);
999 vlc_pa_disconnect(obj, ctx, sys->mainloop);
1001 var_Destroy (aout, "audio-device");