window-basic-main.cpp 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312
  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 <QDesktopWidget>
  23. #include <util/dstr.h>
  24. #include <util/util.hpp>
  25. #include <util/platform.h>
  26. #include <util/profiler.hpp>
  27. #include <graphics/math-defs.h>
  28. #include "obs-app.hpp"
  29. #include "platform.hpp"
  30. #include "visibility-item-widget.hpp"
  31. #include "item-widget-helpers.hpp"
  32. #include "window-basic-settings.hpp"
  33. #include "window-namedialog.hpp"
  34. #include "window-basic-source-select.hpp"
  35. #include "window-basic-main.hpp"
  36. #include "window-basic-main-outputs.hpp"
  37. #include "window-basic-properties.hpp"
  38. #include "window-log-reply.hpp"
  39. #include "window-projector.hpp"
  40. #include "window-remux.hpp"
  41. #include "qt-wrappers.hpp"
  42. #include "display-helpers.hpp"
  43. #include "volume-control.hpp"
  44. #include "remote-text.hpp"
  45. #include "ui_OBSBasic.h"
  46. #include <fstream>
  47. #include <sstream>
  48. #include <QScreen>
  49. #include <QWindow>
  50. using namespace std;
  51. namespace {
  52. template <typename OBSRef>
  53. struct SignalContainer {
  54. OBSRef ref;
  55. vector<shared_ptr<OBSSignal>> handlers;
  56. };
  57. }
  58. Q_DECLARE_METATYPE(OBSScene);
  59. Q_DECLARE_METATYPE(OBSSceneItem);
  60. Q_DECLARE_METATYPE(OBSSource);
  61. Q_DECLARE_METATYPE(obs_order_movement);
  62. Q_DECLARE_METATYPE(SignalContainer<OBSScene>);
  63. template <typename T>
  64. static T GetOBSRef(QListWidgetItem *item)
  65. {
  66. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  67. }
  68. template <typename T>
  69. static void SetOBSRef(QListWidgetItem *item, T &&val)
  70. {
  71. item->setData(static_cast<int>(QtDataRole::OBSRef),
  72. QVariant::fromValue(val));
  73. }
  74. static void AddExtraModulePaths()
  75. {
  76. char base_module_dir[512];
  77. #if defined(_WIN32) || defined(__APPLE__)
  78. int ret = GetProgramDataPath(base_module_dir, sizeof(base_module_dir),
  79. "obs-studio/plugins/%module%");
  80. #else
  81. int ret = GetConfigPath(base_module_dir, sizeof(base_module_dir),
  82. "obs-studio/plugins/%module%");
  83. #endif
  84. if (ret <= 0)
  85. return;
  86. string path = (char*)base_module_dir;
  87. #if defined(__APPLE__)
  88. obs_add_module_path((path + "/bin").c_str(), (path + "/data").c_str());
  89. #elif ARCH_BITS == 64
  90. obs_add_module_path((path + "/bin/64bit").c_str(),
  91. (path + "/data").c_str());
  92. #else
  93. obs_add_module_path((path + "/bin/32bit").c_str(),
  94. (path + "/data").c_str());
  95. #endif
  96. }
  97. static QList<QKeySequence> DeleteKeys;
  98. OBSBasic::OBSBasic(QWidget *parent)
  99. : OBSMainWindow (parent),
  100. ui (new Ui::OBSBasic)
  101. {
  102. ui->setupUi(this);
  103. ui->previewDisabledLabel->setVisible(false);
  104. copyActionsDynamicProperties();
  105. ui->sources->setItemDelegate(new VisibilityItemDelegate(ui->sources));
  106. int width = config_get_int(App()->GlobalConfig(), "BasicWindow", "cx");
  107. // Check if no values are saved (new installation).
  108. if (width != 0) {
  109. int height = config_get_int(App()->GlobalConfig(),
  110. "BasicWindow", "cy");
  111. int posx = config_get_int(App()->GlobalConfig(), "BasicWindow",
  112. "posx");
  113. int posy = config_get_int(App()->GlobalConfig(), "BasicWindow",
  114. "posy");
  115. if (!WindowPositionValid(posx, posy)) {
  116. QRect rect = App()->desktop()->availableGeometry();
  117. setGeometry(QStyle::alignedRect(
  118. Qt::LeftToRight,
  119. Qt::AlignCenter,
  120. size(), rect));
  121. } else {
  122. setGeometry(posx, posy, width, height);
  123. }
  124. }
  125. char styleSheetPath[512];
  126. int ret = GetProfilePath(styleSheetPath, sizeof(styleSheetPath),
  127. "stylesheet.qss");
  128. if (ret > 0) {
  129. if (QFile::exists(styleSheetPath)) {
  130. QString path = QString("file:///") +
  131. QT_UTF8(styleSheetPath);
  132. App()->setStyleSheet(path);
  133. }
  134. }
  135. qRegisterMetaType<OBSScene> ("OBSScene");
  136. qRegisterMetaType<OBSSceneItem>("OBSSceneItem");
  137. qRegisterMetaType<OBSSource> ("OBSSource");
  138. qRegisterMetaType<obs_hotkey_id>("obs_hotkey_id");
  139. qRegisterMetaTypeStreamOperators<
  140. std::vector<std::shared_ptr<OBSSignal>>>(
  141. "std::vector<std::shared_ptr<OBSSignal>>");
  142. qRegisterMetaTypeStreamOperators<OBSScene>("OBSScene");
  143. qRegisterMetaTypeStreamOperators<OBSSceneItem>("OBSSceneItem");
  144. ui->scenes->setAttribute(Qt::WA_MacShowFocusRect, false);
  145. ui->sources->setAttribute(Qt::WA_MacShowFocusRect, false);
  146. auto displayResize = [this]() {
  147. struct obs_video_info ovi;
  148. if (obs_get_video_info(&ovi))
  149. ResizePreview(ovi.base_width, ovi.base_height);
  150. };
  151. connect(windowHandle(), &QWindow::screenChanged, displayResize);
  152. connect(ui->preview, &OBSQTDisplay::DisplayResized, displayResize);
  153. installEventFilter(CreateShortcutFilter());
  154. stringstream name;
  155. name << "OBS " << App()->GetVersionString();
  156. blog(LOG_INFO, "%s", name.str().c_str());
  157. blog(LOG_INFO, "---------------------------------");
  158. UpdateTitleBar();
  159. connect(ui->scenes->itemDelegate(),
  160. SIGNAL(closeEditor(QWidget*,
  161. QAbstractItemDelegate::EndEditHint)),
  162. this,
  163. SLOT(SceneNameEdited(QWidget*,
  164. QAbstractItemDelegate::EndEditHint)));
  165. connect(ui->sources->itemDelegate(),
  166. SIGNAL(closeEditor(QWidget*,
  167. QAbstractItemDelegate::EndEditHint)),
  168. this,
  169. SLOT(SceneItemNameEdited(QWidget*,
  170. QAbstractItemDelegate::EndEditHint)));
  171. cpuUsageInfo = os_cpu_usage_info_start();
  172. cpuUsageTimer = new QTimer(this);
  173. connect(cpuUsageTimer, SIGNAL(timeout()),
  174. ui->statusbar, SLOT(UpdateCPUUsage()));
  175. cpuUsageTimer->start(3000);
  176. DeleteKeys =
  177. #ifdef __APPLE__
  178. QList<QKeySequence>{{Qt::Key_Backspace}} <<
  179. #endif
  180. QKeySequence::keyBindings(QKeySequence::Delete);
  181. #ifdef __APPLE__
  182. ui->actionRemoveSource->setShortcuts(DeleteKeys);
  183. ui->actionRemoveScene->setShortcuts(DeleteKeys);
  184. ui->action_Settings->setMenuRole(QAction::PreferencesRole);
  185. ui->actionE_xit->setMenuRole(QAction::QuitRole);
  186. #endif
  187. auto addNudge = [this](const QKeySequence &seq, const char *s)
  188. {
  189. QAction *nudge = new QAction(ui->preview);
  190. nudge->setShortcut(seq);
  191. nudge->setShortcutContext(Qt::WidgetShortcut);
  192. ui->preview->addAction(nudge);
  193. connect(nudge, SIGNAL(triggered()), this, s);
  194. };
  195. addNudge(Qt::Key_Up, SLOT(NudgeUp()));
  196. addNudge(Qt::Key_Down, SLOT(NudgeDown()));
  197. addNudge(Qt::Key_Left, SLOT(NudgeLeft()));
  198. addNudge(Qt::Key_Right, SLOT(NudgeRight()));
  199. }
  200. static void SaveAudioDevice(const char *name, int channel, obs_data_t *parent,
  201. vector<OBSSource> &audioSources)
  202. {
  203. obs_source_t *source = obs_get_output_source(channel);
  204. if (!source)
  205. return;
  206. audioSources.push_back(source);
  207. obs_data_t *data = obs_save_source(source);
  208. obs_data_set_obj(parent, name, data);
  209. obs_data_release(data);
  210. obs_source_release(source);
  211. }
  212. static obs_data_t *GenerateSaveData(obs_data_array_t *sceneOrder,
  213. obs_data_array_t *quickTransitionData, int transitionDuration,
  214. obs_data_array_t *transitions,
  215. OBSScene &scene, OBSSource &curProgramScene)
  216. {
  217. obs_data_t *saveData = obs_data_create();
  218. vector<OBSSource> audioSources;
  219. audioSources.reserve(5);
  220. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData, audioSources);
  221. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData, audioSources);
  222. SaveAudioDevice(AUX_AUDIO_1, 3, saveData, audioSources);
  223. SaveAudioDevice(AUX_AUDIO_2, 4, saveData, audioSources);
  224. SaveAudioDevice(AUX_AUDIO_3, 5, saveData, audioSources);
  225. auto FilterAudioSources = [&](obs_source_t *source)
  226. {
  227. return find(begin(audioSources), end(audioSources), source) ==
  228. end(audioSources);
  229. };
  230. using FilterAudioSources_t = decltype(FilterAudioSources);
  231. obs_data_array_t *sourcesArray = obs_save_sources_filtered(
  232. [](void *data, obs_source_t *source)
  233. {
  234. return (*static_cast<FilterAudioSources_t*>(data))(source);
  235. }, static_cast<void*>(&FilterAudioSources));
  236. obs_source_t *transition = obs_get_output_source(0);
  237. obs_source_t *currentScene = obs_scene_get_source(scene);
  238. const char *sceneName = obs_source_get_name(currentScene);
  239. const char *programName = obs_source_get_name(curProgramScene);
  240. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  241. "Basic", "SceneCollection");
  242. obs_data_set_string(saveData, "current_scene", sceneName);
  243. obs_data_set_string(saveData, "current_program_scene", programName);
  244. obs_data_set_array(saveData, "scene_order", sceneOrder);
  245. obs_data_set_string(saveData, "name", sceneCollection);
  246. obs_data_set_array(saveData, "sources", sourcesArray);
  247. obs_data_set_array(saveData, "quick_transitions", quickTransitionData);
  248. obs_data_set_array(saveData, "transitions", transitions);
  249. obs_data_array_release(sourcesArray);
  250. obs_data_set_string(saveData, "current_transition",
  251. obs_source_get_name(transition));
  252. obs_data_set_int(saveData, "transition_duration", transitionDuration);
  253. obs_source_release(transition);
  254. return saveData;
  255. }
  256. void OBSBasic::copyActionsDynamicProperties()
  257. {
  258. // Themes need the QAction dynamic properties
  259. for (QAction *x : ui->scenesToolbar->actions()) {
  260. QWidget* temp = ui->scenesToolbar->widgetForAction(x);
  261. for (QByteArray &y : x->dynamicPropertyNames()) {
  262. temp->setProperty(y, x->property(y));
  263. }
  264. }
  265. for (QAction *x : ui->sourcesToolbar->actions()) {
  266. QWidget* temp = ui->sourcesToolbar->widgetForAction(x);
  267. for (QByteArray &y : x->dynamicPropertyNames()) {
  268. temp->setProperty(y, x->property(y));
  269. }
  270. }
  271. }
  272. void OBSBasic::ClearVolumeControls()
  273. {
  274. VolControl *control;
  275. for (size_t i = 0; i < volumes.size(); i++) {
  276. control = volumes[i];
  277. delete control;
  278. }
  279. volumes.clear();
  280. }
  281. obs_data_array_t *OBSBasic::SaveSceneListOrder()
  282. {
  283. obs_data_array_t *sceneOrder = obs_data_array_create();
  284. for (int i = 0; i < ui->scenes->count(); i++) {
  285. obs_data_t *data = obs_data_create();
  286. obs_data_set_string(data, "name",
  287. QT_TO_UTF8(ui->scenes->item(i)->text()));
  288. obs_data_array_push_back(sceneOrder, data);
  289. obs_data_release(data);
  290. }
  291. return sceneOrder;
  292. }
  293. void OBSBasic::Save(const char *file)
  294. {
  295. OBSScene scene = GetCurrentScene();
  296. OBSSource curProgramScene = OBSGetStrongRef(programScene);
  297. if (!curProgramScene)
  298. curProgramScene = obs_scene_get_source(scene);
  299. obs_data_array_t *sceneOrder = SaveSceneListOrder();
  300. obs_data_array_t *transitions = SaveTransitions();
  301. obs_data_array_t *quickTrData = SaveQuickTransitions();
  302. obs_data_t *saveData = GenerateSaveData(sceneOrder, quickTrData,
  303. ui->transitionDuration->value(), transitions,
  304. scene, curProgramScene);
  305. if (!obs_data_save_json_safe(saveData, file, "tmp", "bak"))
  306. blog(LOG_ERROR, "Could not save scene data to %s", file);
  307. obs_data_release(saveData);
  308. obs_data_array_release(sceneOrder);
  309. obs_data_array_release(quickTrData);
  310. obs_data_array_release(transitions);
  311. }
  312. static void LoadAudioDevice(const char *name, int channel, obs_data_t *parent)
  313. {
  314. obs_data_t *data = obs_data_get_obj(parent, name);
  315. if (!data)
  316. return;
  317. obs_source_t *source = obs_load_source(data);
  318. if (source) {
  319. obs_set_output_source(channel, source);
  320. obs_source_release(source);
  321. }
  322. obs_data_release(data);
  323. }
  324. static inline bool HasAudioDevices(const char *source_id)
  325. {
  326. const char *output_id = source_id;
  327. obs_properties_t *props = obs_get_source_properties(output_id);
  328. size_t count = 0;
  329. if (!props)
  330. return false;
  331. obs_property_t *devices = obs_properties_get(props, "device_id");
  332. if (devices)
  333. count = obs_property_list_item_count(devices);
  334. obs_properties_destroy(props);
  335. return count != 0;
  336. }
  337. void OBSBasic::CreateFirstRunSources()
  338. {
  339. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  340. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  341. if (hasDesktopAudio)
  342. ResetAudioDevice(App()->OutputAudioSource(), "default",
  343. Str("Basic.DesktopDevice1"), 1);
  344. if (hasInputAudio)
  345. ResetAudioDevice(App()->InputAudioSource(), "default",
  346. Str("Basic.AuxDevice1"), 3);
  347. }
  348. void OBSBasic::CreateDefaultScene(bool firstStart)
  349. {
  350. disableSaving++;
  351. ClearSceneData();
  352. InitDefaultTransitions();
  353. CreateDefaultQuickTransitions();
  354. ui->transitionDuration->setValue(300);
  355. SetTransition(fadeTransition);
  356. obs_scene_t *scene = obs_scene_create(Str("Basic.Scene"));
  357. if (firstStart)
  358. CreateFirstRunSources();
  359. AddScene(obs_scene_get_source(scene));
  360. SetCurrentScene(scene, true);
  361. obs_scene_release(scene);
  362. disableSaving--;
  363. }
  364. static void ReorderItemByName(QListWidget *lw, const char *name, int newIndex)
  365. {
  366. for (int i = 0; i < lw->count(); i++) {
  367. QListWidgetItem *item = lw->item(i);
  368. if (strcmp(name, QT_TO_UTF8(item->text())) == 0) {
  369. if (newIndex != i) {
  370. item = lw->takeItem(i);
  371. lw->insertItem(newIndex, item);
  372. }
  373. break;
  374. }
  375. }
  376. }
  377. void OBSBasic::LoadSceneListOrder(obs_data_array_t *array)
  378. {
  379. size_t num = obs_data_array_count(array);
  380. for (size_t i = 0; i < num; i++) {
  381. obs_data_t *data = obs_data_array_item(array, i);
  382. const char *name = obs_data_get_string(data, "name");
  383. ReorderItemByName(ui->scenes, name, (int)i);
  384. obs_data_release(data);
  385. }
  386. }
  387. void OBSBasic::Load(const char *file)
  388. {
  389. if (!file || !os_file_exists(file)) {
  390. blog(LOG_INFO, "No scene file found, creating default scene");
  391. CreateDefaultScene(true);
  392. SaveProject();
  393. return;
  394. }
  395. disableSaving++;
  396. obs_data_t *data = obs_data_create_from_json_file_safe(file, "bak");
  397. if (!data) {
  398. disableSaving--;
  399. blog(LOG_ERROR, "Failed to load '%s', creating default scene",
  400. file);
  401. CreateDefaultScene(true);
  402. SaveProject();
  403. return;
  404. }
  405. ClearSceneData();
  406. InitDefaultTransitions();
  407. obs_data_array_t *sceneOrder = obs_data_get_array(data, "scene_order");
  408. obs_data_array_t *sources = obs_data_get_array(data, "sources");
  409. obs_data_array_t *transitions= obs_data_get_array(data, "transitions");
  410. const char *sceneName = obs_data_get_string(data,
  411. "current_scene");
  412. const char *programSceneName = obs_data_get_string(data,
  413. "current_program_scene");
  414. const char *transitionName = obs_data_get_string(data,
  415. "current_transition");
  416. if (!opt_starting_scene.empty()) {
  417. programSceneName = opt_starting_scene.c_str();
  418. if (!IsPreviewProgramMode())
  419. sceneName = opt_starting_scene.c_str();
  420. }
  421. int newDuration = obs_data_get_int(data, "transition_duration");
  422. if (!newDuration)
  423. newDuration = 300;
  424. if (!transitionName)
  425. transitionName = obs_source_get_name(fadeTransition);
  426. const char *curSceneCollection = config_get_string(
  427. App()->GlobalConfig(), "Basic", "SceneCollection");
  428. obs_data_set_default_string(data, "name", curSceneCollection);
  429. const char *name = obs_data_get_string(data, "name");
  430. obs_source_t *curScene;
  431. obs_source_t *curProgramScene;
  432. obs_source_t *curTransition;
  433. if (!name || !*name)
  434. name = curSceneCollection;
  435. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  436. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  437. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  438. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  439. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  440. obs_load_sources(sources, OBSBasic::SourceLoaded, this);
  441. if (transitions)
  442. LoadTransitions(transitions);
  443. if (sceneOrder)
  444. LoadSceneListOrder(sceneOrder);
  445. obs_data_array_release(transitions);
  446. curTransition = FindTransition(transitionName);
  447. if (!curTransition)
  448. curTransition = fadeTransition;
  449. ui->transitionDuration->setValue(newDuration);
  450. SetTransition(curTransition);
  451. retryScene:
  452. curScene = obs_get_source_by_name(sceneName);
  453. curProgramScene = obs_get_source_by_name(programSceneName);
  454. /* if the starting scene command line parameter is bad at all,
  455. * fall back to original settings */
  456. if (!opt_starting_scene.empty() && (!curScene || !curProgramScene)) {
  457. sceneName = obs_data_get_string(data, "current_scene");
  458. programSceneName = obs_data_get_string(data,
  459. "current_program_scene");
  460. obs_source_release(curScene);
  461. obs_source_release(curProgramScene);
  462. opt_starting_scene.clear();
  463. goto retryScene;
  464. }
  465. if (!curProgramScene) {
  466. curProgramScene = curScene;
  467. obs_source_addref(curScene);
  468. }
  469. SetCurrentScene(curScene, true);
  470. if (IsPreviewProgramMode())
  471. TransitionToScene(curProgramScene, true);
  472. obs_source_release(curScene);
  473. obs_source_release(curProgramScene);
  474. obs_data_array_release(sources);
  475. obs_data_array_release(sceneOrder);
  476. std::string file_base = strrchr(file, '/') + 1;
  477. file_base.erase(file_base.size() - 5, 5);
  478. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollection",
  479. name);
  480. config_set_string(App()->GlobalConfig(), "Basic", "SceneCollectionFile",
  481. file_base.c_str());
  482. obs_data_array_t *quickTransitionData = obs_data_get_array(data,
  483. "quick_transitions");
  484. LoadQuickTransitions(quickTransitionData);
  485. obs_data_array_release(quickTransitionData);
  486. RefreshQuickTransitions();
  487. obs_data_release(data);
  488. if (!opt_starting_scene.empty())
  489. opt_starting_scene.clear();
  490. if (opt_start_streaming) {
  491. QMetaObject::invokeMethod(this, "StartStreaming",
  492. Qt::QueuedConnection);
  493. opt_start_streaming = false;
  494. }
  495. if (opt_start_recording) {
  496. QMetaObject::invokeMethod(this, "StartRecording",
  497. Qt::QueuedConnection);
  498. opt_start_recording = false;
  499. }
  500. disableSaving--;
  501. }
  502. #define SERVICE_PATH "service.json"
  503. void OBSBasic::SaveService()
  504. {
  505. if (!service)
  506. return;
  507. char serviceJsonPath[512];
  508. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  509. SERVICE_PATH);
  510. if (ret <= 0)
  511. return;
  512. obs_data_t *data = obs_data_create();
  513. obs_data_t *settings = obs_service_get_settings(service);
  514. obs_data_set_string(data, "type", obs_service_get_type(service));
  515. obs_data_set_obj(data, "settings", settings);
  516. if (!obs_data_save_json_safe(data, serviceJsonPath, "tmp", "bak"))
  517. blog(LOG_WARNING, "Failed to save service");
  518. obs_data_release(settings);
  519. obs_data_release(data);
  520. }
  521. bool OBSBasic::LoadService()
  522. {
  523. const char *type;
  524. char serviceJsonPath[512];
  525. int ret = GetProfilePath(serviceJsonPath, sizeof(serviceJsonPath),
  526. SERVICE_PATH);
  527. if (ret <= 0)
  528. return false;
  529. obs_data_t *data = obs_data_create_from_json_file_safe(serviceJsonPath,
  530. "bak");
  531. obs_data_set_default_string(data, "type", "rtmp_common");
  532. type = obs_data_get_string(data, "type");
  533. obs_data_t *settings = obs_data_get_obj(data, "settings");
  534. obs_data_t *hotkey_data = obs_data_get_obj(data, "hotkeys");
  535. service = obs_service_create(type, "default_service", settings,
  536. hotkey_data);
  537. obs_service_release(service);
  538. obs_data_release(hotkey_data);
  539. obs_data_release(settings);
  540. obs_data_release(data);
  541. return !!service;
  542. }
  543. bool OBSBasic::InitService()
  544. {
  545. ProfileScope("OBSBasic::InitService");
  546. if (LoadService())
  547. return true;
  548. service = obs_service_create("rtmp_common", "default_service", nullptr,
  549. nullptr);
  550. if (!service)
  551. return false;
  552. obs_service_release(service);
  553. return true;
  554. }
  555. static const double scaled_vals[] =
  556. {
  557. 1.0,
  558. 1.25,
  559. (1.0/0.75),
  560. 1.5,
  561. (1.0/0.6),
  562. 1.75,
  563. 2.0,
  564. 2.25,
  565. 2.5,
  566. 2.75,
  567. 3.0,
  568. 0.0
  569. };
  570. bool OBSBasic::InitBasicConfigDefaults()
  571. {
  572. vector<MonitorInfo> monitors;
  573. GetMonitors(monitors);
  574. if (!monitors.size()) {
  575. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  576. "technically shouldn't be possible.");
  577. return false;
  578. }
  579. uint32_t cx = monitors[0].cx;
  580. uint32_t cy = monitors[0].cy;
  581. /* ----------------------------------------------------- */
  582. /* move over mixer values in advanced if older config */
  583. if (config_has_user_value(basicConfig, "AdvOut", "RecTrackIndex") &&
  584. !config_has_user_value(basicConfig, "AdvOut", "RecTracks")) {
  585. uint64_t track = config_get_uint(basicConfig, "AdvOut",
  586. "RecTrackIndex");
  587. track = 1ULL << (track - 1);
  588. config_set_uint(basicConfig, "AdvOut", "RecTracks", track);
  589. config_remove_value(basicConfig, "AdvOut", "RecTrackIndex");
  590. config_save_safe(basicConfig, "tmp", nullptr);
  591. }
  592. /* ----------------------------------------------------- */
  593. config_set_default_string(basicConfig, "Output", "Mode", "Simple");
  594. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  595. GetDefaultVideoSavePath().c_str());
  596. config_set_default_string(basicConfig, "SimpleOutput", "RecFormat",
  597. "flv");
  598. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  599. 2500);
  600. config_set_default_string(basicConfig, "SimpleOutput", "StreamEncoder",
  601. SIMPLE_ENCODER_X264);
  602. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 160);
  603. config_set_default_bool (basicConfig, "SimpleOutput", "UseAdvanced",
  604. false);
  605. config_set_default_bool (basicConfig, "SimpleOutput", "EnforceBitrate",
  606. true);
  607. config_set_default_string(basicConfig, "SimpleOutput", "Preset",
  608. "veryfast");
  609. config_set_default_string(basicConfig, "SimpleOutput", "RecQuality",
  610. "Stream");
  611. config_set_default_string(basicConfig, "SimpleOutput", "RecEncoder",
  612. SIMPLE_ENCODER_X264);
  613. config_set_default_bool (basicConfig, "AdvOut", "ApplyServiceSettings",
  614. true);
  615. config_set_default_bool (basicConfig, "AdvOut", "UseRescale", false);
  616. config_set_default_uint (basicConfig, "AdvOut", "TrackIndex", 1);
  617. config_set_default_string(basicConfig, "AdvOut", "Encoder", "obs_x264");
  618. config_set_default_string(basicConfig, "AdvOut", "RecType", "Standard");
  619. config_set_default_string(basicConfig, "AdvOut", "RecFilePath",
  620. GetDefaultVideoSavePath().c_str());
  621. config_set_default_string(basicConfig, "AdvOut", "RecFormat", "flv");
  622. config_set_default_bool (basicConfig, "AdvOut", "RecUseRescale",
  623. false);
  624. config_set_default_uint (basicConfig, "AdvOut", "RecTracks", (1<<0));
  625. config_set_default_string(basicConfig, "AdvOut", "RecEncoder",
  626. "none");
  627. config_set_default_bool (basicConfig, "AdvOut", "FFOutputToFile",
  628. true);
  629. config_set_default_string(basicConfig, "AdvOut", "FFFilePath",
  630. GetDefaultVideoSavePath().c_str());
  631. config_set_default_string(basicConfig, "AdvOut", "FFExtension", "mp4");
  632. config_set_default_uint (basicConfig, "AdvOut", "FFVBitrate", 2500);
  633. config_set_default_bool (basicConfig, "AdvOut", "FFUseRescale",
  634. false);
  635. config_set_default_uint (basicConfig, "AdvOut", "FFABitrate", 160);
  636. config_set_default_uint (basicConfig, "AdvOut", "FFAudioTrack", 1);
  637. config_set_default_uint (basicConfig, "AdvOut", "Track1Bitrate", 160);
  638. config_set_default_uint (basicConfig, "AdvOut", "Track2Bitrate", 160);
  639. config_set_default_uint (basicConfig, "AdvOut", "Track3Bitrate", 160);
  640. config_set_default_uint (basicConfig, "AdvOut", "Track4Bitrate", 160);
  641. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  642. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  643. config_set_default_string(basicConfig, "Output", "FilenameFormatting",
  644. "%CCYY-%MM-%DD %hh-%mm-%ss");
  645. config_set_default_bool (basicConfig, "Output", "DelayEnable", false);
  646. config_set_default_uint (basicConfig, "Output", "DelaySec", 20);
  647. config_set_default_bool (basicConfig, "Output", "DelayPreserve", true);
  648. config_set_default_bool (basicConfig, "Output", "Reconnect", true);
  649. config_set_default_uint (basicConfig, "Output", "RetryDelay", 10);
  650. config_set_default_uint (basicConfig, "Output", "MaxRetries", 20);
  651. int i = 0;
  652. uint32_t scale_cx = cx;
  653. uint32_t scale_cy = cy;
  654. /* use a default scaled resolution that has a pixel count no higher
  655. * than 1280x720 */
  656. while (((scale_cx * scale_cy) > (1280 * 720)) && scaled_vals[i] > 0.0) {
  657. double scale = scaled_vals[i++];
  658. scale_cx = uint32_t(double(cx) / scale);
  659. scale_cy = uint32_t(double(cy) / scale);
  660. }
  661. config_set_default_uint (basicConfig, "Video", "OutputCX", scale_cx);
  662. config_set_default_uint (basicConfig, "Video", "OutputCY", scale_cy);
  663. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  664. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  665. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  666. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  667. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  668. config_set_default_string(basicConfig, "Video", "ScaleType", "bicubic");
  669. config_set_default_string(basicConfig, "Video", "ColorFormat", "NV12");
  670. config_set_default_string(basicConfig, "Video", "ColorSpace", "601");
  671. config_set_default_string(basicConfig, "Video", "ColorRange",
  672. "Partial");
  673. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  674. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  675. "Stereo");
  676. return true;
  677. }
  678. bool OBSBasic::InitBasicConfig()
  679. {
  680. ProfileScope("OBSBasic::InitBasicConfig");
  681. char configPath[512];
  682. int ret = GetProfilePath(configPath, sizeof(configPath), "");
  683. if (ret <= 0) {
  684. OBSErrorBox(nullptr, "Failed to get profile path");
  685. return false;
  686. }
  687. if (os_mkdir(configPath) == MKDIR_ERROR) {
  688. OBSErrorBox(nullptr, "Failed to create profile path");
  689. return false;
  690. }
  691. ret = GetProfilePath(configPath, sizeof(configPath), "basic.ini");
  692. if (ret <= 0) {
  693. OBSErrorBox(nullptr, "Failed to get base.ini path");
  694. return false;
  695. }
  696. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  697. if (code != CONFIG_SUCCESS) {
  698. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  699. return false;
  700. }
  701. if (config_get_string(basicConfig, "General", "Name") == nullptr) {
  702. const char *curName = config_get_string(App()->GlobalConfig(),
  703. "Basic", "Profile");
  704. config_set_string(basicConfig, "General", "Name", curName);
  705. basicConfig.SaveSafe("tmp");
  706. }
  707. return InitBasicConfigDefaults();
  708. }
  709. void OBSBasic::InitOBSCallbacks()
  710. {
  711. ProfileScope("OBSBasic::InitOBSCallbacks");
  712. signalHandlers.reserve(signalHandlers.size() + 6);
  713. signalHandlers.emplace_back(obs_get_signal_handler(), "source_remove",
  714. OBSBasic::SourceRemoved, this);
  715. signalHandlers.emplace_back(obs_get_signal_handler(), "source_activate",
  716. OBSBasic::SourceActivated, this);
  717. signalHandlers.emplace_back(obs_get_signal_handler(), "source_deactivate",
  718. OBSBasic::SourceDeactivated, this);
  719. signalHandlers.emplace_back(obs_get_signal_handler(), "source_rename",
  720. OBSBasic::SourceRenamed, this);
  721. }
  722. void OBSBasic::InitPrimitives()
  723. {
  724. ProfileScope("OBSBasic::InitPrimitives");
  725. obs_enter_graphics();
  726. gs_render_start(true);
  727. gs_vertex2f(0.0f, 0.0f);
  728. gs_vertex2f(0.0f, 1.0f);
  729. gs_vertex2f(1.0f, 1.0f);
  730. gs_vertex2f(1.0f, 0.0f);
  731. gs_vertex2f(0.0f, 0.0f);
  732. box = gs_render_save();
  733. gs_render_start(true);
  734. gs_vertex2f(0.0f, 0.0f);
  735. gs_vertex2f(0.0f, 1.0f);
  736. boxLeft = gs_render_save();
  737. gs_render_start(true);
  738. gs_vertex2f(0.0f, 0.0f);
  739. gs_vertex2f(1.0f, 0.0f);
  740. boxTop = gs_render_save();
  741. gs_render_start(true);
  742. gs_vertex2f(1.0f, 0.0f);
  743. gs_vertex2f(1.0f, 1.0f);
  744. boxRight = gs_render_save();
  745. gs_render_start(true);
  746. gs_vertex2f(0.0f, 1.0f);
  747. gs_vertex2f(1.0f, 1.0f);
  748. boxBottom = gs_render_save();
  749. gs_render_start(true);
  750. for (int i = 0; i <= 360; i += (360/20)) {
  751. float pos = RAD(float(i));
  752. gs_vertex2f(cosf(pos), sinf(pos));
  753. }
  754. circle = gs_render_save();
  755. obs_leave_graphics();
  756. }
  757. void OBSBasic::ResetOutputs()
  758. {
  759. ProfileScope("OBSBasic::ResetOutputs");
  760. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  761. bool advOut = astrcmpi(mode, "Advanced") == 0;
  762. if (!outputHandler || !outputHandler->Active()) {
  763. outputHandler.reset();
  764. outputHandler.reset(advOut ?
  765. CreateAdvancedOutputHandler(this) :
  766. CreateSimpleOutputHandler(this));
  767. } else {
  768. outputHandler->Update();
  769. }
  770. }
  771. #define MAIN_SEPARATOR \
  772. "====================================================================="
  773. void OBSBasic::OBSInit()
  774. {
  775. ProfileScope("OBSBasic::OBSInit");
  776. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  777. "Basic", "SceneCollectionFile");
  778. char savePath[512];
  779. char fileName[512];
  780. int ret;
  781. if (!sceneCollection)
  782. throw "Failed to get scene collection name";
  783. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  784. sceneCollection);
  785. if (ret <= 0)
  786. throw "Failed to create scene collection file name";
  787. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  788. if (ret <= 0)
  789. throw "Failed to get scene collection json file path";
  790. if (!InitBasicConfig())
  791. throw "Failed to load basic.ini";
  792. if (!ResetAudio())
  793. throw "Failed to initialize audio";
  794. ret = ResetVideo();
  795. switch (ret) {
  796. case OBS_VIDEO_MODULE_NOT_FOUND:
  797. throw "Failed to initialize video: Graphics module not found";
  798. case OBS_VIDEO_NOT_SUPPORTED:
  799. throw "Failed to initialize video: Required graphics API "
  800. "functionality not found on these drivers or "
  801. "unavailable on this equipment";
  802. case OBS_VIDEO_INVALID_PARAM:
  803. throw "Failed to initialize video: Invalid parameters";
  804. default:
  805. if (ret != OBS_VIDEO_SUCCESS)
  806. throw "Failed to initialize video: Unspecified error";
  807. }
  808. InitOBSCallbacks();
  809. InitHotkeys();
  810. AddExtraModulePaths();
  811. obs_load_all_modules();
  812. blog(LOG_INFO, MAIN_SEPARATOR);
  813. ResetOutputs();
  814. CreateHotkeys();
  815. if (!InitService())
  816. throw "Failed to initialize service";
  817. InitPrimitives();
  818. sceneDuplicationMode = config_get_bool(App()->GlobalConfig(),
  819. "BasicWindow", "SceneDuplicationMode");
  820. swapScenesMode = config_get_bool(App()->GlobalConfig(),
  821. "BasicWindow", "SwapScenesMode");
  822. editPropertiesMode = config_get_bool(App()->GlobalConfig(),
  823. "BasicWindow", "EditPropertiesMode");
  824. SetPreviewProgramMode(config_get_bool(App()->GlobalConfig(),
  825. "BasicWindow", "PreviewProgramMode"));
  826. #define SET_VISIBILITY(name, control) \
  827. do { \
  828. if (config_has_user_value(App()->GlobalConfig(), \
  829. "BasicWindow", name)) { \
  830. bool visible = config_get_bool(App()->GlobalConfig(), \
  831. "BasicWindow", name); \
  832. ui->control->setChecked(visible); \
  833. } \
  834. } while (false)
  835. SET_VISIBILITY("ShowTransitions", toggleSceneTransitions);
  836. SET_VISIBILITY("ShowListboxToolbars", toggleListboxToolbars);
  837. SET_VISIBILITY("ShowStatusBar", toggleStatusBar);
  838. #undef SET_VISIBILITY
  839. {
  840. ProfileScope("OBSBasic::Load");
  841. disableSaving--;
  842. Load(savePath);
  843. disableSaving++;
  844. }
  845. TimedCheckForUpdates();
  846. loaded = true;
  847. previewEnabled = config_get_bool(App()->GlobalConfig(),
  848. "BasicWindow", "PreviewEnabled");
  849. if (!previewEnabled && !IsPreviewProgramMode())
  850. QMetaObject::invokeMethod(this, "EnablePreviewDisplay",
  851. Qt::QueuedConnection,
  852. Q_ARG(bool, previewEnabled));
  853. #ifdef _WIN32
  854. uint32_t winVer = GetWindowsVersion();
  855. if (winVer > 0 && winVer < 0x602) {
  856. bool disableAero = config_get_bool(basicConfig, "Video",
  857. "DisableAero");
  858. SetAeroEnabled(!disableAero);
  859. }
  860. #endif
  861. RefreshSceneCollections();
  862. RefreshProfiles();
  863. disableSaving--;
  864. auto addDisplay = [this] (OBSQTDisplay *window)
  865. {
  866. obs_display_add_draw_callback(window->GetDisplay(),
  867. OBSBasic::RenderMain, this);
  868. struct obs_video_info ovi;
  869. if (obs_get_video_info(&ovi))
  870. ResizePreview(ovi.base_width, ovi.base_height);
  871. };
  872. connect(ui->preview, &OBSQTDisplay::DisplayCreated, addDisplay);
  873. #ifdef _WIN32
  874. show();
  875. #endif
  876. bool alwaysOnTop = config_get_bool(App()->GlobalConfig(), "BasicWindow",
  877. "AlwaysOnTop");
  878. if (alwaysOnTop) {
  879. SetAlwaysOnTop(this, true);
  880. ui->actionAlwaysOnTop->setChecked(true);
  881. }
  882. #ifndef _WIN32
  883. show();
  884. #endif
  885. QList<int> defSizes;
  886. int top = config_get_int(App()->GlobalConfig(), "BasicWindow",
  887. "splitterTop");
  888. int bottom = config_get_int(App()->GlobalConfig(), "BasicWindow",
  889. "splitterBottom");
  890. if (!top || !bottom) {
  891. defSizes = ui->mainSplitter->sizes();
  892. int total = defSizes[0] + defSizes[1];
  893. defSizes[0] = total * 75 / 100;
  894. defSizes[1] = total - defSizes[0];
  895. } else {
  896. defSizes.push_back(top);
  897. defSizes.push_back(bottom);
  898. }
  899. ui->mainSplitter->setSizes(defSizes);
  900. }
  901. void OBSBasic::InitHotkeys()
  902. {
  903. ProfileScope("OBSBasic::InitHotkeys");
  904. struct obs_hotkeys_translations t = {};
  905. t.insert = Str("Hotkeys.Insert");
  906. t.del = Str("Hotkeys.Delete");
  907. t.home = Str("Hotkeys.Home");
  908. t.end = Str("Hotkeys.End");
  909. t.page_up = Str("Hotkeys.PageUp");
  910. t.page_down = Str("Hotkeys.PageDown");
  911. t.num_lock = Str("Hotkeys.NumLock");
  912. t.scroll_lock = Str("Hotkeys.ScrollLock");
  913. t.caps_lock = Str("Hotkeys.CapsLock");
  914. t.backspace = Str("Hotkeys.Backspace");
  915. t.tab = Str("Hotkeys.Tab");
  916. t.print = Str("Hotkeys.Print");
  917. t.pause = Str("Hotkeys.Pause");
  918. t.left = Str("Hotkeys.Left");
  919. t.right = Str("Hotkeys.Right");
  920. t.up = Str("Hotkeys.Up");
  921. t.down = Str("Hotkeys.Down");
  922. #ifdef _WIN32
  923. t.meta = Str("Hotkeys.Windows");
  924. #else
  925. t.meta = Str("Hotkeys.Super");
  926. #endif
  927. t.menu = Str("Hotkeys.Menu");
  928. t.space = Str("Hotkeys.Space");
  929. t.numpad_num = Str("Hotkeys.NumpadNum");
  930. t.numpad_multiply = Str("Hotkeys.NumpadMultiply");
  931. t.numpad_divide = Str("Hotkeys.NumpadDivide");
  932. t.numpad_plus = Str("Hotkeys.NumpadAdd");
  933. t.numpad_minus = Str("Hotkeys.NumpadSubtract");
  934. t.numpad_decimal = Str("Hotkeys.NumpadDecimal");
  935. t.apple_keypad_num = Str("Hotkeys.AppleKeypadNum");
  936. t.apple_keypad_multiply = Str("Hotkeys.AppleKeypadMultiply");
  937. t.apple_keypad_divide = Str("Hotkeys.AppleKeypadDivide");
  938. t.apple_keypad_plus = Str("Hotkeys.AppleKeypadAdd");
  939. t.apple_keypad_minus = Str("Hotkeys.AppleKeypadSubtract");
  940. t.apple_keypad_decimal = Str("Hotkeys.AppleKeypadDecimal");
  941. t.apple_keypad_equal = Str("Hotkeys.AppleKeypadEqual");
  942. t.mouse_num = Str("Hotkeys.MouseButton");
  943. obs_hotkeys_set_translations(&t);
  944. obs_hotkeys_set_audio_hotkeys_translations(Str("Mute"), Str("Unmute"),
  945. Str("Push-to-mute"), Str("Push-to-talk"));
  946. obs_hotkeys_set_sceneitem_hotkeys_translations(
  947. Str("SceneItemShow"), Str("SceneItemHide"));
  948. obs_hotkey_enable_callback_rerouting(true);
  949. obs_hotkey_set_callback_routing_func(OBSBasic::HotkeyTriggered, this);
  950. }
  951. void OBSBasic::ProcessHotkey(obs_hotkey_id id, bool pressed)
  952. {
  953. obs_hotkey_trigger_routed_callback(id, pressed);
  954. }
  955. void OBSBasic::HotkeyTriggered(void *data, obs_hotkey_id id, bool pressed)
  956. {
  957. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  958. QMetaObject::invokeMethod(&basic, "ProcessHotkey",
  959. Q_ARG(obs_hotkey_id, id), Q_ARG(bool, pressed));
  960. }
  961. void OBSBasic::CreateHotkeys()
  962. {
  963. ProfileScope("OBSBasic::CreateHotkeys");
  964. auto LoadHotkeyData = [&](const char *name) -> OBSData
  965. {
  966. const char *info = config_get_string(basicConfig,
  967. "Hotkeys", name);
  968. if (!info)
  969. return {};
  970. obs_data_t *data = obs_data_create_from_json(info);
  971. if (!data)
  972. return {};
  973. OBSData res = data;
  974. obs_data_release(data);
  975. return res;
  976. };
  977. auto LoadHotkey = [&](obs_hotkey_id id, const char *name)
  978. {
  979. obs_data_array_t *array =
  980. obs_data_get_array(LoadHotkeyData(name), "bindings");
  981. obs_hotkey_load(id, array);
  982. obs_data_array_release(array);
  983. };
  984. auto LoadHotkeyPair = [&](obs_hotkey_pair_id id, const char *name0,
  985. const char *name1)
  986. {
  987. obs_data_array_t *array0 =
  988. obs_data_get_array(LoadHotkeyData(name0), "bindings");
  989. obs_data_array_t *array1 =
  990. obs_data_get_array(LoadHotkeyData(name1), "bindings");
  991. obs_hotkey_pair_load(id, array0, array1);
  992. obs_data_array_release(array0);
  993. obs_data_array_release(array1);
  994. };
  995. #define MAKE_CALLBACK(pred, method) \
  996. [](void *data, obs_hotkey_pair_id, obs_hotkey_t*, bool pressed) \
  997. { \
  998. OBSBasic &basic = *static_cast<OBSBasic*>(data); \
  999. if (pred && pressed) { \
  1000. method(); \
  1001. return true; \
  1002. } \
  1003. return false; \
  1004. }
  1005. streamingHotkeys = obs_hotkey_pair_register_frontend(
  1006. "OBSBasic.StartStreaming",
  1007. Str("Basic.Hotkeys.StartStreaming"),
  1008. "OBSBasic.StopStreaming",
  1009. Str("Basic.Hotkeys.StopStreaming"),
  1010. MAKE_CALLBACK(!basic.outputHandler->StreamingActive(),
  1011. basic.StartStreaming),
  1012. MAKE_CALLBACK(basic.outputHandler->StreamingActive(),
  1013. basic.StopStreaming),
  1014. this, this);
  1015. LoadHotkeyPair(streamingHotkeys,
  1016. "OBSBasic.StartStreaming", "OBSBasic.StopStreaming");
  1017. auto cb = [] (void *data, obs_hotkey_id, obs_hotkey_t*, bool pressed)
  1018. {
  1019. OBSBasic &basic = *static_cast<OBSBasic*>(data);
  1020. if (basic.outputHandler->StreamingActive() && pressed) {
  1021. basic.ForceStopStreaming();
  1022. }
  1023. };
  1024. forceStreamingStopHotkey = obs_hotkey_register_frontend(
  1025. "OBSBasic.ForceStopStreaming",
  1026. Str("Basic.Main.ForceStopStreaming"),
  1027. cb, this);
  1028. LoadHotkey(forceStreamingStopHotkey,
  1029. "OBSBasic.ForceStopStreaming");
  1030. recordingHotkeys = obs_hotkey_pair_register_frontend(
  1031. "OBSBasic.StartRecording",
  1032. Str("Basic.Hotkeys.StartRecording"),
  1033. "OBSBasic.StopRecording",
  1034. Str("Basic.Hotkeys.StopRecording"),
  1035. MAKE_CALLBACK(!basic.outputHandler->RecordingActive(),
  1036. basic.StartRecording),
  1037. MAKE_CALLBACK(basic.outputHandler->RecordingActive(),
  1038. basic.StopRecording),
  1039. this, this);
  1040. LoadHotkeyPair(recordingHotkeys,
  1041. "OBSBasic.StartRecording", "OBSBasic.StopRecording");
  1042. #undef MAKE_CALLBACK
  1043. auto togglePreviewProgram = [] (void *data, obs_hotkey_id,
  1044. obs_hotkey_t*, bool pressed)
  1045. {
  1046. if (pressed)
  1047. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1048. "on_modeSwitch_clicked",
  1049. Qt::QueuedConnection);
  1050. };
  1051. togglePreviewProgramHotkey = obs_hotkey_register_frontend(
  1052. "OBSBasic.TogglePreviewProgram",
  1053. Str("Basic.TogglePreviewProgramMode"),
  1054. togglePreviewProgram, this);
  1055. LoadHotkey(togglePreviewProgramHotkey, "OBSBasic.TogglePreviewProgram");
  1056. auto transition = [] (void *data, obs_hotkey_id, obs_hotkey_t*,
  1057. bool pressed)
  1058. {
  1059. if (pressed)
  1060. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1061. "TransitionClicked",
  1062. Qt::QueuedConnection);
  1063. };
  1064. transitionHotkey = obs_hotkey_register_frontend(
  1065. "OBSBasic.Transition",
  1066. Str("Transition"), transition, this);
  1067. LoadHotkey(transitionHotkey, "OBSBasic.Transition");
  1068. }
  1069. void OBSBasic::ClearHotkeys()
  1070. {
  1071. obs_hotkey_pair_unregister(streamingHotkeys);
  1072. obs_hotkey_pair_unregister(recordingHotkeys);
  1073. obs_hotkey_unregister(forceStreamingStopHotkey);
  1074. obs_hotkey_unregister(togglePreviewProgramHotkey);
  1075. obs_hotkey_unregister(transitionHotkey);
  1076. }
  1077. OBSBasic::~OBSBasic()
  1078. {
  1079. delete programOptions;
  1080. delete program;
  1081. /* XXX: any obs data must be released before calling obs_shutdown.
  1082. * currently, we can't automate this with C++ RAII because of the
  1083. * delicate nature of obs_shutdown needing to be freed before the UI
  1084. * can be freed, and we have no control over the destruction order of
  1085. * the Qt UI stuff, so we have to manually clear any references to
  1086. * libobs. */
  1087. delete cpuUsageTimer;
  1088. os_cpu_usage_info_destroy(cpuUsageInfo);
  1089. obs_hotkey_set_callback_routing_func(nullptr, nullptr);
  1090. ClearHotkeys();
  1091. service = nullptr;
  1092. outputHandler.reset();
  1093. if (interaction)
  1094. delete interaction;
  1095. if (properties)
  1096. delete properties;
  1097. if (filters)
  1098. delete filters;
  1099. if (transformWindow)
  1100. delete transformWindow;
  1101. if (advAudioWindow)
  1102. delete advAudioWindow;
  1103. obs_display_remove_draw_callback(ui->preview->GetDisplay(),
  1104. OBSBasic::RenderMain, this);
  1105. obs_enter_graphics();
  1106. gs_vertexbuffer_destroy(box);
  1107. gs_vertexbuffer_destroy(boxLeft);
  1108. gs_vertexbuffer_destroy(boxTop);
  1109. gs_vertexbuffer_destroy(boxRight);
  1110. gs_vertexbuffer_destroy(boxBottom);
  1111. gs_vertexbuffer_destroy(circle);
  1112. obs_leave_graphics();
  1113. /* When shutting down, sometimes source references can get in to the
  1114. * event queue, and if we don't forcibly process those events they
  1115. * won't get processed until after obs_shutdown has been called. I
  1116. * really wish there were a more elegant way to deal with this via C++,
  1117. * but Qt doesn't use C++ in a normal way, so you can't really rely on
  1118. * normal C++ behavior for your data to be freed in the order that you
  1119. * expect or want it to. */
  1120. QApplication::sendPostedEvents(this);
  1121. config_set_int(App()->GlobalConfig(), "General", "LastVersion",
  1122. LIBOBS_API_VER);
  1123. QRect lastGeom = normalGeometry();
  1124. QList<int> splitterSizes = ui->mainSplitter->sizes();
  1125. bool alwaysOnTop = IsAlwaysOnTop(this);
  1126. config_set_int(App()->GlobalConfig(), "BasicWindow", "cx",
  1127. lastGeom.width());
  1128. config_set_int(App()->GlobalConfig(), "BasicWindow", "cy",
  1129. lastGeom.height());
  1130. config_set_int(App()->GlobalConfig(), "BasicWindow", "posx",
  1131. lastGeom.x());
  1132. config_set_int(App()->GlobalConfig(), "BasicWindow", "posy",
  1133. lastGeom.y());
  1134. config_set_int(App()->GlobalConfig(), "BasicWindow", "splitterTop",
  1135. splitterSizes[0]);
  1136. config_set_int(App()->GlobalConfig(), "BasicWindow", "splitterBottom",
  1137. splitterSizes[1]);
  1138. config_set_bool(App()->GlobalConfig(), "BasicWindow", "PreviewEnabled",
  1139. previewEnabled);
  1140. config_set_bool(App()->GlobalConfig(), "BasicWindow", "AlwaysOnTop",
  1141. alwaysOnTop);
  1142. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1143. "SceneDuplicationMode", sceneDuplicationMode);
  1144. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1145. "SwapScenesMode", swapScenesMode);
  1146. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1147. "EditPropertiesMode", editPropertiesMode);
  1148. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  1149. "PreviewProgramMode", IsPreviewProgramMode());
  1150. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  1151. #ifdef _WIN32
  1152. uint32_t winVer = GetWindowsVersion();
  1153. if (winVer > 0 && winVer < 0x602) {
  1154. bool disableAero = config_get_bool(basicConfig, "Video",
  1155. "DisableAero");
  1156. if (disableAero) {
  1157. SetAeroEnabled(true);
  1158. }
  1159. }
  1160. #endif
  1161. }
  1162. void OBSBasic::SaveProjectNow()
  1163. {
  1164. if (disableSaving)
  1165. return;
  1166. projectChanged = true;
  1167. SaveProjectDeferred();
  1168. }
  1169. void OBSBasic::SaveProject()
  1170. {
  1171. if (disableSaving)
  1172. return;
  1173. projectChanged = true;
  1174. QMetaObject::invokeMethod(this, "SaveProjectDeferred",
  1175. Qt::QueuedConnection);
  1176. }
  1177. void OBSBasic::SaveProjectDeferred()
  1178. {
  1179. if (disableSaving)
  1180. return;
  1181. if (!projectChanged)
  1182. return;
  1183. projectChanged = false;
  1184. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  1185. "Basic", "SceneCollectionFile");
  1186. char savePath[512];
  1187. char fileName[512];
  1188. int ret;
  1189. if (!sceneCollection)
  1190. return;
  1191. ret = snprintf(fileName, 512, "obs-studio/basic/scenes/%s.json",
  1192. sceneCollection);
  1193. if (ret <= 0)
  1194. return;
  1195. ret = GetConfigPath(savePath, sizeof(savePath), fileName);
  1196. if (ret <= 0)
  1197. return;
  1198. Save(savePath);
  1199. }
  1200. OBSScene OBSBasic::GetCurrentScene()
  1201. {
  1202. QListWidgetItem *item = ui->scenes->currentItem();
  1203. return item ? GetOBSRef<OBSScene>(item) : nullptr;
  1204. }
  1205. OBSSceneItem OBSBasic::GetSceneItem(QListWidgetItem *item)
  1206. {
  1207. return item ? GetOBSRef<OBSSceneItem>(item) : nullptr;
  1208. }
  1209. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  1210. {
  1211. return GetSceneItem(GetTopSelectedSourceItem());
  1212. }
  1213. void OBSBasic::UpdateSources(OBSScene scene)
  1214. {
  1215. ClearListItems(ui->sources);
  1216. obs_scene_enum_items(scene,
  1217. [] (obs_scene_t *scene, obs_sceneitem_t *item, void *p)
  1218. {
  1219. OBSBasic *window = static_cast<OBSBasic*>(p);
  1220. window->InsertSceneItem(item);
  1221. UNUSED_PARAMETER(scene);
  1222. return true;
  1223. }, this);
  1224. }
  1225. void OBSBasic::InsertSceneItem(obs_sceneitem_t *item)
  1226. {
  1227. QListWidgetItem *listItem = new QListWidgetItem();
  1228. SetOBSRef(listItem, OBSSceneItem(item));
  1229. ui->sources->insertItem(0, listItem);
  1230. ui->sources->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
  1231. SetupVisibilityItem(ui->sources, listItem, item);
  1232. }
  1233. void OBSBasic::CreateInteractionWindow(obs_source_t *source)
  1234. {
  1235. if (interaction)
  1236. interaction->close();
  1237. interaction = new OBSBasicInteraction(this, source);
  1238. interaction->Init();
  1239. interaction->setAttribute(Qt::WA_DeleteOnClose, true);
  1240. }
  1241. void OBSBasic::CreatePropertiesWindow(obs_source_t *source)
  1242. {
  1243. if (properties)
  1244. properties->close();
  1245. properties = new OBSBasicProperties(this, source);
  1246. properties->Init();
  1247. properties->setAttribute(Qt::WA_DeleteOnClose, true);
  1248. }
  1249. void OBSBasic::CreateFiltersWindow(obs_source_t *source)
  1250. {
  1251. if (filters)
  1252. filters->close();
  1253. filters = new OBSBasicFilters(this, source);
  1254. filters->Init();
  1255. filters->setAttribute(Qt::WA_DeleteOnClose, true);
  1256. }
  1257. /* Qt callbacks for invokeMethod */
  1258. void OBSBasic::AddScene(OBSSource source)
  1259. {
  1260. const char *name = obs_source_get_name(source);
  1261. obs_scene_t *scene = obs_scene_from_source(source);
  1262. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  1263. SetOBSRef(item, OBSScene(scene));
  1264. ui->scenes->addItem(item);
  1265. obs_hotkey_register_source(source, "OBSBasic.SelectScene",
  1266. Str("Basic.Hotkeys.SelectScene"),
  1267. [](void *data,
  1268. obs_hotkey_id, obs_hotkey_t*, bool pressed)
  1269. {
  1270. OBSBasic *main =
  1271. reinterpret_cast<OBSBasic*>(App()->GetMainWindow());
  1272. auto potential_source = static_cast<obs_source_t*>(data);
  1273. auto source = obs_source_get_ref(potential_source);
  1274. if (source && pressed)
  1275. main->SetCurrentScene(source);
  1276. obs_source_release(source);
  1277. }, static_cast<obs_source_t*>(source));
  1278. signal_handler_t *handler = obs_source_get_signal_handler(source);
  1279. SignalContainer<OBSScene> container;
  1280. container.ref = scene;
  1281. container.handlers.assign({
  1282. std::make_shared<OBSSignal>(handler, "item_add",
  1283. OBSBasic::SceneItemAdded, this),
  1284. std::make_shared<OBSSignal>(handler, "item_remove",
  1285. OBSBasic::SceneItemRemoved, this),
  1286. std::make_shared<OBSSignal>(handler, "item_select",
  1287. OBSBasic::SceneItemSelected, this),
  1288. std::make_shared<OBSSignal>(handler, "item_deselect",
  1289. OBSBasic::SceneItemDeselected, this),
  1290. std::make_shared<OBSSignal>(handler, "reorder",
  1291. OBSBasic::SceneReordered, this),
  1292. });
  1293. item->setData(static_cast<int>(QtDataRole::OBSSignals),
  1294. QVariant::fromValue(container));
  1295. /* if the scene already has items (a duplicated scene) add them */
  1296. auto addSceneItem = [this] (obs_sceneitem_t *item)
  1297. {
  1298. AddSceneItem(item);
  1299. };
  1300. using addSceneItem_t = decltype(addSceneItem);
  1301. obs_scene_enum_items(scene,
  1302. [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  1303. {
  1304. addSceneItem_t *func;
  1305. func = reinterpret_cast<addSceneItem_t*>(param);
  1306. (*func)(item);
  1307. return true;
  1308. }, &addSceneItem);
  1309. SaveProject();
  1310. }
  1311. void OBSBasic::RemoveScene(OBSSource source)
  1312. {
  1313. obs_scene_t *scene = obs_scene_from_source(source);
  1314. QListWidgetItem *sel = nullptr;
  1315. int count = ui->scenes->count();
  1316. for (int i = 0; i < count; i++) {
  1317. auto item = ui->scenes->item(i);
  1318. auto cur_scene = GetOBSRef<OBSScene>(item);
  1319. if (cur_scene != scene)
  1320. continue;
  1321. sel = item;
  1322. break;
  1323. }
  1324. if (sel != nullptr) {
  1325. if (sel == ui->scenes->currentItem())
  1326. ClearListItems(ui->sources);
  1327. delete sel;
  1328. }
  1329. SaveProject();
  1330. }
  1331. void OBSBasic::AddSceneItem(OBSSceneItem item)
  1332. {
  1333. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1334. if (GetCurrentScene() == scene)
  1335. InsertSceneItem(item);
  1336. SaveProject();
  1337. }
  1338. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  1339. {
  1340. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  1341. if (GetCurrentScene() == scene) {
  1342. for (int i = 0; i < ui->sources->count(); i++) {
  1343. QListWidgetItem *listItem = ui->sources->item(i);
  1344. if (GetOBSRef<OBSSceneItem>(listItem) == item) {
  1345. DeleteListItem(ui->sources, listItem);
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. SaveProject();
  1351. }
  1352. void OBSBasic::UpdateSceneSelection(OBSSource source)
  1353. {
  1354. if (source) {
  1355. obs_scene_t *scene = obs_scene_from_source(source);
  1356. const char *name = obs_source_get_name(source);
  1357. if (!scene)
  1358. return;
  1359. QList<QListWidgetItem*> items =
  1360. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  1361. if (items.count()) {
  1362. sceneChanging = true;
  1363. ui->scenes->setCurrentItem(items.first());
  1364. sceneChanging = false;
  1365. UpdateSources(scene);
  1366. }
  1367. }
  1368. }
  1369. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  1370. const QString &prevName)
  1371. {
  1372. QList<QListWidgetItem*> items =
  1373. listWidget->findItems(prevName, Qt::MatchExactly);
  1374. for (int i = 0; i < items.count(); i++)
  1375. items[i]->setText(newName);
  1376. }
  1377. void OBSBasic::RenameSources(QString newName, QString prevName)
  1378. {
  1379. RenameListValues(ui->scenes, newName, prevName);
  1380. for (size_t i = 0; i < volumes.size(); i++) {
  1381. if (volumes[i]->GetName().compare(prevName) == 0)
  1382. volumes[i]->SetName(newName);
  1383. }
  1384. SaveProject();
  1385. }
  1386. void OBSBasic::SelectSceneItem(OBSScene scene, OBSSceneItem item, bool select)
  1387. {
  1388. SignalBlocker sourcesSignalBlocker(ui->sources);
  1389. if (scene != GetCurrentScene() || ignoreSelectionUpdate)
  1390. return;
  1391. for (int i = 0; i < ui->sources->count(); i++) {
  1392. QListWidgetItem *witem = ui->sources->item(i);
  1393. QVariant data =
  1394. witem->data(static_cast<int>(QtDataRole::OBSRef));
  1395. if (!data.canConvert<OBSSceneItem>())
  1396. continue;
  1397. if (item != data.value<OBSSceneItem>())
  1398. continue;
  1399. witem->setSelected(select);
  1400. break;
  1401. }
  1402. }
  1403. void OBSBasic::GetAudioSourceFilters()
  1404. {
  1405. QAction *action = reinterpret_cast<QAction*>(sender());
  1406. VolControl *vol = action->property("volControl").value<VolControl*>();
  1407. obs_source_t *source = vol->GetSource();
  1408. CreateFiltersWindow(source);
  1409. }
  1410. void OBSBasic::GetAudioSourceProperties()
  1411. {
  1412. QAction *action = reinterpret_cast<QAction*>(sender());
  1413. VolControl *vol = action->property("volControl").value<VolControl*>();
  1414. obs_source_t *source = vol->GetSource();
  1415. CreatePropertiesWindow(source);
  1416. }
  1417. void OBSBasic::VolControlContextMenu()
  1418. {
  1419. VolControl *vol = reinterpret_cast<VolControl*>(sender());
  1420. QAction filtersAction(QTStr("Filters"), this);
  1421. QAction propertiesAction(QTStr("Properties"), this);
  1422. connect(&filtersAction, &QAction::triggered,
  1423. this, &OBSBasic::GetAudioSourceFilters,
  1424. Qt::DirectConnection);
  1425. connect(&propertiesAction, &QAction::triggered,
  1426. this, &OBSBasic::GetAudioSourceProperties,
  1427. Qt::DirectConnection);
  1428. filtersAction.setProperty("volControl",
  1429. QVariant::fromValue<VolControl*>(vol));
  1430. propertiesAction.setProperty("volControl",
  1431. QVariant::fromValue<VolControl*>(vol));
  1432. QMenu popup(this);
  1433. popup.addAction(&filtersAction);
  1434. popup.addAction(&propertiesAction);
  1435. popup.exec(QCursor::pos());
  1436. }
  1437. void OBSBasic::ActivateAudioSource(OBSSource source)
  1438. {
  1439. VolControl *vol = new VolControl(source, true);
  1440. connect(vol, &VolControl::ConfigClicked,
  1441. this, &OBSBasic::VolControlContextMenu);
  1442. volumes.push_back(vol);
  1443. ui->volumeWidgets->layout()->addWidget(vol);
  1444. }
  1445. void OBSBasic::DeactivateAudioSource(OBSSource source)
  1446. {
  1447. for (size_t i = 0; i < volumes.size(); i++) {
  1448. if (volumes[i]->GetSource() == source) {
  1449. delete volumes[i];
  1450. volumes.erase(volumes.begin() + i);
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. bool OBSBasic::QueryRemoveSource(obs_source_t *source)
  1456. {
  1457. const char *name = obs_source_get_name(source);
  1458. QString text = QTStr("ConfirmRemove.Text");
  1459. text.replace("$1", QT_UTF8(name));
  1460. QMessageBox remove_source(this);
  1461. remove_source.setText(text);
  1462. QAbstractButton *Yes = remove_source.addButton(QTStr("Yes"),
  1463. QMessageBox::YesRole);
  1464. remove_source.addButton(QTStr("No"), QMessageBox::NoRole);
  1465. remove_source.setIcon(QMessageBox::Question);
  1466. remove_source.setWindowTitle(QTStr("ConfirmRemove.Title"));
  1467. remove_source.exec();
  1468. return Yes == remove_source.clickedButton();
  1469. }
  1470. #define UPDATE_CHECK_INTERVAL (60*60*24*4) /* 4 days */
  1471. #ifdef UPDATE_SPARKLE
  1472. void init_sparkle_updater(bool update_to_undeployed);
  1473. void trigger_sparkle_update();
  1474. #endif
  1475. void OBSBasic::TimedCheckForUpdates()
  1476. {
  1477. #ifdef UPDATE_SPARKLE
  1478. init_sparkle_updater(config_get_bool(App()->GlobalConfig(), "General",
  1479. "UpdateToUndeployed"));
  1480. #else
  1481. long long lastUpdate = config_get_int(App()->GlobalConfig(), "General",
  1482. "LastUpdateCheck");
  1483. uint32_t lastVersion = config_get_int(App()->GlobalConfig(), "General",
  1484. "LastVersion");
  1485. if (lastVersion < LIBOBS_API_VER) {
  1486. lastUpdate = 0;
  1487. config_set_int(App()->GlobalConfig(), "General",
  1488. "LastUpdateCheck", 0);
  1489. }
  1490. long long t = (long long)time(nullptr);
  1491. long long secs = t - lastUpdate;
  1492. if (secs > UPDATE_CHECK_INTERVAL)
  1493. CheckForUpdates();
  1494. #endif
  1495. }
  1496. void OBSBasic::CheckForUpdates()
  1497. {
  1498. #ifdef UPDATE_SPARKLE
  1499. trigger_sparkle_update();
  1500. #else
  1501. ui->actionCheckForUpdates->setEnabled(false);
  1502. if (updateCheckThread) {
  1503. updateCheckThread->wait();
  1504. delete updateCheckThread;
  1505. }
  1506. RemoteTextThread *thread = new RemoteTextThread(
  1507. "https://obsproject.com/obs2_update/basic.json");
  1508. updateCheckThread = thread;
  1509. connect(thread, &RemoteTextThread::Result,
  1510. this, &OBSBasic::updateFileFinished);
  1511. updateCheckThread->start();
  1512. #endif
  1513. }
  1514. #ifdef __APPLE__
  1515. #define VERSION_ENTRY "mac"
  1516. #elif _WIN32
  1517. #define VERSION_ENTRY "windows"
  1518. #else
  1519. #define VERSION_ENTRY "other"
  1520. #endif
  1521. void OBSBasic::updateFileFinished(const QString &text, const QString &error)
  1522. {
  1523. ui->actionCheckForUpdates->setEnabled(true);
  1524. if (text.isEmpty()) {
  1525. blog(LOG_WARNING, "Update check failed: %s", QT_TO_UTF8(error));
  1526. return;
  1527. }
  1528. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  1529. obs_data_t *versionData = obs_data_get_obj(returnData, VERSION_ENTRY);
  1530. const char *description = obs_data_get_string(returnData,
  1531. "description");
  1532. const char *download = obs_data_get_string(versionData, "download");
  1533. if (returnData && versionData && description && download) {
  1534. long major = obs_data_get_int(versionData, "major");
  1535. long minor = obs_data_get_int(versionData, "minor");
  1536. long patch = obs_data_get_int(versionData, "patch");
  1537. long version = MAKE_SEMANTIC_VERSION(major, minor, patch);
  1538. blog(LOG_INFO, "Update check: last known remote version "
  1539. "is %ld.%ld.%ld",
  1540. major, minor, patch);
  1541. if (version > LIBOBS_API_VER) {
  1542. QString str = QTStr("UpdateAvailable.Text");
  1543. QMessageBox messageBox(this);
  1544. str = str.arg(QString::number(major),
  1545. QString::number(minor),
  1546. QString::number(patch),
  1547. download);
  1548. messageBox.setWindowTitle(QTStr("UpdateAvailable"));
  1549. messageBox.setTextFormat(Qt::RichText);
  1550. messageBox.setText(str);
  1551. messageBox.setInformativeText(QT_UTF8(description));
  1552. messageBox.exec();
  1553. long long t = (long long)time(nullptr);
  1554. config_set_int(App()->GlobalConfig(), "General",
  1555. "LastUpdateCheck", t);
  1556. config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
  1557. }
  1558. } else {
  1559. blog(LOG_WARNING, "Bad JSON file received from server");
  1560. }
  1561. obs_data_release(versionData);
  1562. obs_data_release(returnData);
  1563. }
  1564. void OBSBasic::DuplicateSelectedScene()
  1565. {
  1566. OBSScene curScene = GetCurrentScene();
  1567. if (!curScene)
  1568. return;
  1569. OBSSource curSceneSource = obs_scene_get_source(curScene);
  1570. QString format{obs_source_get_name(curSceneSource)};
  1571. format += " %1";
  1572. int i = 2;
  1573. QString placeHolderText = format.arg(i);
  1574. obs_source_t *source = nullptr;
  1575. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1576. obs_source_release(source);
  1577. placeHolderText = format.arg(++i);
  1578. }
  1579. for (;;) {
  1580. string name;
  1581. bool accepted = NameDialog::AskForName(this,
  1582. QTStr("Basic.Main.AddSceneDlg.Title"),
  1583. QTStr("Basic.Main.AddSceneDlg.Text"),
  1584. name,
  1585. placeHolderText);
  1586. if (!accepted)
  1587. return;
  1588. if (name.empty()) {
  1589. QMessageBox::information(this,
  1590. QTStr("NoNameEntered.Title"),
  1591. QTStr("NoNameEntered.Text"));
  1592. continue;
  1593. }
  1594. obs_source_t *source = obs_get_source_by_name(name.c_str());
  1595. if (source) {
  1596. QMessageBox::information(this,
  1597. QTStr("NameExists.Title"),
  1598. QTStr("NameExists.Text"));
  1599. obs_source_release(source);
  1600. continue;
  1601. }
  1602. obs_scene_t *scene = obs_scene_duplicate(curScene,
  1603. name.c_str(), OBS_SCENE_DUP_REFS);
  1604. source = obs_scene_get_source(scene);
  1605. AddScene(source);
  1606. SetCurrentScene(source, true);
  1607. obs_scene_release(scene);
  1608. break;
  1609. }
  1610. }
  1611. void OBSBasic::RemoveSelectedScene()
  1612. {
  1613. OBSScene scene = GetCurrentScene();
  1614. if (scene) {
  1615. obs_source_t *source = obs_scene_get_source(scene);
  1616. if (QueryRemoveSource(source))
  1617. obs_source_remove(source);
  1618. }
  1619. }
  1620. void OBSBasic::RemoveSelectedSceneItem()
  1621. {
  1622. OBSSceneItem item = GetCurrentSceneItem();
  1623. if (item) {
  1624. obs_source_t *source = obs_sceneitem_get_source(item);
  1625. if (QueryRemoveSource(source))
  1626. obs_sceneitem_remove(item);
  1627. }
  1628. }
  1629. struct ReorderInfo {
  1630. int idx = 0;
  1631. OBSBasic *window;
  1632. inline ReorderInfo(OBSBasic *window_) : window(window_) {}
  1633. };
  1634. void OBSBasic::ReorderSceneItem(obs_sceneitem_t *item, size_t idx)
  1635. {
  1636. int count = ui->sources->count();
  1637. int idx_inv = count - (int)idx - 1;
  1638. for (int i = 0; i < count; i++) {
  1639. QListWidgetItem *listItem = ui->sources->item(i);
  1640. OBSSceneItem sceneItem = GetOBSRef<OBSSceneItem>(listItem);
  1641. if (sceneItem == item) {
  1642. if ((int)idx_inv != i) {
  1643. bool sel = (ui->sources->currentRow() == i);
  1644. listItem = TakeListItem(ui->sources, i);
  1645. if (listItem) {
  1646. ui->sources->insertItem(idx_inv,
  1647. listItem);
  1648. SetupVisibilityItem(ui->sources,
  1649. listItem, item);
  1650. if (sel)
  1651. ui->sources->setCurrentRow(
  1652. idx_inv);
  1653. }
  1654. }
  1655. break;
  1656. }
  1657. }
  1658. }
  1659. void OBSBasic::ReorderSources(OBSScene scene)
  1660. {
  1661. ReorderInfo info(this);
  1662. if (scene != GetCurrentScene() || ui->sources->IgnoreReorder())
  1663. return;
  1664. obs_scene_enum_items(scene,
  1665. [] (obs_scene_t*, obs_sceneitem_t *item, void *p)
  1666. {
  1667. ReorderInfo *info =
  1668. reinterpret_cast<ReorderInfo*>(p);
  1669. info->window->ReorderSceneItem(item,
  1670. info->idx++);
  1671. return true;
  1672. }, &info);
  1673. SaveProject();
  1674. }
  1675. /* OBS Callbacks */
  1676. void OBSBasic::SceneReordered(void *data, calldata_t *params)
  1677. {
  1678. OBSBasic *window = static_cast<OBSBasic*>(data);
  1679. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1680. QMetaObject::invokeMethod(window, "ReorderSources",
  1681. Q_ARG(OBSScene, OBSScene(scene)));
  1682. }
  1683. void OBSBasic::SceneItemAdded(void *data, calldata_t *params)
  1684. {
  1685. OBSBasic *window = static_cast<OBSBasic*>(data);
  1686. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1687. QMetaObject::invokeMethod(window, "AddSceneItem",
  1688. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1689. }
  1690. void OBSBasic::SceneItemRemoved(void *data, calldata_t *params)
  1691. {
  1692. OBSBasic *window = static_cast<OBSBasic*>(data);
  1693. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1694. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  1695. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1696. }
  1697. void OBSBasic::SceneItemSelected(void *data, calldata_t *params)
  1698. {
  1699. OBSBasic *window = static_cast<OBSBasic*>(data);
  1700. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1701. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1702. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1703. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1704. Q_ARG(bool, true));
  1705. }
  1706. void OBSBasic::SceneItemDeselected(void *data, calldata_t *params)
  1707. {
  1708. OBSBasic *window = static_cast<OBSBasic*>(data);
  1709. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1710. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1711. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1712. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1713. Q_ARG(bool, false));
  1714. }
  1715. void OBSBasic::SourceLoaded(void *data, obs_source_t *source)
  1716. {
  1717. OBSBasic *window = static_cast<OBSBasic*>(data);
  1718. if (obs_scene_from_source(source) != NULL)
  1719. QMetaObject::invokeMethod(window,
  1720. "AddScene",
  1721. Q_ARG(OBSSource, OBSSource(source)));
  1722. }
  1723. void OBSBasic::SourceRemoved(void *data, calldata_t *params)
  1724. {
  1725. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1726. if (obs_scene_from_source(source) != NULL)
  1727. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1728. "RemoveScene",
  1729. Q_ARG(OBSSource, OBSSource(source)));
  1730. }
  1731. void OBSBasic::SourceActivated(void *data, calldata_t *params)
  1732. {
  1733. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1734. uint32_t flags = obs_source_get_output_flags(source);
  1735. if (flags & OBS_SOURCE_AUDIO)
  1736. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1737. "ActivateAudioSource",
  1738. Q_ARG(OBSSource, OBSSource(source)));
  1739. }
  1740. void OBSBasic::SourceDeactivated(void *data, calldata_t *params)
  1741. {
  1742. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1743. uint32_t flags = obs_source_get_output_flags(source);
  1744. if (flags & OBS_SOURCE_AUDIO)
  1745. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1746. "DeactivateAudioSource",
  1747. Q_ARG(OBSSource, OBSSource(source)));
  1748. }
  1749. void OBSBasic::SourceRenamed(void *data, calldata_t *params)
  1750. {
  1751. const char *newName = calldata_string(params, "new_name");
  1752. const char *prevName = calldata_string(params, "prev_name");
  1753. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1754. "RenameSources",
  1755. Q_ARG(QString, QT_UTF8(newName)),
  1756. Q_ARG(QString, QT_UTF8(prevName)));
  1757. }
  1758. void OBSBasic::DrawBackdrop(float cx, float cy)
  1759. {
  1760. if (!box)
  1761. return;
  1762. gs_effect_t *solid = obs_get_base_effect(OBS_EFFECT_SOLID);
  1763. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  1764. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1765. vec4 colorVal;
  1766. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  1767. gs_effect_set_vec4(color, &colorVal);
  1768. gs_technique_begin(tech);
  1769. gs_technique_begin_pass(tech, 0);
  1770. gs_matrix_push();
  1771. gs_matrix_identity();
  1772. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  1773. gs_load_vertexbuffer(box);
  1774. gs_draw(GS_TRISTRIP, 0, 0);
  1775. gs_matrix_pop();
  1776. gs_technique_end_pass(tech);
  1777. gs_technique_end(tech);
  1778. gs_load_vertexbuffer(nullptr);
  1779. }
  1780. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  1781. {
  1782. OBSBasic *window = static_cast<OBSBasic*>(data);
  1783. obs_video_info ovi;
  1784. obs_get_video_info(&ovi);
  1785. window->previewCX = int(window->previewScale * float(ovi.base_width));
  1786. window->previewCY = int(window->previewScale * float(ovi.base_height));
  1787. gs_viewport_push();
  1788. gs_projection_push();
  1789. /* --------------------------------------- */
  1790. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  1791. -100.0f, 100.0f);
  1792. gs_set_viewport(window->previewX, window->previewY,
  1793. window->previewCX, window->previewCY);
  1794. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  1795. if (window->IsPreviewProgramMode()) {
  1796. OBSScene scene = window->GetCurrentScene();
  1797. obs_source_t *source = obs_scene_get_source(scene);
  1798. if (source)
  1799. obs_source_video_render(source);
  1800. } else {
  1801. obs_render_main_view();
  1802. }
  1803. gs_load_vertexbuffer(nullptr);
  1804. /* --------------------------------------- */
  1805. QSize previewSize = GetPixelSize(window->ui->preview);
  1806. float right = float(previewSize.width()) - window->previewX;
  1807. float bottom = float(previewSize.height()) - window->previewY;
  1808. gs_ortho(-window->previewX, right,
  1809. -window->previewY, bottom,
  1810. -100.0f, 100.0f);
  1811. gs_reset_viewport();
  1812. window->ui->preview->DrawSceneEditing();
  1813. /* --------------------------------------- */
  1814. gs_projection_pop();
  1815. gs_viewport_pop();
  1816. UNUSED_PARAMETER(cx);
  1817. UNUSED_PARAMETER(cy);
  1818. }
  1819. /* Main class functions */
  1820. obs_service_t *OBSBasic::GetService()
  1821. {
  1822. if (!service) {
  1823. service = obs_service_create("rtmp_common", NULL, NULL,
  1824. nullptr);
  1825. obs_service_release(service);
  1826. }
  1827. return service;
  1828. }
  1829. void OBSBasic::SetService(obs_service_t *newService)
  1830. {
  1831. if (newService)
  1832. service = newService;
  1833. }
  1834. bool OBSBasic::StreamingActive() const
  1835. {
  1836. if (!outputHandler)
  1837. return false;
  1838. return outputHandler->StreamingActive();
  1839. }
  1840. bool OBSBasic::Active() const
  1841. {
  1842. if (!outputHandler)
  1843. return false;
  1844. return outputHandler->Active();
  1845. }
  1846. #ifdef _WIN32
  1847. #define IS_WIN32 1
  1848. #else
  1849. #define IS_WIN32 0
  1850. #endif
  1851. static inline int AttemptToResetVideo(struct obs_video_info *ovi)
  1852. {
  1853. return obs_reset_video(ovi);
  1854. }
  1855. static inline enum obs_scale_type GetScaleType(ConfigFile &basicConfig)
  1856. {
  1857. const char *scaleTypeStr = config_get_string(basicConfig,
  1858. "Video", "ScaleType");
  1859. if (astrcmpi(scaleTypeStr, "bilinear") == 0)
  1860. return OBS_SCALE_BILINEAR;
  1861. else if (astrcmpi(scaleTypeStr, "lanczos") == 0)
  1862. return OBS_SCALE_LANCZOS;
  1863. else
  1864. return OBS_SCALE_BICUBIC;
  1865. }
  1866. static inline enum video_format GetVideoFormatFromName(const char *name)
  1867. {
  1868. if (astrcmpi(name, "I420") == 0)
  1869. return VIDEO_FORMAT_I420;
  1870. else if (astrcmpi(name, "NV12") == 0)
  1871. return VIDEO_FORMAT_NV12;
  1872. else if (astrcmpi(name, "I444") == 0)
  1873. return VIDEO_FORMAT_I444;
  1874. #if 0 //currently unsupported
  1875. else if (astrcmpi(name, "YVYU") == 0)
  1876. return VIDEO_FORMAT_YVYU;
  1877. else if (astrcmpi(name, "YUY2") == 0)
  1878. return VIDEO_FORMAT_YUY2;
  1879. else if (astrcmpi(name, "UYVY") == 0)
  1880. return VIDEO_FORMAT_UYVY;
  1881. #endif
  1882. else
  1883. return VIDEO_FORMAT_RGBA;
  1884. }
  1885. int OBSBasic::ResetVideo()
  1886. {
  1887. ProfileScope("OBSBasic::ResetVideo");
  1888. struct obs_video_info ovi;
  1889. int ret;
  1890. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  1891. const char *colorFormat = config_get_string(basicConfig, "Video",
  1892. "ColorFormat");
  1893. const char *colorSpace = config_get_string(basicConfig, "Video",
  1894. "ColorSpace");
  1895. const char *colorRange = config_get_string(basicConfig, "Video",
  1896. "ColorRange");
  1897. ovi.graphics_module = App()->GetRenderModule();
  1898. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  1899. "Video", "BaseCX");
  1900. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  1901. "Video", "BaseCY");
  1902. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  1903. "Video", "OutputCX");
  1904. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  1905. "Video", "OutputCY");
  1906. ovi.output_format = GetVideoFormatFromName(colorFormat);
  1907. ovi.colorspace = astrcmpi(colorSpace, "601") == 0 ?
  1908. VIDEO_CS_601 : VIDEO_CS_709;
  1909. ovi.range = astrcmpi(colorRange, "Full") == 0 ?
  1910. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  1911. ovi.adapter = 0;
  1912. ovi.gpu_conversion = true;
  1913. ovi.scale_type = GetScaleType(basicConfig);
  1914. if (ovi.base_width == 0 || ovi.base_height == 0) {
  1915. ovi.base_width = 1920;
  1916. ovi.base_height = 1080;
  1917. config_set_uint(basicConfig, "Video", "BaseCX", 1920);
  1918. config_set_uint(basicConfig, "Video", "BaseCY", 1080);
  1919. }
  1920. if (ovi.output_width == 0 || ovi.output_height == 0) {
  1921. ovi.output_width = ovi.base_width;
  1922. ovi.output_height = ovi.base_height;
  1923. config_set_uint(basicConfig, "Video", "OutputCX",
  1924. ovi.base_width);
  1925. config_set_uint(basicConfig, "Video", "OutputCY",
  1926. ovi.base_height);
  1927. }
  1928. ret = AttemptToResetVideo(&ovi);
  1929. if (IS_WIN32 && ret != OBS_VIDEO_SUCCESS) {
  1930. /* Try OpenGL if DirectX fails on windows */
  1931. if (astrcmpi(ovi.graphics_module, DL_OPENGL) != 0) {
  1932. blog(LOG_WARNING, "Failed to initialize obs video (%d) "
  1933. "with graphics_module='%s', retrying "
  1934. "with graphics_module='%s'",
  1935. ret, ovi.graphics_module,
  1936. DL_OPENGL);
  1937. ovi.graphics_module = DL_OPENGL;
  1938. ret = AttemptToResetVideo(&ovi);
  1939. }
  1940. } else if (ret == OBS_VIDEO_SUCCESS) {
  1941. ResizePreview(ovi.base_width, ovi.base_height);
  1942. if (program)
  1943. ResizeProgram(ovi.base_width, ovi.base_height);
  1944. }
  1945. return ret;
  1946. }
  1947. bool OBSBasic::ResetAudio()
  1948. {
  1949. ProfileScope("OBSBasic::ResetAudio");
  1950. struct obs_audio_info ai;
  1951. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  1952. "SampleRate");
  1953. const char *channelSetupStr = config_get_string(basicConfig,
  1954. "Audio", "ChannelSetup");
  1955. if (strcmp(channelSetupStr, "Mono") == 0)
  1956. ai.speakers = SPEAKERS_MONO;
  1957. else
  1958. ai.speakers = SPEAKERS_STEREO;
  1959. return obs_reset_audio(&ai);
  1960. }
  1961. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId,
  1962. const char *deviceDesc, int channel)
  1963. {
  1964. obs_source_t *source;
  1965. obs_data_t *settings;
  1966. bool same = false;
  1967. source = obs_get_output_source(channel);
  1968. if (source) {
  1969. settings = obs_source_get_settings(source);
  1970. const char *curId = obs_data_get_string(settings, "device_id");
  1971. same = (strcmp(curId, deviceId) == 0);
  1972. obs_data_release(settings);
  1973. obs_source_release(source);
  1974. }
  1975. if (!same)
  1976. obs_set_output_source(channel, nullptr);
  1977. if (!same && strcmp(deviceId, "disabled") != 0) {
  1978. obs_data_t *settings = obs_data_create();
  1979. obs_data_set_string(settings, "device_id", deviceId);
  1980. source = obs_source_create(sourceId, deviceDesc, settings,
  1981. nullptr);
  1982. obs_data_release(settings);
  1983. obs_set_output_source(channel, source);
  1984. obs_source_release(source);
  1985. }
  1986. }
  1987. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  1988. {
  1989. QSize targetSize;
  1990. /* resize preview panel to fix to the top section of the window */
  1991. targetSize = GetPixelSize(ui->preview);
  1992. GetScaleAndCenterPos(int(cx), int(cy),
  1993. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  1994. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  1995. previewX, previewY, previewScale);
  1996. previewX += float(PREVIEW_EDGE_SIZE);
  1997. previewY += float(PREVIEW_EDGE_SIZE);
  1998. }
  1999. void OBSBasic::CloseDialogs()
  2000. {
  2001. QList<QDialog*> childDialogs = this->findChildren<QDialog *>();
  2002. if (!childDialogs.isEmpty()) {
  2003. for (int i = 0; i < childDialogs.size(); ++i) {
  2004. childDialogs.at(i)->close();
  2005. }
  2006. }
  2007. for (QPointer<QWidget> &projector : projectors) {
  2008. delete projector;
  2009. projector.clear();
  2010. }
  2011. }
  2012. void OBSBasic::ClearSceneData()
  2013. {
  2014. disableSaving++;
  2015. CloseDialogs();
  2016. ClearVolumeControls();
  2017. ClearListItems(ui->scenes);
  2018. ClearListItems(ui->sources);
  2019. ClearQuickTransitions();
  2020. ui->transitions->clear();
  2021. obs_set_output_source(0, nullptr);
  2022. obs_set_output_source(1, nullptr);
  2023. obs_set_output_source(2, nullptr);
  2024. obs_set_output_source(3, nullptr);
  2025. obs_set_output_source(4, nullptr);
  2026. obs_set_output_source(5, nullptr);
  2027. lastScene = nullptr;
  2028. swapScene = nullptr;
  2029. programScene = nullptr;
  2030. auto cb = [](void *unused, obs_source_t *source)
  2031. {
  2032. obs_source_remove(source);
  2033. UNUSED_PARAMETER(unused);
  2034. return true;
  2035. };
  2036. obs_enum_sources(cb, nullptr);
  2037. disableSaving--;
  2038. blog(LOG_INFO, "All scene data cleared");
  2039. blog(LOG_INFO, "------------------------------------------------");
  2040. }
  2041. void OBSBasic::closeEvent(QCloseEvent *event)
  2042. {
  2043. if (outputHandler && outputHandler->Active()) {
  2044. QMessageBox::StandardButton button = QMessageBox::question(
  2045. this, QTStr("ConfirmExit.Title"),
  2046. QTStr("ConfirmExit.Text"));
  2047. if (button == QMessageBox::No) {
  2048. event->ignore();
  2049. return;
  2050. }
  2051. }
  2052. QWidget::closeEvent(event);
  2053. if (!event->isAccepted())
  2054. return;
  2055. if (updateCheckThread)
  2056. updateCheckThread->wait();
  2057. if (logUploadThread)
  2058. logUploadThread->wait();
  2059. signalHandlers.clear();
  2060. SaveProjectNow();
  2061. disableSaving++;
  2062. /* Clear all scene data (dialogs, widgets, widget sub-items, scenes,
  2063. * sources, etc) so that all references are released before shutdown */
  2064. ClearSceneData();
  2065. }
  2066. void OBSBasic::changeEvent(QEvent *event)
  2067. {
  2068. /* TODO */
  2069. UNUSED_PARAMETER(event);
  2070. }
  2071. void OBSBasic::on_actionShow_Recordings_triggered()
  2072. {
  2073. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  2074. const char *path = strcmp(mode, "Advanced") ?
  2075. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  2076. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  2077. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  2078. }
  2079. void OBSBasic::on_actionRemux_triggered()
  2080. {
  2081. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  2082. const char *path = strcmp(mode, "Advanced") ?
  2083. config_get_string(basicConfig, "SimpleOutput", "FilePath") :
  2084. config_get_string(basicConfig, "AdvOut", "RecFilePath");
  2085. OBSRemux remux(path, this);
  2086. remux.exec();
  2087. }
  2088. void OBSBasic::on_action_Settings_triggered()
  2089. {
  2090. OBSBasicSettings settings(this);
  2091. settings.exec();
  2092. }
  2093. void OBSBasic::on_actionAdvAudioProperties_triggered()
  2094. {
  2095. if (advAudioWindow != nullptr) {
  2096. advAudioWindow->raise();
  2097. return;
  2098. }
  2099. advAudioWindow = new OBSBasicAdvAudio(this);
  2100. advAudioWindow->show();
  2101. advAudioWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  2102. connect(advAudioWindow, SIGNAL(destroyed()),
  2103. this, SLOT(on_advAudioProps_destroyed()));
  2104. }
  2105. void OBSBasic::on_advAudioProps_clicked()
  2106. {
  2107. on_actionAdvAudioProperties_triggered();
  2108. }
  2109. void OBSBasic::on_advAudioProps_destroyed()
  2110. {
  2111. advAudioWindow = nullptr;
  2112. }
  2113. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  2114. QListWidgetItem *prev)
  2115. {
  2116. obs_source_t *source = NULL;
  2117. if (sceneChanging)
  2118. return;
  2119. if (current) {
  2120. obs_scene_t *scene;
  2121. scene = GetOBSRef<OBSScene>(current);
  2122. source = obs_scene_get_source(scene);
  2123. }
  2124. SetCurrentScene(source);
  2125. UNUSED_PARAMETER(prev);
  2126. }
  2127. void OBSBasic::EditSceneName()
  2128. {
  2129. QListWidgetItem *item = ui->scenes->currentItem();
  2130. Qt::ItemFlags flags = item->flags();
  2131. item->setFlags(flags | Qt::ItemIsEditable);
  2132. ui->scenes->editItem(item);
  2133. item->setFlags(flags);
  2134. }
  2135. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  2136. const char *slot)
  2137. {
  2138. QAction *action;
  2139. std::vector<MonitorInfo> monitors;
  2140. GetMonitors(monitors);
  2141. for (int i = 0; (size_t)i < monitors.size(); i++) {
  2142. const MonitorInfo &monitor = monitors[i];
  2143. QString str = QString("%1 %2: %3x%4 @ %5,%6").
  2144. arg(QTStr("Display"),
  2145. QString::number(i),
  2146. QString::number((int)monitor.cx),
  2147. QString::number((int)monitor.cy),
  2148. QString::number((int)monitor.x),
  2149. QString::number((int)monitor.y));
  2150. action = parent->addAction(str, target, slot);
  2151. action->setProperty("monitor", i);
  2152. }
  2153. }
  2154. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  2155. {
  2156. QListWidgetItem *item = ui->scenes->itemAt(pos);
  2157. QPointer<QMenu> sceneProjectorMenu;
  2158. QMenu popup(this);
  2159. QMenu order(QTStr("Basic.MainMenu.Edit.Order"), this);
  2160. popup.addAction(QTStr("Add"),
  2161. this, SLOT(on_actionAddScene_triggered()));
  2162. if (item) {
  2163. popup.addSeparator();
  2164. popup.addAction(QTStr("Duplicate"),
  2165. this, SLOT(DuplicateSelectedScene()));
  2166. popup.addAction(QTStr("Rename"),
  2167. this, SLOT(EditSceneName()));
  2168. popup.addAction(QTStr("Remove"),
  2169. this, SLOT(RemoveSelectedScene()),
  2170. DeleteKeys.front());
  2171. popup.addSeparator();
  2172. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveUp"),
  2173. this, SLOT(on_actionSceneUp_triggered()));
  2174. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveDown"),
  2175. this, SLOT(on_actionSceneDown_triggered()));
  2176. order.addSeparator();
  2177. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToTop"),
  2178. this, SLOT(MoveSceneToTop()));
  2179. order.addAction(QTStr("Basic.MainMenu.Edit.Order.MoveToBottom"),
  2180. this, SLOT(MoveSceneToBottom()));
  2181. popup.addMenu(&order);
  2182. popup.addSeparator();
  2183. sceneProjectorMenu = new QMenu(QTStr("SceneProjector"));
  2184. AddProjectorMenuMonitors(sceneProjectorMenu, this,
  2185. SLOT(OpenSceneProjector()));
  2186. popup.addMenu(sceneProjectorMenu);
  2187. popup.addSeparator();
  2188. popup.addAction(QTStr("Filters"), this,
  2189. SLOT(OpenSceneFilters()));
  2190. }
  2191. popup.exec(QCursor::pos());
  2192. }
  2193. void OBSBasic::on_actionAddScene_triggered()
  2194. {
  2195. string name;
  2196. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  2197. int i = 1;
  2198. QString placeHolderText = format.arg(i);
  2199. obs_source_t *source = nullptr;
  2200. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  2201. obs_source_release(source);
  2202. placeHolderText = format.arg(++i);
  2203. }
  2204. bool accepted = NameDialog::AskForName(this,
  2205. QTStr("Basic.Main.AddSceneDlg.Title"),
  2206. QTStr("Basic.Main.AddSceneDlg.Text"),
  2207. name,
  2208. placeHolderText);
  2209. if (accepted) {
  2210. if (name.empty()) {
  2211. QMessageBox::information(this,
  2212. QTStr("NoNameEntered.Title"),
  2213. QTStr("NoNameEntered.Text"));
  2214. on_actionAddScene_triggered();
  2215. return;
  2216. }
  2217. obs_source_t *source = obs_get_source_by_name(name.c_str());
  2218. if (source) {
  2219. QMessageBox::information(this,
  2220. QTStr("NameExists.Title"),
  2221. QTStr("NameExists.Text"));
  2222. obs_source_release(source);
  2223. on_actionAddScene_triggered();
  2224. return;
  2225. }
  2226. obs_scene_t *scene = obs_scene_create(name.c_str());
  2227. source = obs_scene_get_source(scene);
  2228. AddScene(source);
  2229. SetCurrentScene(source);
  2230. obs_scene_release(scene);
  2231. }
  2232. }
  2233. void OBSBasic::on_actionRemoveScene_triggered()
  2234. {
  2235. OBSScene scene = GetCurrentScene();
  2236. obs_source_t *source = obs_scene_get_source(scene);
  2237. if (source && QueryRemoveSource(source))
  2238. obs_source_remove(source);
  2239. }
  2240. void OBSBasic::ChangeSceneIndex(bool relative, int offset, int invalidIdx)
  2241. {
  2242. int idx = ui->scenes->currentRow();
  2243. if (idx == -1 || idx == invalidIdx)
  2244. return;
  2245. sceneChanging = true;
  2246. QListWidgetItem *item = ui->scenes->takeItem(idx);
  2247. if (!relative)
  2248. idx = 0;
  2249. ui->scenes->insertItem(idx + offset, item);
  2250. ui->scenes->setCurrentRow(idx + offset);
  2251. item->setSelected(true);
  2252. sceneChanging = false;
  2253. }
  2254. void OBSBasic::on_actionSceneUp_triggered()
  2255. {
  2256. ChangeSceneIndex(true, -1, 0);
  2257. }
  2258. void OBSBasic::on_actionSceneDown_triggered()
  2259. {
  2260. ChangeSceneIndex(true, 1, ui->scenes->count() - 1);
  2261. }
  2262. void OBSBasic::MoveSceneToTop()
  2263. {
  2264. ChangeSceneIndex(false, 0, 0);
  2265. }
  2266. void OBSBasic::MoveSceneToBottom()
  2267. {
  2268. ChangeSceneIndex(false, ui->scenes->count() - 1,
  2269. ui->scenes->count() - 1);
  2270. }
  2271. void OBSBasic::on_sources_itemSelectionChanged()
  2272. {
  2273. SignalBlocker sourcesSignalBlocker(ui->sources);
  2274. auto updateItemSelection = [&]()
  2275. {
  2276. ignoreSelectionUpdate = true;
  2277. for (int i = 0; i < ui->sources->count(); i++)
  2278. {
  2279. QListWidgetItem *wItem = ui->sources->item(i);
  2280. OBSSceneItem item = GetOBSRef<OBSSceneItem>(wItem);
  2281. obs_sceneitem_select(item, wItem->isSelected());
  2282. }
  2283. ignoreSelectionUpdate = false;
  2284. };
  2285. using updateItemSelection_t = decltype(updateItemSelection);
  2286. obs_scene_atomic_update(GetCurrentScene(),
  2287. [](void *data, obs_scene_t *)
  2288. {
  2289. (*static_cast<updateItemSelection_t*>(data))();
  2290. }, static_cast<void*>(&updateItemSelection));
  2291. }
  2292. void OBSBasic::EditSceneItemName()
  2293. {
  2294. QListWidgetItem *item = GetTopSelectedSourceItem();
  2295. Qt::ItemFlags flags = item->flags();
  2296. OBSSceneItem sceneItem= GetOBSRef<OBSSceneItem>(item);
  2297. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2298. const char *name = obs_source_get_name(source);
  2299. item->setText(QT_UTF8(name));
  2300. item->setFlags(flags | Qt::ItemIsEditable);
  2301. ui->sources->removeItemWidget(item);
  2302. ui->sources->editItem(item);
  2303. item->setFlags(flags);
  2304. }
  2305. void OBSBasic::SetDeinterlacingMode()
  2306. {
  2307. QAction *action = reinterpret_cast<QAction*>(sender());
  2308. obs_deinterlace_mode mode =
  2309. (obs_deinterlace_mode)action->property("mode").toInt();
  2310. OBSSceneItem sceneItem = GetCurrentSceneItem();
  2311. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2312. obs_source_set_deinterlace_mode(source, mode);
  2313. }
  2314. void OBSBasic::SetDeinterlacingOrder()
  2315. {
  2316. QAction *action = reinterpret_cast<QAction*>(sender());
  2317. obs_deinterlace_field_order order =
  2318. (obs_deinterlace_field_order)action->property("order").toInt();
  2319. OBSSceneItem sceneItem = GetCurrentSceneItem();
  2320. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2321. obs_source_set_deinterlace_field_order(source, order);
  2322. }
  2323. QMenu *OBSBasic::AddDeinterlacingMenu(obs_source_t *source)
  2324. {
  2325. QMenu *menu = new QMenu(QTStr("Deinterlacing"));
  2326. obs_deinterlace_mode deinterlaceMode =
  2327. obs_source_get_deinterlace_mode(source);
  2328. obs_deinterlace_field_order deinterlaceOrder =
  2329. obs_source_get_deinterlace_field_order(source);
  2330. QAction *action;
  2331. #define ADD_MODE(name, mode) \
  2332. action = menu->addAction(QTStr("" name), this, \
  2333. SLOT(SetDeinterlacingMode())); \
  2334. action->setProperty("mode", (int)mode); \
  2335. action->setCheckable(true); \
  2336. action->setChecked(deinterlaceMode == mode);
  2337. ADD_MODE("Disable", OBS_DEINTERLACE_MODE_DISABLE);
  2338. ADD_MODE("Deinterlacing.Discard", OBS_DEINTERLACE_MODE_DISCARD);
  2339. ADD_MODE("Deinterlacing.Retro", OBS_DEINTERLACE_MODE_RETRO);
  2340. ADD_MODE("Deinterlacing.Blend", OBS_DEINTERLACE_MODE_BLEND);
  2341. ADD_MODE("Deinterlacing.Blend2x", OBS_DEINTERLACE_MODE_BLEND_2X);
  2342. ADD_MODE("Deinterlacing.Linear", OBS_DEINTERLACE_MODE_LINEAR);
  2343. ADD_MODE("Deinterlacing.Linear2x", OBS_DEINTERLACE_MODE_LINEAR_2X);
  2344. ADD_MODE("Deinterlacing.Yadif", OBS_DEINTERLACE_MODE_YADIF);
  2345. ADD_MODE("Deinterlacing.Yadif2x", OBS_DEINTERLACE_MODE_YADIF_2X);
  2346. #undef ADD_MODE
  2347. menu->addSeparator();
  2348. #define ADD_ORDER(name, order) \
  2349. action = menu->addAction(QTStr("Deinterlacing." name), this, \
  2350. SLOT(SetDeinterlacingOrder())); \
  2351. action->setProperty("order", (int)order); \
  2352. action->setCheckable(true); \
  2353. action->setChecked(deinterlaceOrder == order);
  2354. ADD_ORDER("TopFieldFirst", OBS_DEINTERLACE_FIELD_ORDER_TOP);
  2355. ADD_ORDER("BottomFieldFirst", OBS_DEINTERLACE_FIELD_ORDER_BOTTOM);
  2356. #undef ADD_ORDER
  2357. return menu;
  2358. }
  2359. void OBSBasic::SetScaleFilter()
  2360. {
  2361. QAction *action = reinterpret_cast<QAction*>(sender());
  2362. obs_scale_type mode = (obs_scale_type)action->property("mode").toInt();
  2363. OBSSceneItem sceneItem = GetCurrentSceneItem();
  2364. obs_sceneitem_set_scale_filter(sceneItem, mode);
  2365. }
  2366. QMenu *OBSBasic::AddScaleFilteringMenu(obs_sceneitem_t *item)
  2367. {
  2368. QMenu *menu = new QMenu(QTStr("ScaleFiltering"));
  2369. obs_scale_type scaleFilter = obs_sceneitem_get_scale_filter(item);
  2370. QAction *action;
  2371. #define ADD_MODE(name, mode) \
  2372. action = menu->addAction(QTStr("" name), this, \
  2373. SLOT(SetScaleFilter())); \
  2374. action->setProperty("mode", (int)mode); \
  2375. action->setCheckable(true); \
  2376. action->setChecked(scaleFilter == mode);
  2377. ADD_MODE("Disable", OBS_SCALE_DISABLE);
  2378. ADD_MODE("ScaleFiltering.Point", OBS_SCALE_POINT);
  2379. ADD_MODE("ScaleFiltering.Bilinear", OBS_SCALE_BILINEAR);
  2380. ADD_MODE("ScaleFiltering.Bicubic", OBS_SCALE_BICUBIC);
  2381. ADD_MODE("ScaleFiltering.Lanczos", OBS_SCALE_LANCZOS);
  2382. #undef ADD_MODE
  2383. return menu;
  2384. }
  2385. void OBSBasic::CreateSourcePopupMenu(QListWidgetItem *item, bool preview)
  2386. {
  2387. QMenu popup(this);
  2388. QPointer<QMenu> previewProjector;
  2389. QPointer<QMenu> sourceProjector;
  2390. if (preview) {
  2391. QAction *action = popup.addAction(
  2392. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2393. this, SLOT(TogglePreview()));
  2394. action->setCheckable(true);
  2395. action->setChecked(
  2396. obs_display_enabled(ui->preview->GetDisplay()));
  2397. if (IsPreviewProgramMode())
  2398. action->setEnabled(false);
  2399. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2400. AddProjectorMenuMonitors(previewProjector, this,
  2401. SLOT(OpenPreviewProjector()));
  2402. popup.addMenu(previewProjector);
  2403. popup.addSeparator();
  2404. }
  2405. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  2406. if (addSourceMenu)
  2407. popup.addMenu(addSourceMenu);
  2408. if (item) {
  2409. if (addSourceMenu)
  2410. popup.addSeparator();
  2411. OBSSceneItem sceneItem = GetSceneItem(item);
  2412. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  2413. uint32_t flags = obs_source_get_output_flags(source);
  2414. bool isAsyncVideo = (flags & OBS_SOURCE_ASYNC_VIDEO) ==
  2415. OBS_SOURCE_ASYNC_VIDEO;
  2416. QAction *action;
  2417. popup.addAction(QTStr("Rename"), this,
  2418. SLOT(EditSceneItemName()));
  2419. popup.addAction(QTStr("Remove"), this,
  2420. SLOT(on_actionRemoveSource_triggered()),
  2421. DeleteKeys.front());
  2422. popup.addSeparator();
  2423. popup.addMenu(ui->orderMenu);
  2424. popup.addMenu(ui->transformMenu);
  2425. sourceProjector = new QMenu(QTStr("SourceProjector"));
  2426. AddProjectorMenuMonitors(sourceProjector, this,
  2427. SLOT(OpenSourceProjector()));
  2428. popup.addSeparator();
  2429. if (isAsyncVideo) {
  2430. popup.addMenu(AddDeinterlacingMenu(source));
  2431. popup.addSeparator();
  2432. }
  2433. popup.addMenu(AddScaleFilteringMenu(sceneItem));
  2434. popup.addSeparator();
  2435. popup.addMenu(sourceProjector);
  2436. popup.addSeparator();
  2437. action = popup.addAction(QTStr("Interact"), this,
  2438. SLOT(on_actionInteract_triggered()));
  2439. action->setEnabled(obs_source_get_output_flags(source) &
  2440. OBS_SOURCE_INTERACTION);
  2441. popup.addAction(QTStr("Filters"), this,
  2442. SLOT(OpenFilters()));
  2443. popup.addAction(QTStr("Properties"), this,
  2444. SLOT(on_actionSourceProperties_triggered()));
  2445. }
  2446. popup.exec(QCursor::pos());
  2447. }
  2448. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  2449. {
  2450. if (ui->scenes->count())
  2451. CreateSourcePopupMenu(ui->sources->itemAt(pos), false);
  2452. }
  2453. void OBSBasic::on_sources_itemDoubleClicked(QListWidgetItem *witem)
  2454. {
  2455. if (!witem)
  2456. return;
  2457. OBSSceneItem item = GetSceneItem(witem);
  2458. OBSSource source = obs_sceneitem_get_source(item);
  2459. if (source)
  2460. CreatePropertiesWindow(source);
  2461. }
  2462. void OBSBasic::AddSource(const char *id)
  2463. {
  2464. if (id && *id) {
  2465. OBSBasicSourceSelect sourceSelect(this, id);
  2466. sourceSelect.exec();
  2467. if (sourceSelect.newSource)
  2468. CreatePropertiesWindow(sourceSelect.newSource);
  2469. }
  2470. }
  2471. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  2472. {
  2473. const char *type;
  2474. bool foundValues = false;
  2475. size_t idx = 0;
  2476. QMenu *popup = new QMenu(QTStr("Add"), this);
  2477. auto addSource = [this, popup] (const char *type, const char *name) {
  2478. QAction *popupItem = new QAction(QT_UTF8(name), this);
  2479. popupItem->setData(QT_UTF8(type));
  2480. connect(popupItem, SIGNAL(triggered(bool)),
  2481. this, SLOT(AddSourceFromAction()));
  2482. popup->addAction(popupItem);
  2483. };
  2484. while (obs_enum_input_types(idx++, &type)) {
  2485. const char *name = obs_source_get_display_name(type);
  2486. addSource(type, name);
  2487. foundValues = true;
  2488. }
  2489. addSource("scene", Str("Basic.Scene"));
  2490. if (!foundValues) {
  2491. delete popup;
  2492. popup = nullptr;
  2493. }
  2494. return popup;
  2495. }
  2496. void OBSBasic::AddSourceFromAction()
  2497. {
  2498. QAction *action = qobject_cast<QAction*>(sender());
  2499. if (!action)
  2500. return;
  2501. AddSource(QT_TO_UTF8(action->data().toString()));
  2502. }
  2503. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  2504. {
  2505. if (!GetCurrentScene()) {
  2506. // Tell the user he needs a scene first (help beginners).
  2507. QMessageBox::information(this,
  2508. QTStr("Basic.Main.AddSourceHelp.Title"),
  2509. QTStr("Basic.Main.AddSourceHelp.Text"));
  2510. return;
  2511. }
  2512. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  2513. if (popup)
  2514. popup->exec(pos);
  2515. }
  2516. void OBSBasic::on_actionAddSource_triggered()
  2517. {
  2518. AddSourcePopupMenu(QCursor::pos());
  2519. }
  2520. void OBSBasic::on_actionRemoveSource_triggered()
  2521. {
  2522. vector<OBSSceneItem> items;
  2523. auto func = [] (obs_scene_t *, obs_sceneitem_t *item, void *param)
  2524. {
  2525. vector<OBSSceneItem> &items =
  2526. *reinterpret_cast<vector<OBSSceneItem>*>(param);
  2527. if (obs_sceneitem_selected(item))
  2528. items.emplace_back(item);
  2529. return true;
  2530. };
  2531. obs_scene_enum_items(GetCurrentScene(), func, &items);
  2532. if (!items.size())
  2533. return;
  2534. auto removeMultiple = [this] (size_t count)
  2535. {
  2536. QString text = QTStr("ConfirmRemove.TextMultiple")
  2537. .arg(QString::number(count));
  2538. QMessageBox remove_items(this);
  2539. remove_items.setText(text);
  2540. QAbstractButton *Yes = remove_items.addButton(QTStr("Yes"),
  2541. QMessageBox::YesRole);
  2542. remove_items.addButton(QTStr("No"), QMessageBox::NoRole);
  2543. remove_items.setIcon(QMessageBox::Question);
  2544. remove_items.setWindowTitle(QTStr("ConfirmRemove.Title"));
  2545. remove_items.exec();
  2546. return Yes == remove_items.clickedButton();
  2547. };
  2548. if (items.size() == 1) {
  2549. OBSSceneItem &item = items[0];
  2550. obs_source_t *source = obs_sceneitem_get_source(item);
  2551. if (source && QueryRemoveSource(source))
  2552. obs_sceneitem_remove(item);
  2553. } else {
  2554. if (removeMultiple(items.size())) {
  2555. for (auto &item : items)
  2556. obs_sceneitem_remove(item);
  2557. }
  2558. }
  2559. }
  2560. void OBSBasic::on_actionInteract_triggered()
  2561. {
  2562. OBSSceneItem item = GetCurrentSceneItem();
  2563. OBSSource source = obs_sceneitem_get_source(item);
  2564. if (source)
  2565. CreateInteractionWindow(source);
  2566. }
  2567. void OBSBasic::on_actionSourceProperties_triggered()
  2568. {
  2569. OBSSceneItem item = GetCurrentSceneItem();
  2570. OBSSource source = obs_sceneitem_get_source(item);
  2571. if (source)
  2572. CreatePropertiesWindow(source);
  2573. }
  2574. void OBSBasic::on_actionSourceUp_triggered()
  2575. {
  2576. OBSSceneItem item = GetCurrentSceneItem();
  2577. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  2578. }
  2579. void OBSBasic::on_actionSourceDown_triggered()
  2580. {
  2581. OBSSceneItem item = GetCurrentSceneItem();
  2582. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  2583. }
  2584. void OBSBasic::on_actionMoveUp_triggered()
  2585. {
  2586. OBSSceneItem item = GetCurrentSceneItem();
  2587. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  2588. }
  2589. void OBSBasic::on_actionMoveDown_triggered()
  2590. {
  2591. OBSSceneItem item = GetCurrentSceneItem();
  2592. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  2593. }
  2594. void OBSBasic::on_actionMoveToTop_triggered()
  2595. {
  2596. OBSSceneItem item = GetCurrentSceneItem();
  2597. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_TOP);
  2598. }
  2599. void OBSBasic::on_actionMoveToBottom_triggered()
  2600. {
  2601. OBSSceneItem item = GetCurrentSceneItem();
  2602. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_BOTTOM);
  2603. }
  2604. static BPtr<char> ReadLogFile(const char *log)
  2605. {
  2606. char logDir[512];
  2607. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2608. return nullptr;
  2609. string path = (char*)logDir;
  2610. path += "/";
  2611. path += log;
  2612. BPtr<char> file = os_quick_read_utf8_file(path.c_str());
  2613. if (!file)
  2614. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  2615. return file;
  2616. }
  2617. void OBSBasic::UploadLog(const char *file)
  2618. {
  2619. BPtr<char> fileString{ReadLogFile(file)};
  2620. if (!fileString)
  2621. return;
  2622. if (!*fileString)
  2623. return;
  2624. ui->menuLogFiles->setEnabled(false);
  2625. auto data_deleter = [](obs_data_t *d) { obs_data_release(d); };
  2626. using data_t = unique_ptr<struct obs_data, decltype(data_deleter)>;
  2627. data_t content{obs_data_create(), data_deleter};
  2628. data_t files{obs_data_create(), data_deleter};
  2629. data_t request{obs_data_create(), data_deleter};
  2630. obs_data_set_string(content.get(), "content", fileString);
  2631. obs_data_set_obj(files.get(), file, content.get());
  2632. stringstream ss;
  2633. ss << "OBS " << App()->GetVersionString()
  2634. << " log file uploaded at " << CurrentDateTimeString();
  2635. obs_data_set_string(request.get(), "description", ss.str().c_str());
  2636. obs_data_set_bool(request.get(), "public", false);
  2637. obs_data_set_obj(request.get(), "files", files.get());
  2638. const char *json = obs_data_get_json(request.get());
  2639. if (!json) {
  2640. blog(LOG_ERROR, "Failed to get JSON data for log upload");
  2641. return;
  2642. }
  2643. QBuffer *postData = new QBuffer();
  2644. postData->setData(json, (int) strlen(json));
  2645. if (logUploadThread) {
  2646. logUploadThread->wait();
  2647. delete logUploadThread;
  2648. }
  2649. RemoteTextThread *thread = new RemoteTextThread(
  2650. "https://api.github.com/gists",
  2651. "application/json", json);
  2652. logUploadThread = thread;
  2653. connect(thread, &RemoteTextThread::Result,
  2654. this, &OBSBasic::logUploadFinished);
  2655. logUploadThread->start();
  2656. }
  2657. void OBSBasic::on_actionShowLogs_triggered()
  2658. {
  2659. char logDir[512];
  2660. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2661. return;
  2662. QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
  2663. QDesktopServices::openUrl(url);
  2664. }
  2665. void OBSBasic::on_actionUploadCurrentLog_triggered()
  2666. {
  2667. UploadLog(App()->GetCurrentLog());
  2668. }
  2669. void OBSBasic::on_actionUploadLastLog_triggered()
  2670. {
  2671. UploadLog(App()->GetLastLog());
  2672. }
  2673. void OBSBasic::on_actionViewCurrentLog_triggered()
  2674. {
  2675. char logDir[512];
  2676. if (GetConfigPath(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  2677. return;
  2678. const char* log = App()->GetCurrentLog();
  2679. string path = (char*)logDir;
  2680. path += "/";
  2681. path += log;
  2682. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  2683. QDesktopServices::openUrl(url);
  2684. }
  2685. void OBSBasic::on_actionCheckForUpdates_triggered()
  2686. {
  2687. CheckForUpdates();
  2688. }
  2689. void OBSBasic::logUploadFinished(const QString &text, const QString &error)
  2690. {
  2691. ui->menuLogFiles->setEnabled(true);
  2692. if (text.isEmpty()) {
  2693. QMessageBox::information(this,
  2694. QTStr("LogReturnDialog.ErrorUploadingLog"),
  2695. error);
  2696. return;
  2697. }
  2698. obs_data_t *returnData = obs_data_create_from_json(QT_TO_UTF8(text));
  2699. QString logURL = obs_data_get_string(returnData, "html_url");
  2700. obs_data_release(returnData);
  2701. OBSLogReply logDialog(this, logURL);
  2702. logDialog.exec();
  2703. }
  2704. static void RenameListItem(OBSBasic *parent, QListWidget *listWidget,
  2705. obs_source_t *source, const string &name)
  2706. {
  2707. const char *prevName = obs_source_get_name(source);
  2708. if (name == prevName)
  2709. return;
  2710. obs_source_t *foundSource = obs_get_source_by_name(name.c_str());
  2711. QListWidgetItem *listItem = listWidget->currentItem();
  2712. if (foundSource || name.empty()) {
  2713. listItem->setText(QT_UTF8(prevName));
  2714. if (foundSource) {
  2715. QMessageBox::information(parent,
  2716. QTStr("NameExists.Title"),
  2717. QTStr("NameExists.Text"));
  2718. } else if (name.empty()) {
  2719. QMessageBox::information(parent,
  2720. QTStr("NoNameEntered.Title"),
  2721. QTStr("NoNameEntered.Text"));
  2722. }
  2723. obs_source_release(foundSource);
  2724. } else {
  2725. listItem->setText(QT_UTF8(name.c_str()));
  2726. obs_source_set_name(source, name.c_str());
  2727. }
  2728. }
  2729. void OBSBasic::SceneNameEdited(QWidget *editor,
  2730. QAbstractItemDelegate::EndEditHint endHint)
  2731. {
  2732. OBSScene scene = GetCurrentScene();
  2733. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2734. string text = QT_TO_UTF8(edit->text().trimmed());
  2735. if (!scene)
  2736. return;
  2737. obs_source_t *source = obs_scene_get_source(scene);
  2738. RenameListItem(this, ui->scenes, source, text);
  2739. UNUSED_PARAMETER(endHint);
  2740. }
  2741. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  2742. QAbstractItemDelegate::EndEditHint endHint)
  2743. {
  2744. OBSSceneItem item = GetCurrentSceneItem();
  2745. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  2746. string text = QT_TO_UTF8(edit->text().trimmed());
  2747. if (!item)
  2748. return;
  2749. obs_source_t *source = obs_sceneitem_get_source(item);
  2750. RenameListItem(this, ui->sources, source, text);
  2751. QListWidgetItem *listItem = ui->sources->currentItem();
  2752. listItem->setText(QString());
  2753. SetupVisibilityItem(ui->sources, listItem, item);
  2754. UNUSED_PARAMETER(endHint);
  2755. }
  2756. void OBSBasic::OpenFilters()
  2757. {
  2758. OBSSceneItem item = GetCurrentSceneItem();
  2759. OBSSource source = obs_sceneitem_get_source(item);
  2760. CreateFiltersWindow(source);
  2761. }
  2762. void OBSBasic::OpenSceneFilters()
  2763. {
  2764. OBSScene scene = GetCurrentScene();
  2765. OBSSource source = obs_scene_get_source(scene);
  2766. CreateFiltersWindow(source);
  2767. }
  2768. #define RECORDING_START \
  2769. "==== Recording Start ==============================================="
  2770. #define RECORDING_STOP \
  2771. "==== Recording Stop ================================================"
  2772. #define STREAMING_START \
  2773. "==== Streaming Start ==============================================="
  2774. #define STREAMING_STOP \
  2775. "==== Streaming Stop ================================================"
  2776. void OBSBasic::StartStreaming()
  2777. {
  2778. SaveProject();
  2779. ui->streamButton->setEnabled(false);
  2780. ui->streamButton->setText(QTStr("Basic.Main.Connecting"));
  2781. if (!outputHandler->StartStreaming(service)) {
  2782. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2783. ui->streamButton->setEnabled(true);
  2784. }
  2785. bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
  2786. "BasicWindow", "RecordWhenStreaming");
  2787. if (recordWhenStreaming)
  2788. StartRecording();
  2789. }
  2790. #ifdef _WIN32
  2791. static inline void UpdateProcessPriority()
  2792. {
  2793. const char *priority = config_get_string(App()->GlobalConfig(),
  2794. "General", "ProcessPriority");
  2795. if (priority && strcmp(priority, "Normal") != 0)
  2796. SetProcessPriority(priority);
  2797. }
  2798. static inline void ClearProcessPriority()
  2799. {
  2800. const char *priority = config_get_string(App()->GlobalConfig(),
  2801. "General", "ProcessPriority");
  2802. if (priority && strcmp(priority, "Normal") != 0)
  2803. SetProcessPriority("Normal");
  2804. }
  2805. #else
  2806. #define UpdateProcessPriority() do {} while(false)
  2807. #define ClearProcessPriority() do {} while(false)
  2808. #endif
  2809. void OBSBasic::StopStreaming()
  2810. {
  2811. SaveProject();
  2812. if (outputHandler->StreamingActive())
  2813. outputHandler->StopStreaming();
  2814. if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
  2815. ui->profileMenu->setEnabled(true);
  2816. App()->DecrementSleepInhibition();
  2817. ClearProcessPriority();
  2818. }
  2819. bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
  2820. "BasicWindow", "RecordWhenStreaming");
  2821. bool keepRecordingWhenStreamStops = config_get_bool(GetGlobalConfig(),
  2822. "BasicWindow", "KeepRecordingWhenStreamStops");
  2823. if (recordWhenStreaming && !keepRecordingWhenStreamStops)
  2824. StopRecording();
  2825. }
  2826. void OBSBasic::ForceStopStreaming()
  2827. {
  2828. SaveProject();
  2829. if (outputHandler->StreamingActive())
  2830. outputHandler->ForceStopStreaming();
  2831. if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
  2832. ui->profileMenu->setEnabled(true);
  2833. App()->DecrementSleepInhibition();
  2834. ClearProcessPriority();
  2835. }
  2836. bool recordWhenStreaming = config_get_bool(GetGlobalConfig(),
  2837. "BasicWindow", "RecordWhenStreaming");
  2838. bool keepRecordingWhenStreamStops = config_get_bool(GetGlobalConfig(),
  2839. "BasicWindow", "KeepRecordingWhenStreamStops");
  2840. if (recordWhenStreaming && !keepRecordingWhenStreamStops)
  2841. StopRecording();
  2842. }
  2843. void OBSBasic::StreamDelayStarting(int sec)
  2844. {
  2845. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2846. ui->streamButton->setEnabled(true);
  2847. if (!startStreamMenu.isNull())
  2848. startStreamMenu->deleteLater();
  2849. startStreamMenu = new QMenu();
  2850. startStreamMenu->addAction(QTStr("Basic.Main.StopStreaming"),
  2851. this, SLOT(StopStreaming()));
  2852. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  2853. this, SLOT(ForceStopStreaming()));
  2854. ui->streamButton->setMenu(startStreamMenu);
  2855. ui->statusbar->StreamDelayStarting(sec);
  2856. if (ui->profileMenu->isEnabled()) {
  2857. ui->profileMenu->setEnabled(false);
  2858. App()->IncrementSleepInhibition();
  2859. UpdateProcessPriority();
  2860. }
  2861. }
  2862. void OBSBasic::StreamDelayStopping(int sec)
  2863. {
  2864. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2865. ui->streamButton->setEnabled(true);
  2866. if (!startStreamMenu.isNull())
  2867. startStreamMenu->deleteLater();
  2868. startStreamMenu = new QMenu();
  2869. startStreamMenu->addAction(QTStr("Basic.Main.StartStreaming"),
  2870. this, SLOT(StartStreaming()));
  2871. startStreamMenu->addAction(QTStr("Basic.Main.ForceStopStreaming"),
  2872. this, SLOT(ForceStopStreaming()));
  2873. ui->streamButton->setMenu(startStreamMenu);
  2874. ui->statusbar->StreamDelayStopping(sec);
  2875. }
  2876. void OBSBasic::StreamingStart()
  2877. {
  2878. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  2879. ui->streamButton->setEnabled(true);
  2880. ui->statusbar->StreamStarted(outputHandler->streamOutput);
  2881. if (ui->profileMenu->isEnabled()) {
  2882. ui->profileMenu->setEnabled(false);
  2883. App()->IncrementSleepInhibition();
  2884. UpdateProcessPriority();
  2885. }
  2886. blog(LOG_INFO, STREAMING_START);
  2887. }
  2888. void OBSBasic::StreamStopping()
  2889. {
  2890. ui->streamButton->setText(QTStr("Basic.Main.StoppingStreaming"));
  2891. }
  2892. void OBSBasic::StreamingStop(int code)
  2893. {
  2894. const char *errorMessage;
  2895. switch (code) {
  2896. case OBS_OUTPUT_BAD_PATH:
  2897. errorMessage = Str("Output.ConnectFail.BadPath");
  2898. break;
  2899. case OBS_OUTPUT_CONNECT_FAILED:
  2900. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  2901. break;
  2902. case OBS_OUTPUT_INVALID_STREAM:
  2903. errorMessage = Str("Output.ConnectFail.InvalidStream");
  2904. break;
  2905. default:
  2906. case OBS_OUTPUT_ERROR:
  2907. errorMessage = Str("Output.ConnectFail.Error");
  2908. break;
  2909. case OBS_OUTPUT_DISCONNECTED:
  2910. /* doesn't happen if output is set to reconnect. note that
  2911. * reconnects are handled in the output, not in the UI */
  2912. errorMessage = Str("Output.ConnectFail.Disconnected");
  2913. }
  2914. ui->statusbar->StreamStopped();
  2915. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  2916. ui->streamButton->setEnabled(true);
  2917. if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
  2918. ui->profileMenu->setEnabled(true);
  2919. App()->DecrementSleepInhibition();
  2920. ClearProcessPriority();
  2921. }
  2922. blog(LOG_INFO, STREAMING_STOP);
  2923. if (code != OBS_OUTPUT_SUCCESS)
  2924. QMessageBox::information(this,
  2925. QTStr("Output.ConnectFail.Title"),
  2926. QT_UTF8(errorMessage));
  2927. if (!startStreamMenu.isNull()) {
  2928. ui->streamButton->setMenu(nullptr);
  2929. startStreamMenu->deleteLater();
  2930. startStreamMenu = nullptr;
  2931. }
  2932. }
  2933. void OBSBasic::StartRecording()
  2934. {
  2935. if (outputHandler->RecordingActive())
  2936. return;
  2937. SaveProject();
  2938. outputHandler->StartRecording();
  2939. }
  2940. void OBSBasic::RecordStopping()
  2941. {
  2942. ui->recordButton->setText(QTStr("Basic.Main.StoppingRecording"));
  2943. }
  2944. void OBSBasic::StopRecording()
  2945. {
  2946. SaveProject();
  2947. if (outputHandler->RecordingActive())
  2948. outputHandler->StopRecording();
  2949. if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
  2950. ui->profileMenu->setEnabled(true);
  2951. App()->DecrementSleepInhibition();
  2952. ClearProcessPriority();
  2953. }
  2954. }
  2955. void OBSBasic::RecordingStart()
  2956. {
  2957. ui->statusbar->RecordingStarted(outputHandler->fileOutput);
  2958. ui->recordButton->setText(QTStr("Basic.Main.StopRecording"));
  2959. if (ui->profileMenu->isEnabled()) {
  2960. ui->profileMenu->setEnabled(false);
  2961. App()->IncrementSleepInhibition();
  2962. UpdateProcessPriority();
  2963. }
  2964. blog(LOG_INFO, RECORDING_START);
  2965. }
  2966. void OBSBasic::RecordingStop(int code)
  2967. {
  2968. ui->statusbar->RecordingStopped();
  2969. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  2970. blog(LOG_INFO, RECORDING_STOP);
  2971. if (code == OBS_OUTPUT_UNSUPPORTED) {
  2972. QMessageBox::information(this,
  2973. QTStr("Output.RecordFail.Title"),
  2974. QTStr("Output.RecordFail.Unsupported"));
  2975. } else if (code == OBS_OUTPUT_NO_SPACE) {
  2976. QMessageBox::information(this,
  2977. QTStr("Output.RecordNoSpace.Title"),
  2978. QTStr("Output.RecordNoSpace.Msg"));
  2979. } else if (code != OBS_OUTPUT_SUCCESS) {
  2980. QMessageBox::information(this,
  2981. QTStr("Output.RecordError.Title"),
  2982. QTStr("Output.RecordError.Msg"));
  2983. }
  2984. if (!outputHandler->Active() && !ui->profileMenu->isEnabled()) {
  2985. ui->profileMenu->setEnabled(true);
  2986. App()->DecrementSleepInhibition();
  2987. ClearProcessPriority();
  2988. }
  2989. }
  2990. void OBSBasic::on_streamButton_clicked()
  2991. {
  2992. if (outputHandler->StreamingActive()) {
  2993. bool confirm = config_get_bool(GetGlobalConfig(), "BasicWindow",
  2994. "WarnBeforeStoppingStream");
  2995. if (confirm) {
  2996. QMessageBox::StandardButton button =
  2997. QMessageBox::question(this,
  2998. QTStr("ConfirmStop.Title"),
  2999. QTStr("ConfirmStop.Text"));
  3000. if (button == QMessageBox::No)
  3001. return;
  3002. }
  3003. StopStreaming();
  3004. } else {
  3005. bool confirm = config_get_bool(GetGlobalConfig(), "BasicWindow",
  3006. "WarnBeforeStartingStream");
  3007. if (confirm) {
  3008. QMessageBox::StandardButton button =
  3009. QMessageBox::question(this,
  3010. QTStr("ConfirmStart.Title"),
  3011. QTStr("ConfirmStart.Text"));
  3012. if (button == QMessageBox::No)
  3013. return;
  3014. }
  3015. StartStreaming();
  3016. }
  3017. }
  3018. void OBSBasic::on_recordButton_clicked()
  3019. {
  3020. if (outputHandler->RecordingActive())
  3021. StopRecording();
  3022. else
  3023. StartRecording();
  3024. }
  3025. void OBSBasic::on_settingsButton_clicked()
  3026. {
  3027. OBSBasicSettings settings(this);
  3028. settings.exec();
  3029. }
  3030. void OBSBasic::on_actionWebsite_triggered()
  3031. {
  3032. QUrl url = QUrl("https://obsproject.com", QUrl::TolerantMode);
  3033. QDesktopServices::openUrl(url);
  3034. }
  3035. void OBSBasic::on_actionShowSettingsFolder_triggered()
  3036. {
  3037. char path[512];
  3038. int ret = GetConfigPath(path, 512, "obs-studio");
  3039. if (ret <= 0)
  3040. return;
  3041. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  3042. }
  3043. void OBSBasic::on_actionShowProfileFolder_triggered()
  3044. {
  3045. char path[512];
  3046. int ret = GetProfilePath(path, 512, "");
  3047. if (ret <= 0)
  3048. return;
  3049. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  3050. }
  3051. QListWidgetItem *OBSBasic::GetTopSelectedSourceItem()
  3052. {
  3053. QList<QListWidgetItem*> selectedItems = ui->sources->selectedItems();
  3054. QListWidgetItem *topItem = nullptr;
  3055. if (selectedItems.size() != 0)
  3056. topItem = selectedItems[0];
  3057. return topItem;
  3058. }
  3059. void OBSBasic::on_preview_customContextMenuRequested(const QPoint &pos)
  3060. {
  3061. CreateSourcePopupMenu(GetTopSelectedSourceItem(), true);
  3062. UNUSED_PARAMETER(pos);
  3063. }
  3064. void OBSBasic::on_previewDisabledLabel_customContextMenuRequested(
  3065. const QPoint &pos)
  3066. {
  3067. QMenu popup(this);
  3068. QPointer<QMenu> previewProjector;
  3069. QAction *action = popup.addAction(
  3070. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  3071. this, SLOT(TogglePreview()));
  3072. action->setCheckable(true);
  3073. action->setChecked(obs_display_enabled(ui->preview->GetDisplay()));
  3074. previewProjector = new QMenu(QTStr("PreviewProjector"));
  3075. AddProjectorMenuMonitors(previewProjector, this,
  3076. SLOT(OpenPreviewProjector()));
  3077. popup.addMenu(previewProjector);
  3078. popup.exec(QCursor::pos());
  3079. UNUSED_PARAMETER(pos);
  3080. }
  3081. void OBSBasic::on_actionAlwaysOnTop_triggered()
  3082. {
  3083. CloseDialogs();
  3084. /* Make sure all dialogs are safely and successfully closed before
  3085. * switching the always on top mode due to the fact that windows all
  3086. * have to be recreated, so queue the actual toggle to happen after
  3087. * all events related to closing the dialogs have finished */
  3088. QMetaObject::invokeMethod(this, "ToggleAlwaysOnTop",
  3089. Qt::QueuedConnection);
  3090. }
  3091. void OBSBasic::ToggleAlwaysOnTop()
  3092. {
  3093. bool isAlwaysOnTop = IsAlwaysOnTop(this);
  3094. ui->actionAlwaysOnTop->setChecked(!isAlwaysOnTop);
  3095. SetAlwaysOnTop(this, !isAlwaysOnTop);
  3096. show();
  3097. }
  3098. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  3099. {
  3100. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  3101. if (strcmp(val, "10") == 0) {
  3102. num = 10;
  3103. den = 1;
  3104. } else if (strcmp(val, "20") == 0) {
  3105. num = 20;
  3106. den = 1;
  3107. } else if (strcmp(val, "25") == 0) {
  3108. num = 25;
  3109. den = 1;
  3110. } else if (strcmp(val, "29.97") == 0) {
  3111. num = 30000;
  3112. den = 1001;
  3113. } else if (strcmp(val, "48") == 0) {
  3114. num = 48;
  3115. den = 1;
  3116. } else if (strcmp(val, "59.94") == 0) {
  3117. num = 60000;
  3118. den = 1001;
  3119. } else if (strcmp(val, "60") == 0) {
  3120. num = 60;
  3121. den = 1;
  3122. } else {
  3123. num = 30;
  3124. den = 1;
  3125. }
  3126. }
  3127. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  3128. {
  3129. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  3130. den = 1;
  3131. }
  3132. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  3133. {
  3134. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  3135. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  3136. }
  3137. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  3138. {
  3139. num = 1000000000;
  3140. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  3141. }
  3142. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  3143. {
  3144. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  3145. if (type == 1) //"Integer"
  3146. GetFPSInteger(num, den);
  3147. else if (type == 2) //"Fraction"
  3148. GetFPSFraction(num, den);
  3149. else if (false) //"Nanoseconds", currently not implemented
  3150. GetFPSNanoseconds(num, den);
  3151. else
  3152. GetFPSCommon(num, den);
  3153. }
  3154. config_t *OBSBasic::Config() const
  3155. {
  3156. return basicConfig;
  3157. }
  3158. void OBSBasic::on_actionEditTransform_triggered()
  3159. {
  3160. if (transformWindow)
  3161. transformWindow->close();
  3162. transformWindow = new OBSBasicTransform(this);
  3163. transformWindow->show();
  3164. transformWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  3165. }
  3166. void OBSBasic::on_actionResetTransform_triggered()
  3167. {
  3168. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  3169. {
  3170. if (!obs_sceneitem_selected(item))
  3171. return true;
  3172. obs_sceneitem_defer_update_begin(item);
  3173. obs_transform_info info;
  3174. vec2_set(&info.pos, 0.0f, 0.0f);
  3175. vec2_set(&info.scale, 1.0f, 1.0f);
  3176. info.rot = 0.0f;
  3177. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  3178. info.bounds_type = OBS_BOUNDS_NONE;
  3179. info.bounds_alignment = OBS_ALIGN_CENTER;
  3180. vec2_set(&info.bounds, 0.0f, 0.0f);
  3181. obs_sceneitem_set_info(item, &info);
  3182. obs_sceneitem_crop crop = {};
  3183. obs_sceneitem_set_crop(item, &crop);
  3184. obs_sceneitem_defer_update_end(item);
  3185. UNUSED_PARAMETER(scene);
  3186. UNUSED_PARAMETER(param);
  3187. return true;
  3188. };
  3189. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  3190. }
  3191. static void GetItemBox(obs_sceneitem_t *item, vec3 &tl, vec3 &br)
  3192. {
  3193. matrix4 boxTransform;
  3194. obs_sceneitem_get_box_transform(item, &boxTransform);
  3195. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  3196. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  3197. auto GetMinPos = [&] (float x, float y)
  3198. {
  3199. vec3 pos;
  3200. vec3_set(&pos, x, y, 0.0f);
  3201. vec3_transform(&pos, &pos, &boxTransform);
  3202. vec3_min(&tl, &tl, &pos);
  3203. vec3_max(&br, &br, &pos);
  3204. };
  3205. GetMinPos(0.0f, 0.0f);
  3206. GetMinPos(1.0f, 0.0f);
  3207. GetMinPos(0.0f, 1.0f);
  3208. GetMinPos(1.0f, 1.0f);
  3209. }
  3210. static vec3 GetItemTL(obs_sceneitem_t *item)
  3211. {
  3212. vec3 tl, br;
  3213. GetItemBox(item, tl, br);
  3214. return tl;
  3215. }
  3216. static void SetItemTL(obs_sceneitem_t *item, const vec3 &tl)
  3217. {
  3218. vec3 newTL;
  3219. vec2 pos;
  3220. obs_sceneitem_get_pos(item, &pos);
  3221. newTL = GetItemTL(item);
  3222. pos.x += tl.x - newTL.x;
  3223. pos.y += tl.y - newTL.y;
  3224. obs_sceneitem_set_pos(item, &pos);
  3225. }
  3226. static bool RotateSelectedSources(obs_scene_t *scene, obs_sceneitem_t *item,
  3227. void *param)
  3228. {
  3229. if (!obs_sceneitem_selected(item))
  3230. return true;
  3231. float rot = *reinterpret_cast<float*>(param);
  3232. vec3 tl = GetItemTL(item);
  3233. rot += obs_sceneitem_get_rot(item);
  3234. if (rot >= 360.0f) rot -= 360.0f;
  3235. else if (rot <= -360.0f) rot += 360.0f;
  3236. obs_sceneitem_set_rot(item, rot);
  3237. SetItemTL(item, tl);
  3238. UNUSED_PARAMETER(scene);
  3239. UNUSED_PARAMETER(param);
  3240. return true;
  3241. };
  3242. void OBSBasic::on_actionRotate90CW_triggered()
  3243. {
  3244. float f90CW = 90.0f;
  3245. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  3246. }
  3247. void OBSBasic::on_actionRotate90CCW_triggered()
  3248. {
  3249. float f90CCW = -90.0f;
  3250. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  3251. }
  3252. void OBSBasic::on_actionRotate180_triggered()
  3253. {
  3254. float f180 = 180.0f;
  3255. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  3256. }
  3257. static bool MultiplySelectedItemScale(obs_scene_t *scene, obs_sceneitem_t *item,
  3258. void *param)
  3259. {
  3260. vec2 &mul = *reinterpret_cast<vec2*>(param);
  3261. if (!obs_sceneitem_selected(item))
  3262. return true;
  3263. vec3 tl = GetItemTL(item);
  3264. vec2 scale;
  3265. obs_sceneitem_get_scale(item, &scale);
  3266. vec2_mul(&scale, &scale, &mul);
  3267. obs_sceneitem_set_scale(item, &scale);
  3268. SetItemTL(item, tl);
  3269. UNUSED_PARAMETER(scene);
  3270. return true;
  3271. }
  3272. void OBSBasic::on_actionFlipHorizontal_triggered()
  3273. {
  3274. vec2 scale;
  3275. vec2_set(&scale, -1.0f, 1.0f);
  3276. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  3277. &scale);
  3278. }
  3279. void OBSBasic::on_actionFlipVertical_triggered()
  3280. {
  3281. vec2 scale;
  3282. vec2_set(&scale, 1.0f, -1.0f);
  3283. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  3284. &scale);
  3285. }
  3286. static bool CenterAlignSelectedItems(obs_scene_t *scene, obs_sceneitem_t *item,
  3287. void *param)
  3288. {
  3289. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  3290. if (!obs_sceneitem_selected(item))
  3291. return true;
  3292. obs_video_info ovi;
  3293. obs_get_video_info(&ovi);
  3294. obs_transform_info itemInfo;
  3295. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  3296. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  3297. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  3298. itemInfo.rot = 0.0f;
  3299. vec2_set(&itemInfo.bounds,
  3300. float(ovi.base_width), float(ovi.base_height));
  3301. itemInfo.bounds_type = boundsType;
  3302. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  3303. obs_sceneitem_set_info(item, &itemInfo);
  3304. UNUSED_PARAMETER(scene);
  3305. return true;
  3306. }
  3307. void OBSBasic::on_actionFitToScreen_triggered()
  3308. {
  3309. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  3310. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  3311. &boundsType);
  3312. }
  3313. void OBSBasic::on_actionStretchToScreen_triggered()
  3314. {
  3315. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  3316. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  3317. &boundsType);
  3318. }
  3319. void OBSBasic::on_actionCenterToScreen_triggered()
  3320. {
  3321. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  3322. {
  3323. vec3 tl, br, itemCenter, screenCenter, offset;
  3324. obs_video_info ovi;
  3325. if (!obs_sceneitem_selected(item))
  3326. return true;
  3327. obs_get_video_info(&ovi);
  3328. vec3_set(&screenCenter, float(ovi.base_width),
  3329. float(ovi.base_height), 0.0f);
  3330. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  3331. GetItemBox(item, tl, br);
  3332. vec3_sub(&itemCenter, &br, &tl);
  3333. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  3334. vec3_add(&itemCenter, &itemCenter, &tl);
  3335. vec3_sub(&offset, &screenCenter, &itemCenter);
  3336. vec3_add(&tl, &tl, &offset);
  3337. SetItemTL(item, tl);
  3338. UNUSED_PARAMETER(scene);
  3339. UNUSED_PARAMETER(param);
  3340. return true;
  3341. };
  3342. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  3343. }
  3344. void OBSBasic::EnablePreviewDisplay(bool enable)
  3345. {
  3346. obs_display_set_enabled(ui->preview->GetDisplay(), enable);
  3347. ui->preview->setVisible(enable);
  3348. ui->previewDisabledLabel->setVisible(!enable);
  3349. }
  3350. void OBSBasic::TogglePreview()
  3351. {
  3352. previewEnabled = !previewEnabled;
  3353. EnablePreviewDisplay(previewEnabled);
  3354. }
  3355. void OBSBasic::Nudge(int dist, MoveDir dir)
  3356. {
  3357. struct MoveInfo {
  3358. float dist;
  3359. MoveDir dir;
  3360. } info = {(float)dist, dir};
  3361. auto func = [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  3362. {
  3363. MoveInfo *info = reinterpret_cast<MoveInfo*>(param);
  3364. struct vec2 dir;
  3365. struct vec2 pos;
  3366. vec2_set(&dir, 0.0f, 0.0f);
  3367. if (!obs_sceneitem_selected(item))
  3368. return true;
  3369. switch (info->dir) {
  3370. case MoveDir::Up: dir.y = -info->dist; break;
  3371. case MoveDir::Down: dir.y = info->dist; break;
  3372. case MoveDir::Left: dir.x = -info->dist; break;
  3373. case MoveDir::Right: dir.x = info->dist; break;
  3374. }
  3375. obs_sceneitem_get_pos(item, &pos);
  3376. vec2_add(&pos, &pos, &dir);
  3377. obs_sceneitem_set_pos(item, &pos);
  3378. return true;
  3379. };
  3380. obs_scene_enum_items(GetCurrentScene(), func, &info);
  3381. }
  3382. void OBSBasic::NudgeUp() {Nudge(1, MoveDir::Up);}
  3383. void OBSBasic::NudgeDown() {Nudge(1, MoveDir::Down);}
  3384. void OBSBasic::NudgeLeft() {Nudge(1, MoveDir::Left);}
  3385. void OBSBasic::NudgeRight() {Nudge(1, MoveDir::Right);}
  3386. void OBSBasic::OpenProjector(obs_source_t *source, int monitor)
  3387. {
  3388. /* seriously? 10 monitors? */
  3389. if (monitor > 9)
  3390. return;
  3391. delete projectors[monitor];
  3392. projectors[monitor].clear();
  3393. OBSProjector *projector = new OBSProjector(nullptr, source);
  3394. projector->Init(monitor);
  3395. projectors[monitor] = projector;
  3396. }
  3397. void OBSBasic::OpenPreviewProjector()
  3398. {
  3399. int monitor = sender()->property("monitor").toInt();
  3400. OpenProjector(nullptr, monitor);
  3401. }
  3402. void OBSBasic::OpenSourceProjector()
  3403. {
  3404. int monitor = sender()->property("monitor").toInt();
  3405. OBSSceneItem item = GetCurrentSceneItem();
  3406. if (!item)
  3407. return;
  3408. OpenProjector(obs_sceneitem_get_source(item), monitor);
  3409. }
  3410. void OBSBasic::OpenSceneProjector()
  3411. {
  3412. int monitor = sender()->property("monitor").toInt();
  3413. OBSScene scene = GetCurrentScene();
  3414. if (!scene)
  3415. return;
  3416. OpenProjector(obs_scene_get_source(scene), monitor);
  3417. }
  3418. void OBSBasic::UpdateTitleBar()
  3419. {
  3420. stringstream name;
  3421. const char *profile = config_get_string(App()->GlobalConfig(),
  3422. "Basic", "Profile");
  3423. const char *sceneCollection = config_get_string(App()->GlobalConfig(),
  3424. "Basic", "SceneCollection");
  3425. name << "OBS ";
  3426. if (previewProgramMode)
  3427. name << "Studio ";
  3428. name << App()->GetVersionString();
  3429. name << " - " << Str("TitleBar.Profile") << ": " << profile;
  3430. name << " - " << Str("TitleBar.Scenes") << ": " << sceneCollection;
  3431. setWindowTitle(QT_UTF8(name.str().c_str()));
  3432. }
  3433. int OBSBasic::GetProfilePath(char *path, size_t size, const char *file) const
  3434. {
  3435. char profiles_path[512];
  3436. const char *profile = config_get_string(App()->GlobalConfig(),
  3437. "Basic", "ProfileDir");
  3438. int ret;
  3439. if (!profile)
  3440. return -1;
  3441. if (!path)
  3442. return -1;
  3443. if (!file)
  3444. file = "";
  3445. ret = GetConfigPath(profiles_path, 512, "obs-studio/basic/profiles");
  3446. if (ret <= 0)
  3447. return ret;
  3448. if (!*file)
  3449. return snprintf(path, size, "%s/%s", profiles_path, profile);
  3450. return snprintf(path, size, "%s/%s/%s", profiles_path, profile, file);
  3451. }
  3452. void OBSBasic::on_toggleSceneTransitions_toggled(bool visible)
  3453. {
  3454. ui->sceneTransitionsLabel->setVisible(visible);
  3455. ui->transitionsContainer->setVisible(visible);
  3456. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  3457. "ShowTransitions", visible);
  3458. }
  3459. void OBSBasic::on_toggleListboxToolbars_toggled(bool visible)
  3460. {
  3461. ui->sourcesToolbar->setVisible(visible);
  3462. ui->scenesToolbar->setVisible(visible);
  3463. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  3464. "ShowListboxToolbars", visible);
  3465. }
  3466. void OBSBasic::on_toggleStatusBar_toggled(bool visible)
  3467. {
  3468. ui->statusbar->setVisible(visible);
  3469. config_set_bool(App()->GlobalConfig(), "BasicWindow",
  3470. "ShowStatusBar", visible);
  3471. }