[FFmpeg-cvslog] rtsp: Use a random offset for trying to open UDP ports for RTP

Dmitry Volyntsev git at videolan.org
Mon Jan 23 01:18:09 CET 2012


ffmpeg | branch: master | Dmitry Volyntsev <xeioexception at gmail.com> | Wed Jan 18 13:46:37 2012 +0400| [58f0978581bf736ac6888fe3013f0c5ac88bfbdc] | committer: Martin Storsjö

rtsp: Use a random offset for trying to open UDP ports for RTP

This avoids (for all practical cases) the issue of reusing
the same UDP port as for an earlier connection. If the remote
doesn't know the previous session was closed, he might keep
on sending packets to that port. If we always start off trying
to open the same UDP port, we might get those packets intermixed
with the new ones.

This is occasionally an issue when testing RTSP stuff with
DSS, perhaps also with other servers.

Signed-off-by: Martin Storsjö <martin at martin.st>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=58f0978581bf736ac6888fe3013f0c5ac88bfbdc
---

 libavformat/rtsp.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 0e56d37..487e910 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -1105,7 +1105,7 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
                               int lower_transport, const char *real_challenge)
 {
     RTSPState *rt = s->priv_data;
-    int rtx = 0, j, i, err, interleave = 0;
+    int rtx = 0, j, i, err, interleave = 0, port_off;
     RTSPStream *rtsp_st;
     RTSPMessageHeader reply1, *reply = &reply1;
     char cmd[2048];
@@ -1123,7 +1123,14 @@ int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
     /* XXX: we assume the same server is used for the control of each
      * RTSP stream */
 
-    for (j = rt->rtp_port_min, i = 0; i < rt->nb_rtsp_streams; ++i) {
+    /* Choose a random starting offset within the first half of the
+     * port range, to allow for a number of ports to try even if the offset
+     * happens to be at the end of the random range. */
+    port_off = av_get_random_seed() % ((rt->rtp_port_max - rt->rtp_port_min)/2);
+    /* even random offset */
+    port_off -= port_off & 0x01;
+
+    for (j = rt->rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
         char transport[2048];
 
         /*



More information about the ffmpeg-cvslog mailing list