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.

481 lines
14KB

  1. /*
  2. * UDP prototype streaming system
  3. * Copyright (c) 2000, 2001, 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. * UDP protocol
  24. */
  25. #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
  26. #define _DARWIN_C_SOURCE /* Needed for using IP_MULTICAST_TTL on OS X */
  27. #include "avformat.h"
  28. #include <unistd.h>
  29. #include "internal.h"
  30. #include "network.h"
  31. #include "os_support.h"
  32. #if HAVE_SYS_SELECT_H
  33. #include <sys/select.h>
  34. #endif
  35. #include <sys/time.h>
  36. #ifndef IPV6_ADD_MEMBERSHIP
  37. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  38. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  39. #endif
  40. typedef struct {
  41. int udp_fd;
  42. int ttl;
  43. int buffer_size;
  44. int is_multicast;
  45. int local_port;
  46. int reuse_socket;
  47. struct sockaddr_storage dest_addr;
  48. int dest_addr_len;
  49. } UDPContext;
  50. #define UDP_TX_BUF_SIZE 32768
  51. #define UDP_MAX_PKT_SIZE 65536
  52. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  53. struct sockaddr *addr)
  54. {
  55. #ifdef IP_MULTICAST_TTL
  56. if (addr->sa_family == AF_INET) {
  57. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  58. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
  59. return -1;
  60. }
  61. }
  62. #endif
  63. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  64. if (addr->sa_family == AF_INET6) {
  65. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  66. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
  67. return -1;
  68. }
  69. }
  70. #endif
  71. return 0;
  72. }
  73. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
  74. {
  75. #ifdef IP_ADD_MEMBERSHIP
  76. if (addr->sa_family == AF_INET) {
  77. struct ip_mreq mreq;
  78. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  79. mreq.imr_interface.s_addr= INADDR_ANY;
  80. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  81. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
  82. return -1;
  83. }
  84. }
  85. #endif
  86. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  87. if (addr->sa_family == AF_INET6) {
  88. struct ipv6_mreq mreq6;
  89. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  90. mreq6.ipv6mr_interface= 0;
  91. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  92. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
  93. return -1;
  94. }
  95. }
  96. #endif
  97. return 0;
  98. }
  99. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
  100. {
  101. #ifdef IP_DROP_MEMBERSHIP
  102. if (addr->sa_family == AF_INET) {
  103. struct ip_mreq mreq;
  104. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  105. mreq.imr_interface.s_addr= INADDR_ANY;
  106. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  107. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
  108. return -1;
  109. }
  110. }
  111. #endif
  112. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  113. if (addr->sa_family == AF_INET6) {
  114. struct ipv6_mreq mreq6;
  115. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  116. mreq6.ipv6mr_interface= 0;
  117. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  118. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
  119. return -1;
  120. }
  121. }
  122. #endif
  123. return 0;
  124. }
  125. static struct addrinfo* udp_resolve_host(const char *hostname, int port,
  126. int type, int family, int flags)
  127. {
  128. struct addrinfo hints, *res = 0;
  129. int error;
  130. char sport[16];
  131. const char *node = 0, *service = "0";
  132. if (port > 0) {
  133. snprintf(sport, sizeof(sport), "%d", port);
  134. service = sport;
  135. }
  136. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  137. node = hostname;
  138. }
  139. memset(&hints, 0, sizeof(hints));
  140. hints.ai_socktype = type;
  141. hints.ai_family = family;
  142. hints.ai_flags = flags;
  143. if ((error = getaddrinfo(node, service, &hints, &res))) {
  144. res = NULL;
  145. av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
  146. }
  147. return res;
  148. }
  149. static int udp_set_url(struct sockaddr_storage *addr,
  150. const char *hostname, int port)
  151. {
  152. struct addrinfo *res0;
  153. int addr_len;
  154. res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  155. if (res0 == 0) return AVERROR(EIO);
  156. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  157. addr_len = res0->ai_addrlen;
  158. freeaddrinfo(res0);
  159. return addr_len;
  160. }
  161. static int udp_socket_create(UDPContext *s,
  162. struct sockaddr_storage *addr, int *addr_len)
  163. {
  164. int udp_fd = -1;
  165. struct addrinfo *res0 = NULL, *res = NULL;
  166. int family = AF_UNSPEC;
  167. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  168. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  169. res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
  170. if (res0 == 0)
  171. goto fail;
  172. for (res = res0; res; res=res->ai_next) {
  173. udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
  174. if (udp_fd > 0) break;
  175. av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
  176. }
  177. if (udp_fd < 0)
  178. goto fail;
  179. memcpy(addr, res->ai_addr, res->ai_addrlen);
  180. *addr_len = res->ai_addrlen;
  181. freeaddrinfo(res0);
  182. return udp_fd;
  183. fail:
  184. if (udp_fd >= 0)
  185. closesocket(udp_fd);
  186. if(res0)
  187. freeaddrinfo(res0);
  188. return -1;
  189. }
  190. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  191. {
  192. char sbuf[sizeof(int)*3+1];
  193. if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
  194. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
  195. return -1;
  196. }
  197. return strtol(sbuf, NULL, 10);
  198. }
  199. /**
  200. * If no filename is given to av_open_input_file because you want to
  201. * get the local port first, then you must call this function to set
  202. * the remote server address.
  203. *
  204. * url syntax: udp://host:port[?option=val...]
  205. * option: 'ttl=n' : set the ttl value (for multicast only)
  206. * 'localport=n' : set the local port
  207. * 'pkt_size=n' : set max packet size
  208. * 'reuse=1' : enable reusing the socket
  209. *
  210. * @param h media file context
  211. * @param uri of the remote server
  212. * @return zero if no error.
  213. */
  214. int udp_set_remote_url(URLContext *h, const char *uri)
  215. {
  216. UDPContext *s = h->priv_data;
  217. char hostname[256];
  218. int port;
  219. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  220. /* set the destination address */
  221. s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
  222. if (s->dest_addr_len < 0) {
  223. return AVERROR(EIO);
  224. }
  225. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  226. return 0;
  227. }
  228. /**
  229. * Return the local port used by the UDP connection
  230. * @param h media file context
  231. * @return the local port number
  232. */
  233. int udp_get_local_port(URLContext *h)
  234. {
  235. UDPContext *s = h->priv_data;
  236. return s->local_port;
  237. }
  238. /**
  239. * Return the udp file handle for select() usage to wait for several RTP
  240. * streams at the same time.
  241. * @param h media file context
  242. */
  243. #if (LIBAVFORMAT_VERSION_MAJOR >= 53)
  244. static
  245. #endif
  246. int udp_get_file_handle(URLContext *h)
  247. {
  248. UDPContext *s = h->priv_data;
  249. return s->udp_fd;
  250. }
  251. /* put it in UDP context */
  252. /* return non zero if error */
  253. static int udp_open(URLContext *h, const char *uri, int flags)
  254. {
  255. char hostname[1024];
  256. int port, udp_fd = -1, tmp, bind_ret = -1;
  257. UDPContext *s = NULL;
  258. int is_output;
  259. const char *p;
  260. char buf[256];
  261. struct sockaddr_storage my_addr;
  262. int len;
  263. h->is_streamed = 1;
  264. h->max_packet_size = 1472;
  265. is_output = (flags & URL_WRONLY);
  266. s = av_mallocz(sizeof(UDPContext));
  267. if (!s)
  268. return AVERROR(ENOMEM);
  269. h->priv_data = s;
  270. s->ttl = 16;
  271. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  272. p = strchr(uri, '?');
  273. if (p) {
  274. s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
  275. if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
  276. s->ttl = strtol(buf, NULL, 10);
  277. }
  278. if (find_info_tag(buf, sizeof(buf), "localport", p)) {
  279. s->local_port = strtol(buf, NULL, 10);
  280. }
  281. if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  282. h->max_packet_size = strtol(buf, NULL, 10);
  283. }
  284. if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  285. s->buffer_size = strtol(buf, NULL, 10);
  286. }
  287. }
  288. /* fill the dest addr */
  289. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  290. /* XXX: fix av_url_split */
  291. if (hostname[0] == '\0' || hostname[0] == '?') {
  292. /* only accepts null hostname if input */
  293. if (flags & URL_WRONLY)
  294. goto fail;
  295. } else {
  296. if (udp_set_remote_url(h, uri) < 0)
  297. goto fail;
  298. }
  299. if (s->is_multicast && !(h->flags & URL_WRONLY))
  300. s->local_port = port;
  301. udp_fd = udp_socket_create(s, &my_addr, &len);
  302. if (udp_fd < 0)
  303. goto fail;
  304. if (s->reuse_socket)
  305. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  306. goto fail;
  307. /* the bind is needed to give a port to the socket now */
  308. /* if multicast, try the multicast address bind first */
  309. if (s->is_multicast && !(h->flags & URL_WRONLY)) {
  310. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  311. }
  312. /* bind to the local address if not multicast or if the multicast
  313. * bind failed */
  314. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
  315. goto fail;
  316. len = sizeof(my_addr);
  317. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  318. s->local_port = udp_port(&my_addr, len);
  319. if (s->is_multicast) {
  320. if (h->flags & URL_WRONLY) {
  321. /* output */
  322. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  323. goto fail;
  324. } else {
  325. /* input */
  326. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  327. goto fail;
  328. }
  329. }
  330. if (is_output) {
  331. /* limit the tx buf size to limit latency */
  332. tmp = s->buffer_size;
  333. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  334. av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
  335. goto fail;
  336. }
  337. } else {
  338. /* set udp recv buffer size to the largest possible udp packet size to
  339. * avoid losing data on OSes that set this too low by default. */
  340. tmp = s->buffer_size;
  341. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  342. av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
  343. }
  344. /* make the socket non-blocking */
  345. ff_socket_nonblock(udp_fd, 1);
  346. }
  347. s->udp_fd = udp_fd;
  348. return 0;
  349. fail:
  350. if (udp_fd >= 0)
  351. closesocket(udp_fd);
  352. av_free(s);
  353. return AVERROR(EIO);
  354. }
  355. static int udp_read(URLContext *h, uint8_t *buf, int size)
  356. {
  357. UDPContext *s = h->priv_data;
  358. int len;
  359. fd_set rfds;
  360. int ret;
  361. struct timeval tv;
  362. for(;;) {
  363. if (url_interrupt_cb())
  364. return AVERROR(EINTR);
  365. FD_ZERO(&rfds);
  366. FD_SET(s->udp_fd, &rfds);
  367. tv.tv_sec = 0;
  368. tv.tv_usec = 100 * 1000;
  369. ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
  370. if (ret < 0) {
  371. if (ff_neterrno() == FF_NETERROR(EINTR))
  372. continue;
  373. return AVERROR(EIO);
  374. }
  375. if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
  376. continue;
  377. len = recv(s->udp_fd, buf, size, 0);
  378. if (len < 0) {
  379. if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
  380. ff_neterrno() != FF_NETERROR(EINTR))
  381. return AVERROR(EIO);
  382. } else {
  383. break;
  384. }
  385. }
  386. return len;
  387. }
  388. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  389. {
  390. UDPContext *s = h->priv_data;
  391. int ret;
  392. for(;;) {
  393. ret = sendto (s->udp_fd, buf, size, 0,
  394. (struct sockaddr *) &s->dest_addr,
  395. s->dest_addr_len);
  396. if (ret < 0) {
  397. if (ff_neterrno() != FF_NETERROR(EINTR) &&
  398. ff_neterrno() != FF_NETERROR(EAGAIN))
  399. return ff_neterrno();
  400. } else {
  401. break;
  402. }
  403. }
  404. return size;
  405. }
  406. static int udp_close(URLContext *h)
  407. {
  408. UDPContext *s = h->priv_data;
  409. if (s->is_multicast && !(h->flags & URL_WRONLY))
  410. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  411. closesocket(s->udp_fd);
  412. av_free(s);
  413. return 0;
  414. }
  415. URLProtocol udp_protocol = {
  416. "udp",
  417. udp_open,
  418. udp_read,
  419. udp_write,
  420. NULL, /* seek */
  421. udp_close,
  422. .url_get_file_handle = udp_get_file_handle,
  423. };