From 363871d65898b1fdabe98049b44bc8bee93c565e Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Thu, 3 Jun 2021 23:41:39 +0200 Subject: [PATCH] client: support server_port=0-1 (https://github.com/aler9/rtsp-simple-server/issues/407) --- client_read_test.go | 7 ++++++- clientconn.go | 16 +++++----------- clientconnudpl.go | 4 ++-- pkg/liberrors/client.go | 8 -------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/client_read_test.go b/client_read_test.go index af77e7d3..3a917d90 100644 --- a/client_read_test.go +++ b/client_read_test.go @@ -487,6 +487,7 @@ func TestClientReadNoContentBase(t *testing.T) { func TestClientReadAnyPort(t *testing.T) { for _, ca := range []string{ "zero", + "zero_one", "no", } { t.Run(ca, func(t *testing.T) { @@ -556,8 +557,12 @@ func TestClientReadAnyPort(t *testing.T) { }(), ClientPorts: th.ClientPorts, ServerPorts: func() *[2]int { - if ca == "zero" { + switch ca { + case "zero": return &[2]int{0, 0} + + case "zero_one": + return &[2]int{0, 1} } return nil }(), diff --git a/clientconn.go b/clientconn.go index 4b48de19..0aab449c 100644 --- a/clientconn.go +++ b/clientconn.go @@ -37,6 +37,10 @@ func isErrNOUDPPacketsReceivedRecently(err error) bool { return ok } +func isAnyPort(p int) bool { + return p == 0 || p == 1 +} + type clientConnState int const ( @@ -1304,23 +1308,13 @@ func (cc *ClientConn) doSetup( } if proto == StreamProtocolUDP { - if thRes.ServerPorts != nil { - if (thRes.ServerPorts[0] == 0 && thRes.ServerPorts[1] != 0) || - (thRes.ServerPorts[0] != 0 && thRes.ServerPorts[1] == 0) { - rtpListener.close() - rtcpListener.close() - return nil, liberrors.ErrClientServerPortsZero{} - } - } - if !cc.c.AnyPortEnable { - if thRes.ServerPorts == nil || (thRes.ServerPorts[0] == 0 && thRes.ServerPorts[1] == 0) { + if thRes.ServerPorts == nil || isAnyPort(thRes.ServerPorts[0]) || isAnyPort(thRes.ServerPorts[1]) { rtpListener.close() rtcpListener.close() return nil, liberrors.ErrClientServerPortsNotProvided{} } } - } else { if thRes.InterleavedIDs == nil { return nil, liberrors.ErrClientTransportHeaderNoInterleavedIDs{} diff --git a/clientconnudpl.go b/clientconnudpl.go index 63fcf90b..78625d05 100644 --- a/clientconnudpl.go +++ b/clientconnudpl.go @@ -85,7 +85,7 @@ func (l *clientConnUDPListener) run() { uaddr := addr.(*net.UDPAddr) - if !l.remoteIP.Equal(uaddr.IP) || (l.remotePort != 0 && l.remotePort != uaddr.Port) { + if !l.remoteIP.Equal(uaddr.IP) || (!isAnyPort(l.remotePort) && l.remotePort != uaddr.Port) { continue } @@ -104,7 +104,7 @@ func (l *clientConnUDPListener) run() { uaddr := addr.(*net.UDPAddr) - if !l.remoteIP.Equal(uaddr.IP) || (l.remotePort != 0 && l.remotePort != uaddr.Port) { + if !l.remoteIP.Equal(uaddr.IP) || (!isAnyPort(l.remotePort) && l.remotePort != uaddr.Port) { continue } diff --git a/pkg/liberrors/client.go b/pkg/liberrors/client.go index 6445e3e5..1807805d 100644 --- a/pkg/liberrors/client.go +++ b/pkg/liberrors/client.go @@ -97,14 +97,6 @@ func (e ErrClientUDPPortsNotConsecutive) Error() string { return "rtcpPort must be rtpPort + 1" } -// ErrClientServerPortsZero is an error that can be returned by a client. -type ErrClientServerPortsZero struct{} - -// Error implements the error interface. -func (e ErrClientServerPortsZero) Error() string { - return "server ports must be both zero or both not zero" -} - // ErrClientServerPortsNotProvided is an error that can be returned by a client. type ErrClientServerPortsNotProvided struct{}