2 This file is part of PulseAudio.
4 Copyright 2004-2009 Lennart Poettering
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.
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.
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
26 #include <pulse/xmalloc.h>
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>
36 #include "module-remap-sink-symdef.h"
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);
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> "
49 "channels=<number of channels> "
50 "channel_map=<channel map> "
51 "remix=<remix channels?>");
57 pa_sink_input *sink_input;
62 static const char* const valid_modargs[] = {
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;
81 case PA_SINK_MESSAGE_GET_LATENCY:
83 /* The sink is _put() before the sink input is, so let's
84 * make sure we don't access it yet */
85 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
86 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
87 *((pa_usec_t*) data) = 0;
91 *((pa_usec_t*) data) =
92 /* Get the latency of the master sink */
93 pa_sink_get_latency_within_thread(u->sink_input->sink) +
95 /* Add the latency internal to our sink input on top */
96 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
101 return pa_sink_process_msg(o, code, data, offset, chunk);
104 /* Called from main context */
105 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
108 pa_sink_assert_ref(s);
109 pa_assert_se(u = s->userdata);
111 if (!PA_SINK_IS_LINKED(state) ||
112 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
115 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
119 /* Called from I/O thread context */
120 static void sink_request_rewind(pa_sink *s) {
123 pa_sink_assert_ref(s);
124 pa_assert_se(u = s->userdata);
126 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
127 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
130 pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes, TRUE, FALSE, FALSE);
133 /* Called from I/O thread context */
134 static void sink_update_requested_latency(pa_sink *s) {
137 pa_sink_assert_ref(s);
138 pa_assert_se(u = s->userdata);
140 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
141 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
144 /* Just hand this one over to the master sink */
145 pa_sink_input_set_requested_latency_within_thread(
147 pa_sink_get_requested_latency_within_thread(s));
150 /* Called from I/O thread context */
151 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
154 pa_sink_input_assert_ref(i);
156 pa_assert_se(u = i->userdata);
158 /* Hmm, process any rewind request that might be queued up */
159 pa_sink_process_rewind(u->sink, 0);
161 pa_sink_render(u->sink, nbytes, chunk);
165 /* Called from I/O thread context */
166 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
170 pa_sink_input_assert_ref(i);
171 pa_assert_se(u = i->userdata);
173 if (u->sink->thread_info.rewind_nbytes > 0) {
174 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, nbytes);
175 u->sink->thread_info.rewind_nbytes = 0;
178 pa_sink_process_rewind(u->sink, amount);
181 /* Called from I/O thread context */
182 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
185 pa_sink_input_assert_ref(i);
186 pa_assert_se(u = i->userdata);
188 pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
191 /* Called from I/O thread context */
192 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
195 pa_sink_input_assert_ref(i);
196 pa_assert_se(u = i->userdata);
198 pa_sink_set_max_request_within_thread(u->sink, nbytes);
201 /* Called from I/O thread context */
202 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
205 pa_sink_input_assert_ref(i);
206 pa_assert_se(u = i->userdata);
208 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
211 /* Called from I/O thread context */
212 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
215 pa_sink_input_assert_ref(i);
216 pa_assert_se(u = i->userdata);
218 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
221 /* Called from I/O thread context */
222 static void sink_input_detach_cb(pa_sink_input *i) {
225 pa_sink_input_assert_ref(i);
226 pa_assert_se(u = i->userdata);
228 pa_sink_detach_within_thread(u->sink);
230 pa_sink_set_rtpoll(u->sink, NULL);
233 /* Called from I/O thread context */
234 static void sink_input_attach_cb(pa_sink_input *i) {
237 pa_sink_input_assert_ref(i);
238 pa_assert_se(u = i->userdata);
240 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
241 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
242 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
243 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
244 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
246 pa_sink_attach_within_thread(u->sink);
249 /* Called from main context */
250 static void sink_input_kill_cb(pa_sink_input *i) {
253 pa_sink_input_assert_ref(i);
254 pa_assert_se(u = i->userdata);
256 /* The order here matters! We first kill the sink input, followed
257 * by the sink. That means the sink callbacks must be protected
258 * against an unconnected sink input! */
259 pa_sink_input_unlink(u->sink_input);
260 pa_sink_unlink(u->sink);
262 pa_sink_input_unref(u->sink_input);
263 u->sink_input = NULL;
265 pa_sink_unref(u->sink);
268 pa_module_unload_request(u->module, TRUE);
271 /* Called from IO thread context */
272 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
275 pa_sink_input_assert_ref(i);
276 pa_assert_se(u = i->userdata);
278 /* If we are added for the first time, ask for a rewinding so that
279 * we are heard right-away. */
280 if (PA_SINK_INPUT_IS_LINKED(state) &&
281 i->thread_info.state == PA_SINK_INPUT_INIT) {
282 pa_log_debug("Requesting rewind due to state change.");
283 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
287 /* Called from main context */
288 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
291 pa_sink_input_assert_ref(i);
292 pa_assert_se(u = i->userdata);
294 return u->sink != dest;
297 /* Called from main context */
298 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
301 pa_sink_input_assert_ref(i);
302 pa_assert_se(u = i->userdata);
305 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
306 pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
308 pa_sink_set_asyncmsgq(u->sink, NULL);
310 if (u->auto_desc && dest) {
314 pl = pa_proplist_new();
315 k = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
316 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : dest->name);
318 pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
319 pa_proplist_free(pl);
323 int pa__init(pa_module*m) {
326 pa_channel_map sink_map, stream_map;
329 pa_sink_input_new_data sink_input_data;
330 pa_sink_new_data sink_data;
331 pa_bool_t remix = TRUE;
335 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
336 pa_log("Failed to parse module arguments.");
340 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
341 pa_log("Master sink not found");
345 ss = master->sample_spec;
346 sink_map = master->channel_map;
347 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &sink_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
348 pa_log("Invalid sample format specification or channel map");
352 stream_map = sink_map;
353 if (pa_modargs_get_channel_map(ma, "master_channel_map", &stream_map) < 0) {
354 pa_log("Invalid master channel map");
358 if (stream_map.channels != ss.channels) {
359 pa_log("Number of channels doesn't match");
363 if (pa_channel_map_equal(&stream_map, &master->channel_map))
364 pa_log_warn("No remapping configured, proceeding nonetheless!");
366 if (pa_modargs_get_value_boolean(ma, "remix", &remix) < 0) {
367 pa_log("Invalid boolean remix parameter");
371 u = pa_xnew0(struct userdata, 1);
376 pa_sink_new_data_init(&sink_data);
377 sink_data.driver = __FILE__;
378 sink_data.module = m;
379 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
380 sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
381 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
382 pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
383 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
384 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
386 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
387 pa_log("Invalid properties");
388 pa_sink_new_data_done(&sink_data);
392 if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
395 k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
396 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name);
399 u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
400 pa_sink_new_data_done(&sink_data);
403 pa_log("Failed to create sink.");
407 u->sink->parent.process_msg = sink_process_msg;
408 u->sink->set_state = sink_set_state;
409 u->sink->update_requested_latency = sink_update_requested_latency;
410 u->sink->request_rewind = sink_request_rewind;
411 u->sink->userdata = u;
413 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
415 /* Create sink input */
416 pa_sink_input_new_data_init(&sink_input_data);
417 sink_input_data.driver = __FILE__;
418 sink_input_data.module = m;
419 pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
420 sink_input_data.origin_sink = u->sink;
421 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
422 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
423 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
424 pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
425 sink_input_data.flags = (remix ? 0 : PA_SINK_INPUT_NO_REMIX);
427 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
428 pa_sink_input_new_data_done(&sink_input_data);
433 u->sink_input->pop = sink_input_pop_cb;
434 u->sink_input->process_rewind = sink_input_process_rewind_cb;
435 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
436 u->sink_input->update_max_request = sink_input_update_max_request_cb;
437 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
438 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
439 u->sink_input->attach = sink_input_attach_cb;
440 u->sink_input->detach = sink_input_detach_cb;
441 u->sink_input->kill = sink_input_kill_cb;
442 u->sink_input->state_change = sink_input_state_change_cb;
443 u->sink_input->may_move_to = sink_input_may_move_to_cb;
444 u->sink_input->moving = sink_input_moving_cb;
445 u->sink_input->userdata = u;
447 u->sink->input_to_master = u->sink_input;
449 pa_sink_put(u->sink);
450 pa_sink_input_put(u->sink_input);
465 int pa__get_n_used(pa_module *m) {
469 pa_assert_se(u = m->userdata);
471 return pa_sink_linked_by(u->sink);
474 void pa__done(pa_module*m) {
479 if (!(u = m->userdata))
482 /* See comments in sink_input_kill_cb() above regarding
483 * destruction order! */
486 pa_sink_input_unlink(u->sink_input);
489 pa_sink_unlink(u->sink);
492 pa_sink_input_unref(u->sink_input);
495 pa_sink_unref(u->sink);