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