window-basic-main.cpp 81 KB

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