window-basic-main.cpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh 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 <time.h>
  17. #include <obs.hpp>
  18. #include <QMessageBox>
  19. #include <QShowEvent>
  20. #include <QDesktopServices>
  21. #include <QFileDialog>
  22. #include <util/dstr.h>
  23. #include <util/util.hpp>
  24. #include <util/platform.h>
  25. #include <util/profiler.hpp>
  26. #include <graphics/math-defs.h>
  27. #include "obs-app.hpp"
  28. #include "platform.hpp"
  29. #include "visibility-item-widget.hpp"
  30. #include "item-widget-helpers.hpp"
  31. #include "window-basic-settings.hpp"
  32. #include "window-namedialog.hpp"
  33. #include "window-basic-source-select.hpp"
  34. #include "window-basic-main.hpp"
  35. #include "window-basic-main-outputs.hpp"
  36. #include "window-basic-properties.hpp"
  37. #include "window-log-reply.hpp"
  38. #include "window-projector.hpp"
  39. #include "window-remux.hpp"
  40. #include "qt-wrappers.hpp"
  41. #include "display-helpers.hpp"
  42. #include "volume-control.hpp"
  43. #include "remote-text.hpp"
  44. #include "ui_OBSBasic.h"
  45. #include <fstream>
  46. #include <sstream>
  47. #include <QScreen>
  48. #include <QWindow>
  49. #define PREVIEW_EDGE_SIZE 10
  50. using namespace std;
  51. Q_DECLARE_METATYPE(OBSScene);
  52. Q_DECLARE_METATYPE(OBSSceneItem);
  53. Q_DECLARE_METATYPE(OBSSource);
  54. Q_DECLARE_METATYPE(obs_order_movement);
  55. Q_DECLARE_METATYPE(std::vector<std::shared_ptr<OBSSignal>>);
  56. template <typename T>
  57. static T GetOBSRef(QListWidgetItem *item)
  58. {
  59. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  60. }
  61. template <typename T>
  62. static void SetOBSRef(QListWidgetItem *item, T &&val)
  63. {
  64. item->setData(static_cast<int>(QtDataRole::OBSRef),
  65. QVariant::fromValue(val));
  66. }
  67. static void AddExtraModulePaths()
  68. {
  69. char base_module_dir[512];
  70. int ret = GetConfigPath(base_module_dir, sizeof(base_module_dir),
  71. "obs-studio/plugins/%module%");
  72. if (ret <= 0)
  73. return;
  74. string path = (char*)base_module_dir;
  75. #if defined(__APPLE__)
  76. obs_add_module_path((path + "/bin").c_str(), (path + "/data").c_str());
  77. #elif ARCH_BITS == 64
  78. obs_add_module_path((path + "/bin/64bit").c_str(),
  79. (path + "/data").c_str());
  80. #else
  81. obs_add_module_path((path + "/bin/32bit").c_str(),
  82. (path + "/data").c_str());
  83. #endif
  84. }
  85. static QList<QKeySequence> DeleteKeys;
  86. OBSBasic::OBSBasic(QWidget *parent)
  87. : OBSMainWindow (parent),
  88. ui (new Ui::OBSBasic)
  89. {
  90. ui->setupUi(this);
  91. ui->previewDisabledLabel->setVisible(false);
  92. copyActionsDynamicProperties();
  93. ui->sources->setItemDelegate(new VisibilityItemDelegate(ui->sources));
  94. int width = config_get_int(App()->GlobalConfig(), "BasicWindow", "cx");
  95. // Check if no values are saved (new installation).
  96. if (width != 0) {
  97. int height = config_get_int(App()->GlobalConfig(),
  98. "BasicWindow", "cy");
  99. int posx = config_get_int(App()->GlobalConfig(), "BasicWindow",
  100. "posx");
  101. int posy = config_get_int(App()->GlobalConfig(), "BasicWindow",
  102. "posy");
  103. setGeometry(posx, posy, width, height);
  104. }
  105. char styleSheetPath[512];
  106. int ret = GetProfilePath(styleSheetPath, sizeof(styleSheetPath),
  107. "stylesheet.qss");
  108. if (ret > 0) {
  109. if (QFile::exists(styleSheetPath)) {
  110. QString path = QString("file:///") +
  111. QT_UTF8(styleSheetPath);
  112. App()->setStyleSheet(path);
  113. }
  114. }
  115. qRegisterMetaType<OBSScene> ("OBSScene");
  116. qRegisterMetaType<OBSSceneItem>("OBSSceneItem");
  117. qRegisterMetaType<OBSSource> ("OBSSource");
  118. qRegisterMetaType<obs_hotkey_id>("obs_hotkey_id");
  119. qRegisterMetaTypeStreamOperators<
  120. std::vector<std::shared_ptr<OBSSignal>>>(
  121. "std::vector<std::shared_ptr<OBSSignal>>");
  122. qRegisterMetaTypeStreamOperators<OBSScene>("OBSScene");
  123. qRegisterMetaTypeStreamOperators<OBSSceneItem>("OBSSceneItem");
  124. ui->scenes->setAttribute(Qt::WA_MacShowFocusRect, false);
  125. ui->sources->setAttribute(Qt::WA_MacShowFocusRect, false);
  126. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  127. struct obs_video_info ovi;
  128. if (obs_get_video_info(&ovi))
  129. ResizePreview(ovi.base_width, ovi.base_height);
  130. });
  131. installEventFilter(CreateShortcutFilter());
  132. stringstream name;
  133. name << "OBS " << App()->GetVersionString();
  134. blog(LOG_INFO, "%s", name.str().c_str());
  135. blog(LOG_INFO, "---------------------------------");
  136. UpdateTitleBar();
  137. connect(ui->scenes->itemDelegate(),
  138. SIGNAL(closeEditor(QWidget*,
  139. QAbstractItemDelegate::EndEditHint)),
  140. this,
  141. SLOT(SceneNameEdited(QWidget*,
  142. QAbstractItemDelegate::EndEditHint)));
  143. connect(ui->sources->itemDelegate(),
  144. SIGNAL(closeEditor(QWidget*,
  145. QAbstractItemDelegate::EndEditHint)),
  146. this,
  147. SLOT(SceneItemNameEdited(QWidget*,
  148. QAbstractItemDelegate::EndEditHint)));
  149. cpuUsageInfo = os_cpu_usage_info_start();
  150. cpuUsageTimer = new QTimer(this);
  151. connect(cpuUsageTimer, SIGNAL(timeout()),
  152. ui->statusbar, SLOT(UpdateCPUUsage()));
  153. cpuUsageTimer->start(3000);
  154. DeleteKeys =
  155. #ifdef __APPLE__
  156. QList<QKeySequence>{{Qt::Key_Backspace}} <<
  157. #endif
  158. QKeySequence::keyBindings(QKeySequence::Delete);
  159. #ifdef __APPLE__
  160. ui->actionRemoveSource->setShortcuts(DeleteKeys);
  161. ui->actionRemoveScene->setShortcuts(DeleteKeys);
  162. ui->action_Settings->setMenuRole(QAction::PreferencesRole);
  163. ui->actionE_xit->setMenuRole(QAction::QuitRole);
  164. #endif
  165. auto addNudge = [this](const QKeySequence &seq, const char *s)
  166. {
  167. QAction *nudge = new QAction(ui->preview);
  168. nudge->setShortcut(seq);
  169. nudge->setShortcutContext(Qt::WidgetShortcut);
  170. ui->preview->addAction(nudge);
  171. connect(nudge, SIGNAL(triggered()), this, s);
  172. };
  173. addNudge(Qt::Key_Up, SLOT(NudgeUp()));
  174. addNudge(Qt::Key_Down, SLOT(NudgeDown()));
  175. addNudge(Qt::Key_Left, SLOT(NudgeLeft()));
  176. addNudge(Qt::Key_Right, SLOT(NudgeRight()));
  177. }
  178. static void SaveAudioDevice(const char *name, int channel, obs_data_t *parent)
  179. {
  180. obs_source_t *source = obs_get_output_source(channel);
  181. if (!source)
  182. return;
  183. obs_data_t *data = obs_save_source(source);
  184. obs_data_set_obj(parent, name, data);
  185. obs_data_release(data);
  186. obs_source_release(source);
  187. }
  188. static obs_data_t *GenerateSaveData(obs_data_array_t *sceneOrder)
  189. {
  190. obs_data_t *saveData = obs_data_create();
  191. obs_data_array_t *sourcesArray = obs_save_sources();
  192. obs_source_t *currentScene = obs_get_output_source(0);
  193. const char *sceneName = obs_source_get_name(currentScene);
  194. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  195. "Basic", "SceneCollection");
  196. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  197. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  198. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  199. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  200. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  201. obs_data_set_string(saveData, "current_scene", sceneName);
  202. obs_data_set_array(saveData, "scene_order", sceneOrder);
  203. obs_data_set_string(saveData, "name", sceneCollection);
  204. obs_data_set_array(saveData, "sources", sourcesArray);
  205. obs_data_array_release(sourcesArray);
  206. obs_source_release(currentScene);
  207. return saveData;
  208. }
  209. void OBSBasic::copyActionsDynamicProperties()
  210. {
  211. // Themes need the QAction dynamic properties
  212. for (QAction *x : ui->scenesToolbar->actions()) {
  213. QWidget* temp = ui->scenesToolbar->widgetForAction(x);
  214. for (QByteArray &y : x->dynamicPropertyNames()) {
  215. temp->setProperty(y, x->property(y));
  216. }
  217. }
  218. for (QAction *x : ui->sourcesToolbar->actions()) {
  219. QWidget* temp = ui->sourcesToolbar->widgetForAction(x);
  220. for (QByteArray &y : x->dynamicPropertyNames()) {
  221. temp->setProperty(y, x->property(y));
  222. }
  223. }
  224. }
  225. void OBSBasic::ClearVolumeControls()
  226. {
  227. VolControl *control;
  228. for (size_t i = 0; i < volumes.size(); i++) {
  229. control = volumes[i];
  230. delete control;
  231. }
  232. volumes.clear();
  233. }
  234. obs_data_array_t *OBSBasic::SaveSceneListOrder()
  235. {
  236. obs_data_array_t *sceneOrder = obs_data_array_create();
  237. for (int i = 0; i < ui->scenes->count(); i++) {
  238. obs_data_t *data = obs_data_create();
  239. obs_data_set_string(data, "name",
  240. QT_TO_UTF8(ui->scenes->item(i)->text()));
  241. obs_data_array_push_back(sceneOrder, data);
  242. obs_data_release(data);
  243. }
  244. return sceneOrder;
  245. }
  246. void OBSBasic::Save(const char *file)
  247. {
  248. obs_data_array_t *sceneOrder = SaveSceneListOrder();
  249. obs_data_t *saveData = GenerateSaveData(sceneOrder);
  250. if (!obs_data_save_json_safe(saveData, file, "tmp", "bak"))
  251. blog(LOG_ERROR, "Could not save scene data to %s", file);
  252. obs_data_release(saveData);
  253. obs_data_array_release(sceneOrder);
  254. }
  255. static void LoadAudioDevice(const char *name, int channel, obs_data_t *parent)
  256. {
  257. obs_data_t *data = obs_data_get_obj(parent, name);
  258. if (!data)
  259. return;
  260. obs_source_t *source = obs_load_source(data);
  261. if (source) {
  262. obs_set_output_source(channel, source);
  263. obs_source_release(source);
  264. }
  265. obs_data_release(data);
  266. }
  267. static inline bool HasAudioDevices(const char *source_id)
  268. {
  269. const char *output_id = source_id;
  270. obs_properties_t *props = obs_get_source_properties(
  271. OBS_SOURCE_TYPE_INPUT, output_id);
  272. size_t count = 0;
  273. if (!props)
  274. return false;
  275. obs_property_t *devices = obs_properties_get(props, "device_id");
  276. if (devices)
  277. count = obs_property_list_item_count(devices);
  278. obs_properties_destroy(props);
  279. return count != 0;
  280. }
  281. void OBSBasic::CreateDefaultScene()
  282. {
  283. disableSaving++;
  284. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  285. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  286. ClearSceneData();
  287. obs_scene_t *scene = obs_scene_create(Str("Basic.Scene"));
  288. obs_source_t *source = obs_scene_get_source(scene);
  289. obs_add_source(source);
  290. #ifdef __APPLE__
  291. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, "display_capture",
  292. Str("Basic.DisplayCapture"), NULL, nullptr);
  293. if (source) {
  294. obs_scene_add(scene, source);
  295. obs_add_source(source);
  296. obs_source_release(source);
  297. }
  298. #endif
  299. obs_set_output_source(0, obs_scene_get_source(scene));
  300. obs_scene_release(scene);
  301. if (hasDesktopAudio)
  302. ResetAudioDevice(App()->OutputAudioSource(), "default",
  303. Str("Basic.DesktopDevice1"), 1);
  304. if (hasInputAudio)
  305. ResetAudioDevice(App()->InputAudioSource(), "default",
  306. Str("Basic.AuxDevice1"), 3);
  307. disableSaving--;
  308. }
  309. static void ReorderItemByName(QListWidget *lw, const char *name, int newIndex)
  310. {
  311. for (int i = 0; i < lw->count(); i++) {
  312. QListWidgetItem *item = lw->item(i);
  313. if (strcmp(name, QT_TO_UTF8(item->text())) == 0) {
  314. if (newIndex != i) {
  315. item = lw->takeItem(i);
  316. lw->insertItem(newIndex, item);
  317. }
  318. break;
  319. }
  320. }
  321. }
  322. void OBSBasic::LoadSceneListOrder(obs_data_array_t *array)
  323. {
  324. size_t num = obs_data_array_count(array);
  325. for (size_t i = 0; i < num; i++) {
  326. obs_data_t *data = obs_data_array_item(array, i);
  327. const char *name = obs_data_get_string(data, "name");
  328. ReorderItemByName(ui->scenes, name, (int)i);
  329. obs_data_release(data);
  330. }
  331. }
  332. void OBSBasic::CleanupUnusedSources()
  333. {
  334. auto removeUnusedSources = [&](obs_source_t *source)
  335. {
  336. obs_scene_t *scene = obs_scene_from_source(source);
  337. if (scene)
  338. return;
  339. if (sourceSceneRefs[source] == 0) {
  340. sourceSceneRefs.erase(source);
  341. obs_source_remove(source);
  342. }
  343. };
  344. using func_type = decltype(removeUnusedSources);
  345. obs_enum_sources(
  346. [](void *f, obs_source_t *source)
  347. {
  348. (*static_cast<func_type*>(f))(source);
  349. return true;
  350. },
  351. static_cast<void*>(&removeUnusedSources));
  352. }
  353. void OBSBasic::Load(const char *file)
  354. {
  355. if (!file || !os_file_exists(file)) {
  356. blog(LOG_ERROR, "Could not find file %s", file);
  357. return;
  358. }
  359. disableSaving++;
  360. obs_data_t *data = obs_data_create_from_json_file_safe(file, "bak");
  361. if (!data) {
  362. disableSaving--;
  363. CreateDefaultScene();
  364. SaveProject();
  365. return;
  366. }
  367. ClearSceneData();
  368. obs_data_array_t *sceneOrder = obs_data_get_array(data, "scene_order");
  369. obs_data_array_t *sources = obs_data_get_array(data, "sources");
  370. const char *sceneName = obs_data_get_string(data,
  371. "current_scene");
  372. const char *curSceneCollection = config_get_string(
  373. App()->GlobalConfig(), "Basic", "SceneCollection");
  374. obs_data_set_default_string(data, "name", curSceneCollection);
  375. const char *name = obs_data_get_string(data, "name");
  376. obs_source_t *curScene;
  377. if (!name || !*name)
  378. name = curSceneCollection;
  379. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  380. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  381. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  382. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  383. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  384. obs_load_sources(sources);
  385. if (sceneOrder)
  386. LoadSceneListOrder(sceneOrder);
  387. curScene = obs_get_source_by_name(sceneName);
  388. obs_set_output_source(0, curScene);
  389. obs_source_release(curScene);
  390. obs_data_array_release(sources);
  391. obs_data_array_release(sceneOrder);
  392. std::string file_base = strrchr(file, '/') + 1;
  393. file_base.erase(file_base.size() - 5, 5);
  394. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  395. name);
  396. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  397. file_base.c_str());
  398. obs_data_release(data);
  399. CleanupUnusedSources();
  400. disableSaving--;
  401. }
  402. #define SERVICE_PATH "service.json"
  403. void OBSBasic::SaveService()
  404. {
  405. if (!service)
  406. return;
  407. char serviceJsonPath[512];
  408. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  409. SERVICE_PATH);
  410. if (ret <= 0)
  411. return;
  412. obs_data_t *data = obs_data_create();
  413. obs_data_t *settings = obs_service_get_settings(service);
  414. obs_data_set_string(data, "type", obs_service_get_type(service));
  415. obs_data_set_obj(data, "settings", settings);
  416. if (!obs_data_save_json_safe(data, serviceJsonPath, "tmp", "bak"))
  417. blog(LOG_WARNING, "Failed to save service");
  418. obs_data_release(settings);
  419. obs_data_release(data);
  420. }
  421. bool OBSBasic::LoadService()
  422. {
  423. const char *type;
  424. char serviceJsonPath[512];
  425. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  426. SERVICE_PATH);
  427. if (ret <= 0)
  428. return false;
  429. obs_data_t *data = obs_data_create_from_json_file_safe(serviceJsonPath,
  430. "bak");
  431. obs_data_set_default_string(data, "type", "rtmp_common");
  432. type = obs_data_get_string(data, "type");
  433. obs_data_t *settings = obs_data_get_obj(data, "settings");
  434. obs_data_t *hotkey_data = obs_data_get_obj(data, "hotkeys");
  435. service = obs_service_create(type, "default_service", settings,
  436. hotkey_data);
  437. obs_service_release(service);
  438. obs_data_release(hotkey_data);
  439. obs_data_release(settings);
  440. obs_data_release(data);
  441. return !!service;
  442. }
  443. bool OBSBasic::InitService()
  444. {
  445. ProfileScope("OBSBasic::InitService");
  446. if (LoadService())
  447. return true;
  448. service = obs_service_create("rtmp_common", "default_service", nullptr,
  449. nullptr);
  450. if (!service)
  451. return false;
  452. obs_service_release(service);
  453. return true;
  454. }
  455. static const double scaled_vals[] =
  456. {
  457. 1.0,
  458. 1.25,
  459. (1.0/0.75),
  460. 1.5,
  461. (1.0/0.6),
  462. 1.75,
  463. 2.0,
  464. 2.25,
  465. 2.5,
  466. 2.75,
  467. 3.0,
  468. 0.0
  469. };
  470. bool OBSBasic::InitBasicConfigDefaults()
  471. {
  472. vector<MonitorInfo> monitors;
  473. GetMonitors(monitors);
  474. if (!monitors.size()) {
  475. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  476. "technically shouldn't be possible.");
  477. return false;
  478. }
  479. uint32_t cx = monitors[0].cx;
  480. uint32_t cy = monitors[0].cy;
  481. /* ----------------------------------------------------- */
  482. /* move over mixer values in advanced if older config */
  483. if (config_has_user_value(basicConfig, "AdvOut", "RecTrackIndex") &&
  484. !config_has_user_value(basicConfig, "AdvOut", "RecTracks")) {
  485. uint64_t track = config_get_uint(basicConfig, "AdvOut",
  486. "RecTrackIndex");
  487. track = 1ULL << (track - 1);
  488. config_set_uint(basicConfig, "AdvOut", "RecTracks", track);
  489. config_remove_value(basicConfig, "AdvOut", "RecTrackIndex");
  490. config_save_safe(basicConfig, "tmp", nullptr);
  491. }
  492. /* ----------------------------------------------------- */
  493. config_set_default_string(basicConfig, "Output", "Mode", "Simple");
  494. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  495. GetDefaultVideoSavePath().c_str());
  496. config_set_default_string(basicConfig, "SimpleOutput", "RecFormat",
  497. "flv");
  498. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  499. 2500);
  500. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 160);
  501. config_set_default_bool (basicConfig, "SimpleOutput", "UseAdvanced",
  502. false);
  503. config_set_default_string(basicConfig, "SimpleOutput", "Preset",
  504. "veryfast");
  505. config_set_default_bool (basicConfig, "AdvOut", "ApplyServiceSettings",
  506. true);
  507. config_set_default_bool (basicConfig, "AdvOut", "UseRescale", false);
  508. config_set_default_uint (basicConfig, "AdvOut", "TrackIndex", 1);
  509. config_set_default_string(basicConfig, "AdvOut", "Encoder", "obs_x264");
  510. config_set_default_string(basicConfig, "AdvOut", "RecType", "Standard");
  511. config_set_default_string(basicConfig, "AdvOut", "RecFilePath",
  512. GetDefaultVideoSavePath().c_str());
  513. config_set_default_string(basicConfig, "AdvOut", "RecFormat", "flv");
  514. config_set_default_bool (basicConfig, "AdvOut", "RecUseRescale",
  515. false);
  516. config_set_default_uint (basicConfig, "AdvOut", "RecTracks", (1<<0));
  517. config_set_default_string(basicConfig, "AdvOut", "RecEncoder",
  518. "none");
  519. config_set_default_bool (basicConfig, "AdvOut", "FFOutputToFile",
  520. true);
  521. config_set_default_string(basicConfig, "AdvOut", "FFFilePath",
  522. GetDefaultVideoSavePath().c_str());
  523. config_set_default_string(basicConfig, "AdvOut", "FFExtension", "mp4");
  524. config_set_default_uint (basicConfig, "AdvOut", "FFVBitrate", 2500);
  525. config_set_default_bool (basicConfig, "AdvOut", "FFUseRescale",
  526. false);
  527. config_set_default_uint (basicConfig, "AdvOut", "FFABitrate", 160);
  528. config_set_default_uint (basicConfig, "AdvOut", "FFAudioTrack", 1);
  529. config_set_default_uint (basicConfig, "AdvOut", "Track1Bitrate", 160);
  530. config_set_default_uint (basicConfig, "AdvOut", "Track2Bitrate", 160);
  531. config_set_default_uint (basicConfig, "AdvOut", "Track3Bitrate", 160);
  532. config_set_default_uint (basicConfig, "AdvOut", "Track4Bitrate", 160);
  533. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  534. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  535. config_set_default_bool (basicConfig, "Output", "DelayEnable", false);
  536. config_set_default_uint (basicConfig, "Output", "DelaySec", 20);
  537. config_set_default_bool (basicConfig, "Output", "DelayPreserve", true);
  538. config_set_default_bool (basicConfig, "Output", "Reconnect", true);
  539. config_set_default_uint (basicConfig, "Output", "RetryDelay", 10);
  540. config_set_default_uint (basicConfig, "Output", "MaxRetries", 20);
  541. int i = 0;
  542. uint32_t scale_cx = cx;
  543. uint32_t scale_cy = cy;
  544. /* use a default scaled resolution that has a pixel count no higher
  545. * than 1280x720 */
  546. while (((scale_cx * scale_cy) > (1280 * 720)) && scaled_vals[i] > 0.0) {
  547. double scale = scaled_vals[i++];
  548. scale_cx = uint32_t(double(cx) / scale);
  549. scale_cy = uint32_t(double(cy) / scale);
  550. }
  551. config_set_default_uint (basicConfig, "Video", "OutputCX", scale_cx);
  552. config_set_default_uint (basicConfig, "Video", "OutputCY", scale_cy);
  553. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  554. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  555. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  556. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  557. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  558. config_set_default_string(basicConfig, "Video", "ScaleType", "bicubic");
  559. config_set_default_string(basicConfig, "Video", "ColorFormat", "NV12");
  560. config_set_default_string(basicConfig, "Video", "ColorSpace", "709");
  561. config_set_default_string(basicConfig, "Video", "ColorRange",
  562. "Partial");
  563. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  564. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  565. "Stereo");
  566. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  567. return true;
  568. }
  569. bool OBSBasic::InitBasicConfig()
  570. {
  571. ProfileScope("OBSBasic::InitBasicConfig");
  572. char configPath[512];
  573. int ret = GetProfilePath(configPath, sizeof(configPath), "");
  574. if (ret <= 0) {
  575. OBSErrorBox(nullptr, "Failed to get profile path");
  576. return false;
  577. }
  578. if (os_mkdir(configPath) == MKDIR_ERROR) {
  579. OBSErrorBox(nullptr, "Failed to create profile path");
  580. return false;
  581. }
  582. ret = GetProfilePath(configPath, sizeof(configPath), "basic.ini");
  583. if (ret <= 0) {
  584. OBSErrorBox(nullptr, "Failed to get base.ini path");
  585. return false;
  586. }
  587. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  588. if (code != CONFIG_SUCCESS) {
  589. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  590. return false;
  591. }
  592. if (config_get_string(basicConfig, "General", "Name") == nullptr) {
  593. const char *curName = config_get_string(App()->GlobalConfig(),
  594. "Basic", "Profile");
  595. config_set_string(basicConfig, "General", "Name", curName);
  596. basicConfig.SaveSafe("tmp");
  597. }
  598. return InitBasicConfigDefaults();
  599. }
  600. void OBSBasic::InitOBSCallbacks()
  601. {
  602. ProfileScope("OBSBasic::InitOBSCallbacks");
  603. signalHandlers.reserve(signalHandlers.size() + 6);
  604. signalHandlers.emplace_back(obs_get_signal_handler(), "source_add",
  605. OBSBasic::SourceAdded, this);
  606. signalHandlers.emplace_back(obs_get_signal_handler(), "source_remove",
  607. OBSBasic::SourceRemoved, this);
  608. signalHandlers.emplace_back(obs_get_signal_handler(), "channel_change",
  609. OBSBasic::ChannelChanged, this);
  610. signalHandlers.emplace_back(obs_get_signal_handler(), "source_activate",
  611. OBSBasic::SourceActivated, this);
  612. signalHandlers.emplace_back(obs_get_signal_handler(), "source_deactivate",
  613. OBSBasic::SourceDeactivated, this);
  614. signalHandlers.emplace_back(obs_get_signal_handler(), "source_rename",
  615. OBSBasic::SourceRenamed, this);
  616. }
  617. void OBSBasic::InitPrimitives()
  618. {
  619. ProfileScope("OBSBasic::InitPrimitives");
  620. obs_enter_graphics();
  621. gs_render_start(true);
  622. gs_vertex2f(0.0f, 0.0f);
  623. gs_vertex2f(0.0f, 1.0f);
  624. gs_vertex2f(1.0f, 1.0f);
  625. gs_vertex2f(1.0f, 0.0f);
  626. gs_vertex2f(0.0f, 0.0f);
  627. box = gs_render_save();
  628. gs_render_start(true);
  629. for (int i = 0; i <= 360; i += (360/20)) {
  630. float pos = RAD(float(i));
  631. gs_vertex2f(cosf(pos), sinf(pos));
  632. }
  633. circle = gs_render_save();
  634. obs_leave_graphics();
  635. }
  636. void OBSBasic::ResetOutputs()
  637. {
  638. ProfileScope("OBSBasic::ResetOutputs");
  639. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  640. bool advOut = astrcmpi(mode, "Advanced") == 0;
  641. if (!outputHandler || !outputHandler->Active()) {
  642. outputHandler.reset();
  643. outputHandler.reset(advOut ?
  644. CreateAdvancedOutputHandler(this) :
  645. CreateSimpleOutputHandler(this));
  646. } else {
  647. outputHandler->Update();
  648. }
  649. }
  650. #define MAIN_SEPARATOR \
  651. "====================================================================="
  652. void OBSBasic::OBSInit()
  653. {
  654. ProfileScope("OBSBasic::OBSInit");
  655. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  656. "Basic", "SceneCollectionFile");
  657. char savePath[512];
  658. char fileName[512];
  659. int ret;
  660. if (!sceneCollection)
  661. throw "Failed to get scene collection name";
  662. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  663. sceneCollection);
  664. if (ret <= 0)
  665. throw "Failed to create scene collection file name";
  666. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  667. if (ret <= 0)
  668. throw "Failed to get scene collection json file path";
  669. if (!InitBasicConfig())
  670. throw "Failed to load basic.ini";
  671. if (!ResetAudio())
  672. throw "Failed to initialize audio";
  673. ret = ResetVideo();
  674. switch (ret) {
  675. case OBS_VIDEO_MODULE_NOT_FOUND:
  676. throw "Failed to initialize video: Graphics module not found";
  677. case OBS_VIDEO_NOT_SUPPORTED:
  678. throw "Failed to initialize video: Required graphics API "
  679. "functionality not found on these drivers or "
  680. "unavailable on this equipment";
  681. case OBS_VIDEO_INVALID_PARAM:
  682. throw "Failed to initialize video: Invalid parameters";
  683. default:
  684. if (ret != OBS_VIDEO_SUCCESS)
  685. throw "Failed to initialize video: Unspecified error";
  686. }
  687. InitOBSCallbacks();
  688. InitHotkeys();
  689. AddExtraModulePaths();
  690. obs_load_all_modules();
  691. blog(LOG_INFO, MAIN_SEPARATOR);
  692. ResetOutputs();
  693. CreateHotkeys();
  694. if (!InitService())
  695. throw "Failed to initialize service";
  696. InitPrimitives();
  697. {
  698. ProfileScope("OBSBasic::Load");
  699. disableSaving--;
  700. Load(savePath);
  701. disableSaving++;
  702. }
  703. TimedCheckForUpdates();
  704. loaded = true;
  705. bool previewEnabled = config_get_bool(App()->GlobalConfig(),
  706. "BasicWindow", "PreviewEnabled");
  707. if (!previewEnabled)
  708. QMetaObject::invokeMethod(this, "TogglePreview",
  709. Qt::QueuedConnection);
  710. #ifdef _WIN32
  711. uint32_t winVer = GetWindowsVersion();
  712. if (winVer > 0 && winVer < 0x602) {
  713. bool disableAero = config_get_bool(basicConfig, "Video",
  714. "DisableAero");
  715. SetAeroEnabled(!disableAero);
  716. }
  717. #endif
  718. RefreshSceneCollections();
  719. RefreshProfiles();
  720. disableSaving--;
  721. auto addDisplay = [this] (OBSQTDisplay *window)
  722. {
  723. obs_display_add_draw_callback(window->GetDisplay(),
  724. OBSBasic::RenderMain, this);
  725. struct obs_video_info ovi;
  726. if (obs_get_video_info(&ovi))
  727. ResizePreview(ovi.base_width, ovi.base_height);
  728. };
  729. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDisplay);
  730. show();
  731. }
  732. void OBSBasic::InitHotkeys()
  733. {
  734. ProfileScope("OBSBasic::InitHotkeys");
  735. struct obs_hotkeys_translations t = {};
  736. t.insert = Str("Hotkeys.Insert");
  737. t.del = Str("Hotkeys.Delete");
  738. t.home = Str("Hotkeys.Home");
  739. t.end = Str("Hotkeys.End");
  740. t.page_up = Str("Hotkeys.PageUp");
  741. t.page_down = Str("Hotkeys.PageDown");
  742. t.num_lock = Str("Hotkeys.NumLock");
  743. t.scroll_lock = Str("Hotkeys.ScrollLock");
  744. t.caps_lock = Str("Hotkeys.CapsLock");
  745. t.backspace = Str("Hotkeys.Backspace");
  746. t.tab = Str("Hotkeys.Tab");
  747. t.print = Str("Hotkeys.Print");
  748. t.pause = Str("Hotkeys.Pause");
  749. t.left = Str("Hotkeys.Left");
  750. t.right = Str("Hotkeys.Right");
  751. t.up = Str("Hotkeys.Up");
  752. t.down = Str("Hotkeys.Down");
  753. #ifdef _WIN32
  754. t.meta = Str("Hotkeys.Windows");
  755. #else
  756. t.meta = Str("Hotkeys.Super");
  757. #endif
  758. t.menu = Str("Hotkeys.Menu");
  759. t.space = Str("Hotkeys.Space");
  760. t.numpad_num = Str("Hotkeys.NumpadNum");
  761. t.numpad_multiply = Str("Hotkeys.NumpadMultiply");
  762. t.numpad_divide = Str("Hotkeys.NumpadDivide");
  763. t.numpad_plus = Str("Hotkeys.NumpadAdd");
  764. t.numpad_minus = Str("Hotkeys.NumpadSubtract");
  765. t.numpad_decimal = Str("Hotkeys.NumpadDecimal");
  766. t.apple_keypad_num = Str("Hotkeys.AppleKeypadNum");
  767. t.apple_keypad_multiply = Str("Hotkeys.AppleKeypadMultiply");
  768. t.apple_keypad_divide = Str("Hotkeys.AppleKeypadDivide");
  769. t.apple_keypad_plus = Str("Hotkeys.AppleKeypadAdd");
  770. t.apple_keypad_minus = Str("Hotkeys.AppleKeypadSubtract");
  771. t.apple_keypad_decimal = Str("Hotkeys.AppleKeypadDecimal");
  772. t.apple_keypad_equal = Str("Hotkeys.AppleKeypadEqual");
  773. t.mouse_num = Str("Hotkeys.MouseButton");
  774. obs_hotkeys_set_translations(&t);
  775. obs_hotkeys_set_audio_hotkeys_translations(Str("Mute"), Str("Unmute"),
  776. Str("Push-to-mute"), Str("Push-to-talk"));
  777. obs_hotkeys_set_sceneitem_hotkeys_translations(
  778. Str("SceneItemShow"), Str("SceneItemHide"));
  779. obs_hotkey_enable_callback_rerouting(true);
  780. obs_hotkey_set_callback_routing_func(OBSBasic::HotkeyTriggered, this);
  781. }
  782. void OBSBasic::ProcessHotkey(obs_hotkey_id id, bool pressed)
  783. {
  784. obs_hotkey_trigger_routed_callback(id, pressed);
  785. }
  786. void OBSBasic::HotkeyTriggered(void *data, obs_hotkey_id id, bool pressed)
  787. {
  788. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  789. QMetaObject::invokeMethod(&basic, "ProcessHotkey",
  790. Q_ARG(obs_hotkey_id, id), Q_ARG(bool, pressed));
  791. }
  792. void OBSBasic::CreateHotkeys()
  793. {
  794. ProfileScope("OBSBasic::CreateHotkeys");
  795. auto LoadHotkeyData = [&](const char *name) -> OBSData
  796. {
  797. const char *info = config_get_string(basicConfig,
  798. "Hotkeys", name);
  799. if (!info)
  800. return {};
  801. obs_data_t *data = obs_data_create_from_json(info);
  802. if (!data)
  803. return {};
  804. OBSData res = data;
  805. obs_data_release(data);
  806. return res;
  807. };
  808. auto LoadHotkey = [&](obs_hotkey_id id, const char *name)
  809. {
  810. obs_data_array_t *array =
  811. obs_data_get_array(LoadHotkeyData(name), "bindings");
  812. obs_hotkey_load(id, array);
  813. obs_data_array_release(array);
  814. };
  815. auto LoadHotkeyPair = [&](obs_hotkey_pair_id id, const char *name0,
  816. const char *name1)
  817. {
  818. obs_data_array_t *array0 =
  819. obs_data_get_array(LoadHotkeyData(name0), "bindings");
  820. obs_data_array_t *array1 =
  821. obs_data_get_array(LoadHotkeyData(name1), "bindings");
  822. obs_hotkey_pair_load(id, array0, array1);
  823. obs_data_array_release(array0);
  824. obs_data_array_release(array1);
  825. };
  826. #define MAKE_CALLBACK(pred, method) \
  827. [](void *data, obs_hotkey_pair_id, obs_hotkey_t*, bool pressed) \
  828. { \
  829. OBSBasic &basic = *static_cast<OBSBasic*>(data); \
  830. if (pred && pressed) { \
  831. method(); \
  832. return true; \
  833. } \
  834. return false; \
  835. }
  836. streamingHotkeys = obs_hotkey_pair_register_frontend(
  837. "OBSBasic.StartStreaming",
  838. Str("Basic.Hotkeys.StartStreaming"),
  839. "OBSBasic.StopStreaming",
  840. Str("Basic.Hotkeys.StopStreaming"),
  841. MAKE_CALLBACK(!basic.outputHandler->StreamingActive(),
  842. basic.StartStreaming),
  843. MAKE_CALLBACK(basic.outputHandler->StreamingActive(),
  844. basic.StopStreaming),
  845. this, this);
  846. LoadHotkeyPair(streamingHotkeys,
  847. "OBSBasic.StartStreaming", "OBSBasic.StopStreaming");
  848. auto cb = [] (void *data, obs_hotkey_id, obs_hotkey_t*, bool pressed)
  849. {
  850. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  851. if (basic.outputHandler->StreamingActive() && pressed) {
  852. basic.ForceStopStreaming();
  853. }
  854. };
  855. forceStreamingStopHotkey = obs_hotkey_register_frontend(
  856. "OBSBasic.ForceStopStreaming",
  857. Str("Basic.Main.ForceStopStreaming"),
  858. cb, this);
  859. LoadHotkey(forceStreamingStopHotkey,
  860. "OBSBasic.ForceStopStreaming");
  861. recordingHotkeys = obs_hotkey_pair_register_frontend(
  862. "OBSBasic.StartRecording",
  863. Str("Basic.Hotkeys.StartRecording"),
  864. "OBSBasic.StopRecording",
  865. Str("Basic.Hotkeys.StopRecording"),
  866. MAKE_CALLBACK(!basic.outputHandler->RecordingActive(),
  867. basic.StartRecording),
  868. MAKE_CALLBACK(basic.outputHandler->RecordingActive(),
  869. basic.StopRecording),
  870. this, this);
  871. LoadHotkeyPair(recordingHotkeys,
  872. "OBSBasic.StartRecording", "OBSBasic.StopRecording");
  873. #undef MAKE_CALLBACK
  874. }
  875. void OBSBasic::ClearHotkeys()
  876. {
  877. obs_hotkey_pair_unregister(streamingHotkeys);
  878. obs_hotkey_pair_unregister(recordingHotkeys);
  879. obs_hotkey_unregister(forceStreamingStopHotkey);
  880. }
  881. OBSBasic::~OBSBasic()
  882. {
  883. bool previewEnabled = obs_display_enabled(ui->preview->GetDisplay());
  884. /* XXX: any obs data must be released before calling obs_shutdown.
  885. * currently, we can't automate this with C++ RAII because of the
  886. * delicate nature of obs_shutdown needing to be freed before the UI
  887. * can be freed, and we have no control over the destruction order of
  888. * the Qt UI stuff, so we have to manually clear any references to
  889. * libobs. */
  890. delete cpuUsageTimer;
  891. os_cpu_usage_info_destroy(cpuUsageInfo);
  892. obs_hotkey_set_callback_routing_func(nullptr, nullptr);
  893. ClearHotkeys();
  894. service = nullptr;
  895. outputHandler.reset();
  896. if (interaction)
  897. delete interaction;
  898. if (properties)
  899. delete properties;
  900. if (filters)
  901. delete filters;
  902. if (transformWindow)
  903. delete transformWindow;
  904. if (advAudioWindow)
  905. delete advAudioWindow;
  906. obs_display_remove_draw_callback(ui->preview->GetDisplay(),
  907. OBSBasic::RenderMain, this);
  908. obs_enter_graphics();
  909. gs_vertexbuffer_destroy(box);
  910. gs_vertexbuffer_destroy(circle);
  911. obs_leave_graphics();
  912. /* When shutting down, sometimes source references can get in to the
  913. * event queue, and if we don't forcibly process those events they
  914. * won't get processed until after obs_shutdown has been called. I
  915. * really wish there were a more elegant way to deal with this via C++,
  916. * but Qt doesn't use C++ in a normal way, so you can't really rely on
  917. * normal C++ behavior for your data to be freed in the order that you
  918. * expect or want it to. */
  919. QApplication::sendPostedEvents(this);
  920. config_set_int(App()->GlobalConfig(), "General", "LastVersion",
  921. LIBOBS_API_VER);
  922. QRect lastGeom = normalGeometry();
  923. config_set_int(App()->GlobalConfig(), "BasicWindow", "cx",
  924. lastGeom.width());
  925. config_set_int(App()->GlobalConfig(), "BasicWindow", "cy",
  926. lastGeom.height());
  927. config_set_int(App()->GlobalConfig(), "BasicWindow", "posx",
  928. lastGeom.x());
  929. config_set_int(App()->GlobalConfig(), "BasicWindow", "posy",
  930. lastGeom.y());
  931. config_set_bool(App()->GlobalConfig(), "BasicWindow", "PreviewEnabled",
  932. previewEnabled);
  933. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  934. #ifdef _WIN32
  935. uint32_t winVer = GetWindowsVersion();
  936. if (winVer > 0 && winVer < 0x602) {
  937. bool disableAero = config_get_bool(basicConfig, "Video",
  938. "DisableAero");
  939. if (disableAero) {
  940. SetAeroEnabled(true);
  941. }
  942. }
  943. #endif
  944. }
  945. void OBSBasic::SaveProjectNow()
  946. {
  947. if (disableSaving)
  948. return;
  949. projectChanged = true;
  950. SaveProjectDeferred();
  951. }
  952. void OBSBasic::SaveProject()
  953. {
  954. if (disableSaving)
  955. return;
  956. projectChanged = true;
  957. QMetaObject::invokeMethod(this, "SaveProjectDeferred",
  958. Qt::QueuedConnection);
  959. }
  960. void OBSBasic::SaveProjectDeferred()
  961. {
  962. if (disableSaving)
  963. return;
  964. if (!projectChanged)
  965. return;
  966. projectChanged = false;
  967. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  968. "Basic", "SceneCollectionFile");
  969. char savePath[512];
  970. char fileName[512];
  971. int ret;
  972. if (!sceneCollection)
  973. return;
  974. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  975. sceneCollection);
  976. if (ret <= 0)
  977. return;
  978. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  979. if (ret <= 0)
  980. return;
  981. Save(savePath);
  982. }
  983. OBSScene OBSBasic::GetCurrentScene()
  984. {
  985. QListWidgetItem *item = ui->scenes->currentItem();
  986. return item ? GetOBSRef<OBSScene>(item) : nullptr;
  987. }
  988. OBSSceneItem OBSBasic::GetSceneItem(QListWidgetItem *item)
  989. {
  990. return item ? GetOBSRef<OBSSceneItem>(item) : nullptr;
  991. }
  992. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  993. {
  994. return GetSceneItem(ui->sources->currentItem());
  995. }
  996. void OBSBasic::UpdateSources(OBSScene scene)
  997. {
  998. ClearListItems(ui->sources);
  999. obs_scene_enum_items(scene,
  1000. [] (obs_scene_t *scene, obs_sceneitem_t *item, void *p)
  1001. {
  1002. OBSBasic *window = static_cast<OBSBasic*>(p);
  1003. window->InsertSceneItem(item);
  1004. UNUSED_PARAMETER(scene);
  1005. return true;
  1006. }, this);
  1007. }
  1008. void OBSBasic::InsertSceneItem(obs_sceneitem_t *item)
  1009. {
  1010. obs_source_t *source = obs_sceneitem_get_source(item);
  1011. QListWidgetItem *listItem = new QListWidgetItem();
  1012. SetOBSRef(listItem, OBSSceneItem(item));
  1013. ui->sources->insertItem(0, listItem);
  1014. ui->sources->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
  1015. SetupVisibilityItem(ui->sources, listItem, item);
  1016. /* if the source was just created, open properties dialog */
  1017. if (sourceSceneRefs[source] == 0 && loaded)
  1018. CreatePropertiesWindow(source);
  1019. }
  1020. void OBSBasic::CreateInteractionWindow(obs_source_t *source)
  1021. {
  1022. if (interaction)
  1023. interaction->close();
  1024. interaction = new OBSBasicInteraction(this, source);
  1025. interaction->Init();
  1026. interaction->setAttribute(Qt::WA_DeleteOnClose, true);
  1027. }
  1028. void OBSBasic::CreatePropertiesWindow(obs_source_t *source)
  1029. {
  1030. if (properties)
  1031. properties->close();
  1032. properties = new OBSBasicProperties(this, source);
  1033. properties->Init();
  1034. properties->setAttribute(Qt::WA_DeleteOnClose, true);
  1035. }
  1036. void OBSBasic::CreateFiltersWindow(obs_source_t *source)
  1037. {
  1038. if (filters)
  1039. filters->close();
  1040. filters = new OBSBasicFilters(this, source);
  1041. filters->Init();
  1042. filters->setAttribute(Qt::WA_DeleteOnClose, true);
  1043. }
  1044. /* Qt callbacks for invokeMethod */
  1045. void OBSBasic::AddScene(OBSSource source)
  1046. {
  1047. const char *name = obs_source_get_name(source);
  1048. obs_scene_t *scene = obs_scene_from_source(source);
  1049. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  1050. SetOBSRef(item, OBSScene(scene));
  1051. ui->scenes->addItem(item);
  1052. obs_hotkey_register_source(source, "OBSBasic.SelectScene",
  1053. Str("Basic.Hotkeys.SelectScene"),
  1054. [](void *data,
  1055. obs_hotkey_id, obs_hotkey_t*, bool pressed)
  1056. {
  1057. auto potential_source = static_cast<obs_source_t*>(data);
  1058. auto source = obs_source_get_ref(potential_source);
  1059. if (source && pressed)
  1060. obs_set_output_source(0, source);
  1061. obs_source_release(source);
  1062. }, static_cast<obs_source_t*>(source));
  1063. signal_handler_t *handler = obs_source_get_signal_handler(source);
  1064. std::vector<std::shared_ptr<OBSSignal>> handlers{
  1065. std::make_shared<OBSSignal>(handler, "item_add",
  1066. OBSBasic::SceneItemAdded, this),
  1067. std::make_shared<OBSSignal>(handler, "item_remove",
  1068. OBSBasic::SceneItemRemoved, this),
  1069. std::make_shared<OBSSignal>(handler, "item_select",
  1070. OBSBasic::SceneItemSelected, this),
  1071. std::make_shared<OBSSignal>(handler, "item_deselect",
  1072. OBSBasic::SceneItemDeselected, this),
  1073. std::make_shared<OBSSignal>(handler, "reorder",
  1074. OBSBasic::SceneReordered, this),
  1075. };
  1076. item->setData(static_cast<int>(QtDataRole::OBSSignals),
  1077. QVariant::fromValue(handlers));
  1078. /* if the scene already has items (a duplicated scene) add them */
  1079. auto addSceneItem = [this] (obs_sceneitem_t *item)
  1080. {
  1081. AddSceneItem(item);
  1082. };
  1083. using addSceneItem_t = decltype(addSceneItem);
  1084. obs_scene_enum_items(scene,
  1085. [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  1086. {
  1087. addSceneItem_t *func;
  1088. func = reinterpret_cast<addSceneItem_t*>(param);
  1089. (*func)(item);
  1090. return true;
  1091. }, &addSceneItem);
  1092. SaveProject();
  1093. }
  1094. void OBSBasic::RemoveScene(OBSSource source)
  1095. {
  1096. const char *name = obs_source_get_name(source);
  1097. QListWidgetItem *sel = ui->scenes->currentItem();
  1098. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  1099. Qt::MatchExactly);
  1100. if (sel != nullptr) {
  1101. if (items.contains(sel))
  1102. ClearListItems(ui->sources);
  1103. delete sel;
  1104. }
  1105. auto DeleteSceneRefs = [&](obs_sceneitem_t *si)
  1106. {
  1107. obs_source_t *source = obs_sceneitem_get_source(si);
  1108. sourceSceneRefs[source] -= 1;
  1109. if (!sourceSceneRefs[source]) {
  1110. obs_source_remove(source);
  1111. sourceSceneRefs.erase(source);
  1112. }
  1113. };
  1114. using DeleteSceneRefs_t = decltype(DeleteSceneRefs);
  1115. obs_scene_enum_items(obs_scene_from_source(source),
  1116. [](obs_scene_t *, obs_sceneitem_t *si, void *data)
  1117. {
  1118. (*static_cast<DeleteSceneRefs_t*>(data))(si);
  1119. return true;
  1120. }, static_cast<void*>(&DeleteSceneRefs));
  1121. SaveProject();
  1122. }
  1123. void OBSBasic::AddSceneItem(OBSSceneItem item)
  1124. {
  1125. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1126. obs_source_t *source = obs_sceneitem_get_source(item);
  1127. if (GetCurrentScene() == scene)
  1128. InsertSceneItem(item);
  1129. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  1130. SaveProject();
  1131. }
  1132. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  1133. {
  1134. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1135. if (GetCurrentScene() == scene) {
  1136. for (int i = 0; i < ui->sources->count(); i++) {
  1137. QListWidgetItem *listItem = ui->sources->item(i);
  1138. if (GetOBSRef<OBSSceneItem>(listItem) == item) {
  1139. DeleteListItem(ui->sources, listItem);
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. obs_source_t *source = obs_sceneitem_get_source(item);
  1145. int scenes = sourceSceneRefs[source] - 1;
  1146. sourceSceneRefs[source] = scenes;
  1147. if (scenes == 0) {
  1148. obs_source_remove(source);
  1149. sourceSceneRefs.erase(source);
  1150. }
  1151. SaveProject();
  1152. }
  1153. void OBSBasic::UpdateSceneSelection(OBSSource source)
  1154. {
  1155. if (source) {
  1156. obs_scene_t *scene = obs_scene_from_source(source);
  1157. const char *name = obs_source_get_name(source);
  1158. if (!scene)
  1159. return;
  1160. QList<QListWidgetItem*> items =
  1161. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  1162. if (items.count()) {
  1163. sceneChanging = true;
  1164. ui->scenes->setCurrentItem(items.first());
  1165. sceneChanging = false;
  1166. UpdateSources(scene);
  1167. }
  1168. }
  1169. }
  1170. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  1171. const QString &prevName)
  1172. {
  1173. QList<QListWidgetItem*> items =
  1174. listWidget->findItems(prevName, Qt::MatchExactly);
  1175. for (int i = 0; i < items.count(); i++)
  1176. items[i]->setText(newName);
  1177. }
  1178. void OBSBasic::RenameSources(QString newName, QString prevName)
  1179. {
  1180. RenameListValues(ui->scenes, newName, prevName);
  1181. for (size_t i = 0; i < volumes.size(); i++) {
  1182. if (volumes[i]->GetName().compare(prevName) == 0)
  1183. volumes[i]->SetName(newName);
  1184. }
  1185. SaveProject();
  1186. }
  1187. void OBSBasic::SelectSceneItem(OBSScene scene, OBSSceneItem item, bool select)
  1188. {
  1189. if (scene != GetCurrentScene() || ignoreSelectionUpdate)
  1190. return;
  1191. for (int i = 0; i < ui->sources->count(); i++) {
  1192. QListWidgetItem *witem = ui->sources->item(i);
  1193. QVariant data =
  1194. witem->data(static_cast<int>(QtDataRole::OBSRef));
  1195. if (!data.canConvert<OBSSceneItem>())
  1196. continue;
  1197. if (item != data.value<OBSSceneItem>())
  1198. continue;
  1199. if (select)
  1200. ui->sources->setCurrentItem(witem,
  1201. QItemSelectionModel::ClearAndSelect);
  1202. break;
  1203. }
  1204. }
  1205. void OBSBasic::GetAudioSourceFilters()
  1206. {
  1207. QAction *action = reinterpret_cast<QAction*>(sender());
  1208. VolControl *vol = action->property("volControl").value<VolControl*>();
  1209. obs_source_t *source = vol->GetSource();
  1210. CreateFiltersWindow(source);
  1211. }
  1212. void OBSBasic::GetAudioSourceProperties()
  1213. {
  1214. QAction *action = reinterpret_cast<QAction*>(sender());
  1215. VolControl *vol = action->property("volControl").value<VolControl*>();
  1216. obs_source_t *source = vol->GetSource();
  1217. CreatePropertiesWindow(source);
  1218. }
  1219. void OBSBasic::VolControlContextMenu()
  1220. {
  1221. VolControl *vol = reinterpret_cast<VolControl*>(sender());
  1222. QAction filtersAction(QTStr("Filters"), this);
  1223. QAction propertiesAction(QTStr("Properties"), this);
  1224. connect(&filtersAction, &QAction::triggered,
  1225. this, &OBSBasic::GetAudioSourceFilters,
  1226. Qt::DirectConnection);
  1227. connect(&propertiesAction, &QAction::triggered,
  1228. this, &OBSBasic::GetAudioSourceProperties,
  1229. Qt::DirectConnection);
  1230. filtersAction.setProperty("volControl",
  1231. QVariant::fromValue<VolControl*>(vol));
  1232. propertiesAction.setProperty("volControl",
  1233. QVariant::fromValue<VolControl*>(vol));
  1234. QMenu popup(this);
  1235. popup.addAction(&filtersAction);
  1236. popup.addAction(&propertiesAction);
  1237. popup.exec(QCursor::pos());
  1238. }
  1239. void OBSBasic::ActivateAudioSource(OBSSource source)
  1240. {
  1241. VolControl *vol = new VolControl(source, true);
  1242. connect(vol, &VolControl::ConfigClicked,
  1243. this, &OBSBasic::VolControlContextMenu);
  1244. volumes.push_back(vol);
  1245. ui->volumeWidgets->layout()->addWidget(vol);
  1246. }
  1247. void OBSBasic::DeactivateAudioSource(OBSSource source)
  1248. {
  1249. for (size_t i = 0; i < volumes.size(); i++) {
  1250. if (volumes[i]->GetSource() == source) {
  1251. delete volumes[i];
  1252. volumes.erase(volumes.begin() + i);
  1253. break;
  1254. }
  1255. }
  1256. }
  1257. bool OBSBasic::QueryRemoveSource(obs_source_t *source)
  1258. {
  1259. const char *name = obs_source_get_name(source);
  1260. QString text = QTStr("ConfirmRemove.Text");
  1261. text.replace("$1", QT_UTF8(name));
  1262. QMessageBox remove_source(this);
  1263. remove_source.setText(text);
  1264. QAbstractButton *Yes = remove_source.addButton(QTStr("Yes"),
  1265. QMessageBox::YesRole);
  1266. remove_source.addButton(QTStr("No"), QMessageBox::NoRole);
  1267. remove_source.setIcon(QMessageBox::Question);
  1268. remove_source.setWindowTitle(QTStr("ConfirmRemove.Title"));
  1269. remove_source.exec();
  1270. return Yes == remove_source.clickedButton();
  1271. }
  1272. #define UPDATE_CHECK_INTERVAL (60*60*24*4) /* 4 days */
  1273. #ifdef UPDATE_SPARKLE
  1274. void init_sparkle_updater(bool update_to_undeployed);
  1275. void trigger_sparkle_update();
  1276. #endif
  1277. void OBSBasic::TimedCheckForUpdates()
  1278. {
  1279. #ifdef UPDATE_SPARKLE
  1280. init_sparkle_updater(config_get_bool(App()->GlobalConfig(), "General",
  1281. "UpdateToUndeployed"));
  1282. #else
  1283. long long lastUpdate = config_get_int(App()->GlobalConfig(), "General",
  1284. "LastUpdateCheck");
  1285. uint32_t lastVersion = config_get_int(App()->GlobalConfig(), "General",
  1286. "LastVersion");
  1287. if (lastVersion < LIBOBS_API_VER) {
  1288. lastUpdate = 0;
  1289. config_set_int(App()->GlobalConfig(), "General",
  1290. "LastUpdateCheck", 0);
  1291. }
  1292. long long t = (long long)time(nullptr);
  1293. long long secs = t - lastUpdate;
  1294. if (secs > UPDATE_CHECK_INTERVAL)
  1295. CheckForUpdates();
  1296. #endif
  1297. }
  1298. void OBSBasic::CheckForUpdates()
  1299. {
  1300. #ifdef UPDATE_SPARKLE
  1301. trigger_sparkle_update();
  1302. #else
  1303. ui->actionCheckForUpdates->setEnabled(false);
  1304. if (updateCheckThread) {
  1305. updateCheckThread->wait();
  1306. delete updateCheckThread;
  1307. }
  1308. RemoteTextThread *thread = new RemoteTextThread(
  1309. "https://obsproject.com/obs2_update/basic.json");
  1310. updateCheckThread = thread;
  1311. connect(thread, &RemoteTextThread::Result,
  1312. this, &OBSBasic::updateFileFinished);
  1313. updateCheckThread->start();
  1314. #endif
  1315. }
  1316. #ifdef __APPLE__
  1317. #define VERSION_ENTRY "mac"
  1318. #elif _WIN32
  1319. #define VERSION_ENTRY "windows"
  1320. #else
  1321. #define VERSION_ENTRY "other"
  1322. #endif
  1323. void OBSBasic::updateFileFinished(const QString &text, const QString &error)
  1324. {
  1325. ui->actionCheckForUpdates->setEnabled(true);
  1326. if (text.isEmpty()) {
  1327. blog(LOG_WARNING, "Update check failed: %s", QT_TO_UTF8(error));
  1328. return;
  1329. }
  1330. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  1331. obs_data_t *versionData = obs_data_get_obj(returnData, VERSION_ENTRY);
  1332. const char *description = obs_data_get_string(returnData,
  1333. "description");
  1334. const char *download = obs_data_get_string(versionData, "download");
  1335. if (returnData && versionData && description && download) {
  1336. long major = obs_data_get_int(versionData, "major");
  1337. long minor = obs_data_get_int(versionData, "minor");
  1338. long patch = obs_data_get_int(versionData, "patch");
  1339. long version = MAKE_SEMANTIC_VERSION(major, minor, patch);
  1340. blog(LOG_INFO, "Update check: last known remote version "
  1341. "is %ld.%ld.%ld",
  1342. major, minor, patch);
  1343. if (version > LIBOBS_API_VER) {
  1344. QString str = QTStr("UpdateAvailable.Text");
  1345. QMessageBox messageBox(this);
  1346. str = str.arg(QString::number(major),
  1347. QString::number(minor),
  1348. QString::number(patch),
  1349. download);
  1350. messageBox.setWindowTitle(QTStr("UpdateAvailable"));
  1351. messageBox.setTextFormat(Qt::RichText);
  1352. messageBox.setText(str);
  1353. messageBox.setInformativeText(QT_UTF8(description));
  1354. messageBox.exec();
  1355. long long t = (long long)time(nullptr);
  1356. config_set_int(App()->GlobalConfig(), "General",
  1357. "LastUpdateCheck", t);
  1358. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  1359. }
  1360. } else {
  1361. blog(LOG_WARNING, "Bad JSON file received from server");
  1362. }
  1363. obs_data_release(versionData);
  1364. obs_data_release(returnData);
  1365. }
  1366. void OBSBasic::DuplicateSelectedScene()
  1367. {
  1368. OBSScene curScene = GetCurrentScene();
  1369. if (!curScene)
  1370. return;
  1371. OBSSource curSceneSource = obs_scene_get_source(curScene);
  1372. QString format{obs_source_get_name(curSceneSource)};
  1373. format += " %1";
  1374. int i = 2;
  1375. QString placeHolderText = format.arg(i);
  1376. obs_source_t *source = nullptr;
  1377. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1378. obs_source_release(source);
  1379. placeHolderText = format.arg(++i);
  1380. }
  1381. for (;;) {
  1382. string name;
  1383. bool accepted = NameDialog::AskForName(this,
  1384. QTStr("Basic.Main.AddSceneDlg.Title"),
  1385. QTStr("Basic.Main.AddSceneDlg.Text"),
  1386. name,
  1387. placeHolderText);
  1388. if (!accepted)
  1389. return;
  1390. if (name.empty()) {
  1391. QMessageBox::information(this,
  1392. QTStr("NoNameEntered.Title"),
  1393. QTStr("NoNameEntered.Text"));
  1394. continue;
  1395. }
  1396. obs_source_t *source = obs_get_source_by_name(name.c_str());
  1397. if (source) {
  1398. QMessageBox::information(this,
  1399. QTStr("NameExists.Title"),
  1400. QTStr("NameExists.Text"));
  1401. obs_source_release(source);
  1402. continue;
  1403. }
  1404. obs_scene_t *scene = obs_scene_duplicate(curScene,
  1405. name.c_str());
  1406. source = obs_scene_get_source(scene);
  1407. obs_add_source(source);
  1408. obs_scene_release(scene);
  1409. obs_set_output_source(0, source);
  1410. return;
  1411. }
  1412. }
  1413. void OBSBasic::RemoveSelectedScene()
  1414. {
  1415. OBSScene scene = GetCurrentScene();
  1416. if (scene) {
  1417. obs_source_t *source = obs_scene_get_source(scene);
  1418. if (QueryRemoveSource(source))
  1419. obs_source_remove(source);
  1420. }
  1421. }
  1422. void OBSBasic::RemoveSelectedSceneItem()
  1423. {
  1424. OBSSceneItem item = GetCurrentSceneItem();
  1425. if (item) {
  1426. obs_source_t *source = obs_sceneitem_get_source(item);
  1427. if (QueryRemoveSource(source))
  1428. obs_sceneitem_remove(item);
  1429. }
  1430. }
  1431. struct ReorderInfo {
  1432. int idx = 0;
  1433. OBSBasic *window;
  1434. inline ReorderInfo(OBSBasic *window_) : window(window_) {}
  1435. };
  1436. void OBSBasic::ReorderSceneItem(obs_sceneitem_t *item, size_t idx)
  1437. {
  1438. int count = ui->sources->count();
  1439. int idx_inv = count - (int)idx - 1;
  1440. for (int i = 0; i < count; i++) {
  1441. QListWidgetItem *listItem = ui->sources->item(i);
  1442. OBSSceneItem sceneItem = GetOBSRef<OBSSceneItem>(listItem);
  1443. if (sceneItem == item) {
  1444. if ((int)idx_inv != i) {
  1445. bool sel = (ui->sources->currentRow() == i);
  1446. listItem = TakeListItem(ui->sources, i);
  1447. if (listItem) {
  1448. ui->sources->insertItem(idx_inv,
  1449. listItem);
  1450. SetupVisibilityItem(ui->sources,
  1451. listItem, item);
  1452. if (sel)
  1453. ui->sources->setCurrentRow(
  1454. idx_inv);
  1455. }
  1456. }
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. void OBSBasic::ReorderSources(OBSScene scene)
  1462. {
  1463. ReorderInfo info(this);
  1464. if (scene != GetCurrentScene() || ui->sources->IgnoreReorder())
  1465. return;
  1466. obs_scene_enum_items(scene,
  1467. [] (obs_scene_t*, obs_sceneitem_t *item, void *p)
  1468. {
  1469. ReorderInfo *info =
  1470. reinterpret_cast<ReorderInfo*>(p);
  1471. info->window->ReorderSceneItem(item,
  1472. info->idx++);
  1473. return true;
  1474. }, &info);
  1475. SaveProject();
  1476. }
  1477. /* OBS Callbacks */
  1478. void OBSBasic::SceneReordered(void *data, calldata_t *params)
  1479. {
  1480. OBSBasic *window = static_cast<OBSBasic*>(data);
  1481. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1482. QMetaObject::invokeMethod(window, "ReorderSources",
  1483. Q_ARG(OBSScene, OBSScene(scene)));
  1484. }
  1485. void OBSBasic::SceneItemAdded(void *data, calldata_t *params)
  1486. {
  1487. OBSBasic *window = static_cast<OBSBasic*>(data);
  1488. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1489. QMetaObject::invokeMethod(window, "AddSceneItem",
  1490. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1491. }
  1492. void OBSBasic::SceneItemRemoved(void *data, calldata_t *params)
  1493. {
  1494. OBSBasic *window = static_cast<OBSBasic*>(data);
  1495. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1496. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  1497. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1498. }
  1499. void OBSBasic::SceneItemSelected(void *data, calldata_t *params)
  1500. {
  1501. OBSBasic *window = static_cast<OBSBasic*>(data);
  1502. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1503. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1504. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1505. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1506. Q_ARG(bool, true));
  1507. }
  1508. void OBSBasic::SceneItemDeselected(void *data, calldata_t *params)
  1509. {
  1510. OBSBasic *window = static_cast<OBSBasic*>(data);
  1511. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1512. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1513. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1514. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1515. Q_ARG(bool, false));
  1516. }
  1517. void OBSBasic::SourceAdded(void *data, calldata_t *params)
  1518. {
  1519. OBSBasic *window = static_cast<OBSBasic*>(data);
  1520. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1521. if (obs_scene_from_source(source) != NULL)
  1522. QMetaObject::invokeMethod(window,
  1523. "AddScene",
  1524. Q_ARG(OBSSource, OBSSource(source)));
  1525. }
  1526. void OBSBasic::SourceRemoved(void *data, calldata_t *params)
  1527. {
  1528. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1529. if (obs_scene_from_source(source) != NULL)
  1530. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1531. "RemoveScene",
  1532. Q_ARG(OBSSource, OBSSource(source)));
  1533. }
  1534. void OBSBasic::SourceActivated(void *data, calldata_t *params)
  1535. {
  1536. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1537. uint32_t flags = obs_source_get_output_flags(source);
  1538. if (flags & OBS_SOURCE_AUDIO)
  1539. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1540. "ActivateAudioSource",
  1541. Q_ARG(OBSSource, OBSSource(source)));
  1542. }
  1543. void OBSBasic::SourceDeactivated(void *data, calldata_t *params)
  1544. {
  1545. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1546. uint32_t flags = obs_source_get_output_flags(source);
  1547. if (flags & OBS_SOURCE_AUDIO)
  1548. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1549. "DeactivateAudioSource",
  1550. Q_ARG(OBSSource, OBSSource(source)));
  1551. }
  1552. void OBSBasic::SourceRenamed(void *data, calldata_t *params)
  1553. {
  1554. const char *newName = calldata_string(params, "new_name");
  1555. const char *prevName = calldata_string(params, "prev_name");
  1556. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1557. "RenameSources",
  1558. Q_ARG(QString, QT_UTF8(newName)),
  1559. Q_ARG(QString, QT_UTF8(prevName)));
  1560. }
  1561. void OBSBasic::ChannelChanged(void *data, calldata_t *params)
  1562. {
  1563. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1564. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  1565. if (channel == 0)
  1566. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1567. "UpdateSceneSelection",
  1568. Q_ARG(OBSSource, OBSSource(source)));
  1569. }
  1570. void OBSBasic::DrawBackdrop(float cx, float cy)
  1571. {
  1572. if (!box)
  1573. return;
  1574. gs_effect_t *solid = obs_get_solid_effect();
  1575. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  1576. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1577. vec4 colorVal;
  1578. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  1579. gs_effect_set_vec4(color, &colorVal);
  1580. gs_technique_begin(tech);
  1581. gs_technique_begin_pass(tech, 0);
  1582. gs_matrix_push();
  1583. gs_matrix_identity();
  1584. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  1585. gs_load_vertexbuffer(box);
  1586. gs_draw(GS_TRISTRIP, 0, 0);
  1587. gs_matrix_pop();
  1588. gs_technique_end_pass(tech);
  1589. gs_technique_end(tech);
  1590. gs_load_vertexbuffer(nullptr);
  1591. }
  1592. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  1593. {
  1594. OBSBasic *window = static_cast<OBSBasic*>(data);
  1595. obs_video_info ovi;
  1596. obs_get_video_info(&ovi);
  1597. window->previewCX = int(window->previewScale * float(ovi.base_width));
  1598. window->previewCY = int(window->previewScale * float(ovi.base_height));
  1599. gs_viewport_push();
  1600. gs_projection_push();
  1601. /* --------------------------------------- */
  1602. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  1603. -100.0f, 100.0f);
  1604. gs_set_viewport(window->previewX, window->previewY,
  1605. window->previewCX, window->previewCY);
  1606. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  1607. obs_render_main_view();
  1608. gs_load_vertexbuffer(nullptr);
  1609. /* --------------------------------------- */
  1610. QSize previewSize = GetPixelSize(window->ui->preview);
  1611. float right = float(previewSize.width()) - window->previewX;
  1612. float bottom = float(previewSize.height()) - window->previewY;
  1613. gs_ortho(-window->previewX, right,
  1614. -window->previewY, bottom,
  1615. -100.0f, 100.0f);
  1616. gs_reset_viewport();
  1617. window->ui->preview->DrawSceneEditing();
  1618. /* --------------------------------------- */
  1619. gs_projection_pop();
  1620. gs_viewport_pop();
  1621. UNUSED_PARAMETER(cx);
  1622. UNUSED_PARAMETER(cy);
  1623. }
  1624. /* Main class functions */
  1625. obs_service_t *OBSBasic::GetService()
  1626. {
  1627. if (!service) {
  1628. service = obs_service_create("rtmp_common", NULL, NULL,
  1629. nullptr);
  1630. obs_service_release(service);
  1631. }
  1632. return service;
  1633. }
  1634. void OBSBasic::SetService(obs_service_t *newService)
  1635. {
  1636. if (newService)
  1637. service = newService;
  1638. }
  1639. bool OBSBasic::StreamingActive()
  1640. {
  1641. if (!outputHandler)
  1642. return false;
  1643. return outputHandler->StreamingActive();
  1644. }
  1645. #ifdef _WIN32
  1646. #define IS_WIN32 1
  1647. #else
  1648. #define IS_WIN32 0
  1649. #endif
  1650. static inline int AttemptToResetVideo(struct obs_video_info *ovi)
  1651. {
  1652. return obs_reset_video(ovi);
  1653. }
  1654. static inline enum obs_scale_type GetScaleType(ConfigFile &basicConfig)
  1655. {
  1656. const char *scaleTypeStr = config_get_string(basicConfig,
  1657. "Video", "ScaleType");
  1658. if (astrcmpi(scaleTypeStr, "bilinear") == 0)
  1659. return OBS_SCALE_BILINEAR;
  1660. else if (astrcmpi(scaleTypeStr, "lanczos") == 0)
  1661. return OBS_SCALE_LANCZOS;
  1662. else
  1663. return OBS_SCALE_BICUBIC;
  1664. }
  1665. static inline enum video_format GetVideoFormatFromName(const char *name)
  1666. {
  1667. if (astrcmpi(name, "I420") == 0)
  1668. return VIDEO_FORMAT_I420;
  1669. else if (astrcmpi(name, "NV12") == 0)
  1670. return VIDEO_FORMAT_NV12;
  1671. else if (astrcmpi(name, "I444") == 0)
  1672. return VIDEO_FORMAT_I444;
  1673. #if 0 //currently unsupported
  1674. else if (astrcmpi(name, "YVYU") == 0)
  1675. return VIDEO_FORMAT_YVYU;
  1676. else if (astrcmpi(name, "YUY2") == 0)
  1677. return VIDEO_FORMAT_YUY2;
  1678. else if (astrcmpi(name, "UYVY") == 0)
  1679. return VIDEO_FORMAT_UYVY;
  1680. #endif
  1681. else
  1682. return VIDEO_FORMAT_RGBA;
  1683. }
  1684. int OBSBasic::ResetVideo()
  1685. {
  1686. ProfileScope("OBSBasic::ResetVideo");
  1687. struct obs_video_info ovi;
  1688. int ret;
  1689. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  1690. const char *colorFormat = config_get_string(basicConfig, "Video",
  1691. "ColorFormat");
  1692. const char *colorSpace = config_get_string(basicConfig, "Video",
  1693. "ColorSpace");
  1694. const char *colorRange = config_get_string(basicConfig, "Video",
  1695. "ColorRange");
  1696. ovi.graphics_module = App()->GetRenderModule();
  1697. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  1698. "Video", "BaseCX");
  1699. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  1700. "Video", "BaseCY");
  1701. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  1702. "Video", "OutputCX");
  1703. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  1704. "Video", "OutputCY");
  1705. ovi.output_format = GetVideoFormatFromName(colorFormat);
  1706. ovi.colorspace = astrcmpi(colorSpace, "601") == 0 ?
  1707. VIDEO_CS_601 : VIDEO_CS_709;
  1708. ovi.range = astrcmpi(colorRange, "Full") == 0 ?
  1709. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  1710. ovi.adapter = 0;
  1711. ovi.gpu_conversion = true;
  1712. ovi.scale_type = GetScaleType(basicConfig);
  1713. ret = AttemptToResetVideo(&ovi);
  1714. if (IS_WIN32 && ret != OBS_VIDEO_SUCCESS) {
  1715. /* Try OpenGL if DirectX fails on windows */
  1716. if (astrcmpi(ovi.graphics_module, DL_OPENGL) != 0) {
  1717. blog(LOG_WARNING, "Failed to initialize obs video (%d) "
  1718. "with graphics_module='%s', retrying "
  1719. "with graphics_module='%s'",
  1720. ret, ovi.graphics_module,
  1721. DL_OPENGL);
  1722. ovi.graphics_module = DL_OPENGL;
  1723. ret = AttemptToResetVideo(&ovi);
  1724. }
  1725. } else if (ret == OBS_VIDEO_SUCCESS) {
  1726. ResizePreview(ovi.base_width, ovi.base_height);
  1727. }
  1728. return ret;
  1729. }
  1730. bool OBSBasic::ResetAudio()
  1731. {
  1732. ProfileScope("OBSBasic::ResetAudio");
  1733. struct obs_audio_info ai;
  1734. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  1735. "SampleRate");
  1736. const char *channelSetupStr = config_get_string(basicConfig,
  1737. "Audio", "ChannelSetup");
  1738. if (strcmp(channelSetupStr, "Mono") == 0)
  1739. ai.speakers = SPEAKERS_MONO;
  1740. else
  1741. ai.speakers = SPEAKERS_STEREO;
  1742. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  1743. return obs_reset_audio(&ai);
  1744. }
  1745. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId,
  1746. const char *deviceDesc, int channel)
  1747. {
  1748. obs_source_t *source;
  1749. obs_data_t *settings;
  1750. bool same = false;
  1751. source = obs_get_output_source(channel);
  1752. if (source) {
  1753. settings = obs_source_get_settings(source);
  1754. const char *curId = obs_data_get_string(settings, "device_id");
  1755. same = (strcmp(curId, deviceId) == 0);
  1756. obs_data_release(settings);
  1757. obs_source_release(source);
  1758. }
  1759. if (!same)
  1760. obs_set_output_source(channel, nullptr);
  1761. if (!same && strcmp(deviceId, "disabled") != 0) {
  1762. obs_data_t *settings = obs_data_create();
  1763. obs_data_set_string(settings, "device_id", deviceId);
  1764. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  1765. sourceId, deviceDesc, settings, nullptr);
  1766. obs_data_release(settings);
  1767. obs_set_output_source(channel, source);
  1768. obs_source_release(source);
  1769. }
  1770. }
  1771. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  1772. {
  1773. QSize targetSize;
  1774. /* resize preview panel to fix to the top section of the window */
  1775. targetSize = GetPixelSize(ui->preview);
  1776. GetScaleAndCenterPos(int(cx), int(cy),
  1777. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  1778. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  1779. previewX, previewY, previewScale);
  1780. previewX += float(PREVIEW_EDGE_SIZE);
  1781. previewY += float(PREVIEW_EDGE_SIZE);
  1782. }
  1783. void OBSBasic::CloseDialogs()
  1784. {
  1785. QList<QDialog*> childDialogs = this->findChildren<QDialog *>();
  1786. if (!childDialogs.isEmpty()) {
  1787. for (int i = 0; i < childDialogs.size(); ++i) {
  1788. childDialogs.at(i)->close();
  1789. }
  1790. }
  1791. for (QPointer<QWidget> &projector : projectors) {
  1792. delete projector;
  1793. projector.clear();
  1794. }
  1795. }
  1796. void OBSBasic::ClearSceneData()
  1797. {
  1798. disableSaving++;
  1799. CloseDialogs();
  1800. ClearVolumeControls();
  1801. ClearListItems(ui->scenes);
  1802. ClearListItems(ui->sources);
  1803. obs_set_output_source(0, nullptr);
  1804. obs_set_output_source(1, nullptr);
  1805. obs_set_output_source(2, nullptr);
  1806. obs_set_output_source(3, nullptr);
  1807. obs_set_output_source(4, nullptr);
  1808. obs_set_output_source(5, nullptr);
  1809. auto cb = [](void *unused, obs_source_t *source)
  1810. {
  1811. obs_source_remove(source);
  1812. UNUSED_PARAMETER(unused);
  1813. return true;
  1814. };
  1815. obs_enum_sources(cb, nullptr);
  1816. sourceSceneRefs.clear();
  1817. disableSaving--;
  1818. blog(LOG_INFO, "All scene data cleared");
  1819. blog(LOG_INFO, "------------------------------------------------");
  1820. }
  1821. void OBSBasic::closeEvent(QCloseEvent *event)
  1822. {
  1823. if (outputHandler && outputHandler->Active()) {
  1824. QMessageBox::StandardButton button = QMessageBox::question(
  1825. this, QTStr("ConfirmExit.Title"),
  1826. QTStr("ConfirmExit.Text"));
  1827. if (button == QMessageBox::No) {
  1828. event->ignore();
  1829. return;
  1830. }
  1831. }
  1832. QWidget::closeEvent(event);
  1833. if (!event->isAccepted())
  1834. return;
  1835. if (updateCheckThread)
  1836. updateCheckThread->wait();
  1837. if (logUploadThread)
  1838. logUploadThread->wait();
  1839. signalHandlers.clear();
  1840. SaveProjectNow();
  1841. disableSaving++;
  1842. /* Clear all scene data (dialogs, widgets, widget sub-items, scenes,
  1843. * sources, etc) so that all references are released before shutdown */
  1844. ClearSceneData();
  1845. }
  1846. void OBSBasic::changeEvent(QEvent *event)
  1847. {
  1848. /* TODO */
  1849. UNUSED_PARAMETER(event);
  1850. }
  1851. void OBSBasic::resizeEvent(QResizeEvent *event)
  1852. {
  1853. struct obs_video_info ovi;
  1854. if (obs_get_video_info(&ovi))
  1855. ResizePreview(ovi.base_width, ovi.base_height);
  1856. OBSMainWindow::resizeEvent(event);
  1857. }
  1858. void OBSBasic::on_actionShow_Recordings_triggered()
  1859. {
  1860. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  1861. const char *path = strcmp(mode, "Advanced") ?
  1862. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  1863. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  1864. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  1865. }
  1866. void OBSBasic::on_actionRemux_triggered()
  1867. {
  1868. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  1869. const char *path = strcmp(mode, "Advanced") ?
  1870. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  1871. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  1872. OBSRemux remux(path, this);
  1873. remux.exec();
  1874. }
  1875. void OBSBasic::on_action_Settings_triggered()
  1876. {
  1877. OBSBasicSettings settings(this);
  1878. settings.exec();
  1879. }
  1880. void OBSBasic::on_actionAdvAudioProperties_triggered()
  1881. {
  1882. if (advAudioWindow != nullptr) {
  1883. advAudioWindow->raise();
  1884. return;
  1885. }
  1886. advAudioWindow = new OBSBasicAdvAudio(this);
  1887. advAudioWindow->show();
  1888. advAudioWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  1889. connect(advAudioWindow, SIGNAL(destroyed()),
  1890. this, SLOT(on_advAudioProps_destroyed()));
  1891. }
  1892. void OBSBasic::on_advAudioProps_clicked()
  1893. {
  1894. on_actionAdvAudioProperties_triggered();
  1895. }
  1896. void OBSBasic::on_advAudioProps_destroyed()
  1897. {
  1898. advAudioWindow = nullptr;
  1899. }
  1900. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  1901. QListWidgetItem *prev)
  1902. {
  1903. obs_source_t *source = NULL;
  1904. if (sceneChanging)
  1905. return;
  1906. if (current) {
  1907. obs_scene_t *scene;
  1908. scene = GetOBSRef<OBSScene>(current);
  1909. source = obs_scene_get_source(scene);
  1910. }
  1911. /* TODO: allow transitions */
  1912. obs_set_output_source(0, source);
  1913. UNUSED_PARAMETER(prev);
  1914. }
  1915. void OBSBasic::EditSceneName()
  1916. {
  1917. QListWidgetItem *item = ui->scenes->currentItem();
  1918. Qt::ItemFlags flags = item->flags();
  1919. item->setFlags(flags | Qt::ItemIsEditable);
  1920. ui->scenes->editItem(item);
  1921. item->setFlags(flags);
  1922. }
  1923. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  1924. const char *slot)
  1925. {
  1926. QAction *action;
  1927. std::vector<MonitorInfo> monitors;
  1928. GetMonitors(monitors);
  1929. for (int i = 0; (size_t)i < monitors.size(); i++) {
  1930. const MonitorInfo &monitor = monitors[i];
  1931. QString str = QString("%1 %2: %3x%4 @ %5,%6").
  1932. arg(QTStr("Display"),
  1933. QString::number(i),
  1934. QString::number((int)monitor.cx),
  1935. QString::number((int)monitor.cy),
  1936. QString::number((int)monitor.x),
  1937. QString::number((int)monitor.y));
  1938. action = parent->addAction(str, target, slot);
  1939. action->setProperty("monitor", i);
  1940. }
  1941. }
  1942. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  1943. {
  1944. QListWidgetItem *item = ui->scenes->itemAt(pos);
  1945. QPointer<QMenu> sceneProjectorMenu;
  1946. QMenu popup(this);
  1947. QMenu order(QTStr("Basic.MainMenu.Edit.Order"), this);
  1948. popup.addAction(QTStr("Add"),
  1949. this, SLOT(on_actionAddScene_triggered()));
  1950. if (item) {
  1951. popup.addSeparator();
  1952. popup.addAction(QTStr("Duplicate"),
  1953. this, SLOT(DuplicateSelectedScene()));
  1954. popup.addAction(QTStr("Rename"),
  1955. this, SLOT(EditSceneName()));
  1956. popup.addAction(QTStr("Remove"),
  1957. this, SLOT(RemoveSelectedScene()),
  1958. DeleteKeys.front());
  1959. popup.addSeparator();
  1960. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveUp"),
  1961. this, SLOT(on_actionSceneUp_triggered()));
  1962. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveDown"),
  1963. this, SLOT(on_actionSceneDown_triggered()));
  1964. order.addSeparator();
  1965. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToTop"),
  1966. this, SLOT(MoveSceneToTop()));
  1967. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToBottom"),
  1968. this, SLOT(MoveSceneToBottom()));
  1969. popup.addMenu(&order);
  1970. popup.addSeparator();
  1971. sceneProjectorMenu = new QMenu(QTStr("SceneProjector"));
  1972. AddProjectorMenuMonitors(sceneProjectorMenu, this,
  1973. SLOT(OpenSceneProjector()));
  1974. popup.addMenu(sceneProjectorMenu);
  1975. popup.addSeparator();
  1976. popup.addAction(QTStr("Filters"), this,
  1977. SLOT(OpenSceneFilters()));
  1978. }
  1979. popup.exec(QCursor::pos());
  1980. }
  1981. void OBSBasic::on_actionAddScene_triggered()
  1982. {
  1983. string name;
  1984. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  1985. int i = 1;
  1986. QString placeHolderText = format.arg(i);
  1987. obs_source_t *source = nullptr;
  1988. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1989. obs_source_release(source);
  1990. placeHolderText = format.arg(++i);
  1991. }
  1992. bool accepted = NameDialog::AskForName(this,
  1993. QTStr("Basic.Main.AddSceneDlg.Title"),
  1994. QTStr("Basic.Main.AddSceneDlg.Text"),
  1995. name,
  1996. placeHolderText);
  1997. if (accepted) {
  1998. if (name.empty()) {
  1999. QMessageBox::information(this,
  2000. QTStr("NoNameEntered.Title"),
  2001. QTStr("NoNameEntered.Text"));
  2002. on_actionAddScene_triggered();
  2003. return;
  2004. }
  2005. obs_source_t *source = obs_get_source_by_name(name.c_str());
  2006. if (source) {
  2007. QMessageBox::information(this,
  2008. QTStr("NameExists.Title"),
  2009. QTStr("NameExists.Text"));
  2010. obs_source_release(source);
  2011. on_actionAddScene_triggered();
  2012. return;
  2013. }
  2014. obs_scene_t *scene = obs_scene_create(name.c_str());
  2015. source = obs_scene_get_source(scene);
  2016. obs_add_source(source);
  2017. obs_scene_release(scene);
  2018. obs_set_output_source(0, source);
  2019. }
  2020. }
  2021. void OBSBasic::on_actionRemoveScene_triggered()
  2022. {
  2023. OBSScene scene = GetCurrentScene();
  2024. obs_source_t *source = obs_scene_get_source(scene);
  2025. if (source && QueryRemoveSource(source))
  2026. obs_source_remove(source);
  2027. }
  2028. void OBSBasic::ChangeSceneIndex(bool relative, int offset, int invalidIdx)
  2029. {
  2030. int idx = ui->scenes->currentRow();
  2031. if (idx == -1 || idx == invalidIdx)
  2032. return;
  2033. sceneChanging = true;
  2034. QListWidgetItem *item = ui->scenes->takeItem(idx);
  2035. if (!relative)
  2036. idx = 0;
  2037. ui->scenes->insertItem(idx + offset, item);
  2038. ui->scenes->setCurrentRow(idx + offset);
  2039. item->setSelected(true);
  2040. sceneChanging = false;
  2041. }
  2042. void OBSBasic::on_actionSceneUp_triggered()
  2043. {
  2044. ChangeSceneIndex(true, -1, 0);
  2045. }
  2046. void OBSBasic::on_actionSceneDown_triggered()
  2047. {
  2048. ChangeSceneIndex(true, 1, ui->scenes->count() - 1);
  2049. }
  2050. void OBSBasic::MoveSceneToTop()
  2051. {
  2052. ChangeSceneIndex(false, 0, 0);
  2053. }
  2054. void OBSBasic::MoveSceneToBottom()
  2055. {
  2056. ChangeSceneIndex(false, ui->scenes->count() - 1,
  2057. ui->scenes->count() - 1);
  2058. }
  2059. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  2060. QListWidgetItem *prev)
  2061. {
  2062. auto select_one = [] (obs_scene_t *scene, obs_sceneitem_t *item,
  2063. void *param)
  2064. {
  2065. obs_sceneitem_t *selectedItem =
  2066. *reinterpret_cast<OBSSceneItem*>(param);
  2067. obs_sceneitem_select(item, (selectedItem == item));
  2068. UNUSED_PARAMETER(scene);
  2069. return true;
  2070. };
  2071. if (!current)
  2072. return;
  2073. OBSSceneItem item = GetOBSRef<OBSSceneItem>(current);
  2074. obs_source_t *source = obs_sceneitem_get_source(item);
  2075. if ((obs_source_get_output_flags(source) & OBS_SOURCE_VIDEO) == 0)
  2076. return;
  2077. auto IgnoreSelfUpdate = [&](obs_scene_t *scene)
  2078. {
  2079. ignoreSelectionUpdate = true;
  2080. obs_scene_enum_items(scene, select_one, &item);
  2081. ignoreSelectionUpdate = false;
  2082. };
  2083. using IgnoreSelfUpdate_t = decltype(IgnoreSelfUpdate);
  2084. obs_scene_atomic_update(GetCurrentScene(),
  2085. [](void *data, obs_scene_t *scene)
  2086. {
  2087. (*static_cast<IgnoreSelfUpdate_t*>(data))(scene);
  2088. }, static_cast<void*>(&IgnoreSelfUpdate));
  2089. UNUSED_PARAMETER(prev);
  2090. }
  2091. void OBSBasic::EditSceneItemName()
  2092. {
  2093. QListWidgetItem *item = ui->sources->currentItem();
  2094. Qt::ItemFlags flags = item->flags();
  2095. OBSSceneItem sceneItem= GetOBSRef<OBSSceneItem>(item);
  2096. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2097. const char *name = obs_source_get_name(source);
  2098. item->setText(QT_UTF8(name));
  2099. item->setFlags(flags | Qt::ItemIsEditable);
  2100. ui->sources->removeItemWidget(item);
  2101. ui->sources->editItem(item);
  2102. item->setFlags(flags);
  2103. }
  2104. void OBSBasic::CreateSourcePopupMenu(QListWidgetItem *item, bool preview)
  2105. {
  2106. QMenu popup(this);
  2107. QPointer<QMenu> previewProjector;
  2108. QPointer<QMenu> sourceProjector;
  2109. if (preview) {
  2110. QAction *action = popup.addAction(
  2111. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2112. this, SLOT(TogglePreview()));
  2113. action->setCheckable(true);
  2114. action->setChecked(
  2115. obs_display_enabled(ui->preview->GetDisplay()));
  2116. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2117. AddProjectorMenuMonitors(previewProjector, this,
  2118. SLOT(OpenPreviewProjector()));
  2119. popup.addMenu(previewProjector);
  2120. popup.addSeparator();
  2121. }
  2122. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  2123. if (addSourceMenu)
  2124. popup.addMenu(addSourceMenu);
  2125. if (item) {
  2126. if (addSourceMenu)
  2127. popup.addSeparator();
  2128. OBSSceneItem sceneItem = GetSceneItem(item);
  2129. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2130. QAction *action;
  2131. popup.addAction(QTStr("Rename"), this,
  2132. SLOT(EditSceneItemName()));
  2133. popup.addAction(QTStr("Remove"), this,
  2134. SLOT(on_actionRemoveSource_triggered()),
  2135. DeleteKeys.front());
  2136. popup.addSeparator();
  2137. popup.addMenu(ui->orderMenu);
  2138. popup.addMenu(ui->transformMenu);
  2139. sourceProjector = new QMenu(QTStr("SourceProjector"));
  2140. AddProjectorMenuMonitors(sourceProjector, this,
  2141. SLOT(OpenSourceProjector()));
  2142. popup.addSeparator();
  2143. popup.addMenu(sourceProjector);
  2144. popup.addSeparator();
  2145. action = popup.addAction(QTStr("Interact"), this,
  2146. SLOT(on_actionInteract_triggered()));
  2147. action->setEnabled(obs_source_get_output_flags(source) &
  2148. OBS_SOURCE_INTERACTION);
  2149. popup.addAction(QTStr("Filters"), this,
  2150. SLOT(OpenFilters()));
  2151. popup.addAction(QTStr("Properties"), this,
  2152. SLOT(on_actionSourceProperties_triggered()));
  2153. }
  2154. popup.exec(QCursor::pos());
  2155. }
  2156. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  2157. {
  2158. CreateSourcePopupMenu(ui->sources->itemAt(pos), false);
  2159. }
  2160. void OBSBasic::on_sources_itemDoubleClicked(QListWidgetItem *witem)
  2161. {
  2162. if (!witem)
  2163. return;
  2164. OBSSceneItem item = GetSceneItem(witem);
  2165. OBSSource source = obs_sceneitem_get_source(item);
  2166. if (source)
  2167. CreatePropertiesWindow(source);
  2168. }
  2169. void OBSBasic::AddSource(const char *id)
  2170. {
  2171. if (id && *id) {
  2172. OBSBasicSourceSelect sourceSelect(this, id);
  2173. sourceSelect.exec();
  2174. }
  2175. }
  2176. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  2177. {
  2178. const char *type;
  2179. bool foundValues = false;
  2180. size_t idx = 0;
  2181. QMenu *popup = new QMenu(QTStr("Add"), this);
  2182. while (obs_enum_input_types(idx++, &type)) {
  2183. const char *name = obs_source_get_display_name(
  2184. OBS_SOURCE_TYPE_INPUT, type);
  2185. if (strcmp(type, "scene") == 0)
  2186. continue;
  2187. QAction *popupItem = new QAction(QT_UTF8(name), this);
  2188. popupItem->setData(QT_UTF8(type));
  2189. connect(popupItem, SIGNAL(triggered(bool)),
  2190. this, SLOT(AddSourceFromAction()));
  2191. popup->addAction(popupItem);
  2192. foundValues = true;
  2193. }
  2194. if (!foundValues) {
  2195. delete popup;
  2196. popup = nullptr;
  2197. }
  2198. return popup;
  2199. }
  2200. void OBSBasic::AddSourceFromAction()
  2201. {
  2202. QAction *action = qobject_cast<QAction*>(sender());
  2203. if (!action)
  2204. return;
  2205. AddSource(QT_TO_UTF8(action->data().toString()));
  2206. }
  2207. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  2208. {
  2209. if (!GetCurrentScene()) {
  2210. // Tell the user he needs a scene first (help beginners).
  2211. QMessageBox::information(this,
  2212. QTStr("Basic.Main.AddSourceHelp.Title"),
  2213. QTStr("Basic.Main.AddSourceHelp.Text"));
  2214. return;
  2215. }
  2216. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  2217. if (popup)
  2218. popup->exec(pos);
  2219. }
  2220. void OBSBasic::on_actionAddSource_triggered()
  2221. {
  2222. AddSourcePopupMenu(QCursor::pos());
  2223. }
  2224. void OBSBasic::on_actionRemoveSource_triggered()
  2225. {
  2226. OBSSceneItem item = GetCurrentSceneItem();
  2227. obs_source_t *source = obs_sceneitem_get_source(item);
  2228. if (source && QueryRemoveSource(source))
  2229. obs_sceneitem_remove(item);
  2230. }
  2231. void OBSBasic::on_actionInteract_triggered()
  2232. {
  2233. OBSSceneItem item = GetCurrentSceneItem();
  2234. OBSSource source = obs_sceneitem_get_source(item);
  2235. if (source)
  2236. CreateInteractionWindow(source);
  2237. }
  2238. void OBSBasic::on_actionSourceProperties_triggered()
  2239. {
  2240. OBSSceneItem item = GetCurrentSceneItem();
  2241. OBSSource source = obs_sceneitem_get_source(item);
  2242. if (source)
  2243. CreatePropertiesWindow(source);
  2244. }
  2245. void OBSBasic::on_actionSourceUp_triggered()
  2246. {
  2247. OBSSceneItem item = GetCurrentSceneItem();
  2248. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  2249. }
  2250. void OBSBasic::on_actionSourceDown_triggered()
  2251. {
  2252. OBSSceneItem item = GetCurrentSceneItem();
  2253. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  2254. }
  2255. void OBSBasic::on_actionMoveUp_triggered()
  2256. {
  2257. OBSSceneItem item = GetCurrentSceneItem();
  2258. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  2259. }
  2260. void OBSBasic::on_actionMoveDown_triggered()
  2261. {
  2262. OBSSceneItem item = GetCurrentSceneItem();
  2263. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  2264. }
  2265. void OBSBasic::on_actionMoveToTop_triggered()
  2266. {
  2267. OBSSceneItem item = GetCurrentSceneItem();
  2268. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_TOP);
  2269. }
  2270. void OBSBasic::on_actionMoveToBottom_triggered()
  2271. {
  2272. OBSSceneItem item = GetCurrentSceneItem();
  2273. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_BOTTOM);
  2274. }
  2275. static BPtr<char> ReadLogFile(const char *log)
  2276. {
  2277. char logDir[512];
  2278. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2279. return nullptr;
  2280. string path = (char*)logDir;
  2281. path += "/";
  2282. path += log;
  2283. BPtr<char> file = os_quick_read_utf8_file(path.c_str());
  2284. if (!file)
  2285. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  2286. return file;
  2287. }
  2288. void OBSBasic::UploadLog(const char *file)
  2289. {
  2290. BPtr<char> fileString{ReadLogFile(file)};
  2291. if (!fileString)
  2292. return;
  2293. if (!*fileString)
  2294. return;
  2295. ui->menuLogFiles->setEnabled(false);
  2296. auto data_deleter = [](obs_data_t *d) { obs_data_release(d); };
  2297. using data_t = unique_ptr<struct obs_data, decltype(data_deleter)>;
  2298. data_t content{obs_data_create(), data_deleter};
  2299. data_t files{obs_data_create(), data_deleter};
  2300. data_t request{obs_data_create(), data_deleter};
  2301. obs_data_set_string(content.get(), "content", fileString);
  2302. obs_data_set_obj(files.get(), file, content.get());
  2303. stringstream ss;
  2304. ss << "OBS " << App()->GetVersionString()
  2305. << " log file uploaded at " << CurrentDateTimeString();
  2306. obs_data_set_string(request.get(), "description", ss.str().c_str());
  2307. obs_data_set_bool(request.get(), "public", false);
  2308. obs_data_set_obj(request.get(), "files", files.get());
  2309. const char *json = obs_data_get_json(request.get());
  2310. if (!json) {
  2311. blog(LOG_ERROR, "Failed to get JSON data for log upload");
  2312. return;
  2313. }
  2314. QBuffer *postData = new QBuffer();
  2315. postData->setData(json, (int) strlen(json));
  2316. if (logUploadThread) {
  2317. logUploadThread->wait();
  2318. delete logUploadThread;
  2319. }
  2320. RemoteTextThread *thread = new RemoteTextThread(
  2321. "https://api.github.com/gists",
  2322. "application/json", json);
  2323. logUploadThread = thread;
  2324. connect(thread, &RemoteTextThread::Result,
  2325. this, &OBSBasic::logUploadFinished);
  2326. logUploadThread->start();
  2327. }
  2328. void OBSBasic::on_actionShowLogs_triggered()
  2329. {
  2330. char logDir[512];
  2331. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2332. return;
  2333. QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
  2334. QDesktopServices::openUrl(url);
  2335. }
  2336. void OBSBasic::on_actionUploadCurrentLog_triggered()
  2337. {
  2338. UploadLog(App()->GetCurrentLog());
  2339. }
  2340. void OBSBasic::on_actionUploadLastLog_triggered()
  2341. {
  2342. UploadLog(App()->GetLastLog());
  2343. }
  2344. void OBSBasic::on_actionViewCurrentLog_triggered()
  2345. {
  2346. char logDir[512];
  2347. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2348. return;
  2349. const char* log = App()->GetCurrentLog();
  2350. string path = (char*)logDir;
  2351. path += "/";
  2352. path += log;
  2353. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  2354. QDesktopServices::openUrl(url);
  2355. }
  2356. void OBSBasic::on_actionCheckForUpdates_triggered()
  2357. {
  2358. CheckForUpdates();
  2359. }
  2360. void OBSBasic::logUploadFinished(const QString &text, const QString &error)
  2361. {
  2362. ui->menuLogFiles->setEnabled(true);
  2363. if (text.isEmpty()) {
  2364. QMessageBox::information(this,
  2365. QTStr("LogReturnDialog.ErrorUploadingLog"),
  2366. error);
  2367. return;
  2368. }
  2369. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  2370. QString logURL = obs_data_get_string(returnData, "html_url");
  2371. obs_data_release(returnData);
  2372. OBSLogReply logDialog(this, logURL);
  2373. logDialog.exec();
  2374. }
  2375. static void RenameListItem(OBSBasic *parent, QListWidget *listWidget,
  2376. obs_source_t *source, const string &name)
  2377. {
  2378. const char *prevName = obs_source_get_name(source);
  2379. if (name == prevName)
  2380. return;
  2381. obs_source_t *foundSource = obs_get_source_by_name(name.c_str());
  2382. QListWidgetItem *listItem = listWidget->currentItem();
  2383. if (foundSource || name.empty()) {
  2384. listItem->setText(QT_UTF8(prevName));
  2385. if (foundSource) {
  2386. QMessageBox::information(parent,
  2387. QTStr("NameExists.Title"),
  2388. QTStr("NameExists.Text"));
  2389. } else if (name.empty()) {
  2390. QMessageBox::information(parent,
  2391. QTStr("NoNameEntered.Title"),
  2392. QTStr("NoNameEntered.Text"));
  2393. }
  2394. obs_source_release(foundSource);
  2395. } else {
  2396. listItem->setText(QT_UTF8(name.c_str()));
  2397. obs_source_set_name(source, name.c_str());
  2398. }
  2399. }
  2400. void OBSBasic::SceneNameEdited(QWidget *editor,
  2401. QAbstractItemDelegate::EndEditHint endHint)
  2402. {
  2403. OBSScene scene = GetCurrentScene();
  2404. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2405. string text = QT_TO_UTF8(edit->text().trimmed());
  2406. if (!scene)
  2407. return;
  2408. obs_source_t *source = obs_scene_get_source(scene);
  2409. RenameListItem(this, ui->scenes, source, text);
  2410. UNUSED_PARAMETER(endHint);
  2411. }
  2412. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  2413. QAbstractItemDelegate::EndEditHint endHint)
  2414. {
  2415. OBSSceneItem item = GetCurrentSceneItem();
  2416. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2417. string text = QT_TO_UTF8(edit->text().trimmed());
  2418. if (!item)
  2419. return;
  2420. obs_source_t *source = obs_sceneitem_get_source(item);
  2421. RenameListItem(this, ui->sources, source, text);
  2422. QListWidgetItem *listItem = ui->sources->currentItem();
  2423. listItem->setText(QString());
  2424. SetupVisibilityItem(ui->sources, listItem, item);
  2425. UNUSED_PARAMETER(endHint);
  2426. }
  2427. void OBSBasic::OpenFilters()
  2428. {
  2429. OBSSceneItem item = GetCurrentSceneItem();
  2430. OBSSource source = obs_sceneitem_get_source(item);
  2431. CreateFiltersWindow(source);
  2432. }
  2433. void OBSBasic::OpenSceneFilters()
  2434. {
  2435. OBSScene scene = GetCurrentScene();
  2436. OBSSource source = obs_scene_get_source(scene);
  2437. CreateFiltersWindow(source);
  2438. }
  2439. #define RECORDING_START \
  2440. "==== Recording Start ==============================================="
  2441. #define RECORDING_STOP \
  2442. "==== Recording Stop ================================================"
  2443. #define STREAMING_START \
  2444. "==== Streaming Start ==============================================="
  2445. #define STREAMING_STOP \
  2446. "==== Streaming Stop ================================================"
  2447. void OBSBasic::StartStreaming()
  2448. {
  2449. SaveProject();
  2450. ui->streamButton->setEnabled(false);
  2451. ui->streamButton->setText(QTStr("Basic.Main.Connecting"));
  2452. if (!outputHandler->StartStreaming(service)) {
  2453. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2454. ui->streamButton->setEnabled(true);
  2455. }
  2456. }
  2457. void OBSBasic::StopStreaming()
  2458. {
  2459. SaveProject();
  2460. if (outputHandler->StreamingActive())
  2461. outputHandler->StopStreaming();
  2462. if (!outputHandler->Active()) {
  2463. ui->profileMenu->setEnabled(true);
  2464. }
  2465. }
  2466. void OBSBasic::ForceStopStreaming()
  2467. {
  2468. SaveProject();
  2469. if (outputHandler->StreamingActive())
  2470. outputHandler->ForceStopStreaming();
  2471. if (!outputHandler->Active()) {
  2472. ui->profileMenu->setEnabled(true);
  2473. }
  2474. }
  2475. void OBSBasic::StreamDelayStarting(int sec)
  2476. {
  2477. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2478. ui->streamButton->setEnabled(true);
  2479. if (!startStreamMenu.isNull())
  2480. startStreamMenu->deleteLater();
  2481. startStreamMenu = new QMenu();
  2482. startStreamMenu->addAction(QTStr("Basic.Main.StopStreaming"),
  2483. this, SLOT(StopStreaming()));
  2484. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  2485. this, SLOT(ForceStopStreaming()));
  2486. ui->streamButton->setMenu(startStreamMenu);
  2487. ui->statusbar->StreamDelayStarting(sec);
  2488. }
  2489. void OBSBasic::StreamDelayStopping(int sec)
  2490. {
  2491. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2492. ui->streamButton->setEnabled(true);
  2493. if (!startStreamMenu.isNull())
  2494. startStreamMenu->deleteLater();
  2495. startStreamMenu = new QMenu();
  2496. startStreamMenu->addAction(QTStr("Basic.Main.StartStreaming"),
  2497. this, SLOT(StartStreaming()));
  2498. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  2499. this, SLOT(ForceStopStreaming()));
  2500. ui->streamButton->setMenu(startStreamMenu);
  2501. ui->statusbar->StreamDelayStopping(sec);
  2502. }
  2503. void OBSBasic::StreamingStart()
  2504. {
  2505. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2506. ui->streamButton->setEnabled(true);
  2507. ui->statusbar->StreamStarted(outputHandler->streamOutput);
  2508. ui->profileMenu->setEnabled(false);
  2509. blog(LOG_INFO, STREAMING_START);
  2510. }
  2511. void OBSBasic::StreamingStop(int code)
  2512. {
  2513. const char *errorMessage;
  2514. switch (code) {
  2515. case OBS_OUTPUT_BAD_PATH:
  2516. errorMessage = Str("Output.ConnectFail.BadPath");
  2517. break;
  2518. case OBS_OUTPUT_CONNECT_FAILED:
  2519. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  2520. break;
  2521. case OBS_OUTPUT_INVALID_STREAM:
  2522. errorMessage = Str("Output.ConnectFail.InvalidStream");
  2523. break;
  2524. default:
  2525. case OBS_OUTPUT_ERROR:
  2526. errorMessage = Str("Output.ConnectFail.Error");
  2527. break;
  2528. case OBS_OUTPUT_DISCONNECTED:
  2529. /* doesn't happen if output is set to reconnect. note that
  2530. * reconnects are handled in the output, not in the UI */
  2531. errorMessage = Str("Output.ConnectFail.Disconnected");
  2532. }
  2533. ui->statusbar->StreamStopped();
  2534. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2535. ui->streamButton->setEnabled(true);
  2536. if (!outputHandler->Active())
  2537. ui->profileMenu->setEnabled(true);
  2538. blog(LOG_INFO, STREAMING_STOP);
  2539. if (code != OBS_OUTPUT_SUCCESS)
  2540. QMessageBox::information(this,
  2541. QTStr("Output.ConnectFail.Title"),
  2542. QT_UTF8(errorMessage));
  2543. if (!startStreamMenu.isNull()) {
  2544. ui->streamButton->setMenu(nullptr);
  2545. startStreamMenu->deleteLater();
  2546. startStreamMenu = nullptr;
  2547. }
  2548. }
  2549. void OBSBasic::StartRecording()
  2550. {
  2551. SaveProject();
  2552. if (!outputHandler->RecordingActive())
  2553. outputHandler->StartRecording();
  2554. }
  2555. void OBSBasic::StopRecording()
  2556. {
  2557. SaveProject();
  2558. if (outputHandler->RecordingActive())
  2559. outputHandler->StopRecording();
  2560. if (!outputHandler->Active()) {
  2561. ui->profileMenu->setEnabled(true);
  2562. }
  2563. }
  2564. void OBSBasic::RecordingStart()
  2565. {
  2566. ui->statusbar->RecordingStarted(outputHandler->fileOutput);
  2567. ui->recordButton->setText(QTStr("Basic.Main.StopRecording"));
  2568. ui->profileMenu->setEnabled(false);
  2569. blog(LOG_INFO, RECORDING_START);
  2570. }
  2571. void OBSBasic::RecordingStop(int code)
  2572. {
  2573. ui->statusbar->RecordingStopped();
  2574. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  2575. blog(LOG_INFO, RECORDING_STOP);
  2576. if (code == OBS_OUTPUT_UNSUPPORTED)
  2577. QMessageBox::information(this,
  2578. QTStr("Output.RecordFail.Title"),
  2579. QTStr("Output.RecordFail.Unsupported"));
  2580. if (!outputHandler->Active()) {
  2581. ui->profileMenu->setEnabled(true);
  2582. }
  2583. }
  2584. void OBSBasic::on_streamButton_clicked()
  2585. {
  2586. if (outputHandler->StreamingActive()) {
  2587. StopStreaming();
  2588. } else {
  2589. StartStreaming();
  2590. }
  2591. }
  2592. void OBSBasic::on_recordButton_clicked()
  2593. {
  2594. if (outputHandler->RecordingActive())
  2595. StopRecording();
  2596. else
  2597. StartRecording();
  2598. }
  2599. void OBSBasic::on_settingsButton_clicked()
  2600. {
  2601. OBSBasicSettings settings(this);
  2602. settings.exec();
  2603. }
  2604. void OBSBasic::on_actionWebsite_triggered()
  2605. {
  2606. QUrl url = QUrl("https://obsproject.com", QUrl::TolerantMode);
  2607. QDesktopServices::openUrl(url);
  2608. }
  2609. void OBSBasic::on_actionShowSettingsFolder_triggered()
  2610. {
  2611. char path[512];
  2612. int ret = GetConfigPath(path, 512, "obs-studio");
  2613. if (ret <= 0)
  2614. return;
  2615. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  2616. }
  2617. void OBSBasic::on_actionShowProfileFolder_triggered()
  2618. {
  2619. char path[512];
  2620. int ret = GetProfilePath(path, 512, "");
  2621. if (ret <= 0)
  2622. return;
  2623. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  2624. }
  2625. void OBSBasic::on_preview_customContextMenuRequested(const QPoint &pos)
  2626. {
  2627. CreateSourcePopupMenu(ui->sources->currentItem(), true);
  2628. UNUSED_PARAMETER(pos);
  2629. }
  2630. void OBSBasic::on_previewDisabledLabel_customContextMenuRequested(
  2631. const QPoint &pos)
  2632. {
  2633. QMenu popup(this);
  2634. QPointer<QMenu> previewProjector;
  2635. QAction *action = popup.addAction(
  2636. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2637. this, SLOT(TogglePreview()));
  2638. action->setCheckable(true);
  2639. action->setChecked(obs_display_enabled(ui->preview->GetDisplay()));
  2640. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2641. AddProjectorMenuMonitors(previewProjector, this,
  2642. SLOT(OpenPreviewProjector()));
  2643. popup.addMenu(previewProjector);
  2644. popup.exec(QCursor::pos());
  2645. UNUSED_PARAMETER(pos);
  2646. }
  2647. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  2648. {
  2649. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  2650. if (strcmp(val, "10") == 0) {
  2651. num = 10;
  2652. den = 1;
  2653. } else if (strcmp(val, "20") == 0) {
  2654. num = 20;
  2655. den = 1;
  2656. } else if (strcmp(val, "25") == 0) {
  2657. num = 25;
  2658. den = 1;
  2659. } else if (strcmp(val, "29.97") == 0) {
  2660. num = 30000;
  2661. den = 1001;
  2662. } else if (strcmp(val, "48") == 0) {
  2663. num = 48;
  2664. den = 1;
  2665. } else if (strcmp(val, "59.94") == 0) {
  2666. num = 60000;
  2667. den = 1001;
  2668. } else if (strcmp(val, "60") == 0) {
  2669. num = 60;
  2670. den = 1;
  2671. } else {
  2672. num = 30;
  2673. den = 1;
  2674. }
  2675. }
  2676. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  2677. {
  2678. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  2679. den = 1;
  2680. }
  2681. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  2682. {
  2683. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  2684. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  2685. }
  2686. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  2687. {
  2688. num = 1000000000;
  2689. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  2690. }
  2691. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  2692. {
  2693. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  2694. if (type == 1) //"Integer"
  2695. GetFPSInteger(num, den);
  2696. else if (type == 2) //"Fraction"
  2697. GetFPSFraction(num, den);
  2698. else if (false) //"Nanoseconds", currently not implemented
  2699. GetFPSNanoseconds(num, den);
  2700. else
  2701. GetFPSCommon(num, den);
  2702. }
  2703. config_t *OBSBasic::Config() const
  2704. {
  2705. return basicConfig;
  2706. }
  2707. void OBSBasic::on_actionEditTransform_triggered()
  2708. {
  2709. if (transformWindow)
  2710. transformWindow->close();
  2711. transformWindow = new OBSBasicTransform(this);
  2712. transformWindow->show();
  2713. transformWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  2714. }
  2715. void OBSBasic::on_actionResetTransform_triggered()
  2716. {
  2717. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2718. {
  2719. if (!obs_sceneitem_selected(item))
  2720. return true;
  2721. obs_transform_info info;
  2722. vec2_set(&info.pos, 0.0f, 0.0f);
  2723. vec2_set(&info.scale, 1.0f, 1.0f);
  2724. info.rot = 0.0f;
  2725. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  2726. info.bounds_type = OBS_BOUNDS_NONE;
  2727. info.bounds_alignment = OBS_ALIGN_CENTER;
  2728. vec2_set(&info.bounds, 0.0f, 0.0f);
  2729. obs_sceneitem_set_info(item, &info);
  2730. UNUSED_PARAMETER(scene);
  2731. UNUSED_PARAMETER(param);
  2732. return true;
  2733. };
  2734. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2735. }
  2736. static void GetItemBox(obs_sceneitem_t *item, vec3 &tl, vec3 &br)
  2737. {
  2738. matrix4 boxTransform;
  2739. obs_sceneitem_get_box_transform(item, &boxTransform);
  2740. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  2741. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  2742. auto GetMinPos = [&] (float x, float y)
  2743. {
  2744. vec3 pos;
  2745. vec3_set(&pos, x, y, 0.0f);
  2746. vec3_transform(&pos, &pos, &boxTransform);
  2747. vec3_min(&tl, &tl, &pos);
  2748. vec3_max(&br, &br, &pos);
  2749. };
  2750. GetMinPos(0.0f, 0.0f);
  2751. GetMinPos(1.0f, 0.0f);
  2752. GetMinPos(0.0f, 1.0f);
  2753. GetMinPos(1.0f, 1.0f);
  2754. }
  2755. static vec3 GetItemTL(obs_sceneitem_t *item)
  2756. {
  2757. vec3 tl, br;
  2758. GetItemBox(item, tl, br);
  2759. return tl;
  2760. }
  2761. static void SetItemTL(obs_sceneitem_t *item, const vec3 &tl)
  2762. {
  2763. vec3 newTL;
  2764. vec2 pos;
  2765. obs_sceneitem_get_pos(item, &pos);
  2766. newTL = GetItemTL(item);
  2767. pos.x += tl.x - newTL.x;
  2768. pos.y += tl.y - newTL.y;
  2769. obs_sceneitem_set_pos(item, &pos);
  2770. }
  2771. static bool RotateSelectedSources(obs_scene_t *scene, obs_sceneitem_t *item,
  2772. void *param)
  2773. {
  2774. if (!obs_sceneitem_selected(item))
  2775. return true;
  2776. float rot = *reinterpret_cast<float*>(param);
  2777. vec3 tl = GetItemTL(item);
  2778. rot += obs_sceneitem_get_rot(item);
  2779. if (rot >= 360.0f) rot -= 360.0f;
  2780. else if (rot <= -360.0f) rot += 360.0f;
  2781. obs_sceneitem_set_rot(item, rot);
  2782. SetItemTL(item, tl);
  2783. UNUSED_PARAMETER(scene);
  2784. UNUSED_PARAMETER(param);
  2785. return true;
  2786. };
  2787. void OBSBasic::on_actionRotate90CW_triggered()
  2788. {
  2789. float f90CW = 90.0f;
  2790. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  2791. }
  2792. void OBSBasic::on_actionRotate90CCW_triggered()
  2793. {
  2794. float f90CCW = -90.0f;
  2795. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  2796. }
  2797. void OBSBasic::on_actionRotate180_triggered()
  2798. {
  2799. float f180 = 180.0f;
  2800. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  2801. }
  2802. static bool MultiplySelectedItemScale(obs_scene_t *scene, obs_sceneitem_t *item,
  2803. void *param)
  2804. {
  2805. vec2 &mul = *reinterpret_cast<vec2*>(param);
  2806. if (!obs_sceneitem_selected(item))
  2807. return true;
  2808. vec3 tl = GetItemTL(item);
  2809. vec2 scale;
  2810. obs_sceneitem_get_scale(item, &scale);
  2811. vec2_mul(&scale, &scale, &mul);
  2812. obs_sceneitem_set_scale(item, &scale);
  2813. SetItemTL(item, tl);
  2814. UNUSED_PARAMETER(scene);
  2815. return true;
  2816. }
  2817. void OBSBasic::on_actionFlipHorizontal_triggered()
  2818. {
  2819. vec2 scale;
  2820. vec2_set(&scale, -1.0f, 1.0f);
  2821. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2822. &scale);
  2823. }
  2824. void OBSBasic::on_actionFlipVertical_triggered()
  2825. {
  2826. vec2 scale;
  2827. vec2_set(&scale, 1.0f, -1.0f);
  2828. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2829. &scale);
  2830. }
  2831. static bool CenterAlignSelectedItems(obs_scene_t *scene, obs_sceneitem_t *item,
  2832. void *param)
  2833. {
  2834. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  2835. if (!obs_sceneitem_selected(item))
  2836. return true;
  2837. obs_video_info ovi;
  2838. obs_get_video_info(&ovi);
  2839. obs_transform_info itemInfo;
  2840. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  2841. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  2842. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  2843. itemInfo.rot = 0.0f;
  2844. vec2_set(&itemInfo.bounds,
  2845. float(ovi.base_width), float(ovi.base_height));
  2846. itemInfo.bounds_type = boundsType;
  2847. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  2848. obs_sceneitem_set_info(item, &itemInfo);
  2849. UNUSED_PARAMETER(scene);
  2850. return true;
  2851. }
  2852. void OBSBasic::on_actionFitToScreen_triggered()
  2853. {
  2854. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  2855. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2856. &boundsType);
  2857. }
  2858. void OBSBasic::on_actionStretchToScreen_triggered()
  2859. {
  2860. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  2861. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2862. &boundsType);
  2863. }
  2864. void OBSBasic::on_actionCenterToScreen_triggered()
  2865. {
  2866. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2867. {
  2868. vec3 tl, br, itemCenter, screenCenter, offset;
  2869. obs_video_info ovi;
  2870. if (!obs_sceneitem_selected(item))
  2871. return true;
  2872. obs_get_video_info(&ovi);
  2873. vec3_set(&screenCenter, float(ovi.base_width),
  2874. float(ovi.base_height), 0.0f);
  2875. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  2876. GetItemBox(item, tl, br);
  2877. vec3_sub(&itemCenter, &br, &tl);
  2878. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  2879. vec3_add(&itemCenter, &itemCenter, &tl);
  2880. vec3_sub(&offset, &screenCenter, &itemCenter);
  2881. vec3_add(&tl, &tl, &offset);
  2882. SetItemTL(item, tl);
  2883. UNUSED_PARAMETER(scene);
  2884. UNUSED_PARAMETER(param);
  2885. return true;
  2886. };
  2887. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2888. }
  2889. void OBSBasic::TogglePreview()
  2890. {
  2891. bool enabled = !obs_display_enabled(ui->preview->GetDisplay());
  2892. obs_display_set_enabled(ui->preview->GetDisplay(), enabled);
  2893. ui->preview->setVisible(enabled);
  2894. ui->previewDisabledLabel->setVisible(!enabled);
  2895. }
  2896. void OBSBasic::Nudge(int dist, MoveDir dir)
  2897. {
  2898. struct MoveInfo {
  2899. float dist;
  2900. MoveDir dir;
  2901. } info = {(float)dist, dir};
  2902. auto func = [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  2903. {
  2904. MoveInfo *info = reinterpret_cast<MoveInfo*>(param);
  2905. struct vec2 dir;
  2906. struct vec2 pos;
  2907. vec2_set(&dir, 0.0f, 0.0f);
  2908. if (!obs_sceneitem_selected(item))
  2909. return true;
  2910. switch (info->dir) {
  2911. case MoveDir::Up: dir.y = -info->dist; break;
  2912. case MoveDir::Down: dir.y = info->dist; break;
  2913. case MoveDir::Left: dir.x = -info->dist; break;
  2914. case MoveDir::Right: dir.x = info->dist; break;
  2915. }
  2916. obs_sceneitem_get_pos(item, &pos);
  2917. vec2_add(&pos, &pos, &dir);
  2918. obs_sceneitem_set_pos(item, &pos);
  2919. return true;
  2920. };
  2921. obs_scene_enum_items(GetCurrentScene(), func, &info);
  2922. }
  2923. void OBSBasic::NudgeUp() {Nudge(1, MoveDir::Up);}
  2924. void OBSBasic::NudgeDown() {Nudge(1, MoveDir::Down);}
  2925. void OBSBasic::NudgeLeft() {Nudge(1, MoveDir::Left);}
  2926. void OBSBasic::NudgeRight() {Nudge(1, MoveDir::Right);}
  2927. void OBSBasic::OpenProjector(obs_source_t *source, int monitor)
  2928. {
  2929. /* seriously? 10 monitors? */
  2930. if (monitor > 9)
  2931. return;
  2932. delete projectors[monitor];
  2933. projectors[monitor].clear();
  2934. OBSProjector *projector = new OBSProjector(this, source);
  2935. projector->Init(monitor);
  2936. projectors[monitor] = projector;
  2937. }
  2938. void OBSBasic::OpenPreviewProjector()
  2939. {
  2940. int monitor = sender()->property("monitor").toInt();
  2941. OpenProjector(nullptr, monitor);
  2942. }
  2943. void OBSBasic::OpenSourceProjector()
  2944. {
  2945. int monitor = sender()->property("monitor").toInt();
  2946. OBSSceneItem item = GetCurrentSceneItem();
  2947. if (!item)
  2948. return;
  2949. OpenProjector(obs_sceneitem_get_source(item), monitor);
  2950. }
  2951. void OBSBasic::OpenSceneProjector()
  2952. {
  2953. int monitor = sender()->property("monitor").toInt();
  2954. OBSScene scene = GetCurrentScene();
  2955. if (!scene)
  2956. return;
  2957. OpenProjector(obs_scene_get_source(scene), monitor);
  2958. }
  2959. void OBSBasic::UpdateTitleBar()
  2960. {
  2961. stringstream name;
  2962. const char *profile = config_get_string(App()->GlobalConfig(),
  2963. "Basic", "Profile");
  2964. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  2965. "Basic", "SceneCollection");
  2966. name << "OBS " << App()->GetVersionString();
  2967. name << " - " << Str("TitleBar.Profile") << ": " << profile;
  2968. name << " - " << Str("TitleBar.Scenes") << ": " << sceneCollection;
  2969. setWindowTitle(QT_UTF8(name.str().c_str()));
  2970. }
  2971. int OBSBasic::GetProfilePath(char *path, size_t size, const char *file) const
  2972. {
  2973. char profiles_path[512];
  2974. const char *profile = config_get_string(App()->GlobalConfig(),
  2975. "Basic", "ProfileDir");
  2976. int ret;
  2977. if (!profile)
  2978. return -1;
  2979. if (!path)
  2980. return -1;
  2981. if (!file)
  2982. file = "";
  2983. ret = GetConfigPath(profiles_path, 512, "obs-studio/basic/profiles");
  2984. if (ret <= 0)
  2985. return ret;
  2986. if (!*file)
  2987. return snprintf(path, size, "%s/%s", profiles_path, profile);
  2988. return snprintf(path, size, "%s/%s/%s", profiles_path, profile, file);
  2989. }