Filter out UDP candidates from SDP if allowing only TCP. (#874)
* Filter out UDP candidates from SDP if allowing only TCP. * Do it properly * static check * Adding TCP candidates to test
This commit is contained in:
+20
-13
@@ -573,6 +573,8 @@ func (p *ParticipantImpl) HandleOffer(sdp webrtc.SessionDescription) (answer web
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
answer = p.publisher.FilterCandidates(answer)
|
||||||
|
|
||||||
if err = p.publisher.pc.SetLocalDescription(answer); err != nil {
|
if err = p.publisher.pc.SetLocalDescription(answer); err != nil {
|
||||||
prometheus.ServiceOperationCounter.WithLabelValues("answer", "error", "local_description").Add(1)
|
prometheus.ServiceOperationCounter.WithLabelValues("answer", "error", "local_description").Add(1)
|
||||||
err = errors.Wrap(err, "could not set local description")
|
err = errors.Wrap(err, "could not set local description")
|
||||||
@@ -842,9 +844,7 @@ func (p *ParticipantImpl) MigrateState() types.MigrateState {
|
|||||||
// ICERestart restarts subscriber ICE connections
|
// ICERestart restarts subscriber ICE connections
|
||||||
func (p *ParticipantImpl) ICERestart(iceConfig *types.IceConfig) error {
|
func (p *ParticipantImpl) ICERestart(iceConfig *types.IceConfig) error {
|
||||||
if iceConfig != nil {
|
if iceConfig != nil {
|
||||||
p.lock.Lock()
|
p.SetICEConfig(*iceConfig)
|
||||||
p.iceConfig = *iceConfig
|
|
||||||
p.lock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.subscriber.pc.RemoteDescription() == nil {
|
if p.subscriber.pc.RemoteDescription() == nil {
|
||||||
@@ -868,7 +868,20 @@ func (p *ParticipantImpl) OnICEConfigChanged(f func(participant types.LocalParti
|
|||||||
func (p *ParticipantImpl) SetICEConfig(iceConfig types.IceConfig) {
|
func (p *ParticipantImpl) SetICEConfig(iceConfig types.IceConfig) {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
p.iceConfig = iceConfig
|
p.iceConfig = iceConfig
|
||||||
|
if iceConfig.PreferPubTcp {
|
||||||
|
p.publisher.SetPreferTCP(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
if iceConfig.PreferSubTcp {
|
||||||
|
p.subscriber.SetPreferTCP(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
onICEConfigChanged := p.onICEConfigChanged
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
|
|
||||||
|
if onICEConfigChanged != nil {
|
||||||
|
onICEConfigChanged(p, iceConfig)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -1340,16 +1353,10 @@ func (p *ParticipantImpl) handleConnectionFailed(isPrimary bool) {
|
|||||||
pcTransport.Logger().Infow("short ICE connection", "pair", pair, "duration", duration)
|
pcTransport.Logger().Infow("short ICE connection", "pair", pair, "duration", duration)
|
||||||
}
|
}
|
||||||
pcTransport.Logger().Infow("restricting transport to TCP on both peer connections")
|
pcTransport.Logger().Infow("restricting transport to TCP on both peer connections")
|
||||||
p.lock.Lock()
|
p.SetICEConfig(types.IceConfig{
|
||||||
p.iceConfig.PreferSubTcp = true
|
PreferPubTcp: true,
|
||||||
p.iceConfig.PreferPubTcp = true
|
PreferSubTcp: true,
|
||||||
onICEConfigChanged := p.onICEConfigChanged
|
})
|
||||||
iceConfig := p.iceConfig
|
|
||||||
p.lock.Unlock()
|
|
||||||
|
|
||||||
if onICEConfigChanged != nil {
|
|
||||||
onICEConfigChanged(p, iceConfig)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,6 +79,8 @@ type PCTransport struct {
|
|||||||
streamAllocator *sfu.StreamAllocator
|
streamAllocator *sfu.StreamAllocator
|
||||||
|
|
||||||
previousAnswer *webrtc.SessionDescription
|
previousAnswer *webrtc.SessionDescription
|
||||||
|
|
||||||
|
preferTCP bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type TransportParams struct {
|
type TransportParams struct {
|
||||||
@@ -256,6 +258,12 @@ func (t *PCTransport) GetSelectedPair() (*webrtc.ICECandidatePair, error) {
|
|||||||
return iceTransport.GetSelectedCandidatePair()
|
return iceTransport.GetSelectedCandidatePair()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *PCTransport) SetPreferTCP(preferTCP bool) {
|
||||||
|
t.lock.Lock()
|
||||||
|
t.preferTCP = preferTCP
|
||||||
|
t.lock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (t *PCTransport) createPeerConnection() error {
|
func (t *PCTransport) createPeerConnection() error {
|
||||||
var bwe cc.BandwidthEstimator
|
var bwe cc.BandwidthEstimator
|
||||||
pc, me, err := newPeerConnection(t.params, func(estimator cc.BandwidthEstimator) {
|
pc, me, err := newPeerConnection(t.params, func(estimator cc.BandwidthEstimator) {
|
||||||
@@ -486,6 +494,8 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
offer = t.filterCandidates(offer)
|
||||||
|
|
||||||
err = t.pc.SetLocalDescription(offer)
|
err = t.pc.SetLocalDescription(offer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "local_description").Add(1)
|
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "local_description").Add(1)
|
||||||
@@ -668,6 +678,55 @@ func (t *PCTransport) SetPreviousAnswer(answer *webrtc.SessionDescription) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *PCTransport) FilterCandidates(sd webrtc.SessionDescription) webrtc.SessionDescription {
|
||||||
|
t.lock.RLock()
|
||||||
|
defer t.lock.RUnlock()
|
||||||
|
|
||||||
|
return t.filterCandidates(sd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *PCTransport) filterCandidates(sd webrtc.SessionDescription) webrtc.SessionDescription {
|
||||||
|
parsed, err := sd.Unmarshal()
|
||||||
|
if err != nil {
|
||||||
|
t.params.Logger.Errorw("could not unmarshal SDP to filter candidates", err)
|
||||||
|
return sd
|
||||||
|
}
|
||||||
|
|
||||||
|
filterAttributes := func(attrs []sdp.Attribute) []sdp.Attribute {
|
||||||
|
filteredAttrs := make([]sdp.Attribute, 0, len(attrs))
|
||||||
|
for _, a := range attrs {
|
||||||
|
if a.Key == "candidate" {
|
||||||
|
if t.preferTCP {
|
||||||
|
if strings.Contains(a.Value, "tcp") {
|
||||||
|
filteredAttrs = append(filteredAttrs, a)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
filteredAttrs = append(filteredAttrs, a)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
filteredAttrs = append(filteredAttrs, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredAttrs
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed.Attributes = filterAttributes(parsed.Attributes)
|
||||||
|
for _, m := range parsed.MediaDescriptions {
|
||||||
|
m.Attributes = filterAttributes(m.Attributes)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := parsed.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
t.params.Logger.Errorw("could not marshal SDP to filter candidates", err)
|
||||||
|
return sd
|
||||||
|
}
|
||||||
|
sd.SDP = string(bytes)
|
||||||
|
return sd
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------
|
||||||
|
|
||||||
func getMidValue(media *sdp.MediaDescription) string {
|
func getMidValue(media *sdp.MediaDescription) string {
|
||||||
for _, attr := range media.Attributes {
|
for _, attr := range media.Attributes {
|
||||||
if attr.Key == "mid" {
|
if attr.Key == "mid" {
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package rtc
|
package rtc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pion/sdp/v3"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
@@ -269,6 +271,125 @@ func TestNegotiationFailed(t *testing.T) {
|
|||||||
}, negotiationFailedTimout+time.Second, 10*time.Millisecond, "negotiation failed")
|
}, negotiationFailedTimout+time.Second, 10*time.Millisecond, "negotiation failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilteringCandidates(t *testing.T) {
|
||||||
|
params := TransportParams{
|
||||||
|
ParticipantID: "id",
|
||||||
|
ParticipantIdentity: "identity",
|
||||||
|
Target: livekit.SignalTarget_PUBLISHER,
|
||||||
|
Config: &WebRTCConfig{},
|
||||||
|
EnabledCodecs: []*livekit.Codec{
|
||||||
|
{Mime: webrtc.MimeTypeOpus},
|
||||||
|
{Mime: webrtc.MimeTypeVP8},
|
||||||
|
{Mime: webrtc.MimeTypeH264},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
transport, err := NewPCTransport(params)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = transport.pc.CreateDataChannel("test", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = transport.pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = transport.pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
offer, err := transport.pc.CreateOffer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
offerGatheringComplete := webrtc.GatheringCompletePromise(transport.pc)
|
||||||
|
require.NoError(t, transport.pc.SetLocalDescription(offer))
|
||||||
|
<-offerGatheringComplete
|
||||||
|
|
||||||
|
// should not filter out UDP candidates if TCP is not preferred
|
||||||
|
offer = *transport.pc.LocalDescription()
|
||||||
|
filteredOffer := transport.FilterCandidates(offer)
|
||||||
|
require.EqualValues(t, offer.SDP, filteredOffer.SDP)
|
||||||
|
|
||||||
|
parsed, err := offer.Unmarshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// add a couple of TCP candidates
|
||||||
|
done := false
|
||||||
|
for _, m := range parsed.MediaDescriptions {
|
||||||
|
for _, a := range m.Attributes {
|
||||||
|
if a.Key == "candidate" {
|
||||||
|
for idx, aa := range m.Attributes {
|
||||||
|
if aa.Key == "end-of-candidates" {
|
||||||
|
modifiedAttributes := make([]sdp.Attribute, idx)
|
||||||
|
copy(modifiedAttributes, m.Attributes[:idx])
|
||||||
|
modifiedAttributes = append(modifiedAttributes, []sdp.Attribute{
|
||||||
|
{
|
||||||
|
Key: "candidate",
|
||||||
|
Value: "054225987 1 tcp 2124414975 159.203.70.248 7881 typ host tcptype passive",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Key: "candidate",
|
||||||
|
Value: "054225987 2 tcp 2124414975 159.203.70.248 7881 typ host tcptype passive",
|
||||||
|
},
|
||||||
|
}...)
|
||||||
|
m.Attributes = append(modifiedAttributes, m.Attributes[idx:]...)
|
||||||
|
done = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if done {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if done {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bytes, err := parsed.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
offer.SDP = string(bytes)
|
||||||
|
|
||||||
|
parsed, err = offer.Unmarshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
getNumTransportTypeCandidates := func(sdp *sdp.SessionDescription) (int, int) {
|
||||||
|
numUDPCandidates := 0
|
||||||
|
numTCPCandidates := 0
|
||||||
|
for _, a := range sdp.Attributes {
|
||||||
|
if a.Key == "candidate" {
|
||||||
|
if strings.Contains(a.Value, "udp") {
|
||||||
|
numUDPCandidates++
|
||||||
|
}
|
||||||
|
if strings.Contains(a.Value, "tcp") {
|
||||||
|
numTCPCandidates++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, m := range sdp.MediaDescriptions {
|
||||||
|
for _, a := range m.Attributes {
|
||||||
|
if a.Key == "candidate" {
|
||||||
|
if strings.Contains(a.Value, "udp") {
|
||||||
|
numUDPCandidates++
|
||||||
|
}
|
||||||
|
if strings.Contains(a.Value, "tcp") {
|
||||||
|
numTCPCandidates++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return numUDPCandidates, numTCPCandidates
|
||||||
|
}
|
||||||
|
udp, tcp := getNumTransportTypeCandidates(parsed)
|
||||||
|
require.NotZero(t, udp)
|
||||||
|
require.Equal(t, 2, tcp)
|
||||||
|
|
||||||
|
transport.SetPreferTCP(true)
|
||||||
|
filteredOffer = transport.FilterCandidates(offer)
|
||||||
|
parsed, err = filteredOffer.Unmarshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
udp, tcp = getNumTransportTypeCandidates(parsed)
|
||||||
|
require.Zero(t, udp)
|
||||||
|
require.Equal(t, 2, tcp)
|
||||||
|
}
|
||||||
|
|
||||||
func handleOfferFunc(t *testing.T, current, other *PCTransport) func(sd webrtc.SessionDescription) {
|
func handleOfferFunc(t *testing.T, current, other *PCTransport) func(sd webrtc.SessionDescription) {
|
||||||
return func(sd webrtc.SessionDescription) {
|
return func(sd webrtc.SessionDescription) {
|
||||||
t.Logf("handling offer")
|
t.Logf("handling offer")
|
||||||
|
|||||||
Reference in New Issue
Block a user