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.

641 lines
19KB

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