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.

694 lines
22KB

  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. 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 = AVERROR(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. len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
  314. if (len < 0) {
  315. if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
  316. s->circular_buffer_error = AVERROR(EIO);
  317. goto end;
  318. }
  319. continue;
  320. }
  321. AV_WL32(s->tmp, len);
  322. if(left < len + 4) {
  323. /* No Space left */
  324. if (s->overrun_nonfatal) {
  325. av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
  326. "Surviving due to overrun_nonfatal option\n");
  327. continue;
  328. } else {
  329. av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "
  330. "To avoid, increase fifo_size URL option. "
  331. "To survive in such case, use overrun_nonfatal option\n");
  332. s->circular_buffer_error = AVERROR(EIO);
  333. goto end;
  334. }
  335. }
  336. pthread_mutex_lock(&s->mutex);
  337. av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
  338. pthread_cond_signal(&s->cond);
  339. pthread_mutex_unlock(&s->mutex);
  340. }
  341. end:
  342. pthread_mutex_lock(&s->mutex);
  343. pthread_cond_signal(&s->cond);
  344. pthread_mutex_unlock(&s->mutex);
  345. return NULL;
  346. }
  347. #endif
  348. /* put it in UDP context */
  349. /* return non zero if error */
  350. static int udp_open(URLContext *h, const char *uri, int flags)
  351. {
  352. char hostname[1024], localaddr[1024] = "";
  353. int port, udp_fd = -1, tmp, bind_ret = -1;
  354. UDPContext *s = h->priv_data;
  355. int is_output;
  356. const char *p;
  357. char buf[256];
  358. struct sockaddr_storage my_addr;
  359. int len;
  360. int reuse_specified = 0;
  361. h->is_streamed = 1;
  362. h->max_packet_size = 1472;
  363. is_output = !(flags & AVIO_FLAG_READ);
  364. s->ttl = 16;
  365. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  366. s->circular_buffer_size = 7*188*4096;
  367. p = strchr(uri, '?');
  368. if (p) {
  369. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  370. char *endptr = NULL;
  371. s->reuse_socket = strtol(buf, &endptr, 10);
  372. /* assume if no digits were found it is a request to enable it */
  373. if (buf == endptr)
  374. s->reuse_socket = 1;
  375. reuse_specified = 1;
  376. }
  377. if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
  378. char *endptr = NULL;
  379. s->overrun_nonfatal = strtol(buf, &endptr, 10);
  380. /* assume if no digits were found it is a request to enable it */
  381. if (buf == endptr)
  382. s->overrun_nonfatal = 1;
  383. }
  384. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  385. s->ttl = strtol(buf, NULL, 10);
  386. }
  387. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  388. s->local_port = strtol(buf, NULL, 10);
  389. }
  390. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  391. h->max_packet_size = strtol(buf, NULL, 10);
  392. }
  393. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  394. s->buffer_size = strtol(buf, NULL, 10);
  395. }
  396. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  397. s->is_connected = strtol(buf, NULL, 10);
  398. }
  399. if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
  400. s->circular_buffer_size = strtol(buf, NULL, 10)*188;
  401. }
  402. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  403. av_strlcpy(localaddr, buf, sizeof(localaddr));
  404. }
  405. }
  406. /* fill the dest addr */
  407. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  408. /* XXX: fix av_url_split */
  409. if (hostname[0] == '\0' || hostname[0] == '?') {
  410. /* only accepts null hostname if input */
  411. if (!(flags & AVIO_FLAG_READ))
  412. goto fail;
  413. } else {
  414. if (ff_udp_set_remote_url(h, uri) < 0)
  415. goto fail;
  416. }
  417. if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
  418. s->local_port = port;
  419. udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
  420. if (udp_fd < 0)
  421. goto fail;
  422. /* Follow the requested reuse option, unless it's multicast in which
  423. * case enable reuse unless explicitly disabled.
  424. */
  425. if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
  426. s->reuse_socket = 1;
  427. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  428. goto fail;
  429. }
  430. /* If multicast, try binding the multicast address first, to avoid
  431. * receiving UDP packets from other sources aimed at the same UDP
  432. * port. This fails on windows. This makes sending to the same address
  433. * using sendto() fail, so only do it if we're opened in read-only mode. */
  434. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  435. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  436. }
  437. /* bind to the local address if not multicast or if the multicast
  438. * bind failed */
  439. /* the bind is needed to give a port to the socket now */
  440. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  441. av_log(h, AV_LOG_ERROR, "bind failed: %s\n", strerror(errno));
  442. goto fail;
  443. }
  444. len = sizeof(my_addr);
  445. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  446. s->local_port = udp_port(&my_addr, len);
  447. if (s->is_multicast) {
  448. if (h->flags & AVIO_FLAG_WRITE) {
  449. /* output */
  450. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  451. goto fail;
  452. }
  453. if (h->flags & AVIO_FLAG_READ) {
  454. /* input */
  455. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
  456. goto fail;
  457. }
  458. }
  459. if (is_output) {
  460. /* limit the tx buf size to limit latency */
  461. tmp = s->buffer_size;
  462. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  463. av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
  464. goto fail;
  465. }
  466. } else {
  467. /* set udp recv buffer size to the largest possible udp packet size to
  468. * avoid losing data on OSes that set this too low by default. */
  469. tmp = s->buffer_size;
  470. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  471. av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
  472. }
  473. /* make the socket non-blocking */
  474. ff_socket_nonblock(udp_fd, 1);
  475. }
  476. if (s->is_connected) {
  477. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  478. av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
  479. goto fail;
  480. }
  481. }
  482. s->udp_fd = udp_fd;
  483. #if HAVE_PTHREADS
  484. if (!is_output && s->circular_buffer_size) {
  485. int ret;
  486. /* start the task going */
  487. s->fifo = av_fifo_alloc(s->circular_buffer_size);
  488. ret = pthread_mutex_init(&s->mutex, NULL);
  489. if (ret != 0) {
  490. av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
  491. goto fail;
  492. }
  493. ret = pthread_cond_init(&s->cond, NULL);
  494. if (ret != 0) {
  495. av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
  496. goto cond_fail;
  497. }
  498. ret = pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h);
  499. if (ret != 0) {
  500. av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
  501. goto thread_fail;
  502. }
  503. s->thread_started = 1;
  504. }
  505. #endif
  506. return 0;
  507. #if HAVE_PTHREADS
  508. thread_fail:
  509. pthread_cond_destroy(&s->cond);
  510. cond_fail:
  511. pthread_mutex_destroy(&s->mutex);
  512. #endif
  513. fail:
  514. if (udp_fd >= 0)
  515. closesocket(udp_fd);
  516. av_fifo_free(s->fifo);
  517. return AVERROR(EIO);
  518. }
  519. static int udp_read(URLContext *h, uint8_t *buf, int size)
  520. {
  521. UDPContext *s = h->priv_data;
  522. int ret;
  523. int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
  524. #if HAVE_PTHREADS
  525. if (s->fifo) {
  526. pthread_mutex_lock(&s->mutex);
  527. do {
  528. avail = av_fifo_size(s->fifo);
  529. if (avail) { // >=size) {
  530. uint8_t tmp[4];
  531. pthread_mutex_unlock(&s->mutex);
  532. av_fifo_generic_read(s->fifo, tmp, 4, NULL);
  533. avail= AV_RL32(tmp);
  534. if(avail > size){
  535. av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
  536. avail= size;
  537. }
  538. av_fifo_generic_read(s->fifo, buf, avail, NULL);
  539. av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
  540. return avail;
  541. } else if(s->circular_buffer_error){
  542. pthread_mutex_unlock(&s->mutex);
  543. return s->circular_buffer_error;
  544. } else if(nonblock) {
  545. pthread_mutex_unlock(&s->mutex);
  546. return AVERROR(EAGAIN);
  547. }
  548. else {
  549. /* FIXME: using the monotonic clock would be better,
  550. but it does not exist on all supported platforms. */
  551. int64_t t = av_gettime() + 100000;
  552. struct timespec tv = { .tv_sec = t / 1000000,
  553. .tv_nsec = (t % 1000000) * 1000 };
  554. if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0)
  555. return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
  556. nonblock = 1;
  557. }
  558. } while( 1);
  559. }
  560. #endif
  561. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  562. ret = ff_network_wait_fd(s->udp_fd, 0);
  563. if (ret < 0)
  564. return ret;
  565. }
  566. ret = recv(s->udp_fd, buf, size, 0);
  567. return ret < 0 ? ff_neterrno() : ret;
  568. }
  569. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  570. {
  571. UDPContext *s = h->priv_data;
  572. int ret;
  573. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  574. ret = ff_network_wait_fd(s->udp_fd, 1);
  575. if (ret < 0)
  576. return ret;
  577. }
  578. if (!s->is_connected) {
  579. ret = sendto (s->udp_fd, buf, size, 0,
  580. (struct sockaddr *) &s->dest_addr,
  581. s->dest_addr_len);
  582. } else
  583. ret = send(s->udp_fd, buf, size, 0);
  584. return ret < 0 ? ff_neterrno() : ret;
  585. }
  586. static int udp_close(URLContext *h)
  587. {
  588. UDPContext *s = h->priv_data;
  589. int ret;
  590. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  591. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
  592. closesocket(s->udp_fd);
  593. av_fifo_free(s->fifo);
  594. #if HAVE_PTHREADS
  595. if (s->thread_started) {
  596. s->exit_thread = 1;
  597. ret = pthread_join(s->circular_buffer_thread, NULL);
  598. if (ret != 0)
  599. av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
  600. }
  601. pthread_mutex_destroy(&s->mutex);
  602. pthread_cond_destroy(&s->cond);
  603. #endif
  604. return 0;
  605. }
  606. URLProtocol ff_udp_protocol = {
  607. .name = "udp",
  608. .url_open = udp_open,
  609. .url_read = udp_read,
  610. .url_write = udp_write,
  611. .url_close = udp_close,
  612. .url_get_file_handle = udp_get_file_handle,
  613. .priv_data_size = sizeof(UDPContext),
  614. .flags = URL_PROTOCOL_FLAG_NETWORK,
  615. };