Process header extensions in line (#635)

* WIP commit

* Pass audio config

* Fix test compile
This commit is contained in:
Raja Subramanian
2022-04-20 18:20:28 +05:30
committed by GitHub
parent 1fe485ef0c
commit 6a53891f9f
9 changed files with 155 additions and 161 deletions
-76
View File
@@ -1,76 +0,0 @@
package rtc
import (
"math"
"go.uber.org/atomic"
)
const (
// duration of audio frames for observe window
SilentAudioLevel = 127
)
// keeps track of audio level for a participant
type AudioLevel struct {
levelThreshold uint8
currentLevel *atomic.Uint32
// min duration to be considered active
minActiveDuration uint32
// for Observe goroutine use
// keeps track of current activity
observeLevel uint8
activeDuration uint32 // ms
observedDuration uint32 // ms
durationToObserve uint32 // ms
}
func NewAudioLevel(activeLevel uint8, minPercentile uint8, observeDuration uint32) *AudioLevel {
l := &AudioLevel{
levelThreshold: activeLevel,
minActiveDuration: uint32(minPercentile) * observeDuration / 100,
currentLevel: atomic.NewUint32(SilentAudioLevel),
observeLevel: SilentAudioLevel,
durationToObserve: observeDuration,
}
return l
}
// Observes a new frame, must be called from the same thread
func (l *AudioLevel) Observe(level uint8, durationMs uint32) {
l.observedDuration += durationMs
if level <= l.levelThreshold {
l.activeDuration += durationMs
if l.observeLevel > level {
l.observeLevel = level
}
}
if l.observedDuration >= l.durationToObserve {
// compute and reset
if l.activeDuration >= l.minActiveDuration {
level := uint32(l.observeLevel) - uint32(20*math.Log10(float64(l.activeDuration)/float64(l.durationToObserve)))
l.currentLevel.Store(level)
} else {
l.currentLevel.Store(SilentAudioLevel)
}
l.observeLevel = SilentAudioLevel
l.activeDuration = 0
l.observedDuration = 0
}
}
// returns current audio level, 0 (loudest) to 127 (silent)
func (l *AudioLevel) GetLevel() (uint8, bool) {
level := uint8(l.currentLevel.Load())
active := level != SilentAudioLevel
return level, active
}
// convert decibel back to linear
func ConvertAudioLevel(level uint8) float32 {
const negInv20 = -1.0 / 20
return float32(math.Pow(10, float64(level)*negInv20))
}
-67
View File
@@ -1,67 +0,0 @@
package rtc_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/rtc"
)
const (
samplesPerBatch = 25
defaultActiveLevel = 30
// requires two noisy samples to count
defaultPercentile = 10
defaultObserveDuration = 500 // ms
)
func TestAudioLevel(t *testing.T) {
t.Run("initially to return not noisy, within a few samples", func(t *testing.T) {
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
_, noisy := a.GetLevel()
require.False(t, noisy)
observeSamples(a, 28, 5)
_, noisy = a.GetLevel()
require.False(t, noisy)
})
t.Run("not noisy when all samples are below threshold", func(t *testing.T) {
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
observeSamples(a, 35, 100)
_, noisy := a.GetLevel()
require.False(t, noisy)
})
t.Run("not noisy when less than percentile samples are above threshold", func(t *testing.T) {
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
observeSamples(a, 35, samplesPerBatch-2)
observeSamples(a, 25, 1)
observeSamples(a, 35, 1)
_, noisy := a.GetLevel()
require.False(t, noisy)
})
t.Run("noisy when higher than percentile samples are above threshold", func(t *testing.T) {
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile, defaultObserveDuration)
observeSamples(a, 35, samplesPerBatch-16)
observeSamples(a, 25, 8)
observeSamples(a, 29, 8)
level, noisy := a.GetLevel()
require.True(t, noisy)
require.Less(t, level, uint8(defaultActiveLevel))
require.Greater(t, level, uint8(25))
})
}
func observeSamples(a *rtc.AudioLevel, level uint8, count int) {
for i := 0; i < count; i++ {
a.Observe(level, 20)
}
}
+3 -32
View File
@@ -27,9 +27,6 @@ type MediaTrack struct {
layerSSRCs [livekit.VideoQuality_HIGH + 1]uint32
audioLevelMu sync.RWMutex
audioLevel *AudioLevel
*MediaTrackReceiver
lock sync.RWMutex
@@ -121,24 +118,6 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
return
}
if t.Kind() == livekit.TrackType_AUDIO {
t.audioLevelMu.Lock()
t.audioLevel = NewAudioLevel(t.params.AudioConfig.ActiveLevel, t.params.AudioConfig.MinPercentile, t.params.AudioConfig.UpdateInterval)
buff.OnAudioLevel(func(level uint8, duration uint32) {
t.audioLevelMu.RLock()
defer t.audioLevelMu.RUnlock()
t.audioLevel.Observe(level, duration)
})
t.audioLevelMu.Unlock()
} else if t.Kind() == livekit.TrackType_VIDEO {
if twcc != nil {
buff.OnTransportWideCC(func(sn uint16, timeNS int64, marker bool) {
twcc.Push(sn, timeNS, marker)
})
}
}
rtcpReader.OnPacket(func(bytes []byte) {
pkts, err := rtcp.Unmarshal(bytes)
if err != nil {
@@ -164,7 +143,9 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
t.PublisherID(),
t.params.TrackInfo.Source,
t.params.Logger,
sfu.WithPliThrottle(t.params.PLIThrottleConfig),
twcc,
sfu.WithPliThrottleConfig(t.params.PLIThrottleConfig),
sfu.WithAudioConfig(t.params.AudioConfig),
sfu.WithLoadBalanceThreshold(20),
sfu.WithStreamTrackers(),
)
@@ -221,16 +202,6 @@ func (t *MediaTrack) TrySetSimulcastSSRC(layer uint8, ssrc uint32) {
}
}
func (t *MediaTrack) GetAudioLevel() (level uint8, active bool) {
t.audioLevelMu.RLock()
defer t.audioLevelMu.RUnlock()
if t.audioLevel == nil {
return SilentAudioLevel, false
}
return t.audioLevel.GetLevel()
}
func (t *MediaTrack) GetConnectionScore() float32 {
receiver := t.Receiver()
if receiver == nil {
+10 -1
View File
@@ -15,6 +15,7 @@ import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/audio"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/telemetry"
)
@@ -192,7 +193,6 @@ func (t *MediaTrackReceiver) AddOnClose(f func()) {
// AddSubscriber subscribes sub to current mediaTrack
func (t *MediaTrackReceiver) AddSubscriber(sub types.LocalParticipant) error {
receiver := t.Receiver()
if receiver == nil {
// cannot add, no receiver
return errors.New("cannot subscribe without a receiver in place")
@@ -304,6 +304,15 @@ func (t *MediaTrackReceiver) GetQualityForDimension(width, height uint32) liveki
return quality
}
func (t *MediaTrackReceiver) GetAudioLevel() (uint8, bool) {
receiver := t.Receiver()
if receiver == nil {
return audio.SilentAudioLevel, false
}
return receiver.GetAudioLevel()
}
// handles max loss for audio streams
func (t *MediaTrackReceiver) handleMaxLossFeedback(_ *sfu.DownTrack, report *rtcp.ReceiverReport) {
t.downFracLostLock.Lock()
+3 -4
View File
@@ -23,6 +23,7 @@ import (
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu"
"github.com/livekit/livekit-server/pkg/sfu/audio"
"github.com/livekit/livekit-server/pkg/sfu/connectionquality"
"github.com/livekit/livekit-server/pkg/sfu/twcc"
"github.com/livekit/livekit-server/pkg/telemetry"
@@ -689,7 +690,7 @@ func (p *ParticipantImpl) ICERestart() error {
//
func (p *ParticipantImpl) GetAudioLevel() (level uint8, active bool) {
level = SilentAudioLevel
level = audio.SilentAudioLevel
for _, pt := range p.GetPublishedTracks() {
mediaTrack := pt.(types.LocalMediaTrack)
if mediaTrack.Source() == livekit.TrackSource_MICROPHONE {
@@ -1466,9 +1467,7 @@ func (p *ParticipantImpl) mediaTrackReceived(track *webrtc.TrackRemote, rtpRecei
if p.twcc == nil {
p.twcc = twcc.NewTransportWideCCResponder(ssrc)
p.twcc.OnFeedback(func(pkt rtcp.RawPacket) {
if err := p.publisher.pc.WriteRTCP([]rtcp.Packet{&pkt}); err != nil {
p.params.Logger.Errorw("could not write RTCP to participant", err)
}
p.rtcpCh <- []rtcp.Packet{&pkt}
})
}
p.pendingTracksLock.Unlock()
+3 -2
View File
@@ -16,6 +16,7 @@ import (
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu/audio"
"github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
@@ -130,7 +131,7 @@ func (r *Room) GetActiveSpeakers() []*livekit.SpeakerInfo {
}
speakers = append(speakers, &livekit.SpeakerInfo{
Sid: string(p.ID()),
Level: ConvertAudioLevel(level),
Level: audio.ConvertAudioLevel(level),
Active: active,
})
}
@@ -768,7 +769,7 @@ func (r *Room) audioUpdateWorker() {
smoothValues = make(map[livekit.ParticipantID]float32)
// exponential moving average (EMA), same center of mass with simple moving average (SMA)
smoothFactor = 2 / float32(ss+1)
activeThreshold = ConvertAudioLevel(r.audioConfig.ActiveLevel)
activeThreshold = audio.ConvertAudioLevel(r.audioConfig.ActiveLevel)
}
lastActiveMap := make(map[livekit.ParticipantID]*livekit.SpeakerInfo)
+2 -1
View File
@@ -16,6 +16,7 @@ import (
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
"github.com/livekit/livekit-server/pkg/sfu/audio"
"github.com/livekit/livekit-server/pkg/telemetry"
"github.com/livekit/livekit-server/pkg/telemetry/telemetryfakes"
"github.com/livekit/livekit-server/pkg/testutils"
@@ -360,7 +361,7 @@ func TestActiveSpeakers(t *testing.T) {
p := participants[0].(*typesfakes.FakeLocalParticipant)
op := participants[1].(*typesfakes.FakeLocalParticipant)
p.GetAudioLevelReturns(30, true)
convertedLevel := rtc.ConvertAudioLevel(30)
convertedLevel := audio.ConvertAudioLevel(30)
testutils.WithTimeout(t, func() string {
updates := getActiveSpeakerUpdates(op)