window-basic-main.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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-main.hpp"
  26. #include "qt-wrappers.hpp"
  27. #include "ui_OBSBasic.h"
  28. using namespace std;
  29. Q_DECLARE_METATYPE(OBSScene);
  30. Q_DECLARE_METATYPE(OBSSceneItem);
  31. OBSBasic::OBSBasic(QWidget *parent)
  32. : OBSMainWindow (parent),
  33. outputTest (NULL),
  34. sceneChanging (false),
  35. ui (new Ui::OBSBasic)
  36. {
  37. ui->setupUi(this);
  38. }
  39. bool OBSBasic::InitBasicConfigDefaults()
  40. {
  41. config_set_default_int(basicConfig, "Window", "PosX", -1);
  42. config_set_default_int(basicConfig, "Window", "PosY", -1);
  43. config_set_default_int(basicConfig, "Window", "SizeX", -1);
  44. config_set_default_int(basicConfig, "Window", "SizeY", -1);
  45. vector<MonitorInfo> monitors;
  46. GetMonitors(monitors);
  47. if (!monitors.size()) {
  48. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  49. "technically shouldn't be possible.");
  50. return false;
  51. }
  52. uint32_t cx = monitors[0].cx;
  53. uint32_t cy = monitors[0].cy;
  54. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  55. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  56. cx = cx * 10 / 15;
  57. cy = cy * 10 / 15;
  58. config_set_default_uint (basicConfig, "Video", "OutputCX", cx);
  59. config_set_default_uint (basicConfig, "Video", "OutputCY", cy);
  60. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  61. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  62. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  63. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  64. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  65. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  66. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  67. "Stereo");
  68. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  69. config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
  70. "default");
  71. config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
  72. "disabled");
  73. config_set_default_string(basicConfig, "Audio", "AuxDevice1",
  74. "default");
  75. config_set_default_string(basicConfig, "Audio", "AuxDevice2",
  76. "disabled");
  77. config_set_default_string(basicConfig, "Audio", "AuxDevice3",
  78. "disabled");
  79. return true;
  80. }
  81. bool OBSBasic::InitBasicConfig()
  82. {
  83. BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini"));
  84. int errorcode = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  85. if (errorcode != CONFIG_SUCCESS) {
  86. OBSErrorBox(NULL, "Failed to open basic.ini: %d", errorcode);
  87. return false;
  88. }
  89. return InitBasicConfigDefaults();
  90. }
  91. void OBSBasic::OBSInit()
  92. {
  93. /* make sure it's fully displayed before doing any initialization */
  94. show();
  95. App()->processEvents();
  96. if (!obs_startup())
  97. throw "Failed to initialize libobs";
  98. if (!InitBasicConfig())
  99. throw "Failed to load basic.ini";
  100. if (!ResetVideo())
  101. throw "Failed to initialize video";
  102. if (!ResetAudio())
  103. throw "Failed to initialize audio";
  104. signal_handler_connect(obs_signalhandler(), "source_add",
  105. OBSBasic::SourceAdded, this);
  106. signal_handler_connect(obs_signalhandler(), "source_remove",
  107. OBSBasic::SourceRemoved, this);
  108. signal_handler_connect(obs_signalhandler(), "channel_change",
  109. OBSBasic::ChannelChanged, this);
  110. /* TODO: this is a test, all modules will be searched for and loaded
  111. * automatically later */
  112. obs_load_module("test-input");
  113. obs_load_module("obs-ffmpeg");
  114. #ifdef __APPLE__
  115. obs_load_module("mac-capture");
  116. #elif _WIN32
  117. obs_load_module("win-wasapi");
  118. obs_load_module("win-capture");
  119. #endif
  120. ResetAudioDevices();
  121. }
  122. OBSBasic::~OBSBasic()
  123. {
  124. /* free the lists before shutting down to remove the scene/item
  125. * references */
  126. ui->sources->clear();
  127. ui->scenes->clear();
  128. obs_shutdown();
  129. }
  130. OBSScene OBSBasic::GetCurrentScene()
  131. {
  132. QListWidgetItem *item = ui->scenes->currentItem();
  133. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  134. }
  135. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  136. {
  137. QListWidgetItem *item = ui->sources->currentItem();
  138. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  139. }
  140. void OBSBasic::UpdateSources(OBSScene scene)
  141. {
  142. ui->sources->clear();
  143. obs_scene_enum_items(scene,
  144. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  145. {
  146. OBSBasic *window = static_cast<OBSBasic*>(p);
  147. window->InsertSceneItem(item);
  148. UNUSED_PARAMETER(scene);
  149. return true;
  150. }, this);
  151. }
  152. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  153. {
  154. obs_source_t source = obs_sceneitem_getsource(item);
  155. const char *name = obs_source_getname(source);
  156. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  157. listItem->setData(Qt::UserRole,
  158. QVariant::fromValue(OBSSceneItem(item)));
  159. ui->sources->insertItem(0, listItem);
  160. }
  161. /* Qt callbacks for invokeMethod */
  162. void OBSBasic::AddScene(OBSSource source)
  163. {
  164. const char *name = obs_source_getname(source);
  165. obs_scene_t scene = obs_scene_fromsource(source);
  166. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  167. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  168. ui->scenes->addItem(item);
  169. signal_handler_t handler = obs_source_signalhandler(source);
  170. signal_handler_connect(handler, "item_add",
  171. OBSBasic::SceneItemAdded, this);
  172. signal_handler_connect(handler, "item_remove",
  173. OBSBasic::SceneItemRemoved, this);
  174. }
  175. void OBSBasic::RemoveScene(OBSSource source)
  176. {
  177. const char *name = obs_source_getname(source);
  178. QListWidgetItem *sel = ui->scenes->currentItem();
  179. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  180. Qt::MatchExactly);
  181. if (sel != nullptr) {
  182. if (items.contains(sel))
  183. ui->sources->clear();
  184. delete sel;
  185. }
  186. }
  187. void OBSBasic::AddSceneItem(OBSSceneItem item)
  188. {
  189. obs_scene_t scene = obs_sceneitem_getscene(item);
  190. obs_source_t source = obs_sceneitem_getsource(item);
  191. if (GetCurrentScene() == scene)
  192. InsertSceneItem(item);
  193. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  194. }
  195. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  196. {
  197. obs_scene_t scene = obs_sceneitem_getscene(item);
  198. if (GetCurrentScene() == scene) {
  199. for (int i = 0; i < ui->sources->count(); i++) {
  200. QListWidgetItem *listItem = ui->sources->item(i);
  201. QVariant userData = listItem->data(Qt::UserRole);
  202. if (userData.value<OBSSceneItem>() == item) {
  203. delete listItem;
  204. break;
  205. }
  206. }
  207. }
  208. obs_source_t source = obs_sceneitem_getsource(item);
  209. int scenes = sourceSceneRefs[source] - 1;
  210. if (scenes == 0) {
  211. obs_source_remove(source);
  212. sourceSceneRefs.erase(source);
  213. }
  214. }
  215. void OBSBasic::UpdateSceneSelection(OBSSource source)
  216. {
  217. if (source) {
  218. obs_source_type type;
  219. obs_source_gettype(source, &type, NULL);
  220. if (type != OBS_SOURCE_TYPE_SCENE)
  221. return;
  222. obs_scene_t scene = obs_scene_fromsource(source);
  223. const char *name = obs_source_getname(source);
  224. QList<QListWidgetItem*> items =
  225. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  226. if (items.count()) {
  227. sceneChanging = true;
  228. ui->scenes->setCurrentItem(items.first());
  229. sceneChanging = false;
  230. UpdateSources(scene);
  231. }
  232. }
  233. }
  234. /* OBS Callbacks */
  235. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  236. {
  237. OBSBasic *window = static_cast<OBSBasic*>(data);
  238. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  239. QMetaObject::invokeMethod(window, "AddSceneItem",
  240. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  241. }
  242. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  243. {
  244. OBSBasic *window = static_cast<OBSBasic*>(data);
  245. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  246. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  247. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  248. }
  249. void OBSBasic::SourceAdded(void *data, calldata_t params)
  250. {
  251. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  252. obs_source_type type;
  253. obs_source_gettype(source, &type, NULL);
  254. if (type == OBS_SOURCE_TYPE_SCENE)
  255. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  256. "AddScene",
  257. Q_ARG(OBSSource, OBSSource(source)));
  258. }
  259. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  260. {
  261. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  262. obs_source_type type;
  263. obs_source_gettype(source, &type, NULL);
  264. if (type == OBS_SOURCE_TYPE_SCENE)
  265. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  266. "RemoveScene",
  267. Q_ARG(OBSSource, OBSSource(source)));
  268. }
  269. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  270. {
  271. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  272. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  273. if (channel == 0)
  274. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  275. "UpdateSceneSelection",
  276. Q_ARG(OBSSource, OBSSource(source)));
  277. }
  278. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  279. {
  280. OBSBasic *window = static_cast<OBSBasic*>(data);
  281. gs_matrix_push();
  282. gs_matrix_scale3f(window->previewScale, window->previewScale, 1.0f);
  283. gs_matrix_translate3f(-window->previewX, -window->previewY, 0.0f);
  284. obs_render_main_view();
  285. gs_matrix_pop();
  286. UNUSED_PARAMETER(cx);
  287. UNUSED_PARAMETER(cy);
  288. }
  289. /* Main class functions */
  290. bool OBSBasic::ResetVideo()
  291. {
  292. struct obs_video_info ovi;
  293. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  294. ovi.graphics_module = App()->GetRenderModule();
  295. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  296. "Video", "BaseCX");
  297. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  298. "Video", "BaseCY");
  299. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  300. "Video", "OutputCX");
  301. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  302. "Video", "OutputCY");
  303. ovi.output_format = VIDEO_FORMAT_I420;
  304. ovi.adapter = 0;
  305. ovi.gpu_conversion = true;
  306. QTToGSWindow(ui->preview->winId(), ovi.window);
  307. //required to make opengl display stuff on osx(?)
  308. ResizePreview(ovi.base_width, ovi.base_height);
  309. QSize size = ui->preview->size();
  310. ovi.window_width = size.width();
  311. ovi.window_height = size.height();
  312. if (!obs_reset_video(&ovi))
  313. return false;
  314. obs_add_draw_callback(OBSBasic::RenderMain, this);
  315. return true;
  316. }
  317. bool OBSBasic::ResetAudio()
  318. {
  319. struct audio_output_info ai;
  320. ai.name = "Main Audio Track";
  321. ai.format = AUDIO_FORMAT_FLOAT;
  322. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  323. "SampleRate");
  324. const char *channelSetupStr = config_get_string(basicConfig,
  325. "Audio", "ChannelSetup");
  326. if (strcmp(channelSetupStr, "Mono") == 0)
  327. ai.speakers = SPEAKERS_MONO;
  328. else
  329. ai.speakers = SPEAKERS_STEREO;
  330. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  331. return obs_reset_audio(&ai);
  332. }
  333. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  334. int channel)
  335. {
  336. const char *deviceId = config_get_string(basicConfig, "Audio",
  337. deviceName);
  338. obs_source_t source;
  339. obs_data_t settings;
  340. bool same = false;
  341. source = obs_get_output_source(channel);
  342. if (source) {
  343. settings = obs_source_getsettings(source);
  344. const char *curId = obs_data_getstring(settings, "device_id");
  345. same = (strcmp(curId, deviceId) == 0);
  346. obs_data_release(settings);
  347. obs_source_release(source);
  348. }
  349. if (!same)
  350. obs_set_output_source(channel, nullptr);
  351. if (!same && strcmp(deviceId, "disabled") != 0) {
  352. obs_data_t settings = obs_data_create();
  353. obs_data_setstring(settings, "device_id", deviceId);
  354. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  355. sourceId, deviceName, settings);
  356. obs_data_release(settings);
  357. obs_set_output_source(channel, source);
  358. obs_source_release(source);
  359. }
  360. }
  361. void OBSBasic::ResetAudioDevices()
  362. {
  363. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1", 1);
  364. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2", 2);
  365. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1", 3);
  366. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2", 4);
  367. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3", 5);
  368. }
  369. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  370. {
  371. double targetAspect, baseAspect;
  372. QSize targetSize;
  373. int x, y;
  374. /* resize preview panel to fix to the top section of the window */
  375. targetSize = ui->preview->size();
  376. targetAspect = double(targetSize.width()) / double(targetSize.height());
  377. baseAspect = double(cx) / double(cy);
  378. if (targetAspect > baseAspect) {
  379. previewScale = float(targetSize.height()) / float(cy);
  380. cx = targetSize.height() * baseAspect;
  381. cy = targetSize.height();
  382. } else {
  383. previewScale = float(targetSize.width()) / float(cx);
  384. cx = targetSize.width();
  385. cy = targetSize.width() / baseAspect;
  386. }
  387. previewX = targetSize.width() /2 - cx/2;
  388. previewY = targetSize.height()/2 - cy/2;
  389. if (isVisible()) {
  390. if (resizeTimer)
  391. killTimer(resizeTimer);
  392. resizeTimer = startTimer(100);
  393. }
  394. }
  395. void OBSBasic::closeEvent(QCloseEvent *event)
  396. {
  397. /* TODO */
  398. UNUSED_PARAMETER(event);
  399. }
  400. void OBSBasic::changeEvent(QEvent *event)
  401. {
  402. /* TODO */
  403. UNUSED_PARAMETER(event);
  404. }
  405. void OBSBasic::resizeEvent(QResizeEvent *event)
  406. {
  407. struct obs_video_info ovi;
  408. if (obs_get_video_info(&ovi))
  409. ResizePreview(ovi.base_width, ovi.base_height);
  410. UNUSED_PARAMETER(event);
  411. }
  412. void OBSBasic::timerEvent(QTimerEvent *event)
  413. {
  414. if (event->timerId() == resizeTimer) {
  415. killTimer(resizeTimer);
  416. resizeTimer = 0;
  417. QSize size = ui->preview->size();
  418. obs_resize(size.width(), size.height());
  419. }
  420. }
  421. void OBSBasic::on_action_New_triggered()
  422. {
  423. /* TODO */
  424. }
  425. void OBSBasic::on_action_Open_triggered()
  426. {
  427. /* TODO */
  428. }
  429. void OBSBasic::on_action_Save_triggered()
  430. {
  431. /* TODO */
  432. }
  433. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  434. QListWidgetItem *prev)
  435. {
  436. obs_source_t source = NULL;
  437. if (sceneChanging)
  438. return;
  439. if (current) {
  440. obs_scene_t scene;
  441. scene = current->data(Qt::UserRole).value<OBSScene>();
  442. source = obs_scene_getsource(scene);
  443. }
  444. /* TODO: allow transitions */
  445. obs_set_output_source(0, source);
  446. UNUSED_PARAMETER(prev);
  447. }
  448. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  449. {
  450. /* TODO */
  451. UNUSED_PARAMETER(pos);
  452. }
  453. void OBSBasic::on_actionAddScene_triggered()
  454. {
  455. string name;
  456. bool accepted = NameDialog::AskForName(this,
  457. QTStr("MainWindow.AddSceneDlg.Title"),
  458. QTStr("MainWindow.AddSceneDlg.Text"),
  459. name);
  460. if (accepted) {
  461. obs_source_t source = obs_get_source_by_name(name.c_str());
  462. if (source) {
  463. QMessageBox::information(this,
  464. QTStr("MainWindow.NameExists.Title"),
  465. QTStr("MainWindow.NameExists.Text"));
  466. obs_source_release(source);
  467. on_actionAddScene_triggered();
  468. return;
  469. }
  470. obs_scene_t scene = obs_scene_create(name.c_str());
  471. source = obs_scene_getsource(scene);
  472. obs_add_source(source);
  473. obs_scene_release(scene);
  474. obs_set_output_source(0, source);
  475. }
  476. }
  477. void OBSBasic::on_actionRemoveScene_triggered()
  478. {
  479. QListWidgetItem *item = ui->scenes->currentItem();
  480. if (!item)
  481. return;
  482. QVariant userData = item->data(Qt::UserRole);
  483. obs_scene_t scene = userData.value<OBSScene>();
  484. obs_source_t source = obs_scene_getsource(scene);
  485. obs_source_remove(source);
  486. }
  487. void OBSBasic::on_actionSceneProperties_triggered()
  488. {
  489. /* TODO */
  490. }
  491. void OBSBasic::on_actionSceneUp_triggered()
  492. {
  493. /* TODO */
  494. }
  495. void OBSBasic::on_actionSceneDown_triggered()
  496. {
  497. /* TODO */
  498. }
  499. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  500. QListWidgetItem *prev)
  501. {
  502. /* TODO */
  503. UNUSED_PARAMETER(current);
  504. UNUSED_PARAMETER(prev);
  505. }
  506. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  507. {
  508. /* TODO */
  509. UNUSED_PARAMETER(pos);
  510. }
  511. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  512. {
  513. string name;
  514. bool success = false;
  515. while (!success) {
  516. bool accepted = NameDialog::AskForName(this,
  517. Str("MainWindow.AddSourceDlg.Title"),
  518. Str("MainWindow.AddSourceDlg.Text"),
  519. name);
  520. if (!accepted)
  521. break;
  522. obs_source_t source = obs_get_source_by_name(name.c_str());
  523. if (!source) {
  524. success = true;
  525. } else {
  526. QMessageBox::information(this,
  527. QTStr("MainWindow.NameExists.Title"),
  528. QTStr("MainWindow.NameExists.Text"));
  529. obs_source_release(source);
  530. }
  531. }
  532. if (success) {
  533. obs_source_t source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  534. id, name.c_str(), NULL);
  535. sourceSceneRefs[source] = 0;
  536. obs_add_source(source);
  537. obs_scene_add(scene, source);
  538. obs_source_release(source);
  539. }
  540. }
  541. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  542. {
  543. OBSScene scene = GetCurrentScene();
  544. const char *type;
  545. bool foundValues = false;
  546. size_t idx = 0;
  547. if (!scene)
  548. return;
  549. QMenu popup;
  550. while (obs_enum_input_types(idx++, &type)) {
  551. const char *name = obs_source_getdisplayname(
  552. OBS_SOURCE_TYPE_INPUT,
  553. type, App()->GetLocale());
  554. QAction *popupItem = new QAction(QT_UTF8(name), this);
  555. popupItem->setData(QT_UTF8(type));
  556. popup.addAction(popupItem);
  557. foundValues = true;
  558. }
  559. if (foundValues) {
  560. QAction *ret = popup.exec(pos);
  561. if (ret)
  562. AddSource(scene, ret->data().toString().toUtf8());
  563. }
  564. }
  565. void OBSBasic::on_actionAddSource_triggered()
  566. {
  567. AddSourcePopupMenu(QCursor::pos());
  568. }
  569. void OBSBasic::on_actionRemoveSource_triggered()
  570. {
  571. OBSSceneItem item = GetCurrentSceneItem();
  572. if (item)
  573. obs_sceneitem_remove(item);
  574. }
  575. void OBSBasic::on_actionSourceProperties_triggered()
  576. {
  577. }
  578. void OBSBasic::on_actionSourceUp_triggered()
  579. {
  580. }
  581. void OBSBasic::on_actionSourceDown_triggered()
  582. {
  583. }
  584. void OBSBasic::on_recordButton_clicked()
  585. {
  586. if (outputTest) {
  587. obs_output_destroy(outputTest);
  588. outputTest = NULL;
  589. ui->recordButton->setText("Start Recording");
  590. } else {
  591. QString path = QFileDialog::getSaveFileName(this,
  592. "Please enter a file name", QString(),
  593. "MP4 Files (*.mp4)");
  594. if (path.isNull() || path.isEmpty())
  595. return;
  596. obs_data_t data = obs_data_create();
  597. obs_data_setstring(data, "filename", QT_TO_UTF8(path));
  598. outputTest = obs_output_create("ffmpeg_output", "test", data);
  599. obs_data_release(data);
  600. if (!obs_output_start(outputTest)) {
  601. obs_output_destroy(outputTest);
  602. outputTest = NULL;
  603. return;
  604. }
  605. ui->recordButton->setText("Stop Recording");
  606. }
  607. }
  608. void OBSBasic::on_settingsButton_clicked()
  609. {
  610. OBSBasicSettings settings(this);
  611. settings.exec();
  612. }
  613. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  614. {
  615. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  616. if (strcmp(val, "10") == 0) {
  617. num = 10;
  618. den = 1;
  619. } else if (strcmp(val, "20") == 0) {
  620. num = 20;
  621. den = 1;
  622. } else if (strcmp(val, "25") == 0) {
  623. num = 25;
  624. den = 1;
  625. } else if (strcmp(val, "29.97") == 0) {
  626. num = 30000;
  627. den = 1001;
  628. } else if (strcmp(val, "48") == 0) {
  629. num = 48;
  630. den = 1;
  631. } else if (strcmp(val, "59.94") == 0) {
  632. num = 60000;
  633. den = 1001;
  634. } else if (strcmp(val, "60") == 0) {
  635. num = 60;
  636. den = 1;
  637. } else {
  638. num = 30;
  639. den = 1;
  640. }
  641. }
  642. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  643. {
  644. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  645. den = 1;
  646. }
  647. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  648. {
  649. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  650. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  651. }
  652. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  653. {
  654. num = 1000000000;
  655. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  656. }
  657. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  658. {
  659. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  660. if (type == 1) //"Integer"
  661. GetFPSInteger(num, den);
  662. else if (type == 2) //"Fraction"
  663. GetFPSFraction(num, den);
  664. else if (false) //"Nanoseconds", currently not implemented
  665. GetFPSNanoseconds(num, den);
  666. else
  667. GetFPSCommon(num, den);
  668. }
  669. config_t OBSBasic::Config() const
  670. {
  671. return basicConfig;
  672. }