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:
@@ -128,7 +128,11 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
|
||||
// Bind callback can happen from replaceTrack, so set it up early
|
||||
var reusingTransceiver atomic.Bool
|
||||
var dtState sfu.DownTrackState
|
||||
downTrack.OnBinding(func() {
|
||||
downTrack.OnBinding(func(err error) {
|
||||
if err != nil {
|
||||
go subTrack.Bound(err)
|
||||
return
|
||||
}
|
||||
wr.DetermineReceiver(downTrack.Codec())
|
||||
if reusingTransceiver.Load() {
|
||||
downTrack.SeedState(dtState)
|
||||
@@ -142,7 +146,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
|
||||
)
|
||||
}
|
||||
|
||||
go subTrack.Bound()
|
||||
go subTrack.Bound(nil)
|
||||
|
||||
subTrack.SetPublisherMuted(t.params.MediaTrack.IsMuted())
|
||||
})
|
||||
|
||||
+23
-3
@@ -997,7 +997,10 @@ func (p *ParticipantImpl) onTrackSubscribed(subTrack types.SubscribedTrack) {
|
||||
subTrack.DownTrack().SetActivePaddingOnMuteUpTrack()
|
||||
}
|
||||
|
||||
subTrack.AddOnBind(func() {
|
||||
subTrack.AddOnBind(func(err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if p.TransportManager.HasSubscriberEverConnected() {
|
||||
subTrack.DownTrack().SetConnected()
|
||||
}
|
||||
@@ -2083,8 +2086,25 @@ func (p *ParticipantImpl) onPublicationError(trackID livekit.TrackID) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) onSubscriptionError(trackID livekit.TrackID) {
|
||||
if p.params.ReconnectOnSubscriptionError {
|
||||
func (p *ParticipantImpl) onSubscriptionError(trackID livekit.TrackID, fatal bool, err error) {
|
||||
signalErr := livekit.SubscriptionError_SE_UNKOWN
|
||||
switch {
|
||||
case errors.Is(err, webrtc.ErrUnsupportedCodec):
|
||||
signalErr = livekit.SubscriptionError_SE_CODEC_UNSUPPORTED
|
||||
case errors.Is(err, ErrTrackNotFound):
|
||||
signalErr = livekit.SubscriptionError_SE_TRACK_NOTFOUND
|
||||
}
|
||||
|
||||
_ = p.writeMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_SubscriptionResponse{
|
||||
SubscriptionResponse: &livekit.SubscriptionResponse{
|
||||
TrackSid: string(trackID),
|
||||
Err: signalErr,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if p.params.ReconnectOnSubscriptionError && fatal {
|
||||
p.params.Logger.Infow("issuing full reconnect on subscription error", "trackID", trackID)
|
||||
p.IssueFullReconnect(types.ParticipantCloseReasonPublicationError)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ type SubscribedTrack struct {
|
||||
needsNegotiation atomic.Bool
|
||||
|
||||
bindLock sync.Mutex
|
||||
onBindCallbacks []func()
|
||||
onBindCallbacks []func(error)
|
||||
onClose atomic.Value // func(bool)
|
||||
bound atomic.Bool
|
||||
|
||||
@@ -61,7 +61,7 @@ func NewSubscribedTrack(params SubscribedTrackParams) *SubscribedTrack {
|
||||
return s
|
||||
}
|
||||
|
||||
func (t *SubscribedTrack) AddOnBind(f func()) {
|
||||
func (t *SubscribedTrack) AddOnBind(f func(error)) {
|
||||
t.bindLock.Lock()
|
||||
bound := t.bound.Load()
|
||||
if !bound {
|
||||
@@ -71,19 +71,21 @@ func (t *SubscribedTrack) AddOnBind(f func()) {
|
||||
|
||||
if bound {
|
||||
// fire immediately, do not need to persist since bind is a one time event
|
||||
go f()
|
||||
go f(nil)
|
||||
}
|
||||
}
|
||||
|
||||
// for DownTrack callback to notify us that it's bound
|
||||
func (t *SubscribedTrack) Bound() {
|
||||
func (t *SubscribedTrack) Bound(err error) {
|
||||
t.bindLock.Lock()
|
||||
t.bound.Store(true)
|
||||
if err == nil {
|
||||
t.bound.Store(true)
|
||||
}
|
||||
callbacks := t.onBindCallbacks
|
||||
t.onBindCallbacks = nil
|
||||
t.bindLock.Unlock()
|
||||
|
||||
if t.MediaTrack().Kind() == livekit.TrackType_VIDEO {
|
||||
if err == nil && t.MediaTrack().Kind() == livekit.TrackType_VIDEO {
|
||||
// When AdaptiveStream is enabled, default the subscriber to LOW quality stream
|
||||
// we would want LOW instead of OFF for a couple of reasons
|
||||
// 1. when a subscriber unsubscribes from a track, we would forget their previously defined settings
|
||||
@@ -107,7 +109,7 @@ func (t *SubscribedTrack) Bound() {
|
||||
}
|
||||
|
||||
for _, cb := range callbacks {
|
||||
go cb()
|
||||
go cb(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ type SubscriptionManagerParams struct {
|
||||
TrackResolver types.MediaTrackResolver
|
||||
OnTrackSubscribed func(subTrack types.SubscribedTrack)
|
||||
OnTrackUnsubscribed func(subTrack types.SubscribedTrack)
|
||||
OnSubscriptionError func(trackID livekit.TrackID)
|
||||
OnSubscriptionError func(trackID livekit.TrackID, fatal bool, err error)
|
||||
Telemetry telemetry.TelemetryService
|
||||
|
||||
SubscriptionLimitVideo, SubscriptionLimitAudio int32
|
||||
@@ -309,6 +309,7 @@ func (m *SubscriptionManager) reconcileSubscription(s *trackSubscription) {
|
||||
s.logger.Infow("unsubscribing from track after notFoundTimeout", "error", err)
|
||||
s.setDesired(false)
|
||||
m.queueReconcile(s.trackID)
|
||||
m.params.OnSubscriptionError(s.trackID, false, err)
|
||||
}
|
||||
default:
|
||||
// all other errors
|
||||
@@ -317,7 +318,7 @@ func (m *SubscriptionManager) reconcileSubscription(s *trackSubscription) {
|
||||
"attempt", numAttempts,
|
||||
)
|
||||
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), err, false)
|
||||
m.params.OnSubscriptionError(s.trackID)
|
||||
m.params.OnSubscriptionError(s.trackID, true, err)
|
||||
} else {
|
||||
s.logger.Debugw("failed to subscribe, retrying",
|
||||
"error", err,
|
||||
@@ -353,7 +354,7 @@ func (m *SubscriptionManager) reconcileSubscription(s *trackSubscription) {
|
||||
if s.durationSinceStart() > subscriptionTimeout {
|
||||
s.logger.Errorw("track not bound after timeout", nil)
|
||||
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), ErrTrackNotBound, false)
|
||||
m.params.OnSubscriptionError(s.trackID)
|
||||
m.params.OnSubscriptionError(s.trackID, true, ErrTrackNotBound)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -470,7 +471,14 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error {
|
||||
subTrack.OnClose(func(willBeResumed bool) {
|
||||
m.handleSubscribedTrackClose(s, willBeResumed)
|
||||
})
|
||||
subTrack.AddOnBind(func() {
|
||||
subTrack.AddOnBind(func(err error) {
|
||||
if err != nil {
|
||||
s.logger.Infow("failed to bind track", "err", err)
|
||||
s.maybeRecordError(m.params.Telemetry, m.params.Participant.ID(), err, true)
|
||||
m.UnsubscribeFromTrack(s.trackID)
|
||||
m.params.OnSubscriptionError(s.trackID, false, err)
|
||||
return
|
||||
}
|
||||
s.setBound()
|
||||
s.maybeRecordSuccess(m.params.Telemetry, m.params.Participant.ID())
|
||||
})
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestSubscribe(t *testing.T) {
|
||||
sm.params.OnTrackSubscribed = func(subTrack types.SubscribedTrack) {
|
||||
subCount.Add(1)
|
||||
}
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID, fatal bool, err error) {
|
||||
failed.Store(true)
|
||||
}
|
||||
numParticipantSubscribed := atomic.Int32{}
|
||||
@@ -123,7 +123,7 @@ func TestSubscribe(t *testing.T) {
|
||||
resolver := newTestResolver(false, true, "pub", "pubID")
|
||||
sm.params.TrackResolver = resolver.Resolve
|
||||
failed := atomic.Bool{}
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID, fatal bool, err error) {
|
||||
failed.Store(true)
|
||||
}
|
||||
|
||||
@@ -164,7 +164,7 @@ func TestSubscribe(t *testing.T) {
|
||||
resolver := newTestResolver(true, true, "pub", "pubID")
|
||||
sm.params.TrackResolver = resolver.Resolve
|
||||
failed := atomic.Bool{}
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID, fatal bool, err error) {
|
||||
failed.Store(true)
|
||||
}
|
||||
|
||||
@@ -360,7 +360,7 @@ func TestSubscriptionLimits(t *testing.T) {
|
||||
sm.params.OnTrackSubscribed = func(subTrack types.SubscribedTrack) {
|
||||
subCount.Add(1)
|
||||
}
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID) {
|
||||
sm.params.OnSubscriptionError = func(trackID livekit.TrackID, fatal bool, err error) {
|
||||
failed.Store(true)
|
||||
}
|
||||
numParticipantSubscribed := atomic.Int32{}
|
||||
@@ -463,7 +463,7 @@ func newTestSubscriptionManagerWithParams(t *testing.T, params testSubscriptionP
|
||||
Logger: logger.GetLogger(),
|
||||
OnTrackSubscribed: func(subTrack types.SubscribedTrack) {},
|
||||
OnTrackUnsubscribed: func(subTrack types.SubscribedTrack) {},
|
||||
OnSubscriptionError: func(trackID livekit.TrackID) {},
|
||||
OnSubscriptionError: func(trackID livekit.TrackID, fatal bool, err error) {},
|
||||
TrackResolver: func(identity livekit.ParticipantIdentity, trackID livekit.TrackID) types.MediaResolverResult {
|
||||
return types.MediaResolverResult{}
|
||||
},
|
||||
@@ -526,7 +526,7 @@ func setTestSubscribedTrackBound(t *testing.T, st types.SubscribedTrack) {
|
||||
require.True(t, ok)
|
||||
|
||||
for i := 0; i < fst.AddOnBindCallCount(); i++ {
|
||||
fst.AddOnBindArgsForCall(i)()
|
||||
fst.AddOnBindArgsForCall(i)(nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1865,7 +1865,13 @@ func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription) e
|
||||
|
||||
func (t *PCTransport) handleRemoteAnswerReceived(sd *webrtc.SessionDescription) error {
|
||||
if err := t.setRemoteDescription(*sd); err != nil {
|
||||
return err
|
||||
// Pion will call RTPSender.Send method for each new added Downtrack, and return error if the DownTrack.Bind
|
||||
// returns error. In case of Downtrack.Bind returns ErrUnsupportedCodec, the signal state will be stable as negotiation is aleady compelted
|
||||
// before startRTPSenders, and the peerconnection state can be recovered by next negotiation which will be triggered
|
||||
// by the SubscriptionManager unsubscribe the failure DownTrack. So don't treat this error as negotiation failure.
|
||||
if !errors.Is(err, webrtc.ErrUnsupportedCodec) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
t.clearSignalStateCheckTimer()
|
||||
|
||||
@@ -427,7 +427,7 @@ type LocalMediaTrack interface {
|
||||
|
||||
//counterfeiter:generate . SubscribedTrack
|
||||
type SubscribedTrack interface {
|
||||
AddOnBind(f func())
|
||||
AddOnBind(f func(error))
|
||||
IsBound() bool
|
||||
Close(willBeResumed bool)
|
||||
OnClose(f func(willBeResumed bool))
|
||||
|
||||
@@ -11,10 +11,10 @@ import (
|
||||
)
|
||||
|
||||
type FakeSubscribedTrack struct {
|
||||
AddOnBindStub func(func())
|
||||
AddOnBindStub func(func(error))
|
||||
addOnBindMutex sync.RWMutex
|
||||
addOnBindArgsForCall []struct {
|
||||
arg1 func()
|
||||
arg1 func(error)
|
||||
}
|
||||
CloseStub func(bool)
|
||||
closeMutex sync.RWMutex
|
||||
@@ -174,10 +174,10 @@ type FakeSubscribedTrack struct {
|
||||
invocationsMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (fake *FakeSubscribedTrack) AddOnBind(arg1 func()) {
|
||||
func (fake *FakeSubscribedTrack) AddOnBind(arg1 func(error)) {
|
||||
fake.addOnBindMutex.Lock()
|
||||
fake.addOnBindArgsForCall = append(fake.addOnBindArgsForCall, struct {
|
||||
arg1 func()
|
||||
arg1 func(error)
|
||||
}{arg1})
|
||||
stub := fake.AddOnBindStub
|
||||
fake.recordInvocation("AddOnBind", []interface{}{arg1})
|
||||
@@ -193,13 +193,13 @@ func (fake *FakeSubscribedTrack) AddOnBindCallCount() int {
|
||||
return len(fake.addOnBindArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeSubscribedTrack) AddOnBindCalls(stub func(func())) {
|
||||
func (fake *FakeSubscribedTrack) AddOnBindCalls(stub func(func(error))) {
|
||||
fake.addOnBindMutex.Lock()
|
||||
defer fake.addOnBindMutex.Unlock()
|
||||
fake.AddOnBindStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeSubscribedTrack) AddOnBindArgsForCall(i int) func() {
|
||||
func (fake *FakeSubscribedTrack) AddOnBindArgsForCall(i int) func(error) {
|
||||
fake.addOnBindMutex.RLock()
|
||||
defer fake.addOnBindMutex.RUnlock()
|
||||
argsForCall := fake.addOnBindArgsForCall[i]
|
||||
|
||||
+10
-4
@@ -199,7 +199,7 @@ type DownTrack struct {
|
||||
writeStream webrtc.TrackLocalWriter
|
||||
rtcpReader *buffer.RTCPReader
|
||||
onCloseHandler func(willBeResumed bool)
|
||||
onBinding func()
|
||||
onBinding func(error)
|
||||
|
||||
listenerLock sync.RWMutex
|
||||
receiverReportListeners []ReceiverReportListener
|
||||
@@ -336,8 +336,14 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
}
|
||||
|
||||
if codec.MimeType == "" {
|
||||
err := webrtc.ErrUnsupportedCodec
|
||||
onBinding := d.onBinding
|
||||
d.bindLock.Unlock()
|
||||
return webrtc.RTPCodecParameters{}, webrtc.ErrUnsupportedCodec
|
||||
d.logger.Infow("bind error for unsupported codec", "codecs", d.upstreamCodecs, "remoteParameters", t.CodecParameters())
|
||||
if onBinding != nil {
|
||||
onBinding(err)
|
||||
}
|
||||
return webrtc.RTPCodecParameters{}, err
|
||||
}
|
||||
|
||||
// if a downtrack is closed before bind, it already unsubscribed from client, don't do subsequent operation and return here.
|
||||
@@ -367,7 +373,7 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
|
||||
d.codec = codec.RTPCodecCapability
|
||||
if d.onBinding != nil {
|
||||
d.onBinding()
|
||||
d.onBinding(nil)
|
||||
}
|
||||
d.bound.Store(true)
|
||||
d.bindLock.Unlock()
|
||||
@@ -990,7 +996,7 @@ func (d *DownTrack) OnCloseHandler(fn func(willBeResumed bool)) {
|
||||
d.onCloseHandler = fn
|
||||
}
|
||||
|
||||
func (d *DownTrack) OnBinding(fn func()) {
|
||||
func (d *DownTrack) OnBinding(fn func(error)) {
|
||||
d.onBinding = fn
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user