2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-10 21:23:56 +00:00
|
|
|
"context"
|
2021-04-16 20:46:22 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-10-19 20:17:48 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/base"
|
2021-11-22 21:55:12 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/h264"
|
|
|
|
"github.com/aler9/gortsplib/pkg/rtph264"
|
2022-02-17 23:06:27 +00:00
|
|
|
"github.com/pion/rtcp"
|
2022-03-08 11:54:23 +00:00
|
|
|
"github.com/pion/rtp/v2"
|
2020-10-25 10:41:41 +00:00
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2020-10-19 20:17:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-07-24 13:55:42 +00:00
|
|
|
rtspSourceRetryPause = 5 * time.Second
|
2020-10-19 20:17:48 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspSourceParent interface {
|
2021-10-27 19:01:00 +00:00
|
|
|
log(logger.Level, string, ...interface{})
|
|
|
|
onSourceStaticSetReady(req pathSourceStaticSetReadyReq) pathSourceStaticSetReadyRes
|
2022-01-14 22:42:41 +00:00
|
|
|
onSourceStaticSetNotReady(req pathSourceStaticSetNotReadyReq)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspSource struct {
|
2021-01-10 11:55:53 +00:00
|
|
|
ur string
|
2021-09-27 08:36:28 +00:00
|
|
|
proto conf.SourceProtocol
|
2021-06-03 21:51:37 +00:00
|
|
|
anyPortEnable bool
|
2021-04-16 20:46:22 +00:00
|
|
|
fingerprint string
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration
|
|
|
|
writeTimeout conf.StringDuration
|
2021-02-18 22:26:45 +00:00
|
|
|
readBufferCount int
|
|
|
|
readBufferSize int
|
2021-01-10 11:55:53 +00:00
|
|
|
wg *sync.WaitGroup
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspSourceParent
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newRTSPSource(
|
|
|
|
parentCtx context.Context,
|
2021-04-16 20:46:22 +00:00
|
|
|
ur string,
|
2021-09-27 08:36:28 +00:00
|
|
|
proto conf.SourceProtocol,
|
2021-06-03 21:51:37 +00:00
|
|
|
anyPortEnable bool,
|
2021-04-16 20:46:22 +00:00
|
|
|
fingerprint string,
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration,
|
|
|
|
writeTimeout conf.StringDuration,
|
2021-02-18 22:26:45 +00:00
|
|
|
readBufferCount int,
|
|
|
|
readBufferSize int,
|
2020-11-01 15:51:12 +00:00
|
|
|
wg *sync.WaitGroup,
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspSourceParent) *rtspSource {
|
|
|
|
ctx, ctxCancel := context.WithCancel(parentCtx)
|
2021-05-10 19:32:59 +00:00
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
s := &rtspSource{
|
2021-01-10 11:55:53 +00:00
|
|
|
ur: ur,
|
|
|
|
proto: proto,
|
2021-06-03 21:51:37 +00:00
|
|
|
anyPortEnable: anyPortEnable,
|
2021-04-16 20:46:22 +00:00
|
|
|
fingerprint: fingerprint,
|
2021-01-10 11:55:53 +00:00
|
|
|
readTimeout: readTimeout,
|
|
|
|
writeTimeout: writeTimeout,
|
|
|
|
readBufferCount: readBufferCount,
|
2021-02-18 22:26:45 +00:00
|
|
|
readBufferSize: readBufferSize,
|
2021-01-10 11:55:53 +00:00
|
|
|
wg: wg,
|
|
|
|
parent: parent,
|
2021-05-10 19:32:59 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "started")
|
2020-10-25 10:41:41 +00:00
|
|
|
|
2020-11-01 15:51:12 +00:00
|
|
|
s.wg.Add(1)
|
2020-10-25 10:41:41 +00:00
|
|
|
go s.run()
|
2021-05-23 16:43:49 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
func (s *rtspSource) close() {
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "stopped")
|
2021-05-10 19:32:59 +00:00
|
|
|
s.ctxCancel()
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspSource) log(level logger.Level, format string, args ...interface{}) {
|
2021-10-27 19:01:00 +00:00
|
|
|
s.parent.log(level, "[rtsp source] "+format, args...)
|
2020-12-08 11:21:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspSource) run() {
|
2020-11-01 15:51:12 +00:00
|
|
|
defer s.wg.Done()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
|
|
|
for {
|
2020-10-31 16:03:03 +00:00
|
|
|
ok := func() bool {
|
2020-11-01 15:51:12 +00:00
|
|
|
ok := s.runInner()
|
2020-10-31 16:03:03 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2020-10-31 16:03:03 +00:00
|
|
|
select {
|
2021-07-24 13:55:42 +00:00
|
|
|
case <-time.After(rtspSourceRetryPause):
|
2020-10-31 16:03:03 +00:00
|
|
|
return true
|
2021-05-10 19:32:59 +00:00
|
|
|
case <-s.ctx.Done():
|
2020-10-31 16:03:03 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if !ok {
|
|
|
|
break
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 19:32:59 +00:00
|
|
|
|
|
|
|
s.ctxCancel()
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (s *rtspSource) runInner() bool {
|
2021-04-24 18:47:43 +00:00
|
|
|
s.log(logger.Debug, "connecting")
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-12-03 22:15:21 +00:00
|
|
|
var tlsConfig *tls.Config
|
2021-10-25 19:01:29 +00:00
|
|
|
|
2021-12-03 22:15:21 +00:00
|
|
|
if s.fingerprint != "" {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
VerifyConnection: func(cs tls.ConnectionState) error {
|
|
|
|
h := sha256.New()
|
|
|
|
h.Write(cs.PeerCertificates[0].Raw)
|
|
|
|
hstr := hex.EncodeToString(h.Sum(nil))
|
|
|
|
fingerprintLower := strings.ToLower(s.fingerprint)
|
|
|
|
|
|
|
|
if hstr != fingerprintLower {
|
|
|
|
return fmt.Errorf("server fingerprint do not match: expected %s, got %s",
|
|
|
|
fingerprintLower, hstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-10-25 19:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
c := &gortsplib.Client{
|
2021-10-25 19:01:29 +00:00
|
|
|
Transport: s.proto.Transport,
|
|
|
|
TLSConfig: tlsConfig,
|
2021-09-26 21:06:40 +00:00
|
|
|
ReadTimeout: time.Duration(s.readTimeout),
|
|
|
|
WriteTimeout: time.Duration(s.writeTimeout),
|
2021-05-10 21:23:56 +00:00
|
|
|
ReadBufferCount: s.readBufferCount,
|
|
|
|
ReadBufferSize: s.readBufferSize,
|
2021-06-03 21:51:37 +00:00
|
|
|
AnyPortEnable: s.anyPortEnable,
|
2021-05-10 21:23:56 +00:00
|
|
|
OnRequest: func(req *base.Request) {
|
|
|
|
s.log(logger.Debug, "c->s %v", req)
|
|
|
|
},
|
|
|
|
OnResponse: func(res *base.Response) {
|
|
|
|
s.log(logger.Debug, "s->c %v", res)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
u, err := base.ParseURL(s.ur)
|
2020-10-19 20:17:48 +00:00
|
|
|
if err != nil {
|
2020-12-08 11:21:06 +00:00
|
|
|
s.log(logger.Info, "ERR: %s", err)
|
2020-10-19 20:17:48 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
err = c.Start(u.Scheme, u.Host)
|
|
|
|
if err != nil {
|
|
|
|
s.log(logger.Info, "ERR: %s", err)
|
2021-08-10 16:34:10 +00:00
|
|
|
return true
|
|
|
|
}
|
2022-01-23 18:36:33 +00:00
|
|
|
defer c.Close()
|
2021-03-23 19:37:43 +00:00
|
|
|
|
2021-05-10 21:23:56 +00:00
|
|
|
readErr := make(chan error)
|
|
|
|
go func() {
|
2021-11-12 21:29:56 +00:00
|
|
|
readErr <- func() error {
|
|
|
|
tracks, baseURL, _, err := c.Describe(u)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tracks {
|
2021-11-28 11:58:06 +00:00
|
|
|
_, err := c.Setup(true, t, baseURL, 0, 0)
|
2021-11-12 21:29:56 +00:00
|
|
|
if err != nil {
|
2021-11-13 09:26:14 +00:00
|
|
|
return err
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 21:55:12 +00:00
|
|
|
err = s.handleMissingH264Params(c, tracks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
res := s.parent.onSourceStaticSetReady(pathSourceStaticSetReadyReq{
|
2022-01-14 22:42:41 +00:00
|
|
|
source: s,
|
|
|
|
tracks: c.Tracks(),
|
2021-11-12 21:29:56 +00:00
|
|
|
})
|
2022-01-14 22:42:41 +00:00
|
|
|
if res.err != nil {
|
|
|
|
return res.err
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.log(logger.Info, "ready")
|
|
|
|
|
|
|
|
defer func() {
|
2022-01-14 22:42:41 +00:00
|
|
|
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
|
2021-11-12 21:29:56 +00:00
|
|
|
}()
|
|
|
|
|
2022-02-17 23:06:27 +00:00
|
|
|
c.OnPacketRTP = func(trackID int, pkt *rtp.Packet) {
|
2022-03-15 11:44:01 +00:00
|
|
|
res.stream.writePacketRTP(trackID, pkt)
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 23:06:27 +00:00
|
|
|
c.OnPacketRTCP = func(trackID int, pkt rtcp.Packet) {
|
2022-03-15 11:44:01 +00:00
|
|
|
res.stream.writePacketRTCP(trackID, pkt)
|
2021-11-12 21:29:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Play(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Wait()
|
|
|
|
}()
|
2021-05-10 21:23:56 +00:00
|
|
|
}()
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-04-04 17:45:41 +00:00
|
|
|
select {
|
2021-05-10 21:23:56 +00:00
|
|
|
case err := <-readErr:
|
2021-04-04 17:45:41 +00:00
|
|
|
s.log(logger.Info, "ERR: %s", err)
|
|
|
|
return true
|
2021-11-12 21:29:56 +00:00
|
|
|
|
|
|
|
case <-s.ctx.Done():
|
2022-01-25 14:16:11 +00:00
|
|
|
c.Close()
|
2021-11-12 21:29:56 +00:00
|
|
|
<-readErr
|
|
|
|
return false
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-11-22 21:55:12 +00:00
|
|
|
func (s *rtspSource) handleMissingH264Params(c *gortsplib.Client, tracks gortsplib.Tracks) error {
|
2022-01-30 16:36:42 +00:00
|
|
|
h264Track, h264TrackID := func() (*gortsplib.TrackH264, int) {
|
2021-11-22 21:55:12 +00:00
|
|
|
for i, t := range tracks {
|
2022-01-30 16:36:42 +00:00
|
|
|
if th264, ok := t.(*gortsplib.TrackH264); ok {
|
|
|
|
if th264.SPS() == nil {
|
|
|
|
return th264, i
|
|
|
|
}
|
2021-11-22 21:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-30 16:36:42 +00:00
|
|
|
return nil, -1
|
2021-11-22 21:55:12 +00:00
|
|
|
}()
|
|
|
|
if h264TrackID < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:36:42 +00:00
|
|
|
if h264Track.SPS() != nil && h264Track.PPS() != nil {
|
2021-11-22 21:55:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log(logger.Info, "source has not provided H264 parameters (SPS and PPS)"+
|
|
|
|
" inside the SDP; extracting them from the stream...")
|
|
|
|
|
|
|
|
var streamMutex sync.RWMutex
|
|
|
|
var stream *stream
|
|
|
|
decoder := rtph264.NewDecoder()
|
|
|
|
var sps []byte
|
|
|
|
var pps []byte
|
|
|
|
paramsReceived := make(chan struct{})
|
|
|
|
|
2022-02-17 23:06:27 +00:00
|
|
|
c.OnPacketRTP = func(trackID int, pkt *rtp.Packet) {
|
2021-11-22 21:55:12 +00:00
|
|
|
streamMutex.RLock()
|
|
|
|
defer streamMutex.RUnlock()
|
|
|
|
|
|
|
|
if stream == nil {
|
|
|
|
if trackID != h264TrackID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-paramsReceived:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2022-02-17 23:06:27 +00:00
|
|
|
nalus, _, err := decoder.Decode(pkt)
|
2021-11-22 21:55:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nalu := range nalus {
|
|
|
|
typ := h264.NALUType(nalu[0] & 0x1F)
|
|
|
|
switch typ {
|
|
|
|
case h264.NALUTypeSPS:
|
|
|
|
sps = nalu
|
|
|
|
if sps != nil && pps != nil {
|
|
|
|
close(paramsReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
case h264.NALUTypePPS:
|
|
|
|
pps = nalu
|
|
|
|
if sps != nil && pps != nil {
|
|
|
|
close(paramsReceived)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-15 11:44:01 +00:00
|
|
|
stream.writePacketRTP(trackID, pkt)
|
2021-11-22 21:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 23:06:27 +00:00
|
|
|
c.OnPacketRTCP = func(trackID int, pkt rtcp.Packet) {
|
2021-11-22 21:55:12 +00:00
|
|
|
streamMutex.RLock()
|
|
|
|
defer streamMutex.RUnlock()
|
|
|
|
|
|
|
|
if stream != nil {
|
2022-03-15 11:44:01 +00:00
|
|
|
stream.writePacketRTCP(trackID, pkt)
|
2021-11-22 21:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 16:36:42 +00:00
|
|
|
_, err := c.Play(nil)
|
2021-11-22 21:55:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-03 08:58:03 +00:00
|
|
|
readErr := make(chan error)
|
2021-11-22 21:55:12 +00:00
|
|
|
go func() {
|
2022-02-03 08:58:03 +00:00
|
|
|
readErr <- c.Wait()
|
2021-11-22 21:55:12 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
timeout := time.NewTimer(15 * time.Second)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
|
|
|
select {
|
2022-02-03 08:58:03 +00:00
|
|
|
case err := <-readErr:
|
2021-11-22 21:55:12 +00:00
|
|
|
return err
|
|
|
|
|
|
|
|
case <-timeout.C:
|
2022-02-03 08:58:03 +00:00
|
|
|
c.Close()
|
|
|
|
<-readErr
|
2021-11-22 21:55:12 +00:00
|
|
|
return fmt.Errorf("source did not send H264 parameters in time")
|
|
|
|
|
|
|
|
case <-paramsReceived:
|
|
|
|
s.log(logger.Info, "H264 parameters extracted")
|
|
|
|
|
2022-01-30 16:36:42 +00:00
|
|
|
h264Track.SetSPS(sps)
|
|
|
|
h264Track.SetPPS(pps)
|
2021-11-22 21:55:12 +00:00
|
|
|
|
|
|
|
res := s.parent.onSourceStaticSetReady(pathSourceStaticSetReadyReq{
|
2022-01-14 22:42:41 +00:00
|
|
|
source: s,
|
|
|
|
tracks: tracks,
|
2021-11-22 21:55:12 +00:00
|
|
|
})
|
2022-01-14 22:42:41 +00:00
|
|
|
if res.err != nil {
|
2022-02-03 08:58:03 +00:00
|
|
|
c.Close()
|
|
|
|
<-readErr
|
2022-01-14 22:42:41 +00:00
|
|
|
return res.err
|
2021-11-22 21:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func() {
|
|
|
|
streamMutex.Lock()
|
|
|
|
defer streamMutex.Unlock()
|
2022-01-14 22:42:41 +00:00
|
|
|
stream = res.stream
|
2021-11-22 21:55:12 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
s.log(logger.Info, "ready")
|
|
|
|
|
|
|
|
defer func() {
|
2022-01-14 22:42:41 +00:00
|
|
|
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
|
2021-11-22 21:55:12 +00:00
|
|
|
}()
|
|
|
|
|
2022-02-03 08:58:03 +00:00
|
|
|
return <-readErr
|
|
|
|
}
|
2021-11-22 21:55:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onSourceAPIDescribe implements source.
|
|
|
|
func (*rtspSource) onSourceAPIDescribe() interface{} {
|
2021-07-04 16:13:49 +00:00
|
|
|
return struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{"rtspSource"}
|
|
|
|
}
|