2021-09-05 15:35:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-11 20:01:41 +00:00
|
|
|
"net/http"
|
2021-09-05 15:35:04 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gohlslib"
|
|
|
|
"github.com/bluenviron/gohlslib/pkg/codecs"
|
2023-08-26 16:54:28 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/description"
|
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/format"
|
2021-09-05 15:35:04 +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-08-25 16:11:02 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/unit"
|
2021-09-05 15:35:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type hlsSourceParent 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)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type hlsSource struct {
|
2023-02-13 11:12:04 +00:00
|
|
|
parent hlsSourceParent
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newHLSSource(
|
2022-04-07 10:50:35 +00:00
|
|
|
parent hlsSourceParent,
|
|
|
|
) *hlsSource {
|
2022-07-30 19:51:38 +00:00
|
|
|
return &hlsSource{
|
2023-02-13 11:12:04 +00:00
|
|
|
parent: parent,
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *hlsSource) Log(level logger.Level, format string, args ...interface{}) {
|
2023-08-03 21:12:05 +00:00
|
|
|
s.parent.Log(level, "[HLS source] "+format, args...)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 19:51:38 +00:00
|
|
|
// run implements sourceStaticImpl.
|
2023-10-07 21:32:15 +00:00
|
|
|
func (s *hlsSource) run(ctx context.Context, cnf *conf.Path, reloadConf chan *conf.Path) error {
|
2023-07-30 20:34:35 +00:00
|
|
|
var stream *stream.Stream
|
2021-09-05 15:35:04 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if stream != nil {
|
2023-08-03 21:12:05 +00:00
|
|
|
s.parent.setNotReady(pathSourceStaticSetNotReadyReq{})
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-30 09:24:14 +00:00
|
|
|
decodeErrLogger := logger.NewLimitedLogger(s)
|
2023-08-26 21:34:39 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
var c *gohlslib.Client
|
|
|
|
c = &gohlslib.Client{
|
2023-04-11 20:01:41 +00:00
|
|
|
URI: cnf.Source,
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
2023-07-18 21:39:26 +00:00
|
|
|
TLSClientConfig: tlsConfigForFingerprint(cnf.SourceFingerprint),
|
2023-04-11 20:01:41 +00:00
|
|
|
},
|
|
|
|
},
|
2023-07-30 21:28:54 +00:00
|
|
|
OnDownloadPrimaryPlaylist: func(u string) {
|
2023-08-24 20:42:57 +00:00
|
|
|
s.Log(logger.Debug, "downloading primary playlist %v", u)
|
2023-07-30 21:28:54 +00:00
|
|
|
},
|
|
|
|
OnDownloadStreamPlaylist: func(u string) {
|
2023-08-24 20:42:57 +00:00
|
|
|
s.Log(logger.Debug, "downloading stream playlist %v", u)
|
2023-07-30 21:28:54 +00:00
|
|
|
},
|
|
|
|
OnDownloadSegment: func(u string) {
|
2023-08-24 20:42:57 +00:00
|
|
|
s.Log(logger.Debug, "downloading segment %v", u)
|
2023-07-30 21:28:54 +00:00
|
|
|
},
|
|
|
|
OnDecodeError: func(err error) {
|
2023-08-26 21:34:39 +00:00
|
|
|
decodeErrLogger.Log(logger.Warn, err.Error())
|
2023-03-19 23:34:13 +00:00
|
|
|
},
|
2023-08-07 18:25:45 +00:00
|
|
|
OnTracks: func(tracks []*gohlslib.Track) error {
|
2023-08-26 16:54:28 +00:00
|
|
|
var medias []*description.Media
|
2023-08-07 18:25:45 +00:00
|
|
|
|
|
|
|
for _, track := range tracks {
|
2023-08-26 16:54:28 +00:00
|
|
|
var medi *description.Media
|
2023-08-07 18:25:45 +00:00
|
|
|
|
|
|
|
switch tcodec := track.Codec.(type) {
|
|
|
|
case *codecs.AV1:
|
2023-08-26 16:54:28 +00:00
|
|
|
medi = &description.Media{
|
2023-08-29 17:52:39 +00:00
|
|
|
Type: description.MediaTypeVideo,
|
|
|
|
Formats: []format.Format{&format.AV1{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
}},
|
2023-08-07 18:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.OnDataAV1(track, func(pts time.Duration, tu [][]byte) {
|
2023-08-25 16:11:02 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &unit.AV1{
|
|
|
|
Base: unit.Base{
|
2023-08-07 18:25:45 +00:00
|
|
|
NTP: time.Now(),
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-08-07 18:25:45 +00:00
|
|
|
},
|
2023-08-26 16:54:28 +00:00
|
|
|
TU: tu,
|
2023-08-07 18:25:45 +00:00
|
|
|
})
|
2023-08-06 11:21:43 +00:00
|
|
|
})
|
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
case *codecs.VP9:
|
2023-08-26 16:54:28 +00:00
|
|
|
medi = &description.Media{
|
2023-08-29 17:52:39 +00:00
|
|
|
Type: description.MediaTypeVideo,
|
|
|
|
Formats: []format.Format{&format.VP9{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
}},
|
2023-08-07 18:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.OnDataVP9(track, func(pts time.Duration, frame []byte) {
|
2023-08-25 16:11:02 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &unit.VP9{
|
|
|
|
Base: unit.Base{
|
2023-08-07 18:25:45 +00:00
|
|
|
NTP: time.Now(),
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-08-07 18:25:45 +00:00
|
|
|
},
|
|
|
|
Frame: frame,
|
|
|
|
})
|
|
|
|
})
|
2023-08-06 19:10:16 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
case *codecs.H264:
|
2023-08-26 16:54:28 +00:00
|
|
|
medi = &description.Media{
|
|
|
|
Type: description.MediaTypeVideo,
|
|
|
|
Formats: []format.Format{&format.H264{
|
2023-08-07 18:25:45 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
PacketizationMode: 1,
|
|
|
|
SPS: tcodec.SPS,
|
|
|
|
PPS: tcodec.PPS,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.OnDataH26x(track, func(pts time.Duration, dts time.Duration, au [][]byte) {
|
2023-08-25 16:11:02 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &unit.H264{
|
|
|
|
Base: unit.Base{
|
2023-08-07 18:25:45 +00:00
|
|
|
NTP: time.Now(),
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-08-07 18:25:45 +00:00
|
|
|
},
|
2023-08-26 16:54:28 +00:00
|
|
|
AU: au,
|
2023-08-07 18:25:45 +00:00
|
|
|
})
|
2023-08-06 19:10:16 +00:00
|
|
|
})
|
2023-02-18 23:28:50 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
case *codecs.H265:
|
2023-08-26 16:54:28 +00:00
|
|
|
medi = &description.Media{
|
|
|
|
Type: description.MediaTypeVideo,
|
|
|
|
Formats: []format.Format{&format.H265{
|
2023-08-07 18:25:45 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
VPS: tcodec.VPS,
|
|
|
|
SPS: tcodec.SPS,
|
|
|
|
PPS: tcodec.PPS,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.OnDataH26x(track, func(pts time.Duration, dts time.Duration, au [][]byte) {
|
2023-08-25 16:11:02 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &unit.H265{
|
|
|
|
Base: unit.Base{
|
2023-08-07 18:25:45 +00:00
|
|
|
NTP: time.Now(),
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-08-07 18:25:45 +00:00
|
|
|
},
|
2023-08-26 16:54:28 +00:00
|
|
|
AU: au,
|
2023-08-07 18:25:45 +00:00
|
|
|
})
|
2023-01-06 14:25:05 +00:00
|
|
|
})
|
2023-02-18 23:28:50 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
case *codecs.MPEG4Audio:
|
2023-08-26 16:54:28 +00:00
|
|
|
medi = &description.Media{
|
|
|
|
Type: description.MediaTypeAudio,
|
|
|
|
Formats: []format.Format{&format.MPEG4Audio{
|
2023-08-07 18:25:45 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
SizeLength: 13,
|
|
|
|
IndexLength: 3,
|
|
|
|
IndexDeltaLength: 3,
|
|
|
|
Config: &tcodec.Config,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.OnDataMPEG4Audio(track, func(pts time.Duration, aus [][]byte) {
|
2023-09-21 15:21:18 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &unit.MPEG4Audio{
|
2023-08-25 16:11:02 +00:00
|
|
|
Base: unit.Base{
|
2023-08-07 18:25:45 +00:00
|
|
|
NTP: time.Now(),
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-08-07 18:25:45 +00:00
|
|
|
},
|
|
|
|
AUs: aus,
|
|
|
|
})
|
2023-01-06 14:39:06 +00:00
|
|
|
})
|
2023-02-18 23:28:50 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
case *codecs.Opus:
|
2023-08-26 16:54:28 +00:00
|
|
|
medi = &description.Media{
|
|
|
|
Type: description.MediaTypeAudio,
|
|
|
|
Formats: []format.Format{&format.Opus{
|
2023-08-07 18:25:45 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
IsStereo: (tcodec.ChannelCount == 2),
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.OnDataOpus(track, func(pts time.Duration, packets [][]byte) {
|
2023-08-25 16:11:02 +00:00
|
|
|
stream.WriteUnit(medi, medi.Formats[0], &unit.Opus{
|
|
|
|
Base: unit.Base{
|
2023-08-07 18:25:45 +00:00
|
|
|
NTP: time.Now(),
|
2023-08-26 16:54:28 +00:00
|
|
|
PTS: pts,
|
2023-08-07 18:25:45 +00:00
|
|
|
},
|
|
|
|
Packets: packets,
|
|
|
|
})
|
2023-01-06 14:25:05 +00:00
|
|
|
})
|
2023-04-01 16:39:12 +00:00
|
|
|
}
|
2023-02-18 23:28:50 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
medias = append(medias, medi)
|
2022-12-13 19:54:17 +00:00
|
|
|
}
|
2023-04-01 16:39:12 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
res := s.parent.setReady(pathSourceStaticSetReadyReq{
|
2023-08-26 16:54:28 +00:00
|
|
|
desc: &description.Session{Medias: medias},
|
2023-08-07 18:25:45 +00:00
|
|
|
generateRTPPackets: true,
|
|
|
|
})
|
|
|
|
if res.err != nil {
|
|
|
|
return res.err
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
stream = res.stream
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-08-07 18:25:45 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-02-19 11:25:23 +00:00
|
|
|
|
2023-03-12 15:59:04 +00:00
|
|
|
err := c.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-02-13 11:12:04 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-c.Wait():
|
2023-03-12 15:59:04 +00:00
|
|
|
c.Close()
|
2023-02-13 11:12:04 +00:00
|
|
|
return err
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-02-13 11:12:04 +00:00
|
|
|
case <-reloadConf:
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
c.Close()
|
|
|
|
<-c.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
2023-09-16 19:41:49 +00:00
|
|
|
func (*hlsSource) apiSourceDescribe() apiPathSourceOrReader {
|
|
|
|
return apiPathSourceOrReader{
|
2023-05-14 12:18:03 +00:00
|
|
|
Type: "hlsSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|