2 This file is part of PulseAudio.
4 Copyright 2008 Colin Guthrie
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>
28 #include <pulsecore/core.h>
29 #include <pulsecore/sink-input.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/namereg.h>
33 #include <pulsecore/core-util.h>
35 #include "module-always-sink-symdef.h"
37 PA_MODULE_AUTHOR("Colin Guthrie");
38 PA_MODULE_DESCRIPTION("Always keeps at least one sink loaded even if it's a null one");
39 PA_MODULE_VERSION(PACKAGE_VERSION
);
40 PA_MODULE_LOAD_ONCE(TRUE
);
42 "sink_name=<name of sink>");
44 #define DEFAULT_SINK_NAME "auto_null"
46 static const char* const valid_modargs
[] = {
52 pa_hook_slot
*put_slot
, *unlink_slot
;
58 static void load_null_sink_if_needed(pa_core
*c
, pa_sink
*sink
, struct userdata
* u
) {
66 pa_assert(u
->null_module
== PA_INVALID_INDEX
);
68 /* Loop through all sinks and check to see if we have *any*
69 * sinks. Ignore the sink passed in (if it's not null) */
70 for (target
= pa_idxset_first(c
->sinks
, &idx
); target
; target
= pa_idxset_next(c
->sinks
, &idx
))
71 if (!sink
|| target
!= sink
)
77 pa_log_debug("Autoloading null-sink as no other sinks detected.");
81 t
= pa_sprintf_malloc("sink_name=%s", u
->sink_name
);
82 m
= pa_module_load(c
, "module-null-sink", t
);
83 u
->null_module
= m
? m
->index
: PA_INVALID_INDEX
;
89 pa_log_warn("Unable to load module-null-sink");
92 static pa_hook_result_t
put_hook_callback(pa_core
*c
, pa_sink
*sink
, void* userdata
) {
93 struct userdata
*u
= userdata
;
99 /* This is us detecting ourselves on load... just ignore this. */
103 /* There's no point in doing anything if the core is shut down anyway */
104 if (c
->state
== PA_CORE_SHUTDOWN
)
107 /* Auto-loaded null-sink not active, so ignoring newly detected sink. */
108 if (u
->null_module
== PA_INVALID_INDEX
)
111 /* This is us detecting ourselves on load in a different way... just ignore this too. */
112 if (sink
->module
&& sink
->module
->index
== u
->null_module
)
115 pa_log_info("A new sink has been discovered. Unloading null-sink.");
117 pa_module_unload_request_by_index(c
, u
->null_module
, TRUE
);
118 u
->null_module
= PA_INVALID_INDEX
;
123 static pa_hook_result_t
unlink_hook_callback(pa_core
*c
, pa_sink
*sink
, void* userdata
) {
124 struct userdata
*u
= userdata
;
130 /* First check to see if it's our own null-sink that's been removed... */
131 if (u
->null_module
!= PA_INVALID_INDEX
&& sink
->module
&& sink
->module
->index
== u
->null_module
) {
132 pa_log_debug("Autoloaded null-sink removed");
133 u
->null_module
= PA_INVALID_INDEX
;
137 /* There's no point in doing anything if the core is shut down anyway */
138 if (c
->state
== PA_CORE_SHUTDOWN
)
141 load_null_sink_if_needed(c
, sink
, u
);
146 int pa__init(pa_module
*m
) {
147 pa_modargs
*ma
= NULL
;
152 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
153 pa_log("Failed to parse module arguments");
157 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
158 u
->sink_name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
));
159 u
->put_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) put_hook_callback
, u
);
160 u
->unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], PA_HOOK_EARLY
, (pa_hook_cb_t
) unlink_hook_callback
, u
);
161 u
->null_module
= PA_INVALID_INDEX
;
166 load_null_sink_if_needed(m
->core
, NULL
, u
);
171 void pa__done(pa_module
*m
) {
176 if (!(u
= m
->userdata
))
180 pa_hook_slot_free(u
->put_slot
);
182 pa_hook_slot_free(u
->unlink_slot
);
183 if (u
->null_module
!= PA_INVALID_INDEX
&& m
->core
->state
!= PA_CORE_SHUTDOWN
)
184 pa_module_unload_request_by_index(m
->core
, u
->null_module
, TRUE
);
186 pa_xfree(u
->sink_name
);