mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-24 20:11:56 -08:00
This commit is contained in:
parent
c314d77596
commit
2d17dff3b5
27 changed files with 727 additions and 487 deletions
|
|
@ -11,7 +11,7 @@ import (
|
|||
func TestGenericRemovePadding(t *testing.T) {
|
||||
forma := &formats.Generic{
|
||||
PayloadTyp: 96,
|
||||
RTPMap: "private/90000",
|
||||
RTPMa: "private/90000",
|
||||
}
|
||||
forma.Init()
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
99
internal/formatprocessor/mpeg2audio.go
Normal file
99
internal/formatprocessor/mpeg2audio.go
Normal 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
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue