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>
35 #include "audio_output/vlcpulse.h"
37 static int Open ( vlc_object_t * );
38 static void Close ( vlc_object_t * );
41 set_shortname( "PulseAudio" )
42 set_description( N_("Pulseaudio audio output") )
43 set_capability( "audio output", 160 )
44 set_category( CAT_AUDIO )
45 set_subcategory( SUBCAT_AUDIO_AOUT )
46 add_shortcut( "pulseaudio", "pa" )
47 set_callbacks( Open, Close )
51 * Be careful what you do when the PulseAudio mainloop is held, which is to say
52 * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
53 * In particular, a VLC variable callback cannot be triggered nor deleted with
54 * the PulseAudio mainloop lock held, if the callback acquires the lock. */
60 pa_volume_t base_volume;
66 pa_stream *stream; /**< PulseAudio playback stream object */
67 pa_context *context; /**< PulseAudio connection context */
68 pa_threaded_mainloop *mainloop; /**< PulseAudio thread */
69 pa_time_event *trigger; /**< Deferred stream trigger */
70 pa_volume_t base_volume; /**< 0dB reference volume */
71 pa_cvolume cvolume; /**< actual sink input volume */
72 mtime_t first_pts; /**< Play time of buffer start */
74 pa_volume_t volume_force; /**< Forced volume (stream must be NULL) */
75 pa_stream_flags_t flags_force; /**< Forced flags (stream must be NULL) */
76 char *sink_force; /**< Forced sink name (stream must be NULL) */
78 struct sink *sinks; /**< Locally-cached list of sinks */
81 static void VolumeReport(audio_output_t *aout)
83 aout_sys_t *sys = aout->sys;
84 pa_volume_t volume = pa_cvolume_max(&sys->cvolume);
86 volume = pa_sw_volume_divide(volume, sys->base_volume);
87 aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
91 static struct sink *sink_find(aout_sys_t *sys, uint32_t index)
93 for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
94 if (sink->index == index)
99 static struct sink *sink_find_by_name(aout_sys_t *sys, const char *name)
101 for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
102 if (!strcmp(sink->name, name))
107 static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
110 audio_output_t *aout = userdata;
111 aout_sys_t *sys = aout->sys;
115 pa_threaded_mainloop_signal(sys->mainloop, 0);
120 msg_Dbg(aout, "adding sink %"PRIu32": %s (%s)", i->index, i->name,
122 aout_HotplugReport(aout, i->name, i->description);
124 size_t namelen = strlen(i->name);
125 struct sink *sink = malloc(sizeof (*sink) + namelen);
126 if (unlikely(sink == NULL))
129 sink->next = sys->sinks;
130 sink->index = i->index;
131 /* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
132 * amplification and maximum hardware amplification.
133 * VLC maps DEFAULT / 100% to no gain at all (software/hardware).
134 * Thus we need to use the sink base_volume as a multiplier,
135 * if and only if flat volume is active for our current sink. */
136 if (i->flags & PA_SINK_FLAT_VOLUME)
137 sink->base_volume = i->base_volume;
139 sink->base_volume = PA_VOLUME_NORM;
140 memcpy(sink->name, i->name, namelen + 1);
144 static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
147 audio_output_t *aout = userdata;
148 aout_sys_t *sys = aout->sys;
154 msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
156 aout_HotplugReport(aout, i->name, i->description);
158 struct sink *sink = sink_find(sys, i->index);
159 if (unlikely(sink == NULL))
162 if (i->flags & PA_SINK_FLAT_VOLUME)
163 sink->base_volume = i->base_volume;
165 sink->base_volume = PA_VOLUME_NORM;
168 static void sink_del(uint32_t index, audio_output_t *aout)
170 aout_sys_t *sys = aout->sys;
171 struct sink **pp = &sys->sinks, *sink;
173 msg_Dbg(aout, "removing sink %"PRIu32, index);
175 while ((sink = *pp) != NULL)
176 if (sink->index == index)
179 aout_HotplugReport(aout, sink->name, NULL);
186 static void sink_event(pa_context *ctx, unsigned type, uint32_t idx,
187 audio_output_t *aout)
189 pa_operation *op = NULL;
193 case PA_SUBSCRIPTION_EVENT_NEW:
194 op = pa_context_get_sink_info_by_index(ctx, idx, sink_add_cb,
197 case PA_SUBSCRIPTION_EVENT_CHANGE:
198 op = pa_context_get_sink_info_by_index(ctx, idx, sink_mod_cb,
201 case PA_SUBSCRIPTION_EVENT_REMOVE:
206 pa_operation_unref(op);
210 /*** Latency management and lip synchronization ***/
211 static void stream_start_now(pa_stream *s, audio_output_t *aout)
215 assert (aout->sys->trigger == NULL);
217 op = pa_stream_cork(s, 0, NULL, NULL);
219 pa_operation_unref(op);
220 op = pa_stream_trigger(s, NULL, NULL);
221 if (likely(op != NULL))
222 pa_operation_unref(op);
225 static void stream_stop(pa_stream *s, audio_output_t *aout)
227 aout_sys_t *sys = aout->sys;
230 if (sys->trigger != NULL) {
231 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
235 op = pa_stream_cork(s, 1, NULL, NULL);
237 pa_operation_unref(op);
240 static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
241 const struct timeval *tv, void *userdata)
243 audio_output_t *aout = userdata;
244 aout_sys_t *sys = aout->sys;
246 assert (sys->trigger == e);
248 msg_Dbg(aout, "starting deferred");
249 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
251 stream_start_now(sys->stream, aout);
252 (void) api; (void) e; (void) tv;
256 * Starts or resumes the playback stream.
257 * Tries start playing back audio samples at the most accurate time
258 * in order to minimize desync and resampling during early playback.
259 * @note PulseAudio lock required.
261 static void stream_start(pa_stream *s, audio_output_t *aout)
263 aout_sys_t *sys = aout->sys;
266 assert (sys->first_pts != VLC_TS_INVALID);
268 if (sys->trigger != NULL) {
269 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
273 delta = vlc_pa_get_latency(aout, sys->context, s);
274 if (unlikely(delta == VLC_TS_INVALID)) {
275 msg_Dbg(aout, "cannot synchronize start");
276 delta = 0; /* screwed */
279 delta = (sys->first_pts - mdate()) - delta;
281 msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
282 delta += pa_rtclock_now();
283 sys->trigger = pa_context_rttime_new(sys->context, delta,
284 stream_trigger_cb, aout);
286 msg_Warn(aout, "starting late (%"PRId64" us)", delta);
287 stream_start_now(s, aout);
291 static void stream_latency_cb(pa_stream *s, void *userdata)
293 audio_output_t *aout = userdata;
294 aout_sys_t *sys = aout->sys;
296 /* This callback is _never_ called while paused. */
297 if (sys->first_pts == VLC_TS_INVALID)
298 return; /* nothing to do if buffers are (still) empty */
299 if (pa_stream_is_corked(s) > 0)
300 stream_start(s, aout);
304 /*** Stream helpers ***/
305 static void stream_state_cb(pa_stream *s, void *userdata)
307 pa_threaded_mainloop *mainloop = userdata;
309 switch (pa_stream_get_state(s)) {
310 case PA_STREAM_READY:
311 case PA_STREAM_FAILED:
312 case PA_STREAM_TERMINATED:
313 pa_threaded_mainloop_signal(mainloop, 0);
319 static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
321 audio_output_t *aout = userdata;
322 const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
324 msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
325 "prebuf=%u, minreq=%u",
326 pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
329 static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
332 audio_output_t *aout = userdata;
334 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
335 aout_PolicyReport(aout, true);
337 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
338 aout_PolicyReport(aout, false);
340 /* FIXME: expose aout_Restart() directly */
341 if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
342 msg_Dbg (aout, "format lost");
343 aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
345 msg_Warn (aout, "unhandled stream event \"%s\"", name);
350 static void stream_moved_cb(pa_stream *s, void *userdata)
352 audio_output_t *aout = userdata;
353 aout_sys_t *sys = aout->sys;
354 const char *name = pa_stream_get_device_name(s);
355 struct sink *sink = sink_find(sys, pa_stream_get_device_index(s));
357 msg_Dbg(aout, "connected to sink %s", name);
358 aout_DeviceReport(aout, name);
360 sys->base_volume = likely(sink != NULL) ? sink->base_volume
362 msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
363 if (pa_cvolume_valid(&sys->cvolume))
367 static void stream_overflow_cb(pa_stream *s, void *userdata)
369 audio_output_t *aout = userdata;
370 aout_sys_t *sys = aout->sys;
373 msg_Err(aout, "overflow, flushing");
374 op = pa_stream_flush(s, NULL, NULL);
375 if (unlikely(op == NULL))
377 pa_operation_unref(op);
378 sys->first_pts = VLC_TS_INVALID;
381 static void stream_started_cb(pa_stream *s, void *userdata)
383 audio_output_t *aout = userdata;
385 msg_Dbg(aout, "started");
389 static void stream_suspended_cb(pa_stream *s, void *userdata)
391 audio_output_t *aout = userdata;
393 msg_Dbg(aout, "suspended");
397 static void stream_underflow_cb(pa_stream *s, void *userdata)
399 audio_output_t *aout = userdata;
401 msg_Dbg(aout, "underflow");
405 static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
407 pa_stream_state_t state;
409 while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
410 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
412 pa_threaded_mainloop_wait(mainloop);
419 static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
420 int eol, void *userdata)
422 audio_output_t *aout = userdata;
423 aout_sys_t *sys = aout->sys;
429 sys->cvolume = i->volume; /* cache volume for balance preservation */
430 if (PA_VOLUME_IS_VALID(sys->base_volume))
432 aout_MuteReport(aout, i->mute);
435 static void sink_input_event(pa_context *ctx,
436 pa_subscription_event_type_t type,
437 uint32_t idx, audio_output_t *aout)
441 /* Gee... PA will not provide the infos directly in the event. */
444 case PA_SUBSCRIPTION_EVENT_REMOVE:
445 msg_Err(aout, "sink input killed!");
449 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
451 if (likely(op != NULL))
452 pa_operation_unref(op);
459 static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
460 uint32_t idx, void *userdata)
462 audio_output_t *aout = userdata;
463 aout_sys_t *sys = aout->sys;
464 unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
466 type &= PA_SUBSCRIPTION_EVENT_TYPE_MASK;
469 case PA_SUBSCRIPTION_EVENT_SINK:
470 sink_event(ctx, type, idx, userdata);
473 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
474 /* only interested in our sink input */
475 if (sys->stream != NULL && idx == pa_stream_get_index(sys->stream))
476 sink_input_event(ctx, type, idx, userdata);
479 default: /* unsubscribed facility?! */
485 /*** VLC audio output callbacks ***/
487 static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
489 aout_sys_t *sys = aout->sys;
490 pa_stream *s = sys->stream;
492 if (pa_stream_is_corked(s) > 0)
493 return -1; /* latency is irrelevant if corked */
495 mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
496 if (delta == VLC_TS_INVALID)
503 /* Memory free callback. The block_t address is in front of the data. */
504 static void data_free(void *data)
506 block_t **pp = data, *block;
508 memcpy(&block, pp - 1, sizeof (block));
509 block_Release(block);
512 static void *data_convert(block_t **pp)
514 block_t *block = *pp;
515 /* In most cases, there is enough head room, and this is really cheap: */
516 block = block_Realloc(block, sizeof (block), block->i_buffer);
518 if (unlikely(block == NULL))
521 memcpy(block->p_buffer, &block, sizeof (block));
522 block->p_buffer += sizeof (block);
523 block->i_buffer -= sizeof (block);
524 return block->p_buffer;
528 * Queue one audio frame to the playback stream
530 static void Play(audio_output_t *aout, block_t *block)
532 aout_sys_t *sys = aout->sys;
533 pa_stream *s = sys->stream;
535 const void *ptr = data_convert(&block);
536 if (unlikely(ptr == NULL))
539 size_t len = block->i_buffer;
541 /* Note: The core already holds the output FIFO lock at this point.
542 * Therefore we must not under any circumstances (try to) acquire the
543 * output FIFO lock while the PulseAudio threaded main loop lock is held
544 * (including from PulseAudio stream callbacks). Otherwise lock inversion
545 * will take place, and sooner or later a deadlock. */
546 pa_threaded_mainloop_lock(sys->mainloop);
548 if (sys->first_pts == VLC_TS_INVALID)
549 sys->first_pts = block->i_pts;
551 if (pa_stream_is_corked(s) > 0)
552 stream_start(s, aout);
554 #if 0 /* Fault injector to test underrun recovery */
555 static volatile unsigned u = 0;
556 if ((++u % 1000) == 0) {
557 msg_Err(aout, "fault injection");
558 pa_operation_unref(pa_stream_flush(s, NULL, NULL));
562 if (pa_stream_write(s, ptr, len, data_free, 0, PA_SEEK_RELATIVE) < 0) {
563 vlc_pa_error(aout, "cannot write", sys->context);
564 block_Release(block);
567 pa_threaded_mainloop_unlock(sys->mainloop);
571 * Cork or uncork the playback stream
573 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
575 aout_sys_t *sys = aout->sys;
576 pa_stream *s = sys->stream;
578 pa_threaded_mainloop_lock(sys->mainloop);
581 pa_stream_set_latency_update_callback(s, NULL, NULL);
582 stream_stop(s, aout);
584 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
585 if (likely(sys->first_pts != VLC_TS_INVALID))
586 stream_start_now(s, aout);
589 pa_threaded_mainloop_unlock(sys->mainloop);
594 * Flush or drain the playback stream
596 static void Flush(audio_output_t *aout, bool wait)
598 aout_sys_t *sys = aout->sys;
599 pa_stream *s = sys->stream;
602 pa_threaded_mainloop_lock(sys->mainloop);
605 op = pa_stream_drain(s, NULL, NULL);
606 /* TODO: wait for drain completion*/
608 op = pa_stream_flush(s, NULL, NULL);
610 pa_operation_unref(op);
611 pa_threaded_mainloop_unlock(sys->mainloop);
614 static int VolumeSet(audio_output_t *aout, float vol)
616 aout_sys_t *sys = aout->sys;
617 pa_stream *s = sys->stream;
620 pa_volume_t base_volume;
622 /* VLC provides the software volume so convert directly to PulseAudio
623 * software volume, pa_volume_t. This is not a linear amplification factor
624 * so do not use PulseAudio linear amplification! */
625 vol *= PA_VOLUME_NORM;
626 if (unlikely(vol >= PA_VOLUME_MAX))
629 pa_threaded_mainloop_lock(sys->mainloop);
631 base_volume = sys->base_volume;
632 if (!PA_VOLUME_IS_VALID(base_volume))
633 { /* Base volume is unknown, typically because sink is unknown.
634 * Try to guess the base volume. */
635 const struct sink *sink = sys->sinks;
636 if (unlikely(sink == NULL))
638 msg_Err(aout, "cannot change volume without sink");
642 base_volume = sink->base_volume;
643 while ((sink = sink->next) != NULL)
644 if (sink->base_volume != base_volume)
646 msg_Err(aout, "cannot change volume without base");
651 pa_volume_t volume = pa_sw_volume_multiply(lroundf(vol), base_volume);
655 sys->volume_force = volume;
656 aout_VolumeReport(aout, vol / (float)PA_VOLUME_NORM);
661 if (!pa_cvolume_valid(&sys->cvolume))
663 const pa_sample_spec *ss = pa_stream_get_sample_spec(s);
665 msg_Warn(aout, "balance clobbered by volume change");
666 pa_cvolume_set(&sys->cvolume, ss->channels, PA_VOLUME_NORM);
669 /* Preserve the balance (VLC does not support it). */
670 pa_cvolume cvolume = sys->cvolume;
671 pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
672 pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
673 assert(pa_cvolume_valid(&cvolume));
675 op = pa_context_set_sink_input_volume(sys->context, pa_stream_get_index(s),
676 &cvolume, NULL, NULL);
677 if (likely(op != NULL))
679 pa_operation_unref(op);
683 pa_threaded_mainloop_unlock(sys->mainloop);
687 static int MuteSet(audio_output_t *aout, bool mute)
689 aout_sys_t *sys = aout->sys;
691 if (sys->stream == NULL)
693 sys->flags_force &= ~(PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED);
695 mute ? PA_STREAM_START_MUTED : PA_STREAM_START_UNMUTED;
696 aout_MuteReport(aout, mute);
701 uint32_t idx = pa_stream_get_index(sys->stream);
702 pa_threaded_mainloop_lock(sys->mainloop);
703 op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
704 if (likely(op != NULL))
705 pa_operation_unref(op);
706 pa_threaded_mainloop_unlock(sys->mainloop);
711 static int StreamMove(audio_output_t *aout, const char *name)
713 aout_sys_t *sys = aout->sys;
715 if (sys->stream == NULL)
717 struct sink *sink = sink_find_by_name(sys, name);
719 sys->base_volume = likely(sink != NULL) ? sink->base_volume
721 msg_Dbg(aout, "will connect to sink %s", name);
722 free(sys->sink_force);
723 sys->sink_force = strdup(name);
728 uint32_t idx = pa_stream_get_index(sys->stream);
730 pa_threaded_mainloop_lock(sys->mainloop);
731 op = pa_context_move_sink_input_by_name(sys->context, idx, name,
733 if (likely(op != NULL)) {
734 pa_operation_unref(op);
735 msg_Dbg(aout, "moving to sink %s", name);
737 vlc_pa_error(aout, "cannot move sink input", sys->context);
738 pa_threaded_mainloop_unlock(sys->mainloop);
740 return (op != NULL) ? 0 : -1;
743 static void Stop(audio_output_t *);
746 * Create a PulseAudio playback stream, a.k.a. a sink input.
748 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
750 aout_sys_t *sys = aout->sys;
752 /* Sample format specification */
753 struct pa_sample_spec ss;
754 pa_encoding_t encoding = PA_ENCODING_INVALID;
756 switch (fmt->i_format)
759 fmt->i_format = VLC_CODEC_FL32;
761 ss.format = PA_SAMPLE_FLOAT32NE;
764 ss.format = PA_SAMPLE_S32NE;
767 ss.format = PA_SAMPLE_S16NE;
770 ss.format = PA_SAMPLE_U8;
773 fmt->i_format = VLC_CODEC_SPDIFL;
774 encoding = PA_ENCODING_AC3_IEC61937;
775 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
777 /*case VLC_CODEC_EAC3:
778 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
779 encoding = PA_ENCODING_EAC3_IEC61937;
780 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
783 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
784 encoding = PA_ENCODING_MPEG_IEC61937;
785 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
788 fmt->i_format = VLC_CODEC_SPDIFL;
789 encoding = PA_ENCODING_DTS_IEC61937;
790 ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
795 fmt->i_format = VLC_CODEC_FL32;
796 ss.format = PA_SAMPLE_FLOAT32NE;
800 fmt->i_format = VLC_CODEC_S16N;
801 ss.format = PA_SAMPLE_S16NE;
806 ss.rate = fmt->i_rate;
807 ss.channels = aout_FormatNbChannels(fmt);
808 if (!pa_sample_spec_valid(&ss)) {
809 msg_Err(aout, "unsupported sample specification");
813 /* Channel mapping (order defined in vlc_aout.h) */
814 struct pa_channel_map map;
817 if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
818 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
819 if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
820 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
821 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
822 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
823 if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
824 map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
825 if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
826 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
827 if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
828 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
829 if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
830 map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
831 if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
833 if (ss.channels == 1)
834 map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
836 map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
838 if (fmt->i_physical_channels & AOUT_CHAN_LFE)
839 map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
841 for (unsigned i = 0; map.channels < ss.channels; i++) {
842 map.map[map.channels++] = PA_CHANNEL_POSITION_AUX0 + i;
843 msg_Warn(aout, "mapping channel %"PRIu8" to AUX%u", map.channels, i);
846 if (!pa_channel_map_valid(&map)) {
847 msg_Err(aout, "unsupported channel map");
850 const char *name = pa_channel_map_to_name(&map);
851 msg_Dbg(aout, "using %s channel map", (name != NULL) ? name : "?");
854 /* Stream parameters */
855 pa_stream_flags_t flags = sys->flags_force
856 | PA_STREAM_START_CORKED
857 | PA_STREAM_INTERPOLATE_TIMING
858 | PA_STREAM_NOT_MONOTONIC
859 | PA_STREAM_AUTO_TIMING_UPDATE
860 | PA_STREAM_FIX_RATE;
862 struct pa_buffer_attr attr;
864 /* PulseAudio goes berserk if the target length (tlength) is not
865 * significantly longer than 2 periods (minreq), or when the period length
866 * is unspecified and the target length is short. */
867 attr.tlength = pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME, &ss);
868 attr.prebuf = 0; /* trigger manually */
869 attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
870 attr.fragsize = 0; /* not used for output */
872 pa_cvolume *cvolume = NULL, cvolumebuf;
873 if (PA_VOLUME_IS_VALID(sys->volume_force))
875 cvolume = &cvolumebuf;
876 pa_cvolume_set(cvolume, ss.channels, sys->volume_force);
880 pa_cvolume_init(&sys->cvolume);
881 sys->first_pts = VLC_TS_INVALID;
883 pa_format_info *formatv[2];
884 unsigned formatc = 0;
886 /* Favor digital pass-through if available*/
887 if (encoding != PA_ENCODING_INVALID) {
888 formatv[formatc] = pa_format_info_new();
889 formatv[formatc]->encoding = encoding;
890 pa_format_info_set_rate(formatv[formatc], ss.rate);
891 pa_format_info_set_channels(formatv[formatc], ss.channels);
892 pa_format_info_set_channel_map(formatv[formatc], &map);
895 /* FIX flags are only permitted for PCM, and there is no way to pass
896 * different flags for different formats... */
897 flags &= ~(PA_STREAM_FIX_FORMAT
899 | PA_STREAM_FIX_CHANNELS);
902 /* Fallback to PCM */
903 formatv[formatc] = pa_format_info_new();
904 formatv[formatc]->encoding = PA_ENCODING_PCM;
905 pa_format_info_set_sample_format(formatv[formatc], ss.format);
906 pa_format_info_set_rate(formatv[formatc], ss.rate);
907 pa_format_info_set_channels(formatv[formatc], ss.channels);
908 pa_format_info_set_channel_map(formatv[formatc], &map);
911 /* Create a playback stream */
912 pa_proplist *props = pa_proplist_new();
913 if (likely(props != NULL))
914 /* TODO: set other stream properties */
915 pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
917 pa_threaded_mainloop_lock(sys->mainloop);
918 pa_stream *s = pa_stream_new_extended(sys->context, "audio stream",
919 formatv, formatc, props);
921 if (likely(props != NULL))
922 pa_proplist_free(props);
923 for (unsigned i = 0; i < formatc; i++)
924 pa_format_info_free(formatv[i]);
927 pa_threaded_mainloop_unlock(sys->mainloop);
928 vlc_pa_error(aout, "stream creation failure", sys->context);
931 assert(sys->stream == NULL);
933 pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
934 pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
935 pa_stream_set_event_callback(s, stream_event_cb, aout);
936 pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
937 pa_stream_set_moved_callback(s, stream_moved_cb, aout);
938 pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
939 pa_stream_set_started_callback(s, stream_started_cb, aout);
940 pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
941 pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
943 if (pa_stream_connect_playback(s, sys->sink_force, &attr, flags,
945 || stream_wait(s, sys->mainloop)) {
946 vlc_pa_error(aout, "stream connection failure", sys->context);
949 sys->volume_force = PA_VOLUME_INVALID;
950 sys->flags_force = PA_STREAM_NOFLAGS;
951 free(sys->sink_force);
952 sys->sink_force = NULL;
954 const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
955 if (encoding != PA_ENCODING_INVALID) {
956 const pa_format_info *info = pa_stream_get_format_info(s);
958 assert (info != NULL);
959 if (pa_format_info_is_pcm (info)) {
960 msg_Dbg(aout, "digital pass-through not available");
961 fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
963 msg_Dbg(aout, "digital pass-through enabled");
968 fmt->i_rate = spec->rate;
970 stream_buffer_attr_cb(s, aout);
971 stream_moved_cb(s, aout);
972 pa_threaded_mainloop_unlock(sys->mainloop);
977 pa_threaded_mainloop_unlock(sys->mainloop);
983 * Removes a PulseAudio playback stream
985 static void Stop(audio_output_t *aout)
987 aout_sys_t *sys = aout->sys;
988 pa_stream *s = sys->stream;
990 pa_threaded_mainloop_lock(sys->mainloop);
991 if (unlikely(sys->trigger != NULL))
992 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
993 pa_stream_disconnect(s);
995 /* Clear all callbacks */
996 pa_stream_set_state_callback(s, NULL, NULL);
997 pa_stream_set_buffer_attr_callback(s, NULL, NULL);
998 pa_stream_set_event_callback(s, NULL, NULL);
999 pa_stream_set_latency_update_callback(s, NULL, NULL);
1000 pa_stream_set_moved_callback(s, NULL, NULL);
1001 pa_stream_set_overflow_callback(s, NULL, NULL);
1002 pa_stream_set_started_callback(s, NULL, NULL);
1003 pa_stream_set_suspended_callback(s, NULL, NULL);
1004 pa_stream_set_underflow_callback(s, NULL, NULL);
1008 sys->base_volume = PA_VOLUME_INVALID;
1009 pa_threaded_mainloop_unlock(sys->mainloop);
1012 static int Open(vlc_object_t *obj)
1014 audio_output_t *aout = (audio_output_t *)obj;
1015 aout_sys_t *sys = malloc(sizeof (*sys));
1018 if (unlikely(sys == NULL))
1021 /* Allocate structures */
1022 pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
1026 return VLC_EGENERIC;
1030 sys->base_volume = PA_VOLUME_INVALID;
1031 sys->volume_force = PA_VOLUME_INVALID;
1032 sys->flags_force = PA_STREAM_NOFLAGS;
1033 sys->sink_force = NULL;
1037 aout->start = Start;
1039 aout->time_get = TimeGet;
1041 aout->pause = Pause;
1042 aout->flush = Flush;
1043 aout->volume_set = VolumeSet;
1044 aout->mute_set = MuteSet;
1045 aout->device_select = StreamMove;
1047 pa_threaded_mainloop_lock(sys->mainloop);
1048 /* Sinks (output devices) list */
1049 op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
1050 if (likely(op != NULL))
1052 while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
1053 pa_threaded_mainloop_wait(sys->mainloop);
1054 pa_operation_unref(op);
1057 /* Context events */
1058 const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
1059 | PA_SUBSCRIPTION_MASK_SINK_INPUT;
1060 pa_context_set_subscribe_callback(sys->context, context_cb, aout);
1061 op = pa_context_subscribe(sys->context, mask, NULL, NULL);
1062 if (likely(op != NULL))
1063 pa_operation_unref(op);
1064 pa_threaded_mainloop_unlock(sys->mainloop);
1069 static void Close(vlc_object_t *obj)
1071 audio_output_t *aout = (audio_output_t *)obj;
1072 aout_sys_t *sys = aout->sys;
1073 pa_context *ctx = sys->context;
1075 pa_threaded_mainloop_lock(sys->mainloop);
1076 pa_context_set_subscribe_callback(sys->context, NULL, NULL);
1077 pa_threaded_mainloop_unlock(sys->mainloop);
1078 vlc_pa_disconnect(obj, ctx, sys->mainloop);
1080 for (struct sink *sink = sys->sinks, *next; sink != NULL; sink = next)
1085 free(sys->sink_force);