window-basic-main.cpp 65 KB

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