window-basic-main.cpp 15 KB

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