Skip to content

Commit

Permalink
Handle streams longer than 2^24 ms.
Browse files Browse the repository at this point in the history
The FLV timestamp field is only 24 bits. A stream will hit this in
less than 5 hours, starting from a timestamp of 0. In this case,
write out the timestamp as an extended field for type-0 chunks
(type0 is the only output chunk type that is supported right now).
  • Loading branch information
j0sh committed Feb 14, 2018
1 parent bd41a3b commit 4003a2c
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions format/rtmp/rtmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,15 @@ func (self *Conn) writeAVTag(tag flvio.Tag, ts int32) (err error) {
data = tag.Data
}

b := self.tmpwbuf(chunkHeaderLength + flvio.MaxTagSubHeaderLength)
hdrlen := tag.FillHeader(b[chunkHeaderLength:])
actualChunkHeaderLength := chunkHeaderLength
if uint32(ts) > FlvTimestampMax {
actualChunkHeaderLength += 4
}

b := self.tmpwbuf(actualChunkHeaderLength + flvio.MaxTagSubHeaderLength)
hdrlen := tag.FillHeader(b[actualChunkHeaderLength:])
self.fillChunkHeader(b, csid, ts, msgtypeid, self.avmsgsid, hdrlen+len(data))
n := hdrlen + chunkHeaderLength
n := hdrlen + actualChunkHeaderLength

if n+len(data) > self.writeMaxChunkSize {
if err = self.writeSetChunkSize(n + len(data)); err != nil {
Expand Down Expand Up @@ -1043,6 +1048,7 @@ func (self *Conn) writeSetBufferLength(msgsid uint32, timestamp uint32) (err err
}

const chunkHeaderLength = 12
const FlvTimestampMax = 0xFFFFFF

func (self *Conn) fillChunkHeader(b []byte, csid uint32, timestamp int32, msgtypeid uint8, msgsid uint32, msgdatalen int) (n int) {
// 0 1 2 3
Expand All @@ -1059,14 +1065,22 @@ func (self *Conn) fillChunkHeader(b []byte, csid uint32, timestamp int32, msgtyp

b[n] = byte(csid) & 0x3f
n++
pio.PutU24BE(b[n:], uint32(timestamp))
if uint32(timestamp) <= FlvTimestampMax {
pio.PutU24BE(b[n:], uint32(timestamp))
} else {
pio.PutU24BE(b[n:], FlvTimestampMax)
}
n += 3
pio.PutU24BE(b[n:], uint32(msgdatalen))
n += 3
b[n] = msgtypeid
n++
pio.PutU32LE(b[n:], msgsid)
n += 4
if uint32(timestamp) > FlvTimestampMax {
pio.PutU32BE(b[n:], uint32(timestamp))
n += 4
}

if Debug {
fmt.Printf("rtmp: write chunk msgdatalen=%d msgsid=%d\n", msgdatalen, msgsid)
Expand Down

0 comments on commit 4003a2c

Please sign in to comment.