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.

677 lines
28KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. /* This is for MPEG-TS and it's a default SRTO_PAYLOADSIZE for SRTT_LIVE (8 TS packets) */
  33. #ifndef SRT_LIVE_DEFAULT_PAYLOAD_SIZE
  34. #define SRT_LIVE_DEFAULT_PAYLOAD_SIZE 1316
  35. #endif
  36. /* This is the maximum payload size for Live mode, should you have a different payload type than MPEG-TS */
  37. #ifndef SRT_LIVE_MAX_PAYLOAD_SIZE
  38. #define SRT_LIVE_MAX_PAYLOAD_SIZE 1456
  39. #endif
  40. enum SRTMode {
  41. SRT_MODE_CALLER = 0,
  42. SRT_MODE_LISTENER = 1,
  43. SRT_MODE_RENDEZVOUS = 2
  44. };
  45. typedef struct SRTContext {
  46. const AVClass *class;
  47. int fd;
  48. int eid;
  49. int64_t rw_timeout;
  50. int64_t listen_timeout;
  51. int recv_buffer_size;
  52. int send_buffer_size;
  53. int64_t maxbw;
  54. int pbkeylen;
  55. char *passphrase;
  56. int mss;
  57. int ffs;
  58. int ipttl;
  59. int iptos;
  60. int64_t inputbw;
  61. int oheadbw;
  62. int64_t latency;
  63. int tlpktdrop;
  64. int nakreport;
  65. int64_t connect_timeout;
  66. int payload_size;
  67. int64_t rcvlatency;
  68. int64_t peerlatency;
  69. enum SRTMode mode;
  70. int sndbuf;
  71. int rcvbuf;
  72. int lossmaxttl;
  73. int minversion;
  74. char *streamid;
  75. char *smoother;
  76. int messageapi;
  77. SRT_TRANSTYPE transtype;
  78. } SRTContext;
  79. #define D AV_OPT_FLAG_DECODING_PARAM
  80. #define E AV_OPT_FLAG_ENCODING_PARAM
  81. #define OFFSET(x) offsetof(SRTContext, x)
  82. static const AVOption libsrt_options[] = {
  83. { "rw_timeout", "Timeout of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  84. { "listen_timeout", "Connection awaiting timeout", OFFSET(listen_timeout), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  85. { "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 },
  86. { "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 },
  87. { "pkt_size", "Maximum SRT packet size", OFFSET(payload_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, SRT_LIVE_MAX_PAYLOAD_SIZE, .flags = D|E, "payload_size" },
  88. { "payload_size", "Maximum SRT packet size", OFFSET(payload_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, SRT_LIVE_MAX_PAYLOAD_SIZE, .flags = D|E, "payload_size" },
  89. { "ts_size", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_LIVE_DEFAULT_PAYLOAD_SIZE }, INT_MIN, INT_MAX, .flags = D|E, "payload_size" },
  90. { "max_size", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_LIVE_MAX_PAYLOAD_SIZE }, INT_MIN, INT_MAX, .flags = D|E, "payload_size" },
  91. { "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 },
  92. { "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 },
  93. { "passphrase", "Crypto PBKDF2 Passphrase size[0,10..64] 0:disable crypto", OFFSET(passphrase), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  94. { "mss", "The Maximum Segment Size", OFFSET(mss), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1500, .flags = D|E },
  95. { "ffs", "Flight flag size (window size) (in bytes)", OFFSET(ffs), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  96. { "ipttl", "IP Time To Live", OFFSET(ipttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, .flags = D|E },
  97. { "iptos", "IP Type of Service", OFFSET(iptos), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, .flags = D|E },
  98. { "inputbw", "Estimated input stream rate", OFFSET(inputbw), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  99. { "oheadbw", "MaxBW ceiling based on % over input stream rate", OFFSET(oheadbw), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, .flags = D|E },
  100. { "latency", "receiver delay to absorb bursts of missed packet retransmissions", OFFSET(latency), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  101. { "tsbpddelay", "deprecated, same effect as latency option", OFFSET(latency), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  102. { "rcvlatency", "receive latency", OFFSET(rcvlatency), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  103. { "peerlatency", "peer latency", OFFSET(peerlatency), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, .flags = D|E },
  104. { "tlpktdrop", "Enable receiver pkt drop", OFFSET(tlpktdrop), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
  105. { "nakreport", "Enable receiver to send periodic NAK reports", OFFSET(nakreport), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
  106. { "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 },
  107. { "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" },
  108. { "caller", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_MODE_CALLER }, INT_MIN, INT_MAX, .flags = D|E, "mode" },
  109. { "listener", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_MODE_LISTENER }, INT_MIN, INT_MAX, .flags = D|E, "mode" },
  110. { "rendezvous", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRT_MODE_RENDEZVOUS }, INT_MIN, INT_MAX, .flags = D|E, "mode" },
  111. { "sndbuf", "Send buffer size (in bytes)", OFFSET(sndbuf), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  112. { "rcvbuf", "Receive buffer size (in bytes)", OFFSET(rcvbuf), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  113. { "lossmaxttl", "Maximum possible packet reorder tolerance", OFFSET(lossmaxttl), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  114. { "minversion", "The minimum SRT version that is required from the peer", OFFSET(minversion), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
  115. { "streamid", "A string of up to 512 characters that an Initiator can pass to a Responder", OFFSET(streamid), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  116. { "smoother", "The type of Smoother used for the transmission for that socket", OFFSET(smoother), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
  117. { "messageapi", "Enable message API", OFFSET(messageapi), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
  118. { "transtype", "The transmission type for the socket", OFFSET(transtype), AV_OPT_TYPE_INT, { .i64 = SRTT_INVALID }, SRTT_LIVE, SRTT_INVALID, .flags = D|E, "transtype" },
  119. { "live", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRTT_LIVE }, INT_MIN, INT_MAX, .flags = D|E, "transtype" },
  120. { "file", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = SRTT_FILE }, INT_MIN, INT_MAX, .flags = D|E, "transtype" },
  121. { NULL }
  122. };
  123. static int libsrt_neterrno(URLContext *h)
  124. {
  125. int err = srt_getlasterror(NULL);
  126. av_log(h, AV_LOG_ERROR, "%s\n", srt_getlasterror_str());
  127. if (err == SRT_EASYNCRCV)
  128. return AVERROR(EAGAIN);
  129. return AVERROR_UNKNOWN;
  130. }
  131. static int libsrt_socket_nonblock(int socket, int enable)
  132. {
  133. int ret = srt_setsockopt(socket, 0, SRTO_SNDSYN, &enable, sizeof(enable));
  134. if (ret < 0)
  135. return ret;
  136. return srt_setsockopt(socket, 0, SRTO_RCVSYN, &enable, sizeof(enable));
  137. }
  138. static int libsrt_network_wait_fd(URLContext *h, int eid, int fd, int write)
  139. {
  140. int ret, len = 1;
  141. int modes = write ? SRT_EPOLL_OUT : SRT_EPOLL_IN;
  142. SRTSOCKET ready[1];
  143. if (srt_epoll_add_usock(eid, fd, &modes) < 0)
  144. return libsrt_neterrno(h);
  145. if (write) {
  146. ret = srt_epoll_wait(eid, 0, 0, ready, &len, POLLING_TIME, 0, 0, 0, 0);
  147. } else {
  148. ret = srt_epoll_wait(eid, ready, &len, 0, 0, POLLING_TIME, 0, 0, 0, 0);
  149. }
  150. if (ret < 0) {
  151. if (srt_getlasterror(NULL) == SRT_ETIMEOUT)
  152. ret = AVERROR(EAGAIN);
  153. else
  154. ret = libsrt_neterrno(h);
  155. } else {
  156. ret = 0;
  157. }
  158. if (srt_epoll_remove_usock(eid, fd) < 0)
  159. return libsrt_neterrno(h);
  160. return ret;
  161. }
  162. /* TODO de-duplicate code from ff_network_wait_fd_timeout() */
  163. static int libsrt_network_wait_fd_timeout(URLContext *h, int eid, int fd, int write, int64_t timeout, AVIOInterruptCB *int_cb)
  164. {
  165. int ret;
  166. int64_t wait_start = 0;
  167. while (1) {
  168. if (ff_check_interrupt(int_cb))
  169. return AVERROR_EXIT;
  170. ret = libsrt_network_wait_fd(h, eid, fd, write);
  171. if (ret != AVERROR(EAGAIN))
  172. return ret;
  173. if (timeout > 0) {
  174. if (!wait_start)
  175. wait_start = av_gettime_relative();
  176. else if (av_gettime_relative() - wait_start > timeout)
  177. return AVERROR(ETIMEDOUT);
  178. }
  179. }
  180. }
  181. static int libsrt_listen(int eid, int fd, const struct sockaddr *addr, socklen_t addrlen, URLContext *h, int timeout)
  182. {
  183. int ret;
  184. int reuse = 1;
  185. if (srt_setsockopt(fd, SOL_SOCKET, SRTO_REUSEADDR, &reuse, sizeof(reuse))) {
  186. av_log(h, AV_LOG_WARNING, "setsockopt(SRTO_REUSEADDR) failed\n");
  187. }
  188. ret = srt_bind(fd, addr, addrlen);
  189. if (ret)
  190. return libsrt_neterrno(h);
  191. ret = srt_listen(fd, 1);
  192. if (ret)
  193. return libsrt_neterrno(h);
  194. while ((ret = libsrt_network_wait_fd_timeout(h, eid, fd, 1, timeout, &h->interrupt_callback))) {
  195. switch (ret) {
  196. case AVERROR(ETIMEDOUT):
  197. continue;
  198. default:
  199. return ret;
  200. }
  201. }
  202. ret = srt_accept(fd, NULL, NULL);
  203. if (ret < 0)
  204. return libsrt_neterrno(h);
  205. if (libsrt_socket_nonblock(ret, 1) < 0)
  206. av_log(h, AV_LOG_DEBUG, "libsrt_socket_nonblock failed\n");
  207. return ret;
  208. }
  209. static int libsrt_listen_connect(int eid, int fd, const struct sockaddr *addr, socklen_t addrlen, int timeout, URLContext *h, int will_try_next)
  210. {
  211. int ret;
  212. if (libsrt_socket_nonblock(fd, 1) < 0)
  213. av_log(h, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
  214. while ((ret = srt_connect(fd, addr, addrlen))) {
  215. ret = libsrt_neterrno(h);
  216. switch (ret) {
  217. case AVERROR(EINTR):
  218. if (ff_check_interrupt(&h->interrupt_callback))
  219. return AVERROR_EXIT;
  220. continue;
  221. case AVERROR(EINPROGRESS):
  222. case AVERROR(EAGAIN):
  223. ret = libsrt_network_wait_fd_timeout(h, eid, fd, 1, timeout, &h->interrupt_callback);
  224. if (ret < 0)
  225. return ret;
  226. ret = srt_getlasterror(NULL);
  227. srt_clearlasterror();
  228. if (ret != 0) {
  229. char buf[128];
  230. ret = AVERROR(ret);
  231. av_strerror(ret, buf, sizeof(buf));
  232. if (will_try_next)
  233. av_log(h, AV_LOG_WARNING,
  234. "Connection to %s failed (%s), trying next address\n",
  235. h->filename, buf);
  236. else
  237. av_log(h, AV_LOG_ERROR, "Connection to %s failed: %s\n",
  238. h->filename, buf);
  239. }
  240. default:
  241. return ret;
  242. }
  243. }
  244. return ret;
  245. }
  246. static int libsrt_setsockopt(URLContext *h, int fd, SRT_SOCKOPT optname, const char * optnamestr, const void * optval, int optlen)
  247. {
  248. if (srt_setsockopt(fd, 0, optname, optval, optlen) < 0) {
  249. av_log(h, AV_LOG_ERROR, "failed to set option %s on socket: %s\n", optnamestr, srt_getlasterror_str());
  250. return AVERROR(EIO);
  251. }
  252. return 0;
  253. }
  254. static int libsrt_getsockopt(URLContext *h, int fd, SRT_SOCKOPT optname, const char * optnamestr, void * optval, int * optlen)
  255. {
  256. if (srt_getsockopt(fd, 0, optname, optval, optlen) < 0) {
  257. av_log(h, AV_LOG_ERROR, "failed to get option %s on socket: %s\n", optnamestr, srt_getlasterror_str());
  258. return AVERROR(EIO);
  259. }
  260. return 0;
  261. }
  262. /* - The "POST" options can be altered any time on a connected socket.
  263. They MAY have also some meaning when set prior to connecting; such
  264. option is SRTO_RCVSYN, which makes connect/accept call asynchronous.
  265. Because of that this option is treated special way in this app. */
  266. static int libsrt_set_options_post(URLContext *h, int fd)
  267. {
  268. SRTContext *s = h->priv_data;
  269. if ((s->inputbw >= 0 && libsrt_setsockopt(h, fd, SRTO_INPUTBW, "SRTO_INPUTBW", &s->inputbw, sizeof(s->inputbw)) < 0) ||
  270. (s->oheadbw >= 0 && libsrt_setsockopt(h, fd, SRTO_OHEADBW, "SRTO_OHEADBW", &s->oheadbw, sizeof(s->oheadbw)) < 0)) {
  271. return AVERROR(EIO);
  272. }
  273. return 0;
  274. }
  275. /* - The "PRE" options must be set prior to connecting and can't be altered
  276. on a connected socket, however if set on a listening socket, they are
  277. derived by accept-ed socket. */
  278. static int libsrt_set_options_pre(URLContext *h, int fd)
  279. {
  280. SRTContext *s = h->priv_data;
  281. int yes = 1;
  282. int latency = s->latency / 1000;
  283. int rcvlatency = s->rcvlatency / 1000;
  284. int peerlatency = s->peerlatency / 1000;
  285. int connect_timeout = s->connect_timeout;
  286. if ((s->mode == SRT_MODE_RENDEZVOUS && libsrt_setsockopt(h, fd, SRTO_RENDEZVOUS, "SRTO_RENDEZVOUS", &yes, sizeof(yes)) < 0) ||
  287. (s->transtype != SRTT_INVALID && libsrt_setsockopt(h, fd, SRTO_TRANSTYPE, "SRTO_TRANSTYPE", &s->transtype, sizeof(s->transtype)) < 0) ||
  288. (s->maxbw >= 0 && libsrt_setsockopt(h, fd, SRTO_MAXBW, "SRTO_MAXBW", &s->maxbw, sizeof(s->maxbw)) < 0) ||
  289. (s->pbkeylen >= 0 && libsrt_setsockopt(h, fd, SRTO_PBKEYLEN, "SRTO_PBKEYLEN", &s->pbkeylen, sizeof(s->pbkeylen)) < 0) ||
  290. (s->passphrase && libsrt_setsockopt(h, fd, SRTO_PASSPHRASE, "SRTO_PASSPHRASE", s->passphrase, strlen(s->passphrase)) < 0) ||
  291. (s->mss >= 0 && libsrt_setsockopt(h, fd, SRTO_MSS, "SRTO_MMS", &s->mss, sizeof(s->mss)) < 0) ||
  292. (s->ffs >= 0 && libsrt_setsockopt(h, fd, SRTO_FC, "SRTO_FC", &s->ffs, sizeof(s->ffs)) < 0) ||
  293. (s->ipttl >= 0 && libsrt_setsockopt(h, fd, SRTO_IPTTL, "SRTO_UPTTL", &s->ipttl, sizeof(s->ipttl)) < 0) ||
  294. (s->iptos >= 0 && libsrt_setsockopt(h, fd, SRTO_IPTOS, "SRTO_IPTOS", &s->iptos, sizeof(s->iptos)) < 0) ||
  295. (s->latency >= 0 && libsrt_setsockopt(h, fd, SRTO_LATENCY, "SRTO_LATENCY", &latency, sizeof(latency)) < 0) ||
  296. (s->rcvlatency >= 0 && libsrt_setsockopt(h, fd, SRTO_RCVLATENCY, "SRTO_RCVLATENCY", &rcvlatency, sizeof(rcvlatency)) < 0) ||
  297. (s->peerlatency >= 0 && libsrt_setsockopt(h, fd, SRTO_PEERLATENCY, "SRTO_PEERLATENCY", &peerlatency, sizeof(peerlatency)) < 0) ||
  298. (s->tlpktdrop >= 0 && libsrt_setsockopt(h, fd, SRTO_TLPKTDROP, "SRTO_TLPKDROP", &s->tlpktdrop, sizeof(s->tlpktdrop)) < 0) ||
  299. (s->nakreport >= 0 && libsrt_setsockopt(h, fd, SRTO_NAKREPORT, "SRTO_NAKREPORT", &s->nakreport, sizeof(s->nakreport)) < 0) ||
  300. (connect_timeout >= 0 && libsrt_setsockopt(h, fd, SRTO_CONNTIMEO, "SRTO_CONNTIMEO", &connect_timeout, sizeof(connect_timeout)) <0 ) ||
  301. (s->sndbuf >= 0 && libsrt_setsockopt(h, fd, SRTO_SNDBUF, "SRTO_SNDBUF", &s->sndbuf, sizeof(s->sndbuf)) < 0) ||
  302. (s->rcvbuf >= 0 && libsrt_setsockopt(h, fd, SRTO_RCVBUF, "SRTO_RCVBUF", &s->rcvbuf, sizeof(s->rcvbuf)) < 0) ||
  303. (s->lossmaxttl >= 0 && libsrt_setsockopt(h, fd, SRTO_LOSSMAXTTL, "SRTO_LOSSMAXTTL", &s->lossmaxttl, sizeof(s->lossmaxttl)) < 0) ||
  304. (s->minversion >= 0 && libsrt_setsockopt(h, fd, SRTO_MINVERSION, "SRTO_MINVERSION", &s->minversion, sizeof(s->minversion)) < 0) ||
  305. (s->streamid && libsrt_setsockopt(h, fd, SRTO_STREAMID, "SRTO_STREAMID", s->streamid, strlen(s->streamid)) < 0) ||
  306. (s->smoother && libsrt_setsockopt(h, fd, SRTO_SMOOTHER, "SRTO_SMOOTHER", s->smoother, strlen(s->smoother)) < 0) ||
  307. (s->messageapi >= 0 && libsrt_setsockopt(h, fd, SRTO_MESSAGEAPI, "SRTO_MESSAGEAPI", &s->messageapi, sizeof(s->messageapi)) < 0) ||
  308. (s->payload_size >= 0 && libsrt_setsockopt(h, fd, SRTO_PAYLOADSIZE, "SRTO_PAYLOADSIZE", &s->payload_size, sizeof(s->payload_size)) < 0) ||
  309. ((h->flags & AVIO_FLAG_WRITE) && libsrt_setsockopt(h, fd, SRTO_SENDER, "SRTO_SENDER", &yes, sizeof(yes)) < 0)) {
  310. return AVERROR(EIO);
  311. }
  312. return 0;
  313. }
  314. static int libsrt_setup(URLContext *h, const char *uri, int flags)
  315. {
  316. struct addrinfo hints = { 0 }, *ai, *cur_ai;
  317. int port, fd = -1;
  318. SRTContext *s = h->priv_data;
  319. const char *p;
  320. char buf[256];
  321. int ret;
  322. char hostname[1024],proto[1024],path[1024];
  323. char portstr[10];
  324. int open_timeout = 5000000;
  325. int eid;
  326. eid = srt_epoll_create();
  327. if (eid < 0)
  328. return libsrt_neterrno(h);
  329. s->eid = eid;
  330. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  331. &port, path, sizeof(path), uri);
  332. if (strcmp(proto, "srt"))
  333. return AVERROR(EINVAL);
  334. if (port <= 0 || port >= 65536) {
  335. av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
  336. return AVERROR(EINVAL);
  337. }
  338. p = strchr(uri, '?');
  339. if (p) {
  340. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  341. s->rw_timeout = strtol(buf, NULL, 10);
  342. }
  343. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  344. s->listen_timeout = strtol(buf, NULL, 10);
  345. }
  346. }
  347. if (s->rw_timeout >= 0) {
  348. open_timeout = h->rw_timeout = s->rw_timeout;
  349. }
  350. hints.ai_family = AF_UNSPEC;
  351. hints.ai_socktype = SOCK_DGRAM;
  352. snprintf(portstr, sizeof(portstr), "%d", port);
  353. if (s->mode == SRT_MODE_LISTENER)
  354. hints.ai_flags |= AI_PASSIVE;
  355. ret = getaddrinfo(hostname[0] ? hostname : NULL, portstr, &hints, &ai);
  356. if (ret) {
  357. av_log(h, AV_LOG_ERROR,
  358. "Failed to resolve hostname %s: %s\n",
  359. hostname, gai_strerror(ret));
  360. return AVERROR(EIO);
  361. }
  362. cur_ai = ai;
  363. restart:
  364. fd = srt_socket(cur_ai->ai_family, cur_ai->ai_socktype, 0);
  365. if (fd < 0) {
  366. ret = libsrt_neterrno(h);
  367. goto fail;
  368. }
  369. if ((ret = libsrt_set_options_pre(h, fd)) < 0) {
  370. goto fail;
  371. }
  372. /* Set the socket's send or receive buffer sizes, if specified.
  373. If unspecified or setting fails, system default is used. */
  374. if (s->recv_buffer_size > 0) {
  375. srt_setsockopt(fd, SOL_SOCKET, SRTO_UDP_RCVBUF, &s->recv_buffer_size, sizeof (s->recv_buffer_size));
  376. }
  377. if (s->send_buffer_size > 0) {
  378. srt_setsockopt(fd, SOL_SOCKET, SRTO_UDP_SNDBUF, &s->send_buffer_size, sizeof (s->send_buffer_size));
  379. }
  380. if (s->mode == SRT_MODE_LISTENER) {
  381. // multi-client
  382. if ((ret = libsrt_listen(s->eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen, h, open_timeout / 1000)) < 0)
  383. goto fail1;
  384. fd = ret;
  385. } else {
  386. if (s->mode == SRT_MODE_RENDEZVOUS) {
  387. ret = srt_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
  388. if (ret)
  389. goto fail1;
  390. }
  391. if ((ret = libsrt_listen_connect(s->eid, fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
  392. open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
  393. if (ret == AVERROR_EXIT)
  394. goto fail1;
  395. else
  396. goto fail;
  397. }
  398. }
  399. if ((ret = libsrt_set_options_post(h, fd)) < 0) {
  400. goto fail;
  401. }
  402. if (flags & AVIO_FLAG_WRITE) {
  403. int packet_size = 0;
  404. int optlen = sizeof(packet_size);
  405. ret = libsrt_getsockopt(h, fd, SRTO_PAYLOADSIZE, "SRTO_PAYLOADSIZE", &packet_size, &optlen);
  406. if (ret < 0)
  407. goto fail1;
  408. if (packet_size > 0)
  409. h->max_packet_size = packet_size;
  410. }
  411. h->is_streamed = 1;
  412. s->fd = fd;
  413. freeaddrinfo(ai);
  414. return 0;
  415. fail:
  416. if (cur_ai->ai_next) {
  417. /* Retry with the next sockaddr */
  418. cur_ai = cur_ai->ai_next;
  419. if (fd >= 0)
  420. srt_close(fd);
  421. ret = 0;
  422. goto restart;
  423. }
  424. fail1:
  425. if (fd >= 0)
  426. srt_close(fd);
  427. freeaddrinfo(ai);
  428. return ret;
  429. }
  430. static int libsrt_open(URLContext *h, const char *uri, int flags)
  431. {
  432. SRTContext *s = h->priv_data;
  433. const char * p;
  434. char buf[256];
  435. int ret = 0;
  436. if (srt_startup() < 0) {
  437. return AVERROR_UNKNOWN;
  438. }
  439. /* SRT options (srt/srt.h) */
  440. p = strchr(uri, '?');
  441. if (p) {
  442. if (av_find_info_tag(buf, sizeof(buf), "maxbw", p)) {
  443. s->maxbw = strtoll(buf, NULL, 0);
  444. }
  445. if (av_find_info_tag(buf, sizeof(buf), "pbkeylen", p)) {
  446. s->pbkeylen = strtol(buf, NULL, 10);
  447. }
  448. if (av_find_info_tag(buf, sizeof(buf), "passphrase", p)) {
  449. s->passphrase = av_strndup(buf, strlen(buf));
  450. }
  451. if (av_find_info_tag(buf, sizeof(buf), "mss", p)) {
  452. s->mss = strtol(buf, NULL, 10);
  453. }
  454. if (av_find_info_tag(buf, sizeof(buf), "ffs", p)) {
  455. s->ffs = strtol(buf, NULL, 10);
  456. }
  457. if (av_find_info_tag(buf, sizeof(buf), "ipttl", p)) {
  458. s->ipttl = strtol(buf, NULL, 10);
  459. }
  460. if (av_find_info_tag(buf, sizeof(buf), "iptos", p)) {
  461. s->iptos = strtol(buf, NULL, 10);
  462. }
  463. if (av_find_info_tag(buf, sizeof(buf), "inputbw", p)) {
  464. s->inputbw = strtoll(buf, NULL, 10);
  465. }
  466. if (av_find_info_tag(buf, sizeof(buf), "oheadbw", p)) {
  467. s->oheadbw = strtoll(buf, NULL, 10);
  468. }
  469. if (av_find_info_tag(buf, sizeof(buf), "latency", p)) {
  470. s->latency = strtol(buf, NULL, 10);
  471. }
  472. if (av_find_info_tag(buf, sizeof(buf), "tsbpddelay", p)) {
  473. s->latency = strtol(buf, NULL, 10);
  474. }
  475. if (av_find_info_tag(buf, sizeof(buf), "rcvlatency", p)) {
  476. s->rcvlatency = strtol(buf, NULL, 10);
  477. }
  478. if (av_find_info_tag(buf, sizeof(buf), "peerlatency", p)) {
  479. s->peerlatency = strtol(buf, NULL, 10);
  480. }
  481. if (av_find_info_tag(buf, sizeof(buf), "tlpktdrop", p)) {
  482. s->tlpktdrop = strtol(buf, NULL, 10);
  483. }
  484. if (av_find_info_tag(buf, sizeof(buf), "nakreport", p)) {
  485. s->nakreport = strtol(buf, NULL, 10);
  486. }
  487. if (av_find_info_tag(buf, sizeof(buf), "connect_timeout", p)) {
  488. s->connect_timeout = strtol(buf, NULL, 10);
  489. }
  490. if (av_find_info_tag(buf, sizeof(buf), "payload_size", p) ||
  491. av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  492. s->payload_size = strtol(buf, NULL, 10);
  493. }
  494. if (av_find_info_tag(buf, sizeof(buf), "mode", p)) {
  495. if (!strcmp(buf, "caller")) {
  496. s->mode = SRT_MODE_CALLER;
  497. } else if (!strcmp(buf, "listener")) {
  498. s->mode = SRT_MODE_LISTENER;
  499. } else if (!strcmp(buf, "rendezvous")) {
  500. s->mode = SRT_MODE_RENDEZVOUS;
  501. } else {
  502. return AVERROR(EIO);
  503. }
  504. }
  505. if (av_find_info_tag(buf, sizeof(buf), "sndbuf", p)) {
  506. s->sndbuf = strtol(buf, NULL, 10);
  507. }
  508. if (av_find_info_tag(buf, sizeof(buf), "rcvbuf", p)) {
  509. s->rcvbuf = strtol(buf, NULL, 10);
  510. }
  511. if (av_find_info_tag(buf, sizeof(buf), "lossmaxttl", p)) {
  512. s->lossmaxttl = strtol(buf, NULL, 10);
  513. }
  514. if (av_find_info_tag(buf, sizeof(buf), "minversion", p)) {
  515. s->minversion = strtol(buf, NULL, 0);
  516. }
  517. if (av_find_info_tag(buf, sizeof(buf), "streamid", p)) {
  518. av_freep(&s->streamid);
  519. s->streamid = av_strdup(buf);
  520. if (!s->streamid) {
  521. ret = AVERROR(ENOMEM);
  522. goto err;
  523. }
  524. }
  525. if (av_find_info_tag(buf, sizeof(buf), "smoother", p)) {
  526. av_freep(&s->smoother);
  527. s->smoother = av_strdup(buf);
  528. if(!s->smoother) {
  529. ret = AVERROR(ENOMEM);
  530. goto err;
  531. }
  532. }
  533. if (av_find_info_tag(buf, sizeof(buf), "messageapi", p)) {
  534. s->messageapi = strtol(buf, NULL, 10);
  535. }
  536. if (av_find_info_tag(buf, sizeof(buf), "transtype", p)) {
  537. if (!strcmp(buf, "live")) {
  538. s->transtype = SRTT_LIVE;
  539. } else if (!strcmp(buf, "file")) {
  540. s->transtype = SRTT_FILE;
  541. } else {
  542. ret = AVERROR(EINVAL);
  543. goto err;
  544. }
  545. }
  546. }
  547. return libsrt_setup(h, uri, flags);
  548. err:
  549. av_freep(&s->smoother);
  550. av_freep(&s->streamid);
  551. return ret;
  552. }
  553. static int libsrt_read(URLContext *h, uint8_t *buf, int size)
  554. {
  555. SRTContext *s = h->priv_data;
  556. int ret;
  557. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  558. ret = libsrt_network_wait_fd_timeout(h, s->eid, s->fd, 0, h->rw_timeout, &h->interrupt_callback);
  559. if (ret)
  560. return ret;
  561. }
  562. ret = srt_recvmsg(s->fd, buf, size);
  563. if (ret < 0) {
  564. ret = libsrt_neterrno(h);
  565. }
  566. return ret;
  567. }
  568. static int libsrt_write(URLContext *h, const uint8_t *buf, int size)
  569. {
  570. SRTContext *s = h->priv_data;
  571. int ret;
  572. if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
  573. ret = libsrt_network_wait_fd_timeout(h, s->eid, s->fd, 1, h->rw_timeout, &h->interrupt_callback);
  574. if (ret)
  575. return ret;
  576. }
  577. ret = srt_sendmsg(s->fd, buf, size, -1, 0);
  578. if (ret < 0) {
  579. ret = libsrt_neterrno(h);
  580. }
  581. return ret;
  582. }
  583. static int libsrt_close(URLContext *h)
  584. {
  585. SRTContext *s = h->priv_data;
  586. srt_close(s->fd);
  587. srt_epoll_release(s->eid);
  588. srt_cleanup();
  589. return 0;
  590. }
  591. static int libsrt_get_file_handle(URLContext *h)
  592. {
  593. SRTContext *s = h->priv_data;
  594. return s->fd;
  595. }
  596. static const AVClass libsrt_class = {
  597. .class_name = "libsrt",
  598. .item_name = av_default_item_name,
  599. .option = libsrt_options,
  600. .version = LIBAVUTIL_VERSION_INT,
  601. };
  602. const URLProtocol ff_libsrt_protocol = {
  603. .name = "srt",
  604. .url_open = libsrt_open,
  605. .url_read = libsrt_read,
  606. .url_write = libsrt_write,
  607. .url_close = libsrt_close,
  608. .url_get_file_handle = libsrt_get_file_handle,
  609. .priv_data_size = sizeof(SRTContext),
  610. .flags = URL_PROTOCOL_FLAG_NETWORK,
  611. .priv_data_class = &libsrt_class,
  612. };