]> code.delx.au - pulseaudio/blob - src/modules/module-suspend-on-idle.c
* decouple suspending of monitor sources and their sinks
[pulseaudio] / src / modules / module-suspend-on-idle.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29 #include <pulse/timeval.h>
30
31 #include <pulsecore/core.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
44 static const char* const valid_modargs[] = {
45 "timeout",
46 NULL,
47 };
48
49 struct userdata {
50 pa_core *core;
51 pa_usec_t timeout;
52 pa_hashmap *device_infos;
53 pa_hook_slot
54 *sink_new_slot,
55 *source_new_slot,
56 *sink_unlink_slot,
57 *source_unlink_slot,
58 *sink_state_changed_slot,
59 *source_state_changed_slot;
60
61 pa_hook_slot
62 *sink_input_new_slot,
63 *source_output_new_slot,
64 *sink_input_unlink_slot,
65 *source_output_unlink_slot,
66 *sink_input_move_slot,
67 *source_output_move_slot,
68 *sink_input_move_post_slot,
69 *source_output_move_post_slot,
70 *sink_input_state_changed_slot,
71 *source_output_state_changed_slot;
72 };
73
74 struct device_info {
75 struct userdata *userdata;
76 pa_sink *sink;
77 pa_source *source;
78 struct timeval last_use;
79 pa_time_event *time_event;
80 };
81
82 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
83 struct device_info *d = userdata;
84
85 pa_assert(d);
86
87 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
88
89 if (d->sink && pa_sink_used_by(d->sink) <= 0 && pa_sink_get_state(d->sink) != PA_SINK_SUSPENDED) {
90 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
91 pa_sink_suspend(d->sink, TRUE);
92 }
93
94 if (d->source && pa_source_used_by(d->source) <= 0 && pa_source_get_state(d->source) != PA_SOURCE_SUSPENDED) {
95 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
96 pa_source_suspend(d->source, TRUE);
97 }
98 }
99
100 static void restart(struct device_info *d) {
101 struct timeval tv;
102 pa_assert(d);
103
104 pa_gettimeofday(&tv);
105 d->last_use = tv;
106 pa_timeval_add(&tv, d->userdata->timeout*1000000);
107 d->userdata->core->mainloop->time_restart(d->time_event, &tv);
108
109 if (d->sink)
110 pa_log_debug("Sink %s becomes idle.", d->sink->name);
111 if (d->source)
112 pa_log_debug("Source %s becomes idle.", d->source->name);
113 }
114
115 static void resume(struct device_info *d) {
116 pa_assert(d);
117
118 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
119
120 if (d->sink) {
121 pa_sink_suspend(d->sink, FALSE);
122
123 pa_log_debug("Sink %s becomes busy.", d->sink->name);
124 }
125
126 if (d->source) {
127 pa_source_suspend(d->source, FALSE);
128
129 pa_log_debug("Source %s becomes busy.", d->source->name);
130 }
131 }
132
133 static pa_hook_result_t sink_input_new_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
134 struct device_info *d;
135
136 pa_assert(c);
137 pa_sink_input_assert_ref(s);
138 pa_assert(u);
139
140 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
141 resume(d);
142
143 return PA_HOOK_OK;
144 }
145
146 static pa_hook_result_t source_output_new_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
147 struct device_info *d;
148
149 pa_assert(c);
150 pa_source_output_assert_ref(s);
151 pa_assert(u);
152
153 if ((d = pa_hashmap_get(u->device_infos, s->source)))
154 resume(d);
155
156 return PA_HOOK_OK;
157 }
158
159 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
160 pa_assert(c);
161 pa_sink_input_assert_ref(s);
162 pa_assert(u);
163
164 if (pa_sink_used_by(s->sink) <= 0) {
165 struct device_info *d;
166 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
167 restart(d);
168 }
169
170 return PA_HOOK_OK;
171 }
172
173 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
174 pa_assert(c);
175 pa_source_output_assert_ref(s);
176 pa_assert(u);
177
178 if (pa_source_used_by(s->source) <= 0) {
179 struct device_info *d;
180 if ((d = pa_hashmap_get(u->device_infos, s->source)))
181 restart(d);
182 }
183
184 return PA_HOOK_OK;
185 }
186
187 static pa_hook_result_t sink_input_move_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
188 pa_assert(c);
189 pa_sink_input_assert_ref(s);
190 pa_assert(u);
191
192 if (pa_sink_used_by(s->sink) <= 1) {
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 sink_input_move_post_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
202 struct device_info *d;
203 pa_assert(c);
204 pa_sink_input_assert_ref(s);
205 pa_assert(u);
206
207 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
208 resume(d);
209
210 return PA_HOOK_OK;
211 }
212
213 static pa_hook_result_t source_output_move_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
214 pa_assert(c);
215 pa_source_output_assert_ref(s);
216 pa_assert(u);
217
218 if (pa_source_used_by(s->source) <= 1) {
219 struct device_info *d;
220
221 if ((d = pa_hashmap_get(u->device_infos, s->source)))
222 restart(d);
223 }
224
225 return PA_HOOK_OK;
226 }
227
228 static pa_hook_result_t source_output_move_post_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
229 struct device_info *d;
230 pa_assert(c);
231 pa_source_output_assert_ref(s);
232 pa_assert(u);
233
234 if ((d = pa_hashmap_get(u->device_infos, s->source)))
235 resume(d);
236
237 return PA_HOOK_OK;
238 }
239
240 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
241 struct device_info *d;
242 pa_sink_input_state_t state;
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 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
250 resume(d);
251
252 return PA_HOOK_OK;
253 }
254
255 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
256 struct device_info *d;
257 pa_source_output_state_t state;
258 pa_assert(c);
259 pa_source_output_assert_ref(s);
260 pa_assert(u);
261
262 state = pa_source_output_get_state(s);
263 if (state == PA_SOURCE_OUTPUT_RUNNING)
264 if ((d = pa_hashmap_get(u->device_infos, s->source)))
265 resume(d);
266
267 return PA_HOOK_OK;
268 }
269
270 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
271 struct device_info *d;
272 pa_source *source;
273 pa_sink *sink;
274
275 pa_assert(c);
276 pa_object_assert_ref(o);
277 pa_assert(u);
278
279 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
280 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
281
282 pa_assert(source || sink);
283
284 d = pa_xnew(struct device_info, 1);
285 d->userdata = u;
286 d->source = source ? pa_source_ref(source) : NULL;
287 d->sink = sink ? pa_sink_ref(sink) : NULL;
288 d->time_event = c->mainloop->time_new(c->mainloop, NULL, timeout_cb, d);
289 pa_hashmap_put(u->device_infos, o, d);
290
291 if ((d->sink && pa_sink_used_by(d->sink) <= 0) ||
292 (d->source && pa_source_used_by(d->source) <= 0))
293 restart(d);
294
295 return PA_HOOK_OK;
296 }
297
298 static void device_info_free(struct device_info *d) {
299 pa_assert(d);
300
301 if (d->source)
302 pa_source_unref(d->source);
303 if (d->sink)
304 pa_sink_unref(d->sink);
305
306 d->userdata->core->mainloop->time_free(d->time_event);
307
308 pa_xfree(d);
309 }
310
311 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
312 struct device_info *d;
313
314 pa_assert(c);
315 pa_object_assert_ref(o);
316 pa_assert(u);
317
318 if ((d = pa_hashmap_remove(u->device_infos, o)))
319 device_info_free(d);
320
321 return PA_HOOK_OK;
322 }
323
324 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
325 struct device_info *d;
326
327 pa_assert(c);
328 pa_object_assert_ref(o);
329 pa_assert(u);
330
331 if (!(d = pa_hashmap_get(u->device_infos, o)))
332 return PA_HOOK_OK;
333
334 if (pa_sink_isinstance(o)) {
335 pa_sink *s = PA_SINK(o);
336 pa_sink_state_t state = pa_sink_get_state(s);
337
338 if (pa_sink_used_by(s) <= 0) {
339
340 if (PA_SINK_OPENED(state))
341 restart(d);
342
343 }
344
345 } else if (pa_source_isinstance(o)) {
346 pa_source *s = PA_SOURCE(o);
347 pa_source_state_t state = pa_source_get_state(s);
348
349 if (pa_source_used_by(s) <= 0) {
350
351 if (PA_SOURCE_OPENED(state))
352 restart(d);
353 }
354 }
355
356 return PA_HOOK_OK;
357 }
358
359 int pa__init(pa_module*m) {
360 pa_modargs *ma = NULL;
361 struct userdata *u;
362 uint32_t timeout = 1;
363 uint32_t idx;
364 pa_sink *sink;
365 pa_source *source;
366
367 pa_assert(m);
368
369 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
370 pa_log("Failed to parse module arguments.");
371 goto fail;
372 }
373
374 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
375 pa_log("Failed to parse timeout value.");
376 goto fail;
377 }
378
379 m->userdata = u = pa_xnew(struct userdata, 1);
380 u->core = m->core;
381 u->timeout = timeout;
382 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
383
384 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
385 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
386
387 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
388 device_new_hook_cb(m->core, PA_OBJECT(source), u);
389
390 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], (pa_hook_cb_t) device_new_hook_cb, u);
391 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], (pa_hook_cb_t) device_new_hook_cb, u);
392 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], (pa_hook_cb_t) device_unlink_hook_cb, u);
393 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], (pa_hook_cb_t) device_unlink_hook_cb, u);
394 u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], (pa_hook_cb_t) device_state_changed_hook_cb, u);
395 u->source_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], (pa_hook_cb_t) device_state_changed_hook_cb, u);
396
397 u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_PUT], (pa_hook_cb_t) sink_input_new_hook_cb, u);
398 u->source_output_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PUT], (pa_hook_cb_t) source_output_new_hook_cb, u);
399 u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], (pa_hook_cb_t) sink_input_unlink_hook_cb, u);
400 u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], (pa_hook_cb_t) source_output_unlink_hook_cb, u);
401 u->sink_input_move_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE], (pa_hook_cb_t) sink_input_move_hook_cb, u);
402 u->source_output_move_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE], (pa_hook_cb_t) source_output_move_hook_cb, u);
403 u->sink_input_move_post_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_POST], (pa_hook_cb_t) sink_input_move_post_hook_cb, u);
404 u->source_output_move_post_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST], (pa_hook_cb_t) source_output_move_post_hook_cb, u);
405 u->sink_input_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], (pa_hook_cb_t) sink_input_state_changed_hook_cb, u);
406 u->source_output_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], (pa_hook_cb_t) source_output_state_changed_hook_cb, u);
407
408
409 pa_modargs_free(ma);
410 return 0;
411
412 fail:
413
414 if (ma)
415 pa_modargs_free(ma);
416
417 return -1;
418 }
419
420 void pa__done(pa_module*m) {
421 struct userdata *u;
422 struct device_info *d;
423
424 pa_assert(m);
425
426 if (!m->userdata)
427 return;
428
429 u = m->userdata;
430
431 if (u->sink_new_slot)
432 pa_hook_slot_free(u->sink_new_slot);
433 if (u->sink_unlink_slot)
434 pa_hook_slot_free(u->sink_unlink_slot);
435 if (u->sink_state_changed_slot)
436 pa_hook_slot_free(u->sink_state_changed_slot);
437
438 if (u->source_new_slot)
439 pa_hook_slot_free(u->source_new_slot);
440 if (u->source_unlink_slot)
441 pa_hook_slot_free(u->source_unlink_slot);
442 if (u->source_state_changed_slot)
443 pa_hook_slot_free(u->source_state_changed_slot);
444
445 if (u->sink_input_new_slot)
446 pa_hook_slot_free(u->sink_input_new_slot);
447 if (u->sink_input_unlink_slot)
448 pa_hook_slot_free(u->sink_input_unlink_slot);
449 if (u->sink_input_move_slot)
450 pa_hook_slot_free(u->sink_input_move_slot);
451 if (u->sink_input_move_post_slot)
452 pa_hook_slot_free(u->sink_input_move_post_slot);
453 if (u->sink_input_state_changed_slot)
454 pa_hook_slot_free(u->sink_input_state_changed_slot);
455
456 if (u->source_output_new_slot)
457 pa_hook_slot_free(u->source_output_new_slot);
458 if (u->source_output_unlink_slot)
459 pa_hook_slot_free(u->source_output_unlink_slot);
460 if (u->source_output_move_slot)
461 pa_hook_slot_free(u->source_output_move_slot);
462 if (u->source_output_move_post_slot)
463 pa_hook_slot_free(u->source_output_move_post_slot);
464 if (u->source_output_state_changed_slot)
465 pa_hook_slot_free(u->source_output_state_changed_slot);
466
467 while ((d = pa_hashmap_steal_first(u->device_infos)))
468 device_info_free(d);
469
470 pa_hashmap_free(u->device_infos, NULL, NULL);
471
472 pa_xfree(u);
473 }