window-basic-main.cpp 15 KB

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