udp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. #include "avformat.h"
  27. #include "avio_internal.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/fifo.h"
  30. #include <unistd.h>
  31. #include "internal.h"
  32. #include "network.h"
  33. #include "os_support.h"
  34. #include "url.h"
  35. #if HAVE_PTHREADS
  36. #include <pthread.h>
  37. #endif
  38. #include <sys/time.h>
  39. #ifndef IPV6_ADD_MEMBERSHIP
  40. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  41. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  42. #endif
  43. typedef struct {
  44. int udp_fd;
  45. int ttl;
  46. int buffer_size;
  47. int is_multicast;
  48. int local_port;
  49. int reuse_socket;
  50. struct sockaddr_storage dest_addr;
  51. int dest_addr_len;
  52. int is_connected;
  53. /* Circular Buffer variables for use in UDP receive code */
  54. int circular_buffer_size;
  55. AVFifoBuffer *fifo;
  56. int circular_buffer_error;
  57. #if HAVE_PTHREADS
  58. pthread_t circular_buffer_thread;
  59. #endif
  60. } UDPContext;
  61. #define UDP_TX_BUF_SIZE 32768
  62. #define UDP_MAX_PKT_SIZE 65536
  63. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  64. struct sockaddr *addr)
  65. {
  66. #ifdef IP_MULTICAST_TTL
  67. if (addr->sa_family == AF_INET) {
  68. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  69. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
  70. return -1;
  71. }
  72. }
  73. #endif
  74. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  75. if (addr->sa_family == AF_INET6) {
  76. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  77. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
  78. return -1;
  79. }
  80. }
  81. #endif
  82. return 0;
  83. }
  84. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
  85. {
  86. #ifdef IP_ADD_MEMBERSHIP
  87. if (addr->sa_family == AF_INET) {
  88. struct ip_mreq mreq;
  89. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  90. mreq.imr_interface.s_addr= INADDR_ANY;
  91. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  92. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
  93. return -1;
  94. }
  95. }
  96. #endif
  97. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  98. if (addr->sa_family == AF_INET6) {
  99. struct ipv6_mreq mreq6;
  100. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  101. mreq6.ipv6mr_interface= 0;
  102. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  103. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
  104. return -1;
  105. }
  106. }
  107. #endif
  108. return 0;
  109. }
  110. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
  111. {
  112. #ifdef IP_DROP_MEMBERSHIP
  113. if (addr->sa_family == AF_INET) {
  114. struct ip_mreq mreq;
  115. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  116. mreq.imr_interface.s_addr= INADDR_ANY;
  117. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  118. av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
  119. return -1;
  120. }
  121. }
  122. #endif
  123. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  124. if (addr->sa_family == AF_INET6) {
  125. struct ipv6_mreq mreq6;
  126. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  127. mreq6.ipv6mr_interface= 0;
  128. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  129. av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
  130. return -1;
  131. }
  132. }
  133. #endif
  134. return 0;
  135. }
  136. static struct addrinfo* udp_resolve_host(const char *hostname, int port,
  137. int type, int family, int flags)
  138. {
  139. struct addrinfo hints, *res = 0;
  140. int error;
  141. char sport[16];
  142. const char *node = 0, *service = "0";
  143. if (port > 0) {
  144. snprintf(sport, sizeof(sport), "%d", port);
  145. service = sport;
  146. }
  147. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  148. node = hostname;
  149. }
  150. memset(&hints, 0, sizeof(hints));
  151. hints.ai_socktype = type;
  152. hints.ai_family = family;
  153. hints.ai_flags = flags;
  154. if ((error = getaddrinfo(node, service, &hints, &res))) {
  155. res = NULL;
  156. av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
  157. }
  158. return res;
  159. }
  160. static int udp_set_url(struct sockaddr_storage *addr,
  161. const char *hostname, int port)
  162. {
  163. struct addrinfo *res0;
  164. int addr_len;
  165. res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  166. if (res0 == 0) return AVERROR(EIO);
  167. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  168. addr_len = res0->ai_addrlen;
  169. freeaddrinfo(res0);
  170. return addr_len;
  171. }
  172. static int udp_socket_create(UDPContext *s,
  173. struct sockaddr_storage *addr, int *addr_len)
  174. {
  175. int udp_fd = -1;
  176. struct addrinfo *res0 = NULL, *res = NULL;
  177. int family = AF_UNSPEC;
  178. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  179. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  180. res0 = udp_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
  181. if (res0 == 0)
  182. goto fail;
  183. for (res = res0; res; res=res->ai_next) {
  184. udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
  185. if (udp_fd > 0) break;
  186. av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
  187. }
  188. if (udp_fd < 0)
  189. goto fail;
  190. memcpy(addr, res->ai_addr, res->ai_addrlen);
  191. *addr_len = res->ai_addrlen;
  192. freeaddrinfo(res0);
  193. return udp_fd;
  194. fail:
  195. if (udp_fd >= 0)
  196. closesocket(udp_fd);
  197. if(res0)
  198. freeaddrinfo(res0);
  199. return -1;
  200. }
  201. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  202. {
  203. char sbuf[sizeof(int)*3+1];
  204. if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
  205. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
  206. return -1;
  207. }
  208. return strtol(sbuf, NULL, 10);
  209. }
  210. /**
  211. * If no filename is given to av_open_input_file because you want to
  212. * get the local port first, then you must call this function to set
  213. * the remote server address.
  214. *
  215. * url syntax: udp://host:port[?option=val...]
  216. * option: '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 h media file context
  222. * @param uri of the remote server
  223. * @return zero if no error.
  224. */
  225. int ff_udp_set_remote_url(URLContext *h, const char *uri)
  226. {
  227. UDPContext *s = h->priv_data;
  228. char hostname[256], buf[10];
  229. int port;
  230. const char *p;
  231. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  232. /* set the destination address */
  233. s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
  234. if (s->dest_addr_len < 0) {
  235. return AVERROR(EIO);
  236. }
  237. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  238. p = strchr(uri, '?');
  239. if (p) {
  240. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  241. int was_connected = s->is_connected;
  242. s->is_connected = strtol(buf, NULL, 10);
  243. if (s->is_connected && !was_connected) {
  244. if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
  245. s->dest_addr_len)) {
  246. s->is_connected = 0;
  247. av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
  248. return AVERROR(EIO);
  249. }
  250. }
  251. }
  252. }
  253. return 0;
  254. }
  255. /**
  256. * Return the local port used by the UDP connection
  257. * @param h media file context
  258. * @return the local port number
  259. */
  260. int ff_udp_get_local_port(URLContext *h)
  261. {
  262. UDPContext *s = h->priv_data;
  263. return s->local_port;
  264. }
  265. /**
  266. * Return the udp file handle for select() usage to wait for several RTP
  267. * streams at the same time.
  268. * @param h media file context
  269. */
  270. #if !FF_API_UDP_GET_FILE
  271. static
  272. #endif
  273. int udp_get_file_handle(URLContext *h)
  274. {
  275. UDPContext *s = h->priv_data;
  276. return s->udp_fd;
  277. }
  278. static void *circular_buffer_task( void *_URLContext)
  279. {
  280. URLContext *h = _URLContext;
  281. UDPContext *s = h->priv_data;
  282. fd_set rfds;
  283. struct timeval tv;
  284. for(;;) {
  285. int left;
  286. int ret;
  287. int len;
  288. if (url_interrupt_cb()) {
  289. s->circular_buffer_error = EINTR;
  290. return NULL;
  291. }
  292. FD_ZERO(&rfds);
  293. FD_SET(s->udp_fd, &rfds);
  294. tv.tv_sec = 1;
  295. tv.tv_usec = 0;
  296. ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
  297. if (ret < 0) {
  298. if (ff_neterrno() == AVERROR(EINTR))
  299. continue;
  300. s->circular_buffer_error = EIO;
  301. return NULL;
  302. }
  303. if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
  304. continue;
  305. /* How much do we have left to the end of the buffer */
  306. /* Whats the minimum we can read so that we dont comletely fill the buffer */
  307. left = av_fifo_space(s->fifo);
  308. left = FFMIN(left, s->fifo->end - s->fifo->wptr);
  309. /* No Space left, error, what do we do now */
  310. if( !left) {
  311. av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
  312. s->circular_buffer_error = EIO;
  313. return NULL;
  314. }
  315. len = recv(s->udp_fd, s->fifo->wptr, left, 0);
  316. if (len < 0) {
  317. if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
  318. s->circular_buffer_error = EIO;
  319. return NULL;
  320. }
  321. }
  322. s->fifo->wptr += len;
  323. if (s->fifo->wptr >= s->fifo->end)
  324. s->fifo->wptr = s->fifo->buffer;
  325. s->fifo->wndx += len;
  326. }
  327. return NULL;
  328. }
  329. /* put it in UDP context */
  330. /* return non zero if error */
  331. static int udp_open(URLContext *h, const char *uri, int flags)
  332. {
  333. char hostname[1024];
  334. int port, udp_fd = -1, tmp, bind_ret = -1;
  335. UDPContext *s = NULL;
  336. int is_output;
  337. const char *p;
  338. char buf[256];
  339. struct sockaddr_storage my_addr;
  340. int len;
  341. int reuse_specified = 0;
  342. h->is_streamed = 1;
  343. h->max_packet_size = 1472;
  344. is_output = (flags & AVIO_WRONLY);
  345. s = av_mallocz(sizeof(UDPContext));
  346. if (!s)
  347. return AVERROR(ENOMEM);
  348. h->priv_data = s;
  349. s->ttl = 16;
  350. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  351. s->circular_buffer_size = 7*188*4096;
  352. p = strchr(uri, '?');
  353. if (p) {
  354. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  355. char *endptr = NULL;
  356. s->reuse_socket = strtol(buf, &endptr, 10);
  357. /* assume if no digits were found it is a request to enable it */
  358. if (buf == endptr)
  359. s->reuse_socket = 1;
  360. reuse_specified = 1;
  361. }
  362. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  363. s->ttl = strtol(buf, NULL, 10);
  364. }
  365. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  366. s->local_port = strtol(buf, NULL, 10);
  367. }
  368. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  369. h->max_packet_size = strtol(buf, NULL, 10);
  370. }
  371. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  372. s->buffer_size = strtol(buf, NULL, 10);
  373. }
  374. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  375. s->is_connected = strtol(buf, NULL, 10);
  376. }
  377. if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
  378. s->circular_buffer_size = strtol(buf, NULL, 10)*188;
  379. }
  380. }
  381. /* fill the dest addr */
  382. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  383. /* XXX: fix av_url_split */
  384. if (hostname[0] == '\0' || hostname[0] == '?') {
  385. /* only accepts null hostname if input */
  386. if (flags & AVIO_WRONLY)
  387. goto fail;
  388. } else {
  389. if (ff_udp_set_remote_url(h, uri) < 0)
  390. goto fail;
  391. }
  392. if ((s->is_multicast || !s->local_port) && !(h->flags & AVIO_WRONLY))
  393. s->local_port = port;
  394. udp_fd = udp_socket_create(s, &my_addr, &len);
  395. if (udp_fd < 0)
  396. goto fail;
  397. /* Follow the requested reuse option, unless it's multicast in which
  398. * case enable reuse unless explicitely disabled.
  399. */
  400. if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
  401. s->reuse_socket = 1;
  402. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  403. goto fail;
  404. }
  405. /* the bind is needed to give a port to the socket now */
  406. /* if multicast, try the multicast address bind first */
  407. if (s->is_multicast && !(h->flags & AVIO_WRONLY)) {
  408. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  409. }
  410. /* bind to the local address if not multicast or if the multicast
  411. * bind failed */
  412. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
  413. goto fail;
  414. len = sizeof(my_addr);
  415. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  416. s->local_port = udp_port(&my_addr, len);
  417. if (s->is_multicast) {
  418. if (h->flags & AVIO_WRONLY) {
  419. /* output */
  420. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  421. goto fail;
  422. } else {
  423. /* input */
  424. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  425. goto fail;
  426. }
  427. }
  428. if (is_output) {
  429. /* limit the tx buf size to limit latency */
  430. tmp = s->buffer_size;
  431. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  432. av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
  433. goto fail;
  434. }
  435. } else {
  436. /* set udp recv buffer size to the largest possible udp packet size to
  437. * avoid losing data on OSes that set this too low by default. */
  438. tmp = s->buffer_size;
  439. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  440. av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
  441. }
  442. /* make the socket non-blocking */
  443. ff_socket_nonblock(udp_fd, 1);
  444. }
  445. if (s->is_connected) {
  446. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  447. av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
  448. goto fail;
  449. }
  450. }
  451. s->udp_fd = udp_fd;
  452. #if HAVE_PTHREADS
  453. if (!is_output && s->circular_buffer_size) {
  454. /* start the task going */
  455. s->fifo = av_fifo_alloc(s->circular_buffer_size);
  456. if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) {
  457. av_log(h, AV_LOG_ERROR, "pthread_create failed\n");
  458. goto fail;
  459. }
  460. }
  461. #endif
  462. return 0;
  463. fail:
  464. if (udp_fd >= 0)
  465. closesocket(udp_fd);
  466. av_fifo_free(s->fifo);
  467. av_free(s);
  468. return AVERROR(EIO);
  469. }
  470. static int udp_read(URLContext *h, uint8_t *buf, int size)
  471. {
  472. UDPContext *s = h->priv_data;
  473. int ret;
  474. int avail;
  475. fd_set rfds;
  476. struct timeval tv;
  477. if (s->fifo) {
  478. do {
  479. avail = av_fifo_size(s->fifo);
  480. if (avail) { // >=size) {
  481. // Maximum amount available
  482. size = FFMIN( avail, size);
  483. av_fifo_generic_read(s->fifo, buf, size, NULL);
  484. return size;
  485. }
  486. else {
  487. FD_ZERO(&rfds);
  488. FD_SET(s->udp_fd, &rfds);
  489. tv.tv_sec = 1;
  490. tv.tv_usec = 0;
  491. ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
  492. if (ret<0)
  493. return ret;
  494. }
  495. } while( 1);
  496. }
  497. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  498. ret = ff_network_wait_fd(s->udp_fd, 0);
  499. if (ret < 0)
  500. return ret;
  501. }
  502. ret = recv(s->udp_fd, buf, size, 0);
  503. return ret < 0 ? ff_neterrno() : ret;
  504. }
  505. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  506. {
  507. UDPContext *s = h->priv_data;
  508. int ret;
  509. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  510. ret = ff_network_wait_fd(s->udp_fd, 1);
  511. if (ret < 0)
  512. return ret;
  513. }
  514. if (!s->is_connected) {
  515. ret = sendto (s->udp_fd, buf, size, 0,
  516. (struct sockaddr *) &s->dest_addr,
  517. s->dest_addr_len);
  518. } else
  519. ret = send(s->udp_fd, buf, size, 0);
  520. return ret < 0 ? ff_neterrno() : ret;
  521. }
  522. static int udp_close(URLContext *h)
  523. {
  524. UDPContext *s = h->priv_data;
  525. if (s->is_multicast && !(h->flags & AVIO_WRONLY))
  526. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  527. closesocket(s->udp_fd);
  528. av_fifo_free(s->fifo);
  529. av_free(s);
  530. return 0;
  531. }
  532. URLProtocol ff_udp_protocol = {
  533. .name = "udp",
  534. .url_open = udp_open,
  535. .url_read = udp_read,
  536. .url_write = udp_write,
  537. .url_close = udp_close,
  538. .url_get_file_handle = udp_get_file_handle,
  539. };