]> code.delx.au - pulseaudio/blob - src/modules/module-jack-source.c
649a8f98d19cb88a56e2a01036daaf4783c76637
[pulseaudio] / src / modules / module-jack-source.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <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 <pulse/xmalloc.h>
40
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/iochannel.h>
43 #include <pulsecore/source.h>
44 #include <pulsecore/module.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/log.h>
48 #include <pulse/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 int pipe_fd_type;
85 pa_io_event *io_event;
86
87 jack_nframes_t frames_in_buffer;
88 jack_nframes_t timestamp;
89 };
90
91 static const char* const valid_modargs[] = {
92 "source_name",
93 "server_name",
94 "client_name",
95 "channels",
96 "connect",
97 "channel_map",
98 NULL
99 };
100
101 static void stop_source(struct userdata *u) {
102 assert (u);
103
104 jack_client_close(u->client);
105 u->client = NULL;
106 u->core->mainloop->io_free(u->io_event);
107 u->io_event = NULL;
108 pa_source_disconnect(u->source);
109 pa_source_unref(u->source);
110 u->source = NULL;
111 pa_module_unload_request(u->module);
112 }
113
114 static void io_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
115 struct userdata *u = userdata;
116 char x;
117
118 assert(m);
119 assert(flags == PA_IO_EVENT_INPUT);
120 assert(u);
121 assert(u->pipe_fds[0] == fd);
122
123 pa_read(fd, &x, 1, &u->pipe_fd_type);
124
125 if (u->quit_requested) {
126 stop_source(u);
127 u->quit_requested = 0;
128 return;
129 }
130
131 pthread_mutex_lock(&u->mutex);
132
133 if (u->frames_posted > 0) {
134 unsigned fs;
135 jack_nframes_t frame_idx;
136 pa_memchunk chunk;
137
138 fs = pa_frame_size(&u->source->sample_spec);
139
140 chunk.memblock = pa_memblock_new(chunk.length = u->frames_posted * fs, u->core->memblock_stat);
141 chunk.index = 0;
142
143 for (frame_idx = 0; frame_idx < u->frames_posted; frame_idx ++) {
144 unsigned c;
145
146 for (c = 0; c < u->channels; c++) {
147 float *s = ((float*) u->buffer[c]) + frame_idx;
148 float *d = ((float*) ((uint8_t*) chunk.memblock->data + chunk.index)) + (frame_idx * u->channels) + c;
149
150 *d = *s;
151 }
152 }
153
154 pa_source_post(u->source, &chunk);
155 pa_memblock_unref(chunk.memblock);
156
157 u->frames_posted = 0;
158
159 pthread_cond_signal(&u->cond);
160 }
161
162 pthread_mutex_unlock(&u->mutex);
163 }
164
165 static void request_post(struct userdata *u) {
166 char c = 'x';
167 assert(u);
168
169 assert(u->pipe_fds[1] >= 0);
170 pa_write(u->pipe_fds[1], &c, 1, &u->pipe_fd_type);
171 }
172
173 static void jack_shutdown(void *arg) {
174 struct userdata *u = arg;
175 assert(u);
176
177 u->quit_requested = 1;
178 request_post(u);
179 }
180
181 static int jack_process(jack_nframes_t nframes, void *arg) {
182 struct userdata *u = arg;
183 assert(u);
184
185 if (jack_transport_query(u->client, NULL) == JackTransportRolling) {
186 unsigned c;
187
188 pthread_mutex_lock(&u->mutex);
189
190 u->frames_posted = nframes;
191
192 for (c = 0; c < u->channels; c++) {
193 u->buffer[c] = jack_port_get_buffer(u->port[c], nframes);
194 assert(u->buffer[c]);
195 }
196
197 request_post(u);
198
199 pthread_cond_wait(&u->cond, &u->mutex);
200
201 u->frames_in_buffer = nframes;
202 u->timestamp = jack_get_current_transport_frame(u->client);
203
204 pthread_mutex_unlock(&u->mutex);
205 }
206
207 return 0;
208 }
209
210 static pa_usec_t source_get_latency_cb(pa_source *s) {
211 struct userdata *u;
212 jack_nframes_t n, l, d;
213
214 assert(s);
215 u = s->userdata;
216
217 if (jack_transport_query(u->client, NULL) != JackTransportRolling)
218 return 0;
219
220 n = jack_get_current_transport_frame(u->client);
221
222 if (n < u->timestamp)
223 return 0;
224
225 d = n - u->timestamp;
226 l = jack_port_get_total_latency(u->client, u->port[0]);
227
228 return pa_bytes_to_usec((l + d) * pa_frame_size(&s->sample_spec), &s->sample_spec);
229 }
230
231 static void jack_error_func(const char*t) {
232 pa_log_warn(__FILE__": JACK error >%s<", t);
233 }
234
235 int pa__init(pa_core *c, pa_module*m) {
236 struct userdata *u = NULL;
237 pa_sample_spec ss;
238 pa_channel_map map;
239 pa_modargs *ma = NULL;
240 jack_status_t status;
241 const char *server_name, *client_name;
242 uint32_t channels = 0;
243 int connect = 1;
244 unsigned i;
245 const char **ports = NULL, **p;
246
247 assert(c);
248 assert(m);
249
250 jack_set_error_function(jack_error_func);
251
252 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
253 pa_log(__FILE__": failed to parse module arguments.");
254 goto fail;
255 }
256
257 if (pa_modargs_get_value_boolean(ma, "connect", &connect) < 0) {
258 pa_log(__FILE__": failed to parse connect= argument.");
259 goto fail;
260 }
261
262 server_name = pa_modargs_get_value(ma, "server_name", NULL);
263 client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio");
264
265 u = pa_xnew0(struct userdata, 1);
266 m->userdata = u;
267 u->core = c;
268 u->module = m;
269 u->pipe_fds[0] = u->pipe_fds[1] = -1;
270 u->pipe_fd_type = 0;
271
272 pthread_mutex_init(&u->mutex, NULL);
273 pthread_cond_init(&u->cond, NULL);
274
275 if (pipe(u->pipe_fds) < 0) {
276 pa_log(__FILE__": pipe() failed: %s", pa_cstrerror(errno));
277 goto fail;
278 }
279
280 pa_make_nonblock_fd(u->pipe_fds[1]);
281
282 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
283 pa_log(__FILE__": jack_client_open() failed.");
284 goto fail;
285 }
286
287 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
288
289 channels = 0;
290 for (p = ports; *p; p++)
291 channels++;
292
293 if (!channels)
294 channels = c->default_sample_spec.channels;
295
296 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
297 pa_log(__FILE__": failed to parse channels= argument.");
298 goto fail;
299 }
300
301 pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
302 if (pa_modargs_get_channel_map(ma, &map) < 0 || map.channels != channels) {
303 pa_log(__FILE__": failed to parse channel_map= argument.");
304 goto fail;
305 }
306
307 pa_log_info(__FILE__": Successfully connected as '%s'", jack_get_client_name(u->client));
308
309 ss.channels = u->channels = channels;
310 ss.rate = jack_get_sample_rate(u->client);
311 ss.format = PA_SAMPLE_FLOAT32NE;
312
313 assert(pa_sample_spec_valid(&ss));
314
315 for (i = 0; i < ss.channels; i++) {
316 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
317 pa_log(__FILE__": jack_port_register() failed.");
318 goto fail;
319 }
320 }
321
322 if (!(u->source = pa_source_new(c, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
323 pa_log(__FILE__": failed to create source.");
324 goto fail;
325 }
326
327 u->source->userdata = u;
328 pa_source_set_owner(u->source, m);
329 u->source->description = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client));
330 u->source->get_latency = source_get_latency_cb;
331
332 jack_set_process_callback(u->client, jack_process, u);
333 jack_on_shutdown(u->client, jack_shutdown, u);
334
335 if (jack_activate(u->client)) {
336 pa_log(__FILE__": jack_activate() failed");
337 goto fail;
338 }
339
340 if (connect) {
341 for (i = 0, p = ports; i < ss.channels; i++, p++) {
342
343 if (!*p) {
344 pa_log(__FILE__": not enough physical output ports, leaving unconnected.");
345 break;
346 }
347
348 pa_log_info(__FILE__": connecting %s to %s", jack_port_name(u->port[i]), *p);
349
350 if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
351 pa_log(__FILE__": failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
352 break;
353 }
354 }
355
356 }
357
358 u->io_event = c->mainloop->io_new(c->mainloop, u->pipe_fds[0], PA_IO_EVENT_INPUT, io_event_cb, u);
359
360 free(ports);
361 pa_modargs_free(ma);
362
363 return 0;
364
365 fail:
366 if (ma)
367 pa_modargs_free(ma);
368
369 free(ports);
370
371 pa__done(c, m);
372
373 return -1;
374 }
375
376 void pa__done(pa_core *c, pa_module*m) {
377 struct userdata *u;
378 assert(c && m);
379
380 if (!(u = m->userdata))
381 return;
382
383 if (u->client)
384 jack_client_close(u->client);
385
386 if (u->io_event)
387 c->mainloop->io_free(u->io_event);
388
389 if (u->source) {
390 pa_source_disconnect(u->source);
391 pa_source_unref(u->source);
392 }
393
394 if (u->pipe_fds[0] >= 0)
395 close(u->pipe_fds[0]);
396 if (u->pipe_fds[1] >= 0)
397 close(u->pipe_fds[1]);
398
399 pthread_mutex_destroy(&u->mutex);
400 pthread_cond_destroy(&u->cond);
401 pa_xfree(u);
402 }