window-basic-main.cpp 95 KB

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