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