]> code.delx.au - pulseaudio/blob - src/modules/module-remap-sink.c
get rid of svn $ keywords
[pulseaudio] / src / modules / module-remap-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 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-error.h>
29 #include <pulsecore/namereg.h>
30 #include <pulsecore/sink.h>
31 #include <pulsecore/module.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/modargs.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/thread.h>
36 #include <pulsecore/thread-mq.h>
37 #include <pulsecore/rtpoll.h>
38
39 #include "module-remap-sink-symdef.h"
40
41 PA_MODULE_AUTHOR("Lennart Poettering");
42 PA_MODULE_DESCRIPTION("Virtual channel remapping sink");
43 PA_MODULE_VERSION(PACKAGE_VERSION);
44 PA_MODULE_LOAD_ONCE(FALSE);
45 PA_MODULE_USAGE(
46 "sink_name=<name for the sink> "
47 "master=<name of sink to remap> "
48 "master_channel_map=<channel map> "
49 "format=<sample format> "
50 "channels=<number of channels> "
51 "rate=<sample rate> "
52 "channel_map=<channel map>");
53
54 struct userdata {
55 pa_core *core;
56 pa_module *module;
57
58 pa_sink *sink, *master;
59 pa_sink_input *sink_input;
60 };
61
62 static const char* const valid_modargs[] = {
63 "sink_name",
64 "master",
65 "master_channel_map",
66 "rate",
67 "format",
68 "channels",
69 "channel_map",
70 NULL
71 };
72
73 /* Called from I/O thread context */
74 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
75 struct userdata *u = PA_SINK(o)->userdata;
76
77 switch (code) {
78
79 case PA_SINK_MESSAGE_GET_LATENCY: {
80 pa_usec_t usec = 0;
81
82 /* Get the latency of the master sink */
83 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
84 usec = 0;
85
86 /* Add the latency internal to our sink input on top */
87 usec += pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->master->sample_spec);
88
89 *((pa_usec_t*) data) = usec;
90 return 0;
91 }
92 }
93
94 return pa_sink_process_msg(o, code, data, offset, chunk);
95 }
96
97 /* Called from main context */
98 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
99 struct userdata *u;
100
101 pa_sink_assert_ref(s);
102 pa_assert_se(u = s->userdata);
103
104 if (PA_SINK_IS_LINKED(state) &&
105 u->sink_input &&
106 PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
107
108 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
109
110 return 0;
111 }
112
113 /* Called from I/O thread context */
114 static void sink_request_rewind(pa_sink *s) {
115 struct userdata *u;
116
117 pa_sink_assert_ref(s);
118 pa_assert_se(u = s->userdata);
119
120 pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes, TRUE, FALSE);
121 }
122
123 /* Called from I/O thread context */
124 static void sink_update_requested_latency(pa_sink *s) {
125 struct userdata *u;
126
127 pa_sink_assert_ref(s);
128 pa_assert_se(u = s->userdata);
129
130 /* Just hand this one over to the master sink */
131 pa_sink_input_set_requested_latency_within_thread(
132 u->sink_input,
133 pa_sink_get_requested_latency_within_thread(s));
134 }
135
136 /* Called from I/O thread context */
137 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
138 struct userdata *u;
139
140 pa_sink_input_assert_ref(i);
141 pa_assert(chunk);
142 pa_assert_se(u = i->userdata);
143
144 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
145 return -1;
146
147 pa_sink_render(u->sink, nbytes, chunk);
148 return 0;
149 }
150
151 /* Called from I/O thread context */
152 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
153 struct userdata *u;
154
155 pa_sink_input_assert_ref(i);
156 pa_assert_se(u = i->userdata);
157 pa_assert(nbytes > 0);
158
159 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
160 return;
161
162 if (u->sink->thread_info.rewind_nbytes > 0) {
163 size_t amount;
164
165 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, nbytes);
166 u->sink->thread_info.rewind_nbytes = 0;
167
168 if (amount > 0)
169 pa_sink_process_rewind(u->sink, amount);
170 }
171 }
172
173 /* Called from I/O thread context */
174 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
175 struct userdata *u;
176
177 pa_sink_input_assert_ref(i);
178 pa_assert_se(u = i->userdata);
179
180 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
181 return;
182
183 pa_sink_set_max_rewind(u->sink, nbytes);
184 }
185
186 /* Called from I/O thread context */
187 static void sink_input_detach_cb(pa_sink_input *i) {
188 struct userdata *u;
189
190 pa_sink_input_assert_ref(i);
191 pa_assert_se(u = i->userdata);
192
193 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
194 return;
195
196 pa_sink_detach_within_thread(u->sink);
197 pa_sink_set_asyncmsgq(u->sink, NULL);
198 pa_sink_set_rtpoll(u->sink, NULL);
199 }
200
201 /* Called from I/O thread context */
202 static void sink_input_attach_cb(pa_sink_input *i) {
203 struct userdata *u;
204
205 pa_sink_input_assert_ref(i);
206 pa_assert_se(u = i->userdata);
207
208 if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
209 return;
210
211 pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
212 pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
213 pa_sink_attach_within_thread(u->sink);
214
215 pa_sink_set_latency_range(u->sink, u->master->min_latency, u->master->max_latency);
216 }
217
218 /* Called from main context */
219 static void sink_input_kill_cb(pa_sink_input *i) {
220 struct userdata *u;
221
222 pa_sink_input_assert_ref(i);
223 pa_assert_se(u = i->userdata);
224
225 pa_sink_unlink(u->sink);
226 pa_sink_input_unlink(u->sink_input);
227
228 pa_sink_unref(u->sink);
229 u->sink = NULL;
230 pa_sink_input_unref(u->sink_input);
231 u->sink_input = NULL;
232
233 pa_module_unload_request(u->module);
234 }
235
236 /* Called from IO thread context */
237 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
238 struct userdata *u;
239
240 pa_sink_input_assert_ref(i);
241 pa_assert_se(u = i->userdata);
242
243 /* If we are added for the first time, ask for a rewinding so that
244 * we are heard right-away. */
245 if (PA_SINK_INPUT_IS_LINKED(state) &&
246 i->thread_info.state == PA_SINK_INPUT_INIT) {
247 pa_log_debug("Requesting rewind due to state change.");
248 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
249 }
250 }
251
252 int pa__init(pa_module*m) {
253 struct userdata *u;
254 pa_sample_spec ss;
255 pa_channel_map sink_map, stream_map;
256 pa_modargs *ma;
257 const char *k;
258 pa_sink *master;
259 pa_sink_input_new_data sink_input_data;
260 pa_sink_new_data sink_data;
261
262 pa_assert(m);
263
264 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
265 pa_log("Failed to parse module arguments.");
266 goto fail;
267 }
268
269 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK, 1))) {
270 pa_log("Master sink not found");
271 goto fail;
272 }
273
274 ss = master->sample_spec;
275 sink_map = master->channel_map;
276 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &sink_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
277 pa_log("Invalid sample format specification or channel map");
278 goto fail;
279 }
280
281 stream_map = sink_map;
282 if (pa_modargs_get_channel_map(ma, "master_channel_map", &stream_map) < 0) {
283 pa_log("Invalid master hannel map");
284 goto fail;
285 }
286
287 if (stream_map.channels != ss.channels) {
288 pa_log("Number of channels doesn't match");
289 goto fail;
290 }
291
292 if (pa_channel_map_equal(&stream_map, &master->channel_map))
293 pa_log_warn("No remapping configured, proceeding nonetheless!");
294
295 u = pa_xnew0(struct userdata, 1);
296 u->core = m->core;
297 u->module = m;
298 m->userdata = u;
299 u->master = master;
300 u->sink = NULL;
301 u->sink_input = NULL;
302
303 /* Create sink */
304 pa_sink_new_data_init(&sink_data);
305 sink_data.driver = __FILE__;
306 sink_data.module = m;
307 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
308 sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
309 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
310 pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
311 k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
312 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name);
313 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
314 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
315
316 u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY);
317 pa_sink_new_data_done(&sink_data);
318
319 if (!u->sink) {
320 pa_log("Failed to create sink.");
321 goto fail;
322 }
323
324 u->sink->parent.process_msg = sink_process_msg;
325 u->sink->set_state = sink_set_state;
326 u->sink->update_requested_latency = sink_update_requested_latency;
327 u->sink->request_rewind = sink_request_rewind;
328 u->sink->userdata = u;
329
330 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
331 pa_sink_set_rtpoll(u->sink, master->rtpoll);
332
333 /* Create sink input */
334 pa_sink_input_new_data_init(&sink_input_data);
335 sink_input_data.driver = __FILE__;
336 sink_input_data.module = m;
337 sink_input_data.sink = u->master;
338 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
339 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
340 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
341 pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
342
343 u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
344 pa_sink_input_new_data_done(&sink_input_data);
345
346 if (!u->sink_input)
347 goto fail;
348
349 u->sink_input->pop = sink_input_pop_cb;
350 u->sink_input->process_rewind = sink_input_process_rewind_cb;
351 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
352 u->sink_input->kill = sink_input_kill_cb;
353 u->sink_input->attach = sink_input_attach_cb;
354 u->sink_input->detach = sink_input_detach_cb;
355 u->sink_input->state_change = sink_input_state_change_cb;
356 u->sink_input->userdata = u;
357
358 pa_sink_put(u->sink);
359 pa_sink_input_put(u->sink_input);
360
361 pa_modargs_free(ma);
362
363 return 0;
364
365 fail:
366 if (ma)
367 pa_modargs_free(ma);
368
369 pa__done(m);
370
371 return -1;
372 }
373
374 void pa__done(pa_module*m) {
375 struct userdata *u;
376
377 pa_assert(m);
378
379 if (!(u = m->userdata))
380 return;
381
382 if (u->sink) {
383 pa_sink_unlink(u->sink);
384 pa_sink_unref(u->sink);
385 }
386
387 if (u->sink_input) {
388 pa_sink_input_unlink(u->sink_input);
389 pa_sink_input_unref(u->sink_input);
390 }
391
392 pa_xfree(u);
393 }