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