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.

589 lines
20KB

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