window-basic-main.cpp 96 KB

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