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.

547 lines
20KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * Haivision Open SRT (Secure Reliable Transport) protocol
  21. */
  22. #include <srt/srt.h>
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/parseutils.h"
  26. #include "libavutil/time.h"
  27. #include "avformat.h"
  28. #include "internal.h"
  29. #include "network.h"
  30. #include "os_support.h"
  31. #include "url.h"
  32. enum SRTMode {
  33. SRT_MODE_CALLER = 0,
  34. SRT_MODE_LISTENER = 1,
  35. SRT_MODE_RENDEZVOUS = 2
  36. };
  37. typedef struct SRTContext {
  38. const AVClass *class;
  39. int fd;
  40. int eid;
  41. int64_t rw_timeout;
  42. int64_t listen_timeout;
  43. int recv_buffer_size;
  44. int send_buffer_size;
  45. int64_t maxbw;
  46. int pbkeylen;
  47. char *passphrase;
  48. int mss;
  49. int ffs;
  50. int ipttl;
  51. int iptos;
  52. int64_t inputbw;
  53. int oheadbw;
  54. int64_t tsbpddelay;
  55. int tlpktdrop;
  56. int nakreport;
  57. int64_t connect_timeout;
  58. enum SRTMode mode;
  59. } SRTContext;
  60. #define D AV_OPT_FLAG_DECODING_PARAM
  61. #define E AV_OPT_FLAG_ENCODING_PARAM
  62. #define OFFSET(x) offsetof(SRTContext, x)
  63. static const AVOption libsrt_options[] = {
  64. { "rw_timeout", "Timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  65. { "listen_timeout", "Connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  66. { "send_buffer_size", "Socket send buffer size (in bytes)", OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  67. { "recv_buffer_size", "Socket receive buffer size (in bytes)", OFFSET(recv_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  68. { "maxbw", "Maximum bandwidth (bytes per second) that the connection can use", OFFSET(maxbw), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  69. { "pbkeylen", "Crypto key len in bytes {16,24,32} Default: 16 (128-bit)", OFFSET(pbkeylen), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32, .flags = D|E },
  70. { "passphrase", "Crypto PBKDF2 Passphrase size[0,10..64] 0:disable crypto", OFFSET(passphrase), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  71. { "mss", "The Maximum Segment Size", OFFSET(mss), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1500, .flags = D|E },
  72. { "ffs", "Flight flag size (window size) (in bytes)", OFFSET(ffs), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  73. { "ipttl", "IP Time To Live", OFFSET(ipttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, .flags = D|E },
  74. { "iptos", "IP Type of Service", OFFSET(iptos), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, .flags = D|E },
  75. { "inputbw", "Estimated input stream rate", OFFSET(inputbw), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  76. { "oheadbw", "MaxBW ceiling based on % over input stream rate", OFFSET(oheadbw), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = D|E },
  77. { "tsbpddelay", "TsbPd receiver delay to absorb burst of missed packet retransmission", OFFSET(tsbpddelay), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  78. { "tlpktdrop", "Enable receiver pkt drop", OFFSET(tlpktdrop), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
  79. { "nakreport", "Enable receiver to send periodic NAK reports", OFFSET(nakreport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
  80. { "connect_timeout", "Connect timeout. Caller default: 3000, rendezvous (x 10)", OFFSET(connect_timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  81. { "mode", "Connection mode (caller, listener, rendezvous)", OFFSET(mode), AV_OPT_TYPE_INT, { .i64 = SRT_MODE_CALLER }, SRT_MODE_CALLER, SRT_MODE_RENDEZVOUS, .flags = D|E, "mode" },
  82. { "caller", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_MODE_CALLER }, INT_MIN, INT_MAX, .flags = D|E, "mode" },
  83. { "listener", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_MODE_LISTENER }, INT_MIN, INT_MAX, .flags = D|E, "mode" },
  84. { "rendezvous", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_MODE_RENDEZVOUS }, INT_MIN, INT_MAX, .flags = D|E, "mode" },
  85. { NULL }
  86. };
  87. static int libsrt_neterrno(URLContext *h)
  88. {
  89. int err = srt_getlasterror(NULL);
  90. av_log(h, AV_LOG_ERROR, "%s\n", srt_getlasterror_str());
  91. if (err == SRT_EASYNCRCV)
  92. return AVERROR(EAGAIN);
  93. return AVERROR_UNKNOWN;
  94. }
  95. static int libsrt_socket_nonblock(int socket, int enable)
  96. {
  97. int ret = srt_setsockopt(socket, 0, SRTO_SNDSYN, &enable, sizeof(enable));
  98. if (ret < 0)
  99. return ret;
  100. return srt_setsockopt(socket, 0, SRTO_RCVSYN, &enable, sizeof(enable));
  101. }
  102. static int libsrt_network_wait_fd(URLContext *h, int eid, int fd, int write)
  103. {
  104. int ret, len = 1;
  105. int modes = write ? SRT_EPOLL_OUT : SRT_EPOLL_IN;
  106. SRTSOCKET ready[1];
  107. if (srt_epoll_add_usock(eid, fd, &modes) < 0)
  108. return libsrt_neterrno(h);
  109. if (write) {
  110. ret = srt_epoll_wait(eid, 0, 0, ready, &len, POLLING_TIME, 0, 0, 0, 0);
  111. } else {
  112. ret = srt_epoll_wait(eid, ready, &len, 0, 0, POLLING_TIME, 0, 0, 0, 0);
  113. }
  114. if (ret < 0) {
  115. if (srt_getlasterror(NULL) == SRT_ETIMEOUT)
  116. ret = AVERROR(EAGAIN);
  117. else
  118. ret = libsrt_neterrno(h);
  119. } else {
  120. ret = 0;
  121. }
  122. if (srt_epoll_remove_usock(eid, fd) < 0)
  123. return libsrt_neterrno(h);
  124. return ret;
  125. }
  126. /* TODO de-duplicate code from ff_network_wait_fd_timeout() */
  127. static int libsrt_network_wait_fd_timeout(URLContext *h, int eid, int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
  128. {
  129. int ret;
  130. int64_t wait_start = 0;
  131. while (1) {
  132. if (ff_check_interrupt(int_cb))
  133. return AVERROR_EXIT;
  134. ret = libsrt_network_wait_fd(h, eid, fd, write);
  135. if (ret != AVERROR(EAGAIN))
  136. return ret;
  137. if (timeout > 0) {
  138. if (!wait_start)
  139. wait_start = av_gettime_relative();
  140. else if (av_gettime_relative() - wait_start > timeout)
  141. return AVERROR(ETIMEDOUT);
  142. }
  143. }
  144. }
  145. static int libsrt_listen(int eid, int fd, const struct sockaddr *addr, socklen_t addrlen, URLContext *h, int timeout)
  146. {
  147. int ret;
  148. int reuse = 1;
  149. if (srt_setsockopt(fd, SOL_SOCKET, SRTO_REUSEADDR, &reuse, sizeof(reuse))) {
  150. av_log(h, AV_LOG_WARNING, "setsockopt(SRTO_REUSEADDR) failed\n");
  151. }
  152. ret = srt_bind(fd, addr, addrlen);
  153. if (ret)
  154. return libsrt_neterrno(h);
  155. ret = srt_listen(fd, 1);
  156. if (ret)
  157. return libsrt_neterrno(h);
  158. while ((ret = libsrt_network_wait_fd_timeout(h, eid, fd, 1, timeout, &h->interrupt_callback))) {
  159. switch (ret) {
  160. case AVERROR(ETIMEDOUT):
  161. continue;
  162. default:
  163. return ret;
  164. }
  165. }
  166. ret = srt_accept(fd, NULL, NULL);
  167. if (ret < 0)
  168. return libsrt_neterrno(h);
  169. if (libsrt_socket_nonblock(ret, 1) < 0)
  170. av_log(h, AV_LOG_DEBUG, "libsrt_socket_nonblock failed\n");
  171. return ret;
  172. }
  173. static int libsrt_listen_connect(int eid, int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
  174. {
  175. int ret;
  176. if (libsrt_socket_nonblock(fd, 1) < 0)
  177. av_log(h, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
  178. while ((ret = srt_connect(fd, addr, addrlen))) {
  179. ret = libsrt_neterrno(h);
  180. switch (ret) {
  181. case AVERROR(EINTR):
  182. if (ff_check_interrupt(&h->interrupt_callback))
  183. return AVERROR_EXIT;
  184. continue;
  185. case AVERROR(EINPROGRESS):
  186. case AVERROR(EAGAIN):
  187. ret = libsrt_network_wait_fd_timeout(h, eid, fd, 1, timeout, &h->interrupt_callback);
  188. if (ret < 0)
  189. return ret;
  190. ret = srt_getlasterror(NULL);
  191. srt_clearlasterror();
  192. if (ret != 0) {
  193. char buf[128];
  194. ret = AVERROR(ret);
  195. av_strerror(ret, buf, sizeof(buf));
  196. if (will_try_next)
  197. av_log(h, AV_LOG_WARNING,
  198. "Connection to %s failed (%s), trying next address\n",
  199. h->filename, buf);
  200. else
  201. av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
  202. h->filename, buf);
  203. }
  204. default:
  205. return ret;
  206. }
  207. }
  208. return ret;
  209. }
  210. static int libsrt_setsockopt(URLContext *h, int fd, SRT_SOCKOPT optname, const char * optnamestr, const void * optval, int optlen)
  211. {
  212. if (srt_setsockopt(fd, 0, optname, optval, optlen) < 0) {
  213. av_log(h, AV_LOG_ERROR, "failed to set option %s on socket: %s\n", optnamestr, srt_getlasterror_str());
  214. return AVERROR(EIO);
  215. }
  216. return 0;
  217. }
  218. /* - The "POST" options can be altered any time on a connected socket.
  219. They MAY have also some meaning when set prior to connecting; such
  220. option is SRTO_RCVSYN, which makes connect/accept call asynchronous.
  221. Because of that this option is treated special way in this app. */
  222. static int libsrt_set_options_post(URLContext *h, int fd)
  223. {
  224. SRTContext *s = h->priv_data;
  225. if ((s->inputbw >= 0 && libsrt_setsockopt(h, fd, SRTO_INPUTBW, "SRTO_INPUTBW", &s->inputbw, sizeof(s->inputbw)) < 0) ||
  226. (s->oheadbw >= 0 && libsrt_setsockopt(h, fd, SRTO_OHEADBW, "SRTO_OHEADBW", &s->oheadbw, sizeof(s->oheadbw)) < 0)) {
  227. return AVERROR(EIO);
  228. }
  229. return 0;
  230. }
  231. /* - The "PRE" options must be set prior to connecting and can't be altered
  232. on a connected socket, however if set on a listening socket, they are
  233. derived by accept-ed socket. */
  234. static int libsrt_set_options_pre(URLContext *h, int fd)
  235. {
  236. SRTContext *s = h->priv_data;
  237. int yes = 1;
  238. int tsbpddelay = s->tsbpddelay / 1000;
  239. int connect_timeout = s->connect_timeout;
  240. if ((s->mode == SRT_MODE_RENDEZVOUS && libsrt_setsockopt(h, fd, SRTO_RENDEZVOUS, "SRTO_RENDEZVOUS", &yes, sizeof(yes)) < 0) ||
  241. (s->maxbw >= 0 && libsrt_setsockopt(h, fd, SRTO_MAXBW, "SRTO_MAXBW", &s->maxbw, sizeof(s->maxbw)) < 0) ||
  242. (s->pbkeylen >= 0 && libsrt_setsockopt(h, fd, SRTO_PBKEYLEN, "SRTO_PBKEYLEN", &s->pbkeylen, sizeof(s->pbkeylen)) < 0) ||
  243. (s->passphrase && libsrt_setsockopt(h, fd, SRTO_PASSPHRASE, "SRTO_PASSPHRASE", &s->passphrase, sizeof(s->passphrase)) < 0) ||
  244. (s->mss >= 0 && libsrt_setsockopt(h, fd, SRTO_MSS, "SRTO_MMS", &s->mss, sizeof(s->mss)) < 0) ||
  245. (s->ffs >= 0 && libsrt_setsockopt(h, fd, SRTO_FC, "SRTO_FC", &s->ffs, sizeof(s->ffs)) < 0) ||
  246. (s->ipttl >= 0 && libsrt_setsockopt(h, fd, SRTO_IPTTL, "SRTO_UPTTL", &s->ipttl, sizeof(s->ipttl)) < 0) ||
  247. (s->iptos >= 0 && libsrt_setsockopt(h, fd, SRTO_IPTOS, "SRTO_IPTOS", &s->iptos, sizeof(s->iptos)) < 0) ||
  248. (tsbpddelay >= 0 && libsrt_setsockopt(h, fd, SRTO_TSBPDDELAY, "SRTO_TSBPDELAY", &tsbpddelay, sizeof(tsbpddelay)) < 0) ||
  249. (s->tlpktdrop >= 0 && libsrt_setsockopt(h, fd, SRTO_TLPKTDROP, "SRTO_TLPKDROP", &s->tlpktdrop, sizeof(s->tlpktdrop)) < 0) ||
  250. (s->nakreport >= 0 && libsrt_setsockopt(h, fd, SRTO_NAKREPORT, "SRTO_NAKREPORT", &s->nakreport, sizeof(s->nakreport)) < 0) ||
  251. (connect_timeout >= 0 && libsrt_setsockopt(h, fd, SRTO_CONNTIMEO, "SRTO_CONNTIMEO", &connect_timeout, sizeof(connect_timeout)) <0 )) {
  252. return AVERROR(EIO);
  253. }
  254. return 0;
  255. }
  256. static int libsrt_setup(URLContext *h, const char *uri, int flags)
  257. {
  258. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  259. int port, fd = -1;
  260. SRTContext *s = h->priv_data;
  261. const char *p;
  262. char buf[256];
  263. int ret;
  264. char hostname[1024],proto[1024],path[1024];
  265. char portstr[10];
  266. int open_timeout = 5000000;
  267. int eid;
  268. eid = srt_epoll_create();
  269. if (eid < 0)
  270. return libsrt_neterrno(h);
  271. s->eid = eid;
  272. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  273. &port, path, sizeof(path), uri);
  274. if (strcmp(proto, "srt"))
  275. return AVERROR(EINVAL);
  276. if (port <= 0 || port >= 65536) {
  277. av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
  278. return AVERROR(EINVAL);
  279. }
  280. p = strchr(uri, '?');
  281. if (p) {
  282. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  283. s->rw_timeout = strtol(buf, NULL, 10);
  284. }
  285. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  286. s->listen_timeout = strtol(buf, NULL, 10);
  287. }
  288. }
  289. if (s->rw_timeout >= 0) {
  290. open_timeout = h->rw_timeout = s->rw_timeout;
  291. }
  292. hints.ai_family = AF_UNSPEC;
  293. hints.ai_socktype = SOCK_DGRAM;
  294. snprintf(portstr, sizeof(portstr), "%d", port);
  295. if (s->mode == SRT_MODE_LISTENER)
  296. hints.ai_flags |= AI_PASSIVE;
  297. ret = getaddrinfo(hostname[0] ? hostname : NULL, portstr, &hints, &ai);
  298. if (ret) {
  299. av_log(h, AV_LOG_ERROR,
  300. "Failed to resolve hostname %s: %s\n",
  301. hostname, gai_strerror(ret));
  302. return AVERROR(EIO);
  303. }
  304. cur_ai = ai;
  305. restart:
  306. fd = srt_socket(cur_ai->ai_family, cur_ai->ai_socktype, 0);
  307. if (fd < 0) {
  308. ret = libsrt_neterrno(h);
  309. goto fail;
  310. }
  311. if ((ret = libsrt_set_options_pre(h, fd)) < 0) {
  312. goto fail;
  313. }
  314. /* Set the socket's send or receive buffer sizes, if specified.
  315. If unspecified or setting fails, system default is used. */
  316. if (s->recv_buffer_size > 0) {
  317. srt_setsockopt(fd, SOL_SOCKET, SRTO_UDP_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
  318. }
  319. if (s->send_buffer_size > 0) {
  320. srt_setsockopt(fd, SOL_SOCKET, SRTO_UDP_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
  321. }
  322. if (s->mode == SRT_MODE_LISTENER) {
  323. // multi-client
  324. if ((ret = libsrt_listen(s->eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen, h, open_timeout / 1000)) < 0)
  325. goto fail1;
  326. fd = ret;
  327. } else {
  328. if (s->mode == SRT_MODE_RENDEZVOUS) {
  329. ret = srt_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  330. if (ret)
  331. goto fail1;
  332. }
  333. if ((ret = libsrt_listen_connect(s->eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  334. open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
  335. if (ret == AVERROR_EXIT)
  336. goto fail1;
  337. else
  338. goto fail;
  339. }
  340. }
  341. if ((ret = libsrt_set_options_post(h, fd)) < 0) {
  342. goto fail;
  343. }
  344. h->is_streamed = 1;
  345. s->fd = fd;
  346. freeaddrinfo(ai);
  347. return 0;
  348. fail:
  349. if (cur_ai->ai_next) {
  350. /* Retry with the next sockaddr */
  351. cur_ai = cur_ai->ai_next;
  352. if (fd >= 0)
  353. srt_close(fd);
  354. ret = 0;
  355. goto restart;
  356. }
  357. fail1:
  358. if (fd >= 0)
  359. srt_close(fd);
  360. freeaddrinfo(ai);
  361. return ret;
  362. }
  363. static int libsrt_open(URLContext *h, const char *uri, int flags)
  364. {
  365. SRTContext *s = h->priv_data;
  366. const char * p;
  367. char buf[256];
  368. if (srt_startup() < 0) {
  369. return AVERROR_UNKNOWN;
  370. }
  371. /* SRT options (srt/srt.h) */
  372. p = strchr(uri, '?');
  373. if (p) {
  374. if (av_find_info_tag(buf, sizeof(buf), "maxbw", p)) {
  375. s->maxbw = strtoll(buf, NULL, 0);
  376. }
  377. if (av_find_info_tag(buf, sizeof(buf), "pbkeylen", p)) {
  378. s->pbkeylen = strtol(buf, NULL, 10);
  379. }
  380. if (av_find_info_tag(buf, sizeof(buf), "passphrase", p)) {
  381. s->passphrase = av_strndup(buf, strlen(buf));
  382. }
  383. if (av_find_info_tag(buf, sizeof(buf), "mss", p)) {
  384. s->mss = strtol(buf, NULL, 10);
  385. }
  386. if (av_find_info_tag(buf, sizeof(buf), "ffs", p)) {
  387. s->ffs = strtol(buf, NULL, 10);
  388. }
  389. if (av_find_info_tag(buf, sizeof(buf), "ipttl", p)) {
  390. s->ipttl = strtol(buf, NULL, 10);
  391. }
  392. if (av_find_info_tag(buf, sizeof(buf), "iptos", p)) {
  393. s->iptos = strtol(buf, NULL, 10);
  394. }
  395. if (av_find_info_tag(buf, sizeof(buf), "inputbw", p)) {
  396. s->inputbw = strtoll(buf, NULL, 10);
  397. }
  398. if (av_find_info_tag(buf, sizeof(buf), "oheadbw", p)) {
  399. s->oheadbw = strtoll(buf, NULL, 10);
  400. }
  401. if (av_find_info_tag(buf, sizeof(buf), "tsbpddelay", p)) {
  402. s->tsbpddelay = strtol(buf, NULL, 10);
  403. }
  404. if (av_find_info_tag(buf, sizeof(buf), "tlpktdrop", p)) {
  405. s->tlpktdrop = strtol(buf, NULL, 10);
  406. }
  407. if (av_find_info_tag(buf, sizeof(buf), "nakreport", p)) {
  408. s->nakreport = strtol(buf, NULL, 10);
  409. }
  410. if (av_find_info_tag(buf, sizeof(buf), "connect_timeout", p)) {
  411. s->connect_timeout = strtol(buf, NULL, 10);
  412. }
  413. if (av_find_info_tag(buf, sizeof(buf), "mode", p)) {
  414. if (!strcmp(buf, "caller")) {
  415. s->mode = SRT_MODE_CALLER;
  416. } else if (!strcmp(buf, "listener")) {
  417. s->mode = SRT_MODE_LISTENER;
  418. } else if (!strcmp(buf, "rendezvous")) {
  419. s->mode = SRT_MODE_RENDEZVOUS;
  420. } else {
  421. return AVERROR(EIO);
  422. }
  423. }
  424. }
  425. return libsrt_setup(h, uri, flags);
  426. }
  427. static int libsrt_read(URLContext *h, uint8_t *buf, int size)
  428. {
  429. SRTContext *s = h->priv_data;
  430. int ret;
  431. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  432. ret = libsrt_network_wait_fd_timeout(h, s->eid, s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  433. if (ret)
  434. return ret;
  435. }
  436. ret = srt_recvmsg(s->fd, buf, size);
  437. if (ret < 0) {
  438. ret = libsrt_neterrno(h);
  439. }
  440. return ret;
  441. }
  442. static int libsrt_write(URLContext *h, const uint8_t *buf, int size)
  443. {
  444. SRTContext *s = h->priv_data;
  445. int ret;
  446. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  447. ret = libsrt_network_wait_fd_timeout(h, s->eid, s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  448. if (ret)
  449. return ret;
  450. }
  451. ret = srt_sendmsg(s->fd, buf, size, -1, 0);
  452. if (ret < 0) {
  453. ret = libsrt_neterrno(h);
  454. }
  455. return ret;
  456. }
  457. static int libsrt_close(URLContext *h)
  458. {
  459. SRTContext *s = h->priv_data;
  460. srt_close(s->fd);
  461. srt_epoll_release(s->eid);
  462. srt_cleanup();
  463. return 0;
  464. }
  465. static int libsrt_get_file_handle(URLContext *h)
  466. {
  467. SRTContext *s = h->priv_data;
  468. return s->fd;
  469. }
  470. static const AVClass libsrt_class = {
  471. .class_name = "libsrt",
  472. .item_name = av_default_item_name,
  473. .option = libsrt_options,
  474. .version = LIBAVUTIL_VERSION_INT,
  475. };
  476. const URLProtocol ff_libsrt_protocol = {
  477. .name = "srt",
  478. .url_open = libsrt_open,
  479. .url_read = libsrt_read,
  480. .url_write = libsrt_write,
  481. .url_close = libsrt_close,
  482. .url_get_file_handle = libsrt_get_file_handle,
  483. .priv_data_size = sizeof(SRTContext),
  484. .flags = URL_PROTOCOL_FLAG_NETWORK,
  485. .priv_data_class = &libsrt_class,
  486. };