window-basic-main.cpp 22 KB

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