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
;
58 *sink_state_changed_slot
,
59 *source_state_changed_slot
;
63 *source_output_new_slot
,
64 *sink_input_unlink_slot
,
65 *source_output_unlink_slot
,
66 *sink_input_move_slot
,
67 *source_output_move_slot
,
68 *sink_input_move_post_slot
,
69 *source_output_move_post_slot
,
70 *sink_input_state_changed_slot
,
71 *source_output_state_changed_slot
;
75 struct userdata
*userdata
;
78 struct timeval last_use
;
79 pa_time_event
*time_event
;
82 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
83 struct device_info
*d
= userdata
;
87 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
89 if (d
->sink
&& pa_sink_used_by(d
->sink
) <= 0 && pa_sink_get_state(d
->sink
) != PA_SINK_SUSPENDED
) {
90 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
91 pa_sink_suspend(d
->sink
, TRUE
);
94 if (d
->source
&& pa_source_used_by(d
->source
) <= 0 && pa_source_get_state(d
->source
) != PA_SOURCE_SUSPENDED
) {
95 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
96 pa_source_suspend(d
->source
, TRUE
);
100 static void restart(struct device_info
*d
) {
104 pa_gettimeofday(&tv
);
106 pa_timeval_add(&tv
, d
->userdata
->timeout
*1000000);
107 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, &tv
);
110 pa_log_debug("Sink %s becomes idle.", d
->sink
->name
);
112 pa_log_debug("Source %s becomes idle.", d
->source
->name
);
115 static void resume(struct device_info
*d
) {
118 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
121 pa_sink_suspend(d
->sink
, FALSE
);
123 pa_log_debug("Sink %s becomes busy.", d
->sink
->name
);
127 pa_source_suspend(d
->source
, FALSE
);
129 pa_log_debug("Source %s becomes busy.", d
->source
->name
);
133 static pa_hook_result_t
sink_input_new_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
134 struct device_info
*d
;
137 pa_sink_input_assert_ref(s
);
140 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
146 static pa_hook_result_t
source_output_new_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
147 struct device_info
*d
;
150 pa_source_output_assert_ref(s
);
153 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
159 static pa_hook_result_t
sink_input_unlink_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
161 pa_sink_input_assert_ref(s
);
164 if (pa_sink_used_by(s
->sink
) <= 0) {
165 struct device_info
*d
;
166 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
173 static pa_hook_result_t
source_output_unlink_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
175 pa_source_output_assert_ref(s
);
178 if (pa_source_used_by(s
->source
) <= 0) {
179 struct device_info
*d
;
180 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
187 static pa_hook_result_t
sink_input_move_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
189 pa_sink_input_assert_ref(s
);
192 if (pa_sink_used_by(s
->sink
) <= 1) {
193 struct device_info
*d
;
194 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
201 static pa_hook_result_t
sink_input_move_post_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
202 struct device_info
*d
;
204 pa_sink_input_assert_ref(s
);
207 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
213 static pa_hook_result_t
source_output_move_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
215 pa_source_output_assert_ref(s
);
218 if (pa_source_used_by(s
->source
) <= 1) {
219 struct device_info
*d
;
221 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
228 static pa_hook_result_t
source_output_move_post_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
229 struct device_info
*d
;
231 pa_source_output_assert_ref(s
);
234 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
240 static pa_hook_result_t
sink_input_state_changed_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
241 struct device_info
*d
;
242 pa_sink_input_state_t state
;
244 pa_sink_input_assert_ref(s
);
247 state
= pa_sink_input_get_state(s
);
248 if (state
== PA_SINK_INPUT_RUNNING
|| state
== PA_SINK_INPUT_DRAINED
)
249 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
255 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
256 struct device_info
*d
;
257 pa_source_output_state_t state
;
259 pa_source_output_assert_ref(s
);
262 state
= pa_source_output_get_state(s
);
263 if (state
== PA_SOURCE_OUTPUT_RUNNING
)
264 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
270 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
271 struct device_info
*d
;
276 pa_object_assert_ref(o
);
279 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
280 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
282 pa_assert(source
|| sink
);
284 d
= pa_xnew(struct device_info
, 1);
286 d
->source
= source
? pa_source_ref(source
) : NULL
;
287 d
->sink
= sink
? pa_sink_ref(sink
) : NULL
;
288 d
->time_event
= c
->mainloop
->time_new(c
->mainloop
, NULL
, timeout_cb
, d
);
289 pa_hashmap_put(u
->device_infos
, o
, d
);
291 if ((d
->sink
&& pa_sink_used_by(d
->sink
) <= 0) ||
292 (d
->source
&& pa_source_used_by(d
->source
) <= 0))
298 static void device_info_free(struct device_info
*d
) {
302 pa_source_unref(d
->source
);
304 pa_sink_unref(d
->sink
);
306 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
311 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
312 struct device_info
*d
;
315 pa_object_assert_ref(o
);
318 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
324 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
325 struct device_info
*d
;
328 pa_object_assert_ref(o
);
331 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
334 if (pa_sink_isinstance(o
)) {
335 pa_sink
*s
= PA_SINK(o
);
336 pa_sink_state_t state
= pa_sink_get_state(s
);
338 if (pa_sink_used_by(s
) <= 0) {
340 if (PA_SINK_OPENED(state
))
345 } else if (pa_source_isinstance(o
)) {
346 pa_source
*s
= PA_SOURCE(o
);
347 pa_source_state_t state
= pa_source_get_state(s
);
349 if (pa_source_used_by(s
) <= 0) {
351 if (PA_SOURCE_OPENED(state
))
359 int pa__init(pa_module
*m
) {
360 pa_modargs
*ma
= NULL
;
362 uint32_t timeout
= 1;
369 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
370 pa_log("Failed to parse module arguments.");
374 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
375 pa_log("Failed to parse timeout value.");
379 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
381 u
->timeout
= timeout
;
382 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
384 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
385 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
387 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
388 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
390 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
);
391 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
);
392 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK_POST
], (pa_hook_cb_t
) device_unlink_hook_cb
, u
);
393 u
->source_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK_POST
], (pa_hook_cb_t
) device_unlink_hook_cb
, u
);
394 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
);
395 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
);
397 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
);
398 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
);
399 u
->sink_input_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST
], (pa_hook_cb_t
) sink_input_unlink_hook_cb
, u
);
400 u
->source_output_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST
], (pa_hook_cb_t
) source_output_unlink_hook_cb
, u
);
401 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
);
402 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
);
403 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
);
404 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
);
405 u
->sink_input_state_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED
], (pa_hook_cb_t
) sink_input_state_changed_hook_cb
, u
);
406 u
->source_output_state_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED
], (pa_hook_cb_t
) source_output_state_changed_hook_cb
, u
);
420 void pa__done(pa_module
*m
) {
422 struct device_info
*d
;
431 if (u
->sink_new_slot
)
432 pa_hook_slot_free(u
->sink_new_slot
);
433 if (u
->sink_unlink_slot
)
434 pa_hook_slot_free(u
->sink_unlink_slot
);
435 if (u
->sink_state_changed_slot
)
436 pa_hook_slot_free(u
->sink_state_changed_slot
);
438 if (u
->source_new_slot
)
439 pa_hook_slot_free(u
->source_new_slot
);
440 if (u
->source_unlink_slot
)
441 pa_hook_slot_free(u
->source_unlink_slot
);
442 if (u
->source_state_changed_slot
)
443 pa_hook_slot_free(u
->source_state_changed_slot
);
445 if (u
->sink_input_new_slot
)
446 pa_hook_slot_free(u
->sink_input_new_slot
);
447 if (u
->sink_input_unlink_slot
)
448 pa_hook_slot_free(u
->sink_input_unlink_slot
);
449 if (u
->sink_input_move_slot
)
450 pa_hook_slot_free(u
->sink_input_move_slot
);
451 if (u
->sink_input_move_post_slot
)
452 pa_hook_slot_free(u
->sink_input_move_post_slot
);
453 if (u
->sink_input_state_changed_slot
)
454 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
456 if (u
->source_output_new_slot
)
457 pa_hook_slot_free(u
->source_output_new_slot
);
458 if (u
->source_output_unlink_slot
)
459 pa_hook_slot_free(u
->source_output_unlink_slot
);
460 if (u
->source_output_move_slot
)
461 pa_hook_slot_free(u
->source_output_move_slot
);
462 if (u
->source_output_move_post_slot
)
463 pa_hook_slot_free(u
->source_output_move_post_slot
);
464 if (u
->source_output_state_changed_slot
)
465 pa_hook_slot_free(u
->source_output_state_changed_slot
);
467 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
470 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);