normalize sources

This commit is contained in:
aler9
2022-07-24 18:11:53 +02:00
parent 8267f4c3bb
commit 34eb1d1e7a
3 changed files with 206 additions and 203 deletions

View File

@@ -74,9 +74,20 @@ func (s *hlsSource) run() {
outer:
for {
ok := s.runInner()
if !ok {
break outer
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
innerErr := make(chan error)
go func() {
innerErr <- s.runInner(innerCtx)
}()
select {
case err := <-innerErr:
innerCtxCancel()
s.Log(logger.Info, "ERR: %v", err)
case <-s.ctx.Done():
innerCtxCancel()
<-innerErr
}
select {
@@ -89,7 +100,7 @@ outer:
s.ctxCancel()
}
func (s *hlsSource) runInner() bool {
func (s *hlsSource) runInner(innerCtx context.Context) error {
var stream *stream
var videoTrackID int
var audioTrackID int
@@ -198,19 +209,17 @@ func (s *hlsSource) runInner() bool {
s,
)
if err != nil {
s.Log(logger.Info, "ERR: %v", err)
return true
return err
}
select {
case err := <-c.Wait():
s.Log(logger.Info, "ERR: %v", err)
return true
return err
case <-s.ctx.Done():
case <-innerCtx.Done():
c.Close()
<-c.Wait()
return false
return nil
}
}

View File

@@ -61,7 +61,7 @@ func newRTMPSource(
ctxCancel: ctxCancel,
}
s.log(logger.Info, "started")
s.Log(logger.Info, "started")
s.wg.Add(1)
go s.run()
@@ -71,11 +71,11 @@ func newRTMPSource(
// Close closes a Source.
func (s *rtmpSource) close() {
s.log(logger.Info, "stopped")
s.Log(logger.Info, "stopped")
s.ctxCancel()
}
func (s *rtmpSource) log(level logger.Level, format string, args ...interface{}) {
func (s *rtmpSource) Log(level logger.Level, format string, args ...interface{}) {
s.parent.log(level, "[rtmp source] "+format, args...)
}
@@ -84,9 +84,20 @@ func (s *rtmpSource) run() {
outer:
for {
ok := s.runInner()
if !ok {
break outer
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
innerErr := make(chan error)
go func() {
innerErr <- s.runInner(innerCtx)
}()
select {
case err := <-innerErr:
innerCtxCancel()
s.Log(logger.Info, "ERR: %v", err)
case <-s.ctx.Done():
innerCtxCancel()
<-innerErr
}
select {
@@ -99,13 +110,8 @@ outer:
s.ctxCancel()
}
func (s *rtmpSource) runInner() bool {
innerCtx, innerCtxCancel := context.WithCancel(s.ctx)
runErr := make(chan error)
go func() {
runErr <- func() error {
s.log(logger.Debug, "connecting")
func (s *rtmpSource) runInner(innerCtx context.Context) error {
s.Log(logger.Debug, "connecting")
u, err := url.Parse(s.ur)
if err != nil {
@@ -180,7 +186,7 @@ func (s *rtmpSource) runInner() bool {
return res.err
}
s.log(logger.Info, "ready")
s.Log(logger.Info, "ready")
defer func() {
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
@@ -266,20 +272,6 @@ func (s *rtmpSource) runInner() bool {
<-readDone
return nil
}
}()
}()
select {
case err := <-runErr:
innerCtxCancel()
s.log(logger.Info, "ERR: %s", err)
return true
case <-s.ctx.Done():
innerCtxCancel()
<-runErr
return false
}
}
// onSourceAPIDescribe implements source.

View File

@@ -71,7 +71,7 @@ func newRTSPSource(
ctxCancel: ctxCancel,
}
s.log(logger.Info, "started")
s.Log(logger.Info, "started")
s.wg.Add(1)
go s.run()
@@ -80,44 +80,49 @@ func newRTSPSource(
}
func (s *rtspSource) close() {
s.log(logger.Info, "stopped")
s.Log(logger.Info, "stopped")
s.ctxCancel()
}
func (s *rtspSource) log(level logger.Level, format string, args ...interface{}) {
func (s *rtspSource) Log(level logger.Level, format string, args ...interface{}) {
s.parent.log(level, "[rtsp source] "+format, args...)
}
func (s *rtspSource) run() {
defer s.wg.Done()
outer:
for {
ok := func() bool {
ok := s.runInner()
if !ok {
return false
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
innerErr := make(chan error)
go func() {
innerErr <- s.runInner(innerCtx)
}()
select {
case err := <-innerErr:
innerCtxCancel()
s.Log(logger.Info, "ERR: %v", err)
case <-s.ctx.Done():
innerCtxCancel()
<-innerErr
}
select {
case <-time.After(rtspSourceRetryPause):
return true
case <-s.ctx.Done():
return false
}
}()
if !ok {
break
break outer
}
}
s.ctxCancel()
}
func (s *rtspSource) runInner() bool {
s.log(logger.Debug, "connecting")
func (s *rtspSource) runInner(innerCtx context.Context) error {
s.Log(logger.Debug, "connecting")
var tlsConfig *tls.Config
if s.fingerprint != "" {
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
@@ -145,23 +150,21 @@ func (s *rtspSource) runInner() bool {
ReadBufferCount: s.readBufferCount,
AnyPortEnable: s.anyPortEnable,
OnRequest: func(req *base.Request) {
s.log(logger.Debug, "c->s %v", req)
s.Log(logger.Debug, "c->s %v", req)
},
OnResponse: func(res *base.Response) {
s.log(logger.Debug, "s->c %v", res)
s.Log(logger.Debug, "s->c %v", res)
},
}
u, err := url.Parse(s.ur)
if err != nil {
s.log(logger.Info, "ERR: %s", err)
return true
return err
}
err = c.Start(u.Scheme, u.Host)
if err != nil {
s.log(logger.Info, "ERR: %s", err)
return true
return err
}
defer c.Close()
@@ -188,7 +191,7 @@ func (s *rtspSource) runInner() bool {
return res.err
}
s.log(logger.Info, "ready")
s.Log(logger.Info, "ready")
defer func() {
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
@@ -223,13 +226,12 @@ func (s *rtspSource) runInner() bool {
select {
case err := <-readErr:
s.log(logger.Info, "ERR: %s", err)
return true
return err
case <-s.ctx.Done():
case <-innerCtx.Done():
c.Close()
<-readErr
return false
return nil
}
}