window-basic-main.cpp 95 KB

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