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>
36 #include <pulsecore/namereg.h>
38 #include "module-suspend-on-idle-symdef.h"
40 PA_MODULE_AUTHOR("Lennart Poettering");
41 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
42 PA_MODULE_VERSION(PACKAGE_VERSION
);
43 PA_MODULE_LOAD_ONCE(TRUE
);
46 "mempool_vacuum=<vacuum memory if all sinks and sources are suspended?>");
48 static const char* const valid_modargs
[] = {
57 pa_hashmap
*device_infos
;
63 *sink_state_changed_slot
,
64 *source_state_changed_slot
;
68 *source_output_new_slot
,
69 *sink_input_unlink_slot
,
70 *source_output_unlink_slot
,
71 *sink_input_move_start_slot
,
72 *source_output_move_start_slot
,
73 *sink_input_move_finish_slot
,
74 *source_output_move_finish_slot
,
75 *sink_input_state_changed_slot
,
76 *source_output_state_changed_slot
;
78 pa_bool_t mempool_vacuum
:1;
82 struct userdata
*userdata
;
86 pa_time_event
*time_event
;
89 static void check_meempool_vacuum(struct device_info
*d
) {
95 pa_assert(d
->userdata
);
96 pa_assert(d
->userdata
->core
);
99 PA_IDXSET_FOREACH(si
, d
->userdata
->core
->sinks
, idx
)
100 if (pa_sink_get_state(si
) != PA_SINK_SUSPENDED
)
104 PA_IDXSET_FOREACH(so
, d
->userdata
->core
->sources
, idx
)
105 if (pa_source_get_state(so
) != PA_SOURCE_SUSPENDED
)
108 pa_log_info("All sinks and sources are suspended, vacuuming memory");
109 pa_mempool_vacuum(d
->userdata
->core
->mempool
);
112 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
113 struct device_info
*d
= userdata
;
117 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
119 if (d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0 && !(d
->sink
->suspend_cause
& PA_SUSPEND_IDLE
)) {
120 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
121 pa_sink_suspend(d
->sink
, TRUE
, PA_SUSPEND_IDLE
);
122 if (d
->userdata
->mempool_vacuum
)
123 check_meempool_vacuum(d
);
126 if (d
->source
&& pa_source_check_suspend(d
->source
) <= 0 && !(d
->source
->suspend_cause
& PA_SUSPEND_IDLE
)) {
127 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
128 pa_source_suspend(d
->source
, TRUE
, PA_SUSPEND_IDLE
);
129 if (d
->userdata
->mempool_vacuum
)
130 check_meempool_vacuum(d
);
134 static void restart(struct device_info
*d
) {
140 pa_assert(d
->sink
|| d
->source
);
142 d
->last_use
= now
= pa_rtclock_now();
144 s
= pa_proplist_gets(d
->sink
? d
->sink
->proplist
: d
->source
->proplist
, "module-suspend-on-idle.timeout");
145 if (!s
|| pa_atou(s
, &timeout
) < 0)
146 timeout
= d
->userdata
->timeout
;
148 pa_core_rttime_restart(d
->userdata
->core
, d
->time_event
, now
+ timeout
* PA_USEC_PER_SEC
);
151 pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d
->sink
->name
, timeout
);
153 pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d
->source
->name
, timeout
);
156 static void resume(struct device_info
*d
) {
159 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
162 pa_sink_suspend(d
->sink
, FALSE
, PA_SUSPEND_IDLE
);
164 pa_log_debug("Sink %s becomes busy.", d
->sink
->name
);
168 pa_source_suspend(d
->source
, FALSE
, PA_SUSPEND_IDLE
);
170 pa_log_debug("Source %s becomes busy.", d
->source
->name
);
174 static pa_hook_result_t
sink_input_fixate_hook_cb(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
175 struct device_info
*d
;
181 /* We need to resume the audio device here even for
182 * PA_SINK_INPUT_START_CORKED, since we need the device parameters
183 * to be fully available while the stream is set up. */
185 if ((d
= pa_hashmap_get(u
->device_infos
, data
->sink
)))
191 static pa_hook_result_t
source_output_fixate_hook_cb(pa_core
*c
, pa_source_output_new_data
*data
, struct userdata
*u
) {
192 struct device_info
*d
;
198 if (data
->source
->monitor_of
)
199 d
= pa_hashmap_get(u
->device_infos
, data
->source
->monitor_of
);
201 d
= pa_hashmap_get(u
->device_infos
, data
->source
);
209 static pa_hook_result_t
sink_input_unlink_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
211 pa_sink_input_assert_ref(s
);
217 if (pa_sink_check_suspend(s
->sink
) <= 0) {
218 struct device_info
*d
;
219 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
226 static pa_hook_result_t
source_output_unlink_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
227 struct device_info
*d
= NULL
;
230 pa_source_output_assert_ref(s
);
236 if (s
->source
->monitor_of
) {
237 if (pa_sink_check_suspend(s
->source
->monitor_of
) <= 0)
238 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
240 if (pa_source_check_suspend(s
->source
) <= 0)
241 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
250 static pa_hook_result_t
sink_input_move_start_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
251 struct device_info
*d
;
254 pa_sink_input_assert_ref(s
);
257 if (pa_sink_check_suspend(s
->sink
) <= 1)
258 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
264 static pa_hook_result_t
sink_input_move_finish_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
265 struct device_info
*d
;
266 pa_sink_input_state_t state
;
269 pa_sink_input_assert_ref(s
);
272 state
= pa_sink_input_get_state(s
);
273 if (state
!= PA_SINK_INPUT_RUNNING
&& state
!= PA_SINK_INPUT_DRAINED
)
276 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
282 static pa_hook_result_t
source_output_move_start_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
283 struct device_info
*d
= NULL
;
286 pa_source_output_assert_ref(s
);
289 if (s
->source
->monitor_of
) {
290 if (pa_sink_check_suspend(s
->source
->monitor_of
) <= 1)
291 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
293 if (pa_source_check_suspend(s
->source
) <= 1)
294 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
303 static pa_hook_result_t
source_output_move_finish_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
304 struct device_info
*d
;
307 pa_source_output_assert_ref(s
);
310 if (pa_source_output_get_state(s
) != PA_SOURCE_OUTPUT_RUNNING
)
313 if (s
->source
->monitor_of
)
314 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
316 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
324 static pa_hook_result_t
sink_input_state_changed_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
325 struct device_info
*d
;
326 pa_sink_input_state_t state
;
329 pa_sink_input_assert_ref(s
);
332 state
= pa_sink_input_get_state(s
);
333 if (state
== PA_SINK_INPUT_RUNNING
|| state
== PA_SINK_INPUT_DRAINED
)
334 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
340 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
342 pa_source_output_assert_ref(s
);
345 if (pa_source_output_get_state(s
) == PA_SOURCE_OUTPUT_RUNNING
) {
346 struct device_info
*d
;
348 if (s
->source
->monitor_of
)
349 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
351 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
360 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
361 struct device_info
*d
;
366 pa_object_assert_ref(o
);
369 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
370 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
372 /* Never suspend monitors */
373 if (source
&& source
->monitor_of
)
376 pa_assert(source
|| sink
);
378 d
= pa_xnew(struct device_info
, 1);
380 d
->source
= source
? pa_source_ref(source
) : NULL
;
381 d
->sink
= sink
? pa_sink_ref(sink
) : NULL
;
382 d
->time_event
= pa_core_rttime_new(c
, PA_USEC_INVALID
, timeout_cb
, d
);
383 pa_hashmap_put(u
->device_infos
, o
, d
);
385 if ((d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0) ||
386 (d
->source
&& pa_source_check_suspend(d
->source
) <= 0))
392 static void device_info_free(struct device_info
*d
) {
396 pa_source_unref(d
->source
);
398 pa_sink_unref(d
->sink
);
400 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
405 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
406 struct device_info
*d
;
409 pa_object_assert_ref(o
);
412 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
418 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
419 struct device_info
*d
;
422 pa_object_assert_ref(o
);
425 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
428 if (pa_sink_isinstance(o
)) {
429 pa_sink
*s
= PA_SINK(o
);
430 pa_sink_state_t state
= pa_sink_get_state(s
);
432 if (pa_sink_check_suspend(s
) <= 0)
433 if (PA_SINK_IS_OPENED(state
))
436 } else if (pa_source_isinstance(o
)) {
437 pa_source
*s
= PA_SOURCE(o
);
438 pa_source_state_t state
= pa_source_get_state(s
);
440 if (pa_source_check_suspend(s
) <= 0)
441 if (PA_SOURCE_IS_OPENED(state
))
448 int pa__init(pa_module
*m
) {
449 pa_modargs
*ma
= NULL
;
451 uint32_t timeout
= 5;
452 pa_bool_t mempool_vacuum
= FALSE
;
459 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
460 pa_log("Failed to parse module arguments.");
464 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
465 pa_log("Failed to parse timeout value.");
469 if (pa_modargs_get_value_boolean(ma
, "mempool_vacuum", &mempool_vacuum
) < 0) {
470 pa_log("Failed to parse mempool_vacuum boolean parameter.");
474 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
476 u
->timeout
= timeout
;
477 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
478 u
->mempool_vacuum
= mempool_vacuum
;
480 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
481 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
483 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
484 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
486 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
);
487 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
);
488 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
);
489 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
);
490 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
);
491 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
);
493 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
);
494 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
);
495 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
);
496 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
);
497 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
);
498 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
);
499 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
);
500 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
);
501 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
);
502 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
);
515 void pa__done(pa_module
*m
) {
517 struct device_info
*d
;
526 if (u
->sink_new_slot
)
527 pa_hook_slot_free(u
->sink_new_slot
);
528 if (u
->sink_unlink_slot
)
529 pa_hook_slot_free(u
->sink_unlink_slot
);
530 if (u
->sink_state_changed_slot
)
531 pa_hook_slot_free(u
->sink_state_changed_slot
);
533 if (u
->source_new_slot
)
534 pa_hook_slot_free(u
->source_new_slot
);
535 if (u
->source_unlink_slot
)
536 pa_hook_slot_free(u
->source_unlink_slot
);
537 if (u
->source_state_changed_slot
)
538 pa_hook_slot_free(u
->source_state_changed_slot
);
540 if (u
->sink_input_new_slot
)
541 pa_hook_slot_free(u
->sink_input_new_slot
);
542 if (u
->sink_input_unlink_slot
)
543 pa_hook_slot_free(u
->sink_input_unlink_slot
);
544 if (u
->sink_input_move_start_slot
)
545 pa_hook_slot_free(u
->sink_input_move_start_slot
);
546 if (u
->sink_input_move_finish_slot
)
547 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
548 if (u
->sink_input_state_changed_slot
)
549 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
551 if (u
->source_output_new_slot
)
552 pa_hook_slot_free(u
->source_output_new_slot
);
553 if (u
->source_output_unlink_slot
)
554 pa_hook_slot_free(u
->source_output_unlink_slot
);
555 if (u
->source_output_move_start_slot
)
556 pa_hook_slot_free(u
->source_output_move_start_slot
);
557 if (u
->source_output_move_finish_slot
)
558 pa_hook_slot_free(u
->source_output_move_finish_slot
);
559 if (u
->source_output_state_changed_slot
)
560 pa_hook_slot_free(u
->source_output_state_changed_slot
);
562 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
565 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);