Update FLV muxer

This commit is contained in:
Alex X
2023-09-17 14:07:55 +03:00
parent 9f1e33e0c6
commit 4dffceaf7e
2 changed files with 32 additions and 13 deletions
+23
View File
@@ -199,6 +199,8 @@ func (a *AMF) writeKV(obj map[string]any) {
a.WriteString(v)
case int:
a.WriteNumber(float64(v))
case uint16:
a.WriteNumber(float64(v))
case uint32:
a.WriteNumber(float64(v))
case float64:
@@ -214,3 +216,24 @@ func (a *AMF) writeKV(obj map[string]any) {
func (a *AMF) WriteNull() {
a.buf = append(a.buf, TypeNull)
}
func EncodeItems(items ...any) []byte {
a := &AMF{}
for _, item := range items {
switch v := item.(type) {
case float64:
a.WriteNumber(v)
case int:
a.WriteNumber(float64(v))
case string:
a.WriteString(v)
case map[string]any:
a.WriteObject(v)
case nil:
a.WriteNull()
default:
panic(v)
}
}
return a.Bytes()
}
+9 -13
View File
@@ -45,10 +45,8 @@ func (m *Muxer) GetInit() []byte {
}
}
wr := amf.NewWriter()
wr.WriteString("onMetaData")
wr.WriteEcmaArray(obj)
b = append(b, EncodePacket(TagData, 0, wr.Bytes())...)
data := amf.EncodeItems("@setDataFrame", "onMetaData", obj)
b = append(b, EncodeTag(TagData, 0, data)...)
for _, codec := range m.codecs {
switch codec.Name {
@@ -62,16 +60,14 @@ func (m *Muxer) GetInit() []byte {
}
config := h264.EncodeConfig(sps, pps)
payload := append(encodeAVData(codec, 0), config...)
b = append(b, EncodePacket(TagVideo, 0, payload)...)
video := append(encodeAVData(codec, 0), config...)
b = append(b, EncodeTag(TagVideo, 0, video)...)
case core.CodecAAC:
s := core.Between(codec.FmtpLine, "config=", ";")
config, _ := hex.DecodeString(s)
payload := append(
encodeAVData(codec, 0), config...,
)
b = append(b, EncodePacket(TagAudio, 0, payload)...)
audio := append(encodeAVData(codec, 0), config...)
b = append(b, EncodeTag(TagAudio, 0, audio)...)
}
}
@@ -102,7 +98,7 @@ func (m *Muxer) GetPayloader(codec *core.Codec) func(packet *rtp.Packet) []byte
}
timeMS := (packet.Timestamp - ts0) / k
return EncodePacket(TagVideo, timeMS, buf)
return EncodeTag(TagVideo, timeMS, buf)
}
case core.CodecAAC:
@@ -116,14 +112,14 @@ func (m *Muxer) GetPayloader(codec *core.Codec) func(packet *rtp.Packet) []byte
}
timeMS := (packet.Timestamp - ts0) / k
return EncodePacket(TagAudio, timeMS, buf)
return EncodeTag(TagAudio, timeMS, buf)
}
}
return nil
}
func EncodePacket(tagType byte, timeMS uint32, payload []byte) []byte {
func EncodeTag(tagType byte, timeMS uint32, payload []byte) []byte {
payloadSize := uint32(len(payload))
tagSize := payloadSize + 11