2023-01-05 11:54:00 +00:00
|
|
|
package formatprocessor //nolint:dupl
|
2022-12-13 20:26:35 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-03 20:13:20 +00:00
|
|
|
"errors"
|
2022-12-13 20:26:35 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/format"
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpvp8"
|
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-12-13 20:26:35 +00:00
|
|
|
)
|
|
|
|
|
2022-12-15 23:50:47 +00:00
|
|
|
type formatProcessorVP8 struct {
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int
|
2023-08-26 16:54:28 +00:00
|
|
|
format *format.VP8
|
2023-03-31 09:53:49 +00:00
|
|
|
encoder *rtpvp8.Encoder
|
|
|
|
decoder *rtpvp8.Decoder
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
func newVP8(
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize int,
|
2023-08-26 16:54:28 +00:00
|
|
|
forma *format.VP8,
|
2023-05-04 18:16:41 +00:00
|
|
|
generateRTPPackets bool,
|
2022-12-15 23:50:47 +00:00
|
|
|
) (*formatProcessorVP8, error) {
|
|
|
|
t := &formatProcessorVP8{
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMaxPayloadSize: udpMaxPayloadSize,
|
|
|
|
format: forma,
|
2022-12-13 20:26:35 +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-12-13 20:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2023-06-01 22:38:27 +00:00
|
|
|
func (t *formatProcessorVP8) createEncoder() error {
|
|
|
|
t.encoder = &rtpvp8.Encoder{
|
|
|
|
PayloadMaxSize: t.udpMaxPayloadSize - 12,
|
|
|
|
PayloadType: t.format.PayloadTyp,
|
|
|
|
}
|
|
|
|
return t.encoder.Init()
|
|
|
|
}
|
|
|
|
|
2023-09-16 15:16:33 +00:00
|
|
|
func (t *formatProcessorVP8) ProcessUnit(uu unit.Unit) error { //nolint:dupl
|
|
|
|
u := uu.(*unit.VP8)
|
2022-12-13 20:26:35 +00:00
|
|
|
|
2023-09-16 15:16:33 +00:00
|
|
|
pkts, err := t.encoder.Encode(u.Frame)
|
2022-12-15 23:50:47 +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-15 23:50:47 +00:00
|
|
|
return nil
|
2022-12-13 20:26:35 +00:00
|
|
|
}
|
2023-05-14 12:18:03 +00:00
|
|
|
|
2023-09-16 15:16:33 +00:00
|
|
|
func (t *formatProcessorVP8) ProcessRTPPacket( //nolint:dupl
|
|
|
|
pkt *rtp.Packet,
|
|
|
|
ntp time.Time,
|
|
|
|
pts time.Duration,
|
|
|
|
hasNonRTSPReaders bool,
|
|
|
|
) (Unit, error) {
|
|
|
|
u := &unit.VP8{
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
frame, err := t.decoder.Decode(pkt)
|
|
|
|
if err != nil {
|
2024-01-03 20:13:20 +00:00
|
|
|
if errors.Is(err, rtpvp8.ErrNonStartingPacketAndNoPrevious) ||
|
|
|
|
errors.Is(err, rtpvp8.ErrMorePacketsNeeded) {
|
2023-09-16 15:16:33 +00:00
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Frame = frame
|
|
|
|
}
|
|
|
|
|
|
|
|
// route packet as is
|
|
|
|
return u, nil
|
2023-05-14 12:18:03 +00:00
|
|
|
}
|