window-basic-main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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, newSize;
  223. /* resize preview panel to fix to the top section of the window */
  224. targetSize = ui->previewContainer->size();
  225. targetAspect = double(targetSize.width()) / double(targetSize.height());
  226. baseAspect = double(cx) / double(cy);
  227. if (targetAspect > baseAspect)
  228. newSize = QSize(targetSize.height() * baseAspect,
  229. targetSize.height());
  230. else
  231. newSize = QSize(targetSize.width(),
  232. targetSize.width() / baseAspect);
  233. //ui->preview->resize(newSize);
  234. graphics_t graphics = obs_graphics();
  235. /*if (graphics) {
  236. gs_entercontext(graphics);
  237. gs_resize(newSize.width(), newSize.height());
  238. gs_leavecontext();
  239. }*/
  240. }
  241. void OBSBasic::closeEvent(QCloseEvent *event)
  242. {
  243. }
  244. void OBSBasic::changeEvent(QEvent *event)
  245. {
  246. }
  247. void OBSBasic::resizeEvent(QResizeEvent *event)
  248. {
  249. struct obs_video_info ovi;
  250. if (obs_get_video_info(&ovi))
  251. ResizePreview(ovi.base_width, ovi.base_height);
  252. }
  253. void OBSBasic::on_action_New_triggered()
  254. {
  255. }
  256. void OBSBasic::on_action_Open_triggered()
  257. {
  258. }
  259. void OBSBasic::on_action_Save_triggered()
  260. {
  261. }
  262. void OBSBasic::on_scenes_itemChanged(QListWidgetItem *item)
  263. {
  264. obs_source_t source = NULL;
  265. if (item) {
  266. obs_scene_t scene;
  267. scene = VariantPtr<obs_scene_t>(item->data(Qt::UserRole));
  268. source = obs_scene_getsource(scene);
  269. UpdateSources(scene);
  270. }
  271. /* TODO: allow transitions */
  272. obs_set_output_source(0, source);
  273. }
  274. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  275. {
  276. }
  277. void OBSBasic::on_actionAddScene_triggered()
  278. {
  279. string name;
  280. bool accepted = NameDialog::AskForName(this,
  281. QTStr("MainWindow.AddSceneDlg.Title"),
  282. QTStr("MainWindow.AddSceneDlg.Text"),
  283. name);
  284. if (accepted) {
  285. obs_source_t source = obs_get_source_by_name(name.c_str());
  286. if (source) {
  287. QMessageBox::information(this,
  288. QTStr("MainWindow.NameExists.Title"),
  289. QTStr("MainWindow.NameExists.Text"));
  290. obs_source_release(source);
  291. on_actionAddScene_triggered();
  292. return;
  293. }
  294. obs_scene_t scene = obs_scene_create(name.c_str());
  295. source = obs_scene_getsource(scene);
  296. obs_add_source(source);
  297. obs_scene_release(scene);
  298. obs_set_output_source(0, source);
  299. }
  300. }
  301. void OBSBasic::on_actionRemoveScene_triggered()
  302. {
  303. QListWidgetItem *item = ui->scenes->currentItem();
  304. if (!item)
  305. return;
  306. QVariant userData = item->data(Qt::UserRole);
  307. obs_scene_t scene = VariantPtr<obs_scene_t>(userData);
  308. obs_source_t source = obs_scene_getsource(scene);
  309. obs_source_remove(source);
  310. }
  311. void OBSBasic::on_actionSceneProperties_triggered()
  312. {
  313. }
  314. void OBSBasic::on_actionSceneUp_triggered()
  315. {
  316. }
  317. void OBSBasic::on_actionSceneDown_triggered()
  318. {
  319. }
  320. void OBSBasic::on_sources_itemChanged(QListWidgetItem *item)
  321. {
  322. }
  323. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  324. {
  325. }
  326. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  327. {
  328. string name;
  329. bool success = false;
  330. while (!success) {
  331. bool accepted = NameDialog::AskForName(this,
  332. Str("MainWindow.AddSourceDlg.Title"),
  333. Str("MainWindow.AddSourceDlg.Text"),
  334. name);
  335. if (!accepted)
  336. break;
  337. obs_source_t source = obs_get_source_by_name(name.c_str());
  338. if (!source) {
  339. success = true;
  340. } else {
  341. QMessageBox::information(this,
  342. QTStr("MainWindow.NameExists.Title"),
  343. QTStr("MainWindow.NameExists.Text"));
  344. obs_source_release(source);
  345. }
  346. }
  347. if (success) {
  348. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  349. name.c_str(), NULL);
  350. sourceSceneRefs[source] = 0;
  351. obs_add_source(source);
  352. obs_sceneitem_t item = obs_scene_add(scene, source);
  353. obs_source_release(source);
  354. }
  355. }
  356. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  357. {
  358. QListWidgetItem *sel = ui->scenes->currentItem();
  359. const char *type;
  360. bool foundValues = false;
  361. size_t idx = 0;
  362. if (!sel)
  363. return;
  364. obs_scene_t scene = VariantPtr<obs_scene_t>(sel->data(Qt::UserRole));
  365. obs_scene_addref(scene);
  366. QMenu popup;
  367. while (obs_enum_input_types(idx++, &type)) {
  368. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  369. type, App()->GetLocale());
  370. QAction *popupItem = new QAction(QT_UTF8(name), this);
  371. popupItem->setData(QT_UTF8(type));
  372. popup.addAction(popupItem);
  373. foundValues = true;
  374. }
  375. if (foundValues) {
  376. QAction *ret = popup.exec(pos);
  377. if (ret)
  378. AddSource(scene, ret->data().toString().toUtf8());
  379. }
  380. obs_scene_release(scene);
  381. }
  382. void OBSBasic::on_actionAddSource_triggered()
  383. {
  384. AddSourcePopupMenu(QCursor::pos());
  385. }
  386. void OBSBasic::on_actionRemoveSource_triggered()
  387. {
  388. QListWidgetItem *sel = ui->sources->currentItem();
  389. if (!sel)
  390. return;
  391. QVariant userData = sel->data(Qt::UserRole);
  392. obs_sceneitem_t item = VariantPtr<obs_sceneitem_t>(userData);
  393. obs_source_t source = obs_sceneitem_getsource(item);
  394. obs_sceneitem_destroy(item);
  395. }
  396. void OBSBasic::on_actionSourceProperties_triggered()
  397. {
  398. }
  399. void OBSBasic::on_actionSourceUp_triggered()
  400. {
  401. }
  402. void OBSBasic::on_actionSourceDown_triggered()
  403. {
  404. }
  405. void OBSBasic::on_settingsButton_clicked()
  406. {
  407. }