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.

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