Replace sync/atomic usage with uber/atomic (#471)

This commit is contained in:
Raja Subramanian
2022-02-28 09:57:17 +05:30
committed by GitHub
parent 933b41e42a
commit 2706dc130f
9 changed files with 67 additions and 68 deletions
+7 -6
View File
@@ -2,7 +2,8 @@ package rtc
import (
"math"
"sync/atomic"
"go.uber.org/atomic"
)
const (
@@ -13,7 +14,7 @@ const (
// keeps track of audio level for a participant
type AudioLevel struct {
levelThreshold uint8
currentLevel uint32
currentLevel *atomic.Uint32
// min duration to be considered active
minActiveDuration uint32
@@ -29,7 +30,7 @@ func NewAudioLevel(activeLevel uint8, minPercentile uint8, observeDuration uint3
l := &AudioLevel{
levelThreshold: activeLevel,
minActiveDuration: uint32(minPercentile) * observeDuration / 100,
currentLevel: SilentAudioLevel,
currentLevel: atomic.NewUint32(SilentAudioLevel),
observeLevel: SilentAudioLevel,
durationToObserve: observeDuration,
}
@@ -51,9 +52,9 @@ func (l *AudioLevel) Observe(level uint8, durationMs uint32) {
// compute and reset
if l.activeDuration >= l.minActiveDuration {
level := uint32(l.observeLevel) - uint32(20*math.Log10(float64(l.activeDuration)/float64(l.durationToObserve)))
atomic.StoreUint32(&l.currentLevel, level)
l.currentLevel.Store(level)
} else {
atomic.StoreUint32(&l.currentLevel, SilentAudioLevel)
l.currentLevel.Store(SilentAudioLevel)
}
l.observeLevel = SilentAudioLevel
l.activeDuration = 0
@@ -63,7 +64,7 @@ func (l *AudioLevel) Observe(level uint8, durationMs uint32) {
// returns current audio level, 0 (loudest) to 127 (silent)
func (l *AudioLevel) GetLevel() (uint8, bool) {
level := uint8(atomic.LoadUint32(&l.currentLevel))
level := uint8(l.currentLevel.Load())
active := level != SilentAudioLevel
return level, active
}
+4 -4
View File
@@ -3,7 +3,6 @@ package rtc
import (
"context"
"sync"
"sync/atomic"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
@@ -15,13 +14,15 @@ import (
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/sfu/twcc"
"github.com/livekit/livekit-server/pkg/telemetry"
"go.uber.org/atomic"
)
// MediaTrack represents a WebRTC track that needs to be forwarded
// Implements MediaTrack and PublishedTrack interface
type MediaTrack struct {
params MediaTrackParams
numUpTracks uint32
numUpTracks atomic.Uint32
buffer *buffer.Buffer
layerSSRCs [livekit.VideoQuality_HIGH + 1]uint32
@@ -186,9 +187,8 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
t.Receiver().(*sfu.WebRTCReceiver).AddUpTrack(track, buff)
atomic.AddUint32(&t.numUpTracks, 1)
// LK-TODO: can remove this completely when VideoLayers protocol becomes the default as it has info from client or if we decide to use TrackInfo.Simulcast
if atomic.LoadUint32(&t.numUpTracks) > 1 || track.RID() != "" {
if t.numUpTracks.Inc() > 1 || track.RID() != "" {
// cannot only rely on numUpTracks since we fire metadata events immediately after the first layer
t.MediaTrackReceiver.SetSimulcast(true)
}
+1 -1
View File
@@ -1,13 +1,13 @@
package rtc
import (
"sync/atomic"
"testing"
"time"
"github.com/livekit/protocol/livekit"
"github.com/pion/webrtc/v3"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"github.com/livekit/livekit-server/pkg/testutils"
)