window-basic-main.cpp 65 KB

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