2023-01-05 11:54:00 +00:00
|
|
|
package formatprocessor
|
2023-01-03 17:36:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib/v2/pkg/format"
|
|
|
|
"github.com/aler9/gortsplib/v2/pkg/formatdecenc/rtpsimpleaudio"
|
|
|
|
"github.com/pion/rtp"
|
|
|
|
)
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
// DataOpus is a Opus data unit.
|
|
|
|
type DataOpus struct {
|
|
|
|
RTPPackets []*rtp.Packet
|
|
|
|
NTP time.Time
|
|
|
|
PTS time.Duration
|
|
|
|
Frame []byte
|
2023-01-03 17:36:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
// GetRTPPackets implements Data.
|
|
|
|
func (d *DataOpus) GetRTPPackets() []*rtp.Packet {
|
|
|
|
return d.RTPPackets
|
2023-01-03 17:36:13 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
// GetNTP implements Data.
|
|
|
|
func (d *DataOpus) GetNTP() time.Time {
|
|
|
|
return d.NTP
|
2023-01-03 17:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type formatProcessorOpus struct {
|
|
|
|
format *format.Opus
|
|
|
|
encoder *rtpsimpleaudio.Encoder
|
|
|
|
decoder *rtpsimpleaudio.Decoder
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
func newOpus(
|
2023-01-03 17:36:13 +00:00
|
|
|
forma *format.Opus,
|
|
|
|
allocateEncoder bool,
|
|
|
|
) (*formatProcessorOpus, error) {
|
|
|
|
t := &formatProcessorOpus{
|
|
|
|
format: forma,
|
|
|
|
}
|
|
|
|
|
|
|
|
if allocateEncoder {
|
|
|
|
t.encoder = forma.CreateEncoder()
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
func (t *formatProcessorOpus) Process(dat Data, hasNonRTSPReaders bool) error { //nolint:dupl
|
|
|
|
tdata := dat.(*DataOpus)
|
2023-01-03 17:36:13 +00:00
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
if tdata.RTPPackets != nil {
|
|
|
|
pkt := tdata.RTPPackets[0]
|
2023-01-03 17:36:13 +00:00
|
|
|
|
|
|
|
// remove padding
|
|
|
|
pkt.Header.Padding = false
|
|
|
|
pkt.PaddingSize = 0
|
|
|
|
|
|
|
|
if pkt.MarshalSize() > maxPacketSize {
|
|
|
|
return fmt.Errorf("payload size (%d) is greater than maximum allowed (%d)",
|
|
|
|
pkt.MarshalSize(), maxPacketSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode from RTP
|
|
|
|
if hasNonRTSPReaders {
|
|
|
|
if t.decoder == nil {
|
|
|
|
t.decoder = t.format.CreateDecoder()
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
frame, PTS, err := t.decoder.Decode(pkt)
|
2023-01-03 17:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
tdata.Frame = frame
|
|
|
|
tdata.PTS = PTS
|
2023-01-03 17:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// route packet as is
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
pkt, err := t.encoder.Encode(tdata.Frame, tdata.PTS)
|
2023-01-03 17:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
tdata.RTPPackets = []*rtp.Packet{pkt}
|
2023-01-03 17:36:13 +00:00
|
|
|
return nil
|
|
|
|
}
|