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