window-basic-main.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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, "item-add",
  99. OBSBasic::SceneItemAdded, this);
  100. signal_handler_connect(handler, "item-remove",
  101. OBSBasic::SceneItemRemoved, 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::ResetVideo()
  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. if (!obs_reset_video(&ovi))
  242. return false;
  243. obs_add_draw_callback(OBSBasic::RenderMain, this);
  244. return true;
  245. }
  246. bool OBSBasic::ResetAudio()
  247. {
  248. struct audio_output_info ai;
  249. ai.name = "Main Audio Track";
  250. ai.format = AUDIO_FORMAT_FLOAT;
  251. ai.samples_per_sec = config_get_uint(GetGlobalConfig(), "Audio",
  252. "SampleRate");
  253. const char *channelSetupStr = config_get_string(GetGlobalConfig(),
  254. "Audio", "ChannelSetup");
  255. if (strcmp(channelSetupStr, "Mono") == 0)
  256. ai.speakers = SPEAKERS_MONO;
  257. else
  258. ai.speakers = SPEAKERS_STEREO;
  259. ai.buffer_ms = config_get_uint(GetGlobalConfig(), "Audio",
  260. "BufferingTime");
  261. return obs_reset_audio(&ai);
  262. }
  263. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  264. {
  265. double targetAspect, baseAspect;
  266. QSize targetSize;
  267. int x, y;
  268. /* resize preview panel to fix to the top section of the window */
  269. targetSize = ui->previewContainer->size();
  270. targetAspect = double(targetSize.width()) / double(targetSize.height());
  271. baseAspect = double(cx) / double(cy);
  272. if (targetAspect > baseAspect) {
  273. cx = targetSize.height() * baseAspect;
  274. cy = targetSize.height();
  275. } else {
  276. cx = targetSize.width();
  277. cy = targetSize.width() / baseAspect;
  278. }
  279. x = targetSize.width() /2 - cx/2;
  280. y = targetSize.height()/2 - cy/2;
  281. ui->preview->setGeometry(x, y, cx, cy);
  282. if (isVisible())
  283. obs_resize(cx, cy);
  284. }
  285. void OBSBasic::closeEvent(QCloseEvent *event)
  286. {
  287. /* TODO */
  288. UNUSED_PARAMETER(event);
  289. }
  290. void OBSBasic::changeEvent(QEvent *event)
  291. {
  292. /* TODO */
  293. UNUSED_PARAMETER(event);
  294. }
  295. void OBSBasic::resizeEvent(QResizeEvent *event)
  296. {
  297. struct obs_video_info ovi;
  298. if (obs_get_video_info(&ovi))
  299. ResizePreview(ovi.base_width, ovi.base_height);
  300. UNUSED_PARAMETER(event);
  301. }
  302. void OBSBasic::on_action_New_triggered()
  303. {
  304. /* TODO */
  305. }
  306. void OBSBasic::on_action_Open_triggered()
  307. {
  308. /* TODO */
  309. }
  310. void OBSBasic::on_action_Save_triggered()
  311. {
  312. /* TODO */
  313. }
  314. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  315. QListWidgetItem *prev)
  316. {
  317. obs_source_t source = NULL;
  318. if (sceneChanging)
  319. return;
  320. if (current) {
  321. obs_scene_t scene;
  322. scene = current->data(Qt::UserRole).value<OBSScene>();
  323. source = obs_scene_getsource(scene);
  324. UpdateSources(scene);
  325. }
  326. /* TODO: allow transitions */
  327. obs_set_output_source(0, source);
  328. UNUSED_PARAMETER(prev);
  329. }
  330. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  331. {
  332. /* TODO */
  333. UNUSED_PARAMETER(pos);
  334. }
  335. void OBSBasic::on_actionAddScene_triggered()
  336. {
  337. string name;
  338. bool accepted = NameDialog::AskForName(this,
  339. QTStr("MainWindow.AddSceneDlg.Title"),
  340. QTStr("MainWindow.AddSceneDlg.Text"),
  341. name);
  342. if (accepted) {
  343. obs_source_t source = obs_get_source_by_name(name.c_str());
  344. if (source) {
  345. QMessageBox::information(this,
  346. QTStr("MainWindow.NameExists.Title"),
  347. QTStr("MainWindow.NameExists.Text"));
  348. obs_source_release(source);
  349. on_actionAddScene_triggered();
  350. return;
  351. }
  352. obs_scene_t scene = obs_scene_create(name.c_str());
  353. source = obs_scene_getsource(scene);
  354. obs_add_source(source);
  355. obs_scene_release(scene);
  356. obs_set_output_source(0, source);
  357. }
  358. }
  359. void OBSBasic::on_actionRemoveScene_triggered()
  360. {
  361. QListWidgetItem *item = ui->scenes->currentItem();
  362. if (!item)
  363. return;
  364. QVariant userData = item->data(Qt::UserRole);
  365. obs_scene_t scene = userData.value<OBSScene>();
  366. obs_source_t source = obs_scene_getsource(scene);
  367. obs_source_remove(source);
  368. }
  369. void OBSBasic::on_actionSceneProperties_triggered()
  370. {
  371. /* TODO */
  372. }
  373. void OBSBasic::on_actionSceneUp_triggered()
  374. {
  375. /* TODO */
  376. }
  377. void OBSBasic::on_actionSceneDown_triggered()
  378. {
  379. /* TODO */
  380. }
  381. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  382. QListWidgetItem *prev)
  383. {
  384. /* TODO */
  385. UNUSED_PARAMETER(current);
  386. UNUSED_PARAMETER(prev);
  387. }
  388. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  389. {
  390. /* TODO */
  391. UNUSED_PARAMETER(pos);
  392. }
  393. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  394. {
  395. string name;
  396. bool success = false;
  397. while (!success) {
  398. bool accepted = NameDialog::AskForName(this,
  399. Str("MainWindow.AddSourceDlg.Title"),
  400. Str("MainWindow.AddSourceDlg.Text"),
  401. name);
  402. if (!accepted)
  403. break;
  404. obs_source_t source = obs_get_source_by_name(name.c_str());
  405. if (!source) {
  406. success = true;
  407. } else {
  408. QMessageBox::information(this,
  409. QTStr("MainWindow.NameExists.Title"),
  410. QTStr("MainWindow.NameExists.Text"));
  411. obs_source_release(source);
  412. }
  413. }
  414. if (success) {
  415. obs_source_t source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  416. id, name.c_str(), NULL);
  417. sourceSceneRefs[source] = 0;
  418. obs_add_source(source);
  419. obs_scene_add(scene, source);
  420. obs_source_release(source);
  421. }
  422. }
  423. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  424. {
  425. OBSScene scene = GetCurrentScene();
  426. const char *type;
  427. bool foundValues = false;
  428. size_t idx = 0;
  429. if (!scene)
  430. return;
  431. QMenu popup;
  432. while (obs_enum_input_types(idx++, &type)) {
  433. const char *name = obs_source_getdisplayname(
  434. OBS_SOURCE_TYPE_INPUT,
  435. type, App()->GetLocale());
  436. QAction *popupItem = new QAction(QT_UTF8(name), this);
  437. popupItem->setData(QT_UTF8(type));
  438. popup.addAction(popupItem);
  439. foundValues = true;
  440. }
  441. if (foundValues) {
  442. QAction *ret = popup.exec(pos);
  443. if (ret)
  444. AddSource(scene, ret->data().toString().toUtf8());
  445. }
  446. }
  447. void OBSBasic::on_actionAddSource_triggered()
  448. {
  449. AddSourcePopupMenu(QCursor::pos());
  450. }
  451. void OBSBasic::on_actionRemoveSource_triggered()
  452. {
  453. OBSSceneItem item = GetCurrentSceneItem();
  454. if (item)
  455. obs_sceneitem_remove(item);
  456. }
  457. void OBSBasic::on_actionSourceProperties_triggered()
  458. {
  459. }
  460. void OBSBasic::on_actionSourceUp_triggered()
  461. {
  462. }
  463. void OBSBasic::on_actionSourceDown_triggered()
  464. {
  465. }
  466. void OBSBasic::on_recordButton_clicked()
  467. {
  468. if (outputTest) {
  469. obs_output_destroy(outputTest);
  470. outputTest = NULL;
  471. ui->recordButton->setText("Start Recording");
  472. } else {
  473. QString path = QFileDialog::getSaveFileName(this,
  474. "Please enter a file name", QString(),
  475. "Video Files (*.avi)");
  476. if (path.isNull() || path.isEmpty())
  477. return;
  478. obs_data_t data = obs_data_create();
  479. obs_data_setstring(data, "filename", QT_TO_UTF8(path));
  480. outputTest = obs_output_create("ffmpeg_output", "test", data);
  481. obs_data_release(data);
  482. if (!obs_output_start(outputTest)) {
  483. obs_output_destroy(outputTest);
  484. outputTest = NULL;
  485. return;
  486. }
  487. ui->recordButton->setText("Stop Recording");
  488. }
  489. }
  490. void OBSBasic::on_settingsButton_clicked()
  491. {
  492. OBSBasicSettings settings(this);
  493. settings.exec();
  494. }