Skip to content

Commit

Permalink
Do not attempt to send an offer if the PCTransport was closed (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
biglittlebigben authored Nov 15, 2024
1 parent ffee242 commit a3cf5f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,23 @@ 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 {
e.log.Debugw("successfully set publisher answer")
}
}
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,
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
11 changes: 11 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type PCTransport struct {
pendingRestartIceOffer *webrtc.SessionDescription
restartAfterGathering bool
nackGenerator *sdkinterceptor.NackGeneratorInterceptorFactory
closed bool
rttFromXR atomic.Bool

onRemoteDescriptionSettled func() error
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a3cf5f4

Please sign in to comment.