4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulse/xmalloc.h>
29 #include <pulse/timeval.h>
31 #include <pulsecore/core.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
)
44 static const char* const valid_modargs
[] = {
52 pa_hashmap
*device_infos
;
56 *sink_disconnect_slot
,
57 *source_disconnect_slot
,
58 *sink_state_changed_slot
,
59 *source_state_changed_slot
;
63 *source_output_new_slot
,
64 *sink_input_disconnect_slot
,
65 *source_output_disconnect_slot
,
66 *sink_input_move_slot
,
67 *source_output_move_slot
,
68 *sink_input_move_post_slot
,
69 *source_output_move_post_slot
;
73 struct userdata
*userdata
;
76 struct timeval last_use
;
77 pa_time_event
*time_event
;
80 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
81 struct device_info
*d
= userdata
;
85 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
87 if (d
->sink
&& pa_sink_used_by(d
->sink
) <= 0 && pa_sink_get_state(d
->sink
) != PA_SINK_SUSPENDED
) {
88 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
89 pa_sink_suspend(d
->sink
, 1);
90 pa_source_suspend(d
->sink
->monitor_source
, 1);
93 if (d
->source
&& pa_source_used_by(d
->source
) <= 0 && pa_source_get_state(d
->source
) != PA_SOURCE_SUSPENDED
) {
94 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
95 pa_source_suspend(d
->source
, 1);
99 static void restart(struct device_info
*d
) {
103 pa_gettimeofday(&tv
);
105 pa_timeval_add(&tv
, d
->userdata
->timeout
*1000000);
106 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, &tv
);
109 pa_log_debug("Sink %s becomes idle.", d
->sink
->name
);
111 pa_log_debug("Source %s becomes idle.", d
->source
->name
);
114 static void resume(struct device_info
*d
) {
117 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
120 pa_sink_suspend(d
->sink
, 0);
121 pa_source_suspend(d
->sink
->monitor_source
, 0);
123 pa_log_debug("Sink %s becomes busy.", d
->sink
->name
);
127 pa_source_suspend(d
->source
, 0);
128 if (d
->source
->monitor_of
)
129 pa_sink_suspend(d
->source
->monitor_of
, 0);
131 pa_log_debug("Source %s becomes busy.", d
->source
->name
);
135 static pa_hook_result_t
sink_input_new_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
136 struct device_info
*d
;
139 pa_sink_input_assert_ref(s
);
142 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)));
148 static pa_hook_result_t
source_output_new_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
149 struct device_info
*d
;
152 pa_source_output_assert_ref(s
);
155 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->source
)));
161 static pa_hook_result_t
sink_input_disconnect_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
163 pa_sink_input_assert_ref(s
);
166 if (pa_sink_used_by(s
->sink
) <= 0) {
167 struct device_info
*d
;
168 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)));
175 static pa_hook_result_t
source_output_disconnect_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
177 pa_source_output_assert_ref(s
);
180 if (pa_source_used_by(s
->source
) <= 0) {
181 struct device_info
*d
;
182 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->source
)));
189 static pa_hook_result_t
sink_input_move_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
191 pa_sink_input_assert_ref(s
);
194 if (pa_sink_used_by(s
->sink
) <= 1) {
195 struct device_info
*d
;
196 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)));
203 static pa_hook_result_t
sink_input_move_post_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
204 struct device_info
*d
;
206 pa_sink_input_assert_ref(s
);
209 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)));
215 static pa_hook_result_t
source_output_move_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
217 pa_source_output_assert_ref(s
);
220 if (pa_source_used_by(s
->source
) <= 1) {
221 struct device_info
*d
;
222 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->source
)));
229 static pa_hook_result_t
source_output_move_post_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
230 struct device_info
*d
;
232 pa_source_output_assert_ref(s
);
235 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->source
)));
241 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
242 struct device_info
*d
;
245 pa_object_assert_ref(o
);
248 d
= pa_xnew(struct device_info
, 1);
250 d
->source
= pa_source_isinstance(o
) ? pa_source_ref(PA_SOURCE(o
)) : NULL
;
251 d
->sink
= pa_sink_isinstance(o
) ? pa_sink_ref(PA_SINK(o
)) : NULL
;
252 pa_assert(d
->source
|| d
->sink
);
253 d
->time_event
= c
->mainloop
->time_new(c
->mainloop
, NULL
, timeout_cb
, d
);
254 pa_hashmap_put(u
->device_infos
, o
, d
);
256 if ((d
->sink
&& pa_sink_used_by(d
->sink
) <= 0) ||
257 (d
->source
&& pa_source_used_by(d
->source
) <= 0))
263 static void device_info_free(struct device_info
*d
) {
267 pa_source_unref(d
->source
);
269 pa_sink_unref(d
->sink
);
271 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
276 static pa_hook_result_t
device_disconnect_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
277 struct device_info
*d
;
280 pa_object_assert_ref(o
);
283 pa_assert_se((d
= pa_hashmap_remove(u
->device_infos
, o
)));
289 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
290 struct device_info
*d
;
293 pa_object_assert_ref(o
);
296 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
299 if (pa_sink_isinstance(o
)) {
300 pa_sink
*s
= PA_SINK(o
);
302 if (pa_sink_used_by(s
) <= 0) {
303 pa_sink_state_t state
= pa_sink_get_state(s
);
305 if (state
== PA_SINK_RUNNING
|| state
== PA_SINK_IDLE
)
309 } else if (pa_source_isinstance(o
)) {
310 pa_source
*s
= PA_SOURCE(o
);
312 if (pa_source_used_by(s
) <= 0) {
313 pa_sink_state_t state
= pa_source_get_state(s
);
315 if (state
== PA_SINK_RUNNING
|| state
== PA_SINK_IDLE
)
323 int pa__init(pa_module
*m
) {
324 pa_modargs
*ma
= NULL
;
326 uint32_t timeout
= 1;
333 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
334 pa_log("Failed to parse module arguments.");
338 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
339 pa_log("Failed to parse timeout value.");
343 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
345 u
->timeout
= timeout
;
346 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
348 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
349 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
351 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
352 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
354 u
->sink_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], (pa_hook_cb_t
) device_new_hook_cb
, u
);
355 u
->source_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_NEW_POST
], (pa_hook_cb_t
) device_new_hook_cb
, u
);
356 u
->sink_disconnect_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_DISCONNECT_POST
], (pa_hook_cb_t
) device_disconnect_hook_cb
, u
);
357 u
->source_disconnect_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_DISCONNECT_POST
], (pa_hook_cb_t
) device_disconnect_hook_cb
, u
);
358 u
->sink_state_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_STATE_CHANGED
], (pa_hook_cb_t
) device_state_changed_hook_cb
, u
);
359 u
->source_state_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_STATE_CHANGED
], (pa_hook_cb_t
) device_state_changed_hook_cb
, u
);
361 u
->sink_input_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], (pa_hook_cb_t
) sink_input_new_hook_cb
, u
);
362 u
->source_output_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_PUT
], (pa_hook_cb_t
) source_output_new_hook_cb
, u
);
363 u
->sink_input_disconnect_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_DISCONNECT_POST
], (pa_hook_cb_t
) sink_input_disconnect_hook_cb
, u
);
364 u
->source_output_disconnect_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_DISCONNECT_POST
], (pa_hook_cb_t
) source_output_disconnect_hook_cb
, u
);
365 u
->sink_input_move_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE
], (pa_hook_cb_t
) sink_input_move_hook_cb
, u
);
366 u
->source_output_move_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE
], (pa_hook_cb_t
) source_output_move_hook_cb
, u
);
367 u
->sink_input_move_post_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_POST
], (pa_hook_cb_t
) sink_input_move_post_hook_cb
, u
);
368 u
->source_output_move_post_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_POST
], (pa_hook_cb_t
) source_output_move_post_hook_cb
, u
);
382 void pa__done(pa_module
*m
) {
384 struct device_info
*d
;
393 if (u
->sink_new_slot
)
394 pa_hook_slot_free(u
->sink_new_slot
);
395 if (u
->sink_disconnect_slot
)
396 pa_hook_slot_free(u
->sink_disconnect_slot
);
397 if (u
->sink_state_changed_slot
)
398 pa_hook_slot_free(u
->sink_state_changed_slot
);
400 if (u
->source_new_slot
)
401 pa_hook_slot_free(u
->source_new_slot
);
402 if (u
->source_disconnect_slot
)
403 pa_hook_slot_free(u
->source_disconnect_slot
);
404 if (u
->source_state_changed_slot
)
405 pa_hook_slot_free(u
->source_state_changed_slot
);
407 if (u
->sink_input_new_slot
)
408 pa_hook_slot_free(u
->sink_input_new_slot
);
409 if (u
->sink_input_disconnect_slot
)
410 pa_hook_slot_free(u
->sink_input_disconnect_slot
);
411 if (u
->sink_input_move_slot
)
412 pa_hook_slot_free(u
->sink_input_move_slot
);
413 if (u
->sink_input_move_post_slot
)
414 pa_hook_slot_free(u
->sink_input_move_post_slot
);
416 if (u
->source_output_new_slot
)
417 pa_hook_slot_free(u
->source_output_new_slot
);
418 if (u
->source_output_disconnect_slot
)
419 pa_hook_slot_free(u
->source_output_disconnect_slot
);
420 if (u
->source_output_move_slot
)
421 pa_hook_slot_free(u
->source_output_move_slot
);
422 if (u
->source_output_move_post_slot
)
423 pa_hook_slot_free(u
->source_output_move_post_slot
);
425 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
428 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);