window-basic-main.cpp 14 KB

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