-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy patheti2zmq.c
358 lines (300 loc) · 9.96 KB
/
eti2zmq.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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#ifdef HAVE_ZMQ
#include <zmq.h>
#endif
#define PACKED __attribute__ ((packed))
int Interrupted=0;
#define ETI_NI_FSYNC0 0xb63a07ff
#define ETI_NI_FSYNC1 0x49c5f8ff
#define ETI_NI_RAW_SIZE 6144
#define ETI_NI_FRAME_TIME 24000 //useconds
/*****************************************************************************
* info/help/debug
*****************************************************************************/
//#define DEBUG(format, ...) fprintf (stderr, "DEBUG: "format"\n", ## __VA_ARGS__)
#define DEBUG(format, ...)
#define INFO(format, ...) fprintf (stderr, "INFO: "format"\n", ## __VA_ARGS__)
#define WARN(format, ...) fprintf (stderr, "WARN: "format"\n", ## __VA_ARGS__)
#define ERROR(format, ...) fprintf (stderr, "ERROR: "format"\n", ## __VA_ARGS__)
extern struct ens_info einf;
static void usage(const char *psz)
{
fprintf(stderr, "usage: %s [--delay] [-i <inputfile>] -o zmq+tcp://0.0.0.0:18082\n\n"
"Additional info related to arguments:\n"
"--input or -i <filename> Provide input file for app\n"
"--output or -o <zeromq address> Provide zeromq output address like zmq+tcp://[local_ip]:[port_to_listen]\n"
"--delay or -d Force pseudo-realtume streaming (add 24ms delay for each eti frame)\n"
"--loop or -l Run input file in-a-loop\n"
"--no-align or -L Disable frame alignment for ZeroMQ packing\n"
"--no-cut-tail or -C Disable removing unneeded fill bytes at ETI frames tails\n"
"--activity or -a Display app's activity in console\n"
"--verbose or -v Increase app's verbosity (can be provided multiple times)\n\n",
psz);
exit(EXIT_FAILURE);
}
static void signal_handler(int signum)
{
if (signum != SIGPIPE) {
Interrupted=signum;
}
signal(signum,signal_handler);
}
int verbosity=0;
#define NUM_FRAMES_PER_ZMQ_MESSAGE 4
#ifdef HAVE_ZMQ
typedef struct {
uint32_t version;
int16_t buflen[NUM_FRAMES_PER_ZMQ_MESSAGE];
/* The head stops here. Use the macro below to calculate
* the head size.
*/
uint8_t buf[NUM_FRAMES_PER_ZMQ_MESSAGE*6144];
} PACKED zmq_dab_message_t;
#define ZMQ_DAB_MESSAGE_HEAD_LENGTH (4 + NUM_FRAMES_PER_ZMQ_MESSAGE*2)
int is_initial = 1;
zmq_dab_message_t zmq_msg;
int zmq_msg_ix;
int zmq_align = 1;
int write_zmq(void *privData, void *etiData, int etiLen)
{
int i;
int offset = 0;
uint8_t *etiPkt = (uint8_t *) etiData;
if(verbosity > 1)
INFO("write:%u bytes to zmq part: %d", etiLen, zmq_msg_ix);
// Increment the offset by the accumulated frame offsets
for (i = 0; i < zmq_msg_ix; i++) {
offset += zmq_msg.buflen[i];
}
if (offset + etiLen > NUM_FRAMES_PER_ZMQ_MESSAGE*6144) {
INFO("FAULT: invalid ETI frame size!");
return 0;
}
if(etiLen < 16 || (zmq_align && zmq_msg_ix != ((etiPkt[6] >> 5) & 0x03))) {
zmq_msg_ix=0;
zmq_msg.buflen[0] = -1;
zmq_msg.buflen[1] = -1;
zmq_msg.buflen[2] = -1;
zmq_msg.buflen[3] = -1;
INFO("ZMQ: skip non-aligned frames: %02x != %02x", zmq_msg_ix, ((etiPkt[6] >> 5) & 0x03));
return 0;
}
// Append the new frame to our message
memcpy(zmq_msg.buf + offset, etiPkt, etiLen);
zmq_msg.buflen[zmq_msg_ix] = etiLen;
zmq_msg_ix++;
// As soon as we have NUM_FRAMES_PER_ZMQ_MESSAGE frames, we transmit
if (zmq_msg_ix == NUM_FRAMES_PER_ZMQ_MESSAGE) {
// Size of the header:
int full_length = ZMQ_DAB_MESSAGE_HEAD_LENGTH;
for (i = 0; i < NUM_FRAMES_PER_ZMQ_MESSAGE; i++) {
full_length += zmq_msg.buflen[i];
}
zmq_msg_ix = 0;
const int flags = 0;
if(verbosity > 1)
INFO("zmq send:%u bytes", full_length);
zmq_send(privData, (uint8_t*)&zmq_msg, full_length, flags);
for (i = 0; i < NUM_FRAMES_PER_ZMQ_MESSAGE; i++) {
zmq_msg.buflen[i] = -1;
}
return 1;
}
return 0;
}
#endif
void timespec_diff(const struct timespec *start, const struct timespec *stop, struct timespec *result) {
if ((stop->tv_nsec - start->tv_nsec) < 0) {
result->tv_sec = stop->tv_sec - start->tv_sec - 1;
result->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000L;
} else {
result->tv_sec = stop->tv_sec - start->tv_sec;
result->tv_nsec = stop->tv_nsec - start->tv_nsec;
}
return;
}
int main(int i_argc, char **ppsz_argv)
{
int c, delay=0, loop=0, cut_tail=1;
FILE *inputfile=stdin;
char *outpath = NULL;
int activity=0;
#ifdef HAVE_ZMQ
void *zmq_context = NULL;
void *zmq_publisher = NULL;
#endif
static const struct option long_options[] = {
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o' },
{ "delay", no_argument, NULL, 'd' },
{ "loop", no_argument, NULL, 'l' },
{ "no-align", no_argument, NULL, 'L' },
{ "no-cut-tail", no_argument, NULL, 'C' },
{ "activity", no_argument, NULL, 'a' },
{ "verbose", no_argument, NULL, 'v' },
{ 0, 0, 0, 0 }
};
while ((c = getopt_long(i_argc, ppsz_argv, "i:o:lLdhav", long_options, NULL)) != -1)
{
switch (c) {
case 'i':
inputfile = fopen(optarg, "r");
if(!inputfile) {
ERROR("cant open input file!");
exit(1);
}
break;
#ifdef HAVE_ZMQ
case 'L':
zmq_align=0;
break;
#endif
case 'o':
outpath = optarg;
break;
case 'd':
delay=1;
break;
case 'l':
loop=1;
break;
case 'C':
cut_tail=0;
break;
case 'a':
activity=1;
break;
case 'v':
verbosity++;
break;
case 'h':
default:
usage(ppsz_argv[0]);
}
}
if(!outpath)
usage(ppsz_argv[0]);
if(strncmp("zmq+", outpath, 4) == 0 && strlen(outpath) > 10) {
#ifdef HAVE_ZMQ
zmq_context = zmq_ctx_new ();
zmq_publisher = zmq_socket (zmq_context, ZMQ_PUB);
zmq_bind (zmq_publisher, outpath+4);
zmq_msg.buflen[0] = -1;
zmq_msg.buflen[1] = -1;
zmq_msg.buflen[2] = -1;
zmq_msg.buflen[3] = -1;
zmq_msg.version = 1;
zmq_msg_ix=0;
#else
ERROR("ZEROMQ is disabled! Can't stream to specified destination: %s", outpath);
exit(-1);
#endif
}
fprintf(stderr, "loop=%d, delay=%d, activity=%d, outpath=\"%s\"\n", loop, delay, activity, outpath);
/* space for 1 ETI frame for bitwise seeking */
int bytes_readed = 0;
uint8_t p_ni_search_block[ETI_NI_RAW_SIZE];
uint32_t sync_byte;
int sync_found = 0;
int total_readed = 0;
unsigned long int count = 0;
struct timespec time_start;
clock_gettime(CLOCK_MONOTONIC, &time_start);
signal(SIGINT, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
char ui[4] = { '-', '\\', '|', '/' };
int vertex=0;
/* search for ETI-NI sync */
do {
bytes_readed=0;
if (activity > 0 && !(count % 32)) {
fprintf(stderr, "\x08%c", ui[vertex]);
vertex++;
if(vertex > 3) vertex=0;
}
do {
size_t i_ret = fread(p_ni_search_block + bytes_readed, ETI_NI_RAW_SIZE - bytes_readed, 1, inputfile);
if(i_ret != 1){
ERROR("EOF reached... (%ld/%d)\n", count, total_readed);
if(!loop)
exit(1);
fseek(inputfile, 0, SEEK_SET);
sync_found=0;
bytes_readed=0;
continue;
}
total_readed += ETI_NI_RAW_SIZE - bytes_readed;
bytes_readed = ETI_NI_RAW_SIZE;
do {
sync_byte = p_ni_search_block[3] << 24 | p_ni_search_block[2] << 16 | p_ni_search_block[1] << 8 | p_ni_search_block[0];
if(sync_byte == ETI_NI_FSYNC0 || sync_byte == ETI_NI_FSYNC1) {
sync_found=1;
} else {
bytes_readed--;
memmove(p_ni_search_block, p_ni_search_block+1, bytes_readed);
}
} while (!sync_found && bytes_readed > 4);
} while (!sync_found || bytes_readed != ETI_NI_RAW_SIZE);
count++;
DEBUG("sync=%08x at pos %d", sync_byte, total_readed);
uint32_t fc_val = p_ni_search_block[4] << 24 | p_ni_search_block[5] << 16 | p_ni_search_block[6] << 8 | p_ni_search_block[7];
if(fc_val == 0xffffffff)
continue;
DEBUG("NST=%d", p_ni_search_block[5] & 0x7f);
//fc_t fc;
//uint8_t fct = fc_word >> 24;
#if 0
DEBUG("fc=%08x fct=%d, fc.fp=%d", fc.val, fc.fct, fc.fp);
DEBUG("ficf=%d, nst=%d", fc.ficf, fc.nst);
DEBUG("DAB MODE %d", fc.mid > 0 ? fc.mid : 4);
DEBUG("DAB streams = %d", fc.nst);
/* fic length in words */
uint8_t ficl = (fc.ficf == 0) ? 0 : 24;
if(ficl && fc.mid == 3) ficl = 32;
DEBUG("FIC length = %d words", ficl);
DEBUG("frame len=%d", fc.fl*4);
DEBUG("MST len=%d", (fc.fl - (1 + 1 + fc.nst))*4);
#endif
int is_written = 0;
#ifdef HAVE_ZMQ
is_written=write_zmq(zmq_publisher, p_ni_search_block, ETI_NI_RAW_SIZE);
#endif
if(delay && is_written) {
struct timespec time_now, time_nosleep;
clock_gettime(CLOCK_MONOTONIC, &time_now);
timespec_diff(&time_start, &time_now, &time_nosleep);
time_start.tv_nsec=time_start.tv_nsec + ETI_NI_FRAME_TIME*NUM_FRAMES_PER_ZMQ_MESSAGE*1000;
if(time_start.tv_nsec >= 1000000000L) {
time_start.tv_nsec -= 1000000000L;
time_start.tv_sec++;
}
time_nosleep.tv_nsec=(ETI_NI_FRAME_TIME*NUM_FRAMES_PER_ZMQ_MESSAGE*1000 - time_nosleep.tv_nsec);
if(time_nosleep.tv_sec==0 && time_nosleep.tv_nsec > 0) {
clock_nanosleep(CLOCK_MONOTONIC, 0, &time_nosleep, NULL);
}
}
} while(!Interrupted);
#ifdef HAVE_ZMQ
if(zmq_publisher)
zmq_close (zmq_publisher);
if(zmq_context)
zmq_term (zmq_context);
#endif
fclose(inputfile);
return 0;
}