window-basic-main.cpp 96 KB

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