mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-28 10:32:46 +08:00
解决扩缩ringbuffer的bug
This commit is contained in:
@@ -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 {
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +136,7 @@ func (s *Subscriber) PlayAudio(at *AudioTrack) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
send = func() {
|
send = func() {
|
||||||
if s.OnAudio(ap.Copy(startTimestamp)); at.lastTs -ap.Timestamp > 1000 {
|
if s.OnAudio(ap.Copy(startTimestamp)); at.lastTs-ap.Timestamp > 1000 {
|
||||||
action = drop
|
action = drop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +180,7 @@ func (s *Subscriber) PlayVideo(vt *VideoTrack) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
send = func() {
|
send = func() {
|
||||||
if s.OnVideo(vp.Copy(startTimestamp)); vt.lastTs - vp.Timestamp > 1000 {
|
if s.OnVideo(vp.Copy(startTimestamp)); vt.lastTs-vp.Timestamp > 1000 {
|
||||||
action = drop
|
action = drop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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--
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user