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