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. */
68 pa_stream *stream; /**< PulseAudio playback stream object */
69 pa_context *context; /**< PulseAudio connection context */
70 pa_threaded_mainloop *mainloop; /**< PulseAudio thread */
71 pa_time_event *trigger; /**< Deferred stream trigger */
72 pa_volume_t base_volume; /**< 0dB reference volume */
73 pa_cvolume cvolume; /**< actual sink input volume */
74 mtime_t first_pts; /**< Play time of buffer start */
76 pa_stream_flags_t flags_force; /**< Forced flags (stream must be NULL) */
77 char *sink_force; /**< Forced sink name (stream must be NULL) */
79 struct sink *sinks; /**< Locally-cached list of sinks */
84 static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
87 audio_output_t *aout = userdata;
88 aout_sys_t *sys = aout->sys;
94 msg_Dbg(aout, "adding sink %"PRIu32": %s (%s)", i->index, i->name,
96 aout_HotplugReport(aout, i->name, i->description);
98 size_t namelen = strlen(i->name);
99 struct sink *sink = malloc(sizeof (*sink) + namelen);
100 if (unlikely(sink == NULL))
103 sink->next = sys->sinks;
104 sink->index = i->index;
105 memcpy(sink->name, i->name, namelen + 1);
109 static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
112 audio_output_t *aout = userdata;
118 msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
120 aout_HotplugReport(aout, i->name, i->description);
123 static void sink_del(uint32_t index, audio_output_t *aout)
125 aout_sys_t *sys = aout->sys;
126 struct sink **pp = &sys->sinks, *sink;
128 msg_Dbg(aout, "removing sink %"PRIu32, index);
130 while ((sink = *pp) != NULL)
131 if (sink->index == index)
134 aout_HotplugReport(aout, sink->name, NULL);
141 static void sink_event(pa_context *ctx, unsigned type, uint32_t idx,
142 audio_output_t *aout)
144 pa_operation *op = NULL;
148 case PA_SUBSCRIPTION_EVENT_NEW:
149 op = pa_context_get_sink_info_by_index(ctx, idx, sink_add_cb,
152 case PA_SUBSCRIPTION_EVENT_CHANGE:
153 op = pa_context_get_sink_info_by_index(ctx, idx, sink_mod_cb,
156 case PA_SUBSCRIPTION_EVENT_REMOVE:
161 pa_operation_unref(op);
164 static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
167 audio_output_t *aout = userdata;
168 aout_sys_t *sys = aout->sys;
174 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
175 * amplification and maximum hardware amplification.
176 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
177 * Thus we need to use the sink base_volume as a multiplier,
178 * if and only if flat volume is active for our current sink. */
179 if (i->flags & PA_SINK_FLAT_VOLUME)
180 sys->base_volume = i->base_volume;
182 sys->base_volume = PA_VOLUME_NORM;
183 msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
187 /*** Latency management and lip synchronization ***/
188 static void stream_start_now(pa_stream *s, audio_output_t *aout)
192 assert (aout->sys->trigger == NULL);
194 op = pa_stream_cork(s, 0, NULL, NULL);
196 pa_operation_unref(op);
197 op = pa_stream_trigger(s, NULL, NULL);
198 if (likely(op != NULL))
199 pa_operation_unref(op);
202 static void stream_stop(pa_stream *s, audio_output_t *aout)
204 aout_sys_t *sys = aout->sys;
207 if (sys->trigger != NULL) {
208 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
212 op = pa_stream_cork(s, 1, NULL, NULL);
214 pa_operation_unref(op);
217 static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
218 const struct timeval *tv, void *userdata)
220 audio_output_t *aout = userdata;
221 aout_sys_t *sys = aout->sys;
223 assert (sys->trigger == e);
225 msg_Dbg(aout, "starting deferred");
226 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
228 stream_start_now(sys->stream, aout);
229 (void) api; (void) e; (void) tv;
233 * Starts or resumes the playback stream.
234 * Tries start playing back audio samples at the most accurate time
235 * in order to minimize desync and resampling during early playback.
236 * @note PulseAudio lock required.
238 static void stream_start(pa_stream *s, audio_output_t *aout)
240 aout_sys_t *sys = aout->sys;
243 assert (sys->first_pts != VLC_TS_INVALID);
245 if (sys->trigger != NULL) {
246 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
250 delta = vlc_pa_get_latency(aout, sys->context, s);
251 if (unlikely(delta == VLC_TS_INVALID)) {
252 msg_Dbg(aout, "cannot synchronize start");
253 delta = 0; /* screwed */
256 delta = (sys->first_pts - mdate()) - delta;
258 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
259 delta += pa_rtclock_now();
260 sys->trigger = pa_context_rttime_new(sys->context, delta,
261 stream_trigger_cb, aout);
263 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
264 stream_start_now(s, aout);
268 static void stream_latency_cb(pa_stream *s, void *userdata)
270 audio_output_t *aout = userdata;
271 aout_sys_t *sys = aout->sys;
273 /* This callback is _never_ called while paused. */
274 if (sys->first_pts == VLC_TS_INVALID)
275 return; /* nothing to do if buffers are (still) empty */
276 if (pa_stream_is_corked(s) > 0)
277 stream_start(s, aout);
281 /*** Stream helpers ***/
282 static void stream_state_cb(pa_stream *s, void *userdata)
284 pa_threaded_mainloop *mainloop = userdata;
286 switch (pa_stream_get_state(s)) {
287 case PA_STREAM_READY:
288 case PA_STREAM_FAILED:
289 case PA_STREAM_TERMINATED:
290 pa_threaded_mainloop_signal(mainloop, 0);
296 static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
298 audio_output_t *aout = userdata;
299 const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
301 msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
302 "prebuf=%u, minreq=%u",
303 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
306 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
309 audio_output_t *aout = userdata;
311 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
312 aout_PolicyReport(aout, true);
314 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
315 aout_PolicyReport(aout, false);
317 #if PA_CHECK_VERSION(1,0,0)
318 /* FIXME: expose aout_Restart() directly */
319 if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
320 msg_Dbg (aout, "format lost");
321 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
324 msg_Warn (aout, "unhandled stream event \"%s\"", name);
329 static void stream_moved_cb(pa_stream *s, void *userdata)
331 audio_output_t *aout = userdata;
332 aout_sys_t *sys = aout->sys;
333 const char *name = pa_stream_get_device_name(s);
336 msg_Dbg(aout, "connected to sink %s", name);
337 aout_DeviceReport(aout, name);
339 op = pa_context_get_sink_info_by_name(sys->context, name, sink_info_cb,
341 if (likely(op != NULL))
342 pa_operation_unref(op);
345 static void stream_overflow_cb(pa_stream *s, void *userdata)
347 audio_output_t *aout = userdata;
348 aout_sys_t *sys = aout->sys;
351 msg_Err(aout, "overflow, flushing");
352 op = pa_stream_flush(s, NULL, NULL);
353 if (unlikely(op == NULL))
355 pa_operation_unref(op);
356 sys->first_pts = VLC_TS_INVALID;
359 static void stream_started_cb(pa_stream *s, void *userdata)
361 audio_output_t *aout = userdata;
363 msg_Dbg(aout, "started");
367 static void stream_suspended_cb(pa_stream *s, void *userdata)
369 audio_output_t *aout = userdata;
371 msg_Dbg(aout, "suspended");
375 static void stream_underflow_cb(pa_stream *s, void *userdata)
377 audio_output_t *aout = userdata;
379 msg_Dbg(aout, "underflow");
383 static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
385 pa_stream_state_t state;
387 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
388 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
390 pa_threaded_mainloop_wait(mainloop);
397 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
398 int eol, void *userdata)
400 audio_output_t *aout = userdata;
401 aout_sys_t *sys = aout->sys;
407 sys->cvolume = i->volume; /* cache volume for balance preservation */
409 pa_volume_t volume = pa_cvolume_max(&i->volume);
410 volume = pa_sw_volume_divide(volume, sys->base_volume);
411 aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
412 aout_MuteReport(aout, i->mute);
415 static void sink_input_event(pa_context *ctx,
416 pa_subscription_event_type_t type,
417 uint32_t idx, audio_output_t *aout)
421 /* Gee... PA will not provide the infos directly in the event. */
424 case PA_SUBSCRIPTION_EVENT_REMOVE:
425 msg_Err(aout, "sink input killed!");
429 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
431 if (likely(op != NULL))
432 pa_operation_unref(op);
439 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
440 uint32_t idx, void *userdata)
442 audio_output_t *aout = userdata;
443 aout_sys_t *sys = aout->sys;
444 unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
446 type &= PA_SUBSCRIPTION_EVENT_TYPE_MASK;
449 case PA_SUBSCRIPTION_EVENT_SINK:
450 sink_event(ctx, type, idx, userdata);
453 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
454 /* only interested in our sink input */
455 if (sys->stream != NULL && idx == pa_stream_get_index(sys->stream))
456 sink_input_event(ctx, type, idx, userdata);
459 default: /* unsubscribed facility?! */
465 /*** VLC audio output callbacks ***/
467 static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
469 aout_sys_t *sys = aout->sys;
470 pa_stream *s = sys->stream;
472 if (pa_stream_is_corked(s) > 0)
473 return -1; /* latency is irrelevant if corked */
475 mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
476 if (delta == VLC_TS_INVALID)
483 /* Memory free callback. The block_t address is in front of the data. */
484 static void data_free(void *data)
486 block_t **pp = data, *block;
488 memcpy(&block, pp - 1, sizeof (block));
489 block_Release(block);
492 static void *data_convert(block_t **pp)
494 block_t *block = *pp;
495 /* In most cases, there is enough head room, and this is really cheap: */
496 block = block_Realloc(block, sizeof (block), block->i_buffer);
498 if (unlikely(block == NULL))
501 memcpy(block->p_buffer, &block, sizeof (block));
502 block->p_buffer += sizeof (block);
503 block->i_buffer -= sizeof (block);
504 return block->p_buffer;
508 * Queue one audio frame to the playback stream
510 static void Play(audio_output_t *aout, block_t *block)
512 aout_sys_t *sys = aout->sys;
513 pa_stream *s = sys->stream;
515 const void *ptr = data_convert(&block);
516 if (unlikely(ptr == NULL))
519 size_t len = block->i_buffer;
521 /* Note: The core already holds the output FIFO lock at this point.
522 * Therefore we must not under any circumstances (try to) acquire the
523 * output FIFO lock while the PulseAudio threaded main loop lock is held
524 * (including from PulseAudio stream callbacks). Otherwise lock inversion
525 * will take place, and sooner or later a deadlock. */
526 pa_threaded_mainloop_lock(sys->mainloop);
528 if (sys->first_pts == VLC_TS_INVALID)
529 sys->first_pts = block->i_pts;
531 if (pa_stream_is_corked(s) > 0)
532 stream_start(s, aout);
534 #if 0 /* Fault injector to test underrun recovery */
535 static volatile unsigned u = 0;
536 if ((++u % 1000) == 0) {
537 msg_Err(aout, "fault injection");
538 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
542 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
543 vlc_pa_error(aout, "cannot write", sys->context);
544 block_Release(block);
547 pa_threaded_mainloop_unlock(sys->mainloop);
551 * Cork or uncork the playback stream
553 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
555 aout_sys_t *sys = aout->sys;
556 pa_stream *s = sys->stream;
558 pa_threaded_mainloop_lock(sys->mainloop);
561 pa_stream_set_latency_update_callback(s, NULL, NULL);
562 stream_stop(s, aout);
564 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
565 if (likely(sys->first_pts != VLC_TS_INVALID))
566 stream_start_now(s, aout);
569 pa_threaded_mainloop_unlock(sys->mainloop);
574 * Flush or drain the playback stream
576 static void Flush(audio_output_t *aout, bool wait)
578 aout_sys_t *sys = aout->sys;
579 pa_stream *s = sys->stream;
582 pa_threaded_mainloop_lock(sys->mainloop);
585 op = pa_stream_drain(s, NULL, NULL);
586 /* TODO: wait for drain completion*/
588 op = pa_stream_flush(s, NULL, NULL);
590 pa_operation_unref(op);
591 pa_threaded_mainloop_unlock(sys->mainloop);
594 static int VolumeSet(audio_output_t *aout, float vol)
596 aout_sys_t *sys = aout->sys;
597 if (sys->stream == NULL)
599 msg_Err (aout, "cannot change volume while not playing");
603 /* VLC provides the software volume so convert directly to PulseAudio
604 * software volume, pa_volume_t. This is not a linear amplification factor
605 * so do not use PulseAudio linear amplification! */
606 vol *= PA_VOLUME_NORM;
607 if (unlikely(vol >= PA_VOLUME_MAX))
609 pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
611 /* Preserve the balance (VLC does not support it). */
612 pa_cvolume cvolume = sys->cvolume;
613 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
614 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
615 assert(pa_cvolume_valid(&cvolume));
618 uint32_t idx = pa_stream_get_index(sys->stream);
619 pa_threaded_mainloop_lock(sys->mainloop);
620 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
622 if (likely(op != NULL))
623 pa_operation_unref(op);
624 pa_threaded_mainloop_unlock(sys->mainloop);
629 static int MuteSet(audio_output_t *aout, bool mute)
631 aout_sys_t *sys = aout->sys;
633 if (sys->stream == NULL)
635 sys->flags_force &= ~(PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED);
637 mute ? PA_STREAM_START_MUTED : PA_STREAM_START_UNMUTED;
638 aout_MuteReport(aout, mute);
643 uint32_t idx = pa_stream_get_index(sys->stream);
644 pa_threaded_mainloop_lock(sys->mainloop);
645 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
646 if (likely(op != NULL))
647 pa_operation_unref(op);
648 pa_threaded_mainloop_unlock(sys->mainloop);
653 static int StreamMove(audio_output_t *aout, const char *name)
655 aout_sys_t *sys = aout->sys;
657 if (sys->stream == NULL)
659 msg_Dbg(aout, "will connect to sink %s", name);
660 free(sys->sink_force);
661 sys->sink_force = strdup(name);
666 uint32_t idx = pa_stream_get_index(sys->stream);
668 pa_threaded_mainloop_lock(sys->mainloop);
669 op = pa_context_move_sink_input_by_name(sys->context, idx, name,
671 if (likely(op != NULL)) {
672 pa_operation_unref(op);
673 msg_Dbg(aout, "moving to sink %s", name);
675 vlc_pa_error(aout, "cannot move sink input", sys->context);
676 pa_threaded_mainloop_unlock(sys->mainloop);
678 return (op != NULL) ? 0 : -1;
681 static void Stop(audio_output_t *);
684 * Create a PulseAudio playback stream, a.k.a. a sink input.
686 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
688 aout_sys_t *sys = aout->sys;
690 /* Sample format specification */
691 struct pa_sample_spec ss;
692 #if PA_CHECK_VERSION(1,0,0)
693 pa_encoding_t encoding = PA_ENCODING_INVALID;
696 switch (fmt->i_format)
699 fmt->i_format = VLC_CODEC_FL32;
701 ss.format = PA_SAMPLE_FLOAT32NE;
704 ss.format = PA_SAMPLE_S32NE;
707 ss.format = PA_SAMPLE_S16NE;
710 ss.format = PA_SAMPLE_U8;
712 #if PA_CHECK_VERSION(1,0,0)
714 fmt->i_format = VLC_CODEC_SPDIFL;
715 encoding = PA_ENCODING_AC3_IEC61937;
716 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
718 /*case VLC_CODEC_EAC3:
719 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
720 encoding = PA_ENCODING_EAC3_IEC61937;
721 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
724 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
725 encoding = PA_ENCODING_MPEG_IEC61937;
726 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
729 fmt->i_format = VLC_CODEC_SPDIFL;
730 encoding = PA_ENCODING_DTS_IEC61937;
731 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
737 fmt->i_format = VLC_CODEC_FL32;
738 ss.format = PA_SAMPLE_FLOAT32NE;
742 fmt->i_format = VLC_CODEC_S16N;
743 ss.format = PA_SAMPLE_S16NE;
748 ss.rate = fmt->i_rate;
749 ss.channels = aout_FormatNbChannels(fmt);
750 if (!pa_sample_spec_valid(&ss)) {
751 msg_Err(aout, "unsupported sample specification");
755 /* Channel mapping (order defined in vlc_aout.h) */
756 struct pa_channel_map map;
759 if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
760 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
761 if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
762 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
763 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
764 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
765 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
766 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
767 if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
768 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
769 if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
770 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
771 if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
772 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
773 if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
775 if (ss.channels == 1)
776 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
778 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
780 if (fmt->i_physical_channels & AOUT_CHAN_LFE)
781 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
783 for (unsigned i = 0; map.channels < ss.channels; i++) {
784 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
785 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
788 if (!pa_channel_map_valid(&map)) {
789 msg_Err(aout, "unsupported channel map");
792 const char *name = pa_channel_map_to_name(&map);
793 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
796 /* Stream parameters */
797 pa_stream_flags_t flags = sys->flags_force
798 | PA_STREAM_START_CORKED
799 | PA_STREAM_INTERPOLATE_TIMING
800 | PA_STREAM_NOT_MONOTONIC
801 | PA_STREAM_AUTO_TIMING_UPDATE
802 | PA_STREAM_FIX_RATE;
804 struct pa_buffer_attr attr;
806 /* PulseAudio goes berserk if the target length (tlength) is not
807 * significantly longer than 2 periods (minreq), or when the period length
808 * is unspecified and the target length is short. */
809 attr.tlength = pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME, &ss);
810 attr.prebuf = 0; /* trigger manually */
811 attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
812 attr.fragsize = 0; /* not used for output */
816 sys->first_pts = VLC_TS_INVALID;
819 sys->base_volume = PA_VOLUME_NORM;
820 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
822 #if PA_CHECK_VERSION(1,0,0)
823 pa_format_info *formatv[2];
824 unsigned formatc = 0;
826 /* Favor digital pass-through if available*/
827 if (encoding != PA_ENCODING_INVALID) {
828 formatv[formatc] = pa_format_info_new();
829 formatv[formatc]->encoding = encoding;
830 pa_format_info_set_rate(formatv[formatc], ss.rate);
831 pa_format_info_set_channels(formatv[formatc], ss.channels);
832 pa_format_info_set_channel_map(formatv[formatc], &map);
835 /* FIX flags are only permitted for PCM, and there is no way to pass
836 * different flags for different formats... */
837 flags &= ~(PA_STREAM_FIX_FORMAT
839 | PA_STREAM_FIX_CHANNELS);
842 /* Fallback to PCM */
843 formatv[formatc] = pa_format_info_new();
844 formatv[formatc]->encoding = PA_ENCODING_PCM;
845 pa_format_info_set_sample_format(formatv[formatc], ss.format);
846 pa_format_info_set_rate(formatv[formatc], ss.rate);
847 pa_format_info_set_channels(formatv[formatc], ss.channels);
848 pa_format_info_set_channel_map(formatv[formatc], &map);
851 /* Create a playback stream */
853 pa_proplist *props = pa_proplist_new();
854 if (likely(props != NULL))
855 /* TODO: set other stream properties */
856 pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
858 pa_threaded_mainloop_lock(sys->mainloop);
859 s = pa_stream_new_extended(sys->context, "audio stream", formatv, formatc,
861 if (likely(props != NULL))
862 pa_proplist_free(props);
864 for (unsigned i = 0; i < formatc; i++)
865 pa_format_info_free(formatv[i]);
867 pa_threaded_mainloop_lock(sys->mainloop);
868 pa_stream *s = pa_stream_new(sys->context, "audio stream", &ss, &map);
871 pa_threaded_mainloop_unlock(sys->mainloop);
872 vlc_pa_error(aout, "stream creation failure", sys->context);
876 pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
877 pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
878 pa_stream_set_event_callback(s, stream_event_cb, aout);
879 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
880 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
881 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
882 pa_stream_set_started_callback(s, stream_started_cb, aout);
883 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
884 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
886 if (pa_stream_connect_playback(s, sys->sink_force, &attr, flags, NULL,
888 || stream_wait(s, sys->mainloop)) {
889 vlc_pa_error(aout, "stream connection failure", sys->context);
892 sys->flags_force = PA_STREAM_NOFLAGS;
893 free(sys->sink_force);
894 sys->sink_force = NULL;
896 const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
897 #if PA_CHECK_VERSION(1,0,0)
898 if (encoding != PA_ENCODING_INVALID) {
899 const pa_format_info *info = pa_stream_get_format_info(s);
901 assert (info != NULL);
902 if (pa_format_info_is_pcm (info)) {
903 msg_Dbg(aout, "digital pass-through not available");
904 fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
906 msg_Dbg(aout, "digital pass-through enabled");
912 fmt->i_rate = spec->rate;
914 stream_buffer_attr_cb(s, aout);
915 stream_moved_cb(s, aout);
916 pa_threaded_mainloop_unlock(sys->mainloop);
921 pa_threaded_mainloop_unlock(sys->mainloop);
927 * Removes a PulseAudio playback stream
929 static void Stop(audio_output_t *aout)
931 aout_sys_t *sys = aout->sys;
932 pa_stream *s = sys->stream;
934 pa_threaded_mainloop_lock(sys->mainloop);
935 if (unlikely(sys->trigger != NULL))
936 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
937 pa_stream_disconnect(s);
939 /* Clear all callbacks */
940 pa_stream_set_state_callback(s, NULL, NULL);
941 pa_stream_set_buffer_attr_callback(s, NULL, NULL);
942 pa_stream_set_event_callback(s, NULL, NULL);
943 pa_stream_set_latency_update_callback(s, NULL, NULL);
944 pa_stream_set_moved_callback(s, NULL, NULL);
945 pa_stream_set_overflow_callback(s, NULL, NULL);
946 pa_stream_set_started_callback(s, NULL, NULL);
947 pa_stream_set_suspended_callback(s, NULL, NULL);
948 pa_stream_set_underflow_callback(s, NULL, NULL);
952 pa_threaded_mainloop_unlock(sys->mainloop);
955 static int Open(vlc_object_t *obj)
957 audio_output_t *aout = (audio_output_t *)obj;
958 aout_sys_t *sys = malloc(sizeof (*sys));
961 #if !PA_CHECK_VERSION(0,9,22)
962 if (!vlc_xlib_init(obj))
965 if (unlikely(sys == NULL))
968 /* Allocate structures */
969 pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
977 sys->flags_force = PA_STREAM_NOFLAGS;
978 sys->sink_force = NULL;
984 aout->time_get = TimeGet;
988 aout->volume_set = VolumeSet;
989 aout->mute_set = MuteSet;
990 aout->device_select = StreamMove;
992 pa_threaded_mainloop_lock(sys->mainloop);
993 /* Sinks (output devices) list */
994 op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
996 pa_operation_unref(op);
999 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
1000 | PA_SUBSCRIPTION_MASK_SINK_INPUT;
1001 pa_context_set_subscribe_callback(sys->context, context_cb, aout);
1002 op = pa_context_subscribe(sys->context, mask, NULL, NULL);
1003 if (likely(op != NULL))
1004 pa_operation_unref(op);
1005 pa_threaded_mainloop_unlock(sys->mainloop);
1010 static void Close(vlc_object_t *obj)
1012 audio_output_t *aout = (audio_output_t *)obj;
1013 aout_sys_t *sys = aout->sys;
1014 pa_context *ctx = sys->context;
1016 pa_threaded_mainloop_lock(sys->mainloop);
1017 pa_context_set_subscribe_callback(sys->context, NULL, NULL);
1018 pa_threaded_mainloop_unlock(sys->mainloop);
1019 vlc_pa_disconnect(obj, ctx, sys->mainloop);
1021 for (struct sink *sink = sys->sinks, *next; sink != NULL; sink = next)
1026 free(sys->sink_force);