forked from gwuhaolin/livego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwbase.go
executable file
·55 lines (47 loc) · 1.02 KB
/
rwbase.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
package av
import (
"sync"
"time"
)
type RWBaser struct {
lock sync.Mutex
timeout time.Duration
PreTime time.Time
BaseTimestamp uint32
LastVideoTimestamp uint32
LastAudioTimestamp uint32
}
func NewRWBaser(duration time.Duration) RWBaser {
return RWBaser{
timeout: duration,
PreTime: time.Now(),
}
}
func (rw *RWBaser) BaseTimeStamp() uint32 {
return rw.BaseTimestamp
}
func (rw *RWBaser) CalcBaseTimestamp() {
if rw.LastAudioTimestamp > rw.LastVideoTimestamp {
rw.BaseTimestamp = rw.LastAudioTimestamp
} else {
rw.BaseTimestamp = rw.LastVideoTimestamp
}
}
func (rw *RWBaser) RecTimeStamp(timestamp, typeID uint32) {
if typeID == TAG_VIDEO {
rw.LastVideoTimestamp = timestamp
} else if typeID == TAG_AUDIO {
rw.LastAudioTimestamp = timestamp
}
}
func (rw *RWBaser) SetPreTime() {
rw.lock.Lock()
rw.PreTime = time.Now()
rw.lock.Unlock()
}
func (rw *RWBaser) Alive() bool {
rw.lock.Lock()
b := !(time.Now().Sub(rw.PreTime) >= rw.timeout)
rw.lock.Unlock()
return b
}