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.

454 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 "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. if (include_sources && include_sources[0])
  155. url_add_option(buf, buf_size, "sources=%s", include_sources);
  156. if (exclude_sources && exclude_sources[0])
  157. url_add_option(buf, buf_size, "block=%s", exclude_sources);
  158. }
  159. static void rtp_parse_addr_list(URLContext *h, char *buf,
  160. struct sockaddr_storage ***address_list_ptr,
  161. int *address_list_size_ptr)
  162. {
  163. struct addrinfo *ai = NULL;
  164. struct sockaddr_storage *source_addr;
  165. char tmp = '\0', *p = buf, *next;
  166. /* Resolve all of the IPs */
  167. while (p && p[0]) {
  168. next = strchr(p, ',');
  169. if (next) {
  170. tmp = *next;
  171. *next = '\0';
  172. }
  173. ai = rtp_resolve_host(p, 0, SOCK_DGRAM, AF_UNSPEC, 0);
  174. if (ai) {
  175. source_addr = av_mallocz(sizeof(struct sockaddr_storage));
  176. if (!source_addr)
  177. break;
  178. memcpy(source_addr, ai->ai_addr, ai->ai_addrlen);
  179. freeaddrinfo(ai);
  180. dynarray_add(address_list_ptr, address_list_size_ptr, source_addr);
  181. } else {
  182. av_log(h, AV_LOG_WARNING, "Unable to resolve %s\n", p);
  183. }
  184. if (next) {
  185. *next = tmp;
  186. p = next + 1;
  187. } else {
  188. p = NULL;
  189. }
  190. }
  191. }
  192. /**
  193. * url syntax: rtp://host:port[?option=val...]
  194. * option: 'ttl=n' : set the ttl value (for multicast only)
  195. * 'rtcpport=n' : set the remote rtcp port to n
  196. * 'localrtpport=n' : set the local rtp port to n
  197. * 'localrtcpport=n' : set the local rtcp port to n
  198. * 'pkt_size=n' : set max packet size
  199. * 'connect=0/1' : do a connect() on the UDP socket
  200. * deprecated option:
  201. * 'localport=n' : set the local port to n
  202. * 'sources=ip[,ip]' : list allowed source IP addresses
  203. *
  204. * if rtcpport isn't set the rtcp port will be the rtp port + 1
  205. * if local rtp port isn't set any available port will be used for the local
  206. * rtp and rtcp ports
  207. * if the local rtcp port is not set it will be the local rtp port + 1
  208. */
  209. static int rtp_open(URLContext *h, const char *uri, int flags)
  210. {
  211. RTPContext *s = h->priv_data;
  212. int rtp_port, rtcp_port,
  213. ttl, connect,
  214. local_rtp_port, local_rtcp_port, max_packet_size;
  215. char hostname[256], include_sources[1024] = "", exclude_sources[1024] = "";
  216. char buf[1024];
  217. char path[1024];
  218. const char *p;
  219. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &rtp_port,
  220. path, sizeof(path), uri);
  221. /* extract parameters */
  222. ttl = -1;
  223. rtcp_port = rtp_port+1;
  224. local_rtp_port = -1;
  225. local_rtcp_port = -1;
  226. max_packet_size = -1;
  227. connect = 0;
  228. p = strchr(uri, '?');
  229. if (p) {
  230. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  231. ttl = strtol(buf, NULL, 10);
  232. }
  233. if (av_find_info_tag(buf, sizeof(buf), "rtcpport", p)) {
  234. rtcp_port = strtol(buf, NULL, 10);
  235. }
  236. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  237. local_rtp_port = strtol(buf, NULL, 10);
  238. }
  239. if (av_find_info_tag(buf, sizeof(buf), "localrtpport", p)) {
  240. local_rtp_port = strtol(buf, NULL, 10);
  241. }
  242. if (av_find_info_tag(buf, sizeof(buf), "localrtcpport", p)) {
  243. local_rtcp_port = strtol(buf, NULL, 10);
  244. }
  245. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  246. max_packet_size = strtol(buf, NULL, 10);
  247. }
  248. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  249. connect = strtol(buf, NULL, 10);
  250. }
  251. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  252. av_strlcpy(include_sources, buf, sizeof(include_sources));
  253. rtp_parse_addr_list(h, buf, &s->ssm_include_addrs, &s->nb_ssm_include_addrs);
  254. }
  255. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  256. av_strlcpy(exclude_sources, buf, sizeof(exclude_sources));
  257. rtp_parse_addr_list(h, buf, &s->ssm_exclude_addrs, &s->nb_ssm_exclude_addrs);
  258. }
  259. }
  260. build_udp_url(buf, sizeof(buf),
  261. hostname, rtp_port, local_rtp_port, ttl, max_packet_size,
  262. connect, include_sources, exclude_sources);
  263. if (ffurl_open(&s->rtp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  264. goto fail;
  265. if (local_rtp_port>=0 && local_rtcp_port<0)
  266. local_rtcp_port = ff_udp_get_local_port(s->rtp_hd) + 1;
  267. build_udp_url(buf, sizeof(buf),
  268. hostname, rtcp_port, local_rtcp_port, ttl, max_packet_size,
  269. connect, include_sources, exclude_sources);
  270. if (ffurl_open(&s->rtcp_hd, buf, flags, &h->interrupt_callback, NULL) < 0)
  271. goto fail;
  272. /* just to ease handle access. XXX: need to suppress direct handle
  273. access */
  274. s->rtp_fd = ffurl_get_file_handle(s->rtp_hd);
  275. s->rtcp_fd = ffurl_get_file_handle(s->rtcp_hd);
  276. h->max_packet_size = s->rtp_hd->max_packet_size;
  277. h->is_streamed = 1;
  278. return 0;
  279. fail:
  280. if (s->rtp_hd)
  281. ffurl_close(s->rtp_hd);
  282. if (s->rtcp_hd)
  283. ffurl_close(s->rtcp_hd);
  284. return AVERROR(EIO);
  285. }
  286. static int rtp_read(URLContext *h, uint8_t *buf, int size)
  287. {
  288. RTPContext *s = h->priv_data;
  289. struct sockaddr_storage from;
  290. socklen_t from_len;
  291. int len, n, i;
  292. struct pollfd p[2] = {{s->rtp_fd, POLLIN, 0}, {s->rtcp_fd, POLLIN, 0}};
  293. int poll_delay = h->flags & AVIO_FLAG_NONBLOCK ? 0 : 100;
  294. for(;;) {
  295. if (ff_check_interrupt(&h->interrupt_callback))
  296. return AVERROR_EXIT;
  297. n = poll(p, 2, poll_delay);
  298. if (n > 0) {
  299. /* first try RTCP, then RTP */
  300. for (i = 1; i >= 0; i--) {
  301. if (!(p[i].revents & POLLIN))
  302. continue;
  303. from_len = sizeof(from);
  304. len = recvfrom(p[i].fd, buf, size, 0,
  305. (struct sockaddr *)&from, &from_len);
  306. if (len < 0) {
  307. if (ff_neterrno() == AVERROR(EAGAIN) ||
  308. ff_neterrno() == AVERROR(EINTR))
  309. continue;
  310. return AVERROR(EIO);
  311. }
  312. if (rtp_check_source_lists(s, &from))
  313. continue;
  314. return len;
  315. }
  316. } else if (n < 0) {
  317. if (ff_neterrno() == AVERROR(EINTR))
  318. continue;
  319. return AVERROR(EIO);
  320. }
  321. if (h->flags & AVIO_FLAG_NONBLOCK)
  322. return AVERROR(EAGAIN);
  323. }
  324. return len;
  325. }
  326. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  327. {
  328. RTPContext *s = h->priv_data;
  329. int ret;
  330. URLContext *hd;
  331. if (RTP_PT_IS_RTCP(buf[1])) {
  332. /* RTCP payload type */
  333. hd = s->rtcp_hd;
  334. } else {
  335. /* RTP payload type */
  336. hd = s->rtp_hd;
  337. }
  338. ret = ffurl_write(hd, buf, size);
  339. return ret;
  340. }
  341. static int rtp_close(URLContext *h)
  342. {
  343. RTPContext *s = h->priv_data;
  344. int i;
  345. for (i = 0; i < s->nb_ssm_include_addrs; i++)
  346. av_free(s->ssm_include_addrs[i]);
  347. av_freep(&s->ssm_include_addrs);
  348. for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
  349. av_free(s->ssm_exclude_addrs[i]);
  350. av_freep(&s->ssm_exclude_addrs);
  351. ffurl_close(s->rtp_hd);
  352. ffurl_close(s->rtcp_hd);
  353. return 0;
  354. }
  355. /**
  356. * Return the local rtp port used by the RTP connection
  357. * @param h media file context
  358. * @return the local port number
  359. */
  360. int ff_rtp_get_local_rtp_port(URLContext *h)
  361. {
  362. RTPContext *s = h->priv_data;
  363. return ff_udp_get_local_port(s->rtp_hd);
  364. }
  365. /**
  366. * Return the local rtcp port used by the RTP connection
  367. * @param h media file context
  368. * @return the local port number
  369. */
  370. int ff_rtp_get_local_rtcp_port(URLContext *h)
  371. {
  372. RTPContext *s = h->priv_data;
  373. return ff_udp_get_local_port(s->rtcp_hd);
  374. }
  375. static int rtp_get_file_handle(URLContext *h)
  376. {
  377. RTPContext *s = h->priv_data;
  378. return s->rtp_fd;
  379. }
  380. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  381. int *numhandles)
  382. {
  383. RTPContext *s = h->priv_data;
  384. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  385. if (!hs)
  386. return AVERROR(ENOMEM);
  387. hs[0] = s->rtp_fd;
  388. hs[1] = s->rtcp_fd;
  389. *numhandles = 2;
  390. return 0;
  391. }
  392. URLProtocol ff_rtp_protocol = {
  393. .name = "rtp",
  394. .url_open = rtp_open,
  395. .url_read = rtp_read,
  396. .url_write = rtp_write,
  397. .url_close = rtp_close,
  398. .url_get_file_handle = rtp_get_file_handle,
  399. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  400. .priv_data_size = sizeof(RTPContext),
  401. .flags = URL_PROTOCOL_FLAG_NETWORK,
  402. };