From 564dcd41676772923fac7abdfc801a30b6e22eca Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 25 Jan 2022 23:11:03 +0100 Subject: [PATCH] Move keyframe handling back to the packet level. Doing that at the sample level suffers from too much delay due to buffering in the samplebuilder. --- diskwriter/diskwriter.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/diskwriter/diskwriter.go b/diskwriter/diskwriter.go index 1ea5c37..68a2208 100644 --- a/diskwriter/diskwriter.go +++ b/diskwriter/diskwriter.go @@ -414,7 +414,7 @@ func (t *diskTrack) Write(buf []byte) (int, error) { count := p.SequenceNumber - lastSeqno if count < 256 { for i := uint16(1); i < count; i++ { - recover(t, lastSeqno + i) + recover(t, lastSeqno+i) } } else { requestKeyframe(t) @@ -470,6 +470,9 @@ func (t *diskTrack) writeRTP(p *rtp.Packet) error { kf, _ := gcodecs.Keyframe(codec, p) if kf { t.savedKf = p + t.lastKf = time.Now() + } else if time.Since(t.lastKf) > 4*time.Second { + requestKeyframe(t) } } @@ -518,7 +521,6 @@ func (t *diskTrack) writeBuffered(force bool) error { } } } else { - keyframe = true if t.writer == nil { if !t.conn.hasVideo { err := t.conn.initWriter(0, 0) @@ -533,14 +535,6 @@ func (t *diskTrack) writeBuffered(force bool) error { } } - now := time.Now() - if keyframe { - t.lastKf = now - } else if t.writer == nil || now.Sub(t.lastKf) > 4*time.Second { - requestKeyframe(t) - return nil - } - if t.writer == nil { continue }