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.

458 lines
14KB

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