forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientracklist.go
69 lines (55 loc) · 1.12 KB
/
clientracklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package sfu
import (
"sync"
)
type clientTrackList struct {
mu sync.RWMutex
tracks []iClientTrack
}
func (l *clientTrackList) Add(track iClientTrack) {
l.mu.Lock()
defer l.mu.Unlock()
// TODO: change to non go routine
track.OnEnded(func() {
l.remove(track.ID())
})
l.tracks = append(l.tracks, track)
}
func (l *clientTrackList) remove(id string) {
l.mu.Lock()
defer l.mu.Unlock()
for i, track := range l.tracks {
if track.ID() == id {
l.tracks = append(l.tracks[:i], l.tracks[i+1:]...)
break
}
}
}
func (l *clientTrackList) Get(id string) iClientTrack {
l.mu.RLock()
defer l.mu.RUnlock()
for _, t := range l.tracks {
if t.ID() == id {
return t
}
}
return nil
}
func (l *clientTrackList) Length() int {
l.mu.RLock()
defer l.mu.RUnlock()
return len(l.tracks)
}
func (l *clientTrackList) GetTracks() []iClientTrack {
l.mu.RLock()
defer l.mu.RUnlock()
clientTracks := make([]iClientTrack, len(l.tracks))
copy(clientTracks, l.tracks)
return clientTracks
}
func newClientTrackList() *clientTrackList {
return &clientTrackList{
mu: sync.RWMutex{},
tracks: make([]iClientTrack, 0),
}
}