Add "Rear Mic" to alsa mixer paths.
[profile/ivi/pulseaudio.git] / 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.1 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 #include <pulse/rtclock.h>
29
30 #include <pulsecore/core.h>
31 #include <pulsecore/core-util.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_start_slot,
68         *source_output_move_start_slot,
69         *sink_input_move_finish_slot,
70         *source_output_move_finish_slot,
71         *sink_input_state_changed_slot,
72         *source_output_state_changed_slot;
73 };
74
75 struct device_info {
76     struct userdata *userdata;
77     pa_sink *sink;
78     pa_source *source;
79     pa_usec_t last_use;
80     pa_time_event *time_event;
81 };
82
83 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
84     struct device_info *d = userdata;
85
86     pa_assert(d);
87
88     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
89
90     if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
91         pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
92         pa_sink_suspend(d->sink, TRUE, PA_SUSPEND_IDLE);
93     }
94
95     if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
96         pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
97         pa_source_suspend(d->source, TRUE, PA_SUSPEND_IDLE);
98     }
99 }
100
101 static void restart(struct device_info *d) {
102     pa_usec_t now;
103     const char *s;
104     uint32_t timeout;
105
106     pa_assert(d);
107     pa_assert(d->sink || d->source);
108
109     d->last_use = now = pa_rtclock_now();
110
111     s = pa_proplist_gets(d->sink ? d->sink->proplist : d->source->proplist, "module-suspend-on-idle.timeout");
112     if (!s || pa_atou(s, &timeout) < 0)
113         timeout = d->userdata->timeout;
114
115     pa_core_rttime_restart(d->userdata->core, d->time_event, now + timeout * PA_USEC_PER_SEC);
116
117     if (d->sink)
118         pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d->sink->name, timeout);
119     if (d->source)
120         pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d->source->name, timeout);
121 }
122
123 static void resume(struct device_info *d) {
124     pa_assert(d);
125
126     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
127
128     if (d->sink) {
129         pa_sink_suspend(d->sink, FALSE, PA_SUSPEND_IDLE);
130
131         pa_log_debug("Sink %s becomes busy.", d->sink->name);
132     }
133
134     if (d->source) {
135         pa_source_suspend(d->source, FALSE, PA_SUSPEND_IDLE);
136
137         pa_log_debug("Source %s becomes busy.", d->source->name);
138     }
139 }
140
141 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
142     struct device_info *d;
143
144     pa_assert(c);
145     pa_assert(data);
146     pa_assert(u);
147
148     /* We need to resume the audio device here even for
149      * PA_SINK_INPUT_START_CORKED, since we need the device parameters
150      * to be fully available while the stream is set up. */
151
152     if ((d = pa_hashmap_get(u->device_infos, data->sink)))
153         resume(d);
154
155     return PA_HOOK_OK;
156 }
157
158 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
159     struct device_info *d;
160
161     pa_assert(c);
162     pa_assert(data);
163     pa_assert(u);
164
165     if (data->source->monitor_of)
166         d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
167     else
168         d = pa_hashmap_get(u->device_infos, data->source);
169
170     if (d)
171         resume(d);
172
173     return PA_HOOK_OK;
174 }
175
176 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
177     pa_assert(c);
178     pa_sink_input_assert_ref(s);
179     pa_assert(u);
180
181     if (!s->sink)
182         return PA_HOOK_OK;
183
184     if (pa_sink_check_suspend(s->sink) <= 0) {
185         struct device_info *d;
186         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
187             restart(d);
188     }
189
190     return PA_HOOK_OK;
191 }
192
193 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
194     struct device_info *d = NULL;
195
196     pa_assert(c);
197     pa_source_output_assert_ref(s);
198     pa_assert(u);
199
200     if (!s->source)
201         return PA_HOOK_OK;
202
203     if (s->source->monitor_of) {
204         if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
205             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
206     } else {
207         if (pa_source_check_suspend(s->source) <= 0)
208             d = pa_hashmap_get(u->device_infos, s->source);
209     }
210
211     if (d)
212         restart(d);
213
214     return PA_HOOK_OK;
215 }
216
217 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
218     struct device_info *d;
219
220     pa_assert(c);
221     pa_sink_input_assert_ref(s);
222     pa_assert(u);
223
224     if (pa_sink_check_suspend(s->sink) <= 1)
225         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
226             restart(d);
227
228     return PA_HOOK_OK;
229 }
230
231 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
232     struct device_info *d;
233     pa_sink_input_state_t state;
234
235     pa_assert(c);
236     pa_sink_input_assert_ref(s);
237     pa_assert(u);
238
239     state = pa_sink_input_get_state(s);
240     if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
241         return PA_HOOK_OK;
242
243     if ((d = pa_hashmap_get(u->device_infos, s->sink)))
244         resume(d);
245
246     return PA_HOOK_OK;
247 }
248
249 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
250     struct device_info *d = NULL;
251
252     pa_assert(c);
253     pa_source_output_assert_ref(s);
254     pa_assert(u);
255
256     if (s->source->monitor_of) {
257         if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
258             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
259     } else {
260         if (pa_source_check_suspend(s->source) <= 1)
261             d = pa_hashmap_get(u->device_infos, s->source);
262     }
263
264     if (d)
265         restart(d);
266
267     return PA_HOOK_OK;
268 }
269
270 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
271     struct device_info *d;
272
273     pa_assert(c);
274     pa_source_output_assert_ref(s);
275     pa_assert(u);
276
277     if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
278         return PA_HOOK_OK;
279
280     if (s->source->monitor_of)
281         d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
282     else
283         d = pa_hashmap_get(u->device_infos, s->source);
284
285     if (d)
286         resume(d);
287
288     return PA_HOOK_OK;
289 }
290
291 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
292     struct device_info *d;
293     pa_sink_input_state_t state;
294
295     pa_assert(c);
296     pa_sink_input_assert_ref(s);
297     pa_assert(u);
298
299     state = pa_sink_input_get_state(s);
300     if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
301         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
302             resume(d);
303
304     return PA_HOOK_OK;
305 }
306
307 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
308     pa_assert(c);
309     pa_source_output_assert_ref(s);
310     pa_assert(u);
311
312     if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
313         struct device_info *d;
314
315         if (s->source->monitor_of)
316             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
317         else
318             d = pa_hashmap_get(u->device_infos, s->source);
319
320         if (d)
321             resume(d);
322     }
323
324     return PA_HOOK_OK;
325 }
326
327 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
328     struct device_info *d;
329     pa_source *source;
330     pa_sink *sink;
331
332     pa_assert(c);
333     pa_object_assert_ref(o);
334     pa_assert(u);
335
336     source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
337     sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
338
339     /* Never suspend monitors */
340     if (source && source->monitor_of)
341         return PA_HOOK_OK;
342
343     pa_assert(source || sink);
344
345     d = pa_xnew(struct device_info, 1);
346     d->userdata = u;
347     d->source = source ? pa_source_ref(source) : NULL;
348     d->sink = sink ? pa_sink_ref(sink) : NULL;
349     d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
350     pa_hashmap_put(u->device_infos, o, d);
351
352     if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
353         (d->source && pa_source_check_suspend(d->source) <= 0))
354         restart(d);
355
356     return PA_HOOK_OK;
357 }
358
359 static void device_info_free(struct device_info *d) {
360     pa_assert(d);
361
362     if (d->source)
363         pa_source_unref(d->source);
364     if (d->sink)
365         pa_sink_unref(d->sink);
366
367     d->userdata->core->mainloop->time_free(d->time_event);
368
369     pa_xfree(d);
370 }
371
372 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
373     struct device_info *d;
374
375     pa_assert(c);
376     pa_object_assert_ref(o);
377     pa_assert(u);
378
379     if ((d = pa_hashmap_remove(u->device_infos, o)))
380         device_info_free(d);
381
382     return PA_HOOK_OK;
383 }
384
385 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
386     struct device_info *d;
387
388     pa_assert(c);
389     pa_object_assert_ref(o);
390     pa_assert(u);
391
392     if (!(d = pa_hashmap_get(u->device_infos, o)))
393         return PA_HOOK_OK;
394
395     if (pa_sink_isinstance(o)) {
396         pa_sink *s = PA_SINK(o);
397         pa_sink_state_t state = pa_sink_get_state(s);
398
399         if (pa_sink_check_suspend(s) <= 0)
400             if (PA_SINK_IS_OPENED(state))
401                 restart(d);
402
403     } else if (pa_source_isinstance(o)) {
404         pa_source *s = PA_SOURCE(o);
405         pa_source_state_t state = pa_source_get_state(s);
406
407         if (pa_source_check_suspend(s) <= 0)
408             if (PA_SOURCE_IS_OPENED(state))
409                 restart(d);
410     }
411
412     return PA_HOOK_OK;
413 }
414
415 int pa__init(pa_module*m) {
416     pa_modargs *ma = NULL;
417     struct userdata *u;
418     uint32_t timeout = 5;
419     uint32_t idx;
420     pa_sink *sink;
421     pa_source *source;
422
423     pa_assert(m);
424
425     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
426         pa_log("Failed to parse module arguments.");
427         goto fail;
428     }
429
430     if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
431         pa_log("Failed to parse timeout value.");
432         goto fail;
433     }
434
435     m->userdata = u = pa_xnew(struct userdata, 1);
436     u->core = m->core;
437     u->timeout = timeout;
438     u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
439
440     for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
441         device_new_hook_cb(m->core, PA_OBJECT(sink), u);
442
443     for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
444         device_new_hook_cb(m->core, PA_OBJECT(source), u);
445
446     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);
447     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);
448     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);
449     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);
450     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);
451     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);
452
453     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);
454     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);
455     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);
456     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);
457     u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_start_hook_cb, u);
458     u->source_output_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_start_hook_cb, u);
459     u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_finish_hook_cb, u);
460     u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_finish_hook_cb, u);
461     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);
462     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);
463
464     pa_modargs_free(ma);
465     return 0;
466
467 fail:
468
469     if (ma)
470         pa_modargs_free(ma);
471
472     return -1;
473 }
474
475 void pa__done(pa_module*m) {
476     struct userdata *u;
477     struct device_info *d;
478
479     pa_assert(m);
480
481     if (!m->userdata)
482         return;
483
484     u = m->userdata;
485
486     if (u->sink_new_slot)
487         pa_hook_slot_free(u->sink_new_slot);
488     if (u->sink_unlink_slot)
489         pa_hook_slot_free(u->sink_unlink_slot);
490     if (u->sink_state_changed_slot)
491         pa_hook_slot_free(u->sink_state_changed_slot);
492
493     if (u->source_new_slot)
494         pa_hook_slot_free(u->source_new_slot);
495     if (u->source_unlink_slot)
496         pa_hook_slot_free(u->source_unlink_slot);
497     if (u->source_state_changed_slot)
498         pa_hook_slot_free(u->source_state_changed_slot);
499
500     if (u->sink_input_new_slot)
501         pa_hook_slot_free(u->sink_input_new_slot);
502     if (u->sink_input_unlink_slot)
503         pa_hook_slot_free(u->sink_input_unlink_slot);
504     if (u->sink_input_move_start_slot)
505         pa_hook_slot_free(u->sink_input_move_start_slot);
506     if (u->sink_input_move_finish_slot)
507         pa_hook_slot_free(u->sink_input_move_finish_slot);
508     if (u->sink_input_state_changed_slot)
509         pa_hook_slot_free(u->sink_input_state_changed_slot);
510
511     if (u->source_output_new_slot)
512         pa_hook_slot_free(u->source_output_new_slot);
513     if (u->source_output_unlink_slot)
514         pa_hook_slot_free(u->source_output_unlink_slot);
515     if (u->source_output_move_start_slot)
516         pa_hook_slot_free(u->source_output_move_start_slot);
517     if (u->source_output_move_finish_slot)
518         pa_hook_slot_free(u->source_output_move_finish_slot);
519     if (u->source_output_state_changed_slot)
520         pa_hook_slot_free(u->source_output_state_changed_slot);
521
522     while ((d = pa_hashmap_steal_first(u->device_infos)))
523         device_info_free(d);
524
525     pa_hashmap_free(u->device_infos, NULL, NULL);
526
527     pa_xfree(u);
528 }