mediamtx/internal/core/udp_source.go

165 lines
3.3 KiB
Go
Raw Normal View History

package core
import (
"context"
"fmt"
"net"
"time"
2023-08-26 16:54:28 +00:00
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/multicast"
2023-04-01 16:39:12 +00:00
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
2023-05-16 14:14:20 +00:00
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/stream"
)
const (
// same size as GStreamer's rtspsrc
udpKernelReadBufferSize = 0x80000
)
2023-04-11 16:58:56 +00:00
type packetConnReader struct {
2023-07-30 21:28:54 +00:00
net.PacketConn
2023-04-11 16:58:56 +00:00
}
func newPacketConnReader(pc net.PacketConn) *packetConnReader {
return &packetConnReader{
2023-07-30 21:28:54 +00:00
PacketConn: pc,
2023-04-11 16:58:56 +00:00
}
}
func (r *packetConnReader) Read(p []byte) (int, error) {
2023-07-30 21:28:54 +00:00
n, _, err := r.PacketConn.ReadFrom(p)
2023-07-27 22:06:58 +00:00
return n, err
}
type packetConn interface {
net.PacketConn
SetReadBuffer(int) error
}
type udpSourceParent interface {
logger.Writer
setReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
setNotReady(req pathSourceStaticSetNotReadyReq)
}
type udpSource struct {
readTimeout conf.StringDuration
parent udpSourceParent
}
func newUDPSource(
readTimeout conf.StringDuration,
parent udpSourceParent,
) *udpSource {
return &udpSource{
readTimeout: readTimeout,
parent: parent,
}
}
func (s *udpSource) Log(level logger.Level, format string, args ...interface{}) {
s.parent.Log(level, "[UDP source] "+format, args...)
}
// run implements sourceStaticImpl.
func (s *udpSource) run(ctx context.Context, cnf *conf.Path, _ chan *conf.Path) error {
s.Log(logger.Debug, "connecting")
hostPort := cnf.Source[len("udp://"):]
addr, err := net.ResolveUDPAddr("udp", hostPort)
if err != nil {
return err
}
var pc packetConn
if ip4 := addr.IP.To4(); ip4 != nil && addr.IP.IsMulticast() {
pc, err = multicast.NewMultiConn(hostPort, true, net.ListenPacket)
if err != nil {
return err
}
} else {
tmp, err := net.ListenPacket(restrictNetwork("udp", addr.String()))
if err != nil {
return err
}
pc = tmp.(*net.UDPConn)
}
defer pc.Close()
err = pc.SetReadBuffer(udpKernelReadBufferSize)
if err != nil {
return err
}
readerErr := make(chan error)
go func() {
2023-07-27 22:06:58 +00:00
readerErr <- s.runReader(pc)
}()
select {
case err := <-readerErr:
return err
case <-ctx.Done():
pc.Close()
<-readerErr
return fmt.Errorf("terminated")
}
}
2023-07-27 22:06:58 +00:00
func (s *udpSource) runReader(pc net.PacketConn) error {
pc.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
2023-07-30 21:28:54 +00:00
r, err := mpegts.NewReader(mpegts.NewBufferedReader(newPacketConnReader(pc)))
2023-07-27 22:06:58 +00:00
if err != nil {
return err
}
decodeErrLogger := logger.NewLimitedLogger(s)
2023-08-26 21:34:39 +00:00
r.OnDecodeError(func(err error) {
decodeErrLogger.Log(logger.Warn, err.Error())
})
var stream *stream.Stream
2023-07-27 22:06:58 +00:00
2023-10-14 20:52:10 +00:00
medias, err := mpegtsSetupRead(r, &stream)
if err != nil {
return err
}
res := s.parent.setReady(pathSourceStaticSetReadyReq{
2023-08-26 16:54:28 +00:00
desc: &description.Session{Medias: medias},
2023-07-27 22:06:58 +00:00
generateRTPPackets: true,
})
if res.err != nil {
return res.err
}
defer s.parent.setNotReady(pathSourceStaticSetNotReadyReq{})
2023-07-27 22:06:58 +00:00
stream = res.stream
2023-07-27 22:06:58 +00:00
for {
pc.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
err := r.Read()
if err != nil {
return err
}
}
}
// apiSourceDescribe implements sourceStaticImpl.
func (*udpSource) apiSourceDescribe() apiPathSourceOrReader {
return apiPathSourceOrReader{
Type: "udpSource",
ID: "",
}
}