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. */
69 pa_stream *stream; /**< PulseAudio playback stream object */
70 pa_context *context; /**< PulseAudio connection context */
71 pa_threaded_mainloop *mainloop; /**< PulseAudio thread */
72 pa_time_event *trigger; /**< Deferred stream trigger */
73 pa_volume_t base_volume; /**< 0dB reference volume */
74 pa_cvolume cvolume; /**< actual sink input volume */
75 mtime_t first_pts; /**< Play time of buffer start */
76 mtime_t paused; /**< Time when (last) paused */
78 struct sink *sinks; /**< Locally-cached list of sinks */
83 static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
86 audio_output_t *aout = userdata;
87 aout_sys_t *sys = aout->sys;
93 msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
96 size_t namelen = strlen(i->name);
97 struct sink *sink = malloc(sizeof (*sink) + namelen);
98 if (unlikely(sink == NULL))
101 sink->next = sys->sinks;
102 sink->index = i->index;
103 sink->description = strdup(i->description);
104 memcpy(sink->name, i->name, namelen + 1);
108 static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
111 audio_output_t *aout = userdata;
112 aout_sys_t *sys = aout->sys;
118 for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
119 if (sink->index == i->index)
121 free(sink->description);
122 sink->description = strdup(i->description);
126 static void sink_del(uint32_t index, audio_output_t *aout)
128 aout_sys_t *sys = aout->sys;
129 struct sink **pp = &sys->sinks, *sink;
131 while ((sink = *pp) != NULL)
132 if (sink->index == index)
135 free(sink->description);
142 static void sink_event(pa_context *ctx, unsigned type, uint32_t idx,
143 audio_output_t *aout)
145 pa_operation *op = NULL;
149 case PA_SUBSCRIPTION_EVENT_NEW:
150 op = pa_context_get_sink_info_by_index(ctx, idx, sink_add_cb,
153 case PA_SUBSCRIPTION_EVENT_CHANGE:
154 op = pa_context_get_sink_info_by_index(ctx, idx, sink_mod_cb,
157 case PA_SUBSCRIPTION_EVENT_REMOVE:
162 pa_operation_unref(op);
165 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
168 audio_output_t *aout = userdata;
169 aout_sys_t *sys = aout->sys;
175 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
176 * amplification and maximum hardware amplification.
177 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
178 * Thus we need to use the sink base_volume as a multiplier,
179 * if and only if flat volume is active for our current sink. */
180 if (i->flags & PA_SINK_FLAT_VOLUME)
181 sys->base_volume = i->base_volume;
183 sys->base_volume = PA_VOLUME_NORM;
184 msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
188 /*** Latency management and lip synchronization ***/
189 static void stream_start_now(pa_stream *s, audio_output_t *aout)
191 aout_sys_t *sys = aout->sys;
194 assert (sys->trigger == NULL);
196 op = pa_stream_cork(s, 0, NULL, NULL);
198 pa_operation_unref(op);
199 op = pa_stream_trigger(s, NULL, NULL);
200 if (likely(op != NULL))
201 pa_operation_unref(op);
204 static void stream_stop(pa_stream *s, audio_output_t *aout)
206 aout_sys_t *sys = aout->sys;
209 if (sys->trigger != NULL) {
210 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
214 op = pa_stream_cork(s, 1, NULL, NULL);
216 pa_operation_unref(op);
219 static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
220 const struct timeval *tv, void *userdata)
222 audio_output_t *aout = userdata;
223 aout_sys_t *sys = aout->sys;
225 assert (sys->trigger == e);
227 msg_Dbg(aout, "starting deferred");
228 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
230 stream_start_now(sys->stream, aout);
231 (void) api; (void) e; (void) tv;
235 * Starts or resumes the playback stream.
236 * Tries start playing back audio samples at the most accurate time
237 * in order to minimize desync and resampling during early playback.
238 * @note PulseAudio lock required.
240 static void stream_start(pa_stream *s, audio_output_t *aout)
242 aout_sys_t *sys = aout->sys;
245 assert (sys->first_pts != VLC_TS_INVALID);
247 if (sys->trigger != NULL) {
248 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
252 delta = vlc_pa_get_latency(aout, sys->context, s);
253 if (unlikely(delta == VLC_TS_INVALID)) {
254 msg_Dbg(aout, "cannot synchronize start");
255 delta = 0; /* screwed */
258 delta = (sys->first_pts - mdate()) - delta;
260 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
261 delta += pa_rtclock_now();
262 sys->trigger = pa_context_rttime_new(sys->context, delta,
263 stream_trigger_cb, aout);
265 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
266 stream_start_now(s, aout);
270 static void stream_latency_cb(pa_stream *s, void *userdata)
272 audio_output_t *aout = userdata;
273 aout_sys_t *sys = aout->sys;
275 if (sys->paused != VLC_TS_INVALID)
276 return; /* nothing to do while paused */
277 if (sys->first_pts == VLC_TS_INVALID)
278 return; /* nothing to do if buffers are (still) empty */
279 if (pa_stream_is_corked(s) > 0)
280 stream_start(s, aout);
284 /*** Stream helpers ***/
285 static void stream_state_cb(pa_stream *s, void *userdata)
287 pa_threaded_mainloop *mainloop = userdata;
289 switch (pa_stream_get_state(s)) {
290 case PA_STREAM_READY:
291 case PA_STREAM_FAILED:
292 case PA_STREAM_TERMINATED:
293 pa_threaded_mainloop_signal(mainloop, 0);
299 static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
301 audio_output_t *aout = userdata;
302 const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
304 msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
305 "prebuf=%u, minreq=%u",
306 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
309 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
312 audio_output_t *aout = userdata;
314 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
315 aout_PolicyReport(aout, true);
317 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
318 aout_PolicyReport(aout, false);
320 #if PA_CHECK_VERSION(1,0,0)
321 /* FIXME: expose aout_Restart() directly */
322 if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
323 msg_Dbg (aout, "format lost");
324 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
327 msg_Warn (aout, "unhandled stream event \"%s\"", name);
332 static void stream_moved_cb(pa_stream *s, void *userdata)
334 audio_output_t *aout = userdata;
335 aout_sys_t *sys = aout->sys;
336 const char *name = pa_stream_get_device_name(s);
339 msg_Dbg(aout, "connected to sink %s", name);
340 aout_DeviceReport(aout, name);
342 op = pa_context_get_sink_info_by_name(sys->context, name, sink_info_cb,
344 if (likely(op != NULL))
345 pa_operation_unref(op);
348 static void stream_overflow_cb(pa_stream *s, void *userdata)
350 audio_output_t *aout = userdata;
351 aout_sys_t *sys = aout->sys;
354 msg_Err(aout, "overflow, flushing");
355 op = pa_stream_flush(s, NULL, NULL);
356 if (unlikely(op == NULL))
358 pa_operation_unref(op);
359 sys->first_pts = VLC_TS_INVALID;
362 static void stream_started_cb(pa_stream *s, void *userdata)
364 audio_output_t *aout = userdata;
366 msg_Dbg(aout, "started");
370 static void stream_suspended_cb(pa_stream *s, void *userdata)
372 audio_output_t *aout = userdata;
374 msg_Dbg(aout, "suspended");
378 static void stream_underflow_cb(pa_stream *s, void *userdata)
380 audio_output_t *aout = userdata;
382 msg_Dbg(aout, "underflow");
386 static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
388 pa_stream_state_t state;
390 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
391 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
393 pa_threaded_mainloop_wait(mainloop);
400 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
401 int eol, void *userdata)
403 audio_output_t *aout = userdata;
404 aout_sys_t *sys = aout->sys;
410 sys->cvolume = i->volume; /* cache volume for balance preservation */
412 pa_volume_t volume = pa_cvolume_max(&i->volume);
413 volume = pa_sw_volume_divide(volume, sys->base_volume);
414 aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
415 aout_MuteReport(aout, i->mute);
418 static void sink_input_event(pa_context *ctx,
419 pa_subscription_event_type_t type,
420 uint32_t idx, audio_output_t *aout)
424 /* Gee... PA will not provide the infos directly in the event. */
427 case PA_SUBSCRIPTION_EVENT_REMOVE:
428 msg_Err(aout, "sink input killed!");
432 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
434 if (likely(op != NULL))
435 pa_operation_unref(op);
442 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
443 uint32_t idx, void *userdata)
445 audio_output_t *aout = userdata;
446 aout_sys_t *sys = aout->sys;
447 unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
449 type &= PA_SUBSCRIPTION_EVENT_TYPE_MASK;
452 case PA_SUBSCRIPTION_EVENT_SINK:
453 sink_event(ctx, type, idx, userdata);
456 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
457 /* only interested in our sink input */
458 if (sys->stream != NULL && idx == pa_stream_get_index(sys->stream))
459 sink_input_event(ctx, type, idx, userdata);
462 default: /* unsubscribed facility?! */
468 /*** VLC audio output callbacks ***/
470 static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
472 aout_sys_t *sys = aout->sys;
473 pa_stream *s = sys->stream;
475 if (pa_stream_is_corked(s) > 0)
476 return -1; /* latency is irrelevant if corked */
478 mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
479 if (delta == VLC_TS_INVALID)
486 /* Memory free callback. The block_t address is in front of the data. */
487 static void data_free(void *data)
489 block_t **pp = data, *block;
491 memcpy(&block, pp - 1, sizeof (block));
492 block_Release(block);
495 static void *data_convert(block_t **pp)
497 block_t *block = *pp;
498 /* In most cases, there is enough head room, and this is really cheap: */
499 block = block_Realloc(block, sizeof (block), block->i_buffer);
501 if (unlikely(block == NULL))
504 memcpy(block->p_buffer, &block, sizeof (block));
505 block->p_buffer += sizeof (block);
506 block->i_buffer -= sizeof (block);
507 return block->p_buffer;
511 * Queue one audio frame to the playback stream
513 static void Play(audio_output_t *aout, block_t *block)
515 aout_sys_t *sys = aout->sys;
516 pa_stream *s = sys->stream;
518 assert (sys->paused == VLC_TS_INVALID);
520 const void *ptr = data_convert(&block);
521 if (unlikely(ptr == NULL))
524 size_t len = block->i_buffer;
526 /* Note: The core already holds the output FIFO lock at this point.
527 * Therefore we must not under any circumstances (try to) acquire the
528 * output FIFO lock while the PulseAudio threaded main loop lock is held
529 * (including from PulseAudio stream callbacks). Otherwise lock inversion
530 * will take place, and sooner or later a deadlock. */
531 pa_threaded_mainloop_lock(sys->mainloop);
533 if (sys->first_pts == VLC_TS_INVALID)
534 sys->first_pts = block->i_pts;
536 if (pa_stream_is_corked(s) > 0)
537 stream_start(s, aout);
539 #if 0 /* Fault injector to test underrun recovery */
540 static volatile unsigned u = 0;
541 if ((++u % 1000) == 0) {
542 msg_Err(aout, "fault injection");
543 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
547 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
548 vlc_pa_error(aout, "cannot write", sys->context);
549 block_Release(block);
552 pa_threaded_mainloop_unlock(sys->mainloop);
556 * Cork or uncork the playback stream
558 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
560 aout_sys_t *sys = aout->sys;
561 pa_stream *s = sys->stream;
563 pa_threaded_mainloop_lock(sys->mainloop);
567 stream_stop(s, aout);
569 assert (sys->paused != VLC_TS_INVALID);
571 msg_Dbg(aout, "resuming after %"PRId64" us", date);
572 sys->paused = VLC_TS_INVALID;
574 if (sys->first_pts != VLC_TS_INVALID) {
575 sys->first_pts += date;
576 stream_start(s, aout);
580 pa_threaded_mainloop_unlock(sys->mainloop);
584 * Flush or drain the playback stream
586 static void Flush(audio_output_t *aout, bool wait)
588 aout_sys_t *sys = aout->sys;
589 pa_stream *s = sys->stream;
592 pa_threaded_mainloop_lock(sys->mainloop);
595 op = pa_stream_drain(s, NULL, NULL);
596 /* TODO: wait for drain completion*/
598 op = pa_stream_flush(s, NULL, NULL);
600 pa_operation_unref(op);
601 pa_threaded_mainloop_unlock(sys->mainloop);
604 static int VolumeSet(audio_output_t *aout, float vol)
606 aout_sys_t *sys = aout->sys;
607 if (sys->stream == NULL)
609 msg_Err (aout, "cannot change volume while not playing");
613 /* VLC provides the software volume so convert directly to PulseAudio
614 * software volume, pa_volume_t. This is not a linear amplification factor
615 * so do not use PulseAudio linear amplification! */
616 vol *= PA_VOLUME_NORM;
617 if (unlikely(vol >= PA_VOLUME_MAX))
619 pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
621 /* Preserve the balance (VLC does not support it). */
622 pa_cvolume cvolume = sys->cvolume;
623 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
624 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
625 assert(pa_cvolume_valid(&cvolume));
628 uint32_t idx = pa_stream_get_index(sys->stream);
629 pa_threaded_mainloop_lock(sys->mainloop);
630 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
632 if (likely(op != NULL))
633 pa_operation_unref(op);
634 pa_threaded_mainloop_unlock(sys->mainloop);
639 static int MuteSet(audio_output_t *aout, bool mute)
641 aout_sys_t *sys = aout->sys;
642 if (sys->stream == NULL)
644 msg_Err (aout, "cannot change volume while not playing");
649 uint32_t idx = pa_stream_get_index(sys->stream);
650 pa_threaded_mainloop_lock(sys->mainloop);
651 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
652 if (likely(op != NULL))
653 pa_operation_unref(op);
654 pa_threaded_mainloop_unlock(sys->mainloop);
659 static int SinksList(audio_output_t *aout, char ***namesp, char ***descsp)
661 aout_sys_t *sys = aout->sys;
662 char **names, **descs;
665 pa_threaded_mainloop_lock(sys->mainloop);
666 for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
669 *namesp = names = xmalloc(sizeof(*names) * n);
670 *descsp = descs = xmalloc(sizeof(*descs) * n);
672 for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
674 *(names++) = strdup(sink->name);
675 *(descs++) = strdup(sink->description);
677 pa_threaded_mainloop_unlock(sys->mainloop);
681 static int StreamMove(audio_output_t *aout, const char *name)
683 aout_sys_t *sys = aout->sys;
685 uint32_t idx = pa_stream_get_index(sys->stream);
687 pa_threaded_mainloop_lock(sys->mainloop);
688 op = pa_context_move_sink_input_by_name(sys->context, idx, name,
690 if (likely(op != NULL)) {
691 pa_operation_unref(op);
692 msg_Dbg(aout, "moving to sink %s", name);
694 vlc_pa_error(aout, "cannot move sink input", sys->context);
695 pa_threaded_mainloop_unlock(sys->mainloop);
697 return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
700 static void Stop(audio_output_t *);
703 * Create a PulseAudio playback stream, a.k.a. a sink input.
705 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
707 aout_sys_t *sys = aout->sys;
709 /* Sample format specification */
710 struct pa_sample_spec ss;
711 #if PA_CHECK_VERSION(1,0,0)
712 pa_encoding_t encoding = PA_ENCODING_INVALID;
715 switch (fmt->i_format)
718 fmt->i_format = VLC_CODEC_FL32;
720 ss.format = PA_SAMPLE_FLOAT32NE;
723 ss.format = PA_SAMPLE_S32NE;
726 ss.format = PA_SAMPLE_S16NE;
729 ss.format = PA_SAMPLE_U8;
731 #if PA_CHECK_VERSION(1,0,0)
733 fmt->i_format = VLC_CODEC_SPDIFL;
734 encoding = PA_ENCODING_AC3_IEC61937;
735 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
737 /*case VLC_CODEC_EAC3:
738 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
739 encoding = PA_ENCODING_EAC3_IEC61937;
740 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
743 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
744 encoding = PA_ENCODING_MPEG_IEC61937;
745 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
748 fmt->i_format = VLC_CODEC_SPDIFL;
749 encoding = PA_ENCODING_DTS_IEC61937;
750 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
756 fmt->i_format = VLC_CODEC_FL32;
757 ss.format = PA_SAMPLE_FLOAT32NE;
761 fmt->i_format = VLC_CODEC_S16N;
762 ss.format = PA_SAMPLE_S16NE;
767 ss.rate = fmt->i_rate;
768 ss.channels = aout_FormatNbChannels(fmt);
769 if (!pa_sample_spec_valid(&ss)) {
770 msg_Err(aout, "unsupported sample specification");
774 /* Channel mapping (order defined in vlc_aout.h) */
775 struct pa_channel_map map;
778 if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
779 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
780 if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
781 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
782 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
783 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
784 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
785 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
786 if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
787 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
788 if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
789 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
790 if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
791 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
792 if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
794 if (ss.channels == 1)
795 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
797 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
799 if (fmt->i_physical_channels & AOUT_CHAN_LFE)
800 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
802 for (unsigned i = 0; map.channels < ss.channels; i++) {
803 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
804 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
807 if (!pa_channel_map_valid(&map)) {
808 msg_Err(aout, "unsupported channel map");
811 const char *name = pa_channel_map_to_name(&map);
812 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
815 /* Stream parameters */
816 const pa_stream_flags_t flags = PA_STREAM_START_CORKED
817 | PA_STREAM_INTERPOLATE_TIMING
818 | PA_STREAM_NOT_MONOTONIC
819 | PA_STREAM_AUTO_TIMING_UPDATE
820 | PA_STREAM_FIX_RATE;
822 struct pa_buffer_attr attr;
824 /* PulseAudio goes berserk if the target length (tlength) is not
825 * significantly longer than 2 periods (minreq), or when the period length
826 * is unspecified and the target length is short. */
827 attr.tlength = pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME, &ss);
828 attr.prebuf = 0; /* trigger manually */
829 attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
830 attr.fragsize = 0; /* not used for output */
834 sys->first_pts = VLC_TS_INVALID;
835 sys->paused = VLC_TS_INVALID;
838 sys->base_volume = PA_VOLUME_NORM;
839 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
841 #if PA_CHECK_VERSION(1,0,0)
842 pa_format_info *formatv[2];
843 unsigned formatc = 0;
845 /* Favor digital pass-through if available*/
846 if (encoding != PA_ENCODING_INVALID) {
847 formatv[formatc] = pa_format_info_new();
848 formatv[formatc]->encoding = encoding;
849 pa_format_info_set_rate(formatv[formatc], ss.rate);
850 pa_format_info_set_channels(formatv[formatc], ss.channels);
851 pa_format_info_set_channel_map(formatv[formatc], &map);
855 /* Fallback to PCM */
856 formatv[formatc] = pa_format_info_new();
857 formatv[formatc]->encoding = PA_ENCODING_PCM;
858 pa_format_info_set_sample_format(formatv[formatc], ss.format);
859 pa_format_info_set_rate(formatv[formatc], ss.rate);
860 pa_format_info_set_channels(formatv[formatc], ss.channels);
861 pa_format_info_set_channel_map(formatv[formatc], &map);
864 /* Create a playback stream */
866 pa_proplist *props = pa_proplist_new();
867 if (likely(props != NULL))
868 /* TODO: set other stream properties */
869 pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
871 pa_threaded_mainloop_lock(sys->mainloop);
872 s = pa_stream_new_extended(sys->context, "audio stream", formatv, formatc,
874 if (likely(props != NULL))
875 pa_proplist_free(props);
877 for (unsigned i = 0; i < formatc; i++)
878 pa_format_info_free(formatv[i]);
880 pa_threaded_mainloop_lock(sys->mainloop);
881 pa_stream *s = pa_stream_new(sys->context, "audio stream", &ss, &map);
884 pa_threaded_mainloop_unlock(sys->mainloop);
885 vlc_pa_error(aout, "stream creation failure", sys->context);
889 pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
890 pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
891 pa_stream_set_event_callback(s, stream_event_cb, aout);
892 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
893 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
894 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
895 pa_stream_set_started_callback(s, stream_started_cb, aout);
896 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
897 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
899 if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
900 || stream_wait(s, sys->mainloop)) {
901 vlc_pa_error(aout, "stream connection failure", sys->context);
905 const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
906 #if PA_CHECK_VERSION(1,0,0)
907 if (encoding != PA_ENCODING_INVALID) {
908 const pa_format_info *info = pa_stream_get_format_info(s);
910 assert (info != NULL);
911 if (pa_format_info_is_pcm (info)) {
912 msg_Dbg(aout, "digital pass-through not available");
913 fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
915 msg_Dbg(aout, "digital pass-through enabled");
921 fmt->i_rate = spec->rate;
923 stream_buffer_attr_cb(s, aout);
924 stream_moved_cb(s, aout);
925 pa_threaded_mainloop_unlock(sys->mainloop);
930 pa_threaded_mainloop_unlock(sys->mainloop);
936 * Removes a PulseAudio playback stream
938 static void Stop(audio_output_t *aout)
940 aout_sys_t *sys = aout->sys;
941 pa_stream *s = sys->stream;
943 pa_threaded_mainloop_lock(sys->mainloop);
944 if (unlikely(sys->trigger != NULL))
945 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
946 pa_stream_disconnect(s);
948 /* Clear all callbacks */
949 pa_stream_set_state_callback(s, NULL, NULL);
950 pa_stream_set_buffer_attr_callback(s, NULL, NULL);
951 pa_stream_set_event_callback(s, NULL, NULL);
952 pa_stream_set_latency_update_callback(s, NULL, NULL);
953 pa_stream_set_moved_callback(s, NULL, NULL);
954 pa_stream_set_overflow_callback(s, NULL, NULL);
955 pa_stream_set_started_callback(s, NULL, NULL);
956 pa_stream_set_suspended_callback(s, NULL, NULL);
957 pa_stream_set_underflow_callback(s, NULL, NULL);
961 pa_threaded_mainloop_unlock(sys->mainloop);
964 static int Open(vlc_object_t *obj)
966 audio_output_t *aout = (audio_output_t *)obj;
967 aout_sys_t *sys = malloc(sizeof (*sys));
970 #if !PA_CHECK_VERSION(0,9,22)
971 if (!vlc_xlib_init(obj))
974 if (unlikely(sys == NULL))
977 /* Allocate structures */
978 pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
991 aout->time_get = TimeGet;
995 aout->volume_set = VolumeSet;
996 aout->mute_set = MuteSet;
997 aout->device_enum = SinksList;
998 aout->device_select = StreamMove;
1000 pa_threaded_mainloop_lock(sys->mainloop);
1001 /* Sinks (output devices) list */
1002 op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
1004 pa_operation_unref(op);
1006 /* Context events */
1007 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
1008 | PA_SUBSCRIPTION_MASK_SINK_INPUT;
1009 pa_context_set_subscribe_callback(sys->context, context_cb, aout);
1010 op = pa_context_subscribe(sys->context, mask, NULL, NULL);
1011 if (likely(op != NULL))
1012 pa_operation_unref(op);
1013 pa_threaded_mainloop_unlock(sys->mainloop);
1018 static void Close(vlc_object_t *obj)
1020 audio_output_t *aout = (audio_output_t *)obj;
1021 aout_sys_t *sys = aout->sys;
1022 pa_context *ctx = sys->context;
1024 pa_threaded_mainloop_lock(sys->mainloop);
1025 pa_context_set_subscribe_callback(sys->context, NULL, NULL);
1026 pa_threaded_mainloop_unlock(sys->mainloop);
1027 vlc_pa_disconnect(obj, ctx, sys->mainloop);
1029 for (struct sink *sink = sys->sinks, *next; sink != NULL; sink = next)
1032 free(sink->description);