window-basic-main.cpp 65 KB

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