2023-03-21 10:12:43 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/media"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
|
2023-03-21 10:12:43 +00:00
|
|
|
"golang.org/x/net/ipv4"
|
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/formatprocessor"
|
|
|
|
"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 (
|
|
|
|
multicastTTL = 16
|
2023-03-31 09:53:49 +00:00
|
|
|
udpMTU = 1472
|
2023-03-21 10:12:43 +00:00
|
|
|
)
|
|
|
|
|
2023-05-28 16:12:32 +00:00
|
|
|
func joinMulticastGroupOnAtLeastOneInterface(p *ipv4.PacketConn, listenIP net.IP) error {
|
|
|
|
intfs, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
success := false
|
|
|
|
|
|
|
|
for _, intf := range intfs {
|
|
|
|
if (intf.Flags & net.FlagMulticast) != 0 {
|
|
|
|
err := p.JoinGroup(&intf, &net.UDPAddr{IP: listenIP})
|
|
|
|
if err == nil {
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
return fmt.Errorf("unable to activate multicast on any network interface")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-05-28 15:18:16 +00:00
|
|
|
func (s *udpSource) run(ctx context.Context, cnf *conf.PathConf, _ chan *conf.PathConf) error {
|
2023-03-21 10:12:43 +00:00
|
|
|
s.Log(logger.Debug, "connecting")
|
|
|
|
|
|
|
|
hostPort := cnf.Source[len("udp://"):]
|
|
|
|
|
2023-04-10 20:48:33 +00:00
|
|
|
pc, err := net.ListenPacket(restrictNetwork("udp", hostPort))
|
2023-03-21 10:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer pc.Close()
|
|
|
|
|
|
|
|
host, _, _ := net.SplitHostPort(hostPort)
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
|
|
|
|
if ip.IsMulticast() {
|
|
|
|
p := ipv4.NewPacketConn(pc)
|
|
|
|
|
|
|
|
err = p.SetMulticastTTL(multicastTTL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:12:32 +00:00
|
|
|
err = joinMulticastGroupOnAtLeastOneInterface(p, ip)
|
2023-03-21 10:12:43 +00:00
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
var medias media.Medias
|
2023-07-30 20:34:35 +00:00
|
|
|
var stream *stream.Stream
|
2023-07-27 22:06:58 +00:00
|
|
|
|
|
|
|
var td *mpegts.TimeDecoder
|
|
|
|
decodeTime := func(t int64) time.Duration {
|
|
|
|
if td == nil {
|
|
|
|
td = mpegts.NewTimeDecoder(t)
|
|
|
|
}
|
|
|
|
return td.Decode(t)
|
|
|
|
}
|
|
|
|
|
2023-07-30 21:28:54 +00:00
|
|
|
for _, track := range r.Tracks() { //nolint:dupl
|
2023-07-27 22:06:58 +00:00
|
|
|
var medi *media.Media
|
|
|
|
|
|
|
|
switch tcodec := track.Codec.(type) {
|
|
|
|
case *mpegts.CodecH264:
|
|
|
|
medi = &media.Media{
|
|
|
|
Type: media.TypeVideo,
|
|
|
|
Formats: []formats.Format{&formats.H264{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
PacketizationMode: 1,
|
|
|
|
}},
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
r.OnDataH26x(track, func(pts int64, _ int64, au [][]byte) error {
|
2023-07-30 20:34:35 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitH264{
|
2023-07-30 20:55:13 +00:00
|
|
|
BaseUnit: formatprocessor.BaseUnit{
|
|
|
|
NTP: time.Now(),
|
|
|
|
},
|
2023-07-27 22:06:58 +00:00
|
|
|
PTS: decodeTime(pts),
|
|
|
|
AU: au,
|
|
|
|
})
|
|
|
|
return nil
|
2023-03-21 10:12:43 +00:00
|
|
|
})
|
2023-07-27 22:06:58 +00:00
|
|
|
|
|
|
|
case *mpegts.CodecH265:
|
|
|
|
medi = &media.Media{
|
|
|
|
Type: media.TypeVideo,
|
|
|
|
Formats: []formats.Format{&formats.H265{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
}},
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
r.OnDataH26x(track, func(pts int64, _ int64, au [][]byte) error {
|
2023-07-30 20:34:35 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitH265{
|
2023-07-30 20:55:13 +00:00
|
|
|
BaseUnit: formatprocessor.BaseUnit{
|
|
|
|
NTP: time.Now(),
|
|
|
|
},
|
2023-07-27 22:06:58 +00:00
|
|
|
PTS: decodeTime(pts),
|
|
|
|
AU: au,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
case *mpegts.CodecMPEG4Audio:
|
|
|
|
medi = &media.Media{
|
|
|
|
Type: media.TypeAudio,
|
|
|
|
Formats: []formats.Format{&formats.MPEG4Audio{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
SizeLength: 13,
|
|
|
|
IndexLength: 3,
|
|
|
|
IndexDeltaLength: 3,
|
|
|
|
Config: &tcodec.Config,
|
|
|
|
}},
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
r.OnDataMPEG4Audio(track, func(pts int64, _ int64, aus [][]byte) error {
|
2023-07-30 20:34:35 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitMPEG4AudioGeneric{
|
2023-07-30 20:55:13 +00:00
|
|
|
BaseUnit: formatprocessor.BaseUnit{
|
|
|
|
NTP: time.Now(),
|
|
|
|
},
|
2023-07-27 22:06:58 +00:00
|
|
|
PTS: decodeTime(pts),
|
|
|
|
AUs: aus,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
case *mpegts.CodecOpus:
|
|
|
|
medi = &media.Media{
|
|
|
|
Type: media.TypeAudio,
|
|
|
|
Formats: []formats.Format{&formats.Opus{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
IsStereo: (tcodec.ChannelCount == 2),
|
|
|
|
}},
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
r.OnDataOpus(track, func(pts int64, _ int64, packets [][]byte) error {
|
2023-07-30 20:34:35 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitOpus{
|
2023-07-30 20:55:13 +00:00
|
|
|
BaseUnit: formatprocessor.BaseUnit{
|
|
|
|
NTP: time.Now(),
|
|
|
|
},
|
2023-07-27 22:06:58 +00:00
|
|
|
PTS: decodeTime(pts),
|
|
|
|
Packets: packets,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-07-27 22:06:58 +00:00
|
|
|
medias = append(medias, medi)
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
2023-08-03 21:12:05 +00:00
|
|
|
res := s.parent.setReady(pathSourceStaticSetReadyReq{
|
2023-07-27 22:06:58 +00:00
|
|
|
medias: medias,
|
|
|
|
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-05-14 12:18:03 +00:00
|
|
|
func (*udpSource) apiSourceDescribe() pathAPISourceOrReader {
|
|
|
|
return pathAPISourceOrReader{
|
|
|
|
Type: "udpSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|