Avoid reconnect loop for unsupported downtrack (#1754)
* Avoid reconnect loop for unsupported downtrack If the client subscribes to a track which codec is unsupported by the client, sfu will trigger negotiation failed and issue a full reconnect after received client answer. If the client try to subscribe that track then it will got full reconnect again. That will cause a infinite reconnect loop until the client don't subscribe that track. This PR will unsubscribe the error track for the client and send a SubscriptionResponse that contain the reason to indicates the track's codec is not supported to avoid the reconnect loop.
This commit is contained in:
+30
-5
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -61,6 +62,8 @@ type RTCClient struct {
|
||||
// map of livekit.ParticipantID and last packet
|
||||
lastPackets map[livekit.ParticipantID]*rtp.Packet
|
||||
bytesReceived map[livekit.ParticipantID]uint64
|
||||
|
||||
subscriptionResponse atomic.Pointer[livekit.SubscriptionResponse]
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -80,9 +83,10 @@ var (
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
AutoSubscribe bool
|
||||
Publish string
|
||||
ClientInfo *livekit.ClientInfo
|
||||
AutoSubscribe bool
|
||||
Publish string
|
||||
ClientInfo *livekit.ClientInfo
|
||||
DisabledCodecs []webrtc.RTPCodecCapability
|
||||
}
|
||||
|
||||
func NewWebSocketConn(host, token string, opts *Options) (*websocket.Conn, error) {
|
||||
@@ -116,7 +120,7 @@ func SetAuthorizationToken(header http.Header, token string) {
|
||||
header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
||||
func NewRTCClient(conn *websocket.Conn, opts *Options) (*RTCClient, error) {
|
||||
var err error
|
||||
|
||||
c := &RTCClient{
|
||||
@@ -139,7 +143,8 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
||||
}
|
||||
conf.SettingEngine.SetLite(false)
|
||||
conf.SettingEngine.SetAnsweringDTLSRole(webrtc.DTLSRoleClient)
|
||||
codecs := []*livekit.Codec{
|
||||
var codecs []*livekit.Codec
|
||||
for _, codec := range []*livekit.Codec{
|
||||
{
|
||||
Mime: "audio/opus",
|
||||
},
|
||||
@@ -149,7 +154,21 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
||||
{
|
||||
Mime: "video/h264",
|
||||
},
|
||||
} {
|
||||
var disabled bool
|
||||
if opts != nil {
|
||||
for _, dc := range opts.DisabledCodecs {
|
||||
if strings.EqualFold(dc.MimeType, codec.Mime) && (dc.SDPFmtpLine == "" || dc.SDPFmtpLine == codec.FmtpLine) {
|
||||
disabled = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !disabled {
|
||||
codecs = append(codecs, codec)
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// The signal targets are from point of view of server.
|
||||
// From client side, they are flipped,
|
||||
@@ -348,6 +367,8 @@ func (c *RTCClient) Run() error {
|
||||
c.lock.Unlock()
|
||||
case *livekit.SignalResponse_Pong:
|
||||
c.pongReceivedAt.Store(msg.Pong)
|
||||
case *livekit.SignalResponse_SubscriptionResponse:
|
||||
c.subscriptionResponse.Store(msg.SubscriptionResponse)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -452,6 +473,10 @@ func (c *RTCClient) PongReceivedAt() int64 {
|
||||
return c.pongReceivedAt.Load()
|
||||
}
|
||||
|
||||
func (c *RTCClient) GetSubscriptionResponseAndClear() *livekit.SubscriptionResponse {
|
||||
return c.subscriptionResponse.Swap(nil)
|
||||
}
|
||||
|
||||
func (c *RTCClient) SendPing() error {
|
||||
return c.SendRequest(&livekit.SignalRequest{
|
||||
Message: &livekit.SignalRequest_Ping{
|
||||
|
||||
@@ -196,7 +196,7 @@ func createRTCClient(name string, port int, opts *testclient.Options) *testclien
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c, err := testclient.NewRTCClient(ws)
|
||||
c, err := testclient.NewRTCClient(ws, opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -213,7 +213,7 @@ func createRTCClientWithToken(token string, port int, opts *testclient.Options)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c, err := testclient.NewRTCClient(ws)
|
||||
c, err := testclient.NewRTCClient(ws, opts)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -543,3 +543,111 @@ func TestDeviceCodecOverride(t *testing.T) {
|
||||
}
|
||||
require.True(t, hasSeenVP8, "should have seen VP8 codec in SDP")
|
||||
}
|
||||
|
||||
func TestSubscribeToCodecUnsupported(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.SkipNow()
|
||||
return
|
||||
}
|
||||
|
||||
_, finish := setupSingleNodeTest("TestSubscribeToCodecUnsupported")
|
||||
defer finish()
|
||||
|
||||
c1 := createRTCClient("c1", defaultServerPort, nil)
|
||||
// create a client that doesn't support H264
|
||||
c2 := createRTCClient("c2", defaultServerPort, &testclient.Options{
|
||||
AutoSubscribe: true,
|
||||
DisabledCodecs: []webrtc.RTPCodecCapability{
|
||||
{MimeType: "video/H264"},
|
||||
},
|
||||
})
|
||||
waitUntilConnected(t, c1, c2)
|
||||
|
||||
// publish a vp8 video track and ensure c2 receives it ok
|
||||
t1, err := c1.AddStaticTrack("audio/opus", "audio", "webcam")
|
||||
require.NoError(t, err)
|
||||
defer t1.Stop()
|
||||
t2, err := c1.AddStaticTrack("video/vp8", "video", "webcam")
|
||||
require.NoError(t, err)
|
||||
defer t2.Stop()
|
||||
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if len(c2.SubscribedTracks()) == 0 {
|
||||
return "c2 was not subscribed to anything"
|
||||
}
|
||||
// should have received two tracks
|
||||
if len(c2.SubscribedTracks()[c1.ID()]) != 2 {
|
||||
return "c2 was not subscribed to tracks from c1"
|
||||
}
|
||||
|
||||
tracks := c2.SubscribedTracks()[c1.ID()]
|
||||
for _, t := range tracks {
|
||||
if strings.EqualFold(t.Codec().MimeType, "video/vp8") {
|
||||
return ""
|
||||
|
||||
}
|
||||
}
|
||||
return "did not receive track with vp8"
|
||||
})
|
||||
require.Nil(t, c2.GetSubscriptionResponseAndClear())
|
||||
|
||||
// publish a h264 track and ensure c2 got subscription error
|
||||
t3, err := c1.AddStaticTrackWithCodec(webrtc.RTPCodecCapability{
|
||||
MimeType: "video/h264",
|
||||
ClockRate: 90000,
|
||||
SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
|
||||
}, "videoscreen", "screen")
|
||||
defer t3.Stop()
|
||||
require.NoError(t, err)
|
||||
|
||||
var h264TrackID string
|
||||
require.Eventually(t, func() bool {
|
||||
remoteC1 := c2.GetRemoteParticipant(c1.ID())
|
||||
require.NotNil(t, remoteC1)
|
||||
for _, track := range remoteC1.Tracks {
|
||||
if strings.EqualFold(track.MimeType, "video/h264") {
|
||||
h264TrackID = track.Sid
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}, time.Second, 10*time.Millisecond, "did not receive track info with h264")
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
sr := c2.GetSubscriptionResponseAndClear()
|
||||
if sr == nil {
|
||||
return false
|
||||
}
|
||||
require.Equal(t, h264TrackID, sr.TrackSid)
|
||||
require.Equal(t, livekit.SubscriptionError_SE_CODEC_UNSUPPORTED, sr.Err)
|
||||
return true
|
||||
}, time.Second, 10*time.Millisecond, "did not receive subscription response")
|
||||
|
||||
// publish another vp8 track again, ensure the transport recovered by sfu and c2 can receive it
|
||||
t4, err := c1.AddStaticTrack("video/vp8", "video2", "webcam2")
|
||||
require.NoError(t, err)
|
||||
defer t4.Stop()
|
||||
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if len(c2.SubscribedTracks()) == 0 {
|
||||
return "c2 was not subscribed to anything"
|
||||
}
|
||||
// should have received two tracks
|
||||
if len(c2.SubscribedTracks()[c1.ID()]) != 3 {
|
||||
return "c2 was not subscribed to tracks from c1"
|
||||
}
|
||||
|
||||
var vp8Count int
|
||||
tracks := c2.SubscribedTracks()[c1.ID()]
|
||||
for _, t := range tracks {
|
||||
if strings.EqualFold(t.Codec().MimeType, "video/vp8") {
|
||||
vp8Count++
|
||||
}
|
||||
}
|
||||
if vp8Count == 2 {
|
||||
return ""
|
||||
}
|
||||
return "did not 2 receive track with vp8"
|
||||
})
|
||||
require.Nil(t, c2.GetSubscriptionResponseAndClear())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user