Apply modernize changes (#917)

This commit is contained in:
Tristan Matthews
2025-11-02 11:00:34 -05:00
committed by GitHub
parent 24c512c583
commit 69481ce0bd
23 changed files with 59 additions and 66 deletions
+1 -1
View File
@@ -281,7 +281,7 @@ func supportsGetParameter(header base.Header) bool {
return false
}
for _, m := range strings.Split(pub[0], ",") {
for m := range strings.SplitSeq(pub[0], ",") {
if base.Method(strings.Trim(m, " ")) == base.GetParameter {
return true
}
+6 -6
View File
@@ -182,7 +182,7 @@ func TestClientPlayFormats(t *testing.T) {
})
require.NoError(t, err2)
for i := 0; i < 3; i++ {
for i := range 3 {
req, err2 = conn.ReadRequest()
require.NoError(t, err2)
require.Equal(t, base.Setup, req.Method)
@@ -375,7 +375,7 @@ func TestClientPlay(t *testing.T) {
var srtpInCtx [2]*wrappedSRTPContext
var srtpOutCtx [2]*wrappedSRTPContext
for i := 0; i < 2; i++ {
for i := range 2 {
req, err2 = conn.ReadRequest()
require.NoError(t, err2)
require.Equal(t, base.Setup, req.Method)
@@ -514,7 +514,7 @@ func TestClientPlay(t *testing.T) {
// server -> client
for i := 0; i < 2; i++ {
for i := range 2 {
buf := testRTPPacketMarshaled
if ca.secure == "secure" {
@@ -551,7 +551,7 @@ func TestClientPlay(t *testing.T) {
// skip firewall opening
if ca.transport == "udp" {
for i := 0; i < 2; i++ {
for i := range 2 {
buf := make([]byte, 2048)
_, _, err2 = l2s[i].ReadFrom(buf)
require.NoError(t, err2)
@@ -560,7 +560,7 @@ func TestClientPlay(t *testing.T) {
// client -> server
for i := 0; i < 2; i++ {
for i := range 2 {
var buf []byte
switch ca.transport {
@@ -3711,7 +3711,7 @@ func TestClientPlayBackChannel(t *testing.T) {
var l2s [2]net.PacketConn
var clientPorts [2]*[2]int
for i := 0; i < 2; i++ {
for i := range 2 {
req, err2 = conn.ReadRequest()
require.NoError(t, err2)
require.Equal(t, base.Setup, req.Method)
+1 -1
View File
@@ -1349,7 +1349,7 @@ func TestClientRecordRTCPReport(t *testing.T) {
require.NoError(t, err2)
buf = buf[:n]
} else {
for i := 0; i < 2; i++ {
for range 2 {
_, err2 = conn.ReadInterleavedFrame()
require.NoError(t, err2)
}
@@ -13,7 +13,7 @@ func createDummyAudio(pts int64, prevPTS int64) []byte {
n := 0
ret := make([]byte, sampleCount*2)
for i := int64(0); i < sampleCount; i++ {
for i := range sampleCount {
v := int16(amplitude * math.Sin((float64(prevPTS+i)*frequency*math.Pi*2)/sampleRate))
ret[n] = byte(v >> 8)
ret[n+1] = byte(v)
@@ -13,7 +13,7 @@ func createDummyAudio(pts int64, prevPTS int64) []byte {
n := 0
ret := make([]byte, sampleCount*2)
for i := int64(0); i < sampleCount; i++ {
for i := range sampleCount {
v := int16(amplitude * math.Sin((float64(prevPTS+i)*frequency*math.Pi*2)/sampleRate))
ret[n] = byte(v >> 8)
ret[n+1] = byte(v)
@@ -13,7 +13,7 @@ func createDummyAudio(pts int64, prevPTS int64) []byte {
n := 0
ret := make([]byte, sampleCount*2)
for i := int64(0); i < sampleCount; i++ {
for i := range sampleCount {
v := int16(amplitude * math.Sin((float64(prevPTS+i)*frequency*math.Pi*2)/sampleRate))
ret[n] = byte(v >> 8)
ret[n+1] = byte(v)
@@ -13,7 +13,7 @@ func createDummyAudio(pts int64, prevPTS int64) []byte {
n := 0
ret := make([]byte, sampleCount*2)
for i := int64(0); i < sampleCount; i++ {
for i := range sampleCount {
v := int16(amplitude * math.Sin((float64(prevPTS+i)*frequency*math.Pi*2)/sampleRate))
ret[n] = byte(v >> 8)
ret[n+1] = byte(v)
+2 -4
View File
@@ -79,12 +79,10 @@ func TestServerVsExternal(t *testing.T) {
for _, file := range files {
cfile := file
wg.Add(1)
go func() {
wg.Go(func() {
err := buildImage(cfile.Name())
require.NoError(t, err)
wg.Done()
}()
})
}
wg.Wait()
+1 -1
View File
@@ -134,7 +134,7 @@ func FuzzHeaderUnmarshal(f *testing.F) {
}
str := ""
for i := 0; i < 300; i++ {
for range 300 {
str += "Key: val\r\n"
}
f.Add([]byte(str))
+1 -1
View File
@@ -26,7 +26,7 @@ func NewConn(br *bufio.Reader, w io.Writer) *Conn {
}
// Read reads a Request, a Response or an Interleaved frame.
func (c *Conn) Read() (interface{}, error) {
func (c *Conn) Read() (any, error) {
for {
byts, err := c.br.Peek(2)
if err != nil {
+1 -1
View File
@@ -22,7 +22,7 @@ func TestRead(t *testing.T) {
for _, ca := range []struct {
name string
enc []byte
dec interface{}
dec any
}{
{
"request",
+1 -1
View File
@@ -261,7 +261,7 @@ func (m Media) URL(contentBase *base.URL) (*base.URL, error) {
}
// FindFormat finds a certain format among all the formats in the media.
func (m Media) FindFormat(forma interface{}) bool {
func (m Media) FindFormat(forma any) bool {
for _, formak := range m.Formats {
if reflect.TypeOf(formak) == reflect.TypeOf(forma).Elem() {
reflect.ValueOf(forma).Elem().Set(reflect.ValueOf(formak))
+1 -1
View File
@@ -65,7 +65,7 @@ type Session struct {
// FindFormat finds a certain format among all the formats in all the medias of the stream.
// If the format is found, it is inserted into forma, and its media is returned.
func (d *Session) FindFormat(forma interface{}) *Media {
func (d *Session) FindFormat(forma any) *Media {
for _, media := range d.Medias {
ok := media.FindFormat(forma)
if ok {
+1 -1
View File
@@ -60,7 +60,7 @@ func decodeFMTP(enc string) map[string]string {
ret := make(map[string]string)
for _, kv := range strings.Split(enc, ";") {
for kv := range strings.SplitSeq(enc, ";") {
kv = strings.Trim(kv, " ")
if len(kv) == 0 {
+1 -1
View File
@@ -28,7 +28,7 @@ func (h *RTPInfo) Unmarshal(v base.HeaderValue) error {
return fmt.Errorf("value provided multiple times (%v)", v)
}
for _, part := range strings.Split(v[0], ",") {
for part := range strings.SplitSeq(v[0], ",") {
e := &RTPInfoEntry{}
// remove leading spaces
+4 -4
View File
@@ -11,7 +11,7 @@ type RingBuffer struct {
size uint64
mutex sync.Mutex
cond *sync.Cond
buffer []interface{}
buffer []any
readIndex uint64
writeIndex uint64
closed bool
@@ -27,7 +27,7 @@ func New(size uint64) (*RingBuffer, error) {
r := &RingBuffer{
size: size,
buffer: make([]interface{}, size),
buffer: make([]any, size),
}
r.cond = sync.NewCond(&r.mutex)
@@ -62,7 +62,7 @@ func (r *RingBuffer) Reset() {
}
// Push pushes data at the end of the buffer.
func (r *RingBuffer) Push(data interface{}) bool {
func (r *RingBuffer) Push(data any) bool {
r.mutex.Lock()
if r.buffer[r.writeIndex] != nil {
@@ -81,7 +81,7 @@ func (r *RingBuffer) Push(data interface{}) bool {
}
// Pull pulls data from the beginning of the buffer.
func (r *RingBuffer) Pull() (interface{}, bool) {
func (r *RingBuffer) Pull() (any, bool) {
for {
r.mutex.Lock()
+12 -12
View File
@@ -79,15 +79,15 @@ func TestOverflow(t *testing.T) {
r, err := New(32)
require.NoError(t, err)
for i := 0; i < 32; i++ {
for range 32 {
r.Push([]byte{1, 2, 3, 4})
}
ok := r.Push([]byte{5, 6, 7, 8})
require.Equal(t, false, ok)
for i := 0; i < 32; i++ {
var data interface{}
for range 32 {
var data any
data, ok = r.Pull()
require.Equal(t, true, ok)
require.Equal(t, []byte{1, 2, 3, 4}, data)
@@ -100,16 +100,16 @@ func BenchmarkPushPullContinuous(b *testing.B) {
data := make([]byte, 1024)
for n := 0; n < b.N; n++ {
for b.Loop() {
done := make(chan struct{})
go func() {
defer close(done)
for i := 0; i < 1024*8; i++ {
for range 1024 * 8 {
r.Push(data)
}
}()
for i := 0; i < 1024*8; i++ {
for range 1024 * 8 {
r.Pull()
}
@@ -123,17 +123,17 @@ func BenchmarkPushPullPaused5(b *testing.B) {
data := make([]byte, 1024)
for n := 0; n < b.N; n++ {
for b.Loop() {
done := make(chan struct{})
go func() {
defer close(done)
for i := 0; i < 128; i++ {
for range 128 {
r.Push(data)
time.Sleep(5 * time.Millisecond)
}
}()
for i := 0; i < 128; i++ {
for range 128 {
r.Pull()
}
@@ -147,17 +147,17 @@ func BenchmarkPushPullPaused10(b *testing.B) {
data := make([]byte, 1024)
for n := 0; n < b.N; n++ {
for b.Loop() {
done := make(chan struct{})
go func() {
defer close(done)
for i := 0; i < 128; i++ {
for range 128 {
r.Push(data)
time.Sleep(10 * time.Millisecond)
}
}()
for i := 0; i < 128; i++ {
for range 128 {
r.Pull()
}
+4 -8
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/url"
"slices"
"strconv"
"strings"
@@ -41,12 +42,7 @@ func indexOf(element string, data []string) int {
}
func anyOf(element string, data ...string) bool {
for _, v := range data {
if element == v {
return true
}
}
return false
return slices.Contains(data, element)
}
func parsePort(value string) (int, error) {
@@ -467,7 +463,7 @@ func (s *SessionDescription) unmarshalMediaDescription(value string) error {
// <proto>
// Set according to currently registered with IANA
// https://tools.ietf.org/html/rfc4566#section-5.14
for _, proto := range strings.Split(fields[2], "/") {
for proto := range strings.SplitSeq(fields[2], "/") {
if i := indexOf(proto, []string{
"UDP", "RTP", "AVP", "SAVP", "SAVPF",
"MP2T", "TLS", "DTLS", "SCTP", "AVPF", "TCP",
@@ -691,7 +687,7 @@ func (s *SessionDescription) Unmarshal(byts []byte) error {
state := stateInitial
for _, line := range strings.Split(strings.ReplaceAll(str, "\r", ""), "\n") {
for line := range strings.SplitSeq(strings.ReplaceAll(str, "\r", ""), "\n") {
if line == "" {
continue
}
+6 -7
View File
@@ -8,6 +8,7 @@ import (
"errors"
"net"
gourl "net/url"
"slices"
"strconv"
"strings"
"sync"
@@ -47,10 +48,8 @@ func checkMulticastEnabled(multicastIPRange string, query string) bool {
func checkBackChannelsEnabled(header base.Header) bool {
if vals, ok := header["Require"]; ok {
for _, val := range vals {
if val == "www.onvif.org/ver20/backchannel" {
return true
}
if slices.Contains(vals, "www.onvif.org/ver20/backchannel") {
return true
}
}
return false
@@ -210,7 +209,7 @@ type ServerConn struct {
ctx context.Context
ctxCancel func()
propsMutex sync.RWMutex
userData interface{}
userData any
remoteAddr *net.TCPAddr
bc *bytecounter.ByteCounter
conn *conn.Conn
@@ -259,12 +258,12 @@ func (sc *ServerConn) NetConn() net.Conn {
}
// SetUserData sets some user data associated with the connection.
func (sc *ServerConn) SetUserData(v interface{}) {
func (sc *ServerConn) SetUserData(v any) {
sc.userData = v
}
// UserData returns some user data associated with the connection.
func (sc *ServerConn) UserData() interface{} {
func (sc *ServerConn) UserData() any {
return sc.userData
}
+1 -1
View File
@@ -6,7 +6,7 @@ import (
)
// ServerHandler is the interface implemented by all the server handlers.
type ServerHandler interface{}
type ServerHandler any
// ServerHandlerOnConnOpenCtx is the context of OnConnOpen.
type ServerHandlerOnConnOpenCtx struct {
+2 -2
View File
@@ -535,7 +535,7 @@ func TestServerPlaySetupErrorSameUDPPortsAndIP(t *testing.T) {
require.NoError(t, err)
defer stream.Close()
for i := 0; i < 2; i++ {
for i := range 2 {
var nconn net.Conn
nconn, err = net.Dial("tcp", "localhost:8554")
require.NoError(t, err)
@@ -2738,7 +2738,7 @@ func TestServerPlayBackChannel(t *testing.T) {
var l1s [2]net.PacketConn
var l2s [2]net.PacketConn
for i := 0; i < 2; i++ {
for i := range 2 {
inTH := &headers.Transport{
Mode: ptrOf(headers.TransportModePlay),
}
+6 -6
View File
@@ -652,7 +652,7 @@ func TestServerRecord(t *testing.T) {
err = ctx.Session.WritePacketRTCP(ctx.Session.AnnouncedDescription().Medias[1], &testRTCPPacket)
require.NoError(t, err)
for i := 0; i < 2; i++ {
for i := range 2 {
ctx.Session.OnPacketRTP(
ctx.Session.AnnouncedDescription().Medias[i],
ctx.Session.AnnouncedDescription().Medias[i].Formats[0],
@@ -740,7 +740,7 @@ func TestServerRecord(t *testing.T) {
var srtpOutCtx [2]*wrappedSRTPContext
var srtpInCtx [2]*wrappedSRTPContext
for i := 0; i < 2; i++ {
for i := range 2 {
inTH := &headers.Transport{
Delivery: ptrOf(headers.TransportDeliveryUnicast),
Mode: ptrOf(headers.TransportModeRecord),
@@ -835,7 +835,7 @@ func TestServerRecord(t *testing.T) {
// skip firewall opening
if ca.transport == "udp" {
for i := 0; i < 2; i++ {
for i := range 2 {
buf := make([]byte, 2048)
_, _, err = l2s[i].ReadFrom(buf)
require.NoError(t, err)
@@ -844,7 +844,7 @@ func TestServerRecord(t *testing.T) {
// server -> client
for i := 0; i < 2; i++ {
for i := range 2 {
var buf []byte
if ca.transport == "udp" {
@@ -871,7 +871,7 @@ func TestServerRecord(t *testing.T) {
// client -> server
for i := 0; i < 2; i++ {
for i := range 2 {
buf1 := testRTPPacketMarshaled
if ca.secure == "secure" {
@@ -919,7 +919,7 @@ func TestServerRecord(t *testing.T) {
// server -> client
for i := 0; i < 2; i++ {
for i := range 2 {
var buf []byte
if ca.transport == "udp" {
+3 -3
View File
@@ -436,7 +436,7 @@ type ServerSession struct {
ctxCancel func()
propsMutex sync.RWMutex
conns map[*ServerConn]struct{}
userData interface{}
userData any
state ServerSessionState
setuppedMedias map[*description.Media]*serverSessionMedia
setuppedMediasOrdered []*serverSessionMedia
@@ -543,12 +543,12 @@ func (ss *ServerSession) Medias() []*description.Media {
}
// SetUserData sets some user data associated with the session.
func (ss *ServerSession) SetUserData(v interface{}) {
func (ss *ServerSession) SetUserData(v any) {
ss.userData = v
}
// UserData returns some user data associated with the session.
func (ss *ServerSession) UserData() interface{} {
func (ss *ServerSession) UserData() any {
return ss.userData
}