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:
cnderrauber
2023-05-31 11:41:22 +08:00
committed by GitHub
parent 13d599d2d9
commit c1842cb54f
14 changed files with 226 additions and 47 deletions
+30 -5
View File
@@ -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{