-
Notifications
You must be signed in to change notification settings - Fork 4
/
pob-fec.c
416 lines (342 loc) · 8.41 KB
/
pob-fec.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
/*C
(c) 2005 bl0rg.net
**/
#include "conf.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#ifdef NEED_GETOPT_H__
#include <getopt.h>
#endif /* NEED_GETOPT_H__ */
#include "fec-pkt.h"
#include "aq.h"
#include "fec.h"
#include "network.h"
#include "fec-group.h"
#include "fec-rb.h"
#include "pob-fec.h"
#define FEC_MINSLEEP 20000 /* 200 ms */
/*M
\emph{Structure to hold client accounting information.}
This is used to count the number of wrong packets, duplicate
packets, out of order packets.
**/
typedef struct pob_stat_s {
/*M
Number of packets received.
**/
unsigned int rcvd_pkts;
/*M
Number of duplicated packets received.
**/
unsigned int lost_pkts;
/*M
Number of bad packets received.
**/
unsigned int bad_pkts;
/*M
Number of incomplete groups received.
**/
unsigned int incomplete_groups;
} pob_stat_t;
static pob_stat_t pob_stats = {
0, 0, 0, 0
};
/*M
\emph{Maximal buffer queue size (in packets).}
**/
static unsigned short buffer_size = 16;
/*
- Receive FEC packets, sort them into current group structure
- when group complete, decode group into adus and throw them into
adu_queue
- if older group packet arrives, discard
- if newer group packet arrives, ???
-
*/
/*M
\emph{Timestamp of last played group.}
**/
static unsigned long tstamp_last = 0;
int pob_insert_pkt(fec_pkt_t *pkt) {
assert(pkt != NULL);
/*M
We have received a packet.
**/
pob_stats.rcvd_pkts++;
int num = 0;
/* insert packet into ringbuffer */
if (fec_rb_length() > 0) {
/* get index of first packet */
fec_group_t *first_group = fec_rb_first();
assert(first_group != NULL);
assert(first_group->buf != NULL);
num = net_seqnum_diff(first_group->seq, pkt->hdr.group_seq, 1 << 8);
}
if (!fec_rb_insert_pkt(pkt, num)) {
#ifdef DEBUG
fprintf(stderr, "ring buffer full\n");
#endif
fec_rb_clear();
tstamp_last = pkt->hdr.group_tstamp;
return pob_insert_pkt(pkt);
} else {
return 1;
}
}
int pob_fec_recv_pkt(int sock, fec_pkt_t *pkt) {
/*M
Timeout in order to flush next frame to player.
**/
struct timeval t_out;
t_out.tv_sec = 0;
t_out.tv_usec = FEC_MINSLEEP;
/*M
Listen on receiving socket.
**/
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
/*M
Wait for network input or for timeout.
**/
int ret = select(sock + 1, &fds, NULL, NULL, &t_out);
/*M
Check for interrupted system call.
**/
if (ret == -1) {
if ((errno == EINTR) || (errno == EAGAIN)) {
return 0; /* timeout */
} else {
return -1; /* error */
}
}
/*M
If there is network input, read incoming packet.
**/
if (FD_ISSET(sock, &fds)) {
fec_pkt_init(pkt);
if (fec_pkt_read(pkt, sock) <= 0) {
return -1;
} else {
return 1;
}
}
return 0;
}
/*M
\emph{Simple FEC streaming client main loop.}
The mainloop calls the prebuffering routine each time the frame
queue is empty, then receives the packets, sorts them into groups,
decodes the groups and converts the ADUs into frames.
**/
int pob_mainloop(int sock, int quiet) {
int retval = 0;
/*M
Initialize the ADU queue;
**/
aq_t frame_queue;
aq_init(&frame_queue);
fec_pkt_t pkt;
int finished = 0;
while (!finished) {
static int prebuffering = 0;
if (fec_rb_cnt == 0) {
prebuffering = 1;
}
switch (pob_fec_recv_pkt(sock, &pkt)) {
case -1:
finished = 1;
retval = -1;
continue;
case 0:
break;
default:
/*M
Insert the packet into the FEC buffer.
**/
if (!pob_insert_pkt(&pkt)) {
finished = 1;
retval = -1;
goto exit;
}
}
static unsigned long time_last;
unsigned long time_now;
/*M
Get the current time to see which packets have to be written to
standard out.
**/
struct timeval tv;
gettimeofday(&tv, NULL);
time_now = tv.tv_sec * 1000000 + tv.tv_usec;
if (prebuffering == 1) {
if (fec_rb_cnt >= (fec_rb_size / 2)) {
fec_group_t *first_group = fec_rb_first();
assert(first_group != NULL);
assert(first_group->buf != NULL);
tstamp_last = first_group->tstamp;
time_last = time_now;
prebuffering = 0;
if (!quiet)
fprintf(stderr, "\n");
} else {
/*M
Print prebuffering information.
**/
if (!quiet)
fprintf(stderr, "Prebuffering: %.2f%%\r",
(float)fec_rb_cnt / (fec_rb_size / 2.0) * 100.0);
continue;
}
}
/*M
Print client information.
**/
if (!quiet) {
static int count = 0;
if ((count++ % 10) == 0) {
fprintf(stderr, "pkts: %.8u\tdrop: %.6u\tincomplete: %.6u\tbuf: %.6u len:%.4u\t\r",
pob_stats.rcvd_pkts,
pob_stats.lost_pkts,
pob_stats.incomplete_groups,
fec_rb_cnt,
fec_rb_length());
}
}
#ifdef DEBUG
fec_rb_print();
#endif
unsigned long tstamp_now = tstamp_last + (time_now - time_last);
while (fec_rb_length() > 0) {
fec_group_t *group = fec_rb_first();
assert(group != NULL);
if (group->buf != NULL) {
/* boeser hack XXX */
if (group->tstamp > (tstamp_now + 3000))
break;
/* decode group into adu queue */
if (!fec_group_decode_to_adus(group, &frame_queue)) {
fprintf(stderr, "Could not decode group\n");
/* XXX really continue? */
}
pob_stats.lost_pkts += group->fec_n - group->rcvd_pkts;
if (group->rcvd_pkts < group->fec_k)
pob_stats.incomplete_groups++;
mp3_frame_t *frame;
while ((frame = aq_get_frame(&frame_queue)) != NULL) {
memset(frame->raw, 0, 4 + frame->si_size);
/*M
Write packet payload.
**/
if (!mp3_fill_hdr(frame) ||
!mp3_fill_si(frame) ||
(write(STDOUT_FILENO,
frame->raw,
frame->frame_size) < (int)frame->frame_size)) {
fprintf(stderr, "Error writing to stdout\n");
free(frame);
retval = 0;
goto exit;
}
free(frame);
}
tstamp_last = tstamp_now;
time_last = time_now;
}
fec_rb_pop();
}
}
exit:
/*M
Destroy the ADU queue.
**/
aq_destroy(&frame_queue);
return retval;
}
/*M
\emph{Print FEC client usage.}
**/
static void usage(void) {
fprintf(stderr, "Usage: ./pob [-s address] [-p port] [-b size] [-q]\n");
fprintf(stderr, "\t-s address : destination address (default 0.0.0.0)\n");
fprintf(stderr, "\t-p port : destination port (default 1500)\n");
fprintf(stderr, "\t-b size : maximal number of fec groups in buffer (default 16)\n");
fprintf(stderr, "\t-q : quiet\n");
}
/*M
\emph{FEC RTP client entry routine.}
**/
int main(int argc, char *argv[]) {
char *address = NULL;
unsigned short port = 1500;
int retval = EXIT_SUCCESS, quiet = 0;
/*M
Process the command line arguments.
**/
int c;
while ((c = getopt(argc, argv, "hs:p:b:t:q")) >= 0) {
switch (c) {
case 's':
if (address != NULL)
free(address);
address = strdup(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 'b':
buffer_size = (unsigned short)atoi(optarg);
break;
case 'q':
quiet = 1;
break;
case 'h':
default:
usage();
retval = EXIT_SUCCESS;
goto exit;
}
}
/*M
Initialize the ring buffer.
**/
fec_rb_init(buffer_size);
if (address == NULL) {
#ifdef WITH_IPV6
address = strdup("ff02::4");
#else
address = strdup("0.0.0.0");
#endif /* WITH_IPV6 */
}
/*M
Create the receiving socket.
**/
int sock;
#ifdef WITH_IPV6
sock = net_udp6_recv_socket(address, port);
#else
sock = net_udp4_recv_socket(address, port);
#endif /* WITH_IPV6 */
if (sock < 0) {
fprintf(stderr, "Could not open socket\n");
retval = EXIT_FAILURE;
goto exit;
}
if (!pob_mainloop(sock, quiet))
retval = EXIT_FAILURE;
if (close(sock) < 0)
perror("close");
exit:
fec_rb_destroy();
if (address != NULL)
free(address);
return retval;
}
/*C
**/