57 changed files with 1971 additions and 1716 deletions
@ -0,0 +1,133 @@ |
|||||||
|
package formatprocessor |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/bluenviron/gortsplib/v3/pkg/formats" |
||||||
|
"github.com/bluenviron/gortsplib/v3/pkg/formats/rtpav1" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/av1" |
||||||
|
"github.com/pion/rtp" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/logger" |
||||||
|
) |
||||||
|
|
||||||
|
// UnitAV1 is an AV1 data unit.
|
||||||
|
type UnitAV1 struct { |
||||||
|
RTPPackets []*rtp.Packet |
||||||
|
NTP time.Time |
||||||
|
PTS time.Duration |
||||||
|
OBUs [][]byte |
||||||
|
} |
||||||
|
|
||||||
|
// GetRTPPackets implements Unit.
|
||||||
|
func (d *UnitAV1) GetRTPPackets() []*rtp.Packet { |
||||||
|
return d.RTPPackets |
||||||
|
} |
||||||
|
|
||||||
|
// GetNTP implements Unit.
|
||||||
|
func (d *UnitAV1) GetNTP() time.Time { |
||||||
|
return d.NTP |
||||||
|
} |
||||||
|
|
||||||
|
type formatProcessorAV1 struct { |
||||||
|
udpMaxPayloadSize int |
||||||
|
format *formats.AV1 |
||||||
|
log logger.Writer |
||||||
|
|
||||||
|
encoder *rtpav1.Encoder |
||||||
|
decoder *rtpav1.Decoder |
||||||
|
lastKeyFrameReceived time.Time |
||||||
|
} |
||||||
|
|
||||||
|
func newAV1( |
||||||
|
udpMaxPayloadSize int, |
||||||
|
forma *formats.AV1, |
||||||
|
generateRTPPackets bool, |
||||||
|
log logger.Writer, |
||||||
|
) (*formatProcessorAV1, error) { |
||||||
|
t := &formatProcessorAV1{ |
||||||
|
udpMaxPayloadSize: udpMaxPayloadSize, |
||||||
|
format: forma, |
||||||
|
log: log, |
||||||
|
} |
||||||
|
|
||||||
|
if generateRTPPackets { |
||||||
|
t.encoder = &rtpav1.Encoder{ |
||||||
|
PayloadMaxSize: t.udpMaxPayloadSize - 12, |
||||||
|
} |
||||||
|
t.encoder.Init() |
||||||
|
t.lastKeyFrameReceived = time.Now() |
||||||
|
} |
||||||
|
|
||||||
|
return t, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorAV1) checkKeyFrameInterval(containsKeyFrame bool) { |
||||||
|
if containsKeyFrame { |
||||||
|
t.lastKeyFrameReceived = time.Now() |
||||||
|
} else { |
||||||
|
now := time.Now() |
||||||
|
if now.Sub(t.lastKeyFrameReceived) >= maxKeyFrameInterval { |
||||||
|
t.lastKeyFrameReceived = now |
||||||
|
t.log.Log(logger.Warn, "no AV1 key frames received in %v, stream can't be decoded", maxKeyFrameInterval) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorAV1) checkOBUs(obus [][]byte) { |
||||||
|
containsKeyFrame, _ := av1.ContainsKeyFrame(obus) |
||||||
|
t.checkKeyFrameInterval(containsKeyFrame) |
||||||
|
} |
||||||
|
|
||||||
|
func (t *formatProcessorAV1) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
|
||||||
|
tunit := unit.(*UnitAV1) |
||||||
|
|
||||||
|
if tunit.RTPPackets != nil { |
||||||
|
pkt := tunit.RTPPackets[0] |
||||||
|
|
||||||
|
// remove padding
|
||||||
|
pkt.Header.Padding = false |
||||||
|
pkt.PaddingSize = 0 |
||||||
|
|
||||||
|
if pkt.MarshalSize() > t.udpMaxPayloadSize { |
||||||
|
return fmt.Errorf("payload size (%d) is greater than maximum allowed (%d)", |
||||||
|
pkt.MarshalSize(), t.udpMaxPayloadSize) |
||||||
|
} |
||||||
|
|
||||||
|
// decode from RTP
|
||||||
|
if hasNonRTSPReaders { |
||||||
|
if t.decoder == nil { |
||||||
|
t.decoder = t.format.CreateDecoder() |
||||||
|
t.lastKeyFrameReceived = time.Now() |
||||||
|
} |
||||||
|
|
||||||
|
// DecodeUntilMarker() is necessary, otherwise Encode() generates partial groups
|
||||||
|
obus, pts, err := t.decoder.DecodeUntilMarker(pkt) |
||||||
|
if err != nil { |
||||||
|
if err == rtpav1.ErrNonStartingPacketAndNoPrevious || err == rtpav1.ErrMorePacketsNeeded { |
||||||
|
return nil |
||||||
|
} |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
tunit.OBUs = obus |
||||||
|
t.checkOBUs(obus) |
||||||
|
tunit.PTS = pts |
||||||
|
} |
||||||
|
|
||||||
|
// route packet as is
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
t.checkOBUs(tunit.OBUs) |
||||||
|
|
||||||
|
// encode into RTP
|
||||||
|
pkts, err := t.encoder.Encode(tunit.OBUs, tunit.PTS) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
tunit.RTPPackets = pkts |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
@ -1,24 +0,0 @@ |
|||||||
package chunk |
|
||||||
|
|
||||||
// MessageType is a message type.
|
|
||||||
type MessageType byte |
|
||||||
|
|
||||||
// message types.
|
|
||||||
const ( |
|
||||||
MessageTypeSetChunkSize MessageType = 1 |
|
||||||
MessageTypeAbortMessage MessageType = 2 |
|
||||||
MessageTypeAcknowledge MessageType = 3 |
|
||||||
MessageTypeSetWindowAckSize MessageType = 5 |
|
||||||
MessageTypeSetPeerBandwidth MessageType = 6 |
|
||||||
|
|
||||||
MessageTypeUserControl MessageType = 4 |
|
||||||
|
|
||||||
MessageTypeCommandAMF3 MessageType = 17 |
|
||||||
MessageTypeCommandAMF0 MessageType = 20 |
|
||||||
|
|
||||||
MessageTypeDataAMF3 MessageType = 15 |
|
||||||
MessageTypeDataAMF0 MessageType = 18 |
|
||||||
|
|
||||||
MessageTypeAudio MessageType = 8 |
|
||||||
MessageTypeVideo MessageType = 9 |
|
||||||
) |
|
||||||
@ -0,0 +1,71 @@ |
|||||||
|
package message |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/rawmessage" |
||||||
|
) |
||||||
|
|
||||||
|
// ExtendedCodedFrames is a CodedFrames extended message.
|
||||||
|
type ExtendedCodedFrames struct { |
||||||
|
ChunkStreamID byte |
||||||
|
DTS time.Duration |
||||||
|
MessageStreamID uint32 |
||||||
|
FourCC [4]byte |
||||||
|
PTSDelta time.Duration |
||||||
|
Payload []byte |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal implements Message.
|
||||||
|
func (m *ExtendedCodedFrames) Unmarshal(raw *rawmessage.Message) error { |
||||||
|
if len(raw.Body) < 8 { |
||||||
|
return fmt.Errorf("not enough bytes") |
||||||
|
} |
||||||
|
|
||||||
|
m.ChunkStreamID = raw.ChunkStreamID |
||||||
|
m.DTS = raw.Timestamp |
||||||
|
m.MessageStreamID = raw.MessageStreamID |
||||||
|
copy(m.FourCC[:], raw.Body[1:5]) |
||||||
|
|
||||||
|
if m.FourCC == FourCCHEVC { |
||||||
|
m.PTSDelta = time.Duration(uint32(raw.Body[5])<<16|uint32(raw.Body[6])<<8|uint32(raw.Body[7])) * time.Millisecond |
||||||
|
m.Payload = raw.Body[8:] |
||||||
|
} else { |
||||||
|
m.Payload = raw.Body[5:] |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// Marshal implements Message.
|
||||||
|
func (m ExtendedCodedFrames) Marshal() (*rawmessage.Message, error) { |
||||||
|
var l int |
||||||
|
if m.FourCC == FourCCHEVC { |
||||||
|
l = 8 + len(m.Payload) |
||||||
|
} else { |
||||||
|
l = 5 + len(m.Payload) |
||||||
|
} |
||||||
|
body := make([]byte, l) |
||||||
|
|
||||||
|
body[0] = 0b10000000 | byte(ExtendedTypeCodedFrames) |
||||||
|
copy(body[1:5], m.FourCC[:]) |
||||||
|
|
||||||
|
if m.FourCC == FourCCHEVC { |
||||||
|
tmp := uint32(m.PTSDelta / time.Millisecond) |
||||||
|
body[5] = uint8(tmp >> 16) |
||||||
|
body[6] = uint8(tmp >> 8) |
||||||
|
body[7] = uint8(tmp) |
||||||
|
copy(body[8:], m.Payload) |
||||||
|
} else { |
||||||
|
copy(body[5:], m.Payload) |
||||||
|
} |
||||||
|
|
||||||
|
return &rawmessage.Message{ |
||||||
|
ChunkStreamID: m.ChunkStreamID, |
||||||
|
Timestamp: m.DTS, |
||||||
|
Type: uint8(TypeVideo), |
||||||
|
MessageStreamID: m.MessageStreamID, |
||||||
|
Body: body, |
||||||
|
}, nil |
||||||
|
} |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
package message |
||||||
|
|
||||||
|
import ( |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/rawmessage" |
||||||
|
) |
||||||
|
|
||||||
|
// ExtendedFramesX is a FramesX extended message.
|
||||||
|
type ExtendedFramesX struct { |
||||||
|
ChunkStreamID byte |
||||||
|
DTS time.Duration |
||||||
|
MessageStreamID uint32 |
||||||
|
FourCC [4]byte |
||||||
|
Payload []byte |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal implements Message.
|
||||||
|
func (m *ExtendedFramesX) Unmarshal(raw *rawmessage.Message) error { |
||||||
|
m.ChunkStreamID = raw.ChunkStreamID |
||||||
|
m.DTS = raw.Timestamp |
||||||
|
m.MessageStreamID = raw.MessageStreamID |
||||||
|
copy(m.FourCC[:], raw.Body[1:5]) |
||||||
|
m.Payload = raw.Body[5:] |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// Marshal implements Message.
|
||||||
|
func (m ExtendedFramesX) Marshal() (*rawmessage.Message, error) { |
||||||
|
body := make([]byte, 5+len(m.Payload)) |
||||||
|
|
||||||
|
body[0] = 0b10000000 | byte(ExtendedTypeFramesX) |
||||||
|
copy(body[1:5], m.FourCC[:]) |
||||||
|
copy(body[5:], m.Payload) |
||||||
|
|
||||||
|
return &rawmessage.Message{ |
||||||
|
ChunkStreamID: m.ChunkStreamID, |
||||||
|
Timestamp: m.DTS, |
||||||
|
Type: uint8(TypeVideo), |
||||||
|
MessageStreamID: m.MessageStreamID, |
||||||
|
Body: body, |
||||||
|
}, nil |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
package message |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/rawmessage" |
||||||
|
) |
||||||
|
|
||||||
|
// ExtendedMetadata is a metadata extended message.
|
||||||
|
type ExtendedMetadata struct { |
||||||
|
FourCC [4]byte |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal implements Message.
|
||||||
|
func (m *ExtendedMetadata) Unmarshal(raw *rawmessage.Message) error { |
||||||
|
copy(m.FourCC[:], raw.Body[1:5]) |
||||||
|
|
||||||
|
return fmt.Errorf("ExtendedMetadata is not implemented yet") |
||||||
|
} |
||||||
|
|
||||||
|
// Marshal implements Message.
|
||||||
|
func (m ExtendedMetadata) Marshal() (*rawmessage.Message, error) { |
||||||
|
return nil, fmt.Errorf("TODO") |
||||||
|
} |
||||||
@ -0,0 +1,24 @@ |
|||||||
|
package message |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/rawmessage" |
||||||
|
) |
||||||
|
|
||||||
|
// ExtendedMPEG2TSSequenceStart is a MPEG2-TS sequence start extended message.
|
||||||
|
type ExtendedMPEG2TSSequenceStart struct { |
||||||
|
FourCC [4]byte |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal implements Message.
|
||||||
|
func (m *ExtendedMPEG2TSSequenceStart) Unmarshal(raw *rawmessage.Message) error { |
||||||
|
copy(m.FourCC[:], raw.Body[1:5]) |
||||||
|
|
||||||
|
return fmt.Errorf("ExtendedMPEG2TSSequenceStart is not implemented yet") |
||||||
|
} |
||||||
|
|
||||||
|
// Marshal implements Message.
|
||||||
|
func (m ExtendedMPEG2TSSequenceStart) Marshal() (*rawmessage.Message, error) { |
||||||
|
return nil, fmt.Errorf("TODO") |
||||||
|
} |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
package message |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/rawmessage" |
||||||
|
) |
||||||
|
|
||||||
|
// ExtendedSequenceEnd is a sequence end extended message.
|
||||||
|
type ExtendedSequenceEnd struct { |
||||||
|
FourCC [4]byte |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal implements Message.
|
||||||
|
func (m *ExtendedSequenceEnd) Unmarshal(raw *rawmessage.Message) error { |
||||||
|
if len(raw.Body) != 5 { |
||||||
|
return fmt.Errorf("invalid body size") |
||||||
|
} |
||||||
|
|
||||||
|
copy(m.FourCC[:], raw.Body[1:5]) |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// Marshal implements Message.
|
||||||
|
func (m ExtendedSequenceEnd) Marshal() (*rawmessage.Message, error) { |
||||||
|
return nil, fmt.Errorf("TODO") |
||||||
|
} |
||||||
@ -0,0 +1,26 @@ |
|||||||
|
package message |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/rawmessage" |
||||||
|
) |
||||||
|
|
||||||
|
// ExtendedSequenceStart is a sequence start extended message.
|
||||||
|
type ExtendedSequenceStart struct { |
||||||
|
FourCC [4]byte |
||||||
|
Config []byte |
||||||
|
} |
||||||
|
|
||||||
|
// Unmarshal implements Message.
|
||||||
|
func (m *ExtendedSequenceStart) Unmarshal(raw *rawmessage.Message) error { |
||||||
|
copy(m.FourCC[:], raw.Body[1:5]) |
||||||
|
m.Config = raw.Body[5:] |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// Marshal implements Message.
|
||||||
|
func (m ExtendedSequenceStart) Marshal() (*rawmessage.Message, error) { |
||||||
|
return nil, fmt.Errorf("TODO") |
||||||
|
} |
||||||
@ -1,12 +0,0 @@ |
|||||||
package message |
|
||||||
|
|
||||||
// user control types.
|
|
||||||
const ( |
|
||||||
UserControlTypeStreamBegin = 0 |
|
||||||
UserControlTypeStreamEOF = 1 |
|
||||||
UserControlTypeStreamDry = 2 |
|
||||||
UserControlTypeSetBufferLength = 3 |
|
||||||
UserControlTypeStreamIsRecorded = 4 |
|
||||||
UserControlTypePingRequest = 6 |
|
||||||
UserControlTypePingResponse = 7 |
|
||||||
) |
|
||||||
@ -0,0 +1,37 @@ |
|||||||
|
package tracks |
||||||
|
|
||||||
|
import ( |
||||||
|
gomp4 "github.com/abema/go-mp4" |
||||||
|
) |
||||||
|
|
||||||
|
// BoxTypeAv1C returns the box type.
|
||||||
|
func BoxTypeAv1C() gomp4.BoxType { return gomp4.StrToBoxType("av1C") } |
||||||
|
|
||||||
|
func init() { //nolint:gochecknoinits
|
||||||
|
gomp4.AddBoxDef(&Av1C{}) |
||||||
|
} |
||||||
|
|
||||||
|
// Av1C is a Av1C ISO-BMFF box.
|
||||||
|
type Av1C struct { |
||||||
|
gomp4.Box |
||||||
|
Marker uint8 `mp4:"0,size=1,const=1"` |
||||||
|
Version uint8 `mp4:"1,size=7,const=1"` |
||||||
|
SeqProfile uint8 `mp4:"2,size=3"` |
||||||
|
SeqLevelIdx0 uint8 `mp4:"3,size=5"` |
||||||
|
SeqTier0 uint8 `mp4:"4,size=1"` |
||||||
|
HighBitdepth uint8 `mp4:"5,size=1"` |
||||||
|
TwelveBit uint8 `mp4:"6,size=1"` |
||||||
|
Monochrome uint8 `mp4:"7,size=1"` |
||||||
|
ChromaSubsamplingX uint8 `mp4:"8,size=1"` |
||||||
|
ChromaSubsamplingY uint8 `mp4:"9,size=1"` |
||||||
|
ChromaSamplePosition uint8 `mp4:"10,size=2"` |
||||||
|
Reserved uint8 `mp4:"11,size=3,const=0"` |
||||||
|
InitialPresentationDelayPresent uint8 `mp4:"12,size=1"` |
||||||
|
InitialPresentationDelayMinusOne uint8 `mp4:"13,size=4"` |
||||||
|
ConfigOBUs []uint8 `mp4:"14,size=8"` |
||||||
|
} |
||||||
|
|
||||||
|
// GetType returns the box type.
|
||||||
|
func (Av1C) GetType() gomp4.BoxType { |
||||||
|
return BoxTypeAv1C() |
||||||
|
} |
||||||
@ -0,0 +1,396 @@ |
|||||||
|
// Package tracks contains functions to read and write track metadata.
|
||||||
|
package tracks |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"errors" |
||||||
|
"fmt" |
||||||
|
"time" |
||||||
|
|
||||||
|
gomp4 "github.com/abema/go-mp4" |
||||||
|
"github.com/bluenviron/gortsplib/v3/pkg/formats" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/av1" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/h264" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/h265" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio" |
||||||
|
"github.com/notedit/rtmp/format/flv/flvio" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/h264conf" |
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/message" |
||||||
|
) |
||||||
|
|
||||||
|
func h265FindNALU(array []gomp4.HEVCNaluArray, typ h265.NALUType) []byte { |
||||||
|
for _, entry := range array { |
||||||
|
if entry.NaluType == byte(typ) && entry.NumNalus == 1 && |
||||||
|
h265.NALUType((entry.Nalus[0].NALUnit[0]>>1)&0b111111) == typ { |
||||||
|
return entry.Nalus[0].NALUnit |
||||||
|
} |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func trackFromH264DecoderConfig(data []byte) (formats.Format, error) { |
||||||
|
var conf h264conf.Conf |
||||||
|
err := conf.Unmarshal(data) |
||||||
|
if err != nil { |
||||||
|
return nil, fmt.Errorf("unable to parse H264 config: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
return &formats.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SPS: conf.SPS, |
||||||
|
PPS: conf.PPS, |
||||||
|
PacketizationMode: 1, |
||||||
|
}, nil |
||||||
|
} |
||||||
|
|
||||||
|
func trackFromAACDecoderConfig(data []byte) (*formats.MPEG4Audio, error) { |
||||||
|
var mpegConf mpeg4audio.Config |
||||||
|
err := mpegConf.Unmarshal(data) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
return &formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpegConf, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
}, nil |
||||||
|
} |
||||||
|
|
||||||
|
var errEmptyMetadata = errors.New("metadata is empty") |
||||||
|
|
||||||
|
func readTracksFromMetadata(r *message.ReadWriter, payload []interface{}) (formats.Format, formats.Format, error) { |
||||||
|
if len(payload) != 1 { |
||||||
|
return nil, nil, fmt.Errorf("invalid metadata") |
||||||
|
} |
||||||
|
|
||||||
|
md, ok := payload[0].(flvio.AMFMap) |
||||||
|
if !ok { |
||||||
|
return nil, nil, fmt.Errorf("invalid metadata") |
||||||
|
} |
||||||
|
|
||||||
|
var videoTrack formats.Format |
||||||
|
var audioTrack formats.Format |
||||||
|
|
||||||
|
hasVideo, err := func() (bool, error) { |
||||||
|
v, ok := md.GetV("videocodecid") |
||||||
|
if !ok { |
||||||
|
return false, nil |
||||||
|
} |
||||||
|
|
||||||
|
switch vt := v.(type) { |
||||||
|
case float64: |
||||||
|
switch vt { |
||||||
|
case 0: |
||||||
|
return false, nil |
||||||
|
|
||||||
|
case message.CodecH264: |
||||||
|
return true, nil |
||||||
|
} |
||||||
|
|
||||||
|
case string: |
||||||
|
if vt == "avc1" { |
||||||
|
return true, nil |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false, fmt.Errorf("unsupported video codec: %v", v) |
||||||
|
}() |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
hasAudio, err := func() (bool, error) { |
||||||
|
v, ok := md.GetV("audiocodecid") |
||||||
|
if !ok { |
||||||
|
return false, nil |
||||||
|
} |
||||||
|
|
||||||
|
switch vt := v.(type) { |
||||||
|
case float64: |
||||||
|
switch vt { |
||||||
|
case 0: |
||||||
|
return false, nil |
||||||
|
|
||||||
|
case message.CodecMPEG2Audio: |
||||||
|
audioTrack = &formats.MPEG2Audio{} |
||||||
|
return true, nil |
||||||
|
|
||||||
|
case message.CodecMPEG4Audio: |
||||||
|
return true, nil |
||||||
|
} |
||||||
|
|
||||||
|
case string: |
||||||
|
if vt == "mp4a" { |
||||||
|
return true, nil |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false, fmt.Errorf("unsupported audio codec %v", v) |
||||||
|
}() |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
if !hasVideo && !hasAudio { |
||||||
|
return nil, nil, errEmptyMetadata |
||||||
|
} |
||||||
|
|
||||||
|
for { |
||||||
|
if (!hasVideo || videoTrack != nil) && |
||||||
|
(!hasAudio || audioTrack != nil) { |
||||||
|
return videoTrack, audioTrack, nil |
||||||
|
} |
||||||
|
|
||||||
|
msg, err := r.Read() |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
switch tmsg := msg.(type) { |
||||||
|
case *message.Video: |
||||||
|
if !hasVideo { |
||||||
|
return nil, nil, fmt.Errorf("unexpected video packet") |
||||||
|
} |
||||||
|
|
||||||
|
if videoTrack == nil { |
||||||
|
if tmsg.Type == message.VideoTypeConfig { |
||||||
|
videoTrack, err = trackFromH264DecoderConfig(tmsg.Payload) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
// format used by OBS < 29.1 to publish H265
|
||||||
|
} else if tmsg.Type == message.VideoTypeAU && tmsg.IsKeyFrame { |
||||||
|
nalus, err := h264.AVCCUnmarshal(tmsg.Payload) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
var vps []byte |
||||||
|
var sps []byte |
||||||
|
var pps []byte |
||||||
|
|
||||||
|
for _, nalu := range nalus { |
||||||
|
typ := h265.NALUType((nalu[0] >> 1) & 0b111111) |
||||||
|
|
||||||
|
switch typ { |
||||||
|
case h265.NALUType_VPS_NUT: |
||||||
|
vps = nalu |
||||||
|
|
||||||
|
case h265.NALUType_SPS_NUT: |
||||||
|
sps = nalu |
||||||
|
|
||||||
|
case h265.NALUType_PPS_NUT: |
||||||
|
pps = nalu |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if vps != nil && sps != nil && pps != nil { |
||||||
|
videoTrack = &formats.H265{ |
||||||
|
PayloadTyp: 96, |
||||||
|
VPS: vps, |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
case *message.ExtendedSequenceStart: |
||||||
|
if videoTrack == nil { |
||||||
|
switch tmsg.FourCC { |
||||||
|
case message.FourCCHEVC: |
||||||
|
var hvcc gomp4.HvcC |
||||||
|
_, err := gomp4.Unmarshal(bytes.NewReader(tmsg.Config), uint64(len(tmsg.Config)), &hvcc, gomp4.Context{}) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, fmt.Errorf("invalid H265 configuration: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
vps := h265FindNALU(hvcc.NaluArrays, h265.NALUType_VPS_NUT) |
||||||
|
sps := h265FindNALU(hvcc.NaluArrays, h265.NALUType_SPS_NUT) |
||||||
|
pps := h265FindNALU(hvcc.NaluArrays, h265.NALUType_PPS_NUT) |
||||||
|
if vps == nil || sps == nil || pps == nil { |
||||||
|
return nil, nil, fmt.Errorf("H265 parameters are missing") |
||||||
|
} |
||||||
|
|
||||||
|
videoTrack = &formats.H265{ |
||||||
|
PayloadTyp: 96, |
||||||
|
VPS: vps, |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
} |
||||||
|
|
||||||
|
case message.FourCCAV1: |
||||||
|
var av1c Av1C |
||||||
|
_, err := gomp4.Unmarshal(bytes.NewReader(tmsg.Config), uint64(len(tmsg.Config)), &av1c, gomp4.Context{}) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, fmt.Errorf("invalid AV1 configuration: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
// parse sequence header and metadata contained in ConfigOBUs, but do not use them
|
||||||
|
_, err = av1.BitstreamUnmarshal(av1c.ConfigOBUs, false) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, fmt.Errorf("invalid AV1 configuration: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
videoTrack = &formats.AV1{} |
||||||
|
|
||||||
|
default: // VP9
|
||||||
|
return nil, nil, fmt.Errorf("VP9 is not supported yet") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
case *message.Audio: |
||||||
|
if !hasAudio { |
||||||
|
return nil, nil, fmt.Errorf("unexpected audio packet") |
||||||
|
} |
||||||
|
|
||||||
|
if audioTrack == nil && |
||||||
|
tmsg.Codec == message.CodecMPEG4Audio && |
||||||
|
tmsg.AACType == message.AudioAACTypeConfig { |
||||||
|
audioTrack, err = trackFromAACDecoderConfig(tmsg.Payload) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func readTracksFromMessages(r *message.ReadWriter, msg message.Message) (formats.Format, *formats.MPEG4Audio, error) { |
||||||
|
var startTime *time.Duration |
||||||
|
var videoTrack formats.Format |
||||||
|
var audioTrack *formats.MPEG4Audio |
||||||
|
|
||||||
|
// analyze 1 second of packets
|
||||||
|
outer: |
||||||
|
for { |
||||||
|
switch tmsg := msg.(type) { |
||||||
|
case *message.Video: |
||||||
|
if startTime == nil { |
||||||
|
v := tmsg.DTS |
||||||
|
startTime = &v |
||||||
|
} |
||||||
|
|
||||||
|
if tmsg.Type == message.VideoTypeConfig { |
||||||
|
if videoTrack == nil { |
||||||
|
var err error |
||||||
|
videoTrack, err = trackFromH264DecoderConfig(tmsg.Payload) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
// stop the analysis if both tracks are found
|
||||||
|
if videoTrack != nil && audioTrack != nil { |
||||||
|
return videoTrack, audioTrack, nil |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (tmsg.DTS - *startTime) >= 1*time.Second { |
||||||
|
break outer |
||||||
|
} |
||||||
|
|
||||||
|
case *message.Audio: |
||||||
|
if startTime == nil { |
||||||
|
v := tmsg.DTS |
||||||
|
startTime = &v |
||||||
|
} |
||||||
|
|
||||||
|
if tmsg.AACType == message.AudioAACTypeConfig { |
||||||
|
if audioTrack == nil { |
||||||
|
var err error |
||||||
|
audioTrack, err = trackFromAACDecoderConfig(tmsg.Payload) |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
// stop the analysis if both tracks are found
|
||||||
|
if videoTrack != nil && audioTrack != nil { |
||||||
|
return videoTrack, audioTrack, nil |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (tmsg.DTS - *startTime) >= 1*time.Second { |
||||||
|
break outer |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
var err error |
||||||
|
msg, err = r.Read() |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if videoTrack == nil && audioTrack == nil { |
||||||
|
return nil, nil, fmt.Errorf("no tracks found") |
||||||
|
} |
||||||
|
|
||||||
|
return videoTrack, audioTrack, nil |
||||||
|
} |
||||||
|
|
||||||
|
// Read reads track informations.
|
||||||
|
// It returns the video track and the audio track.
|
||||||
|
func Read(r *message.ReadWriter) (formats.Format, formats.Format, error) { |
||||||
|
msg, err := func() (message.Message, error) { |
||||||
|
for { |
||||||
|
msg, err := r.Read() |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
|
||||||
|
// skip play start and data start
|
||||||
|
if cmd, ok := msg.(*message.CommandAMF0); ok && cmd.Name == "onStatus" { |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
// skip RtmpSampleAccess
|
||||||
|
if data, ok := msg.(*message.DataAMF0); ok && len(data.Payload) >= 1 { |
||||||
|
if s, ok := data.Payload[0].(string); ok && s == "|RtmpSampleAccess" { |
||||||
|
continue |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return msg, nil |
||||||
|
} |
||||||
|
}() |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
if data, ok := msg.(*message.DataAMF0); ok && len(data.Payload) >= 1 { |
||||||
|
payload := data.Payload |
||||||
|
|
||||||
|
if s, ok := payload[0].(string); ok && s == "@setDataFrame" { |
||||||
|
payload = payload[1:] |
||||||
|
} |
||||||
|
|
||||||
|
if len(payload) >= 1 { |
||||||
|
if s, ok := payload[0].(string); ok && s == "onMetaData" { |
||||||
|
videoTrack, audioTrack, err := readTracksFromMetadata(r, payload[1:]) |
||||||
|
if err != nil { |
||||||
|
if err == errEmptyMetadata { |
||||||
|
msg, err := r.Read() |
||||||
|
if err != nil { |
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
return readTracksFromMessages(r, msg) |
||||||
|
} |
||||||
|
|
||||||
|
return nil, nil, err |
||||||
|
} |
||||||
|
|
||||||
|
return videoTrack, audioTrack, nil |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return readTracksFromMessages(r, msg) |
||||||
|
} |
||||||
@ -0,0 +1,484 @@ |
|||||||
|
package tracks |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"testing" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/bluenviron/gortsplib/v3/pkg/formats" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/h264" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio" |
||||||
|
"github.com/notedit/rtmp/format/flv/flvio" |
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/bytecounter" |
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/h264conf" |
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/message" |
||||||
|
) |
||||||
|
|
||||||
|
func TestRead(t *testing.T) { |
||||||
|
sps := []byte{ |
||||||
|
0x67, 0x64, 0x00, 0x0c, 0xac, 0x3b, 0x50, 0xb0, |
||||||
|
0x4b, 0x42, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, |
||||||
|
0x00, 0x03, 0x00, 0x3d, 0x08, |
||||||
|
} |
||||||
|
|
||||||
|
pps := []byte{ |
||||||
|
0x68, 0xee, 0x3c, 0x80, |
||||||
|
} |
||||||
|
|
||||||
|
for _, ca := range []struct { |
||||||
|
name string |
||||||
|
videoTrack formats.Format |
||||||
|
audioTrack formats.Format |
||||||
|
}{ |
||||||
|
{ |
||||||
|
"video+audio", |
||||||
|
&formats.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
PacketizationMode: 1, |
||||||
|
}, |
||||||
|
&formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"video", |
||||||
|
&formats.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
PacketizationMode: 1, |
||||||
|
}, |
||||||
|
nil, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"metadata without codec id", |
||||||
|
&formats.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
PacketizationMode: 1, |
||||||
|
}, |
||||||
|
&formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"missing metadata, video+audio", |
||||||
|
&formats.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
PacketizationMode: 1, |
||||||
|
}, |
||||||
|
&formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"missing metadata, audio", |
||||||
|
nil, |
||||||
|
&formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
"obs studio pre 29.1 h265", |
||||||
|
&formats.H265{ |
||||||
|
PayloadTyp: 96, |
||||||
|
VPS: []byte{ |
||||||
|
0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x40, |
||||||
|
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, |
||||||
|
0x03, 0x00, 0x00, 0x03, 0x00, 0x7b, 0xac, 0x09, |
||||||
|
}, |
||||||
|
SPS: []byte{ |
||||||
|
0x42, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x03, |
||||||
|
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, |
||||||
|
0x03, 0x00, 0x7b, 0xa0, 0x03, 0xc0, 0x80, 0x11, |
||||||
|
0x07, 0xcb, 0x96, 0xb4, 0xa4, 0x25, 0x92, 0xe3, |
||||||
|
0x01, 0x6a, 0x02, 0x02, 0x02, 0x08, 0x00, 0x00, |
||||||
|
0x03, 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0xe3, |
||||||
|
0x00, 0x2e, 0xf2, 0x88, 0x00, 0x09, 0x89, 0x60, |
||||||
|
0x00, 0x04, 0xc4, 0xb4, 0x20, |
||||||
|
}, |
||||||
|
PPS: []byte{ |
||||||
|
0x44, 0x01, 0xc0, 0xf7, 0xc0, 0xcc, 0x90, |
||||||
|
}, |
||||||
|
}, |
||||||
|
&formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
}, |
||||||
|
}, |
||||||
|
} { |
||||||
|
t.Run(ca.name, func(t *testing.T) { |
||||||
|
var buf bytes.Buffer |
||||||
|
bc := bytecounter.NewReadWriter(&buf) |
||||||
|
mrw := message.NewReadWriter(bc, true) |
||||||
|
|
||||||
|
switch ca.name { |
||||||
|
case "video+audio": |
||||||
|
err := mrw.Write(&message.DataAMF0{ |
||||||
|
ChunkStreamID: 4, |
||||||
|
MessageStreamID: 1, |
||||||
|
Payload: []interface{}{ |
||||||
|
"@setDataFrame", |
||||||
|
"onMetaData", |
||||||
|
flvio.AMFMap{ |
||||||
|
{ |
||||||
|
K: "videodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "videocodecid", |
||||||
|
V: float64(message.CodecH264), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiocodecid", |
||||||
|
V: float64(message.CodecMPEG4Audio), |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
buf, _ := h264conf.Conf{ |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
}.Marshal() |
||||||
|
|
||||||
|
err = mrw.Write(&message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeConfig, |
||||||
|
Payload: buf, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
enc, err := mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}.Marshal() |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
case "video": |
||||||
|
err := mrw.Write(&message.DataAMF0{ |
||||||
|
ChunkStreamID: 4, |
||||||
|
MessageStreamID: 1, |
||||||
|
Payload: []interface{}{ |
||||||
|
"@setDataFrame", |
||||||
|
"onMetaData", |
||||||
|
flvio.AMFMap{ |
||||||
|
{ |
||||||
|
K: "videodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "videocodecid", |
||||||
|
V: float64(message.CodecH264), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiocodecid", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
buf, _ := h264conf.Conf{ |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
}.Marshal() |
||||||
|
|
||||||
|
err = mrw.Write(&message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeConfig, |
||||||
|
Payload: buf, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
case "metadata without codec id": |
||||||
|
err := mrw.Write(&message.DataAMF0{ |
||||||
|
ChunkStreamID: 4, |
||||||
|
MessageStreamID: 1, |
||||||
|
Payload: []interface{}{ |
||||||
|
"@setDataFrame", |
||||||
|
"onMetaData", |
||||||
|
flvio.AMFMap{ |
||||||
|
{ |
||||||
|
K: "width", |
||||||
|
V: float64(2688), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "height", |
||||||
|
V: float64(1520), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "framerate", |
||||||
|
V: float64(0o25), |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
buf, _ := h264conf.Conf{ |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
}.Marshal() |
||||||
|
|
||||||
|
err = mrw.Write(&message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeConfig, |
||||||
|
Payload: buf, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
enc, err := mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}.Marshal() |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
case "missing metadata, video+audio": |
||||||
|
buf, _ := h264conf.Conf{ |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
}.Marshal() |
||||||
|
|
||||||
|
err := mrw.Write(&message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeConfig, |
||||||
|
Payload: buf, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
enc, err := mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}.Marshal() |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
case "missing metadata, audio": |
||||||
|
enc, err := mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}.Marshal() |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
DTS: 1 * time.Second, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
case "obs studio pre 29.1 h265": |
||||||
|
err := mrw.Write(&message.DataAMF0{ |
||||||
|
ChunkStreamID: 4, |
||||||
|
MessageStreamID: 1, |
||||||
|
Payload: []interface{}{ |
||||||
|
"@setDataFrame", |
||||||
|
"onMetaData", |
||||||
|
flvio.AMFMap{ |
||||||
|
{ |
||||||
|
K: "videodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "videocodecid", |
||||||
|
V: float64(message.CodecH264), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiocodecid", |
||||||
|
V: float64(message.CodecMPEG4Audio), |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
avcc, err := h264.AVCCMarshal([][]byte{ |
||||||
|
{ // VPS
|
||||||
|
0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x40, |
||||||
|
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, |
||||||
|
0x03, 0x00, 0x00, 0x03, 0x00, 0x7b, 0xac, 0x09, |
||||||
|
}, |
||||||
|
{ // SPS
|
||||||
|
0x42, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x03, |
||||||
|
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, |
||||||
|
0x03, 0x00, 0x7b, 0xa0, 0x03, 0xc0, 0x80, 0x11, |
||||||
|
0x07, 0xcb, 0x96, 0xb4, 0xa4, 0x25, 0x92, 0xe3, |
||||||
|
0x01, 0x6a, 0x02, 0x02, 0x02, 0x08, 0x00, 0x00, |
||||||
|
0x03, 0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0xe3, |
||||||
|
0x00, 0x2e, 0xf2, 0x88, 0x00, 0x09, 0x89, 0x60, |
||||||
|
0x00, 0x04, 0xc4, 0xb4, 0x20, |
||||||
|
}, |
||||||
|
{ |
||||||
|
// PPS
|
||||||
|
0x44, 0x01, 0xc0, 0xf7, 0xc0, 0xcc, 0x90, |
||||||
|
}, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeAU, |
||||||
|
Payload: avcc, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
enc, err := mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}.Marshal() |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
err = mrw.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
}) |
||||||
|
require.NoError(t, err) |
||||||
|
} |
||||||
|
|
||||||
|
videoTrack, audioTrack, err := Read(mrw) |
||||||
|
require.NoError(t, err) |
||||||
|
require.Equal(t, ca.videoTrack, videoTrack) |
||||||
|
require.Equal(t, ca.audioTrack, audioTrack) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,107 @@ |
|||||||
|
package tracks |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/bluenviron/gortsplib/v3/pkg/formats" |
||||||
|
"github.com/notedit/rtmp/format/flv/flvio" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/h264conf" |
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/message" |
||||||
|
) |
||||||
|
|
||||||
|
// Write writes track informations.
|
||||||
|
func Write(w *message.ReadWriter, videoTrack formats.Format, audioTrack formats.Format) error { |
||||||
|
err := w.Write(&message.DataAMF0{ |
||||||
|
ChunkStreamID: 4, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Payload: []interface{}{ |
||||||
|
"@setDataFrame", |
||||||
|
"onMetaData", |
||||||
|
flvio.AMFMap{ |
||||||
|
{ |
||||||
|
K: "videodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "videocodecid", |
||||||
|
V: func() float64 { |
||||||
|
switch videoTrack.(type) { |
||||||
|
case *formats.H264: |
||||||
|
return message.CodecH264 |
||||||
|
|
||||||
|
default: |
||||||
|
return 0 |
||||||
|
} |
||||||
|
}(), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiodatarate", |
||||||
|
V: float64(0), |
||||||
|
}, |
||||||
|
{ |
||||||
|
K: "audiocodecid", |
||||||
|
V: func() float64 { |
||||||
|
switch audioTrack.(type) { |
||||||
|
case *formats.MPEG2Audio: |
||||||
|
return message.CodecMPEG2Audio |
||||||
|
|
||||||
|
case *formats.MPEG4Audio: |
||||||
|
return message.CodecMPEG4Audio |
||||||
|
|
||||||
|
default: |
||||||
|
return 0 |
||||||
|
} |
||||||
|
}(), |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if videoTrack, ok := videoTrack.(*formats.H264); ok { |
||||||
|
// write decoder config only if SPS and PPS are available.
|
||||||
|
// if they're not available yet, they're sent later.
|
||||||
|
if sps, pps := videoTrack.SafeParams(); sps != nil && pps != nil { |
||||||
|
buf, _ := h264conf.Conf{ |
||||||
|
SPS: sps, |
||||||
|
PPS: pps, |
||||||
|
}.Marshal() |
||||||
|
|
||||||
|
err = w.Write(&message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeConfig, |
||||||
|
Payload: buf, |
||||||
|
}) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if mpeg4audioTrack, ok := audioTrack.(*formats.MPEG4Audio); ok { |
||||||
|
enc, err := mpeg4audioTrack.Config.Marshal() |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
err = w.Write(&message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: enc, |
||||||
|
}) |
||||||
|
if err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
||||||
@ -0,0 +1,96 @@ |
|||||||
|
package tracks |
||||||
|
|
||||||
|
import ( |
||||||
|
"bytes" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/bluenviron/gortsplib/v3/pkg/formats" |
||||||
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio" |
||||||
|
"github.com/notedit/rtmp/format/flv/flvio" |
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
|
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/bytecounter" |
||||||
|
"github.com/aler9/mediamtx/internal/rtmp/message" |
||||||
|
) |
||||||
|
|
||||||
|
func TestWrite(t *testing.T) { |
||||||
|
var buf bytes.Buffer |
||||||
|
bc := bytecounter.NewReadWriter(&buf) |
||||||
|
mrw := message.NewReadWriter(bc, true) |
||||||
|
|
||||||
|
videoTrack := &formats.H264{ |
||||||
|
PayloadTyp: 96, |
||||||
|
SPS: []byte{ |
||||||
|
0x67, 0x64, 0x00, 0x0c, 0xac, 0x3b, 0x50, 0xb0, |
||||||
|
0x4b, 0x42, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00, |
||||||
|
0x00, 0x03, 0x00, 0x3d, 0x08, |
||||||
|
}, |
||||||
|
PPS: []byte{ |
||||||
|
0x68, 0xee, 0x3c, 0x80, |
||||||
|
}, |
||||||
|
PacketizationMode: 1, |
||||||
|
} |
||||||
|
|
||||||
|
audioTrack := &formats.MPEG4Audio{ |
||||||
|
PayloadTyp: 96, |
||||||
|
Config: &mpeg4audio.Config{ |
||||||
|
Type: 2, |
||||||
|
SampleRate: 44100, |
||||||
|
ChannelCount: 2, |
||||||
|
}, |
||||||
|
SizeLength: 13, |
||||||
|
IndexLength: 3, |
||||||
|
IndexDeltaLength: 3, |
||||||
|
} |
||||||
|
|
||||||
|
err := Write(mrw, videoTrack, audioTrack) |
||||||
|
require.NoError(t, err) |
||||||
|
|
||||||
|
msg, err := mrw.Read() |
||||||
|
require.NoError(t, err) |
||||||
|
require.Equal(t, &message.DataAMF0{ |
||||||
|
ChunkStreamID: 4, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Payload: []interface{}{ |
||||||
|
"@setDataFrame", |
||||||
|
"onMetaData", |
||||||
|
flvio.AMFMap{ |
||||||
|
{K: "videodatarate", V: float64(0)}, |
||||||
|
{K: "videocodecid", V: float64(7)}, |
||||||
|
{K: "audiodatarate", V: float64(0)}, |
||||||
|
{K: "audiocodecid", V: float64(10)}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, msg) |
||||||
|
|
||||||
|
msg, err = mrw.Read() |
||||||
|
require.NoError(t, err) |
||||||
|
require.Equal(t, &message.Video{ |
||||||
|
ChunkStreamID: message.VideoChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecH264, |
||||||
|
IsKeyFrame: true, |
||||||
|
Type: message.VideoTypeConfig, |
||||||
|
Payload: []byte{ |
||||||
|
0x1, 0x64, 0x0, |
||||||
|
0xc, 0xff, 0xe1, 0x0, 0x15, 0x67, 0x64, 0x0, |
||||||
|
0xc, 0xac, 0x3b, 0x50, 0xb0, 0x4b, 0x42, 0x0, |
||||||
|
0x0, 0x3, 0x0, 0x2, 0x0, 0x0, 0x3, 0x0, |
||||||
|
0x3d, 0x8, 0x1, 0x0, 0x4, 0x68, 0xee, 0x3c, |
||||||
|
0x80, |
||||||
|
}, |
||||||
|
}, msg) |
||||||
|
|
||||||
|
msg, err = mrw.Read() |
||||||
|
require.NoError(t, err) |
||||||
|
require.Equal(t, &message.Audio{ |
||||||
|
ChunkStreamID: message.AudioChunkStreamID, |
||||||
|
MessageStreamID: 0x1000000, |
||||||
|
Codec: message.CodecMPEG4Audio, |
||||||
|
Rate: flvio.SOUND_44Khz, |
||||||
|
Depth: flvio.SOUND_16BIT, |
||||||
|
Channels: flvio.SOUND_STEREO, |
||||||
|
AACType: message.AudioAACTypeConfig, |
||||||
|
Payload: []byte{0x12, 0x10}, |
||||||
|
}, msg) |
||||||
|
} |
||||||
Loading…
Reference in new issue