window-basic-main.cpp 51 KB

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