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