]> git.ipfire.org Git - thirdparty/shairport-sync.git/blame - audio_jack.c
Update check_classic_mac_basic.yml
[thirdparty/shairport-sync.git] / audio_jack.c
CommitLineData
8cabb16f
MB
1/*
2 * jack output driver. This file is part of Shairport Sync.
fd880056 3 * Copyright (c) 2019 -- 2022 Mike Brady <4265913+mikebrady@users.noreply.github.com>,
90f24c62 4 * Jörn Nettingsmeier <nettings@luchtbeweging.nl>
8cabb16f
MB
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>
8cabb16f 24#include <limits.h>
8cabb16f 25#include <pthread.h>
c8b0be30 26#include <stdlib.h>
8cabb16f 27#include <string.h>
8cabb16f
MB
28
29#include <jack/jack.h>
f07c16a6 30#include <jack/ringbuffer.h>
8cabb16f 31
7285b1ee
PDG
32#ifdef CONFIG_SOXR
33#include <soxr.h>
34#endif
35
36#define NPORTS 2
37
38typedef jack_default_audio_sample_t sample_t;
39
40#define jack_sample_size sizeof(sample_t)
41
42// Two-channel, 32bit audio:
fd880056 43const int bytes_per_frame = NPORTS * jack_sample_size;
8cabb16f 44
fd880056
MB
45pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
46pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER;
8cabb16f 47
b361ad84
JN
48// This also affects deinterlacing.
49// So make it exactly the number of incoming audio channels!
fd880056
MB
50jack_port_t *port[NPORTS];
51const char *port_name[NPORTS] = {"out_L", "out_R"};
959aa08b 52
fd880056
MB
53jack_client_t *client;
54jack_nframes_t sample_rate;
55jack_nframes_t jack_latency;
8cabb16f 56
fd880056
MB
57jack_ringbuffer_t *jackbuf;
58int flush_please = 0;
f07c16a6 59
fd880056
MB
60jack_latency_range_t latest_latency_range[NPORTS];
61int64_t time_of_latest_transfer;
8cabb16f 62
7285b1ee 63#ifdef CONFIG_SOXR
32646f22
PDG
64typedef struct soxr_quality {
65 int quality;
7285b1ee 66 const char *name;
32646f22 67} soxr_quality_t;
7285b1ee 68
fd880056
MB
69soxr_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}};
7285b1ee 72
32646f22
PDG
73static int parse_soxr_quality_name(const char *name) {
74 for (soxr_quality_t *s = soxr_quality_table; s->name != NULL; ++s) {
7285b1ee 75 if (!strcmp(s->name, name)) {
32646f22 76 return s->quality;
7285b1ee
PDG
77 }
78 }
79 return -1;
80}
81
fd880056
MB
82soxr_t soxr = NULL;
83soxr_quality_spec_t quality_spec;
84soxr_io_spec_t io_spec;
7285b1ee
PDG
85#endif
86
87static inline sample_t sample_conv(short sample) {
b361ad84
JN
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.
e99837f8
JN
94 return ((sample < 0) ? (-1.0 * sample / SHRT_MIN) : (1.0 * sample / SHRT_MAX));
95}
96
54d761ff 97static void deinterleave(const char *interleaved_input_buffer, sample_t *jack_output_buffer[],
7285b1ee 98 jack_nframes_t offset, jack_nframes_t nframes) {
4f39e0d8 99 jack_nframes_t f;
b361ad84 100 // We're dealing with 16bit audio here:
7285b1ee 101 sample_t *ifp = (sample_t *)interleaved_input_buffer;
b361ad84
JN
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++) {
7285b1ee 106 jack_output_buffer[i][f] = *ifp++;
4f39e0d8 107 }
e99837f8
JN
108 }
109}
110
b361ad84
JN
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.
f1ba07ff 116static int process(jack_nframes_t nframes, __attribute__((unused)) void *arg) {
7285b1ee 117 sample_t *buffer[NPORTS];
b361ad84 118 // Expect an array of two elements because of possible ringbuffer wrap-around:
c8b0be30 119 jack_ringbuffer_data_t v[2] = {0};
e99837f8
JN
120 jack_nframes_t i, thisbuf;
121 int frames_written = 0;
122 int frames_required = 0;
123
b361ad84 124 for (i = 0; i < NPORTS; i++) {
7285b1ee 125 buffer[i] = (sample_t *)jack_port_get_buffer(port[i], nframes);
b361ad84 126 }
83c8857c 127 if (flush_please) {
b361ad84 128 // We just move the read pointer ahead without doing anything with the data.
83c8857c 129 jack_ringbuffer_read_advance(jackbuf, jack_ringbuffer_read_space(jackbuf));
3e6ef019 130 flush_please = 0;
b361ad84 131 // Since we don't change nframes, the whole buffer will be zeroed later.
83c8857c 132 } else {
b361ad84
JN
133 jack_ringbuffer_get_read_vector(jackbuf, v);
134 for (i = 0; i < 2; i++) {
83c8857c
JN
135 thisbuf = v[i].len / bytes_per_frame;
136 if (thisbuf > nframes) {
137 frames_required = nframes;
c8b0be30 138 } else {
83c8857c
JN
139 frames_required = thisbuf;
140 }
7285b1ee 141 deinterleave(v[i].buf, buffer, frames_written, frames_required);
23d4ec2b
JN
142 frames_written += frames_required;
143 nframes -= frames_required;
e99837f8 144 }
83c8857c 145 jack_ringbuffer_read_advance(jackbuf, frames_written * bytes_per_frame);
e99837f8 146 }
b361ad84
JN
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.
e99837f8 150 while (nframes > 0) {
b361ad84 151 for (i = 0; i < NPORTS; i++) {
4f39e0d8
JN
152 buffer[i][frames_written] = 0.0;
153 }
e99837f8
JN
154 frames_written++;
155 nframes--;
156 }
b361ad84 157 return 0; // Tell JACK that all is well.
8cabb16f
MB
158}
159
b361ad84
JN
160// This is the JACK graph reorder callback. Now we know some JACK connections
161// have changed, so we recompute the latency.
c8b0be30 162static int graph(__attribute__((unused)) void *arg) {
4f39e0d8 163 int latency = 0;
31bbd743 164 debug(2, "JACK graph reorder callback called.");
c8b0be30 165 for (int i = 0; i < NPORTS; i++) {
4f39e0d8 166 jack_port_get_latency_range(port[i], JackPlaybackLatency, &latest_latency_range[i]);
c8b0be30
MB
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);
4f39e0d8
JN
169 latency += latest_latency_range[i].max;
170 }
171 latency /= NPORTS;
172 jack_latency = latency;
31bbd743 173 debug(1, "Average maximum JACK latency across all ports: %d", jack_latency);
bda8b99c
JN
174 return 0;
175}
e99837f8 176
b361ad84 177// This the function JACK will call in case of an error in the library.
c8b0be30 178static void error(const char *desc) { warn("JACK error: \"%s\"", desc); }
8cabb16f 179
b361ad84 180// This is the function JACK will call in case of a non-critical event in the library.
c8b0be30 181static void info(const char *desc) { inform("JACK information: \"%s\"", desc); }
8cabb16f 182
fd880056 183static int jack_init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
97370d8b 184 int i;
32646f22 185 int bufsz = -1;
8cabb16f 186 config.audio_backend_latency_offset = 0;
76138873 187 config.audio_backend_buffer_desired_length = 0.500;
b361ad84
JN
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;
8cabb16f 191
b361ad84 192 // Do the "general" audio options. Note, these options are in the "general" stanza!
8cabb16f 193 parse_general_audio_options();
7285b1ee 194#ifdef CONFIG_SOXR
32646f22 195 config.jack_soxr_resample_quality = -1; // don't resample by default
7285b1ee 196#endif
959aa08b 197
b361ad84 198 // Now the options specific to the backend, from the "jack" stanza:
959aa08b
MB
199 if (config.cfg != NULL) {
200 const char *str;
959aa08b
MB
201 if (config_lookup_string(config.cfg, "jack.client_name", &str)) {
202 config.jack_client_name = (char *)str;
203 }
28ea6972
JN
204 if (config_lookup_string(config.cfg, "jack.autoconnect_pattern", &str)) {
205 config.jack_autoconnect_pattern = (char *)str;
206 }
7285b1ee 207#ifdef CONFIG_SOXR
32646f22 208 if (config_lookup_string(config.cfg, "jack.soxr_resample_quality", &str)) {
7285b1ee 209 debug(1, "SOXR quality %s", str);
32646f22 210 config.jack_soxr_resample_quality = parse_soxr_quality_name(str);
7285b1ee
PDG
211 }
212#endif
32646f22
PDG
213 if (config_lookup_int(config.cfg, "jack.bufsz", &bufsz) && bufsz <= 0)
214 die("jack: bufsz must be > 0");
959aa08b 215 }
959aa08b 216 if (config.jack_client_name == NULL)
95246dcd 217 config.jack_client_name = strdup("shairport-sync");
959aa08b 218
32646f22
PDG
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);
06504516 224 if (jackbuf == NULL)
32646f22 225 die("Can't allocate %d bytes for the JACK ringbuffer.", bufsz);
b361ad84
JN
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.
0009fcdd 232 pthread_mutex_lock(&client_mutex);
28267569
JN
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);
7285b1ee 239#ifdef CONFIG_SOXR
32646f22
PDG
240 if (config.jack_soxr_resample_quality >= SOXR_QQ) {
241 quality_spec = soxr_quality_spec(config.jack_soxr_resample_quality, 0);
7285b1ee
PDG
242 io_spec = soxr_io_spec(SOXR_INT16_I, SOXR_FLOAT32_I);
243 } else
244#endif
54d761ff 245 if (sample_rate != 44100) {
b361ad84 246 die("The JACK server is running at the wrong sample rate (%d) for Shairport Sync."
c8b0be30
MB
247 " Must be 44100 Hz.",
248 sample_rate);
28267569 249 }
c00199d6
JN
250 jack_set_process_callback(client, &process, NULL);
251 jack_set_graph_order_callback(client, &graph, NULL);
bda8b99c
JN
252 jack_set_error_function(&error);
253 jack_set_info_function(&info);
c8b0be30
MB
254 for (i = 0; i < NPORTS; i++) {
255 port[i] =
256 jack_port_register(client, port_name[i], JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
4f39e0d8 257 }
28267569
JN
258 if (jack_activate(client)) {
259 die("Could not activate %s JACK client.", config.jack_client_name);
260 } else {
b9cf91b2 261 debug(2, "JACK client %s activated successfully.", config.jack_client_name);
1906bfd7 262 }
28ea6972 263 if (config.jack_autoconnect_pattern != NULL) {
c8b0be30
MB
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);
b361ad84
JN
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.
c8b0be30 269 const char **port_list = jack_get_ports(client, config.jack_autoconnect_pattern,
97370d8b 270 JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput);
e59da783 271 if (port_list != NULL) {
c8b0be30
MB
272 for (i = 0; i < NPORTS; i++) {
273 char *full_port_name[NPORTS];
e59da783
JN
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:
c8b0be30
MB
282 inform("The requested connection from %s to %s already exists.", full_port_name[i],
283 port_list[i]);
e59da783
JN
284 break;
285 case 0:
286 // success
287 break;
288 default:
b9cf91b2 289 warn("JACK error no. %d occurred while trying to connect %s to %s.", err,
c8b0be30 290 full_port_name[i], port_list[i]);
e59da783
JN
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]);
97370d8b 296 }
e59da783 297 free(full_port_name[i]);
97370d8b 298 }
e59da783 299 while (port_list[i++] != NULL) {
c8b0be30 300 inform(
ef01dc54 301 "Additional matching port %s found. Check that the connections are what you intended.",
b7d9c22d 302 port_list[i - 1]);
e59da783
JN
303 }
304 jack_free(port_list);
28ea6972
JN
305 }
306 }
0009fcdd 307 pthread_mutex_unlock(&client_mutex);
959aa08b 308
8cabb16f
MB
309 return 0;
310}
311
fd880056 312static void jack_deinit() {
0009fcdd 313 pthread_mutex_lock(&client_mutex);
28267569 314 if (jack_deactivate(client))
31bbd743 315 warn("Error deactivating jack client");
28267569 316 if (jack_client_close(client))
31bbd743 317 warn("Error closing jack client");
0009fcdd 318 pthread_mutex_unlock(&client_mutex);
edd699df 319 jack_ringbuffer_free(jackbuf);
7285b1ee
PDG
320#ifdef CONFIG_SOXR
321 if (soxr) {
322 soxr_delete(soxr);
323 soxr = NULL;
324 }
325#endif
89a8add7
JN
326}
327
fd880056 328static void jack_start(int i_sample_rate, __attribute__((unused)) int i_sample_format) {
b361ad84
JN
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,
7285b1ee
PDG
331 // We convert the 16bit samples to float, and die if the sample rate is != 44k1 without soxr.
332#ifdef CONFIG_SOXR
32646f22 333 if (config.jack_soxr_resample_quality >= SOXR_QQ) {
7285b1ee
PDG
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;
54d761ff 339 soxr = soxr_create(i_sample_rate, sample_rate, NPORTS, &e, &io_spec, &quality_spec, NULL);
7285b1ee
PDG
340 if (!soxr) {
341 die("Unable to create soxr resampler for JACK: %s", e);
342 }
343 }
344#endif
8cabb16f
MB
345}
346
fd880056 347static void jack_flush() {
b361ad84
JN
348 debug(2, "Only the consumer can safely flush a lock-free ringbuffer. Asking the"
349 " process callback to do it...");
89a8add7
JN
350 flush_please = 1;
351}
352
fd880056 353static int jack_delay(long *the_delay) {
b361ad84
JN
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)
39545cdf 362 pthread_mutex_lock(&buffer_mutex);
67e9b1b6
MB
363 int64_t time_now = get_absolute_time_in_ns();
364 int64_t delta = time_now - time_of_latest_transfer; // nanoseconds
c8b0be30
MB
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);
959aa08b
MB
367 pthread_mutex_unlock(&buffer_mutex);
368
67e9b1b6 369 int64_t frames_processed_since_latest_latency_check = (delta * sample_rate) / 1000000000;
8cabb16f 370 // debug(1,"delta: %" PRId64 " frames.",frames_processed_since_latest_latency_check);
b361ad84
JN
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:
4751a4e1 374 *the_delay = jack_latency + audio_occupancy_now - frames_processed_since_latest_latency_check;
d7c7365b 375 // debug(1,"reporting a delay of %d frames",*the_delay);
8cabb16f
MB
376 return 0;
377}
378
fd880056
MB
379static int play(void *buf, int samples, __attribute__((unused)) int sample_type,
380 __attribute__((unused)) uint32_t timestamp,
381 __attribute__((unused)) uint64_t playtime) {
7285b1ee
PDG
382 jack_ringbuffer_data_t v[2] = {0};
383 size_t i, j, c;
384 jack_nframes_t thisbuf;
b361ad84
JN
385 // It's ok to lock here since we're not in the realtime callback:
386 pthread_mutex_lock(&buffer_mutex);
7285b1ee
PDG
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) {
54d761ff 398 e = soxr_process(soxr, (soxr_in_t)in, samples, &i_done, (soxr_out_t)out, thisbuf, &o_done);
7285b1ee
PDG
399 if (e)
400 die("Error during soxr process: %s", e);
401
311909a2 402 in += i_done * NPORTS; // advance our input buffer
7285b1ee
PDG
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
54d761ff
MB
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);
7285b1ee
PDG
416#ifdef CONFIG_SOXR
417 }
418#endif
419 }
67e9b1b6 420 time_of_latest_transfer = get_absolute_time_in_ns();
89a8add7 421 pthread_mutex_unlock(&buffer_mutex);
7285b1ee
PDG
422 if (samples) {
423 warn("JACK ringbuffer overrun. Dropped %d samples.", samples);
89a8add7
JN
424 }
425 return 0;
aa5698fc 426}
fd880056
MB
427
428audio_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};