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
);
45 "mempool_vacuum=<vacuum memory if all sinks and sources are suspended?>");
47 static const char* const valid_modargs
[] = {
56 pa_hashmap
*device_infos
;
62 *sink_state_changed_slot
,
63 *source_state_changed_slot
;
67 *source_output_new_slot
,
68 *sink_input_unlink_slot
,
69 *source_output_unlink_slot
,
70 *sink_input_move_start_slot
,
71 *source_output_move_start_slot
,
72 *sink_input_move_finish_slot
,
73 *source_output_move_finish_slot
,
74 *sink_input_state_changed_slot
,
75 *source_output_state_changed_slot
;
77 pa_bool_t mempool_vacuum
:1;
81 struct userdata
*userdata
;
85 pa_time_event
*time_event
;
88 static void check_meempool_vacuum(struct device_info
*d
) {
94 pa_assert(d
->userdata
);
95 pa_assert(d
->userdata
->core
);
98 PA_IDXSET_FOREACH(si
, d
->userdata
->core
->sinks
, idx
)
99 if (pa_sink_get_state(si
) != PA_SINK_SUSPENDED
)
103 PA_IDXSET_FOREACH(so
, d
->userdata
->core
->sources
, idx
)
104 if (pa_source_get_state(so
) != PA_SOURCE_SUSPENDED
)
107 pa_log_info("All sinks and sources are suspended, vacuuming memory");
108 pa_mempool_vacuum(d
->userdata
->core
->mempool
);
111 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
112 struct device_info
*d
= userdata
;
116 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
118 if (d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0 && !(d
->sink
->suspend_cause
& PA_SUSPEND_IDLE
)) {
119 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
120 pa_sink_suspend(d
->sink
, TRUE
, PA_SUSPEND_IDLE
);
121 if (d
->userdata
->mempool_vacuum
)
122 check_meempool_vacuum(d
);
125 if (d
->source
&& pa_source_check_suspend(d
->source
) <= 0 && !(d
->source
->suspend_cause
& PA_SUSPEND_IDLE
)) {
126 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
127 pa_source_suspend(d
->source
, TRUE
, PA_SUSPEND_IDLE
);
128 if (d
->userdata
->mempool_vacuum
)
129 check_meempool_vacuum(d
);
133 static void restart(struct device_info
*d
) {
139 pa_assert(d
->sink
|| d
->source
);
141 d
->last_use
= now
= pa_rtclock_now();
143 s
= pa_proplist_gets(d
->sink
? d
->sink
->proplist
: d
->source
->proplist
, "module-suspend-on-idle.timeout");
144 if (!s
|| pa_atou(s
, &timeout
) < 0)
145 timeout
= d
->userdata
->timeout
;
147 pa_core_rttime_restart(d
->userdata
->core
, d
->time_event
, now
+ timeout
* PA_USEC_PER_SEC
);
150 pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d
->sink
->name
, timeout
);
152 pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d
->source
->name
, timeout
);
155 static void resume(struct device_info
*d
) {
158 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
161 pa_sink_suspend(d
->sink
, FALSE
, PA_SUSPEND_IDLE
);
163 pa_log_debug("Sink %s becomes busy.", d
->sink
->name
);
167 pa_source_suspend(d
->source
, FALSE
, PA_SUSPEND_IDLE
);
169 pa_log_debug("Source %s becomes busy.", d
->source
->name
);
173 static pa_hook_result_t
sink_input_fixate_hook_cb(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
174 struct device_info
*d
;
180 /* We need to resume the audio device here even for
181 * PA_SINK_INPUT_START_CORKED, since we need the device parameters
182 * to be fully available while the stream is set up. */
184 if ((d
= pa_hashmap_get(u
->device_infos
, data
->sink
)))
190 static pa_hook_result_t
source_output_fixate_hook_cb(pa_core
*c
, pa_source_output_new_data
*data
, struct userdata
*u
) {
191 struct device_info
*d
;
197 if (data
->source
->monitor_of
)
198 d
= pa_hashmap_get(u
->device_infos
, data
->source
->monitor_of
);
200 d
= pa_hashmap_get(u
->device_infos
, data
->source
);
208 static pa_hook_result_t
sink_input_unlink_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
210 pa_sink_input_assert_ref(s
);
216 if (pa_sink_check_suspend(s
->sink
) <= 0) {
217 struct device_info
*d
;
218 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
225 static pa_hook_result_t
source_output_unlink_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
226 struct device_info
*d
= NULL
;
229 pa_source_output_assert_ref(s
);
235 if (s
->source
->monitor_of
) {
236 if (pa_sink_check_suspend(s
->source
->monitor_of
) <= 0)
237 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
239 if (pa_source_check_suspend(s
->source
) <= 0)
240 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
249 static pa_hook_result_t
sink_input_move_start_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
250 struct device_info
*d
;
253 pa_sink_input_assert_ref(s
);
256 if (pa_sink_check_suspend(s
->sink
) <= 1)
257 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
263 static pa_hook_result_t
sink_input_move_finish_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
264 struct device_info
*d
;
265 pa_sink_input_state_t state
;
268 pa_sink_input_assert_ref(s
);
271 state
= pa_sink_input_get_state(s
);
272 if (state
!= PA_SINK_INPUT_RUNNING
&& state
!= PA_SINK_INPUT_DRAINED
)
275 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
281 static pa_hook_result_t
source_output_move_start_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
282 struct device_info
*d
= NULL
;
285 pa_source_output_assert_ref(s
);
288 if (s
->source
->monitor_of
) {
289 if (pa_sink_check_suspend(s
->source
->monitor_of
) <= 1)
290 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
292 if (pa_source_check_suspend(s
->source
) <= 1)
293 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
302 static pa_hook_result_t
source_output_move_finish_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
303 struct device_info
*d
;
306 pa_source_output_assert_ref(s
);
309 if (pa_source_output_get_state(s
) != PA_SOURCE_OUTPUT_RUNNING
)
312 if (s
->source
->monitor_of
)
313 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
315 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
323 static pa_hook_result_t
sink_input_state_changed_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
324 struct device_info
*d
;
325 pa_sink_input_state_t state
;
328 pa_sink_input_assert_ref(s
);
331 state
= pa_sink_input_get_state(s
);
332 if (state
== PA_SINK_INPUT_RUNNING
|| state
== PA_SINK_INPUT_DRAINED
)
333 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
339 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
341 pa_source_output_assert_ref(s
);
344 if (pa_source_output_get_state(s
) == PA_SOURCE_OUTPUT_RUNNING
) {
345 struct device_info
*d
;
347 if (s
->source
->monitor_of
)
348 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
350 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
359 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
360 struct device_info
*d
;
365 pa_object_assert_ref(o
);
368 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
369 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
371 /* Never suspend monitors */
372 if (source
&& source
->monitor_of
)
375 pa_assert(source
|| sink
);
377 d
= pa_xnew(struct device_info
, 1);
379 d
->source
= source
? pa_source_ref(source
) : NULL
;
380 d
->sink
= sink
? pa_sink_ref(sink
) : NULL
;
381 d
->time_event
= pa_core_rttime_new(c
, PA_USEC_INVALID
, timeout_cb
, d
);
382 pa_hashmap_put(u
->device_infos
, o
, d
);
384 if ((d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0) ||
385 (d
->source
&& pa_source_check_suspend(d
->source
) <= 0))
391 static void device_info_free(struct device_info
*d
) {
395 pa_source_unref(d
->source
);
397 pa_sink_unref(d
->sink
);
399 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
404 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
405 struct device_info
*d
;
408 pa_object_assert_ref(o
);
411 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
417 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
418 struct device_info
*d
;
421 pa_object_assert_ref(o
);
424 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
427 if (pa_sink_isinstance(o
)) {
428 pa_sink
*s
= PA_SINK(o
);
429 pa_sink_state_t state
= pa_sink_get_state(s
);
431 if (pa_sink_check_suspend(s
) <= 0)
432 if (PA_SINK_IS_OPENED(state
))
435 } else if (pa_source_isinstance(o
)) {
436 pa_source
*s
= PA_SOURCE(o
);
437 pa_source_state_t state
= pa_source_get_state(s
);
439 if (pa_source_check_suspend(s
) <= 0)
440 if (PA_SOURCE_IS_OPENED(state
))
447 int pa__init(pa_module
*m
) {
448 pa_modargs
*ma
= NULL
;
450 uint32_t timeout
= 5;
451 pa_bool_t mempool_vacuum
= FALSE
;
458 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
459 pa_log("Failed to parse module arguments.");
463 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
464 pa_log("Failed to parse timeout value.");
468 if (pa_modargs_get_value_boolean(ma
, "mempool_vacuum", &mempool_vacuum
) < 0) {
469 pa_log("Failed to parse mempool_vacuum boolean parameter.");
473 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
475 u
->timeout
= timeout
;
476 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
477 u
->mempool_vacuum
= mempool_vacuum
;
479 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
480 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
482 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
483 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
485 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
);
486 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
);
487 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
);
488 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
);
489 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
);
490 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
);
492 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
);
493 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
);
494 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
);
495 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
);
496 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
);
497 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
);
498 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
);
499 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
);
500 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
);
501 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
);
514 void pa__done(pa_module
*m
) {
516 struct device_info
*d
;
525 if (u
->sink_new_slot
)
526 pa_hook_slot_free(u
->sink_new_slot
);
527 if (u
->sink_unlink_slot
)
528 pa_hook_slot_free(u
->sink_unlink_slot
);
529 if (u
->sink_state_changed_slot
)
530 pa_hook_slot_free(u
->sink_state_changed_slot
);
532 if (u
->source_new_slot
)
533 pa_hook_slot_free(u
->source_new_slot
);
534 if (u
->source_unlink_slot
)
535 pa_hook_slot_free(u
->source_unlink_slot
);
536 if (u
->source_state_changed_slot
)
537 pa_hook_slot_free(u
->source_state_changed_slot
);
539 if (u
->sink_input_new_slot
)
540 pa_hook_slot_free(u
->sink_input_new_slot
);
541 if (u
->sink_input_unlink_slot
)
542 pa_hook_slot_free(u
->sink_input_unlink_slot
);
543 if (u
->sink_input_move_start_slot
)
544 pa_hook_slot_free(u
->sink_input_move_start_slot
);
545 if (u
->sink_input_move_finish_slot
)
546 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
547 if (u
->sink_input_state_changed_slot
)
548 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
550 if (u
->source_output_new_slot
)
551 pa_hook_slot_free(u
->source_output_new_slot
);
552 if (u
->source_output_unlink_slot
)
553 pa_hook_slot_free(u
->source_output_unlink_slot
);
554 if (u
->source_output_move_start_slot
)
555 pa_hook_slot_free(u
->source_output_move_start_slot
);
556 if (u
->source_output_move_finish_slot
)
557 pa_hook_slot_free(u
->source_output_move_finish_slot
);
558 if (u
->source_output_state_changed_slot
)
559 pa_hook_slot_free(u
->source_output_state_changed_slot
);
561 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
564 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);