mediamtx/internal/staticsources/hls/source.go

237 lines
5.5 KiB
Go
Raw Normal View History

// 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
"github.com/bluenviron/mediamtx/internal/conf"
"github.com/bluenviron/mediamtx/internal/defs"
2023-05-16 14:14:20 +00:00
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/protocols/tls"
"github.com/bluenviron/mediamtx/internal/stream"
"github.com/bluenviron/mediamtx/internal/unit"
2021-09-05 15:35:04 +00:00
)
// Source is a HLS static source.
type Source struct {
ReadTimeout conf.StringDuration
Parent defs.StaticSourceParent
2021-09-05 15:35: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
}
// Run implements StaticSource.
func (s *Source) Run(params defs.StaticSourceRunParams) error {
var stream *stream.Stream
2021-09-05 15:35:04 +00:00
defer func() {
if stream != nil {
s.Parent.SetNotReady(defs.PathSourceStaticSetNotReadyReq{})
2021-09-05 15:35:04 +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{
URI: params.Conf.Source,
2023-04-11 20:01:41 +00:00
HTTPClient: &http.Client{
Timeout: time.Duration(s.ReadTimeout),
2023-04-11 20:01:41 +00:00
Transport: &http.Transport{
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) {
s.Log(logger.Debug, "downloading primary playlist %v", u)
2023-07-30 21:28:54 +00:00
},
OnDownloadStreamPlaylist: func(u string) {
s.Log(logger.Debug, "downloading stream playlist %v", u)
2023-07-30 21:28:54 +00:00
},
OnDownloadSegment: func(u string) {
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{
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) {
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-07 18:25:45 +00:00
case *codecs.VP9:
2023-08-26 16:54:28 +00:00
medi = &description.Media{
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) {
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-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) {
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-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) {
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-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{
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-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),
2023-08-07 18:25:45 +00:00
}},
}
c.OnDataOpus(track, func(pts time.Duration, packets [][]byte) {
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-04-01 16:39:12 +00:00
}
2023-08-07 18:25:45 +00:00
medias = append(medias, medi)
}
2023-04-01 16:39:12 +00:00
res := s.Parent.SetReady(defs.PathSourceStaticSetReadyReq{
Desc: &description.Session{Medias: medias},
GenerateRTPPackets: true,
2023-08-07 18:25:45 +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
stream = res.Stream
2021-09-05 15:35:04 +00:00
2023-08-07 18:25:45 +00:00
return nil
},
}
2023-03-12 15:59:04 +00:00
err := c.Start()
if err != nil {
return err
}
2021-09-05 15:35:04 +00:00
for {
select {
case err := <-c.Wait():
2023-03-12 15:59:04 +00:00
c.Close()
return err
2021-09-05 15:35:04 +00:00
case <-params.ReloadConf:
case <-params.Context.Done():
c.Close()
<-c.Wait()
return nil
}
2021-09-05 15:35:04 +00:00
}
}
// APISourceDescribe implements StaticSource.
func (*Source) APISourceDescribe() defs.APIPathSourceOrReader {
return defs.APIPathSourceOrReader{
Type: "hlsSource",
ID: "",
}
2021-09-05 15:35:04 +00:00
}