window-basic-main.cpp 112 KB

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