forked from jiyeyuran/mediasoup-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathortc.go
948 lines (778 loc) · 24.8 KB
/
ortc.go
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
package mediasoup
import (
"bytes"
"errors"
"fmt"
"reflect"
"strings"
"github.com/jiyeyuran/mediasoup-go/h264"
)
var DYNAMIC_PAYLOAD_TYPES = [...]byte{
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, 96, 97, 98, 99, 77,
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 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,
}
type matchOptions struct {
strict bool
modify bool
}
type RtpMapping struct {
Codecs []RtpMappingCodec `json:"codecs,omitempty"`
Encodings []RtpMappingEncoding `json:"encodings,omitempty"`
}
type RtpMappingCodec struct {
PayloadType byte `json:"payloadType"`
MappedPayloadType byte `json:"mappedPayloadType"`
}
type RtpMappingEncoding struct {
Ssrc uint32 `json:"ssrc,omitempty"`
Rid string `json:"rid,omitempty"`
ScalabilityMode string `json:"scalabilityMode,omitempty"`
MappedSsrc uint32 `json:"mappedSsrc"`
}
/**
* Validates RtpCapabilities. It may modify given data by adding missing
* fields with default values.
*/
func validateRtpCapabilities(params *RtpCapabilities) (err error) {
for _, codec := range params.Codecs {
if err = validateRtpCodecCapability(codec); err != nil {
return
}
}
for _, ext := range params.HeaderExtensions {
if err = validateRtpHeaderExtension(ext); err != nil {
return
}
}
return
}
/**
* Validates RtpCodecCapability. It may modify given data by adding missing
* fields with default values.
*/
func validateRtpCodecCapability(code *RtpCodecCapability) (err error) {
mimeType := strings.ToLower(code.MimeType)
// mimeType is mandatory.
if !strings.HasPrefix(mimeType, "audio/") && !strings.HasPrefix(mimeType, "video/") {
return NewTypeError("invalid codec.mimeType")
}
code.Kind = MediaKind(strings.Split(mimeType, "/")[0])
// clockRate is mandatory.
if code.ClockRate == 0 {
return NewTypeError("missing codec.clockRate")
}
// channels is optional. If unset, set it to 1 (just if audio).
if code.Kind == MediaKind_Audio && code.Channels == 0 {
code.Channels = 1
}
for _, fb := range code.RtcpFeedback {
if err = validateRtcpFeedback(fb); err != nil {
return
}
}
return
}
/**
* Validates RtcpFeedback. It may modify given data by adding missing
* fields with default values.
*/
func validateRtcpFeedback(fb RtcpFeedback) error {
if len(fb.Type) == 0 {
return NewTypeError("missing fb.type")
}
return nil
}
/**
* Validates RtpHeaderExtension. It may modify given data by adding missing
* fields with default values.
*/
func validateRtpHeaderExtension(ext *RtpHeaderExtension) (err error) {
if len(ext.Kind) > 0 && ext.Kind != MediaKind_Audio && ext.Kind != MediaKind_Video {
return NewTypeError("invalid ext.kind")
}
// uri is mandatory.
if len(ext.Uri) == 0 {
return NewTypeError("missing ext.uri")
}
// preferredId is mandatory.
if ext.PreferredId == 0 {
return NewTypeError("missing ext.preferredId")
}
// direction is optional. If unset set it to sendrecv.
if len(ext.Direction) == 0 {
ext.Direction = Direction_Sendrecv
}
return
}
/**
* Validates RtpParameters. It may modify given data by adding missing
* fields with default values.
*/
func validateRtpParameters(params *RtpParameters) (err error) {
for _, codec := range params.Codecs {
if err = validateRtpCodecParameters(codec); err != nil {
return
}
}
for _, ext := range params.HeaderExtensions {
if err = validateRtpHeaderExtensionParameters(ext); err != nil {
return
}
}
return validateRtcpParameters(¶ms.Rtcp)
}
/**
* Validates RtpCodecParameters. It may modify given data by adding missing
* fields with default values.
*/
func validateRtpCodecParameters(code *RtpCodecParameters) (err error) {
mimeType := strings.ToLower(code.MimeType)
// mimeType is mandatory.
if !strings.HasPrefix(mimeType, "audio/") && !strings.HasPrefix(mimeType, "video/") {
return NewTypeError("invalid codec.mimeType")
}
// clockRate is mandatory.
if code.ClockRate == 0 {
return NewTypeError("missing codec.clockRate")
}
kind := MediaKind(strings.Split(mimeType, "/")[0])
// channels is optional. If unset, set it to 1 (just if audio).
if kind == MediaKind_Audio && code.Channels == 0 {
code.Channels = 1
}
for _, fb := range code.RtcpFeedback {
if err = validateRtcpFeedback(fb); err != nil {
return
}
}
return
}
/**
* Validates RtpHeaderExtension. It may modify given data by adding missing
* fields with default values.
*/
func validateRtpHeaderExtensionParameters(ext RtpHeaderExtensionParameters) (err error) {
// uri is mandatory.
if len(ext.Uri) == 0 {
return NewTypeError("missing ext.uri")
}
// preferredId is mandatory.
if ext.Id == 0 {
return NewTypeError("missing ext.id")
}
return
}
/**
* Validates RtcpParameters. It may modify given data by adding missing
* fields with default values.
*/
func validateRtcpParameters(rtcp *RtcpParameters) (err error) {
// reducedSize is optional. If unset set it to true.
if rtcp.ReducedSize == nil {
rtcp.ReducedSize = Bool(true)
}
return
}
/**
* Validates SctpCapabilities. It may modify given data by adding missing
* fields with default values.
*/
func validateSctpCapabilities(caps SctpCapabilities) (err error) {
// numStreams is mandatory.
if reflect.DeepEqual(caps.NumStreams, NumSctpStreams{}) {
return NewTypeError("missing caps.numStreams")
}
return validateNumSctpStreams(caps.NumStreams)
}
/**
* Validates NumSctpStreams. It may modify given data by adding missing
* fields with default values.
*/
func validateNumSctpStreams(numStreams NumSctpStreams) (err error) {
// OS is mandatory.
if numStreams.OS == 0 {
return NewTypeError("missing numStreams.OS")
}
// MIS is mandatory.
if numStreams.MIS == 0 {
return NewTypeError("missing numStreams.MIS")
}
return
}
/**
* Validates SctpParameters. It may modify given data by adding missing
* fields with default values.
* It throws if invalid.
*/
func validateSctpParameters(params SctpParameters) (err error) {
// port is mandatory.
if params.Port == 0 {
return NewTypeError("missing params.port")
}
// OS is mandatory.
if params.OS == 0 {
return NewTypeError("missing params.OS")
}
// MIS is mandatory.
if params.MIS == 0 {
return NewTypeError("missing params.MIS")
}
// maxMessageSize is mandatory.
if params.MaxMessageSize == 0 {
return NewTypeError("missing params.maxMessageSize")
}
return
}
/**
* Validates SctpStreamParameters. It may modify given data by adding missing
* fields with default values.
*/
func validateSctpStreamParameters(params *SctpStreamParameters) (err error) {
if params == nil {
return NewTypeError("params is nil")
}
orderedGiven := params.Ordered != nil
if params.Ordered == nil {
params.Ordered = Bool(true)
}
if params.MaxPacketLifeTime > 0 && params.MaxRetransmits > 0 {
return NewTypeError("cannot provide both maxPacketLifeTime and maxRetransmits")
}
if orderedGiven && *params.Ordered &&
(params.MaxPacketLifeTime > 0 || params.MaxRetransmits > 0) {
return NewTypeError("cannot be ordered with maxPacketLifeTime or maxRetransmits")
} else if !orderedGiven && (params.MaxPacketLifeTime > 0 || params.MaxRetransmits > 0) {
params.Ordered = Bool(false)
}
return
}
/**
* Generate RTP capabilities for the Router based on the given media codecs and
* mediasoup supported RTP capabilities.
*/
func generateRouterRtpCapabilities(mediaCodecs []*RtpCodecCapability) (caps RtpCapabilities, err error) {
if len(mediaCodecs) == 0 {
err = NewTypeError("mediaCodecs must be an Array")
return
}
clonedSupportedRtpCapabilities := GetSupportedRtpCapabilities()
supportedCodecs := clonedSupportedRtpCapabilities.Codecs
caps.HeaderExtensions = clonedSupportedRtpCapabilities.HeaderExtensions
dynamicPayloadTypes := make([]byte, len(DYNAMIC_PAYLOAD_TYPES))
copy(dynamicPayloadTypes, DYNAMIC_PAYLOAD_TYPES[:])
for _, mediaCodec := range mediaCodecs {
if err = validateRtpCodecCapability(mediaCodec); err != nil {
return
}
matchedSupportedCodec, matched := findMatchedCodec(mediaCodec, supportedCodecs, matchOptions{})
if !matched {
err = NewUnsupportedError(`media codec not supported [mimeType:%s]`, mediaCodec.MimeType)
return
}
codec := &RtpCodecCapability{}
if err = clone(matchedSupportedCodec, codec); err != nil {
return
}
if mediaCodec.PreferredPayloadType > 0 {
codec.PreferredPayloadType = mediaCodec.PreferredPayloadType
idx := bytes.IndexByte(dynamicPayloadTypes, codec.PreferredPayloadType)
if idx > -1 {
dynamicPayloadTypes = append(dynamicPayloadTypes[:idx], dynamicPayloadTypes[idx+1:]...)
}
} else if codec.PreferredPayloadType == 0 {
if len(dynamicPayloadTypes) == 0 {
err = errors.New("cannot allocate more dynamic codec payload types")
return
}
codec.PreferredPayloadType = dynamicPayloadTypes[0]
dynamicPayloadTypes = dynamicPayloadTypes[1:]
}
for _, capCodec := range caps.Codecs {
if capCodec.PreferredPayloadType == codec.PreferredPayloadType {
err = NewTypeError("duplicated codec.preferredPayloadType")
return
}
}
// Merge the media codec parameters.
override(&codec.Parameters, mediaCodec.Parameters)
// Append to the codec list.
caps.Codecs = append(caps.Codecs, codec)
// Add a RTX video codec if video.
if codec.Kind == MediaKind_Video {
if len(dynamicPayloadTypes) == 0 {
err = errors.New("cannot allocate more dynamic codec payload types")
return
}
pt := dynamicPayloadTypes[0]
dynamicPayloadTypes = dynamicPayloadTypes[1:]
rtxCodec := &RtpCodecCapability{
Kind: codec.Kind,
MimeType: fmt.Sprintf("%s/rtx", codec.Kind),
PreferredPayloadType: pt,
ClockRate: codec.ClockRate,
Parameters: RtpCodecSpecificParameters{
Apt: codec.PreferredPayloadType,
},
RtcpFeedback: []RtcpFeedback{},
}
// Append to the codec list.
caps.Codecs = append(caps.Codecs, rtxCodec)
}
}
return
}
/**
* Get a mapping of the codec payload, RTP header extensions and encodings from
* the given Producer RTP parameters to the values expected by the Router.
*
*/
func getProducerRtpParametersMapping(params RtpParameters, caps RtpCapabilities) (rtpMapping RtpMapping, err error) {
// Match parameters media codecs to capabilities media codecs.
codecToCapCodec := map[*RtpCodecParameters]*RtpCodecCapability{}
for _, codec := range params.Codecs {
if codec.isRtxCodec() {
continue
}
matchedCapCodec, matched := findMatchedCodec(codec, caps.Codecs, matchOptions{strict: true, modify: true})
if !matched {
err = NewUnsupportedError("unsupported codec [mimeType:%s, payloadType:%d]", codec.MimeType, codec.PayloadType)
return
}
codecToCapCodec[codec] = matchedCapCodec
}
for _, codec := range params.Codecs {
if !codec.isRtxCodec() {
continue
}
var associatedMediaCodec *RtpCodecParameters
// Search for the associated media codec.
for _, mediaCodec := range params.Codecs {
if mediaCodec.PayloadType == codec.Parameters.Apt {
associatedMediaCodec = mediaCodec
break
}
}
if associatedMediaCodec == nil {
err = NewTypeError(`missing media codec found for RTX PT %d`, codec.PayloadType)
return
}
capMediaCodec := codecToCapCodec[associatedMediaCodec]
var associatedCapRtxCodec *RtpCodecCapability
// Ensure that the capabilities media codec has a RTX codec.
for _, capCodec := range caps.Codecs {
if !capCodec.isRtxCodec() {
continue
}
if capCodec.Parameters.Apt == capMediaCodec.PreferredPayloadType {
associatedCapRtxCodec = capCodec
break
}
}
if associatedCapRtxCodec == nil {
err = NewUnsupportedError("no RTX codec for capability codec PT %d", capMediaCodec.PreferredPayloadType)
return
}
codecToCapCodec[codec] = associatedCapRtxCodec
}
// Generate codecs mapping.
for codec, capCodec := range codecToCapCodec {
rtpMapping.Codecs = append(rtpMapping.Codecs, RtpMappingCodec{
PayloadType: codec.PayloadType,
MappedPayloadType: capCodec.PreferredPayloadType,
})
}
// Generate encodings mapping.
mappedSsrc := generateRandomNumber()
for _, encoding := range params.Encodings {
mappedEncoding := RtpMappingEncoding{
Rid: encoding.Rid,
Ssrc: encoding.Ssrc,
MappedSsrc: mappedSsrc,
ScalabilityMode: encoding.ScalabilityMode,
}
mappedSsrc++
rtpMapping.Encodings = append(rtpMapping.Encodings, mappedEncoding)
}
return
}
/**
* Generate RTP parameters for Consumers given the RTP parameters of a Producer
* and the RTP capabilities of the Router.
*
*/
func getConsumableRtpParameters(
kind MediaKind,
params RtpParameters,
caps RtpCapabilities,
rtpMapping RtpMapping,
) (consumableParams RtpParameters, err error) {
for _, codec := range params.Codecs {
if codec.isRtxCodec() {
continue
}
var consumableCodecPt byte
for _, entry := range rtpMapping.Codecs {
if entry.PayloadType == codec.PayloadType {
consumableCodecPt = entry.MappedPayloadType
break
}
}
var matchedCapCodec *RtpCodecCapability
for _, capCodec := range caps.Codecs {
if capCodec.PreferredPayloadType == consumableCodecPt {
matchedCapCodec = capCodec
break
}
}
consumableCodec := &RtpCodecParameters{
MimeType: matchedCapCodec.MimeType,
ClockRate: matchedCapCodec.ClockRate,
Channels: matchedCapCodec.Channels,
RtcpFeedback: matchedCapCodec.RtcpFeedback,
Parameters: codec.Parameters, // Keep the Producer parameters.
PayloadType: matchedCapCodec.PreferredPayloadType,
}
consumableCodec.Parameters = codec.Parameters // Keep the Producer parameters.
consumableParams.Codecs = append(consumableParams.Codecs, consumableCodec)
var consumableCapRtxCodec *RtpCodecCapability
for _, capRtxCodec := range caps.Codecs {
if capRtxCodec.isRtxCodec() &&
capRtxCodec.Parameters.Apt == consumableCodec.PayloadType {
consumableCapRtxCodec = capRtxCodec
break
}
}
if consumableCapRtxCodec != nil {
consumableRtxCodec := &RtpCodecParameters{
MimeType: consumableCapRtxCodec.MimeType,
ClockRate: consumableCapRtxCodec.ClockRate,
Channels: consumableCapRtxCodec.Channels,
RtcpFeedback: consumableCapRtxCodec.RtcpFeedback,
Parameters: consumableCapRtxCodec.Parameters,
PayloadType: consumableCapRtxCodec.PreferredPayloadType,
}
consumableParams.Codecs = append(consumableParams.Codecs, consumableRtxCodec)
}
}
for _, capExt := range caps.HeaderExtensions {
if capExt.Kind != kind ||
(capExt.Direction != Direction_Sendrecv && capExt.Direction != Direction_Sendonly) {
continue
}
consumableExt := RtpHeaderExtensionParameters{
Uri: capExt.Uri,
Id: capExt.PreferredId,
Encrypt: capExt.PreferredEncrypt,
}
consumableParams.HeaderExtensions = append(consumableParams.HeaderExtensions, consumableExt)
}
for i, encoding := range params.Encodings {
// Remove useless fields.
encoding.Rid = ""
encoding.Rtx = nil
encoding.CodecPayloadType = 0
// Set the mapped ssrc.
encoding.Ssrc = rtpMapping.Encodings[i].MappedSsrc
consumableParams.Encodings = append(consumableParams.Encodings, encoding)
}
consumableParams.Rtcp = RtcpParameters{
Cname: params.Rtcp.Cname,
ReducedSize: Bool(true),
Mux: Bool(true),
}
return
}
/**
* Check whether the given RTP capabilities can consume the given Producer.
*
*/
func canConsume(consumableParams RtpParameters, caps RtpCapabilities) (ok bool, err error) {
if err = validateRtpCapabilities(&caps); err != nil {
return
}
var matchingCodecs []*RtpCodecCapability
for _, codec := range consumableParams.Codecs {
matchedCodec, matched := findMatchedCodec(codec, caps.Codecs, matchOptions{strict: true})
if !matched {
continue
}
matchingCodecs = append(matchingCodecs, matchedCodec)
}
// Ensure there is at least one media codec.
if len(matchingCodecs) == 0 || matchingCodecs[0].isRtxCodec() {
return
}
return true, nil
}
/**
* Generate RTP parameters for a specific Consumer.
*
* It reduces encodings to just one and takes into account given RTP capabilities
* to reduce codecs, codecs" RTCP feedback and header extensions, and also enables
* or disabled RTX.
*
*/
func getConsumerRtpParameters(consumableParams RtpParameters, caps RtpCapabilities, pipe bool) (consumerParams RtpParameters, err error) {
for _, capCodec := range caps.Codecs {
if err = validateRtpCodecCapability(capCodec); err != nil {
return
}
}
consumableCodecs := []*RtpCodecParameters{}
rtxSupported := false
clone(consumableParams.Codecs, &consumableCodecs)
for _, codec := range consumableCodecs {
matchedCapCodec, matched := findMatchedCodec(codec, caps.Codecs, matchOptions{strict: true})
if !matched {
continue
}
codec.RtcpFeedback = matchedCapCodec.RtcpFeedback
consumerParams.Codecs = append(consumerParams.Codecs, codec)
}
codecs := consumerParams.Codecs[:0]
// Must sanitize the list of matched codecs by removing useless RTX codecs.
for _, codec := range consumerParams.Codecs {
if codec.isRtxCodec() {
// Search for the associated media codec.
for _, mediaCodec := range consumerParams.Codecs {
if mediaCodec.PayloadType == codec.Parameters.Apt {
rtxSupported = true
codecs = append(codecs, codec)
break
}
}
} else {
codecs = append(codecs, codec)
}
}
consumerParams.Codecs = codecs
// Ensure there is at least one media codec.
if len(consumerParams.Codecs) == 0 || consumerParams.Codecs[0].isRtxCodec() {
err = NewUnsupportedError("no compatible media codecs")
return
}
for _, ext := range consumableParams.HeaderExtensions {
for _, capExt := range caps.HeaderExtensions {
if capExt.PreferredId == ext.Id && capExt.Uri == ext.Uri {
consumerParams.HeaderExtensions = append(consumerParams.HeaderExtensions, ext)
break
}
}
}
// Reduce codecs' RTCP feedback. Use Transport-CC if available, REMB otherwise.
if matchHeaderExtensionUri(consumerParams.HeaderExtensions,
"http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01") {
for i := range consumerParams.Codecs {
consumerParams.Codecs[i].RtcpFeedback = filterRtcpFeedback(
consumerParams.Codecs[i].RtcpFeedback,
func(fb RtcpFeedback) bool {
return fb.Type != "goog-remb"
},
)
}
} else if matchHeaderExtensionUri(consumerParams.HeaderExtensions,
"http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time") {
for i := range consumerParams.Codecs {
consumerParams.Codecs[i].RtcpFeedback = filterRtcpFeedback(
consumerParams.Codecs[i].RtcpFeedback,
func(fb RtcpFeedback) bool {
return fb.Type != "transport-cc"
},
)
}
} else {
for i := range consumerParams.Codecs {
consumerParams.Codecs[i].RtcpFeedback = filterRtcpFeedback(
consumerParams.Codecs[i].RtcpFeedback,
func(fb RtcpFeedback) bool {
return fb.Type != "transport-cc" && fb.Type != "goog-remb"
},
)
}
}
if pipe {
var consumableEncodings []RtpEncodingParameters
clone(consumableParams.Encodings, &consumableEncodings)
baseSsrc := generateRandomNumber()
baseRtxSsrc := generateRandomNumber()
for i := 0; i < len(consumableEncodings); i++ {
encoding := consumableEncodings[i]
encoding.Ssrc = baseSsrc + uint32(i)
if rtxSupported {
encoding.Rtx = &RtpEncodingRtx{Ssrc: baseRtxSsrc + uint32(i)}
} else {
encoding.Rtx = nil
}
consumerParams.Encodings = append(consumerParams.Encodings, encoding)
}
return
}
consumerEncoding := RtpEncodingParameters{
Ssrc: generateRandomNumber(),
}
if rtxSupported {
consumerEncoding.Rtx = &RtpEncodingRtx{
Ssrc: generateRandomNumber(),
}
}
var scalabilityMode string
// If any of the consumableParams.encodings has scalabilityMode, process it
// (assume all encodings have the same value).
for _, encoding := range consumableParams.Encodings {
if len(encoding.ScalabilityMode) > 0 {
scalabilityMode = encoding.ScalabilityMode
break
}
}
// If there is simulast, mangle spatial layers in scalabilityMode.
if len(consumableParams.Encodings) > 1 {
temporalLayers := ParseScalabilityMode(scalabilityMode).TemporalLayers
scalabilityMode = fmt.Sprintf("S%dT%d", len(consumableParams.Encodings), temporalLayers)
}
consumerEncoding.ScalabilityMode = scalabilityMode
maxEncodingMaxBitrate := 0
// Use the maximum maxBitrate in any encoding and honor it in the Consumer's encoding.
for _, encoding := range consumableParams.Encodings {
if encoding.MaxBitrate > maxEncodingMaxBitrate {
maxEncodingMaxBitrate = encoding.MaxBitrate
}
}
if maxEncodingMaxBitrate > 0 {
consumerEncoding.MaxBitrate = maxEncodingMaxBitrate
}
// Set a single encoding for the Consumer.
consumerParams.Encodings = append(consumerParams.Encodings, consumerEncoding)
// Copy verbatim.
consumerParams.Rtcp = consumableParams.Rtcp
return
}
/**
* Generate RTP parameters for a pipe Consumer.
*
* It keeps all original consumable encodings and removes support for BWE. If
* enableRtx is false, it also removes RTX and NACK support.
*/
func getPipeConsumerRtpParameters(consumableParams RtpParameters, enableRtx bool) (consumerParams RtpParameters) {
consumerParams.Rtcp = consumableParams.Rtcp
consumableCodecs := []*RtpCodecParameters{}
clone(consumableParams.Codecs, &consumableCodecs)
for _, codec := range consumableCodecs {
if !enableRtx && codec.isRtxCodec() {
continue
}
codec.RtcpFeedback = filterRtcpFeedback(codec.RtcpFeedback, func(fb RtcpFeedback) bool {
return (fb.Type == "nack" && fb.Parameter == "pli") ||
(fb.Type == "ccm" && fb.Parameter == "fir") ||
(enableRtx && fb.Type == "nack" && len(fb.Parameter) == 0)
})
consumerParams.Codecs = append(consumerParams.Codecs, codec)
}
// Reduce RTP extensions by disabling transport MID and BWE related ones.
for _, ext := range consumableParams.HeaderExtensions {
if ext.Uri != "urn:ietf:params:rtp-hdrext:sdes:mid" &&
ext.Uri != "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time" &&
ext.Uri != "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01" {
consumerParams.HeaderExtensions = append(consumerParams.HeaderExtensions, ext)
}
}
consumableEncodings := []RtpEncodingParameters{}
clone(consumableParams.Encodings, &consumableEncodings)
baseSsrc := generateRandomNumber()
baseRtxSsrc := generateRandomNumber()
for i, encoding := range consumableEncodings {
encoding.Ssrc = baseSsrc + uint32(i)
if enableRtx {
encoding.Rtx = &RtpEncodingRtx{Ssrc: baseRtxSsrc + uint32(i)}
} else {
encoding.Rtx = nil
}
consumerParams.Encodings = append(consumerParams.Encodings, encoding)
}
return
}
func findMatchedCodec(aCodec interface{}, bCodecs []*RtpCodecCapability, options matchOptions) (codec *RtpCodecCapability, matched bool) {
var rtpCodecParameters *RtpCodecParameters
switch aCodec := aCodec.(type) {
case *RtpCodecCapability:
rtpCodecParameters = &RtpCodecParameters{
MimeType: aCodec.MimeType,
ClockRate: aCodec.ClockRate,
Channels: aCodec.Channels,
Parameters: aCodec.Parameters,
}
case *RtpCodecParameters:
rtpCodecParameters = aCodec
}
for _, bCodec := range bCodecs {
if matchCodecs(rtpCodecParameters, bCodec, options) {
return bCodec, true
}
}
return
}
func matchCodecs(aCodec *RtpCodecParameters, bCodec *RtpCodecCapability, options matchOptions) (matched bool) {
aMimeType := strings.ToLower(aCodec.MimeType)
bMimeType := strings.ToLower(bCodec.MimeType)
if aMimeType != bMimeType {
return
}
if aCodec.ClockRate != bCodec.ClockRate {
return
}
if strings.HasPrefix(aMimeType, "audio/") &&
aCodec.Channels > 0 &&
bCodec.Channels > 0 &&
aCodec.Channels != bCodec.Channels {
return
}
switch aMimeType {
case "audio/multiopus":
aNumStreams := aCodec.Parameters.NumStreams
bNumstreams := bCodec.Parameters.NumStreams
if aNumStreams != bNumstreams {
return false
}
aCoupledStreams := aCodec.Parameters.CoupledStreams
bCoupledStreams := bCodec.Parameters.CoupledStreams
if aCoupledStreams != bCoupledStreams {
return false
}
case "video/h264":
aParameters, bParameters := aCodec.Parameters, bCodec.Parameters
if aParameters.PacketizationMode != bParameters.PacketizationMode {
return
}
if options.strict {
selectedProfileLevelId, err := h264.GenerateProfileLevelIdForAnswer(
aParameters.RtpParameter, bParameters.RtpParameter)
if err != nil {
return
}
if options.modify {
aParameters.ProfileLevelId = selectedProfileLevelId
aCodec.Parameters = aParameters
}
}
}
return true
}
func matchHeaderExtensionUri(exts []RtpHeaderExtensionParameters, uri string) bool {
for _, ext := range exts {
if ext.Uri == uri {
return true
}
}
return false
}
func filterRtcpFeedback(arr []RtcpFeedback, cond func(RtcpFeedback) bool) []RtcpFeedback {
newArr := arr[:0]
for _, x := range arr {
if cond(x) {
newArr = append(newArr, x)
}
}
return newArr
}