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.

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