window-basic-main.cpp 14 KB

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