]> code.delx.au - pulseaudio/blob - src/modules/jack/module-jack-source.c
Merge branch 'master' of git://0pointer.de/pulseaudio
[pulseaudio] / src / modules / jack / module-jack-source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
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.1 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 <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34
35 #include <jack/jack.h>
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/source.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/thread.h>
46 #include <pulsecore/thread-mq.h>
47 #include <pulsecore/rtpoll.h>
48 #include <pulsecore/sample-util.h>
49
50 #include "module-jack-source-symdef.h"
51
52 /* See module-jack-sink for a few comments how this module basically
53 * works */
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("JACK Source");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(TRUE);
59 PA_MODULE_USAGE(
60 "source_name=<name for the source> "
61 "source_properties=<properties for the source> "
62 "server_name=<jack server name> "
63 "client_name=<jack client name> "
64 "channels=<number of channels> "
65 "channel_map=<channel map> "
66 "connect=<connect ports?>");
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 "source_properties",
94 "server_name",
95 "client_name",
96 "channels",
97 "channel_map",
98 "connect",
99 NULL
100 };
101
102 enum {
103 SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX,
104 SOURCE_MESSAGE_ON_SHUTDOWN
105 };
106
107 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
108 struct userdata *u = PA_SOURCE(o)->userdata;
109
110 switch (code) {
111
112 case SOURCE_MESSAGE_POST:
113
114 /* Handle the new block from the JACK thread */
115 pa_assert(chunk);
116 pa_assert(chunk->length > 0);
117
118 if (u->source->thread_info.state == PA_SOURCE_RUNNING)
119 pa_source_post(u->source, chunk);
120
121 u->saved_frame_time = (jack_nframes_t) offset;
122 u->saved_frame_time_valid = TRUE;
123
124 return 0;
125
126 case SOURCE_MESSAGE_ON_SHUTDOWN:
127 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
128 return 0;
129
130 case PA_SOURCE_MESSAGE_GET_LATENCY: {
131 jack_nframes_t l, ft, d;
132 size_t n;
133
134 /* This is the "worst-case" latency */
135 l = jack_port_get_total_latency(u->client, u->port[0]);
136
137 if (u->saved_frame_time_valid) {
138 /* Adjust the worst case latency by the time that
139 * passed since we last handed data to JACK */
140
141 ft = jack_frame_time(u->client);
142 d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
143 l += d;
144 }
145
146 /* Convert it to usec */
147 n = l * pa_frame_size(&u->source->sample_spec);
148 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
149
150 return 0;
151 }
152 }
153
154 return pa_source_process_msg(o, code, data, offset, chunk);
155 }
156
157 static int jack_process(jack_nframes_t nframes, void *arg) {
158 unsigned c;
159 struct userdata *u = arg;
160 const void *buffer[PA_CHANNELS_MAX];
161 void *p;
162 jack_nframes_t frame_time;
163 pa_memchunk chunk;
164
165 pa_assert(u);
166
167 for (c = 0; c < u->channels; c++)
168 pa_assert_se(buffer[c] = jack_port_get_buffer(u->port[c], nframes));
169
170 /* We interleave the data and pass it on to the other RT thread */
171
172 pa_memchunk_reset(&chunk);
173 chunk.length = nframes * pa_frame_size(&u->source->sample_spec);
174 chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length);
175 p = pa_memblock_acquire(chunk.memblock);
176 pa_interleave(buffer, u->channels, p, sizeof(float), nframes);
177 pa_memblock_release(chunk.memblock);
178
179 frame_time = jack_frame_time(u->client);
180
181 pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, NULL, frame_time, &chunk, NULL);
182
183 pa_memblock_unref(chunk.memblock);
184
185 return 0;
186 }
187
188 static void thread_func(void *userdata) {
189 struct userdata *u = userdata;
190
191 pa_assert(u);
192
193 pa_log_debug("Thread starting up");
194
195 if (u->core->realtime_scheduling)
196 pa_make_realtime(u->core->realtime_priority);
197
198 pa_thread_mq_install(&u->thread_mq);
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->realtime_scheduling)
234 pa_make_realtime(u->core->realtime_priority+4);
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 pa_bool_t do_connect = TRUE;
253 unsigned i;
254 const char **ports = NULL, **p;
255 pa_source_new_data data;
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 m->userdata = u = pa_xnew0(struct userdata, 1);
275 u->core = m->core;
276 u->module = m;
277 u->saved_frame_time_valid = FALSE;
278 u->rtpoll = pa_rtpoll_new();
279 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
280
281 u->jack_msgq = pa_asyncmsgq_new(0);
282 u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq);
283
284 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
285 pa_log("jack_client_open() failed.");
286 goto fail;
287 }
288
289 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput);
290
291 channels = 0;
292 for (p = ports; *p; p++)
293 channels++;
294
295 if (!channels)
296 channels = m->core->default_sample_spec.channels;
297
298 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
299 channels <= 0 ||
300 channels >= PA_CHANNELS_MAX) {
301 pa_log("failed to parse channels= argument.");
302 goto fail;
303 }
304
305 if (channels == m->core->default_channel_map.channels)
306 map = m->core->default_channel_map;
307 else
308 pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA);
309
310 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) {
311 pa_log("failed to parse channel_map= argument.");
312 goto fail;
313 }
314
315 pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
316
317 u->channels = ss.channels = (uint8_t) channels;
318 ss.rate = jack_get_sample_rate(u->client);
319 ss.format = PA_SAMPLE_FLOAT32NE;
320
321 pa_assert(pa_sample_spec_valid(&ss));
322
323 for (i = 0; i < ss.channels; i++) {
324 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) {
325 pa_log("jack_port_register() failed.");
326 goto fail;
327 }
328 }
329
330 pa_source_new_data_init(&data);
331 data.driver = __FILE__;
332 data.module = m;
333 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
334 pa_source_new_data_set_sample_spec(&data, &ss);
335 pa_source_new_data_set_channel_map(&data, &map);
336 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack");
337 if (server_name)
338 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name);
339 pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Jack source (%s)", jack_get_client_name(u->client));
340 pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client));
341
342 if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
343 pa_log("Invalid properties");
344 pa_source_new_data_done(&data);
345 goto fail;
346 }
347
348 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
349 pa_source_new_data_done(&data);
350
351 if (!u->source) {
352 pa_log("Failed to create source.");
353 goto fail;
354 }
355
356 u->source->parent.process_msg = source_process_msg;
357 u->source->userdata = u;
358
359 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
360 pa_source_set_rtpoll(u->source, u->rtpoll);
361
362 jack_set_process_callback(u->client, jack_process, u);
363 jack_on_shutdown(u->client, jack_shutdown, u);
364 jack_set_thread_init_callback(u->client, jack_init, u);
365
366 if (!(u->thread = pa_thread_new(thread_func, u))) {
367 pa_log("Failed to create thread.");
368 goto fail;
369 }
370
371 if (jack_activate(u->client)) {
372 pa_log("jack_activate() failed");
373 goto fail;
374 }
375
376 if (do_connect) {
377 for (i = 0, p = ports; i < ss.channels; i++, p++) {
378
379 if (!*p) {
380 pa_log("Not enough physical output ports, leaving unconnected.");
381 break;
382 }
383
384 pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p);
385
386 if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) {
387 pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
388 break;
389 }
390 }
391
392 }
393
394 pa_source_put(u->source);
395
396 free(ports);
397 pa_modargs_free(ma);
398
399 return 0;
400
401 fail:
402 if (ma)
403 pa_modargs_free(ma);
404
405 free(ports);
406
407 pa__done(m);
408
409 return -1;
410 }
411
412 int pa__get_n_used(pa_module *m) {
413 struct userdata *u;
414
415 pa_assert(m);
416 pa_assert_se(u = m->userdata);
417
418 return pa_source_linked_by(u->source);
419 }
420
421 void pa__done(pa_module*m) {
422 struct userdata *u;
423 pa_assert(m);
424
425 if (!(u = m->userdata))
426 return;
427
428 if (u->client)
429 jack_client_close(u->client);
430
431 if (u->source)
432 pa_source_unlink(u->source);
433
434 if (u->thread) {
435 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
436 pa_thread_free(u->thread);
437 }
438
439 pa_thread_mq_done(&u->thread_mq);
440
441 if (u->source)
442 pa_source_unref(u->source);
443
444 if (u->rtpoll_item)
445 pa_rtpoll_item_free(u->rtpoll_item);
446
447 if (u->jack_msgq)
448 pa_asyncmsgq_unref(u->jack_msgq);
449
450 if (u->rtpoll)
451 pa_rtpoll_free(u->rtpoll);
452
453 pa_xfree(u);
454 }