2021-09-05 15:35:04 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
"github.com/aler9/gortsplib/v2/pkg/format"
|
|
|
|
"github.com/aler9/gortsplib/v2/pkg/media"
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-01-05 11:54:00 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/formatprocessor"
|
2021-09-05 15:35:04 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/hls"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type hlsSourceParent interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
log(logger.Level, string, ...interface{})
|
2022-08-04 19:07:17 +00:00
|
|
|
sourceStaticImplSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
|
|
|
|
sourceStaticImplSetNotReady(req pathSourceStaticSetNotReadyReq)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type hlsSource struct {
|
2021-10-25 19:13:02 +00:00
|
|
|
ur string
|
|
|
|
fingerprint string
|
|
|
|
parent hlsSourceParent
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newHLSSource(
|
|
|
|
ur string,
|
2021-10-25 19:13:02 +00:00
|
|
|
fingerprint string,
|
2022-04-07 10:50:35 +00:00
|
|
|
parent hlsSourceParent,
|
|
|
|
) *hlsSource {
|
2022-07-30 19:51:38 +00:00
|
|
|
return &hlsSource{
|
2021-10-25 19:13:02 +00:00
|
|
|
ur: ur,
|
|
|
|
fingerprint: fingerprint,
|
|
|
|
parent: parent,
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *hlsSource) Log(level logger.Level, format string, args ...interface{}) {
|
2021-10-27 19:01:00 +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.
|
|
|
|
func (s *hlsSource) run(ctx context.Context) error {
|
2021-09-05 15:35:04 +00:00
|
|
|
var stream *stream
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if stream != nil {
|
2022-08-04 19:07:17 +00:00
|
|
|
s.parent.sourceStaticImplSetNotReady(pathSourceStaticSetNotReadyReq{})
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-06 14:25:05 +00:00
|
|
|
c, err := hls.NewClient(
|
|
|
|
s.ur,
|
|
|
|
s.fingerprint,
|
|
|
|
s,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.OnTracks(func(tracks []format.Format) error {
|
2022-12-13 19:54:17 +00:00
|
|
|
var medias media.Medias
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2023-01-06 14:25:05 +00:00
|
|
|
for _, track := range tracks {
|
|
|
|
medi := &media.Media{
|
2022-12-13 19:54:17 +00:00
|
|
|
Type: media.TypeVideo,
|
2023-01-06 14:25:05 +00:00
|
|
|
Formats: []format.Format{track},
|
2022-12-13 19:54:17 +00:00
|
|
|
}
|
2023-01-06 14:25:05 +00:00
|
|
|
medias = append(medias, medi)
|
|
|
|
ctrack := track
|
|
|
|
|
|
|
|
switch track.(type) {
|
|
|
|
case *format.H264:
|
|
|
|
c.OnData(track, func(pts time.Duration, dat interface{}) {
|
|
|
|
err := stream.writeData(medi, ctrack, &formatprocessor.DataH264{
|
|
|
|
PTS: pts,
|
|
|
|
AU: dat.([][]byte),
|
|
|
|
NTP: time.Now(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
case *format.MPEG4Audio:
|
|
|
|
c.OnData(track, func(pts time.Duration, dat interface{}) {
|
|
|
|
err := stream.writeData(medi, ctrack, &formatprocessor.DataMPEG4Audio{
|
|
|
|
PTS: pts,
|
|
|
|
AUs: [][]byte{dat.([]byte)},
|
|
|
|
NTP: time.Now(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
|
|
|
})
|
2022-12-13 19:54:17 +00:00
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 11:01:06 +00:00
|
|
|
res := s.parent.sourceStaticImplSetReady(pathSourceStaticSetReadyReq{
|
2022-12-13 19:54:17 +00:00
|
|
|
medias: medias,
|
2022-08-14 11:01:06 +00:00
|
|
|
generateRTPPackets: true,
|
|
|
|
})
|
2022-01-14 22:42:41 +00:00
|
|
|
if res.err != nil {
|
|
|
|
return res.err
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
s.Log(logger.Info, "ready: %s", sourceMediaInfo(medias))
|
2022-01-14 22:42:41 +00:00
|
|
|
stream = res.stream
|
2021-09-05 15:35:04 +00:00
|
|
|
|
|
|
|
return nil
|
2023-01-06 14:25:05 +00:00
|
|
|
})
|
2022-02-19 11:25:23 +00:00
|
|
|
|
2023-01-06 14:25:05 +00:00
|
|
|
c.Start()
|
2021-09-05 15:35:04 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-c.Wait():
|
2022-07-24 16:11:53 +00:00
|
|
|
return err
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2022-07-30 19:51:38 +00:00
|
|
|
case <-ctx.Done():
|
2021-09-05 15:35:04 +00:00
|
|
|
c.Close()
|
|
|
|
<-c.Wait()
|
2022-07-24 16:11:53 +00:00
|
|
|
return nil
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 19:07:17 +00:00
|
|
|
// apiSourceDescribe implements sourceStaticImpl.
|
|
|
|
func (*hlsSource) apiSourceDescribe() interface{} {
|
2021-09-05 15:35:04 +00:00
|
|
|
return struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{"hlsSource"}
|
|
|
|
}
|