window-basic-main.cpp 74 KB

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