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