Skip to content

Commit

Permalink
webrtc: improve stability by reordering incoming packets (#2570)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Oct 26, 2023
1 parent 20a1612 commit 38f228c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
4 changes: 3 additions & 1 deletion internal/webrtc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func NewAPI(conf APIConf) (*webrtc.API, error) {
}

interceptorRegistry := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(mediaEngine, interceptorRegistry); err != nil {

err := webrtc.RegisterDefaultInterceptors(mediaEngine, interceptorRegistry)
if err != nil {
return nil, err
}

Expand Down
34 changes: 27 additions & 7 deletions internal/webrtc/incoming_track.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/liberrors"
"github.com/bluenviron/gortsplib/v4/pkg/rtplossdetector"
"github.com/bluenviron/gortsplib/v4/pkg/rtpreorderer"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
Expand All @@ -24,8 +24,9 @@ type IncomingTrack struct {
track *webrtc.TrackRemote
log logger.Writer

format format.Format
lossDetector *rtplossdetector.LossDetector
format format.Format
reorderer *rtpreorderer.Reorderer
pkts []*rtp.Packet
}

func newIncomingTrack(
Expand All @@ -35,9 +36,9 @@ func newIncomingTrack(
log logger.Writer,
) (*IncomingTrack, error) {
t := &IncomingTrack{
track: track,
log: log,
lossDetector: rtplossdetector.New(),
track: track,
log: log,
reorderer: rtpreorderer.New(),
}

isVideo := false
Expand Down Expand Up @@ -131,17 +132,36 @@ func (t *IncomingTrack) Format() format.Format {
// ReadRTP reads a RTP packet.
func (t *IncomingTrack) ReadRTP() (*rtp.Packet, error) {
for {
if len(t.pkts) != 0 {
var pkt *rtp.Packet
pkt, t.pkts = t.pkts[0], t.pkts[1:]

// sometimes Chrome sends empty RTP packets. ignore them.
if len(pkt.Payload) == 0 {
continue
}

return pkt, nil
}

pkt, _, err := t.track.ReadRTP()
if err != nil {
return nil, err
}

lost := t.lossDetector.Process(pkt)
var lost int
t.pkts, lost = t.reorderer.Process(pkt)
if lost != 0 {
t.log.Log(logger.Warn, (liberrors.ErrClientRTPPacketsLost{Lost: lost}).Error())
// do not return
}

if len(t.pkts) == 0 {
continue
}

pkt, t.pkts = t.pkts[0], t.pkts[1:]

// sometimes Chrome sends empty RTP packets. ignore them.
if len(pkt.Payload) == 0 {
continue
Expand Down

0 comments on commit 38f228c

Please sign in to comment.