window-basic-main.cpp 67 KB

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