window-basic-main.cpp 96 KB

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