window-basic-main.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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 <util/util.hpp>
  20. #include <util/platform.h>
  21. #include "obs-app.hpp"
  22. #include "platform.hpp"
  23. #include "window-basic-settings.hpp"
  24. #include "window-namedialog.hpp"
  25. #include "window-basic-main.hpp"
  26. #include "window-basic-properties.hpp"
  27. #include "qt-wrappers.hpp"
  28. #include "display-helpers.hpp"
  29. #include "ui_OBSBasic.h"
  30. #include <sstream>
  31. #include <QScreen>
  32. #include <QWindow>
  33. using namespace std;
  34. Q_DECLARE_METATYPE(OBSScene);
  35. Q_DECLARE_METATYPE(OBSSceneItem);
  36. OBSBasic::OBSBasic(QWidget *parent)
  37. : OBSMainWindow (parent),
  38. streamOutput (nullptr),
  39. service (nullptr),
  40. aac (nullptr),
  41. x264 (nullptr),
  42. sceneChanging (false),
  43. resizeTimer (0),
  44. properties (nullptr),
  45. ui (new Ui::OBSBasic)
  46. {
  47. ui->setupUi(this);
  48. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  49. struct obs_video_info ovi;
  50. if (obs_get_video_info(&ovi))
  51. ResizePreview(ovi.base_width, ovi.base_height);
  52. });
  53. }
  54. static obs_data_t GenerateSaveData()
  55. {
  56. obs_data_t saveData = obs_data_create();
  57. obs_data_array_t sourcesArray = obs_save_sources();
  58. obs_source_t currentScene = obs_get_output_source(0);
  59. const char *sceneName = obs_source_getname(currentScene);
  60. obs_data_setstring(saveData, "current_scene", sceneName);
  61. obs_data_setarray(saveData, "sources", sourcesArray);
  62. obs_data_array_release(sourcesArray);
  63. obs_source_release(currentScene);
  64. return saveData;
  65. }
  66. void OBSBasic::Save(const char *file)
  67. {
  68. obs_data_t saveData = GenerateSaveData();
  69. const char *jsonData = obs_data_getjson(saveData);
  70. /* TODO maybe a message box here? */
  71. if (!os_quick_write_utf8_file(file, jsonData, strlen(jsonData), false))
  72. blog(LOG_ERROR, "Could not save scene data to %s", file);
  73. obs_data_release(saveData);
  74. }
  75. void OBSBasic::Load(const char *file)
  76. {
  77. if (!file) {
  78. blog(LOG_ERROR, "Could not find file %s", file);
  79. return;
  80. }
  81. BPtr<char> jsonData = os_quick_read_utf8_file(file);
  82. if (!jsonData)
  83. return;
  84. obs_data_t data = obs_data_create_from_json(jsonData);
  85. obs_data_array_t sources = obs_data_getarray(data, "sources");
  86. const char *sceneName = obs_data_getstring(data, "current_scene");
  87. obs_source_t curScene;
  88. obs_load_sources(sources);
  89. curScene = obs_get_source_by_name(sceneName);
  90. obs_set_output_source(0, curScene);
  91. obs_source_release(curScene);
  92. obs_data_array_release(sources);
  93. obs_data_release(data);
  94. }
  95. static inline bool HasAudioDevices(const char *source_id)
  96. {
  97. const char *output_id = source_id;
  98. obs_properties_t props = obs_get_source_properties(
  99. OBS_SOURCE_TYPE_INPUT, output_id, App()->GetLocale());
  100. size_t count = 0;
  101. if (!props)
  102. return false;
  103. obs_property_t devices = obs_properties_get(props, "device_id");
  104. if (devices)
  105. count = obs_property_list_item_count(devices);
  106. obs_properties_destroy(props);
  107. return count != 0;
  108. }
  109. static void OBSStartStreaming(void *data, calldata_t params)
  110. {
  111. UNUSED_PARAMETER(params);
  112. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  113. "StreamingStart");
  114. }
  115. static void OBSStopStreaming(void *data, calldata_t params)
  116. {
  117. int code = (int)calldata_int(params, "errorcode");
  118. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  119. "StreamingStop", Q_ARG(int, code));
  120. }
  121. #define SERVICE_PATH "obs-studio/basic/service.json"
  122. void OBSBasic::SaveService()
  123. {
  124. if (!service)
  125. return;
  126. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  127. if (!serviceJsonPath)
  128. return;
  129. obs_data_t data = obs_data_create();
  130. obs_data_t settings = obs_service_get_settings(service);
  131. obs_data_setstring(data, "type", obs_service_gettype(service));
  132. obs_data_setobj(data, "settings", settings);
  133. const char *json = obs_data_getjson(data);
  134. os_quick_write_utf8_file(serviceJsonPath, json, strlen(json), false);
  135. obs_data_release(settings);
  136. obs_data_release(data);
  137. }
  138. bool OBSBasic::LoadService()
  139. {
  140. const char *type;
  141. BPtr<char> serviceJsonPath(os_get_config_path(SERVICE_PATH));
  142. if (!serviceJsonPath)
  143. return false;
  144. BPtr<char> jsonText = os_quick_read_utf8_file(serviceJsonPath);
  145. if (!jsonText)
  146. return false;
  147. obs_data_t data = obs_data_create_from_json(jsonText);
  148. obs_data_set_default_string(data, "type", "rtmp_common");
  149. type = obs_data_getstring(data, "type");
  150. obs_data_t settings = obs_data_getobj(data, "settings");
  151. service = obs_service_create(type, "default", settings);
  152. obs_data_release(settings);
  153. obs_data_release(data);
  154. return !!service;
  155. }
  156. bool OBSBasic::InitOutputs()
  157. {
  158. streamOutput = obs_output_create("rtmp_output", "default", nullptr);
  159. if (!streamOutput)
  160. return false;
  161. signal_handler_connect(obs_output_signalhandler(streamOutput),
  162. "start", OBSStartStreaming, this);
  163. signal_handler_connect(obs_output_signalhandler(streamOutput),
  164. "stop", OBSStopStreaming, this);
  165. return true;
  166. }
  167. bool OBSBasic::InitEncoders()
  168. {
  169. aac = obs_audio_encoder_create("ffmpeg_aac", "aac", nullptr);
  170. if (!aac)
  171. return false;
  172. x264 = obs_video_encoder_create("obs_x264", "h264", nullptr);
  173. if (!x264)
  174. return false;
  175. return true;
  176. }
  177. bool OBSBasic::InitService()
  178. {
  179. if (LoadService())
  180. return true;
  181. service = obs_service_create("rtmp_common", nullptr, nullptr);
  182. if (!service)
  183. return false;
  184. return true;
  185. }
  186. bool OBSBasic::InitBasicConfigDefaults()
  187. {
  188. bool hasDesktopAudio = HasAudioDevices(App()->OutputAudioSource());
  189. bool hasInputAudio = HasAudioDevices(App()->InputAudioSource());
  190. config_set_default_int(basicConfig, "Window", "PosX", -1);
  191. config_set_default_int(basicConfig, "Window", "PosY", -1);
  192. config_set_default_int(basicConfig, "Window", "SizeX", -1);
  193. config_set_default_int(basicConfig, "Window", "SizeY", -1);
  194. vector<MonitorInfo> monitors;
  195. GetMonitors(monitors);
  196. if (!monitors.size()) {
  197. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  198. "technically shouldn't be possible.");
  199. return false;
  200. }
  201. uint32_t cx = monitors[0].cx;
  202. uint32_t cy = monitors[0].cy;
  203. /* TODO: temporary */
  204. config_set_default_string(basicConfig, "SimpleOutput", "path", "");
  205. config_set_default_uint (basicConfig, "SimpleOutput", "VBitrate",
  206. 2500);
  207. config_set_default_uint (basicConfig, "SimpleOutput", "ABitrate", 128);
  208. config_set_default_uint (basicConfig, "Video", "BaseCX", cx);
  209. config_set_default_uint (basicConfig, "Video", "BaseCY", cy);
  210. cx = cx * 10 / 15;
  211. cy = cy * 10 / 15;
  212. config_set_default_uint (basicConfig, "Video", "OutputCX", cx);
  213. config_set_default_uint (basicConfig, "Video", "OutputCY", cy);
  214. config_set_default_uint (basicConfig, "Video", "FPSType", 0);
  215. config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
  216. config_set_default_uint (basicConfig, "Video", "FPSInt", 30);
  217. config_set_default_uint (basicConfig, "Video", "FPSNum", 30);
  218. config_set_default_uint (basicConfig, "Video", "FPSDen", 1);
  219. config_set_default_uint (basicConfig, "Audio", "SampleRate", 44100);
  220. config_set_default_string(basicConfig, "Audio", "ChannelSetup",
  221. "Stereo");
  222. config_set_default_uint (basicConfig, "Audio", "BufferingTime", 1000);
  223. config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
  224. hasDesktopAudio ? "default" : "disabled");
  225. config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
  226. "disabled");
  227. config_set_default_string(basicConfig, "Audio", "AuxDevice1",
  228. hasInputAudio ? "default" : "disabled");
  229. config_set_default_string(basicConfig, "Audio", "AuxDevice2",
  230. "disabled");
  231. config_set_default_string(basicConfig, "Audio", "AuxDevice3",
  232. "disabled");
  233. return true;
  234. }
  235. bool OBSBasic::InitBasicConfig()
  236. {
  237. BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini"));
  238. int errorcode = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  239. if (errorcode != CONFIG_SUCCESS) {
  240. OBSErrorBox(NULL, "Failed to open basic.ini: %d", errorcode);
  241. return false;
  242. }
  243. return InitBasicConfigDefaults();
  244. }
  245. void OBSBasic::OBSInit()
  246. {
  247. /* make sure it's fully displayed before doing any initialization */
  248. show();
  249. App()->processEvents();
  250. if (!obs_startup())
  251. throw "Failed to initialize libobs";
  252. if (!InitBasicConfig())
  253. throw "Failed to load basic.ini";
  254. if (!ResetVideo())
  255. throw "Failed to initialize video";
  256. if (!ResetAudio())
  257. throw "Failed to initialize audio";
  258. signal_handler_connect(obs_signalhandler(), "source_add",
  259. OBSBasic::SourceAdded, this);
  260. signal_handler_connect(obs_signalhandler(), "source_remove",
  261. OBSBasic::SourceRemoved, this);
  262. signal_handler_connect(obs_signalhandler(), "channel_change",
  263. OBSBasic::ChannelChanged, this);
  264. /* TODO: this is a test, all modules will be searched for and loaded
  265. * automatically later */
  266. obs_load_module("test-input");
  267. obs_load_module("obs-ffmpeg");
  268. obs_load_module("obs-x264");
  269. obs_load_module("obs-outputs");
  270. obs_load_module("rtmp-services");
  271. #ifdef __APPLE__
  272. obs_load_module("mac-avcapture");
  273. obs_load_module("mac-capture");
  274. #elif _WIN32
  275. obs_load_module("win-wasapi");
  276. obs_load_module("win-capture");
  277. #else
  278. obs_load_module("linux-xshm");
  279. obs_load_module("linux-pulseaudio");
  280. #endif
  281. if (!InitOutputs())
  282. throw "Failed to initialize outputs";
  283. if (!InitEncoders())
  284. throw "Failed to initialize encoders";
  285. if (!InitService())
  286. throw "Failed to initialize service";
  287. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  288. Load(savePath);
  289. ResetAudioDevices();
  290. }
  291. OBSBasic::~OBSBasic()
  292. {
  293. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  294. SaveService();
  295. Save(savePath);
  296. if (properties)
  297. delete properties;
  298. /* free the lists before shutting down to remove the scene/item
  299. * references */
  300. ui->sources->clear();
  301. ui->scenes->clear();
  302. obs_shutdown();
  303. }
  304. OBSScene OBSBasic::GetCurrentScene()
  305. {
  306. QListWidgetItem *item = ui->scenes->currentItem();
  307. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  308. }
  309. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  310. {
  311. QListWidgetItem *item = ui->sources->currentItem();
  312. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  313. }
  314. void OBSBasic::UpdateSources(OBSScene scene)
  315. {
  316. ui->sources->clear();
  317. obs_scene_enum_items(scene,
  318. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  319. {
  320. OBSBasic *window = static_cast<OBSBasic*>(p);
  321. window->InsertSceneItem(item);
  322. UNUSED_PARAMETER(scene);
  323. return true;
  324. }, this);
  325. }
  326. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  327. {
  328. obs_source_t source = obs_sceneitem_getsource(item);
  329. const char *name = obs_source_getname(source);
  330. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  331. listItem->setData(Qt::UserRole,
  332. QVariant::fromValue(OBSSceneItem(item)));
  333. ui->sources->insertItem(0, listItem);
  334. }
  335. /* Qt callbacks for invokeMethod */
  336. void OBSBasic::AddScene(OBSSource source)
  337. {
  338. const char *name = obs_source_getname(source);
  339. obs_scene_t scene = obs_scene_fromsource(source);
  340. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  341. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  342. ui->scenes->addItem(item);
  343. signal_handler_t handler = obs_source_signalhandler(source);
  344. signal_handler_connect(handler, "item_add",
  345. OBSBasic::SceneItemAdded, this);
  346. signal_handler_connect(handler, "item_remove",
  347. OBSBasic::SceneItemRemoved, this);
  348. }
  349. void OBSBasic::RemoveScene(OBSSource source)
  350. {
  351. const char *name = obs_source_getname(source);
  352. QListWidgetItem *sel = ui->scenes->currentItem();
  353. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  354. Qt::MatchExactly);
  355. if (sel != nullptr) {
  356. if (items.contains(sel))
  357. ui->sources->clear();
  358. delete sel;
  359. }
  360. }
  361. void OBSBasic::AddSceneItem(OBSSceneItem item)
  362. {
  363. obs_scene_t scene = obs_sceneitem_getscene(item);
  364. obs_source_t source = obs_sceneitem_getsource(item);
  365. if (GetCurrentScene() == scene)
  366. InsertSceneItem(item);
  367. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  368. }
  369. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  370. {
  371. obs_scene_t scene = obs_sceneitem_getscene(item);
  372. if (GetCurrentScene() == scene) {
  373. for (int i = 0; i < ui->sources->count(); i++) {
  374. QListWidgetItem *listItem = ui->sources->item(i);
  375. QVariant userData = listItem->data(Qt::UserRole);
  376. if (userData.value<OBSSceneItem>() == item) {
  377. delete listItem;
  378. break;
  379. }
  380. }
  381. }
  382. obs_source_t source = obs_sceneitem_getsource(item);
  383. int scenes = sourceSceneRefs[source] - 1;
  384. if (scenes == 0) {
  385. obs_source_remove(source);
  386. sourceSceneRefs.erase(source);
  387. }
  388. }
  389. void OBSBasic::UpdateSceneSelection(OBSSource source)
  390. {
  391. if (source) {
  392. obs_source_type type;
  393. obs_source_gettype(source, &type, NULL);
  394. obs_scene_t scene = obs_scene_fromsource(source);
  395. const char *name = obs_source_getname(source);
  396. if (!scene)
  397. return;
  398. QList<QListWidgetItem*> items =
  399. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  400. if (items.count()) {
  401. sceneChanging = true;
  402. ui->scenes->setCurrentItem(items.first());
  403. sceneChanging = false;
  404. UpdateSources(scene);
  405. }
  406. }
  407. }
  408. /* OBS Callbacks */
  409. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  410. {
  411. OBSBasic *window = static_cast<OBSBasic*>(data);
  412. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  413. QMetaObject::invokeMethod(window, "AddSceneItem",
  414. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  415. }
  416. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  417. {
  418. OBSBasic *window = static_cast<OBSBasic*>(data);
  419. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  420. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  421. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  422. }
  423. void OBSBasic::SourceAdded(void *data, calldata_t params)
  424. {
  425. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  426. if (obs_scene_fromsource(source) != NULL)
  427. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  428. "AddScene",
  429. Q_ARG(OBSSource, OBSSource(source)));
  430. }
  431. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  432. {
  433. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  434. if (obs_scene_fromsource(source) != NULL)
  435. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  436. "RemoveScene",
  437. Q_ARG(OBSSource, OBSSource(source)));
  438. }
  439. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  440. {
  441. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  442. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  443. if (channel == 0)
  444. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  445. "UpdateSceneSelection",
  446. Q_ARG(OBSSource, OBSSource(source)));
  447. }
  448. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  449. {
  450. OBSBasic *window = static_cast<OBSBasic*>(data);
  451. obs_video_info ovi;
  452. int newCX, newCY;
  453. obs_get_video_info(&ovi);
  454. newCX = int(window->previewScale * float(ovi.base_width));
  455. newCY = int(window->previewScale * float(ovi.base_height));
  456. gs_viewport_push();
  457. gs_projection_push();
  458. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  459. -100.0f, 100.0f);
  460. gs_setviewport(window->previewX, window->previewY, newCX, newCY);
  461. obs_render_main_view();
  462. gs_projection_pop();
  463. gs_viewport_pop();
  464. UNUSED_PARAMETER(cx);
  465. UNUSED_PARAMETER(cy);
  466. }
  467. /* Main class functions */
  468. obs_service_t OBSBasic::GetService()
  469. {
  470. if (!service)
  471. service = obs_service_create("rtmp_common", NULL, NULL);
  472. return service;
  473. }
  474. void OBSBasic::SetService(obs_service_t newService)
  475. {
  476. if (newService) {
  477. if (service)
  478. obs_service_destroy(service);
  479. service = newService;
  480. }
  481. }
  482. bool OBSBasic::ResetVideo()
  483. {
  484. struct obs_video_info ovi;
  485. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  486. ovi.graphics_module = App()->GetRenderModule();
  487. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  488. "Video", "BaseCX");
  489. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  490. "Video", "BaseCY");
  491. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  492. "Video", "OutputCX");
  493. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  494. "Video", "OutputCY");
  495. ovi.output_format = VIDEO_FORMAT_NV12;
  496. ovi.adapter = 0;
  497. ovi.gpu_conversion = true;
  498. QTToGSWindow(ui->preview->winId(), ovi.window);
  499. //required to make opengl display stuff on osx(?)
  500. ResizePreview(ovi.base_width, ovi.base_height);
  501. QSize size = GetPixelSize(ui->preview);
  502. ovi.window_width = size.width();
  503. ovi.window_height = size.height();
  504. if (!obs_reset_video(&ovi))
  505. return false;
  506. obs_add_draw_callback(OBSBasic::RenderMain, this);
  507. return true;
  508. }
  509. bool OBSBasic::ResetAudio()
  510. {
  511. struct audio_output_info ai;
  512. ai.name = "Main Audio Track";
  513. ai.format = AUDIO_FORMAT_FLOAT;
  514. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  515. "SampleRate");
  516. const char *channelSetupStr = config_get_string(basicConfig,
  517. "Audio", "ChannelSetup");
  518. if (strcmp(channelSetupStr, "Mono") == 0)
  519. ai.speakers = SPEAKERS_MONO;
  520. else
  521. ai.speakers = SPEAKERS_STEREO;
  522. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  523. return obs_reset_audio(&ai);
  524. }
  525. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  526. int channel)
  527. {
  528. const char *deviceId = config_get_string(basicConfig, "Audio",
  529. deviceName);
  530. obs_source_t source;
  531. obs_data_t settings;
  532. bool same = false;
  533. source = obs_get_output_source(channel);
  534. if (source) {
  535. settings = obs_source_getsettings(source);
  536. const char *curId = obs_data_getstring(settings, "device_id");
  537. same = (strcmp(curId, deviceId) == 0);
  538. obs_data_release(settings);
  539. obs_source_release(source);
  540. }
  541. if (!same)
  542. obs_set_output_source(channel, nullptr);
  543. if (!same && strcmp(deviceId, "disabled") != 0) {
  544. obs_data_t settings = obs_data_create();
  545. obs_data_setstring(settings, "device_id", deviceId);
  546. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  547. sourceId, deviceName, settings);
  548. obs_data_release(settings);
  549. obs_set_output_source(channel, source);
  550. obs_source_release(source);
  551. }
  552. }
  553. void OBSBasic::ResetAudioDevices()
  554. {
  555. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1", 1);
  556. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2", 2);
  557. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1", 3);
  558. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2", 4);
  559. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3", 5);
  560. }
  561. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  562. {
  563. QSize targetSize;
  564. /* resize preview panel to fix to the top section of the window */
  565. targetSize = GetPixelSize(ui->preview);
  566. GetScaleAndCenterPos(int(cx), int(cy),
  567. targetSize.width(), targetSize.height(),
  568. previewX, previewY, previewScale);
  569. if (isVisible()) {
  570. if (resizeTimer)
  571. killTimer(resizeTimer);
  572. resizeTimer = startTimer(100);
  573. }
  574. }
  575. void OBSBasic::closeEvent(QCloseEvent *event)
  576. {
  577. QWidget::closeEvent(event);
  578. if (!event->isAccepted())
  579. return;
  580. // remove draw callback in case our drawable surfaces go away before
  581. // the destructor gets called
  582. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  583. }
  584. void OBSBasic::changeEvent(QEvent *event)
  585. {
  586. /* TODO */
  587. UNUSED_PARAMETER(event);
  588. }
  589. void OBSBasic::resizeEvent(QResizeEvent *event)
  590. {
  591. struct obs_video_info ovi;
  592. if (obs_get_video_info(&ovi))
  593. ResizePreview(ovi.base_width, ovi.base_height);
  594. UNUSED_PARAMETER(event);
  595. }
  596. void OBSBasic::timerEvent(QTimerEvent *event)
  597. {
  598. if (event->timerId() == resizeTimer) {
  599. killTimer(resizeTimer);
  600. resizeTimer = 0;
  601. QSize size = GetPixelSize(ui->preview);
  602. obs_resize(size.width(), size.height());
  603. }
  604. }
  605. void OBSBasic::on_action_New_triggered()
  606. {
  607. /* TODO */
  608. }
  609. void OBSBasic::on_action_Open_triggered()
  610. {
  611. /* TODO */
  612. }
  613. void OBSBasic::on_action_Save_triggered()
  614. {
  615. /* TODO */
  616. }
  617. void OBSBasic::on_action_Settings_triggered()
  618. {
  619. OBSBasicSettings settings(this);
  620. settings.exec();
  621. }
  622. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  623. QListWidgetItem *prev)
  624. {
  625. obs_source_t source = NULL;
  626. if (sceneChanging)
  627. return;
  628. if (current) {
  629. obs_scene_t scene;
  630. scene = current->data(Qt::UserRole).value<OBSScene>();
  631. source = obs_scene_getsource(scene);
  632. }
  633. /* TODO: allow transitions */
  634. obs_set_output_source(0, source);
  635. UNUSED_PARAMETER(prev);
  636. }
  637. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  638. {
  639. /* TODO */
  640. UNUSED_PARAMETER(pos);
  641. }
  642. void OBSBasic::on_actionAddScene_triggered()
  643. {
  644. string name;
  645. bool accepted = NameDialog::AskForName(this,
  646. QTStr("MainWindow.AddSceneDlg.Title"),
  647. QTStr("MainWindow.AddSceneDlg.Text"),
  648. name);
  649. if (accepted) {
  650. if (name.empty()) {
  651. QMessageBox::information(this,
  652. QTStr("MainWindow.NoNameEntered"),
  653. QTStr("MainWindow.NoNameEntered"));
  654. on_actionAddScene_triggered();
  655. return;
  656. }
  657. obs_source_t source = obs_get_source_by_name(name.c_str());
  658. if (source) {
  659. QMessageBox::information(this,
  660. QTStr("MainWindow.NameExists.Title"),
  661. QTStr("MainWindow.NameExists.Text"));
  662. obs_source_release(source);
  663. on_actionAddScene_triggered();
  664. return;
  665. }
  666. obs_scene_t scene = obs_scene_create(name.c_str());
  667. source = obs_scene_getsource(scene);
  668. obs_add_source(source);
  669. obs_scene_release(scene);
  670. obs_set_output_source(0, source);
  671. }
  672. }
  673. void OBSBasic::on_actionRemoveScene_triggered()
  674. {
  675. QListWidgetItem *item = ui->scenes->currentItem();
  676. if (!item)
  677. return;
  678. QVariant userData = item->data(Qt::UserRole);
  679. obs_scene_t scene = userData.value<OBSScene>();
  680. obs_source_t source = obs_scene_getsource(scene);
  681. obs_source_remove(source);
  682. }
  683. void OBSBasic::on_actionSceneProperties_triggered()
  684. {
  685. /* TODO */
  686. }
  687. void OBSBasic::on_actionSceneUp_triggered()
  688. {
  689. /* TODO */
  690. }
  691. void OBSBasic::on_actionSceneDown_triggered()
  692. {
  693. /* TODO */
  694. }
  695. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  696. QListWidgetItem *prev)
  697. {
  698. /* TODO */
  699. UNUSED_PARAMETER(current);
  700. UNUSED_PARAMETER(prev);
  701. }
  702. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  703. {
  704. /* TODO */
  705. UNUSED_PARAMETER(pos);
  706. }
  707. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  708. {
  709. string name;
  710. bool success = false;
  711. while (!success) {
  712. bool accepted = NameDialog::AskForName(this,
  713. Str("MainWindow.AddSourceDlg.Title"),
  714. Str("MainWindow.AddSourceDlg.Text"),
  715. name);
  716. if (!accepted)
  717. break;
  718. if (name.empty()) {
  719. QMessageBox::information(this,
  720. QTStr("MainWindow.NoNameEntered"),
  721. QTStr("MainWindow.NoNameEntered"));
  722. continue;
  723. }
  724. obs_source_t source = obs_get_source_by_name(name.c_str());
  725. if (!source) {
  726. success = true;
  727. break;
  728. } else {
  729. QMessageBox::information(this,
  730. QTStr("MainWindow.NameExists.Title"),
  731. QTStr("MainWindow.NameExists.Text"));
  732. obs_source_release(source);
  733. }
  734. }
  735. if (success) {
  736. obs_source_t source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  737. id, name.c_str(), NULL);
  738. sourceSceneRefs[source] = 0;
  739. obs_add_source(source);
  740. obs_scene_add(scene, source);
  741. obs_source_release(source);
  742. }
  743. }
  744. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  745. {
  746. OBSScene scene = GetCurrentScene();
  747. const char *type;
  748. bool foundValues = false;
  749. size_t idx = 0;
  750. if (!scene)
  751. return;
  752. QMenu popup;
  753. while (obs_enum_input_types(idx++, &type)) {
  754. const char *name = obs_source_getdisplayname(
  755. OBS_SOURCE_TYPE_INPUT,
  756. type, App()->GetLocale());
  757. if (strcmp(type, "scene") == 0)
  758. continue;
  759. if (strcmp(type, App()->InputAudioSource()) == 0)
  760. continue;
  761. if (strcmp(type, App()->OutputAudioSource()) == 0)
  762. continue;
  763. QAction *popupItem = new QAction(QT_UTF8(name), this);
  764. popupItem->setData(QT_UTF8(type));
  765. popup.addAction(popupItem);
  766. foundValues = true;
  767. }
  768. if (foundValues) {
  769. QAction *ret = popup.exec(pos);
  770. if (ret)
  771. AddSource(scene, ret->data().toString().toUtf8());
  772. }
  773. }
  774. void OBSBasic::on_actionAddSource_triggered()
  775. {
  776. AddSourcePopupMenu(QCursor::pos());
  777. }
  778. void OBSBasic::on_actionRemoveSource_triggered()
  779. {
  780. OBSSceneItem item = GetCurrentSceneItem();
  781. if (item)
  782. obs_sceneitem_remove(item);
  783. }
  784. void OBSBasic::on_actionSourceProperties_triggered()
  785. {
  786. OBSSceneItem item = GetCurrentSceneItem();
  787. OBSSource source = obs_sceneitem_getsource(item);
  788. if (source) {
  789. delete properties;
  790. properties = new OBSBasicProperties(this, source);
  791. properties->Init();
  792. }
  793. }
  794. void OBSBasic::on_actionSourceUp_triggered()
  795. {
  796. }
  797. void OBSBasic::on_actionSourceDown_triggered()
  798. {
  799. }
  800. void OBSBasic::StreamingStart()
  801. {
  802. ui->streamButton->setText("Stop Streaming");
  803. }
  804. void OBSBasic::StreamingStop(int errorcode)
  805. {
  806. UNUSED_PARAMETER(errorcode);
  807. ui->streamButton->setText("Start Streaming");
  808. }
  809. void OBSBasic::on_streamButton_clicked()
  810. {
  811. if (obs_output_active(streamOutput)) {
  812. obs_output_stop(streamOutput);
  813. } else {
  814. obs_data_t x264Settings = obs_data_create();
  815. obs_data_t aacSettings = obs_data_create();
  816. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  817. "VBitrate");
  818. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  819. "ABitrate");
  820. SaveService();
  821. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  822. obs_data_setbool(x264Settings, "cbr", true);
  823. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  824. obs_encoder_update(x264, x264Settings);
  825. obs_encoder_update(aac, aacSettings);
  826. obs_data_release(x264Settings);
  827. obs_data_release(aacSettings);
  828. obs_encoder_set_video(x264, obs_video());
  829. obs_encoder_set_audio(aac, obs_audio());
  830. obs_output_set_video_encoder(streamOutput, x264);
  831. obs_output_set_audio_encoder(streamOutput, aac);
  832. obs_output_set_service(streamOutput, service);
  833. obs_output_start(streamOutput);
  834. }
  835. }
  836. void OBSBasic::on_settingsButton_clicked()
  837. {
  838. OBSBasicSettings settings(this);
  839. settings.exec();
  840. }
  841. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  842. {
  843. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  844. if (strcmp(val, "10") == 0) {
  845. num = 10;
  846. den = 1;
  847. } else if (strcmp(val, "20") == 0) {
  848. num = 20;
  849. den = 1;
  850. } else if (strcmp(val, "25") == 0) {
  851. num = 25;
  852. den = 1;
  853. } else if (strcmp(val, "29.97") == 0) {
  854. num = 30000;
  855. den = 1001;
  856. } else if (strcmp(val, "48") == 0) {
  857. num = 48;
  858. den = 1;
  859. } else if (strcmp(val, "59.94") == 0) {
  860. num = 60000;
  861. den = 1001;
  862. } else if (strcmp(val, "60") == 0) {
  863. num = 60;
  864. den = 1;
  865. } else {
  866. num = 30;
  867. den = 1;
  868. }
  869. }
  870. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  871. {
  872. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  873. den = 1;
  874. }
  875. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  876. {
  877. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  878. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  879. }
  880. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  881. {
  882. num = 1000000000;
  883. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  884. }
  885. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  886. {
  887. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  888. if (type == 1) //"Integer"
  889. GetFPSInteger(num, den);
  890. else if (type == 2) //"Fraction"
  891. GetFPSFraction(num, den);
  892. else if (false) //"Nanoseconds", currently not implemented
  893. GetFPSNanoseconds(num, den);
  894. else
  895. GetFPSCommon(num, den);
  896. }
  897. config_t OBSBasic::Config() const
  898. {
  899. return basicConfig;
  900. }