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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
35 #include <pulse/pulseaudio.h>
36 #include "audio_output/vlcpulse.h"
38 static int Open ( vlc_object_t
* );
39 static void Close ( vlc_object_t
* );
42 set_shortname( "PulseAudio" )
43 set_description( N_("Pulseaudio audio output") )
44 set_capability( "audio output", 160 )
45 set_category( CAT_AUDIO
)
46 set_subcategory( SUBCAT_AUDIO_AOUT
)
47 add_shortcut( "pulseaudio", "pa" )
48 set_callbacks( Open
, Close
)
52 * Be careful what you do when the PulseAudio mainloop is held, which is to say
53 * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
54 * In particular, a VLC variable callback cannot be triggered nor deleted with
55 * the PulseAudio mainloop lock held, if the callback acquires the lock. */
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_cvolume cvolume
; /**< actual sink input volume */
71 mtime_t first_pts
; /**< Play time of buffer start */
73 pa_volume_t volume_force
; /**< Forced volume (stream must be NULL) */
74 pa_stream_flags_t flags_force
; /**< Forced flags (stream must be NULL) */
75 char *sink_force
; /**< Forced sink name (stream must be NULL) */
77 struct sink
*sinks
; /**< Locally-cached list of sinks */
80 static void VolumeReport(audio_output_t
*aout
)
82 aout_sys_t
*sys
= aout
->sys
;
83 pa_volume_t volume
= pa_cvolume_max(&sys
->cvolume
);
85 aout_VolumeReport(aout
, (float)volume
/ PA_VOLUME_NORM
);
89 static void sink_add_cb(pa_context
*ctx
, const pa_sink_info
*i
, int eol
,
92 audio_output_t
*aout
= userdata
;
93 aout_sys_t
*sys
= aout
->sys
;
97 pa_threaded_mainloop_signal(sys
->mainloop
, 0);
102 msg_Dbg(aout
, "adding sink %"PRIu32
": %s (%s)", i
->index
, i
->name
,
104 aout_HotplugReport(aout
, i
->name
, i
->description
);
106 size_t namelen
= strlen(i
->name
);
107 struct sink
*sink
= malloc(sizeof (*sink
) + namelen
);
108 if (unlikely(sink
== NULL
))
111 sink
->next
= sys
->sinks
;
112 sink
->index
= i
->index
;
113 memcpy(sink
->name
, i
->name
, namelen
+ 1);
117 static void sink_mod_cb(pa_context
*ctx
, const pa_sink_info
*i
, int eol
,
120 audio_output_t
*aout
= userdata
;
126 msg_Dbg(aout
, "changing sink %"PRIu32
": %s (%s)", i
->index
, i
->name
,
128 aout_HotplugReport(aout
, i
->name
, i
->description
);
131 static void sink_del(uint32_t index
, audio_output_t
*aout
)
133 aout_sys_t
*sys
= aout
->sys
;
134 struct sink
**pp
= &sys
->sinks
, *sink
;
136 msg_Dbg(aout
, "removing sink %"PRIu32
, index
);
138 while ((sink
= *pp
) != NULL
)
139 if (sink
->index
== index
)
142 aout_HotplugReport(aout
, sink
->name
, NULL
);
149 static void sink_event(pa_context
*ctx
, unsigned type
, uint32_t idx
,
150 audio_output_t
*aout
)
152 pa_operation
*op
= NULL
;
156 case PA_SUBSCRIPTION_EVENT_NEW
:
157 op
= pa_context_get_sink_info_by_index(ctx
, idx
, sink_add_cb
,
160 case PA_SUBSCRIPTION_EVENT_CHANGE
:
161 op
= pa_context_get_sink_info_by_index(ctx
, idx
, sink_mod_cb
,
164 case PA_SUBSCRIPTION_EVENT_REMOVE
:
169 pa_operation_unref(op
);
173 /*** Latency management and lip synchronization ***/
174 static void stream_start_now(pa_stream
*s
, audio_output_t
*aout
)
178 assert (aout
->sys
->trigger
== NULL
);
180 op
= pa_stream_cork(s
, 0, NULL
, NULL
);
182 pa_operation_unref(op
);
183 op
= pa_stream_trigger(s
, NULL
, NULL
);
184 if (likely(op
!= NULL
))
185 pa_operation_unref(op
);
188 static void stream_stop(pa_stream
*s
, audio_output_t
*aout
)
190 aout_sys_t
*sys
= aout
->sys
;
193 if (sys
->trigger
!= NULL
) {
194 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
198 op
= pa_stream_cork(s
, 1, NULL
, NULL
);
200 pa_operation_unref(op
);
203 static void stream_trigger_cb(pa_mainloop_api
*api
, pa_time_event
*e
,
204 const struct timeval
*tv
, void *userdata
)
206 audio_output_t
*aout
= userdata
;
207 aout_sys_t
*sys
= aout
->sys
;
209 assert (sys
->trigger
== e
);
211 msg_Dbg(aout
, "starting deferred");
212 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
214 stream_start_now(sys
->stream
, aout
);
215 (void) api
; (void) e
; (void) tv
;
219 * Starts or resumes the playback stream.
220 * Tries start playing back audio samples at the most accurate time
221 * in order to minimize desync and resampling during early playback.
222 * @note PulseAudio lock required.
224 static void stream_start(pa_stream
*s
, audio_output_t
*aout
)
226 aout_sys_t
*sys
= aout
->sys
;
229 assert (sys
->first_pts
!= VLC_TS_INVALID
);
231 if (sys
->trigger
!= NULL
) {
232 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
236 delta
= vlc_pa_get_latency(aout
, sys
->context
, s
);
237 if (unlikely(delta
== VLC_TS_INVALID
)) {
238 msg_Dbg(aout
, "cannot synchronize start");
239 delta
= 0; /* screwed */
242 delta
= (sys
->first_pts
- mdate()) - delta
;
244 msg_Dbg(aout
, "deferring start (%"PRId64
" us)", delta
);
245 delta
+= pa_rtclock_now();
246 sys
->trigger
= pa_context_rttime_new(sys
->context
, delta
,
247 stream_trigger_cb
, aout
);
249 msg_Warn(aout
, "starting late (%"PRId64
" us)", delta
);
250 stream_start_now(s
, aout
);
254 static void stream_latency_cb(pa_stream
*s
, void *userdata
)
256 audio_output_t
*aout
= userdata
;
257 aout_sys_t
*sys
= aout
->sys
;
259 /* This callback is _never_ called while paused. */
260 if (sys
->first_pts
== VLC_TS_INVALID
)
261 return; /* nothing to do if buffers are (still) empty */
262 if (pa_stream_is_corked(s
) > 0)
263 stream_start(s
, aout
);
267 /*** Stream helpers ***/
268 static void stream_state_cb(pa_stream
*s
, void *userdata
)
270 pa_threaded_mainloop
*mainloop
= userdata
;
272 switch (pa_stream_get_state(s
)) {
273 case PA_STREAM_READY
:
274 case PA_STREAM_FAILED
:
275 case PA_STREAM_TERMINATED
:
276 pa_threaded_mainloop_signal(mainloop
, 0);
282 static void stream_buffer_attr_cb(pa_stream
*s
, void *userdata
)
284 audio_output_t
*aout
= userdata
;
285 const pa_buffer_attr
*pba
= pa_stream_get_buffer_attr(s
);
287 msg_Dbg(aout
, "changed buffer metrics: maxlength=%u, tlength=%u, "
288 "prebuf=%u, minreq=%u",
289 pba
->maxlength
, pba
->tlength
, pba
->prebuf
, pba
->minreq
);
292 static void stream_event_cb(pa_stream
*s
, const char *name
, pa_proplist
*pl
,
295 audio_output_t
*aout
= userdata
;
297 if (!strcmp(name
, PA_STREAM_EVENT_REQUEST_CORK
))
298 aout_PolicyReport(aout
, true);
300 if (!strcmp(name
, PA_STREAM_EVENT_REQUEST_UNCORK
))
301 aout_PolicyReport(aout
, false);
303 /* FIXME: expose aout_Restart() directly */
304 if (!strcmp(name
, PA_STREAM_EVENT_FORMAT_LOST
)) {
305 msg_Dbg (aout
, "format lost");
306 aout_RestartRequest (aout
, AOUT_RESTART_OUTPUT
);
308 msg_Warn (aout
, "unhandled stream event \"%s\"", name
);
313 static void stream_moved_cb(pa_stream
*s
, void *userdata
)
315 audio_output_t
*aout
= userdata
;
316 const char *name
= pa_stream_get_device_name(s
);
318 msg_Dbg(aout
, "connected to sink %s", name
);
319 aout_DeviceReport(aout
, name
);
322 static void stream_overflow_cb(pa_stream
*s
, void *userdata
)
324 audio_output_t
*aout
= userdata
;
325 aout_sys_t
*sys
= aout
->sys
;
328 msg_Err(aout
, "overflow, flushing");
329 op
= pa_stream_flush(s
, NULL
, NULL
);
330 if (unlikely(op
== NULL
))
332 pa_operation_unref(op
);
333 sys
->first_pts
= VLC_TS_INVALID
;
336 static void stream_started_cb(pa_stream
*s
, void *userdata
)
338 audio_output_t
*aout
= userdata
;
340 msg_Dbg(aout
, "started");
344 static void stream_suspended_cb(pa_stream
*s
, void *userdata
)
346 audio_output_t
*aout
= userdata
;
348 msg_Dbg(aout
, "suspended");
352 static void stream_underflow_cb(pa_stream
*s
, void *userdata
)
354 audio_output_t
*aout
= userdata
;
356 msg_Dbg(aout
, "underflow");
360 static int stream_wait(pa_stream
*stream
, pa_threaded_mainloop
*mainloop
)
362 pa_stream_state_t state
;
364 while ((state
= pa_stream_get_state(stream
)) != PA_STREAM_READY
) {
365 if (state
== PA_STREAM_FAILED
|| state
== PA_STREAM_TERMINATED
)
367 pa_threaded_mainloop_wait(mainloop
);
374 static void sink_input_info_cb(pa_context
*ctx
, const pa_sink_input_info
*i
,
375 int eol
, void *userdata
)
377 audio_output_t
*aout
= userdata
;
378 aout_sys_t
*sys
= aout
->sys
;
384 sys
->cvolume
= i
->volume
; /* cache volume for balance preservation */
386 aout_MuteReport(aout
, i
->mute
);
389 static void sink_input_event(pa_context
*ctx
,
390 pa_subscription_event_type_t type
,
391 uint32_t idx
, audio_output_t
*aout
)
395 /* Gee... PA will not provide the infos directly in the event. */
398 case PA_SUBSCRIPTION_EVENT_REMOVE
:
399 msg_Err(aout
, "sink input killed!");
403 op
= pa_context_get_sink_input_info(ctx
, idx
, sink_input_info_cb
,
405 if (likely(op
!= NULL
))
406 pa_operation_unref(op
);
413 static void context_cb(pa_context
*ctx
, pa_subscription_event_type_t type
,
414 uint32_t idx
, void *userdata
)
416 audio_output_t
*aout
= userdata
;
417 aout_sys_t
*sys
= aout
->sys
;
418 unsigned facility
= type
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
;
420 type
&= PA_SUBSCRIPTION_EVENT_TYPE_MASK
;
423 case PA_SUBSCRIPTION_EVENT_SINK
:
424 sink_event(ctx
, type
, idx
, userdata
);
427 case PA_SUBSCRIPTION_EVENT_SINK_INPUT
:
428 /* only interested in our sink input */
429 if (sys
->stream
!= NULL
&& idx
== pa_stream_get_index(sys
->stream
))
430 sink_input_event(ctx
, type
, idx
, userdata
);
433 default: /* unsubscribed facility?! */
434 vlc_assert_unreachable();
439 /*** VLC audio output callbacks ***/
441 static int TimeGet(audio_output_t
*aout
, mtime_t
*restrict delay
)
443 aout_sys_t
*sys
= aout
->sys
;
444 pa_stream
*s
= sys
->stream
;
447 pa_threaded_mainloop_lock(sys
->mainloop
);
448 if (pa_stream_is_corked(s
) <= 0)
449 { /* latency is relevant only if not corked */
450 mtime_t delta
= vlc_pa_get_latency(aout
, sys
->context
, s
);
451 if (delta
!= VLC_TS_INVALID
)
457 pa_threaded_mainloop_unlock(sys
->mainloop
);
461 /* Memory free callback. The block_t address is in front of the data. */
462 static void data_free(void *data
)
464 block_t
**pp
= data
, *block
;
466 memcpy(&block
, pp
- 1, sizeof (block
));
467 block_Release(block
);
470 static void *data_convert(block_t
**pp
)
472 block_t
*block
= *pp
;
473 /* In most cases, there is enough head room, and this is really cheap: */
474 block
= block_Realloc(block
, sizeof (block
), block
->i_buffer
);
476 if (unlikely(block
== NULL
))
479 memcpy(block
->p_buffer
, &block
, sizeof (block
));
480 block
->p_buffer
+= sizeof (block
);
481 block
->i_buffer
-= sizeof (block
);
482 return block
->p_buffer
;
486 * Queue one audio frame to the playback stream
488 static void Play(audio_output_t
*aout
, block_t
*block
)
490 aout_sys_t
*sys
= aout
->sys
;
491 pa_stream
*s
= sys
->stream
;
493 const void *ptr
= data_convert(&block
);
494 if (unlikely(ptr
== NULL
))
497 size_t len
= block
->i_buffer
;
499 /* Note: The core already holds the output FIFO lock at this point.
500 * Therefore we must not under any circumstances (try to) acquire the
501 * output FIFO lock while the PulseAudio threaded main loop lock is held
502 * (including from PulseAudio stream callbacks). Otherwise lock inversion
503 * will take place, and sooner or later a deadlock. */
504 pa_threaded_mainloop_lock(sys
->mainloop
);
506 if (sys
->first_pts
== VLC_TS_INVALID
)
507 sys
->first_pts
= block
->i_pts
;
509 if (pa_stream_is_corked(s
) > 0)
510 stream_start(s
, aout
);
512 #if 0 /* Fault injector to test underrun recovery */
513 static volatile unsigned u
= 0;
514 if ((++u
% 1000) == 0) {
515 msg_Err(aout
, "fault injection");
516 pa_operation_unref(pa_stream_flush(s
, NULL
, NULL
));
520 if (pa_stream_write(s
, ptr
, len
, data_free
, 0, PA_SEEK_RELATIVE
) < 0) {
521 vlc_pa_error(aout
, "cannot write", sys
->context
);
522 block_Release(block
);
525 pa_threaded_mainloop_unlock(sys
->mainloop
);
529 * Cork or uncork the playback stream
531 static void Pause(audio_output_t
*aout
, bool paused
, mtime_t date
)
533 aout_sys_t
*sys
= aout
->sys
;
534 pa_stream
*s
= sys
->stream
;
536 pa_threaded_mainloop_lock(sys
->mainloop
);
539 pa_stream_set_latency_update_callback(s
, NULL
, NULL
);
540 stream_stop(s
, aout
);
542 pa_stream_set_latency_update_callback(s
, stream_latency_cb
, aout
);
543 if (likely(sys
->first_pts
!= VLC_TS_INVALID
))
544 stream_start_now(s
, aout
);
547 pa_threaded_mainloop_unlock(sys
->mainloop
);
552 * Flush or drain the playback stream
554 static void Flush(audio_output_t
*aout
, bool wait
)
556 aout_sys_t
*sys
= aout
->sys
;
557 pa_stream
*s
= sys
->stream
;
560 pa_threaded_mainloop_lock(sys
->mainloop
);
563 op
= pa_stream_drain(s
, NULL
, NULL
);
564 /* TODO: wait for drain completion*/
566 op
= pa_stream_flush(s
, NULL
, NULL
);
568 pa_operation_unref(op
);
569 sys
->first_pts
= VLC_TS_INVALID
;
570 stream_stop(s
, aout
);
572 pa_threaded_mainloop_unlock(sys
->mainloop
);
575 static int VolumeSet(audio_output_t
*aout
, float vol
)
577 aout_sys_t
*sys
= aout
->sys
;
578 pa_stream
*s
= sys
->stream
;
582 /* VLC provides the software volume so convert directly to PulseAudio
583 * software volume, pa_volume_t. This is not a linear amplification factor
584 * so do not use PulseAudio linear amplification! */
585 vol
*= PA_VOLUME_NORM
;
586 if (unlikely(vol
>= (float)PA_VOLUME_MAX
))
587 volume
= PA_VOLUME_MAX
;
589 volume
= lroundf(vol
);
593 sys
->volume_force
= volume
;
594 aout_VolumeReport(aout
, (float)volume
/ (float)PA_VOLUME_NORM
);
598 pa_threaded_mainloop_lock(sys
->mainloop
);
600 if (!pa_cvolume_valid(&sys
->cvolume
))
602 const pa_sample_spec
*ss
= pa_stream_get_sample_spec(s
);
604 msg_Warn(aout
, "balance clobbered by volume change");
605 pa_cvolume_set(&sys
->cvolume
, ss
->channels
, PA_VOLUME_NORM
);
608 /* Preserve the balance (VLC does not support it). */
609 pa_cvolume cvolume
= sys
->cvolume
;
610 pa_cvolume_scale(&cvolume
, PA_VOLUME_NORM
);
611 pa_sw_cvolume_multiply_scalar(&cvolume
, &cvolume
, volume
);
612 assert(pa_cvolume_valid(&cvolume
));
614 op
= pa_context_set_sink_input_volume(sys
->context
, pa_stream_get_index(s
),
615 &cvolume
, NULL
, NULL
);
616 if (likely(op
!= NULL
))
617 pa_operation_unref(op
);
618 pa_threaded_mainloop_unlock(sys
->mainloop
);
619 return likely(op
!= NULL
) ? 0 : -1;
622 static int MuteSet(audio_output_t
*aout
, bool mute
)
624 aout_sys_t
*sys
= aout
->sys
;
626 if (sys
->stream
== NULL
)
628 sys
->flags_force
&= ~(PA_STREAM_START_MUTED
|PA_STREAM_START_UNMUTED
);
630 mute
? PA_STREAM_START_MUTED
: PA_STREAM_START_UNMUTED
;
631 aout_MuteReport(aout
, mute
);
636 uint32_t idx
= pa_stream_get_index(sys
->stream
);
637 pa_threaded_mainloop_lock(sys
->mainloop
);
638 op
= pa_context_set_sink_input_mute(sys
->context
, idx
, mute
, NULL
, NULL
);
639 if (likely(op
!= NULL
))
640 pa_operation_unref(op
);
641 pa_threaded_mainloop_unlock(sys
->mainloop
);
646 static int StreamMove(audio_output_t
*aout
, const char *name
)
648 aout_sys_t
*sys
= aout
->sys
;
650 if (sys
->stream
== NULL
)
652 msg_Dbg(aout
, "will connect to sink %s", name
);
653 free(sys
->sink_force
);
654 sys
->sink_force
= strdup(name
);
655 aout_DeviceReport(aout
, name
);
660 uint32_t idx
= pa_stream_get_index(sys
->stream
);
662 pa_threaded_mainloop_lock(sys
->mainloop
);
663 op
= pa_context_move_sink_input_by_name(sys
->context
, idx
, name
,
665 if (likely(op
!= NULL
)) {
666 pa_operation_unref(op
);
667 msg_Dbg(aout
, "moving to sink %s", name
);
669 vlc_pa_error(aout
, "cannot move sink input", sys
->context
);
670 pa_threaded_mainloop_unlock(sys
->mainloop
);
672 return (op
!= NULL
) ? 0 : -1;
675 static void Stop(audio_output_t
*);
677 static int strcmp_void(const void *a
, const void *b
)
679 const char *const *entry
= b
;
680 return strcmp(a
, *entry
);
683 static const char *str_map(const char *key
, const char *const table
[][2],
686 const char **r
= bsearch(key
, table
, n
, sizeof (*table
), strcmp_void
);
687 return (r
!= NULL
) ? r
[1] : NULL
;
691 * Create a PulseAudio playback stream, a.k.a. a sink input.
693 static int Start(audio_output_t
*aout
, audio_sample_format_t
*restrict fmt
)
695 aout_sys_t
*sys
= aout
->sys
;
697 /* Sample format specification */
698 struct pa_sample_spec ss
= { .format
= PA_SAMPLE_INVALID
};
699 pa_encoding_t encoding
= PA_ENCODING_PCM
;
701 switch (fmt
->i_format
)
704 fmt
->i_format
= VLC_CODEC_FL32
;
707 ss
.format
= PA_SAMPLE_FLOAT32NE
;
710 ss
.format
= PA_SAMPLE_S32NE
;
713 ss
.format
= PA_SAMPLE_S16NE
;
716 ss
.format
= PA_SAMPLE_U8
;
719 fmt
->i_format
= VLC_CODEC_SPDIFL
;
720 fmt
->i_bytes_per_frame
= 4;
721 fmt
->i_frame_length
= 1;
722 fmt
->i_physical_channels
= AOUT_CHANS_2_0
;
724 encoding
= PA_ENCODING_AC3_IEC61937
;
727 fmt
->i_format
= VLC_CODEC_SPDIFL
;
728 fmt
->i_bytes_per_frame
= 4;
729 fmt
->i_frame_length
= 1;
730 fmt
->i_physical_channels
= AOUT_CHANS_2_0
;
732 encoding
= PA_ENCODING_EAC3_IEC61937
;
734 /* case VLC_CODEC_MPGA:
735 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
736 encoding = PA_ENCODING_MPEG_IEC61937;
739 fmt
->i_format
= VLC_CODEC_SPDIFL
;
740 fmt
->i_bytes_per_frame
= 4;
741 fmt
->i_frame_length
= 1;
742 fmt
->i_physical_channels
= AOUT_CHANS_2_0
;
744 encoding
= PA_ENCODING_DTS_IEC61937
;
747 if (!AOUT_FMT_LINEAR(fmt
) || aout_FormatNbChannels(fmt
) == 0)
752 fmt
->i_format
= VLC_CODEC_FL32
;
753 ss
.format
= PA_SAMPLE_FLOAT32NE
;
757 fmt
->i_format
= VLC_CODEC_S16N
;
758 ss
.format
= PA_SAMPLE_S16NE
;
763 ss
.rate
= fmt
->i_rate
;
764 ss
.channels
= fmt
->i_channels
;
765 if (!pa_sample_spec_valid(&ss
)) {
766 msg_Err(aout
, "unsupported sample specification");
770 /* Stream parameters */
771 pa_stream_flags_t flags
= sys
->flags_force
772 | PA_STREAM_START_CORKED
773 | PA_STREAM_INTERPOLATE_TIMING
774 | PA_STREAM_NOT_MONOTONIC
775 | PA_STREAM_AUTO_TIMING_UPDATE
776 | PA_STREAM_FIX_RATE
;
778 struct pa_buffer_attr attr
;
780 /* PulseAudio goes berserk if the target length (tlength) is not
781 * significantly longer than 2 periods (minreq), or when the period length
782 * is unspecified and the target length is short. */
783 attr
.tlength
= pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME
, &ss
);
784 attr
.prebuf
= 0; /* trigger manually */
785 attr
.minreq
= pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME
, &ss
);
786 attr
.fragsize
= 0; /* not used for output */
788 pa_cvolume
*cvolume
= NULL
, cvolumebuf
;
789 if (PA_VOLUME_IS_VALID(sys
->volume_force
))
791 cvolume
= &cvolumebuf
;
792 pa_cvolume_set(cvolume
, ss
.channels
, sys
->volume_force
);
796 pa_cvolume_init(&sys
->cvolume
);
797 sys
->first_pts
= VLC_TS_INVALID
;
799 pa_format_info
*formatv
= pa_format_info_new();
800 formatv
->encoding
= encoding
;
801 pa_format_info_set_rate(formatv
, ss
.rate
);
802 if (ss
.format
!= PA_SAMPLE_INVALID
)
803 pa_format_info_set_sample_format(formatv
, ss
.format
);
805 if (fmt
->channel_type
== AUDIO_CHANNEL_TYPE_AMBISONICS
)
807 fmt
->channel_type
= AUDIO_CHANNEL_TYPE_BITMAP
;
809 /* Setup low latency in order to quickly react to ambisonics
810 * filters viewpoint changes. */
811 flags
|= PA_STREAM_ADJUST_LATENCY
;
812 attr
.tlength
= pa_usec_to_bytes(2 * AOUT_MIN_PREPARE_TIME
, &ss
);
815 if (encoding
!= PA_ENCODING_PCM
)
817 pa_format_info_set_channels(formatv
, ss
.channels
);
819 /* FIX flags are only permitted for PCM, and there is no way to pass
820 * different flags for different formats... */
821 flags
&= ~(PA_STREAM_FIX_FORMAT
823 | PA_STREAM_FIX_CHANNELS
);
827 /* Channel mapping (order defined in vlc_aout.h) */
828 struct pa_channel_map map
;
831 if (fmt
->i_physical_channels
& AOUT_CHAN_LEFT
)
832 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_FRONT_LEFT
;
833 if (fmt
->i_physical_channels
& AOUT_CHAN_RIGHT
)
834 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_FRONT_RIGHT
;
835 if (fmt
->i_physical_channels
& AOUT_CHAN_MIDDLELEFT
)
836 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_SIDE_LEFT
;
837 if (fmt
->i_physical_channels
& AOUT_CHAN_MIDDLERIGHT
)
838 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_SIDE_RIGHT
;
839 if (fmt
->i_physical_channels
& AOUT_CHAN_REARLEFT
)
840 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_REAR_LEFT
;
841 if (fmt
->i_physical_channels
& AOUT_CHAN_REARRIGHT
)
842 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_REAR_RIGHT
;
843 if (fmt
->i_physical_channels
& AOUT_CHAN_REARCENTER
)
844 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_REAR_CENTER
;
845 if (fmt
->i_physical_channels
& AOUT_CHAN_CENTER
)
847 if (ss
.channels
== 1)
848 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_MONO
;
850 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_FRONT_CENTER
;
852 if (fmt
->i_physical_channels
& AOUT_CHAN_LFE
)
853 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_LFE
;
855 static_assert(AOUT_CHAN_MAX
== 9, "Missing channels");
857 for (unsigned i
= 0; map
.channels
< ss
.channels
; i
++) {
858 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_AUX0
+ i
;
859 msg_Warn(aout
, "mapping channel %"PRIu8
" to AUX%u", map
.channels
, i
);
862 if (!pa_channel_map_valid(&map
)) {
863 msg_Err(aout
, "unsupported channel map");
866 const char *name
= pa_channel_map_to_name(&map
);
867 msg_Dbg(aout
, "using %s channel map", (name
!= NULL
) ? name
: "?");
870 pa_format_info_set_channels(formatv
, ss
.channels
);
871 pa_format_info_set_channel_map(formatv
, &map
);
874 /* Create a playback stream */
875 pa_proplist
*props
= pa_proplist_new();
876 if (likely(props
!= NULL
))
878 /* TODO: set other stream properties */
879 char *str
= var_InheritString(aout
, "role");
882 static const char *const role_map
[][2] = {
883 { "accessibility", "a11y" },
884 { "animation", "animation" },
885 { "communication", "phone" },
887 { "music", "music" },
888 { "notification", "event" },
889 { "production", "production" },
891 { "video", "video" },
893 const char *role
= str_map(str
, role_map
, ARRAY_SIZE(role_map
));
895 pa_proplist_sets(props
, PA_PROP_MEDIA_ROLE
, role
);
900 pa_threaded_mainloop_lock(sys
->mainloop
);
901 pa_stream
*s
= pa_stream_new_extended(sys
->context
, "audio stream",
904 if (likely(props
!= NULL
))
905 pa_proplist_free(props
);
906 pa_format_info_free(formatv
);
909 pa_threaded_mainloop_unlock(sys
->mainloop
);
910 vlc_pa_error(aout
, "stream creation failure", sys
->context
);
913 assert(sys
->stream
== NULL
);
915 pa_stream_set_state_callback(s
, stream_state_cb
, sys
->mainloop
);
916 pa_stream_set_buffer_attr_callback(s
, stream_buffer_attr_cb
, aout
);
917 pa_stream_set_event_callback(s
, stream_event_cb
, aout
);
918 pa_stream_set_latency_update_callback(s
, stream_latency_cb
, aout
);
919 pa_stream_set_moved_callback(s
, stream_moved_cb
, aout
);
920 pa_stream_set_overflow_callback(s
, stream_overflow_cb
, aout
);
921 pa_stream_set_started_callback(s
, stream_started_cb
, aout
);
922 pa_stream_set_suspended_callback(s
, stream_suspended_cb
, aout
);
923 pa_stream_set_underflow_callback(s
, stream_underflow_cb
, aout
);
925 if (pa_stream_connect_playback(s
, sys
->sink_force
, &attr
, flags
,
927 || stream_wait(s
, sys
->mainloop
)) {
928 if (encoding
!= PA_ENCODING_PCM
)
929 vlc_pa_error(aout
, "digital pass-through stream connection failure",
932 vlc_pa_error(aout
, "stream connection failure", sys
->context
);
935 sys
->volume_force
= PA_VOLUME_INVALID
;
936 sys
->flags_force
= PA_STREAM_NOFLAGS
;
937 free(sys
->sink_force
);
938 sys
->sink_force
= NULL
;
940 if (encoding
== PA_ENCODING_PCM
)
942 const struct pa_sample_spec
*spec
= pa_stream_get_sample_spec(s
);
943 fmt
->i_rate
= spec
->rate
;
946 stream_buffer_attr_cb(s
, aout
);
947 stream_moved_cb(s
, aout
);
948 pa_threaded_mainloop_unlock(sys
->mainloop
);
953 pa_threaded_mainloop_unlock(sys
->mainloop
);
959 * Removes a PulseAudio playback stream
961 static void Stop(audio_output_t
*aout
)
963 aout_sys_t
*sys
= aout
->sys
;
964 pa_stream
*s
= sys
->stream
;
966 pa_threaded_mainloop_lock(sys
->mainloop
);
967 if (unlikely(sys
->trigger
!= NULL
))
968 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
969 pa_stream_disconnect(s
);
971 /* Clear all callbacks */
972 pa_stream_set_state_callback(s
, NULL
, NULL
);
973 pa_stream_set_buffer_attr_callback(s
, NULL
, NULL
);
974 pa_stream_set_event_callback(s
, NULL
, NULL
);
975 pa_stream_set_latency_update_callback(s
, NULL
, NULL
);
976 pa_stream_set_moved_callback(s
, NULL
, NULL
);
977 pa_stream_set_overflow_callback(s
, NULL
, NULL
);
978 pa_stream_set_started_callback(s
, NULL
, NULL
);
979 pa_stream_set_suspended_callback(s
, NULL
, NULL
);
980 pa_stream_set_underflow_callback(s
, NULL
, NULL
);
984 pa_threaded_mainloop_unlock(sys
->mainloop
);
987 static int Open(vlc_object_t
*obj
)
989 audio_output_t
*aout
= (audio_output_t
*)obj
;
990 aout_sys_t
*sys
= malloc(sizeof (*sys
));
993 if (unlikely(sys
== NULL
))
996 /* Allocate structures */
997 pa_context
*ctx
= vlc_pa_connect(obj
, &sys
->mainloop
);
1001 return VLC_EGENERIC
;
1005 sys
->volume_force
= PA_VOLUME_INVALID
;
1006 sys
->flags_force
= PA_STREAM_NOFLAGS
;
1007 sys
->sink_force
= NULL
;
1011 aout
->start
= Start
;
1013 aout
->time_get
= TimeGet
;
1015 aout
->pause
= Pause
;
1016 aout
->flush
= Flush
;
1017 aout
->volume_set
= VolumeSet
;
1018 aout
->mute_set
= MuteSet
;
1019 aout
->device_select
= StreamMove
;
1021 pa_threaded_mainloop_lock(sys
->mainloop
);
1022 /* Sinks (output devices) list */
1023 op
= pa_context_get_sink_info_list(sys
->context
, sink_add_cb
, aout
);
1024 if (likely(op
!= NULL
))
1026 while (pa_operation_get_state(op
) == PA_OPERATION_RUNNING
)
1027 pa_threaded_mainloop_wait(sys
->mainloop
);
1028 pa_operation_unref(op
);
1031 /* Context events */
1032 const pa_subscription_mask_t mask
= PA_SUBSCRIPTION_MASK_SINK
1033 | PA_SUBSCRIPTION_MASK_SINK_INPUT
;
1034 pa_context_set_subscribe_callback(sys
->context
, context_cb
, aout
);
1035 op
= pa_context_subscribe(sys
->context
, mask
, NULL
, NULL
);
1036 if (likely(op
!= NULL
))
1037 pa_operation_unref(op
);
1038 pa_threaded_mainloop_unlock(sys
->mainloop
);
1043 static void Close(vlc_object_t
*obj
)
1045 audio_output_t
*aout
= (audio_output_t
*)obj
;
1046 aout_sys_t
*sys
= aout
->sys
;
1047 pa_context
*ctx
= sys
->context
;
1049 pa_threaded_mainloop_lock(sys
->mainloop
);
1050 pa_context_set_subscribe_callback(sys
->context
, NULL
, NULL
);
1051 pa_threaded_mainloop_unlock(sys
->mainloop
);
1052 vlc_pa_disconnect(obj
, ctx
, sys
->mainloop
);
1054 for (struct sink
*sink
= sys
->sinks
, *next
; sink
!= NULL
; sink
= next
)
1059 free(sys
->sink_force
);