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 */
75 mtime_t paused; /**< Time when (last) paused */
77 pa_stream_flags_t flags_force; /**< Forced flags (stream must be NULL) */
78 char *sink_force; /**< Forced sink name (stream must be NULL) */
80 struct sink *sinks; /**< Locally-cached list of sinks */
85 static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
88 audio_output_t *aout = userdata;
89 aout_sys_t *sys = aout->sys;
95 msg_Dbg(aout, "adding sink %"PRIu32": %s (%s)", i->index, i->name,
97 aout_HotplugReport(aout, i->name, i->description);
99 size_t namelen = strlen(i->name);
100 struct sink *sink = malloc(sizeof (*sink) + namelen);
101 if (unlikely(sink == NULL))
104 sink->next = sys->sinks;
105 sink->index = i->index;
106 memcpy(sink->name, i->name, namelen + 1);
110 static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
113 audio_output_t *aout = userdata;
119 msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
121 aout_HotplugReport(aout, i->name, i->description);
124 static void sink_del(uint32_t index, audio_output_t *aout)
126 aout_sys_t *sys = aout->sys;
127 struct sink **pp = &sys->sinks, *sink;
129 msg_Dbg(aout, "removing sink %"PRIu32, index);
131 while ((sink = *pp) != NULL)
132 if (sink->index == index)
135 aout_HotplugReport(aout, sink->name, NULL);
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)
193 assert (aout->sys->trigger == NULL);
195 op = pa_stream_cork(s, 0, NULL, NULL);
197 pa_operation_unref(op);
198 op = pa_stream_trigger(s, NULL, NULL);
199 if (likely(op != NULL))
200 pa_operation_unref(op);
203 static void stream_stop(pa_stream *s, audio_output_t *aout)
205 aout_sys_t *sys = aout->sys;
208 if (sys->trigger != NULL) {
209 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
213 op = pa_stream_cork(s, 1, NULL, NULL);
215 pa_operation_unref(op);
218 static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
219 const struct timeval *tv, void *userdata)
221 audio_output_t *aout = userdata;
222 aout_sys_t *sys = aout->sys;
224 assert (sys->trigger == e);
226 msg_Dbg(aout, "starting deferred");
227 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
229 stream_start_now(sys->stream, aout);
230 (void) api; (void) e; (void) tv;
234 * Starts or resumes the playback stream.
235 * Tries start playing back audio samples at the most accurate time
236 * in order to minimize desync and resampling during early playback.
237 * @note PulseAudio lock required.
239 static void stream_start(pa_stream *s, audio_output_t *aout)
241 aout_sys_t *sys = aout->sys;
244 assert (sys->first_pts != VLC_TS_INVALID);
246 if (sys->trigger != NULL) {
247 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
251 delta = vlc_pa_get_latency(aout, sys->context, s);
252 if (unlikely(delta == VLC_TS_INVALID)) {
253 msg_Dbg(aout, "cannot synchronize start");
254 delta = 0; /* screwed */
257 delta = (sys->first_pts - mdate()) - delta;
259 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
260 delta += pa_rtclock_now();
261 sys->trigger = pa_context_rttime_new(sys->context, delta,
262 stream_trigger_cb, aout);
264 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
265 stream_start_now(s, aout);
269 static void stream_latency_cb(pa_stream *s, void *userdata)
271 audio_output_t *aout = userdata;
272 aout_sys_t *sys = aout->sys;
274 if (sys->paused != VLC_TS_INVALID)
275 return; /* nothing to do while paused */
276 if (sys->first_pts == VLC_TS_INVALID)
277 return; /* nothing to do if buffers are (still) empty */
278 if (pa_stream_is_corked(s) > 0)
279 stream_start(s, aout);
283 /*** Stream helpers ***/
284 static void stream_state_cb(pa_stream *s, void *userdata)
286 pa_threaded_mainloop *mainloop = userdata;
288 switch (pa_stream_get_state(s)) {
289 case PA_STREAM_READY:
290 case PA_STREAM_FAILED:
291 case PA_STREAM_TERMINATED:
292 pa_threaded_mainloop_signal(mainloop, 0);
298 static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
300 audio_output_t *aout = userdata;
301 const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
303 msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
304 "prebuf=%u, minreq=%u",
305 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
308 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
311 audio_output_t *aout = userdata;
313 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
314 aout_PolicyReport(aout, true);
316 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
317 aout_PolicyReport(aout, false);
319 #if PA_CHECK_VERSION(1,0,0)
320 /* FIXME: expose aout_Restart() directly */
321 if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
322 msg_Dbg (aout, "format lost");
323 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
326 msg_Warn (aout, "unhandled stream event \"%s\"", name);
331 static void stream_moved_cb(pa_stream *s, void *userdata)
333 audio_output_t *aout = userdata;
334 aout_sys_t *sys = aout->sys;
335 const char *name = pa_stream_get_device_name(s);
338 msg_Dbg(aout, "connected to sink %s", name);
339 aout_DeviceReport(aout, name);
341 op = pa_context_get_sink_info_by_name(sys->context, name, sink_info_cb,
343 if (likely(op != NULL))
344 pa_operation_unref(op);
347 static void stream_overflow_cb(pa_stream *s, void *userdata)
349 audio_output_t *aout = userdata;
350 aout_sys_t *sys = aout->sys;
353 msg_Err(aout, "overflow, flushing");
354 op = pa_stream_flush(s, NULL, NULL);
355 if (unlikely(op == NULL))
357 pa_operation_unref(op);
358 sys->first_pts = VLC_TS_INVALID;
361 static void stream_started_cb(pa_stream *s, void *userdata)
363 audio_output_t *aout = userdata;
365 msg_Dbg(aout, "started");
369 static void stream_suspended_cb(pa_stream *s, void *userdata)
371 audio_output_t *aout = userdata;
373 msg_Dbg(aout, "suspended");
377 static void stream_underflow_cb(pa_stream *s, void *userdata)
379 audio_output_t *aout = userdata;
381 msg_Dbg(aout, "underflow");
385 static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
387 pa_stream_state_t state;
389 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
390 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
392 pa_threaded_mainloop_wait(mainloop);
399 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
400 int eol, void *userdata)
402 audio_output_t *aout = userdata;
403 aout_sys_t *sys = aout->sys;
409 sys->cvolume = i->volume; /* cache volume for balance preservation */
411 pa_volume_t volume = pa_cvolume_max(&i->volume);
412 volume = pa_sw_volume_divide(volume, sys->base_volume);
413 aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
414 aout_MuteReport(aout, i->mute);
417 static void sink_input_event(pa_context *ctx,
418 pa_subscription_event_type_t type,
419 uint32_t idx, audio_output_t *aout)
423 /* Gee... PA will not provide the infos directly in the event. */
426 case PA_SUBSCRIPTION_EVENT_REMOVE:
427 msg_Err(aout, "sink input killed!");
431 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
433 if (likely(op != NULL))
434 pa_operation_unref(op);
441 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
442 uint32_t idx, void *userdata)
444 audio_output_t *aout = userdata;
445 aout_sys_t *sys = aout->sys;
446 unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
448 type &= PA_SUBSCRIPTION_EVENT_TYPE_MASK;
451 case PA_SUBSCRIPTION_EVENT_SINK:
452 sink_event(ctx, type, idx, userdata);
455 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
456 /* only interested in our sink input */
457 if (sys->stream != NULL && idx == pa_stream_get_index(sys->stream))
458 sink_input_event(ctx, type, idx, userdata);
461 default: /* unsubscribed facility?! */
467 /*** VLC audio output callbacks ***/
469 static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
471 aout_sys_t *sys = aout->sys;
472 pa_stream *s = sys->stream;
474 if (pa_stream_is_corked(s) > 0)
475 return -1; /* latency is irrelevant if corked */
477 mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
478 if (delta == VLC_TS_INVALID)
485 /* Memory free callback. The block_t address is in front of the data. */
486 static void data_free(void *data)
488 block_t **pp = data, *block;
490 memcpy(&block, pp - 1, sizeof (block));
491 block_Release(block);
494 static void *data_convert(block_t **pp)
496 block_t *block = *pp;
497 /* In most cases, there is enough head room, and this is really cheap: */
498 block = block_Realloc(block, sizeof (block), block->i_buffer);
500 if (unlikely(block == NULL))
503 memcpy(block->p_buffer, &block, sizeof (block));
504 block->p_buffer += sizeof (block);
505 block->i_buffer -= sizeof (block);
506 return block->p_buffer;
510 * Queue one audio frame to the playback stream
512 static void Play(audio_output_t *aout, block_t *block)
514 aout_sys_t *sys = aout->sys;
515 pa_stream *s = sys->stream;
517 assert (sys->paused == VLC_TS_INVALID);
519 const void *ptr = data_convert(&block);
520 if (unlikely(ptr == NULL))
523 size_t len = block->i_buffer;
525 /* Note: The core already holds the output FIFO lock at this point.
526 * Therefore we must not under any circumstances (try to) acquire the
527 * output FIFO lock while the PulseAudio threaded main loop lock is held
528 * (including from PulseAudio stream callbacks). Otherwise lock inversion
529 * will take place, and sooner or later a deadlock. */
530 pa_threaded_mainloop_lock(sys->mainloop);
532 if (sys->first_pts == VLC_TS_INVALID)
533 sys->first_pts = block->i_pts;
535 if (pa_stream_is_corked(s) > 0)
536 stream_start(s, aout);
538 #if 0 /* Fault injector to test underrun recovery */
539 static volatile unsigned u = 0;
540 if ((++u % 1000) == 0) {
541 msg_Err(aout, "fault injection");
542 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
546 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
547 vlc_pa_error(aout, "cannot write", sys->context);
548 block_Release(block);
551 pa_threaded_mainloop_unlock(sys->mainloop);
555 * Cork or uncork the playback stream
557 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
559 aout_sys_t *sys = aout->sys;
560 pa_stream *s = sys->stream;
562 pa_threaded_mainloop_lock(sys->mainloop);
566 stream_stop(s, aout);
568 assert (sys->paused != VLC_TS_INVALID);
570 msg_Dbg(aout, "resuming after %"PRId64" us", date);
571 sys->paused = VLC_TS_INVALID;
573 if (sys->first_pts != VLC_TS_INVALID) {
574 sys->first_pts += date;
575 stream_start(s, aout);
579 pa_threaded_mainloop_unlock(sys->mainloop);
583 * Flush or drain the playback stream
585 static void Flush(audio_output_t *aout, bool wait)
587 aout_sys_t *sys = aout->sys;
588 pa_stream *s = sys->stream;
591 pa_threaded_mainloop_lock(sys->mainloop);
594 op = pa_stream_drain(s, NULL, NULL);
595 /* TODO: wait for drain completion*/
597 op = pa_stream_flush(s, NULL, NULL);
599 pa_operation_unref(op);
600 pa_threaded_mainloop_unlock(sys->mainloop);
603 static int VolumeSet(audio_output_t *aout, float vol)
605 aout_sys_t *sys = aout->sys;
606 if (sys->stream == NULL)
608 msg_Err (aout, "cannot change volume while not playing");
612 /* VLC provides the software volume so convert directly to PulseAudio
613 * software volume, pa_volume_t. This is not a linear amplification factor
614 * so do not use PulseAudio linear amplification! */
615 vol *= PA_VOLUME_NORM;
616 if (unlikely(vol >= PA_VOLUME_MAX))
618 pa_volume_t volume = pa_sw_volume_multiply(lround(vol), sys->base_volume);
620 /* Preserve the balance (VLC does not support it). */
621 pa_cvolume cvolume = sys->cvolume;
622 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
623 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
624 assert(pa_cvolume_valid(&cvolume));
627 uint32_t idx = pa_stream_get_index(sys->stream);
628 pa_threaded_mainloop_lock(sys->mainloop);
629 op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume,
631 if (likely(op != NULL))
632 pa_operation_unref(op);
633 pa_threaded_mainloop_unlock(sys->mainloop);
638 static int MuteSet(audio_output_t *aout, bool mute)
640 aout_sys_t *sys = aout->sys;
642 if (sys->stream == NULL)
644 sys->flags_force &= ~(PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED);
646 mute ? PA_STREAM_START_MUTED : PA_STREAM_START_UNMUTED;
647 aout_MuteReport(aout, mute);
652 uint32_t idx = pa_stream_get_index(sys->stream);
653 pa_threaded_mainloop_lock(sys->mainloop);
654 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
655 if (likely(op != NULL))
656 pa_operation_unref(op);
657 pa_threaded_mainloop_unlock(sys->mainloop);
662 static int StreamMove(audio_output_t *aout, const char *name)
664 aout_sys_t *sys = aout->sys;
666 if (sys->stream == NULL)
668 msg_Dbg(aout, "will connect to sink %s", name);
669 free(sys->sink_force);
670 sys->sink_force = strdup(name);
675 uint32_t idx = pa_stream_get_index(sys->stream);
677 pa_threaded_mainloop_lock(sys->mainloop);
678 op = pa_context_move_sink_input_by_name(sys->context, idx, name,
680 if (likely(op != NULL)) {
681 pa_operation_unref(op);
682 msg_Dbg(aout, "moving to sink %s", name);
684 vlc_pa_error(aout, "cannot move sink input", sys->context);
685 pa_threaded_mainloop_unlock(sys->mainloop);
687 return (op != NULL) ? 0 : -1;
690 static void Stop(audio_output_t *);
693 * Create a PulseAudio playback stream, a.k.a. a sink input.
695 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
697 aout_sys_t *sys = aout->sys;
699 /* Sample format specification */
700 struct pa_sample_spec ss;
701 #if PA_CHECK_VERSION(1,0,0)
702 pa_encoding_t encoding = PA_ENCODING_INVALID;
705 switch (fmt->i_format)
708 fmt->i_format = VLC_CODEC_FL32;
710 ss.format = PA_SAMPLE_FLOAT32NE;
713 ss.format = PA_SAMPLE_S32NE;
716 ss.format = PA_SAMPLE_S16NE;
719 ss.format = PA_SAMPLE_U8;
721 #if PA_CHECK_VERSION(1,0,0)
723 fmt->i_format = VLC_CODEC_SPDIFL;
724 encoding = PA_ENCODING_AC3_IEC61937;
725 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
727 /*case VLC_CODEC_EAC3:
728 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
729 encoding = PA_ENCODING_EAC3_IEC61937;
730 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
733 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
734 encoding = PA_ENCODING_MPEG_IEC61937;
735 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
738 fmt->i_format = VLC_CODEC_SPDIFL;
739 encoding = PA_ENCODING_DTS_IEC61937;
740 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
746 fmt->i_format = VLC_CODEC_FL32;
747 ss.format = PA_SAMPLE_FLOAT32NE;
751 fmt->i_format = VLC_CODEC_S16N;
752 ss.format = PA_SAMPLE_S16NE;
757 ss.rate = fmt->i_rate;
758 ss.channels = aout_FormatNbChannels(fmt);
759 if (!pa_sample_spec_valid(&ss)) {
760 msg_Err(aout, "unsupported sample specification");
764 /* Channel mapping (order defined in vlc_aout.h) */
765 struct pa_channel_map map;
768 if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
769 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
770 if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
771 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
772 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
773 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
774 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
775 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
776 if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
777 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
778 if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
779 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
780 if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
781 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
782 if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
784 if (ss.channels == 1)
785 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
787 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
789 if (fmt->i_physical_channels & AOUT_CHAN_LFE)
790 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
792 for (unsigned i = 0; map.channels < ss.channels; i++) {
793 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
794 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
797 if (!pa_channel_map_valid(&map)) {
798 msg_Err(aout, "unsupported channel map");
801 const char *name = pa_channel_map_to_name(&map);
802 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
805 /* Stream parameters */
806 const pa_stream_flags_t flags = sys->flags_force
807 | PA_STREAM_START_CORKED
808 | PA_STREAM_INTERPOLATE_TIMING
809 | PA_STREAM_NOT_MONOTONIC
810 | PA_STREAM_AUTO_TIMING_UPDATE
811 | PA_STREAM_FIX_RATE;
813 struct pa_buffer_attr attr;
815 /* PulseAudio goes berserk if the target length (tlength) is not
816 * significantly longer than 2 periods (minreq), or when the period length
817 * is unspecified and the target length is short. */
818 attr.tlength = pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME, &ss);
819 attr.prebuf = 0; /* trigger manually */
820 attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
821 attr.fragsize = 0; /* not used for output */
825 sys->first_pts = VLC_TS_INVALID;
826 sys->paused = VLC_TS_INVALID;
829 sys->base_volume = PA_VOLUME_NORM;
830 pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
832 #if PA_CHECK_VERSION(1,0,0)
833 pa_format_info *formatv[2];
834 unsigned formatc = 0;
836 /* Favor digital pass-through if available*/
837 if (encoding != PA_ENCODING_INVALID) {
838 formatv[formatc] = pa_format_info_new();
839 formatv[formatc]->encoding = encoding;
840 pa_format_info_set_rate(formatv[formatc], ss.rate);
841 pa_format_info_set_channels(formatv[formatc], ss.channels);
842 pa_format_info_set_channel_map(formatv[formatc], &map);
846 /* Fallback to PCM */
847 formatv[formatc] = pa_format_info_new();
848 formatv[formatc]->encoding = PA_ENCODING_PCM;
849 pa_format_info_set_sample_format(formatv[formatc], ss.format);
850 pa_format_info_set_rate(formatv[formatc], ss.rate);
851 pa_format_info_set_channels(formatv[formatc], ss.channels);
852 pa_format_info_set_channel_map(formatv[formatc], &map);
855 /* Create a playback stream */
857 pa_proplist *props = pa_proplist_new();
858 if (likely(props != NULL))
859 /* TODO: set other stream properties */
860 pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
862 pa_threaded_mainloop_lock(sys->mainloop);
863 s = pa_stream_new_extended(sys->context, "audio stream", formatv, formatc,
865 if (likely(props != NULL))
866 pa_proplist_free(props);
868 for (unsigned i = 0; i < formatc; i++)
869 pa_format_info_free(formatv[i]);
871 pa_threaded_mainloop_lock(sys->mainloop);
872 pa_stream *s = pa_stream_new(sys->context, "audio stream", &ss, &map);
875 pa_threaded_mainloop_unlock(sys->mainloop);
876 vlc_pa_error(aout, "stream creation failure", sys->context);
880 pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
881 pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
882 pa_stream_set_event_callback(s, stream_event_cb, aout);
883 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
884 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
885 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
886 pa_stream_set_started_callback(s, stream_started_cb, aout);
887 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
888 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
890 if (pa_stream_connect_playback(s, sys->sink_force, &attr, flags, NULL,
892 || stream_wait(s, sys->mainloop)) {
893 vlc_pa_error(aout, "stream connection failure", sys->context);
896 sys->flags_force = PA_STREAM_NOFLAGS;
897 free(sys->sink_force);
898 sys->sink_force = NULL;
900 const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
901 #if PA_CHECK_VERSION(1,0,0)
902 if (encoding != PA_ENCODING_INVALID) {
903 const pa_format_info *info = pa_stream_get_format_info(s);
905 assert (info != NULL);
906 if (pa_format_info_is_pcm (info)) {
907 msg_Dbg(aout, "digital pass-through not available");
908 fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
910 msg_Dbg(aout, "digital pass-through enabled");
916 fmt->i_rate = spec->rate;
918 stream_buffer_attr_cb(s, aout);
919 stream_moved_cb(s, aout);
920 pa_threaded_mainloop_unlock(sys->mainloop);
925 pa_threaded_mainloop_unlock(sys->mainloop);
931 * Removes a PulseAudio playback stream
933 static void Stop(audio_output_t *aout)
935 aout_sys_t *sys = aout->sys;
936 pa_stream *s = sys->stream;
938 pa_threaded_mainloop_lock(sys->mainloop);
939 if (unlikely(sys->trigger != NULL))
940 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
941 pa_stream_disconnect(s);
943 /* Clear all callbacks */
944 pa_stream_set_state_callback(s, NULL, NULL);
945 pa_stream_set_buffer_attr_callback(s, NULL, NULL);
946 pa_stream_set_event_callback(s, NULL, NULL);
947 pa_stream_set_latency_update_callback(s, NULL, NULL);
948 pa_stream_set_moved_callback(s, NULL, NULL);
949 pa_stream_set_overflow_callback(s, NULL, NULL);
950 pa_stream_set_started_callback(s, NULL, NULL);
951 pa_stream_set_suspended_callback(s, NULL, NULL);
952 pa_stream_set_underflow_callback(s, NULL, NULL);
956 pa_threaded_mainloop_unlock(sys->mainloop);
959 static int Open(vlc_object_t *obj)
961 audio_output_t *aout = (audio_output_t *)obj;
962 aout_sys_t *sys = malloc(sizeof (*sys));
965 #if !PA_CHECK_VERSION(0,9,22)
966 if (!vlc_xlib_init(obj))
969 if (unlikely(sys == NULL))
972 /* Allocate structures */
973 pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
981 sys->flags_force = PA_STREAM_NOFLAGS;
982 sys->sink_force = NULL;
988 aout->time_get = TimeGet;
992 aout->volume_set = VolumeSet;
993 aout->mute_set = MuteSet;
994 aout->device_select = StreamMove;
996 pa_threaded_mainloop_lock(sys->mainloop);
997 /* Sinks (output devices) list */
998 op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
1000 pa_operation_unref(op);
1002 /* Context events */
1003 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
1004 | PA_SUBSCRIPTION_MASK_SINK_INPUT;
1005 pa_context_set_subscribe_callback(sys->context, context_cb, aout);
1006 op = pa_context_subscribe(sys->context, mask, NULL, NULL);
1007 if (likely(op != NULL))
1008 pa_operation_unref(op);
1009 pa_threaded_mainloop_unlock(sys->mainloop);
1014 static void Close(vlc_object_t *obj)
1016 audio_output_t *aout = (audio_output_t *)obj;
1017 aout_sys_t *sys = aout->sys;
1018 pa_context *ctx = sys->context;
1020 pa_threaded_mainloop_lock(sys->mainloop);
1021 pa_context_set_subscribe_callback(sys->context, NULL, NULL);
1022 pa_threaded_mainloop_unlock(sys->mainloop);
1023 vlc_pa_disconnect(obj, ctx, sys->mainloop);
1025 for (struct sink *sink = sys->sinks, *next; sink != NULL; sink = next)
1030 free(sys->sink_force);