解决扩缩ringbuffer的bug

This commit is contained in:
李宇翔
2021-07-09 17:47:07 +08:00
parent a8d528b14a
commit 84fef0761a
3 changed files with 22 additions and 28 deletions

View File

@@ -63,7 +63,6 @@ type Stream struct {
subscribeMutex sync.Mutex subscribeMutex sync.Mutex
timeout *time.Timer //更新时间用来做超时处理 timeout *time.Timer //更新时间用来做超时处理
Close func() `json:"-"` Close func() `json:"-"`
prePayload uint32 //需要预拼装ByteStream格式的数据的订阅者数量
} }
func (r *Stream) Update() { func (r *Stream) Update() {
@@ -134,9 +133,6 @@ func (r *Stream) Subscribe(s *Subscriber) {
utils.Print(Sprintf(Yellow("subscribe :%s %s,to Stream %s"), Blue(s.Type), Cyan(s.ID), BrightCyan(r.StreamPath))) utils.Print(Sprintf(Yellow("subscribe :%s %s,to Stream %s"), Blue(s.Type), Cyan(s.ID), BrightCyan(r.StreamPath)))
s.Context, s.cancel = context.WithCancel(r) s.Context, s.cancel = context.WithCancel(r)
r.subscribeMutex.Lock() r.subscribeMutex.Lock()
if s.ByteStreamFormat {
r.prePayload++
}
r.Subscribers = append(r.Subscribers, s) r.Subscribers = append(r.Subscribers, s)
r.subscribeMutex.Unlock() r.subscribeMutex.Unlock()
utils.Print(Sprintf(Yellow("%s subscriber %s added remains:%d"), BrightCyan(r.StreamPath), Cyan(s.ID), Blue(len(r.Subscribers)))) utils.Print(Sprintf(Yellow("%s subscriber %s added remains:%d"), BrightCyan(r.StreamPath), Cyan(s.ID), Blue(len(r.Subscribers))))
@@ -149,9 +145,6 @@ func (r *Stream) UnSubscribe(s *Subscriber) {
if r.Err() == nil { if r.Err() == nil {
var deleted bool var deleted bool
r.subscribeMutex.Lock() r.subscribeMutex.Lock()
if s.ByteStreamFormat {
r.prePayload--
}
r.Subscribers, deleted = DeleteSliceItem_Subscriber(r.Subscribers, s) r.Subscribers, deleted = DeleteSliceItem_Subscriber(r.Subscribers, s)
r.subscribeMutex.Unlock() r.subscribeMutex.Unlock()
if deleted { if deleted {

View File

@@ -25,7 +25,6 @@ type Subscriber struct {
SubscribeArgs url.Values SubscribeArgs url.Values
OnAudio func(pack AudioPack) `json:"-"` OnAudio func(pack AudioPack) `json:"-"`
OnVideo func(pack VideoPack) `json:"-"` OnVideo func(pack VideoPack) `json:"-"`
ByteStreamFormat bool
closeOnce sync.Once closeOnce sync.Once
} }

View File

@@ -47,13 +47,6 @@ type VideoTrack struct {
idrCount int //处于缓冲中的关键帧数量 idrCount int //处于缓冲中的关键帧数量
} }
func (vt *VideoTrack) initVideoRing(v interface{}) {
pack := new(VideoPack)
if vt.writeByteStream != nil {
pack.Buffer = bytes.NewBuffer([]byte{})
}
v.(*RingItem).Value = pack
}
func (s *Stream) NewVideoTrack(codec byte) (vt *VideoTrack) { func (s *Stream) NewVideoTrack(codec byte) (vt *VideoTrack) {
var cancel context.CancelFunc var cancel context.CancelFunc
vt = &VideoTrack{ vt = &VideoTrack{
@@ -66,10 +59,14 @@ func (s *Stream) NewVideoTrack(codec byte) (vt *VideoTrack) {
vt.revIDR = func() { vt.revIDR = func() {
vt.idrCount++ vt.idrCount++
current := vt.current() current := vt.current()
l := vt.Ring.Len() vt.GOP = current.Sequence - idrSequence
if vt.GOP = current.Sequence - idrSequence; vt.GOP < l-5 { if l := vt.Ring.Len() - vt.GOP - 5; l > 5 {
vt.Unlink(l - vt.GOP - 5) //缩小缓冲环节省内存 //缩小缓冲环节省内存
//utils.Printf("%s gop:%d ring atrophy to %d", s.StreamPath, vt.GOP, l) vt.Unlink(l).Do(func(v interface{}) {
if v.(*RingItem).Value.(*VideoPack).IDR {
vt.idrCount--
}
})
} }
vt.IDRing = vt.Ring vt.IDRing = vt.Ring
idrSequence = current.Sequence idrSequence = current.Sequence
@@ -83,7 +80,9 @@ func (s *Stream) NewVideoTrack(codec byte) (vt *VideoTrack) {
vt.Stream = s vt.Stream = s
vt.CodecID = codec vt.CodecID = codec
vt.Init(256) vt.Init(256)
vt.Do(vt.initVideoRing) vt.Do(func(v interface{}) {
v.(*RingItem).Value = new(VideoPack)
})
vt.WaitIDR, cancel = context.WithCancel(context.Background()) vt.WaitIDR, cancel = context.WithCancel(context.Background())
switch codec { switch codec {
case 7: case 7:
@@ -525,14 +524,17 @@ func (vt *VideoTrack) push(pack *VideoPack) {
vt.revIDR() vt.revIDR()
} }
vt.lastTs = pack.Timestamp vt.lastTs = pack.Timestamp
nextPack := vt.NextValue().(*VideoPack) if nextPack := vt.NextValue().(*VideoPack); nextPack.IDR {
if nextPack.IDR {
if vt.idrCount == 1 { if vt.idrCount == 1 {
exRing := NewRingBuffer(5).Ring exRing := ring.New(5)
exRing.Do(vt.initVideoRing) for x := exRing; x.Value == nil; x = x.Next() {
pack := new(VideoPack)
x.Value = &RingItem{Value: pack}
if vt.writeByteStream != nil {
pack.Buffer = bytes.NewBuffer([]byte{})
}
}
vt.Link(exRing) // 扩大缓冲环 vt.Link(exRing) // 扩大缓冲环
//l := vt.Ring.Len()
//utils.Printf("%s ring grow to %d", vt.Stream.StreamPath, l)
} else { } else {
vt.idrCount-- vt.idrCount--
} }