rtmp: add user control messages

This commit is contained in:
aler9
2022-06-05 22:44:55 +02:00
parent 9d3fd3bc37
commit 6a24c82589
10 changed files with 344 additions and 51 deletions

View File

@@ -1,44 +1,12 @@
package message
import (
"encoding/binary"
"fmt"
"github.com/aler9/rtsp-simple-server/internal/rtmp/chunk"
"github.com/aler9/rtsp-simple-server/internal/rtmp/rawmessage"
// user control types.
const (
UserControlTypeStreamBegin = 0
UserControlTypeStreamEOF = 1
UserControlTypeStreamDry = 2
UserControlTypeSetBufferLength = 3
UserControlTypeStreamIsRecorded = 4
UserControlTypePingRequest = 6
UserControlTypePingResponse = 7
)
// MsgUserControl is a user control message.
type MsgUserControl struct {
Type uint16
Payload []byte
}
// Unmarshal implements Message.
func (m *MsgUserControl) Unmarshal(raw *rawmessage.Message) error {
if raw.ChunkStreamID != ControlChunkStreamID {
return fmt.Errorf("unexpected chunk stream ID")
}
if len(raw.Body) < 2 {
return fmt.Errorf("unexpected body size")
}
m.Type = binary.BigEndian.Uint16(raw.Body)
m.Payload = raw.Body[2:]
return nil
}
// Marshal implements Message.
func (m MsgUserControl) Marshal() (*rawmessage.Message, error) {
body := make([]byte, 2+len(m.Payload))
binary.BigEndian.PutUint16(body, m.Type)
copy(body[2:], m.Payload)
return &rawmessage.Message{
ChunkStreamID: ControlChunkStreamID,
Type: chunk.MessageTypeUserControl,
Body: body,
}, nil
}