2023-07-31 19:20:09 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2023-08-26 16:54:28 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/description"
|
2023-07-31 19:20:09 +00:00
|
|
|
"github.com/bluenviron/mediacommon/pkg/formats/mpegts"
|
|
|
|
"github.com/datarhei/gosrt"
|
|
|
|
|
|
|
|
"github.com/bluenviron/mediamtx/internal/conf"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/stream"
|
|
|
|
)
|
|
|
|
|
|
|
|
type srtSourceParent interface {
|
|
|
|
logger.Writer
|
2023-08-03 21:12:05 +00:00
|
|
|
setReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
|
|
|
|
setNotReady(req pathSourceStaticSetNotReadyReq)
|
2023-07-31 19:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type srtSource struct {
|
|
|
|
readTimeout conf.StringDuration
|
|
|
|
parent srtSourceParent
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSRTSource(
|
|
|
|
readTimeout conf.StringDuration,
|
|
|
|
parent srtSourceParent,
|
|
|
|
) *srtSource {
|
|
|
|
s := &srtSource{
|
|
|
|
readTimeout: readTimeout,
|
|
|
|
parent: parent,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *srtSource) Log(level logger.Level, format string, args ...interface{}) {
|
2023-08-03 21:12:05 +00:00
|
|
|
s.parent.Log(level, "[SRT source] "+format, args...)
|
2023-07-31 19:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run implements sourceStaticImpl.
|
2023-10-07 21:32:15 +00:00
|
|
|
func (s *srtSource) run(ctx context.Context, cnf *conf.Path, reloadConf chan *conf.Path) error {
|
2023-07-31 19:20:09 +00:00
|
|
|
s.Log(logger.Debug, "connecting")
|
|
|
|
|
|
|
|
conf := srt.DefaultConfig()
|
|
|
|
address, err := conf.UnmarshalURL(cnf.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conf.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sconn, err := srt.Dial("srt", address, conf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
readDone := make(chan error)
|
|
|
|
go func() {
|
|
|
|
readDone <- s.runReader(sconn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-readDone:
|
|
|
|
sconn.Close()
|
|
|
|
return err
|
|
|
|
|
|
|
|
case <-reloadConf:
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
sconn.Close()
|
|
|
|
<-readDone
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *srtSource) runReader(sconn srt.Conn) error {
|
|
|
|
sconn.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
|
|
|
|
r, err := mpegts.NewReader(mpegts.NewBufferedReader(sconn))
|
|
|
|
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-31 19:20:09 +00:00
|
|
|
var stream *stream.Stream
|
|
|
|
|
2023-09-19 20:33:00 +00:00
|
|
|
medias, err := mpegtsSetupTracks(r, &stream)
|
|
|
|
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-31 19:20:09 +00:00
|
|
|
generateRTPPackets: true,
|
|
|
|
})
|
|
|
|
if res.err != nil {
|
|
|
|
return res.err
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = res.stream
|
|
|
|
|
|
|
|
for {
|
|
|
|
sconn.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeout)))
|
|
|
|
err := r.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
2023-09-16 19:41:49 +00:00
|
|
|
func (*srtSource) apiSourceDescribe() apiPathSourceOrReader {
|
|
|
|
return apiPathSourceOrReader{
|
2023-07-31 19:20:09 +00:00
|
|
|
Type: "srtSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
|
|
|
}
|