* 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
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;
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 {
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)
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;
}
{
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));
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));
}
}
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);
}
* @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)
+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;
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
rtsp_st = rt->rtsp_streams[i];
- /* compute available transports */
- transport[0] = '\0';
-
/* RTP/UDP */
if (protocol == RTSP_PROTOCOL_RTP_UDP) {
char buf[256];
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 == 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");
+ snprintf(transport, sizeof(transport) - 1,
+ "%s/TCP", trans_pref);
}
else if (protocol == 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");
+ 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;
}
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);
}
}
+ if (rt->server_type == RTSP_SERVER_RDT)
+ rt->need_subscription = 1;
+
return 0;
fail:
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,
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;
do {
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;
+ goto fail;
protocol_mask &= ~(1 << protocol);
if (protocol_mask == 0 && err == 1) {
- err = AVERROR(EPROTONOSUPPORT);
+ err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
goto fail;
}
} while (err);
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;
}
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) {
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 */
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,
#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,
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;
#ifdef CONFIG_SDP_DEMUXER
AVInputFormat sdp_demuxer = {
"sdp",
- "SDP",
+ NULL_IF_CONFIG_SMALL("SDP"),
sizeof(RTSPState),
sdp_probe,
sdp_read_header,
AVInputFormat redir_demuxer = {
"redir",
- "Redirector format",
+ NULL_IF_CONFIG_SMALL("Redirector format"),
0,
redir_probe,
redir_read_header,