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.

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