mirror of
https://github.com/gwuhaolin/livego.git
synced 2021-06-01 09:10:22 +03:00
56 lines
1.0 KiB
Go
Executable File
56 lines
1.0 KiB
Go
Executable File
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
|
|
}
|