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