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.

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