OBSBasic.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. #include "ui-config.h"
  18. #include "ColorSelect.hpp"
  19. #include "OBSBasicControls.hpp"
  20. #include "OBSBasicStats.hpp"
  21. #include "VolControl.hpp"
  22. #ifdef YOUTUBE_ENABLED
  23. #include <docks/YouTubeAppDock.hpp>
  24. #endif
  25. #include <dialogs/NameDialog.hpp>
  26. #include <dialogs/OBSAbout.hpp>
  27. #include <dialogs/OBSBasicAdvAudio.hpp>
  28. #include <dialogs/OBSBasicFilters.hpp>
  29. #include <dialogs/OBSBasicInteraction.hpp>
  30. #include <dialogs/OBSBasicProperties.hpp>
  31. #include <dialogs/OBSBasicTransform.hpp>
  32. #include <models/SceneCollection.hpp>
  33. #include <settings/OBSBasicSettings.hpp>
  34. #include <utility/QuickTransition.hpp>
  35. #include <utility/SceneRenameDelegate.hpp>
  36. #if defined(_WIN32) || defined(WHATSNEW_ENABLED)
  37. #include <utility/WhatsNewInfoThread.hpp>
  38. #endif
  39. #include <widgets/OBSProjector.hpp>
  40. #include <OBSStudioAPI.hpp>
  41. #ifdef BROWSER_AVAILABLE
  42. #include <browser-panel.hpp>
  43. #endif
  44. #ifdef ENABLE_WAYLAND
  45. #include <obs-nix-platform.h>
  46. #endif
  47. #include <qt-wrappers.hpp>
  48. #include <QActionGroup>
  49. #include <QThread>
  50. #include <QWidgetAction>
  51. #ifdef _WIN32
  52. #include <sstream>
  53. #endif
  54. #include <string>
  55. #include <unordered_set>
  56. #ifdef _WIN32
  57. #define WIN32_LEAN_AND_MEAN
  58. #include "Windows.h"
  59. #endif
  60. #include "moc_OBSBasic.cpp"
  61. using namespace std;
  62. extern bool portable_mode;
  63. extern bool disable_3p_plugins;
  64. extern bool opt_studio_mode;
  65. extern bool opt_always_on_top;
  66. extern bool opt_minimize_tray;
  67. extern std::string opt_starting_profile;
  68. extern std::string opt_starting_collection;
  69. extern bool safe_mode;
  70. extern bool opt_start_recording;
  71. extern bool opt_start_replaybuffer;
  72. extern bool opt_start_virtualcam;
  73. extern volatile long insideEventLoop;
  74. extern bool restart;
  75. extern bool EncoderAvailable(const char *encoder);
  76. extern void RegisterTwitchAuth();
  77. extern void RegisterRestreamAuth();
  78. #ifdef YOUTUBE_ENABLED
  79. extern void RegisterYoutubeAuth();
  80. #endif
  81. struct QCef;
  82. extern QCef *cef;
  83. extern bool cef_js_avail;
  84. extern void DestroyPanelCookieManager();
  85. extern void CheckExistingCookieId();
  86. static void AddExtraModulePaths()
  87. {
  88. string plugins_path, plugins_data_path;
  89. char *s;
  90. s = getenv("OBS_PLUGINS_PATH");
  91. if (s)
  92. plugins_path = s;
  93. s = getenv("OBS_PLUGINS_DATA_PATH");
  94. if (s)
  95. plugins_data_path = s;
  96. if (!plugins_path.empty() && !plugins_data_path.empty()) {
  97. #if defined(__APPLE__)
  98. plugins_path += "/%module%.plugin/Contents/MacOS";
  99. plugins_data_path += "/%module%.plugin/Contents/Resources";
  100. obs_add_module_path(plugins_path.c_str(), plugins_data_path.c_str());
  101. #else
  102. string data_path_with_module_suffix;
  103. data_path_with_module_suffix += plugins_data_path;
  104. data_path_with_module_suffix += "/%module%";
  105. obs_add_module_path(plugins_path.c_str(), data_path_with_module_suffix.c_str());
  106. #endif
  107. }
  108. if (portable_mode)
  109. return;
  110. char base_module_dir[512];
  111. #if defined(_WIN32)
  112. int ret = GetProgramDataPath(base_module_dir, sizeof(base_module_dir), "obs-studio/plugins/%module%");
  113. #elif defined(__APPLE__)
  114. int ret = GetAppConfigPath(base_module_dir, sizeof(base_module_dir), "obs-studio/plugins/%module%.plugin");
  115. #else
  116. int ret = GetAppConfigPath(base_module_dir, sizeof(base_module_dir), "obs-studio/plugins/%module%");
  117. #endif
  118. if (ret <= 0)
  119. return;
  120. string path = base_module_dir;
  121. #if defined(__APPLE__)
  122. /* User Application Support Search Path */
  123. obs_add_module_path((path + "/Contents/MacOS").c_str(), (path + "/Contents/Resources").c_str());
  124. #ifndef __aarch64__
  125. /* Legacy System Library Search Path */
  126. char system_legacy_module_dir[PATH_MAX];
  127. GetProgramDataPath(system_legacy_module_dir, sizeof(system_legacy_module_dir), "obs-studio/plugins/%module%");
  128. std::string path_system_legacy = system_legacy_module_dir;
  129. obs_add_module_path((path_system_legacy + "/bin").c_str(), (path_system_legacy + "/data").c_str());
  130. /* Legacy User Application Support Search Path */
  131. char user_legacy_module_dir[PATH_MAX];
  132. GetAppConfigPath(user_legacy_module_dir, sizeof(user_legacy_module_dir), "obs-studio/plugins/%module%");
  133. std::string path_user_legacy = user_legacy_module_dir;
  134. obs_add_module_path((path_user_legacy + "/bin").c_str(), (path_user_legacy + "/data").c_str());
  135. #endif
  136. #else
  137. #if ARCH_BITS == 64
  138. obs_add_module_path((path + "/bin/64bit").c_str(), (path + "/data").c_str());
  139. #else
  140. obs_add_module_path((path + "/bin/32bit").c_str(), (path + "/data").c_str());
  141. #endif
  142. #endif
  143. }
  144. /* First-party modules considered to be potentially unsafe to load in Safe Mode
  145. * due to them allowing external code (e.g. scripts) to modify OBS's state. */
  146. static const unordered_set<string> unsafe_modules = {
  147. "frontend-tools", // Scripting
  148. "obs-websocket", // Allows outside modifications
  149. };
  150. static void SetSafeModuleNames()
  151. {
  152. #ifndef SAFE_MODULES
  153. return;
  154. #else
  155. string module;
  156. stringstream modules(SAFE_MODULES);
  157. while (getline(modules, module, '|')) {
  158. /* When only disallowing third-party plugins, still add
  159. * "unsafe" bundled modules to the safe list. */
  160. if (disable_3p_plugins || !unsafe_modules.count(module))
  161. obs_add_safe_module(module.c_str());
  162. }
  163. #endif
  164. }
  165. extern void setupDockAction(QDockWidget *dock);
  166. OBSBasic::OBSBasic(QWidget *parent) : OBSMainWindow(parent), undo_s(ui), ui(new Ui::OBSBasic)
  167. {
  168. collections = {};
  169. setAttribute(Qt::WA_NativeWindow);
  170. #ifdef TWITCH_ENABLED
  171. RegisterTwitchAuth();
  172. #endif
  173. #ifdef RESTREAM_ENABLED
  174. RegisterRestreamAuth();
  175. #endif
  176. #ifdef YOUTUBE_ENABLED
  177. RegisterYoutubeAuth();
  178. #endif
  179. setAcceptDrops(true);
  180. setContextMenuPolicy(Qt::CustomContextMenu);
  181. QEvent::registerEventType(QEvent::User + QEvent::Close);
  182. api = InitializeAPIInterface(this);
  183. ui->setupUi(this);
  184. ui->previewDisabledWidget->setVisible(false);
  185. /* Set up streaming connections */
  186. connect(
  187. this, &OBSBasic::StreamingStarting, this, [this] { this->streamingStarting = true; },
  188. Qt::DirectConnection);
  189. connect(
  190. this, &OBSBasic::StreamingStarted, this, [this] { this->streamingStarting = false; },
  191. Qt::DirectConnection);
  192. connect(
  193. this, &OBSBasic::StreamingStopped, this, [this] { this->streamingStarting = false; },
  194. Qt::DirectConnection);
  195. /* Set up recording connections */
  196. connect(
  197. this, &OBSBasic::RecordingStarted, this,
  198. [this]() {
  199. this->recordingStarted = true;
  200. this->recordingPaused = false;
  201. },
  202. Qt::DirectConnection);
  203. connect(
  204. this, &OBSBasic::RecordingPaused, this, [this]() { this->recordingPaused = true; },
  205. Qt::DirectConnection);
  206. connect(
  207. this, &OBSBasic::RecordingUnpaused, this, [this]() { this->recordingPaused = false; },
  208. Qt::DirectConnection);
  209. connect(
  210. this, &OBSBasic::RecordingStopped, this,
  211. [this]() {
  212. this->recordingStarted = false;
  213. this->recordingPaused = false;
  214. },
  215. Qt::DirectConnection);
  216. /* Add controls dock */
  217. OBSBasicControls *controls = new OBSBasicControls(this);
  218. controlsDock = new OBSDock(this);
  219. controlsDock->setObjectName(QString::fromUtf8("controlsDock"));
  220. controlsDock->setWindowTitle(QTStr("Basic.Main.Controls"));
  221. /* Parenting is done there so controls will be deleted alongside controlsDock */
  222. controlsDock->setWidget(controls);
  223. addDockWidget(Qt::BottomDockWidgetArea, controlsDock);
  224. connect(controls, &OBSBasicControls::StreamButtonClicked, this, &OBSBasic::StreamActionTriggered);
  225. connect(controls, &OBSBasicControls::StartStreamMenuActionClicked, this, &OBSBasic::StartStreaming);
  226. connect(controls, &OBSBasicControls::StopStreamMenuActionClicked, this, &OBSBasic::StopStreaming);
  227. connect(controls, &OBSBasicControls::ForceStopStreamMenuActionClicked, this, &OBSBasic::ForceStopStreaming);
  228. connect(controls, &OBSBasicControls::BroadcastButtonClicked, this, &OBSBasic::BroadcastButtonClicked);
  229. connect(controls, &OBSBasicControls::RecordButtonClicked, this, &OBSBasic::RecordActionTriggered);
  230. connect(controls, &OBSBasicControls::PauseRecordButtonClicked, this, &OBSBasic::RecordPauseToggled);
  231. connect(controls, &OBSBasicControls::ReplayBufferButtonClicked, this, &OBSBasic::ReplayBufferActionTriggered);
  232. connect(controls, &OBSBasicControls::SaveReplayBufferButtonClicked, this, &OBSBasic::ReplayBufferSave);
  233. connect(controls, &OBSBasicControls::VirtualCamButtonClicked, this, &OBSBasic::VirtualCamActionTriggered);
  234. connect(controls, &OBSBasicControls::VirtualCamConfigButtonClicked, this, &OBSBasic::OpenVirtualCamConfig);
  235. connect(controls, &OBSBasicControls::StudioModeButtonClicked, this, &OBSBasic::TogglePreviewProgramMode);
  236. connect(controls, &OBSBasicControls::SettingsButtonClicked, this, &OBSBasic::on_action_Settings_triggered);
  237. startingDockLayout = saveState();
  238. statsDock = new OBSDock();
  239. statsDock->setObjectName(QStringLiteral("statsDock"));
  240. statsDock->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable |
  241. QDockWidget::DockWidgetFloatable);
  242. statsDock->setWindowTitle(QTStr("Basic.Stats"));
  243. addDockWidget(Qt::BottomDockWidgetArea, statsDock);
  244. statsDock->setVisible(false);
  245. statsDock->setFloating(true);
  246. statsDock->resize(700, 200);
  247. copyActionsDynamicProperties();
  248. qRegisterMetaType<int64_t>("int64_t");
  249. qRegisterMetaType<uint32_t>("uint32_t");
  250. qRegisterMetaType<OBSScene>("OBSScene");
  251. qRegisterMetaType<OBSSceneItem>("OBSSceneItem");
  252. qRegisterMetaType<OBSSource>("OBSSource");
  253. qRegisterMetaType<obs_hotkey_id>("obs_hotkey_id");
  254. qRegisterMetaType<SavedProjectorInfo *>("SavedProjectorInfo *");
  255. ui->scenes->setAttribute(Qt::WA_MacShowFocusRect, false);
  256. ui->sources->setAttribute(Qt::WA_MacShowFocusRect, false);
  257. bool sceneGrid = config_get_bool(App()->GetUserConfig(), "BasicWindow", "gridMode");
  258. ui->scenes->SetGridMode(sceneGrid);
  259. if (sceneGrid)
  260. ui->actionSceneGridMode->setChecked(true);
  261. else
  262. ui->actionSceneListMode->setChecked(true);
  263. ui->scenes->setItemDelegate(new SceneRenameDelegate(ui->scenes));
  264. auto displayResize = [this]() {
  265. struct obs_video_info ovi;
  266. if (obs_get_video_info(&ovi))
  267. ResizePreview(ovi.base_width, ovi.base_height);
  268. UpdateContextBarVisibility();
  269. UpdatePreviewControls();
  270. dpi = devicePixelRatioF();
  271. };
  272. dpi = devicePixelRatioF();
  273. connect(windowHandle(), &QWindow::screenChanged, displayResize);
  274. connect(ui->preview, &OBSQTDisplay::DisplayResized, displayResize);
  275. /* TODO: Move these into window-basic-preview */
  276. /* Preview Scaling label */
  277. connect(ui->preview, &OBSBasicPreview::scalingChanged, ui->previewScalePercent,
  278. &OBSPreviewScalingLabel::PreviewScaleChanged);
  279. /* Preview Scaling dropdown */
  280. connect(ui->preview, &OBSBasicPreview::scalingChanged, ui->previewScalingMode,
  281. &OBSPreviewScalingComboBox::PreviewScaleChanged);
  282. connect(ui->preview, &OBSBasicPreview::fixedScalingChanged, ui->previewScalingMode,
  283. &OBSPreviewScalingComboBox::PreviewFixedScalingChanged);
  284. connect(ui->previewScalingMode, &OBSPreviewScalingComboBox::currentIndexChanged, this,
  285. &OBSBasic::PreviewScalingModeChanged);
  286. /* Preview Controls */
  287. connect(ui->previewXScrollBar, &QScrollBar::sliderMoved, ui->preview, &OBSBasicPreview::xScrollBarChanged);
  288. connect(ui->previewYScrollBar, &QScrollBar::valueChanged, ui->preview, &OBSBasicPreview::yScrollBarChanged);
  289. connect(ui->previewZoomInButton, &QPushButton::clicked, ui->preview, &OBSBasicPreview::increaseScalingLevel);
  290. connect(ui->previewZoomOutButton, &QPushButton::clicked, ui->preview, &OBSBasicPreview::decreaseScalingLevel);
  291. /* Preview Actions */
  292. connect(ui->actionScaleWindow, &QAction::triggered, this, &OBSBasic::setPreviewScalingWindow);
  293. connect(ui->actionScaleCanvas, &QAction::triggered, this, &OBSBasic::setPreviewScalingCanvas);
  294. connect(ui->actionScaleOutput, &QAction::triggered, this, &OBSBasic::setPreviewScalingOutput);
  295. connect(ui->actionPreviewZoomIn, &QAction::triggered, ui->preview, &OBSBasicPreview::increaseScalingLevel);
  296. connect(ui->actionPreviewZoomOut, &QAction::triggered, ui->preview, &OBSBasicPreview::decreaseScalingLevel);
  297. connect(ui->actionPreviewResetZoom, &QAction::triggered, ui->preview, &OBSBasicPreview::resetScalingLevel);
  298. connect(this, &OBSBasic::CanvasResized, ui->previewScalingMode, &OBSPreviewScalingComboBox::CanvasResized);
  299. connect(this, &OBSBasic::OutputResized, ui->previewScalingMode, &OBSPreviewScalingComboBox::OutputResized);
  300. delete shortcutFilter;
  301. shortcutFilter = CreateShortcutFilter();
  302. installEventFilter(shortcutFilter);
  303. stringstream name;
  304. name << "OBS " << App()->GetVersionString();
  305. blog(LOG_INFO, "%s", name.str().c_str());
  306. blog(LOG_INFO, "---------------------------------");
  307. UpdateTitleBar();
  308. connect(ui->scenes->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, &OBSBasic::SceneNameEdited);
  309. cpuUsageInfo = os_cpu_usage_info_start();
  310. cpuUsageTimer = new QTimer(this);
  311. connect(cpuUsageTimer.data(), &QTimer::timeout, ui->statusbar, &OBSBasicStatusBar::UpdateCPUUsage);
  312. cpuUsageTimer->start(3000);
  313. diskFullTimer = new QTimer(this);
  314. connect(diskFullTimer, &QTimer::timeout, this, &OBSBasic::CheckDiskSpaceRemaining);
  315. renameScene = new QAction(QTStr("Rename"), ui->scenesDock);
  316. renameScene->setShortcutContext(Qt::WidgetWithChildrenShortcut);
  317. connect(renameScene, &QAction::triggered, this, &OBSBasic::EditSceneName);
  318. ui->scenesDock->addAction(renameScene);
  319. renameSource = new QAction(QTStr("Rename"), ui->sourcesDock);
  320. renameSource->setShortcutContext(Qt::WidgetWithChildrenShortcut);
  321. connect(renameSource, &QAction::triggered, this, &OBSBasic::EditSceneItemName);
  322. ui->sourcesDock->addAction(renameSource);
  323. #ifdef __APPLE__
  324. renameScene->setShortcut({Qt::Key_Return});
  325. renameSource->setShortcut({Qt::Key_Return});
  326. ui->actionRemoveSource->setShortcuts({Qt::Key_Backspace});
  327. ui->actionRemoveScene->setShortcuts({Qt::Key_Backspace});
  328. ui->actionCheckForUpdates->setMenuRole(QAction::AboutQtRole);
  329. ui->action_Settings->setMenuRole(QAction::PreferencesRole);
  330. ui->actionShowMacPermissions->setMenuRole(QAction::ApplicationSpecificRole);
  331. ui->actionE_xit->setMenuRole(QAction::QuitRole);
  332. #else
  333. renameScene->setShortcut({Qt::Key_F2});
  334. renameSource->setShortcut({Qt::Key_F2});
  335. #endif
  336. #ifdef __linux__
  337. ui->actionE_xit->setShortcut(Qt::CTRL | Qt::Key_Q);
  338. #endif
  339. #ifndef ENABLE_IDIAN_PLAYGROUND
  340. ui->idianPlayground->setVisible(false);
  341. #endif
  342. auto addNudge = [this](const QKeySequence &seq, MoveDir direction, int distance) {
  343. QAction *nudge = new QAction(ui->preview);
  344. nudge->setShortcut(seq);
  345. nudge->setShortcutContext(Qt::WidgetShortcut);
  346. ui->preview->addAction(nudge);
  347. connect(nudge, &QAction::triggered, [this, distance, direction]() { Nudge(distance, direction); });
  348. };
  349. addNudge(Qt::Key_Up, MoveDir::Up, 1);
  350. addNudge(Qt::Key_Down, MoveDir::Down, 1);
  351. addNudge(Qt::Key_Left, MoveDir::Left, 1);
  352. addNudge(Qt::Key_Right, MoveDir::Right, 1);
  353. addNudge(Qt::SHIFT | Qt::Key_Up, MoveDir::Up, 10);
  354. addNudge(Qt::SHIFT | Qt::Key_Down, MoveDir::Down, 10);
  355. addNudge(Qt::SHIFT | Qt::Key_Left, MoveDir::Left, 10);
  356. addNudge(Qt::SHIFT | Qt::Key_Right, MoveDir::Right, 10);
  357. /* Setup dock toggle action
  358. * And hide all docks before restoring parent geometry */
  359. #define SETUP_DOCK(dock) \
  360. setupDockAction(dock); \
  361. ui->menuDocks->addAction(dock->toggleViewAction()); \
  362. dock->setVisible(false);
  363. SETUP_DOCK(ui->scenesDock);
  364. SETUP_DOCK(ui->sourcesDock);
  365. SETUP_DOCK(ui->mixerDock);
  366. SETUP_DOCK(ui->transitionsDock);
  367. SETUP_DOCK(controlsDock);
  368. SETUP_DOCK(statsDock);
  369. #undef SETUP_DOCK
  370. // Register shortcuts for Undo/Redo
  371. ui->actionMainUndo->setShortcut(Qt::CTRL | Qt::Key_Z);
  372. QList<QKeySequence> shrt;
  373. shrt << QKeySequence((Qt::CTRL | Qt::SHIFT) | Qt::Key_Z) << QKeySequence(Qt::CTRL | Qt::Key_Y);
  374. ui->actionMainRedo->setShortcuts(shrt);
  375. ui->actionMainUndo->setShortcutContext(Qt::ApplicationShortcut);
  376. ui->actionMainRedo->setShortcutContext(Qt::ApplicationShortcut);
  377. QPoint curPos;
  378. //restore parent window geometry
  379. const char *geometry = config_get_string(App()->GetUserConfig(), "BasicWindow", "geometry");
  380. if (geometry != NULL) {
  381. QByteArray byteArray = QByteArray::fromBase64(QByteArray(geometry));
  382. restoreGeometry(byteArray);
  383. QRect windowGeometry = normalGeometry();
  384. if (!WindowPositionValid(windowGeometry)) {
  385. QRect rect = QGuiApplication::primaryScreen()->geometry();
  386. setGeometry(QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, size(), rect));
  387. }
  388. curPos = pos();
  389. } else {
  390. QRect desktopRect = QGuiApplication::primaryScreen()->geometry();
  391. QSize adjSize = desktopRect.size() / 2 - size() / 2;
  392. curPos = QPoint(adjSize.width(), adjSize.height());
  393. }
  394. QPoint curSize(width(), height());
  395. QPoint statsDockSize(statsDock->width(), statsDock->height());
  396. QPoint statsDockPos = curSize / 2 - statsDockSize / 2;
  397. QPoint newPos = curPos + statsDockPos;
  398. statsDock->move(newPos);
  399. ui->actionReleaseNotes->setVisible(true);
  400. ui->previewDisabledWidget->setContextMenuPolicy(Qt::CustomContextMenu);
  401. connect(ui->enablePreviewButton, &QPushButton::clicked, this, &OBSBasic::TogglePreview);
  402. connect(ui->scenes, &SceneTree::scenesReordered, []() { OBSProjector::UpdateMultiviewProjectors(); });
  403. connect(App(), &OBSApp::StyleChanged, this, [this]() { OnEvent(OBS_FRONTEND_EVENT_THEME_CHANGED); });
  404. QActionGroup *actionGroup = new QActionGroup(this);
  405. actionGroup->addAction(ui->actionSceneListMode);
  406. actionGroup->addAction(ui->actionSceneGridMode);
  407. UpdatePreviewSafeAreas();
  408. UpdatePreviewSpacingHelpers();
  409. UpdatePreviewOverflowSettings();
  410. }
  411. static const double scaled_vals[] = {1.0, 1.25, (1.0 / 0.75), 1.5, (1.0 / 0.6), 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 0.0};
  412. #ifdef __APPLE__ // macOS
  413. #if OBS_RELEASE_CANDIDATE == 0 && OBS_BETA == 0
  414. #define DEFAULT_CONTAINER "fragmented_mov"
  415. #else
  416. #define DEFAULT_CONTAINER "hybrid_mov"
  417. #endif
  418. #else // Windows/Linux
  419. #if OBS_RELEASE_CANDIDATE == 0 && OBS_BETA == 0
  420. #define DEFAULT_CONTAINER "mkv"
  421. #else
  422. #define DEFAULT_CONTAINER "hybrid_mp4"
  423. #endif
  424. #endif
  425. bool OBSBasic::InitBasicConfigDefaults()
  426. {
  427. QList<QScreen *> screens = QGuiApplication::screens();
  428. if (!screens.size()) {
  429. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  430. "technically shouldn't be possible.");
  431. return false;
  432. }
  433. QScreen *primaryScreen = QGuiApplication::primaryScreen();
  434. uint32_t cx = primaryScreen->size().width();
  435. uint32_t cy = primaryScreen->size().height();
  436. cx *= devicePixelRatioF();
  437. cy *= devicePixelRatioF();
  438. bool oldResolutionDefaults = config_get_bool(App()->GetUserConfig(), "General", "Pre19Defaults");
  439. /* use 1920x1080 for new default base res if main monitor is above
  440. * 1920x1080, but don't apply for people from older builds -- only to
  441. * new users */
  442. if (!oldResolutionDefaults && (cx * cy) > (1920 * 1080)) {
  443. cx = 1920;
  444. cy = 1080;
  445. }
  446. bool changed = false;
  447. /* ----------------------------------------------------- */
  448. /* move over old FFmpeg track settings */
  449. if (config_has_user_value(activeConfiguration, "AdvOut", "FFAudioTrack") &&
  450. !config_has_user_value(activeConfiguration, "AdvOut", "Pre22.1Settings")) {
  451. int track = (int)config_get_int(activeConfiguration, "AdvOut", "FFAudioTrack");
  452. config_set_int(activeConfiguration, "AdvOut", "FFAudioMixes", 1LL << (track - 1));
  453. config_set_bool(activeConfiguration, "AdvOut", "Pre22.1Settings", true);
  454. changed = true;
  455. }
  456. /* ----------------------------------------------------- */
  457. /* move over mixer values in advanced if older config */
  458. if (config_has_user_value(activeConfiguration, "AdvOut", "RecTrackIndex") &&
  459. !config_has_user_value(activeConfiguration, "AdvOut", "RecTracks")) {
  460. uint64_t track = config_get_uint(activeConfiguration, "AdvOut", "RecTrackIndex");
  461. track = 1ULL << (track - 1);
  462. config_set_uint(activeConfiguration, "AdvOut", "RecTracks", track);
  463. config_remove_value(activeConfiguration, "AdvOut", "RecTrackIndex");
  464. changed = true;
  465. }
  466. /* ----------------------------------------------------- */
  467. /* set twitch chat extensions to "both" if prev version */
  468. /* is under 24.1 */
  469. if (config_get_bool(App()->GetUserConfig(), "General", "Pre24.1Defaults") &&
  470. !config_has_user_value(activeConfiguration, "Twitch", "AddonChoice")) {
  471. config_set_int(activeConfiguration, "Twitch", "AddonChoice", 3);
  472. changed = true;
  473. }
  474. /* ----------------------------------------------------- */
  475. /* move bitrate enforcement setting to new value */
  476. if (config_has_user_value(activeConfiguration, "SimpleOutput", "EnforceBitrate") &&
  477. !config_has_user_value(activeConfiguration, "Stream1", "IgnoreRecommended") &&
  478. !config_has_user_value(activeConfiguration, "Stream1", "MovedOldEnforce")) {
  479. bool enforce = config_get_bool(activeConfiguration, "SimpleOutput", "EnforceBitrate");
  480. config_set_bool(activeConfiguration, "Stream1", "IgnoreRecommended", !enforce);
  481. config_set_bool(activeConfiguration, "Stream1", "MovedOldEnforce", true);
  482. changed = true;
  483. }
  484. /* ----------------------------------------------------- */
  485. /* enforce minimum retry delay of 1 second prior to 27.1 */
  486. if (config_has_user_value(activeConfiguration, "Output", "RetryDelay")) {
  487. int retryDelay = config_get_uint(activeConfiguration, "Output", "RetryDelay");
  488. if (retryDelay < 1) {
  489. config_set_uint(activeConfiguration, "Output", "RetryDelay", 1);
  490. changed = true;
  491. }
  492. }
  493. /* ----------------------------------------------------- */
  494. /* Migrate old container selection (if any) to new key. */
  495. auto MigrateFormat = [&](const char *section) {
  496. bool has_old_key = config_has_user_value(activeConfiguration, section, "RecFormat");
  497. bool has_new_key = config_has_user_value(activeConfiguration, section, "RecFormat2");
  498. if (!has_new_key && !has_old_key)
  499. return;
  500. string old_format =
  501. config_get_string(activeConfiguration, section, has_new_key ? "RecFormat2" : "RecFormat");
  502. string new_format = old_format;
  503. if (old_format == "ts")
  504. new_format = "mpegts";
  505. else if (old_format == "m3u8")
  506. new_format = "hls";
  507. else if (old_format == "fmp4")
  508. new_format = "fragmented_mp4";
  509. else if (old_format == "fmov")
  510. new_format = "fragmented_mov";
  511. if (new_format != old_format || !has_new_key) {
  512. config_set_string(activeConfiguration, section, "RecFormat2", new_format.c_str());
  513. changed = true;
  514. }
  515. };
  516. MigrateFormat("AdvOut");
  517. MigrateFormat("SimpleOutput");
  518. /* ----------------------------------------------------- */
  519. /* Migrate output scale setting to GPU scaling options. */
  520. if (config_get_bool(activeConfiguration, "AdvOut", "Rescale") &&
  521. !config_has_user_value(activeConfiguration, "AdvOut", "RescaleFilter")) {
  522. config_set_int(activeConfiguration, "AdvOut", "RescaleFilter", OBS_SCALE_BILINEAR);
  523. }
  524. if (config_get_bool(activeConfiguration, "AdvOut", "RecRescale") &&
  525. !config_has_user_value(activeConfiguration, "AdvOut", "RecRescaleFilter")) {
  526. config_set_int(activeConfiguration, "AdvOut", "RecRescaleFilter", OBS_SCALE_BILINEAR);
  527. }
  528. /* ----------------------------------------------------- */
  529. if (changed) {
  530. activeConfiguration.SaveSafe("tmp");
  531. }
  532. /* ----------------------------------------------------- */
  533. config_set_default_string(activeConfiguration, "Output", "Mode", "Simple");
  534. config_set_default_bool(activeConfiguration, "Stream1", "IgnoreRecommended", false);
  535. config_set_default_bool(activeConfiguration, "Stream1", "EnableMultitrackVideo", false);
  536. config_set_default_bool(activeConfiguration, "Stream1", "MultitrackVideoMaximumAggregateBitrateAuto", true);
  537. config_set_default_bool(activeConfiguration, "Stream1", "MultitrackVideoMaximumVideoTracksAuto", true);
  538. config_set_default_string(activeConfiguration, "SimpleOutput", "FilePath", GetDefaultVideoSavePath().c_str());
  539. config_set_default_string(activeConfiguration, "SimpleOutput", "RecFormat2", DEFAULT_CONTAINER);
  540. config_set_default_uint(activeConfiguration, "SimpleOutput", "VBitrate", 2500);
  541. config_set_default_uint(activeConfiguration, "SimpleOutput", "ABitrate", 160);
  542. config_set_default_bool(activeConfiguration, "SimpleOutput", "UseAdvanced", false);
  543. config_set_default_string(activeConfiguration, "SimpleOutput", "Preset", "veryfast");
  544. config_set_default_string(activeConfiguration, "SimpleOutput", "NVENCPreset2", "p5");
  545. config_set_default_string(activeConfiguration, "SimpleOutput", "RecQuality", "Stream");
  546. config_set_default_bool(activeConfiguration, "SimpleOutput", "RecRB", false);
  547. config_set_default_int(activeConfiguration, "SimpleOutput", "RecRBTime", 20);
  548. config_set_default_int(activeConfiguration, "SimpleOutput", "RecRBSize", 512);
  549. config_set_default_string(activeConfiguration, "SimpleOutput", "RecRBPrefix", "Replay");
  550. config_set_default_string(activeConfiguration, "SimpleOutput", "StreamAudioEncoder", "aac");
  551. config_set_default_string(activeConfiguration, "SimpleOutput", "RecAudioEncoder", "aac");
  552. config_set_default_uint(activeConfiguration, "SimpleOutput", "RecTracks", (1 << 0));
  553. config_set_default_bool(activeConfiguration, "AdvOut", "ApplyServiceSettings", true);
  554. config_set_default_bool(activeConfiguration, "AdvOut", "UseRescale", false);
  555. config_set_default_uint(activeConfiguration, "AdvOut", "TrackIndex", 1);
  556. config_set_default_uint(activeConfiguration, "AdvOut", "VodTrackIndex", 2);
  557. config_set_default_string(activeConfiguration, "AdvOut", "Encoder", "obs_x264");
  558. config_set_default_string(activeConfiguration, "AdvOut", "RecType", "Standard");
  559. config_set_default_string(activeConfiguration, "AdvOut", "RecFilePath", GetDefaultVideoSavePath().c_str());
  560. config_set_default_string(activeConfiguration, "AdvOut", "RecFormat2", DEFAULT_CONTAINER);
  561. config_set_default_bool(activeConfiguration, "AdvOut", "RecUseRescale", false);
  562. config_set_default_uint(activeConfiguration, "AdvOut", "RecTracks", (1 << 0));
  563. config_set_default_string(activeConfiguration, "AdvOut", "RecEncoder", "none");
  564. config_set_default_uint(activeConfiguration, "AdvOut", "FLVTrack", 1);
  565. config_set_default_uint(activeConfiguration, "AdvOut", "StreamMultiTrackAudioMixes", 1);
  566. config_set_default_bool(activeConfiguration, "AdvOut", "FFOutputToFile", true);
  567. config_set_default_string(activeConfiguration, "AdvOut", "FFFilePath", GetDefaultVideoSavePath().c_str());
  568. config_set_default_string(activeConfiguration, "AdvOut", "FFExtension", "mp4");
  569. config_set_default_uint(activeConfiguration, "AdvOut", "FFVBitrate", 2500);
  570. config_set_default_uint(activeConfiguration, "AdvOut", "FFVGOPSize", 250);
  571. config_set_default_bool(activeConfiguration, "AdvOut", "FFUseRescale", false);
  572. config_set_default_bool(activeConfiguration, "AdvOut", "FFIgnoreCompat", false);
  573. config_set_default_uint(activeConfiguration, "AdvOut", "FFABitrate", 160);
  574. config_set_default_uint(activeConfiguration, "AdvOut", "FFAudioMixes", 1);
  575. config_set_default_uint(activeConfiguration, "AdvOut", "Track1Bitrate", 160);
  576. config_set_default_uint(activeConfiguration, "AdvOut", "Track2Bitrate", 160);
  577. config_set_default_uint(activeConfiguration, "AdvOut", "Track3Bitrate", 160);
  578. config_set_default_uint(activeConfiguration, "AdvOut", "Track4Bitrate", 160);
  579. config_set_default_uint(activeConfiguration, "AdvOut", "Track5Bitrate", 160);
  580. config_set_default_uint(activeConfiguration, "AdvOut", "Track6Bitrate", 160);
  581. config_set_default_uint(activeConfiguration, "AdvOut", "RecSplitFileTime", 15);
  582. config_set_default_uint(activeConfiguration, "AdvOut", "RecSplitFileSize", 2048);
  583. config_set_default_bool(activeConfiguration, "AdvOut", "RecRB", false);
  584. config_set_default_uint(activeConfiguration, "AdvOut", "RecRBTime", 20);
  585. config_set_default_int(activeConfiguration, "AdvOut", "RecRBSize", 512);
  586. config_set_default_uint(activeConfiguration, "Video", "BaseCX", cx);
  587. config_set_default_uint(activeConfiguration, "Video", "BaseCY", cy);
  588. /* don't allow BaseCX/BaseCY to be susceptible to defaults changing */
  589. if (!config_has_user_value(activeConfiguration, "Video", "BaseCX") ||
  590. !config_has_user_value(activeConfiguration, "Video", "BaseCY")) {
  591. config_set_uint(activeConfiguration, "Video", "BaseCX", cx);
  592. config_set_uint(activeConfiguration, "Video", "BaseCY", cy);
  593. config_save_safe(activeConfiguration, "tmp", nullptr);
  594. }
  595. config_set_default_string(activeConfiguration, "Output", "FilenameFormatting", "%CCYY-%MM-%DD %hh-%mm-%ss");
  596. config_set_default_bool(activeConfiguration, "Output", "DelayEnable", false);
  597. config_set_default_uint(activeConfiguration, "Output", "DelaySec", 20);
  598. config_set_default_bool(activeConfiguration, "Output", "DelayPreserve", true);
  599. config_set_default_bool(activeConfiguration, "Output", "Reconnect", true);
  600. config_set_default_uint(activeConfiguration, "Output", "RetryDelay", 2);
  601. config_set_default_uint(activeConfiguration, "Output", "MaxRetries", 25);
  602. config_set_default_string(activeConfiguration, "Output", "BindIP", "default");
  603. config_set_default_string(activeConfiguration, "Output", "IPFamily", "IPv4+IPv6");
  604. config_set_default_bool(activeConfiguration, "Output", "NewSocketLoopEnable", false);
  605. config_set_default_bool(activeConfiguration, "Output", "LowLatencyEnable", false);
  606. int i = 0;
  607. uint32_t scale_cx = cx;
  608. uint32_t scale_cy = cy;
  609. /* use a default scaled resolution that has a pixel count no higher
  610. * than 1280x720 */
  611. while (((scale_cx * scale_cy) > (1280 * 720)) && scaled_vals[i] > 0.0) {
  612. double scale = scaled_vals[i++];
  613. scale_cx = uint32_t(double(cx) / scale);
  614. scale_cy = uint32_t(double(cy) / scale);
  615. }
  616. config_set_default_uint(activeConfiguration, "Video", "OutputCX", scale_cx);
  617. config_set_default_uint(activeConfiguration, "Video", "OutputCY", scale_cy);
  618. /* don't allow OutputCX/OutputCY to be susceptible to defaults
  619. * changing */
  620. if (!config_has_user_value(activeConfiguration, "Video", "OutputCX") ||
  621. !config_has_user_value(activeConfiguration, "Video", "OutputCY")) {
  622. config_set_uint(activeConfiguration, "Video", "OutputCX", scale_cx);
  623. config_set_uint(activeConfiguration, "Video", "OutputCY", scale_cy);
  624. config_save_safe(activeConfiguration, "tmp", nullptr);
  625. }
  626. config_set_default_uint(activeConfiguration, "Video", "FPSType", 0);
  627. config_set_default_string(activeConfiguration, "Video", "FPSCommon", "30");
  628. config_set_default_uint(activeConfiguration, "Video", "FPSInt", 30);
  629. config_set_default_uint(activeConfiguration, "Video", "FPSNum", 30);
  630. config_set_default_uint(activeConfiguration, "Video", "FPSDen", 1);
  631. config_set_default_string(activeConfiguration, "Video", "ScaleType", "bicubic");
  632. config_set_default_string(activeConfiguration, "Video", "ColorFormat", "NV12");
  633. config_set_default_string(activeConfiguration, "Video", "ColorSpace", "709");
  634. config_set_default_string(activeConfiguration, "Video", "ColorRange", "Partial");
  635. config_set_default_uint(activeConfiguration, "Video", "SdrWhiteLevel", 300);
  636. config_set_default_uint(activeConfiguration, "Video", "HdrNominalPeakLevel", 1000);
  637. config_set_default_string(activeConfiguration, "Audio", "MonitoringDeviceId", "default");
  638. config_set_default_string(activeConfiguration, "Audio", "MonitoringDeviceName",
  639. Str("Basic.Settings.Advanced.Audio.MonitoringDevice"
  640. ".Default"));
  641. config_set_default_uint(activeConfiguration, "Audio", "SampleRate", 48000);
  642. config_set_default_string(activeConfiguration, "Audio", "ChannelSetup", "Stereo");
  643. config_set_default_double(activeConfiguration, "Audio", "MeterDecayRate", VOLUME_METER_DECAY_FAST);
  644. config_set_default_uint(activeConfiguration, "Audio", "PeakMeterType", 0);
  645. CheckExistingCookieId();
  646. return true;
  647. }
  648. void OBSBasic::InitBasicConfigDefaults2()
  649. {
  650. bool oldEncDefaults = config_get_bool(App()->GetUserConfig(), "General", "Pre23Defaults");
  651. bool useNV = EncoderAvailable("ffmpeg_nvenc") && !oldEncDefaults;
  652. config_set_default_string(activeConfiguration, "SimpleOutput", "StreamEncoder",
  653. useNV ? SIMPLE_ENCODER_NVENC : SIMPLE_ENCODER_X264);
  654. config_set_default_string(activeConfiguration, "SimpleOutput", "RecEncoder",
  655. useNV ? SIMPLE_ENCODER_NVENC : SIMPLE_ENCODER_X264);
  656. const char *aac_default = "ffmpeg_aac";
  657. if (EncoderAvailable("CoreAudio_AAC"))
  658. aac_default = "CoreAudio_AAC";
  659. else if (EncoderAvailable("libfdk_aac"))
  660. aac_default = "libfdk_aac";
  661. config_set_default_string(activeConfiguration, "AdvOut", "AudioEncoder", aac_default);
  662. config_set_default_string(activeConfiguration, "AdvOut", "RecAudioEncoder", aac_default);
  663. }
  664. bool OBSBasic::InitBasicConfig()
  665. {
  666. ProfileScope("OBSBasic::InitBasicConfig");
  667. RefreshProfiles(true);
  668. const std::string currentProfileName{config_get_string(App()->GetUserConfig(), "Basic", "Profile")};
  669. const std::optional<OBSProfile> currentProfile = GetProfileByName(currentProfileName);
  670. const std::optional<OBSProfile> foundProfile = GetProfileByName(opt_starting_profile);
  671. try {
  672. if (foundProfile) {
  673. ActivateProfile(foundProfile.value());
  674. } else if (currentProfile) {
  675. ActivateProfile(currentProfile.value());
  676. } else {
  677. const OBSProfile &newProfile = CreateProfile(currentProfileName);
  678. ActivateProfile(newProfile);
  679. }
  680. } catch (const std::logic_error &) {
  681. OBSErrorBox(NULL, "Failed to open basic.ini: %d", -1);
  682. return false;
  683. }
  684. return true;
  685. }
  686. void OBSBasic::InitOBSCallbacks()
  687. {
  688. ProfileScope("OBSBasic::InitOBSCallbacks");
  689. signalHandlers.reserve(signalHandlers.size() + 10);
  690. signalHandlers.emplace_back(obs_get_signal_handler(), "source_create", OBSBasic::SourceCreated, this);
  691. signalHandlers.emplace_back(obs_get_signal_handler(), "source_remove", OBSBasic::SourceRemoved, this);
  692. signalHandlers.emplace_back(obs_get_signal_handler(), "source_activate", OBSBasic::SourceActivated, this);
  693. signalHandlers.emplace_back(obs_get_signal_handler(), "source_deactivate", OBSBasic::SourceDeactivated, this);
  694. signalHandlers.emplace_back(obs_get_signal_handler(), "source_audio_activate", OBSBasic::SourceAudioActivated,
  695. this);
  696. signalHandlers.emplace_back(obs_get_signal_handler(), "source_audio_deactivate",
  697. OBSBasic::SourceAudioDeactivated, this);
  698. signalHandlers.emplace_back(obs_get_signal_handler(), "source_rename", OBSBasic::SourceRenamed, this);
  699. signalHandlers.emplace_back(
  700. obs_get_signal_handler(), "source_filter_add",
  701. [](void *data, calldata_t *) {
  702. QMetaObject::invokeMethod(static_cast<OBSBasic *>(data), "UpdateEditMenu",
  703. Qt::QueuedConnection);
  704. },
  705. this);
  706. signalHandlers.emplace_back(
  707. obs_get_signal_handler(), "source_filter_remove",
  708. [](void *data, calldata_t *) {
  709. QMetaObject::invokeMethod(static_cast<OBSBasic *>(data), "UpdateEditMenu",
  710. Qt::QueuedConnection);
  711. },
  712. this);
  713. signalHandlers.emplace_back(obs_get_signal_handler(), "canvas_remove", OBSBasic::CanvasRemoved, this);
  714. }
  715. #define STARTUP_SEPARATOR "==== Startup complete ==============================================="
  716. #define SHUTDOWN_SEPARATOR "==== Shutting down =================================================="
  717. #define UNSUPPORTED_ERROR \
  718. "Failed to initialize video:\n\nRequired graphics API functionality " \
  719. "not found. Your GPU may not be supported."
  720. #define UNKNOWN_ERROR \
  721. "Failed to initialize video. Your GPU may not be supported, " \
  722. "or your graphics drivers may need to be updated."
  723. static inline void LogEncoders()
  724. {
  725. constexpr uint32_t hide_flags = OBS_ENCODER_CAP_DEPRECATED | OBS_ENCODER_CAP_INTERNAL;
  726. auto list_encoders = [](obs_encoder_type type) {
  727. size_t idx = 0;
  728. const char *encoder_type;
  729. while (obs_enum_encoder_types(idx++, &encoder_type)) {
  730. if (obs_get_encoder_caps(encoder_type) & hide_flags ||
  731. obs_get_encoder_type(encoder_type) != type) {
  732. continue;
  733. }
  734. blog(LOG_INFO, "\t- %s (%s)", encoder_type, obs_encoder_get_display_name(encoder_type));
  735. }
  736. };
  737. blog(LOG_INFO, "---------------------------------");
  738. blog(LOG_INFO, "Available Encoders:");
  739. blog(LOG_INFO, " Video Encoders:");
  740. list_encoders(OBS_ENCODER_VIDEO);
  741. blog(LOG_INFO, " Audio Encoders:");
  742. list_encoders(OBS_ENCODER_AUDIO);
  743. }
  744. void OBSBasic::OBSInit()
  745. {
  746. ProfileScope("OBSBasic::OBSInit");
  747. if (!InitBasicConfig())
  748. throw "Failed to load basic.ini";
  749. if (!ResetAudio())
  750. throw "Failed to initialize audio";
  751. int ret = 0;
  752. ret = ResetVideo();
  753. switch (ret) {
  754. case OBS_VIDEO_MODULE_NOT_FOUND:
  755. throw "Failed to initialize video: Graphics module not found";
  756. case OBS_VIDEO_NOT_SUPPORTED:
  757. throw UNSUPPORTED_ERROR;
  758. case OBS_VIDEO_INVALID_PARAM:
  759. throw "Failed to initialize video: Invalid parameters";
  760. default:
  761. if (ret != OBS_VIDEO_SUCCESS)
  762. throw UNKNOWN_ERROR;
  763. }
  764. /* load audio monitoring */
  765. if (obs_audio_monitoring_available()) {
  766. const char *device_name = config_get_string(activeConfiguration, "Audio", "MonitoringDeviceName");
  767. const char *device_id = config_get_string(activeConfiguration, "Audio", "MonitoringDeviceId");
  768. obs_set_audio_monitoring_device(device_name, device_id);
  769. blog(LOG_INFO, "Audio monitoring device:\n\tname: %s\n\tid: %s", device_name, device_id);
  770. }
  771. InitOBSCallbacks();
  772. InitHotkeys();
  773. ui->preview->Init();
  774. /* hack to prevent elgato from loading its own QtNetwork that it tries
  775. * to ship with */
  776. #if defined(_WIN32) && !defined(_DEBUG)
  777. LoadLibraryW(L"Qt6Network");
  778. #endif
  779. struct obs_module_failure_info mfi;
  780. /* Safe Mode disables third-party plugins so we don't need to add earch
  781. * paths outside the OBS bundle/installation. */
  782. if (safe_mode || disable_3p_plugins) {
  783. SetSafeModuleNames();
  784. } else {
  785. AddExtraModulePaths();
  786. }
  787. /* Modules can access frontend information (i.e. profile and scene collection data) during their initialization, and some modules (e.g. obs-websockets) are known to use the filesystem location of the current profile in their own code.
  788. Thus the profile and scene collection discovery needs to happen before any access to that information (but after intializing global settings) to ensure legacy code gets valid path information.
  789. */
  790. RefreshSceneCollections(true);
  791. blog(LOG_INFO, "---------------------------------");
  792. obs_load_all_modules2(&mfi);
  793. blog(LOG_INFO, "---------------------------------");
  794. obs_log_loaded_modules();
  795. blog(LOG_INFO, "---------------------------------");
  796. obs_post_load_modules();
  797. BPtr<char *> failed_modules = mfi.failed_modules;
  798. #ifdef BROWSER_AVAILABLE
  799. cef = obs_browser_init_panel();
  800. cef_js_avail = cef && obs_browser_qcef_version() >= 3;
  801. #endif
  802. vcamEnabled = (obs_get_output_flags(VIRTUAL_CAM_ID) & OBS_OUTPUT_VIDEO) != 0;
  803. if (vcamEnabled) {
  804. emit VirtualCamEnabled();
  805. }
  806. UpdateProfileEncoders();
  807. LogEncoders();
  808. blog(LOG_INFO, STARTUP_SEPARATOR);
  809. if (!InitService())
  810. throw "Failed to initialize service";
  811. ResetOutputs();
  812. CreateHotkeys();
  813. InitPrimitives();
  814. sceneDuplicationMode = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SceneDuplicationMode");
  815. swapScenesMode = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SwapScenesMode");
  816. editPropertiesMode = config_get_bool(App()->GetUserConfig(), "BasicWindow", "EditPropertiesMode");
  817. if (!opt_studio_mode) {
  818. SetPreviewProgramMode(config_get_bool(App()->GetUserConfig(), "BasicWindow", "PreviewProgramMode"));
  819. } else {
  820. SetPreviewProgramMode(true);
  821. opt_studio_mode = false;
  822. }
  823. #define SET_VISIBILITY(name, control) \
  824. do { \
  825. if (config_has_user_value(App()->GetUserConfig(), "BasicWindow", name)) { \
  826. bool visible = config_get_bool(App()->GetUserConfig(), "BasicWindow", name); \
  827. ui->control->setChecked(visible); \
  828. } \
  829. } while (false)
  830. SET_VISIBILITY("ShowListboxToolbars", toggleListboxToolbars);
  831. SET_VISIBILITY("ShowStatusBar", toggleStatusBar);
  832. #undef SET_VISIBILITY
  833. bool sourceIconsVisible = config_get_bool(App()->GetUserConfig(), "BasicWindow", "ShowSourceIcons");
  834. ui->toggleSourceIcons->setChecked(sourceIconsVisible);
  835. bool contextVisible = config_get_bool(App()->GetUserConfig(), "BasicWindow", "ShowContextToolbars");
  836. ui->toggleContextBar->setChecked(contextVisible);
  837. ui->contextContainer->setVisible(contextVisible);
  838. if (contextVisible)
  839. UpdateContextBar(true);
  840. UpdateEditMenu();
  841. {
  842. ProfileScope("OBSBasic::Load");
  843. const std::string sceneCollectionName{
  844. config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection")};
  845. std::optional<OBS::SceneCollection> configuredCollection =
  846. GetSceneCollectionByName(sceneCollectionName);
  847. std::optional<OBS::SceneCollection> foundCollection = GetSceneCollectionByName(opt_starting_collection);
  848. if (foundCollection) {
  849. ActivateSceneCollection(foundCollection.value());
  850. } else if (configuredCollection) {
  851. ActivateSceneCollection(configuredCollection.value());
  852. } else {
  853. disableSaving--;
  854. SetupNewSceneCollection(sceneCollectionName);
  855. disableSaving++;
  856. }
  857. if (foundCollection || configuredCollection) {
  858. disableSaving--;
  859. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
  860. OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
  861. OnEvent(OBS_FRONTEND_EVENT_SCENE_CHANGED);
  862. OnEvent(OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED);
  863. disableSaving++;
  864. }
  865. }
  866. loaded = true;
  867. previewEnabled = config_get_bool(App()->GetUserConfig(), "BasicWindow", "PreviewEnabled");
  868. if (!previewEnabled && !IsPreviewProgramMode())
  869. QMetaObject::invokeMethod(this, "EnablePreviewDisplay", Qt::QueuedConnection,
  870. Q_ARG(bool, previewEnabled));
  871. else if (!previewEnabled && IsPreviewProgramMode())
  872. QMetaObject::invokeMethod(this, "EnablePreviewDisplay", Qt::QueuedConnection, Q_ARG(bool, true));
  873. disableSaving--;
  874. auto addDisplay = [this](OBSQTDisplay *window) {
  875. obs_display_add_draw_callback(window->GetDisplay(), OBSBasic::RenderMain, this);
  876. struct obs_video_info ovi;
  877. if (obs_get_video_info(&ovi))
  878. ResizePreview(ovi.base_width, ovi.base_height);
  879. };
  880. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDisplay);
  881. /* Show the main window, unless the tray icon isn't available
  882. * or neither the setting nor flag for starting minimized is set. */
  883. bool sysTrayEnabled = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SysTrayEnabled");
  884. bool sysTrayWhenStarted = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SysTrayWhenStarted");
  885. bool hideWindowOnStart = QSystemTrayIcon::isSystemTrayAvailable() && sysTrayEnabled &&
  886. (opt_minimize_tray || sysTrayWhenStarted);
  887. #ifdef _WIN32
  888. SetWin32DropStyle(this);
  889. if (!hideWindowOnStart)
  890. show();
  891. #endif
  892. bool alwaysOnTop = config_get_bool(App()->GetUserConfig(), "BasicWindow", "AlwaysOnTop");
  893. #ifdef ENABLE_WAYLAND
  894. bool isWayland = obs_get_nix_platform() == OBS_NIX_PLATFORM_WAYLAND;
  895. #else
  896. bool isWayland = false;
  897. #endif
  898. if (!isWayland && (alwaysOnTop || opt_always_on_top)) {
  899. SetAlwaysOnTop(this, true);
  900. ui->actionAlwaysOnTop->setChecked(true);
  901. } else if (isWayland) {
  902. if (opt_always_on_top)
  903. blog(LOG_INFO, "Always On Top not available on Wayland, ignoring.");
  904. ui->actionAlwaysOnTop->setEnabled(false);
  905. ui->actionAlwaysOnTop->setVisible(false);
  906. }
  907. #ifndef _WIN32
  908. if (!hideWindowOnStart)
  909. show();
  910. #endif
  911. /* setup stats dock */
  912. OBSBasicStats *statsDlg = new OBSBasicStats(statsDock, false);
  913. statsDock->setWidget(statsDlg);
  914. /* ----------------------------- */
  915. /* add custom browser docks */
  916. #if defined(BROWSER_AVAILABLE) && defined(YOUTUBE_ENABLED)
  917. YouTubeAppDock::CleanupYouTubeUrls();
  918. #endif
  919. #ifdef BROWSER_AVAILABLE
  920. if (cef) {
  921. QAction *action = new QAction(QTStr("Basic.MainMenu.Docks."
  922. "CustomBrowserDocks"),
  923. this);
  924. ui->menuDocks->insertAction(ui->scenesDock->toggleViewAction(), action);
  925. connect(action, &QAction::triggered, this, &OBSBasic::ManageExtraBrowserDocks);
  926. ui->menuDocks->insertSeparator(ui->scenesDock->toggleViewAction());
  927. LoadExtraBrowserDocks();
  928. }
  929. #endif
  930. #ifdef YOUTUBE_ENABLED
  931. /* setup YouTube app dock */
  932. if (YouTubeAppDock::IsYTServiceSelected())
  933. NewYouTubeAppDock();
  934. #endif
  935. const char *dockStateStr = config_get_string(App()->GetUserConfig(), "BasicWindow", "DockState");
  936. if (!dockStateStr) {
  937. on_resetDocks_triggered(true);
  938. } else {
  939. QByteArray dockState = QByteArray::fromBase64(QByteArray(dockStateStr));
  940. if (!restoreState(dockState))
  941. on_resetDocks_triggered(true);
  942. }
  943. bool pre23Defaults = config_get_bool(App()->GetUserConfig(), "General", "Pre23Defaults");
  944. if (pre23Defaults) {
  945. bool resetDockLock23 = config_get_bool(App()->GetUserConfig(), "General", "ResetDockLock23");
  946. if (!resetDockLock23) {
  947. config_set_bool(App()->GetUserConfig(), "General", "ResetDockLock23", true);
  948. config_remove_value(App()->GetUserConfig(), "BasicWindow", "DocksLocked");
  949. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  950. }
  951. }
  952. bool docksLocked = config_get_bool(App()->GetUserConfig(), "BasicWindow", "DocksLocked");
  953. on_lockDocks_toggled(docksLocked);
  954. ui->lockDocks->blockSignals(true);
  955. ui->lockDocks->setChecked(docksLocked);
  956. ui->lockDocks->blockSignals(false);
  957. bool sideDocks = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SideDocks");
  958. on_sideDocks_toggled(sideDocks);
  959. ui->sideDocks->blockSignals(true);
  960. ui->sideDocks->setChecked(sideDocks);
  961. ui->sideDocks->blockSignals(false);
  962. SystemTray(true);
  963. TaskbarOverlayInit();
  964. #ifdef __APPLE__
  965. disableColorSpaceConversion(this);
  966. #endif
  967. bool has_last_version = config_has_user_value(App()->GetAppConfig(), "General", "LastVersion");
  968. bool first_run = config_get_bool(App()->GetUserConfig(), "General", "FirstRun");
  969. if (!first_run) {
  970. config_set_bool(App()->GetUserConfig(), "General", "FirstRun", true);
  971. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  972. }
  973. if (!first_run && !has_last_version && !Active())
  974. QMetaObject::invokeMethod(this, "on_autoConfigure_triggered", Qt::QueuedConnection);
  975. #if (defined(_WIN32) || defined(__APPLE__)) && (OBS_RELEASE_CANDIDATE > 0 || OBS_BETA > 0)
  976. /* Automatically set branch to "beta" the first time a pre-release build is run. */
  977. if (!config_get_bool(App()->GetAppConfig(), "General", "AutoBetaOptIn")) {
  978. config_set_string(App()->GetAppConfig(), "General", "UpdateBranch", "beta");
  979. config_set_bool(App()->GetAppConfig(), "General", "AutoBetaOptIn", true);
  980. config_save_safe(App()->GetAppConfig(), "tmp", nullptr);
  981. }
  982. #endif
  983. TimedCheckForUpdates();
  984. ToggleMixerLayout(config_get_bool(App()->GetUserConfig(), "BasicWindow", "VerticalVolControl"));
  985. if (config_get_bool(activeConfiguration, "General", "OpenStatsOnStartup"))
  986. on_stats_triggered();
  987. OBSBasicStats::InitializeValues();
  988. /* ----------------------- */
  989. /* Add multiview menu */
  990. ui->viewMenu->addSeparator();
  991. connect(ui->viewMenu->menuAction(), &QAction::hovered, this, &OBSBasic::updateMultiviewProjectorMenu);
  992. OBSBasic::updateMultiviewProjectorMenu();
  993. ui->sources->UpdateIcons();
  994. #if !defined(_WIN32)
  995. delete ui->actionRepair;
  996. ui->actionRepair = nullptr;
  997. #if !defined(__APPLE__)
  998. delete ui->actionShowCrashLogs;
  999. delete ui->actionUploadLastCrashLog;
  1000. delete ui->menuCrashLogs;
  1001. delete ui->actionCheckForUpdates;
  1002. ui->actionShowCrashLogs = nullptr;
  1003. ui->actionUploadLastCrashLog = nullptr;
  1004. ui->menuCrashLogs = nullptr;
  1005. ui->actionCheckForUpdates = nullptr;
  1006. #endif
  1007. #endif
  1008. #ifdef __APPLE__
  1009. /* Remove OBS' Fullscreen Interface menu in favor of the one macOS adds by default */
  1010. delete ui->actionFullscreenInterface;
  1011. ui->actionFullscreenInterface = nullptr;
  1012. #else
  1013. /* Don't show menu to raise macOS-only permissions dialog */
  1014. delete ui->actionShowMacPermissions;
  1015. ui->actionShowMacPermissions = nullptr;
  1016. #endif
  1017. #if defined(_WIN32) || defined(__APPLE__)
  1018. if (App()->IsUpdaterDisabled()) {
  1019. ui->actionCheckForUpdates->setEnabled(false);
  1020. #if defined(_WIN32)
  1021. ui->actionRepair->setEnabled(false);
  1022. #endif
  1023. }
  1024. #endif
  1025. #ifndef WHATSNEW_ENABLED
  1026. delete ui->actionShowWhatsNew;
  1027. ui->actionShowWhatsNew = nullptr;
  1028. #endif
  1029. if (safe_mode) {
  1030. ui->actionRestartSafe->setText(QTStr("Basic.MainMenu.Help.RestartNormal"));
  1031. }
  1032. UpdatePreviewProgramIndicators();
  1033. OnFirstLoad();
  1034. if (!hideWindowOnStart)
  1035. activateWindow();
  1036. /* ------------------------------------------- */
  1037. /* display warning message for failed modules */
  1038. if (mfi.count) {
  1039. QString failed_plugins;
  1040. char **plugin = mfi.failed_modules;
  1041. while (*plugin) {
  1042. failed_plugins += *plugin;
  1043. failed_plugins += "\n";
  1044. plugin++;
  1045. }
  1046. QString failed_msg = QTStr("PluginsFailedToLoad.Text").arg(failed_plugins);
  1047. OBSMessageBox::warning(this, QTStr("PluginsFailedToLoad.Title"), failed_msg);
  1048. }
  1049. }
  1050. void OBSBasic::OnFirstLoad()
  1051. {
  1052. OnEvent(OBS_FRONTEND_EVENT_FINISHED_LOADING);
  1053. #ifdef WHATSNEW_ENABLED
  1054. /* Attempt to load init screen if available */
  1055. if (cef) {
  1056. WhatsNewInfoThread *wnit = new WhatsNewInfoThread();
  1057. connect(wnit, &WhatsNewInfoThread::Result, this, &OBSBasic::ReceivedIntroJson, Qt::QueuedConnection);
  1058. introCheckThread.reset(wnit);
  1059. introCheckThread->start();
  1060. }
  1061. #endif
  1062. Auth::Load();
  1063. bool showLogViewerOnStartup = config_get_bool(App()->GetUserConfig(), "LogViewer", "ShowLogStartup");
  1064. if (showLogViewerOnStartup)
  1065. on_actionViewCurrentLog_triggered();
  1066. }
  1067. OBSBasic::~OBSBasic()
  1068. {
  1069. if (!handledShutdown) {
  1070. applicationShutdown();
  1071. }
  1072. }
  1073. void OBSBasic::applicationShutdown() noexcept
  1074. {
  1075. /* clear out UI event queue */
  1076. QApplication::sendPostedEvents(nullptr);
  1077. QApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
  1078. if (updateCheckThread && updateCheckThread->isRunning())
  1079. updateCheckThread->wait();
  1080. if (patronJsonThread && patronJsonThread->isRunning())
  1081. patronJsonThread->wait();
  1082. delete screenshotData;
  1083. delete previewProjector;
  1084. delete studioProgramProjector;
  1085. delete previewProjectorSource;
  1086. delete previewProjectorMain;
  1087. delete sourceProjector;
  1088. delete sceneProjectorMenu;
  1089. delete scaleFilteringMenu;
  1090. delete blendingModeMenu;
  1091. delete colorMenu;
  1092. delete colorWidgetAction;
  1093. delete colorSelect;
  1094. delete deinterlaceMenu;
  1095. delete perSceneTransitionMenu;
  1096. delete shortcutFilter;
  1097. delete trayMenu;
  1098. delete programOptions;
  1099. delete program;
  1100. /* XXX: any obs data must be released before calling obs_shutdown.
  1101. * currently, we can't automate this with C++ RAII because of the
  1102. * delicate nature of obs_shutdown needing to be freed before the UI
  1103. * can be freed, and we have no control over the destruction order of
  1104. * the Qt UI stuff, so we have to manually clear any references to
  1105. * libobs. */
  1106. delete cpuUsageTimer;
  1107. os_cpu_usage_info_destroy(cpuUsageInfo);
  1108. obs_hotkey_set_callback_routing_func(nullptr, nullptr);
  1109. ClearHotkeys();
  1110. service = nullptr;
  1111. outputHandler.reset();
  1112. delete interaction;
  1113. delete properties;
  1114. delete filters;
  1115. delete transformWindow;
  1116. delete advAudioWindow;
  1117. delete about;
  1118. delete remux;
  1119. obs_display_remove_draw_callback(ui->preview->GetDisplay(), OBSBasic::RenderMain, this);
  1120. obs_enter_graphics();
  1121. gs_vertexbuffer_destroy(box);
  1122. gs_vertexbuffer_destroy(boxLeft);
  1123. gs_vertexbuffer_destroy(boxTop);
  1124. gs_vertexbuffer_destroy(boxRight);
  1125. gs_vertexbuffer_destroy(boxBottom);
  1126. gs_vertexbuffer_destroy(circle);
  1127. gs_vertexbuffer_destroy(actionSafeMargin);
  1128. gs_vertexbuffer_destroy(graphicsSafeMargin);
  1129. gs_vertexbuffer_destroy(fourByThreeSafeMargin);
  1130. gs_vertexbuffer_destroy(leftLine);
  1131. gs_vertexbuffer_destroy(topLine);
  1132. gs_vertexbuffer_destroy(rightLine);
  1133. obs_leave_graphics();
  1134. /* When shutting down, sometimes source references can get in to the
  1135. * event queue, and if we don't forcibly process those events they
  1136. * won't get processed until after obs_shutdown has been called. I
  1137. * really wish there were a more elegant way to deal with this via C++,
  1138. * but Qt doesn't use C++ in a normal way, so you can't really rely on
  1139. * normal C++ behavior for your data to be freed in the order that you
  1140. * expect or want it to. */
  1141. QApplication::sendPostedEvents(nullptr);
  1142. config_set_int(App()->GetAppConfig(), "General", "LastVersion", LIBOBS_API_VER);
  1143. config_save_safe(App()->GetAppConfig(), "tmp", nullptr);
  1144. config_set_bool(App()->GetUserConfig(), "BasicWindow", "PreviewEnabled", previewEnabled);
  1145. config_set_bool(App()->GetUserConfig(), "BasicWindow", "AlwaysOnTop", ui->actionAlwaysOnTop->isChecked());
  1146. config_set_bool(App()->GetUserConfig(), "BasicWindow", "SceneDuplicationMode", sceneDuplicationMode);
  1147. config_set_bool(App()->GetUserConfig(), "BasicWindow", "SwapScenesMode", swapScenesMode);
  1148. config_set_bool(App()->GetUserConfig(), "BasicWindow", "EditPropertiesMode", editPropertiesMode);
  1149. config_set_bool(App()->GetUserConfig(), "BasicWindow", "PreviewProgramMode", IsPreviewProgramMode());
  1150. config_set_bool(App()->GetUserConfig(), "BasicWindow", "DocksLocked", ui->lockDocks->isChecked());
  1151. config_set_bool(App()->GetUserConfig(), "BasicWindow", "SideDocks", ui->sideDocks->isChecked());
  1152. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  1153. #ifdef BROWSER_AVAILABLE
  1154. DestroyPanelCookieManager();
  1155. delete cef;
  1156. cef = nullptr;
  1157. #endif
  1158. handledShutdown = true;
  1159. }
  1160. static inline int AttemptToResetVideo(struct obs_video_info *ovi)
  1161. {
  1162. return obs_reset_video(ovi);
  1163. }
  1164. static inline enum obs_scale_type GetScaleType(ConfigFile &activeConfiguration)
  1165. {
  1166. const char *scaleTypeStr = config_get_string(activeConfiguration, "Video", "ScaleType");
  1167. if (astrcmpi(scaleTypeStr, "bilinear") == 0)
  1168. return OBS_SCALE_BILINEAR;
  1169. else if (astrcmpi(scaleTypeStr, "lanczos") == 0)
  1170. return OBS_SCALE_LANCZOS;
  1171. else if (astrcmpi(scaleTypeStr, "area") == 0)
  1172. return OBS_SCALE_AREA;
  1173. else
  1174. return OBS_SCALE_BICUBIC;
  1175. }
  1176. static inline enum video_format GetVideoFormatFromName(const char *name)
  1177. {
  1178. if (astrcmpi(name, "I420") == 0)
  1179. return VIDEO_FORMAT_I420;
  1180. else if (astrcmpi(name, "NV12") == 0)
  1181. return VIDEO_FORMAT_NV12;
  1182. else if (astrcmpi(name, "I444") == 0)
  1183. return VIDEO_FORMAT_I444;
  1184. else if (astrcmpi(name, "I010") == 0)
  1185. return VIDEO_FORMAT_I010;
  1186. else if (astrcmpi(name, "P010") == 0)
  1187. return VIDEO_FORMAT_P010;
  1188. else if (astrcmpi(name, "P216") == 0)
  1189. return VIDEO_FORMAT_P216;
  1190. else if (astrcmpi(name, "P416") == 0)
  1191. return VIDEO_FORMAT_P416;
  1192. #if 0 //currently unsupported
  1193. else if (astrcmpi(name, "YVYU") == 0)
  1194. return VIDEO_FORMAT_YVYU;
  1195. else if (astrcmpi(name, "YUY2") == 0)
  1196. return VIDEO_FORMAT_YUY2;
  1197. else if (astrcmpi(name, "UYVY") == 0)
  1198. return VIDEO_FORMAT_UYVY;
  1199. #endif
  1200. else
  1201. return VIDEO_FORMAT_BGRA;
  1202. }
  1203. static inline enum video_colorspace GetVideoColorSpaceFromName(const char *name)
  1204. {
  1205. enum video_colorspace colorspace = VIDEO_CS_SRGB;
  1206. if (strcmp(name, "601") == 0)
  1207. colorspace = VIDEO_CS_601;
  1208. else if (strcmp(name, "709") == 0)
  1209. colorspace = VIDEO_CS_709;
  1210. else if (strcmp(name, "2100PQ") == 0)
  1211. colorspace = VIDEO_CS_2100_PQ;
  1212. else if (strcmp(name, "2100HLG") == 0)
  1213. colorspace = VIDEO_CS_2100_HLG;
  1214. return colorspace;
  1215. }
  1216. int OBSBasic::ResetVideo()
  1217. {
  1218. if (outputHandler && outputHandler->Active())
  1219. return OBS_VIDEO_CURRENTLY_ACTIVE;
  1220. ProfileScope("OBSBasic::ResetVideo");
  1221. struct obs_video_info ovi;
  1222. int ret;
  1223. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  1224. const char *colorFormat = config_get_string(activeConfiguration, "Video", "ColorFormat");
  1225. const char *colorSpace = config_get_string(activeConfiguration, "Video", "ColorSpace");
  1226. const char *colorRange = config_get_string(activeConfiguration, "Video", "ColorRange");
  1227. ovi.graphics_module = App()->GetRenderModule();
  1228. ovi.base_width = (uint32_t)config_get_uint(activeConfiguration, "Video", "BaseCX");
  1229. ovi.base_height = (uint32_t)config_get_uint(activeConfiguration, "Video", "BaseCY");
  1230. ovi.output_width = (uint32_t)config_get_uint(activeConfiguration, "Video", "OutputCX");
  1231. ovi.output_height = (uint32_t)config_get_uint(activeConfiguration, "Video", "OutputCY");
  1232. ovi.output_format = GetVideoFormatFromName(colorFormat);
  1233. ovi.colorspace = GetVideoColorSpaceFromName(colorSpace);
  1234. ovi.range = astrcmpi(colorRange, "Full") == 0 ? VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  1235. ovi.adapter = config_get_uint(App()->GetUserConfig(), "Video", "AdapterIdx");
  1236. ovi.gpu_conversion = true;
  1237. ovi.scale_type = GetScaleType(activeConfiguration);
  1238. if (ovi.base_width < 32 || ovi.base_height < 32) {
  1239. ovi.base_width = 1920;
  1240. ovi.base_height = 1080;
  1241. config_set_uint(activeConfiguration, "Video", "BaseCX", 1920);
  1242. config_set_uint(activeConfiguration, "Video", "BaseCY", 1080);
  1243. }
  1244. if (ovi.output_width < 32 || ovi.output_height < 32) {
  1245. ovi.output_width = ovi.base_width;
  1246. ovi.output_height = ovi.base_height;
  1247. config_set_uint(activeConfiguration, "Video", "OutputCX", ovi.base_width);
  1248. config_set_uint(activeConfiguration, "Video", "OutputCY", ovi.base_height);
  1249. }
  1250. ret = AttemptToResetVideo(&ovi);
  1251. if (ret == OBS_VIDEO_CURRENTLY_ACTIVE) {
  1252. blog(LOG_WARNING, "Tried to reset when already active");
  1253. return ret;
  1254. }
  1255. if (ret == OBS_VIDEO_SUCCESS) {
  1256. ResizePreview(ovi.base_width, ovi.base_height);
  1257. if (program)
  1258. ResizeProgram(ovi.base_width, ovi.base_height);
  1259. const float sdr_white_level = (float)config_get_uint(activeConfiguration, "Video", "SdrWhiteLevel");
  1260. const float hdr_nominal_peak_level =
  1261. (float)config_get_uint(activeConfiguration, "Video", "HdrNominalPeakLevel");
  1262. obs_set_video_levels(sdr_white_level, hdr_nominal_peak_level);
  1263. OBSBasicStats::InitializeValues();
  1264. OBSProjector::UpdateMultiviewProjectors();
  1265. if (!collections.empty()) {
  1266. const OBS::SceneCollection currentSceneCollection = OBSBasic::GetCurrentSceneCollection();
  1267. bool usingAbsoluteCoordinates = currentSceneCollection.getCoordinateMode() ==
  1268. OBS::SceneCoordinateMode::Absolute;
  1269. OBS::Rect migrationResolution = currentSceneCollection.getMigrationResolution();
  1270. OBS::Rect videoResolution = OBS::Rect(ovi.base_width, ovi.base_height);
  1271. bool canMigrate = usingAbsoluteCoordinates ||
  1272. (!migrationResolution.isZero() && migrationResolution != videoResolution);
  1273. ui->actionRemigrateSceneCollection->setEnabled(canMigrate);
  1274. } else {
  1275. ui->actionRemigrateSceneCollection->setEnabled(false);
  1276. }
  1277. emit CanvasResized(ovi.base_width, ovi.base_height);
  1278. emit OutputResized(ovi.output_width, ovi.output_height);
  1279. }
  1280. return ret;
  1281. }
  1282. bool OBSBasic::ResetAudio()
  1283. {
  1284. ProfileScope("OBSBasic::ResetAudio");
  1285. struct obs_audio_info2 ai = {};
  1286. ai.samples_per_sec = config_get_uint(activeConfiguration, "Audio", "SampleRate");
  1287. const char *channelSetupStr = config_get_string(activeConfiguration, "Audio", "ChannelSetup");
  1288. if (strcmp(channelSetupStr, "Mono") == 0)
  1289. ai.speakers = SPEAKERS_MONO;
  1290. else if (strcmp(channelSetupStr, "2.1") == 0)
  1291. ai.speakers = SPEAKERS_2POINT1;
  1292. else if (strcmp(channelSetupStr, "4.0") == 0)
  1293. ai.speakers = SPEAKERS_4POINT0;
  1294. else if (strcmp(channelSetupStr, "4.1") == 0)
  1295. ai.speakers = SPEAKERS_4POINT1;
  1296. else if (strcmp(channelSetupStr, "5.1") == 0)
  1297. ai.speakers = SPEAKERS_5POINT1;
  1298. else if (strcmp(channelSetupStr, "7.1") == 0)
  1299. ai.speakers = SPEAKERS_7POINT1;
  1300. else
  1301. ai.speakers = SPEAKERS_STEREO;
  1302. bool lowLatencyAudioBuffering = config_get_bool(App()->GetUserConfig(), "Audio", "LowLatencyAudioBuffering");
  1303. if (lowLatencyAudioBuffering) {
  1304. ai.max_buffering_ms = 20;
  1305. ai.fixed_buffering = true;
  1306. }
  1307. return obs_reset_audio2(&ai);
  1308. }
  1309. void OBSBasic::closeEvent(QCloseEvent *event)
  1310. {
  1311. /* Wait for multitrack video stream to start/finish processing in the background */
  1312. if (setupStreamingGuard.valid() &&
  1313. setupStreamingGuard.wait_for(std::chrono::seconds{0}) != std::future_status::ready) {
  1314. QTimer::singleShot(1000, this, &OBSBasic::close);
  1315. event->ignore();
  1316. return;
  1317. }
  1318. /* Do not close window if inside of a temporary event loop because we
  1319. * could be inside of an Auth::LoadUI call. Keep trying once per
  1320. * second until we've exit any known sub-loops. */
  1321. if (os_atomic_load_long(&insideEventLoop) != 0) {
  1322. QTimer::singleShot(1000, this, &OBSBasic::close);
  1323. event->ignore();
  1324. return;
  1325. }
  1326. #ifdef YOUTUBE_ENABLED
  1327. /* Also don't close the window if the youtube stream check is active */
  1328. if (youtubeStreamCheckThread) {
  1329. QTimer::singleShot(1000, this, &OBSBasic::close);
  1330. event->ignore();
  1331. return;
  1332. }
  1333. #endif
  1334. if (isVisible())
  1335. config_set_string(App()->GetUserConfig(), "BasicWindow", "geometry",
  1336. saveGeometry().toBase64().constData());
  1337. bool confirmOnExit = config_get_bool(App()->GetUserConfig(), "General", "ConfirmOnExit");
  1338. if (confirmOnExit && outputHandler && outputHandler->Active() && !clearingFailed) {
  1339. SetShowing(true);
  1340. QMessageBox::StandardButton button =
  1341. OBSMessageBox::question(this, QTStr("ConfirmExit.Title"), QTStr("ConfirmExit.Text"));
  1342. if (button == QMessageBox::No) {
  1343. event->ignore();
  1344. restart = false;
  1345. return;
  1346. }
  1347. }
  1348. if (remux && !remux->close()) {
  1349. event->ignore();
  1350. restart = false;
  1351. return;
  1352. }
  1353. QWidget::closeEvent(event);
  1354. if (!event->isAccepted())
  1355. return;
  1356. blog(LOG_INFO, SHUTDOWN_SEPARATOR);
  1357. closing = true;
  1358. /* While closing, a resize event to OBSQTDisplay could be triggered.
  1359. * The graphics thread on macOS dispatches a lambda function to be
  1360. * executed asynchronously in the main thread. However, the display is
  1361. * sometimes deleted before the lambda function is actually executed.
  1362. * To avoid such a case, destroy displays earlier than others such as
  1363. * deleting browser docks. */
  1364. ui->preview->DestroyDisplay();
  1365. if (program)
  1366. program->DestroyDisplay();
  1367. if (outputHandler->VirtualCamActive())
  1368. outputHandler->StopVirtualCam();
  1369. if (introCheckThread)
  1370. introCheckThread->wait();
  1371. if (whatsNewInitThread)
  1372. whatsNewInitThread->wait();
  1373. if (updateCheckThread)
  1374. updateCheckThread->wait();
  1375. if (logUploadThread)
  1376. logUploadThread->wait();
  1377. if (devicePropertiesThread && devicePropertiesThread->isRunning()) {
  1378. devicePropertiesThread->wait();
  1379. devicePropertiesThread.reset();
  1380. }
  1381. QApplication::sendPostedEvents(nullptr);
  1382. signalHandlers.clear();
  1383. Auth::Save();
  1384. SaveProjectNow();
  1385. auth.reset();
  1386. delete extraBrowsers;
  1387. config_set_string(App()->GetUserConfig(), "BasicWindow", "DockState", saveState().toBase64().constData());
  1388. #ifdef BROWSER_AVAILABLE
  1389. if (cef)
  1390. SaveExtraBrowserDocks();
  1391. ClearExtraBrowserDocks();
  1392. #endif
  1393. OnEvent(OBS_FRONTEND_EVENT_SCRIPTING_SHUTDOWN);
  1394. disableSaving++;
  1395. /* Clear all scene data (dialogs, widgets, widget sub-items, scenes,
  1396. * sources, etc) so that all references are released before shutdown */
  1397. ClearSceneData();
  1398. OnEvent(OBS_FRONTEND_EVENT_EXIT);
  1399. // Destroys the frontend API so plugins can't continue calling it
  1400. obs_frontend_set_callbacks_internal(nullptr);
  1401. api = nullptr;
  1402. QMetaObject::invokeMethod(App(), "quit", Qt::QueuedConnection);
  1403. }
  1404. bool OBSBasic::nativeEvent(const QByteArray &, void *message, qintptr *)
  1405. {
  1406. #ifdef _WIN32
  1407. const MSG &msg = *static_cast<MSG *>(message);
  1408. switch (msg.message) {
  1409. case WM_MOVE:
  1410. for (OBSQTDisplay *const display : findChildren<OBSQTDisplay *>()) {
  1411. display->OnMove();
  1412. }
  1413. break;
  1414. case WM_DISPLAYCHANGE:
  1415. for (OBSQTDisplay *const display : findChildren<OBSQTDisplay *>()) {
  1416. display->OnDisplayChange();
  1417. }
  1418. }
  1419. #else
  1420. UNUSED_PARAMETER(message);
  1421. #endif
  1422. return false;
  1423. }
  1424. void OBSBasic::changeEvent(QEvent *event)
  1425. {
  1426. if (event->type() == QEvent::WindowStateChange) {
  1427. QWindowStateChangeEvent *stateEvent = (QWindowStateChangeEvent *)event;
  1428. if (isMinimized()) {
  1429. if (trayIcon && trayIcon->isVisible() && sysTrayMinimizeToTray()) {
  1430. ToggleShowHide();
  1431. return;
  1432. }
  1433. if (previewEnabled)
  1434. EnablePreviewDisplay(false);
  1435. } else if (stateEvent->oldState() & Qt::WindowMinimized && isVisible()) {
  1436. if (previewEnabled)
  1437. EnablePreviewDisplay(true);
  1438. }
  1439. }
  1440. }
  1441. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  1442. {
  1443. const char *val = config_get_string(activeConfiguration, "Video", "FPSCommon");
  1444. if (strcmp(val, "10") == 0) {
  1445. num = 10;
  1446. den = 1;
  1447. } else if (strcmp(val, "20") == 0) {
  1448. num = 20;
  1449. den = 1;
  1450. } else if (strcmp(val, "24 NTSC") == 0) {
  1451. num = 24000;
  1452. den = 1001;
  1453. } else if (strcmp(val, "25 PAL") == 0) {
  1454. num = 25;
  1455. den = 1;
  1456. } else if (strcmp(val, "29.97") == 0) {
  1457. num = 30000;
  1458. den = 1001;
  1459. } else if (strcmp(val, "48") == 0) {
  1460. num = 48;
  1461. den = 1;
  1462. } else if (strcmp(val, "50 PAL") == 0) {
  1463. num = 50;
  1464. den = 1;
  1465. } else if (strcmp(val, "59.94") == 0) {
  1466. num = 60000;
  1467. den = 1001;
  1468. } else if (strcmp(val, "60") == 0) {
  1469. num = 60;
  1470. den = 1;
  1471. } else {
  1472. num = 30;
  1473. den = 1;
  1474. }
  1475. }
  1476. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  1477. {
  1478. num = (uint32_t)config_get_uint(activeConfiguration, "Video", "FPSInt");
  1479. den = 1;
  1480. }
  1481. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  1482. {
  1483. num = (uint32_t)config_get_uint(activeConfiguration, "Video", "FPSNum");
  1484. den = (uint32_t)config_get_uint(activeConfiguration, "Video", "FPSDen");
  1485. }
  1486. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  1487. {
  1488. num = 1000000000;
  1489. den = (uint32_t)config_get_uint(activeConfiguration, "Video", "FPSNS");
  1490. }
  1491. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  1492. {
  1493. uint32_t type = config_get_uint(activeConfiguration, "Video", "FPSType");
  1494. if (type == 1) //"Integer"
  1495. GetFPSInteger(num, den);
  1496. else if (type == 2) //"Fraction"
  1497. GetFPSFraction(num, den);
  1498. /*
  1499. * else if (false) //"Nanoseconds", currently not implemented
  1500. * GetFPSNanoseconds(num, den);
  1501. */
  1502. else
  1503. GetFPSCommon(num, den);
  1504. }
  1505. config_t *OBSBasic::Config() const
  1506. {
  1507. return activeConfiguration;
  1508. }
  1509. void OBSBasic::UpdateEditMenu()
  1510. {
  1511. QModelIndexList items = GetAllSelectedSourceItems();
  1512. int totalCount = items.count();
  1513. size_t filter_count = 0;
  1514. if (totalCount == 1) {
  1515. OBSSceneItem sceneItem = ui->sources->Get(GetTopSelectedSourceItem());
  1516. OBSSource source = obs_sceneitem_get_source(sceneItem);
  1517. filter_count = obs_source_filter_count(source);
  1518. }
  1519. bool allowPastingDuplicate = !!clipboard.size();
  1520. for (size_t i = clipboard.size(); i > 0; i--) {
  1521. const size_t idx = i - 1;
  1522. OBSWeakSource &weak = clipboard[idx].weak_source;
  1523. if (obs_weak_source_expired(weak)) {
  1524. clipboard.erase(clipboard.begin() + idx);
  1525. continue;
  1526. }
  1527. OBSSourceAutoRelease strong = obs_weak_source_get_source(weak.Get());
  1528. if (allowPastingDuplicate && obs_source_get_output_flags(strong) & OBS_SOURCE_DO_NOT_DUPLICATE)
  1529. allowPastingDuplicate = false;
  1530. }
  1531. int videoCount = 0;
  1532. bool canTransformMultiple = false;
  1533. for (int i = 0; i < totalCount; i++) {
  1534. OBSSceneItem item = ui->sources->Get(items.value(i).row());
  1535. OBSSource source = obs_sceneitem_get_source(item);
  1536. const uint32_t flags = obs_source_get_output_flags(source);
  1537. const bool hasVideo = (flags & OBS_SOURCE_VIDEO) != 0;
  1538. if (hasVideo && !obs_sceneitem_locked(item))
  1539. canTransformMultiple = true;
  1540. if (hasVideo)
  1541. videoCount++;
  1542. }
  1543. const bool canTransformSingle = videoCount == 1 && totalCount == 1;
  1544. OBSSceneItem curItem = GetCurrentSceneItem();
  1545. bool locked = curItem && obs_sceneitem_locked(curItem);
  1546. ui->actionCopySource->setEnabled(totalCount > 0);
  1547. ui->actionEditTransform->setEnabled(canTransformSingle && !locked);
  1548. ui->actionCopyTransform->setEnabled(canTransformSingle);
  1549. ui->actionPasteTransform->setEnabled(canTransformMultiple && hasCopiedTransform && videoCount > 0);
  1550. ui->actionCopyFilters->setEnabled(filter_count > 0);
  1551. ui->actionPasteFilters->setEnabled(!obs_weak_source_expired(copyFiltersSource) && totalCount > 0);
  1552. ui->actionPasteRef->setEnabled(!!clipboard.size());
  1553. ui->actionPasteDup->setEnabled(allowPastingDuplicate);
  1554. ui->actionMoveUp->setEnabled(totalCount > 0);
  1555. ui->actionMoveDown->setEnabled(totalCount > 0);
  1556. ui->actionMoveToTop->setEnabled(totalCount > 0);
  1557. ui->actionMoveToBottom->setEnabled(totalCount > 0);
  1558. ui->actionResetTransform->setEnabled(canTransformMultiple);
  1559. ui->actionRotate90CW->setEnabled(canTransformMultiple);
  1560. ui->actionRotate90CCW->setEnabled(canTransformMultiple);
  1561. ui->actionRotate180->setEnabled(canTransformMultiple);
  1562. ui->actionFlipHorizontal->setEnabled(canTransformMultiple);
  1563. ui->actionFlipVertical->setEnabled(canTransformMultiple);
  1564. ui->actionFitToScreen->setEnabled(canTransformMultiple);
  1565. ui->actionStretchToScreen->setEnabled(canTransformMultiple);
  1566. ui->actionCenterToScreen->setEnabled(canTransformMultiple);
  1567. ui->actionVerticalCenter->setEnabled(canTransformMultiple);
  1568. ui->actionHorizontalCenter->setEnabled(canTransformMultiple);
  1569. }
  1570. void OBSBasic::UpdateTitleBar()
  1571. {
  1572. stringstream name;
  1573. const char *profile = config_get_string(App()->GetUserConfig(), "Basic", "Profile");
  1574. const char *sceneCollection = config_get_string(App()->GetUserConfig(), "Basic", "SceneCollection");
  1575. name << "OBS ";
  1576. if (previewProgramMode)
  1577. name << "Studio ";
  1578. name << App()->GetVersionString(false);
  1579. if (safe_mode)
  1580. name << " (" << Str("TitleBar.SafeMode") << ")";
  1581. if (App()->IsPortableMode())
  1582. name << " - " << Str("TitleBar.PortableMode");
  1583. name << " - " << Str("TitleBar.Profile") << ": " << profile;
  1584. name << " - " << Str("TitleBar.Scenes") << ": " << sceneCollection;
  1585. setWindowTitle(QT_UTF8(name.str().c_str()));
  1586. }
  1587. OBSBasic *OBSBasic::Get()
  1588. {
  1589. return reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1590. }
  1591. void OBSBasic::UpdatePatronJson(const QString &text, const QString &error)
  1592. {
  1593. if (!error.isEmpty())
  1594. return;
  1595. patronJson = QT_TO_UTF8(text);
  1596. }
  1597. void OBSBasic::SetDisplayAffinity(QWindow *window)
  1598. {
  1599. if (!SetDisplayAffinitySupported())
  1600. return;
  1601. bool hideFromCapture = config_get_bool(App()->GetUserConfig(), "BasicWindow", "HideOBSWindowsFromCapture");
  1602. // Don't hide projectors, those are designed to be visible / captured
  1603. if (window->property("isOBSProjectorWindow") == true)
  1604. return;
  1605. #ifdef _WIN32
  1606. HWND hwnd = (HWND)window->winId();
  1607. DWORD curAffinity;
  1608. if (GetWindowDisplayAffinity(hwnd, &curAffinity)) {
  1609. if (hideFromCapture && curAffinity != WDA_EXCLUDEFROMCAPTURE)
  1610. SetWindowDisplayAffinity(hwnd, WDA_EXCLUDEFROMCAPTURE);
  1611. else if (!hideFromCapture && curAffinity != WDA_NONE)
  1612. SetWindowDisplayAffinity(hwnd, WDA_NONE);
  1613. }
  1614. #else
  1615. // TODO: Implement for other platforms if possible. Don't forget to
  1616. // implement SetDisplayAffinitySupported too!
  1617. UNUSED_PARAMETER(hideFromCapture);
  1618. #endif
  1619. }
  1620. void OBSBasic::OnEvent(enum obs_frontend_event event)
  1621. {
  1622. if (api)
  1623. api->on_event(event);
  1624. }
  1625. OBSPromptResult OBSBasic::PromptForName(const OBSPromptRequest &request, const OBSPromptCallback &callback)
  1626. {
  1627. OBSPromptResult result;
  1628. for (;;) {
  1629. result.success = false;
  1630. if (request.withOption && !request.optionPrompt.empty()) {
  1631. result.optionValue = request.optionValue;
  1632. result.success = NameDialog::AskForNameWithOption(
  1633. this, request.title.c_str(), request.prompt.c_str(), result.promptValue,
  1634. request.optionPrompt.c_str(), result.optionValue,
  1635. (request.promptValue.empty() ? nullptr : request.promptValue.c_str()));
  1636. } else {
  1637. result.success = NameDialog::AskForName(
  1638. this, request.title.c_str(), request.prompt.c_str(), result.promptValue,
  1639. (request.promptValue.empty() ? nullptr : request.promptValue.c_str()));
  1640. }
  1641. if (!result.success) {
  1642. break;
  1643. }
  1644. if (result.promptValue.empty()) {
  1645. OBSMessageBox::warning(this, QTStr("NoNameEntered.Title"), QTStr("NoNameEntered.Text"));
  1646. continue;
  1647. }
  1648. if (!callback(result)) {
  1649. OBSMessageBox::warning(this, QTStr("NameExists.Title"), QTStr("NameExists.Text"));
  1650. continue;
  1651. }
  1652. break;
  1653. }
  1654. return result;
  1655. }