Skip to content

Commit

Permalink
Fix: rec time stamp
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Jun 16, 2024
1 parent fbea0cf commit edc8002
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 40 deletions.
1 change: 1 addition & 0 deletions av/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Packet struct {
StreamID uint32
Header PacketHeader
Data []byte
First bool
}

func (p *Packet) Type() uint8 {
Expand Down
23 changes: 15 additions & 8 deletions container/flv/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion container/flv/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion protocol/hls/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion protocol/httpflv/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion protocol/rtmp/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type Reader struct {
conn ChunkReader
ReadBWInfo StaticsBW

closed uint32
closed uint32
loadedFirstPacket bool
}

func NewReader(conn ChunkReader) *Reader {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/rtmp/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 2 additions & 27 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit edc8002

Please sign in to comment.