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