window-basic-main.cpp 65 KB

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