rtmp: fix parsing error caused by extended timestamps (#2393) (#2556) (#2384) (#1550) (#2564) (#2808)

This commit is contained in:
Alessandro Ros
2023-12-16 17:17:42 +01:00
committed by GitHub
parent 0c131a2e92
commit 89560c19a0
14 changed files with 430 additions and 228 deletions

View File

@@ -15,7 +15,7 @@ type Chunk2 struct {
}
// Read reads the chunk.
func (c *Chunk2) Read(r io.Reader, chunkBodyLen uint32) error {
func (c *Chunk2) Read(r io.Reader, bodyLen uint32, _ bool) error {
header := make([]byte, 4)
_, err := io.ReadFull(r, header)
if err != nil {
@@ -25,18 +25,48 @@ func (c *Chunk2) Read(r io.Reader, chunkBodyLen uint32) error {
c.ChunkStreamID = header[0] & 0x3F
c.TimestampDelta = uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
c.Body = make([]byte, chunkBodyLen)
if c.TimestampDelta >= 0xFFFFFF {
_, err = io.ReadFull(r, header[:4])
if err != nil {
return err
}
c.TimestampDelta = uint32(header[0])<<24 | uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
}
c.Body = make([]byte, bodyLen)
_, err = io.ReadFull(r, c.Body)
return err
}
func (c Chunk2) marshalSize() int {
n := 4 + len(c.Body)
if c.TimestampDelta >= 0xFFFFFF {
n += 4
}
return n
}
// Marshal writes the chunk.
func (c Chunk2) Marshal() ([]byte, error) {
buf := make([]byte, 4+len(c.Body))
func (c Chunk2) Marshal(_ bool) ([]byte, error) {
buf := make([]byte, c.marshalSize())
buf[0] = 2<<6 | c.ChunkStreamID
buf[1] = byte(c.TimestampDelta >> 16)
buf[2] = byte(c.TimestampDelta >> 8)
buf[3] = byte(c.TimestampDelta)
copy(buf[4:], c.Body)
if c.TimestampDelta >= 0xFFFFFF {
buf[1] = 0xFF
buf[2] = 0xFF
buf[3] = 0xFF
buf[4] = byte(c.TimestampDelta >> 24)
buf[5] = byte(c.TimestampDelta >> 16)
buf[6] = byte(c.TimestampDelta >> 8)
buf[7] = byte(c.TimestampDelta)
copy(buf[8:], c.Body)
} else {
buf[1] = byte(c.TimestampDelta >> 16)
buf[2] = byte(c.TimestampDelta >> 8)
buf[3] = byte(c.TimestampDelta)
copy(buf[4:], c.Body)
}
return buf, nil
}