mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-15 04:01:15 +08:00
rtmp: add message reader / writer
This commit is contained in:
66
internal/rtmp/chunk/chunk0.go
Normal file
66
internal/rtmp/chunk/chunk0.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package chunk
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Chunk0 is a type 0 chunk.
|
||||
// This type MUST be used at
|
||||
// the start of a chunk stream, and whenever the stream timestamp goes
|
||||
// backward (e.g., because of a backward seek).
|
||||
type Chunk0 struct {
|
||||
ChunkStreamID byte
|
||||
Timestamp uint32
|
||||
Type MessageType
|
||||
MessageStreamID uint32
|
||||
BodyLen uint32
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// Read reads the chunk.
|
||||
func (c *Chunk0) Read(r io.Reader, chunkMaxBodyLen int) error {
|
||||
header := make([]byte, 12)
|
||||
_, err := r.Read(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.ChunkStreamID = header[0] & 0x3F
|
||||
c.Timestamp = uint32(header[1])<<16 | uint32(header[2])<<8 | uint32(header[3])
|
||||
c.BodyLen = uint32(header[4])<<16 | uint32(header[5])<<8 | uint32(header[6])
|
||||
c.Type = MessageType(header[7])
|
||||
c.MessageStreamID = uint32(header[8])<<24 | uint32(header[9])<<16 | uint32(header[10])<<8 | uint32(header[11])
|
||||
|
||||
chunkBodyLen := int(c.BodyLen)
|
||||
if chunkBodyLen > chunkMaxBodyLen {
|
||||
chunkBodyLen = chunkMaxBodyLen
|
||||
}
|
||||
|
||||
c.Body = make([]byte, chunkBodyLen)
|
||||
_, err = r.Read(c.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
// Write writes the chunk.
|
||||
func (c Chunk0) Write(w io.Writer) error {
|
||||
header := make([]byte, 12)
|
||||
header[0] = c.ChunkStreamID
|
||||
header[1] = byte(c.Timestamp >> 16)
|
||||
header[2] = byte(c.Timestamp >> 8)
|
||||
header[3] = byte(c.Timestamp)
|
||||
header[4] = byte(c.BodyLen >> 16)
|
||||
header[5] = byte(c.BodyLen >> 8)
|
||||
header[6] = byte(c.BodyLen)
|
||||
header[7] = byte(c.Type)
|
||||
header[8] = byte(c.MessageStreamID >> 24)
|
||||
header[9] = byte(c.MessageStreamID >> 16)
|
||||
header[10] = byte(c.MessageStreamID >> 8)
|
||||
header[11] = byte(c.MessageStreamID)
|
||||
_, err := w.Write(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = w.Write(c.Body)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user