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
;
84 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
85 struct device_info
*d
= userdata
;
89 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
91 if (d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0 && !(d
->sink
->suspend_cause
& PA_SUSPEND_IDLE
)) {
92 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
93 pa_sink_suspend(d
->sink
, true, PA_SUSPEND_IDLE
);
94 pa_core_maybe_vacuum(d
->userdata
->core
);
97 if (d
->source
&& pa_source_check_suspend(d
->source
) <= 0 && !(d
->source
->suspend_cause
& PA_SUSPEND_IDLE
)) {
98 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
99 pa_source_suspend(d
->source
, true, PA_SUSPEND_IDLE
);
100 pa_core_maybe_vacuum(d
->userdata
->core
);
104 static void restart(struct device_info
*d
) {
108 pa_assert(d
->sink
|| d
->source
);
110 d
->last_use
= now
= pa_rtclock_now();
111 pa_core_rttime_restart(d
->userdata
->core
, d
->time_event
, now
+ d
->timeout
);
114 pa_log_debug("Sink %s becomes idle, timeout in %" PRIu64
" seconds.", d
->sink
->name
, d
->timeout
/ PA_USEC_PER_SEC
);
116 pa_log_debug("Source %s becomes idle, timeout in %" PRIu64
" seconds.", d
->source
->name
, d
->timeout
/ PA_USEC_PER_SEC
);
119 static void resume(struct device_info
*d
) {
122 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
125 pa_log_debug("Sink %s becomes busy, resuming.", d
->sink
->name
);
126 pa_sink_suspend(d
->sink
, false, PA_SUSPEND_IDLE
);
130 pa_log_debug("Source %s becomes busy, resuming.", d
->source
->name
);
131 pa_source_suspend(d
->source
, false, PA_SUSPEND_IDLE
);
135 static pa_hook_result_t
sink_input_fixate_hook_cb(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
136 struct device_info
*d
;
142 /* We need to resume the audio device here even for
143 * PA_SINK_INPUT_START_CORKED, since we need the device parameters
144 * to be fully available while the stream is set up. In that case,
145 * make sure we close the sink again after the timeout interval. */
147 if ((d
= pa_hashmap_get(u
->device_infos
, data
->sink
))) {
149 if (pa_sink_check_suspend(d
->sink
) <= 0)
156 static pa_hook_result_t
source_output_fixate_hook_cb(pa_core
*c
, pa_source_output_new_data
*data
, struct userdata
*u
) {
157 struct device_info
*d
;
163 if (data
->source
->monitor_of
)
164 d
= pa_hashmap_get(u
->device_infos
, data
->source
->monitor_of
);
166 d
= pa_hashmap_get(u
->device_infos
, data
->source
);
170 if (pa_source_check_suspend(d
->source
) <= 0)
177 static pa_hook_result_t
sink_input_unlink_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
179 pa_sink_input_assert_ref(s
);
185 if (pa_sink_check_suspend(s
->sink
) <= 0) {
186 struct device_info
*d
;
187 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
194 static pa_hook_result_t
source_output_unlink_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
195 struct device_info
*d
= NULL
;
198 pa_source_output_assert_ref(s
);
204 if (s
->source
->monitor_of
) {
205 if (pa_sink_check_suspend(s
->source
->monitor_of
) <= 0)
206 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
208 if (pa_source_check_suspend(s
->source
) <= 0)
209 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
218 static pa_hook_result_t
sink_input_move_start_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
219 struct device_info
*d
;
222 pa_sink_input_assert_ref(s
);
225 if (pa_sink_check_suspend(s
->sink
) <= 1)
226 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
232 static pa_hook_result_t
sink_input_move_finish_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
233 struct device_info
*d
;
234 pa_sink_input_state_t state
;
237 pa_sink_input_assert_ref(s
);
240 state
= pa_sink_input_get_state(s
);
241 if (state
!= PA_SINK_INPUT_RUNNING
&& state
!= PA_SINK_INPUT_DRAINED
)
244 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
250 static pa_hook_result_t
source_output_move_start_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
251 struct device_info
*d
= NULL
;
254 pa_source_output_assert_ref(s
);
257 if (s
->source
->monitor_of
) {
258 if (pa_sink_check_suspend(s
->source
->monitor_of
) <= 1)
259 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
261 if (pa_source_check_suspend(s
->source
) <= 1)
262 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
271 static pa_hook_result_t
source_output_move_finish_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
272 struct device_info
*d
;
275 pa_source_output_assert_ref(s
);
278 if (pa_source_output_get_state(s
) != PA_SOURCE_OUTPUT_RUNNING
)
281 if (s
->source
->monitor_of
)
282 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
284 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
292 static pa_hook_result_t
sink_input_state_changed_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
293 struct device_info
*d
;
294 pa_sink_input_state_t state
;
297 pa_sink_input_assert_ref(s
);
300 state
= pa_sink_input_get_state(s
);
301 if (state
== PA_SINK_INPUT_RUNNING
|| state
== PA_SINK_INPUT_DRAINED
)
302 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
308 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
310 pa_source_output_assert_ref(s
);
313 if (pa_source_output_get_state(s
) == PA_SOURCE_OUTPUT_RUNNING
) {
314 struct device_info
*d
;
316 if (s
->source
->monitor_of
)
317 d
= pa_hashmap_get(u
->device_infos
, s
->source
->monitor_of
);
319 d
= pa_hashmap_get(u
->device_infos
, s
->source
);
328 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
329 struct device_info
*d
;
332 const char *timeout_str
;
337 pa_object_assert_ref(o
);
340 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
341 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
343 /* Never suspend monitors */
344 if (source
&& source
->monitor_of
)
347 pa_assert(source
|| sink
);
349 timeout_str
= pa_proplist_gets(sink
? sink
->proplist
: source
->proplist
, "module-suspend-on-idle.timeout");
350 if (timeout_str
&& pa_atoi(timeout_str
, &timeout
) >= 0)
351 timeout_valid
= true;
353 timeout_valid
= false;
355 if (timeout_valid
&& timeout
< 0)
358 d
= pa_xnew(struct device_info
, 1);
360 d
->source
= source
? pa_source_ref(source
) : NULL
;
361 d
->sink
= sink
? pa_sink_ref(sink
) : NULL
;
362 d
->time_event
= pa_core_rttime_new(c
, PA_USEC_INVALID
, timeout_cb
, d
);
365 d
->timeout
= timeout
* PA_USEC_PER_SEC
;
367 d
->timeout
= d
->userdata
->timeout
;
369 pa_hashmap_put(u
->device_infos
, o
, d
);
371 if ((d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0) ||
372 (d
->source
&& pa_source_check_suspend(d
->source
) <= 0))
378 static void device_info_free(struct device_info
*d
) {
382 pa_source_unref(d
->source
);
384 pa_sink_unref(d
->sink
);
386 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
391 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
392 struct device_info
*d
;
395 pa_object_assert_ref(o
);
398 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
404 static pa_hook_result_t
device_state_changed_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_get(u
->device_infos
, o
)))
414 if (pa_sink_isinstance(o
)) {
415 pa_sink
*s
= PA_SINK(o
);
416 pa_sink_state_t state
= pa_sink_get_state(s
);
418 if (pa_sink_check_suspend(s
) <= 0)
419 if (PA_SINK_IS_OPENED(state
))
422 } else if (pa_source_isinstance(o
)) {
423 pa_source
*s
= PA_SOURCE(o
);
424 pa_source_state_t state
= pa_source_get_state(s
);
426 if (pa_source_check_suspend(s
) <= 0)
427 if (PA_SOURCE_IS_OPENED(state
))
434 int pa__init(pa_module
*m
) {
435 pa_modargs
*ma
= NULL
;
437 uint32_t timeout
= 5;
444 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
445 pa_log("Failed to parse module arguments.");
449 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
450 pa_log("Failed to parse timeout value.");
454 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
456 u
->timeout
= timeout
* PA_USEC_PER_SEC
;
457 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
);
459 PA_IDXSET_FOREACH(sink
, m
->core
->sinks
, idx
)
460 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
462 PA_IDXSET_FOREACH(source
, m
->core
->sources
, idx
)
463 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
465 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
);
466 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
);
467 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
);
468 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
);
469 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
);
470 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
);
472 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
);
473 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
);
474 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
);
475 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
);
476 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
);
477 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
);
478 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
);
479 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
);
480 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
);
481 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
);
494 void pa__done(pa_module
*m
) {
504 if (u
->sink_new_slot
)
505 pa_hook_slot_free(u
->sink_new_slot
);
506 if (u
->sink_unlink_slot
)
507 pa_hook_slot_free(u
->sink_unlink_slot
);
508 if (u
->sink_state_changed_slot
)
509 pa_hook_slot_free(u
->sink_state_changed_slot
);
511 if (u
->source_new_slot
)
512 pa_hook_slot_free(u
->source_new_slot
);
513 if (u
->source_unlink_slot
)
514 pa_hook_slot_free(u
->source_unlink_slot
);
515 if (u
->source_state_changed_slot
)
516 pa_hook_slot_free(u
->source_state_changed_slot
);
518 if (u
->sink_input_new_slot
)
519 pa_hook_slot_free(u
->sink_input_new_slot
);
520 if (u
->sink_input_unlink_slot
)
521 pa_hook_slot_free(u
->sink_input_unlink_slot
);
522 if (u
->sink_input_move_start_slot
)
523 pa_hook_slot_free(u
->sink_input_move_start_slot
);
524 if (u
->sink_input_move_finish_slot
)
525 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
526 if (u
->sink_input_state_changed_slot
)
527 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
529 if (u
->source_output_new_slot
)
530 pa_hook_slot_free(u
->source_output_new_slot
);
531 if (u
->source_output_unlink_slot
)
532 pa_hook_slot_free(u
->source_output_unlink_slot
);
533 if (u
->source_output_move_start_slot
)
534 pa_hook_slot_free(u
->source_output_move_start_slot
);
535 if (u
->source_output_move_finish_slot
)
536 pa_hook_slot_free(u
->source_output_move_finish_slot
);
537 if (u
->source_output_state_changed_slot
)
538 pa_hook_slot_free(u
->source_output_state_changed_slot
);
540 pa_hashmap_free(u
->device_infos
);