mediamtx/internal/core/hls_source.go

239 lines
5.5 KiB
Go
Raw Normal View History

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"
"github.com/bluenviron/gortsplib/v3/pkg/formats"
"github.com/bluenviron/gortsplib/v3/pkg/media"
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/formatprocessor"
"github.com/bluenviron/mediamtx/internal/logger"
"github.com/bluenviron/mediamtx/internal/stream"
2021-09-05 15:35:04 +00:00
)
type hlsSourceParent interface {
logger.Writer
setReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
setNotReady(req pathSourceStaticSetNotReadyReq)
2021-09-05 15:35:04 +00:00
}
type hlsSource struct {
parent hlsSourceParent
2021-09-05 15:35:04 +00:00
}
func newHLSSource(
2022-04-07 10:50:35 +00:00
parent hlsSourceParent,
) *hlsSource {
return &hlsSource{
parent: parent,
2021-09-05 15:35:04 +00:00
}
}
func (s *hlsSource) 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 sourceStaticImpl.
func (s *hlsSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan *conf.PathConf) error {
var stream *stream.Stream
2021-09-05 15:35:04 +00:00
defer func() {
if stream != nil {
s.parent.setNotReady(pathSourceStaticSetNotReadyReq{})
2021-09-05 15:35:04 +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{
TLSClientConfig: tlsConfigForFingerprint(cnf.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) {
s.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 {
var medias media.Medias
for _, track := range tracks {
var medi *media.Media
switch tcodec := track.Codec.(type) {
case *codecs.AV1:
medi = &media.Media{
Type: media.TypeVideo,
Formats: []formats.Format{&formats.AV1{}},
}
c.OnDataAV1(track, func(pts time.Duration, tu [][]byte) {
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitAV1{
BaseUnit: formatprocessor.BaseUnit{
NTP: time.Now(),
},
PTS: pts,
TU: tu,
})
})
2023-08-07 18:25:45 +00:00
case *codecs.VP9:
medi = &media.Media{
Type: media.TypeVideo,
Formats: []formats.Format{&formats.VP9{}},
}
c.OnDataVP9(track, func(pts time.Duration, frame []byte) {
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitVP9{
BaseUnit: formatprocessor.BaseUnit{
NTP: time.Now(),
},
PTS: pts,
Frame: frame,
})
})
2023-08-07 18:25:45 +00:00
case *codecs.H264:
medi = &media.Media{
Type: media.TypeVideo,
Formats: []formats.Format{&formats.H264{
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], &formatprocessor.UnitH264{
BaseUnit: formatprocessor.BaseUnit{
NTP: time.Now(),
},
PTS: pts,
AU: au,
})
})
2023-08-07 18:25:45 +00:00
case *codecs.H265:
medi = &media.Media{
Type: media.TypeVideo,
Formats: []formats.Format{&formats.H265{
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], &formatprocessor.UnitH265{
BaseUnit: formatprocessor.BaseUnit{
NTP: time.Now(),
},
PTS: pts,
AU: au,
})
})
2023-08-07 18:25:45 +00:00
case *codecs.MPEG4Audio:
medi = &media.Media{
Type: media.TypeAudio,
Formats: []formats.Format{&formats.MPEG4Audio{
PayloadTyp: 96,
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
Config: &tcodec.Config,
}},
}
c.OnDataMPEG4Audio(track, func(pts time.Duration, aus [][]byte) {
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitMPEG4AudioGeneric{
BaseUnit: formatprocessor.BaseUnit{
NTP: time.Now(),
},
PTS: pts,
AUs: aus,
})
})
2023-08-07 18:25:45 +00:00
case *codecs.Opus:
medi = &media.Media{
Type: media.TypeAudio,
Formats: []formats.Format{&formats.Opus{
PayloadTyp: 96,
IsStereo: (tcodec.ChannelCount == 2),
}},
}
c.OnDataOpus(track, func(pts time.Duration, packets [][]byte) {
stream.WriteUnit(medi, medi.Formats[0], &formatprocessor.UnitOpus{
BaseUnit: formatprocessor.BaseUnit{
NTP: time.Now(),
},
PTS: pts,
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
2023-08-07 18:25:45 +00:00
res := s.parent.setReady(pathSourceStaticSetReadyReq{
medias: medias,
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
},
}
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 <-reloadConf:
case <-ctx.Done():
c.Close()
<-c.Wait()
return nil
}
2021-09-05 15:35:04 +00:00
}
}
// apiSourceDescribe implements sourceStaticImpl.
func (*hlsSource) apiSourceDescribe() pathAPISourceOrReader {
return pathAPISourceOrReader{
Type: "hlsSource",
ID: "",
}
2021-09-05 15:35:04 +00:00
}