]> code.delx.au - pulseaudio/blob - src/modules/module-rescue-streams.c
extend module-rescue-streams to move also source outputs when a source dies
[pulseaudio] / src / modules / module-rescue-streams.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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
41 static const char* const valid_modargs[] = {
42 NULL,
43 };
44
45 struct userdata {
46 pa_hook_slot *sink_slot, *source_slot;
47 };
48
49 static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
50 pa_sink_input *i;
51 pa_sink *target;
52
53 assert(c);
54 assert(sink);
55
56 if (!pa_idxset_size(sink->inputs)) {
57 pa_log_debug(__FILE__": No sink inputs to move away.");
58 return PA_HOOK_OK;
59 }
60
61 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SINK, 0))) {
62 pa_log_info(__FILE__": No evacuation sink found.");
63 return PA_HOOK_OK;
64 }
65
66 assert(target != sink);
67
68 while ((i = pa_idxset_first(sink->inputs, NULL))) {
69 if (pa_sink_input_move_to(i, target, 1) < 0) {
70 pa_log_warn(__FILE__": Failed to move sink input %u \"%s\" to %s.", i->index, i->name, target->name);
71 return PA_HOOK_OK;
72 }
73
74 pa_log_info(__FILE__": Sucessfully moved sink input %u \"%s\" to %s.", i->index, i->name, target->name);
75 }
76
77
78 return PA_HOOK_OK;
79 }
80
81 static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) {
82 pa_source_output *o;
83 pa_source *target;
84
85 assert(c);
86 assert(source);
87
88 if (!pa_idxset_size(source->outputs)) {
89 pa_log_debug(__FILE__": No source outputs to move away.");
90 return PA_HOOK_OK;
91 }
92
93 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SOURCE, 0))) {
94 pa_log_info(__FILE__": No evacuation source found.");
95 return PA_HOOK_OK;
96 }
97
98 assert(target != source);
99
100 while ((o = pa_idxset_first(source->outputs, NULL))) {
101 if (pa_source_output_move_to(o, target) < 0) {
102 pa_log_warn(__FILE__": Failed to move source output %u \"%s\" to %s.", o->index, o->name, target->name);
103 return PA_HOOK_OK;
104 }
105
106 pa_log_info(__FILE__": Sucessfully moved source output %u \"%s\" to %s.", o->index, o->name, target->name);
107 }
108
109
110 return PA_HOOK_OK;
111 }
112
113 int pa__init(pa_core *c, pa_module*m) {
114 pa_modargs *ma = NULL;
115 struct userdata *u;
116
117 assert(c);
118 assert(m);
119
120 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
121 pa_log(__FILE__": Failed to parse module arguments");
122 return -1;
123 }
124
125 m->userdata = u = pa_xnew(struct userdata, 1);
126 u->sink_slot = pa_hook_connect(&c->hook_sink_disconnect, (pa_hook_cb_t) sink_hook_callback, NULL);
127 u->source_slot = pa_hook_connect(&c->hook_source_disconnect, (pa_hook_cb_t) source_hook_callback, NULL);
128
129 pa_modargs_free(ma);
130 return 0;
131 }
132
133 void pa__done(pa_core *c, pa_module*m) {
134 struct userdata *u;
135
136 assert(c);
137 assert(m);
138
139 if (!m->userdata)
140 return;
141
142 u = m->userdata;
143 if (u->sink_slot)
144 pa_hook_slot_free(u->sink_slot);
145 if (u->source_slot)
146 pa_hook_slot_free(u->source_slot);
147
148 pa_xfree(u);
149 }