/* * jack output driver. This file is part of Shairport Sync. * Copyright (c) 2019 Mike Brady , * Jörn Nettingsmeier * * All rights reserved. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "audio.h" #include "common.h" #include #include #include #include #include #include #include // Two-channel, 16bit audio: static const int bytes_per_frame = 4; // Four seconds buffer -- should be plenty #define buffer_size (44100 * 4 * bytes_per_frame) static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER; int jack_init(int, char **); void jack_deinit(void); void jack_start(int, int); int play(void *, int); void jack_stop(void); int jack_delay(long *); void jack_flush(void); audio_output audio_jack = {.name = "jack", .help = NULL, .init = &jack_init, .deinit = &jack_deinit, .start = &jack_start, .stop = NULL, .is_running = NULL, .flush = &jack_flush, .delay = &jack_delay, .play = &play, .volume = NULL, .parameters = NULL, .mute = NULL}; // This also affects deinterlacing. // So make it exactly the number of incoming audio channels! #define NPORTS 2 static jack_port_t *port[NPORTS]; static const char* port_name[NPORTS] = { "out_L", "out_R" }; static jack_client_t *client; static jack_nframes_t sample_rate; static jack_nframes_t jack_latency; static jack_ringbuffer_t *jackbuf; static int flush_please = 0; static jack_latency_range_t latest_latency_range[NPORTS]; static int64_t time_of_latest_transfer; static inline jack_default_audio_sample_t sample_conv(short sample) { // It sounds correct, but I don't understand it. // Zero int needs to be zero float. Check. // Plus 32767 int is 1.0. Check. // Minus 32767 int is -0.99997. And here my brain shuts down. // In my head, it should be 1.0, and we should tolerate an overflow // at minus 32768. But I'm sure there's a textbook explanation somewhere. return ((sample < 0) ? (-1.0 * sample / SHRT_MIN) : (1.0 * sample / SHRT_MAX)); } static void deinterleave_and_convert(const char *interleaved_input_buffer, jack_default_audio_sample_t* jack_output_buffer[], jack_nframes_t offset, jack_nframes_t nframes) { jack_nframes_t f; // We're dealing with 16bit audio here: short *ifp = (short *)interleaved_input_buffer; // Zero-copy, we're working directly on the target and destination buffers, // so deal with an offset for the second part of the input ringbuffer for (f = offset; f < (nframes + offset); f++) { for (int i = 0; i < NPORTS; i++) { jack_output_buffer[i][f] = sample_conv(*ifp++); } } } // This is the JACK process callback. We don't decide when it runs. // It must be hard-realtime safe (i.e. fully deterministic, with constant CPU // usage. No calls to anything that could ever block: no syscalls, no screen // output, no file access, no mutexes... // The JACK ringbuffer we use to get the data in here is explicitly lock-free. static int process(jack_nframes_t nframes, __attribute__((unused)) void *arg) { jack_default_audio_sample_t *buffer[NPORTS]; // Expect an array of two elements because of possible ringbuffer wrap-around: jack_ringbuffer_data_t v[2] = { 0 }; jack_nframes_t i, thisbuf; int frames_written = 0; int frames_required = 0; for (i = 0; i < NPORTS; i++) { buffer[i] = (jack_default_audio_sample_t *)jack_port_get_buffer(port[i], nframes); } if (flush_please) { // We just move the read pointer ahead without doing anything with the data. jack_ringbuffer_read_advance(jackbuf, jack_ringbuffer_read_space(jackbuf)); flush_please = 0; // Since we don't change nframes, the whole buffer will be zeroed later. } else { jack_ringbuffer_get_read_vector(jackbuf, v); for (i = 0; i < 2; i++) { thisbuf = v[i].len / bytes_per_frame; if (thisbuf > nframes) { frames_required = nframes; } else { frames_required = thisbuf; } deinterleave_and_convert(v[i].buf, buffer, frames_written, frames_required); frames_written += frames_required; nframes -= frames_required; } jack_ringbuffer_read_advance(jackbuf, frames_written * bytes_per_frame); } // If there are any more frames to put into the buffer, fill them with // silence. This is a critical underflow situation. Let's at least keep the JACK // graph humming along while preventing the motorboat sound of a repeating buffer. while (nframes > 0) { for (i = 0; i < NPORTS; i++) { buffer[i][frames_written] = 0.0; } frames_written++; nframes--; } return 0; // Tell JACK that all is well. } // This is the JACK graph reorder callback. Now we know some JACK connections // have changed, so we recompute the latency. static int graph(__attribute__((unused)) void * arg) { int latency = 0; debug(2, "JACK graph reorder callback called."); for (int i=0; i> 32; // debug(1,"delta: %" PRId64 " frames.",frames_processed_since_latest_latency_check); // jack_latency is set by the graph() callback, it's the average of the maximum // latencies of all our output ports. Adjust this constant baseline delay according // to the buffer fill level: *the_delay = jack_latency + audio_occupancy_now - frames_processed_since_latest_latency_check; // debug(1,"reporting a delay of %d frames",*the_delay); return 0; } int play(void *buf, int samples) { // debug(1,"jack_play of %d samples.",samples); // copy the samples into the queue size_t bytes_to_transfer, bytes_transferred; bytes_to_transfer = samples * bytes_per_frame; // It's ok to lock here since we're not in the realtime callback: pthread_mutex_lock(&buffer_mutex); bytes_transferred = jack_ringbuffer_write(jackbuf, buf, bytes_to_transfer); time_of_latest_transfer = get_absolute_time_in_fp(); pthread_mutex_unlock(&buffer_mutex); if (bytes_transferred < bytes_to_transfer) { warn("JACK ringbuffer overrun. Only wrote %d of %d bytes.", bytes_transferred, bytes_to_transfer); } return 0; }