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.

453 lines
14KB

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