1
0

window-basic-main.cpp 70 KB

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