Skip to content

Commit

Permalink
server: fix error message
Browse files Browse the repository at this point in the history
Return error 'can't setup medias with different protocols' when trying
to use UDP and TCP in sequence.
  • Loading branch information
aler9 committed Oct 13, 2024
1 parent 1ca217a commit 3d03ece
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
47 changes: 35 additions & 12 deletions server_play_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func TestServerPlaySetupErrors(t *testing.T) {
"different paths",
"double setup",
"closed stream",
"different protocols",
} {
t.Run(ca, func(t *testing.T) {
var stream *ServerStream
Expand All @@ -336,6 +337,9 @@ func TestServerPlaySetupErrors(t *testing.T) {

case "closed stream":
require.EqualError(t, ctx.Error, "stream is closed")

case "different protocols":
require.EqualError(t, ctx.Error, "can't setup medias with different protocols")
}
close(nconnClosed)
},
Expand All @@ -350,7 +354,9 @@ func TestServerPlaySetupErrors(t *testing.T) {
}, stream, nil
},
},
RTSPAddress: "localhost:8554",
RTSPAddress: "localhost:8554",
UDPRTPAddress: "127.0.0.1:8000",
UDPRTCPAddress: "127.0.0.1:8001",
}

err := s.Start()
Expand All @@ -372,10 +378,10 @@ func TestServerPlaySetupErrors(t *testing.T) {
desc := doDescribe(t, conn)

th := &headers.Transport{
Protocol: headers.TransportProtocolTCP,
Delivery: deliveryPtr(headers.TransportDeliveryUnicast),
Mode: transportModePtr(headers.TransportModePlay),
InterleavedIDs: &[2]int{0, 1},
Protocol: headers.TransportProtocolUDP,
Delivery: deliveryPtr(headers.TransportDeliveryUnicast),
Mode: transportModePtr(headers.TransportModePlay),
ClientPorts: &[2]int{35466, 35467},
}

res, err := writeReqReadRes(conn, base.Request{
Expand All @@ -387,14 +393,16 @@ func TestServerPlaySetupErrors(t *testing.T) {
},
})

switch ca {
case "different paths":
if ca != "closed stream" {
require.NoError(t, err)
require.Equal(t, base.StatusOK, res.StatusCode)
}

switch ca {
case "different paths":
session := readSession(t, res)

th.InterleavedIDs = &[2]int{2, 3}
th.ClientPorts = &[2]int{35468, 35469}

res, err = writeReqReadRes(conn, base.Request{
Method: base.Setup,
Expand All @@ -409,12 +417,9 @@ func TestServerPlaySetupErrors(t *testing.T) {
require.Equal(t, base.StatusBadRequest, res.StatusCode)

case "double setup":
require.NoError(t, err)
require.Equal(t, base.StatusOK, res.StatusCode)

session := readSession(t, res)

th.InterleavedIDs = &[2]int{2, 3}
th.ClientPorts = &[2]int{35468, 35469}

res, err = writeReqReadRes(conn, base.Request{
Method: base.Setup,
Expand All @@ -431,6 +436,24 @@ func TestServerPlaySetupErrors(t *testing.T) {
case "closed stream":
require.NoError(t, err)
require.Equal(t, base.StatusBadRequest, res.StatusCode)

case "different protocols":
session := readSession(t, res)

th.Protocol = headers.TransportProtocolTCP
th.InterleavedIDs = &[2]int{0, 1}

res, err = writeReqReadRes(conn, base.Request{
Method: base.Setup,
URL: mediaURL(t, desc.BaseURL, desc.Medias[0]),
Header: base.Header{
"CSeq": base.HeaderValue{"4"},
"Transport": th.Marshal(),
"Session": base.HeaderValue{session},
},
})
require.NoError(t, err)
require.Equal(t, base.StatusBadRequest, res.StatusCode)
}

<-nconnClosed
Expand Down
34 changes: 19 additions & 15 deletions server_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,15 @@ func (ss *ServerSession) handleRequestInner(sc *ServerConn, req *base.Request) (
}, err
}

var inTSH headers.Transports
err = inTSH.Unmarshal(req.Header["Transport"])
var transportHeaders headers.Transports
err = transportHeaders.Unmarshal(req.Header["Transport"])
if err != nil {
return &base.Response{
StatusCode: base.StatusBadRequest,
}, liberrors.ErrServerTransportHeaderInvalid{Err: err}
}

inTH := findFirstSupportedTransportHeader(ss.s, inTSH)
inTH := findFirstSupportedTransportHeader(ss.s, transportHeaders)
if inTH == nil {
return &base.Response{
StatusCode: base.StatusUnsupportedTransport,
Expand Down Expand Up @@ -706,16 +706,26 @@ func (ss *ServerSession) handleRequestInner(sc *ServerConn, req *base.Request) (
transport = TransportUDPMulticast
} else {
transport = TransportUDP

if inTH.ClientPorts == nil {
return &base.Response{
StatusCode: base.StatusBadRequest,
}, liberrors.ErrServerTransportHeaderNoClientPorts{}
}
}
} else {
transport = TransportTCP
}

if ss.setuppedTransport != nil && *ss.setuppedTransport != transport {
return &base.Response{
StatusCode: base.StatusBadRequest,
}, liberrors.ErrServerMediasDifferentProtocols{}
}

switch transport {
case TransportUDP, TransportUDPMulticast:
if inTH.ClientPorts == nil {
return &base.Response{
StatusCode: base.StatusBadRequest,
}, liberrors.ErrServerTransportHeaderNoClientPorts{}
}

default: // TCP
if inTH.InterleavedIDs != nil {
if (inTH.InterleavedIDs[0] + 1) != inTH.InterleavedIDs[1] {
return &base.Response{
Expand All @@ -731,12 +741,6 @@ func (ss *ServerSession) handleRequestInner(sc *ServerConn, req *base.Request) (
}
}

if ss.setuppedTransport != nil && *ss.setuppedTransport != transport {
return &base.Response{
StatusCode: base.StatusBadRequest,
}, liberrors.ErrServerMediasDifferentProtocols{}
}

switch ss.state {
case ServerSessionStateInitial, ServerSessionStatePrePlay: // play
if inTH.Mode != nil && *inTH.Mode != headers.TransportModePlay {
Expand Down

0 comments on commit 3d03ece

Please sign in to comment.