udp.c 18 KB

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