window-basic-main.cpp 80 KB

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