]> code.delx.au - pulseaudio/blob - src/modules/module-suspend-on-idle.c
use single array for storing pa_core hook lists, add sink state changed hook, drop...
[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 *sink_new_slot, *source_new_slot, *sink_disconnect_slot, *source_disconnect_slot, *sink_state_changed_slot, *source_state_changed_slot;
54 pa_hook_slot *sink_input_new_slot, *source_output_new_slot, *sink_input_disconnect_slot, *source_output_disconnect_slot;
55 };
56
57 struct device_info {
58 struct userdata *userdata;
59 pa_sink *sink;
60 pa_source *source;
61 struct timeval last_use;
62 pa_time_event *time_event;
63 };
64
65 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
66 struct device_info *d = userdata;
67
68 pa_assert(d);
69
70 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
71
72 if (d->sink && pa_sink_used_by(d->sink) <= 0 && pa_sink_get_state(d->sink) != PA_SINK_SUSPENDED) {
73 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
74 pa_sink_suspend(d->sink, 1);
75 pa_source_suspend(d->sink->monitor_source, 1);
76 }
77
78 if (d->source && pa_source_used_by(d->source) <= 0 && pa_source_get_state(d->source) != PA_SOURCE_SUSPENDED) {
79 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
80 pa_source_suspend(d->source, 1);
81 }
82 }
83
84 static void restart(struct device_info *d) {
85 struct timeval tv;
86 pa_assert(d);
87
88 pa_gettimeofday(&tv);
89 d->last_use = tv;
90 pa_timeval_add(&tv, d->userdata->timeout*1000000);
91 d->userdata->core->mainloop->time_restart(d->time_event, &tv);
92
93 if (d->source)
94 pa_log_debug("Source %s becomes idle.", d->source->name);
95 if (d->sink)
96 pa_log_debug("Sink %s becomes idle.", d->sink->name);
97 }
98
99 static pa_hook_result_t sink_input_new_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
100 struct device_info *d;
101
102 pa_assert(c);
103 pa_sink_input_assert_ref(s);
104 pa_assert(u);
105
106 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->sink)));
107 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
108
109 pa_sink_suspend(s->sink, 0);
110 pa_source_suspend(s->sink->monitor_source, 0);
111
112 pa_log_debug("Sink %s becomes busy.", s->sink->name);
113
114 return PA_HOOK_OK;
115 }
116
117 static pa_hook_result_t source_output_new_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
118 struct device_info *d;
119
120 pa_assert(c);
121 pa_source_output_assert_ref(s);
122 pa_assert(u);
123
124 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->source)));
125 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
126
127 pa_source_suspend(s->source, 0);
128 if (s->source->monitor_of)
129 pa_sink_suspend(s->source->monitor_of, 0);
130
131 pa_log_debug("Source %s becomes busy.", s->source->name);
132
133 return PA_HOOK_OK;
134 }
135
136 static pa_hook_result_t sink_input_disconnect_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
137 pa_assert(c);
138 pa_sink_input_assert_ref(s);
139 pa_assert(u);
140
141 if (pa_sink_used_by(s->sink) <= 0) {
142 struct device_info *d;
143 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->sink)));
144 restart(d);
145 }
146
147 return PA_HOOK_OK;
148 }
149
150 static pa_hook_result_t source_output_disconnect_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
151 pa_assert(c);
152 pa_source_output_assert_ref(s);
153 pa_assert(u);
154
155 if (pa_source_used_by(s->source) <= 0) {
156 struct device_info *d;
157 pa_assert_se((d = pa_hashmap_get(u->device_infos, s->source)));
158 restart(d);
159 }
160
161 return PA_HOOK_OK;
162 }
163
164 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
165 struct device_info *d;
166
167 pa_assert(c);
168 pa_object_assert_ref(o);
169 pa_assert(u);
170
171 d = pa_xnew(struct device_info, 1);
172 d->userdata = u;
173 d->source = pa_source_isinstance(o) ? pa_source_ref(PA_SOURCE(o)) : NULL;
174 d->sink = pa_sink_isinstance(o) ? pa_sink_ref(PA_SINK(o)) : NULL;
175 pa_assert(d->source || d->sink);
176 d->time_event = c->mainloop->time_new(c->mainloop, NULL, timeout_cb, d);
177 pa_hashmap_put(u->device_infos, o, d);
178
179 if ((d->sink && pa_sink_used_by(d->sink) <= 0) ||
180 (d->source && pa_source_used_by(d->source) <= 0))
181 restart(d);
182
183 return PA_HOOK_OK;
184 }
185
186 static void device_info_free(struct device_info *d) {
187 pa_assert(d);
188
189 if (d->source)
190 pa_source_unref(d->source);
191 if (d->sink)
192 pa_sink_unref(d->sink);
193
194 d->userdata->core->mainloop->time_free(d->time_event);
195
196 pa_xfree(d);
197 }
198
199 static pa_hook_result_t device_disconnect_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
200 struct device_info *d;
201
202 pa_assert(c);
203 pa_object_assert_ref(o);
204 pa_assert(u);
205
206 pa_assert_se((d = pa_hashmap_remove(u->device_infos, o)));
207 device_info_free(d);
208
209 return PA_HOOK_OK;
210 }
211
212 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
213 struct device_info *d;
214
215 pa_assert(c);
216 pa_object_assert_ref(o);
217 pa_assert(u);
218
219 if (!(d = pa_hashmap_get(u->device_infos, o)))
220 return PA_HOOK_OK;
221
222 if (pa_sink_isinstance(o)) {
223 pa_sink *s = PA_SINK(o);
224
225 if (pa_sink_used_by(s) <= 0) {
226 pa_sink_state_t state = pa_sink_get_state(s);
227
228 if (state == PA_SINK_RUNNING || state == PA_SINK_IDLE)
229 restart(d);
230 }
231
232 } else if (pa_source_isinstance(o)) {
233 pa_source *s = PA_SOURCE(o);
234
235 if (pa_source_used_by(s) <= 0) {
236 pa_sink_state_t state = pa_source_get_state(s);
237
238 if (state == PA_SINK_RUNNING || state == PA_SINK_IDLE)
239 restart(d);
240 }
241 }
242
243 return PA_HOOK_OK;
244 }
245
246 int pa__init(pa_module*m) {
247 pa_modargs *ma = NULL;
248 struct userdata *u;
249 uint32_t timeout = 5;
250 uint32_t idx;
251 pa_sink *sink;
252 pa_source *source;
253
254 pa_assert(m);
255
256 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
257 pa_log("Failed to parse module arguments.");
258 goto fail;
259 }
260
261 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
262 pa_log("Failed to parse timeout value.");
263 goto fail;
264 }
265
266 m->userdata = u = pa_xnew(struct userdata, 1);
267 u->core = m->core;
268 u->timeout = timeout;
269 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
270
271 for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
272 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
273
274 for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
275 device_new_hook_cb(m->core, PA_OBJECT(source), u);
276
277 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);
278 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);
279 u->sink_disconnect_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_DISCONNECT], (pa_hook_cb_t) device_disconnect_hook_cb, u);
280 u->source_disconnect_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_DISCONNECT], (pa_hook_cb_t) device_disconnect_hook_cb, u);
281 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);
282 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);
283
284 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);
285 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);
286 u->sink_input_disconnect_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_DISCONNECT_POST], (pa_hook_cb_t) sink_input_disconnect_hook_cb, u);
287 u->source_output_disconnect_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_DISCONNECT_POST], (pa_hook_cb_t) source_output_disconnect_hook_cb, u);
288
289 pa_modargs_free(ma);
290 return 0;
291
292 fail:
293
294 if (ma)
295 pa_modargs_free(ma);
296
297 return -1;
298 }
299
300 void pa__done(pa_module*m) {
301 struct userdata *u;
302 struct device_info *d;
303
304 pa_assert(m);
305
306 if (!m->userdata)
307 return;
308
309 u = m->userdata;
310
311 if (u->sink_new_slot)
312 pa_hook_slot_free(u->sink_new_slot);
313 if (u->sink_disconnect_slot)
314 pa_hook_slot_free(u->sink_disconnect_slot);
315 if (u->sink_state_changed_slot)
316 pa_hook_slot_free(u->sink_state_changed_slot);
317
318 if (u->source_new_slot)
319 pa_hook_slot_free(u->source_new_slot);
320 if (u->source_disconnect_slot)
321 pa_hook_slot_free(u->source_disconnect_slot);
322 if (u->source_state_changed_slot)
323 pa_hook_slot_free(u->source_state_changed_slot);
324
325 if (u->sink_input_new_slot)
326 pa_hook_slot_free(u->sink_input_new_slot);
327 if (u->sink_input_disconnect_slot)
328 pa_hook_slot_free(u->sink_input_disconnect_slot);
329
330 if (u->source_output_new_slot)
331 pa_hook_slot_free(u->source_output_new_slot);
332 if (u->source_output_disconnect_slot)
333 pa_hook_slot_free(u->source_output_disconnect_slot);
334
335 while ((d = pa_hashmap_steal_first(u->device_infos)))
336 device_info_free(d);
337
338 pa_hashmap_free(u->device_infos, NULL, NULL);
339
340 pa_xfree(u);
341 }