1
0

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