-
Notifications
You must be signed in to change notification settings - Fork 119
/
refresher.go
59 lines (48 loc) · 978 Bytes
/
refresher.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
package mediaserver
import (
"sync"
"time"
)
type Refresher struct {
period int
tracks map[string]*IncomingStreamTrack
ticker *time.Ticker
sync.Mutex
}
func NewRefresher(period int) *Refresher {
refresher := &Refresher{}
refresher.tracks = map[string]*IncomingStreamTrack{}
refresher.period = period
return refresher
}
func (r *Refresher) Add(incom *IncomingStreamTrack) {
if incom.GetMedia() == "video" {
r.Lock()
r.tracks[incom.GetID()] = incom
r.Unlock()
}
if r.ticker == nil {
r.ticker = time.NewTicker(time.Duration(r.period) * time.Millisecond)
go func() {
for _ = range r.ticker.C {
for _, track := range r.tracks {
track.Refresh()
}
}
}()
}
}
func (r *Refresher) AddStream(incoming *IncomingStream) {
for _, track := range incoming.GetTracks() {
if track.GetMedia() == "video" {
r.Add(track)
}
}
}
func (r *Refresher) Stop() {
if r.ticker != nil {
r.ticker.Stop()
r.ticker = nil
}
r.tracks = nil
}