1/***
2  This file is part of PulseAudio.
3
4  Copyright (C) 2014 Collabora Ltd. <http://www.collabora.co.uk/>
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  USA.
20***/
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#include <pulse/xmalloc.h>
27
28#include <pulsecore/core.h>
29#include <pulsecore/i18n.h>
30#include <pulsecore/sink-input.h>
31#include <pulsecore/source-output.h>
32#include <pulsecore/modargs.h>
33#include <pulsecore/log.h>
34#include <pulsecore/namereg.h>
35#include <pulsecore/core-util.h>
36
37PA_MODULE_AUTHOR("Guillaume Desmottes");
38PA_MODULE_DESCRIPTION("When a passthrough stream is requested, route all the other streams to a dummy device");
39PA_MODULE_VERSION(PACKAGE_VERSION);
40PA_MODULE_LOAD_ONCE(true);
41
42static const char* const valid_modargs[] = {
43    NULL,
44};
45
46struct userdata {
47    /* (pa_sink *) -> (pa_sink *)
48     * Map the 'real' muted sink to the null-sink currently being used to play
49     * its streams. */
50    pa_hashmap *null_sinks;
51
52    bool moving;
53};
54
55static pa_sink *ensure_null_sink_for_sink(struct userdata *u, pa_sink *s, pa_core *c) {
56    char *t;
57    pa_module *m;
58    pa_sink *sink;
59    uint32_t idx;
60    const char *name;
61
62    sink = pa_hashmap_get(u->null_sinks, s);
63    if (sink != NULL) {
64        /* We already have a null-sink for this sink */
65        return sink;
66    }
67
68    name = pa_proplist_gets(s->proplist, PA_PROP_MEDIA_NAME);
69
70    t = pa_sprintf_malloc("sink_name=allow_passthrough_null_%s sink_properties='device.description=\"%s\"'",
71                          name ? name : "", _("Dummy Output"));
72    pa_module_load(&m, c, "module-null-sink", t);
73    pa_xfree(t);
74
75    if (m == NULL)
76        return NULL;
77
78    PA_IDXSET_FOREACH(sink, c->sinks, idx) {
79        if (sink->module->index == m->index) {
80            pa_hashmap_put(u->null_sinks, s, sink);
81            return sink;
82        }
83    }
84
85   return NULL;
86}
87
88static void unload_null_sink_module_for_sink(struct userdata *u, pa_sink *s, pa_core *c) {
89    pa_sink *null_sink;
90
91    null_sink = pa_hashmap_get(u->null_sinks, s);
92    if (null_sink == NULL)
93        return;
94
95    pa_module_unload_request_by_index(c, null_sink->module->index, true);
96
97    pa_hashmap_remove(u->null_sinks, s);
98}
99
100static void move_stream(struct userdata *u, pa_sink_input *i, pa_sink *target) {
101    u->moving = true;
102    if (pa_sink_input_move_to(i, target, false) < 0)
103        pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
104                    pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
105    else
106        pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
107                    pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
108    u->moving = false;
109}
110
111/* Check if @sink has any passthrough stream, ignoring @ignore */
112static bool sink_has_passthrough_stream(pa_sink *sink, pa_sink_input *ignore)
113{
114    pa_sink_input *stream;
115    uint32_t idx;
116
117    PA_IDXSET_FOREACH(stream, sink->inputs, idx) {
118        if (stream == ignore)
119            continue;
120
121        if (pa_sink_input_is_passthrough(stream))
122            return true;
123    }
124
125    return false;
126}
127
128static pa_hook_result_t new_passthrough_stream(struct userdata *u, pa_core *c, pa_sink *sink, pa_sink_input *i) {
129    uint32_t idx;
130    pa_sink_input *stream;
131    pa_sink *null_sink;
132
133    if (sink_has_passthrough_stream(sink, i)) {
134        pa_log_info("Dropping playing a passthrough stream; ignoring");
135        /* PulseAudio will reject the stream itself */
136        return PA_HOOK_OK;
137    }
138
139    pa_log_info("Just received a passthrough stream; pause all the others streams so it can play");
140
141    null_sink = ensure_null_sink_for_sink(u, sink, c);
142    if (null_sink == NULL)
143        return PA_HOOK_OK;
144
145    PA_IDXSET_FOREACH(stream, sink->inputs, idx) {
146        /* We don't want to move the stream which just moved to the sink and trigger this re-routing */
147        if (stream != i)
148            move_stream(u, stream, null_sink);
149    }
150
151    return PA_HOOK_OK;
152}
153
154/* return a null sink for the new stream if it needs to be re-routed */
155static pa_sink * new_normal_stream(struct userdata *u, pa_core *c, pa_sink *sink) {
156    if (!sink_has_passthrough_stream(sink, NULL))
157        return NULL;
158
159    /* A passthrough stream is already playing on this sink, re-route to a null sink */
160    return ensure_null_sink_for_sink(u, sink, c);
161}
162
163static pa_hook_result_t sink_input_new_cb(pa_core *core, pa_sink_input_new_data *new_data, struct userdata *u) {
164    pa_sink *null_sink;
165
166    pa_core_assert_ref(core);
167    /* This is a bit of a hack, to determine whether the input stream will use
168     * a passthrough stream, the sink should have been selected and a format
169     * renegotiated. This can either happen by an earlier module (e.g. one
170     * doing routing or other policies) and if not pulseaudio core will setup
171     * the defaults after all hooks for this event have been processed.
172     *
173     * Unfortunately if no other module decides on sink/format before this hook
174     * runs, pulse core doing it is too late, so if a sink and/or stream format
175     * haven't been setup & configured just yet do so now using the same code
176     * as pulsecore would use (default sink and higher priority negotiated
177     * format). */
178    if (!new_data->sink) {
179        pa_sink *sink = pa_namereg_get(core, NULL, PA_NAMEREG_SINK);
180        pa_return_val_if_fail(sink, PA_HOOK_OK);
181        pa_sink_input_new_data_set_sink(new_data, sink, false, false);
182    }
183
184    if (!new_data->format && new_data->nego_formats && !pa_idxset_isempty(new_data->nego_formats))
185        new_data->format = pa_format_info_copy(pa_idxset_first(new_data->nego_formats, NULL));
186
187    if (!new_data->format) {
188        /* Sink doesn't support any requested format */
189        pa_log_debug("Default sink does not match sink input requested formats");
190        return PA_HOOK_OK;
191    }
192
193    if (pa_sink_input_new_data_is_passthrough(new_data))
194        return new_passthrough_stream(u, core, new_data->sink, NULL);
195
196    null_sink = new_normal_stream(u, core, new_data->sink);
197
198    if (null_sink) {
199        pa_log_info("Already playing a passthrough stream; re-routing new stream to the null sink");
200        pa_sink_input_new_data_set_sink(new_data, null_sink, false, false);
201    }
202
203    return PA_HOOK_OK;
204}
205
206static pa_hook_result_t passthrough_stream_removed(struct userdata *u, pa_core *c, pa_sink_input *i) {
207    uint32_t idx;
208    pa_sink_input *stream;
209    pa_sink *null_sink;
210
211    pa_assert(i->sink);
212
213    null_sink = pa_hashmap_get(u->null_sinks, i->sink);
214    if (null_sink == NULL)
215        return PA_HOOK_OK;
216
217    pa_log_info("Passthrough stream removed; restore all streams");
218
219    PA_IDXSET_FOREACH(stream, null_sink->inputs, idx) {
220        move_stream(u, stream, i->sink);
221    }
222
223    unload_null_sink_module_for_sink(u, i->sink, c);
224
225    return PA_HOOK_OK;
226}
227
228static pa_hook_result_t sink_input_removed(pa_core *core, pa_sink_input *i, struct userdata *u) {
229    pa_sink_input_assert_ref(i);
230
231    if (pa_sink_input_is_passthrough(i))
232        return passthrough_stream_removed(u, core, i);
233
234    return PA_HOOK_OK;
235}
236
237static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
238    return sink_input_removed(core, i, u);
239}
240
241static pa_hook_result_t sink_input_move_start_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
242    if (u->moving)
243        return PA_HOOK_OK;
244
245    return sink_input_removed(core, i, u);
246}
247
248static pa_hook_result_t sink_input_move_finish_cb(pa_core *core, pa_sink_input *i, struct userdata *u) {
249    pa_sink *null_sink;
250
251    if (u->moving)
252        return PA_HOOK_OK;
253
254    if (pa_sink_input_is_passthrough(i))
255        /* Passthrough stream has been moved to a new sink */
256        return new_passthrough_stream(u, core, i->sink, i);
257
258    null_sink = new_normal_stream(u, core, i->sink);
259    if (null_sink) {
260        pa_log_info("Already playing a passthrough stream; re-routing moved stream to the null sink");
261        move_stream(u, i, null_sink);
262    }
263
264    return PA_HOOK_OK;
265}
266
267int pa__init(pa_module*m) {
268    pa_modargs *ma;
269    struct userdata *u;
270
271    pa_assert(m);
272
273    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
274        pa_log("Failed to parse module arguments");
275        return -1;
276    }
277
278    m->userdata = u = pa_xnew(struct userdata, 1);
279
280    u->null_sinks = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
281
282    pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_new_cb, u);
283    pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_unlink_cb, u);
284    pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_start_cb, u);
285    pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_move_finish_cb, u);
286
287    u->moving = false;
288
289    pa_modargs_free(ma);
290    return 0;
291}
292
293static void unload_all_null_sink_modules(struct userdata *u, pa_core *c) {
294    void *state = NULL;
295    pa_sink *null_sink;
296
297    PA_HASHMAP_FOREACH(null_sink, u->null_sinks, state)
298        pa_module_unload_request_by_index(c, null_sink->module->index, true);
299}
300
301void pa__done(pa_module*m) {
302    struct userdata *u;
303
304    pa_assert(m);
305
306    if (!(u = m->userdata))
307        return;
308
309    if (m->core->state != PA_CORE_SHUTDOWN)
310        unload_all_null_sink_modules(u, m->core);
311
312    if (u->null_sinks)
313        pa_hashmap_free(u->null_sinks);
314
315    pa_xfree(u);
316}
317