]> code.delx.au - pulseaudio/blob - src/modules/module-jack-source.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-jack-source.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 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 <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <limits.h>
36
37 #include <jack/jack.h>
38
39 #include <pulse/xmalloc.h>
40
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/sample-util.h>
51
52 #include "module-jack-source-symdef.h"
53
54 /* See module-jack-sink for a few comments how this module basically
55 * works */
56
57 PA_MODULE_AUTHOR("Lennart Poettering")
58 PA_MODULE_DESCRIPTION("JACK Source")
59 PA_MODULE_VERSION(PACKAGE_VERSION)
60 PA_MODULE_USAGE(
61 "source_name=<name of source> "
62 "server_name=<jack server name> "
63 "client_name=<jack client name> "
64 "channels=<number of channels> "
65 "connect=<connect ports?>"
66 "channel_map=<channel map>")
67
68 #define DEFAULT_SOURCE_NAME "jack_in"
69
70 struct userdata {
71 pa_core *core;
72 pa_module *module;
73 pa_source *source;
74
75 unsigned channels;
76
77 jack_port_t* port[PA_CHANNELS_MAX];
78 jack_client_t *client;
79
80 pa_thread_mq thread_mq;
81 pa_asyncmsgq *jack_msgq;
82 pa_rtpoll *rtpoll;
83 pa_rtpoll_item *rtpoll_item;
84
85 pa_thread *thread;
86
87 jack_nframes_t saved_frame_time;
88 pa_bool_t saved_frame_time_valid;
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 enum {
102 SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX,
103 SOURCE_MESSAGE_ON_SHUTDOWN
104 };
105
106 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
107 struct userdata *u = PA_SOURCE(o)->userdata;
108
109 switch (code) {
110
111 case SOURCE_MESSAGE_POST:
112
113 /* Handle the new block from the JACK thread */
114 pa_assert(chunk);
115 pa_assert(chunk->length > 0);
116
117 if (u->source->thread_info.state == PA_SOURCE_RUNNING)
118 pa_source_post(u->source, chunk);
119
120 u->saved_frame_time = offset;
121 u->saved_frame_time_valid = TRUE;
122
123 return 0;
124
125 case SOURCE_MESSAGE_ON_SHUTDOWN:
126 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
127 return 0;
128
129 case PA_SOURCE_MESSAGE_GET_LATENCY: {
130 jack_nframes_t l, ft, d;
131 size_t n;
132
133 /* This is the "worst-case" latency */
134 l = jack_port_get_total_latency(u->client, u->port[0]);
135
136 if (u->saved_frame_time_valid) {
137 /* Adjust the worst case latency by the time that
138 * passed since we last handed data to JACK */
139
140 ft = jack_frame_time(u->client);
141 d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
142 l += d;
143 }
144
145 /* Convert it to usec */
146 n = l * pa_frame_size(&u->source->sample_spec);
147 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
148
149 return 0;
150 }
151 }
152
153 return pa_source_process_msg(o, code, data, offset, chunk);
154 }
155
156 static int jack_process(jack_nframes_t nframes, void *arg) {
157 unsigned c;
158 struct userdata *u = arg;
159 const void *buffer[PA_CHANNELS_MAX];
160 void *p;
161 jack_nframes_t frame_time;
162 pa_memchunk chunk;
163
164 pa_assert(u);
165
166 for (c = 0; c < u->channels; c++)
167 pa_assert(buffer[c] = jack_port_get_buffer(u->port[c], nframes));
168
169 /* We interleave the data and pass it on to the other RT thread */
170
171 pa_memchunk_reset(&chunk);
172 chunk.length = nframes * pa_frame_size(&u->source->sample_spec);
173 chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
174 p = pa_memblock_acquire(chunk.memblock);
175 pa_interleave(buffer, u->channels, p, sizeof(float), nframes);
176 pa_memblock_release(chunk.memblock);
177
178 frame_time = jack_frame_time(u->client);
179
180 pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, NULL, frame_time, &chunk, NULL);
181
182 pa_memblock_unref(chunk.memblock);
183
184 return 0;
185 }
186
187 static void thread_func(void *userdata) {
188 struct userdata *u = userdata;
189
190 pa_assert(u);
191
192 pa_log_debug("Thread starting up");
193
194 if (u->core->high_priority)
195 pa_make_realtime();
196
197 pa_thread_mq_install(&u->thread_mq);
198 pa_rtpoll_install(u->rtpoll);
199
200 for (;;) {
201 int ret;
202
203 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
204 goto fail;
205
206 if (ret == 0)
207 goto finish;
208 }
209
210 fail:
211 /* If this was no regular exit from the loop we have to continue
212 * processing messages until we received PA_MESSAGE_SHUTDOWN */
213 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
214 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
215
216 finish:
217 pa_log_debug("Thread shutting down");
218 }
219
220 static void jack_error_func(const char*t) {
221 char *s;
222
223 s = pa_xstrndup(t, strcspn(t, "\n\r"));
224 pa_log_warn("JACK error >%s<", s);
225 pa_xfree(s);
226 }
227
228 static void jack_init(void *arg) {
229 struct userdata *u = arg;
230
231 pa_log_info("JACK thread starting up.");
232
233 if (u->core->high_priority)
234 pa_make_realtime();
235 }
236
237 static void jack_shutdown(void* arg) {
238 struct userdata *u = arg;
239
240 pa_log_info("JACK thread shutting down..");
241 pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL);
242 }
243
244 int pa__init(pa_module*m) {
245 struct userdata *u = NULL;
246 pa_sample_spec ss;
247 pa_channel_map map;
248 pa_modargs *ma = NULL;
249 jack_status_t status;
250 const char *server_name, *client_name;
251 uint32_t channels = 0;
252 int do_connect = 1;
253 unsigned i;
254 const char **ports = NULL, **p;
255 char *t;
256
257 pa_assert(m);
258
259 jack_set_error_function(jack_error_func);
260
261 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
262 pa_log("Failed to parse module arguments.");
263 goto fail;
264 }
265
266 if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
267 pa_log("Failed to parse connect= argument.");
268 goto fail;
269 }
270
271 server_name = pa_modargs_get_value(ma, "server_name", NULL);
272 client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Source");
273
274 u = pa_xnew0(struct userdata, 1);
275 u->core = m->core;
276 u->module = m;
277 m->userdata = u;
278 u->saved_frame_time_valid = FALSE;
279
280 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
281 u->rtpoll = pa_rtpoll_new();
282 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
283
284 u->jack_msgq = pa_asyncmsgq_new(0);
285 u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq);
286
287 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
288 pa_log("jack_client_open() failed.");
289 goto fail;
290 }
291
292 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
293
294 channels = 0;
295 for (p = ports; *p; p++)
296 channels++;
297
298 if (!channels)
299 channels = m->core->default_sample_spec.channels;
300
301 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
302 pa_log("failed to parse channels= argument.");
303 goto fail;
304 }
305
306 pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
307 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) {
308 pa_log("failed to parse channel_map= argument.");
309 goto fail;
310 }
311
312 pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
313
314 ss.channels = u->channels = channels;
315 ss.rate = jack_get_sample_rate(u->client);
316 ss.format = PA_SAMPLE_FLOAT32NE;
317
318 pa_assert(pa_sample_spec_valid(&ss));
319
320 for (i = 0; i < ss.channels; i++) {
321 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
322 pa_log("jack_port_register() failed.");
323 goto fail;
324 }
325 }
326
327 if (!(u->source = pa_source_new(m->core, __FILE__, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME), 0, &ss, &map))) {
328 pa_log("failed to create source.");
329 goto fail;
330 }
331
332 u->source->parent.process_msg = source_process_msg;
333 u->source->userdata = u;
334 u->source->flags = PA_SOURCE_LATENCY;
335
336 pa_source_set_module(u->source, m);
337 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
338 pa_source_set_rtpoll(u->source, u->rtpoll);
339 pa_source_set_description(u->source, t = pa_sprintf_malloc("Jack source (%s)", jack_get_client_name(u->client)));
340 pa_xfree(t);
341
342 jack_set_process_callback(u->client, jack_process, u);
343 jack_on_shutdown(u->client, jack_shutdown, u);
344 jack_set_thread_init_callback(u->client, jack_init, u);
345
346 if (!(u->thread = pa_thread_new(thread_func, u))) {
347 pa_log("Failed to create thread.");
348 goto fail;
349 }
350
351 if (jack_activate(u->client)) {
352 pa_log("jack_activate() failed");
353 goto fail;
354 }
355
356 if (do_connect) {
357 for (i = 0, p = ports; i < ss.channels; i++, p++) {
358
359 if (!*p) {
360 pa_log("not enough physical output ports, leaving unconnected.");
361 break;
362 }
363
364 pa_log_info("connecting %s to %s", jack_port_name(u->port[i]), *p);
365
366 if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
367 pa_log("failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
368 break;
369 }
370 }
371
372 }
373
374 pa_source_put(u->source);
375
376 free(ports);
377 pa_modargs_free(ma);
378
379 return 0;
380
381 fail:
382 if (ma)
383 pa_modargs_free(ma);
384
385 free(ports);
386
387 pa__done(m);
388
389 return -1;
390 }
391
392 void pa__done(pa_module*m) {
393 struct userdata *u;
394 pa_assert(m);
395
396 if (!(u = m->userdata))
397 return;
398
399 if (u->client)
400 jack_client_close(u->client);
401
402 if (u->source)
403 pa_source_unlink(u->source);
404
405 if (u->thread) {
406 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
407 pa_thread_free(u->thread);
408 }
409
410 pa_thread_mq_done(&u->thread_mq);
411
412 if (u->source)
413 pa_source_unref(u->source);
414
415 if (u->rtpoll_item)
416 pa_rtpoll_item_free(u->rtpoll_item);
417
418 if (u->jack_msgq)
419 pa_asyncmsgq_unref(u->jack_msgq);
420
421 if (u->rtpoll)
422 pa_rtpoll_free(u->rtpoll);
423
424 pa_xfree(u);
425 }