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 "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. url_add_option(buf, buf_size, "fifo_size=0");
  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;
  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 */
  300. if (p[1].revents & POLLIN) {
  301. from_len = sizeof(from);
  302. len = recvfrom (s->rtcp_fd, buf, size, 0,
  303. (struct sockaddr *)&from, &from_len);
  304. if (len < 0) {
  305. if (ff_neterrno() == AVERROR(EAGAIN) ||
  306. ff_neterrno() == AVERROR(EINTR))
  307. continue;
  308. return AVERROR(EIO);
  309. }
  310. if (rtp_check_source_lists(s, &from))
  311. continue;
  312. break;
  313. }
  314. /* then RTP */
  315. if (p[0].revents & POLLIN) {
  316. from_len = sizeof(from);
  317. len = recvfrom (s->rtp_fd, buf, size, 0,
  318. (struct sockaddr *)&from, &from_len);
  319. if (len < 0) {
  320. if (ff_neterrno() == AVERROR(EAGAIN) ||
  321. ff_neterrno() == AVERROR(EINTR))
  322. continue;
  323. return AVERROR(EIO);
  324. }
  325. if (rtp_check_source_lists(s, &from))
  326. continue;
  327. break;
  328. }
  329. } else if (n < 0) {
  330. if (ff_neterrno() == AVERROR(EINTR))
  331. continue;
  332. return AVERROR(EIO);
  333. }
  334. if (h->flags & AVIO_FLAG_NONBLOCK)
  335. return AVERROR(EAGAIN);
  336. }
  337. return len;
  338. }
  339. static int rtp_write(URLContext *h, const uint8_t *buf, int size)
  340. {
  341. RTPContext *s = h->priv_data;
  342. int ret;
  343. URLContext *hd;
  344. if (RTP_PT_IS_RTCP(buf[1])) {
  345. /* RTCP payload type */
  346. hd = s->rtcp_hd;
  347. } else {
  348. /* RTP payload type */
  349. hd = s->rtp_hd;
  350. }
  351. ret = ffurl_write(hd, buf, size);
  352. return ret;
  353. }
  354. static int rtp_close(URLContext *h)
  355. {
  356. RTPContext *s = h->priv_data;
  357. int i;
  358. for (i = 0; i < s->nb_ssm_include_addrs; i++)
  359. av_free(s->ssm_include_addrs[i]);
  360. av_freep(&s->ssm_include_addrs);
  361. for (i = 0; i < s->nb_ssm_exclude_addrs; i++)
  362. av_free(s->ssm_exclude_addrs[i]);
  363. av_freep(&s->ssm_exclude_addrs);
  364. ffurl_close(s->rtp_hd);
  365. ffurl_close(s->rtcp_hd);
  366. return 0;
  367. }
  368. /**
  369. * Return the local rtp port used by the RTP connection
  370. * @param h media file context
  371. * @return the local port number
  372. */
  373. int ff_rtp_get_local_rtp_port(URLContext *h)
  374. {
  375. RTPContext *s = h->priv_data;
  376. return ff_udp_get_local_port(s->rtp_hd);
  377. }
  378. /**
  379. * Return the local rtcp port used by the RTP connection
  380. * @param h media file context
  381. * @return the local port number
  382. */
  383. int ff_rtp_get_local_rtcp_port(URLContext *h)
  384. {
  385. RTPContext *s = h->priv_data;
  386. return ff_udp_get_local_port(s->rtcp_hd);
  387. }
  388. static int rtp_get_file_handle(URLContext *h)
  389. {
  390. RTPContext *s = h->priv_data;
  391. return s->rtp_fd;
  392. }
  393. static int rtp_get_multi_file_handle(URLContext *h, int **handles,
  394. int *numhandles)
  395. {
  396. RTPContext *s = h->priv_data;
  397. int *hs = *handles = av_malloc(sizeof(**handles) * 2);
  398. if (!hs)
  399. return AVERROR(ENOMEM);
  400. hs[0] = s->rtp_fd;
  401. hs[1] = s->rtcp_fd;
  402. *numhandles = 2;
  403. return 0;
  404. }
  405. URLProtocol ff_rtp_protocol = {
  406. .name = "rtp",
  407. .url_open = rtp_open,
  408. .url_read = rtp_read,
  409. .url_write = rtp_write,
  410. .url_close = rtp_close,
  411. .url_get_file_handle = rtp_get_file_handle,
  412. .url_get_multi_file_handle = rtp_get_multi_file_handle,
  413. .priv_data_size = sizeof(RTPContext),
  414. .flags = URL_PROTOCOL_FLAG_NETWORK,
  415. };