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