Make sink private for Track

This commit is contained in:
Alexey Khit
2022-11-04 20:54:35 +03:00
parent 3cbf2465ae
commit 278f7696b6
3 changed files with 27 additions and 17 deletions
+1 -1
View File
@@ -140,7 +140,7 @@ func (p *Producer) reconnect() {
// move sink from old track to new track // move sink from old track to new track
newTrack := p.element.GetTrack(media, codec) newTrack := p.element.GetTrack(media, codec)
newTrack.Sink = oldTrack.Sink newTrack.GetSink(oldTrack)
p.tracks[i] = newTrack p.tracks[i] = newTrack
break break
+1 -1
View File
@@ -129,7 +129,7 @@ func (s *Stream) RemoveConsumer(cons streamer.Consumer) {
var sink bool var sink bool
for _, track := range producer.tracks { for _, track := range producer.tracks {
if len(track.Sink) > 0 { if track.HasSink() {
sink = true sink = true
} }
} }
+25 -15
View File
@@ -12,44 +12,54 @@ type WrapperFunc func(push WriterFunc) WriterFunc
type Track struct { type Track struct {
Codec *Codec Codec *Codec
Direction string Direction string
Sink map[*Track]WriterFunc sink map[*Track]WriterFunc
mx sync.Mutex sinkMu sync.Mutex
} }
func (t *Track) String() string { func (t *Track) String() string {
s := t.Codec.String() s := t.Codec.String()
s += fmt.Sprintf(", sinks=%d", len(t.Sink)) s += fmt.Sprintf(", sinks=%d", len(t.sink))
return s return s
} }
func (t *Track) WriteRTP(p *rtp.Packet) error { func (t *Track) WriteRTP(p *rtp.Packet) error {
t.mx.Lock() t.sinkMu.Lock()
for _, f := range t.Sink { for _, f := range t.sink {
_ = f(p) _ = f(p)
} }
t.mx.Unlock() t.sinkMu.Unlock()
return nil return nil
} }
func (t *Track) Bind(w WriterFunc) *Track { func (t *Track) Bind(w WriterFunc) *Track {
t.mx.Lock() t.sinkMu.Lock()
if t.Sink == nil { if t.sink == nil {
t.Sink = map[*Track]WriterFunc{} t.sink = map[*Track]WriterFunc{}
} }
clone := &Track{ clone := &Track{
Codec: t.Codec, Direction: t.Direction, Sink: t.Sink, Codec: t.Codec, Direction: t.Direction, sink: t.sink,
} }
t.Sink[clone] = w t.sink[clone] = w
t.mx.Unlock() t.sinkMu.Unlock()
return clone return clone
} }
func (t *Track) Unbind() { func (t *Track) Unbind() {
t.mx.Lock() t.sinkMu.Lock()
delete(t.Sink, t) delete(t.sink, t)
t.mx.Unlock() t.sinkMu.Unlock()
}
func (t *Track) GetSink(from *Track) {
t.sink = from.sink
}
func (t *Track) HasSink() bool {
t.sinkMu.Lock()
defer t.sinkMu.Unlock()
return len(t.sink) > 0
} }