window-basic-main.cpp 95 KB

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