hashmap: Add pa_hashmap_remove_all()
[platform/upstream/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/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         "remix=<remix channels?>");
52
53 struct userdata {
54     pa_module *module;
55
56     pa_sink *sink;
57     pa_sink_input *sink_input;
58
59     pa_bool_t auto_desc;
60 };
61
62 static const char* const valid_modargs[] = {
63     "sink_name",
64     "sink_properties",
65     "master",
66     "master_channel_map",
67     "format",
68     "rate",
69     "channels",
70     "channel_map",
71     "remix",
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
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;
88                 return 0;
89             }
90
91             *((pa_usec_t*) data) =
92                 /* Get the latency of the master sink */
93                 pa_sink_get_latency_within_thread(u->sink_input->sink) +
94
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);
97
98             return 0;
99     }
100
101     return pa_sink_process_msg(o, code, data, offset, chunk);
102 }
103
104 /* Called from main context */
105 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
106     struct userdata *u;
107
108     pa_sink_assert_ref(s);
109     pa_assert_se(u = s->userdata);
110
111     if (!PA_SINK_IS_LINKED(state) ||
112         !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
113         return 0;
114
115     pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
116     return 0;
117 }
118
119 /* Called from I/O thread context */
120 static void sink_request_rewind(pa_sink *s) {
121     struct userdata *u;
122
123     pa_sink_assert_ref(s);
124     pa_assert_se(u = s->userdata);
125
126     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
127         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
128         return;
129
130     pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes, TRUE, FALSE, FALSE);
131 }
132
133 /* Called from I/O thread context */
134 static void sink_update_requested_latency(pa_sink *s) {
135     struct userdata *u;
136
137     pa_sink_assert_ref(s);
138     pa_assert_se(u = s->userdata);
139
140     if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
141         !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
142         return;
143
144     /* Just hand this one over to the master sink */
145     pa_sink_input_set_requested_latency_within_thread(
146             u->sink_input,
147             pa_sink_get_requested_latency_within_thread(s));
148 }
149
150 /* Called from I/O thread context */
151 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
152     struct userdata *u;
153
154     pa_sink_input_assert_ref(i);
155     pa_assert(chunk);
156     pa_assert_se(u = i->userdata);
157
158     /* Hmm, process any rewind request that might be queued up */
159     pa_sink_process_rewind(u->sink, 0);
160
161     pa_sink_render(u->sink, nbytes, chunk);
162     return 0;
163 }
164
165 /* Called from I/O thread context */
166 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
167     size_t amount = 0;
168     struct userdata *u;
169
170     pa_sink_input_assert_ref(i);
171     pa_assert_se(u = i->userdata);
172
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;
176     }
177
178     pa_sink_process_rewind(u->sink, amount);
179 }
180
181 /* Called from I/O thread context */
182 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
183     struct userdata *u;
184
185     pa_sink_input_assert_ref(i);
186     pa_assert_se(u = i->userdata);
187
188     /* FIXME: Too small max_rewind:
189      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
190     pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
191 }
192
193 /* Called from I/O thread context */
194 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
195     struct userdata *u;
196
197     pa_sink_input_assert_ref(i);
198     pa_assert_se(u = i->userdata);
199
200     pa_sink_set_max_request_within_thread(u->sink, nbytes);
201 }
202
203 /* Called from I/O thread context */
204 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
205     struct userdata *u;
206
207     pa_sink_input_assert_ref(i);
208     pa_assert_se(u = i->userdata);
209
210     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
211 }
212
213 /* Called from I/O thread context */
214 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
215     struct userdata *u;
216
217     pa_sink_input_assert_ref(i);
218     pa_assert_se(u = i->userdata);
219
220     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
221 }
222
223 /* Called from I/O thread context */
224 static void sink_input_detach_cb(pa_sink_input *i) {
225     struct userdata *u;
226
227     pa_sink_input_assert_ref(i);
228     pa_assert_se(u = i->userdata);
229
230     pa_sink_detach_within_thread(u->sink);
231
232     pa_sink_set_rtpoll(u->sink, NULL);
233 }
234
235 /* Called from I/O thread context */
236 static void sink_input_attach_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_rtpoll(u->sink, i->sink->thread_info.rtpoll);
243     pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
244     pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
245     pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
246
247     /* FIXME: Too small max_rewind:
248      * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
249     pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
250
251     pa_sink_attach_within_thread(u->sink);
252 }
253
254 /* Called from main context */
255 static void sink_input_kill_cb(pa_sink_input *i) {
256     struct userdata *u;
257
258     pa_sink_input_assert_ref(i);
259     pa_assert_se(u = i->userdata);
260
261     /* The order here matters! We first kill the sink input, followed
262      * by the sink. That means the sink callbacks must be protected
263      * against an unconnected sink input! */
264     pa_sink_input_unlink(u->sink_input);
265     pa_sink_unlink(u->sink);
266
267     pa_sink_input_unref(u->sink_input);
268     u->sink_input = NULL;
269
270     pa_sink_unref(u->sink);
271     u->sink = NULL;
272
273     pa_module_unload_request(u->module, TRUE);
274 }
275
276 /* Called from IO thread context */
277 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
278     struct userdata *u;
279
280     pa_sink_input_assert_ref(i);
281     pa_assert_se(u = i->userdata);
282
283     /* If we are added for the first time, ask for a rewinding so that
284      * we are heard right-away. */
285     if (PA_SINK_INPUT_IS_LINKED(state) &&
286         i->thread_info.state == PA_SINK_INPUT_INIT) {
287         pa_log_debug("Requesting rewind due to state change.");
288         pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
289     }
290 }
291
292 /* Called from main context */
293 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
294     struct userdata *u;
295
296     pa_sink_input_assert_ref(i);
297     pa_assert_se(u = i->userdata);
298
299     return u->sink != dest;
300 }
301
302 /* Called from main context */
303 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
304     struct userdata *u;
305
306     pa_sink_input_assert_ref(i);
307     pa_assert_se(u = i->userdata);
308
309     if (dest) {
310         pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
311         pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
312     } else
313         pa_sink_set_asyncmsgq(u->sink, NULL);
314
315     if (u->auto_desc && dest) {
316         const char *k;
317         pa_proplist *pl;
318
319         pl = pa_proplist_new();
320         k = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
321         pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : dest->name);
322
323         pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
324         pa_proplist_free(pl);
325     }
326 }
327
328 int pa__init(pa_module*m) {
329     struct userdata *u;
330     pa_sample_spec ss;
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     pa_bool_t 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     u = pa_xnew0(struct userdata, 1);
377     u->module = m;
378     m->userdata = u;
379
380     /* Create sink */
381     pa_sink_new_data_init(&sink_data);
382     sink_data.driver = __FILE__;
383     sink_data.module = m;
384     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
385         sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
386     pa_sink_new_data_set_sample_spec(&sink_data, &ss);
387     pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
388     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
389     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
390
391     if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
392         pa_log("Invalid properties");
393         pa_sink_new_data_done(&sink_data);
394         goto fail;
395     }
396
397     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
398         const char *k;
399
400         k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
401         pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name);
402     }
403
404     u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
405     pa_sink_new_data_done(&sink_data);
406
407     if (!u->sink) {
408         pa_log("Failed to create sink.");
409         goto fail;
410     }
411
412     u->sink->parent.process_msg = sink_process_msg;
413     u->sink->set_state = sink_set_state;
414     u->sink->update_requested_latency = sink_update_requested_latency;
415     u->sink->request_rewind = sink_request_rewind;
416     u->sink->userdata = u;
417
418     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
419
420     /* Create sink input */
421     pa_sink_input_new_data_init(&sink_input_data);
422     sink_input_data.driver = __FILE__;
423     sink_input_data.module = m;
424     pa_sink_input_new_data_set_sink(&sink_input_data, master, FALSE);
425     sink_input_data.origin_sink = u->sink;
426     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
427     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
428     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
429     pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
430     sink_input_data.flags = (remix ? 0 : PA_SINK_INPUT_NO_REMIX);
431
432     pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
433     pa_sink_input_new_data_done(&sink_input_data);
434
435     if (!u->sink_input)
436         goto fail;
437
438     u->sink_input->pop = sink_input_pop_cb;
439     u->sink_input->process_rewind = sink_input_process_rewind_cb;
440     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
441     u->sink_input->update_max_request = sink_input_update_max_request_cb;
442     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
443     u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
444     u->sink_input->attach = sink_input_attach_cb;
445     u->sink_input->detach = sink_input_detach_cb;
446     u->sink_input->kill = sink_input_kill_cb;
447     u->sink_input->state_change = sink_input_state_change_cb;
448     u->sink_input->may_move_to = sink_input_may_move_to_cb;
449     u->sink_input->moving = sink_input_moving_cb;
450     u->sink_input->userdata = u;
451
452     u->sink->input_to_master = u->sink_input;
453
454     pa_sink_put(u->sink);
455     pa_sink_input_put(u->sink_input);
456
457     pa_modargs_free(ma);
458
459     return 0;
460
461 fail:
462     if (ma)
463         pa_modargs_free(ma);
464
465     pa__done(m);
466
467     return -1;
468 }
469
470 int pa__get_n_used(pa_module *m) {
471     struct userdata *u;
472
473     pa_assert(m);
474     pa_assert_se(u = m->userdata);
475
476     return pa_sink_linked_by(u->sink);
477 }
478
479 void pa__done(pa_module*m) {
480     struct userdata *u;
481
482     pa_assert(m);
483
484     if (!(u = m->userdata))
485         return;
486
487     /* See comments in sink_input_kill_cb() above regarding
488      * destruction order! */
489
490     if (u->sink_input)
491         pa_sink_input_unlink(u->sink_input);
492
493     if (u->sink)
494         pa_sink_unlink(u->sink);
495
496     if (u->sink_input)
497         pa_sink_input_unref(u->sink_input);
498
499     if (u->sink)
500         pa_sink_unref(u->sink);
501
502     pa_xfree(u);
503 }