obs-app.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <time.h>
  15. #include <stdio.h>
  16. #include <wchar.h>
  17. #include <chrono>
  18. #include <ratio>
  19. #include <sstream>
  20. #include <mutex>
  21. #include <util/bmem.h>
  22. #include <util/dstr.h>
  23. #include <util/platform.h>
  24. #include <util/profiler.hpp>
  25. #include <obs-config.h>
  26. #include <obs.hpp>
  27. #include <QProxyStyle>
  28. #include "qt-wrappers.hpp"
  29. #include "obs-app.hpp"
  30. #include "window-basic-main.hpp"
  31. #include "window-basic-settings.hpp"
  32. #include "window-license-agreement.hpp"
  33. #include "crash-report.hpp"
  34. #include "platform.hpp"
  35. #include <fstream>
  36. #include <curl/curl.h>
  37. #ifdef _WIN32
  38. #include <windows.h>
  39. #else
  40. #include <signal.h>
  41. #endif
  42. using namespace std;
  43. static log_handler_t def_log_handler;
  44. static string currentLogFile;
  45. static string lastLogFile;
  46. static bool portable_mode = false;
  47. QObject *CreateShortcutFilter()
  48. {
  49. return new OBSEventFilter([](QObject *obj, QEvent *event)
  50. {
  51. auto mouse_event = [](QMouseEvent &event)
  52. {
  53. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  54. bool pressed = event.type() == QEvent::MouseButtonPress;
  55. switch (event.button()) {
  56. case Qt::NoButton:
  57. case Qt::LeftButton:
  58. case Qt::RightButton:
  59. case Qt::AllButtons:
  60. case Qt::MouseButtonMask:
  61. return false;
  62. case Qt::MidButton:
  63. hotkey.key = OBS_KEY_MOUSE3;
  64. break;
  65. #define MAP_BUTTON(i, j) case Qt::ExtraButton ## i: \
  66. hotkey.key = OBS_KEY_MOUSE ## j; break;
  67. MAP_BUTTON( 1, 4);
  68. MAP_BUTTON( 2, 5);
  69. MAP_BUTTON( 3, 6);
  70. MAP_BUTTON( 4, 7);
  71. MAP_BUTTON( 5, 8);
  72. MAP_BUTTON( 6, 9);
  73. MAP_BUTTON( 7, 10);
  74. MAP_BUTTON( 8, 11);
  75. MAP_BUTTON( 9, 12);
  76. MAP_BUTTON(10, 13);
  77. MAP_BUTTON(11, 14);
  78. MAP_BUTTON(12, 15);
  79. MAP_BUTTON(13, 16);
  80. MAP_BUTTON(14, 17);
  81. MAP_BUTTON(15, 18);
  82. MAP_BUTTON(16, 19);
  83. MAP_BUTTON(17, 20);
  84. MAP_BUTTON(18, 21);
  85. MAP_BUTTON(19, 22);
  86. MAP_BUTTON(20, 23);
  87. MAP_BUTTON(21, 24);
  88. MAP_BUTTON(22, 25);
  89. MAP_BUTTON(23, 26);
  90. MAP_BUTTON(24, 27);
  91. #undef MAP_BUTTON
  92. }
  93. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  94. event.modifiers());
  95. obs_hotkey_inject_event(hotkey, pressed);
  96. return true;
  97. };
  98. auto key_event = [&](QKeyEvent *event)
  99. {
  100. QDialog *dialog = qobject_cast<QDialog*>(obj);
  101. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  102. bool pressed = event->type() == QEvent::KeyPress;
  103. switch (event->key()) {
  104. case Qt::Key_Shift:
  105. case Qt::Key_Control:
  106. case Qt::Key_Alt:
  107. case Qt::Key_Meta:
  108. break;
  109. #ifdef __APPLE__
  110. case Qt::Key_CapsLock:
  111. // kVK_CapsLock == 57
  112. hotkey.key = obs_key_from_virtual_key(57);
  113. pressed = true;
  114. break;
  115. #endif
  116. case Qt::Key_Enter:
  117. case Qt::Key_Escape:
  118. case Qt::Key_Return:
  119. if (dialog && pressed)
  120. return false;
  121. default:
  122. hotkey.key = obs_key_from_virtual_key(
  123. event->nativeVirtualKey());
  124. }
  125. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  126. event->modifiers());
  127. obs_hotkey_inject_event(hotkey, pressed);
  128. return true;
  129. };
  130. switch (event->type()) {
  131. case QEvent::MouseButtonPress:
  132. case QEvent::MouseButtonRelease:
  133. return mouse_event(*static_cast<QMouseEvent*>(event));
  134. /*case QEvent::MouseButtonDblClick:
  135. case QEvent::Wheel:*/
  136. case QEvent::KeyPress:
  137. case QEvent::KeyRelease:
  138. return key_event(static_cast<QKeyEvent*>(event));
  139. default:
  140. return false;
  141. }
  142. });
  143. }
  144. string CurrentTimeString()
  145. {
  146. using namespace std::chrono;
  147. struct tm tstruct;
  148. char buf[80];
  149. auto tp = system_clock::now();
  150. auto now = system_clock::to_time_t(tp);
  151. tstruct = *localtime(&now);
  152. size_t written = strftime(buf, sizeof(buf), "%X", &tstruct);
  153. if (ratio_less<system_clock::period, seconds::period>::value &&
  154. written && (sizeof(buf) - written) > 5) {
  155. auto tp_secs =
  156. time_point_cast<seconds>(tp);
  157. auto millis =
  158. duration_cast<milliseconds>(tp - tp_secs).count();
  159. snprintf(buf + written, sizeof(buf) - written, ".%03u",
  160. static_cast<unsigned>(millis));
  161. }
  162. return buf;
  163. }
  164. string CurrentDateTimeString()
  165. {
  166. time_t now = time(0);
  167. struct tm tstruct;
  168. char buf[80];
  169. tstruct = *localtime(&now);
  170. strftime(buf, sizeof(buf), "%Y-%m-%d, %X", &tstruct);
  171. return buf;
  172. }
  173. static inline void LogString(fstream &logFile, const char *timeString,
  174. char *str)
  175. {
  176. logFile << timeString << str << endl;
  177. }
  178. static inline void LogStringChunk(fstream &logFile, char *str)
  179. {
  180. char *nextLine = str;
  181. string timeString = CurrentTimeString();
  182. timeString += ": ";
  183. while (*nextLine) {
  184. char *nextLine = strchr(str, '\n');
  185. if (!nextLine)
  186. break;
  187. if (nextLine != str && nextLine[-1] == '\r') {
  188. nextLine[-1] = 0;
  189. } else {
  190. nextLine[0] = 0;
  191. }
  192. LogString(logFile, timeString.c_str(), str);
  193. nextLine++;
  194. str = nextLine;
  195. }
  196. LogString(logFile, timeString.c_str(), str);
  197. }
  198. #define MAX_REPEATED_LINES 30
  199. #define MAX_CHAR_VARIATION (255 * 3)
  200. static inline int sum_chars(const char *str)
  201. {
  202. int val = 0;
  203. for (; *str != 0; str++)
  204. val += *str;
  205. return val;
  206. }
  207. static inline bool too_many_repeated_entries(fstream &logFile, const char *msg,
  208. const char *output_str)
  209. {
  210. static mutex log_mutex;
  211. static const char *last_msg_ptr = nullptr;
  212. static int last_char_sum = 0;
  213. static char cmp_str[4096];
  214. static int rep_count = 0;
  215. int new_sum = sum_chars(output_str);
  216. lock_guard<mutex> guard(log_mutex);
  217. if (last_msg_ptr == msg) {
  218. int diff = std::abs(new_sum - last_char_sum);
  219. if (diff < MAX_CHAR_VARIATION) {
  220. return (rep_count++ >= MAX_REPEATED_LINES);
  221. }
  222. }
  223. if (rep_count > MAX_REPEATED_LINES) {
  224. logFile << CurrentTimeString() <<
  225. ": Last log entry repeated for " <<
  226. to_string(rep_count - MAX_REPEATED_LINES) <<
  227. " more lines" << endl;
  228. }
  229. last_msg_ptr = msg;
  230. strcpy(cmp_str, output_str);
  231. last_char_sum = new_sum;
  232. rep_count = 0;
  233. return false;
  234. }
  235. static void do_log(int log_level, const char *msg, va_list args, void *param)
  236. {
  237. fstream &logFile = *static_cast<fstream*>(param);
  238. char str[4096];
  239. #ifndef _WIN32
  240. va_list args2;
  241. va_copy(args2, args);
  242. #endif
  243. vsnprintf(str, 4095, msg, args);
  244. #ifdef _WIN32
  245. OutputDebugStringA(str);
  246. OutputDebugStringA("\n");
  247. #else
  248. def_log_handler(log_level, msg, args2, nullptr);
  249. #endif
  250. if (too_many_repeated_entries(logFile, msg, str))
  251. return;
  252. if (log_level <= LOG_INFO)
  253. LogStringChunk(logFile, str);
  254. #ifdef _WIN32
  255. if (log_level <= LOG_ERROR && IsDebuggerPresent())
  256. __debugbreak();
  257. #endif
  258. }
  259. #define DEFAULT_LANG "en-US"
  260. bool OBSApp::InitGlobalConfigDefaults()
  261. {
  262. config_set_default_string(globalConfig, "General", "Language",
  263. DEFAULT_LANG);
  264. config_set_default_uint(globalConfig, "General", "MaxLogs", 10);
  265. #if _WIN32
  266. config_set_default_string(globalConfig, "Video", "Renderer",
  267. "Direct3D 11");
  268. #else
  269. config_set_default_string(globalConfig, "Video", "Renderer", "OpenGL");
  270. #endif
  271. config_set_default_bool(globalConfig, "BasicWindow", "PreviewEnabled",
  272. true);
  273. config_set_default_bool(globalConfig, "BasicWindow",
  274. "PreviewProgramMode", false);
  275. config_set_default_bool(globalConfig, "BasicWindow",
  276. "SceneDuplicationMode", true);
  277. config_set_default_bool(globalConfig, "BasicWindow",
  278. "SwapScenesMode", true);
  279. #ifdef __APPLE__
  280. config_set_default_bool(globalConfig, "Video", "DisableOSXVSync", true);
  281. config_set_default_bool(globalConfig, "Video", "ResetOSXVSyncOnExit",
  282. true);
  283. #endif
  284. return true;
  285. }
  286. static bool do_mkdir(const char *path)
  287. {
  288. if (os_mkdirs(path) == MKDIR_ERROR) {
  289. OBSErrorBox(NULL, "Failed to create directory %s", path);
  290. return false;
  291. }
  292. return true;
  293. }
  294. static bool MakeUserDirs()
  295. {
  296. char path[512];
  297. if (GetConfigPath(path, sizeof(path), "obs-studio/basic") <= 0)
  298. return false;
  299. if (!do_mkdir(path))
  300. return false;
  301. if (GetConfigPath(path, sizeof(path), "obs-studio/logs") <= 0)
  302. return false;
  303. if (!do_mkdir(path))
  304. return false;
  305. if (GetConfigPath(path, sizeof(path), "obs-studio/profiler_data") <= 0)
  306. return false;
  307. if (!do_mkdir(path))
  308. return false;
  309. #ifdef _WIN32
  310. if (GetConfigPath(path, sizeof(path), "obs-studio/crashes") <= 0)
  311. return false;
  312. if (!do_mkdir(path))
  313. return false;
  314. #endif
  315. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  316. return false;
  317. if (!do_mkdir(path))
  318. return false;
  319. return true;
  320. }
  321. static bool MakeUserProfileDirs()
  322. {
  323. char path[512];
  324. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles") <= 0)
  325. return false;
  326. if (!do_mkdir(path))
  327. return false;
  328. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes") <= 0)
  329. return false;
  330. if (!do_mkdir(path))
  331. return false;
  332. return true;
  333. }
  334. bool OBSApp::InitGlobalConfig()
  335. {
  336. char path[512];
  337. int len = GetConfigPath(path, sizeof(path),
  338. "obs-studio/global.ini");
  339. if (len <= 0) {
  340. return false;
  341. }
  342. int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
  343. if (errorcode != CONFIG_SUCCESS) {
  344. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  345. return false;
  346. }
  347. return InitGlobalConfigDefaults();
  348. }
  349. bool OBSApp::InitLocale()
  350. {
  351. ProfileScope("OBSApp::InitLocale");
  352. const char *lang = config_get_string(globalConfig, "General",
  353. "Language");
  354. locale = lang;
  355. string englishPath;
  356. if (!GetDataFilePath("locale/" DEFAULT_LANG ".ini", englishPath)) {
  357. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".ini");
  358. return false;
  359. }
  360. textLookup = text_lookup_create(englishPath.c_str());
  361. if (!textLookup) {
  362. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  363. englishPath.c_str());
  364. return false;
  365. }
  366. bool userLocale = config_has_user_value(globalConfig, "General",
  367. "Language");
  368. bool defaultLang = astrcmpi(lang, DEFAULT_LANG) == 0;
  369. if (userLocale && defaultLang)
  370. return true;
  371. if (!userLocale && defaultLang) {
  372. for (auto &locale_ : GetPreferredLocales()) {
  373. if (locale_ == lang)
  374. return true;
  375. stringstream file;
  376. file << "locale/" << locale_ << ".ini";
  377. string path;
  378. if (!GetDataFilePath(file.str().c_str(), path))
  379. continue;
  380. if (!text_lookup_add(textLookup, path.c_str()))
  381. continue;
  382. blog(LOG_INFO, "Using preferred locale '%s'",
  383. locale_.c_str());
  384. locale = locale_;
  385. return true;
  386. }
  387. return true;
  388. }
  389. stringstream file;
  390. file << "locale/" << lang << ".ini";
  391. string path;
  392. if (GetDataFilePath(file.str().c_str(), path)) {
  393. if (!text_lookup_add(textLookup, path.c_str()))
  394. blog(LOG_ERROR, "Failed to add locale file '%s'",
  395. path.c_str());
  396. } else {
  397. blog(LOG_ERROR, "Could not find locale file '%s'",
  398. file.str().c_str());
  399. }
  400. return true;
  401. }
  402. bool OBSApp::SetTheme(std::string name, std::string path)
  403. {
  404. theme = name;
  405. /* Check user dir first, then preinstalled themes. */
  406. if (path == "") {
  407. char userDir[512];
  408. name = "themes/" + name + ".qss";
  409. string temp = "obs-studio/" + name;
  410. int ret = GetConfigPath(userDir, sizeof(userDir),
  411. temp.c_str());
  412. if (ret > 0 && QFile::exists(userDir)) {
  413. path = string(userDir);
  414. } else if (!GetDataFilePath(name.c_str(), path)) {
  415. OBSErrorBox(NULL, "Failed to find %s.", name.c_str());
  416. return false;
  417. }
  418. }
  419. QString mpath = QString("file:///") + path.c_str();
  420. setStyleSheet(mpath);
  421. return true;
  422. }
  423. bool OBSApp::InitTheme()
  424. {
  425. const char *themeName = config_get_string(globalConfig, "General",
  426. "Theme");
  427. if (!themeName)
  428. themeName = "Default";
  429. stringstream t;
  430. t << themeName;
  431. return SetTheme(t.str());
  432. }
  433. OBSApp::OBSApp(int &argc, char **argv, profiler_name_store_t *store)
  434. : QApplication(argc, argv),
  435. profilerNameStore(store)
  436. {
  437. sleepInhibitor = os_inhibit_sleep_create("OBS Video/audio");
  438. }
  439. OBSApp::~OBSApp()
  440. {
  441. #ifdef __APPLE__
  442. bool vsyncDiabled = config_get_bool(globalConfig, "Video",
  443. "DisableOSXVSync");
  444. bool resetVSync = config_get_bool(globalConfig, "Video",
  445. "ResetOSXVSyncOnExit");
  446. if (vsyncDiabled && resetVSync)
  447. EnableOSXVSync(true);
  448. #endif
  449. os_inhibit_sleep_set_active(sleepInhibitor, false);
  450. os_inhibit_sleep_destroy(sleepInhibitor);
  451. }
  452. static void move_basic_to_profiles(void)
  453. {
  454. char path[512];
  455. char new_path[512];
  456. os_glob_t *glob;
  457. /* if not first time use */
  458. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  459. return;
  460. if (!os_file_exists(path))
  461. return;
  462. /* if the profiles directory doesn't already exist */
  463. if (GetConfigPath(new_path, 512, "obs-studio/basic/profiles") <= 0)
  464. return;
  465. if (os_file_exists(new_path))
  466. return;
  467. if (os_mkdir(new_path) == MKDIR_ERROR)
  468. return;
  469. strcat(new_path, "/");
  470. strcat(new_path, Str("Untitled"));
  471. if (os_mkdir(new_path) == MKDIR_ERROR)
  472. return;
  473. strcat(path, "/*.*");
  474. if (os_glob(path, 0, &glob) != 0)
  475. return;
  476. strcpy(path, new_path);
  477. for (size_t i = 0; i < glob->gl_pathc; i++) {
  478. struct os_globent ent = glob->gl_pathv[i];
  479. char *file;
  480. if (ent.directory)
  481. continue;
  482. file = strrchr(ent.path, '/');
  483. if (!file++)
  484. continue;
  485. if (astrcmpi(file, "scenes.json") == 0)
  486. continue;
  487. strcpy(new_path, path);
  488. strcat(new_path, "/");
  489. strcat(new_path, file);
  490. os_rename(ent.path, new_path);
  491. }
  492. os_globfree(glob);
  493. }
  494. static void move_basic_to_scene_collections(void)
  495. {
  496. char path[512];
  497. char new_path[512];
  498. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  499. return;
  500. if (!os_file_exists(path))
  501. return;
  502. if (GetConfigPath(new_path, 512, "obs-studio/basic/scenes") <= 0)
  503. return;
  504. if (os_file_exists(new_path))
  505. return;
  506. if (os_mkdir(new_path) == MKDIR_ERROR)
  507. return;
  508. strcat(path, "/scenes.json");
  509. strcat(new_path, "/");
  510. strcat(new_path, Str("Untitled"));
  511. strcat(new_path, ".json");
  512. os_rename(path, new_path);
  513. }
  514. void OBSApp::AppInit()
  515. {
  516. ProfileScope("OBSApp::AppInit");
  517. if (!InitApplicationBundle())
  518. throw "Failed to initialize application bundle";
  519. if (!MakeUserDirs())
  520. throw "Failed to create required user directories";
  521. if (!InitGlobalConfig())
  522. throw "Failed to initialize global config";
  523. if (!InitLocale())
  524. throw "Failed to load locale";
  525. if (!InitTheme())
  526. throw "Failed to load theme";
  527. config_set_default_string(globalConfig, "Basic", "Profile",
  528. Str("Untitled"));
  529. config_set_default_string(globalConfig, "Basic", "ProfileDir",
  530. Str("Untitled"));
  531. config_set_default_string(globalConfig, "Basic", "SceneCollection",
  532. Str("Untitled"));
  533. config_set_default_string(globalConfig, "Basic", "SceneCollectionFile",
  534. Str("Untitled"));
  535. #ifdef __APPLE__
  536. if (config_get_bool(globalConfig, "Video", "DisableOSXVSync"))
  537. EnableOSXVSync(false);
  538. #endif
  539. move_basic_to_profiles();
  540. move_basic_to_scene_collections();
  541. if (!MakeUserProfileDirs())
  542. throw "Failed to create profile directories";
  543. }
  544. const char *OBSApp::GetRenderModule() const
  545. {
  546. const char *renderer = config_get_string(globalConfig, "Video",
  547. "Renderer");
  548. return (astrcmpi(renderer, "Direct3D 11") == 0) ?
  549. DL_D3D11 : DL_OPENGL;
  550. }
  551. static bool StartupOBS(const char *locale, profiler_name_store_t *store)
  552. {
  553. char path[512];
  554. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  555. return false;
  556. return obs_startup(locale, path, store);
  557. }
  558. bool OBSApp::OBSInit()
  559. {
  560. ProfileScope("OBSApp::OBSInit");
  561. bool licenseAccepted = config_get_bool(globalConfig, "General",
  562. "LicenseAccepted");
  563. OBSLicenseAgreement agreement(nullptr);
  564. if (licenseAccepted || agreement.exec() == QDialog::Accepted) {
  565. if (!licenseAccepted) {
  566. config_set_bool(globalConfig, "General",
  567. "LicenseAccepted", true);
  568. config_save(globalConfig);
  569. }
  570. if (!StartupOBS(locale.c_str(), GetProfilerNameStore()))
  571. return false;
  572. mainWindow = new OBSBasic();
  573. mainWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  574. connect(mainWindow, SIGNAL(destroyed()), this, SLOT(quit()));
  575. mainWindow->OBSInit();
  576. connect(this, &QGuiApplication::applicationStateChanged,
  577. [](Qt::ApplicationState state)
  578. {
  579. obs_hotkey_enable_background_press(
  580. state != Qt::ApplicationActive);
  581. });
  582. obs_hotkey_enable_background_press(
  583. applicationState() != Qt::ApplicationActive);
  584. return true;
  585. } else {
  586. return false;
  587. }
  588. }
  589. string OBSApp::GetVersionString() const
  590. {
  591. stringstream ver;
  592. #ifdef HAVE_OBSCONFIG_H
  593. ver << OBS_VERSION;
  594. #else
  595. ver << LIBOBS_API_MAJOR_VER << "." <<
  596. LIBOBS_API_MINOR_VER << "." <<
  597. LIBOBS_API_PATCH_VER;
  598. #endif
  599. ver << " (";
  600. #ifdef _WIN32
  601. if (sizeof(void*) == 8)
  602. ver << "64bit, ";
  603. ver << "windows)";
  604. #elif __APPLE__
  605. ver << "mac)";
  606. #elif __FreeBSD__
  607. ver << "freebsd)";
  608. #else /* assume linux for the time being */
  609. ver << "linux)";
  610. #endif
  611. return ver.str();
  612. }
  613. #ifdef __APPLE__
  614. #define INPUT_AUDIO_SOURCE "coreaudio_input_capture"
  615. #define OUTPUT_AUDIO_SOURCE "coreaudio_output_capture"
  616. #elif _WIN32
  617. #define INPUT_AUDIO_SOURCE "wasapi_input_capture"
  618. #define OUTPUT_AUDIO_SOURCE "wasapi_output_capture"
  619. #else
  620. #define INPUT_AUDIO_SOURCE "pulse_input_capture"
  621. #define OUTPUT_AUDIO_SOURCE "pulse_output_capture"
  622. #endif
  623. const char *OBSApp::InputAudioSource() const
  624. {
  625. return INPUT_AUDIO_SOURCE;
  626. }
  627. const char *OBSApp::OutputAudioSource() const
  628. {
  629. return OUTPUT_AUDIO_SOURCE;
  630. }
  631. const char *OBSApp::GetLastLog() const
  632. {
  633. return lastLogFile.c_str();
  634. }
  635. const char *OBSApp::GetCurrentLog() const
  636. {
  637. return currentLogFile.c_str();
  638. }
  639. QString OBSTranslator::translate(const char *context, const char *sourceText,
  640. const char *disambiguation, int n) const
  641. {
  642. const char *out = nullptr;
  643. if (!text_lookup_getstr(App()->GetTextLookup(), sourceText, &out))
  644. return QString();
  645. UNUSED_PARAMETER(context);
  646. UNUSED_PARAMETER(disambiguation);
  647. UNUSED_PARAMETER(n);
  648. return QT_UTF8(out);
  649. }
  650. static bool get_token(lexer *lex, string &str, base_token_type type)
  651. {
  652. base_token token;
  653. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  654. return false;
  655. if (token.type != type)
  656. return false;
  657. str.assign(token.text.array, token.text.len);
  658. return true;
  659. }
  660. static bool expect_token(lexer *lex, const char *str, base_token_type type)
  661. {
  662. base_token token;
  663. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  664. return false;
  665. if (token.type != type)
  666. return false;
  667. return strref_cmp(&token.text, str) == 0;
  668. }
  669. static uint64_t convert_log_name(const char *name)
  670. {
  671. BaseLexer lex;
  672. string year, month, day, hour, minute, second;
  673. lexer_start(lex, name);
  674. if (!get_token(lex, year, BASETOKEN_DIGIT)) return 0;
  675. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  676. if (!get_token(lex, month, BASETOKEN_DIGIT)) return 0;
  677. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  678. if (!get_token(lex, day, BASETOKEN_DIGIT)) return 0;
  679. if (!get_token(lex, hour, BASETOKEN_DIGIT)) return 0;
  680. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  681. if (!get_token(lex, minute, BASETOKEN_DIGIT)) return 0;
  682. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  683. if (!get_token(lex, second, BASETOKEN_DIGIT)) return 0;
  684. stringstream timestring;
  685. timestring << year << month << day << hour << minute << second;
  686. return std::stoull(timestring.str());
  687. }
  688. static void delete_oldest_file(const char *location)
  689. {
  690. BPtr<char> logDir(GetConfigPathPtr(location));
  691. string oldestLog;
  692. uint64_t oldest_ts = (uint64_t)-1;
  693. struct os_dirent *entry;
  694. unsigned int maxLogs = (unsigned int)config_get_uint(
  695. App()->GlobalConfig(), "General", "MaxLogs");
  696. os_dir_t *dir = os_opendir(logDir);
  697. if (dir) {
  698. unsigned int count = 0;
  699. while ((entry = os_readdir(dir)) != NULL) {
  700. if (entry->directory || *entry->d_name == '.')
  701. continue;
  702. uint64_t ts = convert_log_name(entry->d_name);
  703. if (ts) {
  704. if (ts < oldest_ts) {
  705. oldestLog = entry->d_name;
  706. oldest_ts = ts;
  707. }
  708. count++;
  709. }
  710. }
  711. os_closedir(dir);
  712. if (count > maxLogs) {
  713. stringstream delPath;
  714. delPath << logDir << "/" << oldestLog;
  715. os_unlink(delPath.str().c_str());
  716. }
  717. }
  718. }
  719. static void get_last_log(void)
  720. {
  721. BPtr<char> logDir(GetConfigPathPtr("obs-studio/logs"));
  722. struct os_dirent *entry;
  723. os_dir_t *dir = os_opendir(logDir);
  724. uint64_t highest_ts = 0;
  725. if (dir) {
  726. while ((entry = os_readdir(dir)) != NULL) {
  727. if (entry->directory || *entry->d_name == '.')
  728. continue;
  729. uint64_t ts = convert_log_name(entry->d_name);
  730. if (ts > highest_ts) {
  731. lastLogFile = entry->d_name;
  732. highest_ts = ts;
  733. }
  734. }
  735. os_closedir(dir);
  736. }
  737. }
  738. string GenerateTimeDateFilename(const char *extension, bool noSpace)
  739. {
  740. time_t now = time(0);
  741. char file[256] = {};
  742. struct tm *cur_time;
  743. cur_time = localtime(&now);
  744. snprintf(file, sizeof(file), "%d-%02d-%02d%c%02d-%02d-%02d.%s",
  745. cur_time->tm_year+1900,
  746. cur_time->tm_mon+1,
  747. cur_time->tm_mday,
  748. noSpace ? '_' : ' ',
  749. cur_time->tm_hour,
  750. cur_time->tm_min,
  751. cur_time->tm_sec,
  752. extension);
  753. return string(file);
  754. }
  755. string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  756. const char *format)
  757. {
  758. time_t now = time(0);
  759. struct tm *cur_time;
  760. cur_time = localtime(&now);
  761. const size_t spec_count = 23;
  762. const char *spec[][2] = {
  763. {"%CCYY", "%Y"},
  764. {"%YY", "%y"},
  765. {"%MM", "%m"},
  766. {"%DD", "%d"},
  767. {"%hh", "%H"},
  768. {"%mm", "%M"},
  769. {"%ss", "%S"},
  770. {"%%", "%%"},
  771. {"%a", ""},
  772. {"%A", ""},
  773. {"%b", ""},
  774. {"%B", ""},
  775. {"%d", ""},
  776. {"%H", ""},
  777. {"%I", ""},
  778. {"%m", ""},
  779. {"%M", ""},
  780. {"%p", ""},
  781. {"%S", ""},
  782. {"%y", ""},
  783. {"%Y", ""},
  784. {"%z", ""},
  785. {"%Z", ""},
  786. };
  787. char convert[128] = {};
  788. string sf = format;
  789. string c;
  790. size_t pos = 0, len;
  791. while (pos < sf.length()) {
  792. len = 0;
  793. for (size_t i = 0; i < spec_count && len == 0; i++) {
  794. if (sf.find(spec[i][0], pos) == pos) {
  795. if (strlen(spec[i][1]))
  796. strftime(convert, sizeof(convert),
  797. spec[i][1], cur_time);
  798. else
  799. strftime(convert, sizeof(convert),
  800. spec[i][0], cur_time);
  801. len = strlen(spec[i][0]);
  802. c = convert;
  803. if (c.length() && c.find_first_not_of(' ') !=
  804. std::string::npos)
  805. sf.replace(pos, len, convert);
  806. }
  807. }
  808. if (len)
  809. pos += strlen(convert);
  810. else if (!len && sf.at(pos) == '%')
  811. sf.erase(pos,1);
  812. else
  813. pos++;
  814. }
  815. if (noSpace)
  816. replace(sf.begin(), sf.end(), ' ', '_');
  817. sf += '.';
  818. sf += extension;
  819. return (sf.length() < 256) ? sf : sf.substr(0, 255);
  820. }
  821. vector<pair<string, string>> GetLocaleNames()
  822. {
  823. string path;
  824. if (!GetDataFilePath("locale.ini", path))
  825. throw "Could not find locale.ini path";
  826. ConfigFile ini;
  827. if (ini.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  828. throw "Could not open locale.ini";
  829. size_t sections = config_num_sections(ini);
  830. vector<pair<string, string>> names;
  831. names.reserve(sections);
  832. for (size_t i = 0; i < sections; i++) {
  833. const char *tag = config_get_section(ini, i);
  834. const char *name = config_get_string(ini, tag, "Name");
  835. names.emplace_back(tag, name);
  836. }
  837. return names;
  838. }
  839. static void create_log_file(fstream &logFile)
  840. {
  841. stringstream dst;
  842. get_last_log();
  843. currentLogFile = GenerateTimeDateFilename("txt");
  844. dst << "obs-studio/logs/" << currentLogFile.c_str();
  845. BPtr<char> path(GetConfigPathPtr(dst.str().c_str()));
  846. logFile.open(path,
  847. ios_base::in | ios_base::out | ios_base::trunc);
  848. if (logFile.is_open()) {
  849. delete_oldest_file("obs-studio/logs");
  850. base_set_log_handler(do_log, &logFile);
  851. } else {
  852. blog(LOG_ERROR, "Failed to open log file");
  853. }
  854. }
  855. static auto ProfilerNameStoreRelease = [](profiler_name_store_t *store)
  856. {
  857. profiler_name_store_free(store);
  858. };
  859. using ProfilerNameStore =
  860. std::unique_ptr<profiler_name_store_t,
  861. decltype(ProfilerNameStoreRelease)>;
  862. ProfilerNameStore CreateNameStore()
  863. {
  864. return ProfilerNameStore{profiler_name_store_create(),
  865. ProfilerNameStoreRelease};
  866. }
  867. static auto SnapshotRelease = [](profiler_snapshot_t *snap)
  868. {
  869. profile_snapshot_free(snap);
  870. };
  871. using ProfilerSnapshot =
  872. std::unique_ptr<profiler_snapshot_t, decltype(SnapshotRelease)>;
  873. ProfilerSnapshot GetSnapshot()
  874. {
  875. return ProfilerSnapshot{profile_snapshot_create(), SnapshotRelease};
  876. }
  877. static void SaveProfilerData(const ProfilerSnapshot &snap)
  878. {
  879. if (currentLogFile.empty())
  880. return;
  881. auto pos = currentLogFile.rfind('.');
  882. if (pos == currentLogFile.npos)
  883. return;
  884. #define LITERAL_SIZE(x) x, (sizeof(x) - 1)
  885. ostringstream dst;
  886. dst.write(LITERAL_SIZE("obs-studio/profiler_data/"));
  887. dst.write(currentLogFile.c_str(), pos);
  888. dst.write(LITERAL_SIZE(".csv.gz"));
  889. #undef LITERAL_SIZE
  890. BPtr<char> path = GetConfigPathPtr(dst.str().c_str());
  891. if (!profiler_snapshot_dump_csv_gz(snap.get(), path))
  892. blog(LOG_WARNING, "Could not save profiler data to '%s'",
  893. static_cast<const char*>(path));
  894. }
  895. static auto ProfilerFree = [](void *)
  896. {
  897. profiler_stop();
  898. auto snap = GetSnapshot();
  899. profiler_print(snap.get());
  900. profiler_print_time_between_calls(snap.get());
  901. SaveProfilerData(snap);
  902. profiler_free();
  903. };
  904. static const char *run_program_init = "run_program_init";
  905. static int run_program(fstream &logFile, int argc, char *argv[])
  906. {
  907. int ret = -1;
  908. auto profilerNameStore = CreateNameStore();
  909. std::unique_ptr<void, decltype(ProfilerFree)>
  910. prof_release(static_cast<void*>(&ProfilerFree),
  911. ProfilerFree);
  912. profiler_start();
  913. profile_register_root(run_program_init, 0);
  914. auto PrintInitProfile = [&]()
  915. {
  916. auto snap = GetSnapshot();
  917. profiler_snapshot_filter_roots(snap.get(), [](void *data,
  918. const char *name, bool *remove)
  919. {
  920. *remove = (*static_cast<const char**>(data)) != name;
  921. return true;
  922. }, static_cast<void*>(&run_program_init));
  923. profiler_print(snap.get());
  924. };
  925. ScopeProfiler prof{run_program_init};
  926. QCoreApplication::addLibraryPath(".");
  927. OBSApp program(argc, argv, profilerNameStore.get());
  928. try {
  929. program.AppInit();
  930. OBSTranslator translator;
  931. create_log_file(logFile);
  932. delete_oldest_file("obs-studio/profiler_data");
  933. program.installTranslator(&translator);
  934. if (!program.OBSInit())
  935. return 0;
  936. prof.Stop();
  937. PrintInitProfile();
  938. return program.exec();
  939. } catch (const char *error) {
  940. blog(LOG_ERROR, "%s", error);
  941. OBSErrorBox(nullptr, "%s", error);
  942. }
  943. return ret;
  944. }
  945. #define MAX_CRASH_REPORT_SIZE (50 * 1024)
  946. #ifdef _WIN32
  947. #define CRASH_MESSAGE \
  948. "Woops, OBS has crashed!\n\nWould you like to copy the crash log " \
  949. "to the clipboard? (Crash logs will still be saved to the " \
  950. "%appdata%\\obs-studio\\crashes directory)"
  951. static void main_crash_handler(const char *format, va_list args, void *param)
  952. {
  953. char *text = new char[MAX_CRASH_REPORT_SIZE];
  954. vsnprintf(text, MAX_CRASH_REPORT_SIZE, format, args);
  955. text[MAX_CRASH_REPORT_SIZE - 1] = 0;
  956. delete_oldest_file("obs-studio/crashes");
  957. string name = "obs-studio/crashes/Crash ";
  958. name += GenerateTimeDateFilename("txt");
  959. BPtr<char> path(GetConfigPathPtr(name.c_str()));
  960. fstream file;
  961. file.open(path, ios_base::in | ios_base::out | ios_base::trunc |
  962. ios_base::binary);
  963. file << text;
  964. file.close();
  965. int ret = MessageBoxA(NULL, CRASH_MESSAGE, "OBS has crashed!",
  966. MB_YESNO | MB_ICONERROR | MB_TASKMODAL);
  967. if (ret == IDYES) {
  968. size_t len = strlen(text);
  969. HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, len);
  970. memcpy(GlobalLock(mem), text, len);
  971. GlobalUnlock(mem);
  972. OpenClipboard(0);
  973. EmptyClipboard();
  974. SetClipboardData(CF_TEXT, mem);
  975. CloseClipboard();
  976. }
  977. exit(-1);
  978. UNUSED_PARAMETER(param);
  979. }
  980. static void load_debug_privilege(void)
  981. {
  982. const DWORD flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
  983. TOKEN_PRIVILEGES tp;
  984. HANDLE token;
  985. LUID val;
  986. if (!OpenProcessToken(GetCurrentProcess(), flags, &token)) {
  987. return;
  988. }
  989. if (!!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &val)) {
  990. tp.PrivilegeCount = 1;
  991. tp.Privileges[0].Luid = val;
  992. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  993. AdjustTokenPrivileges(token, false, &tp,
  994. sizeof(tp), NULL, NULL);
  995. }
  996. CloseHandle(token);
  997. }
  998. #endif
  999. #ifdef __APPLE__
  1000. #define BASE_PATH ".."
  1001. #else
  1002. #define BASE_PATH "../.."
  1003. #endif
  1004. #define CONFIG_PATH BASE_PATH "/config"
  1005. #ifndef OBS_UNIX_STRUCTURE
  1006. #define OBS_UNIX_STRUCTURE 0
  1007. #endif
  1008. int GetConfigPath(char *path, size_t size, const char *name)
  1009. {
  1010. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  1011. if (name && *name) {
  1012. return snprintf(path, size, CONFIG_PATH "/%s", name);
  1013. } else {
  1014. return snprintf(path, size, CONFIG_PATH);
  1015. }
  1016. } else {
  1017. return os_get_config_path(path, size, name);
  1018. }
  1019. }
  1020. char *GetConfigPathPtr(const char *name)
  1021. {
  1022. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  1023. char path[512];
  1024. if (snprintf(path, sizeof(path), CONFIG_PATH "/%s", name) > 0) {
  1025. return bstrdup(path);
  1026. } else {
  1027. return NULL;
  1028. }
  1029. } else {
  1030. return os_get_config_path_ptr(name);
  1031. }
  1032. }
  1033. bool GetFileSafeName(const char *name, std::string &file)
  1034. {
  1035. size_t base_len = strlen(name);
  1036. size_t len = os_utf8_to_wcs(name, base_len, nullptr, 0);
  1037. std::wstring wfile;
  1038. if (!len)
  1039. return false;
  1040. wfile.resize(len);
  1041. os_utf8_to_wcs(name, base_len, &wfile[0], len);
  1042. for (size_t i = wfile.size(); i > 0; i--) {
  1043. size_t im1 = i - 1;
  1044. if (iswspace(wfile[im1])) {
  1045. wfile[im1] = '_';
  1046. } else if (wfile[im1] != '_' && !iswalnum(wfile[im1])) {
  1047. wfile.erase(im1, 1);
  1048. }
  1049. }
  1050. if (wfile.size() == 0)
  1051. wfile = L"characters_only";
  1052. len = os_wcs_to_utf8(wfile.c_str(), wfile.size(), nullptr, 0);
  1053. if (!len)
  1054. return false;
  1055. file.resize(len);
  1056. os_wcs_to_utf8(wfile.c_str(), wfile.size(), &file[0], len);
  1057. return true;
  1058. }
  1059. bool GetClosestUnusedFileName(std::string &path, const char *extension)
  1060. {
  1061. size_t len = path.size();
  1062. if (extension) {
  1063. path += ".";
  1064. path += extension;
  1065. }
  1066. if (!os_file_exists(path.c_str()))
  1067. return true;
  1068. int index = 1;
  1069. do {
  1070. path.resize(len);
  1071. path += std::to_string(++index);
  1072. if (extension) {
  1073. path += ".";
  1074. path += extension;
  1075. }
  1076. } while (os_file_exists(path.c_str()));
  1077. return true;
  1078. }
  1079. static inline bool arg_is(const char *arg,
  1080. const char *long_form, const char *short_form)
  1081. {
  1082. return (long_form && strcmp(arg, long_form) == 0) ||
  1083. (short_form && strcmp(arg, short_form) == 0);
  1084. }
  1085. #if !defined(_WIN32) && !defined(__APPLE__)
  1086. #define IS_UNIX 1
  1087. #endif
  1088. /* if using XDG and was previously using an older build of OBS, move config
  1089. * files to XDG directory */
  1090. #if defined(USE_XDG) && defined(IS_UNIX)
  1091. static void move_to_xdg(void)
  1092. {
  1093. char old_path[512];
  1094. char new_path[512];
  1095. char *home = getenv("HOME");
  1096. if (!home)
  1097. return;
  1098. if (snprintf(old_path, 512, "%s/.obs-studio", home) <= 0)
  1099. return;
  1100. /* make base xdg path if it doesn't already exist */
  1101. if (GetConfigPath(new_path, 512, "") <= 0)
  1102. return;
  1103. if (os_mkdirs(new_path) == MKDIR_ERROR)
  1104. return;
  1105. if (GetConfigPath(new_path, 512, "obs-studio") <= 0)
  1106. return;
  1107. if (os_file_exists(old_path) && !os_file_exists(new_path)) {
  1108. rename(old_path, new_path);
  1109. }
  1110. }
  1111. #endif
  1112. static bool update_ffmpeg_output(ConfigFile &config)
  1113. {
  1114. if (config_has_user_value(config, "AdvOut", "FFOutputToFile"))
  1115. return false;
  1116. const char *url = config_get_string(config, "AdvOut", "FFURL");
  1117. if (!url)
  1118. return false;
  1119. bool isActualURL = strstr(url, "://") != nullptr;
  1120. if (isActualURL)
  1121. return false;
  1122. string urlStr = url;
  1123. string extension;
  1124. for (size_t i = urlStr.length(); i > 0; i--) {
  1125. size_t idx = i - 1;
  1126. if (urlStr[idx] == '.') {
  1127. extension = &urlStr[i];
  1128. }
  1129. if (urlStr[idx] == '\\' || urlStr[idx] == '/') {
  1130. urlStr[idx] = 0;
  1131. break;
  1132. }
  1133. }
  1134. if (urlStr.empty() || extension.empty())
  1135. return false;
  1136. config_remove_value(config, "AdvOut", "FFURL");
  1137. config_set_string(config, "AdvOut", "FFFilePath", urlStr.c_str());
  1138. config_set_string(config, "AdvOut", "FFExtension", extension.c_str());
  1139. config_set_bool(config, "AdvOut", "FFOutputToFile", true);
  1140. return true;
  1141. }
  1142. static bool move_reconnect_settings(ConfigFile &config, const char *sec)
  1143. {
  1144. bool changed = false;
  1145. if (config_has_user_value(config, sec, "Reconnect")) {
  1146. bool reconnect = config_get_bool(config, sec, "Reconnect");
  1147. config_set_bool(config, "Output", "Reconnect", reconnect);
  1148. changed = true;
  1149. }
  1150. if (config_has_user_value(config, sec, "RetryDelay")) {
  1151. int delay = (int)config_get_uint(config, sec, "RetryDelay");
  1152. config_set_uint(config, "Output", "RetryDelay", delay);
  1153. changed = true;
  1154. }
  1155. if (config_has_user_value(config, sec, "MaxRetries")) {
  1156. int retries = (int)config_get_uint(config, sec, "MaxRetries");
  1157. config_set_uint(config, "Output", "MaxRetries", retries);
  1158. changed = true;
  1159. }
  1160. return changed;
  1161. }
  1162. static bool update_reconnect(ConfigFile &config)
  1163. {
  1164. if (!config_has_user_value(config, "Output", "Mode"))
  1165. return false;
  1166. const char *mode = config_get_string(config, "Output", "Mode");
  1167. if (!mode)
  1168. return false;
  1169. const char *section = (strcmp(mode, "Advanced") == 0) ?
  1170. "AdvOut" : "SimpleOutput";
  1171. if (move_reconnect_settings(config, section)) {
  1172. config_remove_value(config, "SimpleOutput", "Reconnect");
  1173. config_remove_value(config, "SimpleOutput", "RetryDelay");
  1174. config_remove_value(config, "SimpleOutput", "MaxRetries");
  1175. config_remove_value(config, "AdvOut", "Reconnect");
  1176. config_remove_value(config, "AdvOut", "RetryDelay");
  1177. config_remove_value(config, "AdvOut", "MaxRetries");
  1178. return true;
  1179. }
  1180. return false;
  1181. }
  1182. static void upgrade_settings(void)
  1183. {
  1184. char path[512];
  1185. int pathlen = GetConfigPath(path, 512, "obs-studio/basic/profiles");
  1186. if (pathlen <= 0)
  1187. return;
  1188. if (!os_file_exists(path))
  1189. return;
  1190. os_dir_t *dir = os_opendir(path);
  1191. if (!dir)
  1192. return;
  1193. struct os_dirent *ent = os_readdir(dir);
  1194. while (ent) {
  1195. if (ent->directory) {
  1196. strcat(path, "/");
  1197. strcat(path, ent->d_name);
  1198. strcat(path, "/basic.ini");
  1199. ConfigFile config;
  1200. int ret;
  1201. ret = config.Open(path, CONFIG_OPEN_EXISTING);
  1202. if (ret == CONFIG_SUCCESS) {
  1203. if (update_ffmpeg_output(config) ||
  1204. update_reconnect(config)) {
  1205. config_save_safe(config, "tmp",
  1206. nullptr);
  1207. }
  1208. }
  1209. path[pathlen] = 0;
  1210. }
  1211. ent = os_readdir(dir);
  1212. }
  1213. os_closedir(dir);
  1214. }
  1215. int main(int argc, char *argv[])
  1216. {
  1217. #ifndef _WIN32
  1218. signal(SIGPIPE, SIG_IGN);
  1219. #endif
  1220. #ifdef _WIN32
  1221. load_debug_privilege();
  1222. base_set_crash_handler(main_crash_handler, nullptr);
  1223. #endif
  1224. base_get_log_handler(&def_log_handler, nullptr);
  1225. #if defined(USE_XDG) && defined(IS_UNIX)
  1226. move_to_xdg();
  1227. #endif
  1228. for (int i = 1; i < argc; i++) {
  1229. if (arg_is(argv[i], "--portable", "-p")) {
  1230. portable_mode = true;
  1231. }
  1232. }
  1233. #if !OBS_UNIX_STRUCTURE
  1234. if (!portable_mode) {
  1235. portable_mode =
  1236. os_file_exists(BASE_PATH "/portable_mode") ||
  1237. os_file_exists(BASE_PATH "/obs_portable_mode") ||
  1238. os_file_exists(BASE_PATH "/portable_mode.txt") ||
  1239. os_file_exists(BASE_PATH "/obs_portable_mode.txt");
  1240. }
  1241. #endif
  1242. upgrade_settings();
  1243. fstream logFile;
  1244. curl_global_init(CURL_GLOBAL_ALL);
  1245. int ret = run_program(logFile, argc, argv);
  1246. blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs());
  1247. base_set_log_handler(nullptr, nullptr);
  1248. return ret;
  1249. }