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
|
2022-12-13 19:54:17 +00:00
|
|
|
var videoMedia *media.Media
|
|
|
|
var audioMedia *media.Media
|
2021-09-05 15:35:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
onTracks := func(videoFormat *format.H264, audioFormat *format.MPEG4Audio) error {
|
|
|
|
var medias media.Medias
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
if videoFormat != nil {
|
|
|
|
videoMedia = &media.Media{
|
|
|
|
Type: media.TypeVideo,
|
|
|
|
Formats: []format.Format{videoFormat},
|
|
|
|
}
|
|
|
|
medias = append(medias, videoMedia)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
if audioFormat != nil {
|
|
|
|
audioMedia = &media.Media{
|
|
|
|
Type: media.TypeAudio,
|
|
|
|
Formats: []format.Format{audioFormat},
|
|
|
|
}
|
|
|
|
medias = append(medias, audioMedia)
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-29 19:46:31 +00:00
|
|
|
onVideoData := func(pts time.Duration, au [][]byte) {
|
2023-01-05 11:54:00 +00:00
|
|
|
err := stream.writeData(videoMedia, videoMedia.Formats[0], &formatprocessor.DataH264{
|
|
|
|
PTS: pts,
|
|
|
|
AU: au,
|
|
|
|
NTP: time.Now(),
|
2022-08-14 11:01:06 +00:00
|
|
|
})
|
2022-11-02 19:52:12 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
2022-02-19 11:25:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-14 10:16:39 +00:00
|
|
|
onAudioData := func(pts time.Duration, au []byte) {
|
2023-01-05 11:54:00 +00:00
|
|
|
err := stream.writeData(audioMedia, audioMedia.Formats[0], &formatprocessor.DataMPEG4Audio{
|
|
|
|
PTS: pts,
|
|
|
|
AUs: [][]byte{au},
|
|
|
|
NTP: time.Now(),
|
2022-08-14 11:01:06 +00:00
|
|
|
})
|
2022-11-02 19:52:12 +00:00
|
|
|
if err != nil {
|
|
|
|
s.Log(logger.Warn, "%v", err)
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 16:57:04 +00:00
|
|
|
c, err := hls.NewClient(
|
2021-09-05 15:35:04 +00:00
|
|
|
s.ur,
|
2021-10-25 19:13:02 +00:00
|
|
|
s.fingerprint,
|
2021-09-05 15:35:04 +00:00
|
|
|
onTracks,
|
2022-02-19 11:25:23 +00:00
|
|
|
onVideoData,
|
|
|
|
onAudioData,
|
2021-09-05 15:35:04 +00:00
|
|
|
s,
|
|
|
|
)
|
2021-10-28 16:57:04 +00:00
|
|
|
if err != nil {
|
2022-07-24 16:11:53 +00:00
|
|
|
return err
|
2021-10-28 16:57:04 +00:00
|
|
|
}
|
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"}
|
|
|
|
}
|