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.

692 lines
21KB

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