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