]> git.ipfire.org Git - thirdparty/shairport-sync.git/blob - audio_jack.c
Update check_classic_systemd_full.yml
[thirdparty/shairport-sync.git] / audio_jack.c
1 /*
2 * jack output driver. This file is part of Shairport Sync.
3 * Copyright (c) 2019 -- 2022 Mike Brady <4265913+mikebrady@users.noreply.github.com>,
4 * Jörn Nettingsmeier <nettings@luchtbeweging.nl>
5 *
6 * All rights reserved.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 #include "audio.h"
22 #include "common.h"
23 #include <errno.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <jack/jack.h>
30 #include <jack/ringbuffer.h>
31
32 #ifdef CONFIG_SOXR
33 #include <soxr.h>
34 #endif
35
36 #define NPORTS 2
37
38 typedef jack_default_audio_sample_t sample_t;
39
40 #define jack_sample_size sizeof(sample_t)
41
42 // Two-channel, 32bit audio:
43 const int bytes_per_frame = NPORTS * jack_sample_size;
44
45 pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
46 pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48 // This also affects deinterlacing.
49 // So make it exactly the number of incoming audio channels!
50 jack_port_t *port[NPORTS];
51 const char *port_name[NPORTS] = {"out_L", "out_R"};
52
53 jack_client_t *client;
54 jack_nframes_t sample_rate;
55 jack_nframes_t jack_latency;
56
57 jack_ringbuffer_t *jackbuf;
58 int flush_please = 0;
59
60 jack_latency_range_t latest_latency_range[NPORTS];
61 int64_t time_of_latest_transfer;
62
63 #ifdef CONFIG_SOXR
64 typedef struct soxr_quality {
65 int quality;
66 const char *name;
67 } soxr_quality_t;
68
69 soxr_quality_t soxr_quality_table[] = {{SOXR_VHQ, "very high"}, {SOXR_HQ, "high"},
70 {SOXR_MQ, "medium"}, {SOXR_LQ, "low"},
71 {SOXR_QQ, "quick"}, {-1, NULL}};
72
73 static int parse_soxr_quality_name(const char *name) {
74 for (soxr_quality_t *s = soxr_quality_table; s->name != NULL; ++s) {
75 if (!strcmp(s->name, name)) {
76 return s->quality;
77 }
78 }
79 return -1;
80 }
81
82 soxr_t soxr = NULL;
83 soxr_quality_spec_t quality_spec;
84 soxr_io_spec_t io_spec;
85 #endif
86
87 static inline sample_t sample_conv(short sample) {
88 // It sounds correct, but I don't understand it.
89 // Zero int needs to be zero float. Check.
90 // Plus 32767 int is 1.0. Check.
91 // Minus 32767 int is -0.99997. And here my brain shuts down.
92 // In my head, it should be 1.0, and we should tolerate an overflow
93 // at minus 32768. But I'm sure there's a textbook explanation somewhere.
94 return ((sample < 0) ? (-1.0 * sample / SHRT_MIN) : (1.0 * sample / SHRT_MAX));
95 }
96
97 static void deinterleave(const char *interleaved_input_buffer, sample_t *jack_output_buffer[],
98 jack_nframes_t offset, jack_nframes_t nframes) {
99 jack_nframes_t f;
100 // We're dealing with 16bit audio here:
101 sample_t *ifp = (sample_t *)interleaved_input_buffer;
102 // Zero-copy, we're working directly on the target and destination buffers,
103 // so deal with an offset for the second part of the input ringbuffer
104 for (f = offset; f < (nframes + offset); f++) {
105 for (int i = 0; i < NPORTS; i++) {
106 jack_output_buffer[i][f] = *ifp++;
107 }
108 }
109 }
110
111 // This is the JACK process callback. We don't decide when it runs.
112 // It must be hard-realtime safe (i.e. fully deterministic, with constant CPU
113 // usage. No calls to anything that could ever block: no syscalls, no screen
114 // output, no file access, no mutexes...
115 // The JACK ringbuffer we use to get the data in here is explicitly lock-free.
116 static int process(jack_nframes_t nframes, __attribute__((unused)) void *arg) {
117 sample_t *buffer[NPORTS];
118 // Expect an array of two elements because of possible ringbuffer wrap-around:
119 jack_ringbuffer_data_t v[2] = {0};
120 jack_nframes_t i, thisbuf;
121 int frames_written = 0;
122 int frames_required = 0;
123
124 for (i = 0; i < NPORTS; i++) {
125 buffer[i] = (sample_t *)jack_port_get_buffer(port[i], nframes);
126 }
127 if (flush_please) {
128 // We just move the read pointer ahead without doing anything with the data.
129 jack_ringbuffer_read_advance(jackbuf, jack_ringbuffer_read_space(jackbuf));
130 flush_please = 0;
131 // Since we don't change nframes, the whole buffer will be zeroed later.
132 } else {
133 jack_ringbuffer_get_read_vector(jackbuf, v);
134 for (i = 0; i < 2; i++) {
135 thisbuf = v[i].len / bytes_per_frame;
136 if (thisbuf > nframes) {
137 frames_required = nframes;
138 } else {
139 frames_required = thisbuf;
140 }
141 deinterleave(v[i].buf, buffer, frames_written, frames_required);
142 frames_written += frames_required;
143 nframes -= frames_required;
144 }
145 jack_ringbuffer_read_advance(jackbuf, frames_written * bytes_per_frame);
146 }
147 // If there are any more frames to put into the buffer, fill them with
148 // silence. This is a critical underflow situation. Let's at least keep the JACK
149 // graph humming along while preventing the motorboat sound of a repeating buffer.
150 while (nframes > 0) {
151 for (i = 0; i < NPORTS; i++) {
152 buffer[i][frames_written] = 0.0;
153 }
154 frames_written++;
155 nframes--;
156 }
157 return 0; // Tell JACK that all is well.
158 }
159
160 // This is the JACK graph reorder callback. Now we know some JACK connections
161 // have changed, so we recompute the latency.
162 static int graph(__attribute__((unused)) void *arg) {
163 int latency = 0;
164 debug(2, "JACK graph reorder callback called.");
165 for (int i = 0; i < NPORTS; i++) {
166 jack_port_get_latency_range(port[i], JackPlaybackLatency, &latest_latency_range[i]);
167 debug(2, "JACK latency for port %s\tmin: %d\t max: %d", port_name[i],
168 latest_latency_range[i].min, latest_latency_range[i].max);
169 latency += latest_latency_range[i].max;
170 }
171 latency /= NPORTS;
172 jack_latency = latency;
173 debug(1, "Average maximum JACK latency across all ports: %d", jack_latency);
174 return 0;
175 }
176
177 // This the function JACK will call in case of an error in the library.
178 static void error(const char *desc) { warn("JACK error: \"%s\"", desc); }
179
180 // This is the function JACK will call in case of a non-critical event in the library.
181 static void info(const char *desc) { inform("JACK information: \"%s\"", desc); }
182
183 static int jack_init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
184 int i;
185 int bufsz = -1;
186 config.audio_backend_latency_offset = 0;
187 config.audio_backend_buffer_desired_length = 0.500;
188 // Below this, soxr interpolation will not occur -- it'll be basic interpolation
189 // instead.
190 config.audio_backend_buffer_interpolation_threshold_in_seconds = 0.25;
191
192 // Do the "general" audio options. Note, these options are in the "general" stanza!
193 parse_general_audio_options();
194 #ifdef CONFIG_SOXR
195 config.jack_soxr_resample_quality = -1; // don't resample by default
196 #endif
197
198 // Now the options specific to the backend, from the "jack" stanza:
199 if (config.cfg != NULL) {
200 const char *str;
201 if (config_lookup_string(config.cfg, "jack.client_name", &str)) {
202 config.jack_client_name = (char *)str;
203 }
204 if (config_lookup_string(config.cfg, "jack.autoconnect_pattern", &str)) {
205 config.jack_autoconnect_pattern = (char *)str;
206 }
207 #ifdef CONFIG_SOXR
208 if (config_lookup_string(config.cfg, "jack.soxr_resample_quality", &str)) {
209 debug(1, "SOXR quality %s", str);
210 config.jack_soxr_resample_quality = parse_soxr_quality_name(str);
211 }
212 #endif
213 if (config_lookup_int(config.cfg, "jack.bufsz", &bufsz) && bufsz <= 0)
214 die("jack: bufsz must be > 0");
215 }
216 if (config.jack_client_name == NULL)
217 config.jack_client_name = strdup("shairport-sync");
218
219 // by default a buffer that can hold up to 4 seconds of 48kHz samples
220 if (bufsz <= 0)
221 bufsz = 48000 * 4 * bytes_per_frame;
222
223 jackbuf = jack_ringbuffer_create((size_t)bufsz);
224 if (jackbuf == NULL)
225 die("Can't allocate %d bytes for the JACK ringbuffer.", bufsz);
226 // Lock the ringbuffer into memory so that it never gets paged out, which would
227 // break realtime constraints.
228 jack_ringbuffer_mlock(jackbuf);
229 // This mutex should not be necessary, but removing it causes segfaults on
230 // shutdown. Apparently, there are multiple threads in the main program trying
231 // to do stuff. FIXME: Try to consolidate into one thread and get rid of this lock.
232 pthread_mutex_lock(&client_mutex);
233 jack_status_t status;
234 client = jack_client_open(config.jack_client_name, JackNoStartServer, &status);
235 if (!client) {
236 die("Could not start JACK server. JackStatus is %x", status);
237 }
238 sample_rate = jack_get_sample_rate(client);
239 #ifdef CONFIG_SOXR
240 if (config.jack_soxr_resample_quality >= SOXR_QQ) {
241 quality_spec = soxr_quality_spec(config.jack_soxr_resample_quality, 0);
242 io_spec = soxr_io_spec(SOXR_INT16_I, SOXR_FLOAT32_I);
243 } else
244 #endif
245 if (sample_rate != 44100) {
246 die("The JACK server is running at the wrong sample rate (%d) for Shairport Sync."
247 " Must be 44100 Hz.",
248 sample_rate);
249 }
250 jack_set_process_callback(client, &process, NULL);
251 jack_set_graph_order_callback(client, &graph, NULL);
252 jack_set_error_function(&error);
253 jack_set_info_function(&info);
254 for (i = 0; i < NPORTS; i++) {
255 port[i] =
256 jack_port_register(client, port_name[i], JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
257 }
258 if (jack_activate(client)) {
259 die("Could not activate %s JACK client.", config.jack_client_name);
260 } else {
261 debug(2, "JACK client %s activated successfully.", config.jack_client_name);
262 }
263 if (config.jack_autoconnect_pattern != NULL) {
264 inform("config.jack_autoconnect_pattern is %s. If you see the program die after this,"
265 "you made a syntax error.",
266 config.jack_autoconnect_pattern);
267 // Sadly, this will throw a segfault if the user provides a syntactically incorrect regex.
268 // I've reported it to the jack-devel mailing list, they're in a better place to fix it.
269 const char **port_list = jack_get_ports(client, config.jack_autoconnect_pattern,
270 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput);
271 if (port_list != NULL) {
272 for (i = 0; i < NPORTS; i++) {
273 char *full_port_name[NPORTS];
274 full_port_name[i] = malloc(sizeof(char) * jack_port_name_size());
275 sprintf(full_port_name[i], "%s:%s", config.jack_client_name, port_name[i]);
276 if (port_list[i] != NULL) {
277 int err;
278 debug(2, "Connecting %s to %s.", full_port_name[i], port_list[i]);
279 err = jack_connect(client, full_port_name[i], port_list[i]);
280 switch (err) {
281 case EEXIST:
282 inform("The requested connection from %s to %s already exists.", full_port_name[i],
283 port_list[i]);
284 break;
285 case 0:
286 // success
287 break;
288 default:
289 warn("JACK error no. %d occurred while trying to connect %s to %s.", err,
290 full_port_name[i], port_list[i]);
291 break;
292 }
293 } else {
294 inform("No matching port found in %s to connect %s to. You may not hear audio.",
295 config.jack_autoconnect_pattern, full_port_name[i]);
296 }
297 free(full_port_name[i]);
298 }
299 while (port_list[i++] != NULL) {
300 inform(
301 "Additional matching port %s found. Check that the connections are what you intended.",
302 port_list[i - 1]);
303 }
304 jack_free(port_list);
305 }
306 }
307 pthread_mutex_unlock(&client_mutex);
308
309 return 0;
310 }
311
312 static void jack_deinit() {
313 pthread_mutex_lock(&client_mutex);
314 if (jack_deactivate(client))
315 warn("Error deactivating jack client");
316 if (jack_client_close(client))
317 warn("Error closing jack client");
318 pthread_mutex_unlock(&client_mutex);
319 jack_ringbuffer_free(jackbuf);
320 #ifdef CONFIG_SOXR
321 if (soxr) {
322 soxr_delete(soxr);
323 soxr = NULL;
324 }
325 #endif
326 }
327
328 static void jack_start(int i_sample_rate, __attribute__((unused)) int i_sample_format) {
329 // Nothing to do, JACK client has already been set up at jack_init().
330 // Also, we have no say over the sample rate or sample format of JACK,
331 // We convert the 16bit samples to float, and die if the sample rate is != 44k1 without soxr.
332 #ifdef CONFIG_SOXR
333 if (config.jack_soxr_resample_quality >= SOXR_QQ) {
334 // we might improve a bit with soxr_clear if the sample_rate doesn't change
335 if (soxr) {
336 soxr_delete(soxr);
337 }
338 soxr_error_t e = NULL;
339 soxr = soxr_create(i_sample_rate, sample_rate, NPORTS, &e, &io_spec, &quality_spec, NULL);
340 if (!soxr) {
341 die("Unable to create soxr resampler for JACK: %s", e);
342 }
343 }
344 #endif
345 }
346
347 static void jack_flush() {
348 debug(2, "Only the consumer can safely flush a lock-free ringbuffer. Asking the"
349 " process callback to do it...");
350 flush_please = 1;
351 }
352
353 static int jack_delay(long *the_delay) {
354 // Semantics change: we now look at the last transfer into the lock-free
355 // ringbuffer, not into the jack buffers directly (because locking those would
356 // violate real-time constraints). On average, that should lead to just a
357 // constant additional latency.
358 // Without the mutex, we could get the time of what is the last transfer of data
359 // to a jack buffer, but then a transfer could occur and we would get the buffer
360 // occupancy after another transfer had occurred, so we could "lose" a full transfer
361 // (e.g. 1024 frames @ 44,100 fps ~ 23.2 milliseconds)
362 pthread_mutex_lock(&buffer_mutex);
363 int64_t time_now = get_absolute_time_in_ns();
364 int64_t delta = time_now - time_of_latest_transfer; // nanoseconds
365 size_t audio_occupancy_now = jack_ringbuffer_read_space(jackbuf) / bytes_per_frame;
366 debug(2, "audio_occupancy_now is %d.", audio_occupancy_now);
367 pthread_mutex_unlock(&buffer_mutex);
368
369 int64_t frames_processed_since_latest_latency_check = (delta * sample_rate) / 1000000000;
370 // debug(1,"delta: %" PRId64 " frames.",frames_processed_since_latest_latency_check);
371 // jack_latency is set by the graph() callback, it's the average of the maximum
372 // latencies of all our output ports. Adjust this constant baseline delay according
373 // to the buffer fill level:
374 *the_delay = jack_latency + audio_occupancy_now - frames_processed_since_latest_latency_check;
375 // debug(1,"reporting a delay of %d frames",*the_delay);
376 return 0;
377 }
378
379 static int play(void *buf, int samples, __attribute__((unused)) int sample_type,
380 __attribute__((unused)) uint32_t timestamp,
381 __attribute__((unused)) uint64_t playtime) {
382 jack_ringbuffer_data_t v[2] = {0};
383 size_t i, j, c;
384 jack_nframes_t thisbuf;
385 // It's ok to lock here since we're not in the realtime callback:
386 pthread_mutex_lock(&buffer_mutex);
387 jack_ringbuffer_get_write_vector(jackbuf, v);
388 short *in = (short *)buf;
389 sample_t *out;
390 for (i = 0; i < 2; ++i) {
391 thisbuf = v[i].len / (jack_sample_size * NPORTS); // #samples per channel
392 out = (sample_t *)v[i].buf;
393 #ifdef CONFIG_SOXR
394 if (soxr) {
395 size_t i_done, o_done;
396 soxr_error_t e;
397 while (samples > 0 && thisbuf > 0) {
398 e = soxr_process(soxr, (soxr_in_t)in, samples, &i_done, (soxr_out_t)out, thisbuf, &o_done);
399 if (e)
400 die("Error during soxr process: %s", e);
401
402 in += i_done * NPORTS; // advance our input buffer
403 samples -= i_done;
404 thisbuf -= o_done;
405 jack_ringbuffer_write_advance(jackbuf, o_done * jack_sample_size * NPORTS);
406 }
407 } else {
408 #endif
409 j = 0;
410 for (j = 0; j < thisbuf && samples > 0; ++j) {
411 for (c = 0; c < NPORTS; ++c)
412 out[j * NPORTS + c] = sample_conv(*in++);
413 --samples;
414 }
415 jack_ringbuffer_write_advance(jackbuf, j * jack_sample_size * NPORTS);
416 #ifdef CONFIG_SOXR
417 }
418 #endif
419 }
420 time_of_latest_transfer = get_absolute_time_in_ns();
421 pthread_mutex_unlock(&buffer_mutex);
422 if (samples) {
423 warn("JACK ringbuffer overrun. Dropped %d samples.", samples);
424 }
425 return 0;
426 }
427
428 audio_output audio_jack = {.name = "jack",
429 .help = NULL,
430 .init = &jack_init,
431 .deinit = &jack_deinit,
432 .prepare = NULL,
433 .start = &jack_start,
434 .stop = NULL,
435 .is_running = NULL,
436 .flush = &jack_flush,
437 .delay = &jack_delay,
438 .stats = NULL,
439 .play = &play,
440 .volume = NULL,
441 .parameters = NULL,
442 .mute = NULL};