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
);
43 PA_MODULE_LOAD_ONCE(TRUE
);
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_slot
,
68 *source_output_move_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_used_by(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_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
, 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
);
163 if (pa_sink_used_by(s
->sink
) <= 0) {
164 struct device_info
*d
;
165 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
172 static pa_hook_result_t
source_output_unlink_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
174 pa_source_output_assert_ref(s
);
177 if (pa_source_used_by(s
->source
) <= 0) {
178 struct device_info
*d
;
179 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
186 static pa_hook_result_t
sink_input_move_hook_cb(pa_core
*c
, pa_sink_input_move_hook_data
*data
, struct userdata
*u
) {
187 struct device_info
*d
;
193 if ((d
= pa_hashmap_get(u
->device_infos
, data
->destination
)))
196 if (pa_sink_used_by(data
->sink_input
->sink
) <= 1)
197 if ((d
= pa_hashmap_get(u
->device_infos
, data
->sink_input
->sink
)))
203 static pa_hook_result_t
source_output_move_hook_cb(pa_core
*c
, pa_source_output_move_hook_data
*data
, struct userdata
*u
) {
204 struct device_info
*d
;
210 if ((d
= pa_hashmap_get(u
->device_infos
, data
->destination
)))
213 if (pa_source_used_by(data
->source_output
->source
) <= 1)
214 if ((d
= pa_hashmap_get(u
->device_infos
, data
->source_output
->source
)))
220 static pa_hook_result_t
sink_input_state_changed_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
221 struct device_info
*d
;
222 pa_sink_input_state_t state
;
224 pa_sink_input_assert_ref(s
);
227 state
= pa_sink_input_get_state(s
);
228 if (state
== PA_SINK_INPUT_RUNNING
|| state
== PA_SINK_INPUT_DRAINED
)
229 if ((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)))
235 static pa_hook_result_t
source_output_state_changed_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
236 struct device_info
*d
;
237 pa_source_output_state_t state
;
239 pa_source_output_assert_ref(s
);
242 state
= pa_source_output_get_state(s
);
243 if (state
== PA_SOURCE_OUTPUT_RUNNING
)
244 if ((d
= pa_hashmap_get(u
->device_infos
, s
->source
)))
250 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
251 struct device_info
*d
;
256 pa_object_assert_ref(o
);
259 source
= pa_source_isinstance(o
) ? PA_SOURCE(o
) : NULL
;
260 sink
= pa_sink_isinstance(o
) ? PA_SINK(o
) : NULL
;
262 pa_assert(source
|| sink
);
264 d
= pa_xnew(struct device_info
, 1);
266 d
->source
= source
? pa_source_ref(source
) : NULL
;
267 d
->sink
= sink
? pa_sink_ref(sink
) : NULL
;
268 d
->time_event
= c
->mainloop
->time_new(c
->mainloop
, NULL
, timeout_cb
, d
);
269 pa_hashmap_put(u
->device_infos
, o
, d
);
271 if ((d
->sink
&& pa_sink_used_by(d
->sink
) <= 0) ||
272 (d
->source
&& pa_source_used_by(d
->source
) <= 0))
278 static void device_info_free(struct device_info
*d
) {
282 pa_source_unref(d
->source
);
284 pa_sink_unref(d
->sink
);
286 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
291 static pa_hook_result_t
device_unlink_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
292 struct device_info
*d
;
295 pa_object_assert_ref(o
);
298 if ((d
= pa_hashmap_remove(u
->device_infos
, o
)))
304 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
305 struct device_info
*d
;
308 pa_object_assert_ref(o
);
311 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
314 if (pa_sink_isinstance(o
)) {
315 pa_sink
*s
= PA_SINK(o
);
316 pa_sink_state_t state
= pa_sink_get_state(s
);
318 if (pa_sink_used_by(s
) <= 0) {
320 if (PA_SINK_OPENED(state
))
325 } else if (pa_source_isinstance(o
)) {
326 pa_source
*s
= PA_SOURCE(o
);
327 pa_source_state_t state
= pa_source_get_state(s
);
329 if (pa_source_used_by(s
) <= 0) {
331 if (PA_SOURCE_OPENED(state
))
339 int pa__init(pa_module
*m
) {
340 pa_modargs
*ma
= NULL
;
342 uint32_t timeout
= 1;
349 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
350 pa_log("Failed to parse module arguments.");
354 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
355 pa_log("Failed to parse timeout value.");
359 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
361 u
->timeout
= timeout
;
362 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
364 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
365 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
367 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
368 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
370 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
);
371 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
);
372 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
);
373 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
);
374 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
);
375 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
);
377 u
->sink_input_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_FIXATE
], (pa_hook_cb_t
) sink_input_fixate_hook_cb
, u
);
378 u
->source_output_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE
], (pa_hook_cb_t
) source_output_fixate_hook_cb
, u
);
379 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
);
380 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
);
381 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
);
382 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
);
383 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
);
384 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
);
397 void pa__done(pa_module
*m
) {
399 struct device_info
*d
;
408 if (u
->sink_new_slot
)
409 pa_hook_slot_free(u
->sink_new_slot
);
410 if (u
->sink_unlink_slot
)
411 pa_hook_slot_free(u
->sink_unlink_slot
);
412 if (u
->sink_state_changed_slot
)
413 pa_hook_slot_free(u
->sink_state_changed_slot
);
415 if (u
->source_new_slot
)
416 pa_hook_slot_free(u
->source_new_slot
);
417 if (u
->source_unlink_slot
)
418 pa_hook_slot_free(u
->source_unlink_slot
);
419 if (u
->source_state_changed_slot
)
420 pa_hook_slot_free(u
->source_state_changed_slot
);
422 if (u
->sink_input_new_slot
)
423 pa_hook_slot_free(u
->sink_input_new_slot
);
424 if (u
->sink_input_unlink_slot
)
425 pa_hook_slot_free(u
->sink_input_unlink_slot
);
426 if (u
->sink_input_move_slot
)
427 pa_hook_slot_free(u
->sink_input_move_slot
);
428 if (u
->sink_input_state_changed_slot
)
429 pa_hook_slot_free(u
->sink_input_state_changed_slot
);
431 if (u
->source_output_new_slot
)
432 pa_hook_slot_free(u
->source_output_new_slot
);
433 if (u
->source_output_unlink_slot
)
434 pa_hook_slot_free(u
->source_output_unlink_slot
);
435 if (u
->source_output_move_slot
)
436 pa_hook_slot_free(u
->source_output_move_slot
);
437 if (u
->source_output_state_changed_slot
)
438 pa_hook_slot_free(u
->source_output_state_changed_slot
);
440 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
443 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);