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.

1154 lines
40KB

  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 _DEFAULT_SOURCE
  26. #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
  27. #include "avformat.h"
  28. #include "avio_internal.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/parseutils.h"
  31. #include "libavutil/fifo.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/log.h"
  36. #include "libavutil/time.h"
  37. #include "internal.h"
  38. #include "network.h"
  39. #include "os_support.h"
  40. #include "url.h"
  41. #if HAVE_UDPLITE_H
  42. #include "udplite.h"
  43. #else
  44. /* On many Linux systems, udplite.h is missing but the kernel supports UDP-Lite.
  45. * So, we provide a fallback here.
  46. */
  47. #define UDPLITE_SEND_CSCOV 10
  48. #define UDPLITE_RECV_CSCOV 11
  49. #endif
  50. #ifndef IPPROTO_UDPLITE
  51. #define IPPROTO_UDPLITE 136
  52. #endif
  53. #if HAVE_PTHREAD_CANCEL
  54. #include <pthread.h>
  55. #endif
  56. #ifndef HAVE_PTHREAD_CANCEL
  57. #define HAVE_PTHREAD_CANCEL 0
  58. #endif
  59. #ifndef IPV6_ADD_MEMBERSHIP
  60. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  61. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  62. #endif
  63. #define UDP_TX_BUF_SIZE 32768
  64. #define UDP_MAX_PKT_SIZE 65536
  65. #define UDP_HEADER_SIZE 8
  66. typedef struct UDPContext {
  67. const AVClass *class;
  68. int udp_fd;
  69. int ttl;
  70. int udplite_coverage;
  71. int buffer_size;
  72. int pkt_size;
  73. int is_multicast;
  74. int is_broadcast;
  75. int local_port;
  76. int reuse_socket;
  77. int overrun_nonfatal;
  78. struct sockaddr_storage dest_addr;
  79. int dest_addr_len;
  80. int is_connected;
  81. /* Circular Buffer variables for use in UDP receive code */
  82. int circular_buffer_size;
  83. AVFifoBuffer *fifo;
  84. int circular_buffer_error;
  85. int64_t packet_gap; /* delay between transmitted packets */
  86. int close_req;
  87. #if HAVE_PTHREAD_CANCEL
  88. pthread_t circular_buffer_thread;
  89. pthread_mutex_t mutex;
  90. pthread_cond_t cond;
  91. int thread_started;
  92. #endif
  93. uint8_t tmp[UDP_MAX_PKT_SIZE+4];
  94. int remaining_in_dg;
  95. char *localaddr;
  96. int timeout;
  97. struct sockaddr_storage local_addr_storage;
  98. char *sources;
  99. char *block;
  100. } UDPContext;
  101. #define OFFSET(x) offsetof(UDPContext, x)
  102. #define D AV_OPT_FLAG_DECODING_PARAM
  103. #define E AV_OPT_FLAG_ENCODING_PARAM
  104. static const AVOption options[] = {
  105. { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  106. { "packet_gap", "Delay between packets", OFFSET(packet_gap), AV_OPT_TYPE_DURATION, { .i64 = 0 }, 0, INT_MAX, .flags = E },
  107. { "localport", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, D|E },
  108. { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  109. { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  110. { "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
  111. { "pkt_size", "Maximum UDP packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = 1472 }, -1, INT_MAX, .flags = D|E },
  112. { "reuse", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, D|E },
  113. { "reuse_socket", "explicitly allow reusing UDP sockets", OFFSET(reuse_socket), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, .flags = D|E },
  114. { "broadcast", "explicitly allow or disallow broadcast destination", OFFSET(is_broadcast), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, E },
  115. { "ttl", "Time to live (multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, E },
  116. { "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E },
  117. { "fifo_size", "set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = 7*4096}, 0, INT_MAX, D },
  118. { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D },
  119. { "timeout", "set raise error timeout (only in read mode)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D },
  120. { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  121. { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  122. { NULL }
  123. };
  124. static const AVClass udp_class = {
  125. .class_name = "udp",
  126. .item_name = av_default_item_name,
  127. .option = options,
  128. .version = LIBAVUTIL_VERSION_INT,
  129. };
  130. static const AVClass udplite_context_class = {
  131. .class_name = "udplite",
  132. .item_name = av_default_item_name,
  133. .option = options,
  134. .version = LIBAVUTIL_VERSION_INT,
  135. };
  136. static void log_net_error(void *ctx, int level, const char* prefix)
  137. {
  138. char errbuf[100];
  139. av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
  140. av_log(ctx, level, "%s: %s\n", prefix, errbuf);
  141. }
  142. static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
  143. struct sockaddr *addr)
  144. {
  145. #ifdef IP_MULTICAST_TTL
  146. if (addr->sa_family == AF_INET) {
  147. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
  148. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
  149. return -1;
  150. }
  151. }
  152. #endif
  153. #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
  154. if (addr->sa_family == AF_INET6) {
  155. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
  156. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
  157. return -1;
  158. }
  159. }
  160. #endif
  161. return 0;
  162. }
  163. static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
  164. {
  165. #ifdef IP_ADD_MEMBERSHIP
  166. if (addr->sa_family == AF_INET) {
  167. struct ip_mreq mreq;
  168. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  169. if (local_addr)
  170. mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
  171. else
  172. mreq.imr_interface.s_addr= INADDR_ANY;
  173. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  174. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
  175. return -1;
  176. }
  177. }
  178. #endif
  179. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  180. if (addr->sa_family == AF_INET6) {
  181. struct ipv6_mreq mreq6;
  182. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  183. mreq6.ipv6mr_interface= 0;
  184. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  185. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
  186. return -1;
  187. }
  188. }
  189. #endif
  190. return 0;
  191. }
  192. static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
  193. {
  194. #ifdef IP_DROP_MEMBERSHIP
  195. if (addr->sa_family == AF_INET) {
  196. struct ip_mreq mreq;
  197. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  198. if (local_addr)
  199. mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
  200. else
  201. mreq.imr_interface.s_addr= INADDR_ANY;
  202. if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  203. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
  204. return -1;
  205. }
  206. }
  207. #endif
  208. #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
  209. if (addr->sa_family == AF_INET6) {
  210. struct ipv6_mreq mreq6;
  211. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  212. mreq6.ipv6mr_interface= 0;
  213. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  214. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
  215. return -1;
  216. }
  217. }
  218. #endif
  219. return 0;
  220. }
  221. static struct addrinfo *udp_resolve_host(URLContext *h,
  222. const char *hostname, int port,
  223. int type, int family, int flags)
  224. {
  225. struct addrinfo hints = { 0 }, *res = 0;
  226. int error;
  227. char sport[16];
  228. const char *node = 0, *service = "0";
  229. if (port > 0) {
  230. snprintf(sport, sizeof(sport), "%d", port);
  231. service = sport;
  232. }
  233. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  234. node = hostname;
  235. }
  236. hints.ai_socktype = type;
  237. hints.ai_family = family;
  238. hints.ai_flags = flags;
  239. if ((error = getaddrinfo(node, service, &hints, &res))) {
  240. res = NULL;
  241. av_log(h, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
  242. node ? node : "unknown",
  243. service,
  244. gai_strerror(error));
  245. }
  246. return res;
  247. }
  248. static int udp_set_multicast_sources(URLContext *h,
  249. int sockfd, struct sockaddr *addr,
  250. int addr_len, char **sources,
  251. int nb_sources, int include)
  252. {
  253. #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
  254. /* These ones are available in the microsoft SDK, but don't seem to work
  255. * as on linux, so just prefer the v4-only approach there for now. */
  256. int i;
  257. for (i = 0; i < nb_sources; i++) {
  258. struct group_source_req mreqs;
  259. int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
  260. struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
  261. SOCK_DGRAM, AF_UNSPEC,
  262. 0);
  263. if (!sourceaddr)
  264. return AVERROR(ENOENT);
  265. mreqs.gsr_interface = 0;
  266. memcpy(&mreqs.gsr_group, addr, addr_len);
  267. memcpy(&mreqs.gsr_source, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
  268. freeaddrinfo(sourceaddr);
  269. if (setsockopt(sockfd, level,
  270. include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
  271. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  272. if (include)
  273. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
  274. else
  275. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
  276. return ff_neterrno();
  277. }
  278. }
  279. #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
  280. int i;
  281. if (addr->sa_family != AF_INET) {
  282. av_log(NULL, AV_LOG_ERROR,
  283. "Setting multicast sources only supported for IPv4\n");
  284. return AVERROR(EINVAL);
  285. }
  286. for (i = 0; i < nb_sources; i++) {
  287. struct ip_mreq_source mreqs;
  288. struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
  289. SOCK_DGRAM, AF_UNSPEC,
  290. 0);
  291. if (!sourceaddr)
  292. return AVERROR(ENOENT);
  293. if (sourceaddr->ai_addr->sa_family != AF_INET) {
  294. freeaddrinfo(sourceaddr);
  295. av_log(NULL, AV_LOG_ERROR, "%s is of incorrect protocol family\n",
  296. sources[i]);
  297. return AVERROR(EINVAL);
  298. }
  299. mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  300. mreqs.imr_interface.s_addr = INADDR_ANY;
  301. mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)sourceaddr->ai_addr)->sin_addr.s_addr;
  302. freeaddrinfo(sourceaddr);
  303. if (setsockopt(sockfd, IPPROTO_IP,
  304. include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
  305. (const void *)&mreqs, sizeof(mreqs)) < 0) {
  306. if (include)
  307. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
  308. else
  309. log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
  310. return ff_neterrno();
  311. }
  312. }
  313. #else
  314. return AVERROR(ENOSYS);
  315. #endif
  316. return 0;
  317. }
  318. static int udp_set_url(URLContext *h,
  319. struct sockaddr_storage *addr,
  320. const char *hostname, int port)
  321. {
  322. struct addrinfo *res0;
  323. int addr_len;
  324. res0 = udp_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
  325. if (!res0) return AVERROR(EIO);
  326. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  327. addr_len = res0->ai_addrlen;
  328. freeaddrinfo(res0);
  329. return addr_len;
  330. }
  331. static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
  332. socklen_t *addr_len, const char *localaddr)
  333. {
  334. UDPContext *s = h->priv_data;
  335. int udp_fd = -1;
  336. struct addrinfo *res0, *res;
  337. int family = AF_UNSPEC;
  338. if (((struct sockaddr *) &s->dest_addr)->sa_family)
  339. family = ((struct sockaddr *) &s->dest_addr)->sa_family;
  340. res0 = udp_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
  341. s->local_port,
  342. SOCK_DGRAM, family, AI_PASSIVE);
  343. if (!res0)
  344. goto fail;
  345. for (res = res0; res; res=res->ai_next) {
  346. if (s->udplite_coverage)
  347. udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, IPPROTO_UDPLITE);
  348. else
  349. udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
  350. if (udp_fd != -1) break;
  351. log_net_error(NULL, AV_LOG_ERROR, "socket");
  352. }
  353. if (udp_fd < 0)
  354. goto fail;
  355. memcpy(addr, res->ai_addr, res->ai_addrlen);
  356. *addr_len = res->ai_addrlen;
  357. freeaddrinfo(res0);
  358. return udp_fd;
  359. fail:
  360. if (udp_fd >= 0)
  361. closesocket(udp_fd);
  362. if(res0)
  363. freeaddrinfo(res0);
  364. return -1;
  365. }
  366. static int udp_port(struct sockaddr_storage *addr, int addr_len)
  367. {
  368. char sbuf[sizeof(int)*3+1];
  369. int error;
  370. if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
  371. av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
  372. return -1;
  373. }
  374. return strtol(sbuf, NULL, 10);
  375. }
  376. /**
  377. * If no filename is given to av_open_input_file because you want to
  378. * get the local port first, then you must call this function to set
  379. * the remote server address.
  380. *
  381. * url syntax: udp://host:port[?option=val...]
  382. * option: 'ttl=n' : set the ttl value (for multicast only)
  383. * 'localport=n' : set the local port
  384. * 'pkt_size=n' : set max packet size
  385. * 'reuse=1' : enable reusing the socket
  386. * 'overrun_nonfatal=1': survive in case of circular buffer overrun
  387. *
  388. * @param h media file context
  389. * @param uri of the remote server
  390. * @return zero if no error.
  391. */
  392. int ff_udp_set_remote_url(URLContext *h, const char *uri)
  393. {
  394. UDPContext *s = h->priv_data;
  395. char hostname[256], buf[10];
  396. int port;
  397. const char *p;
  398. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  399. /* set the destination address */
  400. s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
  401. if (s->dest_addr_len < 0) {
  402. return AVERROR(EIO);
  403. }
  404. s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
  405. p = strchr(uri, '?');
  406. if (p) {
  407. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  408. int was_connected = s->is_connected;
  409. s->is_connected = strtol(buf, NULL, 10);
  410. if (s->is_connected && !was_connected) {
  411. if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
  412. s->dest_addr_len)) {
  413. s->is_connected = 0;
  414. log_net_error(h, AV_LOG_ERROR, "connect");
  415. return AVERROR(EIO);
  416. }
  417. }
  418. }
  419. }
  420. return 0;
  421. }
  422. /**
  423. * Return the local port used by the UDP connection
  424. * @param h media file context
  425. * @return the local port number
  426. */
  427. int ff_udp_get_local_port(URLContext *h)
  428. {
  429. UDPContext *s = h->priv_data;
  430. return s->local_port;
  431. }
  432. /**
  433. * Return the udp file handle for select() usage to wait for several RTP
  434. * streams at the same time.
  435. * @param h media file context
  436. */
  437. static int udp_get_file_handle(URLContext *h)
  438. {
  439. UDPContext *s = h->priv_data;
  440. return s->udp_fd;
  441. }
  442. #if HAVE_PTHREAD_CANCEL
  443. static void *circular_buffer_task_rx( void *_URLContext)
  444. {
  445. URLContext *h = _URLContext;
  446. UDPContext *s = h->priv_data;
  447. int old_cancelstate;
  448. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  449. pthread_mutex_lock(&s->mutex);
  450. if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
  451. av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
  452. s->circular_buffer_error = AVERROR(EIO);
  453. goto end;
  454. }
  455. while(1) {
  456. int len;
  457. pthread_mutex_unlock(&s->mutex);
  458. /* Blocking operations are always cancellation points;
  459. see "General Information" / "Thread Cancelation Overview"
  460. in Single Unix. */
  461. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
  462. len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
  463. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  464. pthread_mutex_lock(&s->mutex);
  465. if (len < 0) {
  466. if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
  467. s->circular_buffer_error = ff_neterrno();
  468. goto end;
  469. }
  470. continue;
  471. }
  472. AV_WL32(s->tmp, len);
  473. if(av_fifo_space(s->fifo) < len + 4) {
  474. /* No Space left */
  475. if (s->overrun_nonfatal) {
  476. av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
  477. "Surviving due to overrun_nonfatal option\n");
  478. continue;
  479. } else {
  480. av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "
  481. "To avoid, increase fifo_size URL option. "
  482. "To survive in such case, use overrun_nonfatal option\n");
  483. s->circular_buffer_error = AVERROR(EIO);
  484. goto end;
  485. }
  486. }
  487. av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
  488. pthread_cond_signal(&s->cond);
  489. }
  490. end:
  491. pthread_cond_signal(&s->cond);
  492. pthread_mutex_unlock(&s->mutex);
  493. return NULL;
  494. }
  495. static void *circular_buffer_task_tx( void *_URLContext)
  496. {
  497. URLContext *h = _URLContext;
  498. UDPContext *s = h->priv_data;
  499. int old_cancelstate;
  500. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  501. pthread_mutex_lock(&s->mutex);
  502. if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
  503. av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
  504. s->circular_buffer_error = AVERROR(EIO);
  505. goto end;
  506. }
  507. for(;;) {
  508. int len;
  509. const uint8_t *p;
  510. uint8_t tmp[4];
  511. len=av_fifo_size(s->fifo);
  512. while (len<4) {
  513. if (s->close_req)
  514. goto end;
  515. if (pthread_cond_wait(&s->cond, &s->mutex) < 0) {
  516. goto end;
  517. }
  518. len=av_fifo_size(s->fifo);
  519. }
  520. av_fifo_generic_read(s->fifo, tmp, 4, NULL);
  521. len=AV_RL32(tmp);
  522. av_assert0(len >= 0);
  523. av_assert0(len <= sizeof(s->tmp));
  524. av_fifo_generic_read(s->fifo, s->tmp, len, NULL);
  525. pthread_mutex_unlock(&s->mutex);
  526. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
  527. p = s->tmp;
  528. while (len) {
  529. int ret;
  530. av_assert0(len > 0);
  531. if (!s->is_connected) {
  532. ret = sendto (s->udp_fd, p, len, 0,
  533. (struct sockaddr *) &s->dest_addr,
  534. s->dest_addr_len);
  535. } else
  536. ret = send(s->udp_fd, p, len, 0);
  537. if (ret >= 0) {
  538. len -= ret;
  539. p += ret;
  540. } else {
  541. ret = ff_neterrno();
  542. if (ret != AVERROR(EAGAIN) && ret != AVERROR(EINTR)) {
  543. pthread_mutex_lock(&s->mutex);
  544. s->circular_buffer_error = ret;
  545. pthread_mutex_unlock(&s->mutex);
  546. return NULL;
  547. }
  548. }
  549. }
  550. av_usleep(s->packet_gap);
  551. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
  552. pthread_mutex_lock(&s->mutex);
  553. }
  554. end:
  555. pthread_mutex_unlock(&s->mutex);
  556. return NULL;
  557. }
  558. #endif
  559. static int parse_source_list(char *buf, char **sources, int *num_sources,
  560. int max_sources)
  561. {
  562. char *source_start;
  563. source_start = buf;
  564. while (1) {
  565. char *next = strchr(source_start, ',');
  566. if (next)
  567. *next = '\0';
  568. sources[*num_sources] = av_strdup(source_start);
  569. if (!sources[*num_sources])
  570. return AVERROR(ENOMEM);
  571. source_start = next + 1;
  572. (*num_sources)++;
  573. if (*num_sources >= max_sources || !next)
  574. break;
  575. }
  576. return 0;
  577. }
  578. /* put it in UDP context */
  579. /* return non zero if error */
  580. static int udp_open(URLContext *h, const char *uri, int flags)
  581. {
  582. char hostname[1024], localaddr[1024] = "";
  583. int port, udp_fd = -1, tmp, bind_ret = -1, dscp = -1;
  584. UDPContext *s = h->priv_data;
  585. int is_output;
  586. const char *p;
  587. char buf[256];
  588. struct sockaddr_storage my_addr;
  589. socklen_t len;
  590. int i, num_include_sources = 0, num_exclude_sources = 0;
  591. char *include_sources[32], *exclude_sources[32];
  592. h->is_streamed = 1;
  593. is_output = !(flags & AVIO_FLAG_READ);
  594. if (s->buffer_size < 0)
  595. s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
  596. if (s->sources) {
  597. if (parse_source_list(s->sources, include_sources,
  598. &num_include_sources,
  599. FF_ARRAY_ELEMS(include_sources)))
  600. goto fail;
  601. }
  602. if (s->block) {
  603. if (parse_source_list(s->block, exclude_sources, &num_exclude_sources,
  604. FF_ARRAY_ELEMS(exclude_sources)))
  605. goto fail;
  606. }
  607. if (s->pkt_size > 0)
  608. h->max_packet_size = s->pkt_size;
  609. p = strchr(uri, '?');
  610. if (p) {
  611. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  612. char *endptr = NULL;
  613. s->reuse_socket = strtol(buf, &endptr, 10);
  614. /* assume if no digits were found it is a request to enable it */
  615. if (buf == endptr)
  616. s->reuse_socket = 1;
  617. }
  618. if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
  619. char *endptr = NULL;
  620. s->overrun_nonfatal = strtol(buf, &endptr, 10);
  621. /* assume if no digits were found it is a request to enable it */
  622. if (buf == endptr)
  623. s->overrun_nonfatal = 1;
  624. if (!HAVE_PTHREAD_CANCEL)
  625. av_log(h, AV_LOG_WARNING,
  626. "'overrun_nonfatal' option was set but it is not supported "
  627. "on this build (pthread support is required)\n");
  628. }
  629. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  630. s->ttl = strtol(buf, NULL, 10);
  631. }
  632. if (av_find_info_tag(buf, sizeof(buf), "udplite_coverage", p)) {
  633. s->udplite_coverage = strtol(buf, NULL, 10);
  634. }
  635. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  636. s->local_port = strtol(buf, NULL, 10);
  637. }
  638. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  639. s->pkt_size = strtol(buf, NULL, 10);
  640. }
  641. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  642. s->buffer_size = strtol(buf, NULL, 10);
  643. }
  644. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  645. s->is_connected = strtol(buf, NULL, 10);
  646. }
  647. if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
  648. dscp = strtol(buf, NULL, 10);
  649. }
  650. if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
  651. s->circular_buffer_size = strtol(buf, NULL, 10);
  652. if (!HAVE_PTHREAD_CANCEL)
  653. av_log(h, AV_LOG_WARNING,
  654. "'circular_buffer_size' option was set but it is not supported "
  655. "on this build (pthread support is required)\n");
  656. }
  657. if (av_find_info_tag(buf, sizeof(buf), "packet_gap", p)) {
  658. if (av_parse_time(&s->packet_gap, buf, 1)<0) {
  659. av_log(h, AV_LOG_ERROR, "Can't parse 'packet_gap'");
  660. goto fail;
  661. }
  662. if (!HAVE_PTHREAD_CANCEL)
  663. av_log(h, AV_LOG_WARNING,
  664. "'packet_gap' option was set but it is not supported "
  665. "on this build (pthread support is required)\n");
  666. }
  667. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  668. av_strlcpy(localaddr, buf, sizeof(localaddr));
  669. }
  670. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  671. if (parse_source_list(buf, include_sources, &num_include_sources,
  672. FF_ARRAY_ELEMS(include_sources)))
  673. goto fail;
  674. }
  675. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  676. if (parse_source_list(buf, exclude_sources, &num_exclude_sources,
  677. FF_ARRAY_ELEMS(exclude_sources)))
  678. goto fail;
  679. }
  680. if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p))
  681. s->timeout = strtol(buf, NULL, 10);
  682. if (is_output && av_find_info_tag(buf, sizeof(buf), "broadcast", p))
  683. s->is_broadcast = strtol(buf, NULL, 10);
  684. }
  685. /* handling needed to support options picking from both AVOption and URL */
  686. s->circular_buffer_size *= 188;
  687. if (flags & AVIO_FLAG_WRITE) {
  688. h->max_packet_size = s->pkt_size;
  689. } else {
  690. h->max_packet_size = UDP_MAX_PKT_SIZE;
  691. }
  692. h->rw_timeout = s->timeout;
  693. /* fill the dest addr */
  694. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  695. /* XXX: fix av_url_split */
  696. if (hostname[0] == '\0' || hostname[0] == '?') {
  697. /* only accepts null hostname if input */
  698. if (!(flags & AVIO_FLAG_READ))
  699. goto fail;
  700. } else {
  701. if (ff_udp_set_remote_url(h, uri) < 0)
  702. goto fail;
  703. }
  704. if ((s->is_multicast || s->local_port <= 0) && (h->flags & AVIO_FLAG_READ))
  705. s->local_port = port;
  706. if (localaddr[0])
  707. udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
  708. else
  709. udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
  710. if (udp_fd < 0)
  711. goto fail;
  712. s->local_addr_storage=my_addr; //store for future multicast join
  713. /* Follow the requested reuse option, unless it's multicast in which
  714. * case enable reuse unless explicitly disabled.
  715. */
  716. if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
  717. s->reuse_socket = 1;
  718. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  719. goto fail;
  720. }
  721. if (s->is_broadcast) {
  722. #ifdef SO_BROADCAST
  723. if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0)
  724. #endif
  725. goto fail;
  726. }
  727. /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
  728. * The receiver coverage has to be less than or equal to the sender coverage.
  729. * Otherwise, the receiver will drop all packets.
  730. */
  731. if (s->udplite_coverage) {
  732. if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
  733. av_log(h, AV_LOG_WARNING, "socket option UDPLITE_SEND_CSCOV not available");
  734. if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
  735. av_log(h, AV_LOG_WARNING, "socket option UDPLITE_RECV_CSCOV not available");
  736. }
  737. if (dscp >= 0) {
  738. dscp <<= 2;
  739. if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
  740. goto fail;
  741. }
  742. /* If multicast, try binding the multicast address first, to avoid
  743. * receiving UDP packets from other sources aimed at the same UDP
  744. * port. This fails on windows. This makes sending to the same address
  745. * using sendto() fail, so only do it if we're opened in read-only mode. */
  746. if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
  747. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  748. }
  749. /* bind to the local address if not multicast or if the multicast
  750. * bind failed */
  751. /* the bind is needed to give a port to the socket now */
  752. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  753. log_net_error(h, AV_LOG_ERROR, "bind failed");
  754. goto fail;
  755. }
  756. len = sizeof(my_addr);
  757. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  758. s->local_port = udp_port(&my_addr, len);
  759. if (s->is_multicast) {
  760. if (h->flags & AVIO_FLAG_WRITE) {
  761. /* output */
  762. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  763. goto fail;
  764. }
  765. if (h->flags & AVIO_FLAG_READ) {
  766. /* input */
  767. if (num_include_sources && num_exclude_sources) {
  768. av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n");
  769. goto fail;
  770. }
  771. if (num_include_sources) {
  772. if (udp_set_multicast_sources(h, udp_fd,
  773. (struct sockaddr *)&s->dest_addr,
  774. s->dest_addr_len,
  775. include_sources,
  776. num_include_sources, 1) < 0)
  777. goto fail;
  778. } else {
  779. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage) < 0)
  780. goto fail;
  781. }
  782. if (num_exclude_sources) {
  783. if (udp_set_multicast_sources(h, udp_fd,
  784. (struct sockaddr *)&s->dest_addr,
  785. s->dest_addr_len,
  786. exclude_sources,
  787. num_exclude_sources, 0) < 0)
  788. goto fail;
  789. }
  790. }
  791. }
  792. if (is_output) {
  793. /* limit the tx buf size to limit latency */
  794. tmp = s->buffer_size;
  795. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  796. log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  797. goto fail;
  798. }
  799. } else {
  800. /* set udp recv buffer size to the requested value (default 64K) */
  801. tmp = s->buffer_size;
  802. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  803. log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  804. }
  805. len = sizeof(tmp);
  806. if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
  807. log_net_error(h, AV_LOG_WARNING, "getsockopt(SO_RCVBUF)");
  808. } else {
  809. av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp);
  810. if(tmp < s->buffer_size)
  811. av_log(h, AV_LOG_WARNING, "attempted to set receive buffer to size %d but it only ended up set as %d", s->buffer_size, tmp);
  812. }
  813. /* make the socket non-blocking */
  814. ff_socket_nonblock(udp_fd, 1);
  815. }
  816. if (s->is_connected) {
  817. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  818. log_net_error(h, AV_LOG_ERROR, "connect");
  819. goto fail;
  820. }
  821. }
  822. for (i = 0; i < num_include_sources; i++)
  823. av_freep(&include_sources[i]);
  824. for (i = 0; i < num_exclude_sources; i++)
  825. av_freep(&exclude_sources[i]);
  826. s->udp_fd = udp_fd;
  827. #if HAVE_PTHREAD_CANCEL
  828. /*
  829. Create thread in case of:
  830. 1. Input and circular_buffer_size is set
  831. 2. Output and packet_gap and circular_buffer_size is set
  832. */
  833. if (is_output && s->packet_gap && !s->circular_buffer_size) {
  834. /* Warn user in case of 'circular_buffer_size' is not set */
  835. av_log(h, AV_LOG_WARNING,"'packet_gap' option was set but 'circular_buffer_size' is not, but required\n");
  836. }
  837. if ((!is_output && s->circular_buffer_size) || (is_output && s->packet_gap && s->circular_buffer_size)) {
  838. int ret;
  839. /* start the task going */
  840. s->fifo = av_fifo_alloc(s->circular_buffer_size);
  841. ret = pthread_mutex_init(&s->mutex, NULL);
  842. if (ret != 0) {
  843. av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
  844. goto fail;
  845. }
  846. ret = pthread_cond_init(&s->cond, NULL);
  847. if (ret != 0) {
  848. av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
  849. goto cond_fail;
  850. }
  851. ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
  852. if (ret != 0) {
  853. av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
  854. goto thread_fail;
  855. }
  856. s->thread_started = 1;
  857. }
  858. #endif
  859. return 0;
  860. #if HAVE_PTHREAD_CANCEL
  861. thread_fail:
  862. pthread_cond_destroy(&s->cond);
  863. cond_fail:
  864. pthread_mutex_destroy(&s->mutex);
  865. #endif
  866. fail:
  867. if (udp_fd >= 0)
  868. closesocket(udp_fd);
  869. av_fifo_freep(&s->fifo);
  870. for (i = 0; i < num_include_sources; i++)
  871. av_freep(&include_sources[i]);
  872. for (i = 0; i < num_exclude_sources; i++)
  873. av_freep(&exclude_sources[i]);
  874. return AVERROR(EIO);
  875. }
  876. static int udplite_open(URLContext *h, const char *uri, int flags)
  877. {
  878. UDPContext *s = h->priv_data;
  879. // set default checksum coverage
  880. s->udplite_coverage = UDP_HEADER_SIZE;
  881. return udp_open(h, uri, flags);
  882. }
  883. static int udp_read(URLContext *h, uint8_t *buf, int size)
  884. {
  885. UDPContext *s = h->priv_data;
  886. int ret;
  887. #if HAVE_PTHREAD_CANCEL
  888. int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
  889. if (s->fifo) {
  890. pthread_mutex_lock(&s->mutex);
  891. do {
  892. avail = av_fifo_size(s->fifo);
  893. if (avail) { // >=size) {
  894. uint8_t tmp[4];
  895. av_fifo_generic_read(s->fifo, tmp, 4, NULL);
  896. avail= AV_RL32(tmp);
  897. if(avail > size){
  898. av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
  899. avail= size;
  900. }
  901. av_fifo_generic_read(s->fifo, buf, avail, NULL);
  902. av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
  903. pthread_mutex_unlock(&s->mutex);
  904. return avail;
  905. } else if(s->circular_buffer_error){
  906. int err = s->circular_buffer_error;
  907. pthread_mutex_unlock(&s->mutex);
  908. return err;
  909. } else if(nonblock) {
  910. pthread_mutex_unlock(&s->mutex);
  911. return AVERROR(EAGAIN);
  912. }
  913. else {
  914. /* FIXME: using the monotonic clock would be better,
  915. but it does not exist on all supported platforms. */
  916. int64_t t = av_gettime() + 100000;
  917. struct timespec tv = { .tv_sec = t / 1000000,
  918. .tv_nsec = (t % 1000000) * 1000 };
  919. if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
  920. pthread_mutex_unlock(&s->mutex);
  921. return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
  922. }
  923. nonblock = 1;
  924. }
  925. } while( 1);
  926. }
  927. #endif
  928. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  929. ret = ff_network_wait_fd(s->udp_fd, 0);
  930. if (ret < 0)
  931. return ret;
  932. }
  933. ret = recv(s->udp_fd, buf, size, 0);
  934. return ret < 0 ? ff_neterrno() : ret;
  935. }
  936. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  937. {
  938. UDPContext *s = h->priv_data;
  939. int ret;
  940. #if HAVE_PTHREAD_CANCEL
  941. if (s->fifo) {
  942. uint8_t tmp[4];
  943. pthread_mutex_lock(&s->mutex);
  944. /*
  945. Return error if last tx failed.
  946. Here we can't know on which packet error was, but it needs to know that error exists.
  947. */
  948. if (s->circular_buffer_error<0) {
  949. int err=s->circular_buffer_error;
  950. pthread_mutex_unlock(&s->mutex);
  951. return err;
  952. }
  953. if(av_fifo_space(s->fifo) < size + 4) {
  954. /* What about a partial packet tx ? */
  955. pthread_mutex_unlock(&s->mutex);
  956. return AVERROR(ENOMEM);
  957. }
  958. AV_WL32(tmp, size);
  959. av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
  960. av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
  961. pthread_cond_signal(&s->cond);
  962. pthread_mutex_unlock(&s->mutex);
  963. return size;
  964. }
  965. #endif
  966. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  967. ret = ff_network_wait_fd(s->udp_fd, 1);
  968. if (ret < 0)
  969. return ret;
  970. }
  971. if (!s->is_connected) {
  972. ret = sendto (s->udp_fd, buf, size, 0,
  973. (struct sockaddr *) &s->dest_addr,
  974. s->dest_addr_len);
  975. } else
  976. ret = send(s->udp_fd, buf, size, 0);
  977. return ret < 0 ? ff_neterrno() : ret;
  978. }
  979. static int udp_close(URLContext *h)
  980. {
  981. UDPContext *s = h->priv_data;
  982. #if HAVE_PTHREAD_CANCEL
  983. // Request close once writing is finished
  984. if (s->thread_started && !(h->flags & AVIO_FLAG_READ)) {
  985. pthread_mutex_lock(&s->mutex);
  986. s->close_req = 1;
  987. pthread_cond_signal(&s->cond);
  988. pthread_mutex_unlock(&s->mutex);
  989. }
  990. #endif
  991. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  992. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage);
  993. #if HAVE_PTHREAD_CANCEL
  994. if (s->thread_started) {
  995. int ret;
  996. // Cancel only read, as write has been signaled as success to the user
  997. if (h->flags & AVIO_FLAG_READ)
  998. pthread_cancel(s->circular_buffer_thread);
  999. ret = pthread_join(s->circular_buffer_thread, NULL);
  1000. if (ret != 0)
  1001. av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
  1002. pthread_mutex_destroy(&s->mutex);
  1003. pthread_cond_destroy(&s->cond);
  1004. }
  1005. #endif
  1006. closesocket(s->udp_fd);
  1007. av_fifo_freep(&s->fifo);
  1008. return 0;
  1009. }
  1010. const URLProtocol ff_udp_protocol = {
  1011. .name = "udp",
  1012. .url_open = udp_open,
  1013. .url_read = udp_read,
  1014. .url_write = udp_write,
  1015. .url_close = udp_close,
  1016. .url_get_file_handle = udp_get_file_handle,
  1017. .priv_data_size = sizeof(UDPContext),
  1018. .priv_data_class = &udp_class,
  1019. .flags = URL_PROTOCOL_FLAG_NETWORK,
  1020. };
  1021. const URLProtocol ff_udplite_protocol = {
  1022. .name = "udplite",
  1023. .url_open = udplite_open,
  1024. .url_read = udp_read,
  1025. .url_write = udp_write,
  1026. .url_close = udp_close,
  1027. .url_get_file_handle = udp_get_file_handle,
  1028. .priv_data_size = sizeof(UDPContext),
  1029. .priv_data_class = &udplite_context_class,
  1030. .flags = URL_PROTOCOL_FLAG_NETWORK,
  1031. };