obs-app.cpp 49 KB

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