1/***
2    This file is part of PulseAudio.
3
4    Copyright 2008 Colin Guthrie
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, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <pulse/xmalloc.h>
25
26#include <pulsecore/core.h>
27#include <pulsecore/core-util.h>
28#include <pulsecore/i18n.h>
29#include <pulsecore/sink.h>
30#include <pulsecore/modargs.h>
31#include <pulsecore/log.h>
32
33PA_MODULE_AUTHOR("Colin Guthrie");
34PA_MODULE_DESCRIPTION(_("Always keeps at least one sink loaded even if it's a null one"));
35PA_MODULE_VERSION(PACKAGE_VERSION);
36PA_MODULE_LOAD_ONCE(true);
37PA_MODULE_USAGE(
38        "sink_name=<name of sink>");
39
40#define DEFAULT_SINK_NAME "auto_null"
41
42static const char* const valid_modargs[] = {
43    "sink_name",
44    NULL,
45};
46
47struct userdata {
48    uint32_t null_module;
49    bool ignore;
50    char *sink_name;
51};
52
53static void load_null_sink_if_needed(pa_core *c, pa_sink *sink, struct userdata* u) {
54    pa_sink *target;
55    uint32_t idx;
56    char *t;
57    pa_module *m;
58
59    pa_assert(c);
60    pa_assert(u);
61
62    if (u->null_module != PA_INVALID_INDEX)
63        return; /* We've already got a null-sink loaded */
64
65    /* Loop through all sinks and check to see if we have *any*
66     * sinks. Ignore the sink passed in (if it's not null), and
67     * don't count filter sinks. */
68    PA_IDXSET_FOREACH(target, c->sinks, idx)
69        if (!sink || ((target != sink) && !pa_sink_is_filter(target)))
70            break;
71
72    if (target)
73        return;
74
75    pa_log_debug("Autoloading null-sink as no other sinks detected.");
76
77    u->ignore = true;
78
79    t = pa_sprintf_malloc("sink_name=%s sink_properties='device.description=\"%s\"'", u->sink_name,
80                          _("Dummy Output"));
81    pa_module_load(&m, c, "module-null-sink", t);
82    u->null_module = m ? m->index : PA_INVALID_INDEX;
83    pa_xfree(t);
84
85    u->ignore = false;
86
87    if (!m)
88        pa_log_warn("Unable to load module-null-sink");
89}
90
91static pa_hook_result_t put_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
92    struct userdata *u = userdata;
93
94    pa_assert(c);
95    pa_assert(sink);
96    pa_assert(u);
97
98    /* This is us detecting ourselves on load... just ignore this. */
99    if (u->ignore)
100        return PA_HOOK_OK;
101
102    /* There's no point in doing anything if the core is shut down anyway */
103    if (c->state == PA_CORE_SHUTDOWN)
104        return PA_HOOK_OK;
105
106    /* Auto-loaded null-sink not active, so ignoring newly detected sink. */
107    if (u->null_module == PA_INVALID_INDEX)
108        return PA_HOOK_OK;
109
110    /* This is us detecting ourselves on load in a different way... just ignore this too. */
111    if (sink->module && sink->module->index == u->null_module)
112        return PA_HOOK_OK;
113
114    /* We don't count filter sinks since they need a real sink */
115    if (pa_sink_is_filter(sink))
116        return PA_HOOK_OK;
117
118    pa_log_info("A new sink has been discovered. Unloading null-sink.");
119
120    pa_module_unload_request_by_index(c, u->null_module, true);
121    u->null_module = PA_INVALID_INDEX;
122
123    return PA_HOOK_OK;
124}
125
126static pa_hook_result_t unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
127    struct userdata *u = userdata;
128
129    pa_assert(c);
130    pa_assert(sink);
131    pa_assert(u);
132
133    /* First check to see if it's our own null-sink that's been removed... */
134    if (u->null_module != PA_INVALID_INDEX && sink->module && sink->module->index == u->null_module) {
135        pa_log_debug("Autoloaded null-sink removed");
136        u->null_module = PA_INVALID_INDEX;
137        return PA_HOOK_OK;
138    }
139
140    /* There's no point in doing anything if the core is shut down anyway */
141    if (c->state == PA_CORE_SHUTDOWN)
142        return PA_HOOK_OK;
143
144    load_null_sink_if_needed(c, sink, u);
145
146    return PA_HOOK_OK;
147}
148
149int pa__init(pa_module*m) {
150    pa_modargs *ma = NULL;
151    struct userdata *u;
152
153    pa_assert(m);
154
155    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
156        pa_log("Failed to parse module arguments");
157        return -1;
158    }
159
160    m->userdata = u = pa_xnew(struct userdata, 1);
161    u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
162    pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) put_hook_callback, u);
163    pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_EARLY, (pa_hook_cb_t) unlink_hook_callback, u);
164    u->null_module = PA_INVALID_INDEX;
165    u->ignore = false;
166
167    pa_modargs_free(ma);
168
169    load_null_sink_if_needed(m->core, NULL, u);
170
171    return 0;
172}
173
174void pa__done(pa_module*m) {
175    struct userdata *u;
176
177    pa_assert(m);
178
179    if (!(u = m->userdata))
180        return;
181
182    if (u->null_module != PA_INVALID_INDEX && m->core->state != PA_CORE_SHUTDOWN)
183        pa_module_unload_request_by_index(m->core, u->null_module, true);
184
185    pa_xfree(u->sink_name);
186    pa_xfree(u);
187}
188