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.

566 lines
18KB

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