2023-01-05 11:54:00 +00:00
|
|
|
package formatprocessor
|
2022-08-14 11:01:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-02 19:52:12 +00:00
|
|
|
"fmt"
|
2022-12-13 20:26:35 +00:00
|
|
|
"time"
|
2022-11-02 19:52:12 +00:00
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/format"
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4audio"
|
2022-12-13 20:26:35 +00:00
|
|
|
"github.com/pion/rtp"
|
2023-05-04 18:16:41 +00:00
|
|
|
|
2023-08-25 16:11:02 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/unit"
|
2022-08-14 11:01:06 +00:00
|
|
|
)
|
|
|
|
|
2023-09-21 15:21:18 +00:00
|
|
|
type formatProcessorMPEG4Audio struct {
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int
|
2023-08-26 16:54:28 +00:00
|
|
|
format *format.MPEG4Audio
|
2023-03-31 09:53:49 +00:00
|
|
|
encoder *rtpmpeg4audio.Encoder
|
|
|
|
decoder *rtpmpeg4audio.Decoder
|
2022-08-14 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 15:21:18 +00:00
|
|
|
func newMPEG4Audio(
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int,
|
2023-08-26 16:54:28 +00:00
|
|
|
forma *format.MPEG4Audio,
|
2023-05-04 18:16:41 +00:00
|
|
|
generateRTPPackets bool,
|
2023-09-21 15:21:18 +00:00
|
|
|
) (*formatProcessorMPEG4Audio, error) {
|
|
|
|
t := &formatProcessorMPEG4Audio{
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize: udpMaxPayloadSize,
|
|
|
|
format: forma,
|
2022-08-14 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
if generateRTPPackets {
|
2023-06-01 22:38:27 +00:00
|
|
|
err := t.createEncoder()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-04-25 16:13:51 +00:00
|
|
|
}
|
2022-08-14 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
return t, nil
|
2022-08-14 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 15:21:18 +00:00
|
|
|
func (t *formatProcessorMPEG4Audio) createEncoder() error {
|
2023-06-01 22:38:27 +00:00
|
|
|
t.encoder = &rtpmpeg4audio.Encoder{
|
|
|
|
PayloadMaxSize: t.udpMaxPayloadSize - 12,
|
|
|
|
PayloadType: t.format.PayloadTyp,
|
|
|
|
SizeLength: t.format.SizeLength,
|
|
|
|
IndexLength: t.format.IndexLength,
|
|
|
|
IndexDeltaLength: t.format.IndexDeltaLength,
|
|
|
|
}
|
|
|
|
return t.encoder.Init()
|
|
|
|
}
|
|
|
|
|
2023-09-21 15:21:18 +00:00
|
|
|
func (t *formatProcessorMPEG4Audio) ProcessUnit(uu unit.Unit) error { //nolint:dupl
|
|
|
|
u := uu.(*unit.MPEG4Audio)
|
2022-11-02 19:52:12 +00:00
|
|
|
|
2023-09-16 15:16:33 +00:00
|
|
|
pkts, err := t.encoder.Encode(u.AUs)
|
2022-12-14 17:50:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-16 15:16:33 +00:00
|
|
|
|
|
|
|
ts := uint32(multiplyAndDivide(u.PTS, time.Duration(t.format.ClockRate()), time.Second))
|
|
|
|
for _, pkt := range pkts {
|
2023-09-21 14:13:40 +00:00
|
|
|
pkt.Timestamp += ts
|
2023-09-16 15:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u.RTPPackets = pkts
|
2023-04-25 16:13:51 +00:00
|
|
|
|
2022-12-14 17:50:08 +00:00
|
|
|
return nil
|
2022-08-14 11:01:06 +00:00
|
|
|
}
|
2023-05-14 12:18:03 +00:00
|
|
|
|
2023-09-21 15:21:18 +00:00
|
|
|
func (t *formatProcessorMPEG4Audio) ProcessRTPPacket( //nolint:dupl
|
2023-09-16 15:16:33 +00:00
|
|
|
pkt *rtp.Packet,
|
|
|
|
ntp time.Time,
|
|
|
|
pts time.Duration,
|
|
|
|
hasNonRTSPReaders bool,
|
|
|
|
) (Unit, error) {
|
2023-09-21 15:21:18 +00:00
|
|
|
u := &unit.MPEG4Audio{
|
2023-08-25 16:11:02 +00:00
|
|
|
Base: unit.Base{
|
2023-07-30 20:55:13 +00:00
|
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
|
|
NTP: ntp,
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-07-30 20:55:13 +00:00
|
|
|
},
|
2023-05-14 12:18:03 +00:00
|
|
|
}
|
2023-09-16 15:16:33 +00:00
|
|
|
|
|
|
|
// remove padding
|
|
|
|
pkt.Header.Padding = false
|
|
|
|
pkt.PaddingSize = 0
|
|
|
|
|
|
|
|
if pkt.MarshalSize() > t.udpMaxPayloadSize {
|
|
|
|
return nil, fmt.Errorf("payload size (%d) is greater than maximum allowed (%d)",
|
|
|
|
pkt.MarshalSize(), t.udpMaxPayloadSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode from RTP
|
|
|
|
if hasNonRTSPReaders || t.decoder != nil {
|
|
|
|
if t.decoder == nil {
|
|
|
|
var err error
|
|
|
|
t.decoder, err = t.format.CreateDecoder()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aus, err := t.decoder.Decode(pkt)
|
|
|
|
if err != nil {
|
|
|
|
if err == rtpmpeg4audio.ErrMorePacketsNeeded {
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.AUs = aus
|
|
|
|
}
|
|
|
|
|
|
|
|
// route packet as is
|
|
|
|
return u, nil
|
2023-05-14 12:18:03 +00:00
|
|
|
}
|