]> code.delx.au - pulseaudio/blob - src/modules/module-remap-sink.c
init min/max latency properly; fix avail_min updating
[pulseaudio] / src / modules / module-remap-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 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 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
85 usec = 0;
86
87 *((pa_usec_t*) data) = usec/* + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec)*/;
88 return 0;
89 }
90 }
91
92 return pa_sink_process_msg(o, code, data, offset, chunk);
93 }
94
95 /* Called from main context */
96 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
97 struct userdata *u;
98
99 pa_sink_assert_ref(s);
100 pa_assert_se(u = s->userdata);
101
102 if (PA_SINK_LINKED(state) && u->sink_input && PA_SINK_INPUT_LINKED(pa_sink_input_get_state(u->sink_input)))
103 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
104
105 return 0;
106 }
107
108 /* Called from I/O thread context */
109 static void sink_request_rewind(pa_sink *s) {
110 struct userdata *u;
111
112 pa_sink_assert_ref(s);
113 pa_assert_se(u = s->userdata);
114
115 pa_sink_input_request_rewrite(u->sink_input, s->thread_info.rewind_nbytes);
116 }
117
118 /* Called from I/O thread context */
119 static void sink_update_requested_latency(pa_sink *s) {
120 struct userdata *u;
121
122 pa_sink_assert_ref(s);
123 pa_assert_se(u = s->userdata);
124
125 /* Just hand this one over to the master sink */
126 u->sink_input->thread_info.requested_sink_latency = pa_sink_get_requested_latency_within_thread(s);
127 pa_sink_invalidate_requested_latency(u->master);
128 }
129
130 /* Called from I/O thread context */
131 static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
132 struct userdata *u = PA_SINK_INPUT(o)->userdata;
133
134 switch (code) {
135 case PA_SINK_INPUT_MESSAGE_GET_LATENCY:
136 *((pa_usec_t*) data) = 0; /*pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec);*/
137
138 /* Fall through, the default handler will add in the extra
139 * latency added by the resampler */
140 break;
141 }
142
143 return pa_sink_input_process_msg(o, code, data, offset, chunk);
144 }
145
146 static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes);
147
148 /* Called from I/O thread context */
149 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
150 struct userdata *u;
151
152 pa_sink_input_assert_ref(i);
153 pa_assert(chunk);
154 pa_assert_se(u = i->userdata);
155
156 pa_sink_render(u->sink, nbytes, chunk);
157
158 return 0;
159 }
160
161 /* Called from I/O thread context */
162 static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) {
163 struct userdata *u;
164
165 pa_sink_input_assert_ref(i);
166 pa_assert_se(u = i->userdata);
167 pa_assert(nbytes > 0);
168
169 u->sink->thread_info.rewind_nbytes = nbytes;
170 pa_sink_process_rewind(u->sink);
171 }
172
173 /* Called from I/O thread context */
174 static void sink_input_set_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
175 struct userdata *u;
176
177 pa_sink_input_assert_ref(i);
178 pa_assert_se(u = i->userdata);
179
180 pa_sink_set_max_rewind(u->sink, nbytes);
181 }
182
183 /* Called from I/O thread context */
184 static void sink_input_detach_cb(pa_sink_input *i) {
185 struct userdata *u;
186
187 pa_sink_input_assert_ref(i);
188 pa_assert_se(u = i->userdata);
189
190 pa_sink_detach_within_thread(u->sink);
191 }
192
193 /* Called from I/O thread context */
194 static void sink_input_attach_cb(pa_sink_input *i) {
195 struct userdata *u;
196
197 pa_sink_input_assert_ref(i);
198 pa_assert_se(u = i->userdata);
199
200 pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
201 pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
202
203 pa_sink_attach_within_thread(u->sink);
204 }
205
206 /* Called from main context */
207 static void sink_input_kill_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 pa_sink_input_unlink(u->sink_input);
214 pa_sink_input_unref(u->sink_input);
215 u->sink_input = NULL;
216
217 pa_sink_unlink(u->sink);
218 pa_sink_unref(u->sink);
219 u->sink = NULL;
220
221 pa_module_unload_request(u->module);
222 }
223
224 int pa__init(pa_module*m) {
225 struct userdata *u;
226 pa_sample_spec ss;
227 pa_channel_map sink_map, stream_map;
228 pa_modargs *ma;
229 char *t;
230 const char *k;
231 pa_sink *master;
232 pa_sink_input_new_data sink_input_data;
233 pa_sink_new_data sink_data;
234
235 pa_assert(m);
236
237 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
238 pa_log("Failed to parse module arguments.");
239 goto fail;
240 }
241
242 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK, 1))) {
243 pa_log("Master sink not found");
244 goto fail;
245 }
246
247 ss = master->sample_spec;
248 sink_map = master->channel_map;
249 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &sink_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
250 pa_log("Invalid sample format specification or channel map");
251 goto fail;
252 }
253
254 stream_map = sink_map;
255 if (pa_modargs_get_channel_map(ma, "master_channel_map", &stream_map) < 0) {
256 pa_log("Invalid master hannel map");
257 goto fail;
258 }
259
260 if (stream_map.channels != ss.channels) {
261 pa_log("Number of channels doesn't match");
262 goto fail;
263 }
264
265 if (pa_channel_map_equal(&stream_map, &master->channel_map))
266 pa_log_warn("No remapping configured, proceeding nonetheless!");
267
268 u = pa_xnew0(struct userdata, 1);
269 u->core = m->core;
270 u->module = m;
271 m->userdata = u;
272 u->master = master;
273 u->sink = NULL;
274 u->sink_input = NULL;
275
276 /* Create sink */
277 pa_sink_new_data_init(&sink_data);
278 sink_data.driver = __FILE__;
279 sink_data.module = m;
280 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
281 sink_data.name = pa_sprintf_malloc("%s.remapped", master->name);
282 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
283 pa_sink_new_data_set_channel_map(&sink_data, &sink_map);
284 k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
285 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, t = pa_sprintf_malloc("Remapped %s", k ? k : master->name));
286 pa_xfree(t);
287 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
288
289 u->sink = pa_sink_new(m->core, &sink_data, 0);
290 pa_sink_new_data_done(&sink_data);
291
292 if (!u->sink) {
293 pa_log("Failed to create sink.");
294 goto fail;
295 }
296
297 u->sink->parent.process_msg = sink_process_msg;
298 u->sink->set_state = sink_set_state;
299 u->sink->update_requested_latency = sink_update_requested_latency;
300 u->sink->request_rewind = sink_request_rewind;
301 u->sink->userdata = u;
302 u->sink->flags = PA_SINK_LATENCY;
303
304 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
305 pa_sink_set_rtpoll(u->sink, master->rtpoll);
306
307 /* Create sink input */
308 pa_sink_input_new_data_init(&sink_input_data);
309 sink_input_data.driver = __FILE__;
310 sink_input_data.module = m;
311 sink_input_data.sink = u->master;
312 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
313 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "routing");
314 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
315 pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
316
317 u->sink_input = pa_sink_input_new(m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
318 pa_sink_input_new_data_done(&sink_input_data);
319
320 if (!u->sink_input)
321 goto fail;
322
323 u->sink_input->parent.process_msg = sink_input_process_msg;
324 u->sink_input->pop = sink_input_pop_cb;
325 u->sink_input->rewind = sink_input_rewind_cb;
326 u->sink_input->set_max_rewind = sink_input_set_max_rewind_cb;
327 u->sink_input->kill = sink_input_kill_cb;
328 u->sink_input->attach = sink_input_attach_cb;
329 u->sink_input->detach = sink_input_detach_cb;
330 u->sink_input->userdata = u;
331
332 pa_sink_put(u->sink);
333 pa_sink_input_put(u->sink_input);
334
335 pa_modargs_free(ma);
336
337 return 0;
338
339 fail:
340 if (ma)
341 pa_modargs_free(ma);
342
343 pa__done(m);
344
345 return -1;
346 }
347
348 void pa__done(pa_module*m) {
349 struct userdata *u;
350
351 pa_assert(m);
352
353 if (!(u = m->userdata))
354 return;
355
356 if (u->sink_input) {
357 pa_sink_input_unlink(u->sink_input);
358 pa_sink_input_unref(u->sink_input);
359 }
360
361 if (u->sink) {
362 pa_sink_unlink(u->sink);
363 pa_sink_unref(u->sink);
364 }
365
366 pa_xfree(u);
367 }