From f9afb6ea5c7e5c5ac436e9fb7a0961e35950a0a3 Mon Sep 17 00:00:00 2001 From: Tristan Matthews Date: Fri, 9 Jan 2026 09:56:46 -0500 Subject: [PATCH 1/2] Bump go to 1.24 This allows for ranging over constant and testing.Loop requires go1.24 or later. --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 45fddcd..45ba5ec 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/pion/rtp -go 1.21 +go 1.24 require ( github.com/pion/randutil v0.1.0 From d98a350349b6b00a8fa86b23c0d920e467a2bc0b Mon Sep 17 00:00:00 2001 From: Tristan Matthews Date: Fri, 9 Jan 2026 09:58:42 -0500 Subject: [PATCH 2/2] Apply go modernize --- abscapturetimeextension_test.go | 16 ++++++++-------- abssendtimeextension_test.go | 8 ++++---- audiolevelextension_test.go | 8 ++++---- header_extension_test.go | 1 - packet_test.go | 13 +++---------- packetizer_test.go | 11 ++++++----- playoutdelayextension_test.go | 8 ++++---- sequencer_test.go | 10 +++++----- transportccextension_test.go | 8 ++++---- vlaextension.go | 10 +++++----- vlaextension_test.go | 14 +++++++------- 11 files changed, 50 insertions(+), 57 deletions(-) diff --git a/abscapturetimeextension_test.go b/abscapturetimeextension_test.go index 1b6d948..562cc3c 100644 --- a/abscapturetimeextension_test.go +++ b/abscapturetimeextension_test.go @@ -104,8 +104,8 @@ var ( func BenchmarkAbsCaptureTimeExtension_Marshal(b *testing.B) { ext := NewAbsCaptureTimeExtension(time.Now()) b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { absCaptureTimeSink, _ = ext.Marshal() } } @@ -113,8 +113,8 @@ func BenchmarkAbsCaptureTimeExtension_Marshal(b *testing.B) { func BenchmarkAbsCaptureTimeExtension_MarshalTo(b *testing.B) { ext := NewAbsCaptureTimeExtension(time.Now()) b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { absCaptureTimeSinkInt, _ = ext.MarshalTo(absCaptureTimeBuf) } } @@ -122,8 +122,8 @@ func BenchmarkAbsCaptureTimeExtension_MarshalTo(b *testing.B) { func BenchmarkAbsCaptureTimeExtensionWithOffset_Marshal(b *testing.B) { ext := NewAbsCaptureTimeExtensionWithCaptureClockOffset(time.Now(), 100*time.Millisecond) b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { absCaptureTimeSink, _ = ext.Marshal() } } @@ -131,8 +131,8 @@ func BenchmarkAbsCaptureTimeExtensionWithOffset_Marshal(b *testing.B) { func BenchmarkAbsCaptureTimeExtensionWithOffset_MarshalTo(b *testing.B) { ext := NewAbsCaptureTimeExtensionWithCaptureClockOffset(time.Now(), 100*time.Millisecond) b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { absCaptureTimeSinkInt, _ = ext.MarshalTo(absCaptureTimeExtendedBuf) } } diff --git a/abssendtimeextension_test.go b/abssendtimeextension_test.go index 9fb09e4..73ec445 100644 --- a/abssendtimeextension_test.go +++ b/abssendtimeextension_test.go @@ -124,8 +124,8 @@ var ( func BenchmarkAbsSendTimeExtension_Marshal(b *testing.B) { ext := AbsSendTimeExtension{Timestamp: 123456} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { absSendTimeSink, _ = ext.Marshal() } } @@ -133,8 +133,8 @@ func BenchmarkAbsSendTimeExtension_Marshal(b *testing.B) { func BenchmarkAbsSendTimeExtension_MarshalTo(b *testing.B) { ext := AbsSendTimeExtension{Timestamp: 123456} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { absSendTimeSinkInt, _ = ext.MarshalTo(absSendTimeBuf) } } diff --git a/audiolevelextension_test.go b/audiolevelextension_test.go index 5e89ed1..fdb2bdf 100644 --- a/audiolevelextension_test.go +++ b/audiolevelextension_test.go @@ -88,8 +88,8 @@ var ( func BenchmarkAudioLevelExtension_Marshal(b *testing.B) { ext := AudioLevelExtension{Level: 8, Voice: true} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { audioLevelSink, _ = ext.Marshal() } } @@ -97,8 +97,8 @@ func BenchmarkAudioLevelExtension_Marshal(b *testing.B) { func BenchmarkAudioLevelExtension_MarshalTo(b *testing.B) { ext := AudioLevelExtension{Level: 8, Voice: true} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { audioLevelSinkInt, _ = ext.MarshalTo(audioLevelBuf) } } diff --git a/header_extension_test.go b/header_extension_test.go index 1b4a1e4..6989059 100644 --- a/header_extension_test.go +++ b/header_extension_test.go @@ -92,7 +92,6 @@ func TestHeaderExtension_RFC8285OneByteMultipleExtensionsWithPadding(t *testing. dstBuf["DirtyBuffer"][i] = 0xFF } for name, buf := range dstBuf { - buf := buf t.Run(name, func(t *testing.T) { n, err := ext.MarshalTo(buf) assert.NoError(t, err) diff --git a/packet_test.go b/packet_test.go index 3b8f8a1..ac46f24 100644 --- a/packet_test.go +++ b/packet_test.go @@ -42,7 +42,7 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop } // Unmarshal to the used Packet should work as well. - for i := 0; i < 2; i++ { + for i := range 2 { t.Run(fmt.Sprintf("Run%d", i+1), func(t *testing.T) { assert.NoError(t, packet.Unmarshal(rawPkt)) assert.Equal(t, packet, parsedPacket) @@ -433,7 +433,6 @@ func TestRFC8285OneByteMultipleExtensionsWithPadding(t *testing.T) { dstBuf["DirtyBuffer"][i] = 0xFF } for name, buf := range dstBuf { - buf := buf t.Run(name, func(t *testing.T) { n, err := packet.MarshalTo(buf) assert.NoError(t, err) @@ -577,7 +576,6 @@ func TestRFC8285TwoByteMultipleExtensionsWithPadding(t *testing.T) { dstBuf["DirtyBuffer"][i] = 0xFF } for name, buf := range dstBuf { - buf := buf t.Run(name, func(t *testing.T) { n, err := packet.MarshalTo(buf) assert.NoError(t, err) @@ -1200,7 +1198,6 @@ func TestUnmarshal_ErrorHandling(t *testing.T) { } for name, testCase := range cases { - testCase := testCase t.Run(name, func(t *testing.T) { h := &Header{} _, err := h.Unmarshal(testCase.input) @@ -1488,9 +1485,7 @@ func BenchmarkMarshal(b *testing.B) { b.Fatal(err) } - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for b.Loop() { _, err = packet.Marshal() if err != nil { b.Fatal(err) @@ -1513,9 +1508,7 @@ func BenchmarkMarshalTo(b *testing.B) { buf := [100]byte{} - b.ResetTimer() - - for i := 0; i < b.N; i++ { + for b.Loop() { _, err = packet.MarshalTo(buf[:]) if err != nil { b.Fatal(err) diff --git a/packetizer_test.go b/packetizer_test.go index b79c2a2..4eba7e4 100644 --- a/packetizer_test.go +++ b/packetizer_test.go @@ -5,6 +5,7 @@ package rtp import ( "fmt" + "strings" "testing" "time" @@ -20,13 +21,13 @@ func TestPacketizer(t *testing.T) { expectedLen := 2 if len(packets) != expectedLen { - packetlengths := "" - for i := 0; i < len(packets); i++ { - packetlengths += fmt.Sprintf("Packet %d length %d\n", i, len(packets[i].Payload)) + var packetlengths strings.Builder + for i := range packets { + packetlengths.WriteString(fmt.Sprintf("Packet %d length %d\n", i, len(packets[i].Payload))) } assert.Failf( t, "Packetize failed", "Generated %d packets instead of %d\n%s", - len(packets), expectedLen, packetlengths, + len(packets), expectedLen, packetlengths.String(), ) } } @@ -188,7 +189,7 @@ func TestPacketizer_Empty_Payload(t *testing.T) { const expectedSamples = uint32(4000) prevTimestamp := uint32(0) - for i := 0; i < 10; i++ { + for i := range 10 { payload := []byte{0x11, 0x12, 0x13, 0x14} isEmptyPayload := i%2 == 0 if isEmptyPayload { diff --git a/playoutdelayextension_test.go b/playoutdelayextension_test.go index bd2fdd5..965bde8 100644 --- a/playoutdelayextension_test.go +++ b/playoutdelayextension_test.go @@ -91,8 +91,8 @@ var ( func BenchmarkPlayoutDelayExtension_Marshal(b *testing.B) { ext := PlayoutDelayExtension{MinDelay: 100, MaxDelay: 200} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { playoutDelaySink, _ = ext.Marshal() } } @@ -100,8 +100,8 @@ func BenchmarkPlayoutDelayExtension_Marshal(b *testing.B) { func BenchmarkPlayoutDelayExtension_MarshalTo(b *testing.B) { ext := PlayoutDelayExtension{MinDelay: 100, MaxDelay: 200} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { playoutDelaySinkInt, _ = ext.MarshalTo(playoutDelayBuf) } } diff --git a/sequencer_test.go b/sequencer_test.go index a701615..638f7df 100644 --- a/sequencer_test.go +++ b/sequencer_test.go @@ -30,7 +30,7 @@ func TestSequencerMultipleRollovers(t *testing.T) { sequencer.NextSequenceNumber() assert.Equal(t, uint64(1), sequencer.RollOverCount()) - for i := 0; i < 65536; i++ { + for range 65536 { sequencer.NextSequenceNumber() } @@ -55,11 +55,11 @@ func TestSequencerConcurrent(t *testing.T) { wg.Add(numGoroutines) results := make([][]uint16, numGoroutines) - for i := 0; i < numGoroutines; i++ { + for i := range numGoroutines { go func(idx int) { defer wg.Done() results[idx] = make([]uint16, numIterations) - for j := 0; j < numIterations; j++ { + for j := range numIterations { results[idx][j] = sequencer.NextSequenceNumber() } }(i) @@ -68,8 +68,8 @@ func TestSequencerConcurrent(t *testing.T) { wg.Wait() seen := make(map[uint16]int) - for i := 0; i < numGoroutines; i++ { - for j := 0; j < numIterations; j++ { + for i := range numGoroutines { + for j := range numIterations { seen[results[i][j]]++ } } diff --git a/transportccextension_test.go b/transportccextension_test.go index 9bee5a2..df759f5 100644 --- a/transportccextension_test.go +++ b/transportccextension_test.go @@ -81,8 +81,8 @@ var ( func BenchmarkTransportCCExtension_Marshal(b *testing.B) { ext := TransportCCExtension{TransportSequence: 1234} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { transportCCSink, _ = ext.Marshal() } } @@ -90,8 +90,8 @@ func BenchmarkTransportCCExtension_Marshal(b *testing.B) { func BenchmarkTransportCCExtension_MarshalTo(b *testing.B) { ext := TransportCCExtension{TransportSequence: 1234} b.ReportAllocs() - b.ResetTimer() - for i := 0; i < b.N; i++ { + + for b.Loop() { transportCCSinkInt, _ = ext.MarshalTo(transportCCBuf) } } diff --git a/vlaextension.go b/vlaextension.go index 21fe629..753b9e6 100644 --- a/vlaextension.go +++ b/vlaextension.go @@ -87,7 +87,7 @@ func (v VLA) preprocessForMashaling(ctx *vlaMarshalingContext) error { //nolint: func (v VLA) calcTargetBitratesSize(ctx *vlaMarshalingContext) { for rtpStreamID := 0; rtpStreamID < v.RTPStreamCount; rtpStreamID++ { - for spatialID := 0; spatialID < 4; spatialID++ { + for spatialID := range 4 { if idx := ctx.slIndices[rtpStreamID][spatialID]; idx >= 0 { for _, kbps := range v.ActiveSpatialLayer[idx].TargetBitrates { ctx.requiredLen += leb128Size(uint(kbps)) //nolint:gosec @@ -176,7 +176,7 @@ func (v VLA) MarshalTo(buf []byte) (int, error) { //nolint:cyclop,gocognit offset++ var temporalLayerIndex int for rtpStreamID := 0; rtpStreamID < v.RTPStreamCount; rtpStreamID++ { - for spatialID := 0; spatialID < 4; spatialID++ { + for spatialID := range 4 { if idx := ctx.slIndices[rtpStreamID][spatialID]; idx >= 0 { if temporalLayerIndex >= 4 { temporalLayerIndex = 0 @@ -191,7 +191,7 @@ func (v VLA) MarshalTo(buf []byte) (int, error) { //nolint:cyclop,gocognit // Target bitrate fields offset++ for rtpStreamID := 0; rtpStreamID < v.RTPStreamCount; rtpStreamID++ { - for spatialID := 0; spatialID < 4; spatialID++ { + for spatialID := range 4 { if idx := ctx.slIndices[rtpStreamID][spatialID]; idx >= 0 { for _, kbps := range v.ActiveSpatialLayer[idx].TargetBitrates { offset += writeLeb128To(buf[offset:], uint(kbps)) //nolint:gosec @@ -231,7 +231,7 @@ func (v VLA) Marshal() ([]byte, error) { func commonSLBMValues(slMBs []uint8) uint8 { var common uint8 - for i := 0; i < len(slMBs); i++ { + for i := range slMBs { if slMBs[i] == 0 { continue } @@ -301,7 +301,7 @@ func (v *VLA) unmarshalTemporalLayers(ctx *vlaUnmarshalingContext) error { // no var temporalLayerIndex int for streamID := 0; streamID < v.RTPStreamCount; streamID++ { - for spatialID := 0; spatialID < 4; spatialID++ { + for spatialID := range 4 { if ctx.slBMs[streamID]&(1<