rtp时间戳计算修改

This commit is contained in:
langhuihui
2021-07-22 21:12:14 +08:00
parent a459993df4
commit 155a6b608a
3 changed files with 18 additions and 8 deletions

24
rtp.go
View File

@@ -1,7 +1,6 @@
package engine package engine
import ( import (
"github.com/Monibuca/utils/v3"
"github.com/Monibuca/utils/v3/codec" "github.com/Monibuca/utils/v3/codec"
"github.com/pion/rtp" "github.com/pion/rtp"
) )
@@ -50,17 +49,23 @@ func (v *RTPVideo) push(payload []byte) {
} }
var p *VideoPack var p *VideoPack
t0 := v.Timestamp t0 := v.Timestamp
absTs := uint32(0)
tmpVT := v.Stream.NewVideoTrack(0) tmpVT := v.Stream.NewVideoTrack(0)
tmpVT.ExtraData = v.ExtraData tmpVT.ExtraData = v.ExtraData
tmpVT.CodecID = v.CodecID tmpVT.CodecID = v.CodecID
start := tmpVT.Ring start := tmpVT.Ring
tmpVT.PushNalu(0, 0, v.Payload) tmpVT.PushNalu(absTs, 0, v.Payload)
v.Push = func(payload []byte) { v.Push = func(payload []byte) {
if err := v.Unmarshal(payload); err != nil { if err := v.Unmarshal(payload); err != nil {
return return
} }
t1 := (v.Timestamp - t0) / 90 if t0 > v.Timestamp && t0-v.Timestamp > 100000 {
utils.Println("video:", t1) absTs += (v.Timestamp)
} else {
absTs += (v.Timestamp - t0)
}
t0 = v.Timestamp
t1 := absTs / 90
tmpVT.PushNalu(t1, 0, v.Payload) tmpVT.PushNalu(t1, 0, v.Payload)
end := tmpVT.Prev() end := tmpVT.Prev()
if start != end { if start != end {
@@ -156,14 +161,21 @@ func (v *RTPAudio) push(payload []byte) {
return return
} }
t0 := v.Timestamp t0 := v.Timestamp
absTs := uint32(0)
switch at.CodecID { switch at.CodecID {
case 10: case 10:
tb := at.SoundRate
v.Push = func(payload []byte) { v.Push = func(payload []byte) {
if err := v.Unmarshal(payload); err != nil { if err := v.Unmarshal(payload); err != nil {
return return
} }
t1 := (v.Timestamp - t0) / 90 if v.Timestamp >= t0 {
utils.Println("audio:", t1) absTs += (v.Timestamp - t0)
} else {
absTs += (v.Timestamp)
}
t0 = v.Timestamp
t1 := absTs * 10 / uint32(tb/100)
for _, payload = range codec.ParseRTPAAC(v.Payload) { for _, payload = range codec.ParseRTPAAC(v.Payload) {
at.PushRaw(t1, payload) at.PushRaw(t1, payload)
} }

View File

@@ -95,7 +95,6 @@ func (s *Subscriber) Play(at *AudioTrack, vt *VideoTrack) {
case <-streamExit: case <-streamExit:
return return
default: default:
utils.Println(vp.Timestamp, ap.Timestamp, ap.Timestamp > vp.Timestamp)
if ap.Timestamp > vp.Timestamp || ap.Timestamp == 0 { if ap.Timestamp > vp.Timestamp || ap.Timestamp == 0 {
s.OnVideo(vp.Copy(vst)) s.OnVideo(vp.Copy(vst))
vr.MoveNext() vr.MoveNext()

View File

@@ -562,7 +562,6 @@ func (vt *VideoTrack) Play(onVideo func(VideoPack), exit1, exit2 <-chan struct{}
vr := vt.SubRing(vt.IDRing) //从关键帧开始读取,首屏秒开 vr := vt.SubRing(vt.IDRing) //从关键帧开始读取,首屏秒开
vp := vr.Read().(*VideoPack) vp := vr.Read().(*VideoPack)
for startTimestamp := vp.Timestamp; vt.Goon(); vp = vr.Read().(*VideoPack) { for startTimestamp := vp.Timestamp; vt.Goon(); vp = vr.Read().(*VideoPack) {
utils.Println(vp.Timestamp)
select { select {
case <-exit1: case <-exit1:
return return