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