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.

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