window-basic-main.cpp 112 KB

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