]> code.delx.au - pulseaudio/blob - src/modules/module-jack-sink.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / modules / module-jack-sink.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 <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include <jack/jack.h>
39
40 #include <pulse/xmalloc.h>
41
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/module.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/thread.h>
49 #include <pulsecore/thread-mq.h>
50 #include <pulsecore/rtpoll.h>
51 #include <pulsecore/sample-util.h>
52
53 #include "module-jack-sink-symdef.h"
54
55 /* General overview:
56 *
57 * Because JACK has a very unflexible event loop management, which
58 * doesn't allow us to add our own event sources to the event thread
59 * we cannot use the JACK real-time thread for dispatching our PA
60 * work. Instead, we run an additional RT thread which does most of
61 * the PA handling, and have the JACK RT thread request data from it
62 * via pa_asyncmsgq. The cost is an additional context switch which
63 * should hopefully not be that expensive if RT scheduling is
64 * enabled. A better fix would only be possible with additional event
65 * source support in JACK.
66 */
67
68 PA_MODULE_AUTHOR("Lennart Poettering")
69 PA_MODULE_DESCRIPTION("JACK Sink")
70 PA_MODULE_VERSION(PACKAGE_VERSION)
71 PA_MODULE_USAGE(
72 "sink_name=<name of sink> "
73 "server_name=<jack server name> "
74 "client_name=<jack client name> "
75 "channels=<number of channels> "
76 "connect=<connect ports?> "
77 "channel_map=<channel map>")
78
79 #define DEFAULT_SINK_NAME "jack_out"
80
81 struct userdata {
82 pa_core *core;
83 pa_module *module;
84 pa_sink *sink;
85
86 unsigned channels;
87
88 jack_port_t* port[PA_CHANNELS_MAX];
89 jack_client_t *client;
90
91 void *buffer[PA_CHANNELS_MAX];
92
93 pa_thread_mq thread_mq;
94 pa_asyncmsgq *jack_msgq;
95 pa_rtpoll *rtpoll;
96 pa_rtpoll_item *rtpoll_item;
97
98 pa_thread *thread;
99
100 jack_nframes_t frames_in_buffer;
101 jack_nframes_t saved_frame_time;
102 pa_bool_t saved_frame_time_valid;
103 };
104
105 static const char* const valid_modargs[] = {
106 "sink_name",
107 "server_name",
108 "client_name",
109 "channels",
110 "connect",
111 "channel_map",
112 NULL
113 };
114
115 enum {
116 SINK_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX,
117 SINK_MESSAGE_ON_SHUTDOWN
118 };
119
120 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) {
121 struct userdata *u = PA_SINK(o)->userdata;
122
123 switch (code) {
124
125 case SINK_MESSAGE_RENDER:
126
127 /* Handle the request from the JACK thread */
128
129 if (u->sink->thread_info.state == PA_SINK_RUNNING) {
130 pa_memchunk chunk;
131 size_t nbytes;
132 void *p;
133
134 pa_assert(offset > 0);
135 nbytes = offset * pa_frame_size(&u->sink->sample_spec);
136
137 pa_sink_render_full(u->sink, nbytes, &chunk);
138
139 p = (uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index;
140 pa_deinterleave(p, u->buffer, u->channels, sizeof(float), offset);
141 pa_memblock_release(chunk.memblock);
142
143 pa_memblock_unref(chunk.memblock);
144 } else {
145 unsigned c;
146 pa_sample_spec ss;
147
148 /* Humm, we're not RUNNING, hence let's write some silence */
149
150 ss = u->sink->sample_spec;
151 ss.channels = 1;
152
153 for (c = 0; c < u->channels; c++)
154 pa_silence_memory(u->buffer[c], offset * pa_sample_size(&ss), &ss);
155 }
156
157 u->frames_in_buffer = offset;
158 u->saved_frame_time = * (jack_nframes_t*) data;
159 u->saved_frame_time_valid = TRUE;
160
161 return 0;
162
163 case SINK_MESSAGE_ON_SHUTDOWN:
164 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
165 return 0;
166
167 case PA_SINK_MESSAGE_GET_LATENCY: {
168 jack_nframes_t l, ft, d;
169 size_t n;
170
171 /* This is the "worst-case" latency */
172 l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer;
173
174 if (u->saved_frame_time_valid) {
175 /* Adjust the worst case latency by the time that
176 * passed since we last handed data to JACK */
177
178 ft = jack_frame_time(u->client);
179 d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
180 l = l > d ? l - d : 0;
181 }
182
183 /* Convert it to usec */
184 n = l * pa_frame_size(&u->sink->sample_spec);
185 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
186
187 return 0;
188 }
189 }
190
191 return pa_sink_process_msg(o, code, data, offset, memchunk);
192 }
193
194 static int jack_process(jack_nframes_t nframes, void *arg) {
195 struct userdata *u = arg;
196 unsigned c;
197 jack_nframes_t frame_time;
198 pa_assert(u);
199
200 /* We just forward the request to our other RT thread */
201
202 for (c = 0; c < u->channels; c++)
203 pa_assert_se(u->buffer[c] = jack_port_get_buffer(u->port[c], nframes));
204
205 frame_time = jack_frame_time(u->client);
206
207 pa_assert_se(pa_asyncmsgq_send(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RENDER, &frame_time, nframes, NULL) == 0);
208 return 0;
209 }
210
211 static void thread_func(void *userdata) {
212 struct userdata *u = userdata;
213
214 pa_assert(u);
215
216 pa_log_debug("Thread starting up");
217
218 if (u->core->high_priority)
219 pa_make_realtime();
220
221 pa_thread_mq_install(&u->thread_mq);
222 pa_rtpoll_install(u->rtpoll);
223
224 for (;;) {
225 int ret;
226
227 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
228 goto fail;
229
230 if (ret == 0)
231 goto finish;
232 }
233
234 fail:
235 /* If this was no regular exit from the loop we have to continue
236 * processing messages until we received PA_MESSAGE_SHUTDOWN */
237 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
238 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
239
240 finish:
241 pa_log_debug("Thread shutting down");
242 }
243
244 static void jack_error_func(const char*t) {
245 char *s;
246
247 s = pa_xstrndup(t, strcspn(t, "\n\r"));
248 pa_log_warn("JACK error >%s<", s);
249 pa_xfree(s);
250 }
251
252 static void jack_init(void *arg) {
253 struct userdata *u = arg;
254
255 pa_log_info("JACK thread starting up.");
256
257 if (u->core->high_priority)
258 pa_make_realtime();
259 }
260
261 static void jack_shutdown(void* arg) {
262 struct userdata *u = arg;
263
264 pa_log_info("JACK thread shutting down..");
265 pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL);
266 }
267
268 int pa__init(pa_module*m) {
269 struct userdata *u = NULL;
270 pa_sample_spec ss;
271 pa_channel_map map;
272 pa_modargs *ma = NULL;
273 jack_status_t status;
274 const char *server_name, *client_name;
275 uint32_t channels = 0;
276 int do_connect = 1;
277 unsigned i;
278 const char **ports = NULL, **p;
279 char *t;
280
281 pa_assert(m);
282
283 jack_set_error_function(jack_error_func);
284
285 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
286 pa_log("Failed to parse module arguments.");
287 goto fail;
288 }
289
290 if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) {
291 pa_log("Failed to parse connect= argument.");
292 goto fail;
293 }
294
295 server_name = pa_modargs_get_value(ma, "server_name", NULL);
296 client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Sink");
297
298 u = pa_xnew0(struct userdata, 1);
299 u->core = m->core;
300 u->module = m;
301 m->userdata = u;
302 u->saved_frame_time_valid = FALSE;
303 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
304 u->rtpoll = pa_rtpoll_new();
305 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
306
307 /* The queue linking the JACK thread and our RT thread */
308 u->jack_msgq = pa_asyncmsgq_new(0);
309
310 /* The msgq from the JACK RT thread should have an even higher
311 * priority than the normal message queues, to match the guarantee
312 * all other drivers make: supplying the audio device with data is
313 * the top priority -- and as long as that is possible we don't do
314 * anything else */
315 u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq);
316
317 if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) {
318 pa_log("jack_client_open() failed.");
319 goto fail;
320 }
321
322 ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput);
323
324 channels = 0;
325 for (p = ports; *p; p++)
326 channels++;
327
328 if (!channels)
329 channels = m->core->default_sample_spec.channels;
330
331 if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || channels <= 0 || channels >= PA_CHANNELS_MAX) {
332 pa_log("Failed to parse channels= argument.");
333 goto fail;
334 }
335
336 pa_channel_map_init_auto(&map, channels, PA_CHANNEL_MAP_ALSA);
337 if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) {
338 pa_log("Failed to parse channel_map= argument.");
339 goto fail;
340 }
341
342 pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client));
343
344 ss.channels = u->channels = channels;
345 ss.rate = jack_get_sample_rate(u->client);
346 ss.format = PA_SAMPLE_FLOAT32NE;
347
348 pa_assert(pa_sample_spec_valid(&ss));
349
350 for (i = 0; i < ss.channels; i++) {
351 if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) {
352 pa_log("jack_port_register() failed.");
353 goto fail;
354 }
355 }
356
357 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
358 pa_log("failed to create sink.");
359 goto fail;
360 }
361
362 u->sink->parent.process_msg = sink_process_msg;
363 u->sink->userdata = u;
364 u->sink->flags = PA_SINK_LATENCY;
365
366 pa_sink_set_module(u->sink, m);
367 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
368 pa_sink_set_rtpoll(u->sink, u->rtpoll);
369 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Jack sink (%s)", jack_get_client_name(u->client)));
370 pa_xfree(t);
371
372 jack_set_process_callback(u->client, jack_process, u);
373 jack_on_shutdown(u->client, jack_shutdown, u);
374 jack_set_thread_init_callback(u->client, jack_init, u);
375
376 if (!(u->thread = pa_thread_new(thread_func, u))) {
377 pa_log("Failed to create thread.");
378 goto fail;
379 }
380
381 if (jack_activate(u->client)) {
382 pa_log("jack_activate() failed");
383 goto fail;
384 }
385
386 if (do_connect) {
387 for (i = 0, p = ports; i < ss.channels; i++, p++) {
388
389 if (!*p) {
390 pa_log("Not enough physical output ports, leaving unconnected.");
391 break;
392 }
393
394 pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p);
395
396 if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) {
397 pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p);
398 break;
399 }
400 }
401 }
402
403 pa_sink_put(u->sink);
404
405 free(ports);
406 pa_modargs_free(ma);
407
408 return 0;
409
410 fail:
411 if (ma)
412 pa_modargs_free(ma);
413
414 free(ports);
415
416 pa__done(m);
417
418 return -1;
419 }
420
421 void pa__done(pa_module*m) {
422 struct userdata *u;
423
424 pa_assert(m);
425
426 if (!(u = m->userdata))
427 return;
428
429 if (u->client)
430 jack_client_close(u->client);
431
432 if (u->sink)
433 pa_sink_unlink(u->sink);
434
435 if (u->thread) {
436 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
437 pa_thread_free(u->thread);
438 }
439
440 pa_thread_mq_done(&u->thread_mq);
441
442 if (u->sink)
443 pa_sink_unref(u->sink);
444
445 if (u->rtpoll_item)
446 pa_rtpoll_item_free(u->rtpoll_item);
447
448 if (u->jack_msgq)
449 pa_asyncmsgq_unref(u->jack_msgq);
450
451 if (u->rtpoll)
452 pa_rtpoll_free(u->rtpoll);
453
454 pa_xfree(u);
455 }