]> code.delx.au - pulseaudio/blob - src/modules/module-suspend-on-idle.c
merge glitch-free branch back into trunk
[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 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_slot,
68 *source_output_move_slot,
69 *sink_input_state_changed_slot,
70 *source_output_state_changed_slot;
71 };
72
73 struct device_info {
74 struct userdata *userdata;
75 pa_sink *sink;
76 pa_source *source;
77 struct timeval last_use;
78 pa_time_event *time_event;
79 };
80
81 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
82 struct device_info *d = userdata;
83
84 pa_assert(d);
85
86 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
87
88 if (d->sink && pa_sink_used_by(d->sink) <= 0 && pa_sink_get_state(d->sink) != PA_SINK_SUSPENDED) {
89 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
90 pa_sink_suspend(d->sink, TRUE);
91 }
92
93 if (d->source && pa_source_used_by(d->source) <= 0 && pa_source_get_state(d->source) != PA_SOURCE_SUSPENDED) {
94 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
95 pa_source_suspend(d->source, TRUE);
96 }
97 }
98
99 static void restart(struct device_info *d) {
100 struct timeval tv;
101 pa_assert(d);
102
103 pa_gettimeofday(&tv);
104 d->last_use = tv;
105 pa_timeval_add(&tv, d->userdata->timeout*1000000);
106 d->userdata->core->mainloop->time_restart(d->time_event, &tv);
107
108 if (d->sink)
109 pa_log_debug("Sink %s becomes idle.", d->sink->name);
110 if (d->source)
111 pa_log_debug("Source %s becomes idle.", d->source->name);
112 }
113
114 static void resume(struct device_info *d) {
115 pa_assert(d);
116
117 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
118
119 if (d->sink) {
120 pa_sink_suspend(d->sink, FALSE);
121
122 pa_log_debug("Sink %s becomes busy.", d->sink->name);
123 }
124
125 if (d->source) {
126 pa_source_suspend(d->source, FALSE);
127
128 pa_log_debug("Source %s becomes busy.", d->source->name);
129 }
130 }
131
132 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
133 struct device_info *d;
134
135 pa_assert(c);
136 pa_assert(data);
137 pa_assert(u);
138
139 if ((d = pa_hashmap_get(u->device_infos, data->sink)))
140 resume(d);
141
142 return PA_HOOK_OK;
143 }
144
145 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
146 struct device_info *d;
147
148 pa_assert(c);
149 pa_assert(data);
150 pa_assert(u);
151
152 if ((d = pa_hashmap_get(u->device_infos, data->source)))
153 resume(d);
154
155 return PA_HOOK_OK;
156 }
157
158 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
159 pa_assert(c);
160 pa_sink_input_assert_ref(s);
161 pa_assert(u);
162
163 if (pa_sink_used_by(s->sink) <= 0) {
164 struct device_info *d;
165 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
166 restart(d);
167 }
168
169 return PA_HOOK_OK;
170 }
171
172 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
173 pa_assert(c);
174 pa_source_output_assert_ref(s);
175 pa_assert(u);
176
177 if (pa_source_used_by(s->source) <= 0) {
178 struct device_info *d;
179 if ((d = pa_hashmap_get(u->device_infos, s->source)))
180 restart(d);
181 }
182
183 return PA_HOOK_OK;
184 }
185
186 static pa_hook_result_t sink_input_move_hook_cb(pa_core *c, pa_sink_input_move_hook_data *data, struct userdata *u) {
187 struct device_info *d;
188
189 pa_assert(c);
190 pa_assert(data);
191 pa_assert(u);
192
193 if ((d = pa_hashmap_get(u->device_infos, data->destination)))
194 resume(d);
195
196 if (pa_sink_used_by(data->sink_input->sink) <= 1)
197 if ((d = pa_hashmap_get(u->device_infos, data->sink_input->sink)))
198 restart(d);
199
200 return PA_HOOK_OK;
201 }
202
203 static pa_hook_result_t source_output_move_hook_cb(pa_core *c, pa_source_output_move_hook_data *data, struct userdata *u) {
204 struct device_info *d;
205
206 pa_assert(c);
207 pa_assert(data);
208 pa_assert(u);
209
210 if ((d = pa_hashmap_get(u->device_infos, data->destination)))
211 resume(d);
212
213 if (pa_source_used_by(data->source_output->source) <= 1)
214 if ((d = pa_hashmap_get(u->device_infos, data->source_output->source)))
215 restart(d);
216
217 return PA_HOOK_OK;
218 }
219
220 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
221 struct device_info *d;
222 pa_sink_input_state_t state;
223 pa_assert(c);
224 pa_sink_input_assert_ref(s);
225 pa_assert(u);
226
227 state = pa_sink_input_get_state(s);
228 if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
229 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
230 resume(d);
231
232 return PA_HOOK_OK;
233 }
234
235 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
236 struct device_info *d;
237 pa_source_output_state_t state;
238 pa_assert(c);
239 pa_source_output_assert_ref(s);
240 pa_assert(u);
241
242 state = pa_source_output_get_state(s);
243 if (state == PA_SOURCE_OUTPUT_RUNNING)
244 if ((d = pa_hashmap_get(u->device_infos, s->source)))
245 resume(d);
246
247 return PA_HOOK_OK;
248 }
249
250 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
251 struct device_info *d;
252 pa_source *source;
253 pa_sink *sink;
254
255 pa_assert(c);
256 pa_object_assert_ref(o);
257 pa_assert(u);
258
259 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
260 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
261
262 pa_assert(source || sink);
263
264 d = pa_xnew(struct device_info, 1);
265 d->userdata = u;
266 d->source = source ? pa_source_ref(source) : NULL;
267 d->sink = sink ? pa_sink_ref(sink) : NULL;
268 d->time_event = c->mainloop->time_new(c->mainloop, NULL, timeout_cb, d);
269 pa_hashmap_put(u->device_infos, o, d);
270
271 if ((d->sink && pa_sink_used_by(d->sink) <= 0) ||
272 (d->source && pa_source_used_by(d->source) <= 0))
273 restart(d);
274
275 return PA_HOOK_OK;
276 }
277
278 static void device_info_free(struct device_info *d) {
279 pa_assert(d);
280
281 if (d->source)
282 pa_source_unref(d->source);
283 if (d->sink)
284 pa_sink_unref(d->sink);
285
286 d->userdata->core->mainloop->time_free(d->time_event);
287
288 pa_xfree(d);
289 }
290
291 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
292 struct device_info *d;
293
294 pa_assert(c);
295 pa_object_assert_ref(o);
296 pa_assert(u);
297
298 if ((d = pa_hashmap_remove(u->device_infos, o)))
299 device_info_free(d);
300
301 return PA_HOOK_OK;
302 }
303
304 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
305 struct device_info *d;
306
307 pa_assert(c);
308 pa_object_assert_ref(o);
309 pa_assert(u);
310
311 if (!(d = pa_hashmap_get(u->device_infos, o)))
312 return PA_HOOK_OK;
313
314 if (pa_sink_isinstance(o)) {
315 pa_sink *s = PA_SINK(o);
316 pa_sink_state_t state = pa_sink_get_state(s);
317
318 if (pa_sink_used_by(s) <= 0) {
319
320 if (PA_SINK_IS_OPENED(state))
321 restart(d);
322
323 }
324
325 } else if (pa_source_isinstance(o)) {
326 pa_source *s = PA_SOURCE(o);
327 pa_source_state_t state = pa_source_get_state(s);
328
329 if (pa_source_used_by(s) <= 0) {
330
331 if (PA_SOURCE_IS_OPENED(state))
332 restart(d);
333 }
334 }
335
336 return PA_HOOK_OK;
337 }
338
339 int pa__init(pa_module*m) {
340 pa_modargs *ma = NULL;
341 struct userdata *u;
342 uint32_t timeout = 1;
343 uint32_t idx;
344 pa_sink *sink;
345 pa_source *source;
346
347 pa_assert(m);
348
349 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
350 pa_log("Failed to parse module arguments.");
351 goto fail;
352 }
353
354 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
355 pa_log("Failed to parse timeout value.");
356 goto fail;
357 }
358
359 m->userdata = u = pa_xnew(struct userdata, 1);
360 u->core = m->core;
361 u->timeout = timeout;
362 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
363
364 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
365 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
366
367 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
368 device_new_hook_cb(m->core, PA_OBJECT(source), u);
369
370 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], (pa_hook_cb_t) device_new_hook_cb, u);
371 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], (pa_hook_cb_t) device_new_hook_cb, u);
372 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);
373 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);
374 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);
375 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);
376
377 u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], (pa_hook_cb_t) sink_input_fixate_hook_cb, u);
378 u->source_output_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], (pa_hook_cb_t) source_output_fixate_hook_cb, u);
379 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);
380 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);
381 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);
382 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);
383 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);
384 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);
385
386 pa_modargs_free(ma);
387 return 0;
388
389 fail:
390
391 if (ma)
392 pa_modargs_free(ma);
393
394 return -1;
395 }
396
397 void pa__done(pa_module*m) {
398 struct userdata *u;
399 struct device_info *d;
400
401 pa_assert(m);
402
403 if (!m->userdata)
404 return;
405
406 u = m->userdata;
407
408 if (u->sink_new_slot)
409 pa_hook_slot_free(u->sink_new_slot);
410 if (u->sink_unlink_slot)
411 pa_hook_slot_free(u->sink_unlink_slot);
412 if (u->sink_state_changed_slot)
413 pa_hook_slot_free(u->sink_state_changed_slot);
414
415 if (u->source_new_slot)
416 pa_hook_slot_free(u->source_new_slot);
417 if (u->source_unlink_slot)
418 pa_hook_slot_free(u->source_unlink_slot);
419 if (u->source_state_changed_slot)
420 pa_hook_slot_free(u->source_state_changed_slot);
421
422 if (u->sink_input_new_slot)
423 pa_hook_slot_free(u->sink_input_new_slot);
424 if (u->sink_input_unlink_slot)
425 pa_hook_slot_free(u->sink_input_unlink_slot);
426 if (u->sink_input_move_slot)
427 pa_hook_slot_free(u->sink_input_move_slot);
428 if (u->sink_input_state_changed_slot)
429 pa_hook_slot_free(u->sink_input_state_changed_slot);
430
431 if (u->source_output_new_slot)
432 pa_hook_slot_free(u->source_output_new_slot);
433 if (u->source_output_unlink_slot)
434 pa_hook_slot_free(u->source_output_unlink_slot);
435 if (u->source_output_move_slot)
436 pa_hook_slot_free(u->source_output_move_slot);
437 if (u->source_output_state_changed_slot)
438 pa_hook_slot_free(u->source_output_state_changed_slot);
439
440 while ((d = pa_hashmap_steal_first(u->device_infos)))
441 device_info_free(d);
442
443 pa_hashmap_free(u->device_infos, NULL, NULL);
444
445 pa_xfree(u);
446 }