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>
29 #include <pulsecore/core.h>
30 #include <pulsecore/sink-input.h>
31 #include <pulsecore/source-output.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/namereg.h>
36 #include "module-suspend-on-idle-symdef.h"
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
40 PA_MODULE_VERSION(PACKAGE_VERSION
);
41 PA_MODULE_LOAD_ONCE(TRUE
);
43 static const char* const valid_modargs
[] = {
51 pa_hashmap
*device_infos
;
57 *sink_state_changed_slot
,
58 *source_state_changed_slot
;
62 *source_output_new_slot
,
63 *sink_input_unlink_slot
,
64 *source_output_unlink_slot
,
65 *sink_input_move_start_slot
,
66 *source_output_move_start_slot
,
67 *sink_input_move_finish_slot
,
68 *source_output_move_finish_slot
,
69 *sink_input_state_changed_slot
,
70 *source_output_state_changed_slot
;
74 struct userdata
*userdata
;
77 struct timeval last_use
;
78 pa_time_event
*time_event
;
81 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
82 struct device_info
*d
= userdata
;
86 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
88 if (d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0 && pa_sink_get_state(d
->sink
) != PA_SINK_SUSPENDED
) {
89 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
90 pa_sink_suspend(d
->sink
, TRUE
);
93 if (d
->source
&& pa_source_check_suspend(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
, TRUE
);
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
, FALSE
);
122 pa_log_debug("Sink %s becomes busy.", d
->sink
->name
);
126 pa_source_suspend(d
->source
, FALSE
);
128 pa_log_debug("Source %s becomes busy.", d
->source
->name
);
132 static pa_hook_result_t
sink_input_fixate_hook_cb(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
133 struct device_info
*d
;
139 if ((d
= pa_hashmap_get(u
->device_infos
, data
->sink
)))
145 static pa_hook_result_t
source_output_fixate_hook_cb(pa_core
*c
, pa_source_output_new_data
*data
, struct userdata
*u
) {
146 struct device_info
*d
;
152 if ((d
= pa_hashmap_get(u
->device_infos
, data
->source
)))
158 static pa_hook_result_t
sink_input_unlink_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
160 pa_sink_input_assert_ref(s
);
166 if (pa_sink_check_suspend(s
->sink
) <= 0) {
167 struct device_info
*d
;
168 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
175 static pa_hook_result_t
source_output_unlink_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
177 pa_source_output_assert_ref(s
);
183 if (pa_source_check_suspend(s
->source
) <= 0) {
184 struct device_info
*d
;
185 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
192 static pa_hook_result_t
sink_input_move_start_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
193 struct device_info
*d
;
196 pa_sink_input_assert_ref(s
);
199 if (pa_sink_check_suspend(s
->sink
) <= 1)
200 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
206 static pa_hook_result_t
sink_input_move_finish_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
207 struct device_info
*d
;
210 pa_sink_input_assert_ref(s
);
213 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
219 static pa_hook_result_t
source_output_move_start_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
220 struct device_info
*d
;
223 pa_source_output_assert_ref(s
);
226 if (pa_source_check_suspend(s
->source
) <= 1)
227 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
233 static pa_hook_result_t
source_output_move_finish_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
234 struct device_info
*d
;
237 pa_source_output_assert_ref(s
);
240 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
246 static pa_hook_result_t
sink_input_state_changed_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
247 struct device_info
*d
;
248 pa_sink_input_state_t state
;
250 pa_sink_input_assert_ref(s
);
253 state
= pa_sink_input_get_state(s
);
254 if (state
== PA_SINK_INPUT_RUNNING
|| state
== PA_SINK_INPUT_DRAINED
)
255 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
261 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
262 struct device_info
*d
;
263 pa_source_output_state_t state
;
265 pa_source_output_assert_ref(s
);
268 state
= pa_source_output_get_state(s
);
269 if (state
== PA_SOURCE_OUTPUT_RUNNING
)
270 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
276 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
277 struct device_info
*d
;
282 pa_object_assert_ref(o
);
285 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
286 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
288 pa_assert(source
|| sink
);
290 d
= pa_xnew(struct device_info
, 1);
292 d
->source
= source
? pa_source_ref(source
) : NULL
;
293 d
->sink
= sink
? pa_sink_ref(sink
) : NULL
;
294 d
->time_event
= c
->mainloop
->time_new(c
->mainloop
, NULL
, timeout_cb
, d
);
295 pa_hashmap_put(u
->device_infos
, o
, d
);
297 if ((d
->sink
&& pa_sink_check_suspend(d
->sink
) <= 0) ||
298 (d
->source
&& pa_source_check_suspend(d
->source
) <= 0))
304 static void device_info_free(struct device_info
*d
) {
308 pa_source_unref(d
->source
);
310 pa_sink_unref(d
->sink
);
312 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
317 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
318 struct device_info
*d
;
321 pa_object_assert_ref(o
);
324 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
330 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
331 struct device_info
*d
;
334 pa_object_assert_ref(o
);
337 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
340 if (pa_sink_isinstance(o
)) {
341 pa_sink
*s
= PA_SINK(o
);
342 pa_sink_state_t state
= pa_sink_get_state(s
);
344 if (pa_sink_check_suspend(s
) <= 0) {
346 if (PA_SINK_IS_OPENED(state
))
351 } else if (pa_source_isinstance(o
)) {
352 pa_source
*s
= PA_SOURCE(o
);
353 pa_source_state_t state
= pa_source_get_state(s
);
355 if (pa_source_check_suspend(s
) <= 0) {
357 if (PA_SOURCE_IS_OPENED(state
))
365 int pa__init(pa_module
*m
) {
366 pa_modargs
*ma
= NULL
;
368 uint32_t timeout
= 5;
375 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
376 pa_log("Failed to parse module arguments.");
380 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
381 pa_log("Failed to parse timeout value.");
385 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
387 u
->timeout
= timeout
;
388 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
390 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
391 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
393 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
394 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
396 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
);
397 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
);
398 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
);
399 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
);
400 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
);
401 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
);
403 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
);
404 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
);
405 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
);
406 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
);
407 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
);
408 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
);
409 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
);
410 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
);
411 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
);
412 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
);
425 void pa__done(pa_module
*m
) {
427 struct device_info
*d
;
436 if (u
->sink_new_slot
)
437 pa_hook_slot_free(u
->sink_new_slot
);
438 if (u
->sink_unlink_slot
)
439 pa_hook_slot_free(u
->sink_unlink_slot
);
440 if (u
->sink_state_changed_slot
)
441 pa_hook_slot_free(u
->sink_state_changed_slot
);
443 if (u
->source_new_slot
)
444 pa_hook_slot_free(u
->source_new_slot
);
445 if (u
->source_unlink_slot
)
446 pa_hook_slot_free(u
->source_unlink_slot
);
447 if (u
->source_state_changed_slot
)
448 pa_hook_slot_free(u
->source_state_changed_slot
);
450 if (u
->sink_input_new_slot
)
451 pa_hook_slot_free(u
->sink_input_new_slot
);
452 if (u
->sink_input_unlink_slot
)
453 pa_hook_slot_free(u
->sink_input_unlink_slot
);
454 if (u
->sink_input_move_start_slot
)
455 pa_hook_slot_free(u
->sink_input_move_start_slot
);
456 if (u
->sink_input_move_finish_slot
)
457 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
458 if (u
->sink_input_state_changed_slot
)
459 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
461 if (u
->source_output_new_slot
)
462 pa_hook_slot_free(u
->source_output_new_slot
);
463 if (u
->source_output_unlink_slot
)
464 pa_hook_slot_free(u
->source_output_unlink_slot
);
465 if (u
->source_output_move_start_slot
)
466 pa_hook_slot_free(u
->source_output_move_start_slot
);
467 if (u
->source_output_move_finish_slot
)
468 pa_hook_slot_free(u
->source_output_move_finish_slot
);
469 if (u
->source_output_state_changed_slot
)
470 pa_hook_slot_free(u
->source_output_state_changed_slot
);
472 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
475 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);