obs-ffmpeg-srt.h 26 KB

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