obs-ffmpeg-srt.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * The following code is a port of FFmpeg/avformat/libsrt.c for obs-studio.
  3. * Port by [email protected].
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #pragma once
  18. #include <obs-module.h>
  19. #include "obs-ffmpeg-url.h"
  20. #include <srt/srt.h>
  21. #include <libavformat/avformat.h>
  22. #define POLLING_TIME 100 /// Time in milliseconds between interrupt check
  23. /* This is for MPEG-TS (7 TS packets) */
  24. #ifndef SRT_LIVE_DEFAULT_PAYLOAD_SIZE
  25. #define SRT_LIVE_DEFAULT_PAYLOAD_SIZE 1316
  26. #endif
  27. enum SRTMode {
  28. SRT_MODE_CALLER = 0,
  29. SRT_MODE_LISTENER = 1,
  30. SRT_MODE_RENDEZVOUS = 2
  31. };
  32. typedef struct SRTContext {
  33. SRTSOCKET fd;
  34. int eid;
  35. int64_t rw_timeout;
  36. int64_t listen_timeout;
  37. int recv_buffer_size;
  38. int send_buffer_size;
  39. int64_t maxbw;
  40. int pbkeylen;
  41. char *passphrase;
  42. #if SRT_VERSION_VALUE >= 0x010302
  43. int enforced_encryption;
  44. int kmrefreshrate;
  45. int kmpreannounce;
  46. int64_t snddropdelay;
  47. #endif
  48. int mss;
  49. int ffs;
  50. int ipttl;
  51. int iptos;
  52. int64_t inputbw;
  53. int oheadbw;
  54. int64_t latency;
  55. int tlpktdrop;
  56. int nakreport;
  57. int64_t connect_timeout;
  58. int payload_size;
  59. int64_t rcvlatency;
  60. int64_t peerlatency;
  61. enum SRTMode mode;
  62. int sndbuf;
  63. int rcvbuf;
  64. int lossmaxttl;
  65. int minversion;
  66. char *streamid;
  67. char *smoother;
  68. int messageapi;
  69. SRT_TRANSTYPE transtype;
  70. int linger;
  71. int tsbpd;
  72. double time; // time in s in order to post logs at definite intervals
  73. } SRTContext;
  74. static int libsrt_neterrno(URLContext *h)
  75. {
  76. SRTContext *s = (SRTContext *)h->priv_data;
  77. int os_errno;
  78. int err = srt_getlasterror(&os_errno);
  79. blog(LOG_ERROR, "[obs-ffmpeg mpegts muxer / libsrt]: %s",
  80. srt_getlasterror_str());
  81. if (err == SRT_EASYNCRCV || err == SRT_EASYNCSND)
  82. return AVERROR(EAGAIN);
  83. if (err == SRT_ECONNREJ) {
  84. int errj = srt_getrejectreason(s->fd);
  85. if (errj == SRT_REJ_BADSECRET)
  86. blog(LOG_ERROR,
  87. "[obs-ffmpeg mpegts muxer / libsrt]: Wrong password");
  88. else
  89. blog(LOG_ERROR,
  90. "[obs-ffmpeg mpegts muxer / libsrt]: Connection rejected, %s",
  91. srt_rejectreason_str(errj));
  92. }
  93. return os_errno ? AVERROR(os_errno) : AVERROR_UNKNOWN;
  94. }
  95. static int libsrt_getsockopt(URLContext *h, SRTSOCKET fd, SRT_SOCKOPT optname,
  96. const char *optnamestr, void *optval, int *optlen)
  97. {
  98. UNUSED_PARAMETER(h);
  99. if (srt_getsockopt(fd, 0, optname, optval, optlen) < 0) {
  100. blog(LOG_INFO,
  101. "[obs-ffmpeg mpegts muxer / libsrt]: Failed to get option %s on socket: %s",
  102. optnamestr, srt_getlasterror_str());
  103. return AVERROR(EIO);
  104. }
  105. return 0;
  106. }
  107. static int libsrt_socket_nonblock(SRTSOCKET socket, int enable)
  108. {
  109. int ret, blocking = enable ? 0 : 1;
  110. /* Setting SRTO_{SND,RCV}SYN options to 1 enable blocking mode, setting them to 0 enable non-blocking mode. */
  111. ret = srt_setsockopt(socket, 0, SRTO_SNDSYN, &blocking,
  112. sizeof(blocking));
  113. if (ret < 0)
  114. return ret;
  115. return srt_setsockopt(socket, 0, SRTO_RCVSYN, &blocking,
  116. sizeof(blocking));
  117. }
  118. static int libsrt_epoll_create(URLContext *h, SRTSOCKET fd, int write)
  119. {
  120. int modes = SRT_EPOLL_ERR | (write ? SRT_EPOLL_OUT : SRT_EPOLL_IN);
  121. int eid = srt_epoll_create();
  122. if (eid < 0)
  123. return libsrt_neterrno(h);
  124. if (srt_epoll_add_usock(eid, fd, &modes) < 0) {
  125. srt_epoll_release(eid);
  126. return libsrt_neterrno(h);
  127. }
  128. return eid;
  129. }
  130. static int libsrt_network_wait_fd(URLContext *h, int eid, int write)
  131. {
  132. int ret, len = 1, errlen = 1;
  133. SRTSOCKET ready[1];
  134. SRTSOCKET error[1];
  135. if (write) {
  136. ret = srt_epoll_wait(eid, error, &errlen, ready, &len,
  137. POLLING_TIME, 0, 0, 0, 0);
  138. } else {
  139. ret = srt_epoll_wait(eid, ready, &len, error, &errlen,
  140. POLLING_TIME, 0, 0, 0, 0);
  141. }
  142. if (len == 1 && errlen == 1) {
  143. /* Socket reported in wsock AND rsock signifies an error. */
  144. int reason = srt_getrejectreason(*ready);
  145. if (reason == SRT_REJ_BADSECRET || reason == SRT_REJ_UNSECURE ||
  146. reason == SRT_REJ_TIMEOUT) {
  147. blog(LOG_ERROR,
  148. "[obs-ffmpeg mpegts muxer / libsrt]: Connection rejected, wrong password or invalid URL");
  149. return OBS_OUTPUT_INVALID_STREAM;
  150. } else {
  151. blog(LOG_ERROR,
  152. "[obs-ffmpeg mpegts muxer / libsrt]: Connection rejected, %s",
  153. srt_rejectreason_str(reason));
  154. }
  155. }
  156. if (ret < 0) {
  157. if (srt_getlasterror(NULL) == SRT_ETIMEOUT)
  158. ret = AVERROR(EAGAIN);
  159. else
  160. ret = libsrt_neterrno(h);
  161. } else {
  162. ret = errlen ? AVERROR(EIO) : 0;
  163. }
  164. return ret;
  165. }
  166. int check_interrupt(AVIOInterruptCB *cb)
  167. {
  168. if (cb && cb->callback)
  169. return cb->callback(cb->opaque);
  170. return 0;
  171. }
  172. static int libsrt_network_wait_fd_timeout(URLContext *h, int eid, int write,
  173. int64_t timeout,
  174. AVIOInterruptCB *int_cb)
  175. {
  176. int ret;
  177. int64_t wait_start = 0;
  178. while (1) {
  179. if (check_interrupt(int_cb))
  180. return AVERROR_EXIT;
  181. ret = libsrt_network_wait_fd(h, eid, write);
  182. if (ret != AVERROR(EAGAIN))
  183. return ret;
  184. if (timeout > 0) {
  185. if (!wait_start)
  186. wait_start = av_gettime_relative();
  187. else if (av_gettime_relative() - wait_start > timeout)
  188. return AVERROR(ETIMEDOUT);
  189. }
  190. }
  191. }
  192. static int libsrt_listen(int eid, SRTSOCKET fd, const struct sockaddr *addr,
  193. socklen_t addrlen, URLContext *h, int64_t timeout)
  194. {
  195. int ret;
  196. int reuse = 1;
  197. /* Max streamid length plus an extra space for the terminating null character */
  198. char streamid[513];
  199. int streamid_len = sizeof(streamid);
  200. if (srt_setsockopt(fd, SOL_SOCKET, SRTO_REUSEADDR, &reuse,
  201. sizeof(reuse))) {
  202. blog(LOG_WARNING,
  203. "[obs-ffmpeg mpegts muxer / libsrt]: setsockopt(SRTO_REUSEADDR) failed");
  204. }
  205. if (srt_bind(fd, addr, addrlen))
  206. return libsrt_neterrno(h);
  207. if (srt_listen(fd, 1))
  208. return libsrt_neterrno(h);
  209. ret = libsrt_network_wait_fd_timeout(h, eid, 1, timeout,
  210. &h->interrupt_callback);
  211. if (ret < 0)
  212. return ret;
  213. ret = srt_accept(fd, NULL, NULL);
  214. if (ret < 0)
  215. return libsrt_neterrno(h);
  216. if (libsrt_socket_nonblock(ret, 1) < 0)
  217. blog(LOG_DEBUG,
  218. "[obs-ffmpeg mpegts muxer / libsrt]: libsrt_socket_nonblock failed");
  219. if (!libsrt_getsockopt(h, ret, SRTO_STREAMID, "SRTO_STREAMID", streamid,
  220. &streamid_len))
  221. /* Note: returned streamid_len doesn't count the terminating null character */
  222. blog(LOG_INFO,
  223. "[obs-ffmpeg mpegts muxer / libsrt]: Accept streamid [%s], length %d",
  224. streamid, streamid_len);
  225. return ret;
  226. }
  227. static int libsrt_listen_connect(int eid, SRTSOCKET fd,
  228. const struct sockaddr *addr, socklen_t addrlen,
  229. int64_t timeout, URLContext *h,
  230. int will_try_next)
  231. {
  232. int ret;
  233. if (srt_connect(fd, addr, addrlen) < 0)
  234. return libsrt_neterrno(h);
  235. ret = libsrt_network_wait_fd_timeout(h, eid, 1, timeout,
  236. &h->interrupt_callback);
  237. if (ret < 0) {
  238. if (will_try_next) {
  239. blog(LOG_WARNING,
  240. "[obs-ffmpeg mpegts muxer / libsrt]: Connection to %s failed (%s), trying next address",
  241. h->url, av_err2str(ret));
  242. } else {
  243. blog(LOG_ERROR,
  244. "[obs-ffmpeg mpegts muxer / libsrt]: Connection to %s failed: %s",
  245. h->url, av_err2str(ret));
  246. }
  247. }
  248. return ret;
  249. }
  250. static int libsrt_setsockopt(URLContext *h, SRTSOCKET fd, SRT_SOCKOPT optname,
  251. const char *optnamestr, const void *optval,
  252. int optlen)
  253. {
  254. UNUSED_PARAMETER(h);
  255. if (srt_setsockopt(fd, 0, optname, optval, optlen) < 0) {
  256. blog(LOG_ERROR,
  257. "[obs-ffmpeg mpegts muxer / libsrt]: Failed to set option %s on socket: %s",
  258. optnamestr, srt_getlasterror_str());
  259. return AVERROR(EIO);
  260. }
  261. return 0;
  262. }
  263. /* - The "POST" options can be altered any time on a connected socket.
  264. They MAY have also some meaning when set prior to connecting; such
  265. option is SRTO_RCVSYN, which makes connect/accept call asynchronous.
  266. Because of that this option is treated special way in this app. */
  267. static int libsrt_set_options_post(URLContext *h, SRTSOCKET fd)
  268. {
  269. SRTContext *s = (SRTContext *)h->priv_data;
  270. if ((s->inputbw >= 0 &&
  271. libsrt_setsockopt(h, fd, SRTO_INPUTBW, "SRTO_INPUTBW", &s->inputbw,
  272. sizeof(s->inputbw)) < 0) ||
  273. (s->oheadbw >= 0 &&
  274. libsrt_setsockopt(h, fd, SRTO_OHEADBW, "SRTO_OHEADBW", &s->oheadbw,
  275. sizeof(s->oheadbw)) < 0)) {
  276. return AVERROR(EIO);
  277. }
  278. return 0;
  279. }
  280. /* - The "PRE" options must be set prior to connecting and can't be altered
  281. on a connected socket, however if set on a listening socket, they are
  282. derived by accept-ed socket. */
  283. static int libsrt_set_options_pre(URLContext *h, SRTSOCKET fd)
  284. {
  285. SRTContext *s = (SRTContext *)h->priv_data;
  286. int yes = 1;
  287. int latency = (int)(s->latency / 1000);
  288. int rcvlatency = (int)(s->rcvlatency / 1000);
  289. int peerlatency = (int)(s->peerlatency / 1000);
  290. #if SRT_VERSION_VALUE >= 0x010302
  291. int snddropdelay = s->snddropdelay > 0 ? (int)(s->snddropdelay / 1000)
  292. : (int)(s->snddropdelay);
  293. #endif
  294. int connect_timeout = (int)(s->connect_timeout);
  295. if ((s->mode == SRT_MODE_RENDEZVOUS &&
  296. libsrt_setsockopt(h, fd, SRTO_RENDEZVOUS, "SRTO_RENDEZVOUS", &yes,
  297. sizeof(yes)) < 0) ||
  298. (s->transtype != SRTT_INVALID &&
  299. libsrt_setsockopt(h, fd, SRTO_TRANSTYPE, "SRTO_TRANSTYPE",
  300. &s->transtype, sizeof(s->transtype)) < 0) ||
  301. (s->maxbw >= 0 &&
  302. libsrt_setsockopt(h, fd, SRTO_MAXBW, "SRTO_MAXBW", &s->maxbw,
  303. sizeof(s->maxbw)) < 0) ||
  304. (s->pbkeylen >= 0 &&
  305. libsrt_setsockopt(h, fd, SRTO_PBKEYLEN, "SRTO_PBKEYLEN",
  306. &s->pbkeylen, sizeof(s->pbkeylen)) < 0) ||
  307. (s->passphrase &&
  308. libsrt_setsockopt(h, fd, SRTO_PASSPHRASE, "SRTO_PASSPHRASE",
  309. s->passphrase,
  310. (int)strlen(s->passphrase)) < 0) ||
  311. #if SRT_VERSION_VALUE >= 0x010302
  312. #if SRT_VERSION_VALUE >= 0x010401
  313. (s->enforced_encryption >= 0 &&
  314. libsrt_setsockopt(h, fd, SRTO_ENFORCEDENCRYPTION,
  315. "SRTO_ENFORCEDENCRYPTION",
  316. &s->enforced_encryption,
  317. sizeof(s->enforced_encryption)) < 0) ||
  318. #else
  319. /* SRTO_STRICTENC == SRTO_ENFORCEDENCRYPTION (53), but for compatibility, we used SRTO_STRICTENC */
  320. (s->enforced_encryption >= 0 &&
  321. libsrt_setsockopt(h, fd, SRTO_STRICTENC, "SRTO_STRICTENC",
  322. &s->enforced_encryption,
  323. sizeof(s->enforced_encryption)) < 0) ||
  324. #endif
  325. (s->kmrefreshrate >= 0 &&
  326. libsrt_setsockopt(h, fd, SRTO_KMREFRESHRATE, "SRTO_KMREFRESHRATE",
  327. &s->kmrefreshrate,
  328. sizeof(s->kmrefreshrate)) < 0) ||
  329. (s->kmpreannounce >= 0 &&
  330. libsrt_setsockopt(h, fd, SRTO_KMPREANNOUNCE, "SRTO_KMPREANNOUNCE",
  331. &s->kmpreannounce,
  332. sizeof(s->kmpreannounce)) < 0) ||
  333. (s->snddropdelay >= -1 &&
  334. libsrt_setsockopt(h, fd, SRTO_SNDDROPDELAY, "SRTO_SNDDROPDELAY",
  335. &snddropdelay, sizeof(snddropdelay)) < 0) ||
  336. #endif
  337. (s->mss >= 0 && libsrt_setsockopt(h, fd, SRTO_MSS, "SRTO_MSS",
  338. &s->mss, sizeof(s->mss)) < 0) ||
  339. (s->ffs >= 0 && libsrt_setsockopt(h, fd, SRTO_FC, "SRTO_FC",
  340. &s->ffs, sizeof(s->ffs)) < 0) ||
  341. (s->ipttl >= 0 &&
  342. libsrt_setsockopt(h, fd, SRTO_IPTTL, "SRTO_IPTTL", &s->ipttl,
  343. sizeof(s->ipttl)) < 0) ||
  344. (s->iptos >= 0 &&
  345. libsrt_setsockopt(h, fd, SRTO_IPTOS, "SRTO_IPTOS", &s->iptos,
  346. sizeof(s->iptos)) < 0) ||
  347. (s->latency >= 0 &&
  348. libsrt_setsockopt(h, fd, SRTO_LATENCY, "SRTO_LATENCY", &latency,
  349. sizeof(latency)) < 0) ||
  350. (s->rcvlatency >= 0 &&
  351. libsrt_setsockopt(h, fd, SRTO_RCVLATENCY, "SRTO_RCVLATENCY",
  352. &rcvlatency, sizeof(rcvlatency)) < 0) ||
  353. (s->peerlatency >= 0 &&
  354. libsrt_setsockopt(h, fd, SRTO_PEERLATENCY, "SRTO_PEERLATENCY",
  355. &peerlatency, sizeof(peerlatency)) < 0) ||
  356. (s->tlpktdrop >= 0 &&
  357. libsrt_setsockopt(h, fd, SRTO_TLPKTDROP, "SRTO_TLPKTDROP",
  358. &s->tlpktdrop, sizeof(s->tlpktdrop)) < 0) ||
  359. (s->nakreport >= 0 &&
  360. libsrt_setsockopt(h, fd, SRTO_NAKREPORT, "SRTO_NAKREPORT",
  361. &s->nakreport, sizeof(s->nakreport)) < 0) ||
  362. (connect_timeout >= 0 &&
  363. libsrt_setsockopt(h, fd, SRTO_CONNTIMEO, "SRTO_CONNTIMEO",
  364. &connect_timeout,
  365. sizeof(connect_timeout)) < 0) ||
  366. (s->sndbuf >= 0 &&
  367. libsrt_setsockopt(h, fd, SRTO_SNDBUF, "SRTO_SNDBUF", &s->sndbuf,
  368. sizeof(s->sndbuf)) < 0) ||
  369. (s->rcvbuf >= 0 &&
  370. libsrt_setsockopt(h, fd, SRTO_RCVBUF, "SRTO_RCVBUF", &s->rcvbuf,
  371. sizeof(s->rcvbuf)) < 0) ||
  372. (s->lossmaxttl >= 0 &&
  373. libsrt_setsockopt(h, fd, SRTO_LOSSMAXTTL, "SRTO_LOSSMAXTTL",
  374. &s->lossmaxttl, sizeof(s->lossmaxttl)) < 0) ||
  375. (s->minversion >= 0 &&
  376. libsrt_setsockopt(h, fd, SRTO_MINVERSION, "SRTO_MINVERSION",
  377. &s->minversion, sizeof(s->minversion)) < 0) ||
  378. (s->streamid &&
  379. libsrt_setsockopt(h, fd, SRTO_STREAMID, "SRTO_STREAMID",
  380. s->streamid, (int)strlen(s->streamid)) < 0) ||
  381. #if SRT_VERSION_VALUE >= 0x010401
  382. (s->smoother &&
  383. libsrt_setsockopt(h, fd, SRTO_CONGESTION, "SRTO_CONGESTION",
  384. s->smoother, (int)strlen(s->smoother)) < 0) ||
  385. #else
  386. (s->smoother &&
  387. libsrt_setsockopt(h, fd, SRTO_SMOOTHER, "SRTO_SMOOTHER",
  388. s->smoother, (int)strlen(s->smoother)) < 0) ||
  389. #endif
  390. (s->messageapi >= 0 &&
  391. libsrt_setsockopt(h, fd, SRTO_MESSAGEAPI, "SRTO_MESSAGEAPI",
  392. &s->messageapi, sizeof(s->messageapi)) < 0) ||
  393. (s->payload_size >= 0 &&
  394. libsrt_setsockopt(h, fd, SRTO_PAYLOADSIZE, "SRTO_PAYLOADSIZE",
  395. &s->payload_size,
  396. sizeof(s->payload_size)) < 0) ||
  397. (/*(h->flags & AVIO_FLAG_WRITE) &&*/
  398. libsrt_setsockopt(h, fd, SRTO_SENDER, "SRTO_SENDER", &yes,
  399. sizeof(yes)) < 0) ||
  400. (s->tsbpd >= 0 &&
  401. libsrt_setsockopt(h, fd, SRTO_TSBPDMODE, "SRTO_TSBPDMODE",
  402. &s->tsbpd, sizeof(s->tsbpd)) < 0)) {
  403. return AVERROR(EIO);
  404. }
  405. if (s->linger >= 0) {
  406. struct linger lin;
  407. lin.l_linger = s->linger;
  408. lin.l_onoff = lin.l_linger > 0 ? 1 : 0;
  409. if (libsrt_setsockopt(h, fd, SRTO_LINGER, "SRTO_LINGER", &lin,
  410. sizeof(lin)) < 0)
  411. return AVERROR(EIO);
  412. }
  413. return 0;
  414. }
  415. static int libsrt_setup(URLContext *h, const char *uri)
  416. {
  417. struct addrinfo hints = {0}, *ai, *cur_ai;
  418. int port;
  419. SRTSOCKET fd;
  420. SRTContext *s = (SRTContext *)h->priv_data;
  421. const char *p;
  422. char buf[1024];
  423. int ret;
  424. char hostname[1024], proto[1024], path[1024];
  425. char portstr[10];
  426. int64_t open_timeout = 0;
  427. int eid, write_eid;
  428. av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
  429. &port, path, sizeof(path), uri);
  430. if (strcmp(proto, "srt")) // should not happen !
  431. return AVERROR(EINVAL);
  432. if (port <= 0 || port >= 65536) {
  433. blog(LOG_ERROR,
  434. "[obs-ffmpeg mpegts muxer / libsrt]: Port missing in uri");
  435. return OBS_OUTPUT_CONNECT_FAILED;
  436. }
  437. p = strchr(uri, '?');
  438. if (p) {
  439. if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
  440. s->rw_timeout = strtoll(buf, NULL, 10);
  441. }
  442. if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
  443. s->listen_timeout = strtoll(buf, NULL, 10);
  444. }
  445. }
  446. if (s->rw_timeout >= 0) {
  447. open_timeout = h->rw_timeout = s->rw_timeout;
  448. }
  449. hints.ai_family = AF_UNSPEC;
  450. hints.ai_socktype = SOCK_DGRAM;
  451. snprintf(portstr, sizeof(portstr), "%d", port);
  452. if (s->mode == SRT_MODE_LISTENER)
  453. hints.ai_flags |= AI_PASSIVE;
  454. ret = getaddrinfo(hostname[0] ? hostname : NULL, portstr, &hints, &ai);
  455. if (ret) {
  456. blog(LOG_ERROR,
  457. "[obs-ffmpeg mpegts muxer / libsrt]: Failed to resolve hostname %s: %s",
  458. hostname,
  459. #ifdef _WIN32
  460. gai_strerrorA(ret)
  461. #else
  462. gai_strerror(ret)
  463. #endif
  464. );
  465. return OBS_OUTPUT_CONNECT_FAILED;
  466. }
  467. cur_ai = ai;
  468. restart:
  469. fd = srt_create_socket();
  470. if (fd < 0) {
  471. ret = libsrt_neterrno(h);
  472. goto fail;
  473. }
  474. if ((ret = libsrt_set_options_pre(h, fd)) < 0) {
  475. goto fail;
  476. }
  477. /* Set the socket's send or receive buffer sizes, if specified.
  478. If unspecified or setting fails, system default is used. */
  479. if (s->recv_buffer_size > 0) {
  480. srt_setsockopt(fd, SOL_SOCKET, SRTO_UDP_RCVBUF,
  481. &s->recv_buffer_size,
  482. sizeof(s->recv_buffer_size));
  483. }
  484. if (s->send_buffer_size > 0) {
  485. srt_setsockopt(fd, SOL_SOCKET, SRTO_UDP_SNDBUF,
  486. &s->send_buffer_size,
  487. sizeof(s->send_buffer_size));
  488. }
  489. if (libsrt_socket_nonblock(fd, 1) < 0)
  490. blog(LOG_DEBUG,
  491. "[obs-ffmpeg mpegts muxer / libsrt]: libsrt_socket_nonblock failed");
  492. ret = write_eid = libsrt_epoll_create(h, fd, 1);
  493. if (ret < 0)
  494. goto fail1;
  495. if (s->mode == SRT_MODE_LISTENER) {
  496. // multi-client
  497. ret = libsrt_listen(write_eid, fd, cur_ai->ai_addr,
  498. (socklen_t)cur_ai->ai_addrlen, h,
  499. s->listen_timeout);
  500. srt_epoll_release(write_eid);
  501. if (ret < 0)
  502. goto fail1;
  503. srt_close(fd);
  504. fd = ret;
  505. } else {
  506. if (s->mode == SRT_MODE_RENDEZVOUS) {
  507. if (srt_bind(fd, cur_ai->ai_addr,
  508. (int)(cur_ai->ai_addrlen))) {
  509. ret = libsrt_neterrno(h);
  510. srt_epoll_release(write_eid);
  511. goto fail1;
  512. }
  513. }
  514. ret = libsrt_listen_connect(write_eid, fd, cur_ai->ai_addr,
  515. (socklen_t)(cur_ai->ai_addrlen),
  516. open_timeout, h, !!cur_ai->ai_next);
  517. srt_epoll_release(write_eid);
  518. if (ret < 0) {
  519. if (ret == AVERROR_EXIT)
  520. goto fail1;
  521. else
  522. goto fail;
  523. }
  524. }
  525. if ((ret = libsrt_set_options_post(h, fd)) < 0) {
  526. goto fail;
  527. }
  528. int packet_size = 0;
  529. int optlen = sizeof(packet_size);
  530. ret = libsrt_getsockopt(h, fd, SRTO_PAYLOADSIZE, "SRTO_PAYLOADSIZE",
  531. &packet_size, &optlen);
  532. if (ret < 0)
  533. goto fail1;
  534. if (packet_size > 0)
  535. h->max_packet_size = packet_size;
  536. ret = eid = libsrt_epoll_create(h, fd, 1 /*flags & AVIO_FLAG_WRITE*/);
  537. if (eid < 0)
  538. goto fail1;
  539. s->fd = fd;
  540. s->eid = eid;
  541. freeaddrinfo(ai);
  542. return 0;
  543. fail:
  544. if (cur_ai->ai_next) {
  545. /* Retry with the next sockaddr */
  546. cur_ai = cur_ai->ai_next;
  547. if (fd >= 0)
  548. srt_close(fd);
  549. ret = 0;
  550. goto restart;
  551. }
  552. fail1:
  553. if (fd >= 0)
  554. srt_close(fd);
  555. freeaddrinfo(ai);
  556. return ret;
  557. }
  558. static void libsrt_set_defaults(SRTContext *s)
  559. {
  560. s->rw_timeout = -1;
  561. s->listen_timeout = -1;
  562. s->send_buffer_size = -1;
  563. s->recv_buffer_size = -1;
  564. s->payload_size = SRT_LIVE_DEFAULT_PAYLOAD_SIZE;
  565. s->maxbw = -1;
  566. s->pbkeylen = -1;
  567. s->mss = -1;
  568. s->ffs = -1;
  569. s->ipttl = -1;
  570. s->iptos = -1;
  571. s->inputbw = -1;
  572. s->oheadbw = -1;
  573. s->latency = -1;
  574. s->rcvlatency = -1;
  575. s->peerlatency = -1;
  576. s->tlpktdrop = -1;
  577. s->nakreport = -1;
  578. s->connect_timeout = -1;
  579. s->mode = SRT_MODE_CALLER;
  580. s->sndbuf = -1;
  581. s->rcvbuf = -1;
  582. s->lossmaxttl = -1;
  583. s->minversion = -1;
  584. s->smoother = NULL;
  585. s->messageapi = -1;
  586. s->transtype = SRTT_LIVE;
  587. s->linger = -1;
  588. s->tsbpd = -1;
  589. }
  590. static int libsrt_open(URLContext *h, const char *uri)
  591. {
  592. SRTContext *s = (SRTContext *)h->priv_data;
  593. const char *p;
  594. char buf[1024];
  595. int ret = 0;
  596. if (srt_startup() < 0) {
  597. blog(LOG_ERROR,
  598. "[obs-ffmpeg mpegts muxer / libsrt]: libsrt failed to load");
  599. return OBS_OUTPUT_CONNECT_FAILED;
  600. } else {
  601. blog(LOG_INFO,
  602. "[obs-ffmpeg mpegts muxer / libsrt]: libsrt version %s loaded",
  603. SRT_VERSION_STRING);
  604. }
  605. libsrt_set_defaults(s);
  606. /* SRT options (srt/srt.h) */
  607. p = strchr(uri, '?');
  608. if (p) {
  609. if (av_find_info_tag(buf, sizeof(buf), "maxbw", p)) {
  610. s->maxbw = strtoll(buf, NULL, 10);
  611. }
  612. if (av_find_info_tag(buf, sizeof(buf), "pbkeylen", p)) {
  613. s->pbkeylen = strtol(buf, NULL, 10);
  614. }
  615. if (av_find_info_tag(buf, sizeof(buf), "passphrase", p)) {
  616. av_freep(&s->passphrase);
  617. s->passphrase = av_strndup(buf, strlen(buf));
  618. }
  619. #if SRT_VERSION_VALUE >= 0x010302
  620. if (av_find_info_tag(buf, sizeof(buf), "enforced_encryption",
  621. p)) {
  622. s->enforced_encryption = strtol(buf, NULL, 10);
  623. }
  624. if (av_find_info_tag(buf, sizeof(buf), "kmrefreshrate", p)) {
  625. s->kmrefreshrate = strtol(buf, NULL, 10);
  626. }
  627. if (av_find_info_tag(buf, sizeof(buf), "kmpreannounce", p)) {
  628. s->kmpreannounce = strtol(buf, NULL, 10);
  629. }
  630. if (av_find_info_tag(buf, sizeof(buf), "snddropdelay", p)) {
  631. s->snddropdelay = strtoll(buf, NULL, 10);
  632. }
  633. #endif
  634. if (av_find_info_tag(buf, sizeof(buf), "mss", p)) {
  635. s->mss = strtol(buf, NULL, 10);
  636. }
  637. if (av_find_info_tag(buf, sizeof(buf), "ffs", p)) {
  638. s->ffs = strtol(buf, NULL, 10);
  639. }
  640. if (av_find_info_tag(buf, sizeof(buf), "ipttl", p)) {
  641. s->ipttl = strtol(buf, NULL, 10);
  642. }
  643. if (av_find_info_tag(buf, sizeof(buf), "iptos", p)) {
  644. s->iptos = strtol(buf, NULL, 10);
  645. }
  646. if (av_find_info_tag(buf, sizeof(buf), "inputbw", p)) {
  647. s->inputbw = strtoll(buf, NULL, 10);
  648. }
  649. if (av_find_info_tag(buf, sizeof(buf), "oheadbw", p)) {
  650. s->oheadbw = strtol(buf, NULL, 10);
  651. }
  652. if (av_find_info_tag(buf, sizeof(buf), "latency", p)) {
  653. s->latency = strtoll(buf, NULL, 10);
  654. }
  655. if (av_find_info_tag(buf, sizeof(buf), "tsbpddelay", p)) {
  656. s->latency = strtoll(buf, NULL, 10);
  657. }
  658. if (av_find_info_tag(buf, sizeof(buf), "rcvlatency", p)) {
  659. s->rcvlatency = strtoll(buf, NULL, 10);
  660. }
  661. if (av_find_info_tag(buf, sizeof(buf), "peerlatency", p)) {
  662. s->peerlatency = strtoll(buf, NULL, 10);
  663. }
  664. if (av_find_info_tag(buf, sizeof(buf), "tlpktdrop", p)) {
  665. s->tlpktdrop = strtol(buf, NULL, 10);
  666. }
  667. if (av_find_info_tag(buf, sizeof(buf), "nakreport", p)) {
  668. s->nakreport = strtol(buf, NULL, 10);
  669. }
  670. if (av_find_info_tag(buf, sizeof(buf), "connect_timeout", p)) {
  671. s->connect_timeout = strtoll(buf, NULL, 10);
  672. }
  673. if (av_find_info_tag(buf, sizeof(buf), "payload_size", p) ||
  674. av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
  675. s->payload_size = strtol(buf, NULL, 10);
  676. }
  677. if (av_find_info_tag(buf, sizeof(buf), "mode", p)) {
  678. if (!strcmp(buf, "caller")) {
  679. s->mode = SRT_MODE_CALLER;
  680. } else if (!strcmp(buf, "listener")) {
  681. s->mode = SRT_MODE_LISTENER;
  682. } else if (!strcmp(buf, "rendezvous")) {
  683. s->mode = SRT_MODE_RENDEZVOUS;
  684. } else {
  685. ret = AVERROR(EINVAL);
  686. goto err;
  687. }
  688. }
  689. if (av_find_info_tag(buf, sizeof(buf), "sndbuf", p)) {
  690. s->sndbuf = strtol(buf, NULL, 10);
  691. }
  692. if (av_find_info_tag(buf, sizeof(buf), "rcvbuf", p)) {
  693. s->rcvbuf = strtol(buf, NULL, 10);
  694. }
  695. if (av_find_info_tag(buf, sizeof(buf), "lossmaxttl", p)) {
  696. s->lossmaxttl = strtol(buf, NULL, 10);
  697. }
  698. if (av_find_info_tag(buf, sizeof(buf), "minversion", p)) {
  699. s->minversion = strtol(buf, NULL, 0);
  700. }
  701. if (av_find_info_tag(buf, sizeof(buf), "streamid", p)) {
  702. av_freep(&s->streamid);
  703. s->streamid = av_strdup(buf);
  704. if (!s->streamid) {
  705. ret = AVERROR(ENOMEM);
  706. goto err;
  707. }
  708. }
  709. if (av_find_info_tag(buf, sizeof(buf), "smoother", p)) {
  710. av_freep(&s->smoother);
  711. s->smoother = av_strdup(buf);
  712. if (!s->smoother) {
  713. ret = AVERROR(ENOMEM);
  714. goto err;
  715. }
  716. }
  717. if (av_find_info_tag(buf, sizeof(buf), "messageapi", p)) {
  718. s->messageapi = strtol(buf, NULL, 10);
  719. }
  720. if (av_find_info_tag(buf, sizeof(buf), "transtype", p)) {
  721. if (!strcmp(buf, "live")) {
  722. s->transtype = SRTT_LIVE;
  723. } else if (!strcmp(buf, "file")) {
  724. s->transtype = SRTT_FILE;
  725. } else {
  726. ret = AVERROR(EINVAL);
  727. goto err;
  728. }
  729. }
  730. if (av_find_info_tag(buf, sizeof(buf), "linger", p)) {
  731. s->linger = strtol(buf, NULL, 10);
  732. }
  733. }
  734. ret = libsrt_setup(h, uri);
  735. if (ret < 0)
  736. goto err;
  737. #ifdef _WIN32
  738. struct timeb timebuffer;
  739. ftime(&timebuffer);
  740. s->time = (double)timebuffer.time + 0.001 * (double)timebuffer.millitm;
  741. #else
  742. struct timespec timesp;
  743. clock_gettime(CLOCK_REALTIME, &timesp);
  744. s->time = (double)timesp.tv_sec + 0.000000001 * (double)timesp.tv_nsec;
  745. #endif
  746. return 0;
  747. err:
  748. av_freep(&s->smoother);
  749. av_freep(&s->streamid);
  750. srt_cleanup();
  751. return ret;
  752. }
  753. static int libsrt_write(URLContext *h, const uint8_t *buf, int size)
  754. {
  755. SRTContext *s = (SRTContext *)h->priv_data;
  756. int ret;
  757. SRT_TRACEBSTATS perf;
  758. ret = libsrt_network_wait_fd_timeout(h, s->eid, 1, h->rw_timeout,
  759. &h->interrupt_callback);
  760. if (ret)
  761. return ret;
  762. ret = srt_send(s->fd, (char *)buf, size);
  763. if (ret < 0) {
  764. ret = libsrt_neterrno(h);
  765. } else {
  766. /* log every 60 seconds the rtt and link bandwidth
  767. * rtt: round-trip time
  768. * link bandwidth: bandwidth from ingest to egress
  769. */
  770. #ifdef _WIN32
  771. struct timeb timebuffer;
  772. ftime(&timebuffer);
  773. double time = (double)timebuffer.time +
  774. 0.001 * (double)timebuffer.millitm;
  775. #else
  776. struct timespec timesp;
  777. clock_gettime(CLOCK_REALTIME, &timesp);
  778. double time = (double)timesp.tv_sec +
  779. 0.000000001 * (double)timesp.tv_nsec;
  780. #endif
  781. if (time > (s->time + 60.0)) {
  782. srt_bistats(s->fd, &perf, 0, 1);
  783. blog(LOG_DEBUG,
  784. "[obs-ffmpeg mpegts muxer / libsrt]: RTT [%.2f ms], Link Bandwidth [%.1f Mbps]",
  785. perf.msRTT, perf.mbpsBandwidth);
  786. s->time = time;
  787. }
  788. }
  789. return ret;
  790. }
  791. static int libsrt_close(URLContext *h)
  792. {
  793. SRTContext *s = (SRTContext *)h->priv_data;
  794. if (s->streamid)
  795. av_freep(&s->streamid);
  796. if (s->passphrase)
  797. av_freep(&s->passphrase);
  798. /* Log stream stats. */
  799. SRT_TRACEBSTATS perf = {0};
  800. srt_bstats(s->fd, &perf, 1);
  801. blog(LOG_INFO, "---------------------------------");
  802. blog(LOG_INFO,
  803. "[obs-ffmpeg mpegts muxer / libsrt]: Session Summary\n"
  804. "\ttime elapsed [%.1f sec]\n"
  805. "\tmean speed [%.1f Mbp]\n"
  806. "\ttotal bytes sent [%.1f MB]\n"
  807. "\tbytes retransmitted [%.1f %%]\n"
  808. "\tbytes dropped [%.1f %%]\n",
  809. (double)perf.msTimeStamp / 1000.0, perf.mbpsSendRate,
  810. (double)perf.byteSentTotal / 1000000.0,
  811. perf.byteSentTotal
  812. ? perf.byteRetransTotal / perf.byteSentTotal * 100.0
  813. : 0,
  814. perf.byteSentTotal
  815. ? perf.byteSndDropTotal / perf.byteSentTotal * 100.0
  816. : 0);
  817. srt_epoll_release(s->eid);
  818. int err = srt_close(s->fd);
  819. if (err < 0) {
  820. blog(LOG_ERROR, "[obs-ffmpeg mpegts muxer / libsrt]: %s",
  821. srt_getlasterror_str());
  822. return -1;
  823. }
  824. srt_cleanup();
  825. blog(LOG_INFO,
  826. "[obs-ffmpeg mpegts muxer / libsrt]: SRT connection closed");
  827. return 0;
  828. }