window-basic-main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /******************************************************************************
  2. Copyright (C) 2013 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 "obs-app.hpp"
  19. #include "window-basic-settings.hpp"
  20. #include "window-namedialog.hpp"
  21. #include "window-basic-main.hpp"
  22. #include "qt-wrappers.hpp"
  23. #include "ui_OBSBasic.h"
  24. using namespace std;
  25. Q_DECLARE_METATYPE(OBSScene);
  26. Q_DECLARE_METATYPE(OBSSceneItem);
  27. OBSBasic::OBSBasic(QWidget *parent)
  28. : OBSMainWindow (parent),
  29. ui (new Ui::OBSBasic)
  30. {
  31. ui->setupUi(this);
  32. }
  33. void OBSBasic::OBSInit()
  34. {
  35. /* make sure it's fully displayed before doing any initialization */
  36. show();
  37. App()->processEvents();
  38. if (!obs_startup())
  39. throw "Failed to initialize libobs";
  40. if (!InitGraphics())
  41. throw "Failed to initialize graphics";
  42. if (!InitAudio())
  43. throw "Failed to initialize audio";
  44. signal_handler_connect(obs_signalhandler(), "source-add",
  45. OBSBasic::SourceAdded, this);
  46. signal_handler_connect(obs_signalhandler(), "source-remove",
  47. OBSBasic::SourceRemoved, this);
  48. signal_handler_connect(obs_signalhandler(), "channel-change",
  49. OBSBasic::ChannelChanged, this);
  50. /* TODO: this is a test */
  51. obs_load_module("test-input");
  52. /*obs_load_module("obs-ffmpeg");
  53. obs_output_t output = obs_output_create("ffmpeg_output", "test",
  54. NULL);
  55. obs_output_start(output);*/
  56. /* HACK: fixes a qt bug with native widgets with native repaint */
  57. ui->previewContainer->repaint();
  58. }
  59. OBSBasic::~OBSBasic()
  60. {
  61. /* free the lists before shutting down to remove the scene/item
  62. * references */
  63. ui->sources->clear();
  64. ui->scenes->clear();
  65. obs_shutdown();
  66. }
  67. OBSScene OBSBasic::GetCurrentScene()
  68. {
  69. QListWidgetItem *item = ui->scenes->currentItem();
  70. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  71. }
  72. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  73. {
  74. QListWidgetItem *item = ui->sources->currentItem();
  75. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  76. }
  77. void OBSBasic::UpdateSources(OBSScene scene)
  78. {
  79. ui->sources->clear();
  80. obs_scene_enum_items(scene,
  81. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  82. {
  83. OBSBasic *window = static_cast<OBSBasic*>(p);
  84. window->AddSceneItem(item);
  85. return true;
  86. }, this);
  87. }
  88. /* Qt callbacks for invokeMethod */
  89. void OBSBasic::AddScene(OBSSource source)
  90. {
  91. const char *name = obs_source_getname(source);
  92. obs_scene_t scene = obs_scene_fromsource(source);
  93. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  94. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  95. ui->scenes->addItem(item);
  96. signal_handler_t handler = obs_source_signalhandler(source);
  97. signal_handler_connect(handler, "add", OBSBasic::SceneItemAdded, this);
  98. signal_handler_connect(handler, "remove", OBSBasic::SceneItemRemoved,
  99. this);
  100. }
  101. void OBSBasic::RemoveScene(OBSSource source)
  102. {
  103. const char *name = obs_source_getname(source);
  104. QListWidgetItem *sel = ui->scenes->currentItem();
  105. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  106. Qt::MatchExactly);
  107. if (sel != nullptr) {
  108. if (items.contains(sel))
  109. ui->sources->clear();
  110. delete sel;
  111. }
  112. }
  113. void OBSBasic::AddSceneItem(OBSSceneItem item)
  114. {
  115. obs_scene_t scene = obs_sceneitem_getscene(item);
  116. obs_source_t source = obs_sceneitem_getsource(item);
  117. const char *name = obs_source_getname(source);
  118. if (GetCurrentScene() == scene) {
  119. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  120. listItem->setData(Qt::UserRole,
  121. QVariant::fromValue(OBSSceneItem(item)));
  122. ui->sources->insertItem(0, listItem);
  123. }
  124. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  125. }
  126. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  127. {
  128. obs_scene_t scene = obs_sceneitem_getscene(item);
  129. if (GetCurrentScene() == scene) {
  130. for (int i = 0; i < ui->sources->count(); i++) {
  131. QListWidgetItem *listItem = ui->sources->item(i);
  132. QVariant userData = listItem->data(Qt::UserRole);
  133. if (userData.value<OBSSceneItem>() == item) {
  134. delete listItem;
  135. break;
  136. }
  137. }
  138. }
  139. obs_source_t source = obs_sceneitem_getsource(item);
  140. int scenes = sourceSceneRefs[source] - 1;
  141. if (scenes == 0) {
  142. obs_source_remove(source);
  143. sourceSceneRefs.erase(source);
  144. }
  145. }
  146. void OBSBasic::UpdateSceneSelection(OBSSource source)
  147. {
  148. if (source) {
  149. obs_source_type type;
  150. obs_source_gettype(source, &type, NULL);
  151. if (type != SOURCE_SCENE)
  152. return;
  153. obs_scene_t scene = obs_scene_fromsource(source);
  154. const char *name = obs_source_getname(source);
  155. QListWidgetItem *sel = ui->scenes->currentItem();
  156. QList<QListWidgetItem*> items =
  157. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  158. if (items.contains(sel)) {
  159. ui->scenes->setCurrentItem(sel);
  160. UpdateSources(scene);
  161. }
  162. }
  163. }
  164. /* OBS Callbacks */
  165. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  166. {
  167. OBSBasic *window = static_cast<OBSBasic*>(data);
  168. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  169. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  170. QMetaObject::invokeMethod(window, "AddSceneItem",
  171. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  172. }
  173. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  174. {
  175. OBSBasic *window = static_cast<OBSBasic*>(data);
  176. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  177. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  178. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  179. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  180. }
  181. void OBSBasic::SourceAdded(void *data, calldata_t params)
  182. {
  183. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  184. obs_source_type type;
  185. obs_source_gettype(source, &type, NULL);
  186. if (type == SOURCE_SCENE)
  187. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  188. "AddScene",
  189. Q_ARG(OBSSource, OBSSource(source)));
  190. }
  191. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  192. {
  193. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  194. obs_source_type type;
  195. obs_source_gettype(source, &type, NULL);
  196. if (type == SOURCE_SCENE)
  197. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  198. "RemoveScene",
  199. Q_ARG(OBSSource, OBSSource(source)));
  200. }
  201. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  202. {
  203. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  204. uint32_t channel = calldata_uint32(params, "channel");
  205. if (channel == 0)
  206. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  207. "UpdateSceneSelection",
  208. Q_ARG(OBSSource, OBSSource(source)));
  209. }
  210. /* Main class functions */
  211. bool OBSBasic::InitGraphics()
  212. {
  213. struct obs_video_info ovi;
  214. App()->GetConfigFPS(ovi.fps_num, ovi.fps_den);
  215. ovi.graphics_module = App()->GetRenderModule();
  216. ovi.base_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  217. "Video", "BaseCX");
  218. ovi.base_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  219. "Video", "BaseCY");
  220. ovi.output_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  221. "Video", "OutputCX");
  222. ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  223. "Video", "OutputCY");
  224. ovi.output_format = VIDEO_FORMAT_RGBA;
  225. ovi.adapter = 0;
  226. //#ifdef __WXGTK__
  227. /* Ugly hack for GTK, I'm hoping this can be avoided eventually... */
  228. // gtk_widget_realize(previewPanel->GetHandle());
  229. //#endif
  230. QTToGSWindow(ui->preview, ovi.window);
  231. //required to make opengl display stuff on osx(?)
  232. ResizePreview(ovi.base_width, ovi.base_height);
  233. QSize size = ui->preview->size();
  234. ovi.window_width = size.width();
  235. ovi.window_height = size.height();
  236. return obs_reset_video(&ovi);
  237. }
  238. bool OBSBasic::InitAudio()
  239. {
  240. /* TODO: load audio settings from config */
  241. struct audio_output_info ai;
  242. ai.name = "test";
  243. ai.samples_per_sec = 44100;
  244. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  245. ai.speakers = SPEAKERS_STEREO;
  246. ai.buffer_ms = 700;
  247. return obs_reset_audio(&ai);
  248. }
  249. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  250. {
  251. double targetAspect, baseAspect;
  252. QSize targetSize;
  253. int x, y;
  254. /* resize preview panel to fix to the top section of the window */
  255. targetSize = ui->previewContainer->size();
  256. targetAspect = double(targetSize.width()) / double(targetSize.height());
  257. baseAspect = double(cx) / double(cy);
  258. if (targetAspect > baseAspect) {
  259. cx = targetSize.height() * baseAspect;
  260. cy = targetSize.height();
  261. } else {
  262. cx = targetSize.width();
  263. cy = targetSize.width() / baseAspect;
  264. }
  265. x = targetSize.width() /2 - cx/2;
  266. y = targetSize.height()/2 - cy/2;
  267. ui->preview->setGeometry(x, y, cx, cy);
  268. graphics_t graphics = obs_graphics();
  269. if (graphics && isVisible()) {
  270. gs_entercontext(graphics);
  271. gs_resize(cx, cy);
  272. gs_leavecontext();
  273. }
  274. }
  275. void OBSBasic::closeEvent(QCloseEvent *event)
  276. {
  277. }
  278. void OBSBasic::changeEvent(QEvent *event)
  279. {
  280. }
  281. void OBSBasic::resizeEvent(QResizeEvent *event)
  282. {
  283. struct obs_video_info ovi;
  284. if (obs_get_video_info(&ovi))
  285. ResizePreview(ovi.base_width, ovi.base_height);
  286. }
  287. void OBSBasic::on_action_New_triggered()
  288. {
  289. }
  290. void OBSBasic::on_action_Open_triggered()
  291. {
  292. }
  293. void OBSBasic::on_action_Save_triggered()
  294. {
  295. }
  296. void OBSBasic::on_scenes_itemChanged(QListWidgetItem *item)
  297. {
  298. obs_source_t source = NULL;
  299. if (item) {
  300. obs_scene_t scene;
  301. scene = item->data(Qt::UserRole).value<OBSScene>();
  302. source = obs_scene_getsource(scene);
  303. UpdateSources(scene);
  304. }
  305. /* TODO: allow transitions */
  306. obs_set_output_source(0, source);
  307. }
  308. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  309. {
  310. }
  311. void OBSBasic::on_actionAddScene_triggered()
  312. {
  313. string name;
  314. bool accepted = NameDialog::AskForName(this,
  315. QTStr("MainWindow.AddSceneDlg.Title"),
  316. QTStr("MainWindow.AddSceneDlg.Text"),
  317. name);
  318. if (accepted) {
  319. obs_source_t source = obs_get_source_by_name(name.c_str());
  320. if (source) {
  321. QMessageBox::information(this,
  322. QTStr("MainWindow.NameExists.Title"),
  323. QTStr("MainWindow.NameExists.Text"));
  324. obs_source_release(source);
  325. on_actionAddScene_triggered();
  326. return;
  327. }
  328. obs_scene_t scene = obs_scene_create(name.c_str());
  329. source = obs_scene_getsource(scene);
  330. obs_add_source(source);
  331. obs_scene_release(scene);
  332. obs_set_output_source(0, source);
  333. }
  334. }
  335. void OBSBasic::on_actionRemoveScene_triggered()
  336. {
  337. QListWidgetItem *item = ui->scenes->currentItem();
  338. if (!item)
  339. return;
  340. QVariant userData = item->data(Qt::UserRole);
  341. obs_scene_t scene = userData.value<OBSScene>();
  342. obs_source_t source = obs_scene_getsource(scene);
  343. obs_source_remove(source);
  344. }
  345. void OBSBasic::on_actionSceneProperties_triggered()
  346. {
  347. }
  348. void OBSBasic::on_actionSceneUp_triggered()
  349. {
  350. }
  351. void OBSBasic::on_actionSceneDown_triggered()
  352. {
  353. }
  354. void OBSBasic::on_sources_itemChanged(QListWidgetItem *item)
  355. {
  356. }
  357. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  358. {
  359. }
  360. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  361. {
  362. string name;
  363. bool success = false;
  364. while (!success) {
  365. bool accepted = NameDialog::AskForName(this,
  366. Str("MainWindow.AddSourceDlg.Title"),
  367. Str("MainWindow.AddSourceDlg.Text"),
  368. name);
  369. if (!accepted)
  370. break;
  371. obs_source_t source = obs_get_source_by_name(name.c_str());
  372. if (!source) {
  373. success = true;
  374. } else {
  375. QMessageBox::information(this,
  376. QTStr("MainWindow.NameExists.Title"),
  377. QTStr("MainWindow.NameExists.Text"));
  378. obs_source_release(source);
  379. }
  380. }
  381. if (success) {
  382. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  383. name.c_str(), NULL);
  384. sourceSceneRefs[source] = 0;
  385. obs_add_source(source);
  386. obs_sceneitem_t item = obs_scene_add(scene, source);
  387. obs_source_release(source);
  388. }
  389. }
  390. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  391. {
  392. OBSScene scene = GetCurrentScene();
  393. const char *type;
  394. bool foundValues = false;
  395. size_t idx = 0;
  396. if (!scene)
  397. return;
  398. QMenu popup;
  399. while (obs_enum_input_types(idx++, &type)) {
  400. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  401. type, App()->GetLocale());
  402. QAction *popupItem = new QAction(QT_UTF8(name), this);
  403. popupItem->setData(QT_UTF8(type));
  404. popup.addAction(popupItem);
  405. foundValues = true;
  406. }
  407. if (foundValues) {
  408. QAction *ret = popup.exec(pos);
  409. if (ret)
  410. AddSource(scene, ret->data().toString().toUtf8());
  411. }
  412. }
  413. void OBSBasic::on_actionAddSource_triggered()
  414. {
  415. AddSourcePopupMenu(QCursor::pos());
  416. }
  417. void OBSBasic::on_actionRemoveSource_triggered()
  418. {
  419. OBSSceneItem item = GetCurrentSceneItem();
  420. if (item)
  421. obs_sceneitem_remove(item);
  422. }
  423. void OBSBasic::on_actionSourceProperties_triggered()
  424. {
  425. }
  426. void OBSBasic::on_actionSourceUp_triggered()
  427. {
  428. }
  429. void OBSBasic::on_actionSourceDown_triggered()
  430. {
  431. }
  432. void OBSBasic::on_settingsButton_clicked()
  433. {
  434. OBSBasicSettings settings(this);
  435. settings.exec();
  436. }