From b851041caab675086f187f41eb32e9e77c6800ff Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Sun, 21 Aug 2022 17:51:19 +0300 Subject: [PATCH] Fix concurrent map iteration for Track --- pkg/streamer/track.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/streamer/track.go b/pkg/streamer/track.go index 14bb40f3..2c2944c6 100644 --- a/pkg/streamer/track.go +++ b/pkg/streamer/track.go @@ -3,6 +3,7 @@ package streamer import ( "fmt" "github.com/pion/rtp" + "sync" ) type WriterFunc func(packet *rtp.Packet) error @@ -12,6 +13,7 @@ type Track struct { Codec *Codec Direction string Sink map[*Track]WriterFunc + mx sync.Mutex } func (t *Track) String() string { @@ -21,9 +23,11 @@ func (t *Track) String() string { } func (t *Track) WriteRTP(p *rtp.Packet) error { + t.mx.Lock() for _, f := range t.Sink { _ = f(p) } + t.mx.Unlock() return nil } @@ -35,10 +39,14 @@ func (t *Track) Bind(w WriterFunc) *Track { clone := &Track{ Codec: t.Codec, Direction: t.Direction, Sink: t.Sink, } + t.mx.Lock() t.Sink[clone] = w + t.mx.Unlock() return clone } func (t *Track) Unbind() { + t.mx.Lock() delete(t.Sink, t) + t.mx.Unlock() }