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