Skip to content

Commit

Permalink
Add support ADTS to magic producer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Nov 17, 2023
1 parent b367ffe commit 33512e7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/aac/adts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
73 changes: 73 additions & 0 deletions pkg/aac/producer.go
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 4 additions & 0 deletions pkg/magic/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 33512e7

Please sign in to comment.