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.

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