2023-10-31 13:19:04 +00:00
|
|
|
// Package udp contains the UDP static source.
|
|
|
|
package udp
|
2023-03-21 10:12:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/description"
|
2023-09-11 22:27:08 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/multicast"
|
2023-10-31 13:19:04 +00:00
|
|
|
mcmpegts "github.com/bluenviron/mediacommon/pkg/formats/mpegts"
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
2023-10-31 13:19:04 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/defs"
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2023-10-31 13:19:04 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/protocols/mpegts"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/restrictnetwork"
|
2023-07-30 20:34:35 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/stream"
|
2023-03-21 10:12:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-09-11 22:27:08 +00:00
|
|
|
// same size as GStreamer's rtspsrc
|
|
|
|
udpKernelReadBufferSize = 0x80000
|
2023-03-21 10:12:43 +00:00
|
|
|
)
|
|
|
|
|
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
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 22:27:08 +00:00
|
|
|
type packetConn interface {
|
|
|
|
net.PacketConn
|
|
|
|
SetReadBuffer(int) error
|
|
|
|
}
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
// Source is a UDP static source.
|
|
|
|
type Source struct {
|
|
|
|
ReadTimeout conf.StringDuration
|
|
|
|
Parent defs.StaticSourceParent
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
// Log implements StaticSource.
|
|
|
|
func (s *Source) Log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
s.Parent.Log(level, "[UDP source] "+format, args...)
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
// Run implements StaticSource.
|
|
|
|
func (s *Source) Run(params defs.StaticSourceRunParams) error {
|
2023-03-21 10:12:43 +00:00
|
|
|
s.Log(logger.Debug, "connecting")
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
hostPort := params.Conf.Source[len("udp://"):]
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-08-05 14:40:51 +00:00
|
|
|
addr, err := net.ResolveUDPAddr("udp", hostPort)
|
2023-03-21 10:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:27:08 +00:00
|
|
|
var pc packetConn
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-09-11 22:27:08 +00:00
|
|
|
if ip4 := addr.IP.To4(); ip4 != nil && addr.IP.IsMulticast() {
|
2023-09-16 15:23:40 +00:00
|
|
|
pc, err = multicast.NewMultiConn(hostPort, true, net.ListenPacket)
|
2023-03-21 10:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 22:27:08 +00:00
|
|
|
} else {
|
2023-10-31 13:19:04 +00:00
|
|
|
tmp, err := net.ListenPacket(restrictnetwork.Restrict("udp", addr.String()))
|
2023-03-21 10:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 22:27:08 +00:00
|
|
|
pc = tmp.(*net.UDPConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer pc.Close()
|
|
|
|
|
|
|
|
err = pc.SetReadBuffer(udpKernelReadBufferSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readerErr := make(chan error)
|
|
|
|
go func() {
|
2023-07-27 22:06:58 +00:00
|
|
|
readerErr <- s.runReader(pc)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-readerErr:
|
|
|
|
return err
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
case <-params.Context.Done():
|
2023-07-27 22:06:58 +00:00
|
|
|
pc.Close()
|
|
|
|
<-readerErr
|
|
|
|
return fmt.Errorf("terminated")
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
func (s *Source) runReader(pc net.PacketConn) error {
|
|
|
|
pc.SetReadDeadline(time.Now().Add(time.Duration(s.ReadTimeout)))
|
|
|
|
r, err := mcmpegts.NewReader(mcmpegts.NewBufferedReader(newPacketConnReader(pc)))
|
2023-07-27 22:06:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-30 09:24:14 +00:00
|
|
|
decodeErrLogger := logger.NewLimitedLogger(s)
|
2023-08-26 21:34:39 +00:00
|
|
|
|
|
|
|
r.OnDecodeError(func(err error) {
|
|
|
|
decodeErrLogger.Log(logger.Warn, err.Error())
|
|
|
|
})
|
|
|
|
|
2023-07-30 20:34:35 +00:00
|
|
|
var stream *stream.Stream
|
2023-07-27 22:06:58 +00:00
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
medias, err := mpegts.ToStream(r, &stream)
|
2023-09-19 20:33:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-05 12:47:20 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
res := s.Parent.SetReady(defs.PathSourceStaticSetReadyReq{
|
|
|
|
Desc: &description.Session{Medias: medias},
|
|
|
|
GenerateRTPPackets: true,
|
2023-07-27 22:06:58 +00:00
|
|
|
})
|
2023-10-31 13:19:04 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
return res.Err
|
2023-07-27 22:06:58 +00:00
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
defer s.Parent.SetNotReady(defs.PathSourceStaticSetNotReadyReq{})
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
stream = res.Stream
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
for {
|
2023-10-31 13:19:04 +00:00
|
|
|
pc.SetReadDeadline(time.Now().Add(time.Duration(s.ReadTimeout)))
|
2023-07-27 22:06:58 +00:00
|
|
|
err := r.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
// APISourceDescribe implements StaticSource.
|
|
|
|
func (*Source) APISourceDescribe() defs.APIPathSourceOrReader {
|
|
|
|
return defs.APIPathSourceOrReader{
|
2023-05-14 12:18:03 +00:00
|
|
|
Type: "udpSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|