Fix concurrent map iteration for Track
This commit is contained in:
@@ -3,6 +3,7 @@ package streamer
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WriterFunc func(packet *rtp.Packet) error
|
type WriterFunc func(packet *rtp.Packet) error
|
||||||
@@ -12,6 +13,7 @@ type Track struct {
|
|||||||
Codec *Codec
|
Codec *Codec
|
||||||
Direction string
|
Direction string
|
||||||
Sink map[*Track]WriterFunc
|
Sink map[*Track]WriterFunc
|
||||||
|
mx sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Track) String() string {
|
func (t *Track) String() string {
|
||||||
@@ -21,9 +23,11 @@ func (t *Track) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Track) WriteRTP(p *rtp.Packet) error {
|
func (t *Track) WriteRTP(p *rtp.Packet) error {
|
||||||
|
t.mx.Lock()
|
||||||
for _, f := range t.Sink {
|
for _, f := range t.Sink {
|
||||||
_ = f(p)
|
_ = f(p)
|
||||||
}
|
}
|
||||||
|
t.mx.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,10 +39,14 @@ func (t *Track) Bind(w WriterFunc) *Track {
|
|||||||
clone := &Track{
|
clone := &Track{
|
||||||
Codec: t.Codec, Direction: t.Direction, Sink: t.Sink,
|
Codec: t.Codec, Direction: t.Direction, Sink: t.Sink,
|
||||||
}
|
}
|
||||||
|
t.mx.Lock()
|
||||||
t.Sink[clone] = w
|
t.Sink[clone] = w
|
||||||
|
t.mx.Unlock()
|
||||||
return clone
|
return clone
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Track) Unbind() {
|
func (t *Track) Unbind() {
|
||||||
|
t.mx.Lock()
|
||||||
delete(t.Sink, t)
|
delete(t.Sink, t)
|
||||||
|
t.mx.Unlock()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user