Skip to content

Consider first packet when reading Simulcast IDs #3144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kvasilye
Copy link

@kvasilye kvasilye commented Jun 5, 2025

The code currently ignores the first packet when reading Simulcast IDs from a new SSRC, and probes only subsequent packets. This commit makes it so that we consider the first packet as well (which we already have read). Helps if the publisher only sends Simulcast IDs on the first packet.

The code currently ignores the first packet when reading Simulcast
IDs from a new SSRC, and probes only subsequent packets. This commit
makes it so that we consider the first packet as well (which we
already have read). Helps if the publisher only sends Simulcast
IDs on the first packet.
Copy link
Contributor

@nils-ohlmeier nils-ohlmeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution!

Only one questions/suggestion for improving it further.

@JoeTurki
Copy link
Member

JoeTurki commented Jun 5, 2025

Would it be possible to add a simple unit test for this?

Thank you so much.

@kvasilye
Copy link
Author

kvasilye commented Jun 5, 2025

Would it be possible to add a simple unit test for this?

Thank you so much.

Would be happy to, can you guide me?

  • First, are there existing tests for peer connection which I could add to?
  • Second, is it necessary to mock anything?
  • Third, are there utilities for constructing RTP packets (for testing)? With extensions?

@JoeTurki
Copy link
Member

JoeTurki commented Jun 5, 2025

Hello, For this PR you can add tests here https://github.com/pion/webrtc/blob/master/peerconnection_media_test.go No need to mock the actual connections. For making RTP packets, there are some helpers exposed from pion/rtp, also there are plenty of tests to copy or base off.

I can try to make a unit test this weekend. Thank you again.

@kvasilye
Copy link
Author

kvasilye commented Jun 5, 2025

Hello, For this PR you can add tests here https://github.com/pion/webrtc/blob/master/peerconnection_media_test.go No need to mock the actual connections. For making RTP packets, there are some helpers exposed from pion/rtp, also there are plenty of tests to copy or base off.

I can try to make a unit test this weekend. Thank you again.

Thank you for guidance @JoeTurki - I will work on a test in my PR as well.

@kvasilye
Copy link
Author

kvasilye commented Jun 6, 2025

Here is my "work in progress" for the test.

	// Assert that we can send just one packet with Simulcast IDs (using extensions) and they will be properly received
	t.Run("ExtractIDs", func(t *testing.T) {
		track, err := NewTrackLocalStaticRTP(RTPCodecCapability{MimeType: MimeTypeVP8}, "video", "pion")
		assert.NoError(t, err)

		offerer, answerer, err := newPair()
		assert.NoError(t, err)

		_, err = offerer.AddTrack(track)
		assert.NoError(t, err)

		ticker := time.NewTicker(time.Millisecond * 20)
		defer ticker.Stop()
		testFinished := make(chan struct{})
		seenOneStream, seenOneStreamCancel := context.WithCancel(context.Background())

		go func() {
			sentOnePacket := false

			for {
				select {
				case <-testFinished:
					return
				case <-ticker.C:
					answerer.dtlsTransport.lock.Lock()
					if len(answerer.dtlsTransport.simulcastStreams) >= 1 {
						seenOneStreamCancel()
					}
					answerer.dtlsTransport.lock.Unlock()

					track.mu.Lock()
					if len(track.bindings) == 1 && !sentOnePacket {
						sentOnePacket = true

						midExtensionID, _, _ := answerer.api.mediaEngine.getHeaderExtensionID(
							RTPHeaderExtensionCapability{sdp.SDESMidURI},
						)
						assert.Greater(t, midExtensionID, 0)

						streamIDExtensionID, _, _ := answerer.api.mediaEngine.getHeaderExtensionID(
							RTPHeaderExtensionCapability{sdp.SDESRTPStreamIDURI},
						)
						assert.Greater(t, streamIDExtensionID, 0)

						header := &rtp.Header{
							Version: 2,
							SSRC:    util.RandUint32(),
						}
						header.Extension = true
						header.ExtensionProfile = 0x1000
						assert.NoError(t, header.SetExtension(uint8(midExtensionID), []byte("0")))
						assert.NoError(t, header.SetExtension(uint8(streamIDExtensionID), []byte("some_layer_id")))

						_, err = track.bindings[0].writeStream.WriteRTP(header, []byte{0, 1, 2, 3, 4, 5})
						assert.NoError(t, err)
					}
					track.mu.Unlock()
				}
			}
		}()

		assert.NoError(t, signalPair(offerer, answerer))

		peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer)
		peerConnectionConnected.Wait()

		<-seenOneStream.Done()

		closePairNow(t, offerer, answerer)
		close(testFinished)
	})

Now the problem is:

The test sends Simulcast extensions with MID="0" and RID="some_layer_id" and they are received by the new code added in this PR, but then we get to func (r *RTPReceiver) receiveForRid on the "remote" side, and there is only one track and its RID is empty.

So the remote peer connection's onTrack is not fired because we can't find a matching track (by RID).

I'm not even sure if the SDP in this test negotiates Simulcast. I guess it doesn't?

How can I fix this?

Copy link

codecov bot commented Jun 6, 2025

Codecov Report

Attention: Patch coverage is 70.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 78.57%. Comparing base (27989a3) to head (4d620ca).

Files with missing lines Patch % Lines
peerconnection.go 70.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3144      +/-   ##
==========================================
- Coverage   78.65%   78.57%   -0.08%     
==========================================
  Files          91       91              
  Lines       11491    11501      +10     
==========================================
- Hits         9038     9037       -1     
- Misses       1959     1969      +10     
- Partials      494      495       +1     
Flag Coverage Δ
go 80.43% <70.00%> (-0.09%) ⬇️
wasm 63.47% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kvasilye
Copy link
Author

kvasilye commented Jun 6, 2025

There are some failing checks:

Linter

  Error: rtptransceiver.go:294:4: handleUnknownRTPPacket - result payloadType is never used (unparam)
  ) (payloadType PayloadType, paddingOnly bool, err error) {
     ^

The linter is right, but I didn't touch this file, keeping things as they were. Should I fix it - or should we ignore the error since I've not made any changes there?

Failing peerconnection tests

=== RUN   TestPeerConnection_Media_Sample
    peerconnection_media_test.go:206: 
        	Error Trace:	/home/runner/work/webrtc/webrtc/peerconnection_media_test.go:206
        	Error:      	Received unexpected error:
        	            	the DTLS transport has not started yet
        	Test:       	TestPeerConnection_Media_Sample
    peerconnection_media_test.go:207: 
        	Error Trace:	/home/runner/work/webrtc/webrtc/peerconnection_media_test.go:207
        	Error:      	Should be false
        	Test:       	TestPeerConnection_Media_Sample
    util.go:42: Unexpected routines on test end: 
        goroutine 17334 [chan send]:
        github.com/pion/webrtc/v4.TestPeerConnection_Media_Sample.func3()
        	/home/runner/work/webrtc/webrtc/peerconnection_media_test.go:176 +0x2fd
        created by github.com/pion/webrtc/v4.TestPeerConnection_Media_Sample in goroutine 17324
        	/home/runner/work/webrtc/webrtc/peerconnection_media_test.go:166 +0x8ab
--- FAIL: TestPeerConnection_Media_Sample (10.91s)

I ran all peerconnection tests on my machine just now and they all passed.

I haven't touched any of the tests which failed.

Are they just flakey when run in CI?

@kvasilye
Copy link
Author

@JoeTurki do we have any news on the tests please?

I would be happy to continue on implementing a test, but would appreciate guidance from you on how I can make my test work. I've posted my questions above. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants