]> code.delx.au - pulseaudio/blob - src/modules/module-remap-sink.c
alsa-mixer: Add surround 2.1 profile
[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/namereg.h>
29 #include <pulsecore/sink.h>
30 #include <pulsecore/module.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/rtpoll.h>
35
36 #include "module-remap-sink-symdef.h"
37
38 PA_MODULE_AUTHOR("Lennart Poettering");
39 PA_MODULE_DESCRIPTION("Virtual channel remapping sink");
40 PA_MODULE_VERSION(PACKAGE_VERSION);
41 PA_MODULE_LOAD_ONCE(false);
42 PA_MODULE_USAGE(
43 "sink_name=<name for the sink> "
44 "sink_properties=<properties for the sink> "
45 "master=<name of sink to remap> "
46 "master_channel_map=<channel map> "
47 "format=<sample format> "
48 "rate=<sample rate> "
49 "channels=<number of channels> "
50 "channel_map=<channel map> "
51 "resample_method=<resampler> "
52 "remix=<remix channels?>");
53
54 struct userdata {
55 pa_module *module;
56
57 pa_sink *sink;
58 pa_sink_input *sink_input;
59
60 bool auto_desc;
61 };
62
63 static const char* const valid_modargs[] = {
64 "sink_name",
65 "sink_properties",
66 "master",
67 "master_channel_map",
68 "format",
69 "rate",
70 "channels",
71 "channel_map",
72 "resample_method",
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
85 /* The sink is _put() before the sink input is, so let's
86 * make sure we don't access it yet */
87 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
88 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
89 *((pa_usec_t*) data) = 0;
90 return 0;
91 }
92
93 *((pa_usec_t*) data) =
94 /* Get the latency of the master sink */
95 pa_sink_get_latency_within_thread(u->sink_input->sink) +
96
97 /* Add the latency internal to our sink input on top */
98 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
99
100 return 0;
101 }
102
103 return pa_sink_process_msg(o, code, data, offset, chunk);
104 }
105
106 /* Called from main context */
107 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
108 struct userdata *u;
109
110 pa_sink_assert_ref(s);
111 pa_assert_se(u = s->userdata);
112
113 if (!PA_SINK_IS_LINKED(state) ||
114 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
115 return 0;
116
117 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
118 return 0;
119 }
120
121 /* Called from I/O thread context */
122 static void sink_request_rewind(pa_sink *s) {
123 struct userdata *u;
124
125 pa_sink_assert_ref(s);
126 pa_assert_se(u = s->userdata);
127
128 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
129 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
130 return;
131
132 pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes, true, false, false);
133 }
134
135 /* Called from I/O thread context */
136 static void sink_update_requested_latency(pa_sink *s) {
137 struct userdata *u;
138
139 pa_sink_assert_ref(s);
140 pa_assert_se(u = s->userdata);
141
142 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
143 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
144 return;
145
146 /* Just hand this one over to the master sink */
147 pa_sink_input_set_requested_latency_within_thread(
148 u->sink_input,
149 pa_sink_get_requested_latency_within_thread(s));
150 }
151
152 /* Called from I/O thread context */
153 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
154 struct userdata *u;
155
156 pa_sink_input_assert_ref(i);
157 pa_assert(chunk);
158 pa_assert_se(u = i->userdata);
159
160 /* Hmm, process any rewind request that might be queued up */
161 pa_sink_process_rewind(u->sink, 0);
162
163 pa_sink_render(u->sink, nbytes, chunk);
164 return 0;
165 }
166
167 /* Called from I/O thread context */
168 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
169 size_t amount = 0;
170 struct userdata *u;
171
172 pa_sink_input_assert_ref(i);
173 pa_assert_se(u = i->userdata);
174
175 if (u->sink->thread_info.rewind_nbytes > 0) {
176 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, nbytes);
177 u->sink->thread_info.rewind_nbytes = 0;
178 }
179
180 pa_sink_process_rewind(u->sink, amount);
181 }
182
183 /* Called from I/O thread context */
184 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
185 struct userdata *u;
186
187 pa_sink_input_assert_ref(i);
188 pa_assert_se(u = i->userdata);
189
190 /* FIXME: Too small max_rewind:
191 * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
192 pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
193 }
194
195 /* Called from I/O thread context */
196 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
197 struct userdata *u;
198
199 pa_sink_input_assert_ref(i);
200 pa_assert_se(u = i->userdata);
201
202 pa_sink_set_max_request_within_thread(u->sink, nbytes);
203 }
204
205 /* Called from I/O thread context */
206 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
207 struct userdata *u;
208
209 pa_sink_input_assert_ref(i);
210 pa_assert_se(u = i->userdata);
211
212 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
213 }
214
215 /* Called from I/O thread context */
216 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
217 struct userdata *u;
218
219 pa_sink_input_assert_ref(i);
220 pa_assert_se(u = i->userdata);
221
222 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
223 }
224
225 /* Called from I/O thread context */
226 static void sink_input_detach_cb(pa_sink_input *i) {
227 struct userdata *u;
228
229 pa_sink_input_assert_ref(i);
230 pa_assert_se(u = i->userdata);
231
232 pa_sink_detach_within_thread(u->sink);
233
234 pa_sink_set_rtpoll(u->sink, NULL);
235 }
236
237 /* Called from I/O thread context */
238 static void sink_input_attach_cb(pa_sink_input *i) {
239 struct userdata *u;
240
241 pa_sink_input_assert_ref(i);
242 pa_assert_se(u = i->userdata);
243
244 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
245 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
246 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
247 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
248
249 /* FIXME: Too small max_rewind:
250 * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
251 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
252
253 pa_sink_attach_within_thread(u->sink);
254 }
255
256 /* Called from main context */
257 static void sink_input_kill_cb(pa_sink_input *i) {
258 struct userdata *u;
259
260 pa_sink_input_assert_ref(i);
261 pa_assert_se(u = i->userdata);
262
263 /* The order here matters! We first kill the sink input, followed
264 * by the sink. That means the sink callbacks must be protected
265 * against an unconnected sink input! */
266 pa_sink_input_unlink(u->sink_input);
267 pa_sink_unlink(u->sink);
268
269 pa_sink_input_unref(u->sink_input);
270 u->sink_input = NULL;
271
272 pa_sink_unref(u->sink);
273 u->sink = NULL;
274
275 pa_module_unload_request(u->module, true);
276 }
277
278 /* Called from IO thread context */
279 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
280 struct userdata *u;
281
282 pa_sink_input_assert_ref(i);
283 pa_assert_se(u = i->userdata);
284
285 /* If we are added for the first time, ask for a rewinding so that
286 * we are heard right-away. */
287 if (PA_SINK_INPUT_IS_LINKED(state) &&
288 i->thread_info.state == PA_SINK_INPUT_INIT) {
289 pa_log_debug("Requesting rewind due to state change.");
290 pa_sink_input_request_rewind(i, 0, false, true, true);
291 }
292 }
293
294 /* Called from main context */
295 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
296 struct userdata *u;
297
298 pa_sink_input_assert_ref(i);
299 pa_assert_se(u = i->userdata);
300
301 if (dest) {
302 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
303 pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
304 } else
305 pa_sink_set_asyncmsgq(u->sink, NULL);
306
307 if (u->auto_desc && dest) {
308 const char *k;
309 pa_proplist *pl;
310
311 pl = pa_proplist_new();
312 k = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
313 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : dest->name);
314
315 pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
316 pa_proplist_free(pl);
317 }
318 }
319
320 int pa__init(pa_module*m) {
321 struct userdata *u;
322 pa_sample_spec ss;
323 pa_resample_method_t resample_method = PA_RESAMPLER_INVALID;
324 pa_channel_map sink_map, stream_map;
325 pa_modargs *ma;
326 pa_sink *master;
327 pa_sink_input_new_data sink_input_data;
328 pa_sink_new_data sink_data;
329 bool remix = true;
330
331 pa_assert(m);
332
333 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
334 pa_log("Failed to parse module arguments.");
335 goto fail;
336 }
337
338 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
339 pa_log("Master sink not found");
340 goto fail;
341 }
342
343 ss = master->sample_spec;
344 sink_map = master->channel_map;
345 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &sink_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
346 pa_log("Invalid sample format specification or channel map");
347 goto fail;
348 }
349
350 stream_map = sink_map;
351 if (pa_modargs_get_channel_map(ma, "master_channel_map", &stream_map) < 0) {
352 pa_log("Invalid master channel map");
353 goto fail;
354 }
355
356 if (stream_map.channels != ss.channels) {
357 pa_log("Number of channels doesn't match");
358 goto fail;
359 }
360
361 if (pa_channel_map_equal(&stream_map, &master->channel_map))
362 pa_log_warn("No remapping configured, proceeding nonetheless!");
363
364 if (pa_modargs_get_value_boolean(ma, "remix", &remix) < 0) {
365 pa_log("Invalid boolean remix parameter");
366 goto fail;
367 }
368
369 if (pa_modargs_get_resample_method(ma, &resample_method) < 0) {
370 pa_log("Invalid resampling method");
371 goto fail;
372 }
373
374 u = pa_xnew0(struct userdata, 1);
375 u->module = m;
376 m->userdata = u;
377
378 /* Create sink */
379 pa_sink_new_data_init(&sink_data);
380 sink_data.driver = __FILE__;
381 sink_data.module = m;
382 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
383 sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
384 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
385 pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
386 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
387 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
388
389 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
390 pa_log("Invalid properties");
391 pa_sink_new_data_done(&sink_data);
392 goto fail;
393 }
394
395 if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
396 const char *k;
397
398 k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
399 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name);
400 }
401
402 u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
403 pa_sink_new_data_done(&sink_data);
404
405 if (!u->sink) {
406 pa_log("Failed to create sink.");
407 goto fail;
408 }
409
410 u->sink->parent.process_msg = sink_process_msg;
411 u->sink->set_state = sink_set_state;
412 u->sink->update_requested_latency = sink_update_requested_latency;
413 u->sink->request_rewind = sink_request_rewind;
414 u->sink->userdata = u;
415
416 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
417
418 /* Create sink input */
419 pa_sink_input_new_data_init(&sink_input_data);
420 sink_input_data.driver = __FILE__;
421 sink_input_data.module = m;
422 pa_sink_input_new_data_set_sink(&sink_input_data, master, false);
423 sink_input_data.origin_sink = u->sink;
424 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
425 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
426 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
427 pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
428 sink_input_data.flags = (remix ? 0 : PA_SINK_INPUT_NO_REMIX);
429 sink_input_data.resample_method = resample_method;
430
431 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
432 pa_sink_input_new_data_done(&sink_input_data);
433
434 if (!u->sink_input)
435 goto fail;
436
437 u->sink_input->pop = sink_input_pop_cb;
438 u->sink_input->process_rewind = sink_input_process_rewind_cb;
439 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
440 u->sink_input->update_max_request = sink_input_update_max_request_cb;
441 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
442 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
443 u->sink_input->attach = sink_input_attach_cb;
444 u->sink_input->detach = sink_input_detach_cb;
445 u->sink_input->kill = sink_input_kill_cb;
446 u->sink_input->state_change = sink_input_state_change_cb;
447 u->sink_input->moving = sink_input_moving_cb;
448 u->sink_input->userdata = u;
449
450 u->sink->input_to_master = u->sink_input;
451
452 pa_sink_put(u->sink);
453 pa_sink_input_put(u->sink_input);
454
455 pa_modargs_free(ma);
456
457 return 0;
458
459 fail:
460 if (ma)
461 pa_modargs_free(ma);
462
463 pa__done(m);
464
465 return -1;
466 }
467
468 int pa__get_n_used(pa_module *m) {
469 struct userdata *u;
470
471 pa_assert(m);
472 pa_assert_se(u = m->userdata);
473
474 return pa_sink_linked_by(u->sink);
475 }
476
477 void pa__done(pa_module*m) {
478 struct userdata *u;
479
480 pa_assert(m);
481
482 if (!(u = m->userdata))
483 return;
484
485 /* See comments in sink_input_kill_cb() above regarding
486 * destruction order! */
487
488 if (u->sink_input)
489 pa_sink_input_unlink(u->sink_input);
490
491 if (u->sink)
492 pa_sink_unlink(u->sink);
493
494 if (u->sink_input)
495 pa_sink_input_unref(u->sink_input);
496
497 if (u->sink)
498 pa_sink_unref(u->sink);
499
500 pa_xfree(u);
501 }