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