2021-04-03 09:39:19 +00:00
|
|
|
package rtmp
|
2021-01-31 15:44:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
2021-08-25 17:51:59 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/rtpaac"
|
2021-01-31 15:44:32 +00:00
|
|
|
"github.com/notedit/rtmp/av"
|
2021-04-03 09:39:19 +00:00
|
|
|
nh264 "github.com/notedit/rtmp/codec/h264"
|
2021-01-31 15:44:32 +00:00
|
|
|
"github.com/notedit/rtmp/format/flv/flvio"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
codecH264 = 7
|
|
|
|
codecAAC = 10
|
|
|
|
)
|
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
// ReadMetadata extracts track informations from a connection that is publishing.
|
2021-04-05 11:35:03 +00:00
|
|
|
func (c *Conn) ReadMetadata() (*gortsplib.Track, *gortsplib.Track, error) {
|
2021-04-03 08:52:59 +00:00
|
|
|
var videoTrack *gortsplib.Track
|
|
|
|
var audioTrack *gortsplib.Track
|
2021-01-31 15:44:32 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
md, err := func() (flvio.AMFMap, error) {
|
2021-04-05 11:35:03 +00:00
|
|
|
pkt, err := c.ReadPacket()
|
2021-04-03 08:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-31 15:44:32 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
if pkt.Type != av.Metadata {
|
|
|
|
return nil, fmt.Errorf("first packet must be metadata")
|
|
|
|
}
|
2021-01-31 15:44:32 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
arr, err := flvio.ParseAMFVals(pkt.Data, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-31 15:44:32 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
if len(arr) != 1 {
|
|
|
|
return nil, fmt.Errorf("invalid metadata")
|
|
|
|
}
|
2021-01-31 15:44:32 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
ma, ok := arr[0].(flvio.AMFMap)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid metadata")
|
|
|
|
}
|
2021-01-31 15:44:32 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
return ma, nil
|
|
|
|
}()
|
2021-01-31 15:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-05 10:15:27 +00:00
|
|
|
hasVideo, err := func() (bool, error) {
|
|
|
|
v, ok := md.GetV("videocodecid")
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
2021-01-31 15:44:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:15:27 +00:00
|
|
|
switch vt := v.(type) {
|
|
|
|
case float64:
|
|
|
|
switch vt {
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
case codecH264:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case string:
|
2021-03-20 13:14:41 +00:00
|
|
|
if vt == "avc1" {
|
2021-03-05 10:15:27 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("unsupported video codec %v", v)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-01-31 15:44:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:15:27 +00:00
|
|
|
hasAudio, err := func() (bool, error) {
|
|
|
|
v, ok := md.GetV("audiocodecid")
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
2021-01-31 15:44:32 +00:00
|
|
|
}
|
2021-03-05 10:15:27 +00:00
|
|
|
|
|
|
|
switch vt := v.(type) {
|
|
|
|
case float64:
|
|
|
|
switch vt {
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
case codecAAC:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case string:
|
2021-03-20 13:14:41 +00:00
|
|
|
if vt == "mp4a" {
|
2021-03-05 10:15:27 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("unsupported audio codec %v", v)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2021-01-31 15:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !hasVideo && !hasAudio {
|
2021-08-31 08:41:20 +00:00
|
|
|
return nil, nil, fmt.Errorf("stream doesn't contain tracks with supported codecs (H264 or AAC)")
|
2021-01-31 15:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
var pkt av.Packet
|
2021-04-05 11:35:03 +00:00
|
|
|
pkt, err = c.ReadPacket()
|
2021-01-31 15:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch pkt.Type {
|
|
|
|
case av.H264DecoderConfig:
|
|
|
|
if !hasVideo {
|
|
|
|
return nil, nil, fmt.Errorf("unexpected video packet")
|
|
|
|
}
|
2021-08-25 17:51:59 +00:00
|
|
|
|
2021-01-31 15:44:32 +00:00
|
|
|
if videoTrack != nil {
|
|
|
|
return nil, nil, fmt.Errorf("video track setupped twice")
|
|
|
|
}
|
|
|
|
|
2021-04-03 09:39:19 +00:00
|
|
|
codec, err := nh264.FromDecoderConfig(pkt.Data)
|
2021-01-31 15:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:51:59 +00:00
|
|
|
videoTrack, err = gortsplib.NewTrackH264(96, &gortsplib.TrackConfigH264{SPS: codec.SPS[0], PPS: codec.PPS[0]})
|
2021-01-31 15:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case av.AACDecoderConfig:
|
|
|
|
if !hasAudio {
|
|
|
|
return nil, nil, fmt.Errorf("unexpected audio packet")
|
|
|
|
}
|
2021-08-25 17:51:59 +00:00
|
|
|
|
2021-01-31 15:44:32 +00:00
|
|
|
if audioTrack != nil {
|
|
|
|
return nil, nil, fmt.Errorf("audio track setupped twice")
|
|
|
|
}
|
|
|
|
|
2021-08-25 17:51:59 +00:00
|
|
|
var mpegConf rtpaac.MPEG4AudioConfig
|
|
|
|
err := mpegConf.Decode(pkt.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
audioTrack, err = gortsplib.NewTrackAAC(96, &gortsplib.TrackConfigAAC{
|
|
|
|
Type: int(mpegConf.Type),
|
|
|
|
SampleRate: mpegConf.SampleRate,
|
|
|
|
ChannelCount: mpegConf.ChannelCount,
|
|
|
|
AOTSpecificConfig: mpegConf.AOTSpecificConfig,
|
|
|
|
})
|
2021-01-31 15:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasVideo || videoTrack != nil) &&
|
|
|
|
(!hasAudio || audioTrack != nil) {
|
|
|
|
return videoTrack, audioTrack, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 14:06:45 +00:00
|
|
|
|
2021-04-03 08:52:59 +00:00
|
|
|
// WriteMetadata writes track informations to a connection that is reading.
|
2021-04-05 11:35:03 +00:00
|
|
|
func (c *Conn) WriteMetadata(videoTrack *gortsplib.Track, audioTrack *gortsplib.Track) error {
|
|
|
|
err := c.WritePacket(av.Packet{
|
2021-03-10 14:06:45 +00:00
|
|
|
Type: av.Metadata,
|
|
|
|
Data: flvio.FillAMF0ValMalloc(flvio.AMFMap{
|
|
|
|
{
|
|
|
|
K: "videodatarate",
|
|
|
|
V: float64(0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
K: "videocodecid",
|
|
|
|
V: func() float64 {
|
|
|
|
if videoTrack != nil {
|
|
|
|
return codecH264
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
K: "audiodatarate",
|
|
|
|
V: float64(0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
K: "audiocodecid",
|
|
|
|
V: func() float64 {
|
|
|
|
if audioTrack != nil {
|
|
|
|
return codecAAC
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
})
|
2021-04-05 11:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if videoTrack != nil {
|
2021-08-25 17:51:59 +00:00
|
|
|
conf, err := videoTrack.ExtractConfigH264()
|
2021-04-05 11:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
codec := nh264.Codec{
|
|
|
|
SPS: map[int][]byte{
|
2021-08-25 17:51:59 +00:00
|
|
|
0: conf.SPS,
|
2021-04-05 11:35:03 +00:00
|
|
|
},
|
|
|
|
PPS: map[int][]byte{
|
2021-08-25 17:51:59 +00:00
|
|
|
0: conf.PPS,
|
2021-04-05 11:35:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
b := make([]byte, 128)
|
|
|
|
var n int
|
|
|
|
codec.ToConfig(b, &n)
|
|
|
|
b = b[:n]
|
|
|
|
|
|
|
|
err = c.WritePacket(av.Packet{
|
|
|
|
Type: av.H264DecoderConfig,
|
|
|
|
Data: b,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if audioTrack != nil {
|
2021-08-25 17:51:59 +00:00
|
|
|
conf, err := audioTrack.ExtractConfigAAC()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := rtpaac.MPEG4AudioConfig{
|
|
|
|
Type: rtpaac.MPEG4AudioType(conf.Type),
|
|
|
|
SampleRate: conf.SampleRate,
|
|
|
|
ChannelCount: conf.ChannelCount,
|
|
|
|
AOTSpecificConfig: conf.AOTSpecificConfig,
|
|
|
|
}.Encode()
|
2021-04-05 11:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.WritePacket(av.Packet{
|
|
|
|
Type: av.AACDecoderConfig,
|
2021-08-25 17:51:59 +00:00
|
|
|
Data: enc,
|
2021-04-05 11:35:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|