mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-09-26 19:51:26 +08:00
115 lines
2.1 KiB
Go
115 lines
2.1 KiB
Go
package codecprocessor //nolint:dupl
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/bluenviron/gortsplib/v5/pkg/format"
|
|
"github.com/bluenviron/gortsplib/v5/pkg/format/rtpvp8"
|
|
"github.com/pion/rtp"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
|
"github.com/bluenviron/mediamtx/internal/unit"
|
|
)
|
|
|
|
type vp8 struct {
|
|
RTPMaxPayloadSize int
|
|
Format *format.VP8
|
|
GenerateRTPPackets bool
|
|
Parent logger.Writer
|
|
|
|
encoder *rtpvp8.Encoder
|
|
decoder *rtpvp8.Decoder
|
|
randomStart uint32
|
|
}
|
|
|
|
func (t *vp8) initialize() error {
|
|
if t.GenerateRTPPackets {
|
|
err := t.createEncoder()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
t.randomStart, err = randUint32()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *vp8) createEncoder() error {
|
|
t.encoder = &rtpvp8.Encoder{
|
|
PayloadMaxSize: t.RTPMaxPayloadSize,
|
|
PayloadType: t.Format.PayloadTyp,
|
|
}
|
|
return t.encoder.Init()
|
|
}
|
|
|
|
func (t *vp8) ProcessUnit(uu unit.Unit) error { //nolint:dupl
|
|
u := uu.(*unit.VP8)
|
|
|
|
pkts, err := t.encoder.Encode(u.Frame)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
u.RTPPackets = pkts
|
|
|
|
for _, pkt := range u.RTPPackets {
|
|
pkt.Timestamp += t.randomStart + uint32(u.PTS)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *vp8) ProcessRTPPacket( //nolint:dupl
|
|
pkt *rtp.Packet,
|
|
ntp time.Time,
|
|
pts int64,
|
|
hasNonRTSPReaders bool,
|
|
) (unit.Unit, error) {
|
|
u := &unit.VP8{
|
|
Base: unit.Base{
|
|
RTPPackets: []*rtp.Packet{pkt},
|
|
NTP: ntp,
|
|
PTS: pts,
|
|
},
|
|
}
|
|
|
|
// remove padding
|
|
pkt.Padding = false
|
|
pkt.PaddingSize = 0
|
|
|
|
if len(pkt.Payload) > t.RTPMaxPayloadSize {
|
|
return nil, fmt.Errorf("RTP payload size (%d) is greater than maximum allowed (%d)",
|
|
len(pkt.Payload), t.RTPMaxPayloadSize)
|
|
}
|
|
|
|
// 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 {
|
|
if errors.Is(err, rtpvp8.ErrNonStartingPacketAndNoPrevious) ||
|
|
errors.Is(err, rtpvp8.ErrMorePacketsNeeded) {
|
|
return u, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
u.Frame = frame
|
|
}
|
|
|
|
// route packet as is
|
|
return u, nil
|
|
}
|