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.

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