修复AAC订阅读取过迟问题

This commit is contained in:
dexter
2023-01-18 23:25:40 +08:00
parent b0dcecdebc
commit 5c5747e669
6 changed files with 18 additions and 15 deletions

View File

@@ -142,6 +142,8 @@ func (av *AVFrame[T]) Reset() {
av.Raw = av.Raw[:0]
}
av.BytesIn = 0
av.AbsTime = 0
av.DeltaTime = 0
}
func (avcc AVCCFrame) IsIDR() bool {

View File

@@ -14,9 +14,9 @@ type AVRing[T RawSlice] struct {
func (r *AVRing[T]) Step() *AVFrame[T] {
current := r.RingBuffer.MoveNext()
current.Sequence = r.MoveCount
current.canRead = false
current.Reset()
current.Sequence = r.MoveCount
r.LastValue.canRead = true
return current
}

View File

@@ -312,9 +312,9 @@ func (s *Subscriber) PlayBlock(subType byte) {
break
}
}
if vstate < SUBSTATE_NORMAL {
continue
}
// if vstate < SUBSTATE_NORMAL {
// continue
// }
}
// 正常模式下或者纯音频模式下,音频开始播放
if hasAudio {

View File

@@ -93,9 +93,7 @@ func (av *Audio) WriteRaw(pts uint32, raw []byte) {
}
func (av *Audio) WriteAVCC(ts uint32, frame AVCCFrame) {
curValue := &av.Value
curValue.BytesIn += len(frame)
curValue.AppendAVCC(frame)
av.Media.WriteAVCC(ts, frame)
av.generateTimestamp(ts * 90)
av.Flush()
}

View File

@@ -130,29 +130,30 @@ func (av *Media[T]) generateTimestamp(ts uint32) {
func (av *Media[T]) WriteAVCC(ts uint32, frame AVCCFrame) {
curValue := &av.Value
cts := frame.CTS()
curValue.AbsTime = ts
curValue.BytesIn += len(frame)
curValue.AppendAVCC(frame)
curValue.DTS = ts * 90
curValue.PTS = (ts + cts) * 90
// av.Stream.Tracef("WriteAVCC:ts %d,cts %d,len %d", ts, cts, len(frame))
}
func (av *Media[T]) Flush() {
curValue, preValue := &av.Value, av.LastValue
// 补完RTP
if config.Global.EnableRTP && len(curValue.RTP) == 0 {
av.CompleteRTP(curValue)
}
// 补完AVCC
if config.Global.EnableAVCC && len(curValue.AVCC) == 0 {
av.CompleteAVCC(curValue)
}
if av.起始时间.IsZero() {
curValue.AbsTime = curValue.DTS / 90
curValue.DeltaTime = 0
av.重置(curValue.AbsTime)
} else {
} else if curValue.AbsTime == 0 {
curValue.DeltaTime = (curValue.DTS - preValue.DTS) / 90
// println(curValue.DeltaTime ,curValue.DTS , preValue.DTS)
curValue.AbsTime = preValue.AbsTime + curValue.DeltaTime
} else {
curValue.DeltaTime = curValue.AbsTime - preValue.AbsTime
}
av.Base.Flush(&curValue.BaseFrame)
if av.等待上限 > 0 {

View File

@@ -132,6 +132,8 @@ func (vt *Video) WriteAnnexB(pts uint32, dts uint32, frame AnnexBFrame) {
func (vt *Video) WriteAVCC(ts uint32, frame AVCCFrame) {
vt.Value.IFrame = frame.IsIDR()
vt.Media.WriteAVCC(ts, frame)
vt.Value.DTS = ts * 90
vt.Value.PTS = (ts + frame.CTS()) * 90
for nalus := frame[5:]; len(nalus) > vt.nalulenSize; {
nalulen := util.ReadBE[int](nalus[:vt.nalulenSize])
if nalulen == 0 {
@@ -227,13 +229,13 @@ func (vt *Video) CompleteAVCC(rv *AVFrame[NALUSlice]) {
}
func (vt *Video) Flush() {
if vt.Value.IFrame {
rv := &vt.Value
if rv.IFrame {
vt.computeGOP()
}
if vt.Attached == 0 && vt.IDRing != nil && vt.DecoderConfiguration.Seq > 0 {
defer vt.Attach()
}
rv := &vt.Value
// 没有实际媒体数据
if len(rv.Raw) == 0 {
rv.Reset()