56 #define POLL_TIMEOUT_MS 100
57 #define READ_PACKET_TIMEOUT_S 10
58 #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / POLL_TIMEOUT_MS
59 #define SDP_MAX_SIZE 16384
60 #define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
61 #define DEFAULT_REORDERING_DELAY 100000
63 #define OFFSET(x) offsetof(RTSPState, x)
64 #define DEC AV_OPT_FLAG_DECODING_PARAM
65 #define ENC AV_OPT_FLAG_ENCODING_PARAM
67 #define RTSP_FLAG_OPTS(name, longname) \
68 { name, longname, OFFSET(rtsp_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtsp_flags" }, \
69 { "filter_src", "Only receive packets from the negotiated peer IP", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_FILTER_SRC}, 0, 0, DEC, "rtsp_flags" }, \
70 { "listen", "Wait for incoming connections", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_LISTEN}, 0, 0, DEC, "rtsp_flags" }
72 #define RTSP_MEDIATYPE_OPTS(name, longname) \
73 { name, longname, OFFSET(media_type_mask), AV_OPT_TYPE_FLAGS, { .i64 = (1 << (AVMEDIA_TYPE_DATA+1)) - 1 }, INT_MIN, INT_MAX, DEC, "allowed_media_types" }, \
74 { "video", "Video", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_VIDEO}, 0, 0, DEC, "allowed_media_types" }, \
75 { "audio", "Audio", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_AUDIO}, 0, 0, DEC, "allowed_media_types" }, \
76 { "data", "Data", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_DATA}, 0, 0, DEC, "allowed_media_types" }
78 #define RTSP_REORDERING_OPTS() \
79 { "reorder_queue_size", "Number of packets to buffer for handling of reordered packets", OFFSET(reordering_queue_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, DEC }
82 {
"initial_pause",
"Don't start playing the stream immediately",
OFFSET(initial_pause),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
DEC },
84 {
"rtsp_transport",
"RTSP transport protocols",
OFFSET(lower_transport_mask),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
DEC|
ENC,
"rtsp_transport" }, \
93 {
"timeout",
"Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies flag listen",
OFFSET(initial_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX,
DEC },
113 const char *sep,
const char **pp)
121 while (!strchr(sep, *p) && *p !=
'\0') {
122 if ((q - buf) < buf_size - 1)
134 if (**pp ==
'/') (*pp)++;
138 static void get_word(
char *buf,
int buf_size,
const char **pp)
173 memcpy(sock, ai->ai_addr,
FFMIN(
sizeof(*sock), ai->ai_addrlen));
186 if (handler->
alloc) {
196 int payload_type,
const char *p)
220 init_rtp_handler(handler, rtsp_st, codec);
277 char *
value,
int value_size)
292 typedef struct SDPParseState {
300 int letter,
const char *buf)
303 char buf1[64], st_type[64];
312 av_dlog(s,
"sdp: %c='%s'\n", letter, buf);
315 if (s1->skip_media && letter !=
'm')
320 if (strcmp(buf1,
"IN") != 0)
323 if (strcmp(buf1,
"IP4") && strcmp(buf1,
"IP6"))
335 s1->default_ip = sdp_ip;
336 s1->default_ttl = ttl;
356 get_word(st_type,
sizeof(st_type), &p);
357 if (!strcmp(st_type,
"audio")) {
359 }
else if (!strcmp(st_type,
"video")) {
361 }
else if (!strcmp(st_type,
"application")) {
374 rtsp_st->
sdp_ip = s1->default_ip;
375 rtsp_st->
sdp_ttl = s1->default_ttl;
381 if (!strcmp(buf1,
"udp"))
413 init_rtp_handler(handler, rtsp_st, st->
codec);
414 if (handler && handler->
init)
426 if (!strncmp(p,
"rtsp://", 7))
437 if (proto[0] ==
'\0') {
451 payload_type = atoi(buf1);
455 sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
462 payload_type = atoi(buf1);
480 }
else if (
av_strstart(p,
"IsRealDataType:integer;",&p)) {
483 }
else if (
av_strstart(p,
"SampleRate:integer;", &p) &&
521 SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
535 while (*p !=
'\n' && *p !=
'\r' && *p !=
'\0') {
536 if ((q - buf) <
sizeof(buf) - 1)
541 sdp_parse_line(s, s1, letter, buf);
543 while (*p !=
'\n' && *p !=
'\0')
608 if (rt->
ts && CONFIG_RTPDEC)
619 if (reordering_queue_size < 0) {
621 reordering_queue_size = 0;
632 if (s->
oformat && CONFIG_RTSP_MUXER) {
647 else if (CONFIG_RTPDEC)
650 reordering_queue_size);
665 #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
666 static void rtsp_parse_range(
int *min_ptr,
int *max_ptr,
const char **pp)
674 v = strtol(q, &p, 10);
678 v = strtol(p, &p, 10);
690 char transport_protocol[16];
692 char lower_transport[16];
706 get_word_sep(transport_protocol,
sizeof(transport_protocol),
710 lower_transport[0] =
'\0';
717 }
else if (!
av_strcasecmp (transport_protocol,
"x-pn-tng") ||
720 get_word_sep(lower_transport,
sizeof(lower_transport),
"/;,", &p);
725 lower_transport[0] =
'\0';
741 while (*p !=
'\0' && *p !=
',') {
743 if (!strcmp(parameter,
"port")) {
748 }
else if (!strcmp(parameter,
"client_port")) {
754 }
else if (!strcmp(parameter,
"server_port")) {
760 }
else if (!strcmp(parameter,
"interleaved")) {
766 }
else if (!strcmp(parameter,
"multicast")) {
769 }
else if (!strcmp(parameter,
"ttl")) {
773 th->
ttl = strtol(p, &end, 10);
776 }
else if (!strcmp(parameter,
"destination")) {
782 }
else if (!strcmp(parameter,
"source")) {
788 }
else if (!strcmp(parameter,
"mode")) {
792 if (!strcmp(buf,
"record") ||
793 !strcmp(buf,
"receive"))
798 while (*p !=
';' && *p !=
'\0' && *p !=
',')
810 static void handle_rtp_info(
RTSPState *rt,
const char *url,
811 uint32_t seq, uint32_t rtptime)
814 if (!rtptime || !url[0])
830 static void rtsp_parse_rtp_info(
RTSPState *rt,
const char *p)
833 char key[20], value[1024], url[1024] =
"";
834 uint32_t seq = 0, rtptime = 0;
846 if (!strcmp(key,
"url"))
848 else if (!strcmp(key,
"seq"))
849 seq = strtoul(value,
NULL, 10);
850 else if (!strcmp(key,
"rtptime"))
851 rtptime = strtoul(value,
NULL, 10);
853 handle_rtp_info(rt, url, seq, rtptime);
862 handle_rtp_info(rt, url, seq, rtptime);
876 (t = strtol(p,
NULL, 10)) > 0) {
882 rtsp_parse_transport(reply, p);
884 reply->
seq = strtol(p,
NULL, 10);
899 }
else if (
av_stristart(p,
"WWW-Authenticate:", &p) && rt) {
902 }
else if (
av_stristart(p,
"Authentication-Info:", &p) && rt) {
905 }
else if (
av_stristart(p,
"Content-Base:", &p) && rt) {
907 if (method && !strcmp(method,
"DESCRIBE"))
911 if (method && !strcmp(method,
"PLAY"))
912 rtsp_parse_rtp_info(rt, p);
914 if (strstr(p,
"GET_PARAMETER") &&
915 method && !strcmp(method,
"OPTIONS"))
917 }
else if (
av_stristart(p,
"x-Accept-Dynamic-Rate:", &p) && rt) {
938 av_dlog(s,
"skipping RTP packet len=%d\n", len);
943 if (len1 >
sizeof(buf))
953 unsigned char **content_ptr,
954 int return_on_interleaved_data,
const char *method)
957 char buf[4096], buf1[1024], *q;
960 int ret, content_length, line_count = 0, request = 0;
961 unsigned char *content =
NULL;
967 memset(reply, 0,
sizeof(*reply));
975 av_dlog(s,
"ret=%d c=%02x [%c]\n", ret, ch, ch);
982 if (return_on_interleaved_data) {
986 }
else if (ch !=
'\r') {
987 if ((q - buf) <
sizeof(buf) - 1)
993 av_dlog(s,
"line='%s'\n", buf);
999 if (line_count == 0) {
1002 if (!strncmp(buf1,
"RTSP/", 5)) {
1023 if (content_length > 0) {
1025 content =
av_malloc(content_length + 1);
1027 content[content_length] =
'\0';
1030 *content_ptr = content;
1037 const char* ptr = buf;
1039 if (!strcmp(reply->
reason,
"OPTIONS")) {
1040 snprintf(buf,
sizeof(buf),
"RTSP/1.0 200 OK\r\n");
1047 snprintf(buf,
sizeof(buf),
"RTSP/1.0 501 Not Implemented\r\n");
1072 if (rt->
seq != reply->
seq) {
1078 if (reply->
notice == 2101 ||
1080 reply->
notice == 2306 ) {
1082 }
else if (reply->
notice >= 4400 && reply->
notice < 5500) {
1084 }
else if (reply->
notice == 2401 ||
1105 const char *method,
const char *url,
1106 const char *headers,
1107 const unsigned char *send_content,
1108 int send_content_length)
1111 char buf[4096], *out_buf;
1117 snprintf(buf,
sizeof(buf),
"%s %s RTSP/1.0\r\n", method, url);
1121 if (rt->
session_id[0] !=
'\0' && (!headers ||
1122 !strstr(headers,
"\nIf-Match:"))) {
1127 rt->
auth, url, method);
1132 if (send_content_length > 0 && send_content)
1133 av_strlcatf(buf,
sizeof(buf),
"Content-Length: %d\r\n", send_content_length);
1139 out_buf = base64buf;
1142 av_dlog(s,
"Sending:\n%s--\n", buf);
1145 if (send_content_length > 0 && send_content) {
1148 "with content data not supported\n");
1159 const char *url,
const char *headers)
1161 return ff_rtsp_send_cmd_with_content_async(s, method, url, headers,
NULL, 0);
1166 unsigned char **content_ptr)
1169 content_ptr,
NULL, 0);
1173 const char *method,
const char *url,
1176 unsigned char **content_ptr,
1177 const unsigned char *send_content,
1178 int send_content_length)
1182 int ret, attempts = 0;
1186 if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
1188 send_content_length)))
1212 int lower_transport,
const char *real_challenge)
1215 int rtx = 0, j, i, err,
interleave = 0, port_off;
1219 const char *trans_pref;
1222 trans_pref =
"x-pn-tng";
1224 trans_pref =
"RAW/RAW";
1226 trans_pref =
"RTP/AVP";
1236 port_off -= port_off & 0x01;
1238 for (j = rt->
rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
1239 char transport[2048];
1275 while (j <= rt->rtp_port_max) {
1277 "?localport=%d", j);
1291 snprintf(transport,
sizeof(transport) - 1,
1292 "%s/UDP;", trans_pref);
1294 av_strlcat(transport,
"unicast;",
sizeof(transport));
1296 "client_port=%d", port);
1299 av_strlcatf(transport,
sizeof(transport),
"-%d", port + 1);
1312 snprintf(transport,
sizeof(transport) - 1,
1313 "%s/TCP;", trans_pref);
1315 av_strlcat(transport,
"unicast;",
sizeof(transport));
1317 "interleaved=%d-%d",
1318 interleave, interleave + 1);
1323 snprintf(transport,
sizeof(transport) - 1,
1324 "%s/UDP;multicast", trans_pref);
1327 av_strlcat(transport,
";mode=record",
sizeof(transport));
1330 av_strlcat(transport,
";mode=play",
sizeof(transport));
1332 "Transport: %s\r\n",
1335 av_strlcat(cmd,
"x-Dynamic-Rate: 0\r\n",
sizeof(cmd));
1337 char real_res[41], real_csum[9];
1342 "RealChallenge2: %s, sd=%s\r\n",
1382 char url[1024],
options[30] =
"";
1385 av_strlcpy(options,
"?connect=1",
sizeof(options));
1410 char url[1024], namebuf[50], optbuf[20] =
"";
1424 snprintf(optbuf,
sizeof(optbuf),
"?ttl=%d", ttl);
1425 getnameinfo((
struct sockaddr*) &addr,
sizeof(addr),
1428 port,
"%s", optbuf);
1466 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
1467 int port, err, tcp_fd;
1469 int lower_transport_mask = 0;
1470 char real_challenge[64] =
"";
1472 socklen_t peer_len =
sizeof(peer);
1499 host,
sizeof(host), &port, path,
sizeof(path), s->
filename);
1506 if (!lower_transport_mask)
1515 "only UDP and TCP are supported for output.\n");
1525 host, port,
"%s", path);
1529 char httpname[1024];
1530 char sessioncookie[17];
1533 ff_url_join(httpname,
sizeof(httpname),
"http", auth, host, port,
"%s", path);
1534 snprintf(sessioncookie,
sizeof(sessioncookie),
"%08x%08x",
1546 "x-sessioncookie: %s\r\n"
1547 "Accept: application/x-rtsp-tunnelled\r\n"
1548 "Pragma: no-cache\r\n"
1549 "Cache-Control: no-cache\r\n",
1568 "x-sessioncookie: %s\r\n"
1569 "Content-Type: application/x-rtsp-tunnelled\r\n"
1570 "Pragma: no-cache\r\n"
1571 "Cache-Control: no-cache\r\n"
1572 "Content-Length: 32767\r\n"
1573 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
1614 if (!getpeername(tcp_fd, (
struct sockaddr*) &peer, &peer_len)) {
1615 getnameinfo((
struct sockaddr*) &peer, peer_len, host,
sizeof(host),
1634 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1635 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1636 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1637 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1656 if (s->
iformat && CONFIG_RTSP_DEMUXER)
1658 else if (CONFIG_RTSP_MUXER)
1664 int lower_transport =
ff_log2_tab[lower_transport_mask &
1665 ~(lower_transport_mask - 1)];
1669 real_challenge :
NULL);
1672 lower_transport_mask &= ~(1 << lower_transport);
1673 if (lower_transport_mask == 0 && err == 1) {
1674 err =
AVERROR(EPROTONOSUPPORT);
1701 uint8_t *buf,
int buf_size, int64_t wait_end)
1705 int n, i, ret, tcp_fd, timeout_cnt = 0;
1707 struct pollfd *p = rt->
p;
1708 int *fds =
NULL, fdsnum, fdsidx;
1718 p[max_p].fd = tcp_fd;
1719 p[max_p++].events = POLLIN;
1733 "Number of fds %d not supported\n", fdsnum);
1736 for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
1737 p[max_p].fd = fds[fdsidx];
1738 p[max_p++].events = POLLIN;
1745 int j = 1 - (tcp_fd == -1);
1750 if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
1753 *prtsp_st = rtsp_st;
1760 #if CONFIG_RTSP_DEMUXER
1761 if (tcp_fd != -1 && p[0].revents & POLLIN) {
1768 "Unable to answer to TEARDOWN\n");
1784 }
else if (n < 0 && errno != EINTR)
1816 "Unable to pick stream for packet - SSRC not known for "
1838 int64_t wait_end = 0;
1849 }
else if (rt->
ts && CONFIG_RTPDEC) {
1860 }
else if (ret == 1) {
1868 int64_t first_queue_time = 0;
1875 if (queue_time && (queue_time - first_queue_time < 0 ||
1876 !first_queue_time)) {
1877 first_queue_time = queue_time;
1881 if (first_queue_time)
1882 wait_end = first_queue_time + s->
max_delay;
1895 #if CONFIG_RTSP_DEMUXER
1908 len = pick_stream(s, &rtsp_st, rt->
recvbuf, len);
1913 if (len ==
AVERROR(EAGAIN) && first_queue_st &&
1915 rtsp_st = first_queue_st;
1945 if (rtpctx2 && st && st2 &&
1964 }
else if (rt->
ts && CONFIG_RTPDEC) {
1990 #if CONFIG_SDP_DEMUXER
1996 while (p < p_end && *p !=
'\0') {
1997 if (p +
sizeof(
"c=IN IP") - 1 < p_end &&
2001 while (p < p_end - 1 && *p !=
'\n') p++;
2034 content[
size] =
'\0';
2050 "?localport=%d&ttl=%d&connect=%d", rtsp_st->
sdp_port,
2076 static const AVClass sdp_demuxer_class = {
2091 .priv_class = &sdp_demuxer_class,
2095 #if CONFIG_RTP_DEMUXER
2106 char host[500], sdp[500];
2113 socklen_t addrlen =
sizeof(addr);
2125 ret =
ffurl_read(in, recvbuf,
sizeof(recvbuf));
2135 if ((recvbuf[0] & 0xc0) != 0x80) {
2144 payload_type = recvbuf[1] & 0x7f;
2153 "without an SDP file describing it\n",
2159 "properly you need an SDP file "
2167 "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
2168 addr.ss_family == AF_INET ? 4 : 6, host,
2171 port, payload_type);
2182 ret = sdp_read_header(s);
2193 static const AVClass rtp_demuxer_class = {
2209 .priv_class = &rtp_demuxer_class,