]> code.delx.au - pulseaudio/blob - src/modules/module-jack-sink.c
support new channel_map argument in sink/source modules
[pulseaudio] / src / modules / module-jack-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 polypaudio; 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 <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <pthread.h>
36
37 #include <jack/jack.h>
38
39 #include <polypcore/iochannel.h>
40 #include <polypcore/sink.h>
41 #include <polypcore/module.h>
42 #include <polypcore/util.h>
43 #include <polypcore/modargs.h>
44 #include <polypcore/xmalloc.h>
45 #include <polypcore/log.h>
46 #include <polyp/mainloop-api.h>
47
48 #include "module-jack-sink-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("Jack Sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE(
54 "sink_name=<name of sink> "
55 "server_name=<jack server name> "
56 "client_name=<jack client name> "
57 "channels=<number of channels> "
58 "connect=<connect ports?> "
59 "channel_map=<channel map>")
60
61 #define DEFAULT_SINK_NAME "jack_out"
62
63 struct userdata {
64 pa_core *core;
65 pa_module *module;
66
67 pa_sink *sink;
68
69 unsigned channels;
70
71 jack_port_t* port[PA_CHANNELS_MAX];
72 jack_client_t *client;
73
74 pthread_mutex_t mutex;
75 pthread_cond_t cond;
76
77 void * buffer[PA_CHANNELS_MAX];
78 jack_nframes_t frames_requested;
79 int quit_requested;
80
81 int pipe_fds[2];
82 pa_io_event *io_event;
83
84 jack_nframes_t frames_in_buffer;
85 jack_nframes_t timestamp;
86 };
87
88 static const char* const valid_modargs[] = {
89 "sink_name",
90 "server_name",
91 "client_name",
92 "channels",
93 "connect",
94 "channel_map",
95 NULL
96 };
97
98 static void stop_sink(struct userdata *u) {
99 assert (u);
100
101 jack_client_close(u->client);
102 u->client = NULL;
103 u->core->mainloop->io_free(u->io_event);
104 u->io_event = NULL;
105 pa_sink_disconnect(u->sink);
106 pa_sink_unref(u->sink);
107 u->sink = NULL;
108 pa_module_unload_request(u->module);
109 }
110
111 static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
112 struct userdata *u = userdata;
113 char x;
114
115 assert(m);
116 assert(e);
117 assert(flags == PA_IO_EVENT_INPUT);
118 assert(u);
119 assert(u->pipe_fds[0] == fd);
120
121 read(fd, &x, 1);
122
123 if (u->quit_requested) {
124 stop_sink(u);
125 u->quit_requested = 0;
126 return;
127 }
128
129 pthread_mutex_lock(&u->mutex);
130
131 if (u->frames_requested > 0) {
132 unsigned fs;
133 jack_nframes_t frame_idx;
134 pa_memchunk chunk;
135
136 fs = pa_frame_size(&u->sink->sample_spec);
137
138 pa_sink_render_full(u->sink, u->frames_requested * fs, &chunk);
139
140 for (frame_idx = 0; frame_idx < u->frames_requested; frame_idx ++) {
141 unsigned c;
142
143 for (c = 0; c < u->channels; c++) {
144 float *s = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
145 float *d = ((float*) u->buffer[c]) + frame_idx;
146
147 *d = *s;
148 }
149 }
150
151 pa_memblock_unref(chunk.memblock);
152
153 u->frames_requested = 0;
154
155 pthread_cond_signal(&u->cond);
156 }
157
158 pthread_mutex_unlock(&u->mutex);
159 }
160
161 static void request_render(struct userdata *u) {
162 char c = 'x';
163 assert(u);
164
165 assert(u->pipe_fds[1] >= 0);
166 write(u->pipe_fds[1], &c, 1);
167 }
168
169 static void jack_shutdown(void *arg) {
170 struct userdata *u = arg;
171 assert(u);
172
173 u->quit_requested = 1;
174 request_render(u);
175 }
176
177 static int jack_process(jack_nframes_t nframes, void *arg) {
178 struct userdata *u = arg;
179 assert(u);
180
181 if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
182 unsigned c;
183
184 pthread_mutex_lock(&u->mutex);
185
186 u->frames_requested = nframes;
187
188 for (c = 0; c < u->channels; c++) {
189 u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
190 assert(u->buffer[c]);
191 }
192
193 request_render(u);
194
195 pthread_cond_wait(&u->cond, &u->mutex);
196
197 u->frames_in_buffer = nframes;
198 u->timestamp = jack_get_current_transport_frame(u->client);
199
200 pthread_mutex_unlock(&u->mutex);
201 }
202
203 return 0;
204 }
205
206 static pa_usec_t sink_get_latency_cb(pa_sink *s) {
207 struct userdata *u;
208 jack_nframes_t n, l, d;
209
210 assert(s);
211 u = s->userdata;
212
213 if (jack_transport_query(u->client, NULL) != JackTransportRolling)
214 return 0;
215
216 n = jack_get_current_transport_frame(u->client);
217
218 if (n < u->timestamp)
219 return 0;
220
221 d = n - u->timestamp;
222 l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer;
223
224 if (d >= l)
225 return 0;
226
227 return pa_bytes_to_usec((l - d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
228 }
229
230 static void jack_error_func(const char*t) {
231 pa_log_warn(__FILE__": JACK error >%s<", t);
232 }
233
234 int pa__init(pa_core *c, pa_module*m) {
235 struct userdata *u = NULL;
236 pa_sample_spec ss;
237 pa_channel_map map;
238 pa_modargs *ma = NULL;
239 jack_status_t status;
240 const char *server_name, *client_name;
241 uint32_t channels = 0;
242 int connect = 1;
243 unsigned i;
244 const char **ports = NULL, **p;
245
246 assert(c);
247 assert(m);
248
249 jack_set_error_function(jack_error_func);
250
251 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
252 pa_log(__FILE__": failed to parse module arguments.");
253 goto fail;
254 }
255
256 if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) {
257 pa_log(__FILE__": failed to parse connect= argument.");
258 goto fail;
259 }
260
261 server_name = pa_modargs_get_value(ma, "server_name", NULL);
262 client_name = pa_modargs_get_value(ma, "client_name", "polypaudio");
263
264 u = pa_xnew0(struct userdata, 1);
265 m->userdata = u;
266 u->core = c;
267 u->module = m;
268 u->pipe_fds[0] = u->pipe_fds[1] = -1;
269
270 pthread_mutex_init(&u->mutex, NULL);
271 pthread_cond_init(&u->cond, NULL);
272
273 if (pipe(u->pipe_fds) < 0) {
274 pa_log(__FILE__": pipe() failed: %s", strerror(errno));
275 goto fail;
276 }
277
278 pa_make_nonblock_fd(u->pipe_fds[1]);
279
280 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
281 pa_log(__FILE__": jack_client_open() failed.");
282 goto fail;
283 }
284
285 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
286
287 channels = 0;
288 for (p = ports; *p; p++)
289 channels++;
290
291 if (!channels)
292 channels = c->default_sample_spec.channels;
293
294 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
295 pa_log(__FILE__": failed to parse channels= argument.");
296 goto fail;
297 }
298
299 pa_channel_map_init_auto(&map, channels);
300 if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
301 pa_log(__FILE__": failed to parse channel_map= argument.");
302 goto fail;
303 }
304
305 pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client));
306
307 ss.channels = u->channels = channels;
308 ss.rate = jack_get_sample_rate(u->client);
309 ss.format = PA_SAMPLE_FLOAT32NE;
310
311 assert(pa_sample_spec_valid(&ss));
312
313 for (i = 0; i < ss.channels; i++) {
314 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) {
315 pa_log(__FILE__": jack_port_register() failed.");
316 goto fail;
317 }
318 }
319
320 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
321 pa_log(__FILE__": failed to create sink.");
322 goto fail;
323 }
324
325 u->sink->userdata = u;
326 pa_sink_set_owner(u->sink, m);
327 u->sink->description = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client));
328 u->sink->get_latency = sink_get_latency_cb;
329
330 jack_set_process_callback(u->client, jack_process, u);
331 jack_on_shutdown(u->client, jack_shutdown, u);
332
333 if (jack_activate(u->client)) {
334 pa_log(__FILE__": jack_activate() failed");
335 goto fail;
336 }
337
338 if (connect) {
339 for (i = 0, p = ports; i < ss.channels; i++, p++) {
340
341 if (!*p) {
342 pa_log(__FILE__": not enough physical output ports, leaving unconnected.");
343 break;
344 }
345
346 pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p);
347
348 if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) {
349 pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
350 break;
351 }
352 }
353
354 }
355
356 u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
357
358 free(ports);
359 pa_modargs_free(ma);
360
361 return 0;
362
363 fail:
364 if (ma)
365 pa_modargs_free(ma);
366
367 free(ports);
368
369 pa__done(c, m);
370
371 return -1;
372 }
373
374 void pa__done(pa_core *c, pa_module*m) {
375 struct userdata *u;
376 assert(c && m);
377
378 if (!(u = m->userdata))
379 return;
380
381 if (u->client)
382 jack_client_close(u->client);
383
384 if (u->io_event)
385 c->mainloop->io_free(u->io_event);
386
387 if (u->sink) {
388 pa_sink_disconnect(u->sink);
389 pa_sink_unref(u->sink);
390 }
391
392 if (u->pipe_fds[0] >= 0)
393 close(u->pipe_fds[0]);
394 if (u->pipe_fds[1] >= 0)
395 close(u->pipe_fds[1]);
396
397 pthread_mutex_destroy(&u->mutex);
398 pthread_cond_destroy(&u->cond);
399 pa_xfree(u);
400 }