window-basic-main.cpp 74 KB

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