utils.AtomicFlag -> atomic.Bool (#466)
* Replacing hand rolled ion-sfu atomic with uber/atomic * Remove another hand rolled atomic * utils.AtomicFlag -> atomic.Bool
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
"go.uber.org/atomic"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ type LocalRouter struct {
|
|||||||
// channels for each participant
|
// channels for each participant
|
||||||
requestChannels map[string]*MessageChannel
|
requestChannels map[string]*MessageChannel
|
||||||
responseChannels map[string]*MessageChannel
|
responseChannels map[string]*MessageChannel
|
||||||
isStarted utils.AtomicFlag
|
isStarted atomic.Bool
|
||||||
|
|
||||||
rtcMessageChan *MessageChannel
|
rtcMessageChan *MessageChannel
|
||||||
|
|
||||||
@@ -147,7 +147,7 @@ func (r *LocalRouter) OnRTCMessage(callback RTCMessageCallback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *LocalRouter) Start() error {
|
func (r *LocalRouter) Start() error {
|
||||||
if !r.isStarted.TrySet(true) {
|
if r.isStarted.Swap(true) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
go r.statsWorker()
|
go r.statsWorker()
|
||||||
@@ -166,7 +166,7 @@ func (r *LocalRouter) Stop() {
|
|||||||
|
|
||||||
func (r *LocalRouter) statsWorker() {
|
func (r *LocalRouter) statsWorker() {
|
||||||
for {
|
for {
|
||||||
if !r.isStarted.Get() {
|
if !r.isStarted.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// update every 10 seconds
|
// update every 10 seconds
|
||||||
@@ -179,7 +179,7 @@ func (r *LocalRouter) statsWorker() {
|
|||||||
|
|
||||||
func (r *LocalRouter) rtcMessageWorker() {
|
func (r *LocalRouter) rtcMessageWorker() {
|
||||||
// is a new channel available? if so swap to that one
|
// is a new channel available? if so swap to that one
|
||||||
if !r.isStarted.Get() {
|
if !r.isStarted.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/utils"
|
"go.uber.org/atomic"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ type RTCNodeSink struct {
|
|||||||
rc *redis.Client
|
rc *redis.Client
|
||||||
nodeID livekit.NodeID
|
nodeID livekit.NodeID
|
||||||
participantKey livekit.ParticipantKey
|
participantKey livekit.ParticipantKey
|
||||||
isClosed utils.AtomicFlag
|
isClosed atomic.Bool
|
||||||
onClose func()
|
onClose func()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,14 +109,14 @@ func NewRTCNodeSink(rc *redis.Client, nodeID livekit.NodeID, participantKey live
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *RTCNodeSink) WriteMessage(msg proto.Message) error {
|
func (s *RTCNodeSink) WriteMessage(msg proto.Message) error {
|
||||||
if s.isClosed.Get() {
|
if s.isClosed.Load() {
|
||||||
return ErrChannelClosed
|
return ErrChannelClosed
|
||||||
}
|
}
|
||||||
return publishRTCMessage(s.rc, s.nodeID, s.participantKey, msg)
|
return publishRTCMessage(s.rc, s.nodeID, s.participantKey, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RTCNodeSink) Close() {
|
func (s *RTCNodeSink) Close() {
|
||||||
if !s.isClosed.TrySet(true) {
|
if s.isClosed.Swap(true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if s.onClose != nil {
|
if s.onClose != nil {
|
||||||
@@ -132,7 +132,7 @@ type SignalNodeSink struct {
|
|||||||
rc *redis.Client
|
rc *redis.Client
|
||||||
nodeID livekit.NodeID
|
nodeID livekit.NodeID
|
||||||
connectionID livekit.ConnectionID
|
connectionID livekit.ConnectionID
|
||||||
isClosed utils.AtomicFlag
|
isClosed atomic.Bool
|
||||||
onClose func()
|
onClose func()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,14 +145,14 @@ func NewSignalNodeSink(rc *redis.Client, nodeID livekit.NodeID, connectionID liv
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignalNodeSink) WriteMessage(msg proto.Message) error {
|
func (s *SignalNodeSink) WriteMessage(msg proto.Message) error {
|
||||||
if s.isClosed.Get() {
|
if s.isClosed.Load() {
|
||||||
return ErrChannelClosed
|
return ErrChannelClosed
|
||||||
}
|
}
|
||||||
return publishSignalMessage(s.rc, s.nodeID, s.connectionID, msg)
|
return publishSignalMessage(s.rc, s.nodeID, s.connectionID, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignalNodeSink) Close() {
|
func (s *SignalNodeSink) Close() {
|
||||||
if !s.isClosed.TrySet(true) {
|
if s.isClosed.Swap(true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = publishSignalMessage(s.rc, s.nodeID, s.connectionID, &livekit.EndSession{})
|
_ = publishSignalMessage(s.rc, s.nodeID, s.connectionID, &livekit.EndSession{})
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
"github.com/livekit/protocol/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"go.uber.org/atomic"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/routing/selector"
|
"github.com/livekit/livekit-server/pkg/routing/selector"
|
||||||
@@ -31,7 +32,7 @@ type RedisRouter struct {
|
|||||||
|
|
||||||
rc *redis.Client
|
rc *redis.Client
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
isStarted utils.AtomicFlag
|
isStarted atomic.Bool
|
||||||
|
|
||||||
pubsub *redis.PubSub
|
pubsub *redis.PubSub
|
||||||
cancel func()
|
cancel func()
|
||||||
@@ -278,7 +279,7 @@ func (r *RedisRouter) startParticipantRTC(ss *livekit.StartSession, participantK
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisRouter) Start() error {
|
func (r *RedisRouter) Start() error {
|
||||||
if !r.isStarted.TrySet(true) {
|
if r.isStarted.Swap(true) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,7 +304,7 @@ func (r *RedisRouter) Drain() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *RedisRouter) Stop() {
|
func (r *RedisRouter) Stop() {
|
||||||
if !r.isStarted.TrySet(false) {
|
if !r.isStarted.Swap(false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Debugw("stopping RedisRouter")
|
logger.Debugw("stopping RedisRouter")
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func TestTrackInfo(t *testing.T) {
|
|||||||
require.Equal(t, ti.Simulcast, outInfo.Simulcast)
|
require.Equal(t, ti.Simulcast, outInfo.Simulcast)
|
||||||
|
|
||||||
// make it simulcasted
|
// make it simulcasted
|
||||||
mt.simulcasted.TrySet(true)
|
mt.simulcasted.Store(true)
|
||||||
require.True(t, mt.ToProto().Simulcast)
|
require.True(t, mt.ToProto().Simulcast)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||||
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/pion/rtcp"
|
"github.com/pion/rtcp"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/sfu"
|
"github.com/livekit/livekit-server/pkg/sfu"
|
||||||
@@ -25,8 +25,8 @@ const (
|
|||||||
|
|
||||||
type MediaTrackReceiver struct {
|
type MediaTrackReceiver struct {
|
||||||
params MediaTrackReceiverParams
|
params MediaTrackReceiverParams
|
||||||
muted utils.AtomicFlag
|
muted atomic.Bool
|
||||||
simulcasted utils.AtomicFlag
|
simulcasted atomic.Bool
|
||||||
|
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
receiver sfu.TrackReceiver
|
receiver sfu.TrackReceiver
|
||||||
@@ -140,11 +140,11 @@ func (t *MediaTrackReceiver) PublisherIdentity() livekit.ParticipantIdentity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *MediaTrackReceiver) IsSimulcast() bool {
|
func (t *MediaTrackReceiver) IsSimulcast() bool {
|
||||||
return t.simulcasted.Get()
|
return t.simulcasted.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MediaTrackReceiver) SetSimulcast(simulcast bool) {
|
func (t *MediaTrackReceiver) SetSimulcast(simulcast bool) {
|
||||||
t.simulcasted.TrySet(simulcast)
|
t.simulcasted.Store(simulcast)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MediaTrackReceiver) Name() string {
|
func (t *MediaTrackReceiver) Name() string {
|
||||||
@@ -152,11 +152,11 @@ func (t *MediaTrackReceiver) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *MediaTrackReceiver) IsMuted() bool {
|
func (t *MediaTrackReceiver) IsMuted() bool {
|
||||||
return t.muted.Get()
|
return t.muted.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *MediaTrackReceiver) SetMuted(muted bool) {
|
func (t *MediaTrackReceiver) SetMuted(muted bool) {
|
||||||
t.muted.TrySet(muted)
|
t.muted.Store(muted)
|
||||||
|
|
||||||
receiver := t.Receiver()
|
receiver := t.Receiver()
|
||||||
if receiver != nil {
|
if receiver != nil {
|
||||||
@@ -339,7 +339,7 @@ func (t *MediaTrackReceiver) DebugInfo() map[string]interface{} {
|
|||||||
info := map[string]interface{}{
|
info := map[string]interface{}{
|
||||||
"ID": t.ID(),
|
"ID": t.ID(),
|
||||||
"Kind": t.Kind().String(),
|
"Kind": t.Kind().String(),
|
||||||
"PubMuted": t.muted.Get(),
|
"PubMuted": t.muted.Load(),
|
||||||
}
|
}
|
||||||
|
|
||||||
info["DownTracks"] = t.MediaTrackSubscriptions.DebugInfo()
|
info["DownTracks"] = t.MediaTrackSubscriptions.DebugInfo()
|
||||||
|
|||||||
@@ -392,8 +392,8 @@ func (t *MediaTrackSubscriptions) DebugInfo() []map[string]interface{} {
|
|||||||
for _, val := range t.getAllSubscribedTracks() {
|
for _, val := range t.getAllSubscribedTracks() {
|
||||||
if st, ok := val.(*SubscribedTrack); ok {
|
if st, ok := val.(*SubscribedTrack); ok {
|
||||||
dt := st.DownTrack().DebugInfo()
|
dt := st.DownTrack().DebugInfo()
|
||||||
dt["PubMuted"] = st.pubMuted.Get()
|
dt["PubMuted"] = st.pubMuted.Load()
|
||||||
dt["SubMuted"] = st.subMuted.Get()
|
dt["SubMuted"] = st.subMuted.Load()
|
||||||
subscribedTrackInfo = append(subscribedTrackInfo, dt)
|
subscribedTrackInfo = append(subscribedTrackInfo, dt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
@@ -17,6 +16,7 @@ import (
|
|||||||
"github.com/pion/rtcp"
|
"github.com/pion/rtcp"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"go.uber.org/atomic"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
@@ -66,7 +66,7 @@ type ParticipantImpl struct {
|
|||||||
params ParticipantParams
|
params ParticipantParams
|
||||||
publisher *PCTransport
|
publisher *PCTransport
|
||||||
subscriber *PCTransport
|
subscriber *PCTransport
|
||||||
isClosed utils.AtomicFlag
|
isClosed atomic.Bool
|
||||||
permission *livekit.ParticipantPermission
|
permission *livekit.ParticipantPermission
|
||||||
state atomic.Value // livekit.ParticipantInfo_State
|
state atomic.Value // livekit.ParticipantInfo_State
|
||||||
updateCache *lru.Cache
|
updateCache *lru.Cache
|
||||||
@@ -304,7 +304,7 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
|
|||||||
JoinedAt: p.ConnectedAt().Unix(),
|
JoinedAt: p.ConnectedAt().Unix(),
|
||||||
Hidden: p.Hidden(),
|
Hidden: p.Hidden(),
|
||||||
Recorder: p.IsRecorder(),
|
Recorder: p.IsRecorder(),
|
||||||
Version: atomic.AddUint32(&p.version, 1),
|
Version: atomic.NewUint32(p.version).Inc(),
|
||||||
}
|
}
|
||||||
info.Tracks = p.UpTrackManager.ToProto()
|
info.Tracks = p.UpTrackManager.ToProto()
|
||||||
if p.params.Grants != nil {
|
if p.params.Grants != nil {
|
||||||
@@ -478,7 +478,7 @@ func (p *ParticipantImpl) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *ParticipantImpl) Close(sendLeave bool) error {
|
func (p *ParticipantImpl) Close(sendLeave bool) error {
|
||||||
if !p.isClosed.TrySet(true) {
|
if p.isClosed.Swap(true) {
|
||||||
// already closed
|
// already closed
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
package rtc
|
package rtc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bep/debounce"
|
"github.com/bep/debounce"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||||
"github.com/livekit/livekit-server/pkg/sfu"
|
"github.com/livekit/livekit-server/pkg/sfu"
|
||||||
@@ -26,8 +25,8 @@ type SubscribedTrackParams struct {
|
|||||||
}
|
}
|
||||||
type SubscribedTrack struct {
|
type SubscribedTrack struct {
|
||||||
params SubscribedTrackParams
|
params SubscribedTrackParams
|
||||||
subMuted utils.AtomicFlag
|
subMuted atomic.Bool
|
||||||
pubMuted utils.AtomicFlag
|
pubMuted atomic.Bool
|
||||||
settings atomic.Value // *livekit.UpdateTrackSettings
|
settings atomic.Value // *livekit.UpdateTrackSettings
|
||||||
|
|
||||||
onBind func()
|
onBind func()
|
||||||
@@ -78,19 +77,19 @@ func (t *SubscribedTrack) MediaTrack() types.MediaTrack {
|
|||||||
|
|
||||||
// has subscriber indicated it wants to mute this track
|
// has subscriber indicated it wants to mute this track
|
||||||
func (t *SubscribedTrack) IsMuted() bool {
|
func (t *SubscribedTrack) IsMuted() bool {
|
||||||
return t.subMuted.Get()
|
return t.subMuted.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *SubscribedTrack) SetPublisherMuted(muted bool) {
|
func (t *SubscribedTrack) SetPublisherMuted(muted bool) {
|
||||||
t.pubMuted.TrySet(muted)
|
t.pubMuted.Store(muted)
|
||||||
t.updateDownTrackMute()
|
t.updateDownTrackMute()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *SubscribedTrack) UpdateSubscriberSettings(settings *livekit.UpdateTrackSettings) {
|
func (t *SubscribedTrack) UpdateSubscriberSettings(settings *livekit.UpdateTrackSettings) {
|
||||||
visibilityChanged := t.subMuted.TrySet(settings.Disabled)
|
prevDisabled := t.subMuted.Swap(settings.Disabled)
|
||||||
t.settings.Store(settings)
|
t.settings.Store(settings)
|
||||||
// avoid frequent changes to mute & video layers, unless it became visible
|
// avoid frequent changes to mute & video layers, unless it became visible
|
||||||
if visibilityChanged && !settings.Disabled {
|
if prevDisabled != settings.Disabled && !settings.Disabled {
|
||||||
t.UpdateVideoLayer()
|
t.UpdateVideoLayer()
|
||||||
} else {
|
} else {
|
||||||
t.debouncer(t.UpdateVideoLayer)
|
t.debouncer(t.UpdateVideoLayer)
|
||||||
@@ -116,6 +115,6 @@ func (t *SubscribedTrack) UpdateVideoLayer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *SubscribedTrack) updateDownTrackMute() {
|
func (t *SubscribedTrack) updateDownTrackMute() {
|
||||||
muted := t.subMuted.Get() || t.pubMuted.Get()
|
muted := t.subMuted.Load() || t.pubMuted.Load()
|
||||||
t.DownTrack().Mute(muted)
|
t.DownTrack().Mute(muted)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ import (
|
|||||||
"github.com/livekit/protocol/auth"
|
"github.com/livekit/protocol/auth"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/pion/turn/v2"
|
"github.com/pion/turn/v2"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/rs/cors"
|
"github.com/rs/cors"
|
||||||
"github.com/urfave/negroni"
|
"github.com/urfave/negroni"
|
||||||
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
"github.com/livekit/livekit-server/pkg/routing"
|
"github.com/livekit/livekit-server/pkg/routing"
|
||||||
@@ -35,7 +35,7 @@ type LivekitServer struct {
|
|||||||
roomManager *RoomManager
|
roomManager *RoomManager
|
||||||
turnServer *turn.Server
|
turnServer *turn.Server
|
||||||
currentNode routing.LocalNode
|
currentNode routing.LocalNode
|
||||||
running utils.AtomicFlag
|
running atomic.Bool
|
||||||
doneChan chan struct{}
|
doneChan chan struct{}
|
||||||
closedChan chan struct{}
|
closedChan chan struct{}
|
||||||
}
|
}
|
||||||
@@ -127,11 +127,11 @@ func (s *LivekitServer) HTTPPort() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *LivekitServer) IsRunning() bool {
|
func (s *LivekitServer) IsRunning() bool {
|
||||||
return s.running.Get()
|
return s.running.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LivekitServer) Start() error {
|
func (s *LivekitServer) Start() error {
|
||||||
if s.running.Get() {
|
if s.running.Load() {
|
||||||
return errors.New("already running")
|
return errors.New("already running")
|
||||||
}
|
}
|
||||||
s.doneChan = make(chan struct{})
|
s.doneChan = make(chan struct{})
|
||||||
@@ -203,7 +203,7 @@ func (s *LivekitServer) Start() error {
|
|||||||
// give time for Serve goroutine to start
|
// give time for Serve goroutine to start
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
s.running.TrySet(true)
|
s.running.Store(true)
|
||||||
|
|
||||||
<-s.doneChan
|
<-s.doneChan
|
||||||
|
|
||||||
@@ -238,7 +238,7 @@ func (s *LivekitServer) Stop(force bool) {
|
|||||||
}
|
}
|
||||||
partTicker.Stop()
|
partTicker.Stop()
|
||||||
|
|
||||||
if !s.running.TrySet(false) {
|
if !s.running.Swap(false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
|
"go.uber.org/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -43,7 +43,7 @@ type ConnectionStats struct {
|
|||||||
qualityWindows map[uint32]*qualityWindow
|
qualityWindows map[uint32]*qualityWindow
|
||||||
|
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
isClosed utils.AtomicFlag
|
isClosed atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnectionStats(params ConnectionStatsParams) *ConnectionStats {
|
func NewConnectionStats(params ConnectionStatsParams) *ConnectionStats {
|
||||||
@@ -60,7 +60,7 @@ func (cs *ConnectionStats) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *ConnectionStats) Close() {
|
func (cs *ConnectionStats) Close() {
|
||||||
if !cs.isClosed.TrySet(true) {
|
if cs.isClosed.Swap(true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ func (cs *ConnectionStats) GetScore() float32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *ConnectionStats) UpdateWindow(ssrc uint32, extHighestSeqNum uint32, packetsLost uint32, rtt uint32, jitter uint32) {
|
func (cs *ConnectionStats) UpdateWindow(ssrc uint32, extHighestSeqNum uint32, packetsLost uint32, rtt uint32, jitter uint32) {
|
||||||
if cs.isClosed.Get() {
|
if cs.isClosed.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import (
|
|||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/pion/interceptor/pkg/cc"
|
"github.com/pion/interceptor/pkg/cc"
|
||||||
"github.com/pion/rtcp"
|
"github.com/pion/rtcp"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
)
|
)
|
||||||
@@ -153,7 +153,7 @@ type StreamAllocator struct {
|
|||||||
eventChMu sync.RWMutex
|
eventChMu sync.RWMutex
|
||||||
eventCh chan Event
|
eventCh chan Event
|
||||||
|
|
||||||
isStopped utils.AtomicFlag
|
isStopped atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
||||||
@@ -182,7 +182,7 @@ func (s *StreamAllocator) Start() {
|
|||||||
|
|
||||||
func (s *StreamAllocator) Stop() {
|
func (s *StreamAllocator) Stop() {
|
||||||
s.eventChMu.Lock()
|
s.eventChMu.Lock()
|
||||||
if !s.isStopped.TrySet(true) {
|
if s.isStopped.Swap(true) {
|
||||||
s.eventChMu.Unlock()
|
s.eventChMu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -320,7 +320,7 @@ func (s *StreamAllocator) onProbeClusterDone(info ProbeClusterInfo) {
|
|||||||
|
|
||||||
func (s *StreamAllocator) postEvent(event Event) {
|
func (s *StreamAllocator) postEvent(event Event) {
|
||||||
s.eventChMu.RLock()
|
s.eventChMu.RLock()
|
||||||
if s.isStopped.Get() {
|
if s.isStopped.Load() {
|
||||||
s.eventChMu.RUnlock()
|
s.eventChMu.RUnlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -340,7 +340,7 @@ func (s *StreamAllocator) ping() {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
<-ticker.C
|
<-ticker.C
|
||||||
if s.isStopped.Get() {
|
if s.isStopped.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type StreamStatus int32
|
type StreamStatus int32
|
||||||
@@ -54,7 +52,7 @@ type StreamTracker struct {
|
|||||||
// only access by the same goroutine as Observe
|
// only access by the same goroutine as Observe
|
||||||
lastSN uint16
|
lastSN uint16
|
||||||
|
|
||||||
isStopped utils.AtomicFlag
|
isStopped atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamTracker(samplesRequired uint32, cyclesRequired uint64, cycleDuration time.Duration) *StreamTracker {
|
func NewStreamTracker(samplesRequired uint32, cyclesRequired uint64, cycleDuration time.Duration) *StreamTracker {
|
||||||
@@ -110,7 +108,7 @@ func (s *StreamTracker) Start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamTracker) Stop() {
|
func (s *StreamTracker) Stop() {
|
||||||
if !s.isStopped.TrySet(true) {
|
if s.isStopped.Swap(true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,7 +117,7 @@ func (s *StreamTracker) Stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *StreamTracker) Reset() {
|
func (s *StreamTracker) Reset() {
|
||||||
if s.isStopped.Get() {
|
if s.isStopped.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+22
-22
@@ -14,11 +14,11 @@ import (
|
|||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/pion/rtcp"
|
"github.com/pion/rtcp"
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/thoas/go-funk"
|
"github.com/thoas/go-funk"
|
||||||
|
"go.uber.org/atomic"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/rtc"
|
"github.com/livekit/livekit-server/pkg/rtc"
|
||||||
@@ -40,8 +40,8 @@ type RTCClient struct {
|
|||||||
wsLock sync.Mutex
|
wsLock sync.Mutex
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
connected utils.AtomicFlag
|
connected atomic.Bool
|
||||||
iceConnected utils.AtomicFlag
|
iceConnected atomic.Bool
|
||||||
me *webrtc.MediaEngine // optional, populated only when receiving tracks
|
me *webrtc.MediaEngine // optional, populated only when receiving tracks
|
||||||
subscribedTracks map[livekit.ParticipantID][]*webrtc.TrackRemote
|
subscribedTracks map[livekit.ParticipantID][]*webrtc.TrackRemote
|
||||||
localParticipant *livekit.ParticipantInfo
|
localParticipant *livekit.ParticipantInfo
|
||||||
@@ -51,8 +51,8 @@ type RTCClient struct {
|
|||||||
reliableDCSub *webrtc.DataChannel
|
reliableDCSub *webrtc.DataChannel
|
||||||
lossyDC *webrtc.DataChannel
|
lossyDC *webrtc.DataChannel
|
||||||
lossyDCSub *webrtc.DataChannel
|
lossyDCSub *webrtc.DataChannel
|
||||||
publisherConnected utils.AtomicFlag
|
publisherConnected atomic.Bool
|
||||||
publisherNegotiated utils.AtomicFlag
|
publisherNegotiated atomic.Bool
|
||||||
|
|
||||||
// tracks waiting to be acked, cid => trackInfo
|
// tracks waiting to be acked, cid => trackInfo
|
||||||
pendingPublishedTracks map[string]*livekit.TrackInfo
|
pendingPublishedTracks map[string]*livekit.TrackInfo
|
||||||
@@ -216,9 +216,9 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
initialConnect := !c.iceConnected.Get()
|
initialConnect := !c.iceConnected.Load()
|
||||||
c.pendingTrackWriters = nil
|
c.pendingTrackWriters = nil
|
||||||
c.iceConnected.TrySet(true)
|
c.iceConnected.Store(true)
|
||||||
|
|
||||||
if initialConnect && c.OnConnected != nil {
|
if initialConnect && c.OnConnected != nil {
|
||||||
go c.OnConnected()
|
go c.OnConnected()
|
||||||
@@ -231,13 +231,13 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
|||||||
"participant", c.localParticipant.Identity)
|
"participant", c.localParticipant.Identity)
|
||||||
|
|
||||||
if state == webrtc.ICEConnectionStateConnected {
|
if state == webrtc.ICEConnectionStateConnected {
|
||||||
c.publisherConnected.TrySet(true)
|
c.publisherConnected.Store(true)
|
||||||
// check if publisher triggered negotiate (!subscriberPrimary)
|
// check if publisher triggered negotiate (!subscriberPrimary)
|
||||||
if c.publisherNegotiated.Get() {
|
if c.publisherNegotiated.Load() {
|
||||||
c.iceConnected.TrySet(true)
|
c.iceConnected.Store(true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
c.publisherConnected.TrySet(false)
|
c.publisherConnected.Store(false)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -277,7 +277,7 @@ func (c *RTCClient) Run() error {
|
|||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
// if publish only, negotiate
|
// if publish only, negotiate
|
||||||
if !msg.Join.SubscriberPrimary {
|
if !msg.Join.SubscriberPrimary {
|
||||||
c.publisherNegotiated.TrySet(true)
|
c.publisherNegotiated.Store(true)
|
||||||
c.publisher.Negotiate()
|
c.publisher.Negotiate()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,7 +345,7 @@ func (c *RTCClient) WaitUntilConnected() error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("%s could not connect after timeout", id)
|
return fmt.Errorf("%s could not connect after timeout", id)
|
||||||
case <-time.After(10 * time.Millisecond):
|
case <-time.After(10 * time.Millisecond):
|
||||||
if c.iceConnected.Get() {
|
if c.iceConnected.Load() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -409,8 +409,8 @@ func (c *RTCClient) Stop() {
|
|||||||
Leave: &livekit.LeaveRequest{},
|
Leave: &livekit.LeaveRequest{},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
c.connected.TrySet(false)
|
c.connected.Store(false)
|
||||||
c.iceConnected.TrySet(false)
|
c.iceConnected.Store(false)
|
||||||
_ = c.conn.Close()
|
_ = c.conn.Close()
|
||||||
c.publisher.Close()
|
c.publisher.Close()
|
||||||
c.subscriber.Close()
|
c.subscriber.Close()
|
||||||
@@ -486,7 +486,7 @@ func (c *RTCClient) AddTrack(track *webrtc.TrackLocalStaticSample, path string)
|
|||||||
writer = NewTrackWriter(c.ctx, track, path)
|
writer = NewTrackWriter(c.ctx, track, path)
|
||||||
|
|
||||||
// write tracks only after ICE connectivity
|
// write tracks only after ICE connectivity
|
||||||
if c.iceConnected.Get() {
|
if c.iceConnected.Load() {
|
||||||
err = writer.Start()
|
err = writer.Start()
|
||||||
} else {
|
} else {
|
||||||
c.pendingTrackWriters = append(c.pendingTrackWriters, writer)
|
c.pendingTrackWriters = append(c.pendingTrackWriters, writer)
|
||||||
@@ -578,7 +578,7 @@ func (c *RTCClient) GetPublishedTrackIDs() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) ensurePublisherConnected() error {
|
func (c *RTCClient) ensurePublisherConnected() error {
|
||||||
if c.publisherConnected.Get() {
|
if c.publisherConnected.Load() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -587,12 +587,12 @@ func (c *RTCClient) ensurePublisherConnected() error {
|
|||||||
c.publisher.Negotiate()
|
c.publisher.Negotiate()
|
||||||
}
|
}
|
||||||
|
|
||||||
dcOpen := utils.AtomicFlag{}
|
dcOpen := atomic.NewBool(false)
|
||||||
c.reliableDC.OnOpen(func() {
|
c.reliableDC.OnOpen(func() {
|
||||||
dcOpen.TrySet(true)
|
dcOpen.Store(true)
|
||||||
})
|
})
|
||||||
if c.reliableDC.ReadyState() == webrtc.DataChannelStateOpen {
|
if c.reliableDC.ReadyState() == webrtc.DataChannelStateOpen {
|
||||||
dcOpen.TrySet(true)
|
dcOpen.Store(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait until connected, increase wait time since it takes more than 10s sometimes on GH
|
// wait until connected, increase wait time since it takes more than 10s sometimes on GH
|
||||||
@@ -603,7 +603,7 @@ func (c *RTCClient) ensurePublisherConnected() error {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return fmt.Errorf("could not connect publisher after timeout")
|
return fmt.Errorf("could not connect publisher after timeout")
|
||||||
case <-time.After(10 * time.Millisecond):
|
case <-time.After(10 * time.Millisecond):
|
||||||
if c.publisherConnected.Get() && dcOpen.Get() {
|
if c.publisherConnected.Load() && dcOpen.Load() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -660,7 +660,7 @@ func (c *RTCClient) handleAnswer(desc webrtc.SessionDescription) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.connected.TrySet(true) {
|
if c.connected.Swap(true) {
|
||||||
// already connected
|
// already connected
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
"github.com/livekit/protocol/utils"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/testutils"
|
"github.com/livekit/livekit-server/pkg/testutils"
|
||||||
testclient "github.com/livekit/livekit-server/test/client"
|
testclient "github.com/livekit/livekit-server/test/client"
|
||||||
@@ -127,17 +127,17 @@ func scenarioDataPublish(t *testing.T) {
|
|||||||
|
|
||||||
payload := "test bytes"
|
payload := "test bytes"
|
||||||
|
|
||||||
received := utils.AtomicFlag{}
|
received := atomic.NewBool(false)
|
||||||
c2.OnDataReceived = func(data []byte, sid string) {
|
c2.OnDataReceived = func(data []byte, sid string) {
|
||||||
if string(data) == payload && livekit.ParticipantID(sid) == c1.ID() {
|
if string(data) == payload && livekit.ParticipantID(sid) == c1.ID() {
|
||||||
received.TrySet(true)
|
received.Store(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, c1.PublishData([]byte(payload), livekit.DataPacket_RELIABLE))
|
require.NoError(t, c1.PublishData([]byte(payload), livekit.DataPacket_RELIABLE))
|
||||||
|
|
||||||
testutils.WithTimeout(t, func() string {
|
testutils.WithTimeout(t, func() string {
|
||||||
if received.Get() {
|
if received.Load() {
|
||||||
return ""
|
return ""
|
||||||
} else {
|
} else {
|
||||||
return "c2 did not receive published data"
|
return "c2 did not receive published data"
|
||||||
|
|||||||
Reference in New Issue
Block a user