Skip to content

Commit edc8002

Browse files
committed
Fix: rec time stamp
1 parent fbea0cf commit edc8002

File tree

8 files changed

+29
-40
lines changed

8 files changed

+29
-40
lines changed

av/pack.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Packet struct {
1515
StreamID uint32
1616
Header PacketHeader
1717
Data []byte
18+
First bool
1819
}
1920

2021
func (p *Packet) Type() uint8 {

container/flv/reader.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
)
1212

1313
type Reader struct {
14-
r *stream.Reader
15-
inited bool
16-
demuxer *Demuxer
17-
tagHeaderBuf []byte
18-
bufSize int
19-
FlvTagHeader FlvTagHeader
14+
r *stream.Reader
15+
inited bool
16+
demuxer *Demuxer
17+
tagHeaderBuf []byte
18+
bufSize int
19+
FlvTagHeader FlvTagHeader
20+
loadedFirstPacket bool
2021
}
2122

2223
type ReaderConf func(*Reader)
@@ -83,12 +84,18 @@ func (fr *Reader) Read() (p *av.Packet, err error) {
8384
return nil, ErrPreDataLen
8485
}
8586

87+
if !fr.loadedFirstPacket {
88+
fr.loadedFirstPacket = true
89+
p.First = true
90+
}
91+
8692
if p.IsMetadata {
8793
p.Data, err = amf.MetaDataReform(p.Data, amf.ADD)
8894
if err != nil {
8995
return
9096
}
97+
return p, nil
98+
} else {
99+
return p, fr.demuxer.DemuxH(p)
91100
}
92-
93-
return p, fr.demuxer.DemuxH(p)
94101
}

container/flv/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (w *Writer) Write(p *av.Packet) error {
8383
return nil
8484
}
8585
dataLen := len(p.Data)
86-
timestamp := w.t.RecTimeStamp(p.TimeStamp)
86+
timestamp := w.t.RecTimeStamp(p.TimeStamp, p.First)
8787

8888
preDataLen := dataLen + headerLen
8989
timestampExt := timestamp >> 24

protocol/hls/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (source *Source) Write(p *av.Packet) (err error) {
8787
}
8888

8989
p = p.Clone()
90-
p.TimeStamp = source.t.RecTimeStamp(p.TimeStamp)
90+
p.TimeStamp = source.t.RecTimeStamp(p.TimeStamp, p.First)
9191

9292
select {
9393
case source.packetQueue <- p:

protocol/httpflv/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (w *HttpFlvWriter) Write(p *av.Packet) (err error) {
6363
return av.ErrClosed
6464
}
6565
p = p.Clone()
66-
p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp)
66+
p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp, p.First)
6767
select {
6868
case w.packetQueue <- p:
6969
default:

protocol/rtmp/reader.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type Reader struct {
1414
conn ChunkReader
1515
ReadBWInfo StaticsBW
1616

17-
closed uint32
17+
closed uint32
18+
loadedFirstPacket bool
1819
}
1920

2021
func NewReader(conn ChunkReader) *Reader {
@@ -75,6 +76,11 @@ func (v *Reader) Read() (p *av.Packet, err error) {
7576
p.Data = cs.Data
7677
p.TimeStamp = cs.Timestamp
7778

79+
if !v.loadedFirstPacket {
80+
v.loadedFirstPacket = true
81+
p.First = true
82+
}
83+
7884
v.SaveStatics(p.StreamID, uint64(len(p.Data)), p.IsVideo)
7985
return p, v.demuxer.DemuxH(p)
8086
}

protocol/rtmp/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (w *Writer) Write(p *av.Packet) (err error) {
6464
}
6565

6666
p = p.Clone()
67-
p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp)
67+
p.TimeStamp = w.t.RecTimeStamp(p.TimeStamp, p.First)
6868

6969
select {
7070
case w.packetQueue <- p:

utils/utils.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,8 @@ type Timestamp struct {
55
lastTimestamp uint32
66
}
77

8-
func (t *Timestamp) RecTimeStamp(timestamp uint32) uint32 {
9-
// if typeID == av.TAG_VIDEO {
10-
// if timestamp < rw.videoTimestamp {
11-
// if rw.lastVideoTimestamp > timestamp {
12-
// rw.videoTimestamp += timestamp
13-
// } else {
14-
// rw.videoTimestamp += timestamp - rw.lastVideoTimestamp
15-
// }
16-
// } else {
17-
// rw.videoTimestamp = timestamp
18-
// }
19-
// rw.lastVideoTimestamp = timestamp
20-
// } else if typeID == av.TAG_AUDIO {
21-
// if timestamp < rw.audioTimestamp {
22-
// if rw.lastAudioTimestamp > timestamp {
23-
// rw.audioTimestamp += timestamp
24-
// } else {
25-
// rw.audioTimestamp += timestamp - rw.lastAudioTimestamp
26-
// }
27-
// } else {
28-
// rw.audioTimestamp = timestamp
29-
// }
30-
// rw.lastAudioTimestamp = timestamp
31-
// }
32-
// return rw.timeStamp()
33-
34-
if t.lastTimestamp > timestamp+100 {
8+
func (t *Timestamp) RecTimeStamp(timestamp uint32, reconn bool) uint32 {
9+
if reconn {
3510
t.baseTimestamp += t.lastTimestamp
3611
t.lastTimestamp = timestamp
3712
}

0 commit comments

Comments
 (0)