8
8
"crypto/elliptic"
9
9
"crypto/rand"
10
10
"fmt"
11
+ "io"
11
12
"strconv"
12
13
"strings"
13
14
"sync"
@@ -877,24 +878,35 @@ func (pc *PeerConnection) SetRemoteDescription(desc SessionDescription) error {
877
878
}
878
879
879
880
func (pc * PeerConnection ) startReceiver (incoming trackDetails , receiver * RTPReceiver ) {
880
- err := receiver .Receive (RTPReceiveParameters {
881
- Encodings : RTPDecodingParameters {
882
- RTPCodingParameters {SSRC : incoming .ssrc },
883
- }})
884
- if err != nil {
881
+ encodings := []RTPDecodingParameters {}
882
+ if incoming .ssrc != 0 {
883
+ encodings = append (encodings , RTPDecodingParameters {RTPCodingParameters {SSRC : incoming .ssrc }})
884
+ }
885
+ for _ , rid := range incoming .rids {
886
+ encodings = append (encodings , RTPDecodingParameters {RTPCodingParameters {RID : rid }})
887
+ }
888
+
889
+ if err := receiver .Receive (RTPReceiveParameters {Encodings : encodings }); err != nil {
885
890
pc .log .Warnf ("RTPReceiver Receive failed %s" , err )
886
891
return
887
892
}
888
893
889
894
// set track id and label early so they can be set as new track information
890
895
// is received from the SDP.
891
- receiver .Track ().mu .Lock ()
892
- receiver .Track ().id = incoming .id
893
- receiver .Track ().label = incoming .label
894
- receiver .Track ().mu .Unlock ()
896
+ for i := range receiver .tracks {
897
+ receiver .tracks [i ].track .mu .Lock ()
898
+ receiver .tracks [i ].track .id = incoming .id
899
+ receiver .tracks [i ].track .label = incoming .label
900
+ receiver .tracks [i ].track .mu .Unlock ()
901
+ }
902
+
903
+ // We can't block and wait for a single SSRC
904
+ if incoming .ssrc == 0 {
905
+ return
906
+ }
895
907
896
908
go func () {
897
- if err = receiver .Track ().determinePayloadType (); err != nil {
909
+ if err : = receiver .Track ().determinePayloadType (); err != nil {
898
910
pc .log .Warnf ("Could not determine PayloadType for SSRC %d" , receiver .Track ().SSRC ())
899
911
return
900
912
}
@@ -922,7 +934,7 @@ func (pc *PeerConnection) startReceiver(incoming trackDetails, receiver *RTPRece
922
934
}
923
935
924
936
// startRTPReceivers opens knows inbound SRTP streams from the RemoteDescription
925
- func (pc * PeerConnection ) startRTPReceivers (incomingTracks map [ uint32 ]trackDetails , currentTransceivers []* RTPTransceiver ) {
937
+ func (pc * PeerConnection ) startRTPReceivers (incomingTracks [ ]trackDetails , currentTransceivers []* RTPTransceiver ) {
926
938
localTransceivers := append ([]* RTPTransceiver {}, currentTransceivers ... )
927
939
928
940
remoteIsPlanB := false
@@ -934,45 +946,54 @@ func (pc *PeerConnection) startRTPReceivers(incomingTracks map[uint32]trackDetai
934
946
}
935
947
936
948
// Ensure we haven't already started a transceiver for this ssrc
937
- for ssrc := range incomingTracks {
938
- for i := range localTransceivers {
939
- if t := localTransceivers [i ]; (t .Receiver ()) == nil || t .Receiver ().Track () == nil || t .Receiver ().Track ().ssrc != ssrc {
949
+ for i := range incomingTracks {
950
+ if len (incomingTracks ) <= i {
951
+ break
952
+ }
953
+ incomingTrack := incomingTracks [i ]
954
+
955
+ for _ , t := range localTransceivers {
956
+ if (t .Receiver ()) == nil || t .Receiver ().Track () == nil || t .Receiver ().Track ().ssrc != incomingTrack .ssrc {
940
957
continue
941
958
}
942
959
943
- delete (incomingTracks , ssrc )
960
+ incomingTracks = filterTrackWithSSRC (incomingTracks , incomingTrack . ssrc )
944
961
}
945
962
}
946
963
947
- for ssrc , incoming := range incomingTracks {
948
- for i := range localTransceivers {
949
- t := localTransceivers [i ]
964
+ for i := range incomingTracks {
965
+ for j := range localTransceivers {
966
+ if len (incomingTracks ) <= i || len (localTransceivers ) <= j {
967
+ break
968
+ }
969
+ t := localTransceivers [j ]
970
+ incomingTrack := incomingTracks [i ]
950
971
951
- if t .Mid () != incoming .mid {
972
+ if t .Mid () != incomingTrack .mid {
952
973
continue
953
974
}
954
975
955
- if (incomingTracks [ ssrc ] .kind != t .kind ) ||
976
+ if (incomingTrack .kind != t .kind ) ||
956
977
(t .Direction () != RTPTransceiverDirectionRecvonly && t .Direction () != RTPTransceiverDirectionSendrecv ) ||
957
978
(t .Receiver ()) == nil ||
958
979
(t .Receiver ().haveReceived ()) {
959
980
continue
960
981
}
961
982
962
- delete (incomingTracks , ssrc )
963
- localTransceivers = append (localTransceivers [:i ], localTransceivers [i + 1 :]... )
964
- pc .startReceiver (incoming , t .Receiver ())
983
+ incomingTracks = append (incomingTracks [: i ], incomingTracks [ i + 1 :] ... )
984
+ localTransceivers = append (localTransceivers [:j ], localTransceivers [j + 1 :]... )
985
+ pc .startReceiver (incomingTrack , t .Receiver ())
965
986
break
966
987
}
967
988
}
968
989
969
990
if remoteIsPlanB {
970
- for ssrc , incoming := range incomingTracks {
991
+ for _ , incoming := range incomingTracks {
971
992
t , err := pc .AddTransceiverFromKind (incoming .kind , RtpTransceiverInit {
972
993
Direction : RTPTransceiverDirectionSendrecv ,
973
994
})
974
995
if err != nil {
975
- pc .log .Warnf ("Could not add transceiver for remote SSRC %d: %s" , ssrc , err )
996
+ pc .log .Warnf ("Could not add transceiver for remote SSRC %d: %s" , incoming . ssrc , err )
976
997
continue
977
998
}
978
999
pc .startReceiver (incoming , t .Receiver ())
@@ -1036,58 +1057,115 @@ func (pc *PeerConnection) startSCTP() {
1036
1057
pc .sctpTransport .lock .Unlock ()
1037
1058
}
1038
1059
1039
- // drainSRTP pulls and discards RTP/RTCP packets that don't match any a:ssrc lines
1040
- // If the remote SDP was only one media section the ssrc doesn't have to be explicitly declared
1041
- func (pc * PeerConnection ) drainSRTP () {
1042
- handleUndeclaredSSRC := func (ssrc uint32 ) bool {
1043
- if remoteDescription := pc .RemoteDescription (); remoteDescription != nil {
1044
- if len (remoteDescription .parsed .MediaDescriptions ) == 1 {
1045
- onlyMediaSection := remoteDescription .parsed .MediaDescriptions [0 ]
1046
- for _ , a := range onlyMediaSection .Attributes {
1047
- if a .Key == ssrcStr {
1048
- return false
1049
- }
1050
- }
1051
-
1052
- incoming := trackDetails {
1053
- ssrc : ssrc ,
1054
- kind : RTPCodecTypeVideo ,
1055
- }
1056
- if onlyMediaSection .MediaName .Media == RTPCodecTypeAudio .String () {
1057
- incoming .kind = RTPCodecTypeAudio
1058
- }
1060
+ func (pc * PeerConnection ) handleUndeclaredSSRC (rtpStream io.Reader , ssrc uint32 ) error {
1061
+ remoteDescription := pc .RemoteDescription ()
1062
+ if remoteDescription == nil {
1063
+ return fmt .Errorf ("remote Description has not been set yet" )
1064
+ }
1059
1065
1060
- t , err := pc .AddTransceiverFromKind (incoming .kind , RtpTransceiverInit {
1061
- Direction : RTPTransceiverDirectionSendrecv ,
1062
- })
1063
- if err != nil {
1064
- pc .log .Warnf ("Could not add transceiver for remote SSRC %d: %s" , ssrc , err )
1065
- return false
1066
- }
1067
- pc .startReceiver (incoming , t .Receiver ())
1068
- return true
1066
+ // If the remote SDP was only one media section the ssrc doesn't have to be explicitly declared
1067
+ if len (remoteDescription .parsed .MediaDescriptions ) == 1 {
1068
+ onlyMediaSection := remoteDescription .parsed .MediaDescriptions [0 ]
1069
+ for _ , a := range onlyMediaSection .Attributes {
1070
+ if a .Key == ssrcStr {
1071
+ return fmt .Errorf ("single media section has an explicit SSRC" )
1069
1072
}
1070
1073
}
1071
1074
1072
- return false
1075
+ incoming := trackDetails {
1076
+ ssrc : ssrc ,
1077
+ kind : RTPCodecTypeVideo ,
1078
+ }
1079
+ if onlyMediaSection .MediaName .Media == RTPCodecTypeAudio .String () {
1080
+ incoming .kind = RTPCodecTypeAudio
1081
+ }
1082
+
1083
+ t , err := pc .AddTransceiverFromKind (incoming .kind , RtpTransceiverInit {
1084
+ Direction : RTPTransceiverDirectionSendrecv ,
1085
+ })
1086
+ if err != nil {
1087
+ return fmt .Errorf ("could not add transceiver for remote SSRC %d: %s" , ssrc , err )
1088
+ }
1089
+ pc .startReceiver (incoming , t .Receiver ())
1090
+ return nil
1091
+ }
1092
+
1093
+ // Simulcast no longer uses SSRCes, but RID instead. We then use that value to populate rest of Track Data
1094
+ matchedSDPMap , err := matchedAnswerExt (pc .RemoteDescription ().parsed , pc .api .settingEngine .getSDPExtensions ())
1095
+ if err != nil {
1096
+ return err
1097
+ }
1098
+
1099
+ sdesMidExtMap := getExtMapByURI (matchedSDPMap , sdp .SDESMidURI )
1100
+ sdesStreamIDExtMap := getExtMapByURI (matchedSDPMap , sdp .SDESRTPStreamIDURI )
1101
+ if sdesMidExtMap == nil || sdesStreamIDExtMap == nil {
1102
+ return fmt .Errorf ("mid and rid RTP Extensions required for Simulcast" )
1103
+ }
1104
+
1105
+ b := make ([]byte , receiveMTU )
1106
+ var mid , rid string
1107
+ for readCount := 0 ; readCount <= simulcastProbeCount ; readCount ++ {
1108
+ i , err := rtpStream .Read (b )
1109
+ if err != nil {
1110
+ return err
1111
+ }
1112
+
1113
+ maybeMid , maybeRid , payloadType , err := handleUnknownRTPPacket (b [:i ], sdesMidExtMap , sdesStreamIDExtMap )
1114
+ if err != nil {
1115
+ return err
1116
+ }
1117
+
1118
+ if maybeMid != "" {
1119
+ mid = maybeMid
1120
+ }
1121
+ if maybeRid != "" {
1122
+ rid = maybeRid
1123
+ }
1124
+
1125
+ if mid == "" || rid == "" {
1126
+ continue
1127
+ }
1128
+
1129
+ codec , err := pc .api .mediaEngine .getCodec (payloadType )
1130
+ if err != nil {
1131
+ return err
1132
+ }
1133
+
1134
+ for _ , t := range pc .GetTransceivers () {
1135
+ if t .Mid () != mid || t .Receiver () == nil {
1136
+ continue
1137
+ }
1138
+
1139
+ track , err := t .Receiver ().receiveForRid (rid , codec , ssrc )
1140
+ if err != nil {
1141
+ return err
1142
+ }
1143
+ pc .onTrack (track , t .Receiver ())
1144
+ return nil
1145
+ }
1073
1146
}
1074
1147
1148
+ return fmt .Errorf ("incoming SSRC failed Simulcast probing" )
1149
+ }
1150
+
1151
+ // undeclaredMediaProcessor handles RTP/RTCP packets that don't match any a:ssrc lines
1152
+ func (pc * PeerConnection ) undeclaredMediaProcessor () {
1075
1153
go func () {
1076
1154
for {
1077
1155
srtpSession , err := pc .dtlsTransport .getSRTPSession ()
1078
1156
if err != nil {
1079
- pc .log .Warnf ("drainSRTP failed to open SrtpSession: %v" , err )
1157
+ pc .log .Warnf ("undeclaredMediaProcessor failed to open SrtpSession: %v" , err )
1080
1158
return
1081
1159
}
1082
1160
1083
- _ , ssrc , err := srtpSession .AcceptStream ()
1161
+ stream , ssrc , err := srtpSession .AcceptStream ()
1084
1162
if err != nil {
1085
1163
pc .log .Warnf ("Failed to accept RTP %v" , err )
1086
1164
return
1087
1165
}
1088
1166
1089
- if ! handleUndeclaredSSRC (ssrc ) {
1090
- pc .log .Warnf ("Incoming unhandled RTP ssrc(%d), OnTrack will not be fired" , ssrc )
1167
+ if err := pc . handleUndeclaredSSRC (stream , ssrc ); err != nil {
1168
+ pc .log .Errorf ("Incoming unhandled RTP ssrc(%d), OnTrack will not be fired. %v " , ssrc , err )
1091
1169
}
1092
1170
}
1093
1171
}()
@@ -1096,7 +1174,7 @@ func (pc *PeerConnection) drainSRTP() {
1096
1174
for {
1097
1175
srtcpSession , err := pc .dtlsTransport .getSRTCPSession ()
1098
1176
if err != nil {
1099
- pc .log .Warnf ("drainSRTP failed to open SrtcpSession: %v" , err )
1177
+ pc .log .Warnf ("undeclaredMediaProcessor failed to open SrtcpSession: %v" , err )
1100
1178
return
1101
1179
}
1102
1180
@@ -1694,13 +1772,14 @@ func (pc *PeerConnection) startRTP(isRenegotiation bool, remoteDesc *SessionDesc
1694
1772
1695
1773
t .Receiver ().Track ().mu .Lock ()
1696
1774
ssrc := t .Receiver ().Track ().ssrc
1697
- if _ , ok := trackDetails [ ssrc ]; ok {
1698
- incoming := trackDetails [ ssrc ]
1699
- t .Receiver ().Track ().id = incoming .id
1700
- t .Receiver ().Track ().label = incoming .label
1775
+
1776
+ if details := trackDetailsForSSRC ( trackDetails , ssrc ); details != nil {
1777
+ t .Receiver ().Track ().id = details .id
1778
+ t .Receiver ().Track ().label = details .label
1701
1779
t .Receiver ().Track ().mu .Unlock ()
1702
1780
continue
1703
1781
}
1782
+
1704
1783
t .Receiver ().Track ().mu .Unlock ()
1705
1784
1706
1785
if err := t .Receiver ().Stop (); err != nil {
@@ -1721,7 +1800,7 @@ func (pc *PeerConnection) startRTP(isRenegotiation bool, remoteDesc *SessionDesc
1721
1800
pc .startRTPSenders (currentTransceivers )
1722
1801
1723
1802
if ! isRenegotiation {
1724
- pc .drainSRTP ()
1803
+ pc .undeclaredMediaProcessor ()
1725
1804
if haveApplicationMediaSection (remoteDesc .parsed ) {
1726
1805
pc .startSCTP ()
1727
1806
}
0 commit comments