/*****************************************************************************
* pulse.c : Pulseaudio output plugin for vlc
*****************************************************************************
- * Copyright (C) 2008 the VideoLAN team
+ * Copyright (C) 2008 VLC authors and VideoLAN
* Copyright (C) 2009-2011 RĂ©mi Denis-Courmont
*
* Authors: Martin Hamrle <hamrle @ post . cz>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include <math.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_aout.h>
#include <vlc_cpu.h>
#include <pulse/pulseaudio.h>
-#include <vlc_pulse.h>
+#include "audio_output/vlcpulse.h"
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
set_callbacks( Open, Close )
vlc_module_end ()
-/* TODO:
- * - pause input on policy event
- * - resample to compensate for long term drift
- * - select music or video stream property correctly (?)
- * - set further appropriate stream properties
- * - update output devices list dynamically
- */
-
/* NOTE:
* Be careful what you do when the PulseAudio mainloop is held, which is to say
- * within PulseAudio callbacks, or after vlc_pa_lock().
+ * within PulseAudio callbacks, or after pa_threaded_mainloop_lock().
* In particular, a VLC variable callback cannot be triggered nor deleted with
* the PulseAudio mainloop lock held, if the callback acquires the lock. */
+struct sink
+{
+ struct sink *next;
+ uint32_t index;
+ pa_volume_t base_volume;
+ char name[1];
+};
+
struct aout_sys_t
{
pa_stream *stream; /**< PulseAudio playback stream object */
pa_context *context; /**< PulseAudio connection context */
+ pa_threaded_mainloop *mainloop; /**< PulseAudio thread */
+ pa_time_event *trigger; /**< Deferred stream trigger */
pa_volume_t base_volume; /**< 0dB reference volume */
pa_cvolume cvolume; /**< actual sink input volume */
- mtime_t pts; /**< Play time of buffer write offset */
- mtime_t desync; /**< Measured desynchronization */
- unsigned rate; /**< Current stream sample rate */
+ mtime_t first_pts; /**< Play time of buffer start */
+
+ pa_volume_t volume_force; /**< Forced volume (stream must be NULL) */
+ pa_stream_flags_t flags_force; /**< Forced flags (stream must be NULL) */
+ char *sink_force; /**< Forced sink name (stream must be NULL) */
+
+ struct sink *sinks; /**< Locally-cached list of sinks */
};
-/*** Sink ***/
-static void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol,
- void *userdata)
+static void VolumeReport(audio_output_t *aout)
{
- aout_instance_t *aout = userdata;
- vlc_value_t val, text;
+ aout_sys_t *sys = aout->sys;
+ pa_volume_t volume = pa_cvolume_max(&sys->cvolume);
- if (eol)
- return;
- (void) c;
+ volume = pa_sw_volume_divide(volume, sys->base_volume);
+ aout_VolumeReport(aout, (float)volume / PA_VOLUME_NORM);
+}
- msg_Dbg(aout, "listing sink %s (%"PRIu32"): %s", i->name, i->index,
- i->description);
- val.i_int = i->index;
- text.psz_string = (char *)i->description;
- var_Change(aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text);
+/*** Sink ***/
+static struct sink *sink_find(aout_sys_t *sys, uint32_t index)
+{
+ for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
+ if (sink->index == index)
+ return sink;
+ return NULL;
}
-static void sink_info_cb(pa_context *c, const pa_sink_info *i, int eol,
- void *userdata)
+static struct sink *sink_find_by_name(aout_sys_t *sys, const char *name)
{
- aout_instance_t *aout = userdata;
- aout_sys_t *sys = aout->output.p_sys;
+ for (struct sink *sink = sys->sinks; sink != NULL; sink = sink->next)
+ if (!strcmp(sink->name, name))
+ return sink;
+ return NULL;
+}
+
+static void sink_add_cb(pa_context *ctx, const pa_sink_info *i, int eol,
+ void *userdata)
+{
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
if (eol)
+ {
+ pa_threaded_mainloop_signal(sys->mainloop, 0);
+ return;
+ }
+ (void) ctx;
+
+ msg_Dbg(aout, "adding sink %"PRIu32": %s (%s)", i->index, i->name,
+ i->description);
+ aout_HotplugReport(aout, i->name, i->description);
+
+ size_t namelen = strlen(i->name);
+ struct sink *sink = malloc(sizeof (*sink) + namelen);
+ if (unlikely(sink == NULL))
return;
- (void) c;
+ sink->next = sys->sinks;
+ sink->index = i->index;
/* PulseAudio flat volume NORM / 100% / 0dB corresponds to no software
* amplification and maximum hardware amplification.
* VLC maps DEFAULT / 100% to no gain at all (software/hardware).
* Thus we need to use the sink base_volume as a multiplier,
* if and only if flat volume is active for our current sink. */
if (i->flags & PA_SINK_FLAT_VOLUME)
- sys->base_volume = i->base_volume;
+ sink->base_volume = i->base_volume;
else
- sys->base_volume = PA_VOLUME_NORM;
- msg_Dbg(aout, "base volume: %f", pa_sw_volume_to_linear(sys->base_volume));
+ sink->base_volume = PA_VOLUME_NORM;
+ memcpy(sink->name, i->name, namelen + 1);
+ sys->sinks = sink;
}
-/*** Stream helpers ***/
-static void stream_reset_sync(pa_stream *s, aout_instance_t *aout)
+static void sink_mod_cb(pa_context *ctx, const pa_sink_info *i, int eol,
+ void *userdata)
{
- aout_sys_t *sys = aout->output.p_sys;
- const unsigned rate = aout->output.output.i_rate;
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
- sys->pts = VLC_TS_INVALID;
- sys->desync = 0;
- pa_operation *op = pa_stream_update_sample_rate(s, rate, NULL, NULL);
- if (unlikely(op == NULL))
+ if (eol)
return;
- pa_operation_unref(op);
- sys->rate = rate;
+ (void) ctx;
+
+ msg_Dbg(aout, "changing sink %"PRIu32": %s (%s)", i->index, i->name,
+ i->description);
+ aout_HotplugReport(aout, i->name, i->description);
+
+ struct sink *sink = sink_find(sys, i->index);
+ if (unlikely(sink == NULL))
+ return;
+
+ if (i->flags & PA_SINK_FLAT_VOLUME)
+ sink->base_volume = i->base_volume;
+ else
+ sink->base_volume = PA_VOLUME_NORM;
}
-static void stream_state_cb(pa_stream *s, void *userdata)
+static void sink_del(uint32_t index, audio_output_t *aout)
{
- switch (pa_stream_get_state(s)) {
- case PA_STREAM_READY:
- case PA_STREAM_FAILED:
- case PA_STREAM_TERMINATED:
- vlc_pa_signal(0);
- default:
- break;
- }
- (void) userdata;
+ aout_sys_t *sys = aout->sys;
+ struct sink **pp = &sys->sinks, *sink;
+
+ msg_Dbg(aout, "removing sink %"PRIu32, index);
+
+ while ((sink = *pp) != NULL)
+ if (sink->index == index)
+ {
+ *pp = sink->next;
+ aout_HotplugReport(aout, sink->name, NULL);
+ free(sink);
+ }
+ else
+ pp = &sink->next;
}
-/* Latency management and lip synchronization */
-static void stream_latency_cb(pa_stream *s, void *userdata)
+static void sink_event(pa_context *ctx, unsigned type, uint32_t idx,
+ audio_output_t *aout)
{
- aout_instance_t *aout = userdata;
- aout_sys_t *sys = aout->output.p_sys;
- mtime_t delta, change;
+ pa_operation *op = NULL;
- if (sys->pts == VLC_TS_INVALID)
+ switch (type)
{
- msg_Dbg(aout, "missing latency from input");
- return;
+ case PA_SUBSCRIPTION_EVENT_NEW:
+ op = pa_context_get_sink_info_by_index(ctx, idx, sink_add_cb,
+ aout);
+ break;
+ case PA_SUBSCRIPTION_EVENT_CHANGE:
+ op = pa_context_get_sink_info_by_index(ctx, idx, sink_mod_cb,
+ aout);
+ break;
+ case PA_SUBSCRIPTION_EVENT_REMOVE:
+ sink_del(idx, aout);
+ break;
}
+ if (op != NULL)
+ pa_operation_unref(op);
+}
- /* Compute lip desynchronization */
- {
- pa_usec_t latency;
- int negative;
- if (pa_stream_get_latency(s, &latency, &negative)) {
- vlc_pa_error(aout, "missing latency", sys->context);
- return;
- }
- delta = sys->pts - mdate();
- if (unlikely(negative))
- delta += latency;
- else
- delta -= latency;
+/*** Latency management and lip synchronization ***/
+static void stream_start_now(pa_stream *s, audio_output_t *aout)
+{
+ pa_operation *op;
+
+ assert (aout->sys->trigger == NULL);
+
+ op = pa_stream_cork(s, 0, NULL, NULL);
+ if (op != NULL)
+ pa_operation_unref(op);
+ op = pa_stream_trigger(s, NULL, NULL);
+ if (likely(op != NULL))
+ pa_operation_unref(op);
+}
+
+static void stream_stop(pa_stream *s, audio_output_t *aout)
+{
+ aout_sys_t *sys = aout->sys;
+ pa_operation *op;
+
+ if (sys->trigger != NULL) {
+ vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+ sys->trigger = NULL;
}
- change = delta - sys->desync;
- sys->desync = delta;
- //msg_Dbg(aout, "desync: %+"PRId64" us (variation: %+"PRId64" us)",
- // delta, change);
+ op = pa_stream_cork(s, 1, NULL, NULL);
+ if (op != NULL)
+ pa_operation_unref(op);
+}
- if (delta < -AOUT_MAX_PTS_DELAY)
- msg_Warn(aout, "too late by %"PRId64" us", -delta);
- else if (delta > +AOUT_MAX_PTS_ADVANCE)
- msg_Warn(aout, "too early by %"PRId64" us", delta);
+static void stream_trigger_cb(pa_mainloop_api *api, pa_time_event *e,
+ const struct timeval *tv, void *userdata)
+{
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
- /* Compute playback sample rate */
- const unsigned inrate = aout->output.output.i_rate;
+ assert (sys->trigger == e);
-#define ADJUST_FACTOR 4
-#define ADJUST_MAX 1000 /* Hz (max rate variation per call) */
- /* This is empirical. Feel free to define something smarter. */
- int adj = sys->rate * (delta + change) / (CLOCK_FREQ * ADJUST_FACTOR);
+ msg_Dbg(aout, "starting deferred");
+ vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+ sys->trigger = NULL;
+ stream_start_now(sys->stream, aout);
+ (void) api; (void) e; (void) tv;
+}
- /* This avoids too fast rate variation. They sound ugly as hell and they
- * make the algorithm unstable (e.g. oscillation around inrate). */
- if (adj > +ADJUST_MAX)
- adj = +ADJUST_MAX;
- if (adj < -ADJUST_MAX)
- adj = -ADJUST_MAX;
+/**
+ * Starts or resumes the playback stream.
+ * Tries start playing back audio samples at the most accurate time
+ * in order to minimize desync and resampling during early playback.
+ * @note PulseAudio lock required.
+ */
+static void stream_start(pa_stream *s, audio_output_t *aout)
+{
+ aout_sys_t *sys = aout->sys;
+ mtime_t delta;
- if (abs(adj) < (inrate >> 10))
- adj = 0; /* favor native rate to avoid resampling */
+ assert (sys->first_pts != VLC_TS_INVALID);
- /* This keeps the effective rate within specified range
- * (+/-AOUT_MAX_RESAMPLING% - see <vlc_aout.h>) of the nominal rate. */
- unsigned outrate = inrate - adj;
- const int limit = inrate * AOUT_MAX_RESAMPLING / 100;
+ if (sys->trigger != NULL) {
+ vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+ sys->trigger = NULL;
+ }
- if (outrate > inrate + limit)
- outrate = inrate + limit;
- if (outrate < inrate - limit)
- outrate = inrate - limit;
+ delta = vlc_pa_get_latency(aout, sys->context, s);
+ if (unlikely(delta == VLC_TS_INVALID)) {
+ msg_Dbg(aout, "cannot synchronize start");
+ delta = 0; /* screwed */
+ }
- /* Apply adjusted sample rate */
- if (outrate == sys->rate)
- return;
- pa_operation *op = pa_stream_update_sample_rate(s, outrate, NULL, NULL);
- if (unlikely(op == NULL)) {
- vlc_pa_error(aout, "cannot change sample rate", sys->context);
- return;
+ delta = (sys->first_pts - mdate()) - delta;
+ if (delta > 0) {
+ msg_Dbg(aout, "deferring start (%"PRId64" us)", delta);
+ delta += pa_rtclock_now();
+ sys->trigger = pa_context_rttime_new(sys->context, delta,
+ stream_trigger_cb, aout);
+ } else {
+ msg_Warn(aout, "starting late (%"PRId64" us)", delta);
+ stream_start_now(s, aout);
}
- pa_operation_unref(op);
- msg_Dbg(aout, "changed sample rate to %u Hz",outrate);
- sys->rate = outrate;
}
-static void stream_moved_cb(pa_stream *s, void *userdata)
+static void stream_latency_cb(pa_stream *s, void *userdata)
{
- aout_instance_t *aout = userdata;
- aout_sys_t *sys = aout->output.p_sys;
- pa_operation *op;
- uint32_t idx = pa_stream_get_device_index(s);
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
+
+ /* This callback is _never_ called while paused. */
+ if (sys->first_pts == VLC_TS_INVALID)
+ return; /* nothing to do if buffers are (still) empty */
+ if (pa_stream_is_corked(s) > 0)
+ stream_start(s, aout);
+}
- msg_Dbg(aout, "connected to sink %"PRIu32": %s", idx,
- pa_stream_get_device_name(s));
- op = pa_context_get_sink_info_by_index(sys->context, idx,
- sink_info_cb, aout);
- if (likely(op != NULL))
- pa_operation_unref(op);
- /* Update the variable if someone else moved our stream */
- var_Change(aout, "audio-device", VLC_VAR_SETVALUE,
- &(vlc_value_t){ .i_int = idx }, NULL);
+/*** Stream helpers ***/
+static void stream_state_cb(pa_stream *s, void *userdata)
+{
+ pa_threaded_mainloop *mainloop = userdata;
+
+ switch (pa_stream_get_state(s)) {
+ case PA_STREAM_READY:
+ case PA_STREAM_FAILED:
+ case PA_STREAM_TERMINATED:
+ pa_threaded_mainloop_signal(mainloop, 0);
+ default:
+ break;
+ }
}
-static void stream_overflow_cb(pa_stream *s, void *userdata)
+static void stream_buffer_attr_cb(pa_stream *s, void *userdata)
{
- aout_instance_t *aout = userdata;
+ audio_output_t *aout = userdata;
+ const pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
- msg_Err(aout, "overflow");
+ msg_Dbg(aout, "changed buffer metrics: maxlength=%u, tlength=%u, "
+ "prebuf=%u, minreq=%u",
+ pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
+}
+
+static void stream_event_cb(pa_stream *s, const char *name, pa_proplist *pl,
+ void *userdata)
+{
+ audio_output_t *aout = userdata;
+
+ if (!strcmp(name, PA_STREAM_EVENT_REQUEST_CORK))
+ aout_PolicyReport(aout, true);
+ else
+ if (!strcmp(name, PA_STREAM_EVENT_REQUEST_UNCORK))
+ aout_PolicyReport(aout, false);
+ else
+ /* FIXME: expose aout_Restart() directly */
+ if (!strcmp(name, PA_STREAM_EVENT_FORMAT_LOST)) {
+ msg_Dbg (aout, "format lost");
+ aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
+ } else
+ msg_Warn (aout, "unhandled stream event \"%s\"", name);
(void) s;
+ (void) pl;
+}
+
+static void stream_moved_cb(pa_stream *s, void *userdata)
+{
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
+ const char *name = pa_stream_get_device_name(s);
+ struct sink *sink = sink_find(sys, pa_stream_get_device_index(s));
+
+ msg_Dbg(aout, "connected to sink %s", name);
+ aout_DeviceReport(aout, name);
+
+ sys->base_volume = likely(sink != NULL) ? sink->base_volume
+ : PA_VOLUME_INVALID;
+ msg_Dbg(aout, "base volume: %"PRIu32, sys->base_volume);
+ if (pa_cvolume_valid(&sys->cvolume))
+ VolumeReport(aout);
+}
+
+static void stream_overflow_cb(pa_stream *s, void *userdata)
+{
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
+ pa_operation *op;
+
+ msg_Err(aout, "overflow, flushing");
+ op = pa_stream_flush(s, NULL, NULL);
+ if (unlikely(op == NULL))
+ return;
+ pa_operation_unref(op);
+ sys->first_pts = VLC_TS_INVALID;
}
static void stream_started_cb(pa_stream *s, void *userdata)
{
- aout_instance_t *aout = userdata;
+ audio_output_t *aout = userdata;
msg_Dbg(aout, "started");
(void) s;
static void stream_suspended_cb(pa_stream *s, void *userdata)
{
- aout_instance_t *aout = userdata;
+ audio_output_t *aout = userdata;
msg_Dbg(aout, "suspended");
- stream_reset_sync(s, aout);
+ (void) s;
}
static void stream_underflow_cb(pa_stream *s, void *userdata)
{
- aout_instance_t *aout = userdata;
- pa_operation *op;
+ audio_output_t *aout = userdata;
- msg_Warn(aout, "underflow");
- op = pa_stream_cork(s, 1, NULL, NULL);
- if (op != NULL)
- pa_operation_unref(op);
- stream_reset_sync(s, aout);
+ msg_Dbg(aout, "underflow");
+ (void) s;
}
-static int stream_wait(pa_stream *stream)
+static int stream_wait(pa_stream *stream, pa_threaded_mainloop *mainloop)
{
pa_stream_state_t state;
while ((state = pa_stream_get_state(stream)) != PA_STREAM_READY) {
if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
return -1;
- vlc_pa_wait();
+ pa_threaded_mainloop_wait(mainloop);
}
return 0;
}
-#ifdef LIBPULSE_GETS_A_CLUE
-static void stream_success_cb(pa_stream *s, int success, void *userdata)
+
+/*** Sink input ***/
+static void sink_input_info_cb(pa_context *ctx, const pa_sink_input_info *i,
+ int eol, void *userdata)
{
- vlc_pa_signal(0);
- (void) s; (void) success; (void) userdata;
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
+
+ if (eol)
+ return;
+ (void) ctx;
+
+ sys->cvolume = i->volume; /* cache volume for balance preservation */
+ if (PA_VOLUME_IS_VALID(sys->base_volume))
+ VolumeReport(aout);
+ aout_MuteReport(aout, i->mute);
+}
+
+static void sink_input_event(pa_context *ctx,
+ pa_subscription_event_type_t type,
+ uint32_t idx, audio_output_t *aout)
+{
+ pa_operation *op;
+
+ /* Gee... PA will not provide the infos directly in the event. */
+ switch (type)
+ {
+ case PA_SUBSCRIPTION_EVENT_REMOVE:
+ msg_Err(aout, "sink input killed!");
+ break;
+
+ default:
+ op = pa_context_get_sink_input_info(ctx, idx, sink_input_info_cb,
+ aout);
+ if (likely(op != NULL))
+ pa_operation_unref(op);
+ break;
+ }
+}
+
+
+/*** Context ***/
+static void context_cb(pa_context *ctx, pa_subscription_event_type_t type,
+ uint32_t idx, void *userdata)
+{
+ audio_output_t *aout = userdata;
+ aout_sys_t *sys = aout->sys;
+ unsigned facility = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
+
+ type &= PA_SUBSCRIPTION_EVENT_TYPE_MASK;
+ switch (facility)
+ {
+ case PA_SUBSCRIPTION_EVENT_SINK:
+ sink_event(ctx, type, idx, userdata);
+ break;
+
+ case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
+ /* only interested in our sink input */
+ if (sys->stream != NULL && idx == pa_stream_get_index(sys->stream))
+ sink_input_event(ctx, type, idx, userdata);
+ break;
+
+ default: /* unsubscribed facility?! */
+ assert(0);
+ }
+}
+
+
+/*** VLC audio output callbacks ***/
+
+static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
+{
+ aout_sys_t *sys = aout->sys;
+ pa_stream *s = sys->stream;
+
+ if (pa_stream_is_corked(s) > 0)
+ return -1; /* latency is irrelevant if corked */
+
+ mtime_t delta = vlc_pa_get_latency(aout, sys->context, s);
+ if (delta == VLC_TS_INVALID)
+ return -1;
+
+ *delay = delta;
+ return 0;
}
-#else
-# define stream_success_cb NULL
-#endif
/* Memory free callback. The block_t address is in front of the data. */
static void data_free(void *data)
}
/**
- * Queue one audio frame to the playabck stream
+ * Queue one audio frame to the playback stream
*/
-static void Play(aout_instance_t *aout)
+static void Play(audio_output_t *aout, block_t *block)
{
- aout_sys_t *sys = aout->output.p_sys;
+ aout_sys_t *sys = aout->sys;
pa_stream *s = sys->stream;
- /* This function is called exactly once per block in the output FIFO. */
- block_t *block = aout_FifoPop(&aout->output.fifo);
- assert (block != NULL);
-
const void *ptr = data_convert(&block);
if (unlikely(ptr == NULL))
return;
size_t len = block->i_buffer;
- mtime_t pts = block->i_pts + block->i_length;
/* Note: The core already holds the output FIFO lock at this point.
* Therefore we must not under any circumstances (try to) acquire the
* output FIFO lock while the PulseAudio threaded main loop lock is held
* (including from PulseAudio stream callbacks). Otherwise lock inversion
* will take place, and sooner or later a deadlock. */
- vlc_pa_lock();
-
- if (pa_stream_is_corked(s) > 0) {
- /* Start or resume the stream. Zeroes are prepended to sync.
- * This does not really work because PulseAudio latency measurement is
- * garbage at start. */
- pa_operation *op;
- pa_usec_t latency;
- int negative;
-
- if (pa_stream_get_latency(s, &latency, &negative) == 0)
- msg_Dbg(aout, "starting with %c%"PRIu64" us latency",
- negative ? '-' : '+', latency);
- else
- latency = negative = 0;
-
- mtime_t advance = block->i_pts - mdate();
- if (negative)
- advance += latency;
- else
- advance -= latency;
+ pa_threaded_mainloop_lock(sys->mainloop);
- if (advance > 0) {
- size_t nb = (advance * aout->output.output.i_rate) / CLOCK_FREQ;
- size_t size = aout->output.output.i_bytes_per_frame;
- float *zeroes = calloc (nb, size);
+ if (sys->first_pts == VLC_TS_INVALID)
+ sys->first_pts = block->i_pts;
- msg_Dbg(aout, "prepending %zu zeroes", nb);
-#if 0 /* Fault injector: add delay */
- pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
- pa_stream_write(s, zeroes, nb * size, NULL, 0, PA_SEEK_RELATIVE);
-#endif
- if (likely(zeroes != NULL))
- if (pa_stream_write(s, zeroes, nb * size, free, 0,
- PA_SEEK_RELATIVE) < 0)
- free(zeroes);
- }
-
- op = pa_stream_cork(s, 0, NULL, NULL);
- if (op != NULL)
- pa_operation_unref(op);
- op = pa_stream_trigger(s, NULL, NULL);
- if (op != NULL)
- pa_operation_unref(op);
- msg_Dbg(aout, "uncorking");
- }
+ if (pa_stream_is_corked(s) > 0)
+ stream_start(s, aout);
#if 0 /* Fault injector to test underrun recovery */
- static unsigned u = 0;
- if ((++u % 500) == 0) {
+ static volatile unsigned u = 0;
+ if ((++u % 1000) == 0) {
msg_Err(aout, "fault injection");
pa_operation_unref(pa_stream_flush(s, NULL, NULL));
}
vlc_pa_error(aout, "cannot write", sys->context);
block_Release(block);
}
- sys->pts = pts;
- vlc_pa_unlock();
+ pa_threaded_mainloop_unlock(sys->mainloop);
}
/**
* Cork or uncork the playback stream
*/
-static void Pause(aout_instance_t *aout, bool b_paused, mtime_t i_date)
+static void Pause(audio_output_t *aout, bool paused, mtime_t date)
{
- aout_sys_t *sys = aout->output.p_sys;
+ aout_sys_t *sys = aout->sys;
pa_stream *s = sys->stream;
- if (!b_paused)
- return; /* nothing to do - yet */
+ pa_threaded_mainloop_lock(sys->mainloop);
- vlc_pa_lock();
+ if (paused) {
+ pa_stream_set_latency_update_callback(s, NULL, NULL);
+ stream_stop(s, aout);
+ } else {
+ pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
+ if (likely(sys->first_pts != VLC_TS_INVALID))
+ stream_start_now(s, aout);
+ }
- pa_operation *op = pa_stream_cork(s, 1, NULL, NULL);
+ pa_threaded_mainloop_unlock(sys->mainloop);
+ (void) date;
+}
+
+/**
+ * Flush or drain the playback stream
+ */
+static void Flush(audio_output_t *aout, bool wait)
+{
+ aout_sys_t *sys = aout->sys;
+ pa_stream *s = sys->stream;
+ pa_operation *op;
+
+ pa_threaded_mainloop_lock(sys->mainloop);
+
+ if (wait)
+ op = pa_stream_drain(s, NULL, NULL);
+ /* TODO: wait for drain completion*/
+ else
+ op = pa_stream_flush(s, NULL, NULL);
if (op != NULL)
pa_operation_unref(op);
- stream_reset_sync(s, aout);
-
- vlc_pa_unlock();
- (void) i_date;
+ pa_threaded_mainloop_unlock(sys->mainloop);
}
-static int VolumeSet(aout_instance_t *aout, float vol, bool mute)
+static int VolumeSet(audio_output_t *aout, float vol)
{
- aout_sys_t *sys = aout->output.p_sys;
+ aout_sys_t *sys = aout->sys;
+ pa_stream *s = sys->stream;
pa_operation *op;
+ int ret = -1;
+ pa_volume_t base_volume;
+
+ /* VLC provides the software volume so convert directly to PulseAudio
+ * software volume, pa_volume_t. This is not a linear amplification factor
+ * so do not use PulseAudio linear amplification! */
+ vol *= PA_VOLUME_NORM;
+ if (unlikely(vol >= PA_VOLUME_MAX))
+ vol = PA_VOLUME_MAX;
+
+ pa_threaded_mainloop_lock(sys->mainloop);
+
+ base_volume = sys->base_volume;
+ if (!PA_VOLUME_IS_VALID(base_volume))
+ { /* Base volume is unknown, typically because sink is unknown.
+ * Try to guess the base volume. */
+ const struct sink *sink = sys->sinks;
+ if (unlikely(sink == NULL))
+ {
+ msg_Err(aout, "cannot change volume without sink");
+ goto out;
+ }
- uint32_t idx = pa_stream_get_index(sys->stream);
- pa_volume_t volume = pa_sw_volume_from_linear(vol);
- pa_cvolume cvolume;
+ base_volume = sink->base_volume;
+ while ((sink = sink->next) != NULL)
+ if (sink->base_volume != base_volume)
+ {
+ msg_Err(aout, "cannot change volume without base");
+ goto out;
+ }
+ }
+
+ pa_volume_t volume = pa_sw_volume_multiply(lroundf(vol), base_volume);
- /* TODO: do not ruin the channel balance (if set outside VLC) */
- /* TODO: notify UI about volume changes by other PulseAudio clients */
- pa_cvolume_set(&sys->cvolume, sys->cvolume.channels, volume);
- pa_sw_cvolume_multiply_scalar(&cvolume, &sys->cvolume, sys->base_volume);
+ if (s == NULL)
+ {
+ sys->volume_force = volume;
+ aout_VolumeReport(aout, vol / (float)PA_VOLUME_NORM);
+ ret = 0;
+ goto out;
+ }
+
+ if (!pa_cvolume_valid(&sys->cvolume))
+ {
+ const pa_sample_spec *ss = pa_stream_get_sample_spec(s);
+
+ msg_Warn(aout, "balance clobbered by volume change");
+ pa_cvolume_set(&sys->cvolume, ss->channels, PA_VOLUME_NORM);
+ }
+
+ /* Preserve the balance (VLC does not support it). */
+ pa_cvolume cvolume = sys->cvolume;
+ pa_cvolume_scale(&cvolume, PA_VOLUME_NORM);
+ pa_sw_cvolume_multiply_scalar(&cvolume, &cvolume, volume);
assert(pa_cvolume_valid(&cvolume));
- vlc_pa_lock();
- op = pa_context_set_sink_input_volume(sys->context, idx, &cvolume, NULL, NULL);
+ op = pa_context_set_sink_input_volume(sys->context, pa_stream_get_index(s),
+ &cvolume, NULL, NULL);
if (likely(op != NULL))
+ {
pa_operation_unref(op);
+ ret = 0;
+ }
+out:
+ pa_threaded_mainloop_unlock(sys->mainloop);
+ return ret;
+}
+
+static int MuteSet(audio_output_t *aout, bool mute)
+{
+ aout_sys_t *sys = aout->sys;
+
+ if (sys->stream == NULL)
+ {
+ sys->flags_force &= ~(PA_STREAM_START_MUTED|PA_STREAM_START_UNMUTED);
+ sys->flags_force |=
+ mute ? PA_STREAM_START_MUTED : PA_STREAM_START_UNMUTED;
+ aout_MuteReport(aout, mute);
+ return 0;
+ }
+
+ pa_operation *op;
+ uint32_t idx = pa_stream_get_index(sys->stream);
+ pa_threaded_mainloop_lock(sys->mainloop);
op = pa_context_set_sink_input_mute(sys->context, idx, mute, NULL, NULL);
if (likely(op != NULL))
pa_operation_unref(op);
- vlc_pa_unlock();
+ pa_threaded_mainloop_unlock(sys->mainloop);
return 0;
}
-static int StreamMove(vlc_object_t *obj, const char *varname, vlc_value_t old,
- vlc_value_t val, void *userdata)
+static int StreamMove(audio_output_t *aout, const char *name)
{
- aout_instance_t *aout = (aout_instance_t *)obj;
- aout_sys_t *sys = aout->output.p_sys;
- pa_stream *s = userdata;
- pa_operation *op;
- uint32_t idx = pa_stream_get_index(s);
- uint32_t sink_idx = val.i_int;
+ aout_sys_t *sys = aout->sys;
- (void) varname; (void) old;
+ if (sys->stream == NULL)
+ {
+ struct sink *sink = sink_find_by_name(sys, name);
+
+ sys->base_volume = likely(sink != NULL) ? sink->base_volume
+ : PA_VOLUME_INVALID;
+ msg_Dbg(aout, "will connect to sink %s", name);
+ free(sys->sink_force);
+ sys->sink_force = strdup(name);
+ return 0;
+ }
- vlc_pa_lock();
- op = pa_context_move_sink_input_by_index(sys->context, idx, sink_idx,
- NULL, NULL);
+ pa_operation *op;
+ uint32_t idx = pa_stream_get_index(sys->stream);
+
+ pa_threaded_mainloop_lock(sys->mainloop);
+ op = pa_context_move_sink_input_by_name(sys->context, idx, name,
+ NULL, NULL);
if (likely(op != NULL)) {
pa_operation_unref(op);
- msg_Dbg(aout, "moving to sink %"PRIu32, sink_idx);
+ msg_Dbg(aout, "moving to sink %s", name);
} else
- vlc_pa_error(obj, "cannot move sink", sys->context);
- vlc_pa_unlock();
+ vlc_pa_error(aout, "cannot move sink input", sys->context);
+ pa_threaded_mainloop_unlock(sys->mainloop);
- return (op != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
+ return (op != NULL) ? 0 : -1;
}
+static void Stop(audio_output_t *);
/**
* Create a PulseAudio playback stream, a.k.a. a sink input.
*/
-static int Open(vlc_object_t *obj)
+static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
{
- aout_instance_t *aout = (aout_instance_t *)obj;
- pa_operation *op;
+ aout_sys_t *sys = aout->sys;
/* Sample format specification */
struct pa_sample_spec ss;
+ pa_encoding_t encoding = PA_ENCODING_INVALID;
- switch(aout->output.output.i_format)
+ switch (fmt->i_format)
{
- case VLC_CODEC_F64B:
- aout->output.output.i_format = VLC_CODEC_F32B;
- case VLC_CODEC_F32B:
- ss.format = PA_SAMPLE_FLOAT32BE;
- break;
- case VLC_CODEC_F64L:
- aout->output.output.i_format = VLC_CODEC_F32L;
- case VLC_CODEC_F32L:
- ss.format = PA_SAMPLE_FLOAT32LE;
- break;
- case VLC_CODEC_FI32:
- aout->output.output.i_format = VLC_CODEC_FL32;
+ case VLC_CODEC_FL64:
+ fmt->i_format = VLC_CODEC_FL32;
+ case VLC_CODEC_FL32:
ss.format = PA_SAMPLE_FLOAT32NE;
break;
- case VLC_CODEC_S32B:
- ss.format = PA_SAMPLE_S32BE;
+ case VLC_CODEC_S32N:
+ ss.format = PA_SAMPLE_S32NE;
break;
- case VLC_CODEC_S32L:
- ss.format = PA_SAMPLE_S32LE;
+ case VLC_CODEC_S16N:
+ ss.format = PA_SAMPLE_S16NE;
break;
- case VLC_CODEC_S24B:
- ss.format = PA_SAMPLE_S24BE;
+ case VLC_CODEC_U8:
+ ss.format = PA_SAMPLE_U8;
break;
- case VLC_CODEC_S24L:
- ss.format = PA_SAMPLE_S24LE;
+ case VLC_CODEC_A52:
+ fmt->i_format = VLC_CODEC_SPDIFL;
+ encoding = PA_ENCODING_AC3_IEC61937;
+ ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
break;
- case VLC_CODEC_S16B:
- ss.format = PA_SAMPLE_S16BE;
+ /*case VLC_CODEC_EAC3:
+ fmt->i_format = VLC_CODEC_SPDIFL FIXME;
+ encoding = PA_ENCODING_EAC3_IEC61937;
+ ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
break;
- case VLC_CODEC_S16L:
- ss.format = PA_SAMPLE_S16LE;
- break;
- case VLC_CODEC_S8:
- aout->output.output.i_format = VLC_CODEC_U8;
- case VLC_CODEC_U8:
- ss.format = PA_SAMPLE_U8;
+ case VLC_CODEC_MPGA:
+ fmt->i_format = VLC_CODEC_SPDIFL FIXME;
+ encoding = PA_ENCODING_MPEG_IEC61937;
+ ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
+ break;*/
+ case VLC_CODEC_DTS:
+ fmt->i_format = VLC_CODEC_SPDIFL;
+ encoding = PA_ENCODING_DTS_IEC61937;
+ ss.format = HAVE_FPU ? PA_SAMPLE_FLOAT32NE : PA_SAMPLE_S16NE;
break;
default:
if (HAVE_FPU)
{
- aout->output.output.i_format = VLC_CODEC_FL32;
+ fmt->i_format = VLC_CODEC_FL32;
ss.format = PA_SAMPLE_FLOAT32NE;
}
else
{
- aout->output.output.i_format = VLC_CODEC_S16N;
+ fmt->i_format = VLC_CODEC_S16N;
ss.format = PA_SAMPLE_S16NE;
}
break;
}
- ss.rate = aout->output.output.i_rate;
- ss.channels = aout_FormatNbChannels(&aout->output.output);
+ ss.rate = fmt->i_rate;
+ ss.channels = aout_FormatNbChannels(fmt);
if (!pa_sample_spec_valid(&ss)) {
msg_Err(aout, "unsupported sample specification");
return VLC_EGENERIC;
struct pa_channel_map map;
map.channels = 0;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_LEFT)
+ if (fmt->i_physical_channels & AOUT_CHAN_LEFT)
map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_RIGHT)
+ if (fmt->i_physical_channels & AOUT_CHAN_RIGHT)
map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLELEFT)
+ if (fmt->i_physical_channels & AOUT_CHAN_MIDDLELEFT)
map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
+ if (fmt->i_physical_channels & AOUT_CHAN_MIDDLERIGHT)
map.map[map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_REARLEFT)
+ if (fmt->i_physical_channels & AOUT_CHAN_REARLEFT)
map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_REARRIGHT)
+ if (fmt->i_physical_channels & AOUT_CHAN_REARRIGHT)
map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_REARCENTER)
+ if (fmt->i_physical_channels & AOUT_CHAN_REARCENTER)
map.map[map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
- if (aout->output.output.i_physical_channels & AOUT_CHAN_CENTER)
+ if (fmt->i_physical_channels & AOUT_CHAN_CENTER)
{
if (ss.channels == 1)
map.map[map.channels++] = PA_CHANNEL_POSITION_MONO;
else
map.map[map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
}
- if (aout->output.output.i_physical_channels & AOUT_CHAN_LFE)
+ if (fmt->i_physical_channels & AOUT_CHAN_LFE)
map.map[map.channels++] = PA_CHANNEL_POSITION_LFE;
for (unsigned i = 0; map.channels < ss.channels; i++) {
}
/* Stream parameters */
- const pa_stream_flags_t flags = PA_STREAM_START_CORKED
- //| PA_STREAM_INTERPOLATE_TIMING
- | PA_STREAM_AUTO_TIMING_UPDATE
- | PA_STREAM_VARIABLE_RATE;
+ pa_stream_flags_t flags = sys->flags_force
+ | PA_STREAM_START_CORKED
+ | PA_STREAM_INTERPOLATE_TIMING
+ | PA_STREAM_NOT_MONOTONIC
+ | PA_STREAM_AUTO_TIMING_UPDATE
+ | PA_STREAM_FIX_RATE;
struct pa_buffer_attr attr;
attr.maxlength = -1;
- /* PulseAudio assumes that tlength bytes are available in the buffer. Thus
- * we need to be conservative and set the minimum value that the VLC
- * audio decoder thread warrants. Otherwise, PulseAudio buffers will
- * underrun on hardware with large buffers. VLC keeps at least
- * AOUT_MIN_PREPARE and at most AOUT_MAX_PREPARE worth of audio buffers.
- * TODO? tlength could be adaptively increased to reduce wakeups. */
- attr.tlength = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
+ /* PulseAudio goes berserk if the target length (tlength) is not
+ * significantly longer than 2 periods (minreq), or when the period length
+ * is unspecified and the target length is short. */
+ attr.tlength = pa_usec_to_bytes(3 * AOUT_MIN_PREPARE_TIME, &ss);
attr.prebuf = 0; /* trigger manually */
- attr.minreq = -1;
+ attr.minreq = pa_usec_to_bytes(AOUT_MIN_PREPARE_TIME, &ss);
attr.fragsize = 0; /* not used for output */
- /* Allocate structures */
- aout_sys_t *sys = malloc(sizeof(*sys));
- if (unlikely(sys == NULL))
- return VLC_ENOMEM;
-
- pa_context *ctx = vlc_pa_connect (obj);
- if (ctx == NULL)
+ pa_cvolume *cvolume = NULL, cvolumebuf;
+ if (PA_VOLUME_IS_VALID(sys->volume_force))
{
- free (sys);
- return VLC_EGENERIC;
+ cvolume = &cvolumebuf;
+ pa_cvolume_set(cvolume, ss.channels, sys->volume_force);
}
- aout->output.p_sys = sys;
- sys->stream = NULL;
- sys->context = ctx;
- sys->pts = VLC_TS_INVALID;
- sys->desync = 0;
- sys->rate = ss.rate;
+ sys->trigger = NULL;
+ pa_cvolume_init(&sys->cvolume);
+ sys->first_pts = VLC_TS_INVALID;
+
+ pa_format_info *formatv[2];
+ unsigned formatc = 0;
+
+ /* Favor digital pass-through if available*/
+ if (encoding != PA_ENCODING_INVALID) {
+ formatv[formatc] = pa_format_info_new();
+ formatv[formatc]->encoding = encoding;
+ pa_format_info_set_rate(formatv[formatc], ss.rate);
+ pa_format_info_set_channels(formatv[formatc], ss.channels);
+ pa_format_info_set_channel_map(formatv[formatc], &map);
+ formatc++;
+
+ /* FIX flags are only permitted for PCM, and there is no way to pass
+ * different flags for different formats... */
+ flags &= ~(PA_STREAM_FIX_FORMAT
+ | PA_STREAM_FIX_RATE
+ | PA_STREAM_FIX_CHANNELS);
+ }
- /* Channel volume */
- sys->base_volume = PA_VOLUME_NORM;
- pa_cvolume_set(&sys->cvolume, ss.channels, PA_VOLUME_NORM);
+ /* Fallback to PCM */
+ formatv[formatc] = pa_format_info_new();
+ formatv[formatc]->encoding = PA_ENCODING_PCM;
+ pa_format_info_set_sample_format(formatv[formatc], ss.format);
+ pa_format_info_set_rate(formatv[formatc], ss.rate);
+ pa_format_info_set_channels(formatv[formatc], ss.channels);
+ pa_format_info_set_channel_map(formatv[formatc], &map);
+ formatc++;
- vlc_pa_lock();
/* Create a playback stream */
- pa_stream *s = pa_stream_new(ctx, "audio stream", &ss, &map);
+ pa_proplist *props = pa_proplist_new();
+ if (likely(props != NULL))
+ /* TODO: set other stream properties */
+ pa_proplist_sets (props, PA_PROP_MEDIA_ROLE, "video");
+
+ pa_threaded_mainloop_lock(sys->mainloop);
+ pa_stream *s = pa_stream_new_extended(sys->context, "audio stream",
+ formatv, formatc, props);
+
+ if (likely(props != NULL))
+ pa_proplist_free(props);
+ for (unsigned i = 0; i < formatc; i++)
+ pa_format_info_free(formatv[i]);
+
if (s == NULL) {
- vlc_pa_error(obj, "stream creation failure", ctx);
- goto fail;
+ pa_threaded_mainloop_unlock(sys->mainloop);
+ vlc_pa_error(aout, "stream creation failure", sys->context);
+ return VLC_EGENERIC;
}
+ assert(sys->stream == NULL);
sys->stream = s;
- pa_stream_set_state_callback(s, stream_state_cb, NULL);
+ pa_stream_set_state_callback(s, stream_state_cb, sys->mainloop);
+ pa_stream_set_buffer_attr_callback(s, stream_buffer_attr_cb, aout);
+ pa_stream_set_event_callback(s, stream_event_cb, aout);
pa_stream_set_latency_update_callback(s, stream_latency_cb, aout);
pa_stream_set_moved_callback(s, stream_moved_cb, aout);
pa_stream_set_overflow_callback(s, stream_overflow_cb, aout);
pa_stream_set_suspended_callback(s, stream_suspended_cb, aout);
pa_stream_set_underflow_callback(s, stream_underflow_cb, aout);
- if (pa_stream_connect_playback(s, NULL, &attr, flags, NULL, NULL) < 0
- || stream_wait(s)) {
- vlc_pa_error(obj, "stream connection failure", ctx);
+ if (pa_stream_connect_playback(s, sys->sink_force, &attr, flags,
+ cvolume, NULL) < 0
+ || stream_wait(s, sys->mainloop)) {
+ vlc_pa_error(aout, "stream connection failure", sys->context);
goto fail;
}
+ sys->volume_force = PA_VOLUME_INVALID;
+ sys->flags_force = PA_STREAM_NOFLAGS;
+ free(sys->sink_force);
+ sys->sink_force = NULL;
+
+ const struct pa_sample_spec *spec = pa_stream_get_sample_spec(s);
+ if (encoding != PA_ENCODING_INVALID) {
+ const pa_format_info *info = pa_stream_get_format_info(s);
+
+ assert (info != NULL);
+ if (pa_format_info_is_pcm (info)) {
+ msg_Dbg(aout, "digital pass-through not available");
+ fmt->i_format = HAVE_FPU ? VLC_CODEC_FL32 : VLC_CODEC_S16N;
+ } else {
+ msg_Dbg(aout, "digital pass-through enabled");
+ spec = NULL;
+ }
+ }
+ if (spec != NULL)
+ fmt->i_rate = spec->rate;
- const struct pa_buffer_attr *pba = pa_stream_get_buffer_attr(s);
- msg_Dbg(aout, "using buffer metrics: maxlength=%u, tlength=%u, "
- "prebuf=%u, minreq=%u",
- pba->maxlength, pba->tlength, pba->prebuf, pba->minreq);
-
- aout->output.i_nb_samples = pba->minreq / pa_frame_size(&ss);
-
- var_Create(aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
- var_Change(aout, "audio-device", VLC_VAR_SETTEXT,
- &(vlc_value_t){ .psz_string = (char *)_("Audio device") },
- NULL);
- var_AddCallback (aout, "audio-device", StreamMove, s);
- op = pa_context_get_sink_info_list(ctx, sink_list_cb, aout);
- /* We may need to wait for completion... once LibVLC supports this */
- if (op != NULL)
- pa_operation_unref(op);
+ stream_buffer_attr_cb(s, aout);
stream_moved_cb(s, aout);
- vlc_pa_unlock();
+ pa_threaded_mainloop_unlock(sys->mainloop);
- aout->output.pf_play = Play;
- aout->output.pf_pause = Pause;
- aout->output.pf_volume_set = VolumeSet;
return VLC_SUCCESS;
fail:
- vlc_pa_unlock();
- Close(obj);
+ pa_threaded_mainloop_unlock(sys->mainloop);
+ Stop(aout);
return VLC_EGENERIC;
}
/**
* Removes a PulseAudio playback stream
*/
-static void Close (vlc_object_t *obj)
+static void Stop(audio_output_t *aout)
{
- aout_instance_t *aout = (aout_instance_t *)obj;
- aout_sys_t *sys = aout->output.p_sys;
- pa_context *ctx = sys->context;
+ aout_sys_t *sys = aout->sys;
pa_stream *s = sys->stream;
- if (s != NULL) {
- /* The callback takes mainloop lock, so it CANNOT be held here! */
- var_DelCallback (aout, "audio-device", StreamMove, s);
- var_Destroy (aout, "audio-device");
- }
+ pa_threaded_mainloop_lock(sys->mainloop);
+ if (unlikely(sys->trigger != NULL))
+ vlc_pa_rttime_free(sys->mainloop, sys->trigger);
+ pa_stream_disconnect(s);
+
+ /* Clear all callbacks */
+ pa_stream_set_state_callback(s, NULL, NULL);
+ pa_stream_set_buffer_attr_callback(s, NULL, NULL);
+ pa_stream_set_event_callback(s, NULL, NULL);
+ pa_stream_set_latency_update_callback(s, NULL, NULL);
+ pa_stream_set_moved_callback(s, NULL, NULL);
+ pa_stream_set_overflow_callback(s, NULL, NULL);
+ pa_stream_set_started_callback(s, NULL, NULL);
+ pa_stream_set_suspended_callback(s, NULL, NULL);
+ pa_stream_set_underflow_callback(s, NULL, NULL);
+
+ pa_stream_unref(s);
+ sys->stream = NULL;
+ sys->base_volume = PA_VOLUME_INVALID;
+ pa_threaded_mainloop_unlock(sys->mainloop);
+}
- vlc_pa_lock();
- if (s != NULL) {
- pa_operation *op;
+static int Open(vlc_object_t *obj)
+{
+ audio_output_t *aout = (audio_output_t *)obj;
+ aout_sys_t *sys = malloc(sizeof (*sys));
+ pa_operation *op;
- if (pa_stream_is_corked(s) > 0)
- /* Stream paused: discard all buffers */
- op = pa_stream_flush(s, stream_success_cb, NULL);
- else
- /* Stream playing: wait until buffers are played */
- op = pa_stream_drain(s, stream_success_cb, NULL);
- if (likely(op != NULL)) {
-#ifdef LIBPULSE_GETS_A_CLUE
- while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
- vlc_pa_wait();
-#endif
- pa_operation_unref(op);
- }
+ if (unlikely(sys == NULL))
+ return VLC_ENOMEM;
- pa_stream_disconnect(s);
- pa_stream_unref(s);
+ /* Allocate structures */
+ pa_context *ctx = vlc_pa_connect(obj, &sys->mainloop);
+ if (ctx == NULL)
+ {
+ free(sys);
+ return VLC_EGENERIC;
}
- vlc_pa_unlock();
+ sys->stream = NULL;
+ sys->context = ctx;
+ sys->base_volume = PA_VOLUME_INVALID;
+ sys->volume_force = PA_VOLUME_INVALID;
+ sys->flags_force = PA_STREAM_NOFLAGS;
+ sys->sink_force = NULL;
+ sys->sinks = NULL;
+
+ aout->sys = sys;
+ aout->start = Start;
+ aout->stop = Stop;
+ aout->time_get = TimeGet;
+ aout->play = Play;
+ aout->pause = Pause;
+ aout->flush = Flush;
+ aout->volume_set = VolumeSet;
+ aout->mute_set = MuteSet;
+ aout->device_select = StreamMove;
+
+ pa_threaded_mainloop_lock(sys->mainloop);
+ /* Sinks (output devices) list */
+ op = pa_context_get_sink_info_list(sys->context, sink_add_cb, aout);
+ if (likely(op != NULL))
+ {
+ while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
+ pa_threaded_mainloop_wait(sys->mainloop);
+ pa_operation_unref(op);
+ }
+
+ /* Context events */
+ const pa_subscription_mask_t mask = PA_SUBSCRIPTION_MASK_SINK
+ | PA_SUBSCRIPTION_MASK_SINK_INPUT;
+ pa_context_set_subscribe_callback(sys->context, context_cb, aout);
+ op = pa_context_subscribe(sys->context, mask, NULL, NULL);
+ if (likely(op != NULL))
+ pa_operation_unref(op);
+ pa_threaded_mainloop_unlock(sys->mainloop);
- vlc_pa_disconnect(obj, ctx);
+ return VLC_SUCCESS;
+}
+
+static void Close(vlc_object_t *obj)
+{
+ audio_output_t *aout = (audio_output_t *)obj;
+ aout_sys_t *sys = aout->sys;
+ pa_context *ctx = sys->context;
+
+ pa_threaded_mainloop_lock(sys->mainloop);
+ pa_context_set_subscribe_callback(sys->context, NULL, NULL);
+ pa_threaded_mainloop_unlock(sys->mainloop);
+ vlc_pa_disconnect(obj, ctx, sys->mainloop);
+
+ for (struct sink *sink = sys->sinks, *next; sink != NULL; sink = next)
+ {
+ next = sink->next;
+ free(sink);
+ }
+ free(sys->sink_force);
free(sys);
}