Repository: bluenviron/gortsplib
Pull request #666 (2024-12-24 15:37:13)

server: always open firewall before sending messages

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/bluenviron/gortsplib/pull/666?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bluenviron) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 82.46%. Comparing base [(`5506eb2`)](https://app.codecov.io/gh/bluenviron/gortsplib/commit/5506eb2f7feeee1094338902948b4b37c58c4429?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bluenviron) to head [(`91c156a`)](https://app.codecov.io/gh/bluenviron/gortsplib/commit/91c156ae590a34ef4ae534eb00f83226495ea937?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bluenviron).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #666      +/-   ##
==========================================
- Coverage   82.48%   82.46%   -0.03%     
==========================================
  Files         116      116              
  Lines       11851    11854       +3     
==========================================
  Hits         9775     9775              
- Misses       1688     1693       +5     
+ Partials      388      386       -2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/bluenviron/gortsplib/pull/666?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bluenviron).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=bluenviron).


Patch:
diff --git a/server_session_media.go b/server_session_media.go
index c558ac55..1b58d39b 100644
--- a/server_session_media.go
+++ b/server_session_media.go
@@ -60,8 +60,11 @@ func (sm *serverSessionMedia) start() {
 				sm.ss.s.udpRTCPListener.addClient(sm.ss.author.ip(), sm.udpRTCPReadPort, sm.readRTCPUDPPlay)
 			} else {
 				// open the firewall by sending empty packets to the counterpart.
-				sm.ss.WritePacketRTP(sm.media, &rtp.Packet{Header: rtp.Header{Version: 2}}) //nolint:errcheck
-				sm.ss.WritePacketRTCP(sm.media, &rtcp.ReceiverReport{})                     //nolint:errcheck
+				byts, _ := (&rtp.Packet{Header: rtp.Header{Version: 2}}).Marshal()
+				sm.ss.s.udpRTPListener.write(byts, sm.udpRTPWriteAddr) //nolint:errcheck
+
+				byts, _ = (&rtcp.ReceiverReport{}).Marshal()
+				sm.ss.s.udpRTCPListener.write(byts, sm.udpRTCPWriteAddr) //nolint:errcheck
 
 				sm.ss.s.udpRTPListener.addClient(sm.ss.author.ip(), sm.udpRTPReadPort, sm.readRTPUDPRecord)
 				sm.ss.s.udpRTCPListener.addClient(sm.ss.author.ip(), sm.udpRTCPReadPort, sm.readRTCPUDPRecord)


Test patch:
diff --git a/server_record_test.go b/server_record_test.go
index 088481bf..c70cba2d 100644
--- a/server_record_test.go
+++ b/server_record_test.go
@@ -713,6 +713,13 @@ func TestServerRecord(t *testing.T) {
 			doRecord(t, conn, "rtsp://localhost:8554/teststream", session)
 
 			for i := 0; i < 2; i++ {
+				// skip firewall opening
+				if transport == "udp" {
+					buf := make([]byte, 2048)
+					_, _, err = l2s[i].ReadFrom(buf)
+					require.NoError(t, err)
+				}
+
 				// server -> client (direct)
 				if transport == "udp" {
 					buf := make([]byte, 2048)
@@ -728,13 +735,6 @@ func TestServerRecord(t *testing.T) {
 					require.Equal(t, testRTCPPacketMarshaled, f.Payload)
 				}
 
-				// skip firewall opening
-				if transport == "udp" {
-					buf := make([]byte, 2048)
-					_, _, err = l2s[i].ReadFrom(buf)
-					require.NoError(t, err)
-				}
-
 				// client -> server
 				if transport == "udp" {
 					_, err = l1s[i].WriteTo(testRTPPacketMarshaled, &net.UDPAddr{