forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdp-utils.c
1764 lines (1720 loc) · 59 KB
/
sdp-utils.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-utils.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief SDP utilities
* \details Implementation of an internal SDP representation. Allows
* to parse SDP strings to an internal janus_sdp object, the manipulation
* of such object by playing with its properties, and a serialization
* to an SDP string that can be passed around. Since they don't have any
* core dependencies, these utilities can be used by plugins as well.
*
* \ingroup core
* \ref core
*/
#include <string.h>
#include "sdp-utils.h"
#include "rtp.h"
#include "utils.h"
#include "debug.h"
#define JANUS_BUFSIZE 8192
/* Preferred codecs when negotiating audio/video, and number of supported codecs */
const char *janus_preferred_audio_codecs[] = {
"opus", "multiopus", "pcmu", "pcma", "g722", "isac16", "isac32"
};
uint janus_audio_codecs = sizeof(janus_preferred_audio_codecs)/sizeof(*janus_preferred_audio_codecs);
const char *janus_preferred_video_codecs[] = {
"vp8", "vp9", "h264", "av1", "h265"
};
uint janus_video_codecs = sizeof(janus_preferred_video_codecs)/sizeof(*janus_preferred_video_codecs);
/* Reference counters management */
void janus_sdp_destroy(janus_sdp *sdp) {
if(!sdp || !g_atomic_int_compare_and_exchange(&sdp->destroyed, 0, 1))
return;
janus_refcount_decrease(&sdp->ref);
}
void janus_sdp_mline_destroy(janus_sdp_mline *m) {
if(!m || !g_atomic_int_compare_and_exchange(&m->destroyed, 0, 1))
return;
janus_refcount_decrease(&m->ref);
}
void janus_sdp_attribute_destroy(janus_sdp_attribute *a) {
if(!a || !g_atomic_int_compare_and_exchange(&a->destroyed, 0, 1))
return;
janus_refcount_decrease(&a->ref);
}
/* Internal frees */
static void janus_sdp_free(const janus_refcount *sdp_ref) {
janus_sdp *sdp = janus_refcount_containerof(sdp_ref, janus_sdp, ref);
/* This SDP instance can be destroyed, free all the resources */
g_free(sdp->o_name);
g_free(sdp->o_addr);
g_free(sdp->s_name);
g_free(sdp->c_addr);
GList *temp = sdp->attributes;
while(temp) {
janus_sdp_attribute *a = (janus_sdp_attribute *)temp->data;
janus_sdp_attribute_destroy(a);
temp = temp->next;
}
g_list_free(sdp->attributes);
sdp->attributes = NULL;
temp = sdp->m_lines;
while(temp) {
janus_sdp_mline *m = (janus_sdp_mline *)temp->data;
janus_sdp_mline_destroy(m);
temp = temp->next;
}
g_list_free(sdp->m_lines);
sdp->m_lines = NULL;
g_free(sdp);
}
static void janus_sdp_mline_free(const janus_refcount *mline_ref) {
janus_sdp_mline *mline = janus_refcount_containerof(mline_ref, janus_sdp_mline, ref);
/* This SDP m-line instance can be destroyed, free all the resources */
g_free(mline->type_str);
g_free(mline->proto);
g_free(mline->c_addr);
g_free(mline->b_name);
g_list_free_full(mline->fmts, (GDestroyNotify)g_free);
mline->fmts = NULL;
g_list_free(mline->ptypes);
mline->ptypes = NULL;
GList *temp = mline->attributes;
while(temp) {
janus_sdp_attribute *a = (janus_sdp_attribute *)temp->data;
janus_sdp_attribute_destroy(a);
temp = temp->next;
}
g_list_free(mline->attributes);
g_free(mline);
}
static void janus_sdp_attribute_free(const janus_refcount *attr_ref) {
janus_sdp_attribute *attr = janus_refcount_containerof(attr_ref, janus_sdp_attribute, ref);
/* This SDP attribute instance can be destroyed, free all the resources */
g_free(attr->name);
g_free(attr->value);
g_free(attr);
}
/* SDP and m-lines/attributes code */
janus_sdp_mline *janus_sdp_mline_create(janus_sdp_mtype type, guint16 port, const char *proto, janus_sdp_mdirection direction) {
janus_sdp_mline *m = g_malloc0(sizeof(janus_sdp_mline));
g_atomic_int_set(&m->destroyed, 0);
janus_refcount_init(&m->ref, janus_sdp_mline_free);
m->type = type;
const char *type_str = janus_sdp_mtype_str(type);
if(type_str == NULL) {
JANUS_LOG(LOG_WARN, "Unknown media type, type_str will have to be set manually\n");
} else {
m->type_str = g_strdup(type_str);
}
m->port = port;
m->proto = proto ? g_strdup(proto) : NULL;
m->direction = direction;
return m;
}
janus_sdp_mline *janus_sdp_mline_find(janus_sdp *sdp, janus_sdp_mtype type) {
if(sdp == NULL)
return NULL;
GList *ml = sdp->m_lines;
while(ml) {
janus_sdp_mline *m = (janus_sdp_mline *)ml->data;
if(m->type == type)
return m;
ml = ml->next;
}
return NULL;
}
int janus_sdp_mline_remove(janus_sdp *sdp, janus_sdp_mtype type) {
if(sdp == NULL)
return -1;
GList *ml = sdp->m_lines;
while(ml) {
janus_sdp_mline *m = (janus_sdp_mline *)ml->data;
if(m->type == type) {
/* Found! */
sdp->m_lines = g_list_remove(sdp->m_lines, m);
janus_sdp_mline_destroy(m);
return 0;
}
ml = ml->next;
}
/* If we got here, we couldn't the m-line */
return -2;
}
janus_sdp_attribute *janus_sdp_attribute_create(const char *name, const char *value, ...) {
if(!name)
return NULL;
janus_sdp_attribute *a = g_malloc(sizeof(janus_sdp_attribute));
g_atomic_int_set(&a->destroyed, 0);
janus_refcount_init(&a->ref, janus_sdp_attribute_free);
a->name = g_strdup(name);
a->direction = JANUS_SDP_DEFAULT;
a->value = NULL;
if(value) {
char buffer[512];
va_list ap;
va_start(ap, value);
g_vsnprintf(buffer, sizeof(buffer), value, ap);
va_end(ap);
a->value = g_strdup(buffer);
}
return a;
}
int janus_sdp_attribute_add_to_mline(janus_sdp_mline *mline, janus_sdp_attribute *attr) {
if(!mline || !attr)
return -1;
mline->attributes = g_list_append(mline->attributes, attr);
return 0;
}
janus_sdp_mtype janus_sdp_parse_mtype(const char *type) {
if(type == NULL)
return JANUS_SDP_OTHER;
if(!strcasecmp(type, "audio"))
return JANUS_SDP_AUDIO;
if(!strcasecmp(type, "video"))
return JANUS_SDP_VIDEO;
if(!strcasecmp(type, "application"))
return JANUS_SDP_APPLICATION;
return JANUS_SDP_OTHER;
}
const char *janus_sdp_mtype_str(janus_sdp_mtype type) {
switch(type) {
case JANUS_SDP_AUDIO:
return "audio";
case JANUS_SDP_VIDEO:
return "video";
case JANUS_SDP_APPLICATION:
return "application";
case JANUS_SDP_OTHER:
default:
break;
}
return NULL;
}
janus_sdp_mdirection janus_sdp_parse_mdirection(const char *direction) {
if(direction == NULL)
return JANUS_SDP_INVALID;
if(!strcasecmp(direction, "sendrecv"))
return JANUS_SDP_SENDRECV;
if(!strcasecmp(direction, "sendonly"))
return JANUS_SDP_SENDONLY;
if(!strcasecmp(direction, "recvonly"))
return JANUS_SDP_RECVONLY;
if(!strcasecmp(direction, "inactive"))
return JANUS_SDP_INACTIVE;
return JANUS_SDP_INVALID;
}
const char *janus_sdp_mdirection_str(janus_sdp_mdirection direction) {
switch(direction) {
case JANUS_SDP_DEFAULT:
case JANUS_SDP_SENDRECV:
return "sendrecv";
case JANUS_SDP_SENDONLY:
return "sendonly";
case JANUS_SDP_RECVONLY:
return "recvonly";
case JANUS_SDP_INACTIVE:
return "inactive";
case JANUS_SDP_INVALID:
default:
break;
}
return NULL;
}
janus_sdp *janus_sdp_parse(const char *sdp, char *error, size_t errlen) {
if(!sdp)
return NULL;
if(strstr(sdp, "v=") != sdp) {
if(error)
g_snprintf(error, errlen, "Invalid SDP (doesn't start with v=)");
return NULL;
}
janus_sdp *imported = g_malloc0(sizeof(janus_sdp));
g_atomic_int_set(&imported->destroyed, 0);
janus_refcount_init(&imported->ref, janus_sdp_free);
imported->o_ipv4 = TRUE;
imported->c_ipv4 = TRUE;
gboolean success = TRUE;
janus_sdp_mline *mline = NULL;
gchar **parts = g_strsplit(sdp, "\n", -1);
if(parts) {
int index = 0;
char *line = NULL, *cr = NULL;
while(success && (line = parts[index]) != NULL) {
cr = strchr(line, '\r');
if(cr != NULL)
*cr = '\0';
if(*line == '\0') {
if(cr != NULL)
*cr = '\r';
index++;
continue;
}
if(strlen(line) < 3) {
if(error)
g_snprintf(error, errlen, "Invalid line (%zu bytes): %s", strlen(line), line);
success = FALSE;
break;
}
if(*(line+1) != '=') {
if(error)
g_snprintf(error, errlen, "Invalid line (2nd char is not '='): %s", line);
success = FALSE;
break;
}
char c = *line;
if(mline == NULL) {
/* Global stuff */
switch(c) {
case 'v': {
if(sscanf(line, "v=%d", &imported->version) != 1) {
if(error)
g_snprintf(error, errlen, "Invalid v= line: %s", line);
success = FALSE;
break;
}
break;
}
case 'o': {
if(imported->o_name || imported->o_addr) {
if(error)
g_snprintf(error, errlen, "Multiple o= lines: %s", line);
success = FALSE;
break;
}
char name[256], addrtype[6], addr[256];
if(sscanf(line, "o=%255s %"SCNu64" %"SCNu64" IN %5s %255s",
name, &imported->o_sessid, &imported->o_version, addrtype, addr) != 5) {
if(error)
g_snprintf(error, errlen, "Invalid o= line: %s", line);
success = FALSE;
break;
}
if(!strcasecmp(addrtype, "IP4"))
imported->o_ipv4 = TRUE;
else if(!strcasecmp(addrtype, "IP6"))
imported->o_ipv4 = FALSE;
else {
if(error)
g_snprintf(error, errlen, "Invalid o= line (unsupported protocol %s): %s", addrtype, line);
success = FALSE;
break;
}
imported->o_name = g_strdup(name);
imported->o_addr = g_strdup(addr);
break;
}
case 's': {
if(imported->s_name) {
if(error)
g_snprintf(error, errlen, "Multiple s= lines: %s", line);
success = FALSE;
break;
}
imported->s_name = g_strdup(line+2);
break;
}
case 't': {
if(sscanf(line, "t=%"SCNu64" %"SCNu64, &imported->t_start, &imported->t_stop) != 2) {
if(error)
g_snprintf(error, errlen, "Invalid t= line: %s", line);
success = FALSE;
break;
}
break;
}
case 'c': {
if(imported->c_addr) {
if(error)
g_snprintf(error, errlen, "Multiple global c= lines: %s", line);
success = FALSE;
break;
}
char addrtype[6], addr[256];
if(sscanf(line, "c=IN %5s %255s", addrtype, addr) != 2) {
if(error)
g_snprintf(error, errlen, "Invalid c= line: %s", line);
success = FALSE;
break;
}
if(!strcasecmp(addrtype, "IP4"))
imported->c_ipv4 = TRUE;
else if(!strcasecmp(addrtype, "IP6"))
imported->c_ipv4 = FALSE;
else {
if(error)
g_snprintf(error, errlen, "Invalid c= line (unsupported protocol %s): %s", addrtype, line);
success = FALSE;
break;
}
imported->c_addr = g_strdup(addr);
break;
}
case 'a': {
janus_sdp_attribute *a = g_malloc0(sizeof(janus_sdp_attribute));
janus_refcount_init(&a->ref, janus_sdp_attribute_free);
line += 2;
char *semicolon = strchr(line, ':');
if(semicolon == NULL) {
a->name = g_strdup(line);
a->value = NULL;
} else {
if(*(semicolon+1) == '\0') {
janus_sdp_attribute_destroy(a);
if(error)
g_snprintf(error, errlen, "Invalid a= line: %s", line);
success = FALSE;
break;
}
*semicolon = '\0';
a->name = g_strdup(line);
a->value = g_strdup(semicolon+1);
a->direction = JANUS_SDP_DEFAULT;
*semicolon = ':';
if(strstr(line, "/sendonly"))
a->direction = JANUS_SDP_SENDONLY;
else if(strstr(line, "/recvonly"))
a->direction = JANUS_SDP_RECVONLY;
if(strstr(line, "/inactive"))
a->direction = JANUS_SDP_INACTIVE;
}
imported->attributes = g_list_append(imported->attributes, a);
break;
}
case 'm': {
janus_sdp_mline *m = g_malloc0(sizeof(janus_sdp_mline));
g_atomic_int_set(&m->destroyed, 0);
janus_refcount_init(&m->ref, janus_sdp_mline_free);
/* Start with media type, port and protocol */
char type[32];
char proto[64];
if(strlen(line) > 200) {
janus_sdp_mline_destroy(m);
if(error)
g_snprintf(error, errlen, "Invalid m= line (too long): %zu", strlen(line));
success = FALSE;
break;
}
if(sscanf(line, "m=%31s %"SCNu16" %63s %*s", type, &m->port, proto) != 3) {
janus_sdp_mline_destroy(m);
if(error)
g_snprintf(error, errlen, "Invalid m= line: %s", line);
success = FALSE;
break;
}
m->type = janus_sdp_parse_mtype(type);
m->type_str = g_strdup(type);
m->proto = g_strdup(proto);
m->direction = JANUS_SDP_SENDRECV;
m->c_ipv4 = TRUE;
if(m->port > 0) {
/* Now let's check the payload types/formats */
gchar **mline_parts = g_strsplit(line+2, " ", -1);
if(!mline_parts) {
janus_sdp_mline_destroy(m);
if(error)
g_snprintf(error, errlen, "Invalid m= line (no payload types/formats): %s", line);
success = FALSE;
break;
}
int mindex = 0;
while(mline_parts[mindex]) {
if(mindex < 3) {
/* We've parsed these before */
mindex++;
continue;
}
/* Add string fmt */
m->fmts = g_list_append(m->fmts, g_strdup(mline_parts[mindex]));
/* Add numeric payload type */
int ptype = atoi(mline_parts[mindex]);
if(ptype < 0) {
JANUS_LOG(LOG_ERR, "Invalid payload type (%s)\n", mline_parts[mindex]);
} else {
m->ptypes = g_list_append(m->ptypes, GINT_TO_POINTER(ptype));
}
mindex++;
}
g_strfreev(mline_parts);
if(m->fmts == NULL || m->ptypes == NULL) {
janus_sdp_mline_destroy(m);
if(error)
g_snprintf(error, errlen, "Invalid m= line (no payload types/formats): %s", line);
success = FALSE;
break;
}
}
/* Append to the list of m-lines */
imported->m_lines = g_list_append(imported->m_lines, m);
/* From now on, we parse this m-line */
mline = m;
break;
}
default:
JANUS_LOG(LOG_WARN, "Ignoring '%c' property\n", c);
break;
}
} else {
/* m-line stuff */
switch(c) {
case 'c': {
if(mline->c_addr) {
if(error)
g_snprintf(error, errlen, "Multiple m-line c= lines: %s", line);
success = FALSE;
break;
}
char addrtype[6], addr[256];
if(sscanf(line, "c=IN %5s %255s", addrtype, addr) != 2) {
if(error)
g_snprintf(error, errlen, "Invalid c= line: %s", line);
success = FALSE;
break;
}
if(!strcasecmp(addrtype, "IP4"))
mline->c_ipv4 = TRUE;
else if(!strcasecmp(addrtype, "IP6"))
mline->c_ipv4 = FALSE;
else {
if(error)
g_snprintf(error, errlen, "Invalid c= line (unsupported protocol %s): %s", addrtype, line);
success = FALSE;
break;
}
mline->c_addr = g_strdup(addr);
break;
}
case 'b': {
if(mline->b_name) {
if(error)
g_snprintf(error, errlen, "Multiple m-line b= lines: %s", line);
success = FALSE;
break;
}
line += 2;
char *semicolon = strchr(line, ':');
if(semicolon == NULL || (*(semicolon+1) == '\0')) {
if(error)
g_snprintf(error, errlen, "Invalid b= line: %s", line);
success = FALSE;
break;
}
*semicolon = '\0';
if(strcmp(line, "AS") && strcmp(line, "TIAS")) {
/* We only support b=AS and b=TIAS, skip */
break;
}
mline->b_name = g_strdup(line);
mline->b_value = atol(semicolon+1);
*semicolon = ':';
break;
}
case 'a': {
janus_sdp_attribute *a = g_malloc0(sizeof(janus_sdp_attribute));
janus_refcount_init(&a->ref, janus_sdp_attribute_free);
line += 2;
char *semicolon = strchr(line, ':');
if(semicolon == NULL) {
/* Is this a media direction attribute? */
janus_sdp_mdirection direction = janus_sdp_parse_mdirection(line);
if(direction != JANUS_SDP_INVALID) {
janus_sdp_attribute_destroy(a);
mline->direction = direction;
break;
}
a->name = g_strdup(line);
a->value = NULL;
} else {
if(*(semicolon+1) == '\0') {
janus_sdp_attribute_destroy(a);
if(error)
g_snprintf(error, errlen, "Invalid a= line: %s", line);
success = FALSE;
break;
}
*semicolon = '\0';
a->name = g_strdup(line);
a->value = g_strdup(semicolon+1);
a->direction = JANUS_SDP_DEFAULT;
*semicolon = ':';
if(strstr(line, "/sendonly"))
a->direction = JANUS_SDP_SENDONLY;
else if(strstr(line, "/recvonly"))
a->direction = JANUS_SDP_RECVONLY;
if(strstr(line, "/inactive"))
a->direction = JANUS_SDP_INACTIVE;
}
mline->attributes = g_list_append(mline->attributes, a);
break;
}
case 'm': {
/* Current m-line ended, back to global parsing */
mline = NULL;
continue;
}
default:
JANUS_LOG(LOG_WARN, "Ignoring '%c' property (m-line)\n", c);
break;
}
}
if(cr != NULL)
*cr = '\r';
index++;
}
if(cr != NULL)
*cr = '\r';
g_strfreev(parts);
}
/* FIXME Do a last check: is all the stuff that's supposed to be there available? */
if(success && (imported->o_name == NULL || imported->o_addr == NULL || imported->s_name == NULL || imported->m_lines == NULL)) {
success = FALSE;
if(error)
g_snprintf(error, errlen, "Missing mandatory lines (o=, s= or m=)");
}
/* If something wrong happened, free and return a failure */
if(!success) {
if(error)
JANUS_LOG(LOG_ERR, "%s\n", error);
janus_sdp_destroy(imported);
imported = NULL;
}
return imported;
}
int janus_sdp_remove_payload_type(janus_sdp *sdp, int pt) {
if(!sdp || pt < 0)
return -1;
GList *ml = sdp->m_lines;
while(ml) {
janus_sdp_mline *m = (janus_sdp_mline *)ml->data;
/* Remove any reference from the m-line */
m->ptypes = g_list_remove(m->ptypes, GINT_TO_POINTER(pt));
/* Also remove all attributes that reference the same payload type */
GList *ma = m->attributes;
while(ma) {
janus_sdp_attribute *a = (janus_sdp_attribute *)ma->data;
if(a->value && atoi(a->value) == pt) {
m->attributes = g_list_remove(m->attributes, a);
ma = m->attributes;
janus_sdp_attribute_destroy(a);
continue;
}
ma = ma->next;
}
ml = ml->next;
}
return 0;
}
int janus_sdp_get_codec_pt(janus_sdp *sdp, const char *codec) {
return janus_sdp_get_codec_pt_full(sdp, codec, NULL);
}
int janus_sdp_get_codec_pt_full(janus_sdp *sdp, const char *codec, const char *profile) {
if(sdp == NULL || codec == NULL)
return -1;
/* Check the format string (note that we only parse what browsers can negotiate) */
gboolean video = FALSE, vp9 = FALSE, h264 = FALSE;
const char *format = NULL, *format2 = NULL;
if(!strcasecmp(codec, "opus")) {
format = "opus/48000/2";
format2 = "OPUS/48000/2";
} else if(!strcasecmp(codec, "multiopus")) {
/* FIXME We're hardcoding to 6 channels, for now */
format = "multiopus/48000/6";
format2 = "MULTIOPUS/48000/6";
} else if(!strcasecmp(codec, "pcmu")) {
/* We know the payload type is 0: we just need to make sure it's there */
format = "pcmu/8000";
format2 = "PCMU/8000";
} else if(!strcasecmp(codec, "pcma")) {
/* We know the payload type is 8: we just need to make sure it's there */
format = "pcma/8000";
format2 = "PCMA/8000";
} else if(!strcasecmp(codec, "g722")) {
/* We know the payload type is 9: we just need to make sure it's there */
format = "g722/8000";
format2 = "G722/8000";
} else if(!strcasecmp(codec, "isac16")) {
format = "isac/16000";
format2 = "ISAC/16000";
} else if(!strcasecmp(codec, "isac32")) {
format = "isac/32000";
format2 = "ISAC/32000";
} else if(!strcasecmp(codec, "dtmf")) {
format = "telephone-event/8000";
format2 = "TELEPHONE-EVENT/8000";
} else if(!strcasecmp(codec, "vp8")) {
video = TRUE;
format = "vp8/90000";
format2 = "VP8/90000";
} else if(!strcasecmp(codec, "vp9")) {
video = TRUE;
vp9 = TRUE; /* We may need to filter on profiles */
format = "vp9/90000";
format2 = "VP9/90000";
} else if(!strcasecmp(codec, "h264")) {
video = TRUE;
h264 = TRUE; /* We may need to filter on profiles */
format = "h264/90000";
format2 = "H264/90000";
} else if(!strcasecmp(codec, "av1")) {
video = TRUE;
format = "av1x/90000";
format2 = "AV1X/90000";
} else if(!strcasecmp(codec, "h265")) {
video = TRUE;
format = "h265/90000";
format2 = "H265/90000";
} else {
JANUS_LOG(LOG_ERR, "Unsupported codec '%s'\n", codec);
return -1;
}
/* Check all m->lines */
GList *ml = sdp->m_lines;
while(ml) {
janus_sdp_mline *m = (janus_sdp_mline *)ml->data;
if((!video && m->type != JANUS_SDP_AUDIO) || (video && m->type != JANUS_SDP_VIDEO)) {
ml = ml->next;
continue;
}
/* Look in all rtpmap attributes */
GList *ma = m->attributes;
int pt = -1;
gboolean check_profile = FALSE;
gboolean got_profile = FALSE;
while(ma) {
janus_sdp_attribute *a = (janus_sdp_attribute *)ma->data;
if(profile != NULL && a->name != NULL && a->value != NULL && !strcasecmp(a->name, "fmtp")) {
if(vp9) {
char profile_id[20];
g_snprintf(profile_id, sizeof(profile_id), "profile-id=%s", profile);
if(strstr(a->value, profile_id) != NULL) {
/* Found */
JANUS_LOG(LOG_VERB, "VP9 profile %s found --> %d\n", profile, pt);
if(check_profile) {
return pt;
} else {
got_profile = TRUE;
}
}
} else if(h264 && strstr(a->value, "packetization-mode=0") == NULL) {
/* We only support packetization-mode=1, no matter the profile */
char profile_level_id[30];
char *profile_lower = g_ascii_strdown(profile, -1);
g_snprintf(profile_level_id, sizeof(profile_level_id), "profile-level-id=%s", profile_lower);
g_free(profile_lower);
if(strstr(a->value, profile_level_id) != NULL) {
/* Found */
JANUS_LOG(LOG_VERB, "H.264 profile %s found --> %d\n", profile, pt);
if(check_profile) {
return pt;
} else {
got_profile = TRUE;
}
}
/* Not found, try converting the profile to upper case */
char *profile_upper = g_ascii_strup(profile, -1);
g_snprintf(profile_level_id, sizeof(profile_level_id), "profile-level-id=%s", profile_upper);
g_free(profile_upper);
if(strstr(a->value, profile_level_id) != NULL) {
/* Found */
JANUS_LOG(LOG_VERB, "H.264 profile %s found --> %d\n", profile, pt);
if(check_profile) {
return pt;
} else {
got_profile = TRUE;
}
}
}
} else if(a->name != NULL && a->value != NULL && !strcasecmp(a->name, "rtpmap")) {
pt = atoi(a->value);
check_profile = FALSE;
if(pt < 0) {
JANUS_LOG(LOG_ERR, "Invalid payload type (%s)\n", a->value);
} else if(strstr(a->value, format) || strstr(a->value, format2)) {
if(profile != NULL && !got_profile && (vp9 || h264)) {
/* Let's check the profile first */
check_profile = TRUE;
} else {
/* Payload type for codec found */
return pt;
}
}
}
ma = ma->next;
}
ml = ml->next;
}
return -1;
}
const char *janus_sdp_get_codec_name(janus_sdp *sdp, int pt) {
if(sdp == NULL || pt < 0)
return NULL;
if(pt == 0)
return "pcmu";
if(pt == 8)
return "pcma";
if(pt == 9)
return "g722";
GList *ml = sdp->m_lines;
while(ml) {
janus_sdp_mline *m = (janus_sdp_mline *)ml->data;
/* Look in all rtpmap attributes */
GList *ma = m->attributes;
while(ma) {
janus_sdp_attribute *a = (janus_sdp_attribute *)ma->data;
if(a->name != NULL && a->value != NULL && !strcasecmp(a->name, "rtpmap")) {
int a_pt = atoi(a->value);
if(a_pt == pt) {
/* Found! */
if(strstr(a->value, "vp8") || strstr(a->value, "VP8"))
return "vp8";
if(strstr(a->value, "vp9") || strstr(a->value, "VP9"))
return "vp9";
if(strstr(a->value, "h264") || strstr(a->value, "H264"))
return "h264";
if(strstr(a->value, "av1") || strstr(a->value, "AV1"))
return "av1";
if(strstr(a->value, "h265") || strstr(a->value, "H265"))
return "h265";
if(strstr(a->value, "multiopus") || strstr(a->value, "MULTIOPUS"))
return "multiopus";
if(strstr(a->value, "opus") || strstr(a->value, "OPUS"))
return "opus";
if(strstr(a->value, "pcmu") || strstr(a->value, "PCMU"))
return "pcmu";
if(strstr(a->value, "pcma") || strstr(a->value, "PCMA"))
return "pcma";
if(strstr(a->value, "g722") || strstr(a->value, "G722"))
return "g722";
if(strstr(a->value, "isac/16") || strstr(a->value, "ISAC/16"))
return "isac16";
if(strstr(a->value, "isac/32") || strstr(a->value, "ISAC/32"))
return "isac32";
if(strstr(a->value, "telephone-event/8000") || strstr(a->value, "telephone-event/8000"))
return "dtmf";
JANUS_LOG(LOG_ERR, "Unsupported codec '%s'\n", a->value);
return NULL;
}
}
ma = ma->next;
}
ml = ml->next;
}
return NULL;
}
const char *janus_sdp_get_codec_rtpmap(const char *codec) {
if(codec == NULL)
return NULL;
if(!strcasecmp(codec, "opus"))
return "opus/48000/2";
if(!strcasecmp(codec, "multiopus"))
/* FIXME We're hardcoding to 6 channels, for now */
return "multiopus/48000/6";
if(!strcasecmp(codec, "pcmu"))
return "PCMU/8000";
if(!strcasecmp(codec, "pcma"))
return "PCMA/8000";
if(!strcasecmp(codec, "g722"))
return "G722/8000";
if(!strcasecmp(codec, "isac16"))
return "ISAC/16000";
if(!strcasecmp(codec, "isac32"))
return "ISAC/32000";
if(!strcasecmp(codec, "dtmf"))
return "telephone-event/8000";
if(!strcasecmp(codec, "vp8"))
return "VP8/90000";
if(!strcasecmp(codec, "vp9"))
return "VP9/90000";
if(!strcasecmp(codec, "h264"))
return "H264/90000";
if(!strcasecmp(codec, "av1"))
return "AV1X/90000";
if(!strcasecmp(codec, "h265"))
return "H265/90000";
JANUS_LOG(LOG_ERR, "Unsupported codec '%s'\n", codec);
return NULL;
}
const char *janus_sdp_get_fmtp(janus_sdp *sdp, int pt) {
if(sdp == NULL || pt < 0)
return NULL;
GList *ml = sdp->m_lines;
while(ml) {
janus_sdp_mline *m = (janus_sdp_mline *)ml->data;
/* Look in all rtpmap attributes */
GList *ma = m->attributes;
while(ma) {
janus_sdp_attribute *a = (janus_sdp_attribute *)ma->data;
if(a->name != NULL && a->value != NULL && !strcasecmp(a->name, "fmtp")) {
int a_pt = atoi(a->value);
if(a_pt == pt) {
/* Found! */
char needle[10];
g_snprintf(needle, sizeof(needle), "%d ", pt);
if(strstr(a->value, needle) == a->value)
return a->value + strlen(needle);
}
}
ma = ma->next;
}
ml = ml->next;
}
return NULL;
}
char *janus_sdp_write(janus_sdp *imported) {
if(!imported)
return NULL;
janus_refcount_increase(&imported->ref);
char *sdp = g_malloc(JANUS_BUFSIZE), buffer[512];
*sdp = '\0';
/* v= */
g_snprintf(buffer, sizeof(buffer), "v=%d\r\n", imported->version);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* o= */
g_snprintf(buffer, sizeof(buffer), "o=%s %"SCNu64" %"SCNu64" IN %s %s\r\n",
imported->o_name, imported->o_sessid, imported->o_version,
imported->o_ipv4 ? "IP4" : "IP6", imported->o_addr);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* s= */
g_snprintf(buffer, sizeof(buffer), "s=%s\r\n", imported->s_name);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* t= */
g_snprintf(buffer, sizeof(buffer), "t=%"SCNu64" %"SCNu64"\r\n", imported->t_start, imported->t_stop);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
/* c= */
if(imported->c_addr != NULL) {
g_snprintf(buffer, sizeof(buffer), "c=IN %s %s\r\n",
imported->c_ipv4 ? "IP4" : "IP6", imported->c_addr);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
/* a= */
GList *temp = imported->attributes;
while(temp) {
janus_sdp_attribute *a = (janus_sdp_attribute *)temp->data;
if(a->value != NULL) {
g_snprintf(buffer, sizeof(buffer), "a=%s:%s\r\n", a->name, a->value);
} else {
g_snprintf(buffer, sizeof(buffer), "a=%s\r\n", a->name);
}
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
temp = temp->next;
}
/* m= */
temp = imported->m_lines;
while(temp) {
janus_sdp_mline *m = (janus_sdp_mline *)temp->data;
g_snprintf(buffer, sizeof(buffer), "m=%s %d %s", m->type_str, m->port, m->proto);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
if(m->port == 0) {
/* Remove all payload types/formats if we're rejecting the media */
g_list_free_full(m->fmts, (GDestroyNotify)g_free);
m->fmts = NULL;
g_list_free(m->ptypes);
m->ptypes = NULL;
m->ptypes = g_list_append(m->ptypes, GINT_TO_POINTER(0));
g_strlcat(sdp, " 0", JANUS_BUFSIZE);
} else {
if(m->proto != NULL && strstr(m->proto, "RTP") != NULL) {
/* RTP profile, use payload types */
GList *ptypes = m->ptypes;
while(ptypes) {
g_snprintf(buffer, sizeof(buffer), " %d", GPOINTER_TO_INT(ptypes->data));
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
ptypes = ptypes->next;
}
} else {
/* Something else, use formats */
GList *fmts = m->fmts;
while(fmts) {
g_snprintf(buffer, sizeof(buffer), " %s", (char *)(fmts->data));
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
fmts = fmts->next;
}
}
}
g_strlcat(sdp, "\r\n", JANUS_BUFSIZE);
/* c= */
if(m->c_addr != NULL) {
g_snprintf(buffer, sizeof(buffer), "c=IN %s %s\r\n",
m->c_ipv4 ? "IP4" : "IP6", m->c_addr);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
if(m->port > 0) {
/* b= */
if(m->b_name != NULL) {
g_snprintf(buffer, sizeof(buffer), "b=%s:%"SCNu32"\r\n", m->b_name, m->b_value);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
}
/* a= (note that we don't format the direction if it's JANUS_SDP_DEFAULT) */
const char *direction = m->direction != JANUS_SDP_DEFAULT ? janus_sdp_mdirection_str(m->direction) : NULL;
if(direction != NULL) {
g_snprintf(buffer, sizeof(buffer), "a=%s\r\n", direction);
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
}
GList *temp2 = m->attributes;
while(temp2) {
janus_sdp_attribute *a = (janus_sdp_attribute *)temp2->data;
if(m->port == 0 && strcasecmp(a->name, "mid")) {
/* This media has been rejected or disabled: we only add the mid attribute, if available */
temp2 = temp2->next;
continue;
}
if(a->value != NULL) {
g_snprintf(buffer, sizeof(buffer), "a=%s:%s\r\n", a->name, a->value);
} else {
g_snprintf(buffer, sizeof(buffer), "a=%s\r\n", a->name);
}
g_strlcat(sdp, buffer, JANUS_BUFSIZE);
temp2 = temp2->next;
}
temp = temp->next;