window-basic-main.cpp 96 KB

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