forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdp.c
1447 lines (1430 loc) · 58.9 KB
/
sdp.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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file sdp.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief SDP processing
* \details Implementation of an SDP
* parser/merger/generator in the server. Each SDP coming from peers is
* stripped/anonymized before it is passed to the plugins: all
* DTLS/ICE/transport related information is removed, only leaving the
* relevant information in place. SDP coming from plugins is stripped/anonymized
* as well, and merged with the proper DTLS/ICE/transport information before
* it is sent to the peers. The actual SDP processing (parsing SDP strings,
* representation of SDP as an internal format, and so on) is done via
* the tools provided in sdp-utils.h.
*
* \ingroup protocols
* \ref protocols
*/
#include <netdb.h>
#include "janus.h"
#include "ice.h"
#include "sdp.h"
#include "utils.h"
#include "ip-utils.h"
#include "debug.h"
#include "events.h"
/* Pre-parse SDP: is this SDP valid? how many audio/video lines? any features to take into account? */
janus_sdp *janus_sdp_preparse(void *ice_handle, const char *jsep_sdp, char *error_str, size_t errlen,
int *audio, int *video, int *data) {
if(!ice_handle || !jsep_sdp || !audio || !video || !data) {
JANUS_LOG(LOG_ERR, " Can't preparse, invalid arguments\n");
return NULL;
}
janus_ice_handle *handle = (janus_ice_handle *)ice_handle;
janus_sdp *parsed_sdp = janus_sdp_parse(jsep_sdp, error_str, errlen);
if(!parsed_sdp) {
JANUS_LOG(LOG_ERR, " Error parsing SDP? %s\n", error_str ? error_str : "(unknown reason)");
/* Invalid SDP */
return NULL;
}
/* Look for m-lines */
GList *temp = parsed_sdp->m_lines;
while(temp) {
janus_sdp_mline *m = (janus_sdp_mline *)temp->data;
if(m->type == JANUS_SDP_AUDIO && m->port > 0) {
*audio = *audio + 1;
} else if(m->type == JANUS_SDP_VIDEO && m->port > 0) {
*video = *video + 1;
}
/* Preparse the mid as well */
GList *tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name) {
if(!strcasecmp(a->name, "mid")) {
/* Found mid attribute */
if(m->type == JANUS_SDP_AUDIO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Audio mid: %s\n", handle->handle_id, a->value);
if(handle->audio_mid == NULL)
handle->audio_mid = g_strdup(a->value);
if(handle->stream_mid == NULL)
handle->stream_mid = handle->audio_mid;
} else if(m->type == JANUS_SDP_VIDEO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Video mid: %s\n", handle->handle_id, a->value);
if(handle->video_mid == NULL)
handle->video_mid = g_strdup(a->value);
if(handle->stream_mid == NULL)
handle->stream_mid = handle->video_mid;
}
}
}
tempA = tempA->next;
}
temp = temp->next;
}
#ifdef HAVE_SCTP
*data = (strstr(jsep_sdp, "DTLS/SCTP") && !strstr(jsep_sdp, " 0 DTLS/SCTP") &&
!strstr(jsep_sdp, " 0 UDP/DTLS/SCTP")) ? 1 : 0; /* FIXME This is a really hacky way of checking... */
#else
*data = 0;
#endif
return parsed_sdp;
}
/* Parse SDP */
int janus_sdp_process(void *ice_handle, janus_sdp *remote_sdp, gboolean update) {
if(!ice_handle || !remote_sdp)
return -1;
janus_ice_handle *handle = (janus_ice_handle *)ice_handle;
janus_ice_stream *stream = handle->stream;
if(!stream)
return -1;
gchar *ruser = NULL, *rpass = NULL, *rhashing = NULL, *rfingerprint = NULL;
int audio = 0, video = 0;
#ifdef HAVE_SCTP
int data = 0;
#endif
gboolean rtx = FALSE;
/* Ok, let's start with global attributes */
GList *temp = remote_sdp->attributes;
while(temp) {
janus_sdp_attribute *a = (janus_sdp_attribute *)temp->data;
if(a && a->name) {
if(!strcasecmp(a->name, "fingerprint")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Fingerprint (global) : %s\n", handle->handle_id, a->value);
if(strcasestr(a->value, "sha-256 ") == a->value) {
rhashing = g_strdup("sha-256");
rfingerprint = g_strdup(a->value + strlen("sha-256 "));
} else if(strcasestr(a->value, "sha-1 ") == a->value) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Hashing algorithm not the one we expected (sha-1 instead of sha-256), but that's ok\n", handle->handle_id);
rhashing = g_strdup("sha-1");
rfingerprint = g_strdup(a->value + strlen("sha-1 "));
} else {
/* FIXME We should handle this somehow anyway... OpenSSL supports them all */
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Hashing algorithm not the one we expected (sha-256/sha-1), *NOT* cool\n", handle->handle_id);
}
} else if(!strcasecmp(a->name, "ice-ufrag")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] ICE ufrag (global): %s\n", handle->handle_id, a->value);
ruser = g_strdup(a->value);
} else if(!strcasecmp(a->name, "ice-pwd")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] ICE pwd (global): %s\n", handle->handle_id, a->value);
rpass = g_strdup(a->value);
}
}
temp = temp->next;
}
/* Now go on with m-line and their attributes */
int mlines = 0;
temp = remote_sdp->m_lines;
while(temp) {
mlines++;
janus_sdp_mline *m = (janus_sdp_mline *)temp->data;
if(m->type == JANUS_SDP_AUDIO) {
if(handle->rtp_profile == NULL && m->proto != NULL)
handle->rtp_profile = g_strdup(m->proto);
audio++;
if(audio > 1) {
temp = temp->next;
continue;
}
if(m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Parsing audio candidates (stream=%d)...\n", handle->handle_id, stream->stream_id);
if(!janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_AUDIO)) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_AUDIO);
stream->audio_ssrc = janus_random_uint32(); /* FIXME Should we look for conflicts? */
if(stream->audio_rtcp_ctx == NULL) {
stream->audio_rtcp_ctx = g_malloc0(sizeof(rtcp_context));
stream->audio_rtcp_ctx->tb = 48000; /* May change later */
}
}
switch(m->direction) {
case JANUS_SDP_INACTIVE:
case JANUS_SDP_INVALID:
stream->audio_send = FALSE;
stream->audio_recv = FALSE;
break;
case JANUS_SDP_SENDONLY:
/* A sendonly peer means recvonly for Janus */
stream->audio_send = FALSE;
stream->audio_recv = TRUE;
break;
case JANUS_SDP_RECVONLY:
/* A recvonly peer means sendonly for Janus */
stream->audio_send = TRUE;
stream->audio_recv = FALSE;
break;
case JANUS_SDP_SENDRECV:
case JANUS_SDP_DEFAULT:
default:
stream->audio_send = TRUE;
stream->audio_recv = TRUE;
break;
}
if(m->ptypes != NULL) {
g_list_free(stream->audio_payload_types);
stream->audio_payload_types = g_list_copy(m->ptypes);
}
} else {
/* Audio rejected? */
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_AUDIO);
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Audio rejected by peer...\n", handle->handle_id);
}
} else if(m->type == JANUS_SDP_VIDEO) {
if(handle->rtp_profile == NULL && m->proto != NULL)
handle->rtp_profile = g_strdup(m->proto);
video++;
if(video > 1) {
temp = temp->next;
continue;
}
if(m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Parsing video candidates (stream=%d)...\n", handle->handle_id, stream->stream_id);
if(!janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_VIDEO)) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_VIDEO);
stream->video_ssrc = janus_random_uint32(); /* FIXME Should we look for conflicts? */
if(stream->video_rtcp_ctx[0] == NULL) {
stream->video_rtcp_ctx[0] = g_malloc0(sizeof(rtcp_context));
stream->video_rtcp_ctx[0]->tb = 90000; /* May change later */
}
}
switch(m->direction) {
case JANUS_SDP_INACTIVE:
case JANUS_SDP_INVALID:
stream->video_send = FALSE;
stream->video_recv = FALSE;
break;
case JANUS_SDP_SENDONLY:
/* A sendonly peer means recvonly for Janus */
stream->video_send = FALSE;
stream->video_recv = TRUE;
break;
case JANUS_SDP_RECVONLY:
/* A recvonly peer means sendonly for Janus */
stream->video_send = TRUE;
stream->video_recv = FALSE;
break;
case JANUS_SDP_SENDRECV:
case JANUS_SDP_DEFAULT:
default:
stream->video_send = TRUE;
stream->video_recv = TRUE;
break;
}
if(m->ptypes != NULL) {
g_list_free(stream->video_payload_types);
stream->video_payload_types = g_list_copy(m->ptypes);
}
} else {
/* Video rejected? */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Video rejected by peer...\n", handle->handle_id);
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_VIDEO);
}
#ifdef HAVE_SCTP
} else if(m->type == JANUS_SDP_APPLICATION) {
/* Is this SCTP for DataChannels? */
if(!strcasecmp(m->proto, "DTLS/SCTP") || !strcasecmp(m->proto, "UDP/DTLS/SCTP")) {
data++;
if(data > 1) {
temp = temp->next;
continue;
}
if(m->port > 0) {
/* Yep */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Parsing SCTP candidates (stream=%d)...\n", handle->handle_id, stream->stream_id);
if(!janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_DATA_CHANNELS)) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_DATA_CHANNELS);
}
if(!strcasecmp(m->proto, "UDP/DTLS/SCTP")) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_NEW_DATACHAN_SDP);
} else {
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_NEW_DATACHAN_SDP);
}
} else {
/* Data channels rejected? */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Data channels rejected by peer...\n", handle->handle_id);
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_DATA_CHANNELS);
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_NEW_DATACHAN_SDP);
}
} else {
/* Unsupported data channels format. */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Data channels format %s unsupported, skipping\n", handle->handle_id, m->proto);
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_DATA_CHANNELS);
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_NEW_DATACHAN_SDP);
}
#endif
} else {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Skipping disabled/unsupported media line...\n", handle->handle_id);
}
if(stream == NULL) {
temp = temp->next;
continue;
}
/* Look for mid, ICE credentials and fingerprint first: check media attributes */
GList *tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name) {
if(!strcasecmp(a->name, "mid")) {
/* Found mid attribute */
if(m->type == JANUS_SDP_AUDIO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Audio mid: %s\n", handle->handle_id, a->value);
if(handle->audio_mid == NULL)
handle->audio_mid = g_strdup(a->value);
if(handle->stream_mid == NULL)
handle->stream_mid = handle->audio_mid;
} else if(m->type == JANUS_SDP_VIDEO && m->port > 0) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Video mid: %s\n", handle->handle_id, a->value);
if(handle->video_mid == NULL)
handle->video_mid = g_strdup(a->value);
if(handle->stream_mid == NULL)
handle->stream_mid = handle->video_mid;
#ifdef HAVE_SCTP
} else if(m->type == JANUS_SDP_APPLICATION) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Data Channel mid: %s\n", handle->handle_id, a->value);
if(handle->data_mid == NULL)
handle->data_mid = g_strdup(a->value);
if(handle->stream_mid == NULL)
handle->stream_mid = handle->data_mid;
#endif
}
} else if(!strcasecmp(a->name, "fingerprint")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Fingerprint (local) : %s\n", handle->handle_id, a->value);
if(strcasestr(a->value, "sha-256 ") == a->value) {
g_free(rhashing); /* FIXME We're overwriting the global one, if any */
rhashing = g_strdup("sha-256");
g_free(rfingerprint); /* FIXME We're overwriting the global one, if any */
rfingerprint = g_strdup(a->value + strlen("sha-256 "));
} else if(strcasestr(a->value, "sha-1 ") == a->value) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Hashing algorithm not the one we expected (sha-1 instead of sha-256), but that's ok\n", handle->handle_id);
g_free(rhashing); /* FIXME We're overwriting the global one, if any */
rhashing = g_strdup("sha-1");
g_free(rfingerprint); /* FIXME We're overwriting the global one, if any */
rfingerprint = g_strdup(a->value + strlen("sha-1 "));
} else {
/* FIXME We should handle this somehow anyway... OpenSSL supports them all */
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Hashing algorithm not the one we expected (sha-256), *NOT* cool\n", handle->handle_id);
}
} else if(!strcasecmp(a->name, "setup")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] DTLS setup (local): %s\n", handle->handle_id, a->value);
if(!update) {
if(!strcasecmp(a->value, "actpass") || !strcasecmp(a->value, "passive")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Setting connect state (DTLS client)\n", handle->handle_id);
stream->dtls_role = JANUS_DTLS_ROLE_CLIENT;
} else if(!strcasecmp(a->value, "active")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Setting accept state (DTLS server)\n", handle->handle_id);
stream->dtls_role = JANUS_DTLS_ROLE_SERVER;
}
if(stream->component && stream->component->dtls)
stream->component->dtls->dtls_role = stream->dtls_role;
}
/* TODO Handle holdconn... */
} else if(!strcasecmp(a->name, "ice-ufrag")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] ICE ufrag (local): %s\n", handle->handle_id, a->value);
g_free(ruser); /* FIXME We're overwriting the global one, if any */
ruser = g_strdup(a->value);
} else if(!strcasecmp(a->name, "ice-pwd")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] ICE pwd (local): %s\n", handle->handle_id, a->value);
g_free(rpass); /* FIXME We're overwriting the global one, if any */
rpass = g_strdup(a->value);
}
}
tempA = tempA->next;
}
if(mlines == 1) {
if(!ruser || !rpass || (janus_is_webrtc_encryption_enabled() && (!rfingerprint || !rhashing))) {
/* Missing mandatory information, failure... */
JANUS_LOG(LOG_ERR, "[%"SCNu64"] SDP missing mandatory information\n", handle->handle_id);
JANUS_LOG(LOG_ERR, "[%"SCNu64"] %p, %p, %p, %p\n", handle->handle_id, ruser, rpass, rfingerprint, rhashing);
if(ruser)
g_free(ruser);
ruser = NULL;
if(rpass)
g_free(rpass);
rpass = NULL;
if(rhashing)
g_free(rhashing);
rhashing = NULL;
if(rfingerprint)
g_free(rfingerprint);
rfingerprint = NULL;
return -2;
}
/* If we received the ICE credentials for the first time, enforce them */
if(ruser && !stream->ruser && rpass && !stream->rpass) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Setting remote credentials...\n", handle->handle_id);
if(!nice_agent_set_remote_credentials(handle->agent, handle->stream_id, ruser, rpass)) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to set remote credentials!\n", handle->handle_id);
}
} else
/* If this is a renegotiation, check if this is an ICE restart */
if((ruser && stream->ruser && strcmp(ruser, stream->ruser)) ||
(rpass && stream->rpass && strcmp(rpass, stream->rpass))) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] ICE restart detected\n", handle->handle_id);
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_ALL_TRICKLES);
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_ICE_RESTART);
}
/* Store fingerprint and hashing */
if(janus_is_webrtc_encryption_enabled()) {
g_free(stream->remote_hashing);
stream->remote_hashing = g_strdup(rhashing);
g_free(stream->remote_fingerprint);
stream->remote_fingerprint = g_strdup(rfingerprint);
}
/* Store the ICE username and password for this stream */
g_free(stream->ruser);
stream->ruser = g_strdup(ruser);
g_free(stream->rpass);
stream->rpass = g_strdup(rpass);
}
/* Is simulcasting enabled, using rid? (we need to check this before parsing SSRCs) */
tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name && !strcasecmp(a->name, "rid") && a->value) {
/* This attribute is used for simulcasting */
char rid[16];
if(sscanf(a->value, "%15s send", rid) != 1) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse rid attribute...\n", handle->handle_id);
} else {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Parsed rid: %s\n", handle->handle_id, rid);
if(stream->rid[0] == NULL) {
stream->rid[0] = g_strdup(rid);
} else if(stream->rid[1] == NULL) {
stream->rid[1] = g_strdup(rid);
} else if(stream->rid[2] == NULL) {
stream->rid[2] = g_strdup(rid);
} else {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Too many RTP Stream IDs, ignoring '%s'...\n", handle->handle_id, rid);
}
}
} else if(a->name && !strcasecmp(a->name, "simulcast") && a->value) {
/* Firefox and Chrome signal simulcast support differently */
stream->legacy_rid = strstr(a->value, "rid=") ? TRUE : FALSE;
}
tempA = tempA->next;
}
/* Let's start figuring out the SSRCs, and any grouping that may be there */
stream->audio_ssrc_peer_new = 0;
stream->video_ssrc_peer_new[0] = 0;
stream->video_ssrc_peer_new[1] = 0;
stream->video_ssrc_peer_new[2] = 0;
stream->video_ssrc_peer_rtx_new[0] = 0;
stream->video_ssrc_peer_rtx_new[1] = 0;
stream->video_ssrc_peer_rtx_new[2] = 0;
/* Any SSRC SIM group? */
tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name && a->value) {
if(!strcasecmp(a->name, "ssrc-group") && strstr(a->value, "SIM")) {
int res = janus_sdp_parse_ssrc_group(stream, (const char *)a->value, m->type == JANUS_SDP_VIDEO);
if(res != 0) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse SSRC SIM group attribute... (%d)\n", handle->handle_id, res);
}
}
}
tempA = tempA->next;
}
/* Any SSRC FID group? */
tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name && a->value) {
if(!strcasecmp(a->name, "ssrc-group") && strstr(a->value, "FID")) {
int res = janus_sdp_parse_ssrc_group(stream, (const char *)a->value, m->type == JANUS_SDP_VIDEO);
if(res != 0) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse SSRC FID group attribute... (%d)\n", handle->handle_id, res);
}
}
}
tempA = tempA->next;
}
/* Any SSRC in general? */
tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name && a->value) {
if(!strcasecmp(a->name, "ssrc")) {
int res = janus_sdp_parse_ssrc(stream, (const char *)a->value, m->type == JANUS_SDP_VIDEO);
if(res != 0) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse SSRC attribute... (%d)\n", handle->handle_id, res);
}
}
}
tempA = tempA->next;
}
/* Now look for candidates and other info */
tempA = m->attributes;
while(tempA) {
janus_sdp_attribute *a = (janus_sdp_attribute *)tempA->data;
if(a->name) {
if(!strcasecmp(a->name, "candidate")) {
if(m->type == JANUS_SDP_AUDIO && mlines > 1) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] This is an audio candidate but we're bundling on another stream, ignoring...\n", handle->handle_id);
} else if(m->type == JANUS_SDP_VIDEO && mlines > 1) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] This is a video candidate but we're bundling on another stream, ignoring...\n", handle->handle_id);
#ifdef HAVE_SCTP
} else if(m->type == JANUS_SDP_APPLICATION && mlines > 1) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] This is a SCTP candidate but we're bundling on another stream, ignoring...\n", handle->handle_id);
#endif
} else {
int res = janus_sdp_parse_candidate(stream, (const char *)a->value, 0);
if(res != 0) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse candidate... (%d)\n", handle->handle_id, res);
}
}
} else if(!strcasecmp(a->name, "rtcp-fb")) {
if(a->value && strstr(a->value, "nack") && stream->component) {
if(m->type == JANUS_SDP_AUDIO) {
/* Enable NACKs for audio */
stream->component->do_audio_nacks = TRUE;
} else if(m->type == JANUS_SDP_VIDEO) {
/* Enable NACKs for video */
stream->component->do_video_nacks = TRUE;
}
}
} else if(!strcasecmp(a->name, "fmtp")) {
if(a->value && strstr(a->value, "apt=")) {
/* RFC4588 rtx payload type mapping */
int ptype = -1, rtx_ptype = -1;
if(sscanf(a->value, "%d apt=%d", &rtx_ptype, &ptype) != 2) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse fmtp/apt attribute...\n", handle->handle_id);
} else {
if(janus_is_rfc4588_enabled()) {
rtx = TRUE;
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RFC4588_RTX);
if(stream->rtx_payload_types == NULL)
stream->rtx_payload_types = g_hash_table_new(NULL, NULL);
g_hash_table_insert(stream->rtx_payload_types, GINT_TO_POINTER(ptype), GINT_TO_POINTER(rtx_ptype));
}
}
}
}
#ifdef HAVE_SCTP
else if(!strcasecmp(a->name, "sctpmap")) {
/* We don't really care */
JANUS_LOG(LOG_VERB, "Got a sctpmap attribute: %s\n", a->value);
}
#endif
}
tempA = tempA->next;
}
/* Any change in SSRCs we should be aware of? */
if(m->type == JANUS_SDP_AUDIO) {
if(stream->audio_ssrc_peer_new > 0) {
if(stream->audio_ssrc_peer > 0 && stream->audio_ssrc_peer != stream->audio_ssrc_peer_new) {
JANUS_LOG(LOG_INFO, "[%"SCNu64"] Audio SSRC changed: %"SCNu32" --> %"SCNu32"\n",
handle->handle_id, stream->audio_ssrc_peer, stream->audio_ssrc_peer_new);
/* FIXME Reset the RTCP context */
janus_ice_component *component = stream->component;
janus_mutex_lock(&component->mutex);
if(stream->audio_rtcp_ctx) {
memset(stream->audio_rtcp_ctx, 0, sizeof(*stream->audio_rtcp_ctx));
stream->audio_rtcp_ctx->tb = 48000; /* May change later */
}
if(component->last_seqs_audio)
janus_seq_list_free(&component->last_seqs_audio);
janus_mutex_unlock(&component->mutex);
}
stream->audio_ssrc_peer = stream->audio_ssrc_peer_new;
stream->audio_ssrc_peer_new = 0;
}
} else if(m->type == JANUS_SDP_VIDEO) {
int vindex = 0;
for(vindex=0; vindex<3; vindex++) {
if(stream->video_ssrc_peer_new[vindex] > 0) {
if(stream->video_ssrc_peer[vindex] > 0 && stream->video_ssrc_peer[vindex] != stream->video_ssrc_peer_new[vindex]) {
JANUS_LOG(LOG_INFO, "[%"SCNu64"] Video SSRC (#%d) changed: %"SCNu32" --> %"SCNu32"\n",
handle->handle_id, vindex, stream->video_ssrc_peer[vindex], stream->video_ssrc_peer_new[vindex]);
/* FIXME Reset the RTCP context */
janus_ice_component *component = stream->component;
if(component != NULL) {
janus_mutex_lock(&component->mutex);
if(stream->video_rtcp_ctx[vindex]) {
memset(stream->video_rtcp_ctx[vindex], 0, sizeof(*stream->video_rtcp_ctx[vindex]));
stream->video_rtcp_ctx[vindex]->tb = 90000;
}
if(component->last_seqs_video[vindex])
janus_seq_list_free(&component->last_seqs_video[vindex]);
janus_mutex_unlock(&component->mutex);
}
}
stream->video_ssrc_peer[vindex] = stream->video_ssrc_peer_new[vindex];
stream->video_ssrc_peer_new[vindex] = 0;
}
/* Do the same with the related rtx SSRC, if any */
if(stream->video_ssrc_peer_rtx_new[vindex] > 0) {
if(stream->video_ssrc_peer_rtx[vindex] > 0 && stream->video_ssrc_peer_rtx[vindex] != stream->video_ssrc_peer_rtx_new[vindex]) {
JANUS_LOG(LOG_INFO, "[%"SCNu64"] Video SSRC (#%d rtx) changed: %"SCNu32" --> %"SCNu32"\n",
handle->handle_id, vindex, stream->video_ssrc_peer_rtx[vindex], stream->video_ssrc_peer_rtx_new[vindex]);
}
stream->video_ssrc_peer_rtx[vindex] = stream->video_ssrc_peer_rtx_new[vindex];
stream->video_ssrc_peer_rtx_new[vindex] = 0;
if(stream->video_ssrc_rtx == 0)
stream->video_ssrc_rtx = janus_random_uint32(); /* FIXME Should we look for conflicts? */
}
}
if(stream->video_ssrc_peer[1] && stream->video_rtcp_ctx[1] == NULL) {
stream->video_rtcp_ctx[1] = g_malloc0(sizeof(rtcp_context));
stream->video_rtcp_ctx[1]->tb = 90000;
}
if(stream->video_ssrc_peer[2] && stream->video_rtcp_ctx[2] == NULL) {
stream->video_rtcp_ctx[2] = g_malloc0(sizeof(rtcp_context));
stream->video_rtcp_ctx[2]->tb = 90000;
}
}
temp = temp->next;
}
/* Disable RFC4588 if the peer didn't negotiate it */
if(!rtx) {
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RFC4588_RTX);
stream->video_ssrc_rtx = 0;
}
/* Cleanup */
g_free(ruser);
g_free(rpass);
g_free(rhashing);
g_free(rfingerprint);
return 0; /* FIXME Handle errors better */
}
int janus_sdp_parse_candidate(void *ice_stream, const char *candidate, int trickle) {
if(ice_stream == NULL || candidate == NULL)
return -1;
janus_ice_stream *stream = (janus_ice_stream *)ice_stream;
janus_ice_handle *handle = stream->handle;
if(handle == NULL)
return -2;
janus_ice_component *component = NULL;
if(strlen(candidate) == 0 || strstr(candidate, "end-of-candidates")) {
/* FIXME Should we do something with this? */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] end-of-candidates received\n", handle->handle_id);
return 0;
}
if(strstr(candidate, "candidate:") == candidate) {
/* Skipping the 'candidate:' prefix Firefox puts in trickle candidates */
candidate += strlen("candidate:");
}
char rfoundation[33], rtransport[4], rip[50], rtype[6], rrelip[40];
guint32 rcomponent, rpriority, rport, rrelport;
int res = sscanf(candidate, "%32s %30u %3s %30u %49s %30u typ %5s %*s %39s %*s %30u",
rfoundation, &rcomponent, rtransport, &rpriority,
rip, &rport, rtype, rrelip, &rrelport);
if(res < 7) {
/* Failed to parse this address, can it be IPv6? */
if(!janus_ice_is_ipv6_enabled()) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Received IPv6 candidate, but IPv6 support is disabled...\n", handle->handle_id);
return res;
}
}
if(res >= 7) {
if(strstr(rip, ".local")) {
/* The IP is actually an mDNS address, try to resolve it
* https://tools.ietf.org/html/draft-ietf-rtcweb-mdns-ice-candidates-00 */
struct addrinfo *info = NULL;
janus_network_address addr;
janus_network_address_string_buffer addr_buf;
if(getaddrinfo(rip, NULL, NULL, &info) != 0 ||
janus_network_address_from_sockaddr(info->ai_addr, &addr) != 0 ||
janus_network_address_to_string_buffer(&addr, &addr_buf) != 0) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Couldn't resolve mDNS address (%s), dropping candidate\n",
handle->handle_id, rip);
if(info)
freeaddrinfo(info);
return res;
}
freeaddrinfo(info);
JANUS_LOG(LOG_WARN, "[%"SCNu64"] mDNS address (%s) resolved: %s\n",
handle->handle_id, rip, janus_network_address_string_from_buffer(&addr_buf));
g_strlcpy(rip, janus_network_address_string_from_buffer(&addr_buf), sizeof(rip));
}
/* Add remote candidate */
component = stream->component;
if(component == NULL || rcomponent > 1) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] -- Skipping component %d in stream %d (rtcp-muxing)\n", handle->handle_id, rcomponent, stream->stream_id);
} else {
//~ if(trickle) {
//~ if(component->dtls != NULL) {
//~ /* This component is already ready, ignore this further candidate */
//~ JANUS_LOG(LOG_VERB, "[%"SCNu64"] -- Ignoring this candidate, the component is already ready\n", handle->handle_id);
//~ return 0;
//~ }
//~ }
component->component_id = rcomponent;
component->stream_id = stream->stream_id;
NiceCandidate *c = NULL;
if(!strcasecmp(rtype, "host")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Adding remote candidate component:%d stream:%d type:host %s:%d\n",
handle->handle_id, rcomponent, stream->stream_id, rip, rport);
/* Unless this is libnice >= 0.1.8, we only support UDP... */
if(!strcasecmp(rtransport, "udp")) {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_HOST);
#ifdef HAVE_LIBNICE_TCP
} else if(!strcasecmp(rtransport, "tcp") && janus_ice_is_ice_tcp_enabled()) {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_HOST);
#endif
} else {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
}
} else if(!strcasecmp(rtype, "srflx")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Adding remote candidate component:%d stream:%d type:srflx %s:%d --> %s:%d \n",
handle->handle_id, rcomponent, stream->stream_id, rrelip, rrelport, rip, rport);
/* Unless this is libnice >= 0.1.8, we only support UDP... */
if(!strcasecmp(rtransport, "udp")) {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE);
#ifdef HAVE_LIBNICE_TCP
} else if(!strcasecmp(rtransport, "tcp") && janus_ice_is_ice_tcp_enabled()) {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE);
#endif
} else {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
}
} else if(!strcasecmp(rtype, "prflx")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Adding remote candidate component:%d stream:%d type:prflx %s:%d --> %s:%d\n",
handle->handle_id, rcomponent, stream->stream_id, rrelip, rrelport, rip, rport);
/* Unless this is libnice >= 0.1.8, we only support UDP... */
if(!strcasecmp(rtransport, "udp")) {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_PEER_REFLEXIVE);
#ifdef HAVE_LIBNICE_TCP
} else if(!strcasecmp(rtransport, "tcp") && janus_ice_is_ice_tcp_enabled()) {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_PEER_REFLEXIVE);
#endif
} else {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
}
} else if(!strcasecmp(rtype, "relay")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Adding remote candidate component:%d stream:%d type:relay %s:%d --> %s:%d\n",
handle->handle_id, rcomponent, stream->stream_id, rrelip, rrelport, rip, rport);
/* We only support UDP/TCP/TLS... */
if(strcasecmp(rtransport, "udp") && strcasecmp(rtransport, "tcp") && strcasecmp(rtransport, "tls")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
} else {
c = nice_candidate_new(NICE_CANDIDATE_TYPE_RELAYED);
}
} else {
/* FIXME What now? */
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Unknown remote candidate type:%s for component:%d stream:%d!\n",
handle->handle_id, rtype, rcomponent, stream->stream_id);
}
if(c != NULL) {
c->component_id = rcomponent;
c->stream_id = stream->stream_id;
#ifndef HAVE_LIBNICE_TCP
c->transport = NICE_CANDIDATE_TRANSPORT_UDP;
#else
if(!strcasecmp(rtransport, "udp")) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Transport: UDP\n", handle->handle_id);
c->transport = NICE_CANDIDATE_TRANSPORT_UDP;
} else {
/* Check the type (https://tools.ietf.org/html/rfc6544#section-4.5) */
const char *type = NULL;
int ctype = 0;
if(strstr(candidate, "tcptype active")) {
type = "active";
ctype = NICE_CANDIDATE_TRANSPORT_TCP_ACTIVE;
} else if(strstr(candidate, "tcptype passive")) {
type = "passive";
ctype = NICE_CANDIDATE_TRANSPORT_TCP_PASSIVE;
} else if(strstr(candidate, "tcptype so")) {
type = "so";
ctype = NICE_CANDIDATE_TRANSPORT_TCP_SO;
} else {
/* TODO: We should actually stop here... */
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Missing tcptype info for the TCP candidate!\n", handle->handle_id);
}
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Transport: TCP (%s)\n", handle->handle_id, type);
c->transport = ctype;
}
#endif
g_strlcpy(c->foundation, rfoundation, NICE_CANDIDATE_MAX_FOUNDATION);
c->priority = rpriority;
nice_address_set_from_string(&c->addr, rip);
nice_address_set_port(&c->addr, rport);
c->username = g_strdup(stream->ruser);
c->password = g_strdup(stream->rpass);
if(c->type == NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE || c->type == NICE_CANDIDATE_TYPE_PEER_REFLEXIVE) {
nice_address_set_from_string(&c->base_addr, rrelip);
nice_address_set_port(&c->base_addr, rrelport);
} else if(c->type == NICE_CANDIDATE_TYPE_RELAYED) {
/* FIXME Do we really need the base address for TURN? */
nice_address_set_from_string(&c->base_addr, rrelip);
nice_address_set_port(&c->base_addr, rrelport);
}
component->candidates = g_slist_append(component->candidates, c);
JANUS_LOG(LOG_HUGE, "[%"SCNu64"] Candidate added to the list! (%u elements for %d/%d)\n", handle->handle_id,
g_slist_length(component->candidates), stream->stream_id, component->component_id);
/* Save for the summary, in case we need it */
component->remote_candidates = g_slist_append(component->remote_candidates, g_strdup(candidate));
/* Notify event handlers */
if(janus_events_is_enabled()) {
janus_session *session = (janus_session *)handle->session;
json_t *info = json_object();
json_object_set_new(info, "remote-candidate", json_string(candidate));
json_object_set_new(info, "stream_id", json_integer(stream->stream_id));
json_object_set_new(info, "component_id", json_integer(component->component_id));
janus_events_notify_handlers(JANUS_EVENT_TYPE_WEBRTC, session->session_id, handle->handle_id, handle->opaque_id, info);
}
/* See if we need to process this */
if(trickle) {
if(janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_START)) {
/* This is a trickle candidate and ICE has started, we should process it right away */
if(!component->process_started) {
/* Actually, ICE has JUST started for this component, take care of the candidates we've added so far */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] ICE already started for this component, setting candidates we have up to now\n", handle->handle_id);
janus_ice_setup_remote_candidates(handle, component->stream_id, component->component_id);
} else {
GSList *candidates = NULL;
candidates = g_slist_append(candidates, c);
if(nice_agent_set_remote_candidates(handle->agent, stream->stream_id, component->component_id, candidates) < 1) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to add trickle candidate :-(\n", handle->handle_id);
} else {
JANUS_LOG(LOG_HUGE, "[%"SCNu64"] Trickle candidate added!\n", handle->handle_id);
}
g_slist_free(candidates);
}
} else {
/* ICE hasn't started yet: to make sure we're not stuck, also check if we stopped processing the SDP */
if(!janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER)) {
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_START);
/* This is a trickle candidate and ICE has started, we should process it right away */
if(!component->process_started) {
/* Actually, ICE has JUST started for this component, take care of the candidates we've added so far */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] SDP processed but ICE not started yet for this component, setting candidates we have up to now\n", handle->handle_id);
janus_ice_setup_remote_candidates(handle, component->stream_id, component->component_id);
} else {
GSList *candidates = NULL;
candidates = g_slist_append(candidates, c);
if(nice_agent_set_remote_candidates(handle->agent, stream->stream_id, component->component_id, candidates) < 1) {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to add trickle candidate :-(\n", handle->handle_id);
} else {
JANUS_LOG(LOG_HUGE, "[%"SCNu64"] Trickle candidate added!\n", handle->handle_id);
}
g_slist_free(candidates);
}
} else {
/* Still processing the offer/answer: queue the trickle candidate for now, we'll process it later */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Queueing trickle candidate, status is not START yet\n", handle->handle_id);
}
}
}
}
}
} else {
JANUS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse candidate (res=%d)...\n", handle->handle_id, res);
return res;
}
return 0;
}
int janus_sdp_parse_ssrc_group(void *ice_stream, const char *group_attr, int video) {
if(ice_stream == NULL || group_attr == NULL)
return -1;
janus_ice_stream *stream = (janus_ice_stream *)ice_stream;
janus_ice_handle *handle = stream->handle;
if(handle == NULL)
return -2;
if(!video)
return -3;
if(stream->rid[0] != NULL) {
/* Simulcasting is rid-based, don't parse SSRCs for now */
return 0;
}
gboolean fid = strstr(group_attr, "FID") != NULL;
gboolean sim = strstr(group_attr, "SIM") != NULL;
guint64 ssrc = 0;
guint32 first_ssrc = 0;
gchar **list = g_strsplit(group_attr, " ", -1);
gchar *index = list[0];
if(index != NULL) {
int i=0;
while(index != NULL) {
if(i > 0 && strlen(index) > 0) {
ssrc = g_ascii_strtoull(index, NULL, 0);
switch(i) {
case 1:
first_ssrc = ssrc;
if(stream->video_ssrc_peer_new[0] == ssrc || stream->video_ssrc_peer_new[1] == ssrc
|| stream->video_ssrc_peer_new[2] == ssrc) {
JANUS_LOG(LOG_HUGE, "[%"SCNu64"] Already parsed this SSRC: %"SCNu64" (%s group)\n",
handle->handle_id, ssrc, (fid ? "FID" : (sim ? "SIM" : "??")));
} else {
if(stream->video_ssrc_peer_new[0] == 0) {
stream->video_ssrc_peer_new[0] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC: %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_new[0]);
} else {
/* We already have a video SSRC: check if rid is involved, and we'll keep track of this for simulcasting */
if(stream->rid[0]) {
if(stream->video_ssrc_peer_new[1] == 0) {
stream->video_ssrc_peer_new[1] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (sim-1): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_new[1]);
} else if(stream->video_ssrc_peer_new[2] == 0) {
stream->video_ssrc_peer_new[2] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (sim-2): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_new[2]);
} else {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Don't know what to do with video SSRC: %"SCNu64"\n", handle->handle_id, ssrc);
}
}
}
}
break;
case 2:
if(fid) {
if(stream->video_ssrc_peer_new[0] == first_ssrc && stream->video_ssrc_peer_rtx_new[0] == 0) {
stream->video_ssrc_peer_rtx_new[0] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (rtx): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_rtx_new[0]);
} else if(stream->video_ssrc_peer_new[1] == first_ssrc && stream->video_ssrc_peer_rtx_new[1] == 0) {
stream->video_ssrc_peer_rtx_new[1] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (sim-1 rtx): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_rtx_new[1]);
} else if(stream->video_ssrc_peer_new[2] == first_ssrc && stream->video_ssrc_peer_rtx_new[2] == 0) {
stream->video_ssrc_peer_rtx_new[2] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (sim-2 rtx): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_rtx_new[2]);
} else {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Don't know what to do with rtx SSRC: %"SCNu64"\n", handle->handle_id, ssrc);
}
} else if(sim) {
stream->video_ssrc_peer_new[1] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (sim-1): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_new[1]);
} else {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Don't know what to do with SSRC: %"SCNu64"\n", handle->handle_id, ssrc);
}
break;
case 3:
if(fid) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Found one too many retransmission SSRC (rtx): %"SCNu64"\n", handle->handle_id, ssrc);
} else if(sim) {
stream->video_ssrc_peer_new[2] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (sim-2): %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_new[2]);
} else {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Don't know what to do with SSRC: %"SCNu64"\n", handle->handle_id, ssrc);
}
break;
default:
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Don't know what to do with video SSRC: %"SCNu64"\n", handle->handle_id, ssrc);
break;
}
}
i++;
index = list[i];
}
}
g_clear_pointer(&list, g_strfreev);
return 0;
}
int janus_sdp_parse_ssrc(void *ice_stream, const char *ssrc_attr, int video) {
if(ice_stream == NULL || ssrc_attr == NULL)
return -1;
janus_ice_stream *stream = (janus_ice_stream *)ice_stream;
janus_ice_handle *handle = stream->handle;
if(handle == NULL)
return -2;
guint64 ssrc = g_ascii_strtoull(ssrc_attr, NULL, 0);
if(ssrc == 0 || ssrc > G_MAXUINT32)
return -3;
if(video) {
if(stream->rid[0] != NULL) {
/* Simulcasting is rid-based, only keep track of a single SSRC for fallback */
if(stream->video_ssrc_peer_temp == 0) {
stream->video_ssrc_peer_temp = ssrc;
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Peer video fallback SSRC: %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_temp);
}
return 0;
}
if(stream->video_ssrc_peer_new[0] == 0) {
stream->video_ssrc_peer_new[0] = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC: %"SCNu32"\n", handle->handle_id, stream->video_ssrc_peer_new[0]);
}
} else {
if(stream->audio_ssrc_peer_new == 0) {
stream->audio_ssrc_peer_new = ssrc;
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Peer audio SSRC: %"SCNu32"\n", handle->handle_id, stream->audio_ssrc_peer_new);
}
}
return 0;
}
int janus_sdp_anonymize(janus_sdp *anon) {
if(anon == NULL)
return -1;
int audio = 0, video = 0, data = 0;
/* o= */
if(anon->o_addr != NULL) {
g_free(anon->o_addr);
anon->o_ipv4 = TRUE;
anon->o_addr = g_strdup("1.1.1.1");
}
/* a= */
GList *temp = anon->attributes;
while(temp) {
janus_sdp_attribute *a = (janus_sdp_attribute *)temp->data;
/* These are attributes we handle ourselves, the plugins don't need them */
if(!strcasecmp(a->name, "ice-ufrag")
|| !strcasecmp(a->name, "ice-pwd")
|| !strcasecmp(a->name, "ice-options")
|| !strcasecmp(a->name, "fingerprint")
|| !strcasecmp(a->name, "group")
|| !strcasecmp(a->name, "msid-semantic")
|| !strcasecmp(a->name, "rtcp-rsize")) {
anon->attributes = g_list_remove(anon->attributes, a);
temp = anon->attributes;
janus_sdp_attribute_destroy(a);
continue;
}
temp = temp->next;
continue;
}
/* m= */
temp = anon->m_lines;
while(temp) {
janus_sdp_mline *m = (janus_sdp_mline *)temp->data;
if(m->type == JANUS_SDP_AUDIO && m->port > 0) {
audio++;
m->port = audio == 1 ? 9 : 0;
} else if(m->type == JANUS_SDP_VIDEO && m->port > 0) {
video++;