]> code.delx.au - pulseaudio/blob - src/modules/module-rescue-streams.c
alsa-mixer: Add surround 2.1 profile
[pulseaudio] / src / modules / module-rescue-streams.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
28 #include <pulsecore/core.h>
29 #include <pulsecore/sink-input.h>
30 #include <pulsecore/source-output.h>
31 #include <pulsecore/modargs.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/namereg.h>
34 #include <pulsecore/core-util.h>
35
36 #include "module-rescue-streams-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("When a sink/source is removed, try to move its streams to the default sink/source");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(true);
42
43 static const char* const valid_modargs[] = {
44 NULL,
45 };
46
47 struct userdata {
48 pa_hook_slot
49 *sink_unlink_slot,
50 *source_unlink_slot,
51 *sink_input_move_fail_slot,
52 *source_output_move_fail_slot;
53 };
54
55 static pa_sink* find_evacuation_sink(pa_core *c, pa_sink_input *i, pa_sink *skip) {
56 pa_sink *target, *def;
57 uint32_t idx;
58
59 pa_assert(c);
60 pa_assert(i);
61
62 def = pa_namereg_get_default_sink(c);
63
64 if (def && def != skip && pa_sink_input_may_move_to(i, def))
65 return def;
66
67 PA_IDXSET_FOREACH(target, c->sinks, idx) {
68 if (target == def)
69 continue;
70
71 if (target == skip)
72 continue;
73
74 if (!PA_SINK_IS_LINKED(pa_sink_get_state(target)))
75 continue;
76
77 if (pa_sink_input_may_move_to(i, target))
78 return target;
79 }
80
81 pa_log_debug("No evacuation sink found.");
82 return NULL;
83 }
84
85 static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
86 pa_sink_input *i;
87 uint32_t idx;
88
89 pa_assert(c);
90 pa_assert(sink);
91
92 /* There's no point in doing anything if the core is shut down anyway */
93 if (c->state == PA_CORE_SHUTDOWN)
94 return PA_HOOK_OK;
95
96 if (pa_idxset_size(sink->inputs) <= 0) {
97 pa_log_debug("No sink inputs to move away.");
98 return PA_HOOK_OK;
99 }
100
101 PA_IDXSET_FOREACH(i, sink->inputs, idx) {
102 pa_sink *target;
103
104 if (!(target = find_evacuation_sink(c, i, sink)))
105 continue;
106
107 if (pa_sink_input_move_to(i, target, false) < 0)
108 pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
109 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
110 else
111 pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
112 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
113 }
114
115 return PA_HOOK_OK;
116 }
117
118 static pa_hook_result_t sink_input_move_fail_hook_callback(pa_core *c, pa_sink_input *i, void *userdata) {
119 pa_sink *target;
120
121 pa_assert(c);
122 pa_assert(i);
123
124 /* There's no point in doing anything if the core is shut down anyway */
125 if (c->state == PA_CORE_SHUTDOWN)
126 return PA_HOOK_OK;
127
128 if (!(target = find_evacuation_sink(c, i, NULL)))
129 return PA_HOOK_OK;
130
131 if (pa_sink_input_finish_move(i, target, false) < 0) {
132 pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
133 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
134 return PA_HOOK_OK;
135
136 } else {
137 pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
138 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
139 return PA_HOOK_STOP;
140 }
141 }
142
143 static pa_source* find_evacuation_source(pa_core *c, pa_source_output *o, pa_source *skip) {
144 pa_source *target, *def;
145 uint32_t idx;
146
147 pa_assert(c);
148 pa_assert(o);
149
150 def = pa_namereg_get_default_source(c);
151
152 if (def && def != skip && pa_source_output_may_move_to(o, def))
153 return def;
154
155 PA_IDXSET_FOREACH(target, c->sources, idx) {
156 if (target == def)
157 continue;
158
159 if (target == skip)
160 continue;
161
162 if (skip && !target->monitor_of != !skip->monitor_of)
163 continue;
164
165 if (!PA_SOURCE_IS_LINKED(pa_source_get_state(target)))
166 continue;
167
168 if (pa_source_output_may_move_to(o, target))
169 return target;
170 }
171
172 pa_log_debug("No evacuation source found.");
173 return NULL;
174 }
175
176 static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, void* userdata) {
177 pa_source_output *o;
178 uint32_t idx;
179
180 pa_assert(c);
181 pa_assert(source);
182
183 /* There's no point in doing anything if the core is shut down anyway */
184 if (c->state == PA_CORE_SHUTDOWN)
185 return PA_HOOK_OK;
186
187 if (pa_idxset_size(source->outputs) <= 0) {
188 pa_log_debug("No source outputs to move away.");
189 return PA_HOOK_OK;
190 }
191
192 PA_IDXSET_FOREACH(o, source->outputs, idx) {
193 pa_source *target;
194
195 if (!(target = find_evacuation_source(c, o, source)))
196 continue;
197
198 if (pa_source_output_move_to(o, target, false) < 0)
199 pa_log_info("Failed to move source output %u \"%s\" to %s.", o->index,
200 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
201 else
202 pa_log_info("Successfully moved source output %u \"%s\" to %s.", o->index,
203 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
204 }
205
206 return PA_HOOK_OK;
207 }
208
209 static pa_hook_result_t source_output_move_fail_hook_callback(pa_core *c, pa_source_output *i, void *userdata) {
210 pa_source *target;
211
212 pa_assert(c);
213 pa_assert(i);
214
215 /* There's no point in doing anything if the core is shut down anyway */
216 if (c->state == PA_CORE_SHUTDOWN)
217 return PA_HOOK_OK;
218
219 if (!(target = find_evacuation_source(c, i, NULL)))
220 return PA_HOOK_OK;
221
222 if (pa_source_output_finish_move(i, target, false) < 0) {
223 pa_log_info("Failed to move source input %u \"%s\" to %s.", i->index,
224 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
225 return PA_HOOK_OK;
226
227 } else {
228 pa_log_info("Successfully moved source input %u \"%s\" to %s.", i->index,
229 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
230 return PA_HOOK_STOP;
231 }
232 }
233
234 int pa__init(pa_module*m) {
235 pa_modargs *ma;
236 struct userdata *u;
237
238 pa_assert(m);
239
240 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
241 pa_log("Failed to parse module arguments");
242 return -1;
243 }
244
245 m->userdata = u = pa_xnew(struct userdata, 1);
246
247 /* A little bit later than module-stream-restore, module-intended-roles... */
248 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_unlink_hook_callback, u);
249 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) source_unlink_hook_callback, u);
250
251 u->sink_input_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_input_move_fail_hook_callback, u);
252 u->source_output_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) source_output_move_fail_hook_callback, u);
253
254 pa_modargs_free(ma);
255 return 0;
256 }
257
258 void pa__done(pa_module*m) {
259 struct userdata *u;
260
261 pa_assert(m);
262
263 if (!(u = m->userdata))
264 return;
265
266 if (u->sink_unlink_slot)
267 pa_hook_slot_free(u->sink_unlink_slot);
268 if (u->source_unlink_slot)
269 pa_hook_slot_free(u->source_unlink_slot);
270
271 if (u->sink_input_move_fail_slot)
272 pa_hook_slot_free(u->sink_input_move_fail_slot);
273 if (u->source_output_move_fail_slot)
274 pa_hook_slot_free(u->source_output_move_fail_slot);
275
276 pa_xfree(u);
277 }