-
Notifications
You must be signed in to change notification settings - Fork 5
/
sdp.c
1210 lines (1189 loc) · 47.5 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 (based on the Sofia-SDP stack) of the SDP
* parser/merger/generator in the gateway. 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.
*
* \ingroup protocols
* \ref protocols
*/
#include "psclient.h"
#include "ice.h"
#include "dtls.h"
#include "sdp.h"
#include "utils.h"
#include "debug.h"
static su_home_t *home = NULL;
/* SDP Initialization */
int janus_sdp_init(void) {
home = su_home_new(sizeof(su_home_t));
if(su_home_init(home) < 0) {
PS_LOG(LOG_FATAL, "Ops, error setting up sofia-sdp?\n");
return -1;
}
return 0;
}
void janus_sdp_deinit(void) {
su_home_deinit(home);
su_home_unref(home);
home = NULL;
}
/* Fake attribute we use for the sendrecv hack */
static sdp_attribute_t fakedir = {
.a_size = sizeof(sdp_attribute_t),
.a_name = "jfmod",
.a_value = "sr"
};
/* SDP parser */
void janus_sdp_free(janus_sdp *sdp) {
if(!sdp)
return;
sdp_parser_t *parser = (sdp_parser_t *)sdp->parser;
if(parser)
sdp_parser_free(parser);
sdp->parser = NULL;
sdp->sdp = NULL;
g_free(sdp);
sdp = NULL;
}
/* Pre-parse SDP: is this SDP valid? how many audio/video lines? any features to take into account? */
janus_sdp *janus_sdp_preparse(const char *jsep_sdp, int *audio, int *video, int *data, int *bundle, int *rtcpmux, int *trickle) {
if(!jsep_sdp || !audio || !video || !data || !bundle || !rtcpmux || !trickle) {
PS_LOG(LOG_ERR, " Can't preparse, invalid arduments\n");
return NULL;
}
sdp_parser_t *parser = sdp_parse(home, jsep_sdp, strlen(jsep_sdp), 0);
sdp_session_t *parsed_sdp = sdp_session(parser);
if(!parsed_sdp) {
PS_LOG(LOG_ERR, " Error parsing SDP? %s\n", sdp_parsing_error(parser));
sdp_parser_free(parser);
/* Invalid SDP */
return NULL;
}
sdp_media_t *m = parsed_sdp->sdp_media;
while(m) {
if(m->m_type == sdp_media_audio && m->m_port > 0) {
*audio = *audio + 1;
} else if(m->m_type == sdp_media_video && m->m_port > 0) {
*video = *video + 1;
}
m = m->m_next;
}
#ifdef HAVE_SCTP
*data = (strstr(jsep_sdp, "DTLS/SCTP") && !strstr(jsep_sdp, " 0 DTLS/SCTP")) ? 1 : 0; /* FIXME This is a really hacky way of checking... */
#else
*data = 0;
#endif
*bundle = strstr(jsep_sdp, "a=group:BUNDLE") ? 1 : 0; /* FIXME This is a really hacky way of checking... */
*rtcpmux = strstr(jsep_sdp, "a=rtcp-mux") ? 1 : 0; /* FIXME Should we make this check per-medium? */
//~ *trickle = (strstr(jsep_sdp, "trickle") || strstr(jsep_sdp, "google-ice") || strstr(jsep_sdp, "Mozilla")) ? 1 : 0; /* FIXME This is a really hacky way of checking... */
/* FIXME We're assuming trickle is always supported, see https://github.com/meetecho/janus-gateway/issues/83 */
*trickle = 1;
janus_sdp *sdp = (janus_sdp *)g_malloc0(sizeof(janus_sdp));
if(sdp == NULL) {
PS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
sdp->parser = parser;
sdp->sdp = parsed_sdp;
return sdp;
}
/* Parse SDP */
int janus_sdp_parse(janus_ice_handle *handle, janus_sdp *sdp) {
if(!handle || !sdp)
return -1;
sdp_session_t *remote_sdp = (sdp_session_t *)sdp->sdp;
if(!remote_sdp)
return -1;
janus_ice_stream *stream = NULL;
gchar *ruser = NULL, *rpass = NULL, *rhashing = NULL, *rfingerprint = NULL;
int audio = 0, video = 0;
#ifdef HAVE_SCTP
int data = 0;
#endif
/* Ok, let's start */
sdp_attribute_t *a = remote_sdp->sdp_attributes;
while(a) {
if(a->a_name) {
if(!strcasecmp(a->a_name, "fingerprint")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Fingerprint (global) : %s\n", handle->handle_id, a->a_value);
if(strcasestr(a->a_value, "sha-256 ") == a->a_value) {
rhashing = g_strdup("sha-256");
rfingerprint = g_strdup(a->a_value + strlen("sha-256 "));
} else if(strcasestr(a->a_value, "sha-1 ") == a->a_value) {
PS_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->a_value + strlen("sha-1 "));
} else {
/* FIXME We should handle this somehow anyway... OpenSSL supports them all */
PS_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->a_name, "ice-ufrag")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] ICE ufrag (global): %s\n", handle->handle_id, a->a_value);
ruser = g_strdup(a->a_value);
} else if(!strcasecmp(a->a_name, "ice-pwd")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] ICE pwd (global): %s\n", handle->handle_id, a->a_value);
rpass = g_strdup(a->a_value);
}
}
a = a->a_next;
}
sdp_media_t *m = remote_sdp->sdp_media;
while(m) {
/* What media type is this? */
if(m->m_type == sdp_media_audio) {
if(handle->rtp_profile == NULL && m->m_proto_name != NULL)
handle->rtp_profile = g_strdup(m->m_proto_name);
if(m->m_port > 0) {
audio++;
if(audio > 1) {
m = m->m_next;
continue;
}
PS_LOG(LOG_VERB, "[%"SCNu64"] Parsing audio candidates (stream=%d)...\n", handle->handle_id, handle->audio_id);
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->audio_id));
} else {
/* Audio rejected? */
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_AUDIO);
PS_LOG(LOG_VERB, "[%"SCNu64"] Audio rejected by peer...\n", handle->handle_id);
}
} else if(m->m_type == sdp_media_video) {
if(handle->rtp_profile == NULL && m->m_proto_name != NULL)
handle->rtp_profile = g_strdup(m->m_proto_name);
if(m->m_port > 0) {
video++;
if(video > 1) {
m = m->m_next;
continue;
}
PS_LOG(LOG_VERB, "[%"SCNu64"] Parsing video candidates (stream=%d)...\n", handle->handle_id, handle->video_id);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->video_id));
} else {
gint id = handle->audio_id > 0 ? handle->audio_id : handle->video_id;
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(id));
}
} else {
/* Video rejected? */
PS_LOG(LOG_VERB, "[%"SCNu64"] Video rejected by peer...\n", handle->handle_id);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_HAS_VIDEO);
}
#ifdef HAVE_SCTP
} else if(m->m_type == sdp_media_application) {
/* Is this SCTP for DataChannels? */
if(m->m_proto_name != NULL && !strcasecmp(m->m_proto_name, "DTLS/SCTP")) {
if(m->m_port > 0) {
/* Yep */
data++;
if(data > 1) {
m = m->m_next;
continue;
}
PS_LOG(LOG_VERB, "[%"SCNu64"] Parsing SCTP candidates (stream=%d)...\n", handle->handle_id, handle->video_id);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->data_id));
} else {
gint id = handle->audio_id > 0 ? handle->audio_id : (handle->video_id > 0 ? handle->video_id : handle->data_id);
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(id));
}
if(stream == NULL) {
PS_LOG(LOG_WARN, "No valid stream for data??\n");
continue;
}
}
} else {
/* Data channels rejected? */
PS_LOG(LOG_VERB, "[%"SCNu64"] Data channels rejected by peer...\n", handle->handle_id);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_DATA_CHANNELS);
}
#endif
} else {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping disabled/unsupported media line...\n", handle->handle_id);
m = m->m_next;
continue;
}
/* Look for mid, ICE credentials and fingerprint first: check media attributes */
a = m->m_attributes;
while(a) {
if(a->a_name) {
if(!strcasecmp(a->a_name, "mid")) {
/* Found mid attribute */
if(m->m_type == sdp_media_audio && m->m_port > 0) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Audio mid: %s\n", handle->handle_id, a->a_value);
handle->audio_mid = g_strdup(a->a_value);
} else if(m->m_type == sdp_media_video && m->m_port > 0) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Video mid: %s\n", handle->handle_id, a->a_value);
handle->video_mid = g_strdup(a->a_value);
#ifdef HAVE_SCTP
} else if(m->m_type == sdp_media_application) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Data Channel mid: %s\n", handle->handle_id, a->a_value);
handle->data_mid = g_strdup(a->a_value);
#endif
}
} else if(!strcasecmp(a->a_name, "fingerprint")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Fingerprint (local) : %s\n", handle->handle_id, a->a_value);
if(strcasestr(a->a_value, "sha-256 ") == a->a_value) {
if(rhashing)
g_free(rhashing); /* FIXME We're overwriting the global one, if any */
rhashing = g_strdup("sha-256");
if(rfingerprint)
g_free(rfingerprint); /* FIXME We're overwriting the global one, if any */
rfingerprint = g_strdup(a->a_value + strlen("sha-256 "));
} else if(strcasestr(a->a_value, "sha-1 ") == a->a_value) {
PS_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);
if(rhashing)
g_free(rhashing); /* FIXME We're overwriting the global one, if any */
rhashing = g_strdup("sha-1");
if(rfingerprint)
g_free(rfingerprint); /* FIXME We're overwriting the global one, if any */
rfingerprint = g_strdup(a->a_value + strlen("sha-1 "));
} else {
/* FIXME We should handle this somehow anyway... OpenSSL supports them all */
PS_LOG(LOG_WARN, "[%"SCNu64"] Hashing algorithm not the one we expected (sha-256), *NOT* cool\n", handle->handle_id);
}
} else if(!strcasecmp(a->a_name, "setup")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] DTLS setup (local): %s\n", handle->handle_id, a->a_value);
if(!strcasecmp(a->a_value, "actpass") || !strcasecmp(a->a_value, "passive"))
stream->dtls_role = JANUS_DTLS_ROLE_CLIENT;
else if(!strcasecmp(a->a_value, "active"))
stream->dtls_role = JANUS_DTLS_ROLE_SERVER;
/* TODO Handle holdconn... */
} else if(!strcasecmp(a->a_name, "ice-ufrag")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] ICE ufrag (local): %s\n", handle->handle_id, a->a_value);
if(ruser)
g_free(ruser); /* FIXME We're overwriting the global one, if any */
ruser = g_strdup(a->a_value);
} else if(!strcasecmp(a->a_name, "ice-pwd")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] ICE pwd (local): %s\n", handle->handle_id, a->a_value);
if(rpass)
g_free(rpass); /* FIXME We're overwriting the global one, if any */
rpass = g_strdup(a->a_value);
}
}
a = a->a_next;
}
if(!ruser || !rpass || !rfingerprint || !rhashing) {
/* Missing mandatory information, failure... */
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;
}
/* FIXME We're replacing the fingerprint info, assuming it's going to be the same for all media */
if(stream->remote_hashing != NULL)
g_free(stream->remote_hashing);
stream->remote_hashing = g_strdup(rhashing);
if(stream->remote_fingerprint != NULL)
g_free(stream->remote_fingerprint);
stream->remote_fingerprint = g_strdup(rfingerprint);
/* Store the ICE username and password for this stream */
if(stream->ruser != NULL)
g_free(stream->ruser);
stream->ruser = g_strdup(ruser);
if(stream->rpass != NULL)
g_free(stream->rpass);
stream->rpass = g_strdup(rpass);
/* Now look for candidates and other info */
a = m->m_attributes;
while(a) {
if(a->a_name) {
if(!strcasecmp(a->a_name, "candidate")) {
if(m->m_type == sdp_media_video && handle->audio_id > 0 && ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
PS_LOG(LOG_VERB, "[%"SCNu64"] This is a video candidate but we're bundling, ignoring...\n", handle->handle_id);
#ifdef HAVE_SCTP
} else if(m->m_type == sdp_media_application && (handle->audio_id > 0 || handle->video_id > 0) && ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
PS_LOG(LOG_VERB, "[%"SCNu64"] This is a SCTP candidate but we're bundling, ignoring...\n", handle->handle_id);
#endif
} else {
int res = janus_sdp_parse_candidate(stream, (const char *)a->a_value, 0);
if(res != 0) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse candidate... (%d)\n", handle->handle_id, res);
}
}
}
if(!strcasecmp(a->a_name, "ssrc")) {
int res = janus_sdp_parse_ssrc(stream, (const char *)a->a_value, m->m_type == sdp_media_video);
if(res != 0) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse SSRC attribute... (%d)\n", handle->handle_id, res);
}
}
#ifdef HAVE_SCTP
if(!strcasecmp(a->a_name, "sctpmap")) {
/* TODO Parse sctpmap line to get the UDP-port value and the number of channels */
PS_LOG(LOG_VERB, "Got a sctpmap attribute: %s\n", a->a_value);
}
#endif
}
a = a->a_next;
}
m = m->m_next;
}
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 0; /* FIXME Handle errors better */
}
int janus_sdp_parse_candidate(janus_ice_stream *stream, const char *candidate, int trickle) {
if(stream == NULL || candidate == NULL)
return -1;
janus_ice_handle *handle = stream->handle;
if(handle == NULL)
return -2;
janus_ice_component *component = NULL;
if(strstr(candidate, "candidate:") == candidate) {
/* Skipping the 'candidate:' prefix Firefox puts in trickle candidates */
candidate += strlen("candidate:");
}
char rfoundation[32], rtransport[4], rip[40], rtype[6], rrelip[40];
guint32 rcomponent, rpriority, rport, rrelport;
int res = sscanf(candidate, "%31s %30u %3s %30u %39s %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()) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Received IPv6 candidate, but IPv6 support is disabled...\n", handle->handle_id);
return res;
}
}
if(res >= 7) {
/* Add remote candidate */
component = g_hash_table_lookup(stream->components, GUINT_TO_POINTER(rcomponent));
if(component == NULL) {
if(rcomponent == 2 && ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX)) {
PS_LOG(LOG_VERB, "[%"SCNu64"] -- Skipping component %d in stream %d (rtcp-muxing)\n", handle->handle_id, rcomponent, stream->stream_id);
} else {
PS_LOG(LOG_ERR, "[%"SCNu64"] -- No such component %d in stream %d?\n", handle->handle_id, rcomponent, stream->stream_id);
}
} else {
if(rcomponent == 2 && ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX)) {
PS_LOG(LOG_VERB, "[%"SCNu64"] -- Skipping component %d in stream %d (rtcp-muxing)\n", handle->handle_id, rcomponent, stream->stream_id);
return 0;
}
//~ if(trickle) {
//~ if(component->dtls != NULL) {
//~ /* This component is already ready, ignore this further candidate */
//~ PS_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")) {
PS_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 {
PS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
}
} else if(!strcasecmp(rtype, "srflx")) {
PS_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 {
PS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
}
} else if(!strcasecmp(rtype, "prflx")) {
PS_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 {
PS_LOG(LOG_VERB, "[%"SCNu64"] Skipping unsupported transport '%s' for media\n", handle->handle_id, rtransport);
}
} else if(!strcasecmp(rtype, "relay")) {
PS_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")) {
PS_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? */
PS_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")) {
PS_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... */
PS_LOG(LOG_ERR, "[%"SCNu64"] Missing tcptype info for the TCP candidate!\n", handle->handle_id);
}
PS_LOG(LOG_VERB, "[%"SCNu64"] Transport: TCP (%s)\n", handle->handle_id, type);
c->transport = ctype;
}
#endif
strncpy(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);
PS_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));
if(trickle) {
if(ps_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 */
PS_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) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Failed to add trickle candidate :-(\n", handle->handle_id);
} else {
PS_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(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER)) {
ps_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 */
PS_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) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Failed to add trickle candidate :-(\n", handle->handle_id);
} else {
PS_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 */
PS_LOG(LOG_VERB, "[%"SCNu64"] Queueing trickle candidate, status is not START yet\n", handle->handle_id);
}
}
}
}
}
} else {
PS_LOG(LOG_ERR, "[%"SCNu64"] Failed to parse candidate (res=%d)...\n", handle->handle_id, res);
return res;
}
return 0;
}
int janus_sdp_parse_ssrc(janus_ice_stream *stream, const char *ssrc_attr, int video) {
if(stream == NULL || ssrc_attr == NULL)
return -1;
janus_ice_handle *handle = stream->handle;
if(handle == NULL)
return -2;
gint64 ssrc = atoll(ssrc_attr);
if(ssrc == 0)
return -3;
if(video) {
if(stream->video_ssrc_peer == 0) {
stream->video_ssrc_peer = ssrc;
PS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC: %u\n", handle->handle_id, stream->video_ssrc_peer);
} else if(stream->video_ssrc_peer != ssrc) {
/* FIXME We assume the second SSRC we get is the one Chrome associates with retransmissions, e.g.
* a=ssrc-group:FID 586466331 2053167359 (SSRC SSRC-rtx)
* SSRC group FID: https://tools.ietf.org/html/rfc3388#section-7 */
stream->video_ssrc_peer_rtx = ssrc;
PS_LOG(LOG_VERB, "[%"SCNu64"] Peer video SSRC (rtx): %u\n", handle->handle_id, stream->video_ssrc_peer_rtx);
}
} else {
if(stream->audio_ssrc_peer == 0) {
stream->audio_ssrc_peer = ssrc;
PS_LOG(LOG_VERB, "[%"SCNu64"] Peer audio SSRC: %u\n", handle->handle_id, stream->audio_ssrc_peer);
}
}
return 0;
}
char *janus_sdp_anonymize(const char *sdp) {
if(sdp == NULL)
return NULL;
sdp_session_t *anon = NULL;
sdp_parser_t *parser = sdp_parse(home, sdp, strlen(sdp), 0);
if(!(anon = sdp_session(parser))) {
PS_LOG(LOG_ERR, "Error parsing/merging SDP: %s\n", sdp_parsing_error(parser));
sdp_parser_free(parser);
return NULL;
}
/* c= */
if(anon->sdp_connection && anon->sdp_connection->c_address) {
anon->sdp_connection->c_address = "1.1.1.1";
}
/* a= */
if(anon->sdp_attributes) {
/* These are attributes we handle ourselves, the plugins don't need them */
while(sdp_attribute_find(anon->sdp_attributes, "ice-ufrag"))
sdp_attribute_remove(&anon->sdp_attributes, "ice-ufrag");
while(sdp_attribute_find(anon->sdp_attributes, "ice-pwd"))
sdp_attribute_remove(&anon->sdp_attributes, "ice-pwd");
while(sdp_attribute_find(anon->sdp_attributes, "ice-options"))
sdp_attribute_remove(&anon->sdp_attributes, "ice-options");
while(sdp_attribute_find(anon->sdp_attributes, "fingerprint"))
sdp_attribute_remove(&anon->sdp_attributes, "fingerprint");
while(sdp_attribute_find(anon->sdp_attributes, "group"))
sdp_attribute_remove(&anon->sdp_attributes, "group");
while(sdp_attribute_find(anon->sdp_attributes, "msid-semantic"))
sdp_attribute_remove(&anon->sdp_attributes, "msid-semantic");
}
/* m= */
if(anon->sdp_media) {
int audio = 0, video = 0;
#ifdef HAVE_SCTP
int data = 0;
#endif
sdp_media_t *m = anon->sdp_media;
while(m) {
if(m->m_type == sdp_media_audio && m->m_port > 0) {
audio++;
m->m_port = audio == 1 ? 1 : 0;
} else if(m->m_type == sdp_media_video && m->m_port > 0) {
video++;
m->m_port = video == 1 ? 1 : 0;
#ifdef HAVE_SCTP
} else if(m->m_type == sdp_media_application) {
if(m->m_proto_name != NULL && !strcasecmp(m->m_proto_name, "DTLS/SCTP") && m->m_port != 0) {
data++;
m->m_port = data == 1 ? 1 : 0;
} else {
m->m_port = 0;
}
#endif
} else {
m->m_port = 0;
}
/* c= */
if(m->m_connections) {
sdp_connection_t *c = m->m_connections;
while(c) {
if(c->c_address) {
c->c_address = "1.1.1.1";
}
c = c->c_next;
}
}
/* a= */
if(m->m_attributes) {
/* These are attributes we handle ourselves, the plugins don't need them */
while(sdp_attribute_find(m->m_attributes, "ice-ufrag"))
sdp_attribute_remove(&m->m_attributes, "ice-ufrag");
while(sdp_attribute_find(m->m_attributes, "ice-pwd"))
sdp_attribute_remove(&m->m_attributes, "ice-pwd");
while(sdp_attribute_find(m->m_attributes, "ice-options"))
sdp_attribute_remove(&m->m_attributes, "ice-options");
while(sdp_attribute_find(m->m_attributes, "crypto"))
sdp_attribute_remove(&m->m_attributes, "crypto");
while(sdp_attribute_find(m->m_attributes, "fingerprint"))
sdp_attribute_remove(&m->m_attributes, "fingerprint");
while(sdp_attribute_find(m->m_attributes, "setup"))
sdp_attribute_remove(&m->m_attributes, "setup");
while(sdp_attribute_find(m->m_attributes, "connection"))
sdp_attribute_remove(&m->m_attributes, "connection");
while(sdp_attribute_find(m->m_attributes, "group"))
sdp_attribute_remove(&m->m_attributes, "group");
while(sdp_attribute_find(m->m_attributes, "mid"))
sdp_attribute_remove(&m->m_attributes, "mid");
while(sdp_attribute_find(m->m_attributes, "msid"))
sdp_attribute_remove(&m->m_attributes, "msid");
while(sdp_attribute_find(m->m_attributes, "msid-semantic"))
sdp_attribute_remove(&m->m_attributes, "msid-semantic");
while(sdp_attribute_find(m->m_attributes, "rtcp"))
sdp_attribute_remove(&m->m_attributes, "rtcp");
while(sdp_attribute_find(m->m_attributes, "rtcp-mux"))
sdp_attribute_remove(&m->m_attributes, "rtcp-mux");
while(sdp_attribute_find(m->m_attributes, "candidate"))
sdp_attribute_remove(&m->m_attributes, "candidate");
while(sdp_attribute_find(m->m_attributes, "ssrc"))
sdp_attribute_remove(&m->m_attributes, "ssrc");
while(sdp_attribute_find(m->m_attributes, "ssrc-group"))
sdp_attribute_remove(&m->m_attributes, "ssrc-group");
while(sdp_attribute_find(m->m_attributes, "extmap")) /* TODO Actually implement RTP extensions */
sdp_attribute_remove(&m->m_attributes, "extmap");
while(sdp_attribute_find(m->m_attributes, "sctpmap"))
sdp_attribute_remove(&m->m_attributes, "sctpmap");
}
if(m->m_type != sdp_media_application && m->m_mode == sdp_sendrecv) {
/* FIXME sendrecv hack: sofia-sdp doesn't print sendrecv, but we want it to */
sdp_attribute_append(&m->m_attributes, &fakedir);
}
m = m->m_next;
}
}
char buf[JANUS_BUFSIZE];
sdp_printer_t *printer = sdp_print(home, anon, buf, JANUS_BUFSIZE, 0);
if(sdp_message(printer)) {
int retval = sdp_message_size(printer);
sdp_printer_free(printer);
sdp_parser_free(parser);
/* FIXME Take care of the sendrecv hack, if needed */
char *replace = strstr(buf, "a=jfmod:sr");
while(replace != NULL) {
memcpy(replace, "a=sendrecv", strlen("a=sendrecv"));
replace++;
replace = strstr(replace, "a=jfmod:sr");
}
PS_LOG(LOG_VERB, " -------------------------------------------\n");
PS_LOG(LOG_VERB, " >> Anonymized (%zu --> %d bytes)\n", strlen(sdp), retval);
PS_LOG(LOG_VERB, " -------------------------------------------\n");
PS_LOG(LOG_VERB, "%s\n", buf);
return g_strdup(buf);
} else {
PS_LOG(LOG_ERR, "Error anonymizing SDP: %s\n", sdp_printing_error(printer));
sdp_printer_free(printer);
sdp_parser_free(parser);
return NULL;
}
}
char *janus_sdp_merge(janus_ice_handle *handle, const char *origsdp) {
if(handle == NULL || origsdp == NULL)
return NULL;
sdp_session_t *anon = NULL;
sdp_parser_t *parser = sdp_parse(home, origsdp, strlen(origsdp), 0);
if(!(anon = sdp_session(parser))) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Error parsing/merging SDP: %s\n", handle->handle_id, sdp_parsing_error(parser));
sdp_parser_free(parser);
return NULL;
}
/* Prepare SDP to merge */
gchar buffer[512];
memset(buffer, 0, 512);
char *sdp = (char*)g_malloc0(JANUS_BUFSIZE);
if(sdp == NULL) {
PS_LOG(LOG_FATAL, "Memory error!\n");
sdp_parser_free(parser);
return NULL;
}
sdp[0] = '\0';
char *rtp_profile = handle->rtp_profile ? handle->rtp_profile : (char *)"RTP/SAVPF";
/* FIXME Any Plan B to take into account? */
int planb = strstr(origsdp, "a=planb:") ? 1 : 0;
if(planb) {
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PLAN_B);
} else {
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PLAN_B);
}
/* Version v= */
g_strlcat(sdp,
"v=0\r\n", JANUS_BUFSIZE);
/* Origin o= */
if(anon->sdp_origin) {
g_snprintf(buffer, 512,
"o=%s %"SCNu64" %"SCNu64" IN IP4 %s\r\n",
anon->sdp_origin->o_username ? anon->sdp_origin->o_username : "-",
anon->sdp_origin->o_id, anon->sdp_origin->o_version,
ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
} else {
gint64 sessid = ps_get_real_time();
gint64 version = sessid; /* FIXME This needs to be increased when it changes, so time should be ok */
g_snprintf(buffer, 512,
"o=%s %"SCNi64" %"SCNi64" IN IP4 %s\r\n",
"-", sessid, version, ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
/* Session name s= */
if(anon->sdp_subject && strlen(anon->sdp_subject) > 0) {
g_snprintf(buffer, 512, "s=%s\r\n", anon->sdp_subject);
} else {
g_snprintf(buffer, 512, "s=%s\r\n", "PacketServo Client");
}
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* Timing t= */
g_snprintf(buffer, 512,
"t=%lu %lu\r\n", anon->sdp_time ? anon->sdp_time->t_start : 0, anon->sdp_time ? anon->sdp_time->t_stop : 0);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* ICE Full or Lite? */
if(janus_ice_is_ice_lite_enabled()) {
/* Janus is acting in ICE Lite mode, advertize this */
g_strlcat(sdp, "a=ice-lite\r\n", JANUS_BUFSIZE);
}
/* bundle: add new global attribute */
int audio = (strstr(origsdp, "m=audio") != NULL);
int video = (strstr(origsdp, "m=video") != NULL);
#ifdef HAVE_SCTP
int data = (strstr(origsdp, "DTLS/SCTP") && !strstr(origsdp, " 0 DTLS/SCTP"));
#else
int data = 0;
#endif
g_strlcat(sdp, "a=group:BUNDLE", JANUS_BUFSIZE);
if(audio) {
g_snprintf(buffer, 512,
" %s", handle->audio_mid ? handle->audio_mid : "audio");
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
if(video) {
g_snprintf(buffer, 512,
" %s", handle->video_mid ? handle->video_mid : "video");
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
if(data) {
g_snprintf(buffer, 512,
" %s", handle->data_mid ? handle->data_mid : "data");
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
g_strlcat(sdp, "\r\n", JANUS_BUFSIZE);
/* msid-semantic: add new global attribute */
g_strlcat(sdp,
"a=msid-semantic: WMS PacketServo\r\n",
JANUS_BUFSIZE);
char wms[JANUS_BUFSIZE];
memset(wms, 0, JANUS_BUFSIZE);
g_strlcat(wms, "WMS", JANUS_BUFSIZE);
/* Copy other global attributes, if any */
if(anon->sdp_attributes) {
sdp_attribute_t *a = anon->sdp_attributes;
while(a) {
if(a->a_value == NULL) {
g_snprintf(buffer, 512,
"a=%s\r\n", a->a_name);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
} else {
g_snprintf(buffer, 512,
"a=%s:%s\r\n", a->a_name, a->a_value);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
a = a->a_next;
}
}
gboolean ipv6 = strstr(ps_get_public_ip(), ":") != NULL;
/* Media lines now */
if(anon->sdp_media) {
int audio = 0, video = 0;
#ifdef HAVE_SCTP
int data = 0;
#endif
sdp_media_t *m = anon->sdp_media;
janus_ice_stream *stream = NULL;
while(m) {
if(m->m_type == sdp_media_audio && m->m_port > 0) {
audio++;
if(audio > 1 || !handle->audio_id) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping audio line (we have %d audio lines, and the id is %d)\n", handle->handle_id, audio, handle->audio_id);
g_snprintf(buffer, 512,
"m=audio 0 %s 0\r\n", rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
g_strlcat(sdp, "a=inactive\r\n", JANUS_BUFSIZE);
m = m->m_next;
continue;
}
/* Audio */
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->audio_id));
if(stream == NULL) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping audio line (invalid stream %d)\n", handle->handle_id, handle->audio_id);
g_snprintf(buffer, 512,
"m=audio 0 %s 0\r\n", rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
g_strlcat(sdp, "a=inactive\r\n", JANUS_BUFSIZE);
m = m->m_next;
continue;
}
g_snprintf(buffer, 512,
"m=audio 1 %s", rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
} else if(m->m_type == sdp_media_video && m->m_port > 0) {
video++;
gint id = handle->video_id;
if(id == 0 && ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE))
id = handle->audio_id > 0 ? handle->audio_id : handle->video_id;
if(video > 1 || !id) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping video line (we have %d video lines, and the id is %d)\n", handle->handle_id, video,
ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE) ? handle->audio_id : handle->video_id);
g_snprintf(buffer, 512,
"m=video 0 %s 0\r\n", rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
g_strlcat(sdp, "a=inactive\r\n", JANUS_BUFSIZE);
m = m->m_next;
continue;
}
/* Video */
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(id));
if(stream == NULL) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping video line (invalid stream %d)\n", handle->handle_id, id);
g_snprintf(buffer, 512,
"m=video 0 %s 0\r\n", rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
g_strlcat(sdp, "a=inactive\r\n", JANUS_BUFSIZE);
m = m->m_next;
continue;
}
g_snprintf(buffer, 512,
"m=video 1 %s", rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
#ifdef HAVE_SCTP
} else if(m->m_type == sdp_media_application) {
/* Is this SCTP for DataChannels? */
if(m->m_port > 0 && m->m_proto_name != NULL && !strcasecmp(m->m_proto_name, "DTLS/SCTP") && m->m_port > 0) {
/* Yep */
data++;
gint id = handle->data_id;
if(id == 0 && ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE))
id = handle->audio_id > 0 ? handle->audio_id : handle->video_id;
if(data > 1 || !id) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping SCTP line (we have %d SCTP lines, and the id is %d)\n", handle->handle_id, data, id);
g_snprintf(buffer, 512,
"m=%s 0 %s 0\r\n",
m->m_type_name, m->m_proto_name);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
m = m->m_next;
continue;
}
/* SCTP */
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(id));
if(stream == NULL) {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping SCTP line (invalid stream %d)\n", handle->handle_id, id);
g_snprintf(buffer, 512,
"m=%s 0 %s 0\r\n",
m->m_type_name, m->m_proto_name);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
m = m->m_next;
continue;
}
g_strlcat(sdp, "m=application 1 DTLS/SCTP", JANUS_BUFSIZE);
} else {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping unsupported application media line...\n", handle->handle_id);
g_snprintf(buffer, 512,
"m=%s 0 %s 0\r\n",
m->m_type_name, m->m_proto_name);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
m = m->m_next;
continue;
}
#endif
} else {
PS_LOG(LOG_WARN, "[%"SCNu64"] Skipping disabled/unsupported media line...\n", handle->handle_id);
g_snprintf(buffer, 512,
"m=%s 0 %s 0\r\n",
m->m_type_name, m->m_type == sdp_media_application ? m->m_proto_name : rtp_profile);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* FIXME Adding a c-line anyway because otherwise Firefox complains? ("c= connection line not specified for every media level, validation failed") */
g_snprintf(buffer, 512,
"c=IN %s %s\r\n", ipv6 ? "IP6" : "IP4", ps_get_public_ip());
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
if(m->m_type == sdp_media_audio || m->m_type == sdp_media_video)
g_strlcat(sdp, "a=inactive\r\n", JANUS_BUFSIZE);
m = m->m_next;
continue;
}
/* Add formats now */
if(!m->m_rtpmaps) {
PS_LOG(LOG_VERB, "[%"SCNu64"] No RTP maps?? trying formats...\n", handle->handle_id);
if(!m->m_format) {
PS_LOG(LOG_ERR, "[%"SCNu64"] No formats either?? this sucks!\n", handle->handle_id);
g_strlcat(sdp, " 0", JANUS_BUFSIZE); /* FIXME Won't work apparently */
} else {
sdp_list_t *fmt = m->m_format;
while(fmt) {
g_snprintf(buffer, 512, " %s", fmt->l_text);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
fmt = fmt->l_next;
}
}
} else {