]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtsp.c
matroskadec: demux relevant subtitle packets after a seek
[ffmpeg] / libavformat / rtsp.c
index b8adfe6ba569338c3006ff096128307eed7528d6..950ec4647438f78cad5a38fe506e315c79ea7e8e 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 */
+#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 +43,12 @@ enum RTSPClientState {
     RTSP_STATE_PAUSED,
 };
 
+enum RTSPServerType {
+    RTSP_SERVER_RTP, /*< Standard-compliant RTP-server */
+    RTSP_SERVER_RDT, /*< Realmedia-style server */
+    RTSP_SERVER_LAST
+};
+
 typedef struct RTSPState {
     URLContext *rtsp_hd; /* RTSP TCP connexion handle */
     int nb_rtsp_streams;
@@ -50,8 +62,10 @@ typedef struct RTSPState {
     int seq;        /* RTSP command sequence number */
     char session_id[512];
     enum RTSPProtocol protocol;
+    enum RTSPServerType server_type;
     char last_reply[2048]; /* XXX: allocate ? */
     RTPDemuxContext *cur_rtp;
+    int need_subscription;
 } RTSPState;
 
 typedef struct RTSPStream {
@@ -90,7 +104,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)
@@ -522,6 +536,12 @@ 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 (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 +551,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));
@@ -699,6 +719,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));
     }
 }
 
@@ -841,7 +864,6 @@ static void rtsp_close_streams(RTSPState *rt)
             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);
 }
@@ -850,7 +872,8 @@ static void rtsp_close_streams(RTSPState *rt)
  * @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 protocol_mask)
+make_setup_request (AVFormatContext *s, const char *host, int port,
+                    int protocol, const char *real_challenge)
 {
     RTSPState *rt = s->priv_data;
     int j, i, err;
@@ -858,6 +881,12 @@ make_setup_request (AVFormatContext *s, const char *host, int port, int protocol
     AVStream *st;
     RTSPHeader reply1, *reply = &reply1;
     char cmd[2048];
+    const char *trans_pref;
+
+    if (rt->server_type == RTSP_SERVER_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
@@ -868,11 +897,8 @@ make_setup_request (AVFormatContext *s, const char *host, int port, int protocol
 
         rtsp_st = rt->rtsp_streams[i];
 
-        /* compute available transports */
-        transport[0] = '\0';
-
         /* RTP/UDP */
-        if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
+        if (protocol == RTSP_PROTOCOL_RTP_UDP) {
             char buf[256];
 
             /* first try in specified port range */
@@ -895,39 +921,44 @@ make_setup_request (AVFormatContext *s, const char *host, int port, int protocol
 
         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;unicast;client_port=%d",
+                     trans_pref, port);
+            if (rt->server_type == RTSP_SERVER_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 (protocol == RTSP_PROTOCOL_RTP_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 (protocol == RTSP_PROTOCOL_RTP_UDP_MULTICAST) {
+            snprintf(transport, sizeof(transport) - 1,
+                     "%s/UDP;multicast", trans_pref);
         }
+        if (rt->server_type == RTSP_SERVER_RDT)
+            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_RDT) {
+            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 == 461 /* Unsupported protocol */ && i == 0) {
             err = 1;
             goto fail;
-        } else
-        if (reply->status_code != RTSP_STATUS_OK ||
-            reply->nb_transports != 1) {
+        } else if (reply->status_code != RTSP_STATUS_OK ||
+                   reply->nb_transports != 1) {
             err = AVERROR_INVALIDDATA;
             goto fail;
         }
@@ -944,7 +975,7 @@ make_setup_request (AVFormatContext *s, const char *host, int port, int protocol
 
         /* close RTP connection if not choosen */
         if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
-            (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
+            (protocol == RTSP_PROTOCOL_RTP_UDP)) {
             url_close(rtsp_st->rtp_handle);
             rtsp_st->rtp_handle = NULL;
         }
@@ -974,7 +1005,7 @@ make_setup_request (AVFormatContext *s, const char *host, int port, int protocol
                 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);
@@ -1004,6 +1035,9 @@ make_setup_request (AVFormatContext *s, const char *host, int port, int protocol
         }
     }
 
+    if (rt->server_type == RTSP_SERVER_RDT)
+        rt->need_subscription = 1;
+
     return 0;
 
 fail:
@@ -1026,6 +1060,7 @@ static int rtsp_read_header(AVFormatContext *s,
     RTSPHeader reply1, *reply = &reply1;
     unsigned char *content = NULL;
     int protocol_mask = 0;
+    char real_challenge[64];
 
     /* extract hostname and port */
     url_split(NULL, 0, NULL, 0,
@@ -1064,11 +1099,56 @@ static int rtsp_read_header(AVFormatContext *s,
     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_RDT)
+            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_RDT && reply->real_challenge[0]) {
+            rt->server_type = RTSP_SERVER_RDT;
+            continue;
+        } else if (rt->server_type == RTSP_SERVER_RDT) {
+            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_RDT) {
+        /**
+         * 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;
@@ -1088,14 +1168,16 @@ static int rtsp_read_header(AVFormatContext *s,
     }
 
     do {
-        int protocol = protocol_mask & ~(protocol_mask - 1);
+        int protocol = ff_log2_tab[protocol_mask & ~(protocol_mask - 1)];
 
-        err = make_setup_request(s, host, port, protocol);
+        err = make_setup_request(s, host, port, protocol,
+                                 rt->server_type == RTSP_SERVER_RDT ?
+                                     real_challenge : NULL);
         if (err < 0)
-        goto fail;
-        protocol_mask &= ~protocol;
+            goto fail;
+        protocol_mask &= ~(1 << protocol);
         if (protocol_mask == 0 && err == 1) {
-            err = AVERROR(EPROTONOSUPPORT);
+            err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
             goto fail;
         }
     } while (err);
@@ -1217,9 +1299,37 @@ static int rtsp_read_packet(AVFormatContext *s,
     int ret, len;
     uint8_t buf[RTP_MAX_PACKET_LENGTH];
 
+    if (rt->server_type == RTSP_SERVER_RDT && 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(
+                rt->rtsp_streams[i]->rtp_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->server_type == RTSP_SERVER_RDT)
+            ret = ff_rdt_parse_packet(rt->cur_rtp, pkt, NULL, 0);
+        else
+            ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
         if (ret == 0) {
             rt->cur_rtp = NULL;
             return 0;
@@ -1246,7 +1356,10 @@ static int rtsp_read_packet(AVFormatContext *s,
     }
     if (len < 0)
         return len;
-    ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
+    if (rt->server_type == RTSP_SERVER_RDT)
+        ret = ff_rdt_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
+    else
+        ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
     if (ret < 0)
         goto redo;
     if (ret == 1) {
@@ -1264,24 +1377,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_RDT && 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 */
@@ -1295,17 +1409,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_RDT && 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,
@@ -1354,7 +1468,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,
@@ -1414,9 +1528,10 @@ 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;
@@ -1461,7 +1576,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,
@@ -1528,7 +1643,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,