Fix stop ffmpeg without matching tracks

This commit is contained in:
Alexey Khit
2022-11-13 22:24:37 +03:00
parent e6d3939c78
commit 4815ce1baf
3 changed files with 23 additions and 27 deletions
+5 -1
View File
@@ -160,9 +160,13 @@ func (p *Producer) stop() {
p.mu.Lock()
defer p.mu.Unlock()
if p.state == stateExternal {
switch p.state {
case stateExternal:
log.Debug().Msgf("[streams] can't stop external producer")
return
case stateNone:
log.Debug().Msgf("[streams] can't stop none producer")
return
}
log.Debug().Msgf("[streams] stop producer url=%s", p.url)
+17 -22
View File
@@ -92,6 +92,7 @@ func (s *Stream) AddConsumer(cons streamer.Consumer) (err error) {
}
if len(producers) == 0 {
s.stopProducers()
return errors.New("couldn't find the matching tracks")
}
@@ -110,11 +111,6 @@ func (s *Stream) AddConsumer(cons streamer.Consumer) (err error) {
func (s *Stream) RemoveConsumer(cons streamer.Consumer) {
s.mu.Lock()
for i, consumer := range s.consumers {
if consumer == nil {
log.Warn().Msgf("empty consumer: %+v\n", s)
continue
}
if consumer.element == cons {
// remove consumer pads from all producers
for _, track := range consumer.tracks {
@@ -125,24 +121,9 @@ func (s *Stream) RemoveConsumer(cons streamer.Consumer) {
break
}
}
for _, producer := range s.producers {
if producer == nil {
log.Warn().Msgf("empty producer: %+v\n", s)
continue
}
var sink bool
for _, track := range producer.tracks {
if track.HasSink() {
sink = true
}
}
if !sink {
producer.stop()
}
}
s.mu.Unlock()
s.stopProducers()
}
func (s *Stream) AddProducer(prod streamer.Producer) {
@@ -163,6 +144,20 @@ func (s *Stream) RemoveProducer(prod streamer.Producer) {
s.mu.Unlock()
}
func (s *Stream) stopProducers() {
s.mu.Lock()
producers:
for _, producer := range s.producers {
for _, track := range producer.tracks {
if track.HasSink() {
continue producers
}
}
producer.stop()
}
s.mu.Unlock()
}
//func (s *Stream) Active() bool {
// if len(s.consumers) > 0 {
// return true
+1 -4
View File
@@ -460,10 +460,7 @@ func (c *Conn) Play() (err error) {
}
func (c *Conn) Teardown() (err error) {
if c.state != StatePlay {
return fmt.Errorf("RTSP TEARDOWN from wrong state: %s", c.state)
}
// allow TEARDOWN from any state (ex. ANNOUNCE > SETUP)
req := &tcp.Request{Method: MethodTeardown, URL: c.URL}
return c.Request(req)
}