Hi there,

A while ago we (Collabora) implemented a module for Valve changing the
default policy regarding passthrough streams.
You can find some context on the original thread: http://lists.freedesk
top.org/archives/pulseaudio-discuss/2014-May/020644.html

This module never reached upstream but is still shipped and used by
default on SteamOS.

I'd be interested resurrecting this work and bring it to a state it
could be merged upstream.
What would be needed to reach this? Would fixing Arun's comments from h
ttp://lists.freedesktop.org/archives/pulseaudio-discuss/2014
-May/020745.html be enough?


Thanks for your input,


        G.
/***
  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);


    /* This is a bit of a hack, to determine whether the input stream will use
     * a passthrough stream, the sink should have been selected and a format
     * renegotiated. This can either happen by an earlier module (e.g. one
     * doing routing or other policies) and if not pulseaudio core will setup
     * the defaults after all hooks for this event have been processed.
     *
     * Unfortunately if no other module decides on sink/format before this hook
     * runs, pulse core doing it is too late, so if a sink and/or stream format
     * haven't been setup & configured just yet do so now using the same code
     * as pulsecore would use (default sink and higher priority negotiated
     * format).
     */
    if (!new_data->sink) {
        pa_sink *sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK);
        pa_return_val_if_fail(sink, -PA_ERR_NOENTITY);
        pa_sink_input_new_data_set_sink(new_data, sink, false);
    }

    if (!new_data->format && new_data->nego_formats && !pa_idxset_isempty(new_data->nego_formats))
      new_data->format = pa_format_info_copy(pa_idxset_first(new_data->nego_formats, NULL));

    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);
}
_______________________________________________
pulseaudio-discuss mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/pulseaudio-discuss

Reply via email to