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>
37 #include "module-suspend-on-idle-symdef.h"
39 PA_MODULE_AUTHOR("Lennart Poettering");
40 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
41 PA_MODULE_VERSION(PACKAGE_VERSION
);
42 PA_MODULE_LOAD_ONCE(TRUE
);
43 PA_MODULE_USAGE("timeout=<timeout>");
45 static const char* const valid_modargs
[] = {
53 pa_hashmap
*device_infos
;
59 *sink_state_changed_slot
,
60 *source_state_changed_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
;
76 struct userdata
*userdata
;
80 pa_time_event
*time_event
;
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
;
88 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
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 pa_core_maybe_vacuum(d
->userdata
->core
);
96 if (d
->source
&& pa_source_check_suspend(d
->source
) <= 0 && !(d
->source
->suspend_cause
& PA_SUSPEND_IDLE
)) {
97 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
98 pa_source_suspend(d
->source
, TRUE
, PA_SUSPEND_IDLE
);
99 pa_core_maybe_vacuum(d
->userdata
->core
);
103 static void restart(struct device_info
*d
) {
109 pa_assert(d
->sink
|| d
->source
);
111 d
->last_use
= now
= pa_rtclock_now();
113 s
= pa_proplist_gets(d
->sink
? d
->sink
->proplist
: d
->source
->proplist
, "module-suspend-on-idle.timeout");
114 if (!s
|| pa_atou(s
, &timeout
) < 0)
115 timeout
= d
->userdata
->timeout
;
117 pa_core_rttime_restart(d
->userdata
->core
, d
->time_event
, now
+ timeout
* PA_USEC_PER_SEC
);
120 pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d
->sink
->name
, timeout
);
122 pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d
->source
->name
, timeout
);
125 static void resume(struct device_info
*d
) {
128 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
131 pa_log_debug("Sink %s becomes busy, resuming.", d
->sink
->name
);
132 pa_sink_suspend(d
->sink
, FALSE
, PA_SUSPEND_IDLE
);
136 pa_log_debug("Source %s becomes busy, resuming.", d
->source
->name
);
137 pa_source_suspend(d
->source
, FALSE
, PA_SUSPEND_IDLE
);
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
;
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. */
152 if ((d
= pa_hashmap_get(u
->device_infos
, data
->sink
)))
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
;
165 if (data
->source
->monitor_of
)
166 d
= pa_hashmap_get(u
->device_infos
, data
->source
->monitor_of
);
168 d
= pa_hashmap_get(u
->device_infos
, data
->source
);
176 static pa_hook_result_t
sink_input_unlink_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
178 pa_sink_input_assert_ref(s
);
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
)))
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
;
197 pa_source_output_assert_ref(s
);
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
);
207 if (pa_source_check_suspend(s
->source
) <= 0)
208 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
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
;
221 pa_sink_input_assert_ref(s
);
224 if (pa_sink_check_suspend(s
->sink
) <= 1)
225 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
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
;
236 pa_sink_input_assert_ref(s
);
239 state
= pa_sink_input_get_state(s
);
240 if (state
!= PA_SINK_INPUT_RUNNING
&& state
!= PA_SINK_INPUT_DRAINED
)
243 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
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
;
253 pa_source_output_assert_ref(s
);
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
);
260 if (pa_source_check_suspend(s
->source
) <= 1)
261 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
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
;
274 pa_source_output_assert_ref(s
);
277 if (pa_source_output_get_state(s
) != PA_SOURCE_OUTPUT_RUNNING
)
280 if (s
->source
->monitor_of
)
281 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
283 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
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
;
296 pa_sink_input_assert_ref(s
);
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
)))
307 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
309 pa_source_output_assert_ref(s
);
312 if (pa_source_output_get_state(s
) == PA_SOURCE_OUTPUT_RUNNING
) {
313 struct device_info
*d
;
315 if (s
->source
->monitor_of
)
316 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
318 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
327 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
328 struct device_info
*d
;
333 pa_object_assert_ref(o
);
336 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
337 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
339 /* Never suspend monitors */
340 if (source
&& source
->monitor_of
)
343 pa_assert(source
|| sink
);
345 d
= pa_xnew(struct device_info
, 1);
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
);
352 if ((d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0) ||
353 (d
->source
&& pa_source_check_suspend(d
->source
) <= 0))
359 static void device_info_free(struct device_info
*d
) {
363 pa_source_unref(d
->source
);
365 pa_sink_unref(d
->sink
);
367 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
372 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
373 struct device_info
*d
;
376 pa_object_assert_ref(o
);
379 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
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
;
389 pa_object_assert_ref(o
);
392 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
395 if (pa_sink_isinstance(o
)) {
396 pa_sink
*s
= PA_SINK(o
);
397 pa_sink_state_t state
= pa_sink_get_state(s
);
399 if (pa_sink_check_suspend(s
) <= 0)
400 if (PA_SINK_IS_OPENED(state
))
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
);
407 if (pa_source_check_suspend(s
) <= 0)
408 if (PA_SOURCE_IS_OPENED(state
))
415 int pa__init(pa_module
*m
) {
416 pa_modargs
*ma
= NULL
;
418 uint32_t timeout
= 5;
425 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
426 pa_log("Failed to parse module arguments.");
430 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
431 pa_log("Failed to parse timeout value.");
435 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
437 u
->timeout
= timeout
;
438 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
440 PA_IDXSET_FOREACH(sink
, m
->core
->sinks
, idx
)
441 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
443 PA_IDXSET_FOREACH(source
, m
->core
->sources
, idx
)
444 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
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
);
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
);
475 void pa__done(pa_module
*m
) {
485 if (u
->sink_new_slot
)
486 pa_hook_slot_free(u
->sink_new_slot
);
487 if (u
->sink_unlink_slot
)
488 pa_hook_slot_free(u
->sink_unlink_slot
);
489 if (u
->sink_state_changed_slot
)
490 pa_hook_slot_free(u
->sink_state_changed_slot
);
492 if (u
->source_new_slot
)
493 pa_hook_slot_free(u
->source_new_slot
);
494 if (u
->source_unlink_slot
)
495 pa_hook_slot_free(u
->source_unlink_slot
);
496 if (u
->source_state_changed_slot
)
497 pa_hook_slot_free(u
->source_state_changed_slot
);
499 if (u
->sink_input_new_slot
)
500 pa_hook_slot_free(u
->sink_input_new_slot
);
501 if (u
->sink_input_unlink_slot
)
502 pa_hook_slot_free(u
->sink_input_unlink_slot
);
503 if (u
->sink_input_move_start_slot
)
504 pa_hook_slot_free(u
->sink_input_move_start_slot
);
505 if (u
->sink_input_move_finish_slot
)
506 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
507 if (u
->sink_input_state_changed_slot
)
508 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
510 if (u
->source_output_new_slot
)
511 pa_hook_slot_free(u
->source_output_new_slot
);
512 if (u
->source_output_unlink_slot
)
513 pa_hook_slot_free(u
->source_output_unlink_slot
);
514 if (u
->source_output_move_start_slot
)
515 pa_hook_slot_free(u
->source_output_move_start_slot
);
516 if (u
->source_output_move_finish_slot
)
517 pa_hook_slot_free(u
->source_output_move_finish_slot
);
518 if (u
->source_output_state_changed_slot
)
519 pa_hook_slot_free(u
->source_output_state_changed_slot
);
521 pa_hashmap_free(u
->device_infos
, (pa_free_cb_t
) device_info_free
);