2
This file is part of PulseAudio.
4
Copyright 2006 Lennart Poettering
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, see <http://www.gnu.org/licenses/>.
24
#include <pulse/xmalloc.h>
26
#include <pulsecore/core.h>
27
#include <pulsecore/sink-input.h>
28
#include <pulsecore/source-output.h>
29
#include <pulsecore/modargs.h>
30
#include <pulsecore/log.h>
31
#include <pulsecore/namereg.h>
32
#include <pulsecore/core-util.h>
34
#include "module-rescue-streams-symdef.h"
36
PA_MODULE_AUTHOR("Lennart Poettering");
37
PA_MODULE_DESCRIPTION("When a sink/source is removed, try to move its streams to the default sink/source");
38
PA_MODULE_VERSION(PACKAGE_VERSION);
39
PA_MODULE_LOAD_ONCE(true);
41
static const char* const valid_modargs[] = {
49
*sink_input_move_fail_slot,
50
*source_output_move_fail_slot;
53
static pa_source* find_source_from_port(pa_core *c, pa_device_port *port) {
62
PA_IDXSET_FOREACH(target, c->sources, idx)
63
PA_HASHMAP_FOREACH(p, target->ports, state)
70
static pa_sink* find_sink_from_port(pa_core *c, pa_device_port *port) {
79
PA_IDXSET_FOREACH(target, c->sinks, idx)
80
PA_HASHMAP_FOREACH(p, target->ports, state)
87
static void build_group_ports(pa_hashmap *g_ports, pa_hashmap *s_ports) {
91
if (!g_ports || !s_ports)
94
PA_HASHMAP_FOREACH(p, s_ports, state)
95
pa_hashmap_put(g_ports, p, p);
98
static pa_sink* find_evacuation_sink(pa_core *c, pa_sink_input *i, pa_sink *skip) {
99
pa_sink *target, *def, *fb_sink = NULL;
101
pa_hashmap *all_ports;
102
pa_device_port *best_port;
107
def = pa_namereg_get_default_sink(c, skip);
109
if (def && pa_sink_input_may_move_to(i, def))
112
all_ports = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
114
PA_IDXSET_FOREACH(target, c->sinks, idx) {
121
if (!PA_SINK_IS_LINKED(pa_sink_get_state(target)))
124
if (!pa_sink_input_may_move_to(i, target))
130
build_group_ports(all_ports, target->ports);
133
best_port = pa_device_port_find_best(all_ports);
135
pa_hashmap_free(all_ports);
138
target = find_sink_from_port(c, best_port);
143
pa_log_debug("No evacuation sink found.");
148
static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
155
/* There's no point in doing anything if the core is shut down anyway */
156
if (c->state == PA_CORE_SHUTDOWN)
159
if (pa_idxset_size(sink->inputs) <= 0) {
160
pa_log_debug("No sink inputs to move away.");
164
PA_IDXSET_FOREACH(i, sink->inputs, idx) {
167
if (!(target = find_evacuation_sink(c, i, sink)))
170
if (pa_sink_input_move_to(i, target, false) < 0)
171
pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
172
pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
174
pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
175
pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
181
static pa_hook_result_t sink_input_move_fail_hook_callback(pa_core *c, pa_sink_input *i, void *userdata) {
187
/* There's no point in doing anything if the core is shut down anyway */
188
if (c->state == PA_CORE_SHUTDOWN)
191
if (!(target = find_evacuation_sink(c, i, NULL)))
194
if (pa_sink_input_finish_move(i, target, false) < 0) {
195
pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
196
pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
200
pa_log_info("Successfully moved sink input %u \"%s\" to %s.", i->index,
201
pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
206
static pa_source* find_evacuation_source(pa_core *c, pa_source_output *o, pa_source *skip) {
207
pa_source *target, *def, *fb_source = NULL;
209
pa_hashmap *all_ports;
210
pa_device_port *best_port;
215
def = pa_namereg_get_default_source(c);
217
if (def && def != skip && pa_source_output_may_move_to(o, def))
220
all_ports = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
222
PA_IDXSET_FOREACH(target, c->sources, idx) {
229
if (skip && !target->monitor_of != !skip->monitor_of)
232
if (!PA_SOURCE_IS_LINKED(pa_source_get_state(target)))
235
if (!pa_source_output_may_move_to(o, target))
241
build_group_ports(all_ports, target->ports);
244
best_port = pa_device_port_find_best(all_ports);
246
pa_hashmap_free(all_ports);
249
target = find_source_from_port(c, best_port);
254
pa_log_debug("No evacuation source found.");
259
static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, void* userdata) {
266
/* There's no point in doing anything if the core is shut down anyway */
267
if (c->state == PA_CORE_SHUTDOWN)
270
if (pa_idxset_size(source->outputs) <= 0) {
271
pa_log_debug("No source outputs to move away.");
275
PA_IDXSET_FOREACH(o, source->outputs, idx) {
278
if (!(target = find_evacuation_source(c, o, source)))
281
if (pa_source_output_move_to(o, target, false) < 0)
282
pa_log_info("Failed to move source output %u \"%s\" to %s.", o->index,
283
pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
285
pa_log_info("Successfully moved source output %u \"%s\" to %s.", o->index,
286
pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
292
static pa_hook_result_t source_output_move_fail_hook_callback(pa_core *c, pa_source_output *i, void *userdata) {
298
/* There's no point in doing anything if the core is shut down anyway */
299
if (c->state == PA_CORE_SHUTDOWN)
302
if (!(target = find_evacuation_source(c, i, NULL)))
305
if (pa_source_output_finish_move(i, target, false) < 0) {
306
pa_log_info("Failed to move source input %u \"%s\" to %s.", i->index,
307
pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
311
pa_log_info("Successfully moved source input %u \"%s\" to %s.", i->index,
312
pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
317
int pa__init(pa_module*m) {
323
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
324
pa_log("Failed to parse module arguments");
328
m->userdata = u = pa_xnew(struct userdata, 1);
330
/* A little bit later than module-stream-restore, module-intended-roles... */
331
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);
332
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);
334
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);
335
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);
341
void pa__done(pa_module*m) {
346
if (!(u = m->userdata))
349
if (u->sink_unlink_slot)
350
pa_hook_slot_free(u->sink_unlink_slot);
351
if (u->source_unlink_slot)
352
pa_hook_slot_free(u->source_unlink_slot);
354
if (u->sink_input_move_fail_slot)
355
pa_hook_slot_free(u->sink_input_move_fail_slot);
356
if (u->source_output_move_fail_slot)
357
pa_hook_slot_free(u->source_output_move_fail_slot);