Convert sort.Slice -> slices.SortFunc (#4472)
* Convert sort.Slice -> slices.SortFunc * active speaker loudness in descending order
This commit is contained in:
@@ -16,7 +16,7 @@ package selector
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"sort"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/thoas/go-funk"
|
"github.com/thoas/go-funk"
|
||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/config"
|
"github.com/livekit/livekit-server/pkg/config"
|
||||||
|
"github.com/livekit/livekit-server/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const AvailableSeconds = 5
|
const AvailableSeconds = 5
|
||||||
@@ -124,42 +125,42 @@ func selectLowestSortedNode(nodes []*livekit.Node, sortBy string) (*livekit.Node
|
|||||||
idx := funk.RandomInt(0, len(nodes))
|
idx := funk.RandomInt(0, len(nodes))
|
||||||
return nodes[idx], nil
|
return nodes[idx], nil
|
||||||
case "sysload":
|
case "sysload":
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *livekit.Node) int {
|
||||||
return GetNodeSysload(nodes[i]) < GetNodeSysload(nodes[j])
|
return utils.Signum(GetNodeSysload(a) - GetNodeSysload(b))
|
||||||
})
|
})
|
||||||
return nodes[0], nil
|
return nodes[0], nil
|
||||||
case "cpuload":
|
case "cpuload":
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *livekit.Node) int {
|
||||||
return nodes[i].Stats.CpuLoad < nodes[j].Stats.CpuLoad
|
return utils.Signum(a.Stats.CpuLoad - b.Stats.CpuLoad)
|
||||||
})
|
})
|
||||||
return nodes[0], nil
|
return nodes[0], nil
|
||||||
case "rooms":
|
case "rooms":
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *livekit.Node) int {
|
||||||
return nodes[i].Stats.NumRooms < nodes[j].Stats.NumRooms
|
return utils.Signum(a.Stats.NumRooms - b.Stats.NumRooms)
|
||||||
})
|
})
|
||||||
return nodes[0], nil
|
return nodes[0], nil
|
||||||
case "clients":
|
case "clients":
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *livekit.Node) int {
|
||||||
return nodes[i].Stats.NumClients < nodes[j].Stats.NumClients
|
return utils.Signum(a.Stats.NumClients - b.Stats.NumClients)
|
||||||
})
|
})
|
||||||
return nodes[0], nil
|
return nodes[0], nil
|
||||||
case "tracks":
|
case "tracks":
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *livekit.Node) int {
|
||||||
return nodes[i].Stats.NumTracksIn+nodes[i].Stats.NumTracksOut < nodes[j].Stats.NumTracksIn+nodes[j].Stats.NumTracksOut
|
return utils.Signum((a.Stats.NumTracksIn + a.Stats.NumTracksOut) - (b.Stats.NumTracksIn + b.Stats.NumTracksOut))
|
||||||
})
|
})
|
||||||
return nodes[0], nil
|
return nodes[0], nil
|
||||||
case "bytespersec":
|
case "bytespersec":
|
||||||
sort.Slice(nodes, func(i, j int) bool {
|
slices.SortFunc(nodes, func(a, b *livekit.Node) int {
|
||||||
ratei := &livekit.NodeStatsRate{}
|
ratea := &livekit.NodeStatsRate{}
|
||||||
if len(nodes[i].Stats.Rates) > 0 {
|
if len(a.Stats.Rates) > 0 {
|
||||||
ratei = nodes[i].Stats.Rates[0]
|
ratea = a.Stats.Rates[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
ratej := &livekit.NodeStatsRate{}
|
rateb := &livekit.NodeStatsRate{}
|
||||||
if len(nodes[j].Stats.Rates) > 0 {
|
if len(b.Stats.Rates) > 0 {
|
||||||
ratej = nodes[j].Stats.Rates[0]
|
rateb = b.Stats.Rates[0]
|
||||||
}
|
}
|
||||||
return ratei.BytesIn+ratei.BytesOut < ratej.BytesIn+ratej.BytesOut
|
return utils.Signum((ratea.BytesIn + ratea.BytesOut) - (rateb.BytesIn + rateb.BytesOut))
|
||||||
})
|
})
|
||||||
return nodes[0], nil
|
return nodes[0], nil
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -15,7 +15,8 @@
|
|||||||
package dynacast
|
package dynacast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"slices"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -498,19 +499,25 @@ func TestCodecRegression(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func subscribedCodecsAsString(c1 []*livekit.SubscribedCodec) string {
|
func subscribedCodecsAsString(c1 []*livekit.SubscribedCodec) string {
|
||||||
sort.Slice(c1, func(i, j int) bool { return c1[i].Codec < c1[j].Codec })
|
slices.SortFunc(c1, func(a, b *livekit.SubscribedCodec) int {
|
||||||
var s1 string
|
return strings.Compare(a.Codec, b.Codec)
|
||||||
|
})
|
||||||
|
|
||||||
|
var s1 strings.Builder
|
||||||
for _, c := range c1 {
|
for _, c := range c1 {
|
||||||
s1 += c.String()
|
s1.WriteString(c.String())
|
||||||
}
|
}
|
||||||
return s1
|
return s1.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func subscribedAudioCodecsAsString(c1 []*livekit.SubscribedAudioCodec) string {
|
func subscribedAudioCodecsAsString(c1 []*livekit.SubscribedAudioCodec) string {
|
||||||
sort.Slice(c1, func(i, j int) bool { return c1[i].Codec < c1[j].Codec })
|
slices.SortFunc(c1, func(a, b *livekit.SubscribedAudioCodec) int {
|
||||||
var s1 string
|
return strings.Compare(a.Codec, b.Codec)
|
||||||
|
})
|
||||||
|
|
||||||
|
var s1 strings.Builder
|
||||||
for _, c := range c1 {
|
for _, c := range c1 {
|
||||||
s1 += c.String()
|
s1.WriteString(c.String())
|
||||||
}
|
}
|
||||||
return s1
|
return s1.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -207,8 +206,8 @@ func (t *MediaTrackReceiver) SetupReceiver(receiver sfu.TrackReceiver, priority
|
|||||||
receivers = append(receivers, &simulcastReceiver{TrackReceiver: receiver, priority: priority})
|
receivers = append(receivers, &simulcastReceiver{TrackReceiver: receiver, priority: priority})
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(receivers, func(i, j int) bool {
|
slices.SortFunc(receivers, func(a, b *simulcastReceiver) int {
|
||||||
return receivers[i].Priority() < receivers[j].Priority()
|
return sutils.Signum(a.Priority() - b.Priority())
|
||||||
})
|
})
|
||||||
|
|
||||||
if mid != "" {
|
if mid != "" {
|
||||||
@@ -349,8 +348,8 @@ func (t *MediaTrackReceiver) SetPotentialCodecs(codecs []webrtc.RTPCodecParamete
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Slice(receivers, func(i, j int) bool {
|
slices.SortFunc(receivers, func(a, b *simulcastReceiver) int {
|
||||||
return receivers[i].Priority() < receivers[j].Priority()
|
return sutils.Signum(a.Priority() - b.Priority())
|
||||||
})
|
})
|
||||||
t.receivers = receivers
|
t.receivers = receivers
|
||||||
t.lock.Unlock()
|
t.lock.Unlock()
|
||||||
@@ -1090,9 +1089,7 @@ func (t *MediaTrackReceiver) GetQualityForDimension(mimeType mime.MimeType, widt
|
|||||||
layerSizes = providedSizes
|
layerSizes = providedSizes
|
||||||
// comparing height always
|
// comparing height always
|
||||||
requestedSize = height
|
requestedSize = height
|
||||||
sort.Slice(layerSizes, func(i, j int) bool {
|
slices.Sort(layerSizes)
|
||||||
return layerSizes[i] < layerSizes[j]
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// finds the highest layer with smallest dimensions that still satisfy client demands
|
// finds the highest layer with smallest dimensions that still satisfy client demands
|
||||||
|
|||||||
+2
-3
@@ -20,7 +20,6 @@ import (
|
|||||||
"maps"
|
"maps"
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -393,8 +392,8 @@ func (r *Room) GetActiveSpeakers() []*livekit.SpeakerInfo {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(speakers, func(i, j int) bool {
|
slices.SortFunc(speakers, func(a, b *livekit.SpeakerInfo) int {
|
||||||
return speakers[i].Level > speakers[j].Level
|
return sutils.Signum(b.Level - a.Level)
|
||||||
})
|
})
|
||||||
|
|
||||||
// quantize to smooth out small changes
|
// quantize to smooth out small changes
|
||||||
|
|||||||
@@ -664,7 +664,7 @@ func (b *BufferBase) SendPLI(force bool) {
|
|||||||
pliThrottle := b.pliThrottle
|
pliThrottle := b.pliThrottle
|
||||||
b.RUnlock()
|
b.RUnlock()
|
||||||
|
|
||||||
if (rtpStats == nil && !force) || !rtpStats.CheckAndUpdatePli(pliThrottle, force) {
|
if rtpStats == nil || !rtpStats.CheckAndUpdatePli(pliThrottle, force) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ package buffer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -288,8 +288,14 @@ func ProcessFrameDependencyStructure(structure *dd.FrameDependencyStructure) []D
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sort decode target layer by spatial and temporal from high to low
|
// sort decode target layer by spatial and temporal from high to low
|
||||||
sort.Slice(decodeTargets, func(i, j int) bool {
|
slices.SortFunc(decodeTargets, func(a, b DependencyDescriptorDecodeTarget) int {
|
||||||
return decodeTargets[i].Layer.GreaterThan(decodeTargets[j].Layer)
|
if a.Layer.GreaterThan(b.Layer) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if b.Layer.GreaterThan(a.Layer) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
})
|
})
|
||||||
|
|
||||||
return decodeTargets
|
return decodeTargets
|
||||||
|
|||||||
@@ -159,6 +159,9 @@ func (w *WebRTCReceiver) GetConnectionScoreAndQuality() (float32, livekit.Connec
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *WebRTCReceiver) ssrc(layer int) uint32 {
|
func (w *WebRTCReceiver) ssrc(layer int) uint32 {
|
||||||
|
w.upTracksMu.Lock()
|
||||||
|
defer w.upTracksMu.Unlock()
|
||||||
|
|
||||||
if track := w.upTracks[layer]; track != nil {
|
if track := w.upTracks[layer]; track != nil {
|
||||||
return uint32(track.SSRC())
|
return uint32(track.SSRC())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
package videolayerselector
|
package videolayerselector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
@@ -287,8 +287,14 @@ func createDDFrames(maxLayer buffer.VideoLayer, startFrameNumber uint16) []*buff
|
|||||||
activeBitMask |= 1 << uint(i*int(maxLayer.Temporal+1)+j)
|
activeBitMask |= 1 << uint(i*int(maxLayer.Temporal+1)+j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Slice(decodeTargets, func(i, j int) bool {
|
slices.SortFunc(decodeTargets, func(a, b buffer.DependencyDescriptorDecodeTarget) int {
|
||||||
return decodeTargets[i].Layer.GreaterThan(decodeTargets[j].Layer)
|
if a.Layer.GreaterThan(b.Layer) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if b.Layer.GreaterThan(a.Layer) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
})
|
})
|
||||||
|
|
||||||
chainDiffs := make([]int, int(maxLayer.Spatial)+1)
|
chainDiffs := make([]int, int(maxLayer.Spatial)+1)
|
||||||
|
|||||||
@@ -310,7 +310,7 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
|
|||||||
|
|
||||||
stat := &livekit.AnalyticsStat{
|
stat := &livekit.AnalyticsStat{
|
||||||
MinScore: minScore,
|
MinScore: minScore,
|
||||||
MedianScore: utils.MedianFloat32(scores),
|
MedianScore: utils.Median(scores),
|
||||||
Streams: []*livekit.AnalyticsStream{coalescedStream},
|
Streams: []*livekit.AnalyticsStream{coalescedStream},
|
||||||
Mime: stats[len(stats)-1].Mime, // use the latest Mime
|
Mime: stats[len(stats)-1].Mime, // use the latest Mime
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-6
@@ -14,10 +14,10 @@
|
|||||||
|
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import "sort"
|
import "slices"
|
||||||
|
|
||||||
// MedianFloat32 gets median value for an array of float32
|
// Median gets median value for an array
|
||||||
func MedianFloat32(input []float32) float32 {
|
func Median[T float32](input []T) T {
|
||||||
num := len(input)
|
num := len(input)
|
||||||
switch num {
|
switch num {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -25,9 +25,7 @@ func MedianFloat32(input []float32) float32 {
|
|||||||
case 1:
|
case 1:
|
||||||
return input[0]
|
return input[0]
|
||||||
}
|
}
|
||||||
sort.Slice(input, func(i, j int) bool {
|
slices.Sort(input)
|
||||||
return input[i] < input[j]
|
|
||||||
})
|
|
||||||
if num%2 != 0 {
|
if num%2 != 0 {
|
||||||
return input[num/2]
|
return input[num/2]
|
||||||
}
|
}
|
||||||
@@ -35,3 +33,16 @@ func MedianFloat32(input []float32) float32 {
|
|||||||
right := input[num/2]
|
right := input[num/2]
|
||||||
return (left + right) / 2
|
return (left + right) / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Signum[T int | int32 | float32](val T) int {
|
||||||
|
switch {
|
||||||
|
case val < 0:
|
||||||
|
return -1
|
||||||
|
|
||||||
|
case val > 0:
|
||||||
|
return 1
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user