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.

462 lines
13KB

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