1package av 2 3import ( 4 "sync" 5 "time" 6) 7 8type RWBaser struct { 9 lock sync.Mutex 10 timeout time.Duration 11 PreTime time.Time 12 BaseTimestamp uint32 13 LastVideoTimestamp uint32 14 LastAudioTimestamp uint32 15} 16 17func NewRWBaser(duration time.Duration) RWBaser { 18 return RWBaser{ 19 timeout: duration, 20 PreTime: time.Now(), 21 } 22} 23 24func (rw *RWBaser) BaseTimeStamp() uint32 { 25 return rw.BaseTimestamp 26} 27 28func (rw *RWBaser) CalcBaseTimestamp() { 29 if rw.LastAudioTimestamp > rw.LastVideoTimestamp { 30 rw.BaseTimestamp = rw.LastAudioTimestamp 31 } else { 32 rw.BaseTimestamp = rw.LastVideoTimestamp 33 } 34} 35 36func (rw *RWBaser) RecTimeStamp(timestamp, typeID uint32) { 37 if typeID == TAG_VIDEO { 38 rw.LastVideoTimestamp = timestamp 39 } else if typeID == TAG_AUDIO { 40 rw.LastAudioTimestamp = timestamp 41 } 42} 43 44func (rw *RWBaser) SetPreTime() { 45 rw.lock.Lock() 46 rw.PreTime = time.Now() 47 rw.lock.Unlock() 48} 49 50func (rw *RWBaser) Alive() bool { 51 rw.lock.Lock() 52 b := !(time.Now().Sub(rw.PreTime) >= rw.timeout) 53 rw.lock.Unlock() 54 return b 55} 56