mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-10-05 23:56:54 +08:00
normalize sources
This commit is contained in:
@@ -74,9 +74,20 @@ func (s *hlsSource) run() {
|
|||||||
|
|
||||||
outer:
|
outer:
|
||||||
for {
|
for {
|
||||||
ok := s.runInner()
|
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
|
||||||
if !ok {
|
innerErr := make(chan error)
|
||||||
break outer
|
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 {
|
select {
|
||||||
@@ -89,7 +100,7 @@ outer:
|
|||||||
s.ctxCancel()
|
s.ctxCancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *hlsSource) runInner() bool {
|
func (s *hlsSource) runInner(innerCtx context.Context) error {
|
||||||
var stream *stream
|
var stream *stream
|
||||||
var videoTrackID int
|
var videoTrackID int
|
||||||
var audioTrackID int
|
var audioTrackID int
|
||||||
@@ -198,19 +209,17 @@ func (s *hlsSource) runInner() bool {
|
|||||||
s,
|
s,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Log(logger.Info, "ERR: %v", err)
|
return err
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case err := <-c.Wait():
|
case err := <-c.Wait():
|
||||||
s.Log(logger.Info, "ERR: %v", err)
|
return err
|
||||||
return true
|
|
||||||
|
|
||||||
case <-s.ctx.Done():
|
case <-innerCtx.Done():
|
||||||
c.Close()
|
c.Close()
|
||||||
<-c.Wait()
|
<-c.Wait()
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -61,7 +61,7 @@ func newRTMPSource(
|
|||||||
ctxCancel: ctxCancel,
|
ctxCancel: ctxCancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.log(logger.Info, "started")
|
s.Log(logger.Info, "started")
|
||||||
|
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
go s.run()
|
go s.run()
|
||||||
@@ -71,11 +71,11 @@ func newRTMPSource(
|
|||||||
|
|
||||||
// Close closes a Source.
|
// Close closes a Source.
|
||||||
func (s *rtmpSource) close() {
|
func (s *rtmpSource) close() {
|
||||||
s.log(logger.Info, "stopped")
|
s.Log(logger.Info, "stopped")
|
||||||
s.ctxCancel()
|
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...)
|
s.parent.log(level, "[rtmp source] "+format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,9 +84,20 @@ func (s *rtmpSource) run() {
|
|||||||
|
|
||||||
outer:
|
outer:
|
||||||
for {
|
for {
|
||||||
ok := s.runInner()
|
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
|
||||||
if !ok {
|
innerErr := make(chan error)
|
||||||
break outer
|
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 {
|
select {
|
||||||
@@ -99,13 +110,8 @@ outer:
|
|||||||
s.ctxCancel()
|
s.ctxCancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rtmpSource) runInner() bool {
|
func (s *rtmpSource) runInner(innerCtx context.Context) error {
|
||||||
innerCtx, innerCtxCancel := context.WithCancel(s.ctx)
|
s.Log(logger.Debug, "connecting")
|
||||||
|
|
||||||
runErr := make(chan error)
|
|
||||||
go func() {
|
|
||||||
runErr <- func() error {
|
|
||||||
s.log(logger.Debug, "connecting")
|
|
||||||
|
|
||||||
u, err := url.Parse(s.ur)
|
u, err := url.Parse(s.ur)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -180,7 +186,7 @@ func (s *rtmpSource) runInner() bool {
|
|||||||
return res.err
|
return res.err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.log(logger.Info, "ready")
|
s.Log(logger.Info, "ready")
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
|
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
|
||||||
@@ -266,20 +272,6 @@ func (s *rtmpSource) runInner() bool {
|
|||||||
<-readDone
|
<-readDone
|
||||||
return nil
|
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.
|
// onSourceAPIDescribe implements source.
|
||||||
|
@@ -71,7 +71,7 @@ func newRTSPSource(
|
|||||||
ctxCancel: ctxCancel,
|
ctxCancel: ctxCancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.log(logger.Info, "started")
|
s.Log(logger.Info, "started")
|
||||||
|
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
go s.run()
|
go s.run()
|
||||||
@@ -80,44 +80,49 @@ func newRTSPSource(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *rtspSource) close() {
|
func (s *rtspSource) close() {
|
||||||
s.log(logger.Info, "stopped")
|
s.Log(logger.Info, "stopped")
|
||||||
s.ctxCancel()
|
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...)
|
s.parent.log(level, "[rtsp source] "+format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rtspSource) run() {
|
func (s *rtspSource) run() {
|
||||||
defer s.wg.Done()
|
defer s.wg.Done()
|
||||||
|
|
||||||
|
outer:
|
||||||
for {
|
for {
|
||||||
ok := func() bool {
|
innerCtx, innerCtxCancel := context.WithCancel(context.Background())
|
||||||
ok := s.runInner()
|
innerErr := make(chan error)
|
||||||
if !ok {
|
go func() {
|
||||||
return false
|
innerErr <- s.runInner(innerCtx)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-innerErr:
|
||||||
|
innerCtxCancel()
|
||||||
|
s.Log(logger.Info, "ERR: %v", err)
|
||||||
|
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
innerCtxCancel()
|
||||||
|
<-innerErr
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(rtspSourceRetryPause):
|
case <-time.After(rtspSourceRetryPause):
|
||||||
return true
|
|
||||||
case <-s.ctx.Done():
|
case <-s.ctx.Done():
|
||||||
return false
|
break outer
|
||||||
}
|
|
||||||
}()
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s.ctxCancel()
|
s.ctxCancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rtspSource) runInner() bool {
|
func (s *rtspSource) runInner(innerCtx context.Context) error {
|
||||||
s.log(logger.Debug, "connecting")
|
s.Log(logger.Debug, "connecting")
|
||||||
|
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
|
|
||||||
if s.fingerprint != "" {
|
if s.fingerprint != "" {
|
||||||
tlsConfig = &tls.Config{
|
tlsConfig = &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
@@ -145,23 +150,21 @@ func (s *rtspSource) runInner() bool {
|
|||||||
ReadBufferCount: s.readBufferCount,
|
ReadBufferCount: s.readBufferCount,
|
||||||
AnyPortEnable: s.anyPortEnable,
|
AnyPortEnable: s.anyPortEnable,
|
||||||
OnRequest: func(req *base.Request) {
|
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) {
|
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)
|
u, err := url.Parse(s.ur)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log(logger.Info, "ERR: %s", err)
|
return err
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.Start(u.Scheme, u.Host)
|
err = c.Start(u.Scheme, u.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log(logger.Info, "ERR: %s", err)
|
return err
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
@@ -188,7 +191,7 @@ func (s *rtspSource) runInner() bool {
|
|||||||
return res.err
|
return res.err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.log(logger.Info, "ready")
|
s.Log(logger.Info, "ready")
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
|
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
|
||||||
@@ -223,13 +226,12 @@ func (s *rtspSource) runInner() bool {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case err := <-readErr:
|
case err := <-readErr:
|
||||||
s.log(logger.Info, "ERR: %s", err)
|
return err
|
||||||
return true
|
|
||||||
|
|
||||||
case <-s.ctx.Done():
|
case <-innerCtx.Done():
|
||||||
c.Close()
|
c.Close()
|
||||||
<-readErr
|
<-readErr
|
||||||
return false
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user