input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / audio_output / pulse.c
blob2753fb87fdb083a3639417e0b1d9f2c76a745c30
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 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <math.h>
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_aout.h>
33 #include <vlc_cpu.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 * );
41 vlc_module_begin ()
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 )
49 vlc_module_end ()
51 /* NOTE:
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. */
57 struct sink
59 struct sink *next;
60 uint32_t index;
61 char name[1];
64 typedef struct
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 */
79 } aout_sys_t;
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);
89 /*** Sink ***/
90 static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
91 void *userdata)
93 audio_output_t *aout = userdata;
94 aout_sys_t *sys = aout->sys;
96 if (eol)
98 pa_threaded_mainloop_signal(sys->mainloop, 0);
99 return;
101 (void) ctx;
103 msg_Dbg(aout, "adding sink %"PRIu32": %s (%s)", i->index, i->name,
104 i->description);
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))
110 return;
112 sink->next = sys->sinks;
113 sink->index = i->index;
114 memcpy(sink->name, i->name, namelen + 1);
115 sys->sinks = sink;
118 static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
119 void *userdata)
121 audio_output_t *aout = userdata;
123 if (eol)
124 return;
125 (void) ctx;
127 msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
128 i->description);
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)
142 *pp = sink->next;
143 aout_HotplugReport(aout, sink->name, NULL);
144 free(sink);
146 else
147 pp = &sink->next;
150 static void sink_event(pa_context *ctx, unsigned type, uint32_t idx,
151 audio_output_t *aout)
153 pa_operation *op = NULL;
155 switch (type)
157 case PA_SUBSCRIPTION_EVENT_NEW:
158 op = pa_context_get_sink_info_by_index(ctx, idx, sink_add_cb,
159 aout);
160 break;
161 case PA_SUBSCRIPTION_EVENT_CHANGE:
162 op = pa_context_get_sink_info_by_index(ctx, idx, sink_mod_cb,
163 aout);
164 break;
165 case PA_SUBSCRIPTION_EVENT_REMOVE:
166 sink_del(idx, aout);
167 break;
169 if (op != NULL)
170 pa_operation_unref(op);
174 /*** Latency management and lip synchronization ***/
175 static void stream_start_now(pa_stream *s, audio_output_t *aout)
177 pa_operation *op;
179 assert ( ((aout_sys_t *)aout->sys)->trigger == NULL );
181 op = pa_stream_cork(s, 0, NULL, NULL);
182 if (op != 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;
192 pa_operation *op;
194 if (sys->trigger != NULL) {
195 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
196 sys->trigger = NULL;
199 op = pa_stream_cork(s, 1, NULL, NULL);
200 if (op != 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);
214 sys->trigger = NULL;
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;
228 vlc_tick_t delta;
230 assert (sys->first_pts != VLC_TICK_INVALID);
232 if (sys->trigger != NULL) {
233 vlc_pa_rttime_free(sys->mainloop, sys->trigger);
234 sys->trigger = NULL;
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;
244 if (delta > 0) {
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);
249 } else {
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);
278 default:
279 break;
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,
294 void *userdata)
296 audio_output_t *aout = userdata;
298 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
299 aout_PolicyReport(aout, true);
300 else
301 if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
302 aout_PolicyReport(aout, false);
303 else
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);
308 } else
309 msg_Warn (aout, "unhandled stream event \"%s\"", name);
310 (void) s;
311 (void) pl;
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;
327 pa_operation *op;
329 msg_Err(aout, "overflow, flushing");
330 op = pa_stream_flush(s, NULL, NULL);
331 if (unlikely(op == NULL))
332 return;
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");
342 (void) s;
345 static void stream_suspended_cb(pa_stream *s, void *userdata)
347 audio_output_t *aout = userdata;
349 msg_Dbg(aout, "suspended");
350 (void) s;
353 static void stream_underflow_cb(pa_stream *s, void *userdata)
355 audio_output_t *aout = userdata;
357 msg_Dbg(aout, "underflow");
358 (void) s;
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)
367 return -1;
368 pa_threaded_mainloop_wait(mainloop);
370 return 0;
374 /*** Sink input ***/
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;
381 if (eol)
382 return;
383 (void) ctx;
385 sys->cvolume = i->volume; /* cache volume for balance preservation */
386 VolumeReport(aout);
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)
394 pa_operation *op;
396 /* Gee... PA will not provide the infos directly in the event. */
397 switch (type)
399 case PA_SUBSCRIPTION_EVENT_REMOVE:
400 msg_Err(aout, "sink input killed!");
401 break;
403 default:
404 op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
405 aout);
406 if (likely(op != NULL))
407 pa_operation_unref(op);
408 break;
413 /*** Context ***/
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;
422 switch (facility)
424 case PA_SUBSCRIPTION_EVENT_SINK:
425 sink_event(ctx, type, idx, userdata);
426 break;
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);
432 break;
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;
446 int ret = -1;
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)
454 *delay = delta;
455 ret = 0;
458 pa_threaded_mainloop_unlock(sys->mainloop);
459 return ret;
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);
476 *pp = block;
477 if (unlikely(block == NULL))
478 return 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))
496 return;
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));
521 #endif
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);
541 if (paused) {
542 pa_stream_set_latency_update_callback(s, NULL, NULL);
543 stream_stop(s, aout);
544 } else {
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);
551 (void) date;
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;
561 pa_operation *op;
563 pa_threaded_mainloop_lock(sys->mainloop);
565 if (wait)
567 op = pa_stream_drain(s, NULL, NULL);
569 /* XXX: Loosy drain emulation.
570 * See #18141: drain callback is never received */
571 vlc_tick_t delay;
572 if (TimeGet(aout, &delay) == 0 && delay <= VLC_TICK_FROM_SEC(5))
573 vlc_tick_sleep(delay);
575 else
576 op = pa_stream_flush(s, NULL, NULL);
577 if (op != 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;
589 pa_operation *op;
590 pa_volume_t volume;
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;
598 else
599 volume = lroundf(vol);
601 if (s == NULL)
603 sys->volume_force = volume;
604 aout_VolumeReport(aout, (float)volume / (float)PA_VOLUME_NORM);
605 return 0;
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);
639 sys->flags_force |=
640 mute ? PA_STREAM_START_MUTED : PA_STREAM_START_UNMUTED;
641 aout_MuteReport(aout, mute);
642 return 0;
645 pa_operation *op;
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);
653 return 0;
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);
666 return 0;
669 pa_operation *op;
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,
674 NULL, NULL);
675 if (likely(op != NULL)) {
676 pa_operation_unref(op);
677 msg_Dbg(aout, "moving to sink %s", name);
678 } else
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],
694 size_t n)
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)
713 case VLC_CODEC_FL64:
714 fmt->i_format = VLC_CODEC_FL32;
715 /* fall through */
716 case VLC_CODEC_FL32:
717 ss.format = PA_SAMPLE_FLOAT32NE;
718 break;
719 case VLC_CODEC_S32N:
720 ss.format = PA_SAMPLE_S32NE;
721 break;
722 case VLC_CODEC_S16N:
723 ss.format = PA_SAMPLE_S16NE;
724 break;
725 case VLC_CODEC_U8:
726 ss.format = PA_SAMPLE_U8;
727 break;
728 case VLC_CODEC_A52:
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;
733 fmt->i_channels = 2;
734 encoding = PA_ENCODING_AC3_IEC61937;
735 ss.format = PA_SAMPLE_S16NE;
736 break;
737 case VLC_CODEC_EAC3:
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;
742 fmt->i_channels = 2;
743 encoding = PA_ENCODING_EAC3_IEC61937;
744 ss.format = PA_SAMPLE_S16NE;
745 break;
746 /* case VLC_CODEC_MPGA:
747 fmt->i_format = VLC_CODEC_SPDIFL FIXME;
748 encoding = PA_ENCODING_MPEG_IEC61937;
749 break;*/
750 case VLC_CODEC_DTS:
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;
755 fmt->i_channels = 2;
756 encoding = PA_ENCODING_DTS_IEC61937;
757 ss.format = PA_SAMPLE_S16NE;
758 break;
759 default:
760 if (!AOUT_FMT_LINEAR(fmt) || aout_FormatNbChannels(fmt) == 0)
761 return VLC_EGENERIC;
763 if (HAVE_FPU)
765 fmt->i_format = VLC_CODEC_FL32;
766 ss.format = PA_SAMPLE_FLOAT32NE;
768 else
770 fmt->i_format = VLC_CODEC_S16N;
771 ss.format = PA_SAMPLE_S16NE;
773 break;
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");
780 return VLC_EGENERIC;
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;
792 attr.maxlength = -1;
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);
808 sys->trigger = NULL;
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
835 | PA_STREAM_FIX_RATE
836 | PA_STREAM_FIX_CHANNELS);
838 else
840 /* Channel mapping (order defined in vlc_aout.h) */
841 struct pa_channel_map map;
842 map.channels = 0;
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;
862 else
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");
877 return VLC_EGENERIC;
878 } else {
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");
893 if (str != NULL)
895 static const char *const role_map[][2] = {
896 { "accessibility", "a11y" },
897 { "animation", "animation" },
898 { "communication", "phone" },
899 { "game", "game" },
900 { "music", "music" },
901 { "notification", "event" },
902 { "production", "production" },
903 { "test", "test" },
904 { "video", "video" },
906 const char *role = str_map(str, role_map, ARRAY_SIZE(role_map));
907 if (role != NULL)
908 pa_proplist_sets(props, PA_PROP_MEDIA_ROLE, role);
909 free(str);
913 pa_threaded_mainloop_lock(sys->mainloop);
914 pa_stream *s = pa_stream_new_extended(sys->context, "audio stream",
915 &formatv, 1, props);
917 if (likely(props != NULL))
918 pa_proplist_free(props);
919 pa_format_info_free(formatv);
921 if (s == NULL) {
922 pa_threaded_mainloop_unlock(sys->mainloop);
923 vlc_pa_error(aout, "stream creation failure", sys->context);
924 return VLC_EGENERIC;
926 assert(sys->stream == NULL);
927 sys->stream = s;
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,
939 cvolume, NULL) < 0
940 || stream_wait(s, sys->mainloop)) {
941 if (encoding != PA_ENCODING_PCM)
942 vlc_pa_error(aout, "digital pass-through stream connection failure",
943 sys->context);
944 else
945 vlc_pa_error(aout, "stream connection failure", sys->context);
946 goto fail;
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);
963 return VLC_SUCCESS;
965 fail:
966 pa_threaded_mainloop_unlock(sys->mainloop);
967 Stop(aout);
968 return VLC_EGENERIC;
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);
995 pa_stream_unref(s);
996 sys->stream = 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));
1004 pa_operation *op;
1006 if (unlikely(sys == NULL))
1007 return VLC_ENOMEM;
1009 /* Allocate structures */
1010 pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
1011 if (ctx == NULL)
1013 free(sys);
1014 return VLC_EGENERIC;
1016 sys->stream = NULL;
1017 sys->context = ctx;
1018 sys->volume_force = PA_VOLUME_INVALID;
1019 sys->flags_force = PA_STREAM_NOFLAGS;
1020 sys->sink_force = NULL;
1021 sys->sinks = NULL;
1023 aout->sys = sys;
1024 aout->start = Start;
1025 aout->stop = Stop;
1026 aout->time_get = TimeGet;
1027 aout->play = Play;
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);
1053 return VLC_SUCCESS;
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)
1069 next = sink->next;
1070 free(sink);
1072 free(sys->sink_force);
1073 free(sys);