-
Notifications
You must be signed in to change notification settings - Fork 21
/
jltcntp.c
471 lines (399 loc) · 12.1 KB
/
jltcntp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* gcc -o jltcntp jltcntp.c `pkg-config --cflags --libs jack ltc`
*
* Copyright (C) 2006, 2012, 2013 Robin Gareus
* Copyright (C) 2015 Dimitry Ishenko
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define LTC_QUEUE_LEN 42 // should be >> ( max(jack period size) * max-speedup / (duration of LTC-frame) )
#define _GNU_SOURCE
#ifdef WIN32
#include <windows.h>
#include <pthread.h>
#define pthread_t //< override jack.h def
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <getopt.h>
#include <sys/mman.h>
#include <jack/jack.h>
#include <ltc.h>
#ifndef WIN32
#include <signal.h>
#include <pthread.h>
#endif
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <time.h>
static int keep_running = 1;
static jack_port_t *input_port = NULL;
static jack_client_t *j_client = NULL;
static uint32_t j_samplerate = 48000;
static LTCDecoder *decoder = NULL;
static pthread_mutex_t ltc_thread_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER;
static int fps_num = 25;
static int fps_den = 1;
static int unit = -1;
static int no_date = 0;
static int verbose = 0;
static struct timeval tv_recv;
struct shmTime
{
int mode; /* 0 - if valid is set: use values, clear valid
* 1 - if valid is set: if count before and after read
* of data is equal: use values clear valid
*/
volatile int count;
time_t clockTimeStampSec;
int clockTimeStampUSec;
time_t receiveTimeStampSec;
int receiveTimeStampUSec;
int leap;
int precision;
int nsamples;
volatile int valid;
unsigned clockTimeStampNSec; /* Unsigned ns timestamps */
unsigned receiveTimeStampNSec; /* Unsigned ns timestamps */
int dummy[8];
};
static volatile struct shmTime *shm = NULL;
/**
* jack audio process callback
*/
int process(jack_nframes_t nframes, void *arg)
{
gettimeofday(&tv_recv, NULL);
jack_default_audio_sample_t *in = jack_port_get_buffer(input_port, nframes);
ltc_decoder_write_float(decoder, in, nframes, 0);
/* notify reader thread */
if (pthread_mutex_trylock(<c_thread_lock) == 0)
{
pthread_cond_signal(&data_ready);
pthread_mutex_unlock(<c_thread_lock);
}
return 0;
}
/**
* jack_shutdown
*/
void jack_shutdown(void *arg)
{
fprintf(stderr, "Received shutdown request from JACK\n");
keep_running = 0;
pthread_cond_signal(&data_ready);
}
/**
* open a client connection to the JACK server
*/
static int init_jack(const char *client_name)
{
jack_status_t status;
j_client = jack_client_open(client_name, JackNullOption, &status);
if (j_client == NULL)
{
fprintf(stderr, "jack_client_open() failed, status = 0x%2x\n", status);
if (status & JackServerFailed)
{
fprintf(stderr, "Unable to connect to JACK server\n");
}
return -1;
}
if (status & JackServerStarted)
{
fprintf(stderr, "JACK server started\n");
}
if (status & JackNameNotUnique)
{
client_name = jack_get_client_name(j_client);
fprintf(stderr, "jack-client name: '%s'\n", client_name);
}
jack_set_process_callback(j_client, process, 0);
#ifndef WIN32
jack_on_shutdown(j_client, jack_shutdown, NULL);
#endif
j_samplerate = jack_get_sample_rate(j_client);
return 0;
}
/**
* jack_portsetup
*/
static int jack_portsetup()
{
/* Allocate data structures that depend on the number of ports. */
decoder = ltc_decoder_create(j_samplerate * fps_den / fps_num, LTC_QUEUE_LEN);
if (!decoder)
{
fprintf (stderr, "Cannot create LTC decoder (out of memory)\n");
return -1;
}
/* create jack port */
if ((input_port = jack_port_register(j_client, "input_1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0)
{
fprintf(stderr, "Cannot register input port 'input_1'!\n");
return -1;
}
return 0;
}
/**
* jack_port_connect
*/
static void jack_port_connect(char **jack_port, int argc)
{
int i;
for (i = 0; i < argc; ++i)
{
if (!jack_port[i]) continue;
if (jack_connect(j_client, jack_port[i], jack_port_name(input_port)))
{
fprintf(stderr, "Cannot connect port %s to %s\n", jack_port[i], jack_port_name(input_port));
}
}
}
/**
* my_decoder_read
*/
static void my_decoder_read(LTCDecoder *d)
{
LTCFrameExt frame;
while (ltc_decoder_read(d, &frame))
{
int use_date = !no_date && frame.ltc.binary_group_flag_bit0 == 0
&& frame.ltc.binary_group_flag_bit2 == 1;
SMPTETimecode stime;
ltc_frame_to_time(&stime, &frame.ltc, use_date ? LTC_USE_DATE : 0);
struct tm tm_clock;
time_t offset = 0;
if (use_date)
{
int code = frame.ltc.user7 + (frame.ltc.user8 << 4);
if (code != 0x38) // user-defined time offset
{
offset = atoi(stime.timezone);
offset = (offset / 100) * 60 + (offset % 100);
offset*= 60; // seconds West of UTC
tzset();
offset -= timezone; // offset between LTC and local timezone
}
tm_clock.tm_mday = stime.days; // 1..31
tm_clock.tm_mon = stime.months - 1; // 0..11
tm_clock.tm_year = stime.years + 100; // years since 1900
tm_clock.tm_isdst = -1; // look up DST
}
else
{
time_t tc = time(NULL);
localtime_r(&tc, &tm_clock);
}
tm_clock.tm_sec = stime.secs;
tm_clock.tm_min = stime.mins;
tm_clock.tm_hour = stime.hours;
struct timeval tv_clock;
tv_clock.tv_sec = mktime(&tm_clock);
tv_clock.tv_usec = 1000000 * fps_den / fps_num * stime.frame;
int sent = 0;
if (shm && tv_clock.tv_sec != -1)
{
shm->mode = 0;
if (!shm->valid)
{
shm->clockTimeStampSec = tv_clock.tv_sec - offset;
shm->clockTimeStampUSec = tv_clock.tv_usec;
shm->receiveTimeStampSec = tv_recv.tv_sec;
shm->receiveTimeStampUSec = tv_recv.tv_usec;
shm->clockTimeStampNSec = 0;
shm->receiveTimeStampNSec = 0;
shm->valid = 1;
}
sent = 1;
}
if (verbose)
{
printf("%02d-%02d-%02d %s %02d:%02d:%02d%c%02d",
stime.years,
stime.months,
stime.days,
stime.timezone,
stime.hours,
stime.mins,
stime.secs,
frame.ltc.dfbit ? '.' : ':',
stime.frame
);
if (sent)
{
printf(" -=> %04d-%02d-%02d %02d:%02d:%02d.%06ld",
tm_clock.tm_year + 1900,
tm_clock.tm_mon + 1,
tm_clock.tm_mday,
tm_clock.tm_hour,
tm_clock.tm_min,
tm_clock.tm_sec,
tv_clock.tv_usec
);
}
printf("\n");
}
}
fflush(stdout);
}
/**
* main_loop
*/
static void main_loop()
{
pthread_mutex_lock(<c_thread_lock);
while (keep_running)
{
my_decoder_read(decoder);
if (!keep_running) break;
pthread_cond_wait(&data_ready, <c_thread_lock);
}
pthread_mutex_unlock (<c_thread_lock);
}
void catchsig(int sig)
{
#ifndef _WIN32
signal(SIGHUP, catchsig);
#endif
fprintf(stderr, "Caught signal - shutting down\n");
keep_running = 0;
pthread_cond_signal(&data_ready);
}
static struct option const long_options[] =
{
{ "help", no_argument, NULL, 'h' },
{ "fps", required_argument, NULL, 'f' },
{ "unit", required_argument, NULL, 'u' },
{ "no-date", no_argument, NULL, 'n' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 },
};
/**
* usage
*/
static void usage(int status)
{
printf("jltcntp - JACK LTC parser with NTP SHM support\n\n");
printf("Usage: jltcntp [ options ] [ JACK-ports ]\n\n");
printf("Options:\n\
-f, --fps <num>[/den] set expected framerate (default 25/1)\n\
-u, --unit <u> send LTC to NTP SHM driver unit <u> (default none)\n\
-n, --no-date ignore date received via LTC\n\
-v, --verbose output data to stdout\n\
-h, --help display this help and exit\n\
-V, --version print version information and exit\n\n");
printf("Website and manual: <https://github.com/x42/ltc-tools>\n");
exit(status);
}
/**
* decode_switches
*/
static int decode_switches(int argc, char **argv)
{
int c;
while ((c = getopt_long(argc, argv,
"h" /* help */
"f:" /* fps */
"u:" /* unit */
"n" /* no_date */
"v" /* verbose */
"V", /* version */
long_options, NULL)) != EOF)
{
switch (c)
{
case 'f':
{
fps_num = atoi(optarg);
char *tmp = strchr(optarg, '/');
if (tmp) fps_den = atoi(++tmp);
}
break;
case 'u':
unit = atoi(optarg);
break;
case 'n':
no_date = 1;
break;
case 'v':
verbose = 1;
break;
case 'V':
printf("jltcntp version %s\n\n", VERSION);
printf("Copyright (C) GPL 2006,2012,2013 Robin Gareus <[email protected]>\n");
printf("Copyright (C) 2015 Dimitry Ishenko <[email protected]>\n");
exit(0);
case 'h': usage (0);
default: usage(EXIT_FAILURE);
}
}
return optind;
}
int main(int argc, char **argv)
{
int i = decode_switches (argc, argv);
if (init_jack("jltcntp")) goto out;
if (jack_portsetup()) goto out;
if (mlockall(MCL_CURRENT | MCL_FUTURE))
{
fprintf(stderr, "Warning: Can not lock memory\n");
}
if (jack_activate(j_client))
{
fprintf(stderr, "Cannot activate client\n");
goto out;
}
jack_port_connect(&argv[i], argc - i);
#ifndef _WIN32
signal(SIGHUP, catchsig);
signal(SIGINT, catchsig);
#endif
if (unit >= 0)
{
int perms = unit > 1 ? 0666 : 0600;
int shmid = shmget(0x4e545030 + unit, sizeof (struct shmTime), IPC_CREAT | perms);
if (shmid != -1)
{
shm = (struct shmTime *) shmat(shmid, NULL, 0);
if (shm == (void *) -1)
{
perror("shmat");
goto out;
}
memset((void *)shm, 0, sizeof(struct shmTime));
shm->precision = -1; /* taken from ntpd/refclock_shm.c */
shm->nsamples = 3; /* taken from ntpd/refclock_shm.c */
}
else
{
perror("shmget");
goto out;
}
}
main_loop();
out:
if (j_client)
{
jack_client_close(j_client);
j_client = NULL;
}
ltc_decoder_free(decoder);
fprintf(stderr, "Bye\n");
return 0;
}