Apply go modernize suggestions

This commit is contained in:
philipch07
2025-09-14 17:13:05 -04:00
committed by philipch07
parent 781ff736bf
commit 39d1b3cb9d
6 changed files with 21 additions and 53 deletions

View File

@@ -13,6 +13,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@@ -1695,10 +1696,8 @@ func (pc *PeerConnection) handleIncomingSSRC(rtpStream io.Reader, ssrc SSRC) err
if track.fecSsrc != nil && ssrc == *track.fecSsrc { if track.fecSsrc != nil && ssrc == *track.fecSsrc {
return nil return nil
} }
for _, trackSsrc := range track.ssrcs { if slices.Contains(track.ssrcs, ssrc) {
if ssrc == trackSsrc { return nil
return nil
}
} }
} }

View File

@@ -65,12 +65,8 @@ func (reader *H264Reader) read(numToRead int) (data []byte, e error) {
} }
reader.readBuffer = append(reader.readBuffer, reader.tmpReadBuf[0:n]...) reader.readBuffer = append(reader.readBuffer, reader.tmpReadBuf[0:n]...)
} }
var numShouldRead int
if numToRead <= len(reader.readBuffer) { numShouldRead := min(numToRead, len(reader.readBuffer))
numShouldRead = numToRead
} else {
numShouldRead = len(reader.readBuffer)
}
data = reader.readBuffer[0:numShouldRead] data = reader.readBuffer[0:numShouldRead]
reader.readBuffer = reader.readBuffer[numShouldRead:] reader.readBuffer = reader.readBuffer[numShouldRead:]

View File

@@ -72,12 +72,8 @@ func (reader *H265Reader) read(numToRead int) (data []byte, e error) {
} }
reader.readBuffer = append(reader.readBuffer, reader.tmpReadBuf[0:n]...) reader.readBuffer = append(reader.readBuffer, reader.tmpReadBuf[0:n]...)
} }
var numShouldRead int
if numToRead <= len(reader.readBuffer) { numShouldRead := min(numToRead, len(reader.readBuffer))
numShouldRead = numToRead
} else {
numShouldRead = len(reader.readBuffer)
}
data = reader.readBuffer[0:numShouldRead] data = reader.readBuffer[0:numShouldRead]
reader.readBuffer = reader.readBuffer[numShouldRead:] reader.readBuffer = reader.readBuffer[numShouldRead:]

View File

@@ -6,6 +6,7 @@ package samplebuilder
import ( import (
"fmt" "fmt"
"runtime" "runtime"
"slices"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
@@ -52,13 +53,7 @@ func (f *fakeDepacketizer) IsPartitionHead(payload []byte) bool {
return true return true
} }
for _, b := range f.headBytes { return slices.Contains(f.headBytes, payload[0])
if payload[0] == b {
return true
}
}
return false
} }
func (f *fakeDepacketizer) IsPartitionTail(marker bool, _ []byte) bool { func (f *fakeDepacketizer) IsPartitionTail(marker bool, _ []byte) bool {

View File

@@ -3,6 +3,8 @@
package webrtc package webrtc
import "slices"
// RTPTransceiverDirection indicates the direction of the RTPTransceiver. // RTPTransceiverDirection indicates the direction of the RTPTransceiver.
type RTPTransceiverDirection int type RTPTransceiverDirection int
@@ -84,10 +86,8 @@ func haveRTPTransceiverDirectionIntersection(
needle []RTPTransceiverDirection, needle []RTPTransceiverDirection,
) bool { ) bool {
for _, n := range needle { for _, n := range needle {
for _, h := range haystack { if slices.Contains(haystack, n) {
if n == h { return true
return true
}
} }
} }

34
sdp.go
View File

@@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"regexp" "regexp"
"slices"
"strconv" "strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
@@ -35,10 +36,8 @@ type trackDetails struct {
func trackDetailsForSSRC(trackDetails []trackDetails, ssrc SSRC) *trackDetails { func trackDetailsForSSRC(trackDetails []trackDetails, ssrc SSRC) *trackDetails {
for i := range trackDetails { for i := range trackDetails {
for j := range trackDetails[i].ssrcs { if slices.Contains(trackDetails[i].ssrcs, ssrc) {
if trackDetails[i].ssrcs[j] == ssrc { return &trackDetails[i]
return &trackDetails[i]
}
} }
} }
@@ -51,10 +50,8 @@ func trackDetailsForRID(trackDetails []trackDetails, mid, rid string) *trackDeta
continue continue
} }
for j := range trackDetails[i].rids { if slices.Contains(trackDetails[i].rids, rid) {
if trackDetails[i].rids[j] == rid { return &trackDetails[i]
return &trackDetails[i]
}
} }
} }
@@ -64,13 +61,7 @@ func trackDetailsForRID(trackDetails []trackDetails, mid, rid string) *trackDeta
func filterTrackWithSSRC(incomingTracks []trackDetails, ssrc SSRC) []trackDetails { func filterTrackWithSSRC(incomingTracks []trackDetails, ssrc SSRC) []trackDetails {
filtered := []trackDetails{} filtered := []trackDetails{}
doesTrackHaveSSRC := func(t trackDetails) bool { doesTrackHaveSSRC := func(t trackDetails) bool {
for i := range t.ssrcs { return slices.Contains(t.ssrcs, ssrc)
if t.ssrcs[i] == ssrc {
return true
}
}
return false
} }
for i := range incomingTracks { for i := range incomingTracks {
@@ -268,10 +259,7 @@ func trackDetailsFromSDP(
} }
func trackDetailsToRTPReceiveParameters(trackDetails *trackDetails) RTPReceiveParameters { func trackDetailsToRTPReceiveParameters(trackDetails *trackDetails) RTPReceiveParameters {
encodingSize := len(trackDetails.ssrcs) encodingSize := max(len(trackDetails.rids), len(trackDetails.ssrcs))
if len(trackDetails.rids) >= encodingSize {
encodingSize = len(trackDetails.rids)
}
encodings := make([]RTPDecodingParameters, encodingSize) encodings := make([]RTPDecodingParameters, encodingSize)
for i := range encodings { for i := range encodings {
@@ -693,13 +681,7 @@ func bundleMatchFromRemote(matchBundleGroup *string) func(mid string) bool {
bundleTags := strings.Split(*matchBundleGroup, " ") bundleTags := strings.Split(*matchBundleGroup, " ")
return func(midValue string) bool { return func(midValue string) bool {
for _, tag := range bundleTags { return slices.Contains(bundleTags, midValue)
if tag == midValue {
return true
}
}
return false
} }
} }