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_SUBTITLE+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" }, \
75 { "subtitle", "Subtitle", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_SUBTITLE}, 0, 0, DEC, "allowed_media_types" }
77 #define RTSP_REORDERING_OPTS() \
78 { "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 }
81 {
"initial_pause",
"do not start playing the stream immediately",
OFFSET(initial_pause),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
DEC },
83 {
"rtsp_transport",
"set RTSP transport protocols",
OFFSET(lower_transport_mask),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
DEC|
ENC,
"rtsp_transport" }, \
90 {
"prefer_tcp",
"try RTP via TCP first, if available", 0,
AV_OPT_TYPE_CONST, {.i64 =
RTSP_FLAG_PREFER_TCP}, 0, 0,
DEC|
ENC,
"rtsp_flags" },
94 {
"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 },
95 {
"stimeout",
"set timeout (in microseconds) of socket TCP I/O operations",
OFFSET(stimeout),
AV_OPT_TYPE_INT, {.i64 = 0}, INT_MIN, INT_MAX,
DEC },
117 const char *sep,
const char **pp)
125 while (!strchr(sep, *p) && *p !=
'\0') {
126 if ((q - buf) < buf_size - 1)
138 if (**pp ==
'/') (*pp)++;
177 memcpy(sock, ai->ai_addr,
FFMIN(
sizeof(*sock), ai->ai_addrlen));
223 int payload_type,
const char *p)
245 init_rtp_handler(handler, rtsp_st, st);
288 finalize_rtp_handler_init(s, rtsp_st, st);
296 char *
value,
int value_size)
311 typedef struct SDPParseState {
316 int nb_default_include_source_addrs;
317 struct RTSPSource **default_include_source_addrs;
318 int nb_default_exclude_source_addrs;
319 struct RTSPSource **default_exclude_source_addrs;
322 char delayed_fmtp[2048];
325 static void copy_default_source_addrs(
struct RTSPSource **addrs,
int count,
330 for (i = 0; i <
count; i++) {
332 rtsp_src2 =
av_malloc(
sizeof(*rtsp_src2));
335 memcpy(rtsp_src2, rtsp_src,
sizeof(*rtsp_src));
341 int payload_type,
const char *
line)
357 int letter,
const char *buf)
360 char buf1[64], st_type[64];
370 av_dlog(s,
"sdp: %c='%s'\n", letter, buf);
373 if (s1->skip_media && letter !=
'm')
378 if (strcmp(buf1,
"IN") != 0)
381 if (strcmp(buf1,
"IP4") && strcmp(buf1,
"IP6"))
393 s1->default_ip = sdp_ip;
394 s1->default_ttl = ttl;
416 get_word(st_type,
sizeof(st_type), &p);
417 if (!strcmp(st_type,
"audio")) {
419 }
else if (!strcmp(st_type,
"video")) {
421 }
else if (!strcmp(st_type,
"application")) {
423 }
else if (!strcmp(st_type,
"text")) {
436 rtsp_st->
sdp_ip = s1->default_ip;
437 rtsp_st->
sdp_ttl = s1->default_ttl;
439 copy_default_source_addrs(s1->default_include_source_addrs,
440 s1->nb_default_include_source_addrs,
443 copy_default_source_addrs(s1->default_exclude_source_addrs,
444 s1->nb_default_exclude_source_addrs,
452 if (!strcmp(buf1,
"udp"))
454 else if (strstr(buf1,
"/AVPF") || strstr(buf1,
"/SAVPF"))
464 if (CONFIG_RTPDEC && !rt->
ts)
470 init_rtp_handler(handler, rtsp_st,
NULL);
471 finalize_rtp_handler_init(s, rtsp_st,
NULL);
494 init_rtp_handler(handler, rtsp_st, st);
495 finalize_rtp_handler_init(s, rtsp_st, st);
507 if (!strncmp(p,
"rtsp://", 7))
518 if (proto[0] ==
'\0') {
532 payload_type = atoi(buf1);
536 sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
540 parse_fmtp(s, rt, payload_type, s1->delayed_fmtp);
546 payload_type = atoi(buf1);
547 if (s1->seen_rtpmap) {
551 av_strlcpy(s1->delayed_fmtp, buf,
sizeof(s1->delayed_fmtp));
572 }
else if (
av_strstart(p,
"IsRealDataType:integer;",&p)) {
575 }
else if (
av_strstart(p,
"SampleRate:integer;", &p) &&
590 if (strcmp(buf1,
"incl") && strcmp(buf1,
"excl"))
592 exclude = !strcmp(buf1,
"excl");
595 if (strcmp(buf1,
"IN") != 0)
598 if (strcmp(buf1,
"IP4") && strcmp(buf1,
"IP6") && strcmp(buf1,
"*"))
610 dynarray_add(&s1->default_exclude_source_addrs, &s1->nb_default_exclude_source_addrs, rtsp_src);
617 dynarray_add(&s1->default_include_source_addrs, &s1->nb_default_include_source_addrs, rtsp_src);
658 SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
672 while (*p !=
'\n' && *p !=
'\r' && *p !=
'\0') {
673 if ((q - buf) <
sizeof(buf) - 1)
678 sdp_parse_line(s, s1, letter, buf);
680 while (*p !=
'\n' && *p !=
'\0')
686 for (i = 0; i < s1->nb_default_include_source_addrs; i++)
687 av_freep(&s1->default_include_source_addrs[i]);
688 av_freep(&s1->default_include_source_addrs);
689 for (i = 0; i < s1->nb_default_exclude_source_addrs; i++)
690 av_freep(&s1->default_exclude_source_addrs[i]);
691 av_freep(&s1->default_exclude_source_addrs);
713 if (CONFIG_RTSP_MUXER && rtpctx->
pb && send_packets)
763 if (CONFIG_RTPDEC && rt->
ts)
774 if (reordering_queue_size < 0) {
776 reordering_queue_size = 0;
787 if (CONFIG_RTSP_MUXER && s->
oformat) {
803 else if (CONFIG_RTPDEC)
806 reordering_queue_size);
825 #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
826 static void rtsp_parse_range(
int *min_ptr,
int *max_ptr,
const char **pp)
834 v = strtol(q, &p, 10);
838 v = strtol(p, &p, 10);
850 char transport_protocol[16];
852 char lower_transport[16];
866 get_word_sep(transport_protocol,
sizeof(transport_protocol),
870 lower_transport[0] =
'\0';
877 }
else if (!
av_strcasecmp (transport_protocol,
"x-pn-tng") ||
880 get_word_sep(lower_transport,
sizeof(lower_transport),
"/;,", &p);
885 lower_transport[0] =
'\0';
901 while (*p !=
'\0' && *p !=
',') {
903 if (!strcmp(parameter,
"port")) {
908 }
else if (!strcmp(parameter,
"client_port")) {
914 }
else if (!strcmp(parameter,
"server_port")) {
920 }
else if (!strcmp(parameter,
"interleaved")) {
926 }
else if (!strcmp(parameter,
"multicast")) {
929 }
else if (!strcmp(parameter,
"ttl")) {
933 th->
ttl = strtol(p, &end, 10);
936 }
else if (!strcmp(parameter,
"destination")) {
942 }
else if (!strcmp(parameter,
"source")) {
948 }
else if (!strcmp(parameter,
"mode")) {
952 if (!strcmp(buf,
"record") ||
953 !strcmp(buf,
"receive"))
958 while (*p !=
';' && *p !=
'\0' && *p !=
',')
970 static void handle_rtp_info(
RTSPState *rt,
const char *url,
971 uint32_t seq, uint32_t rtptime)
974 if (!rtptime || !url[0])
990 static void rtsp_parse_rtp_info(
RTSPState *rt,
const char *p)
993 char key[20], value[1024], url[1024] =
"";
994 uint32_t seq = 0, rtptime = 0;
1006 if (!strcmp(key,
"url"))
1008 else if (!strcmp(key,
"seq"))
1009 seq = strtoul(value,
NULL, 10);
1010 else if (!strcmp(key,
"rtptime"))
1011 rtptime = strtoul(value,
NULL, 10);
1013 handle_rtp_info(rt, url, seq, rtptime);
1022 handle_rtp_info(rt, url, seq, rtptime);
1036 (t = strtol(p,
NULL, 10)) > 0) {
1042 rtsp_parse_transport(reply, p);
1044 reply->
seq = strtol(p,
NULL, 10);
1059 }
else if (
av_stristart(p,
"WWW-Authenticate:", &p) && rt) {
1062 }
else if (
av_stristart(p,
"Authentication-Info:", &p) && rt) {
1065 }
else if (
av_stristart(p,
"Content-Base:", &p) && rt) {
1067 if (method && !strcmp(method,
"DESCRIBE"))
1071 if (method && !strcmp(method,
"PLAY"))
1072 rtsp_parse_rtp_info(rt, p);
1074 if (strstr(p,
"GET_PARAMETER") &&
1075 method && !strcmp(method,
"OPTIONS"))
1077 }
else if (
av_stristart(p,
"x-Accept-Dynamic-Rate:", &p) && rt) {
1098 av_dlog(s,
"skipping RTP packet len=%d\n", len);
1103 if (len1 >
sizeof(buf))
1113 unsigned char **content_ptr,
1114 int return_on_interleaved_data,
const char *method)
1117 char buf[4096], buf1[1024], *q;
1120 int ret, content_length, line_count = 0, request = 0;
1121 unsigned char *content =
NULL;
1127 memset(reply, 0,
sizeof(*reply));
1135 av_dlog(s,
"ret=%d c=%02x [%c]\n", ret, ch, ch);
1142 if (return_on_interleaved_data) {
1146 }
else if (ch !=
'\r') {
1147 if ((q - buf) <
sizeof(buf) - 1)
1153 av_dlog(s,
"line='%s'\n", buf);
1159 if (line_count == 0) {
1162 if (!strncmp(buf1,
"RTSP/", 5)) {
1183 if (content_length > 0) {
1185 content =
av_malloc(content_length + 1);
1189 content[content_length] =
'\0';
1192 *content_ptr = content;
1199 const char* ptr =
buf;
1201 if (!strcmp(reply->
reason,
"OPTIONS")) {
1202 snprintf(buf,
sizeof(buf),
"RTSP/1.0 200 OK\r\n");
1209 snprintf(buf,
sizeof(buf),
"RTSP/1.0 501 Not Implemented\r\n");
1234 if (rt->
seq != reply->
seq) {
1240 if (reply->
notice == 2101 ||
1242 reply->
notice == 2306 ) {
1244 }
else if (reply->
notice >= 4400 && reply->
notice < 5500) {
1246 }
else if (reply->
notice == 2401 ||
1267 const char *method,
const char *url,
1268 const char *headers,
1269 const unsigned char *send_content,
1270 int send_content_length)
1273 char buf[4096], *out_buf;
1279 snprintf(buf,
sizeof(buf),
"%s %s RTSP/1.0\r\n", method, url);
1284 if (rt->
session_id[0] !=
'\0' && (!headers ||
1285 !strstr(headers,
"\nIf-Match:"))) {
1290 rt->
auth, url, method);
1295 if (send_content_length > 0 && send_content)
1296 av_strlcatf(buf,
sizeof(buf),
"Content-Length: %d\r\n", send_content_length);
1302 out_buf = base64buf;
1305 av_dlog(s,
"Sending:\n%s--\n", buf);
1308 if (send_content_length > 0 && send_content) {
1311 "with content data not supported\n");
1322 const char *url,
const char *headers)
1324 return rtsp_send_cmd_with_content_async(s, method, url, headers,
NULL, 0);
1329 unsigned char **content_ptr)
1332 content_ptr,
NULL, 0);
1336 const char *method,
const char *url,
1339 unsigned char **content_ptr,
1340 const unsigned char *send_content,
1341 int send_content_length)
1345 int ret, attempts = 0;
1349 if ((ret = rtsp_send_cmd_with_content_async(s, method, url, header,
1351 send_content_length)))
1375 int lower_transport,
const char *real_challenge)
1378 int rtx = 0, j, i, err,
interleave = 0, port_off;
1382 const char *trans_pref;
1385 trans_pref =
"x-pn-tng";
1387 trans_pref =
"RAW/RAW";
1389 trans_pref =
"RTP/AVP";
1399 port_off -= port_off & 0x01;
1401 for (j = rt->
rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
1402 char transport[2048];
1438 while (j <= rt->rtp_port_max) {
1440 "?localport=%d", j);
1454 snprintf(transport,
sizeof(transport) - 1,
1455 "%s/UDP;", trans_pref);
1457 av_strlcat(transport,
"unicast;",
sizeof(transport));
1459 "client_port=%d", port);
1462 av_strlcatf(transport,
sizeof(transport),
"-%d", port + 1);
1475 snprintf(transport,
sizeof(transport) - 1,
1476 "%s/TCP;", trans_pref);
1478 av_strlcat(transport,
"unicast;",
sizeof(transport));
1480 "interleaved=%d-%d",
1481 interleave, interleave + 1);
1486 snprintf(transport,
sizeof(transport) - 1,
1487 "%s/UDP;multicast", trans_pref);
1490 av_strlcat(transport,
";mode=record",
sizeof(transport));
1493 av_strlcat(transport,
";mode=play",
sizeof(transport));
1495 "Transport: %s\r\n",
1498 av_strlcat(cmd,
"x-Dynamic-Rate: 0\r\n",
sizeof(cmd));
1500 char real_res[41], real_csum[9];
1505 "RealChallenge2: %s, sd=%s\r\n",
1545 char url[1024],
options[30] =
"";
1546 const char *peer = host;
1549 av_strlcpy(options,
"?connect=1",
sizeof(options));
1563 char url[1024], namebuf[50], optbuf[20] =
"";
1577 snprintf(optbuf,
sizeof(optbuf),
"?ttl=%d", ttl);
1578 getnameinfo((
struct sockaddr*) &addr,
sizeof(addr),
1581 port,
"%s", optbuf);
1619 char proto[128], host[1024], path[1024];
1620 char tcpname[1024], cmd[2048], auth[128];
1621 const char *lower_rtsp_proto =
"tcp";
1622 int port, err, tcp_fd;
1624 int lower_transport_mask = 0;
1626 char real_challenge[64] =
"";
1628 socklen_t peer_len =
sizeof(peer);
1654 host,
sizeof(host), &port, path,
sizeof(path), s->
filename);
1656 if (!strcmp(proto,
"rtsps")) {
1657 lower_rtsp_proto =
"tls";
1666 port = default_port;
1670 if (!lower_transport_mask)
1679 "only UDP and TCP are supported for output.\n");
1689 host, port,
"%s", path);
1693 char httpname[1024];
1694 char sessioncookie[17];
1697 ff_url_join(httpname,
sizeof(httpname),
"http", auth, host, port,
"%s", path);
1698 snprintf(sessioncookie,
sizeof(sessioncookie),
"%08x%08x",
1710 "x-sessioncookie: %s\r\n"
1711 "Accept: application/x-rtsp-tunnelled\r\n"
1712 "Pragma: no-cache\r\n"
1713 "Cache-Control: no-cache\r\n",
1732 "x-sessioncookie: %s\r\n"
1733 "Content-Type: application/x-rtsp-tunnelled\r\n"
1734 "Pragma: no-cache\r\n"
1735 "Cache-Control: no-cache\r\n"
1736 "Content-Length: 32767\r\n"
1737 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
1785 if (!getpeername(tcp_fd, (
struct sockaddr*) &peer, &peer_len)) {
1786 getnameinfo((
struct sockaddr*) &peer, peer_len, host,
sizeof(host),
1805 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1806 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1807 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1808 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1827 if (CONFIG_RTSP_DEMUXER && s->
iformat)
1829 else if (CONFIG_RTSP_MUXER)
1837 int lower_transport =
ff_log2_tab[lower_transport_mask &
1838 ~(lower_transport_mask - 1)];
1846 real_challenge :
NULL);
1849 lower_transport_mask &= ~(1 << lower_transport);
1850 if (lower_transport_mask == 0 && err == 1) {
1851 err =
AVERROR(EPROTONOSUPPORT);
1879 uint8_t *buf,
int buf_size, int64_t wait_end)
1883 int n, i,
ret, tcp_fd, timeout_cnt = 0;
1885 struct pollfd *p = rt->
p;
1886 int *fds =
NULL, fdsnum, fdsidx;
1896 p[max_p].fd = tcp_fd;
1897 p[max_p++].events = POLLIN;
1911 "Number of fds %d not supported\n", fdsnum);
1914 for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
1915 p[max_p].fd = fds[fdsidx];
1916 p[max_p++].events = POLLIN;
1923 int j = 1 - (tcp_fd == -1);
1928 if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
1931 *prtsp_st = rtsp_st;
1938 #if CONFIG_RTSP_DEMUXER
1939 if (tcp_fd != -1 && p[0].revents & POLLIN) {
1946 "Unable to answer to TEARDOWN\n");
1962 }
else if (n < 0 && errno != EINTR)
1994 "Unable to pick stream for packet - SSRC not known for "
2016 int64_t wait_end = 0;
2027 }
else if (CONFIG_RTPDEC && rt->
ts) {
2038 }
else if (ret == 1) {
2047 int64_t first_queue_time = 0;
2054 if (queue_time && (queue_time - first_queue_time < 0 ||
2055 !first_queue_time)) {
2056 first_queue_time = queue_time;
2060 if (first_queue_time) {
2061 wait_end = first_queue_time + s->
max_delay;
2064 first_queue_st =
NULL;
2077 #if CONFIG_RTSP_DEMUXER
2094 len = pick_stream(s, &rtsp_st, rt->
recvbuf, len);
2099 if (len ==
AVERROR(EAGAIN) && first_queue_st &&
2101 rtsp_st = first_queue_st;
2137 if (rtpctx2 && st && st2 &&
2166 }
else if (CONFIG_RTPDEC && rt->
ts) {
2192 #if CONFIG_SDP_DEMUXER
2198 while (p < p_end && *p !=
'\0') {
2199 if (p +
sizeof(
"c=IN IP") - 1 < p_end &&
2203 while (p < p_end - 1 && *p !=
'\n') p++;
2212 static void append_source_addrs(
char *buf,
int size,
const char *
name,
2218 av_strlcatf(buf, size,
"&%s=%s", name, addrs[0]->addr);
2219 for (i = 1; i <
count; i++)
2247 content[
size] =
'\0';
2263 "?localport=%d&ttl=%d&connect=%d&write_to_source=%d",
2268 append_source_addrs(url,
sizeof(url),
"sources",
2271 append_source_addrs(url,
sizeof(url),
"block",
2297 static const AVClass sdp_demuxer_class = {
2312 .priv_class = &sdp_demuxer_class,
2316 #if CONFIG_RTP_DEMUXER
2327 char host[500], sdp[500];
2334 socklen_t addrlen =
sizeof(addr);
2346 ret =
ffurl_read(in, recvbuf,
sizeof(recvbuf));
2356 if ((recvbuf[0] & 0xc0) != 0x80) {
2365 payload_type = recvbuf[1] & 0x7f;
2374 "without an SDP file describing it\n",
2380 "properly you need an SDP file "
2388 "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
2389 addr.ss_family == AF_INET ? 4 : 6, host,
2392 port, payload_type);
2403 ret = sdp_read_header(s);
2414 static const AVClass rtp_demuxer_class = {
2430 .priv_class = &rtp_demuxer_class,