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
, *sink_state_changed_slot
, *source_state_changed_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 && pa_sink_get_state(d
->sink
) != PA_SINK_SUSPENDED
) {
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 && pa_source_get_state(d
->source
) != PA_SOURCE_SUSPENDED
) {
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 static pa_hook_result_t
device_state_changed_hook_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
213 struct device_info
*d
;
216 pa_object_assert_ref(o
);
219 if (!(d
= pa_hashmap_get(u
->device_infos
, o
)))
222 if (pa_sink_isinstance(o
)) {
223 pa_sink
*s
= PA_SINK(o
);
225 if (pa_sink_used_by(s
) <= 0) {
226 pa_sink_state_t state
= pa_sink_get_state(s
);
228 if (state
== PA_SINK_RUNNING
|| state
== PA_SINK_IDLE
)
232 } else if (pa_source_isinstance(o
)) {
233 pa_source
*s
= PA_SOURCE(o
);
235 if (pa_source_used_by(s
) <= 0) {
236 pa_sink_state_t state
= pa_source_get_state(s
);
238 if (state
== PA_SINK_RUNNING
|| state
== PA_SINK_IDLE
)
246 int pa__init(pa_module
*m
) {
247 pa_modargs
*ma
= NULL
;
249 uint32_t timeout
= 5;
256 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
257 pa_log("Failed to parse module arguments.");
261 if (pa_modargs_get_value_u32(ma
, "timeout", &timeout
) < 0) {
262 pa_log("Failed to parse timeout value.");
266 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
268 u
->timeout
= timeout
;
269 u
->device_infos
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
271 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
272 device_new_hook_cb(m
->core
, PA_OBJECT(sink
), u
);
274 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
275 device_new_hook_cb(m
->core
, PA_OBJECT(source
), u
);
277 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
);
278 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
);
279 u
->sink_disconnect_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_DISCONNECT
], (pa_hook_cb_t
) device_disconnect_hook_cb
, u
);
280 u
->source_disconnect_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_DISCONNECT
], (pa_hook_cb_t
) device_disconnect_hook_cb
, u
);
281 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
);
282 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
);
284 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
);
285 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
);
286 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
);
287 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
);
300 void pa__done(pa_module
*m
) {
302 struct device_info
*d
;
311 if (u
->sink_new_slot
)
312 pa_hook_slot_free(u
->sink_new_slot
);
313 if (u
->sink_disconnect_slot
)
314 pa_hook_slot_free(u
->sink_disconnect_slot
);
315 if (u
->sink_state_changed_slot
)
316 pa_hook_slot_free(u
->sink_state_changed_slot
);
318 if (u
->source_new_slot
)
319 pa_hook_slot_free(u
->source_new_slot
);
320 if (u
->source_disconnect_slot
)
321 pa_hook_slot_free(u
->source_disconnect_slot
);
322 if (u
->source_state_changed_slot
)
323 pa_hook_slot_free(u
->source_state_changed_slot
);
325 if (u
->sink_input_new_slot
)
326 pa_hook_slot_free(u
->sink_input_new_slot
);
327 if (u
->sink_input_disconnect_slot
)
328 pa_hook_slot_free(u
->sink_input_disconnect_slot
);
330 if (u
->source_output_new_slot
)
331 pa_hook_slot_free(u
->source_output_new_slot
);
332 if (u
->source_output_disconnect_slot
)
333 pa_hook_slot_free(u
->source_output_disconnect_slot
);
335 while ((d
= pa_hashmap_steal_first(u
->device_infos
)))
338 pa_hashmap_free(u
->device_infos
, NULL
, NULL
);