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.

1124 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. if (s->pkt_size > 0)
  581. h->max_packet_size = s->pkt_size;
  582. p = strchr(uri, '?');
  583. if (p) {
  584. if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
  585. char *endptr = NULL;
  586. s->reuse_socket = strtol(buf, &endptr, 10);
  587. /* assume if no digits were found it is a request to enable it */
  588. if (buf == endptr)
  589. s->reuse_socket = 1;
  590. }
  591. if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
  592. char *endptr = NULL;
  593. s->overrun_nonfatal = strtol(buf, &endptr, 10);
  594. /* assume if no digits were found it is a request to enable it */
  595. if (buf == endptr)
  596. s->overrun_nonfatal = 1;
  597. if (!HAVE_PTHREAD_CANCEL)
  598. av_log(h, AV_LOG_WARNING,
  599. "'overrun_nonfatal' option was set but it is not supported "
  600. "on this build (pthread support is required)\n");
  601. }
  602. if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
  603. s->ttl = strtol(buf, NULL, 10);
  604. }
  605. if (av_find_info_tag(buf, sizeof(buf), "udplite_coverage", p)) {
  606. s->udplite_coverage = strtol(buf, NULL, 10);
  607. }
  608. if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
  609. s->local_port = strtol(buf, NULL, 10);
  610. }
  611. if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  612. s->pkt_size = strtol(buf, NULL, 10);
  613. }
  614. if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
  615. s->buffer_size = strtol(buf, NULL, 10);
  616. }
  617. if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
  618. s->is_connected = strtol(buf, NULL, 10);
  619. }
  620. if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
  621. dscp = strtol(buf, NULL, 10);
  622. }
  623. if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
  624. s->circular_buffer_size = strtol(buf, NULL, 10);
  625. if (!HAVE_PTHREAD_CANCEL)
  626. av_log(h, AV_LOG_WARNING,
  627. "'circular_buffer_size' option was set but it is not supported "
  628. "on this build (pthread support is required)\n");
  629. }
  630. if (av_find_info_tag(buf, sizeof(buf), "bitrate", p)) {
  631. s->bitrate = strtoll(buf, NULL, 10);
  632. if (!HAVE_PTHREAD_CANCEL)
  633. av_log(h, AV_LOG_WARNING,
  634. "'bitrate' option was set but it is not supported "
  635. "on this build (pthread support is required)\n");
  636. }
  637. if (av_find_info_tag(buf, sizeof(buf), "burst_bits", p)) {
  638. s->burst_bits = strtoll(buf, NULL, 10);
  639. }
  640. if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
  641. av_strlcpy(localaddr, buf, sizeof(localaddr));
  642. }
  643. if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
  644. if (ff_ip_parse_sources(h, buf, &s->filters) < 0)
  645. goto fail;
  646. }
  647. if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
  648. if (ff_ip_parse_blocks(h, buf, &s->filters) < 0)
  649. goto fail;
  650. }
  651. if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p))
  652. s->timeout = strtol(buf, NULL, 10);
  653. if (is_output && av_find_info_tag(buf, sizeof(buf), "broadcast", p))
  654. s->is_broadcast = strtol(buf, NULL, 10);
  655. }
  656. /* handling needed to support options picking from both AVOption and URL */
  657. s->circular_buffer_size *= 188;
  658. if (flags & AVIO_FLAG_WRITE) {
  659. h->max_packet_size = s->pkt_size;
  660. } else {
  661. h->max_packet_size = UDP_MAX_PKT_SIZE;
  662. }
  663. h->rw_timeout = s->timeout;
  664. /* fill the dest addr */
  665. av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
  666. /* XXX: fix av_url_split */
  667. if (hostname[0] == '\0' || hostname[0] == '?') {
  668. /* only accepts null hostname if input */
  669. if (!(flags & AVIO_FLAG_READ))
  670. goto fail;
  671. } else {
  672. if (ff_udp_set_remote_url(h, uri) < 0)
  673. goto fail;
  674. }
  675. if ((s->is_multicast || s->local_port <= 0) && (h->flags & AVIO_FLAG_READ))
  676. s->local_port = port;
  677. if (localaddr[0])
  678. udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
  679. else
  680. udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
  681. if (udp_fd < 0)
  682. goto fail;
  683. s->local_addr_storage=my_addr; //store for future multicast join
  684. /* Follow the requested reuse option, unless it's multicast in which
  685. * case enable reuse unless explicitly disabled.
  686. */
  687. if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
  688. s->reuse_socket = 1;
  689. if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
  690. goto fail;
  691. }
  692. if (s->is_broadcast) {
  693. #ifdef SO_BROADCAST
  694. if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0)
  695. #endif
  696. goto fail;
  697. }
  698. /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
  699. * The receiver coverage has to be less than or equal to the sender coverage.
  700. * Otherwise, the receiver will drop all packets.
  701. */
  702. if (s->udplite_coverage) {
  703. if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
  704. av_log(h, AV_LOG_WARNING, "socket option UDPLITE_SEND_CSCOV not available");
  705. if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
  706. av_log(h, AV_LOG_WARNING, "socket option UDPLITE_RECV_CSCOV not available");
  707. }
  708. if (dscp >= 0) {
  709. dscp <<= 2;
  710. if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
  711. goto fail;
  712. }
  713. /* If multicast, try binding the multicast address first, to avoid
  714. * receiving UDP packets from other sources aimed at the same UDP
  715. * port. This fails on windows. This makes sending to the same address
  716. * using sendto() fail, so only do it if we're opened in read-only mode. */
  717. if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
  718. bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
  719. }
  720. /* bind to the local address if not multicast or if the multicast
  721. * bind failed */
  722. /* the bind is needed to give a port to the socket now */
  723. if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
  724. ff_log_net_error(h, AV_LOG_ERROR, "bind failed");
  725. goto fail;
  726. }
  727. len = sizeof(my_addr);
  728. getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
  729. s->local_port = udp_port(&my_addr, len);
  730. if (s->is_multicast) {
  731. if (h->flags & AVIO_FLAG_WRITE) {
  732. /* output */
  733. if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
  734. goto fail;
  735. }
  736. if (h->flags & AVIO_FLAG_READ) {
  737. /* input */
  738. if (s->filters.nb_include_addrs) {
  739. if (udp_set_multicast_sources(h, udp_fd,
  740. (struct sockaddr *)&s->dest_addr,
  741. s->dest_addr_len, &s->local_addr_storage,
  742. s->filters.include_addrs,
  743. s->filters.nb_include_addrs, 1) < 0)
  744. goto fail;
  745. } else {
  746. if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage) < 0)
  747. goto fail;
  748. }
  749. if (s->filters.nb_exclude_addrs) {
  750. if (udp_set_multicast_sources(h, udp_fd,
  751. (struct sockaddr *)&s->dest_addr,
  752. s->dest_addr_len, &s->local_addr_storage,
  753. s->filters.exclude_addrs,
  754. s->filters.nb_exclude_addrs, 0) < 0)
  755. goto fail;
  756. }
  757. }
  758. }
  759. if (is_output) {
  760. /* limit the tx buf size to limit latency */
  761. tmp = s->buffer_size;
  762. if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
  763. ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
  764. goto fail;
  765. }
  766. } else {
  767. /* set udp recv buffer size to the requested value (default 64K) */
  768. tmp = s->buffer_size;
  769. if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
  770. ff_log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
  771. }
  772. len = sizeof(tmp);
  773. if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
  774. ff_log_net_error(h, AV_LOG_WARNING, "getsockopt(SO_RCVBUF)");
  775. } else {
  776. av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp);
  777. if(tmp < s->buffer_size)
  778. 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);
  779. }
  780. /* make the socket non-blocking */
  781. ff_socket_nonblock(udp_fd, 1);
  782. }
  783. if (s->is_connected) {
  784. if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
  785. ff_log_net_error(h, AV_LOG_ERROR, "connect");
  786. goto fail;
  787. }
  788. }
  789. s->udp_fd = udp_fd;
  790. #if HAVE_PTHREAD_CANCEL
  791. /*
  792. Create thread in case of:
  793. 1. Input and circular_buffer_size is set
  794. 2. Output and bitrate and circular_buffer_size is set
  795. */
  796. if (is_output && s->bitrate && !s->circular_buffer_size) {
  797. /* Warn user in case of 'circular_buffer_size' is not set */
  798. av_log(h, AV_LOG_WARNING,"'bitrate' option was set but 'circular_buffer_size' is not, but required\n");
  799. }
  800. if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
  801. int ret;
  802. /* start the task going */
  803. s->fifo = av_fifo_alloc(s->circular_buffer_size);
  804. ret = pthread_mutex_init(&s->mutex, NULL);
  805. if (ret != 0) {
  806. av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
  807. goto fail;
  808. }
  809. ret = pthread_cond_init(&s->cond, NULL);
  810. if (ret != 0) {
  811. av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
  812. goto cond_fail;
  813. }
  814. ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
  815. if (ret != 0) {
  816. av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
  817. goto thread_fail;
  818. }
  819. s->thread_started = 1;
  820. }
  821. #endif
  822. return 0;
  823. #if HAVE_PTHREAD_CANCEL
  824. thread_fail:
  825. pthread_cond_destroy(&s->cond);
  826. cond_fail:
  827. pthread_mutex_destroy(&s->mutex);
  828. #endif
  829. fail:
  830. if (udp_fd >= 0)
  831. closesocket(udp_fd);
  832. av_fifo_freep(&s->fifo);
  833. ff_ip_reset_filters(&s->filters);
  834. return AVERROR(EIO);
  835. }
  836. static int udplite_open(URLContext *h, const char *uri, int flags)
  837. {
  838. UDPContext *s = h->priv_data;
  839. // set default checksum coverage
  840. s->udplite_coverage = UDP_HEADER_SIZE;
  841. return udp_open(h, uri, flags);
  842. }
  843. static int udp_read(URLContext *h, uint8_t *buf, int size)
  844. {
  845. UDPContext *s = h->priv_data;
  846. int ret;
  847. struct sockaddr_storage addr;
  848. socklen_t addr_len = sizeof(addr);
  849. #if HAVE_PTHREAD_CANCEL
  850. int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
  851. if (s->fifo) {
  852. pthread_mutex_lock(&s->mutex);
  853. do {
  854. avail = av_fifo_size(s->fifo);
  855. if (avail) { // >=size) {
  856. uint8_t tmp[4];
  857. av_fifo_generic_read(s->fifo, tmp, 4, NULL);
  858. avail= AV_RL32(tmp);
  859. if(avail > size){
  860. av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
  861. avail= size;
  862. }
  863. av_fifo_generic_read(s->fifo, buf, avail, NULL);
  864. av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
  865. pthread_mutex_unlock(&s->mutex);
  866. return avail;
  867. } else if(s->circular_buffer_error){
  868. int err = s->circular_buffer_error;
  869. pthread_mutex_unlock(&s->mutex);
  870. return err;
  871. } else if(nonblock) {
  872. pthread_mutex_unlock(&s->mutex);
  873. return AVERROR(EAGAIN);
  874. }
  875. else {
  876. /* FIXME: using the monotonic clock would be better,
  877. but it does not exist on all supported platforms. */
  878. int64_t t = av_gettime() + 100000;
  879. struct timespec tv = { .tv_sec = t / 1000000,
  880. .tv_nsec = (t % 1000000) * 1000 };
  881. int err = pthread_cond_timedwait(&s->cond, &s->mutex, &tv);
  882. if (err) {
  883. pthread_mutex_unlock(&s->mutex);
  884. return AVERROR(err == ETIMEDOUT ? EAGAIN : err);
  885. }
  886. nonblock = 1;
  887. }
  888. } while( 1);
  889. }
  890. #endif
  891. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  892. ret = ff_network_wait_fd(s->udp_fd, 0);
  893. if (ret < 0)
  894. return ret;
  895. }
  896. ret = recvfrom(s->udp_fd, buf, size, 0, (struct sockaddr *)&addr, &addr_len);
  897. if (ret < 0)
  898. return ff_neterrno();
  899. if (ff_ip_check_source_lists(&addr, &s->filters))
  900. return AVERROR(EINTR);
  901. return ret;
  902. }
  903. static int udp_write(URLContext *h, const uint8_t *buf, int size)
  904. {
  905. UDPContext *s = h->priv_data;
  906. int ret;
  907. #if HAVE_PTHREAD_CANCEL
  908. if (s->fifo) {
  909. uint8_t tmp[4];
  910. pthread_mutex_lock(&s->mutex);
  911. /*
  912. Return error if last tx failed.
  913. Here we can't know on which packet error was, but it needs to know that error exists.
  914. */
  915. if (s->circular_buffer_error<0) {
  916. int err=s->circular_buffer_error;
  917. pthread_mutex_unlock(&s->mutex);
  918. return err;
  919. }
  920. if(av_fifo_space(s->fifo) < size + 4) {
  921. /* What about a partial packet tx ? */
  922. pthread_mutex_unlock(&s->mutex);
  923. return AVERROR(ENOMEM);
  924. }
  925. AV_WL32(tmp, size);
  926. av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
  927. av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
  928. pthread_cond_signal(&s->cond);
  929. pthread_mutex_unlock(&s->mutex);
  930. return size;
  931. }
  932. #endif
  933. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  934. ret = ff_network_wait_fd(s->udp_fd, 1);
  935. if (ret < 0)
  936. return ret;
  937. }
  938. if (!s->is_connected) {
  939. ret = sendto (s->udp_fd, buf, size, 0,
  940. (struct sockaddr *) &s->dest_addr,
  941. s->dest_addr_len);
  942. } else
  943. ret = send(s->udp_fd, buf, size, 0);
  944. return ret < 0 ? ff_neterrno() : ret;
  945. }
  946. static int udp_close(URLContext *h)
  947. {
  948. UDPContext *s = h->priv_data;
  949. #if HAVE_PTHREAD_CANCEL
  950. // Request close once writing is finished
  951. if (s->thread_started && !(h->flags & AVIO_FLAG_READ)) {
  952. pthread_mutex_lock(&s->mutex);
  953. s->close_req = 1;
  954. pthread_cond_signal(&s->cond);
  955. pthread_mutex_unlock(&s->mutex);
  956. }
  957. #endif
  958. if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
  959. udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage);
  960. #if HAVE_PTHREAD_CANCEL
  961. if (s->thread_started) {
  962. int ret;
  963. // Cancel only read, as write has been signaled as success to the user
  964. if (h->flags & AVIO_FLAG_READ) {
  965. #ifdef _WIN32
  966. /* recvfrom() is not a cancellation point for win32, so we shutdown
  967. * the socket and abort pending IO, subsequent recvfrom() calls
  968. * will fail with WSAESHUTDOWN causing the thread to exit. */
  969. shutdown(s->udp_fd, SD_RECEIVE);
  970. CancelIoEx((HANDLE)(SOCKET)s->udp_fd, NULL);
  971. #else
  972. pthread_cancel(s->circular_buffer_thread);
  973. #endif
  974. }
  975. ret = pthread_join(s->circular_buffer_thread, NULL);
  976. if (ret != 0)
  977. av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
  978. pthread_mutex_destroy(&s->mutex);
  979. pthread_cond_destroy(&s->cond);
  980. }
  981. #endif
  982. closesocket(s->udp_fd);
  983. av_fifo_freep(&s->fifo);
  984. ff_ip_reset_filters(&s->filters);
  985. return 0;
  986. }
  987. const URLProtocol ff_udp_protocol = {
  988. .name = "udp",
  989. .url_open = udp_open,
  990. .url_read = udp_read,
  991. .url_write = udp_write,
  992. .url_close = udp_close,
  993. .url_get_file_handle = udp_get_file_handle,
  994. .priv_data_size = sizeof(UDPContext),
  995. .priv_data_class = &udp_class,
  996. .flags = URL_PROTOCOL_FLAG_NETWORK,
  997. };
  998. const URLProtocol ff_udplite_protocol = {
  999. .name = "udplite",
  1000. .url_open = udplite_open,
  1001. .url_read = udp_read,
  1002. .url_write = udp_write,
  1003. .url_close = udp_close,
  1004. .url_get_file_handle = udp_get_file_handle,
  1005. .priv_data_size = sizeof(UDPContext),
  1006. .priv_data_class = &udplite_context_class,
  1007. .flags = URL_PROTOCOL_FLAG_NETWORK,
  1008. };