window-basic-main.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954
  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 <QNetworkRequest>
  23. #include <QNetworkReply>
  24. #include <util/dstr.h>
  25. #include <util/util.hpp>
  26. #include <util/platform.h>
  27. #include <graphics/math-defs.h>
  28. #include "obs-app.hpp"
  29. #include "platform.hpp"
  30. #include "visibility-item-widget.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 "ui_OBSBasic.h"
  44. #include <fstream>
  45. #include <sstream>
  46. #include <QScreen>
  47. #include <QWindow>
  48. #define PREVIEW_EDGE_SIZE 10
  49. using namespace std;
  50. Q_DECLARE_METATYPE(OBSScene);
  51. Q_DECLARE_METATYPE(OBSSceneItem);
  52. Q_DECLARE_METATYPE(OBSSource);
  53. Q_DECLARE_METATYPE(obs_order_movement);
  54. static void AddExtraModulePaths()
  55. {
  56. char base_module_dir[512];
  57. int ret = os_get_config_path(base_module_dir, sizeof(base_module_dir),
  58. "obs-studio/plugins/%module%");
  59. if (ret <= 0)
  60. return;
  61. string path = (char*)base_module_dir;
  62. obs_add_module_path((path + "/bin").c_str(), (path + "/data").c_str());
  63. }
  64. static QList<QKeySequence> DeleteKeys;
  65. OBSBasic::OBSBasic(QWidget *parent)
  66. : OBSMainWindow (parent),
  67. ui (new Ui::OBSBasic)
  68. {
  69. ui->setupUi(this);
  70. ui->previewDisabledLabel->setVisible(false);
  71. copyActionsDynamicProperties();
  72. ui->sources->setItemDelegate(new VisibilityItemDelegate(ui->sources));
  73. int width = config_get_int(App()->GlobalConfig(), "BasicWindow", "cx");
  74. // Check if no values are saved (new installation).
  75. if (width != 0) {
  76. int height = config_get_int(App()->GlobalConfig(),
  77. "BasicWindow", "cy");
  78. int posx = config_get_int(App()->GlobalConfig(), "BasicWindow",
  79. "posx");
  80. int posy = config_get_int(App()->GlobalConfig(), "BasicWindow",
  81. "posy");
  82. setGeometry(posx, posy, width, height);
  83. }
  84. char styleSheetPath[512];
  85. int ret = os_get_config_path(styleSheetPath, sizeof(styleSheetPath),
  86. "obs-studio/basic/stylesheet.qss");
  87. if (ret > 0) {
  88. if (QFile::exists(styleSheetPath)) {
  89. QString path = QString("file:///") +
  90. QT_UTF8(styleSheetPath);
  91. App()->setStyleSheet(path);
  92. }
  93. }
  94. qRegisterMetaType<OBSScene> ("OBSScene");
  95. qRegisterMetaType<OBSSceneItem>("OBSSceneItem");
  96. qRegisterMetaType<OBSSource> ("OBSSource");
  97. ui->scenes->setAttribute(Qt::WA_MacShowFocusRect, false);
  98. ui->sources->setAttribute(Qt::WA_MacShowFocusRect, false);
  99. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  100. struct obs_video_info ovi;
  101. if (obs_get_video_info(&ovi))
  102. ResizePreview(ovi.base_width, ovi.base_height);
  103. });
  104. stringstream name;
  105. name << "OBS " << App()->GetVersionString();
  106. blog(LOG_INFO, "%s", name.str().c_str());
  107. setWindowTitle(QT_UTF8(name.str().c_str()));
  108. connect(ui->scenes->itemDelegate(),
  109. SIGNAL(closeEditor(QWidget*,
  110. QAbstractItemDelegate::EndEditHint)),
  111. this,
  112. SLOT(SceneNameEdited(QWidget*,
  113. QAbstractItemDelegate::EndEditHint)));
  114. connect(ui->sources->itemDelegate(),
  115. SIGNAL(closeEditor(QWidget*,
  116. QAbstractItemDelegate::EndEditHint)),
  117. this,
  118. SLOT(SceneItemNameEdited(QWidget*,
  119. QAbstractItemDelegate::EndEditHint)));
  120. cpuUsageInfo = os_cpu_usage_info_start();
  121. cpuUsageTimer = new QTimer(this);
  122. connect(cpuUsageTimer, SIGNAL(timeout()),
  123. ui->statusbar, SLOT(UpdateCPUUsage()));
  124. cpuUsageTimer->start(3000);
  125. DeleteKeys =
  126. #ifdef __APPLE__
  127. QList<QKeySequence>{{Qt::Key_Backspace}} <<
  128. #endif
  129. QKeySequence::keyBindings(QKeySequence::Delete);
  130. #ifdef __APPLE__
  131. ui->actionRemoveSource->setShortcuts(DeleteKeys);
  132. ui->actionRemoveScene->setShortcuts(DeleteKeys);
  133. ui->action_Settings->setMenuRole(QAction::PreferencesRole);
  134. ui->actionE_xit->setMenuRole(QAction::QuitRole);
  135. #endif
  136. auto addNudge = [this](const QKeySequence &seq, const char *s)
  137. {
  138. QAction *nudge = new QAction(ui->preview);
  139. nudge->setShortcut(seq);
  140. nudge->setShortcutContext(Qt::WidgetShortcut);
  141. ui->preview->addAction(nudge);
  142. connect(nudge, SIGNAL(triggered()), this, s);
  143. };
  144. addNudge(Qt::Key_Up, SLOT(NudgeUp()));
  145. addNudge(Qt::Key_Down, SLOT(NudgeDown()));
  146. addNudge(Qt::Key_Left, SLOT(NudgeLeft()));
  147. addNudge(Qt::Key_Right, SLOT(NudgeRight()));
  148. }
  149. static void SaveAudioDevice(const char *name, int channel, obs_data_t *parent)
  150. {
  151. obs_source_t *source = obs_get_output_source(channel);
  152. if (!source)
  153. return;
  154. obs_data_t *data = obs_save_source(source);
  155. obs_data_set_obj(parent, name, data);
  156. obs_data_release(data);
  157. obs_source_release(source);
  158. }
  159. static obs_data_t *GenerateSaveData()
  160. {
  161. obs_data_t *saveData = obs_data_create();
  162. obs_data_array_t *sourcesArray = obs_save_sources();
  163. obs_source_t *currentScene = obs_get_output_source(0);
  164. const char *sceneName = obs_source_get_name(currentScene);
  165. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  166. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  167. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  168. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  169. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  170. obs_data_set_string(saveData, "current_scene", sceneName);
  171. obs_data_set_array(saveData, "sources", sourcesArray);
  172. obs_data_array_release(sourcesArray);
  173. obs_source_release(currentScene);
  174. return saveData;
  175. }
  176. void OBSBasic::copyActionsDynamicProperties()
  177. {
  178. // Themes need the QAction dynamic properties
  179. for (QAction *x : ui->scenesToolbar->actions()) {
  180. QWidget* temp = ui->scenesToolbar->widgetForAction(x);
  181. for (QByteArray &y : x->dynamicPropertyNames()) {
  182. temp->setProperty(y, x->property(y));
  183. }
  184. }
  185. for (QAction *x : ui->sourcesToolbar->actions()) {
  186. QWidget* temp = ui->sourcesToolbar->widgetForAction(x);
  187. for (QByteArray &y : x->dynamicPropertyNames()) {
  188. temp->setProperty(y, x->property(y));
  189. }
  190. }
  191. }
  192. void OBSBasic::ClearVolumeControls()
  193. {
  194. VolControl *control;
  195. for (size_t i = 0; i < volumes.size(); i++) {
  196. control = volumes[i];
  197. delete control;
  198. }
  199. volumes.clear();
  200. }
  201. void OBSBasic::Save(const char *file)
  202. {
  203. obs_data_t *saveData = GenerateSaveData();
  204. const char *jsonData = obs_data_get_json(saveData);
  205. if (!!jsonData) {
  206. /* TODO: maybe a message box here? */
  207. bool success = os_quick_write_utf8_file(file, jsonData,
  208. strlen(jsonData), false);
  209. if (!success)
  210. blog(LOG_ERROR, "Could not save scene data to %s",
  211. file);
  212. }
  213. obs_data_release(saveData);
  214. }
  215. static void LoadAudioDevice(const char *name, int channel, obs_data_t *parent)
  216. {
  217. obs_data_t *data = obs_data_get_obj(parent, name);
  218. if (!data)
  219. return;
  220. obs_source_t *source = obs_load_source(data);
  221. if (source) {
  222. obs_set_output_source(channel, source);
  223. obs_source_release(source);
  224. }
  225. obs_data_release(data);
  226. }
  227. void OBSBasic::CreateDefaultScene()
  228. {
  229. obs_scene_t *scene = obs_scene_create(Str("Basic.Scene"));
  230. obs_source_t *source = obs_scene_get_source(scene);
  231. obs_add_source(source);
  232. #ifdef __APPLE__
  233. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, "display_capture",
  234. Str("Basic.DisplayCapture"), NULL);
  235. if (source) {
  236. obs_scene_add(scene, source);
  237. obs_add_source(source);
  238. obs_source_release(source);
  239. }
  240. #endif
  241. obs_set_output_source(0, obs_scene_get_source(scene));
  242. obs_scene_release(scene);
  243. }
  244. void OBSBasic::Load(const char *file)
  245. {
  246. if (!file) {
  247. blog(LOG_ERROR, "Could not find file %s", file);
  248. return;
  249. }
  250. BPtr<char> jsonData = os_quick_read_utf8_file(file);
  251. if (!jsonData) {
  252. CreateDefaultScene();
  253. return;
  254. }
  255. obs_data_t *data = obs_data_create_from_json(jsonData);
  256. obs_data_array_t *sources = obs_data_get_array(data, "sources");
  257. const char *sceneName = obs_data_get_string(data,
  258. "current_scene");
  259. obs_source_t *curScene;
  260. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  261. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  262. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  263. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  264. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  265. obs_load_sources(sources);
  266. curScene = obs_get_source_by_name(sceneName);
  267. obs_set_output_source(0, curScene);
  268. obs_source_release(curScene);
  269. obs_data_array_release(sources);
  270. obs_data_release(data);
  271. }
  272. static inline bool HasAudioDevices(const char *source_id)
  273. {
  274. const char *output_id = source_id;
  275. obs_properties_t *props = obs_get_source_properties(
  276. OBS_SOURCE_TYPE_INPUT, output_id);
  277. size_t count = 0;
  278. if (!props)
  279. return false;
  280. obs_property_t *devices = obs_properties_get(props, "device_id");
  281. if (devices)
  282. count = obs_property_list_item_count(devices);
  283. obs_properties_destroy(props);
  284. return count != 0;
  285. }
  286. #define SERVICE_PATH "obs-studio/basic/service.json"
  287. void OBSBasic::SaveService()
  288. {
  289. if (!service)
  290. return;
  291. char serviceJsonPath[512];
  292. int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath),
  293. SERVICE_PATH);
  294. if (ret <= 0)
  295. return;
  296. obs_data_t *data = obs_data_create();
  297. obs_data_t *settings = obs_service_get_settings(service);
  298. obs_data_set_string(data, "type", obs_service_get_type(service));
  299. obs_data_set_obj(data, "settings", settings);
  300. const char *json = obs_data_get_json(data);
  301. os_quick_write_utf8_file(serviceJsonPath, json, strlen(json), false);
  302. obs_data_release(settings);
  303. obs_data_release(data);
  304. }
  305. bool OBSBasic::LoadService()
  306. {
  307. const char *type;
  308. char serviceJsonPath[512];
  309. int ret = os_get_config_path(serviceJsonPath, sizeof(serviceJsonPath),
  310. SERVICE_PATH);
  311. if (ret <= 0)
  312. return false;
  313. BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
  314. if (!jsonText)
  315. return false;
  316. obs_data_t *data = obs_data_create_from_json(jsonText);
  317. obs_data_set_default_string(data, "type", "rtmp_common");
  318. type = obs_data_get_string(data, "type");
  319. obs_data_t *settings = obs_data_get_obj(data, "settings");
  320. service = obs_service_create(type, "default_service", settings);
  321. obs_service_release(service);
  322. obs_data_release(settings);
  323. obs_data_release(data);
  324. return !!service;
  325. }
  326. bool OBSBasic::InitService()
  327. {
  328. if (LoadService())
  329. return true;
  330. service = obs_service_create("rtmp_common", "default_service", nullptr);
  331. if (!service)
  332. return false;
  333. obs_service_release(service);
  334. return true;
  335. }
  336. bool OBSBasic::InitBasicConfigDefaults()
  337. {
  338. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  339. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  340. vector<MonitorInfo> monitors;
  341. GetMonitors(monitors);
  342. if (!monitors.size()) {
  343. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  344. "technically shouldn't be possible.");
  345. return false;
  346. }
  347. uint32_t cx = monitors[0].cx;
  348. uint32_t cy = monitors[0].cy;
  349. config_set_default_string(basicConfig, "Output", "Type", "Simple");
  350. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  351. GetDefaultVideoSavePath().c_str());
  352. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  353. 2500);
  354. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 128);
  355. config_set_default_bool (basicConfig, "SimpleOutput", "Reconnect",
  356. true);
  357. config_set_default_uint (basicConfig, "SimpleOutput", "RetryDelay",
  358. 10);
  359. config_set_default_uint (basicConfig, "SimpleOutput", "MaxRetries",
  360. 20);
  361. config_set_default_bool (basicConfig, "SimpleOutput", "UseAdvanced",
  362. false);
  363. config_set_default_bool (basicConfig, "SimpleOutput", "UseCBR", true);
  364. config_set_default_bool (basicConfig, "SimpleOutput", "UseBufsize",
  365. false);
  366. config_set_default_int (basicConfig, "SimpleOutput", "Bufsize", 2500);
  367. config_set_default_string(basicConfig, "SimpleOutput", "Preset",
  368. "veryfast");
  369. config_set_default_bool (basicConfig, "AdvOut", "Reconnect", true);
  370. config_set_default_uint (basicConfig, "AdvOut", "RetryDelay", 10);
  371. config_set_default_uint (basicConfig, "AdvOut", "MaxRetries", 20);
  372. config_set_default_bool (basicConfig, "AdvOut", "ApplyServiceSettings",
  373. true);
  374. config_set_default_bool (basicConfig, "AdvOut", "UseRescale", false);
  375. config_set_default_bool (basicConfig, "AdvOut", "Multitrack", false);
  376. config_set_default_uint (basicConfig, "AdvOut", "TrackIndex", 1);
  377. config_set_default_uint (basicConfig, "AdvOut", "TrackCount", 1);
  378. config_set_default_string(basicConfig, "AdvOut", "Encoder", "obs_x264");
  379. config_set_default_string(basicConfig, "AdvOut", "RecType", "Standard");
  380. config_set_default_string(basicConfig, "AdvOut", "RecFilePath",
  381. GetDefaultVideoSavePath().c_str());
  382. config_set_default_bool (basicConfig, "AdvOut", "RecUseRescale",
  383. false);
  384. config_set_default_bool (basicConfig, "AdvOut", "RecMultitrack",
  385. false);
  386. config_set_default_uint (basicConfig, "AdvOut", "RecTrackIndex", 1);
  387. config_set_default_uint (basicConfig, "AdvOut", "RecTrackCount", 1);
  388. config_set_default_string(basicConfig, "AdvOut", "RecEncoder",
  389. "none");
  390. config_set_default_uint (basicConfig, "AdvOut", "FFVBitrate", 2500);
  391. config_set_default_bool (basicConfig, "AdvOut", "FFUseRescale",
  392. false);
  393. config_set_default_uint (basicConfig, "AdvOut", "FFABitrate", 160);
  394. config_set_default_uint (basicConfig, "AdvOut", "FFAudioTrack", 1);
  395. config_set_default_uint (basicConfig, "AdvOut", "Track1Bitrate", 160);
  396. config_set_default_uint (basicConfig, "AdvOut", "Track2Bitrate", 160);
  397. config_set_default_uint (basicConfig, "AdvOut", "Track3Bitrate", 160);
  398. config_set_default_uint (basicConfig, "AdvOut", "Track4Bitrate", 160);
  399. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  400. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  401. cx = cx * 10 / 15;
  402. cy = cy * 10 / 15;
  403. config_set_default_uint (basicConfig, "Video", "OutputCX", cx);
  404. config_set_default_uint (basicConfig, "Video", "OutputCY", cy);
  405. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  406. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  407. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  408. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  409. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  410. config_set_default_string(basicConfig, "Video", "ScaleType", "bicubic");
  411. config_set_default_string(basicConfig, "Video", "ColorFormat", "NV12");
  412. config_set_default_string(basicConfig, "Video", "ColorSpace", "709");
  413. config_set_default_string(basicConfig, "Video", "ColorRange",
  414. "Partial");
  415. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  416. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  417. "Stereo");
  418. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  419. config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
  420. hasDesktopAudio ? "default" : "disabled");
  421. config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
  422. "disabled");
  423. config_set_default_string(basicConfig, "Audio", "AuxDevice1",
  424. hasInputAudio ? "default" : "disabled");
  425. config_set_default_string(basicConfig, "Audio", "AuxDevice2",
  426. "disabled");
  427. config_set_default_string(basicConfig, "Audio", "AuxDevice3",
  428. "disabled");
  429. return true;
  430. }
  431. bool OBSBasic::InitBasicConfig()
  432. {
  433. char configPath[512];
  434. int ret = os_get_config_path(configPath, sizeof(configPath),
  435. "obs-studio/basic/basic.ini");
  436. if (ret <= 0) {
  437. OBSErrorBox(nullptr, "Failed to get base.ini path");
  438. return false;
  439. }
  440. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  441. if (code != CONFIG_SUCCESS) {
  442. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  443. return false;
  444. }
  445. return InitBasicConfigDefaults();
  446. }
  447. void OBSBasic::InitOBSCallbacks()
  448. {
  449. signal_handler_connect(obs_get_signal_handler(), "source_add",
  450. OBSBasic::SourceAdded, this);
  451. signal_handler_connect(obs_get_signal_handler(), "source_remove",
  452. OBSBasic::SourceRemoved, this);
  453. signal_handler_connect(obs_get_signal_handler(), "channel_change",
  454. OBSBasic::ChannelChanged, this);
  455. signal_handler_connect(obs_get_signal_handler(), "source_activate",
  456. OBSBasic::SourceActivated, this);
  457. signal_handler_connect(obs_get_signal_handler(), "source_deactivate",
  458. OBSBasic::SourceDeactivated, this);
  459. signal_handler_connect(obs_get_signal_handler(), "source_rename",
  460. OBSBasic::SourceRenamed, this);
  461. }
  462. void OBSBasic::InitPrimitives()
  463. {
  464. obs_enter_graphics();
  465. gs_render_start(true);
  466. gs_vertex2f(0.0f, 0.0f);
  467. gs_vertex2f(0.0f, 1.0f);
  468. gs_vertex2f(1.0f, 1.0f);
  469. gs_vertex2f(1.0f, 0.0f);
  470. gs_vertex2f(0.0f, 0.0f);
  471. box = gs_render_save();
  472. gs_render_start(true);
  473. for (int i = 0; i <= 360; i += (360/20)) {
  474. float pos = RAD(float(i));
  475. gs_vertex2f(cosf(pos), sinf(pos));
  476. }
  477. circle = gs_render_save();
  478. obs_leave_graphics();
  479. }
  480. void OBSBasic::ResetOutputs()
  481. {
  482. const char *mode = config_get_string(basicConfig, "Output", "Mode");
  483. bool advOut = astrcmpi(mode, "Advanced") == 0;
  484. if (!outputHandler || !outputHandler->Active()) {
  485. outputHandler.reset();
  486. outputHandler.reset(advOut ?
  487. CreateAdvancedOutputHandler(this) :
  488. CreateSimpleOutputHandler(this));
  489. } else {
  490. outputHandler->Update();
  491. }
  492. }
  493. void OBSBasic::OBSInit()
  494. {
  495. char savePath[512];
  496. int ret = os_get_config_path(savePath, sizeof(savePath),
  497. "obs-studio/basic/scenes.json");
  498. if (ret <= 0)
  499. throw "Failed to get scenes.json file path";
  500. /* make sure it's fully displayed before doing any initialization */
  501. show();
  502. App()->processEvents();
  503. if (!obs_startup(App()->GetLocale()))
  504. throw "Failed to initialize libobs";
  505. if (!InitBasicConfig())
  506. throw "Failed to load basic.ini";
  507. if (!ResetAudio())
  508. throw "Failed to initialize audio";
  509. ret = ResetVideo();
  510. switch (ret) {
  511. case OBS_VIDEO_MODULE_NOT_FOUND:
  512. throw "Failed to initialize video: Graphics module not found";
  513. case OBS_VIDEO_NOT_SUPPORTED:
  514. throw "Failed to initialize video: Required graphics API "
  515. "functionality not found on these drivers or "
  516. "unavailable on this equipment";
  517. case OBS_VIDEO_INVALID_PARAM:
  518. throw "Failed to initialize video: Invalid parameters";
  519. default:
  520. if (ret != OBS_VIDEO_SUCCESS)
  521. throw "Failed to initialize video: Unspecified error";
  522. }
  523. InitOBSCallbacks();
  524. AddExtraModulePaths();
  525. obs_load_all_modules();
  526. ResetOutputs();
  527. if (!InitService())
  528. throw "Failed to initialize service";
  529. InitPrimitives();
  530. Load(savePath);
  531. ResetAudioDevices();
  532. TimedCheckForUpdates();
  533. loaded = true;
  534. saveTimer = new QTimer(this);
  535. connect(saveTimer, SIGNAL(timeout()), this, SLOT(SaveProject()));
  536. saveTimer->start(20000);
  537. bool previewEnabled = config_get_bool(App()->GlobalConfig(),
  538. "BasicWindow", "PreviewEnabled");
  539. if (!previewEnabled)
  540. QMetaObject::invokeMethod(this, "TogglePreview",
  541. Qt::QueuedConnection);
  542. }
  543. OBSBasic::~OBSBasic()
  544. {
  545. bool previewEnabled = obs_preview_enabled();
  546. /* XXX: any obs data must be released before calling obs_shutdown.
  547. * currently, we can't automate this with C++ RAII because of the
  548. * delicate nature of obs_shutdown needing to be freed before the UI
  549. * can be freed, and we have no control over the destruction order of
  550. * the Qt UI stuff, so we have to manually clear any references to
  551. * libobs. */
  552. delete cpuUsageTimer;
  553. os_cpu_usage_info_destroy(cpuUsageInfo);
  554. service = nullptr;
  555. outputHandler.reset();
  556. if (interaction)
  557. delete interaction;
  558. if (properties)
  559. delete properties;
  560. if (filters)
  561. delete filters;
  562. if (transformWindow)
  563. delete transformWindow;
  564. if (advAudioWindow)
  565. delete advAudioWindow;
  566. obs_enter_graphics();
  567. gs_vertexbuffer_destroy(box);
  568. gs_vertexbuffer_destroy(circle);
  569. obs_leave_graphics();
  570. /* When shutting down, sometimes source references can get in to the
  571. * event queue, and if we don't forcibly process those events they
  572. * won't get processed until after obs_shutdown has been called. I
  573. * really wish there were a more elegant way to deal with this via C++,
  574. * but Qt doesn't use C++ in a normal way, so you can't really rely on
  575. * normal C++ behavior for your data to be freed in the order that you
  576. * expect or want it to. */
  577. QApplication::sendPostedEvents(this);
  578. obs_shutdown();
  579. config_set_int(App()->GlobalConfig(), "General", "LastVersion",
  580. LIBOBS_API_VER);
  581. QRect lastGeom = normalGeometry();
  582. config_set_int(App()->GlobalConfig(), "BasicWindow", "cx",
  583. lastGeom.width());
  584. config_set_int(App()->GlobalConfig(), "BasicWindow", "cy",
  585. lastGeom.height());
  586. config_set_int(App()->GlobalConfig(), "BasicWindow", "posx",
  587. lastGeom.x());
  588. config_set_int(App()->GlobalConfig(), "BasicWindow", "posy",
  589. lastGeom.y());
  590. config_set_bool(App()->GlobalConfig(), "BasicWindow", "PreviewEnabled",
  591. previewEnabled);
  592. config_save(App()->GlobalConfig());
  593. }
  594. void OBSBasic::SaveProject()
  595. {
  596. char savePath[512];
  597. int ret = os_get_config_path(savePath, sizeof(savePath),
  598. "obs-studio/basic/scenes.json");
  599. if (ret <= 0)
  600. return;
  601. Save(savePath);
  602. }
  603. OBSScene OBSBasic::GetCurrentScene()
  604. {
  605. QListWidgetItem *item = ui->scenes->currentItem();
  606. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  607. }
  608. OBSSceneItem OBSBasic::GetSceneItem(QListWidgetItem *item)
  609. {
  610. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  611. }
  612. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  613. {
  614. return GetSceneItem(ui->sources->currentItem());
  615. }
  616. void OBSBasic::UpdateSources(OBSScene scene)
  617. {
  618. ui->sources->clear();
  619. obs_scene_enum_items(scene,
  620. [] (obs_scene_t *scene, obs_sceneitem_t *item, void *p)
  621. {
  622. OBSBasic *window = static_cast<OBSBasic*>(p);
  623. window->InsertSceneItem(item);
  624. UNUSED_PARAMETER(scene);
  625. return true;
  626. }, this);
  627. }
  628. void OBSBasic::InsertSceneItem(obs_sceneitem_t *item)
  629. {
  630. obs_source_t *source = obs_sceneitem_get_source(item);
  631. QListWidgetItem *listItem = new QListWidgetItem();
  632. listItem->setData(Qt::UserRole,
  633. QVariant::fromValue(OBSSceneItem(item)));
  634. ui->sources->insertItem(0, listItem);
  635. ui->sources->setCurrentRow(0);
  636. SetupVisibilityItem(ui->sources, listItem, item);
  637. /* if the source was just created, open properties dialog */
  638. if (sourceSceneRefs[source] == 0 && loaded)
  639. CreatePropertiesWindow(source);
  640. }
  641. void OBSBasic::CreateInteractionWindow(obs_source_t *source)
  642. {
  643. if (interaction)
  644. interaction->close();
  645. interaction = new OBSBasicInteraction(this, source);
  646. interaction->Init();
  647. interaction->setAttribute(Qt::WA_DeleteOnClose, true);
  648. }
  649. void OBSBasic::CreatePropertiesWindow(obs_source_t *source)
  650. {
  651. if (properties)
  652. properties->close();
  653. properties = new OBSBasicProperties(this, source);
  654. properties->Init();
  655. properties->setAttribute(Qt::WA_DeleteOnClose, true);
  656. }
  657. void OBSBasic::CreateFiltersWindow(obs_source_t *source)
  658. {
  659. if (filters)
  660. filters->close();
  661. filters = new OBSBasicFilters(this, source);
  662. filters->Init();
  663. filters->setAttribute(Qt::WA_DeleteOnClose, true);
  664. }
  665. /* Qt callbacks for invokeMethod */
  666. void OBSBasic::AddScene(OBSSource source)
  667. {
  668. const char *name = obs_source_get_name(source);
  669. obs_scene_t *scene = obs_scene_from_source(source);
  670. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  671. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  672. ui->scenes->addItem(item);
  673. signal_handler_t *handler = obs_source_get_signal_handler(source);
  674. signal_handler_connect(handler, "item_add",
  675. OBSBasic::SceneItemAdded, this);
  676. signal_handler_connect(handler, "item_remove",
  677. OBSBasic::SceneItemRemoved, this);
  678. signal_handler_connect(handler, "item_select",
  679. OBSBasic::SceneItemSelected, this);
  680. signal_handler_connect(handler, "item_deselect",
  681. OBSBasic::SceneItemDeselected, this);
  682. signal_handler_connect(handler, "reorder",
  683. OBSBasic::SceneReordered, this);
  684. }
  685. void OBSBasic::RemoveScene(OBSSource source)
  686. {
  687. const char *name = obs_source_get_name(source);
  688. QListWidgetItem *sel = ui->scenes->currentItem();
  689. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  690. Qt::MatchExactly);
  691. if (sel != nullptr) {
  692. if (items.contains(sel))
  693. ui->sources->clear();
  694. delete sel;
  695. }
  696. }
  697. void OBSBasic::AddSceneItem(OBSSceneItem item)
  698. {
  699. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  700. obs_source_t *source = obs_sceneitem_get_source(item);
  701. if (GetCurrentScene() == scene)
  702. InsertSceneItem(item);
  703. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  704. }
  705. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  706. {
  707. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  708. if (GetCurrentScene() == scene) {
  709. for (int i = 0; i < ui->sources->count(); i++) {
  710. QListWidgetItem *listItem = ui->sources->item(i);
  711. QVariant userData = listItem->data(Qt::UserRole);
  712. if (userData.value<OBSSceneItem>() == item) {
  713. delete listItem;
  714. break;
  715. }
  716. }
  717. }
  718. obs_source_t *source = obs_sceneitem_get_source(item);
  719. int scenes = sourceSceneRefs[source] - 1;
  720. sourceSceneRefs[source] = scenes;
  721. if (scenes == 0) {
  722. obs_source_remove(source);
  723. sourceSceneRefs.erase(source);
  724. }
  725. }
  726. void OBSBasic::UpdateSceneSelection(OBSSource source)
  727. {
  728. if (source) {
  729. obs_scene_t *scene = obs_scene_from_source(source);
  730. const char *name = obs_source_get_name(source);
  731. if (!scene)
  732. return;
  733. QList<QListWidgetItem*> items =
  734. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  735. if (items.count()) {
  736. sceneChanging = true;
  737. ui->scenes->setCurrentItem(items.first());
  738. sceneChanging = false;
  739. UpdateSources(scene);
  740. }
  741. }
  742. }
  743. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  744. const QString &prevName)
  745. {
  746. QList<QListWidgetItem*> items =
  747. listWidget->findItems(prevName, Qt::MatchExactly);
  748. for (int i = 0; i < items.count(); i++)
  749. items[i]->setText(newName);
  750. }
  751. void OBSBasic::RenameSources(QString newName, QString prevName)
  752. {
  753. RenameListValues(ui->scenes, newName, prevName);
  754. for (size_t i = 0; i < volumes.size(); i++) {
  755. if (volumes[i]->GetName().compare(prevName) == 0)
  756. volumes[i]->SetName(newName);
  757. }
  758. }
  759. void OBSBasic::SelectSceneItem(OBSScene scene, OBSSceneItem item, bool select)
  760. {
  761. if (scene != GetCurrentScene())
  762. return;
  763. for (int i = 0; i < ui->sources->count(); i++) {
  764. QListWidgetItem *witem = ui->sources->item(i);
  765. QVariant data = witem->data(Qt::UserRole);
  766. if (!data.canConvert<OBSSceneItem>())
  767. continue;
  768. if (item != data.value<OBSSceneItem>())
  769. continue;
  770. if (select) {
  771. ui->sources->setCurrentItem(witem);
  772. } else if (ui->sources->currentItem() == witem) {
  773. ui->sources->setCurrentItem(nullptr);
  774. }
  775. break;
  776. }
  777. }
  778. void OBSBasic::MoveSceneItem(OBSSceneItem item, obs_order_movement movement)
  779. {
  780. OBSScene scene = obs_sceneitem_get_scene(item);
  781. if (scene != GetCurrentScene())
  782. return;
  783. int curRow = ui->sources->currentRow();
  784. if (curRow == -1)
  785. return;
  786. QListWidgetItem *listItem = ui->sources->takeItem(curRow);
  787. switch (movement) {
  788. case OBS_ORDER_MOVE_UP:
  789. if (curRow > 0)
  790. curRow--;
  791. break;
  792. case OBS_ORDER_MOVE_DOWN:
  793. if (curRow < ui->sources->count())
  794. curRow++;
  795. break;
  796. case OBS_ORDER_MOVE_TOP:
  797. curRow = 0;
  798. break;
  799. case OBS_ORDER_MOVE_BOTTOM:
  800. curRow = ui->sources->count();
  801. break;
  802. }
  803. ui->sources->insertItem(curRow, listItem);
  804. ui->sources->setCurrentRow(curRow);
  805. }
  806. void OBSBasic::ActivateAudioSource(OBSSource source)
  807. {
  808. VolControl *vol = new VolControl(source);
  809. volumes.push_back(vol);
  810. ui->volumeWidgets->layout()->addWidget(vol);
  811. }
  812. void OBSBasic::DeactivateAudioSource(OBSSource source)
  813. {
  814. for (size_t i = 0; i < volumes.size(); i++) {
  815. if (volumes[i]->GetSource() == source) {
  816. delete volumes[i];
  817. volumes.erase(volumes.begin() + i);
  818. break;
  819. }
  820. }
  821. }
  822. bool OBSBasic::QueryRemoveSource(obs_source_t *source)
  823. {
  824. const char *name = obs_source_get_name(source);
  825. QString text = QTStr("ConfirmRemove.Text");
  826. text.replace("$1", QT_UTF8(name));
  827. QMessageBox remove_source(this);
  828. remove_source.setText(text);
  829. QAbstractButton *Yes = remove_source.addButton(QTStr("Yes"),
  830. QMessageBox::YesRole);
  831. remove_source.addButton(QTStr("No"), QMessageBox::NoRole);
  832. remove_source.setIcon(QMessageBox::Question);
  833. remove_source.setWindowTitle(QTStr("ConfirmRemove.Title"));
  834. remove_source.exec();
  835. return Yes == remove_source.clickedButton();
  836. }
  837. #define UPDATE_CHECK_INTERVAL (60*60*24*4) /* 4 days */
  838. #ifdef UPDATE_SPARKLE
  839. void init_sparkle_updater(bool update_to_undeployed);
  840. void trigger_sparkle_update();
  841. #endif
  842. void OBSBasic::TimedCheckForUpdates()
  843. {
  844. #ifdef UPDATE_SPARKLE
  845. init_sparkle_updater(config_get_bool(App()->GlobalConfig(), "General",
  846. "UpdateToUndeployed"));
  847. #else
  848. long long lastUpdate = config_get_int(App()->GlobalConfig(), "General",
  849. "LastUpdateCheck");
  850. uint32_t lastVersion = config_get_int(App()->GlobalConfig(), "General",
  851. "LastVersion");
  852. if (lastVersion < LIBOBS_API_VER) {
  853. lastUpdate = 0;
  854. config_set_int(App()->GlobalConfig(), "General",
  855. "LastUpdateCheck", 0);
  856. }
  857. long long t = (long long)time(nullptr);
  858. long long secs = t - lastUpdate;
  859. if (secs > UPDATE_CHECK_INTERVAL)
  860. CheckForUpdates();
  861. #endif
  862. }
  863. void OBSBasic::CheckForUpdates()
  864. {
  865. #ifdef UPDATE_SPARKLE
  866. trigger_sparkle_update();
  867. #else
  868. ui->actionCheckForUpdates->setEnabled(false);
  869. string versionString("obs-basic ");
  870. versionString += App()->GetVersionString();
  871. QNetworkRequest request;
  872. request.setUrl(QUrl("https://obsproject.com/obs2_update/basic.json"));
  873. request.setRawHeader("User-Agent", versionString.c_str());
  874. QNetworkReply *reply = networkManager.get(request);
  875. connect(reply, SIGNAL(finished()),
  876. this, SLOT(updateFileFinished()));
  877. #endif
  878. }
  879. #ifdef __APPLE__
  880. #define VERSION_ENTRY "mac"
  881. #elif _WIN32
  882. #define VERSION_ENTRY "windows"
  883. #else
  884. #define VERSION_ENTRY "other"
  885. #endif
  886. void OBSBasic::updateFileFinished()
  887. {
  888. ui->actionCheckForUpdates->setEnabled(true);
  889. QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
  890. if (!reply || reply->error()) {
  891. blog(LOG_WARNING, "Update check failed: %s",
  892. QT_TO_UTF8(reply->errorString()));
  893. return;
  894. }
  895. QByteArray raw = reply->readAll();
  896. if (!raw.length())
  897. return;
  898. obs_data_t *returnData = obs_data_create_from_json(raw.constData());
  899. obs_data_t *versionData = obs_data_get_obj(returnData, VERSION_ENTRY);
  900. const char *description = obs_data_get_string(returnData,
  901. "description");
  902. const char *download = obs_data_get_string(versionData, "download");
  903. if (returnData && versionData && description && download) {
  904. long major = obs_data_get_int(versionData, "major");
  905. long minor = obs_data_get_int(versionData, "minor");
  906. long patch = obs_data_get_int(versionData, "patch");
  907. long version = MAKE_SEMANTIC_VERSION(major, minor, patch);
  908. blog(LOG_INFO, "Update check: latest version is: %ld.%ld.%ld",
  909. major, minor, patch);
  910. if (version > LIBOBS_API_VER) {
  911. QString str = QTStr("UpdateAvailable.Text");
  912. QMessageBox messageBox(this);
  913. str = str.arg(QString::number(major),
  914. QString::number(minor),
  915. QString::number(patch),
  916. download);
  917. messageBox.setWindowTitle(QTStr("UpdateAvailable"));
  918. messageBox.setTextFormat(Qt::RichText);
  919. messageBox.setText(str);
  920. messageBox.setInformativeText(QT_UTF8(description));
  921. messageBox.exec();
  922. long long t = (long long)time(nullptr);
  923. config_set_int(App()->GlobalConfig(), "General",
  924. "LastUpdateCheck", t);
  925. config_save(App()->GlobalConfig());
  926. }
  927. } else {
  928. blog(LOG_WARNING, "Bad JSON file received from server");
  929. }
  930. obs_data_release(versionData);
  931. obs_data_release(returnData);
  932. reply->deleteLater();
  933. }
  934. void OBSBasic::RemoveSelectedScene()
  935. {
  936. OBSScene scene = GetCurrentScene();
  937. if (scene) {
  938. obs_source_t *source = obs_scene_get_source(scene);
  939. if (QueryRemoveSource(source))
  940. obs_source_remove(source);
  941. }
  942. }
  943. void OBSBasic::RemoveSelectedSceneItem()
  944. {
  945. OBSSceneItem item = GetCurrentSceneItem();
  946. if (item) {
  947. obs_source_t *source = obs_sceneitem_get_source(item);
  948. if (QueryRemoveSource(source))
  949. obs_sceneitem_remove(item);
  950. }
  951. }
  952. struct ReorderInfo {
  953. int idx = 0;
  954. OBSBasic *window;
  955. inline ReorderInfo(OBSBasic *window_) : window(window_) {}
  956. };
  957. void OBSBasic::ReorderSceneItem(obs_sceneitem_t *item, size_t idx)
  958. {
  959. int count = ui->sources->count();
  960. int idx_inv = count - (int)idx - 1;
  961. for (int i = 0; i < count; i++) {
  962. QListWidgetItem *listItem = ui->sources->item(i);
  963. QVariant v = listItem->data(Qt::UserRole);
  964. OBSSceneItem sceneItem = v.value<OBSSceneItem>();
  965. if (sceneItem == item) {
  966. if ((int)idx_inv != i) {
  967. bool sel = (ui->sources->currentRow() == i);
  968. listItem = ui->sources->takeItem(i);
  969. if (listItem) {
  970. ui->sources->insertItem(idx_inv,
  971. listItem);
  972. SetupVisibilityItem(ui->sources,
  973. listItem, item);
  974. if (sel)
  975. ui->sources->setCurrentRow(
  976. idx_inv);
  977. }
  978. }
  979. break;
  980. }
  981. }
  982. }
  983. void OBSBasic::ReorderSources(OBSScene scene)
  984. {
  985. ReorderInfo info(this);
  986. if (scene != GetCurrentScene())
  987. return;
  988. obs_scene_enum_items(scene,
  989. [] (obs_scene_t*, obs_sceneitem_t *item, void *p)
  990. {
  991. ReorderInfo *info =
  992. reinterpret_cast<ReorderInfo*>(p);
  993. info->window->ReorderSceneItem(item,
  994. info->idx++);
  995. return true;
  996. }, &info);
  997. }
  998. /* OBS Callbacks */
  999. void OBSBasic::SceneReordered(void *data, calldata_t *params)
  1000. {
  1001. OBSBasic *window = static_cast<OBSBasic*>(data);
  1002. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1003. QMetaObject::invokeMethod(window, "ReorderSources",
  1004. Q_ARG(OBSScene, OBSScene(scene)));
  1005. }
  1006. void OBSBasic::SceneItemAdded(void *data, calldata_t *params)
  1007. {
  1008. OBSBasic *window = static_cast<OBSBasic*>(data);
  1009. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1010. QMetaObject::invokeMethod(window, "AddSceneItem",
  1011. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1012. }
  1013. void OBSBasic::SceneItemRemoved(void *data, calldata_t *params)
  1014. {
  1015. OBSBasic *window = static_cast<OBSBasic*>(data);
  1016. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1017. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  1018. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  1019. }
  1020. void OBSBasic::SceneItemSelected(void *data, calldata_t *params)
  1021. {
  1022. OBSBasic *window = static_cast<OBSBasic*>(data);
  1023. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1024. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1025. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1026. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1027. Q_ARG(bool, true));
  1028. }
  1029. void OBSBasic::SceneItemDeselected(void *data, calldata_t *params)
  1030. {
  1031. OBSBasic *window = static_cast<OBSBasic*>(data);
  1032. obs_scene_t *scene = (obs_scene_t*)calldata_ptr(params, "scene");
  1033. obs_sceneitem_t *item = (obs_sceneitem_t*)calldata_ptr(params, "item");
  1034. QMetaObject::invokeMethod(window, "SelectSceneItem",
  1035. Q_ARG(OBSScene, scene), Q_ARG(OBSSceneItem, item),
  1036. Q_ARG(bool, false));
  1037. }
  1038. void OBSBasic::SourceAdded(void *data, calldata_t *params)
  1039. {
  1040. OBSBasic *window = static_cast<OBSBasic*>(data);
  1041. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1042. if (obs_scene_from_source(source) != NULL)
  1043. QMetaObject::invokeMethod(window,
  1044. "AddScene",
  1045. Q_ARG(OBSSource, OBSSource(source)));
  1046. }
  1047. void OBSBasic::SourceRemoved(void *data, calldata_t *params)
  1048. {
  1049. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1050. if (obs_scene_from_source(source) != NULL)
  1051. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1052. "RemoveScene",
  1053. Q_ARG(OBSSource, OBSSource(source)));
  1054. }
  1055. void OBSBasic::SourceActivated(void *data, calldata_t *params)
  1056. {
  1057. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1058. uint32_t flags = obs_source_get_output_flags(source);
  1059. if (flags & OBS_SOURCE_AUDIO)
  1060. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1061. "ActivateAudioSource",
  1062. Q_ARG(OBSSource, OBSSource(source)));
  1063. }
  1064. void OBSBasic::SourceDeactivated(void *data, calldata_t *params)
  1065. {
  1066. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1067. uint32_t flags = obs_source_get_output_flags(source);
  1068. if (flags & OBS_SOURCE_AUDIO)
  1069. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1070. "DeactivateAudioSource",
  1071. Q_ARG(OBSSource, OBSSource(source)));
  1072. }
  1073. void OBSBasic::SourceRenamed(void *data, calldata_t *params)
  1074. {
  1075. const char *newName = calldata_string(params, "new_name");
  1076. const char *prevName = calldata_string(params, "prev_name");
  1077. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1078. "RenameSources",
  1079. Q_ARG(QString, QT_UTF8(newName)),
  1080. Q_ARG(QString, QT_UTF8(prevName)));
  1081. }
  1082. void OBSBasic::ChannelChanged(void *data, calldata_t *params)
  1083. {
  1084. obs_source_t *source = (obs_source_t*)calldata_ptr(params, "source");
  1085. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  1086. if (channel == 0)
  1087. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  1088. "UpdateSceneSelection",
  1089. Q_ARG(OBSSource, OBSSource(source)));
  1090. }
  1091. void OBSBasic::DrawBackdrop(float cx, float cy)
  1092. {
  1093. if (!box)
  1094. return;
  1095. gs_effect_t *solid = obs_get_solid_effect();
  1096. gs_eparam_t *color = gs_effect_get_param_by_name(solid, "color");
  1097. gs_technique_t *tech = gs_effect_get_technique(solid, "Solid");
  1098. vec4 colorVal;
  1099. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  1100. gs_effect_set_vec4(color, &colorVal);
  1101. gs_technique_begin(tech);
  1102. gs_technique_begin_pass(tech, 0);
  1103. gs_matrix_push();
  1104. gs_matrix_identity();
  1105. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  1106. gs_load_vertexbuffer(box);
  1107. gs_draw(GS_TRISTRIP, 0, 0);
  1108. gs_matrix_pop();
  1109. gs_technique_end_pass(tech);
  1110. gs_technique_end(tech);
  1111. gs_load_vertexbuffer(nullptr);
  1112. }
  1113. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  1114. {
  1115. OBSBasic *window = static_cast<OBSBasic*>(data);
  1116. obs_video_info ovi;
  1117. obs_get_video_info(&ovi);
  1118. window->previewCX = int(window->previewScale * float(ovi.base_width));
  1119. window->previewCY = int(window->previewScale * float(ovi.base_height));
  1120. gs_viewport_push();
  1121. gs_projection_push();
  1122. /* --------------------------------------- */
  1123. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  1124. -100.0f, 100.0f);
  1125. gs_set_viewport(window->previewX, window->previewY,
  1126. window->previewCX, window->previewCY);
  1127. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  1128. obs_render_main_view();
  1129. gs_load_vertexbuffer(nullptr);
  1130. /* --------------------------------------- */
  1131. QSize previewSize = GetPixelSize(window->ui->preview);
  1132. float right = float(previewSize.width()) - window->previewX;
  1133. float bottom = float(previewSize.height()) - window->previewY;
  1134. gs_ortho(-window->previewX, right,
  1135. -window->previewY, bottom,
  1136. -100.0f, 100.0f);
  1137. gs_reset_viewport();
  1138. window->ui->preview->DrawSceneEditing();
  1139. /* --------------------------------------- */
  1140. gs_projection_pop();
  1141. gs_viewport_pop();
  1142. UNUSED_PARAMETER(cx);
  1143. UNUSED_PARAMETER(cy);
  1144. }
  1145. /* Main class functions */
  1146. obs_service_t *OBSBasic::GetService()
  1147. {
  1148. if (!service) {
  1149. service = obs_service_create("rtmp_common", NULL, NULL);
  1150. obs_service_release(service);
  1151. }
  1152. return service;
  1153. }
  1154. void OBSBasic::SetService(obs_service_t *newService)
  1155. {
  1156. if (newService)
  1157. service = newService;
  1158. }
  1159. bool OBSBasic::StreamingActive()
  1160. {
  1161. if (!outputHandler)
  1162. return false;
  1163. return outputHandler->StreamingActive();
  1164. }
  1165. #ifdef _WIN32
  1166. #define IS_WIN32 1
  1167. #else
  1168. #define IS_WIN32 0
  1169. #endif
  1170. static inline int AttemptToResetVideo(struct obs_video_info *ovi)
  1171. {
  1172. int ret = obs_reset_video(ovi);
  1173. if (ret == OBS_VIDEO_INVALID_PARAM) {
  1174. struct obs_video_info new_params = *ovi;
  1175. if (new_params.window_width == 0)
  1176. new_params.window_width = 512;
  1177. if (new_params.window_height == 0)
  1178. new_params.window_height = 512;
  1179. new_params.output_width = new_params.window_width;
  1180. new_params.output_height = new_params.window_height;
  1181. new_params.base_width = new_params.window_width;
  1182. new_params.base_height = new_params.window_height;
  1183. ret = obs_reset_video(&new_params);
  1184. }
  1185. return ret;
  1186. }
  1187. static inline enum obs_scale_type GetScaleType(ConfigFile &basicConfig)
  1188. {
  1189. const char *scaleTypeStr = config_get_string(basicConfig,
  1190. "Video", "ScaleType");
  1191. if (astrcmpi(scaleTypeStr, "bilinear") == 0)
  1192. return OBS_SCALE_BILINEAR;
  1193. else if (astrcmpi(scaleTypeStr, "lanczos") == 0)
  1194. return OBS_SCALE_LANCZOS;
  1195. else
  1196. return OBS_SCALE_BICUBIC;
  1197. }
  1198. static inline enum video_format GetVideoFormatFromName(const char *name)
  1199. {
  1200. if (astrcmpi(name, "I420") == 0)
  1201. return VIDEO_FORMAT_I420;
  1202. else if (astrcmpi(name, "NV12") == 0)
  1203. return VIDEO_FORMAT_NV12;
  1204. else if (astrcmpi(name, "I444") == 0)
  1205. return VIDEO_FORMAT_I444;
  1206. #if 0 //currently unsupported
  1207. else if (astrcmpi(name, "YVYU") == 0)
  1208. return VIDEO_FORMAT_YVYU;
  1209. else if (astrcmpi(name, "YUY2") == 0)
  1210. return VIDEO_FORMAT_YUY2;
  1211. else if (astrcmpi(name, "UYVY") == 0)
  1212. return VIDEO_FORMAT_UYVY;
  1213. #endif
  1214. else
  1215. return VIDEO_FORMAT_RGBA;
  1216. }
  1217. int OBSBasic::ResetVideo()
  1218. {
  1219. struct obs_video_info ovi;
  1220. int ret;
  1221. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  1222. const char *colorFormat = config_get_string(basicConfig, "Video",
  1223. "ColorFormat");
  1224. const char *colorSpace = config_get_string(basicConfig, "Video",
  1225. "ColorSpace");
  1226. const char *colorRange = config_get_string(basicConfig, "Video",
  1227. "ColorRange");
  1228. ovi.graphics_module = App()->GetRenderModule();
  1229. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  1230. "Video", "BaseCX");
  1231. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  1232. "Video", "BaseCY");
  1233. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  1234. "Video", "OutputCX");
  1235. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  1236. "Video", "OutputCY");
  1237. ovi.output_format = GetVideoFormatFromName(colorFormat);
  1238. ovi.colorspace = astrcmpi(colorSpace, "601") == 0 ?
  1239. VIDEO_CS_601 : VIDEO_CS_709;
  1240. ovi.range = astrcmpi(colorRange, "Full") == 0 ?
  1241. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  1242. ovi.adapter = 0;
  1243. ovi.gpu_conversion = true;
  1244. ovi.scale_type = GetScaleType(basicConfig);
  1245. QTToGSWindow(ui->preview->winId(), ovi.window);
  1246. //required to make opengl display stuff on osx(?)
  1247. ResizePreview(ovi.base_width, ovi.base_height);
  1248. QSize size = GetPixelSize(ui->preview);
  1249. ovi.window_width = size.width();
  1250. ovi.window_height = size.height();
  1251. ret = AttemptToResetVideo(&ovi);
  1252. if (IS_WIN32 && ret != OBS_VIDEO_SUCCESS) {
  1253. /* Try OpenGL if DirectX fails on windows */
  1254. if (astrcmpi(ovi.graphics_module, DL_OPENGL) != 0) {
  1255. blog(LOG_WARNING, "Failed to initialize obs video (%d) "
  1256. "with graphics_module='%s', retrying "
  1257. "with graphics_module='%s'",
  1258. ret, ovi.graphics_module,
  1259. DL_OPENGL);
  1260. ovi.graphics_module = DL_OPENGL;
  1261. ret = AttemptToResetVideo(&ovi);
  1262. }
  1263. }
  1264. if (ret == OBS_VIDEO_SUCCESS)
  1265. obs_add_draw_callback(OBSBasic::RenderMain, this);
  1266. return ret;
  1267. }
  1268. bool OBSBasic::ResetAudio()
  1269. {
  1270. struct obs_audio_info ai;
  1271. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  1272. "SampleRate");
  1273. const char *channelSetupStr = config_get_string(basicConfig,
  1274. "Audio", "ChannelSetup");
  1275. if (strcmp(channelSetupStr, "Mono") == 0)
  1276. ai.speakers = SPEAKERS_MONO;
  1277. else
  1278. ai.speakers = SPEAKERS_STEREO;
  1279. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  1280. return obs_reset_audio(&ai);
  1281. }
  1282. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  1283. const char *deviceDesc, int channel)
  1284. {
  1285. const char *deviceId = config_get_string(basicConfig, "Audio",
  1286. deviceName);
  1287. obs_source_t *source;
  1288. obs_data_t *settings;
  1289. bool same = false;
  1290. source = obs_get_output_source(channel);
  1291. if (source) {
  1292. settings = obs_source_get_settings(source);
  1293. const char *curId = obs_data_get_string(settings, "device_id");
  1294. same = (strcmp(curId, deviceId) == 0);
  1295. obs_data_release(settings);
  1296. obs_source_release(source);
  1297. }
  1298. if (!same)
  1299. obs_set_output_source(channel, nullptr);
  1300. if (!same && strcmp(deviceId, "disabled") != 0) {
  1301. obs_data_t *settings = obs_data_create();
  1302. obs_data_set_string(settings, "device_id", deviceId);
  1303. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  1304. sourceId, deviceDesc, settings);
  1305. obs_data_release(settings);
  1306. obs_set_output_source(channel, source);
  1307. obs_source_release(source);
  1308. }
  1309. }
  1310. void OBSBasic::ResetAudioDevices()
  1311. {
  1312. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1",
  1313. Str("Basic.DesktopDevice1"), 1);
  1314. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2",
  1315. Str("Basic.DesktopDevice2"), 2);
  1316. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1",
  1317. Str("Basic.AuxDevice1"), 3);
  1318. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2",
  1319. Str("Basic.AuxDevice2"), 4);
  1320. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3",
  1321. Str("Basic.AuxDevice3"), 5);
  1322. }
  1323. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  1324. {
  1325. QSize targetSize;
  1326. /* resize preview panel to fix to the top section of the window */
  1327. targetSize = GetPixelSize(ui->preview);
  1328. GetScaleAndCenterPos(int(cx), int(cy),
  1329. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  1330. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  1331. previewX, previewY, previewScale);
  1332. previewX += float(PREVIEW_EDGE_SIZE);
  1333. previewY += float(PREVIEW_EDGE_SIZE);
  1334. if (isVisible()) {
  1335. if (resizeTimer)
  1336. killTimer(resizeTimer);
  1337. resizeTimer = startTimer(100);
  1338. }
  1339. }
  1340. void OBSBasic::closeEvent(QCloseEvent *event)
  1341. {
  1342. if (outputHandler && outputHandler->Active()) {
  1343. QMessageBox::StandardButton button = QMessageBox::question(
  1344. this, QTStr("ConfirmExit.Title"),
  1345. QTStr("ConfirmExit.Text"));
  1346. if (button == QMessageBox::No) {
  1347. event->ignore();
  1348. return;
  1349. }
  1350. }
  1351. QWidget::closeEvent(event);
  1352. if (!event->isAccepted())
  1353. return;
  1354. /* Check all child dialogs and ensure they run their proper closeEvent
  1355. * methods before exiting the application. Otherwise Qt doesn't send
  1356. * the proper QCloseEvent messages. */
  1357. QList<QDialog*> childDialogs = this->findChildren<QDialog *>();
  1358. if (!childDialogs.isEmpty()) {
  1359. for (int i = 0; i < childDialogs.size(); ++i) {
  1360. childDialogs.at(i)->close();
  1361. }
  1362. }
  1363. for (QPointer<QWidget> &projector : projectors) {
  1364. delete projector;
  1365. projector.clear();
  1366. }
  1367. // remove draw callback in case our drawable surfaces go away before
  1368. // the destructor gets called
  1369. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  1370. /* Delete the save timer so it doesn't trigger after this point while
  1371. * the program data is being freed */
  1372. delete saveTimer;
  1373. SaveProject();
  1374. /* Clear the list boxes in ::closeEvent to ensure that we can process
  1375. * any ->deleteLater events in this window created by Qt in relation to
  1376. * their internal data */
  1377. ClearVolumeControls();
  1378. ui->sources->clear();
  1379. ui->scenes->clear();
  1380. }
  1381. void OBSBasic::changeEvent(QEvent *event)
  1382. {
  1383. /* TODO */
  1384. UNUSED_PARAMETER(event);
  1385. }
  1386. void OBSBasic::resizeEvent(QResizeEvent *event)
  1387. {
  1388. struct obs_video_info ovi;
  1389. if (obs_get_video_info(&ovi))
  1390. ResizePreview(ovi.base_width, ovi.base_height);
  1391. UNUSED_PARAMETER(event);
  1392. }
  1393. void OBSBasic::timerEvent(QTimerEvent *event)
  1394. {
  1395. if (event->timerId() == resizeTimer) {
  1396. killTimer(resizeTimer);
  1397. resizeTimer = 0;
  1398. QSize size = GetPixelSize(ui->preview);
  1399. obs_resize(size.width(), size.height());
  1400. }
  1401. }
  1402. void OBSBasic::on_action_New_triggered()
  1403. {
  1404. /* TODO */
  1405. }
  1406. void OBSBasic::on_action_Open_triggered()
  1407. {
  1408. /* TODO */
  1409. }
  1410. void OBSBasic::on_action_Save_triggered()
  1411. {
  1412. /* TODO */
  1413. }
  1414. void OBSBasic::on_actionShow_Recordings_triggered()
  1415. {
  1416. const char *path = config_get_string(basicConfig,
  1417. "SimpleOutput", "FilePath");
  1418. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  1419. }
  1420. void OBSBasic::on_actionRemux_triggered()
  1421. {
  1422. const char *path = config_get_string(basicConfig,
  1423. "SimpleOutput", "FilePath");
  1424. OBSRemux remux(path, this);
  1425. remux.exec();
  1426. }
  1427. void OBSBasic::on_action_Settings_triggered()
  1428. {
  1429. OBSBasicSettings settings(this);
  1430. settings.exec();
  1431. }
  1432. void OBSBasic::on_actionAdvAudioProperties_triggered()
  1433. {
  1434. if (advAudioWindow != nullptr) {
  1435. advAudioWindow->raise();
  1436. return;
  1437. }
  1438. advAudioWindow = new OBSBasicAdvAudio(this);
  1439. advAudioWindow->show();
  1440. advAudioWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  1441. connect(advAudioWindow, SIGNAL(destroyed()),
  1442. this, SLOT(on_advAudioProps_destroyed()));
  1443. }
  1444. void OBSBasic::on_advAudioProps_clicked()
  1445. {
  1446. on_actionAdvAudioProperties_triggered();
  1447. }
  1448. void OBSBasic::on_advAudioProps_destroyed()
  1449. {
  1450. advAudioWindow = nullptr;
  1451. }
  1452. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  1453. QListWidgetItem *prev)
  1454. {
  1455. obs_source_t *source = NULL;
  1456. if (sceneChanging)
  1457. return;
  1458. if (current) {
  1459. obs_scene_t *scene;
  1460. scene = current->data(Qt::UserRole).value<OBSScene>();
  1461. source = obs_scene_get_source(scene);
  1462. }
  1463. /* TODO: allow transitions */
  1464. obs_set_output_source(0, source);
  1465. UNUSED_PARAMETER(prev);
  1466. }
  1467. void OBSBasic::EditSceneName()
  1468. {
  1469. QListWidgetItem *item = ui->scenes->currentItem();
  1470. Qt::ItemFlags flags = item->flags();
  1471. item->setFlags(flags | Qt::ItemIsEditable);
  1472. ui->scenes->editItem(item);
  1473. item->setFlags(flags);
  1474. }
  1475. static void AddProjectorMenuMonitors(QMenu *parent, QObject *target,
  1476. const char *slot)
  1477. {
  1478. QAction *action;
  1479. std::vector<MonitorInfo> monitors;
  1480. GetMonitors(monitors);
  1481. for (int i = 0; (size_t)i < monitors.size(); i++) {
  1482. const MonitorInfo &monitor = monitors[i];
  1483. QString str = QString("%1 %2: %3x%4 @ %5,%6").
  1484. arg(QTStr("Display"),
  1485. QString::number(i),
  1486. QString::number((int)monitor.cx),
  1487. QString::number((int)monitor.cy),
  1488. QString::number((int)monitor.x),
  1489. QString::number((int)monitor.y));
  1490. action = parent->addAction(str, target, slot);
  1491. action->setProperty("monitor", i);
  1492. }
  1493. }
  1494. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  1495. {
  1496. QListWidgetItem *item = ui->scenes->itemAt(pos);
  1497. QPointer<QMenu> sceneProjectorMenu;
  1498. QMenu popup(this);
  1499. popup.addAction(QTStr("Add"),
  1500. this, SLOT(on_actionAddScene_triggered()));
  1501. if (item) {
  1502. popup.addSeparator();
  1503. popup.addAction(QTStr("Rename"),
  1504. this, SLOT(EditSceneName()));
  1505. popup.addAction(QTStr("Remove"),
  1506. this, SLOT(RemoveSelectedScene()),
  1507. DeleteKeys.front());
  1508. popup.addSeparator();
  1509. sceneProjectorMenu = new QMenu(QTStr("SceneProjector"));
  1510. AddProjectorMenuMonitors(sceneProjectorMenu, this,
  1511. SLOT(OpenSceneProjector()));
  1512. popup.addMenu(sceneProjectorMenu);
  1513. popup.addSeparator();
  1514. popup.addAction(QTStr("Filters"), this,
  1515. SLOT(OpenSceneFilters()));
  1516. }
  1517. popup.exec(QCursor::pos());
  1518. }
  1519. void OBSBasic::on_actionAddScene_triggered()
  1520. {
  1521. string name;
  1522. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  1523. int i = 1;
  1524. QString placeHolderText = format.arg(i);
  1525. obs_source_t *source = nullptr;
  1526. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1527. obs_source_release(source);
  1528. placeHolderText = format.arg(++i);
  1529. }
  1530. bool accepted = NameDialog::AskForName(this,
  1531. QTStr("Basic.Main.AddSceneDlg.Title"),
  1532. QTStr("Basic.Main.AddSceneDlg.Text"),
  1533. name,
  1534. placeHolderText);
  1535. if (accepted) {
  1536. if (name.empty()) {
  1537. QMessageBox::information(this,
  1538. QTStr("NoNameEntered.Title"),
  1539. QTStr("NoNameEntered.Text"));
  1540. on_actionAddScene_triggered();
  1541. return;
  1542. }
  1543. obs_source_t *source = obs_get_source_by_name(name.c_str());
  1544. if (source) {
  1545. QMessageBox::information(this,
  1546. QTStr("NameExists.Title"),
  1547. QTStr("NameExists.Text"));
  1548. obs_source_release(source);
  1549. on_actionAddScene_triggered();
  1550. return;
  1551. }
  1552. obs_scene_t *scene = obs_scene_create(name.c_str());
  1553. source = obs_scene_get_source(scene);
  1554. obs_add_source(source);
  1555. obs_scene_release(scene);
  1556. obs_set_output_source(0, source);
  1557. }
  1558. }
  1559. void OBSBasic::on_actionRemoveScene_triggered()
  1560. {
  1561. OBSScene scene = GetCurrentScene();
  1562. obs_source_t *source = obs_scene_get_source(scene);
  1563. if (source && QueryRemoveSource(source))
  1564. obs_source_remove(source);
  1565. }
  1566. void OBSBasic::on_actionSceneProperties_triggered()
  1567. {
  1568. /* TODO */
  1569. }
  1570. void OBSBasic::on_actionSceneUp_triggered()
  1571. {
  1572. /* TODO */
  1573. }
  1574. void OBSBasic::on_actionSceneDown_triggered()
  1575. {
  1576. /* TODO */
  1577. }
  1578. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  1579. QListWidgetItem *prev)
  1580. {
  1581. auto select_one = [] (obs_scene_t *scene, obs_sceneitem_t *item,
  1582. void *param)
  1583. {
  1584. obs_sceneitem_t *selectedItem =
  1585. *reinterpret_cast<OBSSceneItem*>(param);
  1586. obs_sceneitem_select(item, (selectedItem == item));
  1587. UNUSED_PARAMETER(scene);
  1588. return true;
  1589. };
  1590. if (!current)
  1591. return;
  1592. OBSSceneItem item = current->data(Qt::UserRole).value<OBSSceneItem>();
  1593. obs_source_t *source = obs_sceneitem_get_source(item);
  1594. if ((obs_source_get_output_flags(source) & OBS_SOURCE_VIDEO) == 0)
  1595. return;
  1596. obs_scene_enum_items(GetCurrentScene(), select_one, &item);
  1597. UNUSED_PARAMETER(prev);
  1598. }
  1599. void OBSBasic::EditSceneItemName()
  1600. {
  1601. QListWidgetItem *item = ui->sources->currentItem();
  1602. Qt::ItemFlags flags = item->flags();
  1603. OBSSceneItem sceneItem= item->data(Qt::UserRole).value<OBSSceneItem>();
  1604. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  1605. const char *name = obs_source_get_name(source);
  1606. item->setText(QT_UTF8(name));
  1607. item->setFlags(flags | Qt::ItemIsEditable);
  1608. ui->sources->removeItemWidget(item);
  1609. ui->sources->editItem(item);
  1610. item->setFlags(flags);
  1611. }
  1612. void OBSBasic::CreateSourcePopupMenu(QListWidgetItem *item, bool preview)
  1613. {
  1614. QMenu popup(this);
  1615. QPointer<QMenu> previewProjector;
  1616. QPointer<QMenu> sourceProjector;
  1617. if (preview) {
  1618. QAction *action = popup.addAction(
  1619. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  1620. this, SLOT(TogglePreview()));
  1621. action->setCheckable(true);
  1622. action->setChecked(obs_preview_enabled());
  1623. previewProjector = new QMenu(QTStr("PreviewProjector"));
  1624. AddProjectorMenuMonitors(previewProjector, this,
  1625. SLOT(OpenPreviewProjector()));
  1626. popup.addMenu(previewProjector);
  1627. popup.addSeparator();
  1628. }
  1629. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  1630. if (addSourceMenu)
  1631. popup.addMenu(addSourceMenu);
  1632. if (item) {
  1633. if (addSourceMenu)
  1634. popup.addSeparator();
  1635. OBSSceneItem sceneItem = GetSceneItem(item);
  1636. obs_source_t *source = obs_sceneitem_get_source(sceneItem);
  1637. QAction *action;
  1638. popup.addAction(QTStr("Rename"), this,
  1639. SLOT(EditSceneItemName()));
  1640. popup.addAction(QTStr("Remove"), this,
  1641. SLOT(on_actionRemoveSource_triggered()),
  1642. DeleteKeys.front());
  1643. popup.addSeparator();
  1644. popup.addMenu(ui->orderMenu);
  1645. popup.addMenu(ui->transformMenu);
  1646. sourceProjector = new QMenu(QTStr("SourceProjector"));
  1647. AddProjectorMenuMonitors(sourceProjector, this,
  1648. SLOT(OpenSourceProjector()));
  1649. popup.addSeparator();
  1650. popup.addMenu(sourceProjector);
  1651. popup.addSeparator();
  1652. action = popup.addAction(QTStr("Interact"), this,
  1653. SLOT(on_actionInteract_triggered()));
  1654. action->setEnabled(obs_source_get_output_flags(source) &
  1655. OBS_SOURCE_INTERACTION);
  1656. popup.addAction(QTStr("Filters"), this,
  1657. SLOT(OpenFilters()));
  1658. popup.addAction(QTStr("Properties"), this,
  1659. SLOT(on_actionSourceProperties_triggered()));
  1660. }
  1661. popup.exec(QCursor::pos());
  1662. }
  1663. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  1664. {
  1665. CreateSourcePopupMenu(ui->sources->itemAt(pos), false);
  1666. }
  1667. void OBSBasic::on_sources_itemDoubleClicked(QListWidgetItem *witem)
  1668. {
  1669. if (!witem)
  1670. return;
  1671. OBSSceneItem item = GetSceneItem(witem);
  1672. OBSSource source = obs_sceneitem_get_source(item);
  1673. if (source)
  1674. CreatePropertiesWindow(source);
  1675. }
  1676. void OBSBasic::AddSource(const char *id)
  1677. {
  1678. if (id && *id) {
  1679. OBSBasicSourceSelect sourceSelect(this, id);
  1680. sourceSelect.exec();
  1681. }
  1682. }
  1683. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  1684. {
  1685. const char *type;
  1686. bool foundValues = false;
  1687. size_t idx = 0;
  1688. QMenu *popup = new QMenu(QTStr("Add"), this);
  1689. while (obs_enum_input_types(idx++, &type)) {
  1690. const char *name = obs_source_get_display_name(
  1691. OBS_SOURCE_TYPE_INPUT, type);
  1692. if (strcmp(type, "scene") == 0)
  1693. continue;
  1694. QAction *popupItem = new QAction(QT_UTF8(name), this);
  1695. popupItem->setData(QT_UTF8(type));
  1696. connect(popupItem, SIGNAL(triggered(bool)),
  1697. this, SLOT(AddSourceFromAction()));
  1698. popup->addAction(popupItem);
  1699. foundValues = true;
  1700. }
  1701. if (!foundValues) {
  1702. delete popup;
  1703. popup = nullptr;
  1704. }
  1705. return popup;
  1706. }
  1707. void OBSBasic::AddSourceFromAction()
  1708. {
  1709. QAction *action = qobject_cast<QAction*>(sender());
  1710. if (!action)
  1711. return;
  1712. AddSource(QT_TO_UTF8(action->data().toString()));
  1713. }
  1714. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  1715. {
  1716. if (!GetCurrentScene()) {
  1717. // Tell the user he needs a scene first (help beginners).
  1718. QMessageBox::information(this,
  1719. QTStr("Basic.Main.AddSourceHelp.Title"),
  1720. QTStr("Basic.Main.AddSourceHelp.Text"));
  1721. return;
  1722. }
  1723. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  1724. if (popup)
  1725. popup->exec(pos);
  1726. }
  1727. void OBSBasic::on_actionAddSource_triggered()
  1728. {
  1729. AddSourcePopupMenu(QCursor::pos());
  1730. }
  1731. void OBSBasic::on_actionRemoveSource_triggered()
  1732. {
  1733. OBSSceneItem item = GetCurrentSceneItem();
  1734. obs_source_t *source = obs_sceneitem_get_source(item);
  1735. if (source && QueryRemoveSource(source))
  1736. obs_sceneitem_remove(item);
  1737. }
  1738. void OBSBasic::on_actionInteract_triggered()
  1739. {
  1740. OBSSceneItem item = GetCurrentSceneItem();
  1741. OBSSource source = obs_sceneitem_get_source(item);
  1742. if (source)
  1743. CreateInteractionWindow(source);
  1744. }
  1745. void OBSBasic::on_actionSourceProperties_triggered()
  1746. {
  1747. OBSSceneItem item = GetCurrentSceneItem();
  1748. OBSSource source = obs_sceneitem_get_source(item);
  1749. if (source)
  1750. CreatePropertiesWindow(source);
  1751. }
  1752. void OBSBasic::on_actionSourceUp_triggered()
  1753. {
  1754. OBSSceneItem item = GetCurrentSceneItem();
  1755. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  1756. }
  1757. void OBSBasic::on_actionSourceDown_triggered()
  1758. {
  1759. OBSSceneItem item = GetCurrentSceneItem();
  1760. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  1761. }
  1762. void OBSBasic::on_actionMoveUp_triggered()
  1763. {
  1764. OBSSceneItem item = GetCurrentSceneItem();
  1765. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_UP);
  1766. }
  1767. void OBSBasic::on_actionMoveDown_triggered()
  1768. {
  1769. OBSSceneItem item = GetCurrentSceneItem();
  1770. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_DOWN);
  1771. }
  1772. void OBSBasic::on_actionMoveToTop_triggered()
  1773. {
  1774. OBSSceneItem item = GetCurrentSceneItem();
  1775. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_TOP);
  1776. }
  1777. void OBSBasic::on_actionMoveToBottom_triggered()
  1778. {
  1779. OBSSceneItem item = GetCurrentSceneItem();
  1780. obs_sceneitem_set_order(item, OBS_ORDER_MOVE_BOTTOM);
  1781. }
  1782. static BPtr<char> ReadLogFile(const char *log)
  1783. {
  1784. char logDir[512];
  1785. if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  1786. return nullptr;
  1787. string path = (char*)logDir;
  1788. path += "/";
  1789. path += log;
  1790. BPtr<char> file = os_quick_read_utf8_file(path.c_str());
  1791. if (!file)
  1792. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  1793. return file;
  1794. }
  1795. void OBSBasic::UploadLog(const char *file)
  1796. {
  1797. BPtr<char> fileString{ReadLogFile(file)};
  1798. if (!fileString)
  1799. return;
  1800. if (!*fileString)
  1801. return;
  1802. ui->menuLogFiles->setEnabled(false);
  1803. auto data_deleter = [](obs_data_t *d) { obs_data_release(d); };
  1804. using data_t = unique_ptr<struct obs_data, decltype(data_deleter)>;
  1805. data_t content{obs_data_create(), data_deleter};
  1806. data_t files{obs_data_create(), data_deleter};
  1807. data_t request{obs_data_create(), data_deleter};
  1808. obs_data_set_string(content.get(), "content", fileString);
  1809. obs_data_set_obj(files.get(), file, content.get());
  1810. stringstream ss;
  1811. ss << "OBS " << App()->GetVersionString()
  1812. << " log file uploaded at " << CurrentDateTimeString();
  1813. obs_data_set_string(request.get(), "description", ss.str().c_str());
  1814. obs_data_set_bool(request.get(), "public", false);
  1815. obs_data_set_obj(request.get(), "files", files.get());
  1816. const char *json = obs_data_get_json(request.get());
  1817. if (!json) {
  1818. blog(LOG_ERROR, "Failed to get JSON data for log upload");
  1819. return;
  1820. }
  1821. QBuffer *postData = new QBuffer();
  1822. postData->setData(json, (int) strlen(json));
  1823. QNetworkRequest postReq(QUrl("https://api.github.com/gists"));
  1824. postReq.setHeader(QNetworkRequest::ContentTypeHeader,
  1825. "application/json");
  1826. QNetworkReply *reply = networkManager.post(postReq, postData);
  1827. /* set the reply as parent, so the buffer is deleted with the reply */
  1828. postData->setParent(reply);
  1829. connect(reply, SIGNAL(finished()),
  1830. this, SLOT(logUploadFinished()));
  1831. }
  1832. void OBSBasic::on_actionShowLogs_triggered()
  1833. {
  1834. char logDir[512];
  1835. if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  1836. return;
  1837. QUrl url = QUrl::fromLocalFile(QT_UTF8(logDir));
  1838. QDesktopServices::openUrl(url);
  1839. }
  1840. void OBSBasic::on_actionUploadCurrentLog_triggered()
  1841. {
  1842. UploadLog(App()->GetCurrentLog());
  1843. }
  1844. void OBSBasic::on_actionUploadLastLog_triggered()
  1845. {
  1846. UploadLog(App()->GetLastLog());
  1847. }
  1848. void OBSBasic::on_actionViewCurrentLog_triggered()
  1849. {
  1850. char logDir[512];
  1851. if (os_get_config_path(logDir, sizeof(logDir), "obs-studio/logs") <= 0)
  1852. return;
  1853. const char* log = App()->GetCurrentLog();
  1854. string path = (char*)logDir;
  1855. path += "/";
  1856. path += log;
  1857. QUrl url = QUrl::fromLocalFile(QT_UTF8(path.c_str()));
  1858. QDesktopServices::openUrl(url);
  1859. }
  1860. void OBSBasic::on_actionCheckForUpdates_triggered()
  1861. {
  1862. CheckForUpdates();
  1863. }
  1864. void OBSBasic::logUploadFinished()
  1865. {
  1866. ui->menuLogFiles->setEnabled(true);
  1867. QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
  1868. if (!reply || reply->error()) {
  1869. QMessageBox::information(this,
  1870. QTStr("LogReturnDialog.ErrorUploadingLog"),
  1871. reply->errorString());
  1872. return;
  1873. }
  1874. QByteArray raw = reply->readAll();
  1875. if (!raw.length())
  1876. return;
  1877. obs_data_t *returnData = obs_data_create_from_json(raw.constData());
  1878. QString logURL = obs_data_get_string(returnData, "html_url");
  1879. obs_data_release(returnData);
  1880. OBSLogReply logDialog(this, logURL);
  1881. logDialog.exec();
  1882. reply->deleteLater();
  1883. }
  1884. static void RenameListItem(OBSBasic *parent, QListWidget *listWidget,
  1885. obs_source_t *source, const string &name)
  1886. {
  1887. const char *prevName = obs_source_get_name(source);
  1888. if (name == prevName)
  1889. return;
  1890. obs_source_t *foundSource = obs_get_source_by_name(name.c_str());
  1891. QListWidgetItem *listItem = listWidget->currentItem();
  1892. if (foundSource || name.empty()) {
  1893. listItem->setText(QT_UTF8(prevName));
  1894. if (foundSource) {
  1895. QMessageBox::information(parent,
  1896. QTStr("NameExists.Title"),
  1897. QTStr("NameExists.Text"));
  1898. } else if (name.empty()) {
  1899. QMessageBox::information(parent,
  1900. QTStr("NoNameEntered.Title"),
  1901. QTStr("NoNameEntered.Text"));
  1902. }
  1903. obs_source_release(foundSource);
  1904. } else {
  1905. listItem->setText(QT_UTF8(name.c_str()));
  1906. obs_source_set_name(source, name.c_str());
  1907. }
  1908. }
  1909. void OBSBasic::SceneNameEdited(QWidget *editor,
  1910. QAbstractItemDelegate::EndEditHint endHint)
  1911. {
  1912. OBSScene scene = GetCurrentScene();
  1913. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  1914. string text = QT_TO_UTF8(edit->text().trimmed());
  1915. if (!scene)
  1916. return;
  1917. obs_source_t *source = obs_scene_get_source(scene);
  1918. RenameListItem(this, ui->scenes, source, text);
  1919. UNUSED_PARAMETER(endHint);
  1920. }
  1921. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  1922. QAbstractItemDelegate::EndEditHint endHint)
  1923. {
  1924. OBSSceneItem item = GetCurrentSceneItem();
  1925. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  1926. string text = QT_TO_UTF8(edit->text().trimmed());
  1927. if (!item)
  1928. return;
  1929. obs_source_t *source = obs_sceneitem_get_source(item);
  1930. RenameListItem(this, ui->sources, source, text);
  1931. QListWidgetItem *listItem = ui->sources->currentItem();
  1932. listItem->setText(QString());
  1933. SetupVisibilityItem(ui->sources, listItem, item);
  1934. UNUSED_PARAMETER(endHint);
  1935. }
  1936. void OBSBasic::OpenFilters()
  1937. {
  1938. OBSSceneItem item = GetCurrentSceneItem();
  1939. OBSSource source = obs_sceneitem_get_source(item);
  1940. CreateFiltersWindow(source);
  1941. }
  1942. void OBSBasic::OpenSceneFilters()
  1943. {
  1944. OBSScene scene = GetCurrentScene();
  1945. OBSSource source = obs_scene_get_source(scene);
  1946. CreateFiltersWindow(source);
  1947. }
  1948. void OBSBasic::StreamingStart()
  1949. {
  1950. ui->streamButton->setText(QTStr("Basic.Main.StopStreaming"));
  1951. ui->streamButton->setEnabled(true);
  1952. ui->statusbar->StreamStarted(outputHandler->streamOutput);
  1953. }
  1954. void OBSBasic::StreamingStop(int code)
  1955. {
  1956. const char *errorMessage;
  1957. switch (code) {
  1958. case OBS_OUTPUT_BAD_PATH:
  1959. errorMessage = Str("Output.ConnectFail.BadPath");
  1960. break;
  1961. case OBS_OUTPUT_CONNECT_FAILED:
  1962. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  1963. break;
  1964. case OBS_OUTPUT_INVALID_STREAM:
  1965. errorMessage = Str("Output.ConnectFail.InvalidStream");
  1966. break;
  1967. default:
  1968. case OBS_OUTPUT_ERROR:
  1969. errorMessage = Str("Output.ConnectFail.Error");
  1970. break;
  1971. case OBS_OUTPUT_DISCONNECTED:
  1972. /* doesn't happen if output is set to reconnect. note that
  1973. * reconnects are handled in the output, not in the UI */
  1974. errorMessage = Str("Output.ConnectFail.Disconnected");
  1975. }
  1976. ui->statusbar->StreamStopped();
  1977. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  1978. ui->streamButton->setEnabled(true);
  1979. if (code != OBS_OUTPUT_SUCCESS)
  1980. QMessageBox::information(this,
  1981. QTStr("Output.ConnectFail.Title"),
  1982. QT_UTF8(errorMessage));
  1983. }
  1984. void OBSBasic::RecordingStart()
  1985. {
  1986. ui->statusbar->RecordingStarted(outputHandler->fileOutput);
  1987. }
  1988. void OBSBasic::RecordingStop()
  1989. {
  1990. ui->statusbar->RecordingStopped();
  1991. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  1992. }
  1993. void OBSBasic::on_streamButton_clicked()
  1994. {
  1995. SaveProject();
  1996. if (outputHandler->StreamingActive()) {
  1997. outputHandler->StopStreaming();
  1998. } else {
  1999. if (outputHandler->StartStreaming(service)) {
  2000. ui->streamButton->setEnabled(false);
  2001. ui->streamButton->setText(
  2002. QTStr("Basic.Main.Connecting"));
  2003. }
  2004. }
  2005. }
  2006. void OBSBasic::on_recordButton_clicked()
  2007. {
  2008. SaveProject();
  2009. if (outputHandler->RecordingActive()) {
  2010. outputHandler->StopRecording();
  2011. } else {
  2012. if (outputHandler->StartRecording()) {
  2013. ui->recordButton->setText(
  2014. QTStr("Basic.Main.StopRecording"));
  2015. }
  2016. }
  2017. }
  2018. void OBSBasic::on_settingsButton_clicked()
  2019. {
  2020. OBSBasicSettings settings(this);
  2021. settings.exec();
  2022. }
  2023. void OBSBasic::on_preview_customContextMenuRequested(const QPoint &pos)
  2024. {
  2025. CreateSourcePopupMenu(ui->sources->currentItem(), true);
  2026. UNUSED_PARAMETER(pos);
  2027. }
  2028. void OBSBasic::on_previewDisabledLabel_customContextMenuRequested(
  2029. const QPoint &pos)
  2030. {
  2031. QMenu popup(this);
  2032. QPointer<QMenu> previewProjector;
  2033. QAction *action = popup.addAction(
  2034. QTStr("Basic.Main.PreviewConextMenu.Enable"),
  2035. this, SLOT(TogglePreview()));
  2036. action->setCheckable(true);
  2037. action->setChecked(obs_preview_enabled());
  2038. previewProjector = new QMenu(QTStr("PreviewProjector"));
  2039. AddProjectorMenuMonitors(previewProjector, this,
  2040. SLOT(OpenPreviewProjector()));
  2041. popup.addMenu(previewProjector);
  2042. popup.exec(QCursor::pos());
  2043. UNUSED_PARAMETER(pos);
  2044. }
  2045. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  2046. {
  2047. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  2048. if (strcmp(val, "10") == 0) {
  2049. num = 10;
  2050. den = 1;
  2051. } else if (strcmp(val, "20") == 0) {
  2052. num = 20;
  2053. den = 1;
  2054. } else if (strcmp(val, "25") == 0) {
  2055. num = 25;
  2056. den = 1;
  2057. } else if (strcmp(val, "29.97") == 0) {
  2058. num = 30000;
  2059. den = 1001;
  2060. } else if (strcmp(val, "48") == 0) {
  2061. num = 48;
  2062. den = 1;
  2063. } else if (strcmp(val, "59.94") == 0) {
  2064. num = 60000;
  2065. den = 1001;
  2066. } else if (strcmp(val, "60") == 0) {
  2067. num = 60;
  2068. den = 1;
  2069. } else {
  2070. num = 30;
  2071. den = 1;
  2072. }
  2073. }
  2074. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  2075. {
  2076. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  2077. den = 1;
  2078. }
  2079. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  2080. {
  2081. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  2082. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  2083. }
  2084. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  2085. {
  2086. num = 1000000000;
  2087. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  2088. }
  2089. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  2090. {
  2091. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  2092. if (type == 1) //"Integer"
  2093. GetFPSInteger(num, den);
  2094. else if (type == 2) //"Fraction"
  2095. GetFPSFraction(num, den);
  2096. else if (false) //"Nanoseconds", currently not implemented
  2097. GetFPSNanoseconds(num, den);
  2098. else
  2099. GetFPSCommon(num, den);
  2100. }
  2101. config_t *OBSBasic::Config() const
  2102. {
  2103. return basicConfig;
  2104. }
  2105. void OBSBasic::on_actionEditTransform_triggered()
  2106. {
  2107. if (transformWindow)
  2108. transformWindow->close();
  2109. transformWindow = new OBSBasicTransform(this);
  2110. transformWindow->show();
  2111. transformWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  2112. }
  2113. void OBSBasic::on_actionResetTransform_triggered()
  2114. {
  2115. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2116. {
  2117. if (!obs_sceneitem_selected(item))
  2118. return true;
  2119. obs_transform_info info;
  2120. vec2_set(&info.pos, 0.0f, 0.0f);
  2121. vec2_set(&info.scale, 1.0f, 1.0f);
  2122. info.rot = 0.0f;
  2123. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  2124. info.bounds_type = OBS_BOUNDS_NONE;
  2125. info.bounds_alignment = OBS_ALIGN_CENTER;
  2126. vec2_set(&info.bounds, 0.0f, 0.0f);
  2127. obs_sceneitem_set_info(item, &info);
  2128. UNUSED_PARAMETER(scene);
  2129. UNUSED_PARAMETER(param);
  2130. return true;
  2131. };
  2132. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2133. }
  2134. static void GetItemBox(obs_sceneitem_t *item, vec3 &tl, vec3 &br)
  2135. {
  2136. matrix4 boxTransform;
  2137. obs_sceneitem_get_box_transform(item, &boxTransform);
  2138. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  2139. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  2140. auto GetMinPos = [&] (float x, float y)
  2141. {
  2142. vec3 pos;
  2143. vec3_set(&pos, x, y, 0.0f);
  2144. vec3_transform(&pos, &pos, &boxTransform);
  2145. vec3_min(&tl, &tl, &pos);
  2146. vec3_max(&br, &br, &pos);
  2147. };
  2148. GetMinPos(0.0f, 0.0f);
  2149. GetMinPos(1.0f, 0.0f);
  2150. GetMinPos(0.0f, 1.0f);
  2151. GetMinPos(1.0f, 1.0f);
  2152. }
  2153. static vec3 GetItemTL(obs_sceneitem_t *item)
  2154. {
  2155. vec3 tl, br;
  2156. GetItemBox(item, tl, br);
  2157. return tl;
  2158. }
  2159. static void SetItemTL(obs_sceneitem_t *item, const vec3 &tl)
  2160. {
  2161. vec3 newTL;
  2162. vec2 pos;
  2163. obs_sceneitem_get_pos(item, &pos);
  2164. newTL = GetItemTL(item);
  2165. pos.x += tl.x - newTL.x;
  2166. pos.y += tl.y - newTL.y;
  2167. obs_sceneitem_set_pos(item, &pos);
  2168. }
  2169. static bool RotateSelectedSources(obs_scene_t *scene, obs_sceneitem_t *item,
  2170. void *param)
  2171. {
  2172. if (!obs_sceneitem_selected(item))
  2173. return true;
  2174. float rot = *reinterpret_cast<float*>(param);
  2175. vec3 tl = GetItemTL(item);
  2176. rot += obs_sceneitem_get_rot(item);
  2177. if (rot >= 360.0f) rot -= 360.0f;
  2178. else if (rot <= -360.0f) rot += 360.0f;
  2179. obs_sceneitem_set_rot(item, rot);
  2180. SetItemTL(item, tl);
  2181. UNUSED_PARAMETER(scene);
  2182. UNUSED_PARAMETER(param);
  2183. return true;
  2184. };
  2185. void OBSBasic::on_actionRotate90CW_triggered()
  2186. {
  2187. float f90CW = 90.0f;
  2188. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  2189. }
  2190. void OBSBasic::on_actionRotate90CCW_triggered()
  2191. {
  2192. float f90CCW = -90.0f;
  2193. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  2194. }
  2195. void OBSBasic::on_actionRotate180_triggered()
  2196. {
  2197. float f180 = 180.0f;
  2198. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  2199. }
  2200. static bool MultiplySelectedItemScale(obs_scene_t *scene, obs_sceneitem_t *item,
  2201. void *param)
  2202. {
  2203. vec2 &mul = *reinterpret_cast<vec2*>(param);
  2204. if (!obs_sceneitem_selected(item))
  2205. return true;
  2206. vec3 tl = GetItemTL(item);
  2207. vec2 scale;
  2208. obs_sceneitem_get_scale(item, &scale);
  2209. vec2_mul(&scale, &scale, &mul);
  2210. obs_sceneitem_set_scale(item, &scale);
  2211. SetItemTL(item, tl);
  2212. UNUSED_PARAMETER(scene);
  2213. return true;
  2214. }
  2215. void OBSBasic::on_actionFlipHorizontal_triggered()
  2216. {
  2217. vec2 scale;
  2218. vec2_set(&scale, -1.0f, 1.0f);
  2219. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2220. &scale);
  2221. }
  2222. void OBSBasic::on_actionFlipVertical_triggered()
  2223. {
  2224. vec2 scale;
  2225. vec2_set(&scale, 1.0f, -1.0f);
  2226. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  2227. &scale);
  2228. }
  2229. static bool CenterAlignSelectedItems(obs_scene_t *scene, obs_sceneitem_t *item,
  2230. void *param)
  2231. {
  2232. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  2233. if (!obs_sceneitem_selected(item))
  2234. return true;
  2235. obs_video_info ovi;
  2236. obs_get_video_info(&ovi);
  2237. obs_transform_info itemInfo;
  2238. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  2239. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  2240. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  2241. itemInfo.rot = 0.0f;
  2242. vec2_set(&itemInfo.bounds,
  2243. float(ovi.base_width), float(ovi.base_height));
  2244. itemInfo.bounds_type = boundsType;
  2245. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  2246. obs_sceneitem_set_info(item, &itemInfo);
  2247. UNUSED_PARAMETER(scene);
  2248. return true;
  2249. }
  2250. void OBSBasic::on_actionFitToScreen_triggered()
  2251. {
  2252. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  2253. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2254. &boundsType);
  2255. }
  2256. void OBSBasic::on_actionStretchToScreen_triggered()
  2257. {
  2258. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  2259. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  2260. &boundsType);
  2261. }
  2262. void OBSBasic::on_actionCenterToScreen_triggered()
  2263. {
  2264. auto func = [] (obs_scene_t *scene, obs_sceneitem_t *item, void *param)
  2265. {
  2266. vec3 tl, br, itemCenter, screenCenter, offset;
  2267. obs_video_info ovi;
  2268. if (!obs_sceneitem_selected(item))
  2269. return true;
  2270. obs_get_video_info(&ovi);
  2271. vec3_set(&screenCenter, float(ovi.base_width),
  2272. float(ovi.base_height), 0.0f);
  2273. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  2274. GetItemBox(item, tl, br);
  2275. vec3_sub(&itemCenter, &br, &tl);
  2276. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  2277. vec3_add(&itemCenter, &itemCenter, &tl);
  2278. vec3_sub(&offset, &screenCenter, &itemCenter);
  2279. vec3_add(&tl, &tl, &offset);
  2280. SetItemTL(item, tl);
  2281. UNUSED_PARAMETER(scene);
  2282. UNUSED_PARAMETER(param);
  2283. return true;
  2284. };
  2285. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  2286. }
  2287. void OBSBasic::TogglePreview()
  2288. {
  2289. bool enabled = !obs_preview_enabled();
  2290. obs_preview_set_enabled(enabled);
  2291. ui->preview->setVisible(enabled);
  2292. ui->previewDisabledLabel->setVisible(!enabled);
  2293. }
  2294. void OBSBasic::Nudge(int dist, MoveDir dir)
  2295. {
  2296. struct MoveInfo {
  2297. float dist;
  2298. MoveDir dir;
  2299. } info = {(float)dist, dir};
  2300. auto func = [] (obs_scene_t*, obs_sceneitem_t *item, void *param)
  2301. {
  2302. MoveInfo *info = reinterpret_cast<MoveInfo*>(param);
  2303. struct vec2 dir;
  2304. struct vec2 pos;
  2305. vec2_set(&dir, 0.0f, 0.0f);
  2306. if (!obs_sceneitem_selected(item))
  2307. return true;
  2308. switch (info->dir) {
  2309. case MoveDir::Up: dir.y = -info->dist; break;
  2310. case MoveDir::Down: dir.y = info->dist; break;
  2311. case MoveDir::Left: dir.x = -info->dist; break;
  2312. case MoveDir::Right: dir.x = info->dist; break;
  2313. }
  2314. obs_sceneitem_get_pos(item, &pos);
  2315. vec2_add(&pos, &pos, &dir);
  2316. obs_sceneitem_set_pos(item, &pos);
  2317. return true;
  2318. };
  2319. obs_scene_enum_items(GetCurrentScene(), func, &info);
  2320. }
  2321. void OBSBasic::NudgeUp() {Nudge(1, MoveDir::Up);}
  2322. void OBSBasic::NudgeDown() {Nudge(1, MoveDir::Down);}
  2323. void OBSBasic::NudgeLeft() {Nudge(1, MoveDir::Left);}
  2324. void OBSBasic::NudgeRight() {Nudge(1, MoveDir::Right);}
  2325. void OBSBasic::OpenProjector(obs_source_t *source, int monitor)
  2326. {
  2327. /* seriously? 10 monitors? */
  2328. if (monitor > 9)
  2329. return;
  2330. delete projectors[monitor];
  2331. projectors[monitor].clear();
  2332. OBSProjector *projector = new OBSProjector(this, source);
  2333. projector->Init(monitor);
  2334. projectors[monitor] = projector;
  2335. }
  2336. void OBSBasic::OpenPreviewProjector()
  2337. {
  2338. int monitor = sender()->property("monitor").toInt();
  2339. OpenProjector(nullptr, monitor);
  2340. }
  2341. void OBSBasic::OpenSourceProjector()
  2342. {
  2343. int monitor = sender()->property("monitor").toInt();
  2344. OBSSceneItem item = GetCurrentSceneItem();
  2345. if (!item)
  2346. return;
  2347. OpenProjector(obs_sceneitem_get_source(item), monitor);
  2348. }
  2349. void OBSBasic::OpenSceneProjector()
  2350. {
  2351. int monitor = sender()->property("monitor").toInt();
  2352. OBSScene scene = GetCurrentScene();
  2353. if (!scene)
  2354. return;
  2355. OpenProjector(obs_scene_get_source(scene), monitor);
  2356. }