1
0

window-basic-main.cpp 64 KB

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