-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstreams.c
461 lines (386 loc) · 13.7 KB
/
streams.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
/*
* VBAN Receiver
*
* Copyright (C) 2017 Raman Shyshniou <[email protected]>
* All Rights Reserved.
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <errno.h>
#include "vban.h"
#include "logger.h"
#include "streams.h"
#define DATA_BUFFER_SIZE 1436
struct stream *streams = NULL;
/*
* Cleanup streams
*/
void forgetstreams(void)
{
struct stream *stream = streams;
while (stream) {
struct stream *del = stream;
stream = stream->next;
logger(LOG_INF, "[%s@%s] stream offline", del->name, del->ifname);
if (del->curr.data)
free(del->curr.data);
if (del->prev.data)
free(del->prev.data);
free(del);
}
streams = NULL;
}
/*
* Remove stream
*/
void forgetstream(struct stream *stream)
{
logger(LOG_INF, "[%s@%s] stream offline", stream->name, stream->ifname);
if (stream == streams) {
streams = stream->next;
} else {
struct stream *prev;
for (prev = streams; prev->next != stream; prev = prev->next);
prev->next = stream->next;
}
if (stream->curr.data)
free(stream->curr.data);
if (stream->prev.data)
free(stream->prev.data);
free(stream);
}
/*
* Find stream for the packet
*/
struct stream *getstream(struct vbaninfo *info, struct sockaddr *addr, unsigned ifindex)
{
struct stream *stream;
if (!streams)
return NULL;
for (stream = streams; stream; stream = stream->next) {
struct sockaddr *peer = (void *) &stream->peer;
// check interface
if (stream->ifindex != ifindex)
continue;
// check peer address family
if (peer->sa_family != addr->sa_family)
continue;
// check peer address
switch (peer->sa_family) {
case AF_INET: {
struct sockaddr_in *in1 = (void *) addr, *in2 = (void *) peer;
if (in1->sin_port != in2->sin_port)
continue;
if (in1->sin_addr.s_addr != in2->sin_addr.s_addr)
continue;
break;
}
#ifdef AF_INET6
case AF_INET6: {
struct sockaddr_in6 *in1 = (void *) addr, *in2 = (void *) peer;
if (in1->sin6_port != in2->sin6_port)
continue;
if (in1->sin6_flowinfo != in2->sin6_flowinfo)
continue;
if (in1->sin6_scope_id != in2->sin6_scope_id)
continue;
if (memcmp(in1->sin6_addr.s6_addr,
in2->sin6_addr.s6_addr,
sizeof(in1->sin6_addr.s6_addr)))
continue;
break;
}
#endif
default:
continue;
}
// check stream name
if (strcmp(info->stream_name, stream->name))
continue;
// stream found
return stream;
}
return NULL;
}
/*
* Receive and parse next VBAN packet
*/
struct stream *recvvban(int sock)
{
char *buffer;
char vban_header[VBAN_HEADER_SIZE];
uint8_t aux[1024];
int64_t delta;
int64_t delta1;
int64_t delta2;
unsigned ifindex;
struct stream *stream;
struct sockaddr_storage addr;
struct vbaninfo info;
struct iovec iov[2];
struct timespec ts;
struct cmsghdr *cm;
struct msghdr m;
int found_idx;
int found_ts;
ssize_t size;
buffer = malloc(DATA_BUFFER_SIZE);
if (!buffer) {
logger(LOG_ERR, "cannot allocate memory!");
return NULL;
}
while (1) {
iov[0].iov_base = vban_header;
iov[0].iov_len = sizeof(vban_header);
iov[1].iov_base = buffer;
iov[1].iov_len = DATA_BUFFER_SIZE;
m.msg_name = &addr;
m.msg_namelen = sizeof(addr);
m.msg_iov = iov;
m.msg_iovlen = 2;
m.msg_control = aux;
m.msg_controllen = sizeof(aux);
m.msg_flags = 0;
size = recvmsg(sock, &m, 0);
if (size < 0 && errno == EINTR)
continue;
if (size < 0 && errno == EAGAIN) {
free(buffer);
return NULL;
}
if (size < 0) {
logger(LOG_ERR, "recvmsg: %s", strerror(errno));
free(buffer);
return NULL;
}
ifindex = 0;
found_ts = 0;
found_idx = 0;
for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm)) {
if (cm->cmsg_level == IPPROTO_IP && cm->cmsg_type == IP_PKTINFO) {
struct in_pktinfo *ipi = (void *) CMSG_DATA(cm);
ifindex = ipi->ipi_ifindex;
found_idx++;
}
if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SCM_TIMESTAMPNS) {
memcpy(&ts, CMSG_DATA(cm), sizeof(ts));
found_ts++;
}
}
if (!found_idx) {
logger(LOG_ERR, "couldn't find IP_PKTINFO data in auxiliary recvmsg() data!");
free(buffer);
return NULL;
}
if (!found_ts) {
logger(LOG_ERR, "couldn't find SCM_TIMESTAMPNS data in auxiliary recvmsg() data!");
free(buffer);
return NULL;
}
if (vban_parse(vban_header, size, &info) < 0) {
logger(LOG_VRB, "malformed VBAN packet received");
continue;
}
if (info.protocol != VBAN_PROTOCOL_AUDIO) {
logger(LOG_VRB, "[%s] unsupporded protocol", info.stream_name);
continue;
}
if (info.codec != VBAN_CODEC_PCM) {
logger(LOG_VRB, "[%s] unsupported audio codec", info.stream_name);
continue;
}
stream = getstream(&info, (struct sockaddr *) &addr, ifindex);
size -= VBAN_HEADER_SIZE;
if (stream) {
double dt, dv;
// check packet size
if (size < stream->pktsize) {
logger(LOG_VRB, "[%s@%s] too short packet received",
stream->name, stream->ifname);
continue;
} else
if (size > stream->pktsize) {
logger(LOG_VRB, "[%s@%s] too long packet received",
stream->name, stream->ifname);
continue;
}
// check packet type
if (stream->frames != info.frames ||
stream->format != info.format ||
stream->channels != info.channels ||
stream->sample_rate != info.sample_rate) {
logger(LOG_VRB, "[%s@%s] bad packet received",
stream->name, stream->ifname);
continue;
}
// update stats and timestamp
dt = (ts.tv_sec - stream->ts_last.tv_sec) * 1000000000.0 +
(ts.tv_nsec - stream->ts_last.tv_nsec);
// exponentially weighted moving variance
dv = dt - stream->dt_average;
stream->dt_variance = stream->ewma_a2 *
(stream->dt_variance + stream->ewma_a1 * dv * dv);
// exponentially weighted moving average
stream->dt_average = stream->ewma_a1 * dt +
stream->ewma_a2 * stream->dt_average;
// stream timestamp
stream->ts_last = ts;
} else {
char peer[128];
double pps;
// parse peer address
switch (((struct sockaddr *) &addr)->sa_family) {
case AF_INET: {
struct sockaddr_in *in = (void *) &addr;
inet_ntop(AF_INET, &in->sin_addr, peer, sizeof(peer));
sprintf(peer + strlen(peer), ":%d", ntohs(in->sin_port));
break;
};
#ifdef AF_INET6
case AF_INET6: {
struct sockaddr_in6 *in6 = (void *) &addr;
peer[0] = '[';
inet_ntop(AF_INET6, &in6->sin6_addr, peer + 1, sizeof(peer) - 1);
sprintf(peer + strlen(peer), "]:%d", ntohs(in6->sin6_port));
break;
};
#endif
default:
logger(LOG_ERR, "[%s] unsupported address family", info.stream_name);
continue;
}
// create new stream entry
stream = malloc(sizeof(struct stream));
if (!stream) {
logger(LOG_ERR, "[%s] cannot allocate memory", info.stream_name);
free(buffer);
return NULL;
}
// parse interface index
stream->ifindex = ifindex;
if_indextoname(ifindex, stream->ifname);
memcpy(&stream->peer, &addr, sizeof(struct sockaddr_storage));
strcpy(stream->name, info.stream_name);
stream->frames = info.frames;
stream->frame_size = info.frame_size;
stream->sample_size = info.sample_size;
stream->sample_rate = info.sample_rate;
stream->channels = info.channels;
stream->pktsize = size;
stream->format = info.format;
stream->format_name = info.format_name;
stream->lost = 0;
stream->expected = info.seq;
stream->curr.data = NULL;
stream->prev.data = NULL;
stream->ts_first = ts;
stream->ts_last = ts;
stream->ignore = 0;
stream->insync = 0;
stream->offset = 0;
stream->next = NULL;
// stats
pps = (double) stream->sample_rate / (double) stream->frames;
stream->ewma_a1 = 2.0 / (1.0 + 30.0 * pps);
stream->ewma_a2 = 1.0 - stream->ewma_a1;
stream->dt_average = 1000000000.0 / pps;
stream->dt_variance = 0;
logger(LOG_INF, "[%s@%s] stream connected from %s, %s, %ld Hz, %ld channel(s)",
stream->name, stream->ifname, peer, stream->format_name,
stream->sample_rate, stream->channels);
if (streams) {
struct stream *tail = streams;
for (; tail->next; tail = tail->next);
tail->next = stream;
} else {
streams = stream;
}
}
// save data to stream buffers
if (stream->expected == info.seq) {
if (stream->prev.data)
free(stream->prev.data);
stream->expected++;
stream->prev = stream->curr;
stream->curr.data = buffer;
stream->curr.sent = 0;
return stream;
}
// out of order packet received
// check sequence overflow
delta = (int64_t) info.seq - (int64_t) stream->expected;
delta1 = delta + 0x100000000L;
delta2 = delta - 0x100000000L;
if (llabs(delta2) < llabs(delta1))
delta1 = delta2;
if (llabs(delta1) < llabs(delta))
delta = delta1;
if (delta < 0) {
// received lost packet
if (delta == -1 || (delta == -2 && stream->prev.data)) {
// duplicate
logger(LOG_DBG, "[%s@%s] expected %lu, got %lu: duplicate?",
stream->name, stream->ifname, (long unsigned) stream->expected,
(long unsigned) info.seq);
continue;
}
// received previous packet
if (delta == -2 && !stream->prev.data) {
// restore previous packet
stream->lost--;
stream->prev.data = buffer;
stream->prev.sent = 0;
logger(LOG_DBG, "[%s@%s] expected %lu, got %lu: restored",
stream->name, stream->ifname, (long unsigned) stream->expected,
(long unsigned) info.seq);
return stream;
}
logger(LOG_DBG, "[%s@%s] expected %lu, got %lu: dropped",
stream->name, stream->ifname, (long unsigned) stream->expected,
(long unsigned) info.seq);
continue;
}
// lost packets
stream->lost += (long) delta;
if (delta == 1)
logger(LOG_DBG, "[%s@%s] expected %lu, got %lu: lost 1 packet",
stream->name, stream->ifname, (long unsigned) stream->expected,
(long unsigned) info.seq);
else
logger(LOG_DBG, "[%s@%s] expected %lu, got %lu: lost %lld packets",
stream->name, stream->ifname, (long unsigned) stream->expected,
(long unsigned) info.seq, (long long) delta);
if (stream->prev.data)
free(stream->prev.data);
if (stream->curr.data)
free(stream->curr.data);
stream->expected = info.seq + 1;
stream->prev.data = NULL;
stream->prev.sent = 0;
stream->curr.data = buffer;
stream->curr.sent = 0;
return stream;
}
}