window-basic-main.cpp 45 KB

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