-
Notifications
You must be signed in to change notification settings - Fork 5
/
rtcp.c
944 lines (909 loc) · 27.8 KB
/
rtcp.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/*! \file rtcp.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief RTCP processing
* \details Implementation (based on the oRTP structures) of the RTCP
* messages. RTCP messages coming through the gateway are parsed and,
* if needed (according to http://tools.ietf.org/html/draft-ietf-straw-b2bua-rtcp-00),
* fixed before they are sent to the peers (e.g., to fix SSRCs that may
* have been changed by the gateway). Methods to generate FIR messages
* and generate/cap REMB messages are provided as well.
*
* \ingroup protocols
* \ref protocols
*/
#include <math.h>
#include <stdlib.h>
#include "debug.h"
#include "rtp.h"
#include "rtcp.h"
#include "utils.h"
int janus_rtcp_parse(rtcp_context *ctx, char *packet, int len) {
return janus_rtcp_fix_ssrc(ctx, packet, len, 0, 0, 0);
}
guint32 janus_rtcp_get_sender_ssrc(char *packet, int len) {
if(packet == NULL || len == 0)
return 0;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return 0;
int pno = 0, total = len;
while(rtcp) {
pno++;
switch(rtcp->type) {
case RTCP_SR: {
/* SR, sender report */
rtcp_sr *sr = (rtcp_sr*)rtcp;
return ntohl(sr->ssrc);
}
case RTCP_RR: {
/* RR, receiver report */
rtcp_rr *rr = (rtcp_rr*)rtcp;
return ntohl(rr->ssrc);
}
case RTCP_RTPFB: {
/* RTPFB, Transport layer FB message (rfc4585) */
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
return ntohl(rtcpfb->ssrc);
}
case RTCP_PSFB: {
/* PSFB, Payload-specific FB message (rfc4585) */
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
return ntohl(rtcpfb->ssrc);
}
default:
break;
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0) {
break;
}
total -= length*4+4;
if(total <= 0) {
break;
}
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return 0;
}
guint32 janus_rtcp_get_receiver_ssrc(char *packet, int len) {
if(packet == NULL || len == 0)
return 0;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return 0;
int pno = 0, total = len;
while(rtcp) {
pno++;
switch(rtcp->type) {
case RTCP_SR: {
/* SR, sender report */
rtcp_sr *sr = (rtcp_sr*)rtcp;
if(sr->header.rc > 0) {
return ntohl(sr->rb[0].ssrc);
}
break;
}
case RTCP_RR: {
/* RR, receiver report */
rtcp_rr *rr = (rtcp_rr*)rtcp;
if(rr->header.rc > 0) {
return ntohl(rr->rb[0].ssrc);
}
break;
}
default:
break;
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0) {
break;
}
total -= length*4+4;
if(total <= 0) {
break;
}
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return 0;
}
/* Helper to handle an incoming SR: triggered by a call to janus_rtcp_fix_ssrc with fixssrc=0 */
static void janus_rtcp_incoming_sr(rtcp_context *ctx, rtcp_sr *sr) {
if(ctx == NULL)
return;
/* Update the context with info on the monotonic time of last SR received */
ctx->lsr_ts = ps_get_monotonic_time();
/* Compute the last SR received as well */
uint64_t ntp = ntohl(sr->si.ntp_ts_msw);
ntp = (ntp << 32) | ntohl(sr->si.ntp_ts_lsw);
ctx->lsr = (ntp >> 16);
}
int janus_rtcp_fix_ssrc(rtcp_context *ctx, char *packet, int len, int fixssrc, uint32_t newssrcl, uint32_t newssrcr) {
if(packet == NULL || len <= 0)
return -1;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return -2;
int pno = 0, total = len;
PS_LOG(LOG_HUGE, " Parsing compound packet (total of %d bytes)\n", total);
while(rtcp) {
pno++;
/* TODO Should we handle any of these packets ourselves, or just relay them? */
switch(rtcp->type) {
case RTCP_SR: {
/* SR, sender report */
PS_LOG(LOG_HUGE, " #%d SR (200)\n", pno);
rtcp_sr *sr = (rtcp_sr*)rtcp;
if(ctx != NULL) {
/* RTCP context provided, update it with info on this SR */
janus_rtcp_incoming_sr(ctx, sr);
}
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u (%u in RB)\n", ntohl(sr->ssrc), report_block_get_ssrc(&sr->rb[0]));
//~ PS_LOG(LOG_HUGE, " -- Lost: %u/%u\n", report_block_get_fraction_lost(&sr->rb[0]), report_block_get_cum_packet_loss(&sr->rb[0]));
if(fixssrc && newssrcl) {
sr->ssrc = htonl(newssrcl);
}
if(fixssrc && newssrcr && sr->header.rc > 0) {
sr->rb[0].ssrc = htonl(newssrcr);
}
break;
}
case RTCP_RR: {
/* RR, receiver report */
PS_LOG(LOG_HUGE, " #%d RR (201)\n", pno);
rtcp_rr *rr = (rtcp_rr*)rtcp;
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u (%u in RB)\n", ntohl(rr->ssrc), report_block_get_ssrc(&rr->rb[0]));
//~ PS_LOG(LOG_HUGE, " -- Lost: %u/%u\n", report_block_get_fraction_lost(&rr->rb[0]), report_block_get_cum_packet_loss(&rr->rb[0]));
if(fixssrc && newssrcl) {
rr->ssrc = htonl(newssrcl);
}
if(fixssrc && newssrcr && rr->header.rc > 0) {
rr->rb[0].ssrc = htonl(newssrcr);
}
break;
}
case RTCP_SDES: {
/* SDES, source description */
PS_LOG(LOG_HUGE, " #%d SDES (202)\n", pno);
rtcp_sdes *sdes = (rtcp_sdes*)rtcp;
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u\n", ntohl(sdes->chunk.ssrc));
if(fixssrc && newssrcl) {
sdes->chunk.ssrc = htonl(newssrcl);
}
break;
}
case RTCP_BYE: {
/* BYE, goodbye */
PS_LOG(LOG_HUGE, " #%d BYE (203)\n", pno);
rtcp_bye_t *bye = (rtcp_bye_t*)rtcp;
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u\n", ntohl(bye->ssrc[0]));
if(fixssrc && newssrcl) {
bye->ssrc[0] = htonl(newssrcl);
}
break;
}
case RTCP_APP: {
/* APP, application-defined */
PS_LOG(LOG_HUGE, " #%d APP (204)\n", pno);
rtcp_app_t *app = (rtcp_app_t*)rtcp;
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u\n", ntohl(app->ssrc));
if(fixssrc && newssrcl) {
app->ssrc = htonl(newssrcl);
}
break;
}
case RTCP_FIR: {
/* FIR, rfc2032 */
PS_LOG(LOG_HUGE, " #%d FIR (192)\n", pno);
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
if(fixssrc && newssrcr && (ntohs(rtcp->length) >= 20)) {
rtcpfb->media = htonl(newssrcr);
}
if(fixssrc && newssrcr) {
uint32_t *ssrc = (uint32_t *)rtcpfb->fci;
*ssrc = htonl(newssrcr);
}
break;
}
case RTCP_RTPFB: {
/* RTPFB, Transport layer FB message (rfc4585) */
//~ PS_LOG(LOG_HUGE, " #%d RTPFB (205)\n", pno);
gint fmt = rtcp->rc;
//~ PS_LOG(LOG_HUGE, " -- FMT: %u\n", fmt);
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u\n", ntohl(rtcpfb->ssrc));
if(fmt == 1) {
PS_LOG(LOG_HUGE, " #%d NACK -- RTPFB (205)\n", pno);
if(fixssrc && newssrcr) {
rtcpfb->media = htonl(newssrcr);
}
int nacks = ntohs(rtcp->length)-2; /* Skip SSRCs */
if(nacks > 0) {
PS_LOG(LOG_DBG, " Got %d nacks\n", nacks);
rtcp_nack *nack = NULL;
uint16_t pid = 0;
uint16_t blp = 0;
int i=0, j=0;
char bitmask[20];
for(i=0; i< nacks; i++) {
nack = (rtcp_nack *)rtcpfb->fci + i;
pid = ntohs(nack->pid);
blp = ntohs(nack->blp);
memset(bitmask, 0, 20);
for(j=0; j<16; j++) {
bitmask[j] = (blp & ( 1 << j )) >> j ? '1' : '0';
}
bitmask[16] = '\n';
PS_LOG(LOG_DBG, "[%d] %"SCNu16" / %s\n", i, pid, bitmask);
}
}
} else if(fmt == 3) { /* rfc5104 */
/* TMMBR: http://tools.ietf.org/html/rfc5104#section-4.2.1.1 */
PS_LOG(LOG_HUGE, " #%d TMMBR -- RTPFB (205)\n", pno);
if(fixssrc && newssrcr) {
uint32_t *ssrc = (uint32_t *)rtcpfb->fci;
*ssrc = htonl(newssrcr);
}
} else {
PS_LOG(LOG_HUGE, " #%d ??? -- RTPFB (205, fmt=%d)\n", pno, fmt);
}
if(fixssrc && newssrcl) {
rtcpfb->ssrc = htonl(newssrcl);
}
break;
}
case RTCP_PSFB: {
/* PSFB, Payload-specific FB message (rfc4585) */
//~ PS_LOG(LOG_HUGE, " #%d PSFB (206)\n", pno);
gint fmt = rtcp->rc;
//~ PS_LOG(LOG_HUGE, " -- FMT: %u\n", fmt);
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
//~ PS_LOG(LOG_HUGE, " -- SSRC: %u\n", ntohl(rtcpfb->ssrc));
if(fmt == 1) {
PS_LOG(LOG_HUGE, " #%d PLI -- PSFB (206)\n", pno);
if(fixssrc && newssrcr) {
rtcpfb->media = htonl(newssrcr);
}
} else if(fmt == 2) {
PS_LOG(LOG_HUGE, " #%d SLI -- PSFB (206)\n", pno);
} else if(fmt == 3) {
PS_LOG(LOG_HUGE, " #%d RPSI -- PSFB (206)\n", pno);
} else if(fmt == 4) { /* rfc5104 */
/* FIR: http://tools.ietf.org/html/rfc5104#section-4.3.1.1 */
PS_LOG(LOG_HUGE, " #%d FIR -- PSFB (206)\n", pno);
if(fixssrc && newssrcr) {
rtcpfb->media = htonl(newssrcr);
}
if(fixssrc && newssrcr) {
uint32_t *ssrc = (uint32_t *)rtcpfb->fci;
*ssrc = htonl(newssrcr);
}
} else if(fmt == 5) { /* rfc5104 */
/* FIR: http://tools.ietf.org/html/rfc5104#section-4.3.2.1 */
PS_LOG(LOG_HUGE, " #%d PLI -- TSTR (206)\n", pno);
if(fixssrc && newssrcr) {
uint32_t *ssrc = (uint32_t *)rtcpfb->fci;
*ssrc = htonl(newssrcr);
}
} else if(fmt == 15) {
//~ PS_LOG(LOG_HUGE, " -- This is a AFB!\n");
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_remb *remb = (rtcp_remb *)rtcpfb->fci;
if(remb->id[0] == 'R' && remb->id[1] == 'E' && remb->id[2] == 'M' && remb->id[3] == 'B') {
PS_LOG(LOG_HUGE, " #%d REMB -- PSFB (206)\n", pno);
if(fixssrc && newssrcr) {
remb->ssrc[0] = htonl(newssrcr);
}
/* FIXME From rtcp_utility.cc */
unsigned char *_ptrRTCPData = (unsigned char *)remb;
_ptrRTCPData += 4; // Skip unique identifier and num ssrc
//~ PS_LOG(LOG_HUGE, " %02X %02X %02X %02X\n", _ptrRTCPData[0], _ptrRTCPData[1], _ptrRTCPData[2], _ptrRTCPData[3]);
uint8_t numssrc = (_ptrRTCPData[0]);
uint8_t brExp = (_ptrRTCPData[1] >> 2) & 0x3F;
uint32_t brMantissa = (_ptrRTCPData[1] & 0x03) << 16;
brMantissa += (_ptrRTCPData[2] << 8);
brMantissa += (_ptrRTCPData[3]);
uint64_t bitRate = brMantissa << brExp;
PS_LOG(LOG_HUGE, " -- -- -- REMB: %u * 2^%u = %"SCNu64" (%d SSRCs, %u)\n",
brMantissa, brExp, bitRate, numssrc, ntohl(remb->ssrc[0]));
} else {
PS_LOG(LOG_HUGE, " #%d AFB ?? -- PSFB (206)\n", pno);
}
} else {
PS_LOG(LOG_HUGE, " #%d ?? -- PSFB (206, fmt=%d)\n", pno, fmt);
}
if(fixssrc && newssrcl) {
rtcpfb->ssrc = htonl(newssrcl);
}
break;
}
default:
PS_LOG(LOG_ERR, " Unknown RTCP PT %d\n", rtcp->type);
break;
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
PS_LOG(LOG_HUGE, " RTCP PT length: %d bytes\n", length*4+4);
if(length == 0) {
//~ PS_LOG(LOG_HUGE, " 0-length, end of compound packet\n");
break;
}
total -= length*4+4;
//~ PS_LOG(LOG_HUGE, " Packet has length %d (%d bytes, %d remaining), moving to next one...\n", length, length*4+4, total);
if(total <= 0) {
PS_LOG(LOG_HUGE, " End of compound packet\n");
break;
}
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return 0;
}
char *janus_rtcp_filter(char *packet, int len, int *newlen) {
if(packet == NULL || len <= 0 || newlen == NULL)
return NULL;
*newlen = 0;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return NULL;
char *filtered = NULL;
int total = len, length = 0, bytes = 0;
/* Iterate on the compound packets */
gboolean keep = TRUE;
while(rtcp) {
keep = TRUE;
length = ntohs(rtcp->length);
if(length == 0)
break;
bytes = length*4+4;
switch(rtcp->type) {
case RTCP_SR:
case RTCP_RR:
case RTCP_SDES:
/* These are packets we generate ourselves, so remove them */
keep = FALSE;
break;
case RTCP_BYE:
case RTCP_APP:
case RTCP_FIR:
case RTCP_PSFB:
break;
case RTCP_RTPFB:
if(rtcp->rc == 1) {
/* We handle NACKs ourselves as well, remove this too */
keep = FALSE;
break;
}
break;
default:
PS_LOG(LOG_ERR, "Unknown RTCP PT %d\n", rtcp->type);
/* FIXME Should we allow this to go through instead? */
keep = FALSE;
break;
}
if(keep) {
/* Keep this packet */
if(filtered == NULL)
filtered = g_malloc0(total);
memcpy(filtered+*newlen, (char *)rtcp, bytes);
*newlen += bytes;
}
total -= bytes;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return filtered;
}
int janus_rtcp_process_incoming_rtp(rtcp_context *ctx, char *packet, int len, int max_nack_queue) {
if(ctx == NULL || packet == NULL || len < 1)
return -1;
/* RTP packet received: it means we can start sending RR */
ctx->enabled = 1;
/* Parse this RTP packet header and update the rtcp_context instance */
rtp_header *rtp = (rtp_header *)packet;
uint16_t seq_number = ntohs(rtp->seq_number);
if(ctx->base_seq == 0 && ctx->seq_cycle == 0)
ctx->base_seq = seq_number;
if(max_nack_queue + seq_number < ctx->last_seq_nr)
ctx->seq_cycle++;
ctx->last_seq_nr = seq_number;
ctx->received++;
uint32_t rtp_expected = 0x0;
if(ctx->seq_cycle > 0) {
rtp_expected = ctx->seq_cycle;
rtp_expected = rtp_expected << 16;
}
rtp_expected = rtp_expected + 1 + seq_number - ctx->base_seq;
ctx->lost = rtp_expected - ctx->received;
ctx->expected = rtp_expected;
uint64_t arrival = (ps_get_monotonic_time() * ctx->tb) / 1000000;
uint64_t transit = arrival - ntohl(rtp->timestamp);
uint64_t d = abs(transit - ctx->transit);
ctx->transit = transit;
ctx->jitter += (1./16.) * ((double)d - ctx->jitter);
return 0;
}
uint32_t janus_rtcp_context_get_lsr(rtcp_context *ctx) {
return ctx ? ctx->lsr : 0;
}
uint32_t janus_rtcp_context_get_received(rtcp_context *ctx) {
return ctx ? ctx->received : 0;
}
uint32_t janus_rtcp_context_get_lost(rtcp_context *ctx) {
if(ctx == NULL)
return 0;
uint32_t lost;
if(ctx->lost > 0x7FFFFF) {
lost = 0x7FFFFF;
} else {
lost = ctx->lost;
}
return lost;
}
uint32_t janus_rtcp_context_get_lost_fraction(rtcp_context *ctx) {
if(ctx == NULL)
return 0;
uint32_t expected_interval = ctx->expected - ctx->expected_prior;
uint32_t received_interval = ctx->received - ctx->received_prior;
int32_t lost_interval = expected_interval - received_interval;
uint32_t fraction;
if(expected_interval == 0 || lost_interval <=0)
fraction = 0;
else
fraction = (lost_interval << 8) / expected_interval;
return fraction << 24;
}
uint32_t janus_rtcp_context_get_lost_promille(rtcp_context *ctx) {
if(ctx == NULL)
return 0;
uint32_t expected_interval = ctx->expected - ctx->expected_prior;
uint32_t received_interval = ctx->received - ctx->received_prior;
int32_t lost_interval = expected_interval - received_interval;
uint32_t fraction;
if(expected_interval == 0 || lost_interval <=0)
fraction = 0;
else
fraction = (lost_interval << 8) / expected_interval;
fraction = fraction << 24;
return ((fraction>>24) * 1000) >> 8;
}
uint32_t janus_rtcp_context_get_jitter(rtcp_context *ctx) {
if(ctx == NULL || ctx->tb == 0)
return 0;
return (uint32_t) floor(ctx->jitter * 1000.0 / ctx->tb);
}
int janus_rtcp_report_block(rtcp_context *ctx, report_block *rb) {
if(ctx == NULL || rb == NULL)
return -1;
rb->jitter = htonl((uint32_t) ctx->jitter);
rb->ehsnr = htonl((((uint32_t) 0x0 + ctx->seq_cycle) << 16) + ctx->last_seq_nr);
uint32_t lost = janus_rtcp_context_get_lost(ctx);
uint32_t fraction = janus_rtcp_context_get_lost_fraction(ctx);
ctx->expected_prior = ctx->expected;
ctx->received_prior = ctx->received;
rb->flcnpl = htonl(lost | fraction);
rb->lsr = htonl(ctx->lsr);
rb->delay = htonl(((ps_get_monotonic_time() - ctx->lsr_ts) << 16) / 1000000);
ctx->last_sent = ps_get_monotonic_time();
return 0;
}
int janus_rtcp_has_fir(char *packet, int len) {
gboolean got_fir = FALSE;
/* Parse RTCP compound packet */
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return FALSE;
int pno = 0, total = len;
while(rtcp) {
pno++;
switch(rtcp->type) {
case RTCP_FIR:
got_fir = TRUE;
break;
default:
break;
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0)
break;
total -= length*4+4;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return got_fir ? TRUE : FALSE;
}
int janus_rtcp_has_pli(char *packet, int len) {
gboolean got_pli = FALSE;
/* Parse RTCP compound packet */
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return FALSE;
int pno = 0, total = len;
while(rtcp) {
pno++;
switch(rtcp->type) {
case RTCP_PSFB: {
gint fmt = rtcp->rc;
if(fmt == 1)
got_pli = TRUE;
break;
}
default:
break;
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0)
break;
total -= length*4+4;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return got_pli ? TRUE : FALSE;
}
GSList *janus_rtcp_get_nacks(char *packet, int len) {
if(packet == NULL || len == 0)
return NULL;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return NULL;
/* FIXME Get list of sequence numbers we should send again */
GSList *list = NULL;
int total = len;
while(rtcp) {
if(rtcp->type == RTCP_RTPFB) {
gint fmt = rtcp->rc;
if(fmt == 1) {
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
int nacks = ntohs(rtcp->length)-2; /* Skip SSRCs */
if(nacks > 0) {
PS_LOG(LOG_DBG, " Got %d nacks\n", nacks);
rtcp_nack *nack = NULL;
uint16_t pid = 0;
uint16_t blp = 0;
int i=0, j=0;
char bitmask[20];
for(i=0; i< nacks; i++) {
nack = (rtcp_nack *)rtcpfb->fci + i;
pid = ntohs(nack->pid);
list = g_slist_append(list, GUINT_TO_POINTER(pid));
blp = ntohs(nack->blp);
memset(bitmask, 0, 20);
for(j=0; j<16; j++) {
bitmask[j] = (blp & ( 1 << j )) >> j ? '1' : '0';
if((blp & ( 1 << j )) >> j)
list = g_slist_append(list, GUINT_TO_POINTER(pid+j+1));
}
bitmask[16] = '\n';
PS_LOG(LOG_DBG, "[%d] %"SCNu16" / %s\n", i, pid, bitmask);
}
}
}
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0)
break;
total -= length*4+4;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return list;
}
int janus_rtcp_remove_nacks(char *packet, int len) {
if(packet == NULL || len == 0)
return len;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return len;
/* Find the NACK message */
char *nacks = NULL;
int total = len, nacks_len = 0;
while(rtcp) {
if(rtcp->type == RTCP_RTPFB) {
gint fmt = rtcp->rc;
if(fmt == 1) {
nacks = (char *)rtcp;
}
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0)
break;
if(nacks != NULL) {
nacks_len = length*4+4;
break;
}
total -= length*4+4;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
if(nacks != NULL) {
total = len - ((nacks-packet)+nacks_len);
if(total < 0) {
/* FIXME Should never happen, but you never know: do nothing */
return len;
} else if(total == 0) {
/* NACK was the last compound packet, easy enough */
return len-nacks_len;
} else {
/* NACK is between two compound packets, move them around */
int i=0;
for(i=0; i<total; i++)
*(nacks+i) = *(nacks+nacks_len+i);
return len-nacks_len;
}
}
return len;
}
/* Query an existing REMB message */
uint64_t janus_rtcp_get_remb(char *packet, int len) {
if(packet == NULL || len == 0)
return 0;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return 0;
/* Get REMB bitrate, if any */
int total = len;
while(rtcp) {
if(rtcp->type == RTCP_PSFB) {
gint fmt = rtcp->rc;
if(fmt == 15) {
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_remb *remb = (rtcp_remb *)rtcpfb->fci;
if(remb->id[0] == 'R' && remb->id[1] == 'E' && remb->id[2] == 'M' && remb->id[3] == 'B') {
/* FIXME From rtcp_utility.cc */
unsigned char *_ptrRTCPData = (unsigned char *)remb;
_ptrRTCPData += 4; /* Skip unique identifier and num ssrc */
//~ PS_LOG(LOG_VERB, " %02X %02X %02X %02X\n", _ptrRTCPData[0], _ptrRTCPData[1], _ptrRTCPData[2], _ptrRTCPData[3]);
uint8_t brExp = (_ptrRTCPData[1] >> 2) & 0x3F;
uint32_t brMantissa = (_ptrRTCPData[1] & 0x03) << 16;
brMantissa += (_ptrRTCPData[2] << 8);
brMantissa += (_ptrRTCPData[3]);
uint64_t bitrate = brMantissa << brExp;
PS_LOG(LOG_HUGE, "Got REMB bitrate %"SCNu64"\n", bitrate);
return bitrate;
}
}
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0)
break;
total -= length*4+4;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return 0;
}
/* Change an existing REMB message */
int janus_rtcp_cap_remb(char *packet, int len, uint64_t bitrate) {
if(packet == NULL || len == 0)
return -1;
rtcp_header *rtcp = (rtcp_header *)packet;
if(rtcp->version != 2)
return -2;
if(bitrate == 0)
return 0; /* No need to cap */
/* Cap REMB bitrate */
int total = len;
while(rtcp) {
if(rtcp->type == RTCP_PSFB) {
gint fmt = rtcp->rc;
if(fmt == 15) {
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_remb *remb = (rtcp_remb *)rtcpfb->fci;
if(remb->id[0] == 'R' && remb->id[1] == 'E' && remb->id[2] == 'M' && remb->id[3] == 'B') {
/* FIXME From rtcp_utility.cc */
unsigned char *_ptrRTCPData = (unsigned char *)remb;
_ptrRTCPData += 4; /* Skip unique identifier and num ssrc */
//~ PS_LOG(LOG_VERB, " %02X %02X %02X %02X\n", _ptrRTCPData[0], _ptrRTCPData[1], _ptrRTCPData[2], _ptrRTCPData[3]);
uint8_t brExp = (_ptrRTCPData[1] >> 2) & 0x3F;
uint32_t brMantissa = (_ptrRTCPData[1] & 0x03) << 16;
brMantissa += (_ptrRTCPData[2] << 8);
brMantissa += (_ptrRTCPData[3]);
uint64_t origbitrate = brMantissa << brExp;
if(origbitrate > bitrate) {
PS_LOG(LOG_HUGE, "Got REMB bitrate %"SCNu64", need to cap it to %"SCNu64"\n", origbitrate, bitrate);
PS_LOG(LOG_HUGE, " >> %u * 2^%u = %"SCNu64"\n", brMantissa, brExp, origbitrate);
/* bitrate --> brexp/brmantissa */
uint8_t b = 0;
uint8_t newbrexp = 0;
uint32_t newbrmantissa = 0;
for(b=0; b<64; b++) {
if(bitrate <= ((uint64_t) 0x3FFFF << b)) {
newbrexp = b;
break;
}
}
newbrmantissa = bitrate >> b;
PS_LOG(LOG_HUGE, "new brexp: %"SCNu8"\n", newbrexp);
PS_LOG(LOG_HUGE, "new brmantissa: %"SCNu32"\n", newbrmantissa);
/* FIXME From rtcp_sender.cc */
_ptrRTCPData[1] = (uint8_t)((newbrexp << 2) + ((newbrmantissa >> 16) & 0x03));
_ptrRTCPData[2] = (uint8_t)(newbrmantissa >> 8);
_ptrRTCPData[3] = (uint8_t)(newbrmantissa);
}
}
}
}
/* Is this a compound packet? */
int length = ntohs(rtcp->length);
if(length == 0)
break;
total -= length*4+4;
if(total <= 0)
break;
rtcp = (rtcp_header *)((uint32_t*)rtcp + length + 1);
}
return 0;
}
/* Generate a new SDES message */
int janus_rtcp_sdes(char *packet, int len, const char *cname, int cnamelen) {
if(packet == NULL || len <= 0 || cname == NULL || cnamelen <= 0)
return -1;
memset(packet, 0, len);
rtcp_header *rtcp = (rtcp_header *)packet;
/* Set header */
rtcp->version = 2;
rtcp->type = RTCP_SDES;
rtcp->rc = 1;
int plen = 8; /* Header + chunk + item header */
plen += cnamelen+2;
if((cnamelen+2)%4) /* Account for padding */
plen += 4;
if(len < plen) {
PS_LOG(LOG_ERR, "Buffer too small for SDES message: %d < %d\n", len, plen);
return -1;
}
rtcp->length = htons((plen/4)-1);
/* Now set SDES stuff */
rtcp_sdes *rtcpsdes = (rtcp_sdes *)rtcp;
rtcpsdes->item.type = 1;
rtcpsdes->item.len = cnamelen;
memcpy(rtcpsdes->item.content, cname, cnamelen);
return plen;
}
/* Generate a new REMB message */
int janus_rtcp_remb(char *packet, int len, uint64_t bitrate) {
if(packet == NULL || len != 24)
return -1;
rtcp_header *rtcp = (rtcp_header *)packet;
/* Set header */
rtcp->version = 2;
rtcp->type = RTCP_PSFB;
rtcp->rc = 15;
rtcp->length = htons((len/4)-1);
/* Now set REMB stuff */
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_remb *remb = (rtcp_remb *)rtcpfb->fci;
remb->id[0] = 'R';
remb->id[1] = 'E';
remb->id[2] = 'M';
remb->id[3] = 'B';
/* bitrate --> brexp/brmantissa */
uint8_t b = 0;
uint8_t newbrexp = 0;
uint32_t newbrmantissa = 0;
for(b=0; b<64; b++) {
if(bitrate <= ((uint64_t) 0x3FFFF << b)) {
newbrexp = b;
break;
}
}
newbrmantissa = bitrate >> b;
/* FIXME From rtcp_sender.cc */
unsigned char *_ptrRTCPData = (unsigned char *)remb;
_ptrRTCPData += 4; /* Skip unique identifier */
_ptrRTCPData[0] = (uint8_t)(1); /* Just one SSRC */
_ptrRTCPData[1] = (uint8_t)((newbrexp << 2) + ((newbrmantissa >> 16) & 0x03));
_ptrRTCPData[2] = (uint8_t)(newbrmantissa >> 8);
_ptrRTCPData[3] = (uint8_t)(newbrmantissa);
PS_LOG(LOG_HUGE, "[REMB] bitrate=%"SCNu64" (%d bytes)\n", bitrate, 4*(ntohs(rtcp->length)+1));
return 24;
}
/* Generate a new FIR message */
int janus_rtcp_fir(char *packet, int len, int *seqnr) {
if(packet == NULL || len != 20 || seqnr == NULL)
return -1;
rtcp_header *rtcp = (rtcp_header *)packet;
*seqnr = *seqnr + 1;
if(*seqnr < 0 || *seqnr >= 256)
*seqnr = 0; /* Reset sequence number */
/* Set header */
rtcp->version = 2;
rtcp->type = RTCP_PSFB;
rtcp->rc = 4; /* FMT=4 */
rtcp->length = htons((len/4)-1);
/* Now set FIR stuff */
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_fir *fir = (rtcp_fir *)rtcpfb->fci;
fir->seqnr = htonl(*seqnr << 24); /* FCI: Sequence number */
PS_LOG(LOG_HUGE, "[FIR] seqnr=%d (%d bytes)\n", *seqnr, 4*(ntohs(rtcp->length)+1));
return 20;
}
/* Generate a new legacy FIR message */
int janus_rtcp_fir_legacy(char *packet, int len, int *seqnr) {
/* FIXME Right now, this is identical to the new FIR, with the difference that we use 192 as PT */
if(packet == NULL || len != 20 || seqnr == NULL)
return -1;
rtcp_header *rtcp = (rtcp_header *)packet;
*seqnr = *seqnr + 1;
if(*seqnr < 0 || *seqnr >= 256)
*seqnr = 0; /* Reset sequence number */
/* Set header */
rtcp->version = 2;
rtcp->type = RTCP_FIR;
rtcp->rc = 4; /* FMT=4 */
rtcp->length = htons((len/4)-1);
/* Now set FIR stuff */
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_fir *fir = (rtcp_fir *)rtcpfb->fci;
fir->seqnr = htonl(*seqnr << 24); /* FCI: Sequence number */
PS_LOG(LOG_HUGE, "[FIR] seqnr=%d (%d bytes)\n", *seqnr, 4*(ntohs(rtcp->length)+1));
return 20;
}
/* Generate a new PLI message */
int janus_rtcp_pli(char *packet, int len) {
if(packet == NULL || len != 12)
return -1;
rtcp_header *rtcp = (rtcp_header *)packet;
/* Set header */
rtcp->version = 2;
rtcp->type = RTCP_PSFB;
rtcp->rc = 1; /* FMT=1 */
rtcp->length = htons((len/4)-1);
return 12;
}
/* Generate a new NACK message */
int janus_rtcp_nacks(char *packet, int len, GSList *nacks) {
if(packet == NULL || len < 16 || nacks == NULL)
return -1;
memset(packet, 0, len);
rtcp_header *rtcp = (rtcp_header *)packet;
/* Set header */
rtcp->version = 2;
rtcp->type = RTCP_RTPFB;
rtcp->rc = 1; /* FMT=1 */
/* Now set NACK stuff */
rtcp_fb *rtcpfb = (rtcp_fb *)rtcp;
rtcp_nack *nack = (rtcp_nack *)rtcpfb->fci;
/* FIXME We assume the GSList list is already ordered... */
guint16 pid = GPOINTER_TO_UINT(nacks->data);
nack->pid = htons(pid);
nacks = nacks->next;
int words = 3;
while(nacks) {
guint16 npid = GPOINTER_TO_UINT(nacks->data);
if(npid-pid < 1) {
PS_LOG(LOG_HUGE, "Skipping PID to NACK (%"SCNu16" already added)...\n", npid);
} else if(npid-pid > 16) {
/* We need a new block: this sequence number will be its root PID */
PS_LOG(LOG_HUGE, "Adding another block of NACKs (%"SCNu16"-%"SCNu16" > %"SCNu16")...\n", npid, pid, npid-pid);
words++;
if(len < (words*4+4)) {
PS_LOG(LOG_ERR, "Buffer too small: %d < %d (at least %d NACK blocks needed)\n", len, words*4+4, words);
return -1;
}
char *new_block = packet + words*4;
nack = (rtcp_nack *)new_block;
pid = GPOINTER_TO_UINT(nacks->data);
nack->pid = htons(pid);
} else {
uint16_t blp = ntohs(nack->blp);
blp |= 1 << (npid-pid-1);
nack->blp = htons(blp);
}
nacks = nacks->next;
}
rtcp->length = htons(words);
return words*4+4;
}