]> code.delx.au - pulseaudio/blob - src/modules/module-rescue-streams.c
dbusiface-stream: Implement about a half of the Stream D-Bus interface.
[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 their 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 *sink_slot, *source_slot;
49 };
50
51 static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
52 pa_sink_input *i;
53 uint32_t idx;
54 pa_sink *target;
55
56 pa_assert(c);
57 pa_assert(sink);
58
59 /* There's no point in doing anything if the core is shut down anyway */
60 if (c->state == PA_CORE_SHUTDOWN)
61 return PA_HOOK_OK;
62
63 if (pa_idxset_size(sink->inputs) <= 0) {
64 pa_log_debug("No sink inputs to move away.");
65 return PA_HOOK_OK;
66 }
67
68 if (!(target = pa_namereg_get_default_sink(c)) || target == sink) {
69
70 PA_IDXSET_FOREACH(target, c->sinks, idx)
71 if (target != sink)
72 break;
73
74 if (!target) {
75 pa_log_debug("No evacuation sink found.");
76 return PA_HOOK_OK;
77 }
78 }
79
80 pa_assert(target != sink);
81
82 PA_IDXSET_FOREACH(i, sink->inputs, idx) {
83 if (pa_sink_input_move_to(i, target, FALSE) < 0)
84 pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index,
85 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
86 else
87 pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index,
88 pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
89 }
90
91 return PA_HOOK_OK;
92 }
93
94 static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) {
95 pa_source_output *o;
96 pa_source *target;
97 uint32_t idx;
98
99 pa_assert(c);
100 pa_assert(source);
101
102 /* There's no point in doing anything if the core is shut down anyway */
103 if (c->state == PA_CORE_SHUTDOWN)
104 return PA_HOOK_OK;
105
106 if (pa_idxset_size(source->outputs) <= 0) {
107 pa_log_debug("No source outputs to move away.");
108 return PA_HOOK_OK;
109 }
110
111 if (!(target = pa_namereg_get_default_source(c)) || target == source) {
112
113 PA_IDXSET_FOREACH(target, c->sources, idx)
114 if (target != source && !target->monitor_of == !source->monitor_of)
115 break;
116
117 if (!target) {
118 pa_log_info("No evacuation source found.");
119 return PA_HOOK_OK;
120 }
121 }
122
123 pa_assert(target != source);
124
125 PA_IDXSET_FOREACH(o, source->outputs, idx) {
126 if (pa_source_output_move_to(o, target, FALSE) < 0)
127 pa_log_info("Failed to move source output %u \"%s\" to %s.", o->index,
128 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
129 else
130 pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index,
131 pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name);
132 }
133
134 return PA_HOOK_OK;
135 }
136
137 int pa__init(pa_module*m) {
138 pa_modargs *ma;
139 struct userdata *u;
140
141 pa_assert(m);
142
143 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
144 pa_log("Failed to parse module arguments");
145 return -1;
146 }
147
148 m->userdata = u = pa_xnew(struct userdata, 1);
149
150 /* A little bit later than module-stream-restore, module-intended-roles... */
151 u->sink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_hook_callback, u);
152 u->source_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) source_hook_callback, u);
153
154 pa_modargs_free(ma);
155 return 0;
156 }
157
158 void pa__done(pa_module*m) {
159 struct userdata *u;
160
161 pa_assert(m);
162
163 if (!(u = m->userdata))
164 return;
165
166 if (u->sink_slot)
167 pa_hook_slot_free(u->sink_slot);
168 if (u->source_slot)
169 pa_hook_slot_free(u->source_slot);
170
171 pa_xfree(u);
172 }