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.

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