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