window-basic-main.cpp 95 KB

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