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