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
;
53 pa_hook_slot
*sink_new_slot
, *source_new_slot
, *sink_disconnect_slot
, *source_disconnect_slot
;
54 pa_hook_slot
*sink_input_new_slot
, *source_output_new_slot
, *sink_input_disconnect_slot
, *source_output_disconnect_slot
;
58 struct userdata
*userdata
;
61 struct timeval last_use
;
62 pa_time_event
*time_event
;
65 static void timeout_cb(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
66 struct device_info
*d
= userdata
;
70 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
72 if (d
->sink
&& pa_sink_used_by(d
->sink
) <= 0) {
73 pa_log_info("Sink %s idle for too long, suspending ...", d
->sink
->name
);
74 pa_sink_suspend(d
->sink
, 1);
75 pa_source_suspend(d
->sink
->monitor_source
, 1);
78 if (d
->source
&& pa_source_used_by(d
->source
) <= 0) {
79 pa_log_info("Source %s idle for too long, suspending ...", d
->source
->name
);
80 pa_source_suspend(d
->source
, 1);
84 static void restart(struct device_info
*d
) {
90 pa_timeval_add(&tv
, d
->userdata
->timeout
*1000000);
91 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, &tv
);
94 pa_log_debug("Source %s becomes idle.", d
->source
->name
);
96 pa_log_debug("Sink %s becomes idle.", d
->sink
->name
);
99 static pa_hook_result_t
sink_input_new_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
100 struct device_info
*d
;
103 pa_sink_input_assert_ref(s
);
106 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)));
107 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
109 pa_sink_suspend(s
->sink
, 0);
110 pa_source_suspend(s
->sink
->monitor_source
, 0);
112 pa_log_debug("Sink %s becomes busy.", s
->sink
->name
);
117 static pa_hook_result_t
source_output_new_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
118 struct device_info
*d
;
121 pa_source_output_assert_ref(s
);
124 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->source
)));
125 d
->userdata
->core
->mainloop
->time_restart(d
->time_event
, NULL
);
127 pa_source_suspend(s
->source
, 0);
128 if (s
->source
->monitor_of
)
129 pa_sink_suspend(s
->source
->monitor_of
, 0);
131 pa_log_debug("Source %s becomes busy.", s
->source
->name
);
136 static pa_hook_result_t
sink_input_disconnect_hook_cb(pa_core
*c
, pa_sink_input
*s
, struct userdata
*u
) {
138 pa_sink_input_assert_ref(s
);
141 if (pa_sink_used_by(s
->sink
) <= 0) {
142 struct device_info
*d
;
143 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->sink
)));
150 static pa_hook_result_t
source_output_disconnect_hook_cb(pa_core
*c
, pa_source_output
*s
, struct userdata
*u
) {
152 pa_source_output_assert_ref(s
);
155 if (pa_source_used_by(s
->source
) <= 0) {
156 struct device_info
*d
;
157 pa_assert_se((d
= pa_hashmap_get(u
->device_infos
, s
->source
)));
164 static pa_hook_result_t
device_new_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
165 struct device_info
*d
;
168 pa_object_assert_ref(o
);
171 d
= pa_xnew(struct device_info
, 1);
173 d
->source
= pa_source_isinstance(o
) ? pa_source_ref(PA_SOURCE(o
)) : NULL
;
174 d
->sink
= pa_sink_isinstance(o
) ? pa_sink_ref(PA_SINK(o
)) : NULL
;
175 pa_assert(d
->source
|| d
->sink
);
176 d
->time_event
= c
->mainloop
->time_new(c
->mainloop
, NULL
, timeout_cb
, d
);
177 pa_hashmap_put(u
->device_infos
, o
, d
);
179 if ((d
->sink
&& pa_sink_used_by(d
->sink
) <= 0) ||
180 (d
->source
&& pa_source_used_by(d
->source
) <= 0))
186 static void device_info_free(struct device_info
*d
) {
190 pa_source_unref(d
->source
);
192 pa_sink_unref(d
->sink
);
194 d
->userdata
->core
->mainloop
->time_free(d
->time_event
);
199 static pa_hook_result_t
device_disconnect_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
200 struct device_info
*d
;
203 pa_object_assert_ref(o
);
206 pa_assert_se((d
= pa_hashmap_remove(u
->device_infos
, o
)));
212 int pa__init(pa_module
*m
) {
213 pa_modargs
*ma
= NULL
;
215 uint32_t timeout
= 5;
222 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
223 pa_log("Failed to parse module arguments.");
227 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
228 pa_log("Failed to parse timeout value.");
232 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
234 u
->timeout
= timeout
;
235 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
237 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
238 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
240 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
241 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
243 u
->sink_new_slot
= pa_hook_connect(&m
->core
->hook_sink_new_post
, (pa_hook_cb_t
) device_new_hook_cb
, u
);
244 u
->source_new_slot
= pa_hook_connect(&m
->core
->hook_source_new_post
, (pa_hook_cb_t
) device_new_hook_cb
, u
);
245 u
->sink_disconnect_slot
= pa_hook_connect(&m
->core
->hook_sink_disconnect_post
, (pa_hook_cb_t
) device_disconnect_hook_cb
, u
);
246 u
->source_disconnect_slot
= pa_hook_connect(&m
->core
->hook_source_disconnect_post
, (pa_hook_cb_t
) device_disconnect_hook_cb
, u
);
248 u
->sink_input_new_slot
= pa_hook_connect(&m
->core
->hook_sink_input_new_post
, (pa_hook_cb_t
) sink_input_new_hook_cb
, u
);
249 u
->source_output_new_slot
= pa_hook_connect(&m
->core
->hook_source_output_new_post
, (pa_hook_cb_t
) source_output_new_hook_cb
, u
);
250 u
->sink_input_disconnect_slot
= pa_hook_connect(&m
->core
->hook_sink_input_disconnect_post
, (pa_hook_cb_t
) sink_input_disconnect_hook_cb
, u
);
251 u
->source_output_disconnect_slot
= pa_hook_connect(&m
->core
->hook_source_output_disconnect_post
, (pa_hook_cb_t
) source_output_disconnect_hook_cb
, u
);
264 void pa__done(pa_module
*m
) {
266 struct device_info
*d
;
275 if (u
->sink_new_slot
)
276 pa_hook_slot_free(u
->sink_new_slot
);
277 if (u
->sink_disconnect_slot
)
278 pa_hook_slot_free(u
->sink_disconnect_slot
);
280 if (u
->source_new_slot
)
281 pa_hook_slot_free(u
->source_new_slot
);
282 if (u
->source_disconnect_slot
)
283 pa_hook_slot_free(u
->source_disconnect_slot
);
285 if (u
->sink_input_new_slot
)
286 pa_hook_slot_free(u
->sink_input_new_slot
);
287 if (u
->sink_input_disconnect_slot
)
288 pa_hook_slot_free(u
->sink_input_disconnect_slot
);
290 if (u
->source_output_new_slot
)
291 pa_hook_slot_free(u
->source_output_new_slot
);
292 if (u
->source_output_disconnect_slot
)
293 pa_hook_slot_free(u
->source_output_disconnect_slot
);
295 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
298 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);