diff --git a/engine.go b/engine.go index 5d899cd..ee6acae 100644 --- a/engine.go +++ b/engine.go @@ -368,6 +368,11 @@ func (e *RTCEngine) configure( // configure client e.client.OnAnswer = func(sd webrtc.SessionDescription) { + if e.closed.Load() { + e.log.Debugw("ignoring SDP answer after closed") + return + } + if err := e.publisher.SetRemoteDescription(sd); err != nil { e.log.Errorw("could not set remote description", err) } else { @@ -375,6 +380,11 @@ func (e *RTCEngine) configure( } } e.client.OnTrickle = func(init webrtc.ICECandidateInit, target livekit.SignalTarget) { + if e.closed.Load() { + e.log.Debugw("ignoring trickle after closed") + return + } + var err error e.log.Debugw("remote ICE candidate", "target", target, @@ -390,6 +400,11 @@ func (e *RTCEngine) configure( } } e.client.OnOffer = func(sd webrtc.SessionDescription) { + if e.closed.Load() { + e.log.Debugw("ignoring SDP offer after closed") + return + } + e.log.Debugw("received offer for subscriber") if err := e.subscriber.SetRemoteDescription(sd); err != nil { e.log.Errorw("could not set remote description", err) diff --git a/errors.go b/errors.go index 1315a00..3151d2a 100644 --- a/errors.go +++ b/errors.go @@ -29,4 +29,5 @@ var ( ErrCannotConnectSignal = errors.New("could not establish signal connection") ErrCannotDialSignal = errors.New("could not dial signal connection") ErrNoPeerConnection = errors.New("peer connection not established") + ErrAborted = errors.New("operation was aborted") ) diff --git a/transport.go b/transport.go index 15a9967..16bbe3c 100644 --- a/transport.go +++ b/transport.go @@ -59,6 +59,7 @@ type PCTransport struct { pendingRestartIceOffer *webrtc.SessionDescription restartAfterGathering bool nackGenerator *sdkinterceptor.NackGeneratorInterceptorFactory + closed bool rttFromXR atomic.Bool onRemoteDescriptionSettled func() error @@ -257,6 +258,11 @@ func (t *PCTransport) IsConnected() bool { } func (t *PCTransport) Close() error { + t.lock.Lock() + defer t.lock.Unlock() + + t.closed = true + return t.pc.Close() } @@ -381,6 +387,11 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error { t.lock.Lock() defer t.lock.Unlock() + if t.closed { + t.log.Debugw("aborting Offer since transport is closed") + return ErrAborted + } + iceRestart := options != nil && options.ICERestart if iceRestart { if t.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {