1
0

window-basic-main.cpp 78 KB

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