window-basic-main.cpp 15 KB

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