From edc8002681f5c0518becdaf1bd1e4a4df89beff9 Mon Sep 17 00:00:00 2001 From: zijiren233 Date: Mon, 17 Jun 2024 01:39:07 +0800 Subject: [PATCH] Fix: rec time stamp --- av/pack.go | 1 + container/flv/reader.go | 23 +++++++++++++++-------- container/flv/writer.go | 2 +- protocol/hls/source.go | 2 +- protocol/httpflv/writer.go | 2 +- protocol/rtmp/reader.go | 8 +++++++- protocol/rtmp/writer.go | 2 +- utils/utils.go | 29 ++--------------------------- 8 files changed, 29 insertions(+), 40 deletions(-) diff --git a/av/pack.go b/av/pack.go index 3fae27b..de179a7 100644 --- a/av/pack.go +++ b/av/pack.go @@ -15,6 +15,7 @@ type Packet struct { StreamID uint32 Header PacketHeader Data []byte + First bool } func (p *Packet) Type() uint8 { diff --git a/container/flv/reader.go b/container/flv/reader.go index fec1900..0f064f6 100644 --- a/container/flv/reader.go +++ b/container/flv/reader.go @@ -11,12 +11,13 @@ import ( ) type Reader struct { - r *stream.Reader - inited bool - demuxer *Demuxer - tagHeaderBuf []byte - bufSize int - FlvTagHeader FlvTagHeader + r *stream.Reader + inited bool + demuxer *Demuxer + tagHeaderBuf []byte + bufSize int + FlvTagHeader FlvTagHeader + loadedFirstPacket bool } type ReaderConf func(*Reader) @@ -83,12 +84,18 @@ func (fr *Reader) Read() (p *av.Packet, err error) { return nil, ErrPreDataLen } + if !fr.loadedFirstPacket { + fr.loadedFirstPacket = true + p.First = true + } + if p.IsMetadata { p.Data, err = amf.MetaDataReform(p.Data, amf.ADD) if err != nil { return } + return p, nil + } else { + return p, fr.demuxer.DemuxH(p) } - - return p, fr.demuxer.DemuxH(p) } diff --git a/container/flv/writer.go b/container/flv/writer.go index 71e2d80..368651b 100755 --- a/container/flv/writer.go +++ b/container/flv/writer.go @@ -83,7 +83,7 @@ func (w *Writer) Write(p *av.Packet) error { return nil } dataLen := len(p.Data) - timestamp := w.t.RecTimeStamp(p.TimeStamp) + timestamp := w.t.RecTimeStamp(p.TimeStamp, p.First) preDataLen := dataLen + headerLen timestampExt := timestamp >> 24 diff --git a/protocol/hls/source.go b/protocol/hls/source.go index 4934b95..f0c0898 100644 --- a/protocol/hls/source.go +++ b/protocol/hls/source.go @@ -87,7 +87,7 @@ func (source *Source) Write(p *av.Packet) (err error) { } p = p.Clone() - p.TimeStamp = source.t.RecTimeStamp(p.TimeStamp) + p.TimeStamp = source.t.RecTimeStamp(p.TimeStamp, p.First) select { case source.packetQueue <- p: diff --git a/protocol/httpflv/writer.go b/protocol/httpflv/writer.go index 76e87ae..5771e6a 100755 --- a/protocol/httpflv/writer.go +++ b/protocol/httpflv/writer.go @@ -63,7 +63,7 @@ func (w *HttpFlvWriter) Write(p *av.Packet) (err error) { return av.ErrClosed } p = p.Clone() - p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp) + p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp, p.First) select { case w.packetQueue <- p: default: diff --git a/protocol/rtmp/reader.go b/protocol/rtmp/reader.go index 6852dbb..18b392a 100644 --- a/protocol/rtmp/reader.go +++ b/protocol/rtmp/reader.go @@ -14,7 +14,8 @@ type Reader struct { conn ChunkReader ReadBWInfo StaticsBW - closed uint32 + closed uint32 + loadedFirstPacket bool } func NewReader(conn ChunkReader) *Reader { @@ -75,6 +76,11 @@ func (v *Reader) Read() (p *av.Packet, err error) { p.Data = cs.Data p.TimeStamp = cs.Timestamp + if !v.loadedFirstPacket { + v.loadedFirstPacket = true + p.First = true + } + v.SaveStatics(p.StreamID, uint64(len(p.Data)), p.IsVideo) return p, v.demuxer.DemuxH(p) } diff --git a/protocol/rtmp/writer.go b/protocol/rtmp/writer.go index a9cd77c..ddf03dd 100644 --- a/protocol/rtmp/writer.go +++ b/protocol/rtmp/writer.go @@ -64,7 +64,7 @@ func (w *Writer) Write(p *av.Packet) (err error) { } p = p.Clone() - p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp) + p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp, p.First) select { case w.packetQueue <- p: diff --git a/utils/utils.go b/utils/utils.go index 8eecc44..7290449 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,33 +5,8 @@ type Timestamp struct { lastTimestamp uint32 } -func (t *Timestamp) RecTimeStamp(timestamp uint32) uint32 { - // if typeID == av.TAG_VIDEO { - // if timestamp < rw.videoTimestamp { - // if rw.lastVideoTimestamp > timestamp { - // rw.videoTimestamp += timestamp - // } else { - // rw.videoTimestamp += timestamp - rw.lastVideoTimestamp - // } - // } else { - // rw.videoTimestamp = timestamp - // } - // rw.lastVideoTimestamp = timestamp - // } else if typeID == av.TAG_AUDIO { - // if timestamp < rw.audioTimestamp { - // if rw.lastAudioTimestamp > timestamp { - // rw.audioTimestamp += timestamp - // } else { - // rw.audioTimestamp += timestamp - rw.lastAudioTimestamp - // } - // } else { - // rw.audioTimestamp = timestamp - // } - // rw.lastAudioTimestamp = timestamp - // } - // return rw.timeStamp() - - if t.lastTimestamp > timestamp+100 { +func (t *Timestamp) RecTimeStamp(timestamp uint32, reconn bool) uint32 { + if reconn { t.baseTimestamp += t.lastTimestamp t.lastTimestamp = timestamp }