window-basic-main.cpp 78 KB

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