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, see <http://www.gnu.org/licenses/>.
24 #include <pulse/xmalloc.h>
25 #include <pulse/timeval.h>
26 #include <pulse/rtclock.h>
28 #include <pulsecore/core.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/source-output.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/log.h>
35 PA_MODULE_AUTHOR("Lennart Poettering");
36 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
37 PA_MODULE_VERSION(PACKAGE_VERSION);
38 PA_MODULE_LOAD_ONCE(true);
39 PA_MODULE_USAGE("timeout=<timeout>");
41 static const char* const valid_modargs[] = {
49 pa_hashmap *device_infos;
53 struct userdata *userdata;
57 pa_time_event *time_event;
61 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
62 struct device_info *d = userdata;
66 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
68 if (d->sink && pa_sink_check_suspend(d->sink, NULL, NULL) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
69 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
70 pa_sink_suspend(d->sink, true, PA_SUSPEND_IDLE);
71 pa_core_maybe_vacuum(d->userdata->core);
74 if (d->source && pa_source_check_suspend(d->source, NULL) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
75 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
76 pa_source_suspend(d->source, true, PA_SUSPEND_IDLE);
77 pa_core_maybe_vacuum(d->userdata->core);
81 static void restart(struct device_info *d) {
85 pa_assert(d->sink || d->source);
87 d->last_use = now = pa_rtclock_now();
88 pa_core_rttime_restart(d->userdata->core, d->time_event, now + d->timeout);
91 pa_log_debug("Sink %s becomes idle, timeout in %" PRIu64 " seconds.", d->sink->name, d->timeout / PA_USEC_PER_SEC);
93 pa_log_debug("Source %s becomes idle, timeout in %" PRIu64 " seconds.", d->source->name, d->timeout / PA_USEC_PER_SEC);
96 static void resume(struct device_info *d) {
99 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
102 pa_log_debug("Sink %s becomes busy, resuming.", d->sink->name);
103 pa_sink_suspend(d->sink, false, PA_SUSPEND_IDLE);
107 pa_log_debug("Source %s becomes busy, resuming.", d->source->name);
108 pa_source_suspend(d->source, false, PA_SUSPEND_IDLE);
112 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
113 struct device_info *d;
119 /* We need to resume the audio device here even for
120 * PA_SINK_INPUT_START_CORKED, since we need the device parameters
121 * to be fully available while the stream is set up. In that case,
122 * make sure we close the sink again after the timeout interval. */
124 if ((d = pa_hashmap_get(u->device_infos, data->sink))) {
126 if (pa_sink_check_suspend(d->sink, NULL, NULL) <= 0)
133 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
134 struct device_info *d;
140 if (data->source->monitor_of)
141 d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
143 d = pa_hashmap_get(u->device_infos, data->source);
148 if (pa_source_check_suspend(d->source, NULL) <= 0)
151 /* The source output is connected to a monitor source. */
153 if (pa_sink_check_suspend(d->sink, NULL, NULL) <= 0)
161 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
163 pa_sink_input_assert_ref(s);
169 if (pa_sink_check_suspend(s->sink, s, NULL) <= 0) {
170 struct device_info *d;
171 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
178 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
179 struct device_info *d = NULL;
182 pa_source_output_assert_ref(s);
188 if (s->source->monitor_of) {
189 if (pa_sink_check_suspend(s->source->monitor_of, NULL, s) <= 0)
190 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
192 if (pa_source_check_suspend(s->source, s) <= 0)
193 d = pa_hashmap_get(u->device_infos, s->source);
202 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
203 struct device_info *d;
206 pa_sink_input_assert_ref(s);
209 if (pa_sink_check_suspend(s->sink, s, NULL) <= 0)
210 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
216 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
217 struct device_info *d;
220 pa_sink_input_assert_ref(s);
223 if (s->state != PA_SINK_INPUT_RUNNING)
226 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
232 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
233 struct device_info *d = NULL;
236 pa_source_output_assert_ref(s);
239 if (s->source->monitor_of) {
240 if (pa_sink_check_suspend(s->source->monitor_of, NULL, s) <= 0)
241 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
243 if (pa_source_check_suspend(s->source, s) <= 0)
244 d = pa_hashmap_get(u->device_infos, s->source);
253 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
254 struct device_info *d;
257 pa_source_output_assert_ref(s);
260 if (s->state != PA_SOURCE_OUTPUT_RUNNING)
263 if (s->source->monitor_of)
264 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
266 d = pa_hashmap_get(u->device_infos, s->source);
274 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
275 struct device_info *d;
278 pa_sink_input_assert_ref(s);
281 if (s->state == PA_SINK_INPUT_RUNNING && s->sink)
282 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
288 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
290 pa_source_output_assert_ref(s);
293 if (s->state == PA_SOURCE_OUTPUT_RUNNING && s->source) {
294 struct device_info *d;
296 if (s->source->monitor_of)
297 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
299 d = pa_hashmap_get(u->device_infos, s->source);
308 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
309 struct device_info *d;
312 const char *timeout_str;
317 pa_object_assert_ref(o);
320 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
321 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
323 /* Never suspend monitors */
324 if (source && source->monitor_of)
327 pa_assert(source || sink);
329 timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout");
330 if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0)
331 timeout_valid = true;
333 timeout_valid = false;
335 if (timeout_valid && timeout < 0)
338 d = pa_xnew(struct device_info, 1);
340 d->source = source ? pa_source_ref(source) : NULL;
341 d->sink = sink ? pa_sink_ref(sink) : NULL;
342 d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
345 d->timeout = timeout * PA_USEC_PER_SEC;
347 d->timeout = d->userdata->timeout;
349 pa_hashmap_put(u->device_infos, o, d);
351 if ((d->sink && pa_sink_check_suspend(d->sink, NULL, NULL) <= 0) ||
352 (d->source && pa_source_check_suspend(d->source, NULL) <= 0))
358 static void device_info_free(struct device_info *d) {
362 pa_source_unref(d->source);
364 pa_sink_unref(d->sink);
366 d->userdata->core->mainloop->time_free(d->time_event);
371 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
373 pa_object_assert_ref(o);
376 pa_hashmap_remove_and_free(u->device_infos, o);
381 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
382 struct device_info *d;
385 pa_object_assert_ref(o);
388 if (!(d = pa_hashmap_get(u->device_infos, o)))
391 if (pa_sink_isinstance(o)) {
392 pa_sink *s = PA_SINK(o);
394 if (pa_sink_check_suspend(s, NULL, NULL) <= 0)
395 if (PA_SINK_IS_OPENED(s->state))
398 } else if (pa_source_isinstance(o)) {
399 pa_source *s = PA_SOURCE(o);
401 if (pa_source_check_suspend(s, NULL) <= 0)
402 if (PA_SOURCE_IS_OPENED(s->state))
409 int pa__init(pa_module*m) {
410 pa_modargs *ma = NULL;
412 uint32_t timeout = 5;
419 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
420 pa_log("Failed to parse module arguments.");
424 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
425 pa_log("Failed to parse timeout value.");
429 m->userdata = u = pa_xnew(struct userdata, 1);
431 u->timeout = timeout * PA_USEC_PER_SEC;
432 u->device_infos = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL, (pa_free_cb_t) device_info_free);
434 PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
435 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
437 PA_IDXSET_FOREACH(source, m->core->sources, idx)
438 device_new_hook_cb(m->core, PA_OBJECT(source), u);
440 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
441 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
442 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
443 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
444 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
445 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
447 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_fixate_hook_cb, u);
448 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_fixate_hook_cb, u);
449 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_unlink_hook_cb, u);
450 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_unlink_hook_cb, u);
451 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_start_hook_cb, u);
452 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_start_hook_cb, u);
453 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_finish_hook_cb, u);
454 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_finish_hook_cb, u);
455 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_state_changed_hook_cb, u);
456 pa_module_hook_connect(m, &m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_state_changed_hook_cb, u);
469 void pa__done(pa_module*m) {
471 struct device_info *d;
481 PA_HASHMAP_FOREACH(d, u->device_infos, state) {
482 if (d->sink && d->sink->state == PA_SINK_SUSPENDED) {
483 pa_log_debug("Resuming sink %s on module unload.", d->sink->name);
484 pa_sink_suspend(d->sink, false, PA_SUSPEND_IDLE);
487 if (d->source && d->source->state == PA_SOURCE_SUSPENDED) {
488 pa_log_debug("Resuming source %s on module unload.", d->source->name);
489 pa_source_suspend(d->source, false, PA_SUSPEND_IDLE);
493 pa_hashmap_free(u->device_infos);