forked from Monibuca/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_track.go
58 lines (52 loc) · 948 Bytes
/
data_track.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
package engine
import (
"sync"
"time"
"unsafe"
)
type DataTrack struct {
RingBuffer
BaseTrack
*LockItem
sync.Locker // 写入锁,可选,单一写入可以不加锁
}
func (s *Stream) NewDataTrack(l sync.Locker) (dt *DataTrack) {
dt = &DataTrack{
Locker: l,
}
dt.Stream = s
dt.Init(s.Context, 256)
dt.setCurrent()
return
}
func (dt *DataTrack) Push(data interface{}) {
if dt.Locker != nil {
dt.Lock()
defer dt.Unlock()
}
dt.Timestamp = time.Now()
dt.addBytes(int(unsafe.Sizeof(data)))
dt.GetBPS()
if time.Since(dt.ts) > 1000 {
dt.resetBPS()
}
dt.Write(data)
dt.setCurrent()
}
func (at *DataTrack) setCurrent() {
at.LockItem = at.Current()
}
func (dt *DataTrack) Play(onData func(*DataItem), exit1, exit2 <-chan struct{}) {
dr := dt.Clone()
for dp := dr.Read(); ; dp = dr.Read() {
select {
case <-exit1:
return
case <-exit2:
return
default:
onData(dp.(*DataItem))
dr.MoveNext()
}
}
}