release: updated changelog.
[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
37 #ifdef HAVE_PMAPI
38 #include <power.h>
39 #endif
40
41 #include "module-suspend-on-idle-symdef.h"
42
43 PA_MODULE_AUTHOR("Lennart Poettering");
44 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
45 PA_MODULE_VERSION(PACKAGE_VERSION);
46 PA_MODULE_LOAD_ONCE(TRUE);
47 PA_MODULE_USAGE("timeout=<timeout>");
48
49 #ifdef HAVE_PMAPI
50 #define PM_TYPE_SINK   0x01
51 #define PM_TYPE_SOURCE 0x02
52 #define UPDATE_PM_LOCK(current,type)   (current |= type)
53 #define UPDATE_PM_UNLOCK(current,type) (current &= ~type)
54 #endif
55
56 static const char* const valid_modargs[] = {
57     "timeout",
58     NULL,
59 };
60
61 struct userdata {
62     pa_core *core;
63     pa_usec_t timeout;
64     pa_hashmap *device_infos;
65     pa_hook_slot
66         *sink_new_slot,
67         *source_new_slot,
68         *sink_unlink_slot,
69         *source_unlink_slot,
70         *sink_state_changed_slot,
71         *source_state_changed_slot;
72
73     pa_hook_slot
74         *sink_input_new_slot,
75         *source_output_new_slot,
76         *sink_input_unlink_slot,
77         *source_output_unlink_slot,
78         *sink_input_move_start_slot,
79         *source_output_move_start_slot,
80         *sink_input_move_finish_slot,
81         *source_output_move_finish_slot,
82         *sink_input_state_changed_slot,
83         *source_output_state_changed_slot;
84 #ifdef HAVE_PMAPI
85     uint32_t pm_state;
86 #endif
87 };
88
89 struct device_info {
90     struct userdata *userdata;
91     pa_sink *sink;
92     pa_source *source;
93     pa_usec_t last_use;
94     pa_time_event *time_event;
95 };
96
97 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
98     struct device_info *d = userdata;
99
100 #ifdef HAVE_PMAPI
101     int ret = -1;
102 #endif
103
104     pa_assert(d);
105
106     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
107
108     if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
109         pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
110         pa_sink_suspend(d->sink, TRUE, PA_SUSPEND_IDLE);
111         pa_core_maybe_vacuum(d->userdata->core);
112 #ifdef HAVE_PMAPI
113         UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SINK);
114         if(!(d->userdata->pm_state)) {
115             ret = power_unlock_state(POWER_STATE_SCREEN_OFF)
116                 if(!ret)
117                     pa_log_info("sink pm_unlock_state success");
118                 else
119                     pa_log_error("sink pm_unlock_state failed [%d]", ret);
120         }
121 #endif
122     }
123
124     if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
125         pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
126         pa_source_suspend(d->source, TRUE, PA_SUSPEND_IDLE);
127         pa_core_maybe_vacuum(d->userdata->core);
128 #ifdef HAVE_PMAPI
129         UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
130         if(!(d->userdata->pm_state)) {
131             ret = power_unlock_state(POWER_STATE_SCREEN_OFF);
132             if(!ret)
133                 pa_log_info("source pm_unlock_state success");
134             else
135                 pa_log_error("source pm_unlock_state failed [%d]", ret);
136         }
137 #endif
138     }
139 }
140
141 static void restart(struct device_info *d) {
142     pa_usec_t now;
143     const char *s;
144     uint32_t timeout;
145
146 #ifdef HAVE_PMAPI
147     int ret = -1;
148 #endif
149
150     pa_assert(d);
151     pa_assert(d->sink || d->source);
152
153     d->last_use = now = pa_rtclock_now();
154
155     s = pa_proplist_gets(d->sink ? d->sink->proplist : d->source->proplist, "module-suspend-on-idle.timeout");
156     if (!s || pa_atou(s, &timeout) < 0)
157         timeout = d->userdata->timeout;
158
159     pa_core_rttime_restart(d->userdata->core, d->time_event, now + timeout * PA_USEC_PER_SEC);
160
161     if (d->sink) {
162 #ifdef HAVE_PMAPI
163         UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SINK);
164         ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
165         if(!ret)
166             pa_log_info("sink pm_lock_state success");
167         else
168             pa_log_error("sink pm_lock_state failed [%d]", ret);
169 #endif
170         pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d->sink->name, timeout);
171     }
172     if (d->source) {
173 #ifdef HAVE_PMAPI
174         UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
175         ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
176         if(!ret)
177             pa_log_info("source pm_lock_state success");
178         else
179             pa_log_error("source pm_lock_state failed [%d]", ret);
180 #endif
181         pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d->source->name, timeout);
182     }
183 }
184
185 static void resume(struct device_info *d) {
186     pa_assert(d);
187
188     d->userdata->core->mainloop->time_restart(d->time_event, NULL);
189
190     if (d->sink) {
191         pa_sink_suspend(d->sink, FALSE, PA_SUSPEND_IDLE);
192
193         pa_log_debug("Sink %s becomes busy.", d->sink->name);
194     }
195
196     if (d->source) {
197         pa_source_suspend(d->source, FALSE, PA_SUSPEND_IDLE);
198
199         pa_log_debug("Source %s becomes busy.", d->source->name);
200     }
201 }
202
203 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_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     /* We need to resume the audio device here even for
211      * PA_SINK_INPUT_START_CORKED, since we need the device parameters
212      * to be fully available while the stream is set up. */
213
214     if ((d = pa_hashmap_get(u->device_infos, data->sink)))
215         resume(d);
216
217     return PA_HOOK_OK;
218 }
219
220 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
221     struct device_info *d;
222
223     pa_assert(c);
224     pa_assert(data);
225     pa_assert(u);
226
227     if (data->source->monitor_of)
228         d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
229     else
230         d = pa_hashmap_get(u->device_infos, data->source);
231
232     if (d)
233         resume(d);
234
235     return PA_HOOK_OK;
236 }
237
238 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
239     pa_assert(c);
240     pa_sink_input_assert_ref(s);
241     pa_assert(u);
242
243     if (!s->sink)
244         return PA_HOOK_OK;
245
246     if (pa_sink_check_suspend(s->sink) <= 0) {
247         struct device_info *d;
248         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
249             restart(d);
250     }
251
252     return PA_HOOK_OK;
253 }
254
255 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
256     struct device_info *d = NULL;
257
258     pa_assert(c);
259     pa_source_output_assert_ref(s);
260     pa_assert(u);
261
262     if (!s->source)
263         return PA_HOOK_OK;
264
265     if (s->source->monitor_of) {
266         if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
267             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
268     } else {
269         if (pa_source_check_suspend(s->source) <= 0)
270             d = pa_hashmap_get(u->device_infos, s->source);
271     }
272
273     if (d)
274         restart(d);
275
276     return PA_HOOK_OK;
277 }
278
279 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
280     struct device_info *d;
281
282     pa_assert(c);
283     pa_sink_input_assert_ref(s);
284     pa_assert(u);
285
286     if (pa_sink_check_suspend(s->sink) <= 1)
287         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
288             restart(d);
289
290     return PA_HOOK_OK;
291 }
292
293 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
294     struct device_info *d;
295     pa_sink_input_state_t state;
296
297     pa_assert(c);
298     pa_sink_input_assert_ref(s);
299     pa_assert(u);
300
301     state = pa_sink_input_get_state(s);
302     if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
303         return PA_HOOK_OK;
304
305     if ((d = pa_hashmap_get(u->device_infos, s->sink)))
306         resume(d);
307
308     return PA_HOOK_OK;
309 }
310
311 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
312     struct device_info *d = NULL;
313
314     pa_assert(c);
315     pa_source_output_assert_ref(s);
316     pa_assert(u);
317
318     if (s->source->monitor_of) {
319         if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
320             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
321     } else {
322         if (pa_source_check_suspend(s->source) <= 1)
323             d = pa_hashmap_get(u->device_infos, s->source);
324     }
325
326     if (d)
327         restart(d);
328
329     return PA_HOOK_OK;
330 }
331
332 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
333     struct device_info *d;
334
335     pa_assert(c);
336     pa_source_output_assert_ref(s);
337     pa_assert(u);
338
339     if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
340         return PA_HOOK_OK;
341
342     if (s->source->monitor_of)
343         d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
344     else
345         d = pa_hashmap_get(u->device_infos, s->source);
346
347     if (d)
348         resume(d);
349
350     return PA_HOOK_OK;
351 }
352
353 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
354     struct device_info *d;
355     pa_sink_input_state_t state;
356
357     pa_assert(c);
358     pa_sink_input_assert_ref(s);
359     pa_assert(u);
360
361     state = pa_sink_input_get_state(s);
362     if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
363         if ((d = pa_hashmap_get(u->device_infos, s->sink)))
364             resume(d);
365
366     return PA_HOOK_OK;
367 }
368
369 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
370     pa_assert(c);
371     pa_source_output_assert_ref(s);
372     pa_assert(u);
373
374     if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
375         struct device_info *d;
376
377         if (s->source->monitor_of)
378             d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
379         else
380             d = pa_hashmap_get(u->device_infos, s->source);
381
382         if (d)
383             resume(d);
384     }
385
386     return PA_HOOK_OK;
387 }
388
389 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
390     struct device_info *d;
391     pa_source *source;
392     pa_sink *sink;
393
394     pa_assert(c);
395     pa_object_assert_ref(o);
396     pa_assert(u);
397
398     source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
399     sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
400
401     /* Never suspend monitors */
402     if (source && source->monitor_of)
403         return PA_HOOK_OK;
404
405     pa_assert(source || sink);
406
407     d = pa_xnew(struct device_info, 1);
408     d->userdata = u;
409     d->source = source ? pa_source_ref(source) : NULL;
410     d->sink = sink ? pa_sink_ref(sink) : NULL;
411     d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
412     pa_hashmap_put(u->device_infos, o, d);
413
414     if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
415         (d->source && pa_source_check_suspend(d->source) <= 0))
416         restart(d);
417
418     return PA_HOOK_OK;
419 }
420
421 static void device_info_free(struct device_info *d) {
422     pa_assert(d);
423
424     if (d->source)
425         pa_source_unref(d->source);
426     if (d->sink)
427         pa_sink_unref(d->sink);
428
429     d->userdata->core->mainloop->time_free(d->time_event);
430
431     pa_xfree(d);
432 }
433
434 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
435     struct device_info *d;
436
437     pa_assert(c);
438     pa_object_assert_ref(o);
439     pa_assert(u);
440
441     if ((d = pa_hashmap_remove(u->device_infos, o)))
442         device_info_free(d);
443
444     return PA_HOOK_OK;
445 }
446
447 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
448     struct device_info *d;
449
450     pa_assert(c);
451     pa_object_assert_ref(o);
452     pa_assert(u);
453
454     if (!(d = pa_hashmap_get(u->device_infos, o)))
455         return PA_HOOK_OK;
456
457     if (pa_sink_isinstance(o)) {
458         pa_sink *s = PA_SINK(o);
459         pa_sink_state_t state = pa_sink_get_state(s);
460
461         if (pa_sink_check_suspend(s) <= 0)
462             if (PA_SINK_IS_OPENED(state))
463                 restart(d);
464
465     } else if (pa_source_isinstance(o)) {
466         pa_source *s = PA_SOURCE(o);
467         pa_source_state_t state = pa_source_get_state(s);
468
469         if (pa_source_check_suspend(s) <= 0)
470             if (PA_SOURCE_IS_OPENED(state))
471                 restart(d);
472     }
473
474     return PA_HOOK_OK;
475 }
476
477 int pa__init(pa_module*m) {
478     pa_modargs *ma = NULL;
479     struct userdata *u;
480     uint32_t timeout = 5;
481     uint32_t idx;
482     pa_sink *sink;
483     pa_source *source;
484
485     pa_assert(m);
486
487     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
488         pa_log("Failed to parse module arguments.");
489         goto fail;
490     }
491
492     if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
493         pa_log("Failed to parse timeout value.");
494         goto fail;
495     }
496
497     m->userdata = u = pa_xnew(struct userdata, 1);
498     u->core = m->core;
499     u->timeout = timeout;
500     u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
501
502 #ifdef HAVE_PMAPI
503     u->pm_state = 0x00;
504 #endif
505
506     for (sink = pa_idxset_first(m->core->sinks, &idx); sink; sink = pa_idxset_next(m->core->sinks, &idx))
507         device_new_hook_cb(m->core, PA_OBJECT(sink), u);
508
509     for (source = pa_idxset_first(m->core->sources, &idx); source; source = pa_idxset_next(m->core->sources, &idx))
510         device_new_hook_cb(m->core, PA_OBJECT(source), u);
511
512     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);
513     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);
514     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);
515     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);
516     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);
517     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);
518
519     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);
520     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);
521     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);
522     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);
523     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);
524     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);
525     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);
526     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);
527     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);
528     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);
529
530     pa_modargs_free(ma);
531     return 0;
532
533 fail:
534
535     if (ma)
536         pa_modargs_free(ma);
537
538     return -1;
539 }
540
541 void pa__done(pa_module*m) {
542     struct userdata *u;
543     struct device_info *d;
544
545     pa_assert(m);
546
547     if (!m->userdata)
548         return;
549
550     u = m->userdata;
551
552     if (u->sink_new_slot)
553         pa_hook_slot_free(u->sink_new_slot);
554     if (u->sink_unlink_slot)
555         pa_hook_slot_free(u->sink_unlink_slot);
556     if (u->sink_state_changed_slot)
557         pa_hook_slot_free(u->sink_state_changed_slot);
558
559     if (u->source_new_slot)
560         pa_hook_slot_free(u->source_new_slot);
561     if (u->source_unlink_slot)
562         pa_hook_slot_free(u->source_unlink_slot);
563     if (u->source_state_changed_slot)
564         pa_hook_slot_free(u->source_state_changed_slot);
565
566     if (u->sink_input_new_slot)
567         pa_hook_slot_free(u->sink_input_new_slot);
568     if (u->sink_input_unlink_slot)
569         pa_hook_slot_free(u->sink_input_unlink_slot);
570     if (u->sink_input_move_start_slot)
571         pa_hook_slot_free(u->sink_input_move_start_slot);
572     if (u->sink_input_move_finish_slot)
573         pa_hook_slot_free(u->sink_input_move_finish_slot);
574     if (u->sink_input_state_changed_slot)
575         pa_hook_slot_free(u->sink_input_state_changed_slot);
576
577     if (u->source_output_new_slot)
578         pa_hook_slot_free(u->source_output_new_slot);
579     if (u->source_output_unlink_slot)
580         pa_hook_slot_free(u->source_output_unlink_slot);
581     if (u->source_output_move_start_slot)
582         pa_hook_slot_free(u->source_output_move_start_slot);
583     if (u->source_output_move_finish_slot)
584         pa_hook_slot_free(u->source_output_move_finish_slot);
585     if (u->source_output_state_changed_slot)
586         pa_hook_slot_free(u->source_output_state_changed_slot);
587
588     while ((d = pa_hashmap_steal_first(u->device_infos)))
589         device_info_free(d);
590
591     pa_hashmap_free(u->device_infos, NULL, NULL);
592
593     pa_xfree(u);
594 }