2020-06-30 13:12:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-19 20:39:38 +00:00
|
|
|
"sync"
|
2020-09-19 21:52:06 +00:00
|
|
|
"sync/atomic"
|
2020-06-30 13:12:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aler9/gortsplib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-03 19:10:41 +00:00
|
|
|
sourceRtspRetryInterval = 5 * time.Second
|
2020-06-30 13:12:39 +00:00
|
|
|
)
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
type sourceRtspState int
|
2020-07-29 21:30:42 +00:00
|
|
|
|
|
|
|
const (
|
2020-10-03 19:10:41 +00:00
|
|
|
sourceRtspStateStopped sourceRtspState = iota
|
|
|
|
sourceRtspStateRunning
|
2020-07-29 21:30:42 +00:00
|
|
|
)
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
type sourceRtsp struct {
|
2020-08-31 13:46:03 +00:00
|
|
|
p *program
|
|
|
|
path *path
|
2020-10-03 19:10:41 +00:00
|
|
|
state sourceRtspState
|
2020-08-31 13:46:03 +00:00
|
|
|
tracks []*gortsplib.Track
|
|
|
|
innerRunning bool
|
|
|
|
|
|
|
|
innerTerminate chan struct{}
|
|
|
|
innerDone chan struct{}
|
2020-10-03 19:10:41 +00:00
|
|
|
setState chan sourceRtspState
|
2020-08-31 13:46:03 +00:00
|
|
|
terminate chan struct{}
|
|
|
|
done chan struct{}
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func newSourceRtsp(p *program, path *path) *sourceRtsp {
|
|
|
|
s := &sourceRtsp{
|
2020-08-31 20:29:30 +00:00
|
|
|
p: p,
|
|
|
|
path: path,
|
2020-10-03 19:10:41 +00:00
|
|
|
setState: make(chan sourceRtspState),
|
2020-08-31 20:29:30 +00:00
|
|
|
terminate: make(chan struct{}),
|
|
|
|
done: make(chan struct{}),
|
2020-07-29 21:30:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
atomic.AddInt64(p.countSourcesRtsp, +1)
|
2020-09-19 21:52:06 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
if path.conf.SourceOnDemand {
|
|
|
|
s.state = sourceRtspStateStopped
|
2020-07-29 21:30:42 +00:00
|
|
|
} else {
|
2020-10-03 19:10:41 +00:00
|
|
|
s.state = sourceRtspStateRunning
|
|
|
|
atomic.AddInt64(p.countSourcesRtspRunning, +1)
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 09:42:56 +00:00
|
|
|
return s
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) isPublisher() {}
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) run(initialState sourceRtspState) {
|
2020-09-03 14:31:52 +00:00
|
|
|
s.applyState(initialState)
|
2020-07-29 21:30:42 +00:00
|
|
|
|
|
|
|
outer:
|
2020-08-31 13:31:37 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case state := <-s.setState:
|
2020-08-31 13:46:03 +00:00
|
|
|
s.applyState(state)
|
2020-07-29 21:30:42 +00:00
|
|
|
|
2020-08-31 13:31:37 +00:00
|
|
|
case <-s.terminate:
|
2020-07-29 21:30:42 +00:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:46:03 +00:00
|
|
|
if s.innerRunning {
|
|
|
|
close(s.innerTerminate)
|
|
|
|
<-s.innerDone
|
2020-07-29 21:30:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 13:31:37 +00:00
|
|
|
close(s.setState)
|
2020-07-29 21:30:42 +00:00
|
|
|
close(s.done)
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) applyState(state sourceRtspState) {
|
|
|
|
if state == sourceRtspStateRunning {
|
2020-08-31 13:46:03 +00:00
|
|
|
if !s.innerRunning {
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source started")
|
2020-08-31 13:46:03 +00:00
|
|
|
s.innerRunning = true
|
|
|
|
s.innerTerminate = make(chan struct{})
|
|
|
|
s.innerDone = make(chan struct{})
|
|
|
|
go s.runInner()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if s.innerRunning {
|
|
|
|
close(s.innerTerminate)
|
|
|
|
<-s.innerDone
|
|
|
|
s.innerRunning = false
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source stopped")
|
2020-08-31 13:46:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) runInner() {
|
2020-08-31 13:46:03 +00:00
|
|
|
defer close(s.innerDone)
|
2020-07-29 21:30:42 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
outer:
|
2020-06-30 13:12:39 +00:00
|
|
|
for {
|
2020-10-03 19:10:41 +00:00
|
|
|
ok := s.runInnerInner()
|
|
|
|
if !ok {
|
|
|
|
break outer
|
|
|
|
}
|
2020-07-30 11:31:18 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
t := time.NewTimer(sourceRtspRetryInterval)
|
|
|
|
defer t.Stop()
|
2020-07-30 11:31:18 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
select {
|
|
|
|
case <-s.innerTerminate:
|
|
|
|
break outer
|
|
|
|
case <-t.C:
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 10:34:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) runInnerInner() bool {
|
|
|
|
s.path.log("connecting to rtsp source")
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-07-19 20:39:38 +00:00
|
|
|
var conn *gortsplib.ConnClient
|
2020-06-30 13:12:39 +00:00
|
|
|
var err error
|
2020-10-03 19:10:41 +00:00
|
|
|
dialDone := make(chan struct{}, 1)
|
2020-06-30 13:12:39 +00:00
|
|
|
go func() {
|
2020-10-03 19:10:41 +00:00
|
|
|
defer close(dialDone)
|
2020-07-19 20:39:38 +00:00
|
|
|
conn, err = gortsplib.NewConnClient(gortsplib.ConnClientConf{
|
2020-10-03 19:10:41 +00:00
|
|
|
Host: s.path.conf.sourceUrl.Host,
|
2020-09-20 12:17:36 +00:00
|
|
|
ReadTimeout: s.p.conf.ReadTimeout,
|
|
|
|
WriteTimeout: s.p.conf.WriteTimeout,
|
|
|
|
ReadBufferCount: 2,
|
2020-07-19 20:39:38 +00:00
|
|
|
})
|
2020-06-30 13:12:39 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2020-08-31 13:46:03 +00:00
|
|
|
case <-s.innerTerminate:
|
2020-06-30 13:12:39 +00:00
|
|
|
return false
|
|
|
|
case <-dialDone:
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-06-30 13:12:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
_, err = conn.Options(s.path.conf.sourceUrl)
|
2020-06-30 13:12:39 +00:00
|
|
|
if err != nil {
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-06-30 13:12:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
tracks, _, err := conn.Describe(s.path.conf.sourceUrl)
|
2020-06-30 13:12:39 +00:00
|
|
|
if err != nil {
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-06-30 13:12:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a filtered SDP that is used by the server (not by the client)
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.publisherSdp = tracks.Write()
|
2020-09-05 12:51:36 +00:00
|
|
|
s.path.publisherTrackCount = len(tracks)
|
2020-10-03 19:10:41 +00:00
|
|
|
s.tracks = tracks
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
if s.path.conf.sourceProtocolParsed == gortsplib.StreamProtocolUDP {
|
2020-09-05 11:19:55 +00:00
|
|
|
return s.runUDP(conn)
|
2020-06-30 13:12:39 +00:00
|
|
|
} else {
|
2020-09-05 11:19:55 +00:00
|
|
|
return s.runTCP(conn)
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) runUDP(conn *gortsplib.ConnClient) bool {
|
2020-07-19 20:39:38 +00:00
|
|
|
for _, track := range s.tracks {
|
2020-10-03 19:10:41 +00:00
|
|
|
_, err := conn.SetupUDP(s.path.conf.sourceUrl, gortsplib.SetupModePlay, track, 0, 0)
|
2020-09-20 09:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-09-20 09:41:05 +00:00
|
|
|
return true
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
_, err := conn.Play(s.path.conf.sourceUrl)
|
2020-06-30 13:12:39 +00:00
|
|
|
if err != nil {
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-06-30 13:12:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
s.p.sourceRtspReady <- s
|
|
|
|
s.path.log("rtsp source ready")
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-07-19 20:39:38 +00:00
|
|
|
var wg sync.WaitGroup
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-08-29 13:21:56 +00:00
|
|
|
// receive RTP packets
|
2020-09-27 13:52:48 +00:00
|
|
|
for _, track := range s.tracks {
|
2020-08-29 13:21:56 +00:00
|
|
|
wg.Add(1)
|
2020-09-27 13:52:48 +00:00
|
|
|
go func(track *gortsplib.Track) {
|
2020-07-19 20:39:38 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
2020-09-27 13:52:48 +00:00
|
|
|
buf, err := conn.ReadFrameUDP(track, gortsplib.StreamTypeRtp)
|
2020-07-19 20:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-27 13:52:48 +00:00
|
|
|
s.p.readersMap.forwardFrame(s.path, track.Id,
|
2020-09-20 12:17:36 +00:00
|
|
|
gortsplib.StreamTypeRtp, buf)
|
2020-07-19 20:39:38 +00:00
|
|
|
}
|
2020-09-27 13:52:48 +00:00
|
|
|
}(track)
|
2020-08-29 13:21:56 +00:00
|
|
|
}
|
2020-07-19 20:39:38 +00:00
|
|
|
|
2020-08-29 13:21:56 +00:00
|
|
|
// receive RTCP packets
|
2020-09-27 13:52:48 +00:00
|
|
|
for _, track := range s.tracks {
|
2020-08-29 13:21:56 +00:00
|
|
|
wg.Add(1)
|
2020-09-27 13:52:48 +00:00
|
|
|
go func(track *gortsplib.Track) {
|
2020-07-19 20:39:38 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for {
|
2020-09-27 13:52:48 +00:00
|
|
|
buf, err := conn.ReadFrameUDP(track, gortsplib.StreamTypeRtcp)
|
2020-07-19 20:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-27 13:52:48 +00:00
|
|
|
s.p.readersMap.forwardFrame(s.path, track.Id,
|
2020-09-20 12:17:36 +00:00
|
|
|
gortsplib.StreamTypeRtcp, buf)
|
2020-07-19 20:39:38 +00:00
|
|
|
}
|
2020-09-27 13:52:48 +00:00
|
|
|
}(track)
|
2020-07-19 20:39:38 +00:00
|
|
|
}
|
2020-07-19 15:54:31 +00:00
|
|
|
|
2020-07-19 16:50:43 +00:00
|
|
|
tcpConnDone := make(chan error)
|
2020-07-19 15:54:31 +00:00
|
|
|
go func() {
|
2020-10-03 19:10:41 +00:00
|
|
|
tcpConnDone <- conn.LoopUDP(s.path.conf.sourceUrl)
|
2020-07-19 15:54:31 +00:00
|
|
|
}()
|
|
|
|
|
2020-07-12 10:34:35 +00:00
|
|
|
var ret bool
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-07-12 10:34:35 +00:00
|
|
|
outer:
|
2020-06-30 13:12:39 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-08-31 13:46:03 +00:00
|
|
|
case <-s.innerTerminate:
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-07-19 16:50:43 +00:00
|
|
|
<-tcpConnDone
|
2020-07-12 10:34:35 +00:00
|
|
|
ret = false
|
|
|
|
break outer
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-07-19 15:54:31 +00:00
|
|
|
case err := <-tcpConnDone:
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-07-19 15:54:31 +00:00
|
|
|
ret = true
|
|
|
|
break outer
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 10:34:35 +00:00
|
|
|
|
2020-07-19 20:39:38 +00:00
|
|
|
wg.Wait()
|
2020-07-12 10:34:35 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
s.p.sourceRtspNotReady <- s
|
|
|
|
s.path.log("rtsp source not ready")
|
2020-08-29 13:21:56 +00:00
|
|
|
|
2020-07-12 10:34:35 +00:00
|
|
|
return ret
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (s *sourceRtsp) runTCP(conn *gortsplib.ConnClient) bool {
|
2020-07-19 15:54:31 +00:00
|
|
|
for _, track := range s.tracks {
|
2020-10-03 19:10:41 +00:00
|
|
|
_, err := conn.SetupTCP(s.path.conf.sourceUrl, gortsplib.SetupModePlay, track)
|
2020-06-30 13:12:39 +00:00
|
|
|
if err != nil {
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-06-30 13:12:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
_, err := conn.Play(s.path.conf.sourceUrl)
|
2020-06-30 13:12:39 +00:00
|
|
|
if err != nil {
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-06-30 13:12:39 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
s.p.sourceRtspReady <- s
|
|
|
|
s.path.log("rtsp source ready")
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-07-19 16:50:43 +00:00
|
|
|
tcpConnDone := make(chan error)
|
2020-06-30 13:12:39 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2020-09-27 13:52:48 +00:00
|
|
|
frame, err := conn.ReadFrameTCP()
|
2020-06-30 13:12:39 +00:00
|
|
|
if err != nil {
|
2020-07-19 16:43:39 +00:00
|
|
|
tcpConnDone <- err
|
|
|
|
return
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:08:05 +00:00
|
|
|
s.p.readersMap.forwardFrame(s.path, frame.TrackId, frame.StreamType, frame.Content)
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-12 10:34:35 +00:00
|
|
|
var ret bool
|
|
|
|
|
2020-07-12 20:53:22 +00:00
|
|
|
outer:
|
2020-07-12 10:34:35 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-08-31 13:46:03 +00:00
|
|
|
case <-s.innerTerminate:
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-07-19 16:50:43 +00:00
|
|
|
<-tcpConnDone
|
2020-07-12 10:34:35 +00:00
|
|
|
ret = false
|
2020-07-12 20:53:22 +00:00
|
|
|
break outer
|
2020-07-12 10:34:35 +00:00
|
|
|
|
2020-07-19 16:43:39 +00:00
|
|
|
case err := <-tcpConnDone:
|
2020-08-29 13:21:56 +00:00
|
|
|
conn.Close()
|
2020-10-03 19:10:41 +00:00
|
|
|
s.path.log("rtsp source ERR: %s", err)
|
2020-07-12 10:34:35 +00:00
|
|
|
ret = true
|
2020-07-12 20:53:22 +00:00
|
|
|
break outer
|
2020-07-12 10:34:35 +00:00
|
|
|
}
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|
2020-07-12 10:34:35 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
s.p.sourceRtspNotReady <- s
|
|
|
|
s.path.log("rtsp source not ready")
|
2020-07-12 10:34:35 +00:00
|
|
|
|
|
|
|
return ret
|
2020-06-30 13:12:39 +00:00
|
|
|
}
|