obs-ffmpeg-srt.h 27 KB

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