Skip to content

Commit

Permalink
fix(sfu): Unbind downtrack locking
Browse files Browse the repository at this point in the history
  • Loading branch information
OrlandoCo committed Jun 9, 2021
1 parent 557f902 commit 7ecf8fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pkg/sfu/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type WebRTCReceiver struct {
trackID string
streamID string
kind webrtc.RTPCodecType
closed atomicBool
bandwidth uint64
lastPli int64
stream string
Expand Down Expand Up @@ -100,8 +101,11 @@ func (w *WebRTCReceiver) Kind() webrtc.RTPCodecType {
}

func (w *WebRTCReceiver) AddUpTrack(track *webrtc.TrackRemote, buff *buffer.Buffer, bestQualityFirst bool) {
var layer int
if w.closed.get() {
return
}

var layer int
switch track.RID() {
case fullResolution:
layer = 2
Expand Down Expand Up @@ -167,6 +171,10 @@ func (w *WebRTCReceiver) AddUpTrack(track *webrtc.TrackRemote, buff *buffer.Buff
}

func (w *WebRTCReceiver) AddDownTrack(track *DownTrack, bestQualityFirst bool) {
if w.closed.get() {
return
}

layer := 0
if w.isSimulcast {
w.Lock()
Expand Down Expand Up @@ -205,6 +213,10 @@ func (w *WebRTCReceiver) AddDownTrack(track *DownTrack, bestQualityFirst bool) {
}

func (w *WebRTCReceiver) SwitchDownTrack(track *DownTrack, layer int) error {
if w.closed.get() {
return errNoReceiverFound
}

if buf := w.buffers[layer]; buf != nil {
w.locks[layer].Lock()
w.pendingTracks[layer] = append(w.pendingTracks[layer], track)
Expand Down Expand Up @@ -241,6 +253,10 @@ func (w *WebRTCReceiver) OnCloseHandler(fn func()) {

// DeleteDownTrack removes a DownTrack from a Receiver
func (w *WebRTCReceiver) DeleteDownTrack(layer int, id string) {
if w.closed.get() {
return
}

w.locks[layer].Lock()
idx := -1
for i, dt := range w.downTracks[layer] {
Expand Down Expand Up @@ -329,7 +345,8 @@ func (w *WebRTCReceiver) RetransmitPackets(track *DownTrack, packets []packetMet
func (w *WebRTCReceiver) writeRTP(layer int) {
defer func() {
w.closeOnce.Do(func() {
go w.closeTracks()
w.closed.set(true)
w.closeTracks()
})
}()

Expand Down
1 change: 1 addition & 0 deletions pkg/sfu/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (s *Subscriber) RemoveDownTrack(streamID string, downTrack *DownTrack) {
for i, dt := range dts {
if dt == downTrack {
idx = i
break
}
}
if idx >= 0 {
Expand Down

0 comments on commit 7ecf8fb

Please sign in to comment.