]> code.delx.au - pulseaudio/blob - src/modules/module-always-sink.c
merge Colin Guthrie's module-always-sink module, and add priorization to the hook...
[pulseaudio] / src / modules / module-always-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2008 Colin Guthrie
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/core.h>
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/namereg.h>
35 #include <pulsecore/core-util.h>
36
37 #include "module-always-sink-symdef.h"
38
39 PA_MODULE_AUTHOR("Colin Guthrie");
40 PA_MODULE_DESCRIPTION("Always keeps at least one sink loaded even if it's a null one");
41 PA_MODULE_VERSION(PACKAGE_VERSION);
42 PA_MODULE_LOAD_ONCE(TRUE);
43 PA_MODULE_USAGE(
44 "sink_name=<name of sink>");
45
46 #define DEFAULT_SINK_NAME "auto_null"
47
48 static const char* const valid_modargs[] = {
49 "sink_name",
50 NULL,
51 };
52
53 struct userdata {
54 pa_hook_slot *put_slot, *unlink_slot;
55 pa_module* null_module;
56 pa_bool_t ignore;
57 char *sink_name;
58 };
59
60 static void load_null_sink_if_needed(pa_core *c, pa_sink *sink, struct userdata* u) {
61 pa_sink *target;
62 uint32_t idx;
63 char *t;
64
65 pa_assert(c);
66 pa_assert(u);
67 pa_assert(!u->null_module);
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", u->sink_name);
83 u->null_module = pa_module_load(c, "module-null-sink", t);
84 pa_xfree(t);
85
86 u->ignore = FALSE;
87
88 if (!u->null_module)
89 pa_log_warn("Unable to load module-null-sink");
90 }
91
92 static pa_hook_result_t put_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
93 struct userdata *u = userdata;
94
95 pa_assert(c);
96 pa_assert(sink);
97 pa_assert(u);
98
99 /* This is us detecting ourselves on load... just ignore this. */
100 if (u->ignore)
101 return PA_HOOK_OK;
102
103 /* Auto-loaded null-sink not active, so ignoring newly detected sink. */
104 if (!u->null_module)
105 return PA_HOOK_OK;
106
107 /* This is us detecting ourselves on load in a different way... just ignore this too. */
108 if (sink->module == u->null_module)
109 return PA_HOOK_OK;
110
111 pa_log_info("A new sink has been discovered. Unloading null-sink.");
112
113 pa_module_unload_request(u->null_module);
114 u->null_module = NULL;
115
116 return PA_HOOK_OK;
117 }
118
119 static pa_hook_result_t unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
120 struct userdata *u = userdata;
121
122 pa_assert(c);
123 pa_assert(sink);
124 pa_assert(u);
125
126 /* First check to see if it's our own null-sink that's been removed... */
127 if (u->null_module && sink->module == u->null_module) {
128 pa_log_debug("Autoloaded null-sink removed");
129 u->null_module = NULL;
130 return PA_HOOK_OK;
131 }
132
133 load_null_sink_if_needed(c, sink, u);
134
135 return PA_HOOK_OK;
136 }
137
138 int pa__init(pa_module*m) {
139 pa_modargs *ma = NULL;
140 struct userdata *u;
141
142 pa_assert(m);
143
144 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
145 pa_log("Failed to parse module arguments");
146 return -1;
147 }
148
149 m->userdata = u = pa_xnew(struct userdata, 1);
150 u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
151 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);
152 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);
153 u->null_module = NULL;
154 u->ignore = FALSE;
155
156 pa_modargs_free(ma);
157
158 load_null_sink_if_needed(m->core, NULL, u);
159
160 return 0;
161 }
162
163 void pa__done(pa_module*m) {
164 struct userdata *u;
165
166 pa_assert(m);
167
168 if (!(u = m->userdata))
169 return;
170
171 if (u->put_slot)
172 pa_hook_slot_free(u->put_slot);
173 if (u->unlink_slot)
174 pa_hook_slot_free(u->unlink_slot);
175 if (u->null_module)
176 pa_module_unload_request(u->null_module);
177
178 pa_xfree(u->sink_name);
179 pa_xfree(u);
180 }