-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathSessionDescription.c
1376 lines (1195 loc) · 75.7 KB
/
SessionDescription.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
#define LOG_CLASS "SessionDescription"
#include "../Include_i.h"
STATUS serializeSessionDescriptionInit(PRtcSessionDescriptionInit pSessionDescriptionInit, PCHAR sessionDescriptionJSON,
PUINT32 sessionDescriptionJSONLen)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PCHAR curr, tail, next;
UINT32 lineLen, inputSize = 0, amountWritten;
CHK(pSessionDescriptionInit != NULL && sessionDescriptionJSONLen != NULL, STATUS_NULL_ARG);
inputSize = *sessionDescriptionJSONLen;
*sessionDescriptionJSONLen = 0;
amountWritten =
SNPRINTF(sessionDescriptionJSON, sessionDescriptionJSON == NULL ? 0 : inputSize - *sessionDescriptionJSONLen,
SESSION_DESCRIPTION_INIT_TEMPLATE_HEAD, pSessionDescriptionInit->type == SDP_TYPE_OFFER ? SDP_OFFER_VALUE : SDP_ANSWER_VALUE);
CHK(sessionDescriptionJSON == NULL || ((inputSize - *sessionDescriptionJSONLen) >= amountWritten), STATUS_BUFFER_TOO_SMALL);
*sessionDescriptionJSONLen += amountWritten;
curr = pSessionDescriptionInit->sdp;
tail = pSessionDescriptionInit->sdp + STRLEN(pSessionDescriptionInit->sdp);
while ((next = STRNCHR(curr, (UINT32) (tail - curr), '\n')) != NULL) {
lineLen = (UINT32) (next - curr);
if (lineLen > 0 && curr[lineLen - 1] == '\r') {
lineLen--;
}
amountWritten =
SNPRINTF(sessionDescriptionJSON + *sessionDescriptionJSONLen, sessionDescriptionJSON == NULL ? 0 : inputSize - *sessionDescriptionJSONLen,
"%*.*s%s", lineLen, lineLen, curr, SESSION_DESCRIPTION_INIT_LINE_ENDING);
CHK(sessionDescriptionJSON == NULL || ((inputSize - *sessionDescriptionJSONLen) >= amountWritten), STATUS_BUFFER_TOO_SMALL);
*sessionDescriptionJSONLen += amountWritten;
curr = next + 1;
}
amountWritten = SNPRINTF(sessionDescriptionJSON + *sessionDescriptionJSONLen,
sessionDescriptionJSON == NULL ? 0 : inputSize - *sessionDescriptionJSONLen, SESSION_DESCRIPTION_INIT_TEMPLATE_TAIL);
CHK(sessionDescriptionJSON == NULL || ((inputSize - *sessionDescriptionJSONLen) >= amountWritten), STATUS_BUFFER_TOO_SMALL);
*sessionDescriptionJSONLen += (amountWritten + 1); // NULL terminator
CleanUp:
LEAVES();
return retStatus;
}
STATUS deserializeSessionDescriptionInit(PCHAR sessionDescriptionJSON, UINT32 sessionDescriptionJSONLen,
PRtcSessionDescriptionInit pSessionDescriptionInit)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
jsmntok_t tokens[MAX_JSON_TOKEN_COUNT];
jsmn_parser parser;
INT32 i, j, tokenCount, lineLen;
PCHAR curr, next, tail;
CHK(pSessionDescriptionInit != NULL && sessionDescriptionJSON != NULL, STATUS_NULL_ARG);
MEMSET(pSessionDescriptionInit, 0x00, SIZEOF(RtcSessionDescriptionInit));
jsmn_init(&parser);
tokenCount = jsmn_parse(&parser, sessionDescriptionJSON, sessionDescriptionJSONLen, tokens, ARRAY_SIZE(tokens));
CHK(tokenCount > 1, STATUS_INVALID_API_CALL_RETURN_JSON);
CHK(tokens[0].type == JSMN_OBJECT, STATUS_SESSION_DESCRIPTION_INIT_NOT_OBJECT);
for (i = 1; i < tokenCount; i += 2) {
if (STRNCMP(SDP_TYPE_KEY, sessionDescriptionJSON + tokens[i].start, ARRAY_SIZE(SDP_TYPE_KEY) - 1) == 0) {
if (STRNCMP(SDP_OFFER_VALUE, sessionDescriptionJSON + tokens[i + 1].start, ARRAY_SIZE(SDP_OFFER_VALUE) - 1) == 0) {
pSessionDescriptionInit->type = SDP_TYPE_OFFER;
} else if (STRNCMP(SDP_ANSWER_VALUE, sessionDescriptionJSON + tokens[i + 1].start, ARRAY_SIZE(SDP_ANSWER_VALUE) - 1) == 0) {
pSessionDescriptionInit->type = SDP_TYPE_ANSWER;
} else {
CHK(FALSE, STATUS_SESSION_DESCRIPTION_INIT_INVALID_TYPE);
}
} else if (STRNCMP(SDP_KEY, sessionDescriptionJSON + tokens[i].start, ARRAY_SIZE(SDP_KEY) - 1) == 0) {
CHK((tokens[i + 1].end - tokens[i + 1].start) <= MAX_SESSION_DESCRIPTION_INIT_SDP_LEN,
STATUS_SESSION_DESCRIPTION_INIT_MAX_SDP_LEN_EXCEEDED);
curr = sessionDescriptionJSON + tokens[i + 1].start;
tail = sessionDescriptionJSON + tokens[i + 1].end;
j = 0;
// Unescape carriage return and line feed characters. The SDP that we receive at this point is in
// JSON format, meaning that carriage return and line feed characters are escaped. So, to represent
// these characters, a single escape character is prepended to each of them.
//
// When we store the sdp in memory, we want to recover the original format, without the escape characters.
//
// For example:
// \r becomes '\' and 'r'
// \n becomes '\' and 'n'
while ((next = STRNSTR(curr, SESSION_DESCRIPTION_INIT_LINE_ENDING_WITHOUT_CR, tail - curr)) != NULL) {
lineLen = (INT32) (next - curr);
// Check if the SDP format is using \r\n or \n separator.
// There are escape characters before \n and \r, so we need to move back 1 more character
if (lineLen > 1 && curr[lineLen - 2] == '\\' && curr[lineLen - 1] == 'r') {
lineLen -= 2;
}
MEMCPY((pSessionDescriptionInit->sdp) + j, curr, lineLen * SIZEOF(CHAR));
// Since we're adding 2 characters to the line, \r and \n (SDP record is separated by crlf),
// we need to add 2 to the serialized line so that the next iteration will not overwrite
// these 2 characters.
j += (lineLen + 2);
pSessionDescriptionInit->sdp[j - 2] = '\r';
pSessionDescriptionInit->sdp[j - 1] = '\n';
curr = next + 2;
}
}
}
CHK(pSessionDescriptionInit->sdp[0] != '\0', STATUS_SESSION_DESCRIPTION_INIT_MISSING_SDP);
CHK(pSessionDescriptionInit->type != 0, STATUS_SESSION_DESCRIPTION_INIT_MISSING_TYPE);
CleanUp:
LEAVES();
return retStatus;
}
/*
* Populate map with PayloadTypes if we are offering
*/
STATUS setPayloadTypesForOffer(PHashTable codecTable)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_MULAW, DEFAULT_PAYLOAD_MULAW));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_ALAW, DEFAULT_PAYLOAD_ALAW));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_VP8, DEFAULT_PAYLOAD_VP8));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_OPUS, DEFAULT_PAYLOAD_OPUS));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE, DEFAULT_PAYLOAD_H264));
CleanUp:
return retStatus;
}
/*
* Populate map with PayloadTypes for codecs a KvsPeerConnection has enabled.
*/
STATUS setPayloadTypesFromOffer(PHashTable codecTable, PHashTable rtxTable, PSessionDescription pSessionDescription)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PSdpMediaDescription pMediaDescription = NULL;
UINT8 currentAttribute;
UINT16 currentMedia;
PCHAR attributeValue, end;
UINT64 parsedPayloadType, hashmapPayloadType, fmtpVal, aptVal;
UINT16 aptFmtpVals[MAX_SDP_FMTP_VALUES];
UINT16 aptFmtVal;
BOOL supportCodec;
UINT32 tokenLen, i, aptFmtpValCount;
PCHAR fmtp;
UINT64 fmtpScore, bestFmtpScore;
for (currentMedia = 0; currentMedia < pSessionDescription->mediaCount; currentMedia++) {
pMediaDescription = &(pSessionDescription->mediaDescriptions[currentMedia]);
aptFmtpValCount = 0;
bestFmtpScore = 0;
attributeValue = pMediaDescription->mediaName;
do {
if ((end = STRCHR(attributeValue, ' ')) != NULL) {
tokenLen = (end - attributeValue);
} else {
tokenLen = STRLEN(attributeValue);
}
if (STRNCMP(DEFAULT_PAYLOAD_MULAW_STR, attributeValue, tokenLen) == 0) {
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_MULAW, DEFAULT_PAYLOAD_MULAW));
} else if (STRNCMP(DEFAULT_PAYLOAD_ALAW_STR, attributeValue, tokenLen) == 0) {
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_ALAW, DEFAULT_PAYLOAD_ALAW));
}
if (end != NULL) {
attributeValue = end + 1;
}
} while (end != NULL);
for (currentAttribute = 0; currentAttribute < pMediaDescription->mediaAttributesCount; currentAttribute++) {
attributeValue = pMediaDescription->sdpAttributes[currentAttribute].attributeValue;
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE, &supportCodec));
if (supportCodec && (end = STRSTR(attributeValue, H264_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(attributeValue, end - 1, 10, &parsedPayloadType));
fmtp = fmtpForPayloadType(parsedPayloadType, pSessionDescription);
fmtpScore = getH264FmtpScore(fmtp);
// When there's no match, the last fmtp will be chosen. This will allow us to not break existing customers who might be using
// flexible decoders which can infer the video profile from the SPS header.
if (fmtpScore >= bestFmtpScore) {
DLOGV("Found H264 payload type %" PRId64 " with score %lu: %s", parsedPayloadType, fmtpScore, fmtp);
CHK_STATUS(
hashTableUpsert(codecTable, RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE, parsedPayloadType));
bestFmtpScore = fmtpScore;
}
}
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_OPUS, &supportCodec));
if (supportCodec && (end = STRSTR(attributeValue, OPUS_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(attributeValue, end - 1, 10, &parsedPayloadType));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_OPUS, parsedPayloadType));
}
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_VP8, &supportCodec));
if (supportCodec && (end = STRSTR(attributeValue, VP8_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(attributeValue, end - 1, 10, &parsedPayloadType));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_VP8, parsedPayloadType));
}
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_MULAW, &supportCodec));
if (supportCodec && (end = STRSTR(attributeValue, MULAW_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(attributeValue, end - 1, 10, &parsedPayloadType));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_MULAW, parsedPayloadType));
}
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_ALAW, &supportCodec));
if (supportCodec && (end = STRSTR(attributeValue, ALAW_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(attributeValue, end - 1, 10, &parsedPayloadType));
CHK_STATUS(hashTableUpsert(codecTable, RTC_CODEC_ALAW, parsedPayloadType));
}
if ((end = STRSTR(attributeValue, RTX_CODEC_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(end + STRLEN(RTX_CODEC_VALUE), NULL, 10, &parsedPayloadType));
if ((end = STRSTR(attributeValue, FMTP_VALUE)) != NULL) {
CHK_STATUS(STRTOUI64(end + STRLEN(FMTP_VALUE), NULL, 10, &fmtpVal));
aptFmtpVals[aptFmtpValCount++] = (UINT32) ((fmtpVal << 8u) & parsedPayloadType);
}
}
}
for (i = 0; i < aptFmtpValCount; i++) {
aptFmtVal = aptFmtpVals[i];
fmtpVal = aptFmtVal >> 8u;
aptVal = aptFmtVal & 0xFFu;
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE, &supportCodec));
if (supportCodec) {
CHK_STATUS(hashTableGet(codecTable, RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE, &hashmapPayloadType));
if (aptVal == hashmapPayloadType) {
CHK_STATUS(hashTableUpsert(rtxTable, RTC_RTX_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE, fmtpVal));
DLOGV("found apt type %" PRId64 " for fmtp %" PRId64, aptVal, fmtpVal);
}
}
CHK_STATUS(hashTableContains(codecTable, RTC_CODEC_VP8, &supportCodec));
if (supportCodec) {
CHK_STATUS(hashTableGet(codecTable, RTC_CODEC_VP8, &hashmapPayloadType));
if (aptVal == hashmapPayloadType) {
CHK_STATUS(hashTableUpsert(rtxTable, RTC_RTX_CODEC_VP8, fmtpVal));
}
}
}
}
CleanUp:
LEAVES();
return retStatus;
}
STATUS setTransceiverPayloadTypes(PHashTable codecTable, PHashTable rtxTable, PDoubleList pTransceivers)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PDoubleListNode pCurNode = NULL;
PKvsRtpTransceiver pKvsRtpTransceiver;
UINT64 data;
// Loop over Transceivers and set the payloadType (which what we got from the other side)
// If a codec we want to send wasn't supported by the other return an error
CHK_STATUS(doubleListGetHeadNode(pTransceivers, &pCurNode));
while (pCurNode != NULL) {
CHK_STATUS(doubleListGetNodeData(pCurNode, &data));
pCurNode = pCurNode->pNext;
pKvsRtpTransceiver = (PKvsRtpTransceiver) data;
if (pKvsRtpTransceiver != NULL &&
(pKvsRtpTransceiver->transceiver.direction == RTC_RTP_TRANSCEIVER_DIRECTION_SENDRECV ||
pKvsRtpTransceiver->transceiver.direction == RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY)) {
CHK_STATUS(hashTableGet(codecTable, pKvsRtpTransceiver->sender.track.codec, &data));
pKvsRtpTransceiver->sender.payloadType = (UINT8) data;
pKvsRtpTransceiver->sender.rtxPayloadType = (UINT8) data;
// NACKs may have distinct PayloadTypes, look in the rtxTable and check. Otherwise NACKs will just be re-sending the same seqnum
if (hashTableGet(rtxTable, pKvsRtpTransceiver->sender.track.codec, &data) == STATUS_SUCCESS) {
pKvsRtpTransceiver->sender.rtxPayloadType = (UINT8) data;
}
}
UINT64 rollingBufferCapacity =
(UINT64) (pKvsRtpTransceiver->rollingBufferDurationSec * pKvsRtpTransceiver->rollingBufferBitratebps / 8 / DEFAULT_MTU_SIZE_BYTES);
DLOGI("The rolling buffer is configured to store %d RTP packets", rollingBufferCapacity);
CHK_STATUS(createRtpRollingBuffer(rollingBufferCapacity, &pKvsRtpTransceiver->sender.packetBuffer));
CHK_STATUS(createRetransmitter(DEFAULT_SEQ_NUM_BUFFER_SIZE, DEFAULT_VALID_INDEX_BUFFER_SIZE, &pKvsRtpTransceiver->sender.retransmitter));
}
CleanUp:
LEAVES();
return retStatus;
}
PCHAR fmtpForPayloadType(UINT64 payloadType, PSessionDescription pSessionDescription)
{
UINT32 currentMedia, currentAttribute;
PSdpMediaDescription pMediaDescription = NULL;
CHAR payloadStr[MAX_SDP_ATTRIBUTE_VALUE_LENGTH];
INT32 amountWritten = 0;
MEMSET(payloadStr, 0x00, MAX_SDP_ATTRIBUTE_VALUE_LENGTH);
amountWritten = SNPRINTF(payloadStr, SIZEOF(payloadStr), "%" PRId64, payloadType);
if (amountWritten < 0) {
DLOGE("Internal error: Full payload type for fmtp could not be written");
} else {
for (currentMedia = 0; currentMedia < pSessionDescription->mediaCount; currentMedia++) {
pMediaDescription = &(pSessionDescription->mediaDescriptions[currentMedia]);
for (currentAttribute = 0; currentAttribute < pMediaDescription->mediaAttributesCount; currentAttribute++) {
if (STRCMP(pMediaDescription->sdpAttributes[currentAttribute].attributeName, "fmtp") == 0 &&
STRNCMP(pMediaDescription->sdpAttributes[currentAttribute].attributeValue, payloadStr, STRLEN(payloadStr)) == 0) {
return pMediaDescription->sdpAttributes[currentAttribute].attributeValue + STRLEN(payloadStr) + 1;
}
}
}
}
return NULL;
}
/*
* Extracts a (hex) value after the provided prefix string. Returns true if
* successful.
*/
BOOL readHexValue(PCHAR input, PCHAR prefix, PUINT32 value)
{
PCHAR substr = STRSTR(input, prefix);
if (substr != NULL && SSCANF(substr + STRLEN(prefix), "%x", value) == 1) {
return TRUE;
}
return FALSE;
}
/*
* Scores the provided fmtp string based on this library's ability to
* process various types of H264 streams. A score of 0 indicates an
* incompatible fmtp line. Beyond this, a higher score indicates more
* compatibility with the desired characteristics, packetization-mode=1,
* level-asymmetry-allowed=1, and inbound match with our preferred
* profile-level-id.
*
* At some future time, it may be worth expressing this as a true distance
* function as defined here, although dealing with infinite floating point
* values can get tricky:
* https://www.w3.org/TR/mediacapture-streams/#dfn-fitness-distance
*/
UINT64 getH264FmtpScore(PCHAR fmtp)
{
UINT32 profileId = 0, packetizationMode = 0, levelAsymmetry = 0;
UINT64 score = 0;
// No ftmp match found.
if (fmtp == NULL) {
return 0;
}
// Currently, the packetization mode must be 1, as the packetization logic
// is currently not configurable, and sends both NALU and FU-A packets.
// https://tools.ietf.org/html/rfc7742#section-6.2
if (readHexValue(fmtp, "packetization-mode=", &packetizationMode) && packetizationMode == 1) {
score++;
}
if (readHexValue(fmtp, "profile-level-id=", &profileId) &&
(profileId & H264_FMTP_SUBPROFILE_MASK) == (H264_PROFILE_42E01F & H264_FMTP_SUBPROFILE_MASK) &&
(profileId & H264_FMTP_PROFILE_LEVEL_MASK) <= (H264_PROFILE_42E01F & H264_FMTP_PROFILE_LEVEL_MASK)) {
score++;
}
if (readHexValue(fmtp, "level-asymmetry-allowed=", &levelAsymmetry) && levelAsymmetry == 1) {
score++;
}
return score;
}
// Populate a single media section from a PKvsRtpTransceiver
STATUS populateSingleMediaSection(PKvsPeerConnection pKvsPeerConnection, PKvsRtpTransceiver pKvsRtpTransceiver,
PSdpMediaDescription pSdpMediaDescription, PSessionDescription pRemoteSessionDescription,
PCHAR pCertificateFingerprint, UINT32 mediaSectionId, PCHAR pDtlsRole, PHashTable pUnknownCodecPayloadTypesTable,
PHashTable pUnknownCodecRtpmapTable, UINT32 unknownCodecHashTableKey)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
UINT64 payloadType, rtxPayloadType;
BOOL containRtx = FALSE;
BOOL directionFound = FALSE;
UINT32 i, remoteAttributeCount, attributeCount, attributeNameBuffLen, amountWritten = 0;
PRtcMediaStreamTrack pRtcMediaStreamTrack = &(pKvsRtpTransceiver->sender.track);
PSdpMediaDescription pSdpMediaDescriptionRemote;
PCHAR currentFmtp = NULL, rtpMapValue = NULL;
CHAR remoteSdpAttributeValue[MAX_SDP_ATTRIBUTE_VALUE_LENGTH];
INT32 amountWritten = 0;
MEMSET(remoteSdpAttributeValue, 0, MAX_SDP_ATTRIBUTE_VALUE_LENGTH);
if (pRtcMediaStreamTrack->codec == RTC_CODEC_UNKNOWN && pUnknownCodecPayloadTypesTable != NULL) {
CHK_STATUS(hashTableGet(pUnknownCodecPayloadTypesTable, unknownCodecHashTableKey, &payloadType));
} else {
CHK_STATUS(hashTableGet(pKvsPeerConnection->pCodecTable, pRtcMediaStreamTrack->codec, &payloadType));
if (!pKvsPeerConnection->isOffer) {
currentFmtp = fmtpForPayloadType(payloadType, pRemoteSessionDescription);
}
}
if (pRtcMediaStreamTrack->kind == MEDIA_STREAM_TRACK_KIND_VIDEO) {
if (pRtcMediaStreamTrack->codec == RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE) {
retStatus = hashTableGet(pKvsPeerConnection->pRtxTable, RTC_RTX_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE,
&rtxPayloadType);
} else if (pRtcMediaStreamTrack->codec == RTC_CODEC_VP8) {
retStatus = hashTableGet(pKvsPeerConnection->pRtxTable, RTC_RTX_CODEC_VP8, &rtxPayloadType);
} else {
retStatus = STATUS_HASH_KEY_NOT_PRESENT;
}
CHK(retStatus == STATUS_SUCCESS || retStatus == STATUS_HASH_KEY_NOT_PRESENT, retStatus);
containRtx = (retStatus == STATUS_SUCCESS);
retStatus = STATUS_SUCCESS;
if (containRtx) {
amountWritten = SNPRINTF(pSdpMediaDescription->mediaName, SIZEOF(pSdpMediaDescription->mediaName),
"video 9 UDP/TLS/RTP/SAVPF %" PRId64 " %" PRId64, payloadType, rtxPayloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full video (with rtx) media name attribute could not be written");
} else {
amountWritten =
SNPRINTF(pSdpMediaDescription->mediaName, SIZEOF(pSdpMediaDescription->mediaName), "video 9 UDP/TLS/RTP/SAVPF %" PRId64, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full video media name attribute could not be written");
}
} else if (pRtcMediaStreamTrack->kind == MEDIA_STREAM_TRACK_KIND_AUDIO) {
amountWritten =
SNPRINTF(pSdpMediaDescription->mediaName, SIZEOF(pSdpMediaDescription->mediaName), "audio 9 UDP/TLS/RTP/SAVPF %" PRId64, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full audio media name attribute could not be written");
}
CHK_STATUS(iceAgentPopulateSdpMediaDescriptionCandidates(pKvsPeerConnection->pIceAgent, pSdpMediaDescription, MAX_SDP_ATTRIBUTE_VALUE_LENGTH,
&attributeCount));
attributeNameBuffLen = SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName);
if (containRtx) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "msid");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%s %sRTX",
pRtcMediaStreamTrack->streamId, pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full msid value (with rtx) could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc-group");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "FID %u %u",
pKvsRtpTransceiver->sender.ssrc, pKvsRtpTransceiver->sender.rtxSsrc);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc-grp value (with rtx) could not be written");
attributeCount++;
} else {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "msid");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%s %s", pRtcMediaStreamTrack->streamId,
pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full msid value could not be written");
attributeCount++;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u cname:%s",
pKvsRtpTransceiver->sender.ssrc, pKvsPeerConnection->localCNAME);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc cname could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u msid:%s %s",
pKvsRtpTransceiver->sender.ssrc, pRtcMediaStreamTrack->streamId, pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc msid could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u mslabel:%s",
pKvsRtpTransceiver->sender.ssrc, pRtcMediaStreamTrack->streamId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc mslabel could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u label:%s",
pKvsRtpTransceiver->sender.ssrc, pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc label could not be written");
attributeCount++;
if (containRtx) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u cname:%s",
pKvsRtpTransceiver->sender.rtxSsrc, pKvsPeerConnection->localCNAME);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc cname (with rtx) could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u msid:%s %sRTX",
pKvsRtpTransceiver->sender.rtxSsrc, pRtcMediaStreamTrack->streamId, pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc msid (with rtx) could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u mslabel:%sRTX",
pKvsRtpTransceiver->sender.rtxSsrc, pRtcMediaStreamTrack->streamId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc mslabel (with rtx) could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u label:%sRTX",
pKvsRtpTransceiver->sender.rtxSsrc, pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ssrc label (with rtx) could not be written");
attributeCount++;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, "9 IN IP4 0.0.0.0");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ice-ufrag");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, pKvsPeerConnection->localIceUfrag);
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ice-pwd");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, pKvsPeerConnection->localIcePwd);
attributeCount++;
if (pKvsPeerConnection->canTrickleIce.value) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ice-options");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, "trickle");
attributeCount++;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "fingerprint");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, "sha-256 ");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue + 8, pCertificateFingerprint);
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "setup");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, pDtlsRole);
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "mid");
if (!pKvsPeerConnection->isOffer) {
// check all session attribute lines to see if a line with mid is present. If it is present, copy its content and break
for (i = 0; i < pRemoteSessionDescription->mediaDescriptions[mediaSectionId].mediaAttributesCount; i++) {
if (STRCMP(pRemoteSessionDescription->mediaDescriptions[mediaSectionId].sdpAttributes[i].attributeName, MID_KEY) == 0) {
STRCPY(remoteSdpAttributeValue, pRemoteSessionDescription->mediaDescriptions[mediaSectionId].sdpAttributes[i].attributeValue);
break;
}
}
}
// check if we already have a value for the "mid" session attribute from remote description. If we have it, we use it.
// If we don't have it, we loop over, create and add them
if (STRLEN(remoteSdpAttributeValue) > 0) {
CHK(STRLEN(remoteSdpAttributeValue) < MAX_SDP_ATTRIBUTE_VALUE_LENGTH, STATUS_BUFFER_TOO_SMALL);
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%s", remoteSdpAttributeValue);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Mid exists, but remote SDP value could not be written");
} else {
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%d", mediaSectionId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full media section Id could not be written");
}
attributeCount++;
if (pKvsPeerConnection->isOffer) {
switch (pKvsRtpTransceiver->transceiver.direction) {
case RTC_RTP_TRANSCEIVER_DIRECTION_SENDRECV:
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "sendrecv");
break;
case RTC_RTP_TRANSCEIVER_DIRECTION_SENDONLY:
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "sendonly");
break;
case RTC_RTP_TRANSCEIVER_DIRECTION_RECVONLY:
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "recvonly");
break;
default:
// https://www.w3.org/TR/webrtc/#dom-rtcrtptransceiverdirection
DLOGW("Incorrect/no transceiver direction set...this attribute will be set to inactive");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "inactive");
}
} else {
pSdpMediaDescriptionRemote = &pRemoteSessionDescription->mediaDescriptions[mediaSectionId];
remoteAttributeCount = pSdpMediaDescriptionRemote->mediaAttributesCount;
// in case of a missing m-line, we respond with the same m-line but direction set to inactive
if (pKvsRtpTransceiver->transceiver.direction == RTC_RTP_TRANSCEIVER_DIRECTION_INACTIVE) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "inactive");
directionFound = TRUE;
}
for (i = 0; i < remoteAttributeCount && directionFound == FALSE; i++) {
if (STRCMP(pSdpMediaDescriptionRemote->sdpAttributes[i].attributeName, "sendrecv") == 0) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "sendrecv");
directionFound = TRUE;
} else if (STRCMP(pSdpMediaDescriptionRemote->sdpAttributes[i].attributeName, "recvonly") == 0) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "sendonly");
directionFound = TRUE;
} else if (STRCMP(pSdpMediaDescriptionRemote->sdpAttributes[i].attributeName, "sendonly") == 0) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "recvonly");
directionFound = TRUE;
}
}
}
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp-mux");
attributeCount++;
if (mediaSectionId != 0) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp-rsize");
attributeCount++;
}
if (pRtcMediaStreamTrack->codec == RTC_CODEC_H264_PROFILE_42E01F_LEVEL_ASYMMETRY_ALLOWED_PACKETIZATION_MODE) {
// TODO: Need additional condition for a signaling channel with an ENABLED media storage configuration
if (pKvsPeerConnection->isOffer) {
currentFmtp = DEFAULT_H264_FMTP;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " H264/90000", payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full H264 payload type could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp-fb");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " nack", payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full H264 rtcp-fb nack value could not be written");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " nack pli", payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full H264 rtcp-fb nack-pli value could not be written");
attributeCount++;
// TODO: If level asymmetry is allowed, consider sending back DEFAULT_H264_FMTP instead of the received fmtp value.
if (currentFmtp != NULL) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "fmtp");
amountWritten =
SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " %s", payloadType, currentFmtp);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full H264 fmtp value could not be written");
attributeCount++;
}
if (containRtx) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten =
SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " " RTX_VALUE, rtxPayloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full H264 rtpmap (with rtx) could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "fmtp");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " apt=%" PRId64 "",
rtxPayloadType, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full H264 fmtp apt value (with rtx) could not be written");
attributeCount++;
}
} else if (pRtcMediaStreamTrack->codec == RTC_CODEC_OPUS) {
if (pKvsPeerConnection->isOffer) {
currentFmtp = DEFAULT_OPUS_FMTP;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " opus/48000/2", payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full Opus rtpmap could not be written");
attributeCount++;
if (currentFmtp != NULL) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "fmtp");
amountWritten =
SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " %s", payloadType, currentFmtp);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full Opus fmtp could not be written");
attributeCount++;
}
} else if (pRtcMediaStreamTrack->codec == RTC_CODEC_VP8) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " " VP8_VALUE, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full VP8 rtpmap could not be written");
attributeCount++;
if (containRtx) {
CHK_STATUS(hashTableGet(pKvsPeerConnection->pRtxTable, RTC_RTX_CODEC_VP8, &rtxPayloadType));
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten =
SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " " RTX_VALUE, rtxPayloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full VP8 rtpmap payload type (with rtx) could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "fmtp");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " apt=%" PRId64 "",
rtxPayloadType, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full VP8 rtpmap fmtp apt value (with rtx) could not be written");
attributeCount++;
}
} else if (pRtcMediaStreamTrack->codec == RTC_CODEC_MULAW) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " " MULAW_VALUE, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full MULAW rtpmap could not be written");
attributeCount++;
} else if (pRtcMediaStreamTrack->codec == RTC_CODEC_ALAW) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " " ALAW_VALUE, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full ALAW rtpmap could not be written");
attributeCount++;
} else if (pRtcMediaStreamTrack->codec == RTC_CODEC_UNKNOWN) {
CHK_STATUS(hashTableGet(pUnknownCodecRtpmapTable, unknownCodecHashTableKey, (PUINT64) &rtpMapValue));
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtpmap");
amountWritten =
SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " %s", payloadType, rtpMapValue);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full Unknown rtpmap could not be written");
attributeCount++;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u cname:%s",
pKvsRtpTransceiver->sender.ssrc, pKvsPeerConnection->localCNAME);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full transceiver ssrc cname could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ssrc");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%u msid:%s %s",
pKvsRtpTransceiver->sender.ssrc, pRtcMediaStreamTrack->streamId, pRtcMediaStreamTrack->trackId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full transceiver ssrc msid could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp-fb");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " goog-remb", payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full rtcp-fb goog-remb could not be written");
attributeCount++;
if (pKvsPeerConnection->twccExtId != 0) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp-fb");
amountWritten =
SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%" PRId64 " " TWCC_SDP_ATTR, payloadType);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full rtcp-fb twcc could not be written");
attributeCount++;
}
pSdpMediaDescription->mediaAttributesCount = attributeCount;
CleanUp:
LEAVES();
return retStatus;
}
STATUS populateSessionDescriptionDataChannel(PKvsPeerConnection pKvsPeerConnection, PSdpMediaDescription pSdpMediaDescription,
PCHAR pCertificateFingerprint, UINT32 mediaSectionId, PCHAR pDtlsRole)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
UINT32 attributeCount = 0;
INT32 amountWritten = 0;
amountWritten =
SNPRINTF(pSdpMediaDescription->mediaName, SIZEOF(pSdpMediaDescription->mediaName), "application 9 UDP/DTLS/SCTP webrtc-datachannel");
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full data channel media name could not be written");
CHK_STATUS(iceAgentPopulateSdpMediaDescriptionCandidates(pKvsPeerConnection->pIceAgent, pSdpMediaDescription, MAX_SDP_ATTRIBUTE_VALUE_LENGTH,
&attributeCount));
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "rtcp");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, "9 IN IP4 0.0.0.0");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ice-ufrag");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, pKvsPeerConnection->localIceUfrag);
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ice-pwd");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, pKvsPeerConnection->localIcePwd);
attributeCount++;
if (pKvsPeerConnection->canTrickleIce.value) {
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "ice-options");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, "trickle");
attributeCount++;
}
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "fingerprint");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, "sha-256 ");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue + 8, pCertificateFingerprint);
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "setup");
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue, pDtlsRole);
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "mid");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "%d", mediaSectionId);
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full data channel mid media section could not be written");
attributeCount++;
STRCPY(pSdpMediaDescription->sdpAttributes[attributeCount].attributeName, "sctp-port");
amountWritten = SNPRINTF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue,
SIZEOF(pSdpMediaDescription->sdpAttributes[attributeCount].attributeValue), "5000");
CHK_ERR(amountWritten > 0, STATUS_INTERNAL_ERROR, "Full data channel sctp-port could not be written");
attributeCount++;
pSdpMediaDescription->mediaAttributesCount = attributeCount;
CleanUp:
LEAVES();
return retStatus;
}
BOOL isPresentInRemote(PKvsRtpTransceiver pKvsRtpTransceiver, PSessionDescription pRemoteSessionDescription)
{
PCHAR remoteAttributeValue, end;
UINT32 remoteTokenLen, i;
PSdpMediaDescription pRemoteMediaDescription;
MEDIA_STREAM_TRACK_KIND localTrackKind = pKvsRtpTransceiver->sender.track.kind;
BOOL wasFound = FALSE;
for (i = 0; i < pRemoteSessionDescription->mediaCount && wasFound == FALSE; i++) {
pRemoteMediaDescription = &pRemoteSessionDescription->mediaDescriptions[i];
remoteAttributeValue = pRemoteMediaDescription->mediaName;
if ((end = STRCHR(remoteAttributeValue, ' ')) != NULL) {
remoteTokenLen = (end - remoteAttributeValue);
} else {
remoteTokenLen = STRLEN(remoteAttributeValue);
}
switch (localTrackKind) {
case MEDIA_STREAM_TRACK_KIND_AUDIO:
if (remoteTokenLen == (ARRAY_SIZE(MEDIA_SECTION_AUDIO_VALUE) - 1) &&
STRNCMP(MEDIA_SECTION_AUDIO_VALUE, remoteAttributeValue, remoteTokenLen) == 0) {
wasFound = TRUE;
}
break;
case MEDIA_STREAM_TRACK_KIND_VIDEO:
if (remoteTokenLen == (ARRAY_SIZE(MEDIA_SECTION_VIDEO_VALUE) - 1) &&
STRNCMP(MEDIA_SECTION_VIDEO_VALUE, remoteAttributeValue, remoteTokenLen) == 0) {
wasFound = TRUE;
}
break;
default:
DLOGW("Unknown track kind: %d", localTrackKind);
}
}
return wasFound;
}
// Populate the media sections of a SessionDescription with the current state of the KvsPeerConnection
STATUS populateSessionDescriptionMedia(PKvsPeerConnection pKvsPeerConnection, PSessionDescription pRemoteSessionDescription,
PSessionDescription pLocalSessionDescription)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
PDoubleListNode pCurNode = NULL;
CHAR certificateFingerprint[CERTIFICATE_FINGERPRINT_LENGTH];
UINT64 data;
PKvsRtpTransceiver pKvsRtpTransceiver;
PCHAR pDtlsRole = NULL;
PHashTable pUnknownCodecPayloadTypesTable = NULL, pUnknownCodecRtpmapTable = NULL;
UINT32 unknownCodecHashTableKey = 0;
UINT32 unknownHashTableBucketCount = 0;
CHK_STATUS(dtlsSessionGetLocalCertificateFingerprint(pKvsPeerConnection->pDtlsSession, certificateFingerprint, CERTIFICATE_FINGERPRINT_LENGTH));
if (pKvsPeerConnection->isOffer) {
pDtlsRole = DTLS_ROLE_ACTPASS;
CHK_STATUS(doubleListGetHeadNode(pKvsPeerConnection->pTransceivers, &pCurNode));
while (pCurNode != NULL) {
CHK_STATUS(doubleListGetNodeData(pCurNode, &data));
pCurNode = pCurNode->pNext;
pKvsRtpTransceiver = (PKvsRtpTransceiver) data;
if (pKvsRtpTransceiver != NULL) {
CHK(pLocalSessionDescription->mediaCount < MAX_SDP_SESSION_MEDIA_COUNT, STATUS_SESSION_DESCRIPTION_MAX_MEDIA_COUNT);
// If generating answer, need to check if Local Description is present in remote -- if not, we don't need to create a local
// description for it or else our Answer will have an extra m-line, for offer the local is the offer itself, don't care about remote
CHK_STATUS(populateSingleMediaSection(
pKvsPeerConnection, pKvsRtpTransceiver, &(pLocalSessionDescription->mediaDescriptions[pLocalSessionDescription->mediaCount]),
pRemoteSessionDescription, certificateFingerprint, pLocalSessionDescription->mediaCount, pDtlsRole, NULL, NULL, 0));
pLocalSessionDescription->mediaCount++;
}
}
} else {
pDtlsRole = DTLS_ROLE_ACTIVE;
unknownHashTableBucketCount =
pRemoteSessionDescription->mediaCount < MIN_HASH_BUCKET_COUNT ? MIN_HASH_BUCKET_COUNT : pRemoteSessionDescription->mediaCount;
CHK_STATUS(hashTableCreateWithParams(unknownHashTableBucketCount, CODEC_RTPMAP_PAYLOAD_TYPES_HASH_TABLE_BUCKET_LENGTH,
&pUnknownCodecPayloadTypesTable));
CHK_STATUS(
hashTableCreateWithParams(unknownHashTableBucketCount, CODEC_RTPMAP_PAYLOAD_TYPES_HASH_TABLE_BUCKET_LENGTH, &pUnknownCodecRtpmapTable));
// this function creates a list of transceivers corresponding to each m-line and adds it answerTransceivers
// if an m-line does not have a corresponding transceiver created by the user, we create a fake transceiver
CHK_STATUS(findTransceiversByRemoteDescription(pKvsPeerConnection, pRemoteSessionDescription, pUnknownCodecPayloadTypesTable,
pUnknownCodecRtpmapTable));
// pAnswerTransceivers contains transceivers created by the user as well as fake transceivers
CHK_STATUS(doubleListGetHeadNode(pKvsPeerConnection->pAnswerTransceivers, &pCurNode));
while (pCurNode != NULL) {
CHK_STATUS(doubleListGetNodeData(pCurNode, &data));
pCurNode = pCurNode->pNext;
pKvsRtpTransceiver = (PKvsRtpTransceiver) data;
if (pKvsRtpTransceiver != NULL) {
CHK(pLocalSessionDescription->mediaCount < MAX_SDP_SESSION_MEDIA_COUNT, STATUS_SESSION_DESCRIPTION_MAX_MEDIA_COUNT);
if (isPresentInRemote(pKvsRtpTransceiver, pRemoteSessionDescription)) {
if (pKvsRtpTransceiver->sender.track.codec == RTC_CODEC_UNKNOWN) {
CHK_STATUS(populateSingleMediaSection(pKvsPeerConnection, pKvsRtpTransceiver,
&(pLocalSessionDescription->mediaDescriptions[pLocalSessionDescription->mediaCount]),
pRemoteSessionDescription, certificateFingerprint, pLocalSessionDescription->mediaCount,
pDtlsRole, pUnknownCodecPayloadTypesTable, pUnknownCodecRtpmapTable,
unknownCodecHashTableKey));
unknownCodecHashTableKey++;
// unknownCodecHashTableKey is the key for pUnknownCodecRtpmapTable and pUnknownCodecPayloadTypesTable
// a value for the same key in both hashtables corresponds to rtpmap and payloadtype for the same m-line / unknown codec
} else {
// in case of a user-added transceiver, the pUnknownCodecPayloadTypesTable, pUnknownCodecRtpmapTable are not populated by
// the function findTransceiversByRemoteDescription and are NULL
CHK_STATUS(populateSingleMediaSection(pKvsPeerConnection, pKvsRtpTransceiver,
&(pLocalSessionDescription->mediaDescriptions[pLocalSessionDescription->mediaCount]),
pRemoteSessionDescription, certificateFingerprint, pLocalSessionDescription->mediaCount,
pDtlsRole, NULL, NULL, 0));
}
pLocalSessionDescription->mediaCount++;
}
}
}
}
if (ATOMIC_LOAD_BOOL(&pKvsPeerConnection->sctpIsEnabled)) {
CHK(pLocalSessionDescription->mediaCount < MAX_SDP_SESSION_MEDIA_COUNT, STATUS_SESSION_DESCRIPTION_MAX_MEDIA_COUNT);
CHK_STATUS(populateSessionDescriptionDataChannel(pKvsPeerConnection,
&(pLocalSessionDescription->mediaDescriptions[pLocalSessionDescription->mediaCount]),
certificateFingerprint, pLocalSessionDescription->mediaCount, pDtlsRole));
pLocalSessionDescription->mediaCount++;
}
CleanUp:
if (pUnknownCodecPayloadTypesTable != NULL) {
CHK_STATUS(hashTableFree(pUnknownCodecPayloadTypesTable));
}
if (pUnknownCodecRtpmapTable != NULL) {
CHK_STATUS(hashTableFree(pUnknownCodecRtpmapTable));
}
LEAVES();
return retStatus;
}
// Populate a SessionDescription with the current state of the KvsPeerConnection
STATUS populateSessionDescription(PKvsPeerConnection pKvsPeerConnection, PSessionDescription pRemoteSessionDescription,
PSessionDescription pLocalSessionDescription)
{
ENTERS();
STATUS retStatus = STATUS_SUCCESS;
CHAR bundleValue[MAX_SDP_ATTRIBUTE_VALUE_LENGTH], wmsValue[MAX_SDP_ATTRIBUTE_VALUE_LENGTH],
remoteSdpAttributeValue[MAX_SDP_ATTRIBUTE_VALUE_LENGTH];
PCHAR curr = NULL;
UINT32 i, sizeRemaining;
INT32 charsCopied;
CHK(pKvsPeerConnection != NULL && pLocalSessionDescription != NULL, STATUS_NULL_ARG);
if (!pKvsPeerConnection->isOffer) {
CHK(pRemoteSessionDescription != NULL, STATUS_NULL_ARG);
}
CHK_STATUS(populateSessionDescriptionMedia(pKvsPeerConnection, pRemoteSessionDescription, pLocalSessionDescription));
MEMSET(bundleValue, 0, MAX_SDP_ATTRIBUTE_VALUE_LENGTH);