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 vlc_tick_t first_pts
; /**< Play stream timestamp of buffer start */
72 vlc_tick_t first_date
; /**< Play system timestamp 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 aout_VolumeReport(aout
, (float)volume
/ PA_VOLUME_NORM
);
90 static void sink_add_cb(pa_context
*ctx
, const pa_sink_info
*i
, int eol
,
93 audio_output_t
*aout
= userdata
;
94 aout_sys_t
*sys
= aout
->sys
;
98 pa_threaded_mainloop_signal(sys
->mainloop
, 0);
103 msg_Dbg(aout
, "adding sink %"PRIu32
": %s (%s)", i
->index
, i
->name
,
105 aout_HotplugReport(aout
, i
->name
, i
->description
);
107 size_t namelen
= strlen(i
->name
);
108 struct sink
*sink
= malloc(sizeof (*sink
) + namelen
);
109 if (unlikely(sink
== NULL
))
112 sink
->next
= sys
->sinks
;
113 sink
->index
= i
->index
;
114 memcpy(sink
->name
, i
->name
, namelen
+ 1);
118 static void sink_mod_cb(pa_context
*ctx
, const pa_sink_info
*i
, int eol
,
121 audio_output_t
*aout
= userdata
;
127 msg_Dbg(aout
, "changing sink %"PRIu32
": %s (%s)", i
->index
, i
->name
,
129 aout_HotplugReport(aout
, i
->name
, i
->description
);
132 static void sink_del(uint32_t index
, audio_output_t
*aout
)
134 aout_sys_t
*sys
= aout
->sys
;
135 struct sink
**pp
= &sys
->sinks
, *sink
;
137 msg_Dbg(aout
, "removing sink %"PRIu32
, index
);
139 while ((sink
= *pp
) != NULL
)
140 if (sink
->index
== index
)
143 aout_HotplugReport(aout
, sink
->name
, NULL
);
150 static void sink_event(pa_context
*ctx
, unsigned type
, uint32_t idx
,
151 audio_output_t
*aout
)
153 pa_operation
*op
= NULL
;
157 case PA_SUBSCRIPTION_EVENT_NEW
:
158 op
= pa_context_get_sink_info_by_index(ctx
, idx
, sink_add_cb
,
161 case PA_SUBSCRIPTION_EVENT_CHANGE
:
162 op
= pa_context_get_sink_info_by_index(ctx
, idx
, sink_mod_cb
,
165 case PA_SUBSCRIPTION_EVENT_REMOVE
:
170 pa_operation_unref(op
);
174 /*** Latency management and lip synchronization ***/
175 static void stream_start_now(pa_stream
*s
, audio_output_t
*aout
)
179 assert ( ((aout_sys_t
*)aout
->sys
)->trigger
== NULL
);
181 op
= pa_stream_cork(s
, 0, NULL
, NULL
);
183 pa_operation_unref(op
);
184 op
= pa_stream_trigger(s
, NULL
, NULL
);
185 if (likely(op
!= NULL
))
186 pa_operation_unref(op
);
189 static void stream_stop(pa_stream
*s
, audio_output_t
*aout
)
191 aout_sys_t
*sys
= aout
->sys
;
194 if (sys
->trigger
!= NULL
) {
195 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
199 op
= pa_stream_cork(s
, 1, NULL
, NULL
);
201 pa_operation_unref(op
);
204 static void stream_trigger_cb(pa_mainloop_api
*api
, pa_time_event
*e
,
205 const struct timeval
*tv
, void *userdata
)
207 audio_output_t
*aout
= userdata
;
208 aout_sys_t
*sys
= aout
->sys
;
210 assert (sys
->trigger
== e
);
212 msg_Dbg(aout
, "starting deferred");
213 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
215 stream_start_now(sys
->stream
, aout
);
216 (void) api
; (void) e
; (void) tv
;
220 * Starts or resumes the playback stream.
221 * Tries start playing back audio samples at the most accurate time
222 * in order to minimize desync and resampling during early playback.
223 * @note PulseAudio lock required.
225 static void stream_start(pa_stream
*s
, audio_output_t
*aout
, vlc_tick_t date
)
227 aout_sys_t
*sys
= aout
->sys
;
230 assert (sys
->first_pts
!= VLC_TICK_INVALID
);
232 if (sys
->trigger
!= NULL
) {
233 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
237 delta
= vlc_pa_get_latency(aout
, sys
->context
, s
);
238 if (unlikely(delta
== VLC_TICK_INVALID
)) {
239 msg_Dbg(aout
, "cannot synchronize start");
240 delta
= 0; /* screwed */
243 delta
= (date
- vlc_tick_now()) - delta
;
245 msg_Dbg(aout
, "deferring start (%"PRId64
" us)", delta
);
246 delta
+= pa_rtclock_now();
247 sys
->trigger
= pa_context_rttime_new(sys
->context
, delta
,
248 stream_trigger_cb
, aout
);
250 msg_Warn(aout
, "starting late (%"PRId64
" us)", delta
);
251 stream_start_now(s
, aout
);
255 static void stream_latency_cb(pa_stream
*s
, void *userdata
)
257 audio_output_t
*aout
= userdata
;
258 aout_sys_t
*sys
= aout
->sys
;
260 /* This callback is _never_ called while paused. */
261 if (sys
->first_pts
== VLC_TICK_INVALID
)
262 return; /* nothing to do if buffers are (still) empty */
263 if (pa_stream_is_corked(s
) > 0)
264 stream_start(s
, aout
, sys
->first_date
);
268 /*** Stream helpers ***/
269 static void stream_state_cb(pa_stream
*s
, void *userdata
)
271 pa_threaded_mainloop
*mainloop
= userdata
;
273 switch (pa_stream_get_state(s
)) {
274 case PA_STREAM_READY
:
275 case PA_STREAM_FAILED
:
276 case PA_STREAM_TERMINATED
:
277 pa_threaded_mainloop_signal(mainloop
, 0);
283 static void stream_buffer_attr_cb(pa_stream
*s
, void *userdata
)
285 audio_output_t
*aout
= userdata
;
286 const pa_buffer_attr
*pba
= pa_stream_get_buffer_attr(s
);
288 msg_Dbg(aout
, "changed buffer metrics: maxlength=%u, tlength=%u, "
289 "prebuf=%u, minreq=%u",
290 pba
->maxlength
, pba
->tlength
, pba
->prebuf
, pba
->minreq
);
293 static void stream_event_cb(pa_stream
*s
, const char *name
, pa_proplist
*pl
,
296 audio_output_t
*aout
= userdata
;
298 if (!strcmp(name
, PA_STREAM_EVENT_REQUEST_CORK
))
299 aout_PolicyReport(aout
, true);
301 if (!strcmp(name
, PA_STREAM_EVENT_REQUEST_UNCORK
))
302 aout_PolicyReport(aout
, false);
304 /* FIXME: expose aout_Restart() directly */
305 if (!strcmp(name
, PA_STREAM_EVENT_FORMAT_LOST
)) {
306 msg_Dbg (aout
, "format lost");
307 aout_RestartRequest (aout
, AOUT_RESTART_OUTPUT
);
309 msg_Warn (aout
, "unhandled stream event \"%s\"", name
);
314 static void stream_moved_cb(pa_stream
*s
, void *userdata
)
316 audio_output_t
*aout
= userdata
;
317 const char *name
= pa_stream_get_device_name(s
);
319 msg_Dbg(aout
, "connected to sink %s", name
);
320 aout_DeviceReport(aout
, name
);
323 static void stream_overflow_cb(pa_stream
*s
, void *userdata
)
325 audio_output_t
*aout
= userdata
;
326 aout_sys_t
*sys
= aout
->sys
;
329 msg_Err(aout
, "overflow, flushing");
330 op
= pa_stream_flush(s
, NULL
, NULL
);
331 if (unlikely(op
== NULL
))
333 pa_operation_unref(op
);
334 sys
->first_pts
= VLC_TICK_INVALID
;
337 static void stream_started_cb(pa_stream
*s
, void *userdata
)
339 audio_output_t
*aout
= userdata
;
341 msg_Dbg(aout
, "started");
345 static void stream_suspended_cb(pa_stream
*s
, void *userdata
)
347 audio_output_t
*aout
= userdata
;
349 msg_Dbg(aout
, "suspended");
353 static void stream_underflow_cb(pa_stream
*s
, void *userdata
)
355 audio_output_t
*aout
= userdata
;
357 msg_Dbg(aout
, "underflow");
361 static int stream_wait(pa_stream
*stream
, pa_threaded_mainloop
*mainloop
)
363 pa_stream_state_t state
;
365 while ((state
= pa_stream_get_state(stream
)) != PA_STREAM_READY
) {
366 if (state
== PA_STREAM_FAILED
|| state
== PA_STREAM_TERMINATED
)
368 pa_threaded_mainloop_wait(mainloop
);
375 static void sink_input_info_cb(pa_context
*ctx
, const pa_sink_input_info
*i
,
376 int eol
, void *userdata
)
378 audio_output_t
*aout
= userdata
;
379 aout_sys_t
*sys
= aout
->sys
;
385 sys
->cvolume
= i
->volume
; /* cache volume for balance preservation */
387 aout_MuteReport(aout
, i
->mute
);
390 static void sink_input_event(pa_context
*ctx
,
391 pa_subscription_event_type_t type
,
392 uint32_t idx
, audio_output_t
*aout
)
396 /* Gee... PA will not provide the infos directly in the event. */
399 case PA_SUBSCRIPTION_EVENT_REMOVE
:
400 msg_Err(aout
, "sink input killed!");
404 op
= pa_context_get_sink_input_info(ctx
, idx
, sink_input_info_cb
,
406 if (likely(op
!= NULL
))
407 pa_operation_unref(op
);
414 static void context_cb(pa_context
*ctx
, pa_subscription_event_type_t type
,
415 uint32_t idx
, void *userdata
)
417 audio_output_t
*aout
= userdata
;
418 aout_sys_t
*sys
= aout
->sys
;
419 unsigned facility
= type
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
;
421 type
&= PA_SUBSCRIPTION_EVENT_TYPE_MASK
;
424 case PA_SUBSCRIPTION_EVENT_SINK
:
425 sink_event(ctx
, type
, idx
, userdata
);
428 case PA_SUBSCRIPTION_EVENT_SINK_INPUT
:
429 /* only interested in our sink input */
430 if (sys
->stream
!= NULL
&& idx
== pa_stream_get_index(sys
->stream
))
431 sink_input_event(ctx
, type
, idx
, userdata
);
434 default: /* unsubscribed facility?! */
435 vlc_assert_unreachable();
440 /*** VLC audio output callbacks ***/
442 static int TimeGet(audio_output_t
*aout
, vlc_tick_t
*restrict delay
)
444 aout_sys_t
*sys
= aout
->sys
;
445 pa_stream
*s
= sys
->stream
;
448 pa_threaded_mainloop_lock(sys
->mainloop
);
449 if (pa_stream_is_corked(s
) <= 0)
450 { /* latency is relevant only if not corked */
451 vlc_tick_t delta
= vlc_pa_get_latency(aout
, sys
->context
, s
);
452 if (delta
!= VLC_TICK_INVALID
)
458 pa_threaded_mainloop_unlock(sys
->mainloop
);
462 /* Memory free callback. The block_t address is in front of the data. */
463 static void data_free(void *data
)
465 block_t
**pp
= data
, *block
;
467 memcpy(&block
, pp
- 1, sizeof (block
));
468 block_Release(block
);
471 static void *data_convert(block_t
**pp
)
473 block_t
*block
= *pp
;
474 /* In most cases, there is enough head room, and this is really cheap: */
475 block
= block_Realloc(block
, sizeof (block
), block
->i_buffer
);
477 if (unlikely(block
== NULL
))
480 memcpy(block
->p_buffer
, &block
, sizeof (block
));
481 block
->p_buffer
+= sizeof (block
);
482 block
->i_buffer
-= sizeof (block
);
483 return block
->p_buffer
;
487 * Queue one audio frame to the playback stream
489 static void Play(audio_output_t
*aout
, block_t
*block
, vlc_tick_t date
)
491 aout_sys_t
*sys
= aout
->sys
;
492 pa_stream
*s
= sys
->stream
;
494 const void *ptr
= data_convert(&block
);
495 if (unlikely(ptr
== NULL
))
498 size_t len
= block
->i_buffer
;
500 /* Note: The core already holds the output FIFO lock at this point.
501 * Therefore we must not under any circumstances (try to) acquire the
502 * output FIFO lock while the PulseAudio threaded main loop lock is held
503 * (including from PulseAudio stream callbacks). Otherwise lock inversion
504 * will take place, and sooner or later a deadlock. */
505 pa_threaded_mainloop_lock(sys
->mainloop
);
507 if (sys
->first_pts
== VLC_TICK_INVALID
) {
508 sys
->first_pts
= block
->i_pts
;
509 sys
->first_date
= date
;
512 if (pa_stream_is_corked(s
) > 0)
513 stream_start(s
, aout
, date
);
515 #if 0 /* Fault injector to test underrun recovery */
516 static volatile unsigned u
= 0;
517 if ((++u
% 1000) == 0) {
518 msg_Err(aout
, "fault injection");
519 pa_operation_unref(pa_stream_flush(s
, NULL
, NULL
));
523 if (pa_stream_write(s
, ptr
, len
, data_free
, 0, PA_SEEK_RELATIVE
) < 0) {
524 vlc_pa_error(aout
, "cannot write", sys
->context
);
525 block_Release(block
);
528 pa_threaded_mainloop_unlock(sys
->mainloop
);
532 * Cork or uncork the playback stream
534 static void Pause(audio_output_t
*aout
, bool paused
, vlc_tick_t date
)
536 aout_sys_t
*sys
= aout
->sys
;
537 pa_stream
*s
= sys
->stream
;
539 pa_threaded_mainloop_lock(sys
->mainloop
);
542 pa_stream_set_latency_update_callback(s
, NULL
, NULL
);
543 stream_stop(s
, aout
);
545 pa_stream_set_latency_update_callback(s
, stream_latency_cb
, aout
);
546 if (likely(sys
->first_pts
!= VLC_TICK_INVALID
))
547 stream_start_now(s
, aout
);
550 pa_threaded_mainloop_unlock(sys
->mainloop
);
555 * Flush or drain the playback stream
557 static void Flush(audio_output_t
*aout
, bool wait
)
559 aout_sys_t
*sys
= aout
->sys
;
560 pa_stream
*s
= sys
->stream
;
563 pa_threaded_mainloop_lock(sys
->mainloop
);
567 op
= pa_stream_drain(s
, NULL
, NULL
);
569 /* XXX: Loosy drain emulation.
570 * See #18141: drain callback is never received */
572 if (TimeGet(aout
, &delay
) == 0 && delay
<= VLC_TICK_FROM_SEC(5))
573 vlc_tick_sleep(delay
);
576 op
= pa_stream_flush(s
, NULL
, NULL
);
578 pa_operation_unref(op
);
579 sys
->first_pts
= VLC_TICK_INVALID
;
580 stream_stop(s
, aout
);
582 pa_threaded_mainloop_unlock(sys
->mainloop
);
585 static int VolumeSet(audio_output_t
*aout
, float vol
)
587 aout_sys_t
*sys
= aout
->sys
;
588 pa_stream
*s
= sys
->stream
;
592 /* VLC provides the software volume so convert directly to PulseAudio
593 * software volume, pa_volume_t. This is not a linear amplification factor
594 * so do not use PulseAudio linear amplification! */
595 vol
*= PA_VOLUME_NORM
;
596 if (unlikely(vol
>= (float)PA_VOLUME_MAX
))
597 volume
= PA_VOLUME_MAX
;
599 volume
= lroundf(vol
);
603 sys
->volume_force
= volume
;
604 aout_VolumeReport(aout
, (float)volume
/ (float)PA_VOLUME_NORM
);
608 pa_threaded_mainloop_lock(sys
->mainloop
);
610 if (!pa_cvolume_valid(&sys
->cvolume
))
612 const pa_sample_spec
*ss
= pa_stream_get_sample_spec(s
);
614 msg_Warn(aout
, "balance clobbered by volume change");
615 pa_cvolume_set(&sys
->cvolume
, ss
->channels
, PA_VOLUME_NORM
);
618 /* Preserve the balance (VLC does not support it). */
619 pa_cvolume cvolume
= sys
->cvolume
;
620 pa_cvolume_scale(&cvolume
, PA_VOLUME_NORM
);
621 pa_sw_cvolume_multiply_scalar(&cvolume
, &cvolume
, volume
);
622 assert(pa_cvolume_valid(&cvolume
));
624 op
= pa_context_set_sink_input_volume(sys
->context
, pa_stream_get_index(s
),
625 &cvolume
, NULL
, NULL
);
626 if (likely(op
!= NULL
))
627 pa_operation_unref(op
);
628 pa_threaded_mainloop_unlock(sys
->mainloop
);
629 return likely(op
!= NULL
) ? 0 : -1;
632 static int MuteSet(audio_output_t
*aout
, bool mute
)
634 aout_sys_t
*sys
= aout
->sys
;
636 if (sys
->stream
== NULL
)
638 sys
->flags_force
&= ~(PA_STREAM_START_MUTED
|PA_STREAM_START_UNMUTED
);
640 mute
? PA_STREAM_START_MUTED
: PA_STREAM_START_UNMUTED
;
641 aout_MuteReport(aout
, mute
);
646 uint32_t idx
= pa_stream_get_index(sys
->stream
);
647 pa_threaded_mainloop_lock(sys
->mainloop
);
648 op
= pa_context_set_sink_input_mute(sys
->context
, idx
, mute
, NULL
, NULL
);
649 if (likely(op
!= NULL
))
650 pa_operation_unref(op
);
651 pa_threaded_mainloop_unlock(sys
->mainloop
);
656 static int StreamMove(audio_output_t
*aout
, const char *name
)
658 aout_sys_t
*sys
= aout
->sys
;
660 if (sys
->stream
== NULL
)
662 msg_Dbg(aout
, "will connect to sink %s", name
);
663 free(sys
->sink_force
);
664 sys
->sink_force
= strdup(name
);
665 aout_DeviceReport(aout
, name
);
670 uint32_t idx
= pa_stream_get_index(sys
->stream
);
672 pa_threaded_mainloop_lock(sys
->mainloop
);
673 op
= pa_context_move_sink_input_by_name(sys
->context
, idx
, name
,
675 if (likely(op
!= NULL
)) {
676 pa_operation_unref(op
);
677 msg_Dbg(aout
, "moving to sink %s", name
);
679 vlc_pa_error(aout
, "cannot move sink input", sys
->context
);
680 pa_threaded_mainloop_unlock(sys
->mainloop
);
682 return (op
!= NULL
) ? 0 : -1;
685 static void Stop(audio_output_t
*);
687 static int strcmp_void(const void *a
, const void *b
)
689 const char *const *entry
= b
;
690 return strcmp(a
, *entry
);
693 static const char *str_map(const char *key
, const char *const table
[][2],
696 const char **r
= bsearch(key
, table
, n
, sizeof (*table
), strcmp_void
);
697 return (r
!= NULL
) ? r
[1] : NULL
;
701 * Create a PulseAudio playback stream, a.k.a. a sink input.
703 static int Start(audio_output_t
*aout
, audio_sample_format_t
*restrict fmt
)
705 aout_sys_t
*sys
= aout
->sys
;
707 /* Sample format specification */
708 struct pa_sample_spec ss
= { .format
= PA_SAMPLE_INVALID
};
709 pa_encoding_t encoding
= PA_ENCODING_PCM
;
711 switch (fmt
->i_format
)
714 fmt
->i_format
= VLC_CODEC_FL32
;
717 ss
.format
= PA_SAMPLE_FLOAT32NE
;
720 ss
.format
= PA_SAMPLE_S32NE
;
723 ss
.format
= PA_SAMPLE_S16NE
;
726 ss
.format
= PA_SAMPLE_U8
;
729 fmt
->i_format
= VLC_CODEC_SPDIFL
;
730 fmt
->i_bytes_per_frame
= 4;
731 fmt
->i_frame_length
= 1;
732 fmt
->i_physical_channels
= AOUT_CHANS_2_0
;
734 encoding
= PA_ENCODING_AC3_IEC61937
;
735 ss
.format
= PA_SAMPLE_S16NE
;
738 fmt
->i_format
= VLC_CODEC_SPDIFL
;
739 fmt
->i_bytes_per_frame
= 4;
740 fmt
->i_frame_length
= 1;
741 fmt
->i_physical_channels
= AOUT_CHANS_2_0
;
743 encoding
= PA_ENCODING_EAC3_IEC61937
;
744 ss
.format
= PA_SAMPLE_S16NE
;
746 /* case VLC_CODEC_MPGA:
747 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
748 encoding = PA_ENCODING_MPEG_IEC61937;
751 fmt
->i_format
= VLC_CODEC_SPDIFL
;
752 fmt
->i_bytes_per_frame
= 4;
753 fmt
->i_frame_length
= 1;
754 fmt
->i_physical_channels
= AOUT_CHANS_2_0
;
756 encoding
= PA_ENCODING_DTS_IEC61937
;
757 ss
.format
= PA_SAMPLE_S16NE
;
760 if (!AOUT_FMT_LINEAR(fmt
) || aout_FormatNbChannels(fmt
) == 0)
765 fmt
->i_format
= VLC_CODEC_FL32
;
766 ss
.format
= PA_SAMPLE_FLOAT32NE
;
770 fmt
->i_format
= VLC_CODEC_S16N
;
771 ss
.format
= PA_SAMPLE_S16NE
;
776 ss
.rate
= fmt
->i_rate
;
777 ss
.channels
= fmt
->i_channels
;
778 if (!pa_sample_spec_valid(&ss
)) {
779 msg_Err(aout
, "unsupported sample specification");
783 /* Stream parameters */
784 pa_stream_flags_t flags
= sys
->flags_force
785 | PA_STREAM_START_CORKED
786 | PA_STREAM_INTERPOLATE_TIMING
787 | PA_STREAM_NOT_MONOTONIC
788 | PA_STREAM_AUTO_TIMING_UPDATE
789 | PA_STREAM_FIX_RATE
;
791 struct pa_buffer_attr attr
;
793 /* PulseAudio goes berserk if the target length (tlength) is not
794 * significantly longer than 2 periods (minreq), or when the period length
795 * is unspecified and the target length is short. */
796 attr
.tlength
= pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME
, &ss
);
797 attr
.prebuf
= 0; /* trigger manually */
798 attr
.minreq
= pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME
, &ss
);
799 attr
.fragsize
= 0; /* not used for output */
801 pa_cvolume
*cvolume
= NULL
, cvolumebuf
;
802 if (PA_VOLUME_IS_VALID(sys
->volume_force
))
804 cvolume
= &cvolumebuf
;
805 pa_cvolume_set(cvolume
, ss
.channels
, sys
->volume_force
);
809 pa_cvolume_init(&sys
->cvolume
);
810 sys
->first_pts
= VLC_TICK_INVALID
;
812 pa_format_info
*formatv
= pa_format_info_new();
813 formatv
->encoding
= encoding
;
814 pa_format_info_set_rate(formatv
, ss
.rate
);
815 if (ss
.format
!= PA_SAMPLE_INVALID
)
816 pa_format_info_set_sample_format(formatv
, ss
.format
);
818 if (fmt
->channel_type
== AUDIO_CHANNEL_TYPE_AMBISONICS
)
820 fmt
->channel_type
= AUDIO_CHANNEL_TYPE_BITMAP
;
822 /* Setup low latency in order to quickly react to ambisonics
823 * filters viewpoint changes. */
824 flags
|= PA_STREAM_ADJUST_LATENCY
;
825 attr
.tlength
= pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME
, &ss
);
828 if (encoding
!= PA_ENCODING_PCM
)
830 pa_format_info_set_channels(formatv
, ss
.channels
);
832 /* FIX flags are only permitted for PCM, and there is no way to pass
833 * different flags for different formats... */
834 flags
&= ~(PA_STREAM_FIX_FORMAT
836 | PA_STREAM_FIX_CHANNELS
);
840 /* Channel mapping (order defined in vlc_aout.h) */
841 struct pa_channel_map map
;
844 if (fmt
->i_physical_channels
& AOUT_CHAN_LEFT
)
845 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_FRONT_LEFT
;
846 if (fmt
->i_physical_channels
& AOUT_CHAN_RIGHT
)
847 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_FRONT_RIGHT
;
848 if (fmt
->i_physical_channels
& AOUT_CHAN_MIDDLELEFT
)
849 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_SIDE_LEFT
;
850 if (fmt
->i_physical_channels
& AOUT_CHAN_MIDDLERIGHT
)
851 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_SIDE_RIGHT
;
852 if (fmt
->i_physical_channels
& AOUT_CHAN_REARLEFT
)
853 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_REAR_LEFT
;
854 if (fmt
->i_physical_channels
& AOUT_CHAN_REARRIGHT
)
855 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_REAR_RIGHT
;
856 if (fmt
->i_physical_channels
& AOUT_CHAN_REARCENTER
)
857 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_REAR_CENTER
;
858 if (fmt
->i_physical_channels
& AOUT_CHAN_CENTER
)
860 if (ss
.channels
== 1)
861 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_MONO
;
863 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_FRONT_CENTER
;
865 if (fmt
->i_physical_channels
& AOUT_CHAN_LFE
)
866 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_LFE
;
868 static_assert(AOUT_CHAN_MAX
== 9, "Missing channels");
870 for (unsigned i
= 0; map
.channels
< ss
.channels
; i
++) {
871 map
.map
[map
.channels
++] = PA_CHANNEL_POSITION_AUX0
+ i
;
872 msg_Warn(aout
, "mapping channel %"PRIu8
" to AUX%u", map
.channels
, i
);
875 if (!pa_channel_map_valid(&map
)) {
876 msg_Err(aout
, "unsupported channel map");
879 const char *name
= pa_channel_map_to_name(&map
);
880 msg_Dbg(aout
, "using %s channel map", (name
!= NULL
) ? name
: "?");
883 pa_format_info_set_channels(formatv
, ss
.channels
);
884 pa_format_info_set_channel_map(formatv
, &map
);
887 /* Create a playback stream */
888 pa_proplist
*props
= pa_proplist_new();
889 if (likely(props
!= NULL
))
891 /* TODO: set other stream properties */
892 char *str
= var_InheritString(aout
, "role");
895 static const char *const role_map
[][2] = {
896 { "accessibility", "a11y" },
897 { "animation", "animation" },
898 { "communication", "phone" },
900 { "music", "music" },
901 { "notification", "event" },
902 { "production", "production" },
904 { "video", "video" },
906 const char *role
= str_map(str
, role_map
, ARRAY_SIZE(role_map
));
908 pa_proplist_sets(props
, PA_PROP_MEDIA_ROLE
, role
);
913 pa_threaded_mainloop_lock(sys
->mainloop
);
914 pa_stream
*s
= pa_stream_new_extended(sys
->context
, "audio stream",
917 if (likely(props
!= NULL
))
918 pa_proplist_free(props
);
919 pa_format_info_free(formatv
);
922 pa_threaded_mainloop_unlock(sys
->mainloop
);
923 vlc_pa_error(aout
, "stream creation failure", sys
->context
);
926 assert(sys
->stream
== NULL
);
928 pa_stream_set_state_callback(s
, stream_state_cb
, sys
->mainloop
);
929 pa_stream_set_buffer_attr_callback(s
, stream_buffer_attr_cb
, aout
);
930 pa_stream_set_event_callback(s
, stream_event_cb
, aout
);
931 pa_stream_set_latency_update_callback(s
, stream_latency_cb
, aout
);
932 pa_stream_set_moved_callback(s
, stream_moved_cb
, aout
);
933 pa_stream_set_overflow_callback(s
, stream_overflow_cb
, aout
);
934 pa_stream_set_started_callback(s
, stream_started_cb
, aout
);
935 pa_stream_set_suspended_callback(s
, stream_suspended_cb
, aout
);
936 pa_stream_set_underflow_callback(s
, stream_underflow_cb
, aout
);
938 if (pa_stream_connect_playback(s
, sys
->sink_force
, &attr
, flags
,
940 || stream_wait(s
, sys
->mainloop
)) {
941 if (encoding
!= PA_ENCODING_PCM
)
942 vlc_pa_error(aout
, "digital pass-through stream connection failure",
945 vlc_pa_error(aout
, "stream connection failure", sys
->context
);
948 sys
->volume_force
= PA_VOLUME_INVALID
;
949 sys
->flags_force
= PA_STREAM_NOFLAGS
;
950 free(sys
->sink_force
);
951 sys
->sink_force
= NULL
;
953 if (encoding
== PA_ENCODING_PCM
)
955 const struct pa_sample_spec
*spec
= pa_stream_get_sample_spec(s
);
956 fmt
->i_rate
= spec
->rate
;
959 stream_buffer_attr_cb(s
, aout
);
960 stream_moved_cb(s
, aout
);
961 pa_threaded_mainloop_unlock(sys
->mainloop
);
966 pa_threaded_mainloop_unlock(sys
->mainloop
);
972 * Removes a PulseAudio playback stream
974 static void Stop(audio_output_t
*aout
)
976 aout_sys_t
*sys
= aout
->sys
;
977 pa_stream
*s
= sys
->stream
;
979 pa_threaded_mainloop_lock(sys
->mainloop
);
980 if (unlikely(sys
->trigger
!= NULL
))
981 vlc_pa_rttime_free(sys
->mainloop
, sys
->trigger
);
982 pa_stream_disconnect(s
);
984 /* Clear all callbacks */
985 pa_stream_set_state_callback(s
, NULL
, NULL
);
986 pa_stream_set_buffer_attr_callback(s
, NULL
, NULL
);
987 pa_stream_set_event_callback(s
, NULL
, NULL
);
988 pa_stream_set_latency_update_callback(s
, NULL
, NULL
);
989 pa_stream_set_moved_callback(s
, NULL
, NULL
);
990 pa_stream_set_overflow_callback(s
, NULL
, NULL
);
991 pa_stream_set_started_callback(s
, NULL
, NULL
);
992 pa_stream_set_suspended_callback(s
, NULL
, NULL
);
993 pa_stream_set_underflow_callback(s
, NULL
, NULL
);
997 pa_threaded_mainloop_unlock(sys
->mainloop
);
1000 static int Open(vlc_object_t
*obj
)
1002 audio_output_t
*aout
= (audio_output_t
*)obj
;
1003 aout_sys_t
*sys
= malloc(sizeof (*sys
));
1006 if (unlikely(sys
== NULL
))
1009 /* Allocate structures */
1010 pa_context
*ctx
= vlc_pa_connect(obj
, &sys
->mainloop
);
1014 return VLC_EGENERIC
;
1018 sys
->volume_force
= PA_VOLUME_INVALID
;
1019 sys
->flags_force
= PA_STREAM_NOFLAGS
;
1020 sys
->sink_force
= NULL
;
1024 aout
->start
= Start
;
1026 aout
->time_get
= TimeGet
;
1028 aout
->pause
= Pause
;
1029 aout
->flush
= Flush
;
1030 aout
->volume_set
= VolumeSet
;
1031 aout
->mute_set
= MuteSet
;
1032 aout
->device_select
= StreamMove
;
1034 pa_threaded_mainloop_lock(sys
->mainloop
);
1035 /* Sinks (output devices) list */
1036 op
= pa_context_get_sink_info_list(sys
->context
, sink_add_cb
, aout
);
1037 if (likely(op
!= NULL
))
1039 while (pa_operation_get_state(op
) == PA_OPERATION_RUNNING
)
1040 pa_threaded_mainloop_wait(sys
->mainloop
);
1041 pa_operation_unref(op
);
1044 /* Context events */
1045 const pa_subscription_mask_t mask
= PA_SUBSCRIPTION_MASK_SINK
1046 | PA_SUBSCRIPTION_MASK_SINK_INPUT
;
1047 pa_context_set_subscribe_callback(sys
->context
, context_cb
, aout
);
1048 op
= pa_context_subscribe(sys
->context
, mask
, NULL
, NULL
);
1049 if (likely(op
!= NULL
))
1050 pa_operation_unref(op
);
1051 pa_threaded_mainloop_unlock(sys
->mainloop
);
1056 static void Close(vlc_object_t
*obj
)
1058 audio_output_t
*aout
= (audio_output_t
*)obj
;
1059 aout_sys_t
*sys
= aout
->sys
;
1060 pa_context
*ctx
= sys
->context
;
1062 pa_threaded_mainloop_lock(sys
->mainloop
);
1063 pa_context_set_subscribe_callback(sys
->context
, NULL
, NULL
);
1064 pa_threaded_mainloop_unlock(sys
->mainloop
);
1065 vlc_pa_disconnect(obj
, ctx
, sys
->mainloop
);
1067 for (struct sink
*sink
= sys
->sinks
, *next
; sink
!= NULL
; sink
= next
)
1072 free(sys
->sink_force
);