From a9af245ef877431829d64bb7a047f96721a07ba8 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Mon, 22 Aug 2022 15:40:28 +0300 Subject: [PATCH] Fix async requests to Producer --- cmd/streams/producer.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/streams/producer.go b/cmd/streams/producer.go index dfb4c64e..38283d27 100644 --- a/cmd/streams/producer.go +++ b/cmd/streams/producer.go @@ -2,6 +2,7 @@ package streams import ( "github.com/AlexxIT/go2rtc/pkg/streamer" + "sync" ) type state byte @@ -21,9 +22,13 @@ type Producer struct { tracks []*streamer.Track state state + mx sync.Mutex } func (p *Producer) GetMedias() []*streamer.Media { + p.mx.Lock() + defer p.mx.Unlock() + if p.state == stateNone { 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 { + p.mx.Lock() + defer p.mx.Unlock() + if p.state == stateMedias { p.state = stateTracks } @@ -61,6 +69,9 @@ func (p *Producer) GetTrack(media *streamer.Media, codec *streamer.Codec) *strea // internals func (p *Producer) start() { + p.mx.Lock() + defer p.mx.Unlock() + if p.state != stateTracks { return } @@ -72,6 +83,8 @@ func (p *Producer) start() { } func (p *Producer) stop() { + p.mx.Lock() + log.Debug().Str("url", p.url).Msg("[streams] stop producer") if p.element != nil { @@ -82,4 +95,6 @@ func (p *Producer) stop() { } p.tracks = nil p.state = stateNone + + p.mx.Unlock() }