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