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.

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