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" }, \
69 { "listen", "Wait for incoming connections", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_LISTEN}, 0, 0, DEC, "rtsp_flags" }
71 #define RTSP_MEDIATYPE_OPTS(name, longname) \
72 { 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" }, \
73 { "video", "Video", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_VIDEO}, 0, 0, DEC, "allowed_media_types" }, \
74 { "audio", "Audio", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_AUDIO}, 0, 0, DEC, "allowed_media_types" }, \
75 { "data", "Data", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_DATA}, 0, 0, DEC, "allowed_media_types" }
77 #define RTSP_REORDERING_OPTS() \
78 { "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 }
81 {
"initial_pause",
"Don't start playing the stream immediately",
OFFSET(initial_pause),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
DEC },
83 {
"rtsp_transport",
"RTSP transport protocols",
OFFSET(lower_transport_mask),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX,
DEC|
ENC,
"rtsp_transport" }, \
92 {
"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 },
112 const char *sep,
const char **pp)
120 while (!strchr(sep, *p) && *p !=
'\0') {
121 if ((q - buf) < buf_size - 1)
133 if (**pp ==
'/') (*pp)++;
137 static void get_word(
char *buf,
int buf_size,
const char **pp)
172 memcpy(sock, ai->ai_addr,
FFMIN(
sizeof(*sock), ai->ai_addrlen));
185 if (handler->
alloc) {
195 int payload_type,
const char *p)
219 init_rtp_handler(handler, rtsp_st, codec);
238 av_log(s, AV_LOG_DEBUG,
"audio codec set to: %s\n", c_name);
253 av_log(s, AV_LOG_DEBUG,
"audio samplerate set to: %i\n",
255 av_log(s, AV_LOG_DEBUG,
"audio channels set to: %i\n",
259 av_log(s, AV_LOG_DEBUG,
"video codec set to: %s\n", c_name);
276 char *value,
int value_size)
291 typedef struct SDPParseState {
299 int letter,
const char *buf)
302 char buf1[64], st_type[64];
311 av_dlog(s,
"sdp: %c='%s'\n", letter, buf);
314 if (s1->skip_media && letter !=
'm')
319 if (strcmp(buf1,
"IN") != 0)
322 if (strcmp(buf1,
"IP4") && strcmp(buf1,
"IP6"))
334 s1->default_ip = sdp_ip;
335 s1->default_ttl = ttl;
355 get_word(st_type,
sizeof(st_type), &p);
356 if (!strcmp(st_type,
"audio")) {
358 }
else if (!strcmp(st_type,
"video")) {
360 }
else if (!strcmp(st_type,
"application")) {
373 rtsp_st->
sdp_ip = s1->default_ip;
374 rtsp_st->
sdp_ttl = s1->default_ttl;
380 if (!strcmp(buf1,
"udp"))
412 init_rtp_handler(handler, rtsp_st, st->
codec);
413 if (handler && handler->
init)
425 if (!strncmp(p,
"rtsp://", 7))
436 if (proto[0] ==
'\0') {
450 payload_type = atoi(buf1);
454 sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
461 payload_type = atoi(buf1);
478 AV_NOPTS_VALUE : end - start;
479 }
else if (
av_strstart(p,
"IsRealDataType:integer;",&p)) {
482 }
else if (
av_strstart(p,
"SampleRate:integer;", &p) &&
520 SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
534 while (*p !=
'\n' && *p !=
'\r' && *p !=
'\0') {
535 if ((q - buf) <
sizeof(buf) - 1)
540 sdp_parse_line(s, s1, letter, buf);
542 while (*p !=
'\n' && *p !=
'\0')
618 if (reordering_queue_size < 0) {
620 reordering_queue_size = 0;
649 reordering_queue_size);
664 #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
665 static void rtsp_parse_range(
int *min_ptr,
int *max_ptr,
const char **pp)
673 v = strtol(q, &p, 10);
677 v = strtol(p, &p, 10);
689 char transport_protocol[16];
691 char lower_transport[16];
705 get_word_sep(transport_protocol,
sizeof(transport_protocol),
709 lower_transport[0] =
'\0';
716 }
else if (!
av_strcasecmp (transport_protocol,
"x-pn-tng") ||
719 get_word_sep(lower_transport,
sizeof(lower_transport),
"/;,", &p);
724 lower_transport[0] =
'\0';
740 while (*p !=
'\0' && *p !=
',') {
742 if (!strcmp(parameter,
"port")) {
747 }
else if (!strcmp(parameter,
"client_port")) {
753 }
else if (!strcmp(parameter,
"server_port")) {
759 }
else if (!strcmp(parameter,
"interleaved")) {
765 }
else if (!strcmp(parameter,
"multicast")) {
768 }
else if (!strcmp(parameter,
"ttl")) {
772 th->
ttl = strtol(p, &end, 10);
775 }
else if (!strcmp(parameter,
"destination")) {
781 }
else if (!strcmp(parameter,
"source")) {
787 }
else if (!strcmp(parameter,
"mode")) {
791 if (!strcmp(buf,
"record") ||
792 !strcmp(buf,
"receive"))
797 while (*p !=
';' && *p !=
'\0' && *p !=
',')
809 static void handle_rtp_info(
RTSPState *rt,
const char *url,
810 uint32_t seq, uint32_t rtptime)
813 if (!rtptime || !url[0])
829 static void rtsp_parse_rtp_info(
RTSPState *rt,
const char *p)
832 char key[20], value[1024], url[1024] =
"";
833 uint32_t seq = 0, rtptime = 0;
845 if (!strcmp(key,
"url"))
847 else if (!strcmp(key,
"seq"))
848 seq = strtoul(value,
NULL, 10);
849 else if (!strcmp(key,
"rtptime"))
850 rtptime = strtoul(value,
NULL, 10);
852 handle_rtp_info(rt, url, seq, rtptime);
861 handle_rtp_info(rt, url, seq, rtptime);
875 (t = strtol(p,
NULL, 10)) > 0) {
881 rtsp_parse_transport(reply, p);
883 reply->
seq = strtol(p,
NULL, 10);
898 }
else if (
av_stristart(p,
"WWW-Authenticate:", &p) && rt) {
901 }
else if (
av_stristart(p,
"Authentication-Info:", &p) && rt) {
904 }
else if (
av_stristart(p,
"Content-Base:", &p) && rt) {
906 if (method && !strcmp(method,
"DESCRIBE"))
910 if (method && !strcmp(method,
"PLAY"))
911 rtsp_parse_rtp_info(rt, p);
913 if (strstr(p,
"GET_PARAMETER") &&
914 method && !strcmp(method,
"OPTIONS"))
916 }
else if (
av_stristart(p,
"x-Accept-Dynamic-Rate:", &p) && rt) {
937 av_dlog(s,
"skipping RTP packet len=%d\n", len);
942 if (len1 >
sizeof(buf))
952 unsigned char **content_ptr,
953 int return_on_interleaved_data,
const char *method)
956 char buf[4096], buf1[1024], *q;
959 int ret, content_length, line_count = 0, request = 0;
960 unsigned char *content =
NULL;
966 memset(reply, 0,
sizeof(*reply));
974 av_dlog(s,
"ret=%d c=%02x [%c]\n", ret, ch, ch);
981 if (return_on_interleaved_data) {
985 }
else if (ch !=
'\r') {
986 if ((q - buf) <
sizeof(buf) - 1)
992 av_dlog(s,
"line='%s'\n", buf);
998 if (line_count == 0) {
1001 if (!strncmp(buf1,
"RTSP/", 5)) {
1022 if (content_length > 0) {
1024 content =
av_malloc(content_length + 1);
1026 content[content_length] =
'\0';
1029 *content_ptr = content;
1036 const char* ptr = buf;
1038 if (!strcmp(reply->
reason,
"OPTIONS")) {
1039 snprintf(buf,
sizeof(buf),
"RTSP/1.0 200 OK\r\n");
1046 snprintf(buf,
sizeof(buf),
"RTSP/1.0 501 Not Implemented\r\n");
1071 if (rt->
seq != reply->
seq) {
1072 av_log(s, AV_LOG_WARNING,
"CSeq %d expected, %d received.\n",
1077 if (reply->
notice == 2101 ||
1079 reply->
notice == 2306 ) {
1081 }
else if (reply->
notice >= 4400 && reply->
notice < 5500) {
1083 }
else if (reply->
notice == 2401 ||
1104 const char *method,
const char *url,
1105 const char *headers,
1106 const unsigned char *send_content,
1107 int send_content_length)
1110 char buf[4096], *out_buf;
1116 snprintf(buf,
sizeof(buf),
"%s %s RTSP/1.0\r\n", method, url);
1120 if (rt->
session_id[0] !=
'\0' && (!headers ||
1121 !strstr(headers,
"\nIf-Match:"))) {
1126 rt->
auth, url, method);
1131 if (send_content_length > 0 && send_content)
1132 av_strlcatf(buf,
sizeof(buf),
"Content-Length: %d\r\n", send_content_length);
1138 out_buf = base64buf;
1141 av_dlog(s,
"Sending:\n%s--\n", buf);
1144 if (send_content_length > 0 && send_content) {
1146 av_log(s, AV_LOG_ERROR,
"tunneling of RTSP requests "
1147 "with content data not supported\n");
1158 const char *url,
const char *headers)
1160 return ff_rtsp_send_cmd_with_content_async(s, method, url, headers,
NULL, 0);
1165 unsigned char **content_ptr)
1168 content_ptr,
NULL, 0);
1172 const char *method,
const char *url,
1175 unsigned char **content_ptr,
1176 const unsigned char *send_content,
1177 int send_content_length)
1181 int ret, attempts = 0;
1185 if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
1187 send_content_length)))
1200 av_log(s, AV_LOG_ERROR,
"method %s failed: %d%s\n",
1211 int lower_transport,
const char *real_challenge)
1214 int rtx = 0, j, i, err, interleave = 0, port_off;
1218 const char *trans_pref;
1221 trans_pref =
"x-pn-tng";
1223 trans_pref =
"RAW/RAW";
1225 trans_pref =
"RTP/AVP";
1239 port_off -= port_off & 0x01;
1241 for (j = rt->
rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
1242 char transport[2048];
1278 while (j <= rt->rtp_port_max) {
1280 "?localport=%d", j);
1288 av_log(s, AV_LOG_ERROR,
"Unable to open an input RTP port\n");
1295 snprintf(transport,
sizeof(transport) - 1,
1296 "%s/UDP;", trans_pref);
1298 av_strlcat(transport,
"unicast;",
sizeof(transport));
1300 "client_port=%d", port);
1303 av_strlcatf(transport,
sizeof(transport),
"-%d", port + 1);
1316 snprintf(transport,
sizeof(transport) - 1,
1317 "%s/TCP;", trans_pref);
1319 av_strlcat(transport,
"unicast;",
sizeof(transport));
1321 "interleaved=%d-%d",
1322 interleave, interleave + 1);
1327 snprintf(transport,
sizeof(transport) - 1,
1328 "%s/UDP;multicast", trans_pref);
1331 av_strlcat(transport,
";mode=record",
sizeof(transport));
1334 av_strlcat(transport,
";mode=play",
sizeof(transport));
1335 snprintf(cmd,
sizeof(cmd),
1336 "Transport: %s\r\n",
1339 av_strlcat(cmd,
"x-Dynamic-Rate: 0\r\n",
sizeof(cmd));
1341 char real_res[41], real_csum[9];
1346 "RealChallenge2: %s, sd=%s\r\n",
1374 av_log(s, AV_LOG_ERROR,
"Nonmatching transport in server reply\n");
1386 char url[1024],
options[30] =
"";
1389 av_strlcpy(options,
"?connect=1",
sizeof(options));
1414 char url[1024], namebuf[50], optbuf[20] =
"";
1428 snprintf(optbuf,
sizeof(optbuf),
"?ttl=%d", ttl);
1429 getnameinfo((
struct sockaddr*) &addr,
sizeof(addr),
1432 port,
"%s", optbuf);
1470 char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
1471 int port, err, tcp_fd;
1473 int lower_transport_mask = 0;
1474 char real_challenge[64] =
"";
1476 socklen_t peer_len =
sizeof(peer);
1479 av_log(s, AV_LOG_ERROR,
"Invalid UDP port range, max port %d less "
1503 host,
sizeof(host), &port, path,
sizeof(path), s->
filename);
1510 if (!lower_transport_mask)
1518 av_log(s, AV_LOG_ERROR,
"Unsupported lower transport method, "
1519 "only UDP and TCP are supported for output.\n");
1529 host, port,
"%s", path);
1533 char httpname[1024];
1534 char sessioncookie[17];
1537 ff_url_join(httpname,
sizeof(httpname),
"http", auth, host, port,
"%s", path);
1538 snprintf(sessioncookie,
sizeof(sessioncookie),
"%08x%08x",
1549 snprintf(headers,
sizeof(headers),
1550 "x-sessioncookie: %s\r\n"
1551 "Accept: application/x-rtsp-tunnelled\r\n"
1552 "Pragma: no-cache\r\n"
1553 "Cache-Control: no-cache\r\n",
1571 snprintf(headers,
sizeof(headers),
1572 "x-sessioncookie: %s\r\n"
1573 "Content-Type: application/x-rtsp-tunnelled\r\n"
1574 "Pragma: no-cache\r\n"
1575 "Cache-Control: no-cache\r\n"
1576 "Content-Length: 32767\r\n"
1577 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
1618 if (!getpeername(tcp_fd, (
struct sockaddr*) &peer, &peer_len)) {
1619 getnameinfo((
struct sockaddr*) &peer, peer_len, host,
sizeof(host),
1638 "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
1639 "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
1640 "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
1641 "GUID: 00000000-0000-0000-0000-000000000000\r\n",
1668 int lower_transport =
ff_log2_tab[lower_transport_mask &
1669 ~(lower_transport_mask - 1)];
1673 real_challenge :
NULL);
1676 lower_transport_mask &= ~(1 << lower_transport);
1677 if (lower_transport_mask == 0 && err == 1) {
1678 err =
AVERROR(EPROTONOSUPPORT);
1693 av_log(s, AV_LOG_INFO,
"Status %d: Redirecting to %s\n",
1705 uint8_t *buf,
int buf_size, int64_t wait_end)
1709 int n, i, ret, tcp_fd, timeout_cnt = 0;
1711 struct pollfd *p = rt->
p;
1712 int *fds =
NULL, fdsnum, fdsidx;
1722 p[max_p].fd = tcp_fd;
1723 p[max_p++].events = POLLIN;
1732 av_log(s, AV_LOG_ERROR,
"Unable to recover rtp ports\n");
1737 "Number of fds %d not supported\n", fdsnum);
1740 for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
1741 p[max_p].fd = fds[fdsidx];
1742 p[max_p++].events = POLLIN;
1749 int j = 1 - (tcp_fd == -1);
1754 if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
1757 *prtsp_st = rtsp_st;
1764 #if CONFIG_RTSP_DEMUXER
1765 if (tcp_fd != -1 && p[0].revents & POLLIN) {
1771 av_log(s, AV_LOG_WARNING,
1772 "Unable to answer to TEARDOWN\n");
1788 }
else if (n < 0 && errno != EINTR)
1819 av_log(s, AV_LOG_WARNING,
1820 "Unable to pick stream for packet - SSRC not known for "
1833 av_log(s, AV_LOG_WARNING,
"Unable to pick stream for packet\n");
1842 int64_t wait_end = 0;
1864 }
else if (ret == 1) {
1872 int64_t first_queue_time = 0;
1879 if (queue_time && (queue_time - first_queue_time < 0 ||
1880 !first_queue_time)) {
1881 first_queue_time = queue_time;
1885 if (first_queue_time)
1886 wait_end = first_queue_time + s->
max_delay;
1899 #if CONFIG_RTSP_DEMUXER
1912 len = pick_stream(s, &rtsp_st, rt->
recvbuf, len);
1917 if (len ==
AVERROR(EAGAIN) && first_queue_st &&
1919 rtsp_st = first_queue_st;
1949 if (rtpctx2 && st && st2 &&
1961 av_log(s, AV_LOG_DEBUG,
"Received BYE for stream %d (%d/%d)\n",
1994 #if CONFIG_SDP_DEMUXER
2000 while (p < p_end && *p !=
'\0') {
2001 if (p +
sizeof(
"c=IN IP") - 1 < p_end &&
2005 while (p < p_end - 1 && *p !=
'\n') p++;
2038 content[
size] =
'\0';
2054 "?localport=%d&ttl=%d&connect=%d", rtsp_st->
sdp_port,
2080 static const AVClass sdp_demuxer_class = {
2095 .priv_class = &sdp_demuxer_class,
2099 #if CONFIG_RTP_DEMUXER
2110 char host[500], sdp[500];
2117 socklen_t addrlen =
sizeof(addr);
2129 ret =
ffurl_read(in, recvbuf,
sizeof(recvbuf));
2135 av_log(s, AV_LOG_WARNING,
"Received too short packet\n");
2139 if ((recvbuf[0] & 0xc0) != 0x80) {
2140 av_log(s, AV_LOG_WARNING,
"Unsupported RTP version packet "
2148 payload_type = recvbuf[1] & 0x7f;
2156 av_log(s, AV_LOG_ERROR,
"Unable to receive RTP payload type %d "
2157 "without an SDP file describing it\n",
2162 av_log(s, AV_LOG_WARNING,
"Guessing on RTP content - if not received "
2163 "properly you need an SDP file "
2170 snprintf(sdp,
sizeof(sdp),
2171 "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
2172 addr.ss_family == AF_INET ? 4 : 6, host,
2175 port, payload_type);
2176 av_log(s, AV_LOG_VERBOSE,
"SDP:\n%s\n", sdp);
2186 ret = sdp_read_header(s);
2197 static const AVClass rtp_demuxer_class = {
2212 .
flags = AVFMT_NOFILE,
2213 .priv_class = &rtp_demuxer_class,