Fix async requests to Producer

This commit is contained in:
Alexey Khit
2022-08-22 15:40:28 +03:00
parent f251129a2f
commit a9af245ef8
+15
View File
@@ -2,6 +2,7 @@ package streams
import ( import (
"github.com/AlexxIT/go2rtc/pkg/streamer" "github.com/AlexxIT/go2rtc/pkg/streamer"
"sync"
) )
type state byte type state byte
@@ -21,9 +22,13 @@ type Producer struct {
tracks []*streamer.Track tracks []*streamer.Track
state state state state
mx sync.Mutex
} }
func (p *Producer) GetMedias() []*streamer.Media { func (p *Producer) GetMedias() []*streamer.Media {
p.mx.Lock()
defer p.mx.Unlock()
if p.state == stateNone { if p.state == stateNone {
log.Debug().Str("url", p.url).Msg("[streams] probe producer") log.Debug().Str("url", p.url).Msg("[streams] probe producer")
@@ -41,6 +46,9 @@ func (p *Producer) GetMedias() []*streamer.Media {
} }
func (p *Producer) GetTrack(media *streamer.Media, codec *streamer.Codec) *streamer.Track { func (p *Producer) GetTrack(media *streamer.Media, codec *streamer.Codec) *streamer.Track {
p.mx.Lock()
defer p.mx.Unlock()
if p.state == stateMedias { if p.state == stateMedias {
p.state = stateTracks p.state = stateTracks
} }
@@ -61,6 +69,9 @@ func (p *Producer) GetTrack(media *streamer.Media, codec *streamer.Codec) *strea
// internals // internals
func (p *Producer) start() { func (p *Producer) start() {
p.mx.Lock()
defer p.mx.Unlock()
if p.state != stateTracks { if p.state != stateTracks {
return return
} }
@@ -72,6 +83,8 @@ func (p *Producer) start() {
} }
func (p *Producer) stop() { func (p *Producer) stop() {
p.mx.Lock()
log.Debug().Str("url", p.url).Msg("[streams] stop producer") log.Debug().Str("url", p.url).Msg("[streams] stop producer")
if p.element != nil { if p.element != nil {
@@ -82,4 +95,6 @@ func (p *Producer) stop() {
} }
p.tracks = nil p.tracks = nil
p.state = stateNone p.state = stateNone
p.mx.Unlock()
} }