]> code.delx.au - pulseaudio/blob - src/pulsecore/module.c
merge glitch-free branch back into trunk
[pulseaudio] / src / pulsecore / module.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35
36 #include <pulse/timeval.h>
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/macro.h>
43 #include <pulsecore/ltdl-helper.h>
44
45 #include "module.h"
46
47 #define PA_SYMBOL_INIT "pa__init"
48 #define PA_SYMBOL_DONE "pa__done"
49 #define PA_SYMBOL_LOAD_ONCE "pa__load_once"
50
51 #define UNLOAD_POLL_TIME 2
52
53 static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, PA_GCC_UNUSED const struct timeval *tv, void *userdata) {
54 pa_core *c = PA_CORE(userdata);
55 struct timeval ntv;
56
57 pa_core_assert_ref(c);
58 pa_assert(c->mainloop == m);
59 pa_assert(c->module_auto_unload_event == e);
60
61 pa_module_unload_unused(c);
62
63 pa_gettimeofday(&ntv);
64 pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
65 m->time_restart(e, &ntv);
66 }
67
68 pa_module* pa_module_load(pa_core *c, const char *name, const char *argument) {
69 pa_module *m = NULL;
70 pa_bool_t (*load_once)(void);
71
72 pa_assert(c);
73 pa_assert(name);
74
75 if (c->disallow_module_loading)
76 goto fail;
77
78 m = pa_xnew(pa_module, 1);
79 m->name = pa_xstrdup(name);
80 m->argument = pa_xstrdup(argument);
81
82 if (!(m->dl = lt_dlopenext(name))) {
83 pa_log("Failed to open module \"%s\": %s", name, lt_dlerror());
84 goto fail;
85 }
86
87 if ((load_once = (pa_bool_t (*)(void)) pa_load_sym(m->dl, name, PA_SYMBOL_LOAD_ONCE))) {
88
89 if (load_once() && c->modules) {
90 pa_module *i;
91 uint32_t idx;
92 /* OK, the module only wants to be loaded once, let's make sure it is */
93
94 for (i = pa_idxset_first(c->modules, &idx); i; i = pa_idxset_next(c->modules, &idx)) {
95 if (strcmp(name, i->name) == 0) {
96 pa_log("Module \"%s\" should be loaded once at most. Refusing to load.", name);
97 goto fail;
98 }
99 }
100 }
101 }
102
103 if (!(m->init = (int (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_INIT))) {
104 pa_log("Failed to load module \"%s\": symbol \""PA_SYMBOL_INIT"\" not found.", name);
105 goto fail;
106 }
107
108 m->done = (void (*)(pa_module*_m)) pa_load_sym(m->dl, name, PA_SYMBOL_DONE);
109 m->userdata = NULL;
110 m->core = c;
111 m->n_used = -1;
112 m->auto_unload = FALSE;
113 m->unload_requested = FALSE;
114
115 if (m->init(m) < 0) {
116 pa_log_error("Failed to load module \"%s\" (argument: \"%s\"): initialization failed.", name, argument ? argument : "");
117 goto fail;
118 }
119
120 if (!c->modules)
121 c->modules = pa_idxset_new(NULL, NULL);
122
123 if (!c->module_auto_unload_event) {
124 struct timeval ntv;
125 pa_gettimeofday(&ntv);
126 pa_timeval_add(&ntv, UNLOAD_POLL_TIME*1000000);
127 c->module_auto_unload_event = c->mainloop->time_new(c->mainloop, &ntv, timeout_callback, c);
128 }
129
130 pa_assert_se(pa_idxset_put(c->modules, m, &m->index) >= 0);
131 pa_assert(m->index != PA_IDXSET_INVALID);
132
133 pa_log_info("Loaded \"%s\" (index: #%u; argument: \"%s\").", m->name, m->index, m->argument ? m->argument : "");
134
135 pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_NEW, m->index);
136
137 return m;
138
139 fail:
140
141 if (m) {
142 pa_xfree(m->argument);
143 pa_xfree(m->name);
144
145 if (m->dl)
146 lt_dlclose(m->dl);
147
148 pa_xfree(m);
149 }
150
151 return NULL;
152 }
153
154 static void pa_module_free(pa_module *m) {
155 pa_assert(m);
156 pa_assert(m->core);
157
158 if (m->core->disallow_module_loading)
159 return;
160
161 pa_log_info("Unloading \"%s\" (index: #%u).", m->name, m->index);
162
163 if (m->done)
164 m->done(m);
165
166 lt_dlclose(m->dl);
167
168 pa_log_info("Unloaded \"%s\" (index: #%u).", m->name, m->index);
169
170 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_REMOVE, m->index);
171
172 pa_xfree(m->name);
173 pa_xfree(m->argument);
174 pa_xfree(m);
175 }
176
177 void pa_module_unload(pa_core *c, pa_module *m) {
178 pa_assert(c);
179 pa_assert(m);
180
181 pa_assert(c->modules);
182 if (!(m = pa_idxset_remove_by_data(c->modules, m, NULL)))
183 return;
184
185 pa_module_free(m);
186 }
187
188 void pa_module_unload_by_index(pa_core *c, uint32_t idx) {
189 pa_module *m;
190 pa_assert(c);
191 pa_assert(idx != PA_IDXSET_INVALID);
192
193 if (!(m = pa_idxset_remove_by_index(c->modules, idx)))
194 return;
195
196 pa_module_free(m);
197 }
198
199 static void free_callback(void *p, PA_GCC_UNUSED void *userdata) {
200 pa_module *m = p;
201 pa_assert(m);
202 pa_module_free(m);
203 }
204
205 void pa_module_unload_all(pa_core *c) {
206 pa_module *m;
207
208 pa_assert(c);
209
210 if (!c->modules)
211 return;
212
213 while ((m = pa_idxset_first(c->modules, NULL)))
214 pa_module_unload(c, m);
215
216 pa_idxset_free(c->modules, free_callback, NULL);
217 c->modules = NULL;
218
219 if (c->module_auto_unload_event) {
220 c->mainloop->time_free(c->module_auto_unload_event);
221 c->module_auto_unload_event = NULL;
222 }
223
224 if (c->module_defer_unload_event) {
225 c->mainloop->defer_free(c->module_defer_unload_event);
226 c->module_defer_unload_event = NULL;
227 }
228 }
229
230 static int unused_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void *userdata) {
231 pa_module *m = p;
232 time_t *now = userdata;
233
234 pa_assert(m);
235 pa_assert(del);
236 pa_assert(now);
237
238 if (m->n_used == 0 && m->auto_unload && m->last_used_time+m->core->module_idle_time <= *now) {
239 pa_module_free(m);
240 *del = 1;
241 }
242
243 return 0;
244 }
245
246 void pa_module_unload_unused(pa_core *c) {
247 time_t now;
248 pa_assert(c);
249
250 if (!c->modules)
251 return;
252
253 time(&now);
254 pa_idxset_foreach(c->modules, unused_callback, &now);
255 }
256
257 static int unload_callback(void *p, PA_GCC_UNUSED uint32_t idx, int *del, PA_GCC_UNUSED void *userdata) {
258 pa_module *m = p;
259 pa_assert(m);
260
261 if (m->unload_requested) {
262 pa_module_free(m);
263 *del = 1;
264 }
265
266 return 0;
267 }
268
269 static void defer_cb(pa_mainloop_api*api, pa_defer_event *e, void *userdata) {
270 pa_core *core = PA_CORE(userdata);
271
272 pa_core_assert_ref(core);
273 api->defer_enable(e, 0);
274
275 if (!core->modules)
276 return;
277
278 pa_idxset_foreach(core->modules, unload_callback, NULL);
279 }
280
281 void pa_module_unload_request(pa_module *m) {
282 pa_assert(m);
283
284 m->unload_requested = TRUE;
285
286 if (!m->core->module_defer_unload_event)
287 m->core->module_defer_unload_event = m->core->mainloop->defer_new(m->core->mainloop, defer_cb, m->core);
288
289 m->core->mainloop->defer_enable(m->core->module_defer_unload_event, 1);
290 }
291
292 void pa_module_set_used(pa_module*m, int used) {
293 pa_assert(m);
294
295 if (m->n_used != used)
296 pa_subscription_post(m->core, PA_SUBSCRIPTION_EVENT_MODULE|PA_SUBSCRIPTION_EVENT_CHANGE, m->index);
297
298 if (used == 0 && m->n_used > 0)
299 time(&m->last_used_time);
300
301 m->n_used = used;
302 }
303
304 pa_modinfo *pa_module_get_info(pa_module *m) {
305 pa_assert(m);
306
307 return pa_modinfo_get_by_handle(m->dl, m->name);
308 }