window-basic-main.cpp 95 KB

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