From 2347363a182e616410b0bffe0a904b505fc5bcbc Mon Sep 17 00:00:00 2001 From: liquanqing Date: Fri, 5 Jun 2020 07:59:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8gop=5Fcache=E4=B8=AD=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E5=AF=B9=E7=8E=AF=E5=BD=A2=E9=98=9F=E5=88=97=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E8=BF=9B=E8=A1=8C=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/logic/gop_cache.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/pkg/logic/gop_cache.go b/pkg/logic/gop_cache.go index 3bbdbfc..404622a 100644 --- a/pkg/logic/gop_cache.go +++ b/pkg/logic/gop_cache.go @@ -99,20 +99,32 @@ func (gc *GOPCache) Feed(msg rtmp.AVMsg, lg LazyGet) { if gc.gopSize > 1 { if msg.IsVideoKeyNalu() { - gc.gopRing[gc.gopRingLast].Clear() - gc.gopRing[gc.gopRingLast].Feed(msg, lg()) - gc.gopRingLast = (gc.gopRingLast + 1) % gc.gopSize - if gc.gopRingLast == gc.gopRingFirst { - gc.gopRingFirst = (gc.gopRingFirst + 1) % gc.gopSize - } + gc.feedNewGop(msg, lg()) } else { - if gc.gopRingLast != gc.gopRingFirst { - gc.gopRing[(gc.gopRingLast - 1 + gc.gopSize) % gc.gopSize].Feed(msg, lg()) - } + gc.feedLastGop(msg, lg()) } } } +func (gc *GOPCache) feedLastGop(msg rtmp.AVMsg, b []byte) { + if gc.gopRingLast != gc.gopRingFirst { + gc.gopRing[(gc.gopRingLast - 1 + gc.gopSize) % gc.gopSize].Feed(msg, b) + } +} + +func (gc *GOPCache) feedNewGop(msg rtmp.AVMsg, b []byte) { + gc.gopRing[gc.gopRingLast].Clear() + gc.gopRing[gc.gopRingLast].Feed(msg, b) + gc.gopRingLast = (gc.gopRingLast + 1) % gc.gopSize + if gc.gopRingLast == gc.gopRingFirst { + gc.gopRingFirst = (gc.gopRingFirst + 1) % gc.gopSize + } +} + +func (gc *GOPCache) isGopEmpty() bool { + return gc.gopRingFirst == gc.gopRingLast +} + func (gc *GOPCache) GetGopLen() int{ return (gc.gopRingLast + gc.gopSize - gc.gopRingFirst) % gc.gopSize } @@ -124,13 +136,6 @@ func (gc *GOPCache) GetGopDataAt(pos int) [][]byte { return gc.gopRing[(pos + gc.gopRingFirst) % gc.gopSize].data } -func (gc *GOPCache) LastGOP() *GOP { - if gc.GetGopLen() == 0 { - return nil - } - return &gc.gopRing[(gc.gopRingLast - 1 + gc.gopSize) % gc.gopSize] -} - func (gc *GOPCache) Clear() { gc.Metadata = nil gc.VideoSeqHeader = nil