window-basic-main.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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 <obs.hpp>
  16. #include <QMessageBox>
  17. #include <QShowEvent>
  18. #include <QFileDialog>
  19. #include <QNetworkRequest>
  20. #include <QNetworkReply>
  21. #include <util/dstr.h>
  22. #include <util/util.hpp>
  23. #include <util/platform.h>
  24. #include <graphics/math-defs.h>
  25. #include "obs-app.hpp"
  26. #include "platform.hpp"
  27. #include "window-basic-settings.hpp"
  28. #include "window-namedialog.hpp"
  29. #include "window-basic-source-select.hpp"
  30. #include "window-basic-main.hpp"
  31. #include "window-basic-properties.hpp"
  32. #include "window-log-reply.hpp"
  33. #include "qt-wrappers.hpp"
  34. #include "display-helpers.hpp"
  35. #include "volume-control.hpp"
  36. #include "ui_OBSBasic.h"
  37. #include <fstream>
  38. #include <sstream>
  39. #include <QScreen>
  40. #include <QWindow>
  41. #define PREVIEW_EDGE_SIZE 10
  42. using namespace std;
  43. Q_DECLARE_METATYPE(OBSScene);
  44. Q_DECLARE_METATYPE(OBSSceneItem);
  45. Q_DECLARE_METATYPE(order_movement);
  46. OBSBasic::OBSBasic(QWidget *parent)
  47. : OBSMainWindow (parent),
  48. ui (new Ui::OBSBasic)
  49. {
  50. ui->setupUi(this);
  51. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  52. struct obs_video_info ovi;
  53. if (obs_get_video_info(&ovi))
  54. ResizePreview(ovi.base_width, ovi.base_height);
  55. });
  56. stringstream name;
  57. name << "OBS " << App()->GetVersionString();
  58. blog(LOG_INFO, "%s", name.str().c_str());
  59. setWindowTitle(QT_UTF8(name.str().c_str()));
  60. connect(ui->scenes->itemDelegate(),
  61. SIGNAL(closeEditor(QWidget*,
  62. QAbstractItemDelegate::EndEditHint)),
  63. this,
  64. SLOT(SceneNameEdited(QWidget*,
  65. QAbstractItemDelegate::EndEditHint)));
  66. connect(ui->sources->itemDelegate(),
  67. SIGNAL(closeEditor(QWidget*,
  68. QAbstractItemDelegate::EndEditHint)),
  69. this,
  70. SLOT(SceneItemNameEdited(QWidget*,
  71. QAbstractItemDelegate::EndEditHint)));
  72. }
  73. static void SaveAudioDevice(const char *name, int channel, obs_data_t parent)
  74. {
  75. obs_source_t source = obs_get_output_source(channel);
  76. if (!source)
  77. return;
  78. obs_data_t data = obs_save_source(source);
  79. obs_data_setobj(parent, name, data);
  80. obs_data_release(data);
  81. obs_source_release(source);
  82. }
  83. static obs_data_t GenerateSaveData()
  84. {
  85. obs_data_t saveData = obs_data_create();
  86. obs_data_array_t sourcesArray = obs_save_sources();
  87. obs_source_t currentScene = obs_get_output_source(0);
  88. const char *sceneName = obs_source_getname(currentScene);
  89. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  90. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  91. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  92. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  93. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  94. obs_data_setstring(saveData, "current_scene", sceneName);
  95. obs_data_setarray(saveData, "sources", sourcesArray);
  96. obs_data_array_release(sourcesArray);
  97. obs_source_release(currentScene);
  98. return saveData;
  99. }
  100. void OBSBasic::ClearVolumeControls()
  101. {
  102. VolControl *control;
  103. for (size_t i = 0; i < volumes.size(); i++) {
  104. control = volumes[i];
  105. delete control;
  106. }
  107. volumes.clear();
  108. }
  109. void OBSBasic::Save(const char *file)
  110. {
  111. obs_data_t saveData = GenerateSaveData();
  112. const char *jsonData = obs_data_getjson(saveData);
  113. /* TODO maybe a message box here? */
  114. if (!os_quick_write_utf8_file(file, jsonData, strlen(jsonData), false))
  115. blog(LOG_ERROR, "Could not save scene data to %s", file);
  116. obs_data_release(saveData);
  117. }
  118. static void LoadAudioDevice(const char *name, int channel, obs_data_t parent)
  119. {
  120. obs_data_t data = obs_data_getobj(parent, name);
  121. if (!data)
  122. return;
  123. obs_source_t source = obs_load_source(data);
  124. if (source) {
  125. obs_set_output_source(channel, source);
  126. obs_source_release(source);
  127. }
  128. obs_data_release(data);
  129. }
  130. void OBSBasic::CreateDefaultScene()
  131. {
  132. obs_scene_t scene = obs_scene_create(Str("Basic.Scene"));
  133. obs_source_t source = obs_scene_getsource(scene);
  134. obs_add_source(source);
  135. #ifdef __APPLE__
  136. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, "display_capture",
  137. Str("Basic.DisplayCapture"), NULL);
  138. if (source) {
  139. obs_scene_add(scene, source);
  140. obs_add_source(source);
  141. obs_source_release(source);
  142. }
  143. #endif
  144. obs_set_output_source(0, obs_scene_getsource(scene));
  145. obs_scene_release(scene);
  146. }
  147. void OBSBasic::Load(const char *file)
  148. {
  149. if (!file) {
  150. blog(LOG_ERROR, "Could not find file %s", file);
  151. return;
  152. }
  153. BPtr<char> jsonData = os_quick_read_utf8_file(file);
  154. if (!jsonData) {
  155. CreateDefaultScene();
  156. return;
  157. }
  158. obs_data_t data = obs_data_create_from_json(jsonData);
  159. obs_data_array_t sources = obs_data_getarray(data, "sources");
  160. const char *sceneName = obs_data_getstring(data, "current_scene");
  161. obs_source_t curScene;
  162. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  163. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  164. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  165. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  166. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  167. obs_load_sources(sources);
  168. curScene = obs_get_source_by_name(sceneName);
  169. obs_set_output_source(0, curScene);
  170. obs_source_release(curScene);
  171. obs_data_array_release(sources);
  172. obs_data_release(data);
  173. }
  174. static inline bool HasAudioDevices(const char *source_id)
  175. {
  176. const char *output_id = source_id;
  177. obs_properties_t props = obs_get_source_properties(
  178. OBS_SOURCE_TYPE_INPUT, output_id);
  179. size_t count = 0;
  180. if (!props)
  181. return false;
  182. obs_property_t devices = obs_properties_get(props, "device_id");
  183. if (devices)
  184. count = obs_property_list_item_count(devices);
  185. obs_properties_destroy(props);
  186. return count != 0;
  187. }
  188. static void OBSStartStreaming(void *data, calldata_t params)
  189. {
  190. UNUSED_PARAMETER(params);
  191. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  192. "StreamingStart");
  193. }
  194. static void OBSStopStreaming(void *data, calldata_t params)
  195. {
  196. int code = (int)calldata_int(params, "code");
  197. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  198. "StreamingStop", Q_ARG(int, code));
  199. }
  200. static void OBSStopRecording(void *data, calldata_t params)
  201. {
  202. UNUSED_PARAMETER(params);
  203. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  204. "RecordingStop");
  205. }
  206. #define SERVICE_PATH "obs-studio/basic/service.json"
  207. void OBSBasic::SaveService()
  208. {
  209. if (!service)
  210. return;
  211. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  212. if (!serviceJsonPath)
  213. return;
  214. obs_data_t data = obs_data_create();
  215. obs_data_t settings = obs_service_get_settings(service);
  216. obs_data_setstring(data, "type", obs_service_gettype(service));
  217. obs_data_setobj(data, "settings", settings);
  218. const char *json = obs_data_getjson(data);
  219. os_quick_write_utf8_file(serviceJsonPath, json, strlen(json), false);
  220. obs_data_release(settings);
  221. obs_data_release(data);
  222. }
  223. bool OBSBasic::LoadService()
  224. {
  225. const char *type;
  226. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  227. if (!serviceJsonPath)
  228. return false;
  229. BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
  230. if (!jsonText)
  231. return false;
  232. obs_data_t data = obs_data_create_from_json(jsonText);
  233. obs_data_set_default_string(data, "type", "rtmp_common");
  234. type = obs_data_getstring(data, "type");
  235. obs_data_t settings = obs_data_getobj(data, "settings");
  236. service = obs_service_create(type, "default", settings);
  237. obs_data_release(settings);
  238. obs_data_release(data);
  239. return !!service;
  240. }
  241. bool OBSBasic::InitOutputs()
  242. {
  243. fileOutput = obs_output_create("flv_output", "default", nullptr);
  244. if (!fileOutput)
  245. return false;
  246. streamOutput = obs_output_create("rtmp_output", "default", nullptr);
  247. if (!streamOutput)
  248. return false;
  249. signal_handler_connect(obs_output_signalhandler(streamOutput),
  250. "start", OBSStartStreaming, this);
  251. signal_handler_connect(obs_output_signalhandler(streamOutput),
  252. "stop", OBSStopStreaming, this);
  253. signal_handler_connect(obs_output_signalhandler(fileOutput),
  254. "stop", OBSStopRecording, this);
  255. return true;
  256. }
  257. bool OBSBasic::InitEncoders()
  258. {
  259. x264 = obs_video_encoder_create("obs_x264", "h264", nullptr);
  260. if (!x264)
  261. return false;
  262. aac = obs_audio_encoder_create("libfdk_aac", "aac", nullptr);
  263. if(!aac)
  264. aac = obs_audio_encoder_create("ffmpeg_aac", "aac", nullptr);
  265. if (!aac)
  266. return false;
  267. return true;
  268. }
  269. bool OBSBasic::InitService()
  270. {
  271. if (LoadService())
  272. return true;
  273. service = obs_service_create("rtmp_common", nullptr, nullptr);
  274. if (!service)
  275. return false;
  276. return true;
  277. }
  278. bool OBSBasic::InitBasicConfigDefaults()
  279. {
  280. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  281. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  282. config_set_default_int(basicConfig, "Window", "PosX", -1);
  283. config_set_default_int(basicConfig, "Window", "PosY", -1);
  284. config_set_default_int(basicConfig, "Window", "SizeX", -1);
  285. config_set_default_int(basicConfig, "Window", "SizeY", -1);
  286. vector<MonitorInfo> monitors;
  287. GetMonitors(monitors);
  288. if (!monitors.size()) {
  289. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  290. "technically shouldn't be possible.");
  291. return false;
  292. }
  293. uint32_t cx = monitors[0].cx;
  294. uint32_t cy = monitors[0].cy;
  295. /* TODO: temporary */
  296. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  297. GetDefaultVideoSavePath().c_str());
  298. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  299. 2500);
  300. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 128);
  301. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  302. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  303. cx = cx * 10 / 15;
  304. cy = cy * 10 / 15;
  305. config_set_default_uint (basicConfig, "Video", "OutputCX", cx);
  306. config_set_default_uint (basicConfig, "Video", "OutputCY", cy);
  307. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  308. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  309. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  310. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  311. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  312. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  313. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  314. "Stereo");
  315. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  316. config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
  317. hasDesktopAudio ? "default" : "disabled");
  318. config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
  319. "disabled");
  320. config_set_default_string(basicConfig, "Audio", "AuxDevice1",
  321. hasInputAudio ? "default" : "disabled");
  322. config_set_default_string(basicConfig, "Audio", "AuxDevice2",
  323. "disabled");
  324. config_set_default_string(basicConfig, "Audio", "AuxDevice3",
  325. "disabled");
  326. return true;
  327. }
  328. bool OBSBasic::InitBasicConfig()
  329. {
  330. BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini"));
  331. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  332. if (code != CONFIG_SUCCESS) {
  333. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  334. return false;
  335. }
  336. return InitBasicConfigDefaults();
  337. }
  338. void OBSBasic::InitOBSCallbacks()
  339. {
  340. signal_handler_connect(obs_signalhandler(), "source_add",
  341. OBSBasic::SourceAdded, this);
  342. signal_handler_connect(obs_signalhandler(), "source_remove",
  343. OBSBasic::SourceRemoved, this);
  344. signal_handler_connect(obs_signalhandler(), "channel_change",
  345. OBSBasic::ChannelChanged, this);
  346. signal_handler_connect(obs_signalhandler(), "source_activate",
  347. OBSBasic::SourceActivated, this);
  348. signal_handler_connect(obs_signalhandler(), "source_deactivate",
  349. OBSBasic::SourceDeactivated, this);
  350. signal_handler_connect(obs_signalhandler(), "source_rename",
  351. OBSBasic::SourceRenamed, this);
  352. }
  353. void OBSBasic::InitPrimitives()
  354. {
  355. gs_entercontext(obs_graphics());
  356. gs_renderstart(true);
  357. gs_vertex2f(0.0f, 0.0f);
  358. gs_vertex2f(0.0f, 1.0f);
  359. gs_vertex2f(1.0f, 1.0f);
  360. gs_vertex2f(1.0f, 0.0f);
  361. gs_vertex2f(0.0f, 0.0f);
  362. box = gs_rendersave();
  363. gs_renderstart(true);
  364. for (int i = 0; i <= 360; i += (360/20)) {
  365. float pos = RAD(float(i));
  366. gs_vertex2f(cosf(pos), sinf(pos));
  367. }
  368. circle = gs_rendersave();
  369. gs_leavecontext();
  370. }
  371. void OBSBasic::OBSInit()
  372. {
  373. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  374. /* make sure it's fully displayed before doing any initialization */
  375. show();
  376. App()->processEvents();
  377. if (!obs_startup(App()->GetLocale()))
  378. throw "Failed to initialize libobs";
  379. if (!InitBasicConfig())
  380. throw "Failed to load basic.ini";
  381. if (!ResetVideo())
  382. throw "Failed to initialize video";
  383. if (!ResetAudio())
  384. throw "Failed to initialize audio";
  385. InitOBSCallbacks();
  386. /* TODO: this is a test, all modules will be searched for and loaded
  387. * automatically later */
  388. obs_load_module("image-source");
  389. obs_load_module("test-input");
  390. obs_load_module("obs-ffmpeg");
  391. obs_load_module("obs-libfdk");
  392. obs_load_module("obs-x264");
  393. obs_load_module("obs-outputs");
  394. obs_load_module("rtmp-services");
  395. #ifdef __APPLE__
  396. obs_load_module("mac-avcapture");
  397. obs_load_module("mac-capture");
  398. #elif _WIN32
  399. obs_load_module("win-wasapi");
  400. obs_load_module("win-capture");
  401. obs_load_module("win-dshow");
  402. #else
  403. obs_load_module("linux-xshm");
  404. obs_load_module("linux-xcomposite");
  405. obs_load_module("linux-pulseaudio");
  406. obs_load_module("linux-v4l2");
  407. #endif
  408. if (!InitOutputs())
  409. throw "Failed to initialize outputs";
  410. if (!InitEncoders())
  411. throw "Failed to initialize encoders";
  412. if (!InitService())
  413. throw "Failed to initialize service";
  414. InitPrimitives();
  415. Load(savePath);
  416. ResetAudioDevices();
  417. loaded = true;
  418. }
  419. OBSBasic::~OBSBasic()
  420. {
  421. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  422. SaveService();
  423. Save(savePath);
  424. /* XXX: any obs data must be released before calling obs_shutdown.
  425. * currently, we can't automate this with C++ RAII because of the
  426. * delicate nature of obs_shutdown needing to be freed before the UI
  427. * can be freed, and we have no control over the destruction order of
  428. * the Qt UI stuff, so we have to manually clear any references to
  429. * libobs. */
  430. if (properties)
  431. delete properties;
  432. if (transformWindow)
  433. delete transformWindow;
  434. ClearVolumeControls();
  435. ui->sources->clear();
  436. ui->scenes->clear();
  437. gs_entercontext(obs_graphics());
  438. vertexbuffer_destroy(box);
  439. vertexbuffer_destroy(circle);
  440. gs_leavecontext();
  441. obs_shutdown();
  442. }
  443. OBSScene OBSBasic::GetCurrentScene()
  444. {
  445. QListWidgetItem *item = ui->scenes->currentItem();
  446. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  447. }
  448. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  449. {
  450. QListWidgetItem *item = ui->sources->currentItem();
  451. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  452. }
  453. void OBSBasic::UpdateSources(OBSScene scene)
  454. {
  455. ui->sources->clear();
  456. obs_scene_enum_items(scene,
  457. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  458. {
  459. OBSBasic *window = static_cast<OBSBasic*>(p);
  460. window->InsertSceneItem(item);
  461. UNUSED_PARAMETER(scene);
  462. return true;
  463. }, this);
  464. }
  465. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  466. {
  467. obs_source_t source = obs_sceneitem_getsource(item);
  468. const char *name = obs_source_getname(source);
  469. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  470. listItem->setFlags(listItem->flags() | Qt::ItemIsEditable);
  471. listItem->setData(Qt::UserRole,
  472. QVariant::fromValue(OBSSceneItem(item)));
  473. ui->sources->insertItem(0, listItem);
  474. ui->sources->setCurrentRow(0);
  475. /* if the source was just created, open properties dialog */
  476. if (sourceSceneRefs[source] == 0 && loaded) {
  477. delete properties;
  478. properties = new OBSBasicProperties(this, source);
  479. properties->Init();
  480. }
  481. }
  482. /* Qt callbacks for invokeMethod */
  483. void OBSBasic::AddScene(OBSSource source)
  484. {
  485. const char *name = obs_source_getname(source);
  486. obs_scene_t scene = obs_scene_fromsource(source);
  487. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  488. item->setFlags(item->flags() | Qt::ItemIsEditable);
  489. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  490. ui->scenes->addItem(item);
  491. signal_handler_t handler = obs_source_signalhandler(source);
  492. signal_handler_connect(handler, "item_add",
  493. OBSBasic::SceneItemAdded, this);
  494. signal_handler_connect(handler, "item_remove",
  495. OBSBasic::SceneItemRemoved, this);
  496. signal_handler_connect(handler, "item_move_up",
  497. OBSBasic::SceneItemMoveUp, this);
  498. signal_handler_connect(handler, "item_move_down",
  499. OBSBasic::SceneItemMoveDown, this);
  500. signal_handler_connect(handler, "item_move_top",
  501. OBSBasic::SceneItemMoveTop, this);
  502. signal_handler_connect(handler, "item_move_bottom",
  503. OBSBasic::SceneItemMoveBottom, this);
  504. }
  505. void OBSBasic::RemoveScene(OBSSource source)
  506. {
  507. const char *name = obs_source_getname(source);
  508. QListWidgetItem *sel = ui->scenes->currentItem();
  509. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  510. Qt::MatchExactly);
  511. if (sel != nullptr) {
  512. if (items.contains(sel))
  513. ui->sources->clear();
  514. delete sel;
  515. }
  516. }
  517. void OBSBasic::AddSceneItem(OBSSceneItem item)
  518. {
  519. obs_scene_t scene = obs_sceneitem_getscene(item);
  520. obs_source_t source = obs_sceneitem_getsource(item);
  521. if (GetCurrentScene() == scene)
  522. InsertSceneItem(item);
  523. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  524. }
  525. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  526. {
  527. obs_scene_t scene = obs_sceneitem_getscene(item);
  528. if (GetCurrentScene() == scene) {
  529. for (int i = 0; i < ui->sources->count(); i++) {
  530. QListWidgetItem *listItem = ui->sources->item(i);
  531. QVariant userData = listItem->data(Qt::UserRole);
  532. if (userData.value<OBSSceneItem>() == item) {
  533. delete listItem;
  534. break;
  535. }
  536. }
  537. }
  538. obs_source_t source = obs_sceneitem_getsource(item);
  539. int scenes = sourceSceneRefs[source] - 1;
  540. sourceSceneRefs[source] = scenes;
  541. if (scenes == 0) {
  542. obs_source_remove(source);
  543. sourceSceneRefs.erase(source);
  544. }
  545. }
  546. void OBSBasic::UpdateSceneSelection(OBSSource source)
  547. {
  548. if (source) {
  549. obs_source_type type;
  550. obs_source_gettype(source, &type, NULL);
  551. obs_scene_t scene = obs_scene_fromsource(source);
  552. const char *name = obs_source_getname(source);
  553. if (!scene)
  554. return;
  555. QList<QListWidgetItem*> items =
  556. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  557. if (items.count()) {
  558. sceneChanging = true;
  559. ui->scenes->setCurrentItem(items.first());
  560. sceneChanging = false;
  561. UpdateSources(scene);
  562. }
  563. }
  564. }
  565. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  566. const QString &prevName)
  567. {
  568. QList<QListWidgetItem*> items =
  569. listWidget->findItems(prevName, Qt::MatchExactly);
  570. for (int i = 0; i < items.count(); i++)
  571. items[i]->setText(newName);
  572. }
  573. void OBSBasic::RenameSources(QString newName, QString prevName)
  574. {
  575. RenameListValues(ui->scenes, newName, prevName);
  576. RenameListValues(ui->sources, newName, prevName);
  577. }
  578. void OBSBasic::MoveSceneItem(OBSSceneItem item, order_movement movement)
  579. {
  580. OBSScene scene = obs_sceneitem_getscene(item);
  581. if (scene != GetCurrentScene())
  582. return;
  583. int curRow = ui->sources->currentRow();
  584. if (curRow == -1)
  585. return;
  586. QListWidgetItem *listItem = ui->sources->takeItem(curRow);
  587. switch (movement) {
  588. case ORDER_MOVE_UP:
  589. if (curRow > 0)
  590. curRow--;
  591. break;
  592. case ORDER_MOVE_DOWN:
  593. if (curRow < ui->sources->count())
  594. curRow++;
  595. break;
  596. case ORDER_MOVE_TOP:
  597. curRow = 0;
  598. break;
  599. case ORDER_MOVE_BOTTOM:
  600. curRow = ui->sources->count();
  601. break;
  602. }
  603. ui->sources->insertItem(curRow, listItem);
  604. ui->sources->setCurrentRow(curRow);
  605. }
  606. void OBSBasic::ActivateAudioSource(OBSSource source)
  607. {
  608. VolControl *vol = new VolControl(source);
  609. volumes.push_back(vol);
  610. ui->volumeWidgets->layout()->addWidget(vol);
  611. }
  612. void OBSBasic::DeactivateAudioSource(OBSSource source)
  613. {
  614. for (size_t i = 0; i < volumes.size(); i++) {
  615. if (volumes[i]->GetSource() == source) {
  616. delete volumes[i];
  617. volumes.erase(volumes.begin() + i);
  618. break;
  619. }
  620. }
  621. }
  622. /* OBS Callbacks */
  623. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  624. {
  625. OBSBasic *window = static_cast<OBSBasic*>(data);
  626. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  627. QMetaObject::invokeMethod(window, "AddSceneItem",
  628. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  629. }
  630. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  631. {
  632. OBSBasic *window = static_cast<OBSBasic*>(data);
  633. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  634. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  635. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  636. }
  637. void OBSBasic::SourceAdded(void *data, calldata_t params)
  638. {
  639. OBSBasic *window = static_cast<OBSBasic*>(data);
  640. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  641. if (obs_scene_fromsource(source) != NULL)
  642. QMetaObject::invokeMethod(window,
  643. "AddScene",
  644. Q_ARG(OBSSource, OBSSource(source)));
  645. else
  646. window->sourceSceneRefs[source] = 0;
  647. }
  648. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  649. {
  650. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  651. if (obs_scene_fromsource(source) != NULL)
  652. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  653. "RemoveScene",
  654. Q_ARG(OBSSource, OBSSource(source)));
  655. }
  656. void OBSBasic::SourceActivated(void *data, calldata_t params)
  657. {
  658. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  659. uint32_t flags = obs_source_get_output_flags(source);
  660. if (flags & OBS_SOURCE_AUDIO)
  661. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  662. "ActivateAudioSource",
  663. Q_ARG(OBSSource, OBSSource(source)));
  664. }
  665. void OBSBasic::SourceDeactivated(void *data, calldata_t params)
  666. {
  667. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  668. uint32_t flags = obs_source_get_output_flags(source);
  669. if (flags & OBS_SOURCE_AUDIO)
  670. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  671. "DeactivateAudioSource",
  672. Q_ARG(OBSSource, OBSSource(source)));
  673. }
  674. void OBSBasic::SourceRenamed(void *data, calldata_t params)
  675. {
  676. const char *newName = calldata_string(params, "new_name");
  677. const char *prevName = calldata_string(params, "prev_name");
  678. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  679. "RenameSources",
  680. Q_ARG(QString, QT_UTF8(newName)),
  681. Q_ARG(QString, QT_UTF8(prevName)));
  682. }
  683. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  684. {
  685. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  686. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  687. if (channel == 0)
  688. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  689. "UpdateSceneSelection",
  690. Q_ARG(OBSSource, OBSSource(source)));
  691. }
  692. void OBSBasic::DrawBackdrop(float cx, float cy)
  693. {
  694. if (!box)
  695. return;
  696. effect_t solid = obs_get_solid_effect();
  697. eparam_t color = effect_getparambyname(solid, "color");
  698. technique_t tech = effect_gettechnique(solid, "Solid");
  699. vec4 colorVal;
  700. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  701. effect_setvec4(color, &colorVal);
  702. technique_begin(tech);
  703. technique_beginpass(tech, 0);
  704. gs_matrix_push();
  705. gs_matrix_identity();
  706. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  707. gs_load_vertexbuffer(box);
  708. gs_draw(GS_TRISTRIP, 0, 0);
  709. gs_matrix_pop();
  710. technique_endpass(tech);
  711. technique_end(tech);
  712. gs_load_vertexbuffer(nullptr);
  713. }
  714. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  715. {
  716. OBSBasic *window = static_cast<OBSBasic*>(data);
  717. obs_video_info ovi;
  718. obs_get_video_info(&ovi);
  719. window->previewCX = int(window->previewScale * float(ovi.base_width));
  720. window->previewCY = int(window->previewScale * float(ovi.base_height));
  721. gs_viewport_push();
  722. gs_projection_push();
  723. /* --------------------------------------- */
  724. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  725. -100.0f, 100.0f);
  726. gs_setviewport(window->previewX, window->previewY,
  727. window->previewCX, window->previewCY);
  728. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  729. obs_render_main_view();
  730. gs_load_vertexbuffer(nullptr);
  731. /* --------------------------------------- */
  732. float right = float(window->ui->preview->width()) - window->previewX;
  733. float bottom = float(window->ui->preview->height()) - window->previewY;
  734. gs_ortho(-window->previewX, right,
  735. -window->previewY, bottom,
  736. -100.0f, 100.0f);
  737. gs_resetviewport();
  738. window->ui->preview->DrawSceneEditing();
  739. /* --------------------------------------- */
  740. gs_projection_pop();
  741. gs_viewport_pop();
  742. UNUSED_PARAMETER(cx);
  743. UNUSED_PARAMETER(cy);
  744. }
  745. void OBSBasic::SceneItemMoveUp(void *data, calldata_t params)
  746. {
  747. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  748. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  749. "MoveSceneItem",
  750. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  751. Q_ARG(order_movement, ORDER_MOVE_UP));
  752. }
  753. void OBSBasic::SceneItemMoveDown(void *data, calldata_t params)
  754. {
  755. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  756. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  757. "MoveSceneItem",
  758. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  759. Q_ARG(order_movement, ORDER_MOVE_DOWN));
  760. }
  761. void OBSBasic::SceneItemMoveTop(void *data, calldata_t params)
  762. {
  763. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  764. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  765. "MoveSceneItem",
  766. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  767. Q_ARG(order_movement, ORDER_MOVE_TOP));
  768. }
  769. void OBSBasic::SceneItemMoveBottom(void *data, calldata_t params)
  770. {
  771. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  772. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  773. "MoveSceneItem",
  774. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  775. Q_ARG(order_movement, ORDER_MOVE_BOTTOM));
  776. }
  777. /* Main class functions */
  778. obs_service_t OBSBasic::GetService()
  779. {
  780. if (!service)
  781. service = obs_service_create("rtmp_common", NULL, NULL);
  782. return service;
  783. }
  784. void OBSBasic::SetService(obs_service_t newService)
  785. {
  786. if (newService) {
  787. if (service)
  788. obs_service_destroy(service);
  789. service = newService;
  790. }
  791. }
  792. bool OBSBasic::ResetVideo()
  793. {
  794. struct obs_video_info ovi;
  795. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  796. ovi.graphics_module = App()->GetRenderModule();
  797. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  798. "Video", "BaseCX");
  799. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  800. "Video", "BaseCY");
  801. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  802. "Video", "OutputCX");
  803. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  804. "Video", "OutputCY");
  805. ovi.output_format = VIDEO_FORMAT_NV12;
  806. ovi.adapter = 0;
  807. ovi.gpu_conversion = true;
  808. QTToGSWindow(ui->preview->winId(), ovi.window);
  809. //required to make opengl display stuff on osx(?)
  810. ResizePreview(ovi.base_width, ovi.base_height);
  811. QSize size = GetPixelSize(ui->preview);
  812. ovi.window_width = size.width();
  813. ovi.window_height = size.height();
  814. if (!obs_reset_video(&ovi))
  815. return false;
  816. obs_add_draw_callback(OBSBasic::RenderMain, this);
  817. return true;
  818. }
  819. bool OBSBasic::ResetAudio()
  820. {
  821. struct audio_output_info ai;
  822. ai.name = "Main Audio Track";
  823. ai.format = AUDIO_FORMAT_FLOAT;
  824. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  825. "SampleRate");
  826. const char *channelSetupStr = config_get_string(basicConfig,
  827. "Audio", "ChannelSetup");
  828. if (strcmp(channelSetupStr, "Mono") == 0)
  829. ai.speakers = SPEAKERS_MONO;
  830. else
  831. ai.speakers = SPEAKERS_STEREO;
  832. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  833. return obs_reset_audio(&ai);
  834. }
  835. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  836. const char *deviceDesc, int channel)
  837. {
  838. const char *deviceId = config_get_string(basicConfig, "Audio",
  839. deviceName);
  840. obs_source_t source;
  841. obs_data_t settings;
  842. bool same = false;
  843. source = obs_get_output_source(channel);
  844. if (source) {
  845. settings = obs_source_getsettings(source);
  846. const char *curId = obs_data_getstring(settings, "device_id");
  847. same = (strcmp(curId, deviceId) == 0);
  848. obs_data_release(settings);
  849. obs_source_release(source);
  850. }
  851. if (!same)
  852. obs_set_output_source(channel, nullptr);
  853. if (!same && strcmp(deviceId, "disabled") != 0) {
  854. obs_data_t settings = obs_data_create();
  855. obs_data_setstring(settings, "device_id", deviceId);
  856. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  857. sourceId, deviceDesc, settings);
  858. obs_data_release(settings);
  859. obs_set_output_source(channel, source);
  860. obs_source_release(source);
  861. }
  862. }
  863. void OBSBasic::ResetAudioDevices()
  864. {
  865. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1",
  866. Str("Basic.DesktopDevice1"), 1);
  867. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2",
  868. Str("Basic.DesktopDevice2"), 2);
  869. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1",
  870. Str("Basic.AuxDevice1"), 3);
  871. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2",
  872. Str("Basic.AuxDevice2"), 4);
  873. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3",
  874. Str("Basic.AuxDevice3"), 5);
  875. }
  876. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  877. {
  878. QSize targetSize;
  879. /* resize preview panel to fix to the top section of the window */
  880. targetSize = GetPixelSize(ui->preview);
  881. GetScaleAndCenterPos(int(cx), int(cy),
  882. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  883. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  884. previewX, previewY, previewScale);
  885. previewX += float(PREVIEW_EDGE_SIZE);
  886. previewY += float(PREVIEW_EDGE_SIZE);
  887. if (isVisible()) {
  888. if (resizeTimer)
  889. killTimer(resizeTimer);
  890. resizeTimer = startTimer(100);
  891. }
  892. }
  893. void OBSBasic::closeEvent(QCloseEvent *event)
  894. {
  895. QWidget::closeEvent(event);
  896. if (!event->isAccepted())
  897. return;
  898. // remove draw callback in case our drawable surfaces go away before
  899. // the destructor gets called
  900. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  901. }
  902. void OBSBasic::changeEvent(QEvent *event)
  903. {
  904. /* TODO */
  905. UNUSED_PARAMETER(event);
  906. }
  907. void OBSBasic::resizeEvent(QResizeEvent *event)
  908. {
  909. struct obs_video_info ovi;
  910. if (obs_get_video_info(&ovi))
  911. ResizePreview(ovi.base_width, ovi.base_height);
  912. UNUSED_PARAMETER(event);
  913. }
  914. void OBSBasic::timerEvent(QTimerEvent *event)
  915. {
  916. if (event->timerId() == resizeTimer) {
  917. killTimer(resizeTimer);
  918. resizeTimer = 0;
  919. QSize size = GetPixelSize(ui->preview);
  920. obs_resize(size.width(), size.height());
  921. }
  922. }
  923. void OBSBasic::on_action_New_triggered()
  924. {
  925. /* TODO */
  926. }
  927. void OBSBasic::on_action_Open_triggered()
  928. {
  929. /* TODO */
  930. }
  931. void OBSBasic::on_action_Save_triggered()
  932. {
  933. /* TODO */
  934. }
  935. void OBSBasic::on_action_Settings_triggered()
  936. {
  937. OBSBasicSettings settings(this);
  938. settings.exec();
  939. }
  940. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  941. QListWidgetItem *prev)
  942. {
  943. obs_source_t source = NULL;
  944. if (sceneChanging)
  945. return;
  946. if (current) {
  947. obs_scene_t scene;
  948. scene = current->data(Qt::UserRole).value<OBSScene>();
  949. source = obs_scene_getsource(scene);
  950. }
  951. /* TODO: allow transitions */
  952. obs_set_output_source(0, source);
  953. UNUSED_PARAMETER(prev);
  954. }
  955. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  956. {
  957. /* TODO */
  958. UNUSED_PARAMETER(pos);
  959. }
  960. void OBSBasic::on_actionAddScene_triggered()
  961. {
  962. string name;
  963. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  964. int i = 1;
  965. QString placeHolderText = format.arg(i);
  966. obs_source_t source = nullptr;
  967. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  968. obs_source_release(source);
  969. placeHolderText = format.arg(++i);
  970. }
  971. bool accepted = NameDialog::AskForName(this,
  972. QTStr("Basic.Main.AddSceneDlg.Title"),
  973. QTStr("Basic.Main.AddSceneDlg.Text"),
  974. name,
  975. placeHolderText);
  976. if (accepted) {
  977. if (name.empty()) {
  978. QMessageBox::information(this,
  979. QTStr("NoNameEntered"),
  980. QTStr("NoNameEntered"));
  981. on_actionAddScene_triggered();
  982. return;
  983. }
  984. obs_source_t source = obs_get_source_by_name(name.c_str());
  985. if (source) {
  986. QMessageBox::information(this,
  987. QTStr("NameExists.Title"),
  988. QTStr("NameExists.Text"));
  989. obs_source_release(source);
  990. on_actionAddScene_triggered();
  991. return;
  992. }
  993. obs_scene_t scene = obs_scene_create(name.c_str());
  994. source = obs_scene_getsource(scene);
  995. obs_add_source(source);
  996. obs_scene_release(scene);
  997. obs_set_output_source(0, source);
  998. }
  999. }
  1000. void OBSBasic::on_actionRemoveScene_triggered()
  1001. {
  1002. QListWidgetItem *item = ui->scenes->currentItem();
  1003. if (!item)
  1004. return;
  1005. QVariant userData = item->data(Qt::UserRole);
  1006. obs_scene_t scene = userData.value<OBSScene>();
  1007. obs_source_t source = obs_scene_getsource(scene);
  1008. obs_source_remove(source);
  1009. }
  1010. void OBSBasic::on_actionSceneProperties_triggered()
  1011. {
  1012. /* TODO */
  1013. }
  1014. void OBSBasic::on_actionSceneUp_triggered()
  1015. {
  1016. /* TODO */
  1017. }
  1018. void OBSBasic::on_actionSceneDown_triggered()
  1019. {
  1020. /* TODO */
  1021. }
  1022. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  1023. QListWidgetItem *prev)
  1024. {
  1025. auto select_one = [] (obs_scene_t scene, obs_sceneitem_t item,
  1026. void *param)
  1027. {
  1028. obs_sceneitem_t selectedItem =
  1029. *reinterpret_cast<OBSSceneItem*>(param);
  1030. obs_sceneitem_select(item, (selectedItem == item));
  1031. UNUSED_PARAMETER(scene);
  1032. return true;
  1033. };
  1034. if (!current)
  1035. return;
  1036. OBSSceneItem item = current->data(Qt::UserRole).value<OBSSceneItem>();
  1037. obs_scene_enum_items(GetCurrentScene(), select_one, &item);
  1038. UNUSED_PARAMETER(prev);
  1039. }
  1040. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  1041. {
  1042. /* TODO */
  1043. UNUSED_PARAMETER(pos);
  1044. }
  1045. void OBSBasic::AddSource(const char *id)
  1046. {
  1047. if (id && *id) {
  1048. OBSBasicSourceSelect sourceSelect(this, id);
  1049. sourceSelect.exec();
  1050. }
  1051. }
  1052. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  1053. {
  1054. const char *type;
  1055. bool foundValues = false;
  1056. size_t idx = 0;
  1057. QMenu *popup = new QMenu(QTStr("Add"));
  1058. while (obs_enum_input_types(idx++, &type)) {
  1059. const char *name = obs_source_getdisplayname(
  1060. OBS_SOURCE_TYPE_INPUT, type);
  1061. if (strcmp(type, "scene") == 0)
  1062. continue;
  1063. QAction *popupItem = new QAction(QT_UTF8(name), this);
  1064. popupItem->setData(QT_UTF8(type));
  1065. connect(popupItem, SIGNAL(triggered(bool)),
  1066. this, SLOT(AddSourceFromAction()));
  1067. popup->addAction(popupItem);
  1068. foundValues = true;
  1069. }
  1070. if (!foundValues) {
  1071. delete popup;
  1072. popup = nullptr;
  1073. }
  1074. return popup;
  1075. }
  1076. void OBSBasic::AddSourceFromAction()
  1077. {
  1078. QAction *action = qobject_cast<QAction*>(sender());
  1079. if (!action)
  1080. return;
  1081. AddSource(QT_TO_UTF8(action->data().toString()));
  1082. }
  1083. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  1084. {
  1085. if (!GetCurrentScene()) {
  1086. // Tell the user he needs a scene first (help beginners).
  1087. QMessageBox::information(this,
  1088. QTStr("Basic.Main.AddSourceHelp.Title"),
  1089. QTStr("Basic.Main.AddSourceHelp.Text"));
  1090. return;
  1091. }
  1092. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  1093. if (popup)
  1094. popup->exec(pos);
  1095. }
  1096. void OBSBasic::on_actionAddSource_triggered()
  1097. {
  1098. AddSourcePopupMenu(QCursor::pos());
  1099. }
  1100. void OBSBasic::on_actionRemoveSource_triggered()
  1101. {
  1102. OBSSceneItem item = GetCurrentSceneItem();
  1103. if (item)
  1104. obs_sceneitem_remove(item);
  1105. }
  1106. void OBSBasic::on_actionSourceProperties_triggered()
  1107. {
  1108. OBSSceneItem item = GetCurrentSceneItem();
  1109. OBSSource source = obs_sceneitem_getsource(item);
  1110. if (source) {
  1111. delete properties;
  1112. properties = new OBSBasicProperties(this, source);
  1113. properties->Init();
  1114. }
  1115. }
  1116. void OBSBasic::on_actionSourceUp_triggered()
  1117. {
  1118. OBSSceneItem item = GetCurrentSceneItem();
  1119. obs_sceneitem_setorder(item, ORDER_MOVE_UP);
  1120. }
  1121. void OBSBasic::on_actionSourceDown_triggered()
  1122. {
  1123. OBSSceneItem item = GetCurrentSceneItem();
  1124. obs_sceneitem_setorder(item, ORDER_MOVE_DOWN);
  1125. }
  1126. static char *ReadLogFile(const char *log)
  1127. {
  1128. BPtr<char> logDir(os_get_config_path("obs-studio/logs"));
  1129. string path = (char*)logDir;
  1130. path += "/";
  1131. path += log;
  1132. char *file = os_quick_read_utf8_file(path.c_str());
  1133. if (!file)
  1134. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  1135. return file;
  1136. }
  1137. void OBSBasic::UploadLog(const char *file)
  1138. {
  1139. dstr fileString = {};
  1140. stringstream ss;
  1141. string jsonData;
  1142. dstr_move_array(&fileString, ReadLogFile(file));
  1143. if (!fileString.array)
  1144. return;
  1145. if (!*fileString.array) {
  1146. dstr_free(&fileString);
  1147. return;
  1148. }
  1149. ui->menuLogFiles->setEnabled(false);
  1150. dstr_replace(&fileString, "\\", "\\\\");
  1151. dstr_replace(&fileString, "\"", "\\\"");
  1152. dstr_replace(&fileString, "\n", "\\n");
  1153. dstr_replace(&fileString, "\r", "\\r");
  1154. dstr_replace(&fileString, "\t", "\\t");
  1155. dstr_replace(&fileString, "\t", "\\t");
  1156. dstr_replace(&fileString, "/", "\\/");
  1157. ss << "{ \"public\": false, \"description\": \"OBS " <<
  1158. App()->GetVersionString() << " log file uploaded at " <<
  1159. CurrentDateTimeString() << "\", \"files\": { \"" <<
  1160. file << "\": { \"content\": \"" <<
  1161. fileString.array << "\" } } }";
  1162. jsonData = std::move(ss.str());
  1163. logUploadPostData.setData(jsonData.c_str(), (int)jsonData.size());
  1164. QUrl url("https://api.github.com/gists");
  1165. logUploadReply = networkManager.post(QNetworkRequest(url),
  1166. &logUploadPostData);
  1167. connect(logUploadReply, SIGNAL(finished()),
  1168. this, SLOT(logUploadFinished()));
  1169. connect(logUploadReply, SIGNAL(readyRead()),
  1170. this, SLOT(logUploadRead()));
  1171. dstr_free(&fileString);
  1172. }
  1173. void OBSBasic::on_actionUploadCurrentLog_triggered()
  1174. {
  1175. UploadLog(App()->GetCurrentLog());
  1176. }
  1177. void OBSBasic::on_actionUploadLastLog_triggered()
  1178. {
  1179. UploadLog(App()->GetLastLog());
  1180. }
  1181. void OBSBasic::logUploadRead()
  1182. {
  1183. logUploadReturnData.push_back(logUploadReply->readAll());
  1184. }
  1185. void OBSBasic::logUploadFinished()
  1186. {
  1187. ui->menuLogFiles->setEnabled(true);
  1188. if (logUploadReply->error()) {
  1189. QMessageBox::information(this,
  1190. QTStr("LogReturnDialog.ErrorUploadingLog"),
  1191. logUploadReply->errorString());
  1192. return;
  1193. }
  1194. const char *jsonReply = logUploadReturnData.constData();
  1195. if (!jsonReply || !*jsonReply)
  1196. return;
  1197. obs_data_t returnData = obs_data_create_from_json(jsonReply);
  1198. QString logURL = obs_data_getstring(returnData, "html_url");
  1199. obs_data_release(returnData);
  1200. OBSLogReply logDialog(this, logURL);
  1201. logDialog.exec();
  1202. }
  1203. void OBSBasic::SceneNameEdited(QWidget *editor,
  1204. QAbstractItemDelegate::EndEditHint endHint)
  1205. {
  1206. OBSScene scene = GetCurrentScene();
  1207. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  1208. string text = QT_TO_UTF8(edit->text());
  1209. if (!scene)
  1210. return;
  1211. obs_source_t sceneSource = obs_scene_getsource(scene);
  1212. const char *prevName = obs_source_getname(sceneSource);
  1213. obs_source_t source = obs_get_source_by_name(text.c_str());
  1214. if (source) {
  1215. QListWidgetItem *listItem = ui->scenes->currentItem();
  1216. if (listItem)
  1217. listItem->setText(QT_UTF8(prevName));
  1218. obs_source_release(source);
  1219. } else {
  1220. obs_source_setname(sceneSource, text.c_str());
  1221. }
  1222. UNUSED_PARAMETER(endHint);
  1223. }
  1224. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  1225. QAbstractItemDelegate::EndEditHint endHint)
  1226. {
  1227. OBSSceneItem item = GetCurrentSceneItem();
  1228. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  1229. string text = QT_TO_UTF8(edit->text());
  1230. if (!item)
  1231. return;
  1232. obs_source_t itemSource = obs_sceneitem_getsource(item);
  1233. const char *prevName = obs_source_getname(itemSource);
  1234. obs_source_t source = obs_get_source_by_name(text.c_str());
  1235. if (source) {
  1236. QListWidgetItem *listItem = ui->sources->currentItem();
  1237. if (listItem)
  1238. listItem->setText(QT_UTF8(prevName));
  1239. obs_source_release(source);
  1240. } else {
  1241. obs_source_setname(itemSource, text.c_str());
  1242. }
  1243. UNUSED_PARAMETER(endHint);
  1244. }
  1245. void OBSBasic::StreamingStart()
  1246. {
  1247. ui->streamButton->setText("Stop Streaming");
  1248. ui->streamButton->setEnabled(true);
  1249. }
  1250. void OBSBasic::StreamingStop(int code)
  1251. {
  1252. const char *errorMessage;
  1253. switch (code) {
  1254. case OBS_OUTPUT_BAD_PATH:
  1255. errorMessage = Str("Output.ConnectFail.BadPath");
  1256. break;
  1257. case OBS_OUTPUT_CONNECT_FAILED:
  1258. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  1259. break;
  1260. case OBS_OUTPUT_INVALID_STREAM:
  1261. errorMessage = Str("Output.ConnectFail.InvalidStream");
  1262. break;
  1263. default:
  1264. case OBS_OUTPUT_ERROR:
  1265. errorMessage = Str("Output.ConnectFail.Error");
  1266. break;
  1267. case OBS_OUTPUT_DISCONNECTED:
  1268. /* doesn't happen if output is set to reconnect. note that
  1269. * reconnects are handled in the output, not in the UI */
  1270. errorMessage = Str("Output.ConnectFail.Disconnected");
  1271. }
  1272. activeRefs--;
  1273. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  1274. ui->streamButton->setEnabled(true);
  1275. if (code != OBS_OUTPUT_SUCCESS)
  1276. QMessageBox::information(this,
  1277. QTStr("Output.ConnectFail.Title"),
  1278. QT_UTF8(errorMessage));
  1279. }
  1280. void OBSBasic::RecordingStop()
  1281. {
  1282. activeRefs--;
  1283. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  1284. }
  1285. void OBSBasic::SetupEncoders()
  1286. {
  1287. if (activeRefs == 0) {
  1288. obs_data_t x264Settings = obs_data_create();
  1289. obs_data_t aacSettings = obs_data_create();
  1290. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  1291. "VBitrate");
  1292. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  1293. "ABitrate");
  1294. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  1295. obs_data_setbool(x264Settings, "cbr", true);
  1296. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  1297. obs_encoder_update(x264, x264Settings);
  1298. obs_encoder_update(aac, aacSettings);
  1299. obs_data_release(x264Settings);
  1300. obs_data_release(aacSettings);
  1301. obs_encoder_set_video(x264, obs_video());
  1302. obs_encoder_set_audio(aac, obs_audio());
  1303. }
  1304. }
  1305. void OBSBasic::on_streamButton_clicked()
  1306. {
  1307. if (obs_output_active(streamOutput)) {
  1308. obs_output_stop(streamOutput);
  1309. } else {
  1310. SaveService();
  1311. SetupEncoders();
  1312. obs_output_set_video_encoder(streamOutput, x264);
  1313. obs_output_set_audio_encoder(streamOutput, aac);
  1314. obs_output_set_service(streamOutput, service);
  1315. if (obs_output_start(streamOutput)) {
  1316. activeRefs++;
  1317. ui->streamButton->setEnabled(false);
  1318. ui->streamButton->setText(
  1319. QTStr("Basic.Main.Connecting"));
  1320. }
  1321. }
  1322. }
  1323. void OBSBasic::on_recordButton_clicked()
  1324. {
  1325. if (obs_output_active(fileOutput)) {
  1326. obs_output_stop(fileOutput);
  1327. } else {
  1328. const char *path = config_get_string(basicConfig,
  1329. "SimpleOutput", "FilePath");
  1330. os_dir_t dir = path ? os_opendir(path) : nullptr;
  1331. if (!dir) {
  1332. QMessageBox::information(this,
  1333. QTStr("Output.BadPath.Title"),
  1334. QTStr("Output.BadPath.Text"));
  1335. return;
  1336. }
  1337. os_closedir(dir);
  1338. string strPath;
  1339. strPath += path;
  1340. char lastChar = strPath.back();
  1341. if (lastChar != '/' && lastChar != '\\')
  1342. strPath += "/";
  1343. strPath += GenerateTimeDateFilename("flv");
  1344. SetupEncoders();
  1345. obs_output_set_video_encoder(fileOutput, x264);
  1346. obs_output_set_audio_encoder(fileOutput, aac);
  1347. obs_data_t settings = obs_data_create();
  1348. obs_data_setstring(settings, "path", strPath.c_str());
  1349. obs_output_update(fileOutput, settings);
  1350. obs_data_release(settings);
  1351. if (obs_output_start(fileOutput)) {
  1352. activeRefs++;
  1353. ui->recordButton->setText(
  1354. QTStr("Basic.Main.StopRecording"));
  1355. }
  1356. }
  1357. }
  1358. void OBSBasic::on_settingsButton_clicked()
  1359. {
  1360. OBSBasicSettings settings(this);
  1361. settings.exec();
  1362. }
  1363. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  1364. {
  1365. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  1366. if (strcmp(val, "10") == 0) {
  1367. num = 10;
  1368. den = 1;
  1369. } else if (strcmp(val, "20") == 0) {
  1370. num = 20;
  1371. den = 1;
  1372. } else if (strcmp(val, "25") == 0) {
  1373. num = 25;
  1374. den = 1;
  1375. } else if (strcmp(val, "29.97") == 0) {
  1376. num = 30000;
  1377. den = 1001;
  1378. } else if (strcmp(val, "48") == 0) {
  1379. num = 48;
  1380. den = 1;
  1381. } else if (strcmp(val, "59.94") == 0) {
  1382. num = 60000;
  1383. den = 1001;
  1384. } else if (strcmp(val, "60") == 0) {
  1385. num = 60;
  1386. den = 1;
  1387. } else {
  1388. num = 30;
  1389. den = 1;
  1390. }
  1391. }
  1392. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  1393. {
  1394. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  1395. den = 1;
  1396. }
  1397. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  1398. {
  1399. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  1400. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  1401. }
  1402. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  1403. {
  1404. num = 1000000000;
  1405. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  1406. }
  1407. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  1408. {
  1409. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  1410. if (type == 1) //"Integer"
  1411. GetFPSInteger(num, den);
  1412. else if (type == 2) //"Fraction"
  1413. GetFPSFraction(num, den);
  1414. else if (false) //"Nanoseconds", currently not implemented
  1415. GetFPSNanoseconds(num, den);
  1416. else
  1417. GetFPSCommon(num, den);
  1418. }
  1419. config_t OBSBasic::Config() const
  1420. {
  1421. return basicConfig;
  1422. }
  1423. void OBSBasic::on_actionEditTransform_triggered()
  1424. {
  1425. delete transformWindow;
  1426. transformWindow = new OBSBasicTransform(this);
  1427. transformWindow->show();
  1428. }
  1429. void OBSBasic::on_actionResetTransform_triggered()
  1430. {
  1431. auto func = [] (obs_scene_t scene, obs_sceneitem_t item, void *param)
  1432. {
  1433. if (!obs_sceneitem_selected(item))
  1434. return true;
  1435. obs_sceneitem_info info;
  1436. vec2_set(&info.pos, 0.0f, 0.0f);
  1437. vec2_set(&info.scale, 1.0f, 1.0f);
  1438. info.rot = 0.0f;
  1439. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1440. info.bounds_type = OBS_BOUNDS_NONE;
  1441. info.bounds_alignment = OBS_ALIGN_CENTER;
  1442. vec2_set(&info.bounds, 0.0f, 0.0f);
  1443. obs_sceneitem_set_info(item, &info);
  1444. UNUSED_PARAMETER(scene);
  1445. UNUSED_PARAMETER(param);
  1446. return true;
  1447. };
  1448. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  1449. }
  1450. static void GetItemBox(obs_sceneitem_t item, vec3 &tl, vec3 &br)
  1451. {
  1452. matrix4 boxTransform;
  1453. obs_sceneitem_get_box_transform(item, &boxTransform);
  1454. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  1455. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  1456. auto GetMinPos = [&] (float x, float y)
  1457. {
  1458. vec3 pos;
  1459. vec3_set(&pos, x, y, 0.0f);
  1460. vec3_transform(&pos, &pos, &boxTransform);
  1461. vec3_min(&tl, &tl, &pos);
  1462. vec3_max(&br, &br, &pos);
  1463. };
  1464. GetMinPos(0.0f, 0.0f);
  1465. GetMinPos(1.0f, 0.0f);
  1466. GetMinPos(0.0f, 1.0f);
  1467. GetMinPos(1.0f, 1.0f);
  1468. }
  1469. static vec3 GetItemTL(obs_sceneitem_t item)
  1470. {
  1471. vec3 tl, br;
  1472. GetItemBox(item, tl, br);
  1473. return tl;
  1474. }
  1475. static void SetItemTL(obs_sceneitem_t item, const vec3 &tl)
  1476. {
  1477. vec3 newTL;
  1478. vec2 pos;
  1479. obs_sceneitem_getpos(item, &pos);
  1480. newTL = GetItemTL(item);
  1481. pos.x += tl.x - newTL.x;
  1482. pos.y += tl.y - newTL.y;
  1483. obs_sceneitem_setpos(item, &pos);
  1484. }
  1485. static bool RotateSelectedSources(obs_scene_t scene, obs_sceneitem_t item,
  1486. void *param)
  1487. {
  1488. if (!obs_sceneitem_selected(item))
  1489. return true;
  1490. float rot = *reinterpret_cast<float*>(param);
  1491. vec3 tl = GetItemTL(item);
  1492. rot += obs_sceneitem_getrot(item);
  1493. if (rot >= 360.0f) rot -= 360.0f;
  1494. else if (rot <= -360.0f) rot += 360.0f;
  1495. obs_sceneitem_setrot(item, rot);
  1496. SetItemTL(item, tl);
  1497. UNUSED_PARAMETER(scene);
  1498. UNUSED_PARAMETER(param);
  1499. return true;
  1500. };
  1501. void OBSBasic::on_actionRotate90CW_triggered()
  1502. {
  1503. float f90CW = 90.0f;
  1504. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  1505. }
  1506. void OBSBasic::on_actionRotate90CCW_triggered()
  1507. {
  1508. float f90CCW = -90.0f;
  1509. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  1510. }
  1511. void OBSBasic::on_actionRotate180_triggered()
  1512. {
  1513. float f180 = 180.0f;
  1514. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  1515. }
  1516. static bool MultiplySelectedItemScale(obs_scene_t scene, obs_sceneitem_t item,
  1517. void *param)
  1518. {
  1519. vec2 &mul = *reinterpret_cast<vec2*>(param);
  1520. if (!obs_sceneitem_selected(item))
  1521. return true;
  1522. vec3 tl = GetItemTL(item);
  1523. vec2 scale;
  1524. obs_sceneitem_getscale(item, &scale);
  1525. vec2_mul(&scale, &scale, &mul);
  1526. obs_sceneitem_setscale(item, &scale);
  1527. SetItemTL(item, tl);
  1528. UNUSED_PARAMETER(scene);
  1529. return true;
  1530. }
  1531. void OBSBasic::on_actionFlipHorizontal_triggered()
  1532. {
  1533. vec2 scale;
  1534. vec2_set(&scale, -1.0f, 1.0f);
  1535. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  1536. &scale);
  1537. }
  1538. void OBSBasic::on_actionFlipVertical_triggered()
  1539. {
  1540. vec2 scale;
  1541. vec2_set(&scale, 1.0f, -1.0f);
  1542. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  1543. &scale);
  1544. }
  1545. static bool CenterAlignSelectedItems(obs_scene_t scene, obs_sceneitem_t item,
  1546. void *param)
  1547. {
  1548. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  1549. if (!obs_sceneitem_selected(item))
  1550. return true;
  1551. obs_video_info ovi;
  1552. obs_get_video_info(&ovi);
  1553. obs_sceneitem_info itemInfo;
  1554. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  1555. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  1556. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  1557. itemInfo.rot = 0.0f;
  1558. vec2_set(&itemInfo.bounds,
  1559. float(ovi.base_width), float(ovi.base_height));
  1560. itemInfo.bounds_type = boundsType;
  1561. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  1562. obs_sceneitem_set_info(item, &itemInfo);
  1563. UNUSED_PARAMETER(scene);
  1564. return true;
  1565. }
  1566. void OBSBasic::on_actionFitToScreen_triggered()
  1567. {
  1568. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  1569. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  1570. &boundsType);
  1571. }
  1572. void OBSBasic::on_actionStretchToScreen_triggered()
  1573. {
  1574. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  1575. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  1576. &boundsType);
  1577. }
  1578. void OBSBasic::on_actionCenterToScreen_triggered()
  1579. {
  1580. auto func = [] (obs_scene_t scene, obs_sceneitem_t item, void *param)
  1581. {
  1582. vec3 tl, br, itemCenter, screenCenter, offset;
  1583. obs_video_info ovi;
  1584. if (!obs_sceneitem_selected(item))
  1585. return true;
  1586. obs_get_video_info(&ovi);
  1587. vec3_set(&screenCenter, float(ovi.base_width),
  1588. float(ovi.base_height), 0.0f);
  1589. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  1590. GetItemBox(item, tl, br);
  1591. vec3_sub(&itemCenter, &br, &tl);
  1592. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  1593. vec3_add(&itemCenter, &itemCenter, &tl);
  1594. vec3_sub(&offset, &screenCenter, &itemCenter);
  1595. vec3_add(&tl, &tl, &offset);
  1596. SetItemTL(item, tl);
  1597. UNUSED_PARAMETER(scene);
  1598. UNUSED_PARAMETER(param);
  1599. return true;
  1600. };
  1601. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  1602. }