]> code.delx.au - pulseaudio/blob - src/modules/module-suspend-on-idle.c
suspend-on-idle: don't crash when so->source is NULL
[pulseaudio] / src / modules / module-suspend-on-idle.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
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 #include <pulse/timeval.h>
28
29 #include <pulsecore/core.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/source-output.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/namereg.h>
35
36 #include "module-suspend-on-idle-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(TRUE);
42
43 static const char* const valid_modargs[] = {
44 "timeout",
45 NULL,
46 };
47
48 struct userdata {
49 pa_core *core;
50 pa_usec_t timeout;
51 pa_hashmap *device_infos;
52 pa_hook_slot
53 *sink_new_slot,
54 *source_new_slot,
55 *sink_unlink_slot,
56 *source_unlink_slot,
57 *sink_state_changed_slot,
58 *source_state_changed_slot;
59
60 pa_hook_slot
61 *sink_input_new_slot,
62 *source_output_new_slot,
63 *sink_input_unlink_slot,
64 *source_output_unlink_slot,
65 *sink_input_move_start_slot,
66 *source_output_move_start_slot,
67 *sink_input_move_finish_slot,
68 *source_output_move_finish_slot,
69 *sink_input_state_changed_slot,
70 *source_output_state_changed_slot;
71 };
72
73 struct device_info {
74 struct userdata *userdata;
75 pa_sink *sink;
76 pa_source *source;
77 struct timeval last_use;
78 pa_time_event *time_event;
79 };
80
81 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
82 struct device_info *d = userdata;
83
84 pa_assert(d);
85
86 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
87
88 if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && pa_sink_get_state(d->sink) != PA_SINK_SUSPENDED) {
89 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
90 pa_sink_suspend(d->sink, TRUE);
91 }
92
93 if (d->source && pa_source_check_suspend(d->source) <= 0 && pa_source_get_state(d->source) != PA_SOURCE_SUSPENDED) {
94 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
95 pa_source_suspend(d->source, TRUE);
96 }
97 }
98
99 static void restart(struct device_info *d) {
100 struct timeval tv;
101 pa_assert(d);
102
103 pa_gettimeofday(&tv);
104 d->last_use = tv;
105 pa_timeval_add(&tv, d->userdata->timeout*1000000);
106 d->userdata->core->mainloop->time_restart(d->time_event, &tv);
107
108 if (d->sink)
109 pa_log_debug("Sink %s becomes idle.", d->sink->name);
110 if (d->source)
111 pa_log_debug("Source %s becomes idle.", d->source->name);
112 }
113
114 static void resume(struct device_info *d) {
115 pa_assert(d);
116
117 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
118
119 if (d->sink) {
120 pa_sink_suspend(d->sink, FALSE);
121
122 pa_log_debug("Sink %s becomes busy.", d->sink->name);
123 }
124
125 if (d->source) {
126 pa_source_suspend(d->source, FALSE);
127
128 pa_log_debug("Source %s becomes busy.", d->source->name);
129 }
130 }
131
132 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
133 struct device_info *d;
134
135 pa_assert(c);
136 pa_assert(data);
137 pa_assert(u);
138
139 if ((d = pa_hashmap_get(u->device_infos, data->sink)))
140 resume(d);
141
142 return PA_HOOK_OK;
143 }
144
145 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
146 struct device_info *d;
147
148 pa_assert(c);
149 pa_assert(data);
150 pa_assert(u);
151
152 if ((d = pa_hashmap_get(u->device_infos, data->source)))
153 resume(d);
154
155 return PA_HOOK_OK;
156 }
157
158 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
159 pa_assert(c);
160 pa_sink_input_assert_ref(s);
161 pa_assert(u);
162
163 if (pa_sink_check_suspend(s->sink) <= 0) {
164 struct device_info *d;
165 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
166 restart(d);
167 }
168
169 return PA_HOOK_OK;
170 }
171
172 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
173 pa_assert(c);
174 pa_source_output_assert_ref(s);
175 pa_assert(u);
176
177 if (!s->source)
178 return PA_HOOK_OK;
179
180 if (pa_source_check_suspend(s->source) <= 0) {
181 struct device_info *d;
182 if ((d = pa_hashmap_get(u->device_infos, s->source)))
183 restart(d);
184 }
185
186 return PA_HOOK_OK;
187 }
188
189 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
190 struct device_info *d;
191
192 pa_assert(c);
193 pa_sink_input_assert_ref(s);
194 pa_assert(u);
195
196 if (pa_sink_check_suspend(s->sink) <= 1)
197 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
198 restart(d);
199
200 return PA_HOOK_OK;
201 }
202
203 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
204 struct device_info *d;
205
206 pa_assert(c);
207 pa_sink_input_assert_ref(s);
208 pa_assert(u);
209
210 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
211 resume(d);
212
213 return PA_HOOK_OK;
214 }
215
216 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
217 struct device_info *d;
218
219 pa_assert(c);
220 pa_source_output_assert_ref(s);
221 pa_assert(u);
222
223 if (pa_source_check_suspend(s->source) <= 1)
224 if ((d = pa_hashmap_get(u->device_infos, s->source)))
225 restart(d);
226
227 return PA_HOOK_OK;
228 }
229
230 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
231 struct device_info *d;
232
233 pa_assert(c);
234 pa_source_output_assert_ref(s);
235 pa_assert(u);
236
237 if ((d = pa_hashmap_get(u->device_infos, s->source)))
238 resume(d);
239
240 return PA_HOOK_OK;
241 }
242
243 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
244 struct device_info *d;
245 pa_sink_input_state_t state;
246 pa_assert(c);
247 pa_sink_input_assert_ref(s);
248 pa_assert(u);
249
250 state = pa_sink_input_get_state(s);
251 if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
252 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
253 resume(d);
254
255 return PA_HOOK_OK;
256 }
257
258 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
259 struct device_info *d;
260 pa_source_output_state_t state;
261 pa_assert(c);
262 pa_source_output_assert_ref(s);
263 pa_assert(u);
264
265 state = pa_source_output_get_state(s);
266 if (state == PA_SOURCE_OUTPUT_RUNNING)
267 if ((d = pa_hashmap_get(u->device_infos, s->source)))
268 resume(d);
269
270 return PA_HOOK_OK;
271 }
272
273 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
274 struct device_info *d;
275 pa_source *source;
276 pa_sink *sink;
277
278 pa_assert(c);
279 pa_object_assert_ref(o);
280 pa_assert(u);
281
282 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
283 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
284
285 pa_assert(source || sink);
286
287 d = pa_xnew(struct device_info, 1);
288 d->userdata = u;
289 d->source = source ? pa_source_ref(source) : NULL;
290 d->sink = sink ? pa_sink_ref(sink) : NULL;
291 d->time_event = c->mainloop->time_new(c->mainloop, NULL, timeout_cb, d);
292 pa_hashmap_put(u->device_infos, o, d);
293
294 if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
295 (d->source && pa_source_check_suspend(d->source) <= 0))
296 restart(d);
297
298 return PA_HOOK_OK;
299 }
300
301 static void device_info_free(struct device_info *d) {
302 pa_assert(d);
303
304 if (d->source)
305 pa_source_unref(d->source);
306 if (d->sink)
307 pa_sink_unref(d->sink);
308
309 d->userdata->core->mainloop->time_free(d->time_event);
310
311 pa_xfree(d);
312 }
313
314 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
315 struct device_info *d;
316
317 pa_assert(c);
318 pa_object_assert_ref(o);
319 pa_assert(u);
320
321 if ((d = pa_hashmap_remove(u->device_infos, o)))
322 device_info_free(d);
323
324 return PA_HOOK_OK;
325 }
326
327 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
328 struct device_info *d;
329
330 pa_assert(c);
331 pa_object_assert_ref(o);
332 pa_assert(u);
333
334 if (!(d = pa_hashmap_get(u->device_infos, o)))
335 return PA_HOOK_OK;
336
337 if (pa_sink_isinstance(o)) {
338 pa_sink *s = PA_SINK(o);
339 pa_sink_state_t state = pa_sink_get_state(s);
340
341 if (pa_sink_check_suspend(s) <= 0) {
342
343 if (PA_SINK_IS_OPENED(state))
344 restart(d);
345
346 }
347
348 } else if (pa_source_isinstance(o)) {
349 pa_source *s = PA_SOURCE(o);
350 pa_source_state_t state = pa_source_get_state(s);
351
352 if (pa_source_check_suspend(s) <= 0) {
353
354 if (PA_SOURCE_IS_OPENED(state))
355 restart(d);
356 }
357 }
358
359 return PA_HOOK_OK;
360 }
361
362 int pa__init(pa_module*m) {
363 pa_modargs *ma = NULL;
364 struct userdata *u;
365 uint32_t timeout = 5;
366 uint32_t idx;
367 pa_sink *sink;
368 pa_source *source;
369
370 pa_assert(m);
371
372 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
373 pa_log("Failed to parse module arguments.");
374 goto fail;
375 }
376
377 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
378 pa_log("Failed to parse timeout value.");
379 goto fail;
380 }
381
382 m->userdata = u = pa_xnew(struct userdata, 1);
383 u->core = m->core;
384 u->timeout = timeout;
385 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
386
387 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
388 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
389
390 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
391 device_new_hook_cb(m->core, PA_OBJECT(source), u);
392
393 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
394 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
395 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
396 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
397 u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
398 u->source_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
399
400 u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_fixate_hook_cb, u);
401 u->source_output_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_fixate_hook_cb, u);
402 u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_unlink_hook_cb, u);
403 u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_unlink_hook_cb, u);
404 u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_start_hook_cb, u);
405 u->source_output_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_start_hook_cb, u);
406 u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_finish_hook_cb, u);
407 u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_finish_hook_cb, u);
408 u->sink_input_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_state_changed_hook_cb, u);
409 u->source_output_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_state_changed_hook_cb, u);
410
411 pa_modargs_free(ma);
412 return 0;
413
414 fail:
415
416 if (ma)
417 pa_modargs_free(ma);
418
419 return -1;
420 }
421
422 void pa__done(pa_module*m) {
423 struct userdata *u;
424 struct device_info *d;
425
426 pa_assert(m);
427
428 if (!m->userdata)
429 return;
430
431 u = m->userdata;
432
433 if (u->sink_new_slot)
434 pa_hook_slot_free(u->sink_new_slot);
435 if (u->sink_unlink_slot)
436 pa_hook_slot_free(u->sink_unlink_slot);
437 if (u->sink_state_changed_slot)
438 pa_hook_slot_free(u->sink_state_changed_slot);
439
440 if (u->source_new_slot)
441 pa_hook_slot_free(u->source_new_slot);
442 if (u->source_unlink_slot)
443 pa_hook_slot_free(u->source_unlink_slot);
444 if (u->source_state_changed_slot)
445 pa_hook_slot_free(u->source_state_changed_slot);
446
447 if (u->sink_input_new_slot)
448 pa_hook_slot_free(u->sink_input_new_slot);
449 if (u->sink_input_unlink_slot)
450 pa_hook_slot_free(u->sink_input_unlink_slot);
451 if (u->sink_input_move_start_slot)
452 pa_hook_slot_free(u->sink_input_move_start_slot);
453 if (u->sink_input_move_finish_slot)
454 pa_hook_slot_free(u->sink_input_move_finish_slot);
455 if (u->sink_input_state_changed_slot)
456 pa_hook_slot_free(u->sink_input_state_changed_slot);
457
458 if (u->source_output_new_slot)
459 pa_hook_slot_free(u->source_output_new_slot);
460 if (u->source_output_unlink_slot)
461 pa_hook_slot_free(u->source_output_unlink_slot);
462 if (u->source_output_move_start_slot)
463 pa_hook_slot_free(u->source_output_move_start_slot);
464 if (u->source_output_move_finish_slot)
465 pa_hook_slot_free(u->source_output_move_finish_slot);
466 if (u->source_output_state_changed_slot)
467 pa_hook_slot_free(u->source_output_state_changed_slot);
468
469 while ((d = pa_hashmap_steal_first(u->device_infos)))
470 device_info_free(d);
471
472 pa_hashmap_free(u->device_infos, NULL, NULL);
473
474 pa_xfree(u);
475 }