mirror of
https://github.com/livekit/livekit.git
synced 2026-03-30 19:55:41 +00:00
Related to livekit/protocol#273 This PR adds: - ParticipantResumed - for when ICE restart or migration had occurred - TrackPublishRequested - when we initiate a publication - TrackSubscribeRequested - when we initiate a subscription - TrackMuted - publisher muted track - TrackUnmuted - publisher unmuted track - TrackPublish/TrackSubcribe events will indicate when those actions have been successful, to differentiate.
95 lines
1.5 KiB
Go
95 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/livekit/protocol/livekit"
|
|
)
|
|
|
|
type TimedVersionGenerator interface {
|
|
New() *TimedVersion
|
|
}
|
|
|
|
func NewDefaultTimedVersionGenerator() TimedVersionGenerator {
|
|
return &timedVersionGenerator{}
|
|
}
|
|
|
|
type timedVersionGenerator struct {
|
|
lock sync.Mutex
|
|
ts int64
|
|
ticks int32
|
|
}
|
|
|
|
func (g *timedVersionGenerator) New() *TimedVersion {
|
|
ts := time.Now().UnixMicro()
|
|
var ticks int32
|
|
|
|
g.lock.Lock()
|
|
if ts <= g.ts {
|
|
g.ticks++
|
|
ts = g.ts
|
|
ticks = g.ticks
|
|
} else {
|
|
g.ts = ts
|
|
g.ticks = 0
|
|
}
|
|
g.lock.Unlock()
|
|
|
|
return &TimedVersion{
|
|
ts: ts,
|
|
ticks: ticks,
|
|
}
|
|
}
|
|
|
|
type TimedVersion struct {
|
|
lock sync.RWMutex
|
|
ts int64
|
|
ticks int32
|
|
}
|
|
|
|
func NewTimedVersionFromProto(ptv *livekit.TimedVersion) *TimedVersion {
|
|
return &TimedVersion{
|
|
ts: ptv.UnixMicro,
|
|
ticks: ptv.Ticks,
|
|
}
|
|
}
|
|
|
|
func (t *TimedVersion) Update(other *TimedVersion) {
|
|
t.lock.Lock()
|
|
if other.After(t) {
|
|
t.ts = other.ts
|
|
t.ticks = other.ticks
|
|
}
|
|
t.lock.Unlock()
|
|
}
|
|
|
|
func (t *TimedVersion) After(other *TimedVersion) bool {
|
|
t.lock.RLock()
|
|
defer t.lock.RUnlock()
|
|
|
|
if t.ts == other.ts {
|
|
return t.ticks > other.ticks
|
|
}
|
|
|
|
return t.ts > other.ts
|
|
}
|
|
|
|
func (t *TimedVersion) ToProto() *livekit.TimedVersion {
|
|
t.lock.RLock()
|
|
defer t.lock.RUnlock()
|
|
|
|
return &livekit.TimedVersion{
|
|
UnixMicro: t.ts,
|
|
Ticks: t.ticks,
|
|
}
|
|
}
|
|
|
|
func (t *TimedVersion) String() string {
|
|
t.lock.RLock()
|
|
defer t.lock.RUnlock()
|
|
|
|
return fmt.Sprintf("%d.%d", t.ts, t.ticks)
|
|
}
|