Skip to content

Commit

Permalink
Fix tracking of receiving tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Nov 26, 2024
1 parent 987efe2 commit 88381ec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
13 changes: 4 additions & 9 deletions service/rtc/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (c *call) addSession(cfg SessionConfig, rtcConn *webrtc.PeerConnection, clo
screenRateMonitors: make(map[string]*RateMonitor),
log: log,
call: c,
rxTracks: make(map[string]webrtc.TrackLocal),
}

c.sessions[cfg.SessionID] = s
Expand Down Expand Up @@ -128,15 +129,6 @@ func (c *call) handleSessionClose(us *session) {
defer us.mut.Unlock()

cleanUp := func(sessionID string, sender *webrtc.RTPSender, track webrtc.TrackLocal) {
if isValidTrackID(track.ID()) {
c.metrics.DecRTPTracks(us.cfg.GroupID, "out", getTrackType(track.Kind()))
} else {
us.log.Warn("invalid track ID",
mlog.String("sessionID", sessionID),
mlog.String("trackID", track.ID()),
mlog.Any("trackKind", track.Kind()))
}

if err := sender.ReplaceTrack(nil); err != nil {
us.log.Error("failed to replace track on sender",
mlog.String("sessionID", sessionID),
Expand Down Expand Up @@ -175,6 +167,9 @@ func (c *call) handleSessionClose(us *session) {
cleanUp(us.cfg.SessionID, sender, track)
}
}
for _, track := range us.rxTracks {
c.metrics.DecRTPTracks(us.cfg.GroupID, "out", getTrackType(track.Kind()))
}

// We check whether the closing session was also sending any track
// (e.g. voice, screen).
Expand Down
11 changes: 7 additions & 4 deletions service/rtc/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type session struct {
// Receiver
bwEstimator cc.BandwidthEstimator
screenTrackSender *webrtc.RTPSender
rxTracks map[string]webrtc.TrackLocal

closeCh chan struct{}
closeCb func() error
Expand Down Expand Up @@ -426,6 +427,7 @@ func (s *session) addTrack(sdpOutCh chan<- Message, track webrtc.TrackLocal) (er
mlog.String("trackID", track.ID()))
} else {
s.call.metrics.DecRTPTracks(s.cfg.GroupID, "out", getTrackType(track.Kind()))
delete(s.rxTracks, track.ID())
}
s.mut.Unlock()
}()
Expand All @@ -444,11 +446,13 @@ func (s *session) addTrack(sdpOutCh chan<- Message, track webrtc.TrackLocal) (er
if err := s.rtcConn.SetRemoteDescription(answer); err != nil {
return fmt.Errorf("failed to set remote description for track %s: %w", track.ID(), err)
}

s.mut.Lock()
if track.Kind() == webrtc.RTPCodecTypeVideo {
s.mut.Lock()
s.screenTrackSender = sender
s.mut.Unlock()
}
s.rxTracks[track.ID()] = track
s.mut.Unlock()
case <-time.After(signalingTimeout):
return fmt.Errorf("timed out signaling")
case <-s.closeCh:
Expand Down Expand Up @@ -488,7 +492,7 @@ func (s *session) removeTrack(sdpOutCh chan<- Message, track webrtc.TrackLocal)
return fmt.Errorf("failed to remove track: %w", err)
}
s.call.metrics.DecRTPTracks(s.cfg.GroupID, "out", getTrackType(track.Kind()))

delete(s.rxTracks, track.ID())
if s.screenTrackSender == sender {
s.screenTrackSender = nil
}
Expand Down Expand Up @@ -576,7 +580,6 @@ func (s *session) InitVAD(log mlog.LoggerIFace, msgCh chan<- Message) error {
log.Error("failed to send VAD message: channel is full")
}
})

if err != nil {
return fmt.Errorf("failed to create vad monitor: %w", err)
}
Expand Down

0 comments on commit 88381ec

Please sign in to comment.