forked from gwuhaolin/livego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
executable file
·84 lines (75 loc) · 1.67 KB
/
cache.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
79
80
81
82
83
84
package hls
import (
"bytes"
"container/list"
"fmt"
"sync"
)
const (
maxTSCacheNum = 3
)
var (
ErrNoKey = fmt.Errorf("No key for cache")
)
type TSCacheItem struct {
id string
num int
lock sync.RWMutex
ll *list.List
lm map[string]TSItem
}
func NewTSCacheItem(id string) *TSCacheItem {
return &TSCacheItem{
id: id,
ll: list.New(),
num: maxTSCacheNum,
lm: make(map[string]TSItem),
}
}
func (tcCacheItem *TSCacheItem) ID() string {
return tcCacheItem.id
}
// TODO: found data race, fix it
func (tcCacheItem *TSCacheItem) GenM3U8PlayList() ([]byte, error) {
var seq int
var getSeq bool
var maxDuration int
m3u8body := bytes.NewBuffer(nil)
for e := tcCacheItem.ll.Front(); e != nil; e = e.Next() {
key := e.Value.(string)
v, ok := tcCacheItem.lm[key]
if ok {
if v.Duration > maxDuration {
maxDuration = v.Duration
}
if !getSeq {
getSeq = true
seq = v.SeqNum
}
fmt.Fprintf(m3u8body, "#EXTINF:%.3f,\n%s\n", float64(v.Duration)/float64(1000), v.Name)
}
}
w := bytes.NewBuffer(nil)
fmt.Fprintf(w,
"#EXTM3U\n#EXT-X-VERSION:3\n#EXT-X-ALLOW-CACHE:NO\n#EXT-X-TARGETDURATION:%d\n#EXT-X-MEDIA-SEQUENCE:%d\n\n",
maxDuration/1000+1, seq)
w.Write(m3u8body.Bytes())
return w.Bytes(), nil
}
func (tcCacheItem *TSCacheItem) SetItem(key string, item TSItem) {
if tcCacheItem.ll.Len() == tcCacheItem.num {
e := tcCacheItem.ll.Front()
tcCacheItem.ll.Remove(e)
k := e.Value.(string)
delete(tcCacheItem.lm, k)
}
tcCacheItem.lm[key] = item
tcCacheItem.ll.PushBack(key)
}
func (tcCacheItem *TSCacheItem) GetItem(key string) (TSItem, error) {
item, ok := tcCacheItem.lm[key]
if !ok {
return item, ErrNoKey
}
return item, nil
}