window-basic-main.cpp 12 KB

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