-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframecache.go
78 lines (69 loc) · 1.76 KB
/
framecache.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
70
71
72
73
74
75
76
77
78
package streamcast
import (
"log"
)
type FrameCache struct {
currentFrame uint32
cache []*Frame
cacheSize uint32
}
func NewFrameCache(cacheSize uint32) (fc *FrameCache) {
fc = new(FrameCache)
fc.cacheSize = cacheSize
fc.cache = make([]*Frame, cacheSize)
return fc
}
func (fc *FrameCache) FastForwardTo(frameId uint32) {
// Clear cached frames that we advanced over.
maxToClear := frameId - fc.currentFrame
if debug {
log.Printf("FFWD from %d to %d", fc.currentFrame, frameId)
}
// Don't clear more than 1 window.
if maxToClear > fc.cacheSize {
maxToClear = fc.cacheSize
}
for i := fc.currentFrame; i < fc.currentFrame+maxToClear; i++ {
if debug {
log.Printf("Clearing %d idx %d \n", i, i%fc.cacheSize)
}
fc.cache[i%fc.cacheSize] = nil
}
fc.currentFrame = frameId
}
func (fc *FrameCache) Get(frameId uint32) (f *Frame) {
if fc.currentFrame != frameId {
fc.FastForwardTo(frameId)
}
cacheIndex := fc.currentFrame % fc.cacheSize
if fc.cache[cacheIndex] != nil {
f := fc.cache[cacheIndex]
fc.cache[cacheIndex] = nil
if debug {
log.Printf("Returning from cache %d idx %d \n", fc.currentFrame, cacheIndex)
}
fc.currentFrame++
return f
}
return nil
}
func (fc *FrameCache) Put(f *Frame) {
// Drop frames so far in the future, they're outside our window
distanceFromNow := f.FrameId - fc.currentFrame
if debug {
log.Printf("cache dist from now == %d; cache size == %d", distanceFromNow, fc.cacheSize)
}
if distanceFromNow >= fc.cacheSize {
if debug {
log.Printf("Dropped received frame (data coming faster than expected)")
}
return
}
if debug {
log.Printf("Setting %d idx %d\n", f.FrameId, f.FrameId%fc.cacheSize)
}
cacheidx := f.FrameId % fc.cacheSize
if fc.cache[cacheidx] == nil {
fc.cache[cacheidx] = f
}
}