]> code.delx.au - pulseaudio/blob - src/modules/module-remap-sink.c
fix remapping sink for glitch-free
[pulseaudio] / src / modules / module-remap-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2008 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/core-error.h>
31 #include <pulsecore/namereg.h>
32 #include <pulsecore/sink.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/modargs.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/thread.h>
38 #include <pulsecore/thread-mq.h>
39 #include <pulsecore/rtpoll.h>
40
41 #include "module-remap-sink-symdef.h"
42
43 PA_MODULE_AUTHOR("Lennart Poettering");
44 PA_MODULE_DESCRIPTION("Virtual channel remapping sink");
45 PA_MODULE_VERSION(PACKAGE_VERSION);
46 PA_MODULE_LOAD_ONCE(FALSE);
47 PA_MODULE_USAGE(
48 "sink_name=<name for the sink> "
49 "master=<name of sink to remap> "
50 "master_channel_map=<channel map> "
51 "format=<sample format> "
52 "channels=<number of channels> "
53 "rate=<sample rate> "
54 "channel_map=<channel map>");
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 "master",
67 "master_channel_map",
68 "rate",
69 "format",
70 "channels",
71 "channel_map",
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(
123 u->sink_input,
124 s->thread_info.rewind_nbytes,
125 FALSE, FALSE);
126 }
127
128 /* Called from I/O thread context */
129 static void sink_update_requested_latency(pa_sink *s) {
130 struct userdata *u;
131
132 pa_sink_assert_ref(s);
133 pa_assert_se(u = s->userdata);
134
135 /* Just hand this one over to the master sink */
136 pa_sink_input_set_requested_latency_within_thread(
137 u->sink_input,
138 pa_sink_get_requested_latency_within_thread(s));
139 }
140
141 /* Called from I/O thread context */
142 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
143 struct userdata *u;
144
145 pa_sink_input_assert_ref(i);
146 pa_assert(chunk);
147 pa_assert_se(u = i->userdata);
148
149 if (!u->sink)
150 return -1;
151
152 pa_sink_render(u->sink, nbytes, chunk);
153 return 0;
154 }
155
156 /* Called from I/O thread context */
157 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
158 struct userdata *u;
159
160 pa_sink_input_assert_ref(i);
161 pa_assert_se(u = i->userdata);
162 pa_assert(nbytes > 0);
163
164 if (!u->sink)
165 return;
166
167 if (u->sink->thread_info.rewind_nbytes > 0) {
168 size_t amount;
169
170 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, nbytes);
171 u->sink->thread_info.rewind_nbytes = 0;
172
173 if (amount > 0)
174 pa_sink_process_rewind(u->sink, amount);
175 }
176 }
177
178 /* Called from I/O thread context */
179 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
180 struct userdata *u;
181
182 pa_sink_input_assert_ref(i);
183 pa_assert_se(u = i->userdata);
184
185 if (!u->sink)
186 return;
187
188 pa_sink_set_max_rewind(u->sink, nbytes);
189 }
190
191 /* Called from I/O thread context */
192 static void sink_input_detach_cb(pa_sink_input *i) {
193 struct userdata *u;
194
195 pa_sink_input_assert_ref(i);
196 pa_assert_se(u = i->userdata);
197
198 if (!u->sink)
199 return;
200
201 pa_sink_detach_within_thread(u->sink);
202 pa_sink_set_asyncmsgq(u->sink, NULL);
203 pa_sink_set_rtpoll(u->sink, NULL);
204 }
205
206 /* Called from I/O thread context */
207 static void sink_input_attach_cb(pa_sink_input *i) {
208 struct userdata *u;
209
210 pa_sink_input_assert_ref(i);
211 pa_assert_se(u = i->userdata);
212
213 if (!u->sink)
214 return;
215
216 pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
217 pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
218 pa_sink_attach_within_thread(u->sink);
219
220 u->sink->max_latency = u->master->max_latency;
221 u->sink->min_latency = u->master->min_latency;
222 }
223
224 /* Called from main context */
225 static void sink_input_kill_cb(pa_sink_input *i) {
226 struct userdata *u;
227
228 pa_sink_input_assert_ref(i);
229 pa_assert_se(u = i->userdata);
230
231 pa_sink_unlink(u->sink);
232 pa_sink_unref(u->sink);
233 u->sink = NULL;
234
235 pa_sink_input_unlink(u->sink_input);
236 pa_sink_input_unref(u->sink_input);
237 u->sink_input = NULL;
238
239 pa_module_unload_request(u->module);
240 }
241
242 /* Called from IO thread context */
243 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
244 struct userdata *u;
245
246 pa_sink_input_assert_ref(i);
247 pa_assert_se(u = i->userdata);
248
249 /* If we are added for the first time, ask for a rewinding so that
250 * we are heard right-away. */
251 if (PA_SINK_INPUT_IS_LINKED(state) &&
252 i->thread_info.state == PA_SINK_INPUT_INIT) {
253 pa_log_debug("Requesting rewind due to state change.");
254 pa_sink_input_request_rewind(i, 0, TRUE, TRUE);
255 }
256 }
257
258 int pa__init(pa_module*m) {
259 struct userdata *u;
260 pa_sample_spec ss;
261 pa_channel_map sink_map, stream_map;
262 pa_modargs *ma;
263 const char *k;
264 pa_sink *master;
265 pa_sink_input_new_data sink_input_data;
266 pa_sink_new_data sink_data;
267
268 pa_assert(m);
269
270 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
271 pa_log("Failed to parse module arguments.");
272 goto fail;
273 }
274
275 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK, 1))) {
276 pa_log("Master sink not found");
277 goto fail;
278 }
279
280 ss = master->sample_spec;
281 sink_map = master->channel_map;
282 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &sink_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
283 pa_log("Invalid sample format specification or channel map");
284 goto fail;
285 }
286
287 stream_map = sink_map;
288 if (pa_modargs_get_channel_map(ma, "master_channel_map", &stream_map) < 0) {
289 pa_log("Invalid master hannel map");
290 goto fail;
291 }
292
293 if (stream_map.channels != ss.channels) {
294 pa_log("Number of channels doesn't match");
295 goto fail;
296 }
297
298 if (pa_channel_map_equal(&stream_map, &master->channel_map))
299 pa_log_warn("No remapping configured, proceeding nonetheless!");
300
301 u = pa_xnew0(struct userdata, 1);
302 u->core = m->core;
303 u->module = m;
304 m->userdata = u;
305 u->master = master;
306 u->sink = NULL;
307 u->sink_input = NULL;
308
309 /* Create sink */
310 pa_sink_new_data_init(&sink_data);
311 sink_data.driver = __FILE__;
312 sink_data.module = m;
313 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
314 sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
315 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
316 pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
317 k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
318 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name);
319 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
320 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
321
322 u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY);
323 pa_sink_new_data_done(&sink_data);
324
325 if (!u->sink) {
326 pa_log("Failed to create sink.");
327 goto fail;
328 }
329
330 u->sink->parent.process_msg = sink_process_msg;
331 u->sink->set_state = sink_set_state;
332 u->sink->update_requested_latency = sink_update_requested_latency;
333 u->sink->request_rewind = sink_request_rewind;
334 u->sink->userdata = u;
335
336 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
337 pa_sink_set_rtpoll(u->sink, master->rtpoll);
338
339 /* Create sink input */
340 pa_sink_input_new_data_init(&sink_input_data);
341 sink_input_data.driver = __FILE__;
342 sink_input_data.module = m;
343 sink_input_data.sink = u->master;
344 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
345 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
346 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
347 pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
348
349 u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
350 pa_sink_input_new_data_done(&sink_input_data);
351
352 if (!u->sink_input)
353 goto fail;
354
355 u->sink_input->pop = sink_input_pop_cb;
356 u->sink_input->process_rewind = sink_input_process_rewind_cb;
357 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
358 u->sink_input->kill = sink_input_kill_cb;
359 u->sink_input->attach = sink_input_attach_cb;
360 u->sink_input->detach = sink_input_detach_cb;
361 u->sink_input->state_change = sink_input_state_change_cb;
362 u->sink_input->userdata = u;
363
364 pa_sink_put(u->sink);
365 pa_sink_input_put(u->sink_input);
366
367 pa_modargs_free(ma);
368
369 return 0;
370
371 fail:
372 if (ma)
373 pa_modargs_free(ma);
374
375 pa__done(m);
376
377 return -1;
378 }
379
380 void pa__done(pa_module*m) {
381 struct userdata *u;
382
383 pa_assert(m);
384
385 if (!(u = m->userdata))
386 return;
387
388 if (u->sink) {
389 pa_sink_unlink(u->sink);
390 pa_sink_unref(u->sink);
391 }
392
393 if (u->sink_input) {
394 pa_sink_input_unlink(u->sink_input);
395 pa_sink_input_unref(u->sink_input);
396 }
397
398 pa_xfree(u);
399 }