Skip to content

Commit

Permalink
Synchronizer RemoveTrack (#302)
Browse files Browse the repository at this point in the history
* remove track

* update RemoveTrack
  • Loading branch information
frostbyte73 authored Aug 25, 2023
1 parent 747aa85 commit fcf5bdf
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ type Synchronizer struct {

psByIdentity map[string]*participantSynchronizer
psBySSRC map[uint32]*participantSynchronizer
ssrcByID map[string]uint32
}

func NewSynchronizer(onStarted func()) *Synchronizer {
return &Synchronizer{
onStarted: onStarted,
psByIdentity: make(map[string]*participantSynchronizer),
psBySSRC: make(map[uint32]*participantSynchronizer),
ssrcByID: make(map[string]uint32),
}
}

Expand All @@ -54,6 +56,7 @@ func (s *Synchronizer) AddTrack(track TrackRemote, identity string) *TrackSynchr
s.psByIdentity[identity] = p
}
ssrc := uint32(track.SSRC())
s.ssrcByID[track.ID()] = ssrc
s.psBySSRC[ssrc] = p
s.Unlock()

Expand All @@ -64,6 +67,26 @@ func (s *Synchronizer) AddTrack(track TrackRemote, identity string) *TrackSynchr
return t
}

func (s *Synchronizer) RemoveTrack(trackID string) {
s.Lock()
ssrc := s.ssrcByID[trackID]
p := s.psBySSRC[ssrc]
delete(s.ssrcByID, trackID)
delete(s.psBySSRC, ssrc)
s.Unlock()
if p == nil {
return
}

p.Lock()
if ts := p.tracks[ssrc]; ts != nil {
ts.sync = nil
}
delete(p.tracks, ssrc)
delete(p.senderReports, ssrc)
p.Unlock()
}

func (s *Synchronizer) GetStartedAt() int64 {
s.RLock()
defer s.RUnlock()
Expand Down Expand Up @@ -94,7 +117,7 @@ func (s *Synchronizer) OnRTCP(packet rtcp.Packet) {
endedAt := s.endedAt
s.Unlock()

if endedAt != 0 {
if endedAt != 0 || p == nil {
return
}

Expand Down

0 comments on commit fcf5bdf

Please sign in to comment.