diff --git a/pkg/aac/adts.go b/pkg/aac/adts.go index 45925903..d5e7828e 100644 --- a/pkg/aac/adts.go +++ b/pkg/aac/adts.go @@ -10,7 +10,7 @@ import ( func IsADTS(b []byte) bool { _ = b[1] - return len(b) > 7 && b[0] == 0xFF && b[1]&0xF0 == 0xF0 + return len(b) > 7 && b[0] == 0xFF && b[1]&0xF6 == 0xF0 } func ADTSToCodec(b []byte) *core.Codec { diff --git a/pkg/aac/producer.go b/pkg/aac/producer.go new file mode 100644 index 00000000..e9be71fd --- /dev/null +++ b/pkg/aac/producer.go @@ -0,0 +1,73 @@ +package aac + +import ( + "bufio" + "encoding/binary" + "io" + + "github.com/AlexxIT/go2rtc/pkg/core" + "github.com/pion/rtp" +) + +type Producer struct { + core.SuperProducer + rd *bufio.Reader + cl io.Closer +} + +func Open(r io.Reader) (*Producer, error) { + rd := bufio.NewReader(r) + + b, err := rd.Peek(8) + if err != nil { + return nil, err + } + + codec := ADTSToCodec(b) + + prod := &Producer{rd: rd, cl: r.(io.Closer)} + prod.Type = "ADTS producer" + prod.Medias = []*core.Media{ + { + Kind: core.KindAudio, + Direction: core.DirectionRecvonly, + Codecs: []*core.Codec{codec}, + }, + } + return prod, nil +} + +func (c *Producer) Start() error { + for { + b, err := c.rd.Peek(6) + if err != nil { + return err + } + + auSize := ReadADTSSize(b) + payload := make([]byte, 2+2+auSize) + if _, err = io.ReadFull(c.rd, payload[4:]); err != nil { + return err + } + + c.Recv += int(auSize) + + if len(c.Receivers) == 0 { + continue + } + + payload[1] = 16 // header size in bits + binary.BigEndian.PutUint16(payload[2:], auSize<<3) + + pkt := &rtp.Packet{ + Header: rtp.Header{Timestamp: core.Now90000()}, + Payload: payload, + } + c.Receivers[0].WriteRTP(pkt) + } +} + +func (c *Producer) Stop() error { + _ = c.SuperProducer.Close() + return c.cl.Close() +} diff --git a/pkg/magic/producer.go b/pkg/magic/producer.go index 46002807..6bda7d14 100644 --- a/pkg/magic/producer.go +++ b/pkg/magic/producer.go @@ -6,6 +6,7 @@ import ( "errors" "io" + "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/flv" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" @@ -33,6 +34,9 @@ func Open(r io.Reader) (core.Producer, error) { case bytes.HasPrefix(b, []byte(flv.Signature)): return flv.Open(rd) + case bytes.HasPrefix(b, []byte{0xFF, 0xF1}): + return aac.Open(rd) + case bytes.HasPrefix(b, []byte("--")): return multipart.Open(rd)