You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
20KB

  1. /*
  2. * RTP network protocol
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RTP protocol
  24. */
  25. #include "libavutil/parseutils.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/opt.h"
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "rtp.h"
  31. #include "rtpproto.h"
  32. #include "url.h"
  33. #include "ip.h"
  34. #include <stdarg.h>
  35. #include "internal.h"
  36. #include "network.h"
  37. #include "os_support.h"
  38. #include <fcntl.h>
  39. #if HAVE_POLL_H
  40. #include <poll.h>
  41. #endif
  42. typedef struct RTPContext {
  43. const AVClass *class;
  44. URLContext *rtp_hd, *rtcp_hd, *fec_hd;
  45. int rtp_fd, rtcp_fd;
  46. IPSourceFilters filters;
  47. int write_to_source;
  48. struct sockaddr_storage last_rtp_source, last_rtcp_source;
  49. socklen_t last_rtp_source_len, last_rtcp_source_len;
  50. int ttl;
  51. int buffer_size;
  52. int rtcp_port, local_rtpport, local_rtcpport;
  53. int connect;
  54. int pkt_size;
  55. int dscp;
  56. char *sources;
  57. char *block;
  58. char *fec_options_str;
  59. } RTPContext;
  60. #define OFFSET(x) offsetof(RTPContext, x)
  61. #define D AV_OPT_FLAG_DECODING_PARAM
  62. #define E AV_OPT_FLAG_ENCODING_PARAM
  63. static const AVOption options[] = {
  64. { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  65. { "buffer_size", "Send/Receive buffer size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  66. { "rtcp_port", "Custom rtcp port", OFFSET(rtcp_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  67. { "local_rtpport", "Local rtp port", OFFSET(local_rtpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  68. { "local_rtcpport", "Local rtcp port", OFFSET(local_rtcpport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  69. { "connect", "Connect socket", OFFSET(connect), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
  70. { "write_to_source", "Send packets to the source address of the latest received packet", OFFSET(write_to_source), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
  71. { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  72. { "dscp", "DSCP class", OFFSET(dscp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  73. { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  74. { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  75. { "fec", "FEC", OFFSET(fec_options_str), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = E },
  76. { NULL }
  77. };
  78. static const AVClass rtp_class = {
  79. .class_name = "rtp",
  80. .item_name = av_default_item_name,
  81. .option = options,
  82. .version = LIBAVUTIL_VERSION_INT,
  83. };
  84. /**
  85. * If no filename is given to av_open_input_file because you want to
  86. * get the local port first, then you must call this function to set
  87. * the remote server address.
  88. *
  89. * @param h media file context
  90. * @param uri of the remote server
  91. * @return zero if no error.
  92. */
  93. int ff_rtp_set_remote_url(URLContext *h, const char *uri)
  94. {
  95. RTPContext *s = h->priv_data;
  96. char hostname[256];
  97. int port, rtcp_port;
  98. const char *p;
  99. char buf[1024];
  100. char path[1024];
  101. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port,
  102. path, sizeof(path), uri);
  103. rtcp_port = port + 1;
  104. p = strchr(uri, '?');
  105. if (p) {
  106. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  107. rtcp_port = strtol(buf, NULL, 10);
  108. }
  109. }
  110. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, port, "%s", path);
  111. ff_udp_set_remote_url(s->rtp_hd, buf);
  112. ff_url_join(buf, sizeof(buf), "udp", NULL, hostname, rtcp_port, "%s", path);
  113. ff_udp_set_remote_url(s->rtcp_hd, buf);
  114. return 0;
  115. }
  116. static int get_port(const struct sockaddr_storage *ss)
  117. {
  118. if (ss->ss_family == AF_INET)
  119. return ntohs(((const struct sockaddr_in *)ss)->sin_port);
  120. #if HAVE_STRUCT_SOCKADDR_IN6
  121. if (ss->ss_family == AF_INET6)
  122. return ntohs(((const struct sockaddr_in6 *)ss)->sin6_port);
  123. #endif
  124. return 0;
  125. }
  126. static void set_port(struct sockaddr_storage *ss, int port)
  127. {
  128. if (ss->ss_family == AF_INET)
  129. ((struct sockaddr_in *)ss)->sin_port = htons(port);
  130. #if HAVE_STRUCT_SOCKADDR_IN6
  131. else if (ss->ss_family == AF_INET6)
  132. ((struct sockaddr_in6 *)ss)->sin6_port = htons(port);
  133. #endif
  134. }
  135. /**
  136. * add option to url of the form:
  137. * "http://host:port/path?option1=val1&option2=val2...
  138. */
  139. static av_printf_format(3, 4) void url_add_option(char *buf, int buf_size, const char *fmt, ...)
  140. {
  141. char buf1[1024];
  142. va_list ap;
  143. va_start(ap, fmt);
  144. if (strchr(buf, '?'))
  145. av_strlcat(buf, "&", buf_size);
  146. else
  147. av_strlcat(buf, "?", buf_size);
  148. vsnprintf(buf1, sizeof(buf1), fmt, ap);
  149. av_strlcat(buf, buf1, buf_size);
  150. va_end(ap);
  151. }
  152. static void build_udp_url(RTPContext *s,
  153. char *buf, int buf_size,
  154. const char *hostname,
  155. int port, int local_port,
  156. const char *include_sources,
  157. const char *exclude_sources)
  158. {
  159. ff_url_join(buf, buf_size, "udp", NULL, hostname, port, NULL);
  160. if (local_port >= 0)
  161. url_add_option(buf, buf_size, "localport=%d", local_port);
  162. if (s->ttl >= 0)
  163. url_add_option(buf, buf_size, "ttl=%d", s->ttl);
  164. if (s->buffer_size >= 0)
  165. url_add_option(buf, buf_size, "buffer_size=%d", s->buffer_size);
  166. if (s->pkt_size >= 0)
  167. url_add_option(buf, buf_size, "pkt_size=%d", s->pkt_size);
  168. if (s->connect)
  169. url_add_option(buf, buf_size, "connect=1");
  170. if (s->dscp >= 0)
  171. url_add_option(buf, buf_size, "dscp=%d", s->dscp);
  172. url_add_option(buf, buf_size, "fifo_size=0");
  173. if (include_sources && include_sources[0])
  174. url_add_option(buf, buf_size, "sources=%s", include_sources);
  175. if (exclude_sources && exclude_sources[0])
  176. url_add_option(buf, buf_size, "block=%s", exclude_sources);
  177. }
  178. /**
  179. * url syntax: rtp://host:port[?option=val...]
  180. * option: 'ttl=n' : set the ttl value (for multicast only)
  181. * 'rtcpport=n' : set the remote rtcp port to n
  182. * 'localrtpport=n' : set the local rtp port to n
  183. * 'localrtcpport=n' : set the local rtcp port to n
  184. * 'pkt_size=n' : set max packet size
  185. * 'connect=0/1' : do a connect() on the UDP socket
  186. * 'sources=ip[,ip]' : list allowed source IP addresses
  187. * 'block=ip[,ip]' : list disallowed source IP addresses
  188. * 'write_to_source=0/1' : send packets to the source address of the latest received packet
  189. * 'dscp=n' : set DSCP value to n (QoS)
  190. * deprecated option:
  191. * 'localport=n' : set the local port to n
  192. *
  193. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  194. * if local rtp port isn't set any available port will be used for the local
  195. * rtp and rtcp ports
  196. * if the local rtcp port is not set it will be the local rtp port + 1
  197. */
  198. static int rtp_open(URLContext *h, const char *uri, int flags)
  199. {
  200. RTPContext *s = h->priv_data;
  201. AVDictionary *fec_opts = NULL;
  202. int rtp_port;
  203. char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
  204. char *sources = include_sources, *block = exclude_sources;
  205. char *fec_protocol = NULL;
  206. char buf[1024];
  207. char path[1024];
  208. const char *p;
  209. int i, max_retry_count = 3;
  210. int rtcpflags;
  211. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  212. path, sizeof(path), uri);
  213. /* extract parameters */
  214. if (s->rtcp_port < 0)
  215. s->rtcp_port = rtp_port + 1;
  216. p = strchr(uri, '?');
  217. if (p) {
  218. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  219. s->ttl = strtol(buf, NULL, 10);
  220. }
  221. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  222. s->rtcp_port = strtol(buf, NULL, 10);
  223. }
  224. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  225. s->local_rtpport = strtol(buf, NULL, 10);
  226. }
  227. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  228. s->local_rtpport = strtol(buf, NULL, 10);
  229. }
  230. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  231. s->local_rtcpport = strtol(buf, NULL, 10);
  232. }
  233. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  234. s->pkt_size = strtol(buf, NULL, 10);
  235. }
  236. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  237. s->connect = strtol(buf, NULL, 10);
  238. }
  239. if (av_find_info_tag(buf, sizeof(buf), "write_to_source", p)) {
  240. s->write_to_source = strtol(buf, NULL, 10);
  241. }
  242. if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
  243. s->dscp = strtol(buf, NULL, 10);
  244. }
  245. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  246. av_strlcpy(include_sources, buf, sizeof(include_sources));
  247. ff_ip_parse_sources(h, buf, &s->filters);
  248. } else {
  249. ff_ip_parse_sources(h, s->sources, &s->filters);
  250. sources = s->sources;
  251. }
  252. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  253. av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
  254. ff_ip_parse_blocks(h, buf, &s->filters);
  255. } else {
  256. ff_ip_parse_blocks(h, s->block, &s->filters);
  257. block = s->block;
  258. }
  259. }
  260. if (s->fec_options_str) {
  261. p = s->fec_options_str;
  262. if (!(fec_protocol = av_get_token(&p, "="))) {
  263. av_log(h, AV_LOG_ERROR, "Failed to parse the FEC protocol value\n");
  264. goto fail;
  265. }
  266. if (strcmp(fec_protocol, "prompeg")) {
  267. av_log(h, AV_LOG_ERROR, "Unsupported FEC protocol %s\n", fec_protocol);
  268. goto fail;
  269. }
  270. p = s->fec_options_str + strlen(fec_protocol);
  271. while (*p && *p == '=') p++;
  272. if (av_dict_parse_string(&fec_opts, p, "=", ":", 0) < 0) {
  273. av_log(h, AV_LOG_ERROR, "Failed to parse the FEC options\n");
  274. goto fail;
  275. }
  276. if (s->ttl > 0) {
  277. snprintf(buf, sizeof (buf), "%d", s->ttl);
  278. av_dict_set(&fec_opts, "ttl", buf, 0);
  279. }
  280. }
  281. for (i = 0; i < max_retry_count; i++) {
  282. build_udp_url(s, buf, sizeof(buf),
  283. hostname, rtp_port, s->local_rtpport,
  284. sources, block);
  285. if (ffurl_open_whitelist(&s->rtp_hd, buf, flags, &h->interrupt_callback,
  286. NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
  287. goto fail;
  288. s->local_rtpport = ff_udp_get_local_port(s->rtp_hd);
  289. if(s->local_rtpport == 65535) {
  290. s->local_rtpport = -1;
  291. continue;
  292. }
  293. rtcpflags = flags | AVIO_FLAG_WRITE;
  294. if (s->local_rtcpport < 0) {
  295. s->local_rtcpport = s->local_rtpport + 1;
  296. build_udp_url(s, buf, sizeof(buf),
  297. hostname, s->rtcp_port, s->local_rtcpport,
  298. sources, block);
  299. if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags,
  300. &h->interrupt_callback, NULL,
  301. h->protocol_whitelist, h->protocol_blacklist, h) < 0) {
  302. s->local_rtpport = s->local_rtcpport = -1;
  303. continue;
  304. }
  305. break;
  306. }
  307. build_udp_url(s, buf, sizeof(buf),
  308. hostname, s->rtcp_port, s->local_rtcpport,
  309. sources, block);
  310. if (ffurl_open_whitelist(&s->rtcp_hd, buf, rtcpflags, &h->interrupt_callback,
  311. NULL, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
  312. goto fail;
  313. break;
  314. }
  315. s->fec_hd = NULL;
  316. if (fec_protocol) {
  317. ff_url_join(buf, sizeof(buf), fec_protocol, NULL, hostname, rtp_port, NULL);
  318. if (ffurl_open_whitelist(&s->fec_hd, buf, flags, &h->interrupt_callback,
  319. &fec_opts, h->protocol_whitelist, h->protocol_blacklist, h) < 0)
  320. goto fail;
  321. }
  322. /* just to ease handle access. XXX: need to suppress direct handle
  323. access */
  324. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  325. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  326. h->max_packet_size = s->rtp_hd->max_packet_size;
  327. h->is_streamed = 1;
  328. av_free(fec_protocol);
  329. av_dict_free(&fec_opts);
  330. return 0;
  331. fail:
  332. if (s->rtp_hd)
  333. ffurl_close(s->rtp_hd);
  334. if (s->rtcp_hd)
  335. ffurl_close(s->rtcp_hd);
  336. ffurl_closep(&s->fec_hd);
  337. av_free(fec_protocol);
  338. av_dict_free(&fec_opts);
  339. return AVERROR(EIO);
  340. }
  341. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  342. {
  343. RTPContext *s = h->priv_data;
  344. int len, n, i;
  345. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  346. int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
  347. struct sockaddr_storage *addrs[2] = { &s->last_rtp_source, &s->last_rtcp_source };
  348. socklen_t *addr_lens[2] = { &s->last_rtp_source_len, &s->last_rtcp_source_len };
  349. for(;;) {
  350. if (ff_check_interrupt(&h->interrupt_callback))
  351. return AVERROR_EXIT;
  352. n = poll(p, 2, poll_delay);
  353. if (n > 0) {
  354. /* first try RTCP, then RTP */
  355. for (i = 1; i >= 0; i--) {
  356. if (!(p[i].revents & POLLIN))
  357. continue;
  358. *addr_lens[i] = sizeof(*addrs[i]);
  359. len = recvfrom(p[i].fd, buf, size, 0,
  360. (struct sockaddr *)addrs[i], addr_lens[i]);
  361. if (len < 0) {
  362. if (ff_neterrno() == AVERROR(EAGAIN) ||
  363. ff_neterrno() == AVERROR(EINTR))
  364. continue;
  365. return AVERROR(EIO);
  366. }
  367. if (ff_ip_check_source_lists(addrs[i], &s->filters))
  368. continue;
  369. return len;
  370. }
  371. } else if (n < 0) {
  372. if (ff_neterrno() == AVERROR(EINTR))
  373. continue;
  374. return AVERROR(EIO);
  375. }
  376. if (h->flags & AVIO_FLAG_NONBLOCK)
  377. return AVERROR(EAGAIN);
  378. }
  379. }
  380. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  381. {
  382. RTPContext *s = h->priv_data;
  383. int ret, ret_fec;
  384. URLContext *hd;
  385. if (size < 2)
  386. return AVERROR(EINVAL);
  387. if ((buf[0] & 0xc0) != (RTP_VERSION << 6))
  388. av_log(h, AV_LOG_WARNING, "Data doesn't look like RTP packets, "
  389. "make sure the RTP muxer is used\n");
  390. if (s->write_to_source) {
  391. int fd;
  392. struct sockaddr_storage *source, temp_source;
  393. socklen_t *source_len, temp_len;
  394. if (!s->last_rtp_source.ss_family && !s->last_rtcp_source.ss_family) {
  395. av_log(h, AV_LOG_ERROR,
  396. "Unable to send packet to source, no packets received yet\n");
  397. // Intentionally not returning an error here
  398. return size;
  399. }
  400. if (RTP_PT_IS_RTCP(buf[1])) {
  401. fd = s->rtcp_fd;
  402. source = &s->last_rtcp_source;
  403. source_len = &s->last_rtcp_source_len;
  404. } else {
  405. fd = s->rtp_fd;
  406. source = &s->last_rtp_source;
  407. source_len = &s->last_rtp_source_len;
  408. }
  409. if (!source->ss_family) {
  410. source = &temp_source;
  411. source_len = &temp_len;
  412. if (RTP_PT_IS_RTCP(buf[1])) {
  413. temp_source = s->last_rtp_source;
  414. temp_len = s->last_rtp_source_len;
  415. set_port(source, get_port(source) + 1);
  416. av_log(h, AV_LOG_INFO,
  417. "Not received any RTCP packets yet, inferring peer port "
  418. "from the RTP port\n");
  419. } else {
  420. temp_source = s->last_rtcp_source;
  421. temp_len = s->last_rtcp_source_len;
  422. set_port(source, get_port(source) - 1);
  423. av_log(h, AV_LOG_INFO,
  424. "Not received any RTP packets yet, inferring peer port "
  425. "from the RTCP port\n");
  426. }
  427. }
  428. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  429. ret = ff_network_wait_fd(fd, 1);
  430. if (ret < 0)
  431. return ret;
  432. }
  433. ret = sendto(fd, buf, size, 0, (struct sockaddr *) source,
  434. *source_len);
  435. return ret < 0 ? ff_neterrno() : ret;
  436. }
  437. if (RTP_PT_IS_RTCP(buf[1])) {
  438. /* RTCP payload type */
  439. hd = s->rtcp_hd;
  440. } else {
  441. /* RTP payload type */
  442. hd = s->rtp_hd;
  443. }
  444. if ((ret = ffurl_write(hd, buf, size)) < 0) {
  445. return ret;
  446. }
  447. if (s->fec_hd && !RTP_PT_IS_RTCP(buf[1])) {
  448. if ((ret_fec = ffurl_write(s->fec_hd, buf, size)) < 0) {
  449. av_log(h, AV_LOG_ERROR, "Failed to send FEC\n");
  450. return ret_fec;
  451. }
  452. }
  453. return ret;
  454. }
  455. static int rtp_close(URLContext *h)
  456. {
  457. RTPContext *s = h->priv_data;
  458. ff_ip_reset_filters(&s->filters);
  459. ffurl_close(s->rtp_hd);
  460. ffurl_close(s->rtcp_hd);
  461. ffurl_closep(&s->fec_hd);
  462. return 0;
  463. }
  464. /**
  465. * Return the local rtp port used by the RTP connection
  466. * @param h media file context
  467. * @return the local port number
  468. */
  469. int ff_rtp_get_local_rtp_port(URLContext *h)
  470. {
  471. RTPContext *s = h->priv_data;
  472. return ff_udp_get_local_port(s->rtp_hd);
  473. }
  474. /**
  475. * Return the local rtcp port used by the RTP connection
  476. * @param h media file context
  477. * @return the local port number
  478. */
  479. static int rtp_get_file_handle(URLContext *h)
  480. {
  481. RTPContext *s = h->priv_data;
  482. return s->rtp_fd;
  483. }
  484. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  485. int *numhandles)
  486. {
  487. RTPContext *s = h->priv_data;
  488. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  489. if (!hs)
  490. return AVERROR(ENOMEM);
  491. hs[0] = s->rtp_fd;
  492. hs[1] = s->rtcp_fd;
  493. *numhandles = 2;
  494. return 0;
  495. }
  496. const URLProtocol ff_rtp_protocol = {
  497. .name = "rtp",
  498. .url_open = rtp_open,
  499. .url_read = rtp_read,
  500. .url_write = rtp_write,
  501. .url_close = rtp_close,
  502. .url_get_file_handle = rtp_get_file_handle,
  503. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  504. .priv_data_size = sizeof(RTPContext),
  505. .flags = URL_PROTOCOL_FLAG_NETWORK,
  506. .priv_data_class = &rtp_class,
  507. };