2023-03-21 10:12:43 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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-04-01 16:39:12 +00:00
|
|
|
"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"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
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-03-21 10:12:43 +00:00
|
|
|
type udpSourceParent interface {
|
2023-05-04 18:16:41 +00:00
|
|
|
logger.Writer
|
2023-08-03 21:12:05 +00:00
|
|
|
setReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
|
|
|
|
setNotReady(req pathSourceStaticSetNotReadyReq)
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}) {
|
2023-08-03 21:12:05 +00:00
|
|
|
s.parent.Log(level, "[UDP source] "+format, args...)
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run implements sourceStaticImpl.
|
2023-10-07 21:32:15 +00:00
|
|
|
func (s *udpSource) run(ctx context.Context, cnf *conf.Path, _ chan *conf.Path) error {
|
2023-03-21 10:12:43 +00:00
|
|
|
s.Log(logger.Debug, "connecting")
|
|
|
|
|
|
|
|
hostPort := cnf.Source[len("udp://"):]
|
|
|
|
|
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 {
|
|
|
|
tmp, err := net.ListenPacket(restrictNetwork("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
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
pc.Close()
|
|
|
|
<-readerErr
|
|
|
|
return fmt.Errorf("terminated")
|
|
|
|
}
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-14 20:52:10 +00:00
|
|
|
medias, err := mpegtsSetupRead(r, &stream)
|
2023-09-19 20:33:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-05 12:47:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-03 21:12:05 +00:00
|
|
|
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
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-08-03 21:12:05 +00:00
|
|
|
defer s.parent.setNotReady(pathSourceStaticSetNotReadyReq{})
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
stream = res.stream
|
2023-03-21 10:12:43 +00:00
|
|
|
|
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
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
2023-09-16 19:41:49 +00:00
|
|
|
func (*udpSource) apiSourceDescribe() apiPathSourceOrReader {
|
|
|
|
return apiPathSourceOrReader{
|
2023-05-14 12:18:03 +00:00
|
|
|
Type: "udpSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|