From 478e9d7f47c371133ec1eb79b2e502eaf30397ff Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 31 Jan 2017 14:31:48 +0100 Subject: [PATCH] RTSP now uses ephemeral UDP ports instead of semi-random ports, preventing collisions --- src/output/output_rtsp.cpp | 5 ++++- src/output/output_rtsp.h | 13 +++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/output/output_rtsp.cpp b/src/output/output_rtsp.cpp index 79284c4d..cf0022f9 100644 --- a/src/output/output_rtsp.cpp +++ b/src/output/output_rtsp.cpp @@ -437,7 +437,10 @@ namespace Mist { for (std::map::iterator it = tracks.begin(); it != tracks.end(); ++it){ Socket::UDPConnection & s = it->second.data; while (s.Receive()){ - if (s.getDestPort() != it->second.cPort){continue;}//port mismatch = skip + if (s.getDestPort() != it->second.cPort){ + //wrong sending port, ignore packet + continue; + } RTP::Packet pack(s.data, s.data_len); if (!it->second.rtpSeq){it->second.rtpSeq = pack.getSequence();} //packet is very early - assume dropped after 10 packets diff --git a/src/output/output_rtsp.h b/src/output/output_rtsp.h index 7ec834af..5700ba8a 100644 --- a/src/output/output_rtsp.h +++ b/src/output/output_rtsp.h @@ -109,18 +109,23 @@ namespace Mist { channel = -1; size_t port_loc = transport.rfind("client_port=") + 12; cPort = atol(transport.substr(port_loc, transport.rfind('-') - port_loc).c_str()); - unsigned int rand_offset = ((rand() % 4000) << 1) + 2000; + uint32_t portA, portB; //find available ports locally; int sendbuff = 4*1024*1024; data.SetDestination(host, cPort); - data.bind(rand_offset + trk.trackID * 2); + portA = data.bind(0); setsockopt(data.getSock(), SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); rtcp.SetDestination(host, cPort + 1); - rtcp.bind(rand_offset + trk.trackID * 2 + 1); + portB = rtcp.bind(0); setsockopt(rtcp.getSock(), SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); std::stringstream tStr; - tStr << "RTP/AVP/UDP;unicast;client_port=" << cPort << '-' << cPort + 1 << ";source="<< source <<";server_port=" << (rand_offset + trk.trackID * 2) << "-" << (rand_offset + trk.trackID * 2 + 1) << ";ssrc=" << std::hex << SSrc << std::dec; + tStr << "RTP/AVP/UDP;unicast;client_port=" << cPort << '-' << cPort + 1 << ";"; + if (source.size()){ + tStr << "source=" << source << ";"; + } + tStr << "server_port=" << portA << "-" << portB << ";ssrc=" << std::hex << SSrc << std::dec; transportString = tStr.str(); + INFO_MSG("Transport string: %s", transportString.c_str()); } return true; }