move units into dedicated package (#2245)

needed by #2244
This commit is contained in:
Alessandro Ros 2023-08-25 18:11:02 +02:00 committed by GitHub
parent 23ddaac481
commit e0fb11040e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 390 additions and 318 deletions

View file

@ -9,15 +9,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitAV1 is an AV1 data unit.
type UnitAV1 struct {
BaseUnit
PTS time.Duration
TU [][]byte
}
type formatProcessorAV1 struct {
udpMaxPayloadSize int
format *formats.AV1
@ -56,8 +50,8 @@ func (t *formatProcessorAV1) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorAV1) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitAV1)
func (t *formatProcessorAV1) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.AV1)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -108,9 +102,9 @@ func (t *formatProcessorAV1) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
func (t *formatProcessorAV1) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitAV1{
BaseUnit: BaseUnit{
func (t *formatProcessorAV1) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.AV1{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -8,13 +8,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitGeneric is a generic data unit.
type UnitGeneric struct {
BaseUnit
}
type formatProcessorGeneric struct {
udpMaxPayloadSize int
}
@ -34,8 +30,8 @@ func newGeneric(
}, nil
}
func (t *formatProcessorGeneric) Process(unit Unit, _ bool) error {
tunit := unit.(*UnitGeneric)
func (t *formatProcessorGeneric) Process(u unit.Unit, _ bool) error {
tunit := u.(*unit.Generic)
pkt := tunit.RTPPackets[0]
@ -51,9 +47,9 @@ func (t *formatProcessorGeneric) Process(unit Unit, _ bool) error {
return nil
}
func (t *formatProcessorGeneric) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitGeneric{
BaseUnit: BaseUnit{
func (t *formatProcessorGeneric) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.Generic{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -6,6 +6,8 @@ import (
"github.com/bluenviron/gortsplib/v3/pkg/formats"
"github.com/pion/rtp"
"github.com/stretchr/testify/require"
"github.com/bluenviron/mediamtx/internal/unit"
)
func TestGenericRemovePadding(t *testing.T) {
@ -33,8 +35,8 @@ func TestGenericRemovePadding(t *testing.T) {
PaddingSize: 20,
}
err = p.Process(&UnitGeneric{
BaseUnit: BaseUnit{
err = p.Process(&unit.Generic{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
},
}, false)

View file

@ -10,6 +10,7 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// extract SPS and PPS without decoding RTP packets
@ -69,13 +70,6 @@ func rtpH264ExtractSPSPPS(pkt *rtp.Packet) ([]byte, []byte) {
}
}
// UnitH264 is a H264 data unit.
type UnitH264 struct {
BaseUnit
PTS time.Duration
AU [][]byte
}
type formatProcessorH264 struct {
udpMaxPayloadSize int
format *formats.H264
@ -230,8 +224,8 @@ func (t *formatProcessorH264) remuxAccessUnit(au [][]byte) [][]byte {
return filteredNALUs
}
func (t *formatProcessorH264) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitH264)
func (t *formatProcessorH264) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.H264)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -304,9 +298,9 @@ func (t *formatProcessorH264) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
func (t *formatProcessorH264) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitH264{
BaseUnit: BaseUnit{
func (t *formatProcessorH264) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.H264{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -8,6 +8,8 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/h264"
"github.com/pion/rtp"
"github.com/stretchr/testify/require"
"github.com/bluenviron/mediamtx/internal/unit"
)
func TestH264DynamicParams(t *testing.T) {
@ -25,8 +27,8 @@ func TestH264DynamicParams(t *testing.T) {
pkts, err := enc.Encode([][]byte{{byte(h264.NALUTypeIDR)}}, 0)
require.NoError(t, err)
data := &UnitH264{
BaseUnit: BaseUnit{
data := &unit.H264{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}
@ -40,8 +42,8 @@ func TestH264DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{7, 4, 5, 6}}, 0) // SPS
require.NoError(t, err)
err = p.Process(&UnitH264{
BaseUnit: BaseUnit{
err = p.Process(&unit.H264{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}, false)
@ -50,8 +52,8 @@ func TestH264DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{8, 1}}, 0) // PPS
require.NoError(t, err)
err = p.Process(&UnitH264{
BaseUnit: BaseUnit{
err = p.Process(&unit.H264{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}, false)
@ -63,8 +65,8 @@ func TestH264DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{byte(h264.NALUTypeIDR)}}, 0)
require.NoError(t, err)
data = &UnitH264{
BaseUnit: BaseUnit{
data = &unit.H264{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}
@ -129,8 +131,8 @@ func TestH264OversizedPackets(t *testing.T) {
Payload: []byte{0x1c, 0b01000000, 0x01, 0x02, 0x03, 0x04},
},
} {
data := &UnitH264{
BaseUnit: BaseUnit{
data := &unit.H264{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
},
}
@ -192,7 +194,7 @@ func TestH264EmptyPacket(t *testing.T) {
p, err := New(1472, forma, true, nil)
require.NoError(t, err)
unit := &UnitH264{
unit := &unit.H264{
AU: [][]byte{
{0x07, 0x01, 0x02, 0x03}, // SPS
{0x08, 0x01, 0x02}, // PPS

View file

@ -10,6 +10,7 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// extract VPS, SPS and PPS without decoding RTP packets
@ -76,13 +77,6 @@ func rtpH265ExtractVPSSPSPPS(pkt *rtp.Packet) ([]byte, []byte, []byte) {
}
}
// UnitH265 is a H265 data unit.
type UnitH265 struct {
BaseUnit
PTS time.Duration
AU [][]byte
}
type formatProcessorH265 struct {
udpMaxPayloadSize int
format *formats.H265
@ -252,8 +246,8 @@ func (t *formatProcessorH265) remuxAccessUnit(au [][]byte) [][]byte {
return filteredNALUs
}
func (t *formatProcessorH265) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitH265)
func (t *formatProcessorH265) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.H265)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -326,9 +320,9 @@ func (t *formatProcessorH265) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
func (t *formatProcessorH265) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitH265{
BaseUnit: BaseUnit{
func (t *formatProcessorH265) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -8,6 +8,8 @@ import (
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
"github.com/pion/rtp"
"github.com/stretchr/testify/require"
"github.com/bluenviron/mediamtx/internal/unit"
)
func TestH265DynamicParams(t *testing.T) {
@ -24,8 +26,8 @@ func TestH265DynamicParams(t *testing.T) {
pkts, err := enc.Encode([][]byte{{byte(h265.NALUType_CRA_NUT) << 1, 0}}, 0)
require.NoError(t, err)
data := &UnitH265{
BaseUnit: BaseUnit{
data := &unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}
@ -39,8 +41,8 @@ func TestH265DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_VPS_NUT) << 1, 1, 2, 3}}, 0)
require.NoError(t, err)
err = p.Process(&UnitH265{
BaseUnit: BaseUnit{
err = p.Process(&unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}, false)
@ -49,8 +51,8 @@ func TestH265DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_SPS_NUT) << 1, 4, 5, 6}}, 0)
require.NoError(t, err)
err = p.Process(&UnitH265{
BaseUnit: BaseUnit{
err = p.Process(&unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}, false)
@ -59,8 +61,8 @@ func TestH265DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_PPS_NUT) << 1, 7, 8, 9}}, 0)
require.NoError(t, err)
err = p.Process(&UnitH265{
BaseUnit: BaseUnit{
err = p.Process(&unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}, false)
@ -73,8 +75,8 @@ func TestH265DynamicParams(t *testing.T) {
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_CRA_NUT) << 1, 0}}, 0)
require.NoError(t, err)
data = &UnitH265{
BaseUnit: BaseUnit{
data = &unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkts[0]},
},
}
@ -128,8 +130,8 @@ func TestH265OversizedPackets(t *testing.T) {
Payload: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 2000/4),
},
} {
data := &UnitH265{
BaseUnit: BaseUnit{
data := &unit.H265{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
},
}
@ -190,7 +192,7 @@ func TestH265EmptyPacket(t *testing.T) {
p, err := New(1472, forma, true, nil)
require.NoError(t, err)
unit := &UnitH265{
unit := &unit.H265{
AU: [][]byte{
{byte(h265.NALUType_VPS_NUT) << 1, 10, 11, 12}, // VPS
{byte(h265.NALUType_SPS_NUT) << 1, 13, 14, 15}, // SPS

View file

@ -9,15 +9,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitMPEG1Audio is a MPEG-1/2 Audio data unit.
type UnitMPEG1Audio struct {
BaseUnit
PTS time.Duration
Frames [][]byte
}
type formatProcessorMPEG1Audio struct {
udpMaxPayloadSize int
format *formats.MPEG1Audio
@ -53,8 +47,8 @@ func (t *formatProcessorMPEG1Audio) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorMPEG1Audio) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitMPEG1Audio)
func (t *formatProcessorMPEG1Audio) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.MPEG1Audio)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -104,9 +98,9 @@ func (t *formatProcessorMPEG1Audio) Process(unit Unit, hasNonRTSPReaders bool) e
return nil
}
func (t *formatProcessorMPEG1Audio) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitMPEG1Audio{
BaseUnit: BaseUnit{
func (t *formatProcessorMPEG1Audio) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.MPEG1Audio{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -9,15 +9,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitMPEG4AudioGeneric is a MPEG-4 Audio data unit.
type UnitMPEG4AudioGeneric struct {
BaseUnit
PTS time.Duration
AUs [][]byte
}
type formatProcessorMPEG4AudioGeneric struct {
udpMaxPayloadSize int
format *formats.MPEG4Audio
@ -58,8 +52,8 @@ func (t *formatProcessorMPEG4AudioGeneric) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorMPEG4AudioGeneric) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitMPEG4AudioGeneric)
func (t *formatProcessorMPEG4AudioGeneric) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.MPEG4AudioGeneric)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -109,9 +103,9 @@ func (t *formatProcessorMPEG4AudioGeneric) Process(unit Unit, hasNonRTSPReaders
return nil
}
func (t *formatProcessorMPEG4AudioGeneric) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitMPEG4AudioGeneric{
BaseUnit: BaseUnit{
func (t *formatProcessorMPEG4AudioGeneric) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.MPEG4AudioGeneric{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -9,15 +9,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitMPEG4AudioLATM is a MPEG-4 Audio data unit.
type UnitMPEG4AudioLATM struct {
BaseUnit
PTS time.Duration
AU []byte
}
type formatProcessorMPEG4AudioLATM struct {
udpMaxPayloadSize int
format *formats.MPEG4AudioLATM
@ -54,8 +48,8 @@ func (t *formatProcessorMPEG4AudioLATM) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorMPEG4AudioLATM) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitMPEG4AudioLATM)
func (t *formatProcessorMPEG4AudioLATM) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.MPEG4AudioLATM)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -105,9 +99,9 @@ func (t *formatProcessorMPEG4AudioLATM) Process(unit Unit, hasNonRTSPReaders boo
return nil
}
func (t *formatProcessorMPEG4AudioLATM) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitMPEG4AudioLATM{
BaseUnit: BaseUnit{
func (t *formatProcessorMPEG4AudioLATM) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.MPEG4AudioLATM{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -10,15 +10,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitOpus is a Opus data unit.
type UnitOpus struct {
BaseUnit
PTS time.Duration
Packets [][]byte
}
type formatProcessorOpus struct {
udpMaxPayloadSize int
format *formats.Opus
@ -56,8 +50,8 @@ func (t *formatProcessorOpus) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorOpus) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitOpus)
func (t *formatProcessorOpus) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.Opus)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -111,9 +105,9 @@ func (t *formatProcessorOpus) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
func (t *formatProcessorOpus) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitOpus{
BaseUnit: BaseUnit{
func (t *formatProcessorOpus) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.Opus{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -8,15 +8,16 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// Processor cleans and normalizes streams.
type Processor interface {
// cleans and normalizes a data unit.
Process(Unit, bool) error
Process(unit.Unit, bool) error
// wraps a RTP packet into a Unit.
UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit
UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit
}
// New allocates a Processor.

View file

@ -1,32 +0,0 @@
package formatprocessor
import (
"time"
"github.com/pion/rtp"
)
// BaseUnit contains fields shared across all units.
type BaseUnit struct {
RTPPackets []*rtp.Packet
NTP time.Time
}
// GetRTPPackets implements Unit.
func (u *BaseUnit) GetRTPPackets() []*rtp.Packet {
return u.RTPPackets
}
// GetNTP implements Unit.
func (u *BaseUnit) GetNTP() time.Time {
return u.NTP
}
// Unit is the elementary data unit routed across the server.
type Unit interface {
// returns RTP packets contained into the unit.
GetRTPPackets() []*rtp.Packet
// returns the NTP timestamp of the unit.
GetNTP() time.Time
}

View file

@ -9,15 +9,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitVP8 is a VP8 data unit.
type UnitVP8 struct {
BaseUnit
PTS time.Duration
Frame []byte
}
type formatProcessorVP8 struct {
udpMaxPayloadSize int
format *formats.VP8
@ -54,8 +48,8 @@ func (t *formatProcessorVP8) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorVP8) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitVP8)
func (t *formatProcessorVP8) Process(y unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := y.(*unit.VP8)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -105,9 +99,9 @@ func (t *formatProcessorVP8) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
func (t *formatProcessorVP8) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitVP8{
BaseUnit: BaseUnit{
func (t *formatProcessorVP8) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.VP8{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},

View file

@ -9,15 +9,9 @@ import (
"github.com/pion/rtp"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/unit"
)
// UnitVP9 is a VP9 data unit.
type UnitVP9 struct {
BaseUnit
PTS time.Duration
Frame []byte
}
type formatProcessorVP9 struct {
udpMaxPayloadSize int
format *formats.VP9
@ -54,8 +48,8 @@ func (t *formatProcessorVP9) createEncoder() error {
return t.encoder.Init()
}
func (t *formatProcessorVP9) Process(unit Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := unit.(*UnitVP9)
func (t *formatProcessorVP9) Process(u unit.Unit, hasNonRTSPReaders bool) error { //nolint:dupl
tunit := u.(*unit.VP9)
if tunit.RTPPackets != nil {
pkt := tunit.RTPPackets[0]
@ -105,9 +99,9 @@ func (t *formatProcessorVP9) Process(unit Unit, hasNonRTSPReaders bool) error {
return nil
}
func (t *formatProcessorVP9) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) Unit {
return &UnitVP9{
BaseUnit: BaseUnit{
func (t *formatProcessorVP9) UnitForRTPPacket(pkt *rtp.Packet, ntp time.Time) unit.Unit {
return &unit.VP9{
Base: unit.Base{
RTPPackets: []*rtp.Packet{pkt},
NTP: ntp,
},