mediamtx/internal/rtmp/chunk/chunk2.go

43 lines
982 B
Go
Raw Normal View History

2022-06-04 23:06:40 +00:00
package chunk
2022-05-13 22:11:01 +00:00
import (
"io"
)
// Chunk2 is a type 2 chunk.
2022-09-17 19:19:45 +00:00
// Neither the stream ID nor the
2022-05-13 22:11:01 +00:00
// message length is included; this chunk has the same stream ID and
// message length as the preceding chunk.
type Chunk2 struct {
ChunkStreamID byte
TimestampDelta uint32
Body []byte
}
2022-05-16 09:57:29 +00:00
// Read reads the chunk.
2022-06-08 18:47:36 +00:00
func (c *Chunk2) Read(r io.Reader, chunkBodyLen uint32) error {
2022-05-16 09:57:29 +00:00
header := make([]byte, 4)
_, err := io.ReadFull(r, header)
2022-05-16 09:57:29 +00:00
if err != nil {
return err
}
c.ChunkStreamID = header[0] & 0x3F
2022-06-04 23:06:40 +00:00
c.TimestampDelta = uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
2022-05-16 09:57:29 +00:00
c.Body = make([]byte, chunkBodyLen)
_, err = io.ReadFull(r, c.Body)
2022-05-16 09:57:29 +00:00
return err
}
2022-06-27 15:51:54 +00:00
// Marshal writes the chunk.
func (c Chunk2) Marshal() ([]byte, error) {
2022-06-08 12:07:30 +00:00
buf := make([]byte, 4+len(c.Body))
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)
return buf, nil
2022-05-13 22:11:01 +00:00
}