]> git.ipfire.org Git - thirdparty/shairport-sync.git/blame - audio_jack.c
Add lock-free ringbuffer. Preparation to make jack_stream_write_cb realtime safe.
[thirdparty/shairport-sync.git] / audio_jack.c
CommitLineData
8cabb16f
MB
1/*
2 * jack output driver. This file is part of Shairport Sync.
3 * Copyright (c) 2018 Mike Brady <mikebrady@iercom.net>
4 *
5 * All rights reserved.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "audio.h"
21#include "common.h"
22#include <errno.h>
23#include <getopt.h>
24#include <limits.h>
25#include <math.h>
26#include <pthread.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
8cabb16f
MB
30#include <unistd.h>
31
32#include <jack/jack.h>
33#include <jack/transport.h>
f07c16a6 34#include <jack/ringbuffer.h>
8cabb16f
MB
35
36enum ift_type {
37 IFT_frame_left_sample = 0,
38 IFT_frame_right_sample,
39} ift_type;
40
41// Four seconds buffer -- should be plenty
42#define buffer_size 44100 * 4 * 2 * 2
43
44static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
6c6dda9c 45static pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER;
8cabb16f
MB
46
47char *audio_lmb, *audio_umb, *audio_toq, *audio_eoq;
48size_t audio_occupancy; // this is in frames, not bytes. A frame is a left and
49 // right sample, each 16 bits, hence 4 bytes
6c6dda9c
MB
50pthread_t *open_client_if_necessary_thread = NULL;
51
c1d5815c 52int jack_init(int, char **);
959aa08b 53void jack_deinit(void);
8cabb16f
MB
54void jack_start(int, int);
55int play(void *, int);
56void jack_stop(void);
57int jack_is_running(void);
8cabb16f
MB
58int jack_delay(long *);
59void jack_flush(void);
60
61audio_output audio_jack = {.name = "jack",
62 .help = NULL,
c1d5815c 63 .init = &jack_init,
959aa08b 64 .deinit = &jack_deinit,
8cabb16f
MB
65 .start = &jack_start,
66 .stop = &jack_stop,
67 .is_running = &jack_is_running,
68 .flush = &jack_flush,
69 .delay = &jack_delay,
70 .play = &play,
71 .volume = NULL,
72 .parameters = NULL,
73 .mute = NULL};
74
8cabb16f
MB
75jack_port_t *left_port;
76jack_port_t *right_port;
959aa08b 77
8cabb16f 78long offset = 0;
8cabb16f
MB
79
80int client_is_open;
81jack_client_t *client;
82jack_nframes_t sample_rate;
c1d5815c 83jack_nframes_t jack_latency;
8cabb16f 84
f07c16a6
JN
85static jack_ringbuffer_t *jackbuf;
86
39545cdf 87jack_latency_range_t latest_left_latency_range, latest_right_latency_range;
959aa08b 88int64_t time_of_latest_transfer;
8cabb16f
MB
89
90int play(void *buf, int samples) {
91 // debug(1,"jack_play of %d samples.",samples);
92 // copy the samples into the queue
93 size_t bytes_to_transfer = samples * 2 * 2;
94 size_t space_to_end_of_buffer = audio_umb - audio_eoq;
95 if (space_to_end_of_buffer >= bytes_to_transfer) {
96 memcpy(audio_eoq, buf, bytes_to_transfer);
97 pthread_mutex_lock(&buffer_mutex);
98 audio_occupancy += samples;
99 audio_eoq += bytes_to_transfer;
100 pthread_mutex_unlock(&buffer_mutex);
101 } else {
102 memcpy(audio_eoq, buf, space_to_end_of_buffer);
103 buf += space_to_end_of_buffer;
104 memcpy(audio_lmb, buf, bytes_to_transfer - space_to_end_of_buffer);
105 pthread_mutex_lock(&buffer_mutex);
106 audio_occupancy += samples;
107 audio_eoq = audio_lmb + bytes_to_transfer - space_to_end_of_buffer;
108 pthread_mutex_unlock(&buffer_mutex);
109 }
8cabb16f
MB
110 return 0;
111}
112
113void deinterleave_and_convert_stream(const char *interleaved_frames,
959aa08b 114 const jack_default_audio_sample_t *jack_frame_buffer,
8cabb16f
MB
115 jack_nframes_t number_of_frames, enum ift_type side) {
116 jack_nframes_t i;
117 short *ifp = (short *)interleaved_frames;
959aa08b 118 jack_default_audio_sample_t *fp = (jack_default_audio_sample_t *)jack_frame_buffer;
8cabb16f
MB
119 if (side == IFT_frame_right_sample)
120 ifp++;
121 for (i = 0; i < number_of_frames; i++) {
122 short sample = *ifp;
959aa08b 123 jack_default_audio_sample_t converted_value;
8cabb16f
MB
124 if (sample >= 0)
125 converted_value = (1.0 * sample) / SHRT_MAX;
126 else
127 converted_value = -(1.0 * sample) / SHRT_MIN;
128 *fp = converted_value;
129 ifp++;
130 ifp++;
131 fp++;
132 }
133}
134
135int jack_stream_write_cb(jack_nframes_t nframes, __attribute__((unused)) void *arg) {
136
959aa08b
MB
137 jack_default_audio_sample_t *left_buffer =
138 (jack_default_audio_sample_t *)jack_port_get_buffer(left_port, nframes);
139 jack_default_audio_sample_t *right_buffer =
140 (jack_default_audio_sample_t *)jack_port_get_buffer(right_port, nframes);
8cabb16f
MB
141
142 size_t frames_we_can_transfer = nframes;
143 // lock
144 pthread_mutex_lock(&buffer_mutex);
145 if (audio_occupancy < frames_we_can_transfer) {
146 frames_we_can_transfer = audio_occupancy;
175d91a4
MB
147 // This means we effectively have underflow from the Shairport Sync source.
148 // In fact, it may be that there is nothing at all coming from the source,
149 // but the Shairport Sync client is open and active, so it must continue to output something.
8cabb16f
MB
150 }
151
8cabb16f
MB
152 if (frames_we_can_transfer * 2 * 2 <= (size_t)(audio_umb - audio_toq)) {
153 // the bytes are all in a row in the audio buffer
154 deinterleave_and_convert_stream(audio_toq, &left_buffer[0], frames_we_can_transfer,
155 IFT_frame_left_sample);
156 deinterleave_and_convert_stream(audio_toq, &right_buffer[0], frames_we_can_transfer,
157 IFT_frame_right_sample);
158 audio_toq += frames_we_can_transfer * 2 * 2;
159 } else {
160 // the bytes are in two places in the audio buffer
161 size_t first_portion_to_write = (audio_umb - audio_toq) / (2 * 2);
162 if (first_portion_to_write != 0) {
163 deinterleave_and_convert_stream(audio_toq, &left_buffer[0], first_portion_to_write,
164 IFT_frame_left_sample);
165 deinterleave_and_convert_stream(audio_toq, &right_buffer[0], first_portion_to_write,
166 IFT_frame_right_sample);
167 }
168 deinterleave_and_convert_stream(audio_lmb, &left_buffer[first_portion_to_write],
169 frames_we_can_transfer - first_portion_to_write,
170 IFT_frame_left_sample);
171 deinterleave_and_convert_stream(audio_lmb, &right_buffer[first_portion_to_write],
172 frames_we_can_transfer - first_portion_to_write,
173 IFT_frame_right_sample);
174 audio_toq = audio_lmb + (frames_we_can_transfer - first_portion_to_write) * 2 * 2;
175 }
176 // debug(1,"transferring %u frames",frames_we_can_transfer);
177 audio_occupancy -= frames_we_can_transfer;
959aa08b
MB
178 jack_port_get_latency_range(left_port, JackPlaybackLatency, &latest_left_latency_range);
179 jack_port_get_latency_range(right_port, JackPlaybackLatency, &latest_right_latency_range);
180 time_of_latest_transfer = get_absolute_time_in_fp();
8cabb16f
MB
181 pthread_mutex_unlock(&buffer_mutex);
182 // unlock
959aa08b
MB
183
184 // now, if there are any more frames to put into the buffer, fill them with
8cabb16f
MB
185 // silence
186 jack_nframes_t i;
8cabb16f
MB
187 for (i = frames_we_can_transfer; i < nframes; i++) {
188 left_buffer[i] = 0.0;
189 right_buffer[i] = 0.0;
190 }
8cabb16f
MB
191 return 0;
192}
193
959aa08b 194void default_jack_error_callback(const char *desc) { debug(2, "jackd error: \"%s\"", desc); }
8cabb16f 195
959aa08b 196void default_jack_info_callback(const char *desc) { inform("jackd information: \"%s\"", desc); }
8cabb16f 197
c1d5815c
MB
198void default_jack_set_latency_callback(jack_latency_callback_mode_t mode,
199 __attribute__((unused)) void *arg) {
200 if (mode == JackPlaybackLatency) {
201 jack_latency_range_t left_latency_range, right_latency_range;
202 jack_port_get_latency_range(left_port, JackPlaybackLatency, &left_latency_range);
203 jack_port_get_latency_range(right_port, JackPlaybackLatency, &right_latency_range);
204
205 jack_nframes_t b_latency = (left_latency_range.min + left_latency_range.max) / 2;
206 if (b_latency == 0)
207 b_latency = (right_latency_range.min + right_latency_range.max) / 2;
208 // jack_latency = b_latency; // actually, we are not interested in the latency of the jack
209 // devices connected...
210 jack_latency = 0;
211 debug(1, "playback latency callback: %" PRIu32 ".", jack_latency);
212 }
213}
214
8cabb16f 215int jack_is_running() {
76138873 216 int reply = -1; // meaning jack is not running
8cabb16f
MB
217 // if the client is open and initialised, see if the status is "rolling"
218 if (client_is_open) {
76138873
MB
219
220 // check if the ports have a zero latency -- if they both have, then it's disconnected.
221
222 jack_latency_range_t left_latency_range, right_latency_range;
223 jack_port_get_latency_range(left_port, JackPlaybackLatency, &left_latency_range);
224 jack_port_get_latency_range(right_port, JackPlaybackLatency, &right_latency_range);
225
c1d5815c
MB
226 // if ((left_latency_range.min == 0) && (left_latency_range.max == 0) &&
227 // (right_latency_range.min == 0) && (right_latency_range.max == 0)) {
228 // reply = -2; // meaning Shairport Sync is not connected
229 // } else {
230 reply = 0; // meaning jack is open and Shairport Sync is connected to it
231 // }
8cabb16f
MB
232 }
233 return reply;
234}
235
959aa08b 236int jack_client_open_if_needed(void) {
6c6dda9c 237 pthread_mutex_lock(&client_mutex);
959aa08b
MB
238 if (client_is_open == 0) {
239 jack_status_t status;
240 client = jack_client_open(config.jack_client_name, JackNoStartServer, &status);
241 if (client) {
242 jack_set_process_callback(client, jack_stream_write_cb, 0);
243 left_port = jack_port_register(client, config.jack_left_channel_name, JACK_DEFAULT_AUDIO_TYPE,
244 JackPortIsOutput, 0);
245 right_port = jack_port_register(client, config.jack_right_channel_name,
246 JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
247 sample_rate = jack_get_sample_rate(client);
248 // debug(1, "jackaudio sample rate = %" PRId32 ".", sample_rate);
249 if (sample_rate == 44100) {
c1d5815c
MB
250 if (jack_set_latency_callback(client, default_jack_set_latency_callback, NULL) == 0) {
251 if (jack_activate(client)) {
252 debug(1, "jackaudio cannot activate client");
253 } else {
254 debug(2, "jackaudio client opened.");
255 client_is_open = 1;
256 }
959aa08b 257 } else {
c1d5815c 258 debug(1, "jackaudio cannot set latency callback");
959aa08b
MB
259 }
260 } else {
261 inform(
262 "jackaudio is running at the wrong speed (%d) for Shairport Sync, which must be 44100",
263 sample_rate);
264 }
265 }
266 }
6c6dda9c 267 pthread_mutex_unlock(&client_mutex);
959aa08b
MB
268 return client_is_open;
269}
270
6c6dda9c
MB
271void jack_close(void) {
272 pthread_mutex_lock(&client_mutex);
959aa08b
MB
273 if (client_is_open) {
274 if (jack_deactivate(client))
275 debug(1, "Error deactivating jack client");
276 if (jack_client_close(client))
277 debug(1, "Error closing jack client");
6c6dda9c
MB
278 client_is_open = 0;
279 }
280 pthread_mutex_unlock(&client_mutex);
281}
282
283void jack_deinit() {
284 jack_close();
285 if (open_client_if_necessary_thread) {
286 pthread_cancel(*open_client_if_necessary_thread);
287 free((char *)open_client_if_necessary_thread);
f07c16a6
JN
288
289 jack_ringbuffer_free(jackbuf);
290
959aa08b
MB
291 }
292}
293
6c6dda9c
MB
294void *open_client_if_necessary_thread_function(void *arg) {
295 int *interval = (int *)arg;
296 while (*interval != 0) {
297 if (client_is_open == 0) {
298 debug(1, "Try to open the jack client");
299 jack_client_open_if_needed();
300 }
301 sleep(*interval);
39545cdf 302 }
6c6dda9c
MB
303 pthread_exit(NULL);
304}
305
c1d5815c 306int jack_init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) {
8cabb16f 307 config.audio_backend_latency_offset = 0;
76138873 308 config.audio_backend_buffer_desired_length = 0.500;
80f15e1f
MB
309 config.audio_backend_buffer_interpolation_threshold_in_seconds =
310 0.25; // below this, soxr interpolation will not occur -- it'll be basic interpolation
311 // instead.
6c6dda9c 312 config.jack_auto_client_open_interval = 1; // check every second
8cabb16f
MB
313
314 // get settings from settings file first, allow them to be overridden by
315 // command line options
316
317 // do the "general" audio options. Note, these options are in the "general" stanza!
318 parse_general_audio_options();
959aa08b 319
8cabb16f
MB
320 // other options would be picked up here...
321
959aa08b
MB
322 // now the specific options
323 if (config.cfg != NULL) {
324 const char *str;
6c6dda9c 325 int value;
959aa08b
MB
326 /* Get the Client Name. */
327 if (config_lookup_string(config.cfg, "jack.client_name", &str)) {
328 config.jack_client_name = (char *)str;
329 }
330 /* Get the Left Channel Name. */
331 if (config_lookup_string(config.cfg, "jack.left_channel_name", &str)) {
332 config.jack_left_channel_name = (char *)str;
333 }
334 /* Get the Right Channel Name. */
335 if (config_lookup_string(config.cfg, "jack.right_channel_name", &str)) {
336 config.jack_right_channel_name = (char *)str;
337 }
39545cdf
MB
338
339 /* See if we should attempt to connect to the jack server automatically, and, if so, how often
340 * we should try. */
6c6dda9c
MB
341 if (config_lookup_int(config.cfg, "jack.auto_client_open_interval", &value)) {
342 if ((value < 0) || (value > 300))
2e442853
MB
343 debug(1,
344 "Invalid jack auto_client_open_interval \"%sd\". It should be between 0 and 300, "
345 "default is %d.",
76138873 346 value, config.jack_auto_client_open_interval);
6c6dda9c
MB
347 else
348 config.jack_auto_client_open_interval = value;
349 }
350
351 /* See if we should close the client at then end of a play session. */
39545cdf
MB
352 config_set_lookup_bool(config.cfg, "jack.auto_client_disconnect",
353 &config.jack_auto_client_disconnect);
959aa08b
MB
354 }
355
356 if (config.jack_client_name == NULL)
357 config.jack_client_name = strdup("Shairport Sync");
358 if (config.jack_left_channel_name == NULL)
359 config.jack_left_channel_name = strdup("left");
360 if (config.jack_right_channel_name == NULL)
361 config.jack_right_channel_name = strdup("right");
362
f07c16a6
JN
363 jackbuf = jack_ringbuffer_create(buffer_size); // make the jack ringbuffer the same size as audio_lmb
364 jack_ringbuffer_mlock(jackbuf); // lock buffer into memory so that it never gets paged out
365
959aa08b 366 jack_set_error_function(default_jack_error_callback);
8cabb16f 367 jack_set_info_function(default_jack_info_callback);
8cabb16f
MB
368
369 // allocate space for the audio buffer
370 audio_lmb = malloc(buffer_size);
371 if (audio_lmb == NULL)
372 die("Can't allocate %d bytes for jackaudio buffer.", buffer_size);
373 audio_toq = audio_eoq = audio_lmb;
374 audio_umb = audio_lmb + buffer_size;
375 audio_occupancy = 0; // frames
959aa08b
MB
376
377 client_is_open = 0;
39545cdf 378
175d91a4 379 // now, if selected, start a thread to automatically open a client when there is a server.
6c6dda9c
MB
380 if (config.jack_auto_client_open_interval != 0) {
381 open_client_if_necessary_thread = malloc(sizeof(pthread_t));
175d91a4 382 if (open_client_if_necessary_thread == NULL) {
76138873 383 debug(1, "Couldn't allocate space for jack server scanner thread");
175d91a4
MB
384 jack_client_open_if_needed();
385 } else {
76138873
MB
386 pthread_create(open_client_if_necessary_thread, NULL,
387 open_client_if_necessary_thread_function,
388 &config.jack_auto_client_open_interval);
175d91a4 389 }
6c6dda9c
MB
390 } else {
391 jack_client_open_if_needed();
392 }
959aa08b 393
8cabb16f
MB
394 return 0;
395}
396
959aa08b
MB
397void jack_start(__attribute__((unused)) int i_sample_rate,
398 __attribute__((unused)) int i_sample_format) {
6c6dda9c 399 // debug(1, "jack start");
959aa08b
MB
400 // see if the client is running. If not, try to open and initialise it
401
402 if (jack_client_open_if_needed() == 0)
403 debug(1, "cannot open a jack client for a play session");
8cabb16f
MB
404}
405
406int jack_delay(long *the_delay) {
39545cdf 407
959aa08b 408 // without the mutex, we could get the time of what is the last transfer of data to a jack buffer,
39545cdf
MB
409 // but then a transfer could occur and we would get the buffer occupancy after another transfer
410 // had occurred
411 // so we could "lose" a full transfer (e.g. 1024 frames @ 44,100 fps ~ 23.2 milliseconds)
412 pthread_mutex_lock(&buffer_mutex);
959aa08b 413 int64_t time_now = get_absolute_time_in_fp();
39545cdf
MB
414 int64_t delta = time_now - time_of_latest_transfer; // this is the time back to the last time data
415 // was transferred into a jack buffer
416 size_t audio_occupancy_now = audio_occupancy; // this is the buffer occupancy before any
76138873
MB
417 // subsequent transfer because transfer is blocked
418 // by the mutex
959aa08b
MB
419 pthread_mutex_unlock(&buffer_mutex);
420
8cabb16f 421 int64_t frames_processed_since_latest_latency_check = (delta * 44100) >> 32;
8cabb16f 422 // debug(1,"delta: %" PRId64 " frames.",frames_processed_since_latest_latency_check);
39545cdf 423 jack_nframes_t base_latency = (latest_left_latency_range.min + latest_left_latency_range.max) / 2;
959aa08b 424 if (base_latency == 0)
39545cdf
MB
425 base_latency = (latest_right_latency_range.min + latest_right_latency_range.max) / 2;
426 *the_delay = base_latency + audio_occupancy_now - frames_processed_since_latest_latency_check;
8cabb16f
MB
427 // debug(1,"reporting a delay of %d frames",*the_delay);
428
429 return 0;
430}
431
432void jack_flush() {
959aa08b 433 // debug(1,"jack flush");
8cabb16f
MB
434 pthread_mutex_lock(&buffer_mutex);
435 audio_toq = audio_eoq = audio_lmb;
436 audio_umb = audio_lmb + buffer_size;
437 audio_occupancy = 0; // frames
438 pthread_mutex_unlock(&buffer_mutex);
8cabb16f
MB
439}
440
6c6dda9c
MB
441void jack_stop(void) {
442 // debug(1, "jack stop");
443 if (config.jack_auto_client_disconnect)
444 jack_close();
aa5698fc 445}