]> code.delx.au - pulseaudio/blob - src/modules/module-rescue-streams.c
Merge commit 'origin/master-tx'
[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 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
35 #include "module-rescue-streams-symdef.h"
36
37 PA_MODULE_AUTHOR("Lennart Poettering");
38 PA_MODULE_DESCRIPTION("When a sink/source is removed, try to move their streams to the default sink/source");
39 PA_MODULE_VERSION(PACKAGE_VERSION);
40 PA_MODULE_LOAD_ONCE(TRUE);
41
42 static const char* const valid_modargs[] = {
43 NULL,
44 };
45
46 struct userdata {
47 pa_hook_slot *sink_slot, *source_slot;
48 };
49
50 static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
51 pa_sink_input *i;
52 pa_sink *target;
53
54 pa_assert(c);
55 pa_assert(sink);
56
57 /* There's no point in doing anything if the core is shut down anyway */
58 if (c->state == PA_CORE_SHUTDOWN)
59 return PA_HOOK_OK;
60
61 if (!pa_idxset_size(sink->inputs)) {
62 pa_log_debug("No sink inputs to move away.");
63 return PA_HOOK_OK;
64 }
65
66 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)) || target == sink) {
67 uint32_t idx;
68
69 for (target = pa_idxset_first(c->sinks, &idx); target; target = pa_idxset_next(c->sinks, &idx))
70 if (target != sink)
71 break;
72
73 if (!target) {
74 pa_log_info("No evacuation sink found.");
75 return PA_HOOK_OK;
76 }
77 }
78
79 while ((i = pa_idxset_first(sink->inputs, NULL))) {
80 if (pa_sink_input_move_to(i, target, FALSE) < 0) {
81 pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i->index, pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME), target->name);
82 return PA_HOOK_OK;
83 }
84
85 pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME), target->name);
86 }
87
88
89 return PA_HOOK_OK;
90 }
91
92 static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) {
93 pa_source_output *o;
94 pa_source *target;
95
96 pa_assert(c);
97 pa_assert(source);
98
99 /* There's no point in doing anything if the core is shut down anyway */
100 if (c->state == PA_CORE_SHUTDOWN)
101 return PA_HOOK_OK;
102
103 if (!pa_idxset_size(source->outputs)) {
104 pa_log_debug("No source outputs to move away.");
105 return PA_HOOK_OK;
106 }
107
108 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SOURCE)) || target == source) {
109 uint32_t idx;
110
111 for (target = pa_idxset_first(c->sources, &idx); target; target = pa_idxset_next(c->sources, &idx))
112 if (target != source && !target->monitor_of == !source->monitor_of)
113 break;
114
115 if (!target) {
116 pa_log_info("No evacuation source found.");
117 return PA_HOOK_OK;
118 }
119 }
120
121 pa_assert(target != source);
122
123 while ((o = pa_idxset_first(source->outputs, NULL))) {
124 if (pa_source_output_move_to(o, target, FALSE) < 0) {
125 pa_log_warn("Failed to move source output %u \"%s\" to %s.", o->index, pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME), target->name);
126 return PA_HOOK_OK;
127 }
128
129 pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME), target->name);
130 }
131
132
133 return PA_HOOK_OK;
134 }
135
136 int pa__init(pa_module*m) {
137 pa_modargs *ma = NULL;
138 struct userdata *u;
139
140 pa_assert(m);
141
142 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
143 pa_log("Failed to parse module arguments");
144 return -1;
145 }
146
147 m->userdata = u = pa_xnew(struct userdata, 1);
148 u->sink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_hook_callback, NULL);
149 u->source_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_hook_callback, NULL);
150
151 pa_modargs_free(ma);
152 return 0;
153 }
154
155 void pa__done(pa_module*m) {
156 struct userdata *u;
157
158 pa_assert(m);
159
160 if (!m->userdata)
161 return;
162
163 u = m->userdata;
164 if (u->sink_slot)
165 pa_hook_slot_free(u->sink_slot);
166 if (u->source_slot)
167 pa_hook_slot_free(u->source_slot);
168
169 pa_xfree(u);
170 }