]> code.delx.au - pulseaudio/blob - src/modules/module-jack-source.c
move modules to ${libdir}/polypaudio-${PA_MAJORMINOR}/modules/
[pulseaudio] / src / modules / module-jack-source.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 <polyp/error.h>
40 #include <polyp/xmalloc.h>
41
42 #include <polypcore/iochannel.h>
43 #include <polypcore/source.h>
44 #include <polypcore/module.h>
45 #include <polypcore/core-util.h>
46 #include <polypcore/modargs.h>
47 #include <polypcore/log.h>
48 #include <polyp/mainloop-api.h>
49
50 #include "module-jack-source-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering")
53 PA_MODULE_DESCRIPTION("Jack Source")
54 PA_MODULE_VERSION(PACKAGE_VERSION)
55 PA_MODULE_USAGE(
56 "source_name=<name of source> "
57 "server_name=<jack server name> "
58 "client_name=<jack client name> "
59 "channels=<number of channels> "
60 "connect=<connect ports?>"
61 "channel_map=<channel map>")
62
63 #define DEFAULT_SOURCE_NAME "jack_in"
64
65 struct userdata {
66 pa_core *core;
67 pa_module *module;
68
69 pa_source *source;
70
71 unsigned channels;
72
73 jack_port_t* port[PA_CHANNELS_MAX];
74 jack_client_t *client;
75
76 pthread_mutex_t mutex;
77 pthread_cond_t cond;
78
79 void * buffer[PA_CHANNELS_MAX];
80 jack_nframes_t frames_posted;
81 int quit_requested;
82
83 int pipe_fds[2];
84 pa_io_event *io_event;
85
86 jack_nframes_t frames_in_buffer;
87 jack_nframes_t timestamp;
88 };
89
90 static const char* const valid_modargs[] = {
91 "source_name",
92 "server_name",
93 "client_name",
94 "channels",
95 "connect",
96 "channel_map",
97 NULL
98 };
99
100 static void stop_source(struct userdata *u) {
101 assert (u);
102
103 jack_client_close(u->client);
104 u->client = NULL;
105 u->core->mainloop->io_free(u->io_event);
106 u->io_event = NULL;
107 pa_source_disconnect(u->source);
108 pa_source_unref(u->source);
109 u->source = NULL;
110 pa_module_unload_request(u->module);
111 }
112
113 static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
114 struct userdata *u = userdata;
115 char x;
116
117 assert(m);
118 assert(flags == PA_IO_EVENT_INPUT);
119 assert(u);
120 assert(u->pipe_fds[0] == fd);
121
122 read(fd, &x, 1);
123
124 if (u->quit_requested) {
125 stop_source(u);
126 u->quit_requested = 0;
127 return;
128 }
129
130 pthread_mutex_lock(&u->mutex);
131
132 if (u->frames_posted > 0) {
133 unsigned fs;
134 jack_nframes_t frame_idx;
135 pa_memchunk chunk;
136
137 fs = pa_frame_size(&u->source->sample_spec);
138
139 chunk.memblock = pa_memblock_new(chunk.length = u->frames_posted * fs, u->core->memblock_stat);
140 chunk.index = 0;
141
142 for (frame_idx = 0; frame_idx < u->frames_posted; frame_idx ++) {
143 unsigned c;
144
145 for (c = 0; c < u->channels; c++) {
146 float *s = ((float*) u->buffer[c]) + frame_idx;
147 float *d = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
148
149 *d = *s;
150 }
151 }
152
153 pa_source_post(u->source, &chunk);
154 pa_memblock_unref(chunk.memblock);
155
156 u->frames_posted = 0;
157
158 pthread_cond_signal(&u->cond);
159 }
160
161 pthread_mutex_unlock(&u->mutex);
162 }
163
164 static void request_post(struct userdata *u) {
165 char c = 'x';
166 assert(u);
167
168 assert(u->pipe_fds[1] >= 0);
169 write(u->pipe_fds[1], &c, 1);
170 }
171
172 static void jack_shutdown(void *arg) {
173 struct userdata *u = arg;
174 assert(u);
175
176 u->quit_requested = 1;
177 request_post(u);
178 }
179
180 static int jack_process(jack_nframes_t nframes, void *arg) {
181 struct userdata *u = arg;
182 assert(u);
183
184 if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
185 unsigned c;
186
187 pthread_mutex_lock(&u->mutex);
188
189 u->frames_posted = nframes;
190
191 for (c = 0; c < u->channels; c++) {
192 u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
193 assert(u->buffer[c]);
194 }
195
196 request_post(u);
197
198 pthread_cond_wait(&u->cond, &u->mutex);
199
200 u->frames_in_buffer = nframes;
201 u->timestamp = jack_get_current_transport_frame(u->client);
202
203 pthread_mutex_unlock(&u->mutex);
204 }
205
206 return 0;
207 }
208
209 static pa_usec_t source_get_latency_cb(pa_source *s) {
210 struct userdata *u;
211 jack_nframes_t n, l, d;
212
213 assert(s);
214 u = s->userdata;
215
216 if (jack_transport_query(u->client, NULL) != JackTransportRolling)
217 return 0;
218
219 n = jack_get_current_transport_frame(u->client);
220
221 if (n < u->timestamp)
222 return 0;
223
224 d = n - u->timestamp;
225 l = jack_port_get_total_latency(u->client, u->port[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", pa_cstrerror(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|JackPortIsOutput);
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, PA_CHANNEL_MAP_ALSA);
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, JackPortIsInput|JackPortIsTerminal, 0))) {
315 pa_log(__FILE__": jack_port_register() failed.");
316 goto fail;
317 }
318 }
319
320 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
321 pa_log(__FILE__": failed to create source.");
322 goto fail;
323 }
324
325 u->source->userdata = u;
326 pa_source_set_owner(u->source, m);
327 u->source->description = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client));
328 u->source->get_latency = source_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, *p, jack_port_name(u->port[i]))) {
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->source) {
388 pa_source_disconnect(u->source);
389 pa_source_unref(u->source);
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 }