Browse Source

add read timeout to RTMP sources

pull/235/head
aler9 6 years ago
parent
commit
f03ff73ef3
  1. 1
      internal/path/path.go
  2. 25
      internal/sourcertmp/source.go

1
internal/path/path.go

@ -432,6 +432,7 @@ func (pa *Path) startExternalSource() {
} else if strings.HasPrefix(pa.conf.Source, "rtmp://") { } else if strings.HasPrefix(pa.conf.Source, "rtmp://") {
pa.source = sourcertmp.New( pa.source = sourcertmp.New(
pa.conf.Source, pa.conf.Source,
pa.readTimeout,
&pa.sourceWg, &pa.sourceWg,
pa.stats, pa.stats,
pa) pa)

25
internal/sourcertmp/source.go

@ -37,10 +37,11 @@ type Parent interface {
// Source is a RTMP source. // Source is a RTMP source.
type Source struct { type Source struct {
ur string ur string
wg *sync.WaitGroup readTimeout time.Duration
stats *stats.Stats wg *sync.WaitGroup
parent Parent stats *stats.Stats
parent Parent
// in // in
terminate chan struct{} terminate chan struct{}
@ -48,15 +49,17 @@ type Source struct {
// New allocates a Source. // New allocates a Source.
func New(ur string, func New(ur string,
readTimeout time.Duration,
wg *sync.WaitGroup, wg *sync.WaitGroup,
stats *stats.Stats, stats *stats.Stats,
parent Parent) *Source { parent Parent) *Source {
s := &Source{ s := &Source{
ur: ur, ur: ur,
wg: wg, readTimeout: readTimeout,
stats: stats, wg: wg,
parent: parent, stats: stats,
terminate: make(chan struct{}), parent: parent,
terminate: make(chan struct{}),
} }
atomic.AddInt64(s.stats.CountSourcesRtmp, +1) atomic.AddInt64(s.stats.CountSourcesRtmp, +1)
@ -170,6 +173,9 @@ func (s *Source) runInner() bool {
var audioRTCPSender *rtcpsender.RTCPSender var audioRTCPSender *rtcpsender.RTCPSender
var aacEncoder *rtpaac.Encoder var aacEncoder *rtpaac.Encoder
// configuration must be completed within readTimeout
nconn.SetReadDeadline(time.Now().Add(s.readTimeout))
confDone := make(chan error) confDone := make(chan error)
go func() { go func() {
confDone <- func() error { confDone <- func() error {
@ -331,6 +337,7 @@ func (s *Source) runInner() bool {
go func() { go func() {
readerDone <- func() error { readerDone <- func() error {
for { for {
nconn.SetReadDeadline(time.Now().Add(s.readTimeout))
pkt, err := conn.ReadPacket() pkt, err := conn.ReadPacket()
if err != nil { if err != nil {
return err return err

Loading…
Cancel
Save