2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
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.
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.
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
26 #include <pulse/xmalloc.h>
27 #include <pulse/timeval.h>
28 #include <pulse/rtclock.h>
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>
41 #include "module-suspend-on-idle-symdef.h"
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>");
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)
56 static const char* const valid_modargs[] = {
64 pa_hashmap *device_infos;
70 *sink_state_changed_slot,
71 *source_state_changed_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;
90 struct userdata *userdata;
94 pa_time_event *time_event;
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;
106 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
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);
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)
117 pa_log_info("sink pm_unlock_state success");
119 pa_log_error("sink pm_unlock_state failed [%d]", ret);
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);
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);
133 pa_log_info("source pm_unlock_state success");
135 pa_log_error("source pm_unlock_state failed [%d]", ret);
141 static void restart(struct device_info *d) {
151 pa_assert(d->sink || d->source);
153 d->last_use = now = pa_rtclock_now();
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;
159 pa_core_rttime_restart(d->userdata->core, d->time_event, now + timeout * PA_USEC_PER_SEC);
163 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SINK);
164 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
166 pa_log_info("sink pm_lock_state success");
168 pa_log_error("sink pm_lock_state failed [%d]", ret);
170 pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d->sink->name, timeout);
174 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
175 ret = power_lock_state(POWER_STATE_SCREEN_OFF, 0);
177 pa_log_info("source pm_lock_state success");
179 pa_log_error("source pm_lock_state failed [%d]", ret);
181 pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d->source->name, timeout);
185 static void resume(struct device_info *d) {
188 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
191 pa_sink_suspend(d->sink, FALSE, PA_SUSPEND_IDLE);
193 pa_log_debug("Sink %s becomes busy.", d->sink->name);
197 pa_source_suspend(d->source, FALSE, PA_SUSPEND_IDLE);
199 pa_log_debug("Source %s becomes busy.", d->source->name);
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;
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. */
214 if ((d = pa_hashmap_get(u->device_infos, data->sink)))
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;
227 if (data->source->monitor_of)
228 d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
230 d = pa_hashmap_get(u->device_infos, data->source);
238 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
240 pa_sink_input_assert_ref(s);
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)))
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;
259 pa_source_output_assert_ref(s);
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);
269 if (pa_source_check_suspend(s->source) <= 0)
270 d = pa_hashmap_get(u->device_infos, s->source);
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;
283 pa_sink_input_assert_ref(s);
286 if (pa_sink_check_suspend(s->sink) <= 1)
287 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
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;
298 pa_sink_input_assert_ref(s);
301 state = pa_sink_input_get_state(s);
302 if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
305 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
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;
315 pa_source_output_assert_ref(s);
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);
322 if (pa_source_check_suspend(s->source) <= 1)
323 d = pa_hashmap_get(u->device_infos, s->source);
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;
336 pa_source_output_assert_ref(s);
339 if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
342 if (s->source->monitor_of)
343 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
345 d = pa_hashmap_get(u->device_infos, s->source);
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;
358 pa_sink_input_assert_ref(s);
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)))
369 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
371 pa_source_output_assert_ref(s);
374 if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
375 struct device_info *d;
377 if (s->source->monitor_of)
378 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
380 d = pa_hashmap_get(u->device_infos, s->source);
389 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
390 struct device_info *d;
395 pa_object_assert_ref(o);
398 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
399 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
401 /* Never suspend monitors */
402 if (source && source->monitor_of)
405 pa_assert(source || sink);
407 d = pa_xnew(struct device_info, 1);
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);
414 if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
415 (d->source && pa_source_check_suspend(d->source) <= 0))
421 static void device_info_free(struct device_info *d) {
425 pa_source_unref(d->source);
427 pa_sink_unref(d->sink);
429 d->userdata->core->mainloop->time_free(d->time_event);
434 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
435 struct device_info *d;
438 pa_object_assert_ref(o);
441 if ((d = pa_hashmap_remove(u->device_infos, o)))
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;
451 pa_object_assert_ref(o);
454 if (!(d = pa_hashmap_get(u->device_infos, o)))
457 if (pa_sink_isinstance(o)) {
458 pa_sink *s = PA_SINK(o);
459 pa_sink_state_t state = pa_sink_get_state(s);
461 if (pa_sink_check_suspend(s) <= 0)
462 if (PA_SINK_IS_OPENED(state))
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);
469 if (pa_source_check_suspend(s) <= 0)
470 if (PA_SOURCE_IS_OPENED(state))
477 int pa__init(pa_module*m) {
478 pa_modargs *ma = NULL;
480 uint32_t timeout = 5;
487 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
488 pa_log("Failed to parse module arguments.");
492 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
493 pa_log("Failed to parse timeout value.");
497 m->userdata = u = pa_xnew(struct userdata, 1);
499 u->timeout = timeout;
500 u->device_infos = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
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);
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);
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);
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);
541 void pa__done(pa_module*m) {
543 struct device_info *d;
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);
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);
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);
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);
588 while ((d = pa_hashmap_steal_first(u->device_infos)))
591 pa_hashmap_free(u->device_infos, NULL, NULL);