]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtsp.c
Delete unnecessary 'extern' keywords.
[ffmpeg] / libavformat / rtsp.c
index 14f0a5ce43a0dfb88c96754bb12f1904de459ba8..33f0e5539e288d4ca247c976bc69557b7c464c47 100644 (file)
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+
+/* needed by inet_aton() */
+#define _SVID_SOURCE
+
+#include "libavutil/avstring.h"
 #include "avformat.h"
 
 #include <sys/time.h>
-#include <unistd.h> /* for select() prototype */
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+#include <strings.h>
 #include "network.h"
-#include "avstring.h"
 #include "rtsp.h"
 
 #include "rtp_internal.h"
+#include "rdt.h"
 
 //#define DEBUG
 //#define DEBUG_RTP_TCP
@@ -37,6 +45,18 @@ enum RTSPClientState {
     RTSP_STATE_PAUSED,
 };
 
+enum RTSPServerType {
+    RTSP_SERVER_RTP,  /*< Standard-compliant RTP-server */
+    RTSP_SERVER_REAL, /*< Realmedia-style server */
+    RTSP_SERVER_LAST
+};
+
+enum RTSPTransport {
+    RTSP_TRANSPORT_RTP,
+    RTSP_TRANSPORT_RDT,
+    RTSP_TRANSPORT_LAST
+};
+
 typedef struct RTSPState {
     URLContext *rtsp_hd; /* RTSP TCP connexion handle */
     int nb_rtsp_streams;
@@ -49,14 +69,17 @@ typedef struct RTSPState {
     //    ByteIOContext rtsp_gb;
     int seq;        /* RTSP command sequence number */
     char session_id[512];
-    enum RTSPProtocol protocol;
+    enum RTSPTransport transport;
+    enum RTSPLowerTransport lower_transport;
+    enum RTSPServerType server_type;
     char last_reply[2048]; /* XXX: allocate ? */
-    RTPDemuxContext *cur_rtp;
+    void *cur_tx;
+    int need_subscription;
 } RTSPState;
 
 typedef struct RTSPStream {
     URLContext *rtp_handle; /* RTP stream handle */
-    RTPDemuxContext *rtp_ctx; /* RTP parse context */
+    void *tx_ctx; /* RTP/RDT parse context */
 
     int stream_index; /* corresponding stream index, if any. -1 if none (MPEG2TS case) */
     int interleaved_min, interleaved_max;  /* interleave ids, if TCP transport */
@@ -69,7 +92,7 @@ typedef struct RTSPStream {
     rtp_payload_data_t rtp_payload_data; /* rtp payload parsing infos from SDP */
 
     RTPDynamicProtocolHandler *dynamic_handler; ///< Only valid if it's a dynamic protocol. (This is the handler structure)
-    void *dynamic_protocol_context; ///< Only valid if it's a dynamic protocol. (This is any private data associated with the dynamic protocol)
+    PayloadContext *dynamic_protocol_context; ///< Only valid if it's a dynamic protocol. (This is any private data associated with the dynamic protocol)
 } RTSPStream;
 
 static int rtsp_read_play(AVFormatContext *s);
@@ -78,7 +101,7 @@ static int rtsp_read_play(AVFormatContext *s);
    changing this variable */
 
 #if LIBAVFORMAT_VERSION_INT < (53 << 16)
-int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_UDP);
+int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
 #endif
 
 static int rtsp_probe(AVProbeData *p)
@@ -90,7 +113,7 @@ static int rtsp_probe(AVProbeData *p)
 
 static int redir_isspace(int c)
 {
-    return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
+    return c == ' ' || c == '\t' || c == '\n' || c == '\r';
 }
 
 static void skip_spaces(const char **pp)
@@ -270,7 +293,7 @@ typedef struct attrname_map
 /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
 #define ATTR_NAME_TYPE_INT 0
 #define ATTR_NAME_TYPE_STR 1
-static attrname_map_t attr_names[]=
+static const attrname_map_t attr_names[]=
 {
     {"SizeLength",       ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, sizelength)},
     {"IndexLength",      ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexlength)},
@@ -366,7 +389,8 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
     RTSPState *rt = s->priv_data;
     char buf1[64], st_type[64];
     const char *p;
-    int codec_type, payload_type, i;
+    enum CodecType codec_type;
+    int payload_type, i;
     AVStream *st;
     RTSPStream *rtsp_st;
     struct in_addr sdp_ip;
@@ -522,6 +546,15 @@ static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
             rtsp_parse_range_npt(p, &start, &end);
             s->start_time= start;
             s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
+        } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
+            if (atoi(p) == 1)
+                rt->transport = RTSP_TRANSPORT_RDT;
+        } else if (s->nb_streams > 0) {
+            rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
+            if (rtsp_st->dynamic_handler &&
+                rtsp_st->dynamic_handler->parse_sdp_a_line)
+                rtsp_st->dynamic_handler->parse_sdp_a_line(s->streams[s->nb_streams - 1],
+                    rtsp_st->dynamic_protocol_context, buf);
         }
         break;
     }
@@ -531,7 +564,7 @@ static int sdp_parse(AVFormatContext *s, const char *content)
 {
     const char *p;
     int letter;
-    char buf[1024], *q;
+    char buf[2048], *q;
     SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
 
     memset(s1, 0, sizeof(SDPParseState));
@@ -614,16 +647,19 @@ static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
                 p++;
                 get_word_sep(lower_transport, sizeof(lower_transport),
                              ";,", &p);
-                }
-        } else if (!strcasecmp (transport_protocol, "x-pn-tng")) {
+            }
+            th->transport = RTSP_TRANSPORT_RTP;
+        } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
+                   !strcasecmp (transport_protocol, "x-real-rdt")) {
             /* x-pn-tng/<protocol> */
             get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
             profile[0] = '\0';
+            th->transport = RTSP_TRANSPORT_RDT;
         }
         if (!strcasecmp(lower_transport, "TCP"))
-            th->protocol = RTSP_PROTOCOL_RTP_TCP;
+            th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
         else
-            th->protocol = RTSP_PROTOCOL_RTP_UDP;
+            th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
 
         if (*p == ';')
             p++;
@@ -654,8 +690,8 @@ static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
                                      &th->interleaved_max, &p);
                 }
             } else if (!strcmp(parameter, "multicast")) {
-                if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
-                    th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
+                if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
+                    th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
             } else if (!strcmp(parameter, "ttl")) {
                 if (*p == '=') {
                     p++;
@@ -699,6 +735,9 @@ void rtsp_parse_line(RTSPHeader *reply, const char *buf)
         reply->seq = strtol(p, NULL, 10);
     } else if (av_stristart(p, "Range:", &p)) {
         rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
+    } else if (av_stristart(p, "RealChallenge1:", &p)) {
+        skip_spaces(&p);
+        av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
     }
 }
 
@@ -834,90 +873,73 @@ static void rtsp_close_streams(RTSPState *rt)
     for(i=0;i<rt->nb_rtsp_streams;i++) {
         rtsp_st = rt->rtsp_streams[i];
         if (rtsp_st) {
-            if (rtsp_st->rtp_ctx)
-                rtp_parse_close(rtsp_st->rtp_ctx);
+            if (rtsp_st->tx_ctx) {
+                if (rt->transport == RTSP_TRANSPORT_RDT)
+                    ff_rdt_parse_close(rtsp_st->tx_ctx);
+                else
+                    rtp_parse_close(rtsp_st->tx_ctx);
+            }
             if (rtsp_st->rtp_handle)
                 url_close(rtsp_st->rtp_handle);
             if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
                 rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
         }
-        av_free(rtsp_st);
     }
     av_free(rt->rtsp_streams);
 }
 
-static int rtsp_read_header(AVFormatContext *s,
-                            AVFormatParameters *ap)
+static int
+rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
 {
     RTSPState *rt = s->priv_data;
-    char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
-    URLContext *rtsp_hd;
-    int port, i, j, ret, err;
-    RTSPHeader reply1, *reply = &reply1;
-    unsigned char *content = NULL;
-    RTSPStream *rtsp_st;
-    int protocol_mask = 0;
-    AVStream *st;
-
-    /* extract hostname and port */
-    url_split(NULL, 0, NULL, 0,
-              host, sizeof(host), &port, path, sizeof(path), s->filename);
-    if (port < 0)
-        port = RTSP_DEFAULT_PORT;
-
-    /* search for options */
-    option_list = strchr(path, '?');
-    if (option_list) {
-        /* remove the options from the path */
-        *option_list++ = 0;
-        while(option_list) {
-            /* move the option pointer */
-            option = option_list;
-            option_list = strchr(option_list, '&');
-            if (option_list)
-                *(option_list++) = 0;
-            /* handle the options */
-            if (strcmp(option, "udp") == 0)
-                protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP);
-            else if (strcmp(option, "multicast") == 0)
-                protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP_MULTICAST);
-            else if (strcmp(option, "tcp") == 0)
-                protocol_mask = (1<< RTSP_PROTOCOL_RTP_TCP);
+    AVStream *st = NULL;
+
+    /* open the RTP context */
+    if (rtsp_st->stream_index >= 0)
+        st = s->streams[rtsp_st->stream_index];
+    if (!st)
+        s->ctx_flags |= AVFMTCTX_NOHEADER;
+
+    if (rt->transport == RTSP_TRANSPORT_RDT)
+        rtsp_st->tx_ctx = ff_rdt_parse_open(s, st->index,
+                                            rtsp_st->dynamic_protocol_context,
+                                            rtsp_st->dynamic_handler);
+    else
+        rtsp_st->tx_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle,
+                                         rtsp_st->sdp_payload_type,
+                                         &rtsp_st->rtp_payload_data);
+
+    if (!rtsp_st->tx_ctx) {
+         return AVERROR(ENOMEM);
+    } else if (rt->transport != RTSP_TRANSPORT_RDT) {
+        if(rtsp_st->dynamic_handler) {
+            rtp_parse_set_dynamic_protocol(rtsp_st->tx_ctx,
+                                           rtsp_st->dynamic_protocol_context,
+                                           rtsp_st->dynamic_handler);
         }
     }
 
-    if (!protocol_mask)
-        protocol_mask = rtsp_default_protocols;
-
-    /* open the tcp connexion */
-    snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
-    if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
-        return AVERROR(EIO);
-    rt->rtsp_hd = rtsp_hd;
-    rt->seq = 0;
+    return 0;
+}
 
-    /* describe the stream */
-    snprintf(cmd, sizeof(cmd),
-             "DESCRIBE %s RTSP/1.0\r\n"
-             "Accept: application/sdp\r\n",
-             s->filename);
-    rtsp_send_cmd(s, cmd, reply, &content);
-    if (!content) {
-        err = AVERROR_INVALIDDATA;
-        goto fail;
-    }
-    if (reply->status_code != RTSP_STATUS_OK) {
-        err = AVERROR_INVALIDDATA;
-        goto fail;
-    }
+/**
+ * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
+ */
+static int
+make_setup_request (AVFormatContext *s, const char *host, int port,
+                    int lower_transport, const char *real_challenge)
+{
+    RTSPState *rt = s->priv_data;
+    int j, i, err;
+    RTSPStream *rtsp_st;
+    RTSPHeader reply1, *reply = &reply1;
+    char cmd[2048];
+    const char *trans_pref;
 
-    /* now we got the SDP description, we parse it */
-    ret = sdp_parse(s, (const char *)content);
-    av_freep(&content);
-    if (ret < 0) {
-        err = AVERROR_INVALIDDATA;
-        goto fail;
-    }
+    if (rt->transport == RTSP_TRANSPORT_RDT)
+        trans_pref = "x-pn-tng";
+    else
+        trans_pref = "RTP/AVP";
 
     /* for each stream, make the setup request */
     /* XXX: we assume the same server is used for the control of each
@@ -928,11 +950,8 @@ static int rtsp_read_header(AVFormatContext *s,
 
         rtsp_st = rt->rtsp_streams[i];
 
-        /* compute available transports */
-        transport[0] = '\0';
-
         /* RTP/UDP */
-        if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
+        if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
             char buf[256];
 
             /* first try in specified port range */
@@ -955,63 +974,77 @@ static int rtsp_read_header(AVFormatContext *s,
 
         rtp_opened:
             port = rtp_get_local_port(rtsp_st->rtp_handle);
-            if (transport[0] != '\0')
-                av_strlcat(transport, ",", sizeof(transport));
-            snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
-                     "RTP/AVP/UDP;unicast;client_port=%d-%d",
-                     port, port + 1);
+            snprintf(transport, sizeof(transport) - 1,
+                     "%s/UDP;", trans_pref);
+            if (rt->server_type != RTSP_SERVER_REAL)
+                av_strlcat(transport, "unicast;", sizeof(transport));
+            av_strlcatf(transport, sizeof(transport),
+                     "client_port=%d", port);
+            if (rt->transport == RTSP_TRANSPORT_RTP)
+                av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
         }
 
         /* RTP/TCP */
-        else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
-            if (transport[0] != '\0')
-                av_strlcat(transport, ",", sizeof(transport));
-            snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
-                     "RTP/AVP/TCP");
+        else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
+            snprintf(transport, sizeof(transport) - 1,
+                     "%s/TCP", trans_pref);
         }
 
-        else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
-            if (transport[0] != '\0')
-                av_strlcat(transport, ",", sizeof(transport));
-            snprintf(transport + strlen(transport),
-                     sizeof(transport) - strlen(transport) - 1,
-                     "RTP/AVP/UDP;multicast");
+        else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
+            snprintf(transport, sizeof(transport) - 1,
+                     "%s/UDP;multicast", trans_pref);
         }
+        if (rt->server_type == RTSP_SERVER_REAL)
+            av_strlcat(transport, ";mode=play", sizeof(transport));
         snprintf(cmd, sizeof(cmd),
                  "SETUP %s RTSP/1.0\r\n"
                  "Transport: %s\r\n",
                  rtsp_st->control_url, transport);
+        if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
+            char real_res[41], real_csum[9];
+            ff_rdt_calc_response_and_checksum(real_res, real_csum,
+                                              real_challenge);
+            av_strlcatf(cmd, sizeof(cmd),
+                        "If-Match: %s\r\n"
+                        "RealChallenge2: %s, sd=%s\r\n",
+                        rt->session_id, real_res, real_csum);
+        }
         rtsp_send_cmd(s, cmd, reply, NULL);
-        if (reply->status_code != RTSP_STATUS_OK ||
-            reply->nb_transports != 1) {
+        if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
+            err = 1;
+            goto fail;
+        } else if (reply->status_code != RTSP_STATUS_OK ||
+                   reply->nb_transports != 1) {
             err = AVERROR_INVALIDDATA;
             goto fail;
         }
 
         /* XXX: same protocol for all streams is required */
         if (i > 0) {
-            if (reply->transports[0].protocol != rt->protocol) {
+            if (reply->transports[0].lower_transport != rt->lower_transport ||
+                reply->transports[0].transport != rt->transport) {
                 err = AVERROR_INVALIDDATA;
                 goto fail;
             }
         } else {
-            rt->protocol = reply->transports[0].protocol;
+            rt->lower_transport = reply->transports[0].lower_transport;
+            rt->transport = reply->transports[0].transport;
         }
 
         /* close RTP connection if not choosen */
-        if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
-            (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
+        if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
+            (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
             url_close(rtsp_st->rtp_handle);
             rtsp_st->rtp_handle = NULL;
         }
 
-        switch(reply->transports[0].protocol) {
-        case RTSP_PROTOCOL_RTP_TCP:
+        switch(reply->transports[0].lower_transport) {
+        case RTSP_LOWER_TRANSPORT_TCP:
             rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
             rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
             break;
 
-        case RTSP_PROTOCOL_RTP_UDP:
+        case RTSP_LOWER_TRANSPORT_UDP:
             {
                 char url[1024];
 
@@ -1024,13 +1057,13 @@ static int rtsp_read_header(AVFormatContext *s,
                 }
             }
             break;
-        case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
+        case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
             {
                 char url[1024];
                 struct in_addr in;
 
                 in.s_addr = htonl(reply->transports[0].destination);
-                snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
+                snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
                          inet_ntoa(in),
                          reply->transports[0].port_min,
                          reply->transports[0].ttl);
@@ -1041,24 +1074,157 @@ static int rtsp_read_header(AVFormatContext *s,
             }
             break;
         }
-        /* open the RTP context */
-        st = NULL;
-        if (rtsp_st->stream_index >= 0)
-            st = s->streams[rtsp_st->stream_index];
-        if (!st)
-            s->ctx_flags |= AVFMTCTX_NOHEADER;
-        rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
-
-        if (!rtsp_st->rtp_ctx) {
-            err = AVERROR(ENOMEM);
+
+        if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
             goto fail;
-        } else {
-            if(rtsp_st->dynamic_handler) {
-                rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
-                rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
-            }
+    }
+
+    if (rt->server_type == RTSP_SERVER_REAL)
+        rt->need_subscription = 1;
+
+    return 0;
+
+fail:
+    for (i=0; i<rt->nb_rtsp_streams; i++) {
+        if (rt->rtsp_streams[i]->rtp_handle) {
+            url_close(rt->rtsp_streams[i]->rtp_handle);
+            rt->rtsp_streams[i]->rtp_handle = NULL;
         }
     }
+    return err;
+}
+
+static int rtsp_read_header(AVFormatContext *s,
+                            AVFormatParameters *ap)
+{
+    RTSPState *rt = s->priv_data;
+    char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
+    URLContext *rtsp_hd;
+    int port, ret, err;
+    RTSPHeader reply1, *reply = &reply1;
+    unsigned char *content = NULL;
+    int lower_transport_mask = 0;
+    char real_challenge[64];
+
+    /* extract hostname and port */
+    url_split(NULL, 0, NULL, 0,
+              host, sizeof(host), &port, path, sizeof(path), s->filename);
+    if (port < 0)
+        port = RTSP_DEFAULT_PORT;
+
+    /* search for options */
+    option_list = strchr(path, '?');
+    if (option_list) {
+        /* remove the options from the path */
+        *option_list++ = 0;
+        while(option_list) {
+            /* move the option pointer */
+            option = option_list;
+            option_list = strchr(option_list, '&');
+            if (option_list)
+                *(option_list++) = 0;
+            /* handle the options */
+            if (strcmp(option, "udp") == 0)
+                lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
+            else if (strcmp(option, "multicast") == 0)
+                lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
+            else if (strcmp(option, "tcp") == 0)
+                lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
+        }
+    }
+
+    if (!lower_transport_mask)
+        lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_LAST) - 1;
+
+    /* open the tcp connexion */
+    snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
+    if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
+        return AVERROR(EIO);
+    rt->rtsp_hd = rtsp_hd;
+    rt->seq = 0;
+
+    /* request options supported by the server; this also detects server type */
+    for (rt->server_type = RTSP_SERVER_RTP;;) {
+        snprintf(cmd, sizeof(cmd),
+                 "OPTIONS %s RTSP/1.0\r\n", s->filename);
+        if (rt->server_type == RTSP_SERVER_REAL)
+            av_strlcat(cmd,
+                       /**
+                        * The following entries are required for proper
+                        * streaming from a Realmedia server. They are
+                        * interdependent in some way although we currently
+                        * don't quite understand how. Values were copied
+                        * from mplayer SVN r23589.
+                        * @param CompanyID is a 16-byte ID in base64
+                        * @param ClientChallenge is a 16-byte ID in hex
+                        */
+                       "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
+                       "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
+                       "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
+                       "GUID: 00000000-0000-0000-0000-000000000000\r\n",
+                       sizeof(cmd));
+        rtsp_send_cmd(s, cmd, reply, NULL);
+        if (reply->status_code != RTSP_STATUS_OK) {
+            err = AVERROR_INVALIDDATA;
+            goto fail;
+        }
+
+        /* detect server type if not standard-compliant RTP */
+        if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
+            rt->server_type = RTSP_SERVER_REAL;
+            continue;
+        } else if (rt->server_type == RTSP_SERVER_REAL) {
+            strcpy(real_challenge, reply->real_challenge);
+        }
+        break;
+    }
+
+    /* describe the stream */
+    snprintf(cmd, sizeof(cmd),
+             "DESCRIBE %s RTSP/1.0\r\n"
+             "Accept: application/sdp\r\n",
+             s->filename);
+    if (rt->server_type == RTSP_SERVER_REAL) {
+        /**
+         * The Require: attribute is needed for proper streaming from
+         * Realmedia servers.
+         */
+        av_strlcat(cmd,
+                   "Require: com.real.retain-entity-for-setup\r\n",
+                   sizeof(cmd));
+    }
+    rtsp_send_cmd(s, cmd, reply, &content);
+    if (!content) {
+        err = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+    if (reply->status_code != RTSP_STATUS_OK) {
+        err = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+
+    /* now we got the SDP description, we parse it */
+    ret = sdp_parse(s, (const char *)content);
+    av_freep(&content);
+    if (ret < 0) {
+        err = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+
+    do {
+        int lower_transport = ff_log2_tab[lower_transport_mask & ~(lower_transport_mask - 1)];
+
+        err = make_setup_request(s, host, port, lower_transport,
+                                 rt->server_type == RTSP_SERVER_REAL ?
+                                     real_challenge : NULL);
+        if (err < 0)
+            goto fail;
+        lower_transport_mask &= ~(1 << lower_transport);
+        if (lower_transport_mask == 0 && err == 1) {
+            err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
+            goto fail;
+        }
+    } while (err);
 
     rt->state = RTSP_STATE_IDLE;
     rt->seek_timestamp = 0; /* default is to start stream at position
@@ -1114,6 +1280,9 @@ static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
     ret = url_readbuf(rt->rtsp_hd, buf, len);
     if (ret != len)
         return -1;
+    if (rt->transport == RTSP_TRANSPORT_RDT &&
+        ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
+        return -1;
 
     /* find the matching stream */
     for(i = 0; i < rt->nb_rtsp_streams; i++) {
@@ -1177,41 +1346,74 @@ static int rtsp_read_packet(AVFormatContext *s,
     int ret, len;
     uint8_t buf[RTP_MAX_PACKET_LENGTH];
 
+    if (rt->server_type == RTSP_SERVER_REAL && rt->need_subscription) {
+        int i;
+        RTSPHeader reply1, *reply = &reply1;
+        char cmd[1024];
+
+        snprintf(cmd, sizeof(cmd),
+                 "SET_PARAMETER %s RTSP/1.0\r\n"
+                 "Subscribe: ",
+                 s->filename);
+        for (i = 0; i < rt->nb_rtsp_streams; i++) {
+            if (i != 0) av_strlcat(cmd, ",", sizeof(cmd));
+            ff_rdt_subscribe_rule(cmd, sizeof(cmd), i, 0);
+            if (rt->transport == RTSP_TRANSPORT_RDT)
+                ff_rdt_subscribe_rule2(
+                    rt->rtsp_streams[i]->tx_ctx,
+                    cmd, sizeof(cmd), i, 0);
+        }
+        av_strlcat(cmd, "\r\n", sizeof(cmd));
+        rtsp_send_cmd(s, cmd, reply, NULL);
+        if (reply->status_code != RTSP_STATUS_OK)
+            return AVERROR_INVALIDDATA;
+        rt->need_subscription = 0;
+
+        if (rt->state == RTSP_STATE_PLAYING)
+            rtsp_read_play (s);
+    }
+
     /* get next frames from the same RTP packet */
-    if (rt->cur_rtp) {
-        ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
+    if (rt->cur_tx) {
+        if (rt->transport == RTSP_TRANSPORT_RDT)
+            ret = ff_rdt_parse_packet(rt->cur_tx, pkt, NULL, 0);
+        else
+            ret = rtp_parse_packet(rt->cur_tx, pkt, NULL, 0);
         if (ret == 0) {
-            rt->cur_rtp = NULL;
+            rt->cur_tx = NULL;
             return 0;
         } else if (ret == 1) {
             return 0;
         } else {
-            rt->cur_rtp = NULL;
+            rt->cur_tx = NULL;
         }
     }
 
     /* read next RTP packet */
  redo:
-    switch(rt->protocol) {
+    switch(rt->lower_transport) {
     default:
-    case RTSP_PROTOCOL_RTP_TCP:
+    case RTSP_LOWER_TRANSPORT_TCP:
         len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
         break;
-    case RTSP_PROTOCOL_RTP_UDP:
-    case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
+    case RTSP_LOWER_TRANSPORT_UDP:
+    case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
         len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
-        if (len >=0 && rtsp_st->rtp_ctx)
-            rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);
+        if (len >=0 && rtsp_st->tx_ctx && rt->transport == RTSP_TRANSPORT_RTP)
+            rtp_check_and_send_back_rr(rtsp_st->tx_ctx, len);
         break;
     }
     if (len < 0)
         return len;
-    ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
+    if (rt->transport == RTSP_TRANSPORT_RDT)
+        ret = ff_rdt_parse_packet(rtsp_st->tx_ctx, pkt, buf, len);
+    else
+        ret = rtp_parse_packet(rtsp_st->tx_ctx, pkt, buf, len);
     if (ret < 0)
         goto redo;
     if (ret == 1) {
         /* more packets may follow, so we save the RTP context */
-        rt->cur_rtp = rtsp_st->rtp_ctx;
+        rt->cur_tx = rtsp_st->tx_ctx;
     }
     return 0;
 }
@@ -1224,24 +1426,25 @@ static int rtsp_read_play(AVFormatContext *s)
 
     av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
 
-    if (rt->state == RTSP_STATE_PAUSED) {
-        snprintf(cmd, sizeof(cmd),
-                 "PLAY %s RTSP/1.0\r\n",
-                 s->filename);
-    } else {
-        snprintf(cmd, sizeof(cmd),
-                 "PLAY %s RTSP/1.0\r\n"
-                 "Range: npt=%0.3f-\r\n",
-                 s->filename,
-                 (double)rt->seek_timestamp / AV_TIME_BASE);
-    }
-    rtsp_send_cmd(s, cmd, reply, NULL);
-    if (reply->status_code != RTSP_STATUS_OK) {
-        return -1;
-    } else {
-        rt->state = RTSP_STATE_PLAYING;
-        return 0;
+    if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
+        if (rt->state == RTSP_STATE_PAUSED) {
+            snprintf(cmd, sizeof(cmd),
+                     "PLAY %s RTSP/1.0\r\n",
+                     s->filename);
+        } else {
+            snprintf(cmd, sizeof(cmd),
+                     "PLAY %s RTSP/1.0\r\n"
+                     "Range: npt=%0.3f-\r\n",
+                     s->filename,
+                     (double)rt->seek_timestamp / AV_TIME_BASE);
+        }
+        rtsp_send_cmd(s, cmd, reply, NULL);
+        if (reply->status_code != RTSP_STATUS_OK) {
+            return -1;
+        }
     }
+    rt->state = RTSP_STATE_PLAYING;
+    return 0;
 }
 
 /* pause the stream */
@@ -1255,17 +1458,17 @@ static int rtsp_read_pause(AVFormatContext *s)
 
     if (rt->state != RTSP_STATE_PLAYING)
         return 0;
-
-    snprintf(cmd, sizeof(cmd),
-             "PAUSE %s RTSP/1.0\r\n",
-             s->filename);
-    rtsp_send_cmd(s, cmd, reply, NULL);
-    if (reply->status_code != RTSP_STATUS_OK) {
-        return -1;
-    } else {
-        rt->state = RTSP_STATE_PAUSED;
-        return 0;
+    else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
+        snprintf(cmd, sizeof(cmd),
+                 "PAUSE %s RTSP/1.0\r\n",
+                 s->filename);
+        rtsp_send_cmd(s, cmd, reply, NULL);
+        if (reply->status_code != RTSP_STATUS_OK) {
+            return -1;
+        }
     }
+    rt->state = RTSP_STATE_PAUSED;
+    return 0;
 }
 
 static int rtsp_read_seek(AVFormatContext *s, int stream_index,
@@ -1297,7 +1500,7 @@ static int rtsp_read_close(AVFormatContext *s)
 
 #if 0
     /* NOTE: it is valid to flush the buffer here */
-    if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
+    if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
         url_fclose(&rt->rtsp_gb);
     }
 #endif
@@ -1314,7 +1517,7 @@ static int rtsp_read_close(AVFormatContext *s)
 #ifdef CONFIG_RTSP_DEMUXER
 AVInputFormat rtsp_demuxer = {
     "rtsp",
-    "RTSP input format",
+    NULL_IF_CONFIG_SMALL("RTSP input format"),
     sizeof(RTSPState),
     rtsp_probe,
     rtsp_read_header,
@@ -1355,7 +1558,6 @@ static int sdp_read_header(AVFormatContext *s,
     int size, i, err;
     char *content;
     char url[1024];
-    AVStream *st;
 
     /* read the whole sdp file */
     /* XXX: better loading */
@@ -1374,30 +1576,17 @@ static int sdp_read_header(AVFormatContext *s,
     for(i=0;i<rt->nb_rtsp_streams;i++) {
         rtsp_st = rt->rtsp_streams[i];
 
-        snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
+        snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
                  inet_ntoa(rtsp_st->sdp_ip),
                  rtsp_st->sdp_port,
+                 rtsp_st->sdp_port,
                  rtsp_st->sdp_ttl);
         if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
             err = AVERROR_INVALIDDATA;
             goto fail;
         }
-        /* open the RTP context */
-        st = NULL;
-        if (rtsp_st->stream_index >= 0)
-            st = s->streams[rtsp_st->stream_index];
-        if (!st)
-            s->ctx_flags |= AVFMTCTX_NOHEADER;
-        rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
-        if (!rtsp_st->rtp_ctx) {
-            err = AVERROR(ENOMEM);
+        if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
             goto fail;
-        } else {
-            if(rtsp_st->dynamic_handler) {
-                rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
-                rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
-            }
-        }
     }
     return 0;
  fail:
@@ -1421,7 +1610,7 @@ static int sdp_read_close(AVFormatContext *s)
 #ifdef CONFIG_SDP_DEMUXER
 AVInputFormat sdp_demuxer = {
     "sdp",
-    "SDP",
+    NULL_IF_CONFIG_SMALL("SDP"),
     sizeof(RTSPState),
     sdp_probe,
     sdp_read_header,
@@ -1488,7 +1677,7 @@ static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
 
 AVInputFormat redir_demuxer = {
     "redir",
-    "Redirector format",
+    NULL_IF_CONFIG_SMALL("Redirector format"),
     0,
     redir_probe,
     redir_read_header,