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