55 #define POLL_TIMEOUT_MS 100
56 #define READ_PACKET_TIMEOUT_S 10
57 #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / POLL_TIMEOUT_MS
58 #define SDP_MAX_SIZE 16384
59 #define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
60 #define DEFAULT_REORDERING_DELAY 100000
62 #define OFFSET(x) offsetof(RTSPState, x)
63 #define DEC AV_OPT_FLAG_DECODING_PARAM
64 #define ENC AV_OPT_FLAG_ENCODING_PARAM
66 #define RTSP_FLAG_OPTS(name, longname) \
67 { name, longname, OFFSET(rtsp_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtsp_flags" }, \
68 { "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 #define RTSP_MEDIATYPE_OPTS(name, longname) \
71 { 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" }, \
72 { "video", "Video", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_VIDEO}, 0, 0, DEC, "allowed_media_types" }, \
73 { "audio", "Audio", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_AUDIO}, 0, 0, DEC, "allowed_media_types" }, \
74 { "data", "Data", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_DATA}, 0, 0, DEC, "allowed_media_types" }
76 #define RTSP_REORDERING_OPTS() \
77 { "reorder_queue_size", "set number of packets to buffer for handling of reordered packets", OFFSET(reordering_queue_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, DEC }
80 {
"initial_pause",
"do not start playing the stream immediately",
OFFSET(initial_pause),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
DEC },
82 {
"rtsp_transport",
"set RTSP transport protocols",
OFFSET(lower_transport_mask),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
DEC|
ENC,
"rtsp_transport" }, \
92 {
"timeout",
"set maximum timeout (in seconds) to wait for incoming connections (-1 is infinite, imply flag listen)",
OFFSET(initial_timeout),
AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX,
DEC },
93 {
"stimeout",
"set timeout (in micro seconds) of socket TCP I/O operations",
OFFSET(stimeout),
AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX,
DEC },
115 const char *sep,
const char **pp)
123 while (!strchr(sep, *p) && *p !=
'\0') {
124 if ((q - buf) < buf_size - 1)
136 if (**pp ==
'/') (*pp)++;
171 struct addrinfo hints = { 0 }, *ai = NULL;
175 memcpy(sock, ai->ai_addr,
FFMIN(
sizeof(*sock), ai->ai_addrlen));
189 if (handler->
alloc) {
199 int payload_type,
const char *p)
221 init_rtp_handler(handler, rtsp_st, codec);
274 char *
value,
int value_size)
289 typedef struct SDPParseState {
294 int nb_default_include_source_addrs;
295 struct RTSPSource **default_include_source_addrs;
296 int nb_default_exclude_source_addrs;
297 struct RTSPSource **default_exclude_source_addrs;
300 static void copy_default_source_addrs(
struct RTSPSource **addrs,
int count,
305 for (i = 0; i <
count; i++) {
307 rtsp_src2 =
av_malloc(
sizeof(*rtsp_src2));
310 memcpy(rtsp_src2, rtsp_src,
sizeof(*rtsp_src));
316 int letter,
const char *buf)
319 char buf1[64], st_type[64];
329 av_dlog(s,
"sdp: %c='%s'\n", letter, buf);
332 if (s1->skip_media && letter !=
'm')
337 if (strcmp(buf1,
"IN") != 0)
340 if (strcmp(buf1,
"IP4") && strcmp(buf1,
"IP6"))
352 s1->default_ip = sdp_ip;
353 s1->default_ttl = ttl;
373 get_word(st_type,
sizeof(st_type), &p);
374 if (!strcmp(st_type,
"audio")) {
376 }
else if (!strcmp(st_type,
"video")) {
378 }
else if (!strcmp(st_type,
"application")) {
391 rtsp_st->
sdp_ip = s1->default_ip;
392 rtsp_st->
sdp_ttl = s1->default_ttl;
394 copy_default_source_addrs(s1->default_include_source_addrs,
395 s1->nb_default_include_source_addrs,
398 copy_default_source_addrs(s1->default_exclude_source_addrs,
399 s1->nb_default_exclude_source_addrs,
407 if (!strcmp(buf1,
"udp"))
409 else if (strstr(buf1,
"/AVPF") || strstr(buf1,
"/SAVPF"))
419 if (!rt->
ts && CONFIG_RTPDEC)
425 init_rtp_handler(handler, rtsp_st, NULL);
426 if (handler && handler->
init)
450 init_rtp_handler(handler, rtsp_st, st->
codec);
451 if (handler && handler->
init)
463 if (!strncmp(p,
"rtsp://", 7))
474 if (proto[0] ==
'\0') {
488 payload_type = atoi(buf1);
492 sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
499 payload_type = atoi(buf1);
517 }
else if (
av_strstart(p,
"IsRealDataType:integer;",&p)) {
520 }
else if (
av_strstart(p,
"SampleRate:integer;", &p) &&
535 if (strcmp(buf1,
"incl") && strcmp(buf1,
"excl"))
537 exclude = !strcmp(buf1,
"excl");
540 if (strcmp(buf1,
"IN") != 0)
543 if (strcmp(buf1,
"IP4") && strcmp(buf1,
"IP6") && strcmp(buf1,
"*"))
555 dynarray_add(&s1->default_exclude_source_addrs, &s1->nb_default_exclude_source_addrs, rtsp_src);
562 dynarray_add(&s1->default_include_source_addrs, &s1->nb_default_include_source_addrs, rtsp_src);
603 SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
617 while (*p !=
'\n' && *p !=
'\r' && *p !=
'\0') {
618 if ((q - buf) <
sizeof(buf) - 1)
623 sdp_parse_line(s, s1, letter, buf);
625 while (*p !=
'\n' && *p !=
'\0')
631 for (i = 0; i < s1->nb_default_include_source_addrs; i++)
632 av_free(s1->default_include_source_addrs[i]);
633 av_freep(&s1->default_include_source_addrs);
634 for (i = 0; i < s1->nb_default_exclude_source_addrs; i++)
635 av_free(s1->default_exclude_source_addrs[i]);
636 av_freep(&s1->default_exclude_source_addrs);
659 if (CONFIG_RTSP_MUXER && rtpctx->
pb && send_packets)
707 if (rt->
ts && CONFIG_RTPDEC)
718 if (reordering_queue_size < 0) {
720 reordering_queue_size = 0;
731 if (s->
oformat && CONFIG_RTSP_MUXER) {
746 else if (CONFIG_RTPDEC)
749 reordering_queue_size);
768 #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
769 static void rtsp_parse_range(
int *min_ptr,
int *max_ptr,
const char **pp)
777 v = strtol(q, &p, 10);
781 v = strtol(p, &p, 10);
793 char transport_protocol[16];
795 char lower_transport[16];
809 get_word_sep(transport_protocol,
sizeof(transport_protocol),
813 lower_transport[0] =
'\0';
820 }
else if (!
av_strcasecmp (transport_protocol,
"x-pn-tng") ||
823 get_word_sep(lower_transport,
sizeof(lower_transport),
"/;,", &p);
828 lower_transport[0] =
'\0';
844 while (*p !=
'\0' && *p !=
',') {
846 if (!strcmp(parameter,
"port")) {
851 }
else if (!strcmp(parameter,
"client_port")) {
857 }
else if (!strcmp(parameter,
"server_port")) {
863 }
else if (!strcmp(parameter,
"interleaved")) {
869 }
else if (!strcmp(parameter,
"multicast")) {
872 }
else if (!strcmp(parameter,
"ttl")) {
876 th->
ttl = strtol(p, &end, 10);
879 }
else if (!strcmp(parameter,
"destination")) {
885 }
else if (!strcmp(parameter,
"source")) {
891 }
else if (!strcmp(parameter,
"mode")) {
895 if (!strcmp(buf,
"record") ||
896 !strcmp(buf,
"receive"))
901 while (*p !=
';' && *p !=
'\0' && *p !=
',')
913 static void handle_rtp_info(
RTSPState *rt,
const char *url,
914 uint32_t seq, uint32_t rtptime)
917 if (!rtptime || !url[0])
933 static void rtsp_parse_rtp_info(
RTSPState *rt,
const char *p)
936 char key[20], value[1024], url[1024] =
"";
937 uint32_t seq = 0, rtptime = 0;
949 if (!strcmp(key,
"url"))
951 else if (!strcmp(key,
"seq"))
952 seq = strtoul(value, NULL, 10);
953 else if (!strcmp(key,
"rtptime"))
954 rtptime = strtoul(value, NULL, 10);
956 handle_rtp_info(rt, url, seq, rtptime);
965 handle_rtp_info(rt, url, seq, rtptime);
979 (t = strtol(p, NULL, 10)) > 0) {
985 rtsp_parse_transport(reply, p);
987 reply->
seq = strtol(p, NULL, 10);
998 reply->
notice = strtol(p, NULL, 10);
1002 }
else if (
av_stristart(p,
"WWW-Authenticate:", &p) && rt) {
1005 }
else if (
av_stristart(p,
"Authentication-Info:", &p) && rt) {
1008 }
else if (
av_stristart(p,
"Content-Base:", &p) && rt) {
1010 if (method && !strcmp(method,
"DESCRIBE"))
1014 if (method && !strcmp(method,
"PLAY"))
1015 rtsp_parse_rtp_info(rt, p);
1017 if (strstr(p,
"GET_PARAMETER") &&
1018 method && !strcmp(method,
"OPTIONS"))
1020 }
else if (
av_stristart(p,
"x-Accept-Dynamic-Rate:", &p) && rt) {
1041 av_dlog(s,
"skipping RTP packet len=%d\n", len);
1046 if (len1 >
sizeof(buf))
1056 unsigned char **content_ptr,
1057 int return_on_interleaved_data,
const char *method)
1060 char buf[4096], buf1[1024], *q;
1063 int ret, content_length, line_count = 0, request = 0;
1064 unsigned char *content = NULL;
1070 memset(reply, 0,
sizeof(*reply));
1078 av_dlog(s,
"ret=%d c=%02x [%c]\n", ret, ch, ch);
1085 if (return_on_interleaved_data) {
1089 }
else if (ch !=
'\r') {
1090 if ((q - buf) <
sizeof(buf) - 1)
1096 av_dlog(s,
"line='%s'\n", buf);
1102 if (line_count == 0) {
1105 if (!strncmp(buf1,
"RTSP/", 5)) {
1126 if (content_length > 0) {
1128 content =
av_malloc(content_length + 1);
1130 content[content_length] =
'\0';
1133 *content_ptr = content;
1140 const char* ptr =
buf;
1142 if (!strcmp(reply->
reason,
"OPTIONS")) {
1143 snprintf(buf,
sizeof(buf),
"RTSP/1.0 200 OK\r\n");
1150 snprintf(buf,
sizeof(buf),
"RTSP/1.0 501 Not Implemented\r\n");
1175 if (rt->
seq != reply->
seq) {
1181 if (reply->
notice == 2101 ||
1183 reply->
notice == 2306 ) {
1185 }
else if (reply->
notice >= 4400 && reply->
notice < 5500) {
1187 }
else if (reply->
notice == 2401 ||
1208 const char *method,
const char *url,
1209 const char *headers,
1210 const unsigned char *send_content,
1211 int send_content_length)
1214 char buf[4096], *out_buf;
1220 snprintf(buf,
sizeof(buf),
"%s %s RTSP/1.0\r\n", method, url);
1225 if (rt->
session_id[0] !=
'\0' && (!headers ||
1226 !strstr(headers,
"\nIf-Match:"))) {
1231 rt->
auth, url, method);
1236 if (send_content_length > 0 && send_content)
1237 av_strlcatf(buf,
sizeof(buf),
"Content-Length: %d\r\n", send_content_length);
1243 out_buf = base64buf;
1246 av_dlog(s,
"Sending:\n%s--\n", buf);
1249 if (send_content_length > 0 && send_content) {
1252 "with content data not supported\n");
1263 const char *url,
const char *headers)
1265 return rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
1270 unsigned char **content_ptr)
1273 content_ptr, NULL, 0);
1277 const char *method,
const char *url,
1280 unsigned char **content_ptr,
1281 const unsigned char *send_content,
1282 int send_content_length)
1286 int ret, attempts = 0;
1290 if ((ret = rtsp_send_cmd_with_content_async(s, method, url, header,
1292 send_content_length)))
1316 int lower_transport,
const char *real_challenge)
1319 int rtx = 0, j, i, err,
interleave = 0, port_off;
1323 const char *trans_pref;
1326 trans_pref =
"x-pn-tng";
1328 trans_pref =
"RAW/RAW";
1330 trans_pref =
"RTP/AVP";
1340 port_off -= port_off & 0x01;
1342 for (j = rt->
rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
1343 char transport[2048];
1379 while (j <= rt->rtp_port_max) {
1380 ff_url_join(buf,
sizeof(buf),
"rtp", NULL, host, -1,
1381 "?localport=%d", j);
1395 snprintf(transport,
sizeof(transport) - 1,
1396 "%s/UDP;", trans_pref);
1398 av_strlcat(transport,
"unicast;",
sizeof(transport));
1400 "client_port=%d", port);
1403 av_strlcatf(transport,
sizeof(transport),
"-%d", port + 1);
1416 snprintf(transport,
sizeof(transport) - 1,
1417 "%s/TCP;", trans_pref);
1419 av_strlcat(transport,
"unicast;",
sizeof(transport));
1421 "interleaved=%d-%d",
1422 interleave, interleave + 1);
1427 snprintf(transport,
sizeof(transport) - 1,
1428 "%s/UDP;multicast", trans_pref);
1431 av_strlcat(transport,
";mode=record",
sizeof(transport));
1434 av_strlcat(transport,
";mode=play",
sizeof(transport));
1436 "Transport: %s\r\n",
1439 av_strlcat(cmd,
"x-Dynamic-Rate: 0\r\n",
sizeof(cmd));
1441 char real_res[41], real_csum[9];
1446 "RealChallenge2: %s, sd=%s\r\n",
1486 char url[1024],
options[30] =
"";
1487 const char *peer = host;
1490 av_strlcpy(options,
"?connect=1",
sizeof(options));
1511 char url[1024], namebuf[50], optbuf[20] =
"";
1525 snprintf(optbuf,
sizeof(optbuf),
"?ttl=%d", ttl);
1526 getnameinfo((
struct sockaddr*) &addr,
sizeof(addr),
1528 ff_url_join(url,
sizeof(url),
"rtp", NULL, namebuf,
1529 port,
"%s", optbuf);
1567 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
1568 int port, err, tcp_fd;
1570 int lower_transport_mask = 0;
1571 char real_challenge[64] =
"";
1573 socklen_t peer_len =
sizeof(peer);
1600 host,
sizeof(host), &port, path,
sizeof(path), s->
filename);
1607 if (!lower_transport_mask)
1616 "only UDP and TCP are supported for output.\n");
1626 host, port,
"%s", path);
1630 char httpname[1024];
1631 char sessioncookie[17];
1634 ff_url_join(httpname,
sizeof(httpname),
"http", auth, host, port,
"%s", path);
1635 snprintf(sessioncookie,
sizeof(sessioncookie),
"%08x%08x",
1647 "x-sessioncookie: %s\r\n"
1648 "Accept: application/x-rtsp-tunnelled\r\n"
1649 "Pragma: no-cache\r\n"
1650 "Cache-Control: no-cache\r\n",
1669 "x-sessioncookie: %s\r\n"
1670 "Content-Type: application/x-rtsp-tunnelled\r\n"
1671 "Pragma: no-cache\r\n"
1672 "Cache-Control: no-cache\r\n"
1673 "Content-Length: 32767\r\n"
1674 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
1704 ff_url_join(tcpname,
sizeof(tcpname),
"tcp", NULL, host, port,
1716 if (!getpeername(tcp_fd, (
struct sockaddr*) &peer, &peer_len)) {
1717 getnameinfo((
struct sockaddr*) &peer, peer_len, host,
sizeof(host),
1736 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1737 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1738 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1739 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1758 if (s->
iformat && CONFIG_RTSP_DEMUXER)
1760 else if (CONFIG_RTSP_MUXER)
1766 int lower_transport =
ff_log2_tab[lower_transport_mask &
1767 ~(lower_transport_mask - 1)];
1771 real_challenge : NULL);
1774 lower_transport_mask &= ~(1 << lower_transport);
1775 if (lower_transport_mask == 0 && err == 1) {
1776 err =
AVERROR(EPROTONOSUPPORT);
1803 uint8_t *buf,
int buf_size, int64_t wait_end)
1807 int n, i,
ret, tcp_fd, timeout_cnt = 0;
1809 struct pollfd *p = rt->
p;
1810 int *fds = NULL, fdsnum, fdsidx;
1820 p[max_p].fd = tcp_fd;
1821 p[max_p++].events = POLLIN;
1835 "Number of fds %d not supported\n", fdsnum);
1838 for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
1839 p[max_p].fd = fds[fdsidx];
1840 p[max_p++].events = POLLIN;
1847 int j = 1 - (tcp_fd == -1);
1852 if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
1855 *prtsp_st = rtsp_st;
1862 #if CONFIG_RTSP_DEMUXER
1863 if (tcp_fd != -1 && p[0].revents & POLLIN) {
1870 "Unable to answer to TEARDOWN\n");
1886 }
else if (n < 0 && errno != EINTR)
1918 "Unable to pick stream for packet - SSRC not known for "
1940 int64_t wait_end = 0;
1951 }
else if (rt->
ts && CONFIG_RTPDEC) {
1962 }
else if (ret == 1) {
1971 int64_t first_queue_time = 0;
1978 if (queue_time && (queue_time - first_queue_time < 0 ||
1979 !first_queue_time)) {
1980 first_queue_time = queue_time;
1984 if (first_queue_time) {
1985 wait_end = first_queue_time + s->
max_delay;
1988 first_queue_st = NULL;
2001 #if CONFIG_RTSP_DEMUXER
2018 len = pick_stream(s, &rtsp_st, rt->
recvbuf, len);
2023 if (len ==
AVERROR(EAGAIN) && first_queue_st &&
2025 rtsp_st = first_queue_st;
2061 if (rtpctx2 && st && st2 &&
2080 }
else if (rt->
ts && CONFIG_RTPDEC) {
2106 #if CONFIG_SDP_DEMUXER
2112 while (p < p_end && *p !=
'\0') {
2113 if (p +
sizeof(
"c=IN IP") - 1 < p_end &&
2117 while (p < p_end - 1 && *p !=
'\n') p++;
2126 static void append_source_addrs(
char *buf,
int size,
const char *
name,
2132 av_strlcatf(buf, size,
"&%s=%s", name, addrs[0]->addr);
2133 for (i = 1; i <
count; i++)
2161 content[
size] =
'\0';
2177 "?localport=%d&ttl=%d&connect=%d&write_to_source=%d",
2182 append_source_addrs(url,
sizeof(url),
"sources",
2185 append_source_addrs(url,
sizeof(url),
"block",
2211 static const AVClass sdp_demuxer_class = {
2226 .priv_class = &sdp_demuxer_class,
2230 #if CONFIG_RTP_DEMUXER
2241 char host[500], sdp[500];
2248 socklen_t addrlen =
sizeof(addr);
2260 ret =
ffurl_read(in, recvbuf,
sizeof(recvbuf));
2270 if ((recvbuf[0] & 0xc0) != 0x80) {
2279 payload_type = recvbuf[1] & 0x7f;
2288 "without an SDP file describing it\n",
2294 "properly you need an SDP file "
2298 av_url_split(NULL, 0, NULL, 0, host,
sizeof(host), &port,
2302 "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
2303 addr.ss_family == AF_INET ? 4 : 6, host,
2306 port, payload_type);
2317 ret = sdp_read_header(s);
2328 static const AVClass rtp_demuxer_class = {
2344 .priv_class = &rtp_demuxer_class,