window-basic-main.cpp 14 KB

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