修改在feedNewGOP时先判断是否队列是否满,在添加数据

pull/8/head
lqq 5 years ago committed by yoko
parent 69958df6b6
commit 7e804f5f9e

@ -106,12 +106,12 @@ func (gc *GOPCache) Feed(msg rtmp.AVMsg, lg LazyGet) {
}
}
func (gc *GOPCache) GetGOPLen() int {
func (gc *GOPCache) GetGOPCount() int {
return (gc.gopRingLast + gc.gopSize - gc.gopRingFirst) % gc.gopSize
}
func (gc *GOPCache) GetGOPDataAt(pos int) [][]byte {
if pos >= gc.GetGOPLen() || pos < 0 {
if pos >= gc.GetGOPCount() || pos < 0 {
return nil
}
return gc.gopRing[(pos+gc.gopRingFirst)%gc.gopSize].data
@ -132,12 +132,12 @@ func (gc *GOPCache) feedLastGOP(msg rtmp.AVMsg, b []byte) {
}
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.isGOPRingFull() {
gc.gopRingFirst = (gc.gopRingFirst + 1) % gc.gopSize
}
gc.gopRing[gc.gopRingLast].Clear()
gc.gopRing[gc.gopRingLast].Feed(msg, b)
gc.gopRingLast = (gc.gopRingLast + 1) % gc.gopSize
}
func (gc *GOPCache) isGOPRingFull() bool {

@ -50,61 +50,61 @@ func TestGOPCache_Feed(t *testing.T) {
p4f := func() []byte { return []byte{0, 4} }
nc := NewGOPCache("rtmp", "test", 3)
assert.Equal(t, 0, nc.GetGopLen())
assert.Equal(t, nil, nc.GetGopDataAt(0))
assert.Equal(t, nil, nc.GetGopDataAt(1))
assert.Equal(t, nil, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 0, nc.GetGOPCount())
assert.Equal(t, nil, nc.GetGOPDataAt(0))
assert.Equal(t, nil, nc.GetGOPDataAt(1))
assert.Equal(t, nil, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(i1, i1f)
assert.Equal(t, 1, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 1}}, nc.GetGopDataAt(0))
assert.Equal(t, nil, nc.GetGopDataAt(1))
assert.Equal(t, nil, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 1, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 1}}, nc.GetGOPDataAt(0))
assert.Equal(t, nil, nc.GetGOPDataAt(1))
assert.Equal(t, nil, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(p1, p1f)
assert.Equal(t, 1, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGopDataAt(0))
assert.Equal(t, nil, nc.GetGopDataAt(1))
assert.Equal(t, nil, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 1, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGOPDataAt(0))
assert.Equal(t, nil, nc.GetGOPDataAt(1))
assert.Equal(t, nil, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(i2, i2f)
assert.Equal(t, 2, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGopDataAt(0))
assert.Equal(t, [][]byte{{1, 2}}, nc.GetGopDataAt(1))
assert.Equal(t, nil, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 2, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGOPDataAt(0))
assert.Equal(t, [][]byte{{1, 2}}, nc.GetGOPDataAt(1))
assert.Equal(t, nil, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(p2, p2f)
assert.Equal(t, 2, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGopDataAt(0))
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGopDataAt(1))
assert.Equal(t, nil, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 2, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGOPDataAt(0))
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGOPDataAt(1))
assert.Equal(t, nil, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(i3, i3f)
assert.Equal(t, 3, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGopDataAt(0))
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGopDataAt(1))
assert.Equal(t, [][]byte{{1, 3}}, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 3, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGOPDataAt(0))
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGOPDataAt(1))
assert.Equal(t, [][]byte{{1, 3}}, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(p3, p3f)
assert.Equal(t, 3, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGopDataAt(0))
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGopDataAt(1))
assert.Equal(t, [][]byte{{1, 3}, {0, 3}}, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 3, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 1}, {0, 1}}, nc.GetGOPDataAt(0))
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGOPDataAt(1))
assert.Equal(t, [][]byte{{1, 3}, {0, 3}}, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(i4, i4f)
assert.Equal(t, 3, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGopDataAt(0))
assert.Equal(t, [][]byte{{1, 3}, {0, 3}}, nc.GetGopDataAt(1))
assert.Equal(t, [][]byte{{1, 4}}, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 3, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGOPDataAt(0))
assert.Equal(t, [][]byte{{1, 3}, {0, 3}}, nc.GetGOPDataAt(1))
assert.Equal(t, [][]byte{{1, 4}}, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
nc.Feed(p4, p4f)
assert.Equal(t, 3, nc.GetGopLen())
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGopDataAt(0))
assert.Equal(t, [][]byte{{1, 3}, {0, 3}}, nc.GetGopDataAt(1))
assert.Equal(t, [][]byte{{1, 4}, {0, 4}}, nc.GetGopDataAt(2))
assert.Equal(t, nil, nc.GetGopDataAt(3))
assert.Equal(t, 3, nc.GetGOPCount())
assert.Equal(t, [][]byte{{1, 2}, {0, 2}}, nc.GetGOPDataAt(0))
assert.Equal(t, [][]byte{{1, 3}, {0, 3}}, nc.GetGOPDataAt(1))
assert.Equal(t, [][]byte{{1, 4}, {0, 4}}, nc.GetGOPDataAt(2))
assert.Equal(t, nil, nc.GetGOPDataAt(3))
}

@ -193,8 +193,8 @@ func (group *Group) broadcastRTMP(msg rtmp.AVMsg) {
if group.gopCache.AACSeqHeader != nil {
_ = session.AsyncWrite(group.gopCache.AACSeqHeader)
}
for i := 0; i < group.gopCache.GetGopLen(); i++ {
for _, item := range group.gopCache.GetGopDataAt(i) {
for i := 0; i < group.gopCache.GetGOPCount(); i++ {
for _, item := range group.gopCache.GetGOPDataAt(i) {
_ = session.AsyncWrite(item)
}
}
@ -218,8 +218,8 @@ func (group *Group) broadcastRTMP(msg rtmp.AVMsg) {
if group.httpflvGopCache.AACSeqHeader != nil {
session.WriteRawPacket(group.httpflvGopCache.AACSeqHeader)
}
for i := 0; i < group.httpflvGopCache.GetGopLen(); i++ {
for _, item := range group.httpflvGopCache.GetGopDataAt(i) {
for i := 0; i < group.httpflvGopCache.GetGOPCount(); i++ {
for _, item := range group.httpflvGopCache.GetGOPDataAt(i) {
session.WriteRawPacket(item)
}
}

Loading…
Cancel
Save