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