]> code.delx.au - pulseaudio/blob - src/modules/module-suspend-on-idle.c
Implement new flags DONT_INHIBIT_AUTO_SUSPEND and START_UNMUTED
[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 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
29 #include <pulsecore/core.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/source-output.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/namereg.h>
35
36 #include "module-suspend-on-idle-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(TRUE);
42
43 static const char* const valid_modargs[] = {
44 "timeout",
45 NULL,
46 };
47
48 struct userdata {
49 pa_core *core;
50 pa_usec_t timeout;
51 pa_hashmap *device_infos;
52 pa_hook_slot
53 *sink_new_slot,
54 *source_new_slot,
55 *sink_unlink_slot,
56 *source_unlink_slot,
57 *sink_state_changed_slot,
58 *source_state_changed_slot;
59
60 pa_hook_slot
61 *sink_input_new_slot,
62 *source_output_new_slot,
63 *sink_input_unlink_slot,
64 *source_output_unlink_slot,
65 *sink_input_move_slot,
66 *source_output_move_slot,
67 *sink_input_state_changed_slot,
68 *source_output_state_changed_slot;
69 };
70
71 struct device_info {
72 struct userdata *userdata;
73 pa_sink *sink;
74 pa_source *source;
75 struct timeval last_use;
76 pa_time_event *time_event;
77 };
78
79 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
80 struct device_info *d = userdata;
81
82 pa_assert(d);
83
84 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
85
86 if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && pa_sink_get_state(d->sink) != PA_SINK_SUSPENDED) {
87 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
88 pa_sink_suspend(d->sink, TRUE);
89 }
90
91 if (d->source && pa_source_check_suspend(d->source) <= 0 && pa_source_get_state(d->source) != PA_SOURCE_SUSPENDED) {
92 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
93 pa_source_suspend(d->source, TRUE);
94 }
95 }
96
97 static void restart(struct device_info *d) {
98 struct timeval tv;
99 pa_assert(d);
100
101 pa_gettimeofday(&tv);
102 d->last_use = tv;
103 pa_timeval_add(&tv, d->userdata->timeout*1000000);
104 d->userdata->core->mainloop->time_restart(d->time_event, &tv);
105
106 if (d->sink)
107 pa_log_debug("Sink %s becomes idle.", d->sink->name);
108 if (d->source)
109 pa_log_debug("Source %s becomes idle.", d->source->name);
110 }
111
112 static void resume(struct device_info *d) {
113 pa_assert(d);
114
115 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
116
117 if (d->sink) {
118 pa_sink_suspend(d->sink, FALSE);
119
120 pa_log_debug("Sink %s becomes busy.", d->sink->name);
121 }
122
123 if (d->source) {
124 pa_source_suspend(d->source, FALSE);
125
126 pa_log_debug("Source %s becomes busy.", d->source->name);
127 }
128 }
129
130 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
131 struct device_info *d;
132
133 pa_assert(c);
134 pa_assert(data);
135 pa_assert(u);
136
137 if ((d = pa_hashmap_get(u->device_infos, data->sink)))
138 resume(d);
139
140 return PA_HOOK_OK;
141 }
142
143 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
144 struct device_info *d;
145
146 pa_assert(c);
147 pa_assert(data);
148 pa_assert(u);
149
150 if ((d = pa_hashmap_get(u->device_infos, data->source)))
151 resume(d);
152
153 return PA_HOOK_OK;
154 }
155
156 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
157 pa_assert(c);
158 pa_sink_input_assert_ref(s);
159 pa_assert(u);
160
161 if (pa_sink_check_suspend(s->sink) <= 0) {
162 struct device_info *d;
163 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
164 restart(d);
165 }
166
167 return PA_HOOK_OK;
168 }
169
170 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
171 pa_assert(c);
172 pa_source_output_assert_ref(s);
173 pa_assert(u);
174
175 if (pa_source_check_suspend(s->source) <= 0) {
176 struct device_info *d;
177 if ((d = pa_hashmap_get(u->device_infos, s->source)))
178 restart(d);
179 }
180
181 return PA_HOOK_OK;
182 }
183
184 static pa_hook_result_t sink_input_move_hook_cb(pa_core *c, pa_sink_input_move_hook_data *data, struct userdata *u) {
185 struct device_info *d;
186
187 pa_assert(c);
188 pa_assert(data);
189 pa_assert(u);
190
191 if ((d = pa_hashmap_get(u->device_infos, data->destination)))
192 resume(d);
193
194 if (pa_sink_check_suspend(data->sink_input->sink) <= 1)
195 if ((d = pa_hashmap_get(u->device_infos, data->sink_input->sink)))
196 restart(d);
197
198 return PA_HOOK_OK;
199 }
200
201 static pa_hook_result_t source_output_move_hook_cb(pa_core *c, pa_source_output_move_hook_data *data, struct userdata *u) {
202 struct device_info *d;
203
204 pa_assert(c);
205 pa_assert(data);
206 pa_assert(u);
207
208 if ((d = pa_hashmap_get(u->device_infos, data->destination)))
209 resume(d);
210
211 if (pa_source_check_suspend(data->source_output->source) <= 1)
212 if ((d = pa_hashmap_get(u->device_infos, data->source_output->source)))
213 restart(d);
214
215 return PA_HOOK_OK;
216 }
217
218 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
219 struct device_info *d;
220 pa_sink_input_state_t state;
221 pa_assert(c);
222 pa_sink_input_assert_ref(s);
223 pa_assert(u);
224
225 state = pa_sink_input_get_state(s);
226 if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
227 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
228 resume(d);
229
230 return PA_HOOK_OK;
231 }
232
233 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
234 struct device_info *d;
235 pa_source_output_state_t state;
236 pa_assert(c);
237 pa_source_output_assert_ref(s);
238 pa_assert(u);
239
240 state = pa_source_output_get_state(s);
241 if (state == PA_SOURCE_OUTPUT_RUNNING)
242 if ((d = pa_hashmap_get(u->device_infos, s->source)))
243 resume(d);
244
245 return PA_HOOK_OK;
246 }
247
248 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
249 struct device_info *d;
250 pa_source *source;
251 pa_sink *sink;
252
253 pa_assert(c);
254 pa_object_assert_ref(o);
255 pa_assert(u);
256
257 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
258 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
259
260 pa_assert(source || sink);
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_check_suspend(d->sink) <= 0) ||
270 (d->source && pa_source_check_suspend(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 pa_sink_state_t state = pa_sink_get_state(s);
315
316 if (pa_sink_check_suspend(s) <= 0) {
317
318 if (PA_SINK_IS_OPENED(state))
319 restart(d);
320
321 }
322
323 } else if (pa_source_isinstance(o)) {
324 pa_source *s = PA_SOURCE(o);
325 pa_source_state_t state = pa_source_get_state(s);
326
327 if (pa_source_check_suspend(s) <= 0) {
328
329 if (PA_SOURCE_IS_OPENED(state))
330 restart(d);
331 }
332 }
333
334 return PA_HOOK_OK;
335 }
336
337 int pa__init(pa_module*m) {
338 pa_modargs *ma = NULL;
339 struct userdata *u;
340 uint32_t timeout = 5;
341 uint32_t idx;
342 pa_sink *sink;
343 pa_source *source;
344
345 pa_assert(m);
346
347 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
348 pa_log("Failed to parse module arguments.");
349 goto fail;
350 }
351
352 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
353 pa_log("Failed to parse timeout value.");
354 goto fail;
355 }
356
357 m->userdata = u = pa_xnew(struct userdata, 1);
358 u->core = m->core;
359 u->timeout = timeout;
360 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
361
362 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
363 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
364
365 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
366 device_new_hook_cb(m->core, PA_OBJECT(source), u);
367
368 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);
369 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);
370 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);
371 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);
372 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);
373 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);
374
375 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);
376 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);
377 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);
378 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);
379 u->sink_input_move_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_hook_cb, u);
380 u->source_output_move_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_hook_cb, u);
381 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);
382 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);
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_state_changed_slot)
427 pa_hook_slot_free(u->sink_input_state_changed_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_state_changed_slot)
436 pa_hook_slot_free(u->source_output_state_changed_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 }