[pulseaudio-discuss] [PATCH 1/2] allow-passthrough: Add module to allow passthrough streams always go through
Sjoerd Simons
sjoerd.simons at collabora.co.uk
Mon May 19 06:58:18 PDT 2014
From: Guillaume Desmottes <guillaume.desmottes at collabora.co.uk>
For various use-cases a passthrough stream should have priority over all
other streams and get exclusive access to the sink regardless of whether
any other streams are playing.
An example use-case is ensuring XBMC can successfully start video
playback (with passthrough) even if an external notification sound
happened to be playing at the same time.
---
src/Makefile.am | 12 +-
src/modules/module-allow-passthrough.c | 270 +++++++++++++++++++++++++++++++++
2 files changed, 280 insertions(+), 2 deletions(-)
create mode 100644 src/modules/module-allow-passthrough.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 5c2d5bc..4b51d6f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1119,7 +1119,8 @@ modlibexec_LTLIBRARIES += \
module-switch-on-port-available.la \
module-filter-apply.la \
module-filter-heuristics.la \
- module-role-ducking.la
+ module-role-ducking.la \
+ module-allow-passthrough.la
if HAVE_ESOUND
modlibexec_LTLIBRARIES += \
@@ -1455,7 +1456,8 @@ SYMDEF_FILES = \
module-switch-on-connect-symdef.h \
module-switch-on-port-available-symdef.h \
module-filter-apply-symdef.h \
- module-filter-heuristics-symdef.h
+ module-filter-heuristics-symdef.h \
+ module-allow-passthrough-symdef.h
if HAVE_ESOUND
SYMDEF_FILES += \
@@ -2101,6 +2103,12 @@ module_rygel_media_server_la_LDFLAGS = $(MODULE_LDFLAGS)
module_rygel_media_server_la_LIBADD = $(MODULE_LIBADD) $(DBUS_LIBS) libprotocol-http.la
module_rygel_media_server_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)
+# Allow passthrough module
+module_allow_passthrough_la_SOURCES = modules/module-allow-passthrough.c
+module_allow_passthrough_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_allow_passthrough_la_LIBADD = $(MODULE_LIBADD)
+module_allow_passthrough_la_CFLAGS = $(AM_CFLAGS)
+
###################################
# Some minor stuff #
###################################
diff --git a/src/modules/module-allow-passthrough.c b/src/modules/module-allow-passthrough.c
new file mode 100644
index 0000000..310f5af
--- /dev/null
+++ b/src/modules/module-allow-passthrough.c
@@ -0,0 +1,270 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright (C) 2014 Collabora Ltd. <http://www.collabora.co.uk/>
+
+ PulseAudio 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.
+
+ PulseAudio 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.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/i18n.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/namereg.h>
+#include <pulsecore/core-util.h>
+
+#include "module-allow-passthrough-symdef.h"
+
+PA_MODULE_AUTHOR("Guillaume Desmottes");
+PA_MODULE_DESCRIPTION("When a passthrough stream is requested, route all the other streams to a dummy device");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(true);
+
+static const char* const valid_modargs[] = {
+ NULL,
+};
+
+struct userdata {
+ /* (pa_sink *) -> (pa_sink *)
+ * Map the 'real' muted sink to the null-sink currently being used to play
+ * its streams. */
+ pa_hashmap *null_sinks;
+
+ pa_hook_slot
+ *sink_input_new_slot,
+ *sink_input_unlink_slot;
+};
+
+static pa_sink *ensure_null_sink_for_sink(struct userdata *u, pa_sink *s, pa_core *c) {
+ char *t;
+ pa_module *m;
+ pa_sink *sink;
+ uint32_t idx;
+
+ sink = pa_hashmap_get(u->null_sinks, s);
+ if (sink != NULL)
+ /* We already have a null-sink for this sink */
+ return sink;
+
+ t = pa_sprintf_malloc("sink_name=allow_passthrough_null sink_properties='device.description=\"%s\"'",
+ _("Dummy Output"));
+ m = pa_module_load(c, "module-null-sink", t);
+ pa_xfree(t);
+
+ if (m == NULL)
+ return NULL;
+
+ PA_IDXSET_FOREACH(sink, c->sinks, idx) {
+ if (sink->module->index == m->index) {
+ pa_hashmap_put(u->null_sinks, s, sink);
+ return sink;
+ }
+ }
+
+ return NULL;
+}
+
+static void unload_null_sink_module_for_sink(struct userdata *u, pa_sink *s, pa_core *c) {
+ pa_sink *null_sink;
+
+ null_sink = pa_hashmap_get(u->null_sinks, s);
+ if (null_sink == NULL)
+ return;
+
+ pa_module_unload_request_by_index(c, null_sink->module->index, true);
+
+ pa_hashmap_remove(u->null_sinks, s);
+}
+
+static void move_stream(pa_sink_input *i, pa_sink *target) {
+ if (pa_sink_input_move_to(i, target, false) < 0)
+ pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+ else
+ pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
+ pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+}
+
+/* FIXME: remove this hack */
+static bool stream_is_fake_passthrough(pa_proplist *proplist) {
+ const char *role = NULL;
+
+ role = pa_proplist_gets(proplist, PA_PROP_MEDIA_ROLE);
+ return (role != NULL && strcmp(role, "fake-passthrough") == 0);
+}
+
+static bool sink_has_passthrough_stream(pa_sink *sink)
+{
+ /* FIXME: use pa_sink_is_passthrough() once we get rid of the
+ * "fake-passthrough" hack. */
+ pa_sink_input *stream;
+ uint32_t idx;
+
+ PA_IDXSET_FOREACH(stream, sink->inputs, idx) {
+ if (stream_is_fake_passthrough (stream->proplist) ||
+ pa_sink_input_is_passthrough (stream))
+ return true;
+ }
+
+ return false;
+}
+
+static pa_hook_result_t new_passthrough_stream(struct userdata *u, pa_core *c, pa_sink_input_new_data *new_data) {
+ uint32_t idx;
+ pa_sink_input *stream;
+ pa_sink *null_sink;
+
+ if (sink_has_passthrough_stream(new_data->sink)) {
+ pa_log_info("Dropping playing a passthrough stream; ignoring");
+ if (stream_is_fake_passthrough(new_data->proplist))
+ return PA_HOOK_CANCEL;
+ else
+ /* Pulseaudio will reject the stream itself */
+ return PA_HOOK_OK;
+ }
+
+ pa_log_info("Just received a passthrough stream; pause all the others streams so it can play");
+
+ null_sink = ensure_null_sink_for_sink(u, new_data->sink, c);
+ if (null_sink == NULL)
+ return PA_HOOK_OK;
+
+ PA_IDXSET_FOREACH(stream, new_data->sink->inputs, idx) {
+ move_stream(stream, null_sink);
+ }
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t new_normal_stream(struct userdata *u, pa_core *c, pa_sink_input_new_data *new_data) {
+ pa_sink *null_sink;
+
+ if (!sink_has_passthrough_stream(new_data->sink))
+ return PA_HOOK_OK;
+
+ null_sink = ensure_null_sink_for_sink(u, new_data->sink, c);
+ if (null_sink == NULL)
+ return PA_HOOK_OK;
+
+ pa_log_info("Already playing a passthrough stream; re-routing new stream to the null sink");
+
+ pa_sink_input_new_data_set_sink(new_data, null_sink, false);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_new_cb(pa_core *core, pa_sink_input_new_data *new_data, struct userdata *u) {
+ pa_core_assert_ref(core);
+
+ if (stream_is_fake_passthrough (new_data->proplist) ||
+ pa_sink_input_new_data_is_passthrough(new_data))
+ return new_passthrough_stream(u, core, new_data);
+
+ return new_normal_stream(u, core, new_data);
+}
+
+static pa_hook_result_t passthrough_stream_removed(struct userdata *u, pa_core *c, pa_sink_input *i) {
+ uint32_t idx;
+ pa_sink_input *stream;
+ pa_sink *null_sink;
+
+ pa_assert(i->sink);
+
+ null_sink = pa_hashmap_get(u->null_sinks, i->sink);
+ if (null_sink == NULL)
+ return PA_HOOK_OK;
+
+ pa_log_info("Passthrough stream removed; restore all streams");
+
+ PA_IDXSET_FOREACH(stream, null_sink->inputs, idx) {
+ if (stream != i)
+ move_stream(stream, i->sink);
+ }
+
+ unload_null_sink_module_for_sink(u, i->sink, c);
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
+ pa_sink_input_assert_ref(i);
+
+ if (stream_is_fake_passthrough (i->proplist) ||
+ pa_sink_input_is_passthrough (i))
+ return passthrough_stream_removed(u, core, i);
+
+ return PA_HOOK_OK;
+}
+
+int pa__init(pa_module*m) {
+ pa_modargs *ma;
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ return -1;
+ }
+
+ m->userdata = u = pa_xnew(struct userdata, 1);
+
+ u->null_sinks = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
+
+ u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_new_cb, u);
+ u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_unlink_cb, u);
+
+ pa_modargs_free(ma);
+ return 0;
+}
+
+static void unload_all_null_sink_modules(struct userdata *u, pa_core *c) {
+ void *state = NULL;
+ pa_sink *null_sink;
+
+ PA_HASHMAP_FOREACH(null_sink, u->null_sinks, state)
+ pa_module_unload_request_by_index(c, null_sink->module->index, true);
+}
+
+void pa__done(pa_module*m) {
+ struct userdata *u;
+
+ pa_assert(m);
+
+ if (!(u = m->userdata))
+ return;
+
+ if (u->sink_input_new_slot)
+ pa_hook_slot_free(u->sink_input_new_slot);
+ if (u->sink_input_unlink_slot)
+ pa_hook_slot_free(u->sink_input_unlink_slot);
+
+ if (m->core->state != PA_CORE_SHUTDOWN)
+ unload_all_null_sink_modules(u, m->core);
+
+ if (u->null_sinks)
+ pa_hashmap_free(u->null_sinks);
+
+ pa_xfree(u);
+}
--
2.0.0.rc2
More information about the pulseaudio-discuss
mailing list