support publishing, reading and proxying MPEG-2 audio (MP3) tracks with RTMP (#1102) (#1736)

This commit is contained in:
Alessandro Ros 2023-04-25 18:13:51 +02:00 committed by GitHub
parent c314d77596
commit 2d17dff3b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 727 additions and 487 deletions

View file

@ -11,7 +11,7 @@ import (
func TestGenericRemovePadding(t *testing.T) {
forma := &formats.Generic{
PayloadTyp: 96,
RTPMap: "private/90000",
RTPMa: "private/90000",
}
forma.Init()

View file

@ -104,7 +104,12 @@ func newH264(
}
if allocateEncoder {
t.encoder = forma.CreateEncoder()
t.encoder = &rtph264.Encoder{
PayloadMaxSize: udpMaxPayloadSize - 12,
PayloadType: forma.PayloadTyp,
PacketizationMode: forma.PacketizationMode,
}
t.encoder.Init()
}
return t, nil
@ -280,6 +285,7 @@ func (t *formatProcessorH264) Process(unit Unit, hasNonRTSPReaders bool) error {
tunit.AU = t.remuxAccessUnit(tunit.AU)
}
// encode into RTP
if len(tunit.AU) != 0 {
pkts, err := t.encoder.Encode(tunit.AU, tunit.PTS)
if err != nil {

View file

@ -111,7 +111,11 @@ func newH265(
}
if allocateEncoder {
t.encoder = forma.CreateEncoder()
t.encoder = &rtph265.Encoder{
PayloadMaxSize: t.udpMaxPayloadSize - 12,
PayloadType: forma.PayloadTyp,
}
t.encoder.Init()
}
return t, nil
@ -303,6 +307,7 @@ func (t *formatProcessorH265) Process(unit Unit, hasNonRTSPReaders bool) error {
tunit.AU = t.remuxAccessUnit(tunit.AU)
}
// encode into RTP
if len(tunit.AU) != 0 {
pkts, err := t.encoder.Encode(tunit.AU, tunit.PTS)
if err != nil {

View file

@ -0,0 +1,99 @@
package formatprocessor
import (
"fmt"
"time"
"github.com/bluenviron/gortsplib/v3/pkg/formats"
"github.com/bluenviron/gortsplib/v3/pkg/formats/rtpmpeg2audio"
"github.com/pion/rtp"
)
// UnitMPEG2Audio is a MPEG2-audio data unit.
type UnitMPEG2Audio struct {
RTPPackets []*rtp.Packet
NTP time.Time
PTS time.Duration
Frames [][]byte
}
// GetRTPPackets implements Unit.
func (d *UnitMPEG2Audio) GetRTPPackets() []*rtp.Packet {
return d.RTPPackets
}
// GetNTP implements Unit.
func (d *UnitMPEG2Audio) GetNTP() time.Time {
return d.NTP
}
type formatProcessorMPEG2Audio struct {
udpMaxPayloadSize int
format *formats.MPEG2Audio
encoder *rtpmpeg2audio.Encoder
decoder *rtpmpeg2audio.Decoder
}
func newMPEG2Audio(
udpMaxPayloadSize int,
forma *formats.MPEG2Audio,
allocateEncoder bool,
) (*formatProcessorMPEG2Audio, error) {
t := &formatProcessorMPEG2Audio{
udpMaxPayloadSize: udpMaxPayloadSize,
format: forma,
}
if allocateEncoder {
t.encoder = &rtpmpeg2audio.Encoder{
PayloadMaxSize: t.udpMaxPayloadSize - 12,
}
t.encoder.Init()
}
return t, nil
}
func (t *formatProcessorMPEG2Audio) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitMPEG2Audio)
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()
}
frames, pts, err := t.decoder.Decode(pkt)
if err != nil {
return err
}
tunit.Frames = frames
tunit.PTS = pts
}
// route packet as is
return nil
}
// encode into RTP
pkts, err := t.encoder.Encode(tunit.Frames, tunit.PTS)
if err != nil {
return err
}
tunit.RTPPackets = pkts
return nil
}

View file

@ -45,7 +45,15 @@ func newMPEG4Audio(
}
if allocateEncoder {
t.encoder = forma.CreateEncoder()
t.encoder = &rtpmpeg4audio.Encoder{
PayloadMaxSize: t.udpMaxPayloadSize - 12,
PayloadType: forma.PayloadTyp,
SampleRate: forma.Config.SampleRate,
SizeLength: forma.SizeLength,
IndexLength: forma.IndexLength,
IndexDeltaLength: forma.IndexDeltaLength,
}
t.encoder.Init()
}
return t, nil
@ -88,11 +96,12 @@ func (t *formatProcessorMPEG4Audio) Process(unit Unit, hasNonRTSPReaders bool) e
return nil
}
// encode into RTP
pkts, err := t.encoder.Encode(tunit.AUs, tunit.PTS)
if err != nil {
return err
}
tunit.RTPPackets = pkts
return nil
}

View file

@ -45,7 +45,12 @@ func newOpus(
}
if allocateEncoder {
t.encoder = forma.CreateEncoder()
t.encoder = &rtpsimpleaudio.Encoder{
PayloadMaxSize: t.udpMaxPayloadSize - 12,
PayloadType: forma.PayloadTyp,
SampleRate: 48000,
}
t.encoder.Init()
}
return t, nil
@ -85,11 +90,12 @@ func (t *formatProcessorOpus) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
// encode into RTP
pkt, err := t.encoder.Encode(tunit.Frame, tunit.PTS)
if err != nil {
return err
}
tunit.RTPPackets = []*rtp.Packet{pkt}
return nil
}

View file

@ -30,6 +30,9 @@ func New(
case *formats.VP9:
return newVP9(udpMaxPayloadSize, forma, generateRTPPackets)
case *formats.MPEG2Audio:
return newMPEG2Audio(udpMaxPayloadSize, forma, generateRTPPackets)
case *formats.MPEG4Audio:
return newMPEG4Audio(udpMaxPayloadSize, forma, generateRTPPackets)

View file

@ -45,7 +45,11 @@ func newVP8(
}
if allocateEncoder {
t.encoder = forma.CreateEncoder()
t.encoder = &rtpvp8.Encoder{
PayloadMaxSize: t.udpMaxPayloadSize - 12,
PayloadType: forma.PayloadTyp,
}
t.encoder.Init()
}
return t, nil
@ -88,11 +92,12 @@ func (t *formatProcessorVP8) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
// encode into RTP
pkts, err := t.encoder.Encode(tunit.Frame, tunit.PTS)
if err != nil {
return err
}
tunit.RTPPackets = pkts
return nil
}

View file

@ -45,7 +45,11 @@ func newVP9(
}
if allocateEncoder {
t.encoder = forma.CreateEncoder()
t.encoder = &rtpvp9.Encoder{
PayloadMaxSize: t.udpMaxPayloadSize - 12,
PayloadType: forma.PayloadTyp,
}
t.encoder.Init()
}
return t, nil
@ -88,11 +92,12 @@ func (t *formatProcessorVP9) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
// encode into RTP
pkts, err := t.encoder.Encode(tunit.Frame, tunit.PTS)
if err != nil {
return err
}
tunit.RTPPackets = pkts
return nil
}