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