2023-10-31 13:19:04 +00:00
|
|
|
// Package hls contains the HLS static source.
|
|
|
|
package hls
|
2021-09-05 15:35:04 +00:00
|
|
|
|
|
|
|
import (
|
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-11-02 11:45:46 +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/tls"
|
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
|
|
|
)
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
// Source is a HLS static source.
|
|
|
|
type Source struct {
|
2023-11-02 11:45:46 +00:00
|
|
|
ReadTimeout conf.StringDuration
|
|
|
|
Parent defs.StaticSourceParent
|
2021-09-05 15:35:04 +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, "[HLS source] "+format, args...)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
// Run implements StaticSource.
|
|
|
|
func (s *Source) Run(params defs.StaticSourceRunParams) 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-10-31 13:19:04 +00:00
|
|
|
s.Parent.SetNotReady(defs.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-10-31 13:19:04 +00:00
|
|
|
URI: params.Conf.Source,
|
2023-04-11 20:01:41 +00:00
|
|
|
HTTPClient: &http.Client{
|
2023-11-02 11:45:46 +00:00
|
|
|
Timeout: time.Duration(s.ReadTimeout),
|
2023-04-11 20:01:41 +00:00
|
|
|
Transport: &http.Transport{
|
2023-10-31 13:19:04 +00:00
|
|
|
TLSClientConfig: tls.ConfigForFingerprint(params.Conf.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,
|
2023-11-24 21:07:33 +00:00
|
|
|
IsStereo: (tcodec.ChannelCount >= 2),
|
2023-08-07 18:25:45 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-31 13:19:04 +00:00
|
|
|
res := s.Parent.SetReady(defs.PathSourceStaticSetReadyReq{
|
|
|
|
Desc: &description.Session{Medias: medias},
|
|
|
|
GenerateRTPPackets: true,
|
2023-08-07 18:25:45 +00:00
|
|
|
})
|
2023-10-31 13:19:04 +00:00
|
|
|
if res.Err != nil {
|
|
|
|
return res.Err
|
2023-08-07 18:25:45 +00:00
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-10-31 13:19:04 +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-10-31 13:19:04 +00:00
|
|
|
case <-params.ReloadConf:
|
2023-02-13 11:12:04 +00:00
|
|
|
|
2023-10-31 13:19:04 +00:00
|
|
|
case <-params.Context.Done():
|
2023-02-13 11:12:04 +00:00
|
|
|
c.Close()
|
|
|
|
<-c.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-05 15:35:04 +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: "hlsSource",
|
|
|
|
ID: "",
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|