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