window-basic-main.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  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 <obs.hpp>
  16. #include <QMessageBox>
  17. #include <QShowEvent>
  18. #include <QFileDialog>
  19. #include <QNetworkRequest>
  20. #include <QNetworkReply>
  21. #include <util/dstr.h>
  22. #include <util/util.hpp>
  23. #include <util/platform.h>
  24. #include <graphics/math-defs.h>
  25. #include "obs-app.hpp"
  26. #include "platform.hpp"
  27. #include "window-basic-settings.hpp"
  28. #include "window-namedialog.hpp"
  29. #include "window-basic-source-select.hpp"
  30. #include "window-basic-main.hpp"
  31. #include "window-basic-properties.hpp"
  32. #include "window-log-reply.hpp"
  33. #include "qt-wrappers.hpp"
  34. #include "display-helpers.hpp"
  35. #include "volume-control.hpp"
  36. #include "ui_OBSBasic.h"
  37. #include <fstream>
  38. #include <sstream>
  39. #include <QScreen>
  40. #include <QWindow>
  41. #define PREVIEW_EDGE_SIZE 10
  42. using namespace std;
  43. Q_DECLARE_METATYPE(OBSScene);
  44. Q_DECLARE_METATYPE(OBSSceneItem);
  45. Q_DECLARE_METATYPE(order_movement);
  46. OBSBasic::OBSBasic(QWidget *parent)
  47. : OBSMainWindow (parent),
  48. ui (new Ui::OBSBasic)
  49. {
  50. ui->setupUi(this);
  51. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  52. struct obs_video_info ovi;
  53. if (obs_get_video_info(&ovi))
  54. ResizePreview(ovi.base_width, ovi.base_height);
  55. });
  56. stringstream name;
  57. name << "OBS " << App()->GetVersionString();
  58. blog(LOG_INFO, "%s", name.str().c_str());
  59. setWindowTitle(QT_UTF8(name.str().c_str()));
  60. connect(ui->scenes->itemDelegate(),
  61. SIGNAL(closeEditor(QWidget*,
  62. QAbstractItemDelegate::EndEditHint)),
  63. this,
  64. SLOT(SceneNameEdited(QWidget*,
  65. QAbstractItemDelegate::EndEditHint)));
  66. connect(ui->sources->itemDelegate(),
  67. SIGNAL(closeEditor(QWidget*,
  68. QAbstractItemDelegate::EndEditHint)),
  69. this,
  70. SLOT(SceneItemNameEdited(QWidget*,
  71. QAbstractItemDelegate::EndEditHint)));
  72. }
  73. static void SaveAudioDevice(const char *name, int channel, obs_data_t parent)
  74. {
  75. obs_source_t source = obs_get_output_source(channel);
  76. if (!source)
  77. return;
  78. obs_data_t data = obs_save_source(source);
  79. obs_data_setobj(parent, name, data);
  80. obs_data_release(data);
  81. obs_source_release(source);
  82. }
  83. static obs_data_t GenerateSaveData()
  84. {
  85. obs_data_t saveData = obs_data_create();
  86. obs_data_array_t sourcesArray = obs_save_sources();
  87. obs_source_t currentScene = obs_get_output_source(0);
  88. const char *sceneName = obs_source_getname(currentScene);
  89. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  90. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  91. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  92. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  93. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  94. obs_data_setstring(saveData, "current_scene", sceneName);
  95. obs_data_setarray(saveData, "sources", sourcesArray);
  96. obs_data_array_release(sourcesArray);
  97. obs_source_release(currentScene);
  98. return saveData;
  99. }
  100. void OBSBasic::ClearVolumeControls()
  101. {
  102. VolControl *control;
  103. for (size_t i = 0; i < volumes.size(); i++) {
  104. control = volumes[i];
  105. delete control;
  106. }
  107. volumes.clear();
  108. }
  109. void OBSBasic::Save(const char *file)
  110. {
  111. obs_data_t saveData = GenerateSaveData();
  112. const char *jsonData = obs_data_getjson(saveData);
  113. /* TODO maybe a message box here? */
  114. if (!os_quick_write_utf8_file(file, jsonData, strlen(jsonData), false))
  115. blog(LOG_ERROR, "Could not save scene data to %s", file);
  116. obs_data_release(saveData);
  117. }
  118. static void LoadAudioDevice(const char *name, int channel, obs_data_t parent)
  119. {
  120. obs_data_t data = obs_data_getobj(parent, name);
  121. if (!data)
  122. return;
  123. obs_source_t source = obs_load_source(data);
  124. if (source) {
  125. obs_set_output_source(channel, source);
  126. obs_source_release(source);
  127. }
  128. obs_data_release(data);
  129. }
  130. void OBSBasic::CreateDefaultScene()
  131. {
  132. obs_scene_t scene = obs_scene_create(Str("Basic.Scene"));
  133. obs_source_t source = obs_scene_getsource(scene);
  134. obs_add_source(source);
  135. #ifdef __APPLE__
  136. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, "display_capture",
  137. Str("Basic.DisplayCapture"), NULL);
  138. if (source) {
  139. obs_scene_add(scene, source);
  140. obs_add_source(source);
  141. obs_source_release(source);
  142. }
  143. #endif
  144. obs_set_output_source(0, obs_scene_getsource(scene));
  145. obs_scene_release(scene);
  146. }
  147. void OBSBasic::Load(const char *file)
  148. {
  149. if (!file) {
  150. blog(LOG_ERROR, "Could not find file %s", file);
  151. return;
  152. }
  153. BPtr<char> jsonData = os_quick_read_utf8_file(file);
  154. if (!jsonData) {
  155. CreateDefaultScene();
  156. return;
  157. }
  158. obs_data_t data = obs_data_create_from_json(jsonData);
  159. obs_data_array_t sources = obs_data_getarray(data, "sources");
  160. const char *sceneName = obs_data_getstring(data, "current_scene");
  161. obs_source_t curScene;
  162. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  163. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  164. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  165. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  166. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  167. obs_load_sources(sources);
  168. curScene = obs_get_source_by_name(sceneName);
  169. obs_set_output_source(0, curScene);
  170. obs_source_release(curScene);
  171. obs_data_array_release(sources);
  172. obs_data_release(data);
  173. }
  174. static inline bool HasAudioDevices(const char *source_id)
  175. {
  176. const char *output_id = source_id;
  177. obs_properties_t props = obs_get_source_properties(
  178. OBS_SOURCE_TYPE_INPUT, output_id);
  179. size_t count = 0;
  180. if (!props)
  181. return false;
  182. obs_property_t devices = obs_properties_get(props, "device_id");
  183. if (devices)
  184. count = obs_property_list_item_count(devices);
  185. obs_properties_destroy(props);
  186. return count != 0;
  187. }
  188. static void OBSStartStreaming(void *data, calldata_t params)
  189. {
  190. UNUSED_PARAMETER(params);
  191. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  192. "StreamingStart");
  193. }
  194. static void OBSStopStreaming(void *data, calldata_t params)
  195. {
  196. int code = (int)calldata_int(params, "code");
  197. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  198. "StreamingStop", Q_ARG(int, code));
  199. }
  200. static void OBSStopRecording(void *data, calldata_t params)
  201. {
  202. UNUSED_PARAMETER(params);
  203. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  204. "RecordingStop");
  205. }
  206. #define SERVICE_PATH "obs-studio/basic/service.json"
  207. void OBSBasic::SaveService()
  208. {
  209. if (!service)
  210. return;
  211. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  212. if (!serviceJsonPath)
  213. return;
  214. obs_data_t data = obs_data_create();
  215. obs_data_t settings = obs_service_get_settings(service);
  216. obs_data_setstring(data, "type", obs_service_gettype(service));
  217. obs_data_setobj(data, "settings", settings);
  218. const char *json = obs_data_getjson(data);
  219. os_quick_write_utf8_file(serviceJsonPath, json, strlen(json), false);
  220. obs_data_release(settings);
  221. obs_data_release(data);
  222. }
  223. bool OBSBasic::LoadService()
  224. {
  225. const char *type;
  226. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  227. if (!serviceJsonPath)
  228. return false;
  229. BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
  230. if (!jsonText)
  231. return false;
  232. obs_data_t data = obs_data_create_from_json(jsonText);
  233. obs_data_set_default_string(data, "type", "rtmp_common");
  234. type = obs_data_getstring(data, "type");
  235. obs_data_t settings = obs_data_getobj(data, "settings");
  236. service = obs_service_create(type, "default", settings);
  237. obs_data_release(settings);
  238. obs_data_release(data);
  239. return !!service;
  240. }
  241. bool OBSBasic::InitOutputs()
  242. {
  243. fileOutput = obs_output_create("flv_output", "default", nullptr);
  244. if (!fileOutput)
  245. return false;
  246. streamOutput = obs_output_create("rtmp_output", "default", nullptr);
  247. if (!streamOutput)
  248. return false;
  249. signal_handler_connect(obs_output_signalhandler(streamOutput),
  250. "start", OBSStartStreaming, this);
  251. signal_handler_connect(obs_output_signalhandler(streamOutput),
  252. "stop", OBSStopStreaming, this);
  253. signal_handler_connect(obs_output_signalhandler(fileOutput),
  254. "stop", OBSStopRecording, this);
  255. return true;
  256. }
  257. bool OBSBasic::InitEncoders()
  258. {
  259. x264 = obs_video_encoder_create("obs_x264", "h264", nullptr);
  260. if (!x264)
  261. return false;
  262. aac = obs_audio_encoder_create("libfdk_aac", "aac", nullptr);
  263. if(!aac)
  264. aac = obs_audio_encoder_create("ffmpeg_aac", "aac", nullptr);
  265. if (!aac)
  266. return false;
  267. return true;
  268. }
  269. bool OBSBasic::InitService()
  270. {
  271. if (LoadService())
  272. return true;
  273. service = obs_service_create("rtmp_common", nullptr, nullptr);
  274. if (!service)
  275. return false;
  276. return true;
  277. }
  278. bool OBSBasic::InitBasicConfigDefaults()
  279. {
  280. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  281. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  282. config_set_default_int(basicConfig, "Window", "PosX", -1);
  283. config_set_default_int(basicConfig, "Window", "PosY", -1);
  284. config_set_default_int(basicConfig, "Window", "SizeX", -1);
  285. config_set_default_int(basicConfig, "Window", "SizeY", -1);
  286. vector<MonitorInfo> monitors;
  287. GetMonitors(monitors);
  288. if (!monitors.size()) {
  289. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  290. "technically shouldn't be possible.");
  291. return false;
  292. }
  293. uint32_t cx = monitors[0].cx;
  294. uint32_t cy = monitors[0].cy;
  295. /* TODO: temporary */
  296. config_set_default_string(basicConfig, "SimpleOutput", "FilePath",
  297. GetDefaultVideoSavePath().c_str());
  298. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  299. 2500);
  300. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 128);
  301. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  302. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  303. cx = cx * 10 / 15;
  304. cy = cy * 10 / 15;
  305. config_set_default_uint (basicConfig, "Video", "OutputCX", cx);
  306. config_set_default_uint (basicConfig, "Video", "OutputCY", cy);
  307. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  308. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  309. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  310. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  311. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  312. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  313. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  314. "Stereo");
  315. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  316. config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
  317. hasDesktopAudio ? "default" : "disabled");
  318. config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
  319. "disabled");
  320. config_set_default_string(basicConfig, "Audio", "AuxDevice1",
  321. hasInputAudio ? "default" : "disabled");
  322. config_set_default_string(basicConfig, "Audio", "AuxDevice2",
  323. "disabled");
  324. config_set_default_string(basicConfig, "Audio", "AuxDevice3",
  325. "disabled");
  326. return true;
  327. }
  328. bool OBSBasic::InitBasicConfig()
  329. {
  330. BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini"));
  331. int code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  332. if (code != CONFIG_SUCCESS) {
  333. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  334. return false;
  335. }
  336. return InitBasicConfigDefaults();
  337. }
  338. void OBSBasic::InitOBSCallbacks()
  339. {
  340. signal_handler_connect(obs_signalhandler(), "source_add",
  341. OBSBasic::SourceAdded, this);
  342. signal_handler_connect(obs_signalhandler(), "source_remove",
  343. OBSBasic::SourceRemoved, this);
  344. signal_handler_connect(obs_signalhandler(), "channel_change",
  345. OBSBasic::ChannelChanged, this);
  346. signal_handler_connect(obs_signalhandler(), "source_activate",
  347. OBSBasic::SourceActivated, this);
  348. signal_handler_connect(obs_signalhandler(), "source_deactivate",
  349. OBSBasic::SourceDeactivated, this);
  350. signal_handler_connect(obs_signalhandler(), "source_rename",
  351. OBSBasic::SourceRenamed, this);
  352. }
  353. void OBSBasic::InitPrimitives()
  354. {
  355. gs_entercontext(obs_graphics());
  356. gs_renderstart(true);
  357. gs_vertex2f(0.0f, 0.0f);
  358. gs_vertex2f(0.0f, 1.0f);
  359. gs_vertex2f(1.0f, 1.0f);
  360. gs_vertex2f(1.0f, 0.0f);
  361. gs_vertex2f(0.0f, 0.0f);
  362. box = gs_rendersave();
  363. gs_renderstart(true);
  364. for (int i = 0; i <= 360; i += (360/20)) {
  365. float pos = RAD(float(i));
  366. gs_vertex2f(cosf(pos), sinf(pos));
  367. }
  368. circle = gs_rendersave();
  369. gs_leavecontext();
  370. }
  371. void OBSBasic::OBSInit()
  372. {
  373. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  374. /* make sure it's fully displayed before doing any initialization */
  375. show();
  376. App()->processEvents();
  377. if (!obs_startup(App()->GetLocale()))
  378. throw "Failed to initialize libobs";
  379. if (!InitBasicConfig())
  380. throw "Failed to load basic.ini";
  381. if (!ResetVideo())
  382. throw "Failed to initialize video";
  383. if (!ResetAudio())
  384. throw "Failed to initialize audio";
  385. InitOBSCallbacks();
  386. /* TODO: this is a test, all modules will be searched for and loaded
  387. * automatically later */
  388. obs_load_module("image-source");
  389. obs_load_module("test-input");
  390. obs_load_module("obs-ffmpeg");
  391. obs_load_module("obs-libfdk");
  392. obs_load_module("obs-x264");
  393. obs_load_module("obs-outputs");
  394. obs_load_module("rtmp-services");
  395. #ifdef __APPLE__
  396. obs_load_module("mac-avcapture");
  397. obs_load_module("mac-capture");
  398. #elif _WIN32
  399. obs_load_module("win-wasapi");
  400. obs_load_module("win-capture");
  401. obs_load_module("win-dshow");
  402. #else
  403. obs_load_module("linux-xshm");
  404. obs_load_module("linux-xcomposite");
  405. obs_load_module("linux-pulseaudio");
  406. obs_load_module("linux-v4l2");
  407. #endif
  408. if (!InitOutputs())
  409. throw "Failed to initialize outputs";
  410. if (!InitEncoders())
  411. throw "Failed to initialize encoders";
  412. if (!InitService())
  413. throw "Failed to initialize service";
  414. InitPrimitives();
  415. Load(savePath);
  416. ResetAudioDevices();
  417. loaded = true;
  418. }
  419. OBSBasic::~OBSBasic()
  420. {
  421. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  422. SaveService();
  423. Save(savePath);
  424. /* XXX: any obs data must be released before calling obs_shutdown.
  425. * currently, we can't automate this with C++ RAII because of the
  426. * delicate nature of obs_shutdown needing to be freed before the UI
  427. * can be freed, and we have no control over the destruction order of
  428. * the Qt UI stuff, so we have to manually clear any references to
  429. * libobs. */
  430. if (properties)
  431. delete properties;
  432. if (transformWindow)
  433. delete transformWindow;
  434. ClearVolumeControls();
  435. ui->sources->clear();
  436. ui->scenes->clear();
  437. gs_entercontext(obs_graphics());
  438. vertexbuffer_destroy(box);
  439. vertexbuffer_destroy(circle);
  440. gs_leavecontext();
  441. obs_shutdown();
  442. }
  443. OBSScene OBSBasic::GetCurrentScene()
  444. {
  445. QListWidgetItem *item = ui->scenes->currentItem();
  446. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  447. }
  448. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  449. {
  450. QListWidgetItem *item = ui->sources->currentItem();
  451. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  452. }
  453. void OBSBasic::UpdateSources(OBSScene scene)
  454. {
  455. ui->sources->clear();
  456. obs_scene_enum_items(scene,
  457. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  458. {
  459. OBSBasic *window = static_cast<OBSBasic*>(p);
  460. window->InsertSceneItem(item);
  461. UNUSED_PARAMETER(scene);
  462. return true;
  463. }, this);
  464. }
  465. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  466. {
  467. obs_source_t source = obs_sceneitem_getsource(item);
  468. const char *name = obs_source_getname(source);
  469. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  470. listItem->setFlags(listItem->flags() | Qt::ItemIsEditable);
  471. listItem->setData(Qt::UserRole,
  472. QVariant::fromValue(OBSSceneItem(item)));
  473. ui->sources->insertItem(0, listItem);
  474. ui->sources->setCurrentRow(0);
  475. /* if the source was just created, open properties dialog */
  476. if (sourceSceneRefs[source] == 0 && loaded) {
  477. delete properties;
  478. properties = new OBSBasicProperties(this, source);
  479. properties->Init();
  480. }
  481. }
  482. /* Qt callbacks for invokeMethod */
  483. void OBSBasic::AddScene(OBSSource source)
  484. {
  485. const char *name = obs_source_getname(source);
  486. obs_scene_t scene = obs_scene_fromsource(source);
  487. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  488. item->setFlags(item->flags() | Qt::ItemIsEditable);
  489. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  490. ui->scenes->addItem(item);
  491. signal_handler_t handler = obs_source_signalhandler(source);
  492. signal_handler_connect(handler, "item_add",
  493. OBSBasic::SceneItemAdded, this);
  494. signal_handler_connect(handler, "item_remove",
  495. OBSBasic::SceneItemRemoved, this);
  496. signal_handler_connect(handler, "item_move_up",
  497. OBSBasic::SceneItemMoveUp, this);
  498. signal_handler_connect(handler, "item_move_down",
  499. OBSBasic::SceneItemMoveDown, this);
  500. signal_handler_connect(handler, "item_move_top",
  501. OBSBasic::SceneItemMoveTop, this);
  502. signal_handler_connect(handler, "item_move_bottom",
  503. OBSBasic::SceneItemMoveBottom, this);
  504. }
  505. void OBSBasic::RemoveScene(OBSSource source)
  506. {
  507. const char *name = obs_source_getname(source);
  508. QListWidgetItem *sel = ui->scenes->currentItem();
  509. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  510. Qt::MatchExactly);
  511. if (sel != nullptr) {
  512. if (items.contains(sel))
  513. ui->sources->clear();
  514. delete sel;
  515. }
  516. }
  517. void OBSBasic::AddSceneItem(OBSSceneItem item)
  518. {
  519. obs_scene_t scene = obs_sceneitem_getscene(item);
  520. obs_source_t source = obs_sceneitem_getsource(item);
  521. if (GetCurrentScene() == scene)
  522. InsertSceneItem(item);
  523. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  524. }
  525. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  526. {
  527. obs_scene_t scene = obs_sceneitem_getscene(item);
  528. if (GetCurrentScene() == scene) {
  529. for (int i = 0; i < ui->sources->count(); i++) {
  530. QListWidgetItem *listItem = ui->sources->item(i);
  531. QVariant userData = listItem->data(Qt::UserRole);
  532. if (userData.value<OBSSceneItem>() == item) {
  533. delete listItem;
  534. break;
  535. }
  536. }
  537. }
  538. obs_source_t source = obs_sceneitem_getsource(item);
  539. int scenes = sourceSceneRefs[source] - 1;
  540. sourceSceneRefs[source] = scenes;
  541. if (scenes == 0) {
  542. obs_source_remove(source);
  543. sourceSceneRefs.erase(source);
  544. }
  545. }
  546. void OBSBasic::UpdateSceneSelection(OBSSource source)
  547. {
  548. if (source) {
  549. obs_source_type type;
  550. obs_source_gettype(source, &type, NULL);
  551. obs_scene_t scene = obs_scene_fromsource(source);
  552. const char *name = obs_source_getname(source);
  553. if (!scene)
  554. return;
  555. QList<QListWidgetItem*> items =
  556. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  557. if (items.count()) {
  558. sceneChanging = true;
  559. ui->scenes->setCurrentItem(items.first());
  560. sceneChanging = false;
  561. UpdateSources(scene);
  562. }
  563. }
  564. }
  565. static void RenameListValues(QListWidget *listWidget, const QString &newName,
  566. const QString &prevName)
  567. {
  568. QList<QListWidgetItem*> items =
  569. listWidget->findItems(prevName, Qt::MatchExactly);
  570. for (int i = 0; i < items.count(); i++)
  571. items[i]->setText(newName);
  572. }
  573. void OBSBasic::RenameSources(QString newName, QString prevName)
  574. {
  575. RenameListValues(ui->scenes, newName, prevName);
  576. RenameListValues(ui->sources, newName, prevName);
  577. }
  578. void OBSBasic::MoveSceneItem(OBSSceneItem item, order_movement movement)
  579. {
  580. OBSScene scene = obs_sceneitem_getscene(item);
  581. if (scene != GetCurrentScene())
  582. return;
  583. int curRow = ui->sources->currentRow();
  584. if (curRow == -1)
  585. return;
  586. QListWidgetItem *listItem = ui->sources->takeItem(curRow);
  587. switch (movement) {
  588. case ORDER_MOVE_UP:
  589. if (curRow > 0)
  590. curRow--;
  591. break;
  592. case ORDER_MOVE_DOWN:
  593. if (curRow < ui->sources->count())
  594. curRow++;
  595. break;
  596. case ORDER_MOVE_TOP:
  597. curRow = 0;
  598. break;
  599. case ORDER_MOVE_BOTTOM:
  600. curRow = ui->sources->count();
  601. break;
  602. }
  603. ui->sources->insertItem(curRow, listItem);
  604. ui->sources->setCurrentRow(curRow);
  605. }
  606. void OBSBasic::ActivateAudioSource(OBSSource source)
  607. {
  608. VolControl *vol = new VolControl(source);
  609. volumes.push_back(vol);
  610. ui->volumeWidgets->layout()->addWidget(vol);
  611. }
  612. void OBSBasic::DeactivateAudioSource(OBSSource source)
  613. {
  614. for (size_t i = 0; i < volumes.size(); i++) {
  615. if (volumes[i]->GetSource() == source) {
  616. delete volumes[i];
  617. volumes.erase(volumes.begin() + i);
  618. break;
  619. }
  620. }
  621. }
  622. bool OBSBasic::QueryRemoveSource(obs_source_t source)
  623. {
  624. const char *name = obs_source_getname(source);
  625. QString text = QTStr("ConfirmRemove.Text");
  626. text.replace("$1", QT_UTF8(name));
  627. QMessageBox::StandardButton button;
  628. button = QMessageBox::question(this,
  629. QTStr("ConfirmRemove.Remove"), text);
  630. return button == QMessageBox::Yes;
  631. }
  632. void OBSBasic::RemoveSelectedScene()
  633. {
  634. OBSScene scene = GetCurrentScene();
  635. if (scene) {
  636. obs_source_t source = obs_scene_getsource(scene);
  637. if (QueryRemoveSource(source))
  638. obs_source_remove(source);
  639. }
  640. }
  641. void OBSBasic::RemoveSelectedSceneItem()
  642. {
  643. OBSSceneItem item = GetCurrentSceneItem();
  644. if (item) {
  645. obs_source_t source = obs_sceneitem_getsource(item);
  646. if (QueryRemoveSource(source))
  647. obs_sceneitem_remove(item);
  648. }
  649. }
  650. /* OBS Callbacks */
  651. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  652. {
  653. OBSBasic *window = static_cast<OBSBasic*>(data);
  654. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  655. QMetaObject::invokeMethod(window, "AddSceneItem",
  656. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  657. }
  658. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  659. {
  660. OBSBasic *window = static_cast<OBSBasic*>(data);
  661. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  662. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  663. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  664. }
  665. void OBSBasic::SourceAdded(void *data, calldata_t params)
  666. {
  667. OBSBasic *window = static_cast<OBSBasic*>(data);
  668. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  669. if (obs_scene_fromsource(source) != NULL)
  670. QMetaObject::invokeMethod(window,
  671. "AddScene",
  672. Q_ARG(OBSSource, OBSSource(source)));
  673. else
  674. window->sourceSceneRefs[source] = 0;
  675. }
  676. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  677. {
  678. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  679. if (obs_scene_fromsource(source) != NULL)
  680. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  681. "RemoveScene",
  682. Q_ARG(OBSSource, OBSSource(source)));
  683. }
  684. void OBSBasic::SourceActivated(void *data, calldata_t params)
  685. {
  686. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  687. uint32_t flags = obs_source_get_output_flags(source);
  688. if (flags & OBS_SOURCE_AUDIO)
  689. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  690. "ActivateAudioSource",
  691. Q_ARG(OBSSource, OBSSource(source)));
  692. }
  693. void OBSBasic::SourceDeactivated(void *data, calldata_t params)
  694. {
  695. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  696. uint32_t flags = obs_source_get_output_flags(source);
  697. if (flags & OBS_SOURCE_AUDIO)
  698. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  699. "DeactivateAudioSource",
  700. Q_ARG(OBSSource, OBSSource(source)));
  701. }
  702. void OBSBasic::SourceRenamed(void *data, calldata_t params)
  703. {
  704. const char *newName = calldata_string(params, "new_name");
  705. const char *prevName = calldata_string(params, "prev_name");
  706. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  707. "RenameSources",
  708. Q_ARG(QString, QT_UTF8(newName)),
  709. Q_ARG(QString, QT_UTF8(prevName)));
  710. }
  711. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  712. {
  713. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  714. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  715. if (channel == 0)
  716. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  717. "UpdateSceneSelection",
  718. Q_ARG(OBSSource, OBSSource(source)));
  719. }
  720. void OBSBasic::DrawBackdrop(float cx, float cy)
  721. {
  722. if (!box)
  723. return;
  724. effect_t solid = obs_get_solid_effect();
  725. eparam_t color = effect_getparambyname(solid, "color");
  726. technique_t tech = effect_gettechnique(solid, "Solid");
  727. vec4 colorVal;
  728. vec4_set(&colorVal, 0.0f, 0.0f, 0.0f, 1.0f);
  729. effect_setvec4(color, &colorVal);
  730. technique_begin(tech);
  731. technique_beginpass(tech, 0);
  732. gs_matrix_push();
  733. gs_matrix_identity();
  734. gs_matrix_scale3f(float(cx), float(cy), 1.0f);
  735. gs_load_vertexbuffer(box);
  736. gs_draw(GS_TRISTRIP, 0, 0);
  737. gs_matrix_pop();
  738. technique_endpass(tech);
  739. technique_end(tech);
  740. gs_load_vertexbuffer(nullptr);
  741. }
  742. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  743. {
  744. OBSBasic *window = static_cast<OBSBasic*>(data);
  745. obs_video_info ovi;
  746. obs_get_video_info(&ovi);
  747. window->previewCX = int(window->previewScale * float(ovi.base_width));
  748. window->previewCY = int(window->previewScale * float(ovi.base_height));
  749. gs_viewport_push();
  750. gs_projection_push();
  751. /* --------------------------------------- */
  752. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  753. -100.0f, 100.0f);
  754. gs_setviewport(window->previewX, window->previewY,
  755. window->previewCX, window->previewCY);
  756. window->DrawBackdrop(float(ovi.base_width), float(ovi.base_height));
  757. obs_render_main_view();
  758. gs_load_vertexbuffer(nullptr);
  759. /* --------------------------------------- */
  760. float right = float(window->ui->preview->width()) - window->previewX;
  761. float bottom = float(window->ui->preview->height()) - window->previewY;
  762. gs_ortho(-window->previewX, right,
  763. -window->previewY, bottom,
  764. -100.0f, 100.0f);
  765. gs_resetviewport();
  766. window->ui->preview->DrawSceneEditing();
  767. /* --------------------------------------- */
  768. gs_projection_pop();
  769. gs_viewport_pop();
  770. UNUSED_PARAMETER(cx);
  771. UNUSED_PARAMETER(cy);
  772. }
  773. void OBSBasic::SceneItemMoveUp(void *data, calldata_t params)
  774. {
  775. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  776. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  777. "MoveSceneItem",
  778. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  779. Q_ARG(order_movement, ORDER_MOVE_UP));
  780. }
  781. void OBSBasic::SceneItemMoveDown(void *data, calldata_t params)
  782. {
  783. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  784. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  785. "MoveSceneItem",
  786. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  787. Q_ARG(order_movement, ORDER_MOVE_DOWN));
  788. }
  789. void OBSBasic::SceneItemMoveTop(void *data, calldata_t params)
  790. {
  791. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  792. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  793. "MoveSceneItem",
  794. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  795. Q_ARG(order_movement, ORDER_MOVE_TOP));
  796. }
  797. void OBSBasic::SceneItemMoveBottom(void *data, calldata_t params)
  798. {
  799. OBSSceneItem item = (obs_sceneitem_t)calldata_ptr(params, "item");
  800. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  801. "MoveSceneItem",
  802. Q_ARG(OBSSceneItem, OBSSceneItem(item)),
  803. Q_ARG(order_movement, ORDER_MOVE_BOTTOM));
  804. }
  805. /* Main class functions */
  806. obs_service_t OBSBasic::GetService()
  807. {
  808. if (!service)
  809. service = obs_service_create("rtmp_common", NULL, NULL);
  810. return service;
  811. }
  812. void OBSBasic::SetService(obs_service_t newService)
  813. {
  814. if (newService) {
  815. if (service)
  816. obs_service_destroy(service);
  817. service = newService;
  818. }
  819. }
  820. bool OBSBasic::ResetVideo()
  821. {
  822. struct obs_video_info ovi;
  823. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  824. ovi.graphics_module = App()->GetRenderModule();
  825. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  826. "Video", "BaseCX");
  827. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  828. "Video", "BaseCY");
  829. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  830. "Video", "OutputCX");
  831. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  832. "Video", "OutputCY");
  833. ovi.output_format = VIDEO_FORMAT_NV12;
  834. ovi.adapter = 0;
  835. ovi.gpu_conversion = true;
  836. QTToGSWindow(ui->preview->winId(), ovi.window);
  837. //required to make opengl display stuff on osx(?)
  838. ResizePreview(ovi.base_width, ovi.base_height);
  839. QSize size = GetPixelSize(ui->preview);
  840. ovi.window_width = size.width();
  841. ovi.window_height = size.height();
  842. if (!obs_reset_video(&ovi))
  843. return false;
  844. obs_add_draw_callback(OBSBasic::RenderMain, this);
  845. return true;
  846. }
  847. bool OBSBasic::ResetAudio()
  848. {
  849. struct audio_output_info ai;
  850. ai.name = "Main Audio Track";
  851. ai.format = AUDIO_FORMAT_FLOAT;
  852. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  853. "SampleRate");
  854. const char *channelSetupStr = config_get_string(basicConfig,
  855. "Audio", "ChannelSetup");
  856. if (strcmp(channelSetupStr, "Mono") == 0)
  857. ai.speakers = SPEAKERS_MONO;
  858. else
  859. ai.speakers = SPEAKERS_STEREO;
  860. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  861. return obs_reset_audio(&ai);
  862. }
  863. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  864. const char *deviceDesc, int channel)
  865. {
  866. const char *deviceId = config_get_string(basicConfig, "Audio",
  867. deviceName);
  868. obs_source_t source;
  869. obs_data_t settings;
  870. bool same = false;
  871. source = obs_get_output_source(channel);
  872. if (source) {
  873. settings = obs_source_getsettings(source);
  874. const char *curId = obs_data_getstring(settings, "device_id");
  875. same = (strcmp(curId, deviceId) == 0);
  876. obs_data_release(settings);
  877. obs_source_release(source);
  878. }
  879. if (!same)
  880. obs_set_output_source(channel, nullptr);
  881. if (!same && strcmp(deviceId, "disabled") != 0) {
  882. obs_data_t settings = obs_data_create();
  883. obs_data_setstring(settings, "device_id", deviceId);
  884. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  885. sourceId, deviceDesc, settings);
  886. obs_data_release(settings);
  887. obs_set_output_source(channel, source);
  888. obs_source_release(source);
  889. }
  890. }
  891. void OBSBasic::ResetAudioDevices()
  892. {
  893. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1",
  894. Str("Basic.DesktopDevice1"), 1);
  895. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2",
  896. Str("Basic.DesktopDevice2"), 2);
  897. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1",
  898. Str("Basic.AuxDevice1"), 3);
  899. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2",
  900. Str("Basic.AuxDevice2"), 4);
  901. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3",
  902. Str("Basic.AuxDevice3"), 5);
  903. }
  904. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  905. {
  906. QSize targetSize;
  907. /* resize preview panel to fix to the top section of the window */
  908. targetSize = GetPixelSize(ui->preview);
  909. GetScaleAndCenterPos(int(cx), int(cy),
  910. targetSize.width() - PREVIEW_EDGE_SIZE * 2,
  911. targetSize.height() - PREVIEW_EDGE_SIZE * 2,
  912. previewX, previewY, previewScale);
  913. previewX += float(PREVIEW_EDGE_SIZE);
  914. previewY += float(PREVIEW_EDGE_SIZE);
  915. if (isVisible()) {
  916. if (resizeTimer)
  917. killTimer(resizeTimer);
  918. resizeTimer = startTimer(100);
  919. }
  920. }
  921. void OBSBasic::closeEvent(QCloseEvent *event)
  922. {
  923. QWidget::closeEvent(event);
  924. if (!event->isAccepted())
  925. return;
  926. // remove draw callback in case our drawable surfaces go away before
  927. // the destructor gets called
  928. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  929. }
  930. void OBSBasic::changeEvent(QEvent *event)
  931. {
  932. /* TODO */
  933. UNUSED_PARAMETER(event);
  934. }
  935. void OBSBasic::resizeEvent(QResizeEvent *event)
  936. {
  937. struct obs_video_info ovi;
  938. if (obs_get_video_info(&ovi))
  939. ResizePreview(ovi.base_width, ovi.base_height);
  940. UNUSED_PARAMETER(event);
  941. }
  942. void OBSBasic::timerEvent(QTimerEvent *event)
  943. {
  944. if (event->timerId() == resizeTimer) {
  945. killTimer(resizeTimer);
  946. resizeTimer = 0;
  947. QSize size = GetPixelSize(ui->preview);
  948. obs_resize(size.width(), size.height());
  949. }
  950. }
  951. void OBSBasic::on_action_New_triggered()
  952. {
  953. /* TODO */
  954. }
  955. void OBSBasic::on_action_Open_triggered()
  956. {
  957. /* TODO */
  958. }
  959. void OBSBasic::on_action_Save_triggered()
  960. {
  961. /* TODO */
  962. }
  963. void OBSBasic::on_action_Settings_triggered()
  964. {
  965. OBSBasicSettings settings(this);
  966. settings.exec();
  967. }
  968. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  969. QListWidgetItem *prev)
  970. {
  971. obs_source_t source = NULL;
  972. if (sceneChanging)
  973. return;
  974. if (current) {
  975. obs_scene_t scene;
  976. scene = current->data(Qt::UserRole).value<OBSScene>();
  977. source = obs_scene_getsource(scene);
  978. }
  979. /* TODO: allow transitions */
  980. obs_set_output_source(0, source);
  981. UNUSED_PARAMETER(prev);
  982. }
  983. void OBSBasic::EditSceneName()
  984. {
  985. ui->scenes->editItem(ui->scenes->currentItem());
  986. }
  987. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  988. {
  989. QListWidgetItem *item = ui->scenes->itemAt(pos);
  990. QMenu popup;
  991. popup.addAction(QTStr("Add"),
  992. this, SLOT(on_actionAddScene_triggered()));
  993. if (item)
  994. popup.addAction(QTStr("Remove"),
  995. this, SLOT(RemoveSelectedScene()));
  996. popup.exec(QCursor::pos());
  997. }
  998. void OBSBasic::on_actionAddScene_triggered()
  999. {
  1000. string name;
  1001. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  1002. int i = 1;
  1003. QString placeHolderText = format.arg(i);
  1004. obs_source_t source = nullptr;
  1005. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  1006. obs_source_release(source);
  1007. placeHolderText = format.arg(++i);
  1008. }
  1009. bool accepted = NameDialog::AskForName(this,
  1010. QTStr("Basic.Main.AddSceneDlg.Title"),
  1011. QTStr("Basic.Main.AddSceneDlg.Text"),
  1012. name,
  1013. placeHolderText);
  1014. if (accepted) {
  1015. if (name.empty()) {
  1016. QMessageBox::information(this,
  1017. QTStr("NoNameEntered"),
  1018. QTStr("NoNameEntered"));
  1019. on_actionAddScene_triggered();
  1020. return;
  1021. }
  1022. obs_source_t source = obs_get_source_by_name(name.c_str());
  1023. if (source) {
  1024. QMessageBox::information(this,
  1025. QTStr("NameExists.Title"),
  1026. QTStr("NameExists.Text"));
  1027. obs_source_release(source);
  1028. on_actionAddScene_triggered();
  1029. return;
  1030. }
  1031. obs_scene_t scene = obs_scene_create(name.c_str());
  1032. source = obs_scene_getsource(scene);
  1033. obs_add_source(source);
  1034. obs_scene_release(scene);
  1035. obs_set_output_source(0, source);
  1036. }
  1037. }
  1038. void OBSBasic::on_actionRemoveScene_triggered()
  1039. {
  1040. OBSScene scene = GetCurrentScene();
  1041. obs_source_t source = obs_scene_getsource(scene);
  1042. if (source && QueryRemoveSource(source))
  1043. obs_source_remove(source);
  1044. }
  1045. void OBSBasic::on_actionSceneProperties_triggered()
  1046. {
  1047. /* TODO */
  1048. }
  1049. void OBSBasic::on_actionSceneUp_triggered()
  1050. {
  1051. /* TODO */
  1052. }
  1053. void OBSBasic::on_actionSceneDown_triggered()
  1054. {
  1055. /* TODO */
  1056. }
  1057. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  1058. QListWidgetItem *prev)
  1059. {
  1060. auto select_one = [] (obs_scene_t scene, obs_sceneitem_t item,
  1061. void *param)
  1062. {
  1063. obs_sceneitem_t selectedItem =
  1064. *reinterpret_cast<OBSSceneItem*>(param);
  1065. obs_sceneitem_select(item, (selectedItem == item));
  1066. UNUSED_PARAMETER(scene);
  1067. return true;
  1068. };
  1069. if (!current)
  1070. return;
  1071. OBSSceneItem item = current->data(Qt::UserRole).value<OBSSceneItem>();
  1072. obs_scene_enum_items(GetCurrentScene(), select_one, &item);
  1073. UNUSED_PARAMETER(prev);
  1074. }
  1075. void OBSBasic::EditSceneItemName()
  1076. {
  1077. ui->sources->editItem(ui->sources->currentItem());
  1078. }
  1079. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  1080. {
  1081. QListWidgetItem *item = ui->sources->itemAt(pos);
  1082. QMenu popup;
  1083. QPointer<QMenu> addSourceMenu = CreateAddSourcePopupMenu();
  1084. if (addSourceMenu)
  1085. popup.addMenu(addSourceMenu);
  1086. if (item) {
  1087. if (addSourceMenu)
  1088. popup.addSeparator();
  1089. popup.addAction(QTStr("Rename"), this,
  1090. SLOT(EditSceneItemName()));
  1091. popup.addAction(QTStr("Remove"), this,
  1092. SLOT(on_actionRemoveSource_triggered()),
  1093. QKeySequence::Delete);
  1094. popup.addSeparator();
  1095. popup.addMenu(ui->orderMenu);
  1096. popup.addMenu(ui->transformMenu);
  1097. popup.addSeparator();
  1098. popup.addAction(QTStr("Properties"), this,
  1099. SLOT(on_actionSourceProperties_triggered()));
  1100. }
  1101. popup.exec(QCursor::pos());
  1102. }
  1103. void OBSBasic::AddSource(const char *id)
  1104. {
  1105. if (id && *id) {
  1106. OBSBasicSourceSelect sourceSelect(this, id);
  1107. sourceSelect.exec();
  1108. }
  1109. }
  1110. QMenu *OBSBasic::CreateAddSourcePopupMenu()
  1111. {
  1112. const char *type;
  1113. bool foundValues = false;
  1114. size_t idx = 0;
  1115. QMenu *popup = new QMenu(QTStr("Add"));
  1116. while (obs_enum_input_types(idx++, &type)) {
  1117. const char *name = obs_source_getdisplayname(
  1118. OBS_SOURCE_TYPE_INPUT, type);
  1119. if (strcmp(type, "scene") == 0)
  1120. continue;
  1121. QAction *popupItem = new QAction(QT_UTF8(name), this);
  1122. popupItem->setData(QT_UTF8(type));
  1123. connect(popupItem, SIGNAL(triggered(bool)),
  1124. this, SLOT(AddSourceFromAction()));
  1125. popup->addAction(popupItem);
  1126. foundValues = true;
  1127. }
  1128. if (!foundValues) {
  1129. delete popup;
  1130. popup = nullptr;
  1131. }
  1132. return popup;
  1133. }
  1134. void OBSBasic::AddSourceFromAction()
  1135. {
  1136. QAction *action = qobject_cast<QAction*>(sender());
  1137. if (!action)
  1138. return;
  1139. AddSource(QT_TO_UTF8(action->data().toString()));
  1140. }
  1141. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  1142. {
  1143. if (!GetCurrentScene()) {
  1144. // Tell the user he needs a scene first (help beginners).
  1145. QMessageBox::information(this,
  1146. QTStr("Basic.Main.AddSourceHelp.Title"),
  1147. QTStr("Basic.Main.AddSourceHelp.Text"));
  1148. return;
  1149. }
  1150. QPointer<QMenu> popup = CreateAddSourcePopupMenu();
  1151. if (popup)
  1152. popup->exec(pos);
  1153. }
  1154. void OBSBasic::on_actionAddSource_triggered()
  1155. {
  1156. AddSourcePopupMenu(QCursor::pos());
  1157. }
  1158. void OBSBasic::on_actionRemoveSource_triggered()
  1159. {
  1160. OBSSceneItem item = GetCurrentSceneItem();
  1161. obs_source_t source = obs_sceneitem_getsource(item);
  1162. if (source && QueryRemoveSource(source))
  1163. obs_sceneitem_remove(item);
  1164. }
  1165. void OBSBasic::on_actionSourceProperties_triggered()
  1166. {
  1167. OBSSceneItem item = GetCurrentSceneItem();
  1168. OBSSource source = obs_sceneitem_getsource(item);
  1169. if (source) {
  1170. delete properties;
  1171. properties = new OBSBasicProperties(this, source);
  1172. properties->Init();
  1173. }
  1174. }
  1175. void OBSBasic::on_actionSourceUp_triggered()
  1176. {
  1177. OBSSceneItem item = GetCurrentSceneItem();
  1178. obs_sceneitem_setorder(item, ORDER_MOVE_UP);
  1179. }
  1180. void OBSBasic::on_actionSourceDown_triggered()
  1181. {
  1182. OBSSceneItem item = GetCurrentSceneItem();
  1183. obs_sceneitem_setorder(item, ORDER_MOVE_DOWN);
  1184. }
  1185. void OBSBasic::on_actionMoveUp_triggered()
  1186. {
  1187. OBSSceneItem item = GetCurrentSceneItem();
  1188. obs_sceneitem_setorder(item, ORDER_MOVE_UP);
  1189. }
  1190. void OBSBasic::on_actionMoveDown_triggered()
  1191. {
  1192. OBSSceneItem item = GetCurrentSceneItem();
  1193. obs_sceneitem_setorder(item, ORDER_MOVE_DOWN);
  1194. }
  1195. void OBSBasic::on_actionMoveToTop_triggered()
  1196. {
  1197. OBSSceneItem item = GetCurrentSceneItem();
  1198. obs_sceneitem_setorder(item, ORDER_MOVE_TOP);
  1199. }
  1200. void OBSBasic::on_actionMoveToBottom_triggered()
  1201. {
  1202. OBSSceneItem item = GetCurrentSceneItem();
  1203. obs_sceneitem_setorder(item, ORDER_MOVE_BOTTOM);
  1204. }
  1205. static char *ReadLogFile(const char *log)
  1206. {
  1207. BPtr<char> logDir(os_get_config_path("obs-studio/logs"));
  1208. string path = (char*)logDir;
  1209. path += "/";
  1210. path += log;
  1211. char *file = os_quick_read_utf8_file(path.c_str());
  1212. if (!file)
  1213. blog(LOG_WARNING, "Failed to read log file %s", path.c_str());
  1214. return file;
  1215. }
  1216. void OBSBasic::UploadLog(const char *file)
  1217. {
  1218. dstr fileString = {};
  1219. stringstream ss;
  1220. string jsonData;
  1221. dstr_move_array(&fileString, ReadLogFile(file));
  1222. if (!fileString.array)
  1223. return;
  1224. if (!*fileString.array) {
  1225. dstr_free(&fileString);
  1226. return;
  1227. }
  1228. ui->menuLogFiles->setEnabled(false);
  1229. dstr_replace(&fileString, "\\", "\\\\");
  1230. dstr_replace(&fileString, "\"", "\\\"");
  1231. dstr_replace(&fileString, "\n", "\\n");
  1232. dstr_replace(&fileString, "\r", "\\r");
  1233. dstr_replace(&fileString, "\t", "\\t");
  1234. dstr_replace(&fileString, "\t", "\\t");
  1235. dstr_replace(&fileString, "/", "\\/");
  1236. ss << "{ \"public\": false, \"description\": \"OBS " <<
  1237. App()->GetVersionString() << " log file uploaded at " <<
  1238. CurrentDateTimeString() << "\", \"files\": { \"" <<
  1239. file << "\": { \"content\": \"" <<
  1240. fileString.array << "\" } } }";
  1241. jsonData = std::move(ss.str());
  1242. logUploadPostData.setData(jsonData.c_str(), (int)jsonData.size());
  1243. QUrl url("https://api.github.com/gists");
  1244. logUploadReply = networkManager.post(QNetworkRequest(url),
  1245. &logUploadPostData);
  1246. connect(logUploadReply, SIGNAL(finished()),
  1247. this, SLOT(logUploadFinished()));
  1248. connect(logUploadReply, SIGNAL(readyRead()),
  1249. this, SLOT(logUploadRead()));
  1250. dstr_free(&fileString);
  1251. }
  1252. void OBSBasic::on_actionUploadCurrentLog_triggered()
  1253. {
  1254. UploadLog(App()->GetCurrentLog());
  1255. }
  1256. void OBSBasic::on_actionUploadLastLog_triggered()
  1257. {
  1258. UploadLog(App()->GetLastLog());
  1259. }
  1260. void OBSBasic::logUploadRead()
  1261. {
  1262. logUploadReturnData.push_back(logUploadReply->readAll());
  1263. }
  1264. void OBSBasic::logUploadFinished()
  1265. {
  1266. ui->menuLogFiles->setEnabled(true);
  1267. if (logUploadReply->error()) {
  1268. QMessageBox::information(this,
  1269. QTStr("LogReturnDialog.ErrorUploadingLog"),
  1270. logUploadReply->errorString());
  1271. return;
  1272. }
  1273. const char *jsonReply = logUploadReturnData.constData();
  1274. if (!jsonReply || !*jsonReply)
  1275. return;
  1276. obs_data_t returnData = obs_data_create_from_json(jsonReply);
  1277. QString logURL = obs_data_getstring(returnData, "html_url");
  1278. obs_data_release(returnData);
  1279. OBSLogReply logDialog(this, logURL);
  1280. logDialog.exec();
  1281. }
  1282. static void RenameListItem(QListWidget *listWidget, obs_source_t source,
  1283. const string &name)
  1284. {
  1285. const char *prevName = obs_source_getname(source);
  1286. obs_source_t foundSource = obs_get_source_by_name(name.c_str());
  1287. QListWidgetItem *listItem = listWidget->currentItem();
  1288. if (foundSource || name.compare(prevName) == 0 || name.empty()) {
  1289. listItem->setText(QT_UTF8(prevName));
  1290. obs_source_release(foundSource);
  1291. } else {
  1292. listItem->setText(QT_UTF8(name.c_str()));
  1293. obs_source_setname(source, name.c_str());
  1294. }
  1295. }
  1296. void OBSBasic::SceneNameEdited(QWidget *editor,
  1297. QAbstractItemDelegate::EndEditHint endHint)
  1298. {
  1299. OBSScene scene = GetCurrentScene();
  1300. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  1301. string text = QT_TO_UTF8(edit->text().trimmed());
  1302. if (!scene)
  1303. return;
  1304. obs_source_t source = obs_scene_getsource(scene);
  1305. RenameListItem(ui->scenes, source, text);
  1306. UNUSED_PARAMETER(endHint);
  1307. }
  1308. void OBSBasic::SceneItemNameEdited(QWidget *editor,
  1309. QAbstractItemDelegate::EndEditHint endHint)
  1310. {
  1311. OBSSceneItem item = GetCurrentSceneItem();
  1312. QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
  1313. string text = QT_TO_UTF8(edit->text().trimmed());
  1314. if (!item)
  1315. return;
  1316. obs_source_t source = obs_sceneitem_getsource(item);
  1317. RenameListItem(ui->sources, source, text);
  1318. UNUSED_PARAMETER(endHint);
  1319. }
  1320. void OBSBasic::StreamingStart()
  1321. {
  1322. ui->streamButton->setText("Stop Streaming");
  1323. ui->streamButton->setEnabled(true);
  1324. }
  1325. void OBSBasic::StreamingStop(int code)
  1326. {
  1327. const char *errorMessage;
  1328. switch (code) {
  1329. case OBS_OUTPUT_BAD_PATH:
  1330. errorMessage = Str("Output.ConnectFail.BadPath");
  1331. break;
  1332. case OBS_OUTPUT_CONNECT_FAILED:
  1333. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  1334. break;
  1335. case OBS_OUTPUT_INVALID_STREAM:
  1336. errorMessage = Str("Output.ConnectFail.InvalidStream");
  1337. break;
  1338. default:
  1339. case OBS_OUTPUT_ERROR:
  1340. errorMessage = Str("Output.ConnectFail.Error");
  1341. break;
  1342. case OBS_OUTPUT_DISCONNECTED:
  1343. /* doesn't happen if output is set to reconnect. note that
  1344. * reconnects are handled in the output, not in the UI */
  1345. errorMessage = Str("Output.ConnectFail.Disconnected");
  1346. }
  1347. activeRefs--;
  1348. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  1349. ui->streamButton->setEnabled(true);
  1350. if (code != OBS_OUTPUT_SUCCESS)
  1351. QMessageBox::information(this,
  1352. QTStr("Output.ConnectFail.Title"),
  1353. QT_UTF8(errorMessage));
  1354. }
  1355. void OBSBasic::RecordingStop()
  1356. {
  1357. activeRefs--;
  1358. ui->recordButton->setText(QTStr("Basic.Main.StartRecording"));
  1359. }
  1360. void OBSBasic::SetupEncoders()
  1361. {
  1362. if (activeRefs == 0) {
  1363. obs_data_t x264Settings = obs_data_create();
  1364. obs_data_t aacSettings = obs_data_create();
  1365. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  1366. "VBitrate");
  1367. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  1368. "ABitrate");
  1369. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  1370. obs_data_setbool(x264Settings, "cbr", true);
  1371. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  1372. obs_encoder_update(x264, x264Settings);
  1373. obs_encoder_update(aac, aacSettings);
  1374. obs_data_release(x264Settings);
  1375. obs_data_release(aacSettings);
  1376. obs_encoder_set_video(x264, obs_video());
  1377. obs_encoder_set_audio(aac, obs_audio());
  1378. }
  1379. }
  1380. void OBSBasic::on_streamButton_clicked()
  1381. {
  1382. if (obs_output_active(streamOutput)) {
  1383. obs_output_stop(streamOutput);
  1384. } else {
  1385. SaveService();
  1386. SetupEncoders();
  1387. obs_output_set_video_encoder(streamOutput, x264);
  1388. obs_output_set_audio_encoder(streamOutput, aac);
  1389. obs_output_set_service(streamOutput, service);
  1390. if (obs_output_start(streamOutput)) {
  1391. activeRefs++;
  1392. ui->streamButton->setEnabled(false);
  1393. ui->streamButton->setText(
  1394. QTStr("Basic.Main.Connecting"));
  1395. }
  1396. }
  1397. }
  1398. void OBSBasic::on_recordButton_clicked()
  1399. {
  1400. if (obs_output_active(fileOutput)) {
  1401. obs_output_stop(fileOutput);
  1402. } else {
  1403. const char *path = config_get_string(basicConfig,
  1404. "SimpleOutput", "FilePath");
  1405. os_dir_t dir = path ? os_opendir(path) : nullptr;
  1406. if (!dir) {
  1407. QMessageBox::information(this,
  1408. QTStr("Output.BadPath.Title"),
  1409. QTStr("Output.BadPath.Text"));
  1410. return;
  1411. }
  1412. os_closedir(dir);
  1413. string strPath;
  1414. strPath += path;
  1415. char lastChar = strPath.back();
  1416. if (lastChar != '/' && lastChar != '\\')
  1417. strPath += "/";
  1418. strPath += GenerateTimeDateFilename("flv");
  1419. SetupEncoders();
  1420. obs_output_set_video_encoder(fileOutput, x264);
  1421. obs_output_set_audio_encoder(fileOutput, aac);
  1422. obs_data_t settings = obs_data_create();
  1423. obs_data_setstring(settings, "path", strPath.c_str());
  1424. obs_output_update(fileOutput, settings);
  1425. obs_data_release(settings);
  1426. if (obs_output_start(fileOutput)) {
  1427. activeRefs++;
  1428. ui->recordButton->setText(
  1429. QTStr("Basic.Main.StopRecording"));
  1430. }
  1431. }
  1432. }
  1433. void OBSBasic::on_settingsButton_clicked()
  1434. {
  1435. OBSBasicSettings settings(this);
  1436. settings.exec();
  1437. }
  1438. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  1439. {
  1440. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  1441. if (strcmp(val, "10") == 0) {
  1442. num = 10;
  1443. den = 1;
  1444. } else if (strcmp(val, "20") == 0) {
  1445. num = 20;
  1446. den = 1;
  1447. } else if (strcmp(val, "25") == 0) {
  1448. num = 25;
  1449. den = 1;
  1450. } else if (strcmp(val, "29.97") == 0) {
  1451. num = 30000;
  1452. den = 1001;
  1453. } else if (strcmp(val, "48") == 0) {
  1454. num = 48;
  1455. den = 1;
  1456. } else if (strcmp(val, "59.94") == 0) {
  1457. num = 60000;
  1458. den = 1001;
  1459. } else if (strcmp(val, "60") == 0) {
  1460. num = 60;
  1461. den = 1;
  1462. } else {
  1463. num = 30;
  1464. den = 1;
  1465. }
  1466. }
  1467. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  1468. {
  1469. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  1470. den = 1;
  1471. }
  1472. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  1473. {
  1474. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  1475. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  1476. }
  1477. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  1478. {
  1479. num = 1000000000;
  1480. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  1481. }
  1482. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  1483. {
  1484. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  1485. if (type == 1) //"Integer"
  1486. GetFPSInteger(num, den);
  1487. else if (type == 2) //"Fraction"
  1488. GetFPSFraction(num, den);
  1489. else if (false) //"Nanoseconds", currently not implemented
  1490. GetFPSNanoseconds(num, den);
  1491. else
  1492. GetFPSCommon(num, den);
  1493. }
  1494. config_t OBSBasic::Config() const
  1495. {
  1496. return basicConfig;
  1497. }
  1498. void OBSBasic::on_actionEditTransform_triggered()
  1499. {
  1500. delete transformWindow;
  1501. transformWindow = new OBSBasicTransform(this);
  1502. transformWindow->show();
  1503. }
  1504. void OBSBasic::on_actionResetTransform_triggered()
  1505. {
  1506. auto func = [] (obs_scene_t scene, obs_sceneitem_t item, void *param)
  1507. {
  1508. if (!obs_sceneitem_selected(item))
  1509. return true;
  1510. obs_sceneitem_info info;
  1511. vec2_set(&info.pos, 0.0f, 0.0f);
  1512. vec2_set(&info.scale, 1.0f, 1.0f);
  1513. info.rot = 0.0f;
  1514. info.alignment = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1515. info.bounds_type = OBS_BOUNDS_NONE;
  1516. info.bounds_alignment = OBS_ALIGN_CENTER;
  1517. vec2_set(&info.bounds, 0.0f, 0.0f);
  1518. obs_sceneitem_set_info(item, &info);
  1519. UNUSED_PARAMETER(scene);
  1520. UNUSED_PARAMETER(param);
  1521. return true;
  1522. };
  1523. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  1524. }
  1525. static void GetItemBox(obs_sceneitem_t item, vec3 &tl, vec3 &br)
  1526. {
  1527. matrix4 boxTransform;
  1528. obs_sceneitem_get_box_transform(item, &boxTransform);
  1529. vec3_set(&tl, M_INFINITE, M_INFINITE, 0.0f);
  1530. vec3_set(&br, -M_INFINITE, -M_INFINITE, 0.0f);
  1531. auto GetMinPos = [&] (float x, float y)
  1532. {
  1533. vec3 pos;
  1534. vec3_set(&pos, x, y, 0.0f);
  1535. vec3_transform(&pos, &pos, &boxTransform);
  1536. vec3_min(&tl, &tl, &pos);
  1537. vec3_max(&br, &br, &pos);
  1538. };
  1539. GetMinPos(0.0f, 0.0f);
  1540. GetMinPos(1.0f, 0.0f);
  1541. GetMinPos(0.0f, 1.0f);
  1542. GetMinPos(1.0f, 1.0f);
  1543. }
  1544. static vec3 GetItemTL(obs_sceneitem_t item)
  1545. {
  1546. vec3 tl, br;
  1547. GetItemBox(item, tl, br);
  1548. return tl;
  1549. }
  1550. static void SetItemTL(obs_sceneitem_t item, const vec3 &tl)
  1551. {
  1552. vec3 newTL;
  1553. vec2 pos;
  1554. obs_sceneitem_getpos(item, &pos);
  1555. newTL = GetItemTL(item);
  1556. pos.x += tl.x - newTL.x;
  1557. pos.y += tl.y - newTL.y;
  1558. obs_sceneitem_setpos(item, &pos);
  1559. }
  1560. static bool RotateSelectedSources(obs_scene_t scene, obs_sceneitem_t item,
  1561. void *param)
  1562. {
  1563. if (!obs_sceneitem_selected(item))
  1564. return true;
  1565. float rot = *reinterpret_cast<float*>(param);
  1566. vec3 tl = GetItemTL(item);
  1567. rot += obs_sceneitem_getrot(item);
  1568. if (rot >= 360.0f) rot -= 360.0f;
  1569. else if (rot <= -360.0f) rot += 360.0f;
  1570. obs_sceneitem_setrot(item, rot);
  1571. SetItemTL(item, tl);
  1572. UNUSED_PARAMETER(scene);
  1573. UNUSED_PARAMETER(param);
  1574. return true;
  1575. };
  1576. void OBSBasic::on_actionRotate90CW_triggered()
  1577. {
  1578. float f90CW = 90.0f;
  1579. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CW);
  1580. }
  1581. void OBSBasic::on_actionRotate90CCW_triggered()
  1582. {
  1583. float f90CCW = -90.0f;
  1584. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f90CCW);
  1585. }
  1586. void OBSBasic::on_actionRotate180_triggered()
  1587. {
  1588. float f180 = 180.0f;
  1589. obs_scene_enum_items(GetCurrentScene(), RotateSelectedSources, &f180);
  1590. }
  1591. static bool MultiplySelectedItemScale(obs_scene_t scene, obs_sceneitem_t item,
  1592. void *param)
  1593. {
  1594. vec2 &mul = *reinterpret_cast<vec2*>(param);
  1595. if (!obs_sceneitem_selected(item))
  1596. return true;
  1597. vec3 tl = GetItemTL(item);
  1598. vec2 scale;
  1599. obs_sceneitem_getscale(item, &scale);
  1600. vec2_mul(&scale, &scale, &mul);
  1601. obs_sceneitem_setscale(item, &scale);
  1602. SetItemTL(item, tl);
  1603. UNUSED_PARAMETER(scene);
  1604. return true;
  1605. }
  1606. void OBSBasic::on_actionFlipHorizontal_triggered()
  1607. {
  1608. vec2 scale;
  1609. vec2_set(&scale, -1.0f, 1.0f);
  1610. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  1611. &scale);
  1612. }
  1613. void OBSBasic::on_actionFlipVertical_triggered()
  1614. {
  1615. vec2 scale;
  1616. vec2_set(&scale, 1.0f, -1.0f);
  1617. obs_scene_enum_items(GetCurrentScene(), MultiplySelectedItemScale,
  1618. &scale);
  1619. }
  1620. static bool CenterAlignSelectedItems(obs_scene_t scene, obs_sceneitem_t item,
  1621. void *param)
  1622. {
  1623. obs_bounds_type boundsType = *reinterpret_cast<obs_bounds_type*>(param);
  1624. if (!obs_sceneitem_selected(item))
  1625. return true;
  1626. obs_video_info ovi;
  1627. obs_get_video_info(&ovi);
  1628. obs_sceneitem_info itemInfo;
  1629. vec2_set(&itemInfo.pos, 0.0f, 0.0f);
  1630. vec2_set(&itemInfo.scale, 1.0f, 1.0f);
  1631. itemInfo.alignment = OBS_ALIGN_LEFT | OBS_ALIGN_TOP;
  1632. itemInfo.rot = 0.0f;
  1633. vec2_set(&itemInfo.bounds,
  1634. float(ovi.base_width), float(ovi.base_height));
  1635. itemInfo.bounds_type = boundsType;
  1636. itemInfo.bounds_alignment = OBS_ALIGN_CENTER;
  1637. obs_sceneitem_set_info(item, &itemInfo);
  1638. UNUSED_PARAMETER(scene);
  1639. return true;
  1640. }
  1641. void OBSBasic::on_actionFitToScreen_triggered()
  1642. {
  1643. obs_bounds_type boundsType = OBS_BOUNDS_SCALE_INNER;
  1644. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  1645. &boundsType);
  1646. }
  1647. void OBSBasic::on_actionStretchToScreen_triggered()
  1648. {
  1649. obs_bounds_type boundsType = OBS_BOUNDS_STRETCH;
  1650. obs_scene_enum_items(GetCurrentScene(), CenterAlignSelectedItems,
  1651. &boundsType);
  1652. }
  1653. void OBSBasic::on_actionCenterToScreen_triggered()
  1654. {
  1655. auto func = [] (obs_scene_t scene, obs_sceneitem_t item, void *param)
  1656. {
  1657. vec3 tl, br, itemCenter, screenCenter, offset;
  1658. obs_video_info ovi;
  1659. if (!obs_sceneitem_selected(item))
  1660. return true;
  1661. obs_get_video_info(&ovi);
  1662. vec3_set(&screenCenter, float(ovi.base_width),
  1663. float(ovi.base_height), 0.0f);
  1664. vec3_mulf(&screenCenter, &screenCenter, 0.5f);
  1665. GetItemBox(item, tl, br);
  1666. vec3_sub(&itemCenter, &br, &tl);
  1667. vec3_mulf(&itemCenter, &itemCenter, 0.5f);
  1668. vec3_add(&itemCenter, &itemCenter, &tl);
  1669. vec3_sub(&offset, &screenCenter, &itemCenter);
  1670. vec3_add(&tl, &tl, &offset);
  1671. SetItemTL(item, tl);
  1672. UNUSED_PARAMETER(scene);
  1673. UNUSED_PARAMETER(param);
  1674. return true;
  1675. };
  1676. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  1677. }