You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

515 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "conf.h"
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <sstream>
  7. namespace mtx {
  8. static std::string trim(const std::string & s)
  9. {
  10. size_t b = s.find_first_not_of(" \t\r\n");
  11. if (b == std::string::npos)
  12. {
  13. return std::string();
  14. }
  15. size_t e = s.find_last_not_of(" \t\r\n");
  16. return s.substr(b, e - b + 1);
  17. }
  18. static bool parseBool(const std::string & v, bool & out)
  19. {
  20. if (v == "yes" || v == "true" || v == "1" || v == "on")
  21. {
  22. out = true;
  23. return true;
  24. }
  25. if (v == "no" || v == "false" || v == "0" || v == "off")
  26. {
  27. out = false;
  28. return true;
  29. }
  30. return false;
  31. }
  32. bool parseDurationMs(const std::string & s, int64_t & ms)
  33. {
  34. if (s.empty())
  35. {
  36. return false;
  37. }
  38. char * tEnd = nullptr;
  39. double tV = strtod(s.c_str(), &tEnd);
  40. if (tEnd == s.c_str() || tV < 0)
  41. {
  42. return false;
  43. }
  44. std::string tUnit(tEnd);
  45. double tMul;
  46. if (tUnit.empty() || tUnit == "s")
  47. {
  48. tMul = 1000.0;
  49. }
  50. else if (tUnit == "ms")
  51. {
  52. tMul = 1.0;
  53. }
  54. else if (tUnit == "m")
  55. {
  56. tMul = 60000.0;
  57. }
  58. else if (tUnit == "h")
  59. {
  60. tMul = 3600000.0;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. ms = (int64_t) (tV * tMul + 0.5);
  67. return true;
  68. }
  69. bool validPathName(const std::string & name)
  70. {
  71. if (name.empty() || name.size() > 128)
  72. {
  73. return false;
  74. }
  75. for (char c : name)
  76. {
  77. if (! (isalnum((unsigned char) c) || c == '_' || c == '-' || c == '.' || c == '~'))
  78. {
  79. return false;
  80. }
  81. }
  82. return name != "." && name != "..";
  83. }
  84. bool splitHostPort(const std::string & addr, std::string & host, uint16_t & port)
  85. {
  86. size_t tColon = addr.rfind(':');
  87. if (tColon == std::string::npos)
  88. {
  89. return false;
  90. }
  91. host = addr.substr(0, tColon);
  92. int tPort = atoi(addr.c_str() + tColon + 1);
  93. if (tPort <= 0 || tPort > 65535)
  94. {
  95. return false;
  96. }
  97. port = (uint16_t) tPort;
  98. if (host.size() >= 2 && host.front() == '[' && host.back() == ']')
  99. {
  100. host = host.substr(1, host.size() - 2);
  101. }
  102. return true;
  103. }
  104. std::string hideCredentials(const std::string & url)
  105. {
  106. size_t tScheme = url.find("://");
  107. if (tScheme == std::string::npos)
  108. {
  109. return url;
  110. }
  111. size_t tHostStart = tScheme + 3;
  112. size_t tSlash = url.find('/', tHostStart);
  113. size_t tAt = url.rfind('@', tSlash == std::string::npos ? url.size() : tSlash);
  114. if (tAt == std::string::npos || tAt < tHostStart)
  115. {
  116. return url;
  117. }
  118. return url.substr(0, tHostStart) + url.substr(tAt + 1);
  119. }
  120. static bool parsePathLine(const std::string & line, PathConf & pc, std::string & err)
  121. {
  122. std::istringstream tIn(line);
  123. std::vector<std::string> tTok;
  124. std::string t;
  125. while (tIn >> t)
  126. {
  127. tTok.push_back(t);
  128. }
  129. if (tTok.size() < 2)
  130. {
  131. err = "expected: name source [record=yes|no] [transport=tcp|udp]";
  132. return false;
  133. }
  134. pc = PathConf();
  135. pc.name = tTok[0];
  136. pc.source = tTok[1];
  137. if (! validPathName(pc.name))
  138. {
  139. err = "invalid path name '" + pc.name + "'";
  140. return false;
  141. }
  142. if (pc.source.compare(0, 7, "rtsp://") != 0)
  143. {
  144. err = "path " + pc.name + ": only rtsp:// sources are supported";
  145. return false;
  146. }
  147. for (size_t i = 2; i < tTok.size(); i++)
  148. {
  149. size_t tEq = tTok[i].find('=');
  150. if (tEq == std::string::npos)
  151. {
  152. err = "path " + pc.name + ": bad option '" + tTok[i] + "'";
  153. return false;
  154. }
  155. std::string k = tTok[i].substr(0, tEq), v = tTok[i].substr(tEq + 1);
  156. if (k == "record")
  157. {
  158. if (! parseBool(v, pc.record))
  159. {
  160. err = "path " + pc.name + ": bad record value '" + v + "'";
  161. return false;
  162. }
  163. }
  164. else if (k == "transport" || k == "rtspTransport")
  165. {
  166. if (v == "tcp")
  167. {
  168. pc.transport = RtspTransport::Tcp;
  169. }
  170. else if (v == "udp")
  171. {
  172. pc.transport = RtspTransport::Udp;
  173. }
  174. else
  175. {
  176. err = "path " + pc.name + ": transport must be tcp or udp";
  177. return false;
  178. }
  179. }
  180. else
  181. {
  182. err = "path " + pc.name + ": unknown option '" + k + "'";
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. bool loadPathsFile(const std::string & file, std::vector<PathConf> & out, std::string & err)
  189. {
  190. std::ifstream tIn(file);
  191. if (! tIn)
  192. {
  193. err = "cannot open paths file " + file;
  194. return false;
  195. }
  196. out.clear();
  197. std::string tLine;
  198. int tNo = 0;
  199. while (std::getline(tIn, tLine))
  200. {
  201. tNo++;
  202. size_t tHash = tLine.find('#');
  203. if (tHash != std::string::npos)
  204. {
  205. tLine = tLine.substr(0, tHash);
  206. }
  207. tLine = trim(tLine);
  208. if (tLine.empty())
  209. {
  210. continue;
  211. }
  212. PathConf tPc;
  213. std::string tErr;
  214. if (! parsePathLine(tLine, tPc, tErr))
  215. {
  216. err = file + ":" + std::to_string(tNo) + ": " + tErr;
  217. return false;
  218. }
  219. for (const PathConf & p : out)
  220. {
  221. if (p.name == tPc.name)
  222. {
  223. err = file + ":" + std::to_string(tNo) + ": duplicate path " + tPc.name;
  224. return false;
  225. }
  226. }
  227. out.push_back(tPc);
  228. }
  229. return true;
  230. }
  231. bool savePathsFile(const std::string & file, const std::vector<PathConf> & paths, std::string & err)
  232. {
  233. std::string tTmp = file + ".tmp";
  234. FILE * tF = fopen(tTmp.c_str(), "w");
  235. if (tF == nullptr)
  236. {
  237. err = "cannot write " + tTmp;
  238. return false;
  239. }
  240. fprintf(tF, "# MediaMtx paths: name source [record=yes|no] [transport=tcp|udp]\n");
  241. for (const PathConf & p : paths)
  242. {
  243. fprintf(tF, "%s %s record=%s transport=%s\n", p.name.c_str(), p.source.c_str(), p.record ? "yes" : "no", p.transport == RtspTransport::Udp ? "udp" : "tcp");
  244. }
  245. if (fclose(tF) != 0 || rename(tTmp.c_str(), file.c_str()) != 0)
  246. {
  247. err = "cannot write " + file;
  248. return false;
  249. }
  250. return true;
  251. }
  252. void Conf::mergeCmdPaths(std::vector<PathConf> & out) const
  253. {
  254. // a path given on the command line replaces the one of the paths file
  255. for (const PathConf & p : cmdPaths)
  256. {
  257. bool tFound = false;
  258. for (PathConf & q : out)
  259. {
  260. if (q.name == p.name)
  261. {
  262. q = p;
  263. tFound = true;
  264. }
  265. }
  266. if (! tFound)
  267. {
  268. out.push_back(p);
  269. }
  270. }
  271. }
  272. void Conf::printUsage(const char * prog)
  273. {
  274. fprintf(stderr,
  275. "MediaMtx (C++): RTSP camera pull -> WebRTC (WHEP) + fMP4 recording\n"
  276. "usage: %s [options]\n"
  277. " --paths FILE paths file (name source [record=yes] [transport=tcp|udp]); SIGHUP reloads it\n"
  278. " --path NAME=URL[,record][,udp] add a path (repeatable)\n"
  279. " --logLevel debug|info|warn|error (info)\n"
  280. " --logFile FILE also log to FILE (e.g. mediamtx.log)\n"
  281. " --logStdout yes|no (yes)\n"
  282. " --readTimeout DUR source without data -> reconnect (10s)\n"
  283. " --sourceRetryPause DUR pause before reconnecting a source (5s)\n"
  284. " --apiAddress ADDR control API, empty to disable (127.0.0.1:9997)\n"
  285. " --webrtcAddress ADDR WHEP HTTP listener (:8889)\n"
  286. " --webrtcAllowOrigin ORIGIN CORS Access-Control-Allow-Origin (*)\n"
  287. " --webrtcICEServers LIST comma separated, e.g. stun:stun.l.google.com:19302\n"
  288. " --webrtcBindAddress IP ICE local address (all)\n"
  289. " --webrtcUDPPorts MIN-MAX ICE UDP port range (8189-8289)\n"
  290. " --recordPath PATTERN (./recordings/%%path/%%Y-%%m-%%d_%%H-%%M-%%S-%%f), .mp4 is added\n"
  291. " --recordSegmentDuration DUR (60s)\n"
  292. " --recordPartDuration DUR fragment written to disk every DUR (1s)\n"
  293. " DUR: 500ms, 10s, 1m, 1h\n",
  294. prog);
  295. }
  296. bool Conf::load(int argc, char ** argv, std::string & err)
  297. {
  298. std::vector<PathConf> tCmdPaths;
  299. for (int i = 1; i < argc; i++)
  300. {
  301. std::string tOpt = argv[i];
  302. std::string tVal;
  303. size_t tEq = tOpt.find('=');
  304. if (tOpt.compare(0, 2, "--") != 0)
  305. {
  306. err = "unknown argument " + tOpt;
  307. return false;
  308. }
  309. if (tOpt == "--help" || tOpt == "-h")
  310. {
  311. err = "help";
  312. return false;
  313. }
  314. if (tEq != std::string::npos)
  315. {
  316. tVal = tOpt.substr(tEq + 1);
  317. tOpt = tOpt.substr(0, tEq);
  318. }
  319. else
  320. {
  321. if (i + 1 >= argc)
  322. {
  323. err = "missing value for " + tOpt;
  324. return false;
  325. }
  326. tVal = argv[++i];
  327. }
  328. bool tOk = true;
  329. if (tOpt == "--paths")
  330. {
  331. pathsFile = tVal;
  332. }
  333. else if (tOpt == "--path")
  334. {
  335. // NAME=URL[,record][,udp]
  336. size_t tE = tVal.find('=');
  337. if (tE == std::string::npos)
  338. {
  339. err = "--path expects NAME=URL[,record][,udp]";
  340. return false;
  341. }
  342. std::string tLine = tVal.substr(0, tE) + " ";
  343. std::string tRest = tVal.substr(tE + 1);
  344. std::string tUrl = tRest;
  345. std::string tFlags;
  346. size_t tC = tRest.find(',');
  347. if (tC != std::string::npos)
  348. {
  349. tUrl = tRest.substr(0, tC);
  350. tFlags = tRest.substr(tC + 1);
  351. }
  352. tLine += tUrl;
  353. std::istringstream tF(tFlags);
  354. std::string tFl;
  355. while (std::getline(tF, tFl, ','))
  356. {
  357. if (tFl == "record")
  358. {
  359. tLine += " record=yes";
  360. }
  361. else if (tFl == "udp" || tFl == "tcp")
  362. {
  363. tLine += " transport=" + tFl;
  364. }
  365. else if (! tFl.empty())
  366. {
  367. err = "--path: unknown flag " + tFl;
  368. return false;
  369. }
  370. }
  371. PathConf tPc;
  372. if (! parsePathLine(tLine, tPc, err))
  373. {
  374. return false;
  375. }
  376. tCmdPaths.push_back(tPc);
  377. }
  378. else if (tOpt == "--logLevel")
  379. {
  380. logLevel = parseLogLevel(tVal, &tOk);
  381. }
  382. else if (tOpt == "--logFile")
  383. {
  384. logFile = tVal;
  385. }
  386. else if (tOpt == "--logStdout")
  387. {
  388. tOk = parseBool(tVal, logStdout);
  389. }
  390. else if (tOpt == "--readTimeout")
  391. {
  392. tOk = parseDurationMs(tVal, readTimeoutMs) && readTimeoutMs >= 1000;
  393. }
  394. else if (tOpt == "--sourceRetryPause")
  395. {
  396. tOk = parseDurationMs(tVal, sourceRetryPauseMs);
  397. }
  398. else if (tOpt == "--apiAddress")
  399. {
  400. apiAddress = tVal;
  401. }
  402. else if (tOpt == "--webrtcAddress")
  403. {
  404. webrtcAddress = tVal;
  405. }
  406. else if (tOpt == "--webrtcAllowOrigin")
  407. {
  408. webrtcAllowOrigin = tVal;
  409. }
  410. else if (tOpt == "--webrtcICEServers")
  411. {
  412. webrtcICEServers.clear();
  413. std::istringstream tS(tVal);
  414. std::string tSrv;
  415. while (std::getline(tS, tSrv, ','))
  416. {
  417. if (! trim(tSrv).empty())
  418. {
  419. webrtcICEServers.push_back(trim(tSrv));
  420. }
  421. }
  422. }
  423. else if (tOpt == "--webrtcBindAddress")
  424. {
  425. webrtcBindAddress = tVal;
  426. }
  427. else if (tOpt == "--webrtcUDPPorts")
  428. {
  429. int tMin = 0, tMax = 0;
  430. tOk = sscanf(tVal.c_str(), "%d-%d", &tMin, &tMax) == 2 && tMin > 0 && tMax <= 65535 && tMin <= tMax;
  431. if (tOk)
  432. {
  433. webrtcUDPPortMin = (uint16_t) tMin;
  434. webrtcUDPPortMax = (uint16_t) tMax;
  435. }
  436. }
  437. else if (tOpt == "--recordPath")
  438. {
  439. recordPath = tVal;
  440. tOk = tVal.find("%path") != std::string::npos;
  441. if (! tOk)
  442. {
  443. err = "--recordPath must contain %path";
  444. return false;
  445. }
  446. }
  447. else if (tOpt == "--recordSegmentDuration")
  448. {
  449. tOk = parseDurationMs(tVal, recordSegmentDurationMs) && recordSegmentDurationMs >= 1000;
  450. }
  451. else if (tOpt == "--recordPartDuration")
  452. {
  453. tOk = parseDurationMs(tVal, recordPartDurationMs) && recordPartDurationMs >= 100;
  454. }
  455. else
  456. {
  457. err = "unknown option " + tOpt;
  458. return false;
  459. }
  460. if (! tOk)
  461. {
  462. err = "bad value '" + tVal + "' for " + tOpt;
  463. return false;
  464. }
  465. }
  466. std::string tHost;
  467. uint16_t tPort;
  468. if (! splitHostPort(webrtcAddress, tHost, tPort))
  469. {
  470. err = "bad --webrtcAddress " + webrtcAddress;
  471. return false;
  472. }
  473. if (! apiAddress.empty() && ! splitHostPort(apiAddress, tHost, tPort))
  474. {
  475. err = "bad --apiAddress " + apiAddress;
  476. return false;
  477. }
  478. paths.clear();
  479. if (! pathsFile.empty())
  480. {
  481. FILE * tF = fopen(pathsFile.c_str(), "r");
  482. if (tF == nullptr)
  483. {
  484. // a missing file is created by the API on the first change
  485. Logger::instance().log(LogLevel::Warn, "paths file %s does not exist yet", pathsFile.c_str());
  486. }
  487. else
  488. {
  489. fclose(tF);
  490. if (! loadPathsFile(pathsFile, paths, err))
  491. {
  492. return false;
  493. }
  494. }
  495. }
  496. cmdPaths = tCmdPaths;
  497. mergeCmdPaths(paths);
  498. return true;
  499. }
  500. } // namespace mtx