window-basic-main.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 by Hugh Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include <obs.hpp>
  16. #include <QMessageBox>
  17. #include <QShowEvent>
  18. #include <QFileDialog>
  19. #include <util/util.hpp>
  20. #include <util/platform.h>
  21. #include "obs-app.hpp"
  22. #include "platform.hpp"
  23. #include "window-basic-settings.hpp"
  24. #include "window-namedialog.hpp"
  25. #include "window-basic-source-select.hpp"
  26. #include "window-basic-main.hpp"
  27. #include "window-basic-properties.hpp"
  28. #include "qt-wrappers.hpp"
  29. #include "display-helpers.hpp"
  30. #include "volume-control.hpp"
  31. #include "ui_OBSBasic.h"
  32. #include <sstream>
  33. #include <QScreen>
  34. #include <QWindow>
  35. using namespace std;
  36. Q_DECLARE_METATYPE(OBSScene);
  37. Q_DECLARE_METATYPE(OBSSceneItem);
  38. OBSBasic::OBSBasic(QWidget *parent)
  39. : OBSMainWindow (parent),
  40. properties (nullptr),
  41. streamOutput (nullptr),
  42. service (nullptr),
  43. aac (nullptr),
  44. x264 (nullptr),
  45. sceneChanging (false),
  46. resizeTimer (0),
  47. ui (new Ui::OBSBasic)
  48. {
  49. ui->setupUi(this);
  50. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  51. struct obs_video_info ovi;
  52. if (obs_get_video_info(&ovi))
  53. ResizePreview(ovi.base_width, ovi.base_height);
  54. });
  55. }
  56. static void SaveAudioDevice(const char *name, int channel, obs_data_t parent)
  57. {
  58. obs_source_t source = obs_get_output_source(channel);
  59. if (!source)
  60. return;
  61. obs_data_t data = obs_save_source(source);
  62. obs_data_setobj(parent, name, data);
  63. obs_data_release(data);
  64. obs_source_release(source);
  65. }
  66. static obs_data_t GenerateSaveData()
  67. {
  68. obs_data_t saveData = obs_data_create();
  69. obs_data_array_t sourcesArray = obs_save_sources();
  70. obs_source_t currentScene = obs_get_output_source(0);
  71. const char *sceneName = obs_source_getname(currentScene);
  72. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  73. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  74. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  75. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  76. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  77. obs_data_setstring(saveData, "current_scene", sceneName);
  78. obs_data_setarray(saveData, "sources", sourcesArray);
  79. obs_data_array_release(sourcesArray);
  80. obs_source_release(currentScene);
  81. return saveData;
  82. }
  83. void OBSBasic::ClearVolumeControls()
  84. {
  85. VolControl *control;
  86. for (size_t i = 0; i < volumes.size(); i++) {
  87. control = volumes[i];
  88. delete control;
  89. }
  90. volumes.clear();
  91. }
  92. void OBSBasic::Save(const char *file)
  93. {
  94. obs_data_t saveData = GenerateSaveData();
  95. const char *jsonData = obs_data_getjson(saveData);
  96. /* TODO maybe a message box here? */
  97. if (!os_quick_write_utf8_file(file, jsonData, strlen(jsonData), false))
  98. blog(LOG_ERROR, "Could not save scene data to %s", file);
  99. obs_data_release(saveData);
  100. }
  101. static void LoadAudioDevice(const char *name, int channel, obs_data_t parent)
  102. {
  103. obs_data_t data = obs_data_getobj(parent, name);
  104. if (!data)
  105. return;
  106. obs_source_t source = obs_load_source(data);
  107. if (source) {
  108. obs_set_output_source(channel, source);
  109. obs_source_release(source);
  110. }
  111. obs_data_release(data);
  112. }
  113. void OBSBasic::CreateDefaultScene()
  114. {
  115. obs_scene_t scene = obs_scene_create(Str("Studio.Basic.Scene"));
  116. obs_source_t source = obs_scene_getsource(scene);
  117. obs_add_source(source);
  118. #ifdef __APPLE__
  119. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, "display_capture",
  120. Str("Studio.Basic.DisplayCapture"), NULL);
  121. if (source) {
  122. obs_scene_add(scene, source);
  123. obs_add_source(source);
  124. obs_source_release(source);
  125. }
  126. #endif
  127. obs_set_output_source(0, obs_scene_getsource(scene));
  128. obs_scene_release(scene);
  129. }
  130. void OBSBasic::Load(const char *file)
  131. {
  132. if (!file) {
  133. blog(LOG_ERROR, "Could not find file %s", file);
  134. return;
  135. }
  136. BPtr<char> jsonData = os_quick_read_utf8_file(file);
  137. if (!jsonData) {
  138. CreateDefaultScene();
  139. return;
  140. }
  141. obs_data_t data = obs_data_create_from_json(jsonData);
  142. obs_data_array_t sources = obs_data_getarray(data, "sources");
  143. const char *sceneName = obs_data_getstring(data, "current_scene");
  144. obs_source_t curScene;
  145. LoadAudioDevice(DESKTOP_AUDIO_1, 1, data);
  146. LoadAudioDevice(DESKTOP_AUDIO_2, 2, data);
  147. LoadAudioDevice(AUX_AUDIO_1, 3, data);
  148. LoadAudioDevice(AUX_AUDIO_2, 4, data);
  149. LoadAudioDevice(AUX_AUDIO_3, 5, data);
  150. obs_load_sources(sources);
  151. curScene = obs_get_source_by_name(sceneName);
  152. obs_set_output_source(0, curScene);
  153. obs_source_release(curScene);
  154. obs_data_array_release(sources);
  155. obs_data_release(data);
  156. }
  157. static inline bool HasAudioDevices(const char *source_id)
  158. {
  159. const char *output_id = source_id;
  160. obs_properties_t props = obs_get_source_properties(
  161. OBS_SOURCE_TYPE_INPUT, output_id, App()->GetLocale());
  162. size_t count = 0;
  163. if (!props)
  164. return false;
  165. obs_property_t devices = obs_properties_get(props, "device_id");
  166. if (devices)
  167. count = obs_property_list_item_count(devices);
  168. obs_properties_destroy(props);
  169. return count != 0;
  170. }
  171. static void OBSStartStreaming(void *data, calldata_t params)
  172. {
  173. UNUSED_PARAMETER(params);
  174. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  175. "StreamingStart");
  176. }
  177. static void OBSStopStreaming(void *data, calldata_t params)
  178. {
  179. int code = (int)calldata_int(params, "errorcode");
  180. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  181. "StreamingStop", Q_ARG(int, code));
  182. }
  183. #define SERVICE_PATH "obs-studio/basic/service.json"
  184. void OBSBasic::SaveService()
  185. {
  186. if (!service)
  187. return;
  188. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  189. if (!serviceJsonPath)
  190. return;
  191. obs_data_t data = obs_data_create();
  192. obs_data_t settings = obs_service_get_settings(service);
  193. obs_data_setstring(data, "type", obs_service_gettype(service));
  194. obs_data_setobj(data, "settings", settings);
  195. const char *json = obs_data_getjson(data);
  196. os_quick_write_utf8_file(serviceJsonPath, json, strlen(json), false);
  197. obs_data_release(settings);
  198. obs_data_release(data);
  199. }
  200. bool OBSBasic::LoadService()
  201. {
  202. const char *type;
  203. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  204. if (!serviceJsonPath)
  205. return false;
  206. BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
  207. if (!jsonText)
  208. return false;
  209. obs_data_t data = obs_data_create_from_json(jsonText);
  210. obs_data_set_default_string(data, "type", "rtmp_common");
  211. type = obs_data_getstring(data, "type");
  212. obs_data_t settings = obs_data_getobj(data, "settings");
  213. service = obs_service_create(type, "default", settings);
  214. obs_data_release(settings);
  215. obs_data_release(data);
  216. return !!service;
  217. }
  218. bool OBSBasic::InitOutputs()
  219. {
  220. streamOutput = obs_output_create("rtmp_output", "default", nullptr);
  221. if (!streamOutput)
  222. return false;
  223. signal_handler_connect(obs_output_signalhandler(streamOutput),
  224. "start", OBSStartStreaming, this);
  225. signal_handler_connect(obs_output_signalhandler(streamOutput),
  226. "stop", OBSStopStreaming, this);
  227. return true;
  228. }
  229. bool OBSBasic::InitEncoders()
  230. {
  231. aac = obs_audio_encoder_create("ffmpeg_aac", "aac", nullptr);
  232. if (!aac)
  233. return false;
  234. x264 = obs_video_encoder_create("obs_x264", "h264", nullptr);
  235. if (!x264)
  236. return false;
  237. return true;
  238. }
  239. bool OBSBasic::InitService()
  240. {
  241. if (LoadService())
  242. return true;
  243. service = obs_service_create("rtmp_common", nullptr, nullptr);
  244. if (!service)
  245. return false;
  246. return true;
  247. }
  248. bool OBSBasic::InitBasicConfigDefaults()
  249. {
  250. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  251. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  252. config_set_default_int(basicConfig, "Window", "PosX", -1);
  253. config_set_default_int(basicConfig, "Window", "PosY", -1);
  254. config_set_default_int(basicConfig, "Window", "SizeX", -1);
  255. config_set_default_int(basicConfig, "Window", "SizeY", -1);
  256. vector<MonitorInfo> monitors;
  257. GetMonitors(monitors);
  258. if (!monitors.size()) {
  259. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  260. "technically shouldn't be possible.");
  261. return false;
  262. }
  263. uint32_t cx = monitors[0].cx;
  264. uint32_t cy = monitors[0].cy;
  265. /* TODO: temporary */
  266. config_set_default_string(basicConfig, "SimpleOutput", "path", "");
  267. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  268. 2500);
  269. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 128);
  270. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  271. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  272. cx = cx * 10 / 15;
  273. cy = cy * 10 / 15;
  274. config_set_default_uint (basicConfig, "Video", "OutputCX", cx);
  275. config_set_default_uint (basicConfig, "Video", "OutputCY", cy);
  276. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  277. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  278. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  279. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  280. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  281. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  282. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  283. "Stereo");
  284. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  285. config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
  286. hasDesktopAudio ? "default" : "disabled");
  287. config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
  288. "disabled");
  289. config_set_default_string(basicConfig, "Audio", "AuxDevice1",
  290. hasInputAudio ? "default" : "disabled");
  291. config_set_default_string(basicConfig, "Audio", "AuxDevice2",
  292. "disabled");
  293. config_set_default_string(basicConfig, "Audio", "AuxDevice3",
  294. "disabled");
  295. return true;
  296. }
  297. bool OBSBasic::InitBasicConfig()
  298. {
  299. BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini"));
  300. int errorcode = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  301. if (errorcode != CONFIG_SUCCESS) {
  302. OBSErrorBox(NULL, "Failed to open basic.ini: %d", errorcode);
  303. return false;
  304. }
  305. return InitBasicConfigDefaults();
  306. }
  307. void OBSBasic::InitOBSCallbacks()
  308. {
  309. signal_handler_connect(obs_signalhandler(), "source_add",
  310. OBSBasic::SourceAdded, this);
  311. signal_handler_connect(obs_signalhandler(), "source_remove",
  312. OBSBasic::SourceRemoved, this);
  313. signal_handler_connect(obs_signalhandler(), "channel_change",
  314. OBSBasic::ChannelChanged, this);
  315. signal_handler_connect(obs_signalhandler(), "source_activate",
  316. OBSBasic::SourceActivated, this);
  317. signal_handler_connect(obs_signalhandler(), "source_deactivate",
  318. OBSBasic::SourceDeactivated, this);
  319. }
  320. void OBSBasic::OBSInit()
  321. {
  322. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  323. /* make sure it's fully displayed before doing any initialization */
  324. show();
  325. App()->processEvents();
  326. if (!obs_startup())
  327. throw "Failed to initialize libobs";
  328. if (!InitBasicConfig())
  329. throw "Failed to load basic.ini";
  330. if (!ResetVideo())
  331. throw "Failed to initialize video";
  332. if (!ResetAudio())
  333. throw "Failed to initialize audio";
  334. InitOBSCallbacks();
  335. /* TODO: this is a test, all modules will be searched for and loaded
  336. * automatically later */
  337. obs_load_module("test-input");
  338. obs_load_module("obs-ffmpeg");
  339. obs_load_module("obs-x264");
  340. obs_load_module("obs-outputs");
  341. obs_load_module("rtmp-services");
  342. #ifdef __APPLE__
  343. obs_load_module("mac-avcapture");
  344. obs_load_module("mac-capture");
  345. #elif _WIN32
  346. obs_load_module("win-wasapi");
  347. obs_load_module("win-capture");
  348. #else
  349. obs_load_module("linux-xshm");
  350. obs_load_module("linux-pulseaudio");
  351. #endif
  352. if (!InitOutputs())
  353. throw "Failed to initialize outputs";
  354. if (!InitEncoders())
  355. throw "Failed to initialize encoders";
  356. if (!InitService())
  357. throw "Failed to initialize service";
  358. Load(savePath);
  359. ResetAudioDevices();
  360. }
  361. OBSBasic::~OBSBasic()
  362. {
  363. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  364. SaveService();
  365. Save(savePath);
  366. if (properties)
  367. delete properties;
  368. /* free the lists before shutting down to remove the scene/item
  369. * references */
  370. ClearVolumeControls();
  371. ui->sources->clear();
  372. ui->scenes->clear();
  373. obs_shutdown();
  374. }
  375. OBSScene OBSBasic::GetCurrentScene()
  376. {
  377. QListWidgetItem *item = ui->scenes->currentItem();
  378. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  379. }
  380. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  381. {
  382. QListWidgetItem *item = ui->sources->currentItem();
  383. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  384. }
  385. void OBSBasic::UpdateSources(OBSScene scene)
  386. {
  387. ui->sources->clear();
  388. obs_scene_enum_items(scene,
  389. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  390. {
  391. OBSBasic *window = static_cast<OBSBasic*>(p);
  392. window->InsertSceneItem(item);
  393. UNUSED_PARAMETER(scene);
  394. return true;
  395. }, this);
  396. }
  397. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  398. {
  399. obs_source_t source = obs_sceneitem_getsource(item);
  400. const char *name = obs_source_getname(source);
  401. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  402. listItem->setData(Qt::UserRole,
  403. QVariant::fromValue(OBSSceneItem(item)));
  404. ui->sources->insertItem(0, listItem);
  405. }
  406. /* Qt callbacks for invokeMethod */
  407. void OBSBasic::AddScene(OBSSource source)
  408. {
  409. const char *name = obs_source_getname(source);
  410. obs_scene_t scene = obs_scene_fromsource(source);
  411. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  412. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  413. ui->scenes->addItem(item);
  414. signal_handler_t handler = obs_source_signalhandler(source);
  415. signal_handler_connect(handler, "item_add",
  416. OBSBasic::SceneItemAdded, this);
  417. signal_handler_connect(handler, "item_remove",
  418. OBSBasic::SceneItemRemoved, this);
  419. }
  420. void OBSBasic::RemoveScene(OBSSource source)
  421. {
  422. const char *name = obs_source_getname(source);
  423. QListWidgetItem *sel = ui->scenes->currentItem();
  424. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  425. Qt::MatchExactly);
  426. if (sel != nullptr) {
  427. if (items.contains(sel))
  428. ui->sources->clear();
  429. delete sel;
  430. }
  431. }
  432. void OBSBasic::AddSceneItem(OBSSceneItem item)
  433. {
  434. obs_scene_t scene = obs_sceneitem_getscene(item);
  435. obs_source_t source = obs_sceneitem_getsource(item);
  436. if (GetCurrentScene() == scene)
  437. InsertSceneItem(item);
  438. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  439. }
  440. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  441. {
  442. obs_scene_t scene = obs_sceneitem_getscene(item);
  443. if (GetCurrentScene() == scene) {
  444. for (int i = 0; i < ui->sources->count(); i++) {
  445. QListWidgetItem *listItem = ui->sources->item(i);
  446. QVariant userData = listItem->data(Qt::UserRole);
  447. if (userData.value<OBSSceneItem>() == item) {
  448. delete listItem;
  449. break;
  450. }
  451. }
  452. }
  453. obs_source_t source = obs_sceneitem_getsource(item);
  454. int scenes = sourceSceneRefs[source] - 1;
  455. sourceSceneRefs[source] = scenes;
  456. if (scenes == 0) {
  457. obs_source_remove(source);
  458. sourceSceneRefs.erase(source);
  459. }
  460. }
  461. void OBSBasic::UpdateSceneSelection(OBSSource source)
  462. {
  463. if (source) {
  464. obs_source_type type;
  465. obs_source_gettype(source, &type, NULL);
  466. obs_scene_t scene = obs_scene_fromsource(source);
  467. const char *name = obs_source_getname(source);
  468. if (!scene)
  469. return;
  470. QList<QListWidgetItem*> items =
  471. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  472. if (items.count()) {
  473. sceneChanging = true;
  474. ui->scenes->setCurrentItem(items.first());
  475. sceneChanging = false;
  476. UpdateSources(scene);
  477. }
  478. }
  479. }
  480. void OBSBasic::ActivateAudioSource(OBSSource source)
  481. {
  482. VolControl *vol = new VolControl(source);
  483. volumes.push_back(vol);
  484. ui->volumeWidgets->layout()->addWidget(vol);
  485. }
  486. void OBSBasic::DeactivateAudioSource(OBSSource source)
  487. {
  488. for (size_t i = 0; i < volumes.size(); i++) {
  489. if (volumes[i]->GetSource() == source) {
  490. delete volumes[i];
  491. volumes.erase(volumes.begin() + i);
  492. break;
  493. }
  494. }
  495. }
  496. /* OBS Callbacks */
  497. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  498. {
  499. OBSBasic *window = static_cast<OBSBasic*>(data);
  500. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  501. QMetaObject::invokeMethod(window, "AddSceneItem",
  502. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  503. }
  504. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  505. {
  506. OBSBasic *window = static_cast<OBSBasic*>(data);
  507. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  508. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  509. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  510. }
  511. void OBSBasic::SourceAdded(void *data, calldata_t params)
  512. {
  513. OBSBasic *window = static_cast<OBSBasic*>(data);
  514. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  515. if (obs_scene_fromsource(source) != NULL)
  516. QMetaObject::invokeMethod(window,
  517. "AddScene",
  518. Q_ARG(OBSSource, OBSSource(source)));
  519. else
  520. window->sourceSceneRefs[source] = 0;
  521. }
  522. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  523. {
  524. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  525. if (obs_scene_fromsource(source) != NULL)
  526. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  527. "RemoveScene",
  528. Q_ARG(OBSSource, OBSSource(source)));
  529. }
  530. void OBSBasic::SourceActivated(void *data, calldata_t params)
  531. {
  532. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  533. uint32_t flags = obs_source_get_output_flags(source);
  534. if (flags & OBS_SOURCE_AUDIO)
  535. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  536. "ActivateAudioSource",
  537. Q_ARG(OBSSource, OBSSource(source)));
  538. }
  539. void OBSBasic::SourceDeactivated(void *data, calldata_t params)
  540. {
  541. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  542. uint32_t flags = obs_source_get_output_flags(source);
  543. if (flags & OBS_SOURCE_AUDIO)
  544. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  545. "DeactivateAudioSource",
  546. Q_ARG(OBSSource, OBSSource(source)));
  547. }
  548. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  549. {
  550. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  551. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  552. if (channel == 0)
  553. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  554. "UpdateSceneSelection",
  555. Q_ARG(OBSSource, OBSSource(source)));
  556. }
  557. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  558. {
  559. OBSBasic *window = static_cast<OBSBasic*>(data);
  560. obs_video_info ovi;
  561. int newCX, newCY;
  562. obs_get_video_info(&ovi);
  563. newCX = int(window->previewScale * float(ovi.base_width));
  564. newCY = int(window->previewScale * float(ovi.base_height));
  565. gs_viewport_push();
  566. gs_projection_push();
  567. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  568. -100.0f, 100.0f);
  569. gs_setviewport(window->previewX, window->previewY, newCX, newCY);
  570. obs_render_main_view();
  571. gs_projection_pop();
  572. gs_viewport_pop();
  573. UNUSED_PARAMETER(cx);
  574. UNUSED_PARAMETER(cy);
  575. }
  576. /* Main class functions */
  577. obs_service_t OBSBasic::GetService()
  578. {
  579. if (!service)
  580. service = obs_service_create("rtmp_common", NULL, NULL);
  581. return service;
  582. }
  583. void OBSBasic::SetService(obs_service_t newService)
  584. {
  585. if (newService) {
  586. if (service)
  587. obs_service_destroy(service);
  588. service = newService;
  589. }
  590. }
  591. bool OBSBasic::ResetVideo()
  592. {
  593. struct obs_video_info ovi;
  594. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  595. ovi.graphics_module = App()->GetRenderModule();
  596. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  597. "Video", "BaseCX");
  598. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  599. "Video", "BaseCY");
  600. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  601. "Video", "OutputCX");
  602. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  603. "Video", "OutputCY");
  604. ovi.output_format = VIDEO_FORMAT_NV12;
  605. ovi.adapter = 0;
  606. ovi.gpu_conversion = true;
  607. QTToGSWindow(ui->preview->winId(), ovi.window);
  608. //required to make opengl display stuff on osx(?)
  609. ResizePreview(ovi.base_width, ovi.base_height);
  610. QSize size = GetPixelSize(ui->preview);
  611. ovi.window_width = size.width();
  612. ovi.window_height = size.height();
  613. if (!obs_reset_video(&ovi))
  614. return false;
  615. obs_add_draw_callback(OBSBasic::RenderMain, this);
  616. return true;
  617. }
  618. bool OBSBasic::ResetAudio()
  619. {
  620. struct audio_output_info ai;
  621. ai.name = "Main Audio Track";
  622. ai.format = AUDIO_FORMAT_FLOAT;
  623. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  624. "SampleRate");
  625. const char *channelSetupStr = config_get_string(basicConfig,
  626. "Audio", "ChannelSetup");
  627. if (strcmp(channelSetupStr, "Mono") == 0)
  628. ai.speakers = SPEAKERS_MONO;
  629. else
  630. ai.speakers = SPEAKERS_STEREO;
  631. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  632. return obs_reset_audio(&ai);
  633. }
  634. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  635. int channel)
  636. {
  637. const char *deviceId = config_get_string(basicConfig, "Audio",
  638. deviceName);
  639. obs_source_t source;
  640. obs_data_t settings;
  641. bool same = false;
  642. source = obs_get_output_source(channel);
  643. if (source) {
  644. settings = obs_source_getsettings(source);
  645. const char *curId = obs_data_getstring(settings, "device_id");
  646. same = (strcmp(curId, deviceId) == 0);
  647. obs_data_release(settings);
  648. obs_source_release(source);
  649. }
  650. if (!same)
  651. obs_set_output_source(channel, nullptr);
  652. if (!same && strcmp(deviceId, "disabled") != 0) {
  653. obs_data_t settings = obs_data_create();
  654. obs_data_setstring(settings, "device_id", deviceId);
  655. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  656. sourceId, Str(deviceName), settings);
  657. obs_data_release(settings);
  658. obs_set_output_source(channel, source);
  659. obs_source_release(source);
  660. }
  661. }
  662. void OBSBasic::ResetAudioDevices()
  663. {
  664. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1", 1);
  665. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2", 2);
  666. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1", 3);
  667. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2", 4);
  668. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3", 5);
  669. }
  670. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  671. {
  672. QSize targetSize;
  673. /* resize preview panel to fix to the top section of the window */
  674. targetSize = GetPixelSize(ui->preview);
  675. GetScaleAndCenterPos(int(cx), int(cy),
  676. targetSize.width(), targetSize.height(),
  677. previewX, previewY, previewScale);
  678. if (isVisible()) {
  679. if (resizeTimer)
  680. killTimer(resizeTimer);
  681. resizeTimer = startTimer(100);
  682. }
  683. }
  684. void OBSBasic::closeEvent(QCloseEvent *event)
  685. {
  686. QWidget::closeEvent(event);
  687. if (!event->isAccepted())
  688. return;
  689. // remove draw callback in case our drawable surfaces go away before
  690. // the destructor gets called
  691. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  692. }
  693. void OBSBasic::changeEvent(QEvent *event)
  694. {
  695. /* TODO */
  696. UNUSED_PARAMETER(event);
  697. }
  698. void OBSBasic::resizeEvent(QResizeEvent *event)
  699. {
  700. struct obs_video_info ovi;
  701. if (obs_get_video_info(&ovi))
  702. ResizePreview(ovi.base_width, ovi.base_height);
  703. UNUSED_PARAMETER(event);
  704. }
  705. void OBSBasic::timerEvent(QTimerEvent *event)
  706. {
  707. if (event->timerId() == resizeTimer) {
  708. killTimer(resizeTimer);
  709. resizeTimer = 0;
  710. QSize size = GetPixelSize(ui->preview);
  711. obs_resize(size.width(), size.height());
  712. }
  713. }
  714. void OBSBasic::on_action_New_triggered()
  715. {
  716. /* TODO */
  717. }
  718. void OBSBasic::on_action_Open_triggered()
  719. {
  720. /* TODO */
  721. }
  722. void OBSBasic::on_action_Save_triggered()
  723. {
  724. /* TODO */
  725. }
  726. void OBSBasic::on_action_Settings_triggered()
  727. {
  728. OBSBasicSettings settings(this);
  729. settings.exec();
  730. }
  731. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  732. QListWidgetItem *prev)
  733. {
  734. obs_source_t source = NULL;
  735. if (sceneChanging)
  736. return;
  737. if (current) {
  738. obs_scene_t scene;
  739. scene = current->data(Qt::UserRole).value<OBSScene>();
  740. source = obs_scene_getsource(scene);
  741. }
  742. /* TODO: allow transitions */
  743. obs_set_output_source(0, source);
  744. UNUSED_PARAMETER(prev);
  745. }
  746. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  747. {
  748. /* TODO */
  749. UNUSED_PARAMETER(pos);
  750. }
  751. void OBSBasic::on_actionAddScene_triggered()
  752. {
  753. string name;
  754. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  755. QString placeHolderText = format.arg(ui->scenes->count() + 1);
  756. bool accepted = NameDialog::AskForName(this,
  757. QTStr("MainWindow.AddSceneDlg.Title"),
  758. QTStr("MainWindow.AddSceneDlg.Text"),
  759. name,
  760. placeHolderText);
  761. if (accepted) {
  762. if (name.empty()) {
  763. QMessageBox::information(this,
  764. QTStr("MainWindow.NoNameEntered"),
  765. QTStr("MainWindow.NoNameEntered"));
  766. on_actionAddScene_triggered();
  767. return;
  768. }
  769. obs_source_t source = obs_get_source_by_name(name.c_str());
  770. if (source) {
  771. QMessageBox::information(this,
  772. QTStr("MainWindow.NameExists.Title"),
  773. QTStr("MainWindow.NameExists.Text"));
  774. obs_source_release(source);
  775. on_actionAddScene_triggered();
  776. return;
  777. }
  778. obs_scene_t scene = obs_scene_create(name.c_str());
  779. source = obs_scene_getsource(scene);
  780. obs_add_source(source);
  781. obs_scene_release(scene);
  782. obs_set_output_source(0, source);
  783. }
  784. }
  785. void OBSBasic::on_actionRemoveScene_triggered()
  786. {
  787. QListWidgetItem *item = ui->scenes->currentItem();
  788. if (!item)
  789. return;
  790. QVariant userData = item->data(Qt::UserRole);
  791. obs_scene_t scene = userData.value<OBSScene>();
  792. obs_source_t source = obs_scene_getsource(scene);
  793. obs_source_remove(source);
  794. }
  795. void OBSBasic::on_actionSceneProperties_triggered()
  796. {
  797. /* TODO */
  798. }
  799. void OBSBasic::on_actionSceneUp_triggered()
  800. {
  801. /* TODO */
  802. }
  803. void OBSBasic::on_actionSceneDown_triggered()
  804. {
  805. /* TODO */
  806. }
  807. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  808. QListWidgetItem *prev)
  809. {
  810. /* TODO */
  811. UNUSED_PARAMETER(current);
  812. UNUSED_PARAMETER(prev);
  813. }
  814. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  815. {
  816. /* TODO */
  817. UNUSED_PARAMETER(pos);
  818. }
  819. void OBSBasic::AddSource(const char *id)
  820. {
  821. OBSBasicSourceSelect sourceSelect(this, id);
  822. sourceSelect.exec();
  823. }
  824. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  825. {
  826. const char *type;
  827. bool foundValues = false;
  828. size_t idx = 0;
  829. if (!GetCurrentScene()) {
  830. // Tell the user he needs a scene first (help beginners).
  831. QMessageBox::information(this,
  832. QTStr("Basic.Main.AddSourceHelp.Title"),
  833. QTStr("Basic.Main.AddSourceHelp.Text"));
  834. return;
  835. }
  836. QMenu popup;
  837. while (obs_enum_input_types(idx++, &type)) {
  838. const char *name = obs_source_getdisplayname(
  839. OBS_SOURCE_TYPE_INPUT,
  840. type, App()->GetLocale());
  841. if (strcmp(type, "scene") == 0)
  842. continue;
  843. QAction *popupItem = new QAction(QT_UTF8(name), this);
  844. popupItem->setData(QT_UTF8(type));
  845. popup.addAction(popupItem);
  846. foundValues = true;
  847. }
  848. if (foundValues) {
  849. QAction *ret = popup.exec(pos);
  850. if (ret)
  851. AddSource(ret->data().toString().toUtf8());
  852. }
  853. }
  854. void OBSBasic::on_actionAddSource_triggered()
  855. {
  856. AddSourcePopupMenu(QCursor::pos());
  857. }
  858. void OBSBasic::on_actionRemoveSource_triggered()
  859. {
  860. OBSSceneItem item = GetCurrentSceneItem();
  861. if (item)
  862. obs_sceneitem_remove(item);
  863. }
  864. void OBSBasic::on_actionSourceProperties_triggered()
  865. {
  866. OBSSceneItem item = GetCurrentSceneItem();
  867. OBSSource source = obs_sceneitem_getsource(item);
  868. if (source) {
  869. delete properties;
  870. properties = new OBSBasicProperties(this, source);
  871. properties->Init();
  872. }
  873. }
  874. void OBSBasic::on_actionSourceUp_triggered()
  875. {
  876. }
  877. void OBSBasic::on_actionSourceDown_triggered()
  878. {
  879. }
  880. void OBSBasic::StreamingStart()
  881. {
  882. ui->streamButton->setText("Stop Streaming");
  883. }
  884. void OBSBasic::StreamingStop(int errorcode)
  885. {
  886. UNUSED_PARAMETER(errorcode);
  887. ui->streamButton->setText("Start Streaming");
  888. }
  889. void OBSBasic::on_streamButton_clicked()
  890. {
  891. if (obs_output_active(streamOutput)) {
  892. obs_output_stop(streamOutput);
  893. } else {
  894. obs_data_t x264Settings = obs_data_create();
  895. obs_data_t aacSettings = obs_data_create();
  896. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  897. "VBitrate");
  898. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  899. "ABitrate");
  900. SaveService();
  901. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  902. obs_data_setbool(x264Settings, "cbr", true);
  903. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  904. obs_encoder_update(x264, x264Settings);
  905. obs_encoder_update(aac, aacSettings);
  906. obs_data_release(x264Settings);
  907. obs_data_release(aacSettings);
  908. obs_encoder_set_video(x264, obs_video());
  909. obs_encoder_set_audio(aac, obs_audio());
  910. obs_output_set_video_encoder(streamOutput, x264);
  911. obs_output_set_audio_encoder(streamOutput, aac);
  912. obs_output_set_service(streamOutput, service);
  913. obs_output_start(streamOutput);
  914. }
  915. }
  916. void OBSBasic::on_settingsButton_clicked()
  917. {
  918. OBSBasicSettings settings(this);
  919. settings.exec();
  920. }
  921. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  922. {
  923. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  924. if (strcmp(val, "10") == 0) {
  925. num = 10;
  926. den = 1;
  927. } else if (strcmp(val, "20") == 0) {
  928. num = 20;
  929. den = 1;
  930. } else if (strcmp(val, "25") == 0) {
  931. num = 25;
  932. den = 1;
  933. } else if (strcmp(val, "29.97") == 0) {
  934. num = 30000;
  935. den = 1001;
  936. } else if (strcmp(val, "48") == 0) {
  937. num = 48;
  938. den = 1;
  939. } else if (strcmp(val, "59.94") == 0) {
  940. num = 60000;
  941. den = 1001;
  942. } else if (strcmp(val, "60") == 0) {
  943. num = 60;
  944. den = 1;
  945. } else {
  946. num = 30;
  947. den = 1;
  948. }
  949. }
  950. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  951. {
  952. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  953. den = 1;
  954. }
  955. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  956. {
  957. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  958. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  959. }
  960. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  961. {
  962. num = 1000000000;
  963. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  964. }
  965. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  966. {
  967. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  968. if (type == 1) //"Integer"
  969. GetFPSInteger(num, den);
  970. else if (type == 2) //"Fraction"
  971. GetFPSFraction(num, den);
  972. else if (false) //"Nanoseconds", currently not implemented
  973. GetFPSNanoseconds(num, den);
  974. else
  975. GetFPSCommon(num, den);
  976. }
  977. config_t OBSBasic::Config() const
  978. {
  979. return basicConfig;
  980. }