mediamtx/internal/rtmp/message/msg_video.go

83 lines
1.8 KiB
Go
Raw Normal View History

2022-06-04 23:06:40 +00:00
package message
import (
2022-06-07 20:48:10 +00:00
"fmt"
"time"
2022-06-07 20:48:10 +00:00
"github.com/notedit/rtmp/format/flv/flvio"
"github.com/aler9/mediamtx/internal/rtmp/chunk"
"github.com/aler9/mediamtx/internal/rtmp/rawmessage"
2022-06-04 23:06:40 +00:00
)
const (
2022-08-16 15:17:42 +00:00
// MsgVideoChunkStreamID is the chunk stream ID that is usually used to send MsgVideo{}
MsgVideoChunkStreamID = 6
)
2022-06-04 23:06:40 +00:00
// MsgVideo is a video message.
type MsgVideo struct {
ChunkStreamID byte
DTS time.Duration
2022-06-04 23:06:40 +00:00
MessageStreamID uint32
2022-06-07 20:48:10 +00:00
IsKeyFrame bool
H264Type uint8
PTSDelta time.Duration
2022-06-07 20:48:10 +00:00
Payload []byte
2022-06-04 23:06:40 +00:00
}
// Unmarshal implements Message.
func (m *MsgVideo) Unmarshal(raw *rawmessage.Message) error {
m.ChunkStreamID = raw.ChunkStreamID
m.DTS = raw.Timestamp
2022-06-04 23:06:40 +00:00
m.MessageStreamID = raw.MessageStreamID
2022-06-07 20:48:10 +00:00
if len(raw.Body) < 5 {
return fmt.Errorf("invalid body size")
}
m.IsKeyFrame = (raw.Body[0] >> 4) == flvio.FRAME_KEY
codec := raw.Body[0] & 0x0F
if codec != flvio.VIDEO_H264 {
return fmt.Errorf("unsupported video codec: %d", codec)
}
m.H264Type = raw.Body[1]
tmp := uint32(raw.Body[2])<<16 | uint32(raw.Body[3])<<8 | uint32(raw.Body[4])
m.PTSDelta = time.Duration(tmp) * time.Millisecond
2022-06-07 20:48:10 +00:00
m.Payload = raw.Body[5:]
2022-06-04 23:06:40 +00:00
return nil
}
// Marshal implements Message.
func (m MsgVideo) Marshal() (*rawmessage.Message, error) {
2022-06-07 20:48:10 +00:00
body := make([]byte, 5+len(m.Payload))
if m.IsKeyFrame {
body[0] = flvio.FRAME_KEY << 4
} else {
body[0] = flvio.FRAME_INTER << 4
}
body[0] |= flvio.VIDEO_H264
body[1] = m.H264Type
tmp := uint32(m.PTSDelta / time.Millisecond)
body[2] = uint8(tmp >> 16)
body[3] = uint8(tmp >> 8)
body[4] = uint8(tmp)
2022-06-07 20:48:10 +00:00
copy(body[5:], m.Payload)
2022-06-04 23:06:40 +00:00
return &rawmessage.Message{
ChunkStreamID: m.ChunkStreamID,
Timestamp: m.DTS,
2022-06-04 23:06:40 +00:00
Type: chunk.MessageTypeVideo,
MessageStreamID: m.MessageStreamID,
2022-06-07 20:48:10 +00:00
Body: body,
2022-06-04 23:06:40 +00:00
}, nil
}