]>
Commit | Line | Data |
---|---|---|
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 | |
36 | enum ift_type { | |
37 | IFT_frame_left_sample = 0, | |
38 | IFT_frame_right_sample, | |
39 | } ift_type; | |
40 | ||
e99837f8 JN |
41 | // Two-channel, 16bit audio: |
42 | static const int bytes_per_frame = 4; | |
8cabb16f | 43 | // Four seconds buffer -- should be plenty |
e99837f8 | 44 | #define buffer_size 44100 * 4 * bytes_per_frame |
8cabb16f MB |
45 | |
46 | static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER; | |
6c6dda9c | 47 | static pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER; |
8cabb16f | 48 | |
e99837f8 | 49 | /* |
8cabb16f | 50 | char *audio_lmb, *audio_umb, *audio_toq, *audio_eoq; |
e99837f8 | 51 | |
8cabb16f MB |
52 | size_t audio_occupancy; // this is in frames, not bytes. A frame is a left and |
53 | // right sample, each 16 bits, hence 4 bytes | |
e99837f8 | 54 | */ |
6c6dda9c MB |
55 | pthread_t *open_client_if_necessary_thread = NULL; |
56 | ||
c1d5815c | 57 | int jack_init(int, char **); |
959aa08b | 58 | void jack_deinit(void); |
8cabb16f MB |
59 | void jack_start(int, int); |
60 | int play(void *, int); | |
61 | void jack_stop(void); | |
62 | int jack_is_running(void); | |
8cabb16f MB |
63 | int jack_delay(long *); |
64 | void jack_flush(void); | |
65 | ||
66 | audio_output audio_jack = {.name = "jack", | |
67 | .help = NULL, | |
c1d5815c | 68 | .init = &jack_init, |
959aa08b | 69 | .deinit = &jack_deinit, |
8cabb16f MB |
70 | .start = &jack_start, |
71 | .stop = &jack_stop, | |
72 | .is_running = &jack_is_running, | |
73 | .flush = &jack_flush, | |
74 | .delay = &jack_delay, | |
75 | .play = &play, | |
76 | .volume = NULL, | |
77 | .parameters = NULL, | |
78 | .mute = NULL}; | |
79 | ||
8cabb16f MB |
80 | jack_port_t *left_port; |
81 | jack_port_t *right_port; | |
959aa08b | 82 | |
8cabb16f | 83 | long offset = 0; |
8cabb16f MB |
84 | |
85 | int client_is_open; | |
86 | jack_client_t *client; | |
87 | jack_nframes_t sample_rate; | |
c1d5815c | 88 | jack_nframes_t jack_latency; |
8cabb16f | 89 | |
f07c16a6 | 90 | static jack_ringbuffer_t *jackbuf; |
e99837f8 | 91 | static int flush_please = 0; |
f07c16a6 | 92 | |
39545cdf | 93 | jack_latency_range_t latest_left_latency_range, latest_right_latency_range; |
959aa08b | 94 | int64_t time_of_latest_transfer; |
8cabb16f MB |
95 | |
96 | int play(void *buf, int samples) { | |
97 | // debug(1,"jack_play of %d samples.",samples); | |
98 | // copy the samples into the queue | |
e99837f8 JN |
99 | size_t bytes_to_transfer, bytes_transferred; |
100 | bytes_to_transfer = samples * bytes_per_frame; | |
101 | pthread_mutex_lock(&buffer_mutex); // it's ok to lock here since we're not in the realtime callback | |
102 | bytes_transferred = jack_ringbuffer_write(jackbuf, buf, bytes_to_transfer); | |
7ab56696 JN |
103 | // semantics change: we now measure the last time audio was moved into the ringbuffer, not the jack output buffers. |
104 | time_of_latest_transfer = get_absolute_time_in_fp(); | |
e99837f8 JN |
105 | pthread_mutex_unlock(&buffer_mutex); |
106 | if (bytes_transferred < bytes_to_transfer) { | |
107 | debug(1, "JACK ringbuffer overrun. Only wrote %d of %d bytes.", bytes_transferred, bytes_to_transfer); | |
108 | } | |
109 | /* | |
8cabb16f MB |
110 | size_t space_to_end_of_buffer = audio_umb - audio_eoq; |
111 | if (space_to_end_of_buffer >= bytes_to_transfer) { | |
112 | memcpy(audio_eoq, buf, bytes_to_transfer); | |
113 | pthread_mutex_lock(&buffer_mutex); | |
114 | audio_occupancy += samples; | |
115 | audio_eoq += bytes_to_transfer; | |
116 | pthread_mutex_unlock(&buffer_mutex); | |
117 | } else { | |
118 | memcpy(audio_eoq, buf, space_to_end_of_buffer); | |
119 | buf += space_to_end_of_buffer; | |
120 | memcpy(audio_lmb, buf, bytes_to_transfer - space_to_end_of_buffer); | |
121 | pthread_mutex_lock(&buffer_mutex); | |
122 | audio_occupancy += samples; | |
123 | audio_eoq = audio_lmb + bytes_to_transfer - space_to_end_of_buffer; | |
124 | pthread_mutex_unlock(&buffer_mutex); | |
125 | } | |
e99837f8 | 126 | */ |
8cabb16f MB |
127 | return 0; |
128 | } | |
129 | ||
130 | void deinterleave_and_convert_stream(const char *interleaved_frames, | |
959aa08b | 131 | const jack_default_audio_sample_t *jack_frame_buffer, |
8cabb16f MB |
132 | jack_nframes_t number_of_frames, enum ift_type side) { |
133 | jack_nframes_t i; | |
134 | short *ifp = (short *)interleaved_frames; | |
959aa08b | 135 | jack_default_audio_sample_t *fp = (jack_default_audio_sample_t *)jack_frame_buffer; |
8cabb16f MB |
136 | if (side == IFT_frame_right_sample) |
137 | ifp++; | |
138 | for (i = 0; i < number_of_frames; i++) { | |
139 | short sample = *ifp; | |
959aa08b | 140 | jack_default_audio_sample_t converted_value; |
8cabb16f MB |
141 | if (sample >= 0) |
142 | converted_value = (1.0 * sample) / SHRT_MAX; | |
143 | else | |
144 | converted_value = -(1.0 * sample) / SHRT_MIN; | |
145 | *fp = converted_value; | |
146 | ifp++; | |
147 | ifp++; | |
148 | fp++; | |
149 | } | |
150 | } | |
151 | ||
e99837f8 JN |
152 | static inline jack_default_audio_sample_t sample_conv(short sample) { |
153 | return ((sample < 0) ? (-1.0 * sample / SHRT_MIN) : (1.0 * sample / SHRT_MAX)); | |
154 | } | |
155 | ||
156 | void deinterleave_and_convert(const char *interleaved_frames, | |
157 | jack_default_audio_sample_t * const jack_buffer_left, | |
158 | jack_default_audio_sample_t * const jack_buffer_right, | |
159 | jack_nframes_t nframes) { | |
160 | jack_nframes_t i; | |
161 | short *ifp = (short *)interleaved_frames; // we're dealing with 16bit audio here | |
162 | jack_default_audio_sample_t *fpl = jack_buffer_left; | |
163 | jack_default_audio_sample_t *fpr = jack_buffer_right; | |
164 | for (i=0; i<nframes; i++) { | |
165 | fpl[i] = sample_conv(*ifp++); | |
166 | fpr[i] = sample_conv(*ifp++); | |
167 | } | |
168 | } | |
169 | ||
8cabb16f MB |
170 | int jack_stream_write_cb(jack_nframes_t nframes, __attribute__((unused)) void *arg) { |
171 | ||
959aa08b MB |
172 | jack_default_audio_sample_t *left_buffer = |
173 | (jack_default_audio_sample_t *)jack_port_get_buffer(left_port, nframes); | |
174 | jack_default_audio_sample_t *right_buffer = | |
175 | (jack_default_audio_sample_t *)jack_port_get_buffer(right_port, nframes); | |
8cabb16f | 176 | |
e99837f8 JN |
177 | jack_ringbuffer_data_t v[2] = { 0 }; |
178 | jack_nframes_t i, thisbuf; | |
179 | int frames_written = 0; | |
180 | int frames_required = 0; | |
181 | ||
83c8857c JN |
182 | if (flush_please) { |
183 | jack_ringbuffer_read_advance(jackbuf, jack_ringbuffer_read_space(jackbuf)); | |
3e6ef019 | 184 | flush_please = 0; |
83c8857c JN |
185 | } else { |
186 | jack_ringbuffer_get_read_vector(jackbuf, v); // an array of two elements because of possible ringbuffer wrap-around | |
187 | for (i=0; i<2; i++) { | |
188 | thisbuf = v[i].len / bytes_per_frame; | |
189 | if (thisbuf > nframes) { | |
190 | frames_required = nframes; | |
191 | } else { | |
192 | frames_required = thisbuf; | |
193 | } | |
194 | deinterleave_and_convert(v[i].buf, &left_buffer[frames_written], &right_buffer[frames_written], frames_required); | |
195 | frames_written = frames_required; | |
196 | nframes -= frames_written; | |
e99837f8 | 197 | } |
83c8857c | 198 | jack_ringbuffer_read_advance(jackbuf, frames_written * bytes_per_frame); |
e99837f8 | 199 | } |
e99837f8 JN |
200 | // debug(1,"transferring %u frames",written); |
201 | // Why are we doing this? The port latency should never change unless the JACK graph is reordered. Should happen on a graph reorder callback only. | |
3e6ef019 JN |
202 | jack_port_get_latency_range(left_port, JackPlaybackLatency, &latest_left_latency_range); |
203 | jack_port_get_latency_range(right_port, JackPlaybackLatency, &latest_right_latency_range); | |
e99837f8 JN |
204 | |
205 | // now, if there are any more frames to put into the buffer, fill them with | |
206 | // silence | |
207 | while (nframes > 0) { | |
208 | left_buffer[frames_written] = 0.0; | |
209 | right_buffer[frames_written] = 0.0; | |
210 | frames_written++; | |
211 | nframes--; | |
212 | } | |
e99837f8 JN |
213 | |
214 | /* | |
8cabb16f MB |
215 | size_t frames_we_can_transfer = nframes; |
216 | // lock | |
217 | pthread_mutex_lock(&buffer_mutex); | |
218 | if (audio_occupancy < frames_we_can_transfer) { | |
219 | frames_we_can_transfer = audio_occupancy; | |
175d91a4 MB |
220 | // This means we effectively have underflow from the Shairport Sync source. |
221 | // In fact, it may be that there is nothing at all coming from the source, | |
222 | // but the Shairport Sync client is open and active, so it must continue to output something. | |
8cabb16f MB |
223 | } |
224 | ||
8cabb16f MB |
225 | if (frames_we_can_transfer * 2 * 2 <= (size_t)(audio_umb - audio_toq)) { |
226 | // the bytes are all in a row in the audio buffer | |
227 | deinterleave_and_convert_stream(audio_toq, &left_buffer[0], frames_we_can_transfer, | |
228 | IFT_frame_left_sample); | |
229 | deinterleave_and_convert_stream(audio_toq, &right_buffer[0], frames_we_can_transfer, | |
230 | IFT_frame_right_sample); | |
231 | audio_toq += frames_we_can_transfer * 2 * 2; | |
232 | } else { | |
233 | // the bytes are in two places in the audio buffer | |
234 | size_t first_portion_to_write = (audio_umb - audio_toq) / (2 * 2); | |
235 | if (first_portion_to_write != 0) { | |
236 | deinterleave_and_convert_stream(audio_toq, &left_buffer[0], first_portion_to_write, | |
237 | IFT_frame_left_sample); | |
238 | deinterleave_and_convert_stream(audio_toq, &right_buffer[0], first_portion_to_write, | |
239 | IFT_frame_right_sample); | |
240 | } | |
241 | deinterleave_and_convert_stream(audio_lmb, &left_buffer[first_portion_to_write], | |
242 | frames_we_can_transfer - first_portion_to_write, | |
243 | IFT_frame_left_sample); | |
244 | deinterleave_and_convert_stream(audio_lmb, &right_buffer[first_portion_to_write], | |
245 | frames_we_can_transfer - first_portion_to_write, | |
246 | IFT_frame_right_sample); | |
247 | audio_toq = audio_lmb + (frames_we_can_transfer - first_portion_to_write) * 2 * 2; | |
248 | } | |
8cabb16f | 249 | audio_occupancy -= frames_we_can_transfer; |
959aa08b MB |
250 | jack_port_get_latency_range(left_port, JackPlaybackLatency, &latest_left_latency_range); |
251 | jack_port_get_latency_range(right_port, JackPlaybackLatency, &latest_right_latency_range); | |
252 | time_of_latest_transfer = get_absolute_time_in_fp(); | |
8cabb16f MB |
253 | pthread_mutex_unlock(&buffer_mutex); |
254 | // unlock | |
959aa08b | 255 | |
e99837f8 | 256 | */ |
8cabb16f MB |
257 | return 0; |
258 | } | |
259 | ||
e99837f8 JN |
260 | // FIXME: set_graph_order_callback(), recompute latencies here! |
261 | ||
959aa08b | 262 | void default_jack_error_callback(const char *desc) { debug(2, "jackd error: \"%s\"", desc); } |
8cabb16f | 263 | |
959aa08b | 264 | void default_jack_info_callback(const char *desc) { inform("jackd information: \"%s\"", desc); } |
8cabb16f | 265 | |
c1d5815c MB |
266 | void default_jack_set_latency_callback(jack_latency_callback_mode_t mode, |
267 | __attribute__((unused)) void *arg) { | |
268 | if (mode == JackPlaybackLatency) { | |
269 | jack_latency_range_t left_latency_range, right_latency_range; | |
270 | jack_port_get_latency_range(left_port, JackPlaybackLatency, &left_latency_range); | |
271 | jack_port_get_latency_range(right_port, JackPlaybackLatency, &right_latency_range); | |
272 | ||
273 | jack_nframes_t b_latency = (left_latency_range.min + left_latency_range.max) / 2; | |
274 | if (b_latency == 0) | |
275 | b_latency = (right_latency_range.min + right_latency_range.max) / 2; | |
276 | // jack_latency = b_latency; // actually, we are not interested in the latency of the jack | |
277 | // devices connected... | |
e99837f8 | 278 | // FIXME: yes, we are. |
c1d5815c MB |
279 | jack_latency = 0; |
280 | debug(1, "playback latency callback: %" PRIu32 ".", jack_latency); | |
281 | } | |
282 | } | |
283 | ||
8cabb16f | 284 | int jack_is_running() { |
76138873 | 285 | int reply = -1; // meaning jack is not running |
8cabb16f | 286 | if (client_is_open) { |
76138873 MB |
287 | |
288 | // check if the ports have a zero latency -- if they both have, then it's disconnected. | |
289 | ||
83472239 JN |
290 | // FIXME: this causes a segfault when shairport-sync is exited with CTRL-C, because |
291 | // the client_is_open flag is stale by then. Also, this test is not necessary. | |
292 | // shairport-sync should not worry what's reading its ports. As long as jack is alive, | |
293 | // deliver audio, even if nothing is connected. This behaviour probably stems from | |
294 | // the wish to not hog an audio device if not needed, which is no longer an issue with | |
295 | // jack. Moreover, don't "conserve" CPU this way, because in a realtime system you want | |
296 | // deterministic CPU load more than anything else. | |
297 | // jack_latency_range_t left_latency_range, right_latency_range; | |
298 | // jack_port_get_latency_range(left_port, JackPlaybackLatency, &left_latency_range); | |
299 | // jack_port_get_latency_range(right_port, JackPlaybackLatency, &right_latency_range); | |
76138873 | 300 | |
c1d5815c MB |
301 | // if ((left_latency_range.min == 0) && (left_latency_range.max == 0) && |
302 | // (right_latency_range.min == 0) && (right_latency_range.max == 0)) { | |
303 | // reply = -2; // meaning Shairport Sync is not connected | |
304 | // } else { | |
83472239 JN |
305 | |
306 | // FIXME: For now, we assume JACK is always running, as it should. | |
307 | // Still need to understand why shairport-sync needs this function. | |
c1d5815c MB |
308 | reply = 0; // meaning jack is open and Shairport Sync is connected to it |
309 | // } | |
8cabb16f MB |
310 | } |
311 | return reply; | |
312 | } | |
313 | ||
959aa08b | 314 | int jack_client_open_if_needed(void) { |
6c6dda9c | 315 | pthread_mutex_lock(&client_mutex); |
959aa08b MB |
316 | if (client_is_open == 0) { |
317 | jack_status_t status; | |
318 | client = jack_client_open(config.jack_client_name, JackNoStartServer, &status); | |
319 | if (client) { | |
320 | jack_set_process_callback(client, jack_stream_write_cb, 0); | |
321 | left_port = jack_port_register(client, config.jack_left_channel_name, JACK_DEFAULT_AUDIO_TYPE, | |
322 | JackPortIsOutput, 0); | |
323 | right_port = jack_port_register(client, config.jack_right_channel_name, | |
324 | JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); | |
325 | sample_rate = jack_get_sample_rate(client); | |
326 | // debug(1, "jackaudio sample rate = %" PRId32 ".", sample_rate); | |
327 | if (sample_rate == 44100) { | |
c1d5815c MB |
328 | if (jack_set_latency_callback(client, default_jack_set_latency_callback, NULL) == 0) { |
329 | if (jack_activate(client)) { | |
330 | debug(1, "jackaudio cannot activate client"); | |
331 | } else { | |
332 | debug(2, "jackaudio client opened."); | |
333 | client_is_open = 1; | |
334 | } | |
959aa08b | 335 | } else { |
c1d5815c | 336 | debug(1, "jackaudio cannot set latency callback"); |
959aa08b MB |
337 | } |
338 | } else { | |
339 | inform( | |
340 | "jackaudio is running at the wrong speed (%d) for Shairport Sync, which must be 44100", | |
341 | sample_rate); | |
342 | } | |
343 | } | |
344 | } | |
6c6dda9c | 345 | pthread_mutex_unlock(&client_mutex); |
959aa08b MB |
346 | return client_is_open; |
347 | } | |
348 | ||
6c6dda9c MB |
349 | void jack_close(void) { |
350 | pthread_mutex_lock(&client_mutex); | |
959aa08b MB |
351 | if (client_is_open) { |
352 | if (jack_deactivate(client)) | |
353 | debug(1, "Error deactivating jack client"); | |
354 | if (jack_client_close(client)) | |
355 | debug(1, "Error closing jack client"); | |
6c6dda9c MB |
356 | client_is_open = 0; |
357 | } | |
358 | pthread_mutex_unlock(&client_mutex); | |
359 | } | |
360 | ||
361 | void jack_deinit() { | |
362 | jack_close(); | |
363 | if (open_client_if_necessary_thread) { | |
364 | pthread_cancel(*open_client_if_necessary_thread); | |
365 | free((char *)open_client_if_necessary_thread); | |
f07c16a6 JN |
366 | |
367 | jack_ringbuffer_free(jackbuf); | |
368 | ||
959aa08b MB |
369 | } |
370 | } | |
371 | ||
6c6dda9c MB |
372 | void *open_client_if_necessary_thread_function(void *arg) { |
373 | int *interval = (int *)arg; | |
374 | while (*interval != 0) { | |
375 | if (client_is_open == 0) { | |
376 | debug(1, "Try to open the jack client"); | |
377 | jack_client_open_if_needed(); | |
378 | } | |
379 | sleep(*interval); | |
39545cdf | 380 | } |
6c6dda9c MB |
381 | pthread_exit(NULL); |
382 | } | |
383 | ||
c1d5815c | 384 | int jack_init(__attribute__((unused)) int argc, __attribute__((unused)) char **argv) { |
8cabb16f | 385 | config.audio_backend_latency_offset = 0; |
76138873 | 386 | config.audio_backend_buffer_desired_length = 0.500; |
80f15e1f MB |
387 | config.audio_backend_buffer_interpolation_threshold_in_seconds = |
388 | 0.25; // below this, soxr interpolation will not occur -- it'll be basic interpolation | |
389 | // instead. | |
6c6dda9c | 390 | config.jack_auto_client_open_interval = 1; // check every second |
8cabb16f MB |
391 | |
392 | // get settings from settings file first, allow them to be overridden by | |
393 | // command line options | |
394 | ||
395 | // do the "general" audio options. Note, these options are in the "general" stanza! | |
396 | parse_general_audio_options(); | |
959aa08b | 397 | |
8cabb16f MB |
398 | // other options would be picked up here... |
399 | ||
959aa08b MB |
400 | // now the specific options |
401 | if (config.cfg != NULL) { | |
402 | const char *str; | |
6c6dda9c | 403 | int value; |
959aa08b MB |
404 | /* Get the Client Name. */ |
405 | if (config_lookup_string(config.cfg, "jack.client_name", &str)) { | |
406 | config.jack_client_name = (char *)str; | |
407 | } | |
408 | /* Get the Left Channel Name. */ | |
409 | if (config_lookup_string(config.cfg, "jack.left_channel_name", &str)) { | |
410 | config.jack_left_channel_name = (char *)str; | |
411 | } | |
412 | /* Get the Right Channel Name. */ | |
413 | if (config_lookup_string(config.cfg, "jack.right_channel_name", &str)) { | |
414 | config.jack_right_channel_name = (char *)str; | |
415 | } | |
39545cdf MB |
416 | |
417 | /* See if we should attempt to connect to the jack server automatically, and, if so, how often | |
418 | * we should try. */ | |
6c6dda9c MB |
419 | if (config_lookup_int(config.cfg, "jack.auto_client_open_interval", &value)) { |
420 | if ((value < 0) || (value > 300)) | |
2e442853 MB |
421 | debug(1, |
422 | "Invalid jack auto_client_open_interval \"%sd\". It should be between 0 and 300, " | |
423 | "default is %d.", | |
76138873 | 424 | value, config.jack_auto_client_open_interval); |
6c6dda9c MB |
425 | else |
426 | config.jack_auto_client_open_interval = value; | |
427 | } | |
428 | ||
429 | /* See if we should close the client at then end of a play session. */ | |
39545cdf MB |
430 | config_set_lookup_bool(config.cfg, "jack.auto_client_disconnect", |
431 | &config.jack_auto_client_disconnect); | |
959aa08b MB |
432 | } |
433 | ||
434 | if (config.jack_client_name == NULL) | |
435 | config.jack_client_name = strdup("Shairport Sync"); | |
436 | if (config.jack_left_channel_name == NULL) | |
437 | config.jack_left_channel_name = strdup("left"); | |
438 | if (config.jack_right_channel_name == NULL) | |
439 | config.jack_right_channel_name = strdup("right"); | |
440 | ||
f07c16a6 | 441 | jackbuf = jack_ringbuffer_create(buffer_size); // make the jack ringbuffer the same size as audio_lmb |
06504516 JN |
442 | if (jackbuf == NULL) |
443 | die("Can't allocate %d bytes for the JACK ringbuffer.", buffer_size); | |
f07c16a6 JN |
444 | jack_ringbuffer_mlock(jackbuf); // lock buffer into memory so that it never gets paged out |
445 | ||
959aa08b | 446 | jack_set_error_function(default_jack_error_callback); |
8cabb16f | 447 | jack_set_info_function(default_jack_info_callback); |
8cabb16f | 448 | |
e99837f8 | 449 | /* |
8cabb16f MB |
450 | // allocate space for the audio buffer |
451 | audio_lmb = malloc(buffer_size); | |
452 | if (audio_lmb == NULL) | |
453 | die("Can't allocate %d bytes for jackaudio buffer.", buffer_size); | |
454 | audio_toq = audio_eoq = audio_lmb; | |
455 | audio_umb = audio_lmb + buffer_size; | |
456 | audio_occupancy = 0; // frames | |
e99837f8 | 457 | */ |
959aa08b MB |
458 | |
459 | client_is_open = 0; | |
39545cdf | 460 | |
175d91a4 | 461 | // now, if selected, start a thread to automatically open a client when there is a server. |
6c6dda9c MB |
462 | if (config.jack_auto_client_open_interval != 0) { |
463 | open_client_if_necessary_thread = malloc(sizeof(pthread_t)); | |
175d91a4 | 464 | if (open_client_if_necessary_thread == NULL) { |
76138873 | 465 | debug(1, "Couldn't allocate space for jack server scanner thread"); |
175d91a4 MB |
466 | jack_client_open_if_needed(); |
467 | } else { | |
76138873 MB |
468 | pthread_create(open_client_if_necessary_thread, NULL, |
469 | open_client_if_necessary_thread_function, | |
470 | &config.jack_auto_client_open_interval); | |
175d91a4 | 471 | } |
6c6dda9c MB |
472 | } else { |
473 | jack_client_open_if_needed(); | |
474 | } | |
959aa08b | 475 | |
8cabb16f MB |
476 | return 0; |
477 | } | |
478 | ||
959aa08b MB |
479 | void jack_start(__attribute__((unused)) int i_sample_rate, |
480 | __attribute__((unused)) int i_sample_format) { | |
6c6dda9c | 481 | // debug(1, "jack start"); |
959aa08b MB |
482 | // see if the client is running. If not, try to open and initialise it |
483 | ||
484 | if (jack_client_open_if_needed() == 0) | |
485 | debug(1, "cannot open a jack client for a play session"); | |
8cabb16f MB |
486 | } |
487 | ||
488 | int jack_delay(long *the_delay) { | |
39545cdf | 489 | |
7ab56696 JN |
490 | // semantics change: we now look at the last transfer into the lock-free ringbuffer, not |
491 | // into the jack buffers directly (because locking those would violate real-time constraints). | |
492 | // on average, that should lead to just a constant additional latency. the old comment still applies: | |
493 | ||
959aa08b | 494 | // without the mutex, we could get the time of what is the last transfer of data to a jack buffer, |
39545cdf MB |
495 | // but then a transfer could occur and we would get the buffer occupancy after another transfer |
496 | // had occurred | |
497 | // so we could "lose" a full transfer (e.g. 1024 frames @ 44,100 fps ~ 23.2 milliseconds) | |
e99837f8 | 498 | |
39545cdf | 499 | pthread_mutex_lock(&buffer_mutex); |
e99837f8 JN |
500 | int64_t time_now = get_absolute_time_in_fp(); |
501 | // this is the time back to the last time data | |
502 | // was transferred into a jack buffer | |
503 | int64_t delta = time_now - time_of_latest_transfer; | |
504 | // this is the buffer occupancy before any | |
505 | // subsequent transfer because transfer is blocked | |
506 | // by the mutex | |
a238ac54 | 507 | size_t audio_occupancy_now = jack_ringbuffer_read_space(jackbuf) / bytes_per_frame; |
959aa08b MB |
508 | pthread_mutex_unlock(&buffer_mutex); |
509 | ||
8cabb16f | 510 | int64_t frames_processed_since_latest_latency_check = (delta * 44100) >> 32; |
8cabb16f | 511 | // debug(1,"delta: %" PRId64 " frames.",frames_processed_since_latest_latency_check); |
39545cdf | 512 | jack_nframes_t base_latency = (latest_left_latency_range.min + latest_left_latency_range.max) / 2; |
959aa08b | 513 | if (base_latency == 0) |
39545cdf MB |
514 | base_latency = (latest_right_latency_range.min + latest_right_latency_range.max) / 2; |
515 | *the_delay = base_latency + audio_occupancy_now - frames_processed_since_latest_latency_check; | |
8cabb16f | 516 | // debug(1,"reporting a delay of %d frames",*the_delay); |
8cabb16f MB |
517 | return 0; |
518 | } | |
519 | ||
520 | void jack_flush() { | |
7ab56696 | 521 | debug(1, "Only the consumer can safely flush a lock-free ringbuffer. Asking the process callback to do it..."); |
e99837f8 JN |
522 | flush_please = 1; |
523 | /* | |
959aa08b | 524 | // debug(1,"jack flush"); |
8cabb16f MB |
525 | audio_toq = audio_eoq = audio_lmb; |
526 | audio_umb = audio_lmb + buffer_size; | |
527 | audio_occupancy = 0; // frames | |
e99837f8 | 528 | */ |
8cabb16f MB |
529 | } |
530 | ||
6c6dda9c MB |
531 | void jack_stop(void) { |
532 | // debug(1, "jack stop"); | |
533 | if (config.jack_auto_client_disconnect) | |
534 | jack_close(); | |
aa5698fc | 535 | } |