window-basic-main.cpp 65 KB

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