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.

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