obs-app.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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 <string>
  20. #include <sstream>
  21. #include <mutex>
  22. #include <util/bmem.h>
  23. #include <util/dstr.h>
  24. #include <util/platform.h>
  25. #include <util/profiler.hpp>
  26. #include <obs-config.h>
  27. #include <obs.hpp>
  28. #include <QGuiApplication>
  29. #include <QProxyStyle>
  30. #include <QScreen>
  31. #include "qt-wrappers.hpp"
  32. #include "obs-app.hpp"
  33. #include "window-basic-main.hpp"
  34. #include "window-basic-settings.hpp"
  35. #include "window-license-agreement.hpp"
  36. #include "crash-report.hpp"
  37. #include "platform.hpp"
  38. #include <fstream>
  39. #include <curl/curl.h>
  40. #ifdef _WIN32
  41. #include <windows.h>
  42. #else
  43. #include <signal.h>
  44. #endif
  45. #include <iostream>
  46. using namespace std;
  47. static log_handler_t def_log_handler;
  48. static string currentLogFile;
  49. static string lastLogFile;
  50. static string lastCrashLogFile;
  51. bool portable_mode = false;
  52. static bool multi = false;
  53. static bool log_verbose = false;
  54. static bool unfiltered_log = false;
  55. bool opt_start_streaming = false;
  56. bool opt_start_recording = false;
  57. bool opt_studio_mode = false;
  58. bool opt_start_replaybuffer = false;
  59. bool opt_minimize_tray = false;
  60. bool opt_allow_opengl = false;
  61. bool opt_always_on_top = false;
  62. string opt_starting_collection;
  63. string opt_starting_profile;
  64. string opt_starting_scene;
  65. // AMD PowerXpress High Performance Flags
  66. #ifdef _MSC_VER
  67. extern "C" __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  68. #endif
  69. QObject *CreateShortcutFilter()
  70. {
  71. return new OBSEventFilter([](QObject *obj, QEvent *event)
  72. {
  73. auto mouse_event = [](QMouseEvent &event)
  74. {
  75. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  76. bool pressed = event.type() == QEvent::MouseButtonPress;
  77. switch (event.button()) {
  78. case Qt::NoButton:
  79. case Qt::LeftButton:
  80. case Qt::RightButton:
  81. case Qt::AllButtons:
  82. case Qt::MouseButtonMask:
  83. return false;
  84. case Qt::MidButton:
  85. hotkey.key = OBS_KEY_MOUSE3;
  86. break;
  87. #define MAP_BUTTON(i, j) case Qt::ExtraButton ## i: \
  88. hotkey.key = OBS_KEY_MOUSE ## j; break;
  89. MAP_BUTTON( 1, 4);
  90. MAP_BUTTON( 2, 5);
  91. MAP_BUTTON( 3, 6);
  92. MAP_BUTTON( 4, 7);
  93. MAP_BUTTON( 5, 8);
  94. MAP_BUTTON( 6, 9);
  95. MAP_BUTTON( 7, 10);
  96. MAP_BUTTON( 8, 11);
  97. MAP_BUTTON( 9, 12);
  98. MAP_BUTTON(10, 13);
  99. MAP_BUTTON(11, 14);
  100. MAP_BUTTON(12, 15);
  101. MAP_BUTTON(13, 16);
  102. MAP_BUTTON(14, 17);
  103. MAP_BUTTON(15, 18);
  104. MAP_BUTTON(16, 19);
  105. MAP_BUTTON(17, 20);
  106. MAP_BUTTON(18, 21);
  107. MAP_BUTTON(19, 22);
  108. MAP_BUTTON(20, 23);
  109. MAP_BUTTON(21, 24);
  110. MAP_BUTTON(22, 25);
  111. MAP_BUTTON(23, 26);
  112. MAP_BUTTON(24, 27);
  113. #undef MAP_BUTTON
  114. }
  115. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  116. event.modifiers());
  117. obs_hotkey_inject_event(hotkey, pressed);
  118. return true;
  119. };
  120. auto key_event = [&](QKeyEvent *event)
  121. {
  122. QDialog *dialog = qobject_cast<QDialog*>(obj);
  123. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  124. bool pressed = event->type() == QEvent::KeyPress;
  125. switch (event->key()) {
  126. case Qt::Key_Shift:
  127. case Qt::Key_Control:
  128. case Qt::Key_Alt:
  129. case Qt::Key_Meta:
  130. break;
  131. #ifdef __APPLE__
  132. case Qt::Key_CapsLock:
  133. // kVK_CapsLock == 57
  134. hotkey.key = obs_key_from_virtual_key(57);
  135. pressed = true;
  136. break;
  137. #endif
  138. case Qt::Key_Enter:
  139. case Qt::Key_Escape:
  140. case Qt::Key_Return:
  141. if (dialog && pressed)
  142. return false;
  143. /* Falls through. */
  144. default:
  145. hotkey.key = obs_key_from_virtual_key(
  146. event->nativeVirtualKey());
  147. }
  148. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  149. event->modifiers());
  150. obs_hotkey_inject_event(hotkey, pressed);
  151. return true;
  152. };
  153. switch (event->type()) {
  154. case QEvent::MouseButtonPress:
  155. case QEvent::MouseButtonRelease:
  156. return mouse_event(*static_cast<QMouseEvent*>(event));
  157. /*case QEvent::MouseButtonDblClick:
  158. case QEvent::Wheel:*/
  159. case QEvent::KeyPress:
  160. case QEvent::KeyRelease:
  161. return key_event(static_cast<QKeyEvent*>(event));
  162. default:
  163. return false;
  164. }
  165. });
  166. }
  167. string CurrentTimeString()
  168. {
  169. using namespace std::chrono;
  170. struct tm tstruct;
  171. char buf[80];
  172. auto tp = system_clock::now();
  173. auto now = system_clock::to_time_t(tp);
  174. tstruct = *localtime(&now);
  175. size_t written = strftime(buf, sizeof(buf), "%X", &tstruct);
  176. if (ratio_less<system_clock::period, seconds::period>::value &&
  177. written && (sizeof(buf) - written) > 5) {
  178. auto tp_secs =
  179. time_point_cast<seconds>(tp);
  180. auto millis =
  181. duration_cast<milliseconds>(tp - tp_secs).count();
  182. snprintf(buf + written, sizeof(buf) - written, ".%03u",
  183. static_cast<unsigned>(millis));
  184. }
  185. return buf;
  186. }
  187. string CurrentDateTimeString()
  188. {
  189. time_t now = time(0);
  190. struct tm tstruct;
  191. char buf[80];
  192. tstruct = *localtime(&now);
  193. strftime(buf, sizeof(buf), "%Y-%m-%d, %X", &tstruct);
  194. return buf;
  195. }
  196. static inline void LogString(fstream &logFile, const char *timeString,
  197. char *str)
  198. {
  199. logFile << timeString << str << endl;
  200. }
  201. static inline void LogStringChunk(fstream &logFile, char *str)
  202. {
  203. char *nextLine = str;
  204. string timeString = CurrentTimeString();
  205. timeString += ": ";
  206. while (*nextLine) {
  207. char *nextLine = strchr(str, '\n');
  208. if (!nextLine)
  209. break;
  210. if (nextLine != str && nextLine[-1] == '\r') {
  211. nextLine[-1] = 0;
  212. } else {
  213. nextLine[0] = 0;
  214. }
  215. LogString(logFile, timeString.c_str(), str);
  216. nextLine++;
  217. str = nextLine;
  218. }
  219. LogString(logFile, timeString.c_str(), str);
  220. }
  221. #define MAX_REPEATED_LINES 30
  222. #define MAX_CHAR_VARIATION (255 * 3)
  223. static inline int sum_chars(const char *str)
  224. {
  225. int val = 0;
  226. for (; *str != 0; str++)
  227. val += *str;
  228. return val;
  229. }
  230. static inline bool too_many_repeated_entries(fstream &logFile, const char *msg,
  231. const char *output_str)
  232. {
  233. static mutex log_mutex;
  234. static const char *last_msg_ptr = nullptr;
  235. static int last_char_sum = 0;
  236. static char cmp_str[4096];
  237. static int rep_count = 0;
  238. int new_sum = sum_chars(output_str);
  239. lock_guard<mutex> guard(log_mutex);
  240. if (unfiltered_log) {
  241. return false;
  242. }
  243. if (last_msg_ptr == msg) {
  244. int diff = std::abs(new_sum - last_char_sum);
  245. if (diff < MAX_CHAR_VARIATION) {
  246. return (rep_count++ >= MAX_REPEATED_LINES);
  247. }
  248. }
  249. if (rep_count > MAX_REPEATED_LINES) {
  250. logFile << CurrentTimeString() <<
  251. ": Last log entry repeated for " <<
  252. to_string(rep_count - MAX_REPEATED_LINES) <<
  253. " more lines" << endl;
  254. }
  255. last_msg_ptr = msg;
  256. strcpy(cmp_str, output_str);
  257. last_char_sum = new_sum;
  258. rep_count = 0;
  259. return false;
  260. }
  261. static void do_log(int log_level, const char *msg, va_list args, void *param)
  262. {
  263. fstream &logFile = *static_cast<fstream*>(param);
  264. char str[4096];
  265. #ifndef _WIN32
  266. va_list args2;
  267. va_copy(args2, args);
  268. #endif
  269. vsnprintf(str, 4095, msg, args);
  270. #ifdef _WIN32
  271. if (IsDebuggerPresent()) {
  272. int wNum = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
  273. if (wNum > 1) {
  274. static wstring wide_buf;
  275. static mutex wide_mutex;
  276. lock_guard<mutex> lock(wide_mutex);
  277. wide_buf.reserve(wNum + 1);
  278. wide_buf.resize(wNum - 1);
  279. MultiByteToWideChar(CP_UTF8, 0, str, -1, &wide_buf[0],
  280. wNum);
  281. wide_buf.push_back('\n');
  282. OutputDebugStringW(wide_buf.c_str());
  283. }
  284. }
  285. #else
  286. def_log_handler(log_level, msg, args2, nullptr);
  287. #endif
  288. if (log_level <= LOG_INFO || log_verbose) {
  289. if (too_many_repeated_entries(logFile, msg, str))
  290. return;
  291. LogStringChunk(logFile, str);
  292. }
  293. #if defined(_WIN32) && defined(OBS_DEBUGBREAK_ON_ERROR)
  294. if (log_level <= LOG_ERROR && IsDebuggerPresent())
  295. __debugbreak();
  296. #endif
  297. }
  298. #define DEFAULT_LANG "en-US"
  299. bool OBSApp::InitGlobalConfigDefaults()
  300. {
  301. config_set_default_string(globalConfig, "General", "Language",
  302. DEFAULT_LANG);
  303. config_set_default_uint(globalConfig, "General", "MaxLogs", 10);
  304. config_set_default_string(globalConfig, "General", "ProcessPriority",
  305. "Normal");
  306. config_set_default_bool(globalConfig, "General", "EnableAutoUpdates",
  307. true);
  308. #if _WIN32
  309. config_set_default_string(globalConfig, "Video", "Renderer",
  310. "Direct3D 11");
  311. #else
  312. config_set_default_string(globalConfig, "Video", "Renderer", "OpenGL");
  313. #endif
  314. config_set_default_bool(globalConfig, "BasicWindow", "PreviewEnabled",
  315. true);
  316. config_set_default_bool(globalConfig, "BasicWindow",
  317. "PreviewProgramMode", false);
  318. config_set_default_bool(globalConfig, "BasicWindow",
  319. "SceneDuplicationMode", true);
  320. config_set_default_bool(globalConfig, "BasicWindow",
  321. "SwapScenesMode", true);
  322. config_set_default_bool(globalConfig, "BasicWindow",
  323. "SnappingEnabled", true);
  324. config_set_default_bool(globalConfig, "BasicWindow",
  325. "ScreenSnapping", true);
  326. config_set_default_bool(globalConfig, "BasicWindow",
  327. "SourceSnapping", true);
  328. config_set_default_bool(globalConfig, "BasicWindow",
  329. "CenterSnapping", false);
  330. config_set_default_double(globalConfig, "BasicWindow",
  331. "SnapDistance", 10.0);
  332. config_set_default_bool(globalConfig, "BasicWindow",
  333. "RecordWhenStreaming", false);
  334. config_set_default_bool(globalConfig, "BasicWindow",
  335. "KeepRecordingWhenStreamStops", false);
  336. config_set_default_bool(globalConfig, "BasicWindow",
  337. "SysTrayEnabled", true);
  338. config_set_default_bool(globalConfig, "BasicWindow",
  339. "SysTrayWhenStarted", false);
  340. config_set_default_bool(globalConfig, "BasicWindow",
  341. "SaveProjectors", false);
  342. config_set_default_bool(globalConfig, "BasicWindow",
  343. "ShowTransitions", true);
  344. config_set_default_bool(globalConfig, "BasicWindow",
  345. "ShowListboxToolbars", true);
  346. config_set_default_bool(globalConfig, "BasicWindow",
  347. "ShowStatusBar", true);
  348. if (!config_get_bool(globalConfig, "General", "Pre21Defaults")) {
  349. config_set_default_string(globalConfig, "General",
  350. "CurrentTheme", "Dark");
  351. }
  352. config_set_default_bool(globalConfig, "BasicWindow",
  353. "VerticalVolControl", false);
  354. config_set_default_bool(globalConfig, "BasicWindow",
  355. "MultiviewMouseSwitch", true);
  356. config_set_default_bool(globalConfig, "BasicWindow",
  357. "MultiviewDrawNames", true);
  358. #ifdef _WIN32
  359. config_set_default_bool(globalConfig, "Audio", "DisableAudioDucking",
  360. true);
  361. #endif
  362. #ifdef __APPLE__
  363. config_set_default_bool(globalConfig, "Video", "DisableOSXVSync", true);
  364. config_set_default_bool(globalConfig, "Video", "ResetOSXVSyncOnExit",
  365. true);
  366. #endif
  367. return true;
  368. }
  369. static bool do_mkdir(const char *path)
  370. {
  371. if (os_mkdirs(path) == MKDIR_ERROR) {
  372. OBSErrorBox(NULL, "Failed to create directory %s", path);
  373. return false;
  374. }
  375. return true;
  376. }
  377. static bool MakeUserDirs()
  378. {
  379. char path[512];
  380. if (GetConfigPath(path, sizeof(path), "obs-studio/basic") <= 0)
  381. return false;
  382. if (!do_mkdir(path))
  383. return false;
  384. if (GetConfigPath(path, sizeof(path), "obs-studio/logs") <= 0)
  385. return false;
  386. if (!do_mkdir(path))
  387. return false;
  388. if (GetConfigPath(path, sizeof(path), "obs-studio/profiler_data") <= 0)
  389. return false;
  390. if (!do_mkdir(path))
  391. return false;
  392. #ifdef _WIN32
  393. if (GetConfigPath(path, sizeof(path), "obs-studio/crashes") <= 0)
  394. return false;
  395. if (!do_mkdir(path))
  396. return false;
  397. if (GetConfigPath(path, sizeof(path), "obs-studio/updates") <= 0)
  398. return false;
  399. if (!do_mkdir(path))
  400. return false;
  401. #endif
  402. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  403. return false;
  404. if (!do_mkdir(path))
  405. return false;
  406. return true;
  407. }
  408. static bool MakeUserProfileDirs()
  409. {
  410. char path[512];
  411. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles") <= 0)
  412. return false;
  413. if (!do_mkdir(path))
  414. return false;
  415. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes") <= 0)
  416. return false;
  417. if (!do_mkdir(path))
  418. return false;
  419. return true;
  420. }
  421. static string GetProfileDirFromName(const char *name)
  422. {
  423. string outputPath;
  424. os_glob_t *glob;
  425. char path[512];
  426. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles") <= 0)
  427. return outputPath;
  428. strcat(path, "/*");
  429. if (os_glob(path, 0, &glob) != 0)
  430. return outputPath;
  431. for (size_t i = 0; i < glob->gl_pathc; i++) {
  432. struct os_globent ent = glob->gl_pathv[i];
  433. if (!ent.directory)
  434. continue;
  435. strcpy(path, ent.path);
  436. strcat(path, "/basic.ini");
  437. ConfigFile config;
  438. if (config.Open(path, CONFIG_OPEN_EXISTING) != 0)
  439. continue;
  440. const char *curName = config_get_string(config, "General",
  441. "Name");
  442. if (astrcmpi(curName, name) == 0) {
  443. outputPath = ent.path;
  444. break;
  445. }
  446. }
  447. os_globfree(glob);
  448. if (!outputPath.empty()) {
  449. replace(outputPath.begin(), outputPath.end(), '\\', '/');
  450. const char *start = strrchr(outputPath.c_str(), '/');
  451. if (start)
  452. outputPath.erase(0, start - outputPath.c_str() + 1);
  453. }
  454. return outputPath;
  455. }
  456. static string GetSceneCollectionFileFromName(const char *name)
  457. {
  458. string outputPath;
  459. os_glob_t *glob;
  460. char path[512];
  461. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes") <= 0)
  462. return outputPath;
  463. strcat(path, "/*.json");
  464. if (os_glob(path, 0, &glob) != 0)
  465. return outputPath;
  466. for (size_t i = 0; i < glob->gl_pathc; i++) {
  467. struct os_globent ent = glob->gl_pathv[i];
  468. if (ent.directory)
  469. continue;
  470. obs_data_t *data =
  471. obs_data_create_from_json_file_safe(ent.path, "bak");
  472. const char *curName = obs_data_get_string(data, "name");
  473. if (astrcmpi(name, curName) == 0) {
  474. outputPath = ent.path;
  475. obs_data_release(data);
  476. break;
  477. }
  478. obs_data_release(data);
  479. }
  480. os_globfree(glob);
  481. if (!outputPath.empty()) {
  482. outputPath.resize(outputPath.size() - 5);
  483. replace(outputPath.begin(), outputPath.end(), '\\', '/');
  484. const char *start = strrchr(outputPath.c_str(), '/');
  485. if (start)
  486. outputPath.erase(0, start - outputPath.c_str() + 1);
  487. }
  488. return outputPath;
  489. }
  490. bool OBSApp::UpdatePre22MultiviewLayout(const char *layout)
  491. {
  492. if (!layout)
  493. return false;
  494. if (astrcmpi(layout, "horizontaltop") == 0) {
  495. config_set_int(globalConfig, "BasicWindow", "MultiviewLayout",
  496. static_cast<int>(
  497. MultiviewLayout::HORIZONTAL_TOP_8_SCENES));
  498. return true;
  499. }
  500. if (astrcmpi(layout, "horizontalbottom") == 0) {
  501. config_set_int(globalConfig, "BasicWindow", "MultiviewLayout",
  502. static_cast<int>(
  503. MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES));
  504. return true;
  505. }
  506. if (astrcmpi(layout, "verticalleft") == 0) {
  507. config_set_int(globalConfig, "BasicWindow", "MultiviewLayout",
  508. static_cast<int>(
  509. MultiviewLayout::VERTICAL_LEFT_8_SCENES));
  510. return true;
  511. }
  512. if (astrcmpi(layout, "verticalright") == 0) {
  513. config_set_int(globalConfig, "BasicWindow", "MultiviewLayout",
  514. static_cast<int>(
  515. MultiviewLayout::VERTICAL_RIGHT_8_SCENES));
  516. return true;
  517. }
  518. return false;
  519. }
  520. bool OBSApp::InitGlobalConfig()
  521. {
  522. char path[512];
  523. bool changed = false;
  524. int len = GetConfigPath(path, sizeof(path),
  525. "obs-studio/global.ini");
  526. if (len <= 0) {
  527. return false;
  528. }
  529. int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
  530. if (errorcode != CONFIG_SUCCESS) {
  531. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  532. return false;
  533. }
  534. if (!opt_starting_collection.empty()) {
  535. string path = GetSceneCollectionFileFromName(
  536. opt_starting_collection.c_str());
  537. if (!path.empty()) {
  538. config_set_string(globalConfig,
  539. "Basic", "SceneCollection",
  540. opt_starting_collection.c_str());
  541. config_set_string(globalConfig,
  542. "Basic", "SceneCollectionFile",
  543. path.c_str());
  544. changed = true;
  545. }
  546. }
  547. if (!opt_starting_profile.empty()) {
  548. string path = GetProfileDirFromName(
  549. opt_starting_profile.c_str());
  550. if (!path.empty()) {
  551. config_set_string(globalConfig, "Basic", "Profile",
  552. opt_starting_profile.c_str());
  553. config_set_string(globalConfig, "Basic", "ProfileDir",
  554. path.c_str());
  555. changed = true;
  556. }
  557. }
  558. if (!config_has_user_value(globalConfig, "General", "Pre19Defaults")) {
  559. uint32_t lastVersion = config_get_int(globalConfig, "General",
  560. "LastVersion");
  561. bool useOldDefaults = lastVersion &&
  562. lastVersion < MAKE_SEMANTIC_VERSION(19, 0, 0);
  563. config_set_bool(globalConfig, "General", "Pre19Defaults",
  564. useOldDefaults);
  565. changed = true;
  566. }
  567. if (!config_has_user_value(globalConfig, "General", "Pre21Defaults")) {
  568. uint32_t lastVersion = config_get_int(globalConfig, "General",
  569. "LastVersion");
  570. bool useOldDefaults = lastVersion &&
  571. lastVersion < MAKE_SEMANTIC_VERSION(21, 0, 0);
  572. config_set_bool(globalConfig, "General", "Pre21Defaults",
  573. useOldDefaults);
  574. changed = true;
  575. }
  576. if (config_has_user_value(globalConfig, "BasicWindow",
  577. "MultiviewLayout")) {
  578. const char *layout = config_get_string(globalConfig,
  579. "BasicWindow", "MultiviewLayout");
  580. changed |= UpdatePre22MultiviewLayout(layout);
  581. }
  582. if (changed)
  583. config_save_safe(globalConfig, "tmp", nullptr);
  584. return InitGlobalConfigDefaults();
  585. }
  586. bool OBSApp::InitLocale()
  587. {
  588. ProfileScope("OBSApp::InitLocale");
  589. const char *lang = config_get_string(globalConfig, "General",
  590. "Language");
  591. locale = lang;
  592. string englishPath;
  593. if (!GetDataFilePath("locale/" DEFAULT_LANG ".ini", englishPath)) {
  594. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".ini");
  595. return false;
  596. }
  597. textLookup = text_lookup_create(englishPath.c_str());
  598. if (!textLookup) {
  599. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  600. englishPath.c_str());
  601. return false;
  602. }
  603. bool userLocale = config_has_user_value(globalConfig, "General",
  604. "Language");
  605. bool defaultLang = astrcmpi(lang, DEFAULT_LANG) == 0;
  606. if (userLocale && defaultLang)
  607. return true;
  608. if (!userLocale && defaultLang) {
  609. for (auto &locale_ : GetPreferredLocales()) {
  610. if (locale_ == lang)
  611. return true;
  612. stringstream file;
  613. file << "locale/" << locale_ << ".ini";
  614. string path;
  615. if (!GetDataFilePath(file.str().c_str(), path))
  616. continue;
  617. if (!text_lookup_add(textLookup, path.c_str()))
  618. continue;
  619. blog(LOG_INFO, "Using preferred locale '%s'",
  620. locale_.c_str());
  621. locale = locale_;
  622. return true;
  623. }
  624. return true;
  625. }
  626. stringstream file;
  627. file << "locale/" << lang << ".ini";
  628. string path;
  629. if (GetDataFilePath(file.str().c_str(), path)) {
  630. if (!text_lookup_add(textLookup, path.c_str()))
  631. blog(LOG_ERROR, "Failed to add locale file '%s'",
  632. path.c_str());
  633. } else {
  634. blog(LOG_ERROR, "Could not find locale file '%s'",
  635. file.str().c_str());
  636. }
  637. return true;
  638. }
  639. bool OBSApp::SetTheme(std::string name, std::string path)
  640. {
  641. theme = name;
  642. /* Check user dir first, then preinstalled themes. */
  643. if (path == "") {
  644. char userDir[512];
  645. name = "themes/" + name + ".qss";
  646. string temp = "obs-studio/" + name;
  647. int ret = GetConfigPath(userDir, sizeof(userDir),
  648. temp.c_str());
  649. if (ret > 0 && QFile::exists(userDir)) {
  650. path = string(userDir);
  651. } else if (!GetDataFilePath(name.c_str(), path)) {
  652. OBSErrorBox(NULL, "Failed to find %s.", name.c_str());
  653. return false;
  654. }
  655. }
  656. QString mpath = QString("file:///") + path.c_str();
  657. setStyleSheet(mpath);
  658. return true;
  659. }
  660. bool OBSApp::InitTheme()
  661. {
  662. const char *themeName = config_get_string(globalConfig, "General",
  663. "CurrentTheme");
  664. if (!themeName) {
  665. /* Use deprecated "Theme" value if available */
  666. themeName = config_get_string(globalConfig,
  667. "General", "Theme");
  668. if (!themeName)
  669. themeName = "Default";
  670. }
  671. if (strcmp(themeName, "Default") != 0 && SetTheme(themeName))
  672. return true;
  673. return SetTheme("Default");
  674. }
  675. OBSApp::OBSApp(int &argc, char **argv, profiler_name_store_t *store)
  676. : QApplication(argc, argv),
  677. profilerNameStore(store)
  678. {
  679. sleepInhibitor = os_inhibit_sleep_create("OBS Video/audio");
  680. }
  681. OBSApp::~OBSApp()
  682. {
  683. #ifdef _WIN32
  684. bool disableAudioDucking = config_get_bool(globalConfig, "Audio",
  685. "DisableAudioDucking");
  686. if (disableAudioDucking)
  687. DisableAudioDucking(false);
  688. #endif
  689. #ifdef __APPLE__
  690. bool vsyncDiabled = config_get_bool(globalConfig, "Video",
  691. "DisableOSXVSync");
  692. bool resetVSync = config_get_bool(globalConfig, "Video",
  693. "ResetOSXVSyncOnExit");
  694. if (vsyncDiabled && resetVSync)
  695. EnableOSXVSync(true);
  696. #endif
  697. os_inhibit_sleep_set_active(sleepInhibitor, false);
  698. os_inhibit_sleep_destroy(sleepInhibitor);
  699. }
  700. static void move_basic_to_profiles(void)
  701. {
  702. char path[512];
  703. char new_path[512];
  704. os_glob_t *glob;
  705. /* if not first time use */
  706. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  707. return;
  708. if (!os_file_exists(path))
  709. return;
  710. /* if the profiles directory doesn't already exist */
  711. if (GetConfigPath(new_path, 512, "obs-studio/basic/profiles") <= 0)
  712. return;
  713. if (os_file_exists(new_path))
  714. return;
  715. if (os_mkdir(new_path) == MKDIR_ERROR)
  716. return;
  717. strcat(new_path, "/");
  718. strcat(new_path, Str("Untitled"));
  719. if (os_mkdir(new_path) == MKDIR_ERROR)
  720. return;
  721. strcat(path, "/*.*");
  722. if (os_glob(path, 0, &glob) != 0)
  723. return;
  724. strcpy(path, new_path);
  725. for (size_t i = 0; i < glob->gl_pathc; i++) {
  726. struct os_globent ent = glob->gl_pathv[i];
  727. char *file;
  728. if (ent.directory)
  729. continue;
  730. file = strrchr(ent.path, '/');
  731. if (!file++)
  732. continue;
  733. if (astrcmpi(file, "scenes.json") == 0)
  734. continue;
  735. strcpy(new_path, path);
  736. strcat(new_path, "/");
  737. strcat(new_path, file);
  738. os_rename(ent.path, new_path);
  739. }
  740. os_globfree(glob);
  741. }
  742. static void move_basic_to_scene_collections(void)
  743. {
  744. char path[512];
  745. char new_path[512];
  746. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  747. return;
  748. if (!os_file_exists(path))
  749. return;
  750. if (GetConfigPath(new_path, 512, "obs-studio/basic/scenes") <= 0)
  751. return;
  752. if (os_file_exists(new_path))
  753. return;
  754. if (os_mkdir(new_path) == MKDIR_ERROR)
  755. return;
  756. strcat(path, "/scenes.json");
  757. strcat(new_path, "/");
  758. strcat(new_path, Str("Untitled"));
  759. strcat(new_path, ".json");
  760. os_rename(path, new_path);
  761. }
  762. void OBSApp::AppInit()
  763. {
  764. ProfileScope("OBSApp::AppInit");
  765. if (!InitApplicationBundle())
  766. throw "Failed to initialize application bundle";
  767. if (!MakeUserDirs())
  768. throw "Failed to create required user directories";
  769. if (!InitGlobalConfig())
  770. throw "Failed to initialize global config";
  771. if (!InitLocale())
  772. throw "Failed to load locale";
  773. if (!InitTheme())
  774. throw "Failed to load theme";
  775. config_set_default_string(globalConfig, "Basic", "Profile",
  776. Str("Untitled"));
  777. config_set_default_string(globalConfig, "Basic", "ProfileDir",
  778. Str("Untitled"));
  779. config_set_default_string(globalConfig, "Basic", "SceneCollection",
  780. Str("Untitled"));
  781. config_set_default_string(globalConfig, "Basic", "SceneCollectionFile",
  782. Str("Untitled"));
  783. #ifdef _WIN32
  784. bool disableAudioDucking = config_get_bool(globalConfig, "Audio",
  785. "DisableAudioDucking");
  786. if (disableAudioDucking)
  787. DisableAudioDucking(true);
  788. #endif
  789. #ifdef __APPLE__
  790. if (config_get_bool(globalConfig, "Video", "DisableOSXVSync"))
  791. EnableOSXVSync(false);
  792. #endif
  793. enableHotkeysInFocus = !config_get_bool(globalConfig, "General",
  794. "DisableHotkeysInFocus");
  795. move_basic_to_profiles();
  796. move_basic_to_scene_collections();
  797. if (!MakeUserProfileDirs())
  798. throw "Failed to create profile directories";
  799. }
  800. const char *OBSApp::GetRenderModule() const
  801. {
  802. const char *renderer = config_get_string(globalConfig, "Video",
  803. "Renderer");
  804. return (astrcmpi(renderer, "Direct3D 11") == 0) ?
  805. DL_D3D11 : DL_OPENGL;
  806. }
  807. static bool StartupOBS(const char *locale, profiler_name_store_t *store)
  808. {
  809. char path[512];
  810. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  811. return false;
  812. return obs_startup(locale, path, store);
  813. }
  814. inline void OBSApp::ResetHotkeyState(bool inFocus)
  815. {
  816. obs_hotkey_enable_background_press(
  817. inFocus || enableHotkeysInFocus);
  818. }
  819. void OBSApp::EnableInFocusHotkeys(bool enable)
  820. {
  821. enableHotkeysInFocus = enable;
  822. ResetHotkeyState(applicationState() != Qt::ApplicationActive);
  823. }
  824. bool OBSApp::OBSInit()
  825. {
  826. ProfileScope("OBSApp::OBSInit");
  827. bool licenseAccepted = config_get_bool(globalConfig, "General",
  828. "LicenseAccepted");
  829. OBSLicenseAgreement agreement(nullptr);
  830. if (licenseAccepted || agreement.exec() == QDialog::Accepted) {
  831. if (!licenseAccepted) {
  832. config_set_bool(globalConfig, "General",
  833. "LicenseAccepted", true);
  834. config_save(globalConfig);
  835. }
  836. if (!StartupOBS(locale.c_str(), GetProfilerNameStore()))
  837. return false;
  838. blog(LOG_INFO, "Portable mode: %s",
  839. portable_mode ? "true" : "false");
  840. setQuitOnLastWindowClosed(false);
  841. mainWindow = new OBSBasic();
  842. mainWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  843. connect(mainWindow, SIGNAL(destroyed()), this, SLOT(quit()));
  844. mainWindow->OBSInit();
  845. connect(this, &QGuiApplication::applicationStateChanged,
  846. [this](Qt::ApplicationState state)
  847. {
  848. ResetHotkeyState(
  849. state != Qt::ApplicationActive);
  850. });
  851. ResetHotkeyState(applicationState() != Qt::ApplicationActive);
  852. return true;
  853. } else {
  854. return false;
  855. }
  856. }
  857. string OBSApp::GetVersionString() const
  858. {
  859. stringstream ver;
  860. #ifdef HAVE_OBSCONFIG_H
  861. ver << OBS_VERSION;
  862. #else
  863. ver << LIBOBS_API_MAJOR_VER << "." <<
  864. LIBOBS_API_MINOR_VER << "." <<
  865. LIBOBS_API_PATCH_VER;
  866. #endif
  867. ver << " (";
  868. #ifdef _WIN32
  869. if (sizeof(void*) == 8)
  870. ver << "64bit, ";
  871. ver << "windows)";
  872. #elif __APPLE__
  873. ver << "mac)";
  874. #elif __FreeBSD__
  875. ver << "freebsd)";
  876. #else /* assume linux for the time being */
  877. ver << "linux)";
  878. #endif
  879. return ver.str();
  880. }
  881. bool OBSApp::IsPortableMode()
  882. {
  883. return portable_mode;
  884. }
  885. #ifdef __APPLE__
  886. #define INPUT_AUDIO_SOURCE "coreaudio_input_capture"
  887. #define OUTPUT_AUDIO_SOURCE "coreaudio_output_capture"
  888. #elif _WIN32
  889. #define INPUT_AUDIO_SOURCE "wasapi_input_capture"
  890. #define OUTPUT_AUDIO_SOURCE "wasapi_output_capture"
  891. #else
  892. #define INPUT_AUDIO_SOURCE "pulse_input_capture"
  893. #define OUTPUT_AUDIO_SOURCE "pulse_output_capture"
  894. #endif
  895. const char *OBSApp::InputAudioSource() const
  896. {
  897. return INPUT_AUDIO_SOURCE;
  898. }
  899. const char *OBSApp::OutputAudioSource() const
  900. {
  901. return OUTPUT_AUDIO_SOURCE;
  902. }
  903. const char *OBSApp::GetLastLog() const
  904. {
  905. return lastLogFile.c_str();
  906. }
  907. const char *OBSApp::GetCurrentLog() const
  908. {
  909. return currentLogFile.c_str();
  910. }
  911. const char *OBSApp::GetLastCrashLog() const
  912. {
  913. return lastCrashLogFile.c_str();
  914. }
  915. bool OBSApp::TranslateString(const char *lookupVal, const char **out) const
  916. {
  917. for (obs_frontend_translate_ui_cb cb : translatorHooks) {
  918. if (cb(lookupVal, out))
  919. return true;
  920. }
  921. return text_lookup_getstr(App()->GetTextLookup(), lookupVal, out);
  922. }
  923. QString OBSTranslator::translate(const char *context, const char *sourceText,
  924. const char *disambiguation, int n) const
  925. {
  926. const char *out = nullptr;
  927. if (!App()->TranslateString(sourceText, &out))
  928. return QString(sourceText);
  929. UNUSED_PARAMETER(context);
  930. UNUSED_PARAMETER(disambiguation);
  931. UNUSED_PARAMETER(n);
  932. return QT_UTF8(out);
  933. }
  934. static bool get_token(lexer *lex, string &str, base_token_type type)
  935. {
  936. base_token token;
  937. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  938. return false;
  939. if (token.type != type)
  940. return false;
  941. str.assign(token.text.array, token.text.len);
  942. return true;
  943. }
  944. static bool expect_token(lexer *lex, const char *str, base_token_type type)
  945. {
  946. base_token token;
  947. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  948. return false;
  949. if (token.type != type)
  950. return false;
  951. return strref_cmp(&token.text, str) == 0;
  952. }
  953. static uint64_t convert_log_name(bool has_prefix, const char *name)
  954. {
  955. BaseLexer lex;
  956. string year, month, day, hour, minute, second;
  957. lexer_start(lex, name);
  958. if (has_prefix) {
  959. string temp;
  960. if (!get_token(lex, temp, BASETOKEN_ALPHA)) return 0;
  961. }
  962. if (!get_token(lex, year, BASETOKEN_DIGIT)) return 0;
  963. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  964. if (!get_token(lex, month, BASETOKEN_DIGIT)) return 0;
  965. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  966. if (!get_token(lex, day, BASETOKEN_DIGIT)) return 0;
  967. if (!get_token(lex, hour, BASETOKEN_DIGIT)) return 0;
  968. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  969. if (!get_token(lex, minute, BASETOKEN_DIGIT)) return 0;
  970. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  971. if (!get_token(lex, second, BASETOKEN_DIGIT)) return 0;
  972. stringstream timestring;
  973. timestring << year << month << day << hour << minute << second;
  974. return std::stoull(timestring.str());
  975. }
  976. static void delete_oldest_file(bool has_prefix, const char *location)
  977. {
  978. BPtr<char> logDir(GetConfigPathPtr(location));
  979. string oldestLog;
  980. uint64_t oldest_ts = (uint64_t)-1;
  981. struct os_dirent *entry;
  982. unsigned int maxLogs = (unsigned int)config_get_uint(
  983. App()->GlobalConfig(), "General", "MaxLogs");
  984. os_dir_t *dir = os_opendir(logDir);
  985. if (dir) {
  986. unsigned int count = 0;
  987. while ((entry = os_readdir(dir)) != NULL) {
  988. if (entry->directory || *entry->d_name == '.')
  989. continue;
  990. uint64_t ts = convert_log_name(has_prefix,
  991. entry->d_name);
  992. if (ts) {
  993. if (ts < oldest_ts) {
  994. oldestLog = entry->d_name;
  995. oldest_ts = ts;
  996. }
  997. count++;
  998. }
  999. }
  1000. os_closedir(dir);
  1001. if (count > maxLogs) {
  1002. stringstream delPath;
  1003. delPath << logDir << "/" << oldestLog;
  1004. os_unlink(delPath.str().c_str());
  1005. }
  1006. }
  1007. }
  1008. static void get_last_log(bool has_prefix, const char *subdir_to_use,
  1009. std::string &last)
  1010. {
  1011. BPtr<char> logDir(GetConfigPathPtr(subdir_to_use));
  1012. struct os_dirent *entry;
  1013. os_dir_t *dir = os_opendir(logDir);
  1014. uint64_t highest_ts = 0;
  1015. if (dir) {
  1016. while ((entry = os_readdir(dir)) != NULL) {
  1017. if (entry->directory || *entry->d_name == '.')
  1018. continue;
  1019. uint64_t ts = convert_log_name(has_prefix,
  1020. entry->d_name);
  1021. if (ts > highest_ts) {
  1022. last = entry->d_name;
  1023. highest_ts = ts;
  1024. }
  1025. }
  1026. os_closedir(dir);
  1027. }
  1028. }
  1029. string GenerateTimeDateFilename(const char *extension, bool noSpace)
  1030. {
  1031. time_t now = time(0);
  1032. char file[256] = {};
  1033. struct tm *cur_time;
  1034. cur_time = localtime(&now);
  1035. snprintf(file, sizeof(file), "%d-%02d-%02d%c%02d-%02d-%02d.%s",
  1036. cur_time->tm_year+1900,
  1037. cur_time->tm_mon+1,
  1038. cur_time->tm_mday,
  1039. noSpace ? '_' : ' ',
  1040. cur_time->tm_hour,
  1041. cur_time->tm_min,
  1042. cur_time->tm_sec,
  1043. extension);
  1044. return string(file);
  1045. }
  1046. string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  1047. const char *format)
  1048. {
  1049. BPtr<char> filename = os_generate_formatted_filename(extension,
  1050. !noSpace, format);
  1051. return string(filename);
  1052. }
  1053. vector<pair<string, string>> GetLocaleNames()
  1054. {
  1055. string path;
  1056. if (!GetDataFilePath("locale.ini", path))
  1057. throw "Could not find locale.ini path";
  1058. ConfigFile ini;
  1059. if (ini.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  1060. throw "Could not open locale.ini";
  1061. size_t sections = config_num_sections(ini);
  1062. vector<pair<string, string>> names;
  1063. names.reserve(sections);
  1064. for (size_t i = 0; i < sections; i++) {
  1065. const char *tag = config_get_section(ini, i);
  1066. const char *name = config_get_string(ini, tag, "Name");
  1067. names.emplace_back(tag, name);
  1068. }
  1069. return names;
  1070. }
  1071. static void create_log_file(fstream &logFile)
  1072. {
  1073. stringstream dst;
  1074. get_last_log(false, "obs-studio/logs", lastLogFile);
  1075. #ifdef _WIN32
  1076. get_last_log(true, "obs-studio/crashes", lastCrashLogFile);
  1077. #endif
  1078. currentLogFile = GenerateTimeDateFilename("txt");
  1079. dst << "obs-studio/logs/" << currentLogFile.c_str();
  1080. BPtr<char> path(GetConfigPathPtr(dst.str().c_str()));
  1081. #ifdef _WIN32
  1082. BPtr<wchar_t> wpath;
  1083. os_utf8_to_wcs_ptr(path, 0, &wpath);
  1084. logFile.open(wpath,
  1085. ios_base::in | ios_base::out | ios_base::trunc);
  1086. #else
  1087. logFile.open(path,
  1088. ios_base::in | ios_base::out | ios_base::trunc);
  1089. #endif
  1090. if (logFile.is_open()) {
  1091. delete_oldest_file(false, "obs-studio/logs");
  1092. base_set_log_handler(do_log, &logFile);
  1093. } else {
  1094. blog(LOG_ERROR, "Failed to open log file");
  1095. }
  1096. }
  1097. static auto ProfilerNameStoreRelease = [](profiler_name_store_t *store)
  1098. {
  1099. profiler_name_store_free(store);
  1100. };
  1101. using ProfilerNameStore =
  1102. std::unique_ptr<profiler_name_store_t,
  1103. decltype(ProfilerNameStoreRelease)>;
  1104. ProfilerNameStore CreateNameStore()
  1105. {
  1106. return ProfilerNameStore{profiler_name_store_create(),
  1107. ProfilerNameStoreRelease};
  1108. }
  1109. static auto SnapshotRelease = [](profiler_snapshot_t *snap)
  1110. {
  1111. profile_snapshot_free(snap);
  1112. };
  1113. using ProfilerSnapshot =
  1114. std::unique_ptr<profiler_snapshot_t, decltype(SnapshotRelease)>;
  1115. ProfilerSnapshot GetSnapshot()
  1116. {
  1117. return ProfilerSnapshot{profile_snapshot_create(), SnapshotRelease};
  1118. }
  1119. static void SaveProfilerData(const ProfilerSnapshot &snap)
  1120. {
  1121. if (currentLogFile.empty())
  1122. return;
  1123. auto pos = currentLogFile.rfind('.');
  1124. if (pos == currentLogFile.npos)
  1125. return;
  1126. #define LITERAL_SIZE(x) x, (sizeof(x) - 1)
  1127. ostringstream dst;
  1128. dst.write(LITERAL_SIZE("obs-studio/profiler_data/"));
  1129. dst.write(currentLogFile.c_str(), pos);
  1130. dst.write(LITERAL_SIZE(".csv.gz"));
  1131. #undef LITERAL_SIZE
  1132. BPtr<char> path = GetConfigPathPtr(dst.str().c_str());
  1133. if (!profiler_snapshot_dump_csv_gz(snap.get(), path))
  1134. blog(LOG_WARNING, "Could not save profiler data to '%s'",
  1135. static_cast<const char*>(path));
  1136. }
  1137. static auto ProfilerFree = [](void *)
  1138. {
  1139. profiler_stop();
  1140. auto snap = GetSnapshot();
  1141. profiler_print(snap.get());
  1142. profiler_print_time_between_calls(snap.get());
  1143. SaveProfilerData(snap);
  1144. profiler_free();
  1145. };
  1146. static const char *run_program_init = "run_program_init";
  1147. static int run_program(fstream &logFile, int argc, char *argv[])
  1148. {
  1149. int ret = -1;
  1150. auto profilerNameStore = CreateNameStore();
  1151. std::unique_ptr<void, decltype(ProfilerFree)>
  1152. prof_release(static_cast<void*>(&ProfilerFree),
  1153. ProfilerFree);
  1154. profiler_start();
  1155. profile_register_root(run_program_init, 0);
  1156. ScopeProfiler prof{run_program_init};
  1157. QCoreApplication::addLibraryPath(".");
  1158. OBSApp program(argc, argv, profilerNameStore.get());
  1159. try {
  1160. bool created_log = false;
  1161. program.AppInit();
  1162. delete_oldest_file(false, "obs-studio/profiler_data");
  1163. OBSTranslator translator;
  1164. program.installTranslator(&translator);
  1165. #ifdef _WIN32
  1166. /* --------------------------------------- */
  1167. /* check and warn if already running */
  1168. bool cancel_launch = false;
  1169. bool already_running = false;
  1170. RunOnceMutex rom = GetRunOnceMutex(already_running);
  1171. if (!already_running) {
  1172. goto run;
  1173. }
  1174. if (!multi) {
  1175. QMessageBox::StandardButtons buttons(
  1176. QMessageBox::Yes | QMessageBox::Cancel);
  1177. QMessageBox mb(QMessageBox::Question,
  1178. QTStr("AlreadyRunning.Title"),
  1179. QTStr("AlreadyRunning.Text"), buttons,
  1180. nullptr);
  1181. mb.setButtonText(QMessageBox::Yes,
  1182. QTStr("AlreadyRunning.LaunchAnyway"));
  1183. mb.setButtonText(QMessageBox::Cancel, QTStr("Cancel"));
  1184. mb.setDefaultButton(QMessageBox::Cancel);
  1185. QMessageBox::StandardButton button;
  1186. button = (QMessageBox::StandardButton)mb.exec();
  1187. cancel_launch = button == QMessageBox::Cancel;
  1188. }
  1189. if (cancel_launch)
  1190. return 0;
  1191. if (!created_log) {
  1192. create_log_file(logFile);
  1193. created_log = true;
  1194. }
  1195. if (multi) {
  1196. blog(LOG_INFO, "User enabled --multi flag and is now "
  1197. "running multiple instances of OBS.");
  1198. } else {
  1199. blog(LOG_WARNING, "================================");
  1200. blog(LOG_WARNING, "Warning: OBS is already running!");
  1201. blog(LOG_WARNING, "================================");
  1202. blog(LOG_WARNING, "User is now running multiple "
  1203. "instances of OBS!");
  1204. }
  1205. /* --------------------------------------- */
  1206. run:
  1207. #endif
  1208. if (!created_log) {
  1209. create_log_file(logFile);
  1210. created_log = true;
  1211. }
  1212. if (argc > 1) {
  1213. stringstream stor;
  1214. stor << argv[1];
  1215. for (int i = 2; i < argc; ++i) {
  1216. stor << " " << argv[i];
  1217. }
  1218. blog(LOG_INFO, "Command Line Arguments: %s", stor.str().c_str());
  1219. }
  1220. if (!program.OBSInit())
  1221. return 0;
  1222. prof.Stop();
  1223. return program.exec();
  1224. } catch (const char *error) {
  1225. blog(LOG_ERROR, "%s", error);
  1226. OBSErrorBox(nullptr, "%s", error);
  1227. }
  1228. return ret;
  1229. }
  1230. #define MAX_CRASH_REPORT_SIZE (150 * 1024)
  1231. #ifdef _WIN32
  1232. #define CRASH_MESSAGE \
  1233. "Woops, OBS has crashed!\n\nWould you like to copy the crash log " \
  1234. "to the clipboard? (Crash logs will still be saved to the " \
  1235. "%appdata%\\obs-studio\\crashes directory)"
  1236. static void main_crash_handler(const char *format, va_list args, void *param)
  1237. {
  1238. char *text = new char[MAX_CRASH_REPORT_SIZE];
  1239. vsnprintf(text, MAX_CRASH_REPORT_SIZE, format, args);
  1240. text[MAX_CRASH_REPORT_SIZE - 1] = 0;
  1241. delete_oldest_file(true, "obs-studio/crashes");
  1242. string name = "obs-studio/crashes/Crash ";
  1243. name += GenerateTimeDateFilename("txt");
  1244. BPtr<char> path(GetConfigPathPtr(name.c_str()));
  1245. fstream file;
  1246. #ifdef _WIN32
  1247. BPtr<wchar_t> wpath;
  1248. os_utf8_to_wcs_ptr(path, 0, &wpath);
  1249. file.open(wpath, ios_base::in | ios_base::out | ios_base::trunc |
  1250. ios_base::binary);
  1251. #else
  1252. file.open(path, ios_base::in | ios_base::out | ios_base::trunc |
  1253. ios_base::binary);
  1254. #endif
  1255. file << text;
  1256. file.close();
  1257. int ret = MessageBoxA(NULL, CRASH_MESSAGE, "OBS has crashed!",
  1258. MB_YESNO | MB_ICONERROR | MB_TASKMODAL);
  1259. if (ret == IDYES) {
  1260. size_t len = strlen(text);
  1261. HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, len);
  1262. memcpy(GlobalLock(mem), text, len);
  1263. GlobalUnlock(mem);
  1264. OpenClipboard(0);
  1265. EmptyClipboard();
  1266. SetClipboardData(CF_TEXT, mem);
  1267. CloseClipboard();
  1268. }
  1269. exit(-1);
  1270. UNUSED_PARAMETER(param);
  1271. }
  1272. static void load_debug_privilege(void)
  1273. {
  1274. const DWORD flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
  1275. TOKEN_PRIVILEGES tp;
  1276. HANDLE token;
  1277. LUID val;
  1278. if (!OpenProcessToken(GetCurrentProcess(), flags, &token)) {
  1279. return;
  1280. }
  1281. if (!!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &val)) {
  1282. tp.PrivilegeCount = 1;
  1283. tp.Privileges[0].Luid = val;
  1284. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1285. AdjustTokenPrivileges(token, false, &tp,
  1286. sizeof(tp), NULL, NULL);
  1287. }
  1288. CloseHandle(token);
  1289. }
  1290. #endif
  1291. #ifdef __APPLE__
  1292. #define BASE_PATH ".."
  1293. #else
  1294. #define BASE_PATH "../.."
  1295. #endif
  1296. #define CONFIG_PATH BASE_PATH "/config"
  1297. #ifndef OBS_UNIX_STRUCTURE
  1298. #define OBS_UNIX_STRUCTURE 0
  1299. #endif
  1300. int GetConfigPath(char *path, size_t size, const char *name)
  1301. {
  1302. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  1303. if (name && *name) {
  1304. return snprintf(path, size, CONFIG_PATH "/%s", name);
  1305. } else {
  1306. return snprintf(path, size, CONFIG_PATH);
  1307. }
  1308. } else {
  1309. return os_get_config_path(path, size, name);
  1310. }
  1311. }
  1312. char *GetConfigPathPtr(const char *name)
  1313. {
  1314. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  1315. char path[512];
  1316. if (snprintf(path, sizeof(path), CONFIG_PATH "/%s", name) > 0) {
  1317. return bstrdup(path);
  1318. } else {
  1319. return NULL;
  1320. }
  1321. } else {
  1322. return os_get_config_path_ptr(name);
  1323. }
  1324. }
  1325. int GetProgramDataPath(char *path, size_t size, const char *name)
  1326. {
  1327. return os_get_program_data_path(path, size, name);
  1328. }
  1329. char *GetProgramDataPathPtr(const char *name)
  1330. {
  1331. return os_get_program_data_path_ptr(name);
  1332. }
  1333. bool GetFileSafeName(const char *name, std::string &file)
  1334. {
  1335. size_t base_len = strlen(name);
  1336. size_t len = os_utf8_to_wcs(name, base_len, nullptr, 0);
  1337. std::wstring wfile;
  1338. if (!len)
  1339. return false;
  1340. wfile.resize(len);
  1341. os_utf8_to_wcs(name, base_len, &wfile[0], len);
  1342. for (size_t i = wfile.size(); i > 0; i--) {
  1343. size_t im1 = i - 1;
  1344. if (iswspace(wfile[im1])) {
  1345. wfile[im1] = '_';
  1346. } else if (wfile[im1] != '_' && !iswalnum(wfile[im1])) {
  1347. wfile.erase(im1, 1);
  1348. }
  1349. }
  1350. if (wfile.size() == 0)
  1351. wfile = L"characters_only";
  1352. len = os_wcs_to_utf8(wfile.c_str(), wfile.size(), nullptr, 0);
  1353. if (!len)
  1354. return false;
  1355. file.resize(len);
  1356. os_wcs_to_utf8(wfile.c_str(), wfile.size(), &file[0], len);
  1357. return true;
  1358. }
  1359. bool GetClosestUnusedFileName(std::string &path, const char *extension)
  1360. {
  1361. size_t len = path.size();
  1362. if (extension) {
  1363. path += ".";
  1364. path += extension;
  1365. }
  1366. if (!os_file_exists(path.c_str()))
  1367. return true;
  1368. int index = 1;
  1369. do {
  1370. path.resize(len);
  1371. path += std::to_string(++index);
  1372. if (extension) {
  1373. path += ".";
  1374. path += extension;
  1375. }
  1376. } while (os_file_exists(path.c_str()));
  1377. return true;
  1378. }
  1379. bool WindowPositionValid(QRect rect)
  1380. {
  1381. for (QScreen* screen: QGuiApplication::screens()) {
  1382. if (screen->availableGeometry().intersects(rect))
  1383. return true;
  1384. }
  1385. return false;
  1386. }
  1387. static inline bool arg_is(const char *arg,
  1388. const char *long_form, const char *short_form)
  1389. {
  1390. return (long_form && strcmp(arg, long_form) == 0) ||
  1391. (short_form && strcmp(arg, short_form) == 0);
  1392. }
  1393. #if !defined(_WIN32) && !defined(__APPLE__)
  1394. #define IS_UNIX 1
  1395. #endif
  1396. /* if using XDG and was previously using an older build of OBS, move config
  1397. * files to XDG directory */
  1398. #if defined(USE_XDG) && defined(IS_UNIX)
  1399. static void move_to_xdg(void)
  1400. {
  1401. char old_path[512];
  1402. char new_path[512];
  1403. char *home = getenv("HOME");
  1404. if (!home)
  1405. return;
  1406. if (snprintf(old_path, 512, "%s/.obs-studio", home) <= 0)
  1407. return;
  1408. /* make base xdg path if it doesn't already exist */
  1409. if (GetConfigPath(new_path, 512, "") <= 0)
  1410. return;
  1411. if (os_mkdirs(new_path) == MKDIR_ERROR)
  1412. return;
  1413. if (GetConfigPath(new_path, 512, "obs-studio") <= 0)
  1414. return;
  1415. if (os_file_exists(old_path) && !os_file_exists(new_path)) {
  1416. rename(old_path, new_path);
  1417. }
  1418. }
  1419. #endif
  1420. static bool update_ffmpeg_output(ConfigFile &config)
  1421. {
  1422. if (config_has_user_value(config, "AdvOut", "FFOutputToFile"))
  1423. return false;
  1424. const char *url = config_get_string(config, "AdvOut", "FFURL");
  1425. if (!url)
  1426. return false;
  1427. bool isActualURL = strstr(url, "://") != nullptr;
  1428. if (isActualURL)
  1429. return false;
  1430. string urlStr = url;
  1431. string extension;
  1432. for (size_t i = urlStr.length(); i > 0; i--) {
  1433. size_t idx = i - 1;
  1434. if (urlStr[idx] == '.') {
  1435. extension = &urlStr[i];
  1436. }
  1437. if (urlStr[idx] == '\\' || urlStr[idx] == '/') {
  1438. urlStr[idx] = 0;
  1439. break;
  1440. }
  1441. }
  1442. if (urlStr.empty() || extension.empty())
  1443. return false;
  1444. config_remove_value(config, "AdvOut", "FFURL");
  1445. config_set_string(config, "AdvOut", "FFFilePath", urlStr.c_str());
  1446. config_set_string(config, "AdvOut", "FFExtension", extension.c_str());
  1447. config_set_bool(config, "AdvOut", "FFOutputToFile", true);
  1448. return true;
  1449. }
  1450. static bool move_reconnect_settings(ConfigFile &config, const char *sec)
  1451. {
  1452. bool changed = false;
  1453. if (config_has_user_value(config, sec, "Reconnect")) {
  1454. bool reconnect = config_get_bool(config, sec, "Reconnect");
  1455. config_set_bool(config, "Output", "Reconnect", reconnect);
  1456. changed = true;
  1457. }
  1458. if (config_has_user_value(config, sec, "RetryDelay")) {
  1459. int delay = (int)config_get_uint(config, sec, "RetryDelay");
  1460. config_set_uint(config, "Output", "RetryDelay", delay);
  1461. changed = true;
  1462. }
  1463. if (config_has_user_value(config, sec, "MaxRetries")) {
  1464. int retries = (int)config_get_uint(config, sec, "MaxRetries");
  1465. config_set_uint(config, "Output", "MaxRetries", retries);
  1466. changed = true;
  1467. }
  1468. return changed;
  1469. }
  1470. static bool update_reconnect(ConfigFile &config)
  1471. {
  1472. if (!config_has_user_value(config, "Output", "Mode"))
  1473. return false;
  1474. const char *mode = config_get_string(config, "Output", "Mode");
  1475. if (!mode)
  1476. return false;
  1477. const char *section = (strcmp(mode, "Advanced") == 0) ?
  1478. "AdvOut" : "SimpleOutput";
  1479. if (move_reconnect_settings(config, section)) {
  1480. config_remove_value(config, "SimpleOutput", "Reconnect");
  1481. config_remove_value(config, "SimpleOutput", "RetryDelay");
  1482. config_remove_value(config, "SimpleOutput", "MaxRetries");
  1483. config_remove_value(config, "AdvOut", "Reconnect");
  1484. config_remove_value(config, "AdvOut", "RetryDelay");
  1485. config_remove_value(config, "AdvOut", "MaxRetries");
  1486. return true;
  1487. }
  1488. return false;
  1489. }
  1490. static void convert_x264_settings(obs_data_t *data)
  1491. {
  1492. bool use_bufsize = obs_data_get_bool(data, "use_bufsize");
  1493. if (use_bufsize) {
  1494. int buffer_size = (int)obs_data_get_int(data, "buffer_size");
  1495. if (buffer_size == 0)
  1496. obs_data_set_string(data, "rate_control", "CRF");
  1497. }
  1498. }
  1499. static void convert_14_2_encoder_setting(const char *encoder, const char *file)
  1500. {
  1501. obs_data_t *data = obs_data_create_from_json_file_safe(file, "bak");
  1502. obs_data_item_t *cbr_item = obs_data_item_byname(data, "cbr");
  1503. obs_data_item_t *rc_item = obs_data_item_byname(data, "rate_control");
  1504. bool modified = false;
  1505. bool cbr = true;
  1506. if (cbr_item) {
  1507. cbr = obs_data_item_get_bool(cbr_item);
  1508. obs_data_item_unset_user_value(cbr_item);
  1509. obs_data_set_string(data, "rate_control", cbr ? "CBR" : "VBR");
  1510. modified = true;
  1511. }
  1512. if (!rc_item && astrcmpi(encoder, "obs_x264") == 0) {
  1513. if (!cbr_item)
  1514. obs_data_set_string(data, "rate_control", "CBR");
  1515. else if (!cbr)
  1516. convert_x264_settings(data);
  1517. modified = true;
  1518. }
  1519. if (modified)
  1520. obs_data_save_json_safe(data, file, "tmp", "bak");
  1521. obs_data_item_release(&rc_item);
  1522. obs_data_item_release(&cbr_item);
  1523. obs_data_release(data);
  1524. }
  1525. static void upgrade_settings(void)
  1526. {
  1527. char path[512];
  1528. int pathlen = GetConfigPath(path, 512, "obs-studio/basic/profiles");
  1529. if (pathlen <= 0)
  1530. return;
  1531. if (!os_file_exists(path))
  1532. return;
  1533. os_dir_t *dir = os_opendir(path);
  1534. if (!dir)
  1535. return;
  1536. struct os_dirent *ent = os_readdir(dir);
  1537. while (ent) {
  1538. if (ent->directory && strcmp(ent->d_name, ".") != 0 &&
  1539. strcmp(ent->d_name, "..") != 0) {
  1540. strcat(path, "/");
  1541. strcat(path, ent->d_name);
  1542. strcat(path, "/basic.ini");
  1543. ConfigFile config;
  1544. int ret;
  1545. ret = config.Open(path, CONFIG_OPEN_EXISTING);
  1546. if (ret == CONFIG_SUCCESS) {
  1547. if (update_ffmpeg_output(config) ||
  1548. update_reconnect(config)) {
  1549. config_save_safe(config, "tmp",
  1550. nullptr);
  1551. }
  1552. }
  1553. if (config) {
  1554. const char *sEnc = config_get_string(config,
  1555. "AdvOut", "Encoder");
  1556. const char *rEnc = config_get_string(config,
  1557. "AdvOut", "RecEncoder");
  1558. /* replace "cbr" option with "rate_control" for
  1559. * each profile's encoder data */
  1560. path[pathlen] = 0;
  1561. strcat(path, "/");
  1562. strcat(path, ent->d_name);
  1563. strcat(path, "/recordEncoder.json");
  1564. convert_14_2_encoder_setting(rEnc, path);
  1565. path[pathlen] = 0;
  1566. strcat(path, "/");
  1567. strcat(path, ent->d_name);
  1568. strcat(path, "/streamEncoder.json");
  1569. convert_14_2_encoder_setting(sEnc, path);
  1570. }
  1571. path[pathlen] = 0;
  1572. }
  1573. ent = os_readdir(dir);
  1574. }
  1575. os_closedir(dir);
  1576. }
  1577. int main(int argc, char *argv[])
  1578. {
  1579. #ifndef _WIN32
  1580. signal(SIGPIPE, SIG_IGN);
  1581. #endif
  1582. #ifdef _WIN32
  1583. obs_init_win32_crash_handler();
  1584. SetErrorMode(SEM_FAILCRITICALERRORS);
  1585. load_debug_privilege();
  1586. base_set_crash_handler(main_crash_handler, nullptr);
  1587. #endif
  1588. base_get_log_handler(&def_log_handler, nullptr);
  1589. #if defined(USE_XDG) && defined(IS_UNIX)
  1590. move_to_xdg();
  1591. #endif
  1592. for (int i = 1; i < argc; i++) {
  1593. if (arg_is(argv[i], "--portable", "-p")) {
  1594. portable_mode = true;
  1595. } else if (arg_is(argv[i], "--multi", "-m")) {
  1596. multi = true;
  1597. } else if (arg_is(argv[i], "--verbose", nullptr)) {
  1598. log_verbose = true;
  1599. } else if (arg_is(argv[i], "--always-on-top", nullptr)) {
  1600. opt_always_on_top = true;
  1601. } else if (arg_is(argv[i], "--unfiltered_log", nullptr)) {
  1602. unfiltered_log = true;
  1603. } else if (arg_is(argv[i], "--startstreaming", nullptr)) {
  1604. opt_start_streaming = true;
  1605. } else if (arg_is(argv[i], "--startrecording", nullptr)) {
  1606. opt_start_recording = true;
  1607. } else if (arg_is(argv[i], "--startreplaybuffer", nullptr)) {
  1608. opt_start_replaybuffer = true;
  1609. } else if (arg_is(argv[i], "--collection", nullptr)) {
  1610. if (++i < argc) opt_starting_collection = argv[i];
  1611. } else if (arg_is(argv[i], "--profile", nullptr)) {
  1612. if (++i < argc) opt_starting_profile = argv[i];
  1613. } else if (arg_is(argv[i], "--scene", nullptr)) {
  1614. if (++i < argc) opt_starting_scene = argv[i];
  1615. } else if (arg_is(argv[i], "--minimize-to-tray", nullptr)) {
  1616. opt_minimize_tray = true;
  1617. } else if (arg_is(argv[i], "--studio-mode", nullptr)) {
  1618. opt_studio_mode = true;
  1619. } else if (arg_is(argv[i], "--allow-opengl", nullptr)) {
  1620. opt_allow_opengl = true;
  1621. } else if (arg_is(argv[i], "--help", "-h")) {
  1622. std::cout <<
  1623. "--help, -h: Get list of available commands.\n\n" <<
  1624. "--startstreaming: Automatically start streaming.\n" <<
  1625. "--startrecording: Automatically start recording.\n" <<
  1626. "--startreplaybuffer: Start replay buffer.\n\n" <<
  1627. "--collection <string>: Use specific scene collection."
  1628. << "\n" <<
  1629. "--profile <string>: Use specific profile.\n" <<
  1630. "--scene <string>: Start with specific scene.\n\n" <<
  1631. "--studio-mode: Enable studio mode.\n" <<
  1632. "--minimize-to-tray: Minimize to system tray.\n" <<
  1633. "--portable, -p: Use portable mode.\n" <<
  1634. "--multi, -m: Don't warn when launching multiple instances.\n\n" <<
  1635. "--verbose: Make log more verbose.\n" <<
  1636. "--always-on-top: Start in 'always on top' mode.\n\n" <<
  1637. "--unfiltered_log: Make log unfiltered.\n\n" <<
  1638. "--allow-opengl: Allow OpenGL on Windows.\n\n" <<
  1639. "--version, -V: Get current version.\n";
  1640. exit(0);
  1641. } else if (arg_is(argv[i], "--version", "-V")) {
  1642. std::cout << "OBS Studio - " <<
  1643. App()->GetVersionString() << "\n";
  1644. exit(0);
  1645. }
  1646. }
  1647. #if !OBS_UNIX_STRUCTURE
  1648. if (!portable_mode) {
  1649. portable_mode =
  1650. os_file_exists(BASE_PATH "/portable_mode") ||
  1651. os_file_exists(BASE_PATH "/obs_portable_mode") ||
  1652. os_file_exists(BASE_PATH "/portable_mode.txt") ||
  1653. os_file_exists(BASE_PATH "/obs_portable_mode.txt");
  1654. }
  1655. #endif
  1656. upgrade_settings();
  1657. fstream logFile;
  1658. curl_global_init(CURL_GLOBAL_ALL);
  1659. int ret = run_program(logFile, argc, argv);
  1660. blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs());
  1661. base_set_log_handler(nullptr, nullptr);
  1662. return ret;
  1663. }