window-basic-main.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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-capture");
  273. #elif _WIN32
  274. obs_load_module("win-wasapi");
  275. obs_load_module("win-capture");
  276. #else
  277. obs_load_module("linux-xshm");
  278. obs_load_module("linux-pulseaudio");
  279. #endif
  280. if (!InitOutputs())
  281. throw "Failed to initialize outputs";
  282. if (!InitEncoders())
  283. throw "Failed to initialize encoders";
  284. if (!InitService())
  285. throw "Failed to initialize service";
  286. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  287. Load(savePath);
  288. ResetAudioDevices();
  289. }
  290. OBSBasic::~OBSBasic()
  291. {
  292. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  293. SaveService();
  294. Save(savePath);
  295. if (properties)
  296. delete properties;
  297. /* free the lists before shutting down to remove the scene/item
  298. * references */
  299. ui->sources->clear();
  300. ui->scenes->clear();
  301. obs_shutdown();
  302. }
  303. OBSScene OBSBasic::GetCurrentScene()
  304. {
  305. QListWidgetItem *item = ui->scenes->currentItem();
  306. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  307. }
  308. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  309. {
  310. QListWidgetItem *item = ui->sources->currentItem();
  311. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  312. }
  313. void OBSBasic::UpdateSources(OBSScene scene)
  314. {
  315. ui->sources->clear();
  316. obs_scene_enum_items(scene,
  317. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  318. {
  319. OBSBasic *window = static_cast<OBSBasic*>(p);
  320. window->InsertSceneItem(item);
  321. UNUSED_PARAMETER(scene);
  322. return true;
  323. }, this);
  324. }
  325. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  326. {
  327. obs_source_t source = obs_sceneitem_getsource(item);
  328. const char *name = obs_source_getname(source);
  329. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  330. listItem->setData(Qt::UserRole,
  331. QVariant::fromValue(OBSSceneItem(item)));
  332. ui->sources->insertItem(0, listItem);
  333. }
  334. /* Qt callbacks for invokeMethod */
  335. void OBSBasic::AddScene(OBSSource source)
  336. {
  337. const char *name = obs_source_getname(source);
  338. obs_scene_t scene = obs_scene_fromsource(source);
  339. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  340. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  341. ui->scenes->addItem(item);
  342. signal_handler_t handler = obs_source_signalhandler(source);
  343. signal_handler_connect(handler, "item_add",
  344. OBSBasic::SceneItemAdded, this);
  345. signal_handler_connect(handler, "item_remove",
  346. OBSBasic::SceneItemRemoved, this);
  347. }
  348. void OBSBasic::RemoveScene(OBSSource source)
  349. {
  350. const char *name = obs_source_getname(source);
  351. QListWidgetItem *sel = ui->scenes->currentItem();
  352. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  353. Qt::MatchExactly);
  354. if (sel != nullptr) {
  355. if (items.contains(sel))
  356. ui->sources->clear();
  357. delete sel;
  358. }
  359. }
  360. void OBSBasic::AddSceneItem(OBSSceneItem item)
  361. {
  362. obs_scene_t scene = obs_sceneitem_getscene(item);
  363. obs_source_t source = obs_sceneitem_getsource(item);
  364. if (GetCurrentScene() == scene)
  365. InsertSceneItem(item);
  366. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  367. }
  368. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  369. {
  370. obs_scene_t scene = obs_sceneitem_getscene(item);
  371. if (GetCurrentScene() == scene) {
  372. for (int i = 0; i < ui->sources->count(); i++) {
  373. QListWidgetItem *listItem = ui->sources->item(i);
  374. QVariant userData = listItem->data(Qt::UserRole);
  375. if (userData.value<OBSSceneItem>() == item) {
  376. delete listItem;
  377. break;
  378. }
  379. }
  380. }
  381. obs_source_t source = obs_sceneitem_getsource(item);
  382. int scenes = sourceSceneRefs[source] - 1;
  383. if (scenes == 0) {
  384. obs_source_remove(source);
  385. sourceSceneRefs.erase(source);
  386. }
  387. }
  388. void OBSBasic::UpdateSceneSelection(OBSSource source)
  389. {
  390. if (source) {
  391. obs_source_type type;
  392. obs_source_gettype(source, &type, NULL);
  393. obs_scene_t scene = obs_scene_fromsource(source);
  394. const char *name = obs_source_getname(source);
  395. if (!scene)
  396. return;
  397. QList<QListWidgetItem*> items =
  398. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  399. if (items.count()) {
  400. sceneChanging = true;
  401. ui->scenes->setCurrentItem(items.first());
  402. sceneChanging = false;
  403. UpdateSources(scene);
  404. }
  405. }
  406. }
  407. /* OBS Callbacks */
  408. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  409. {
  410. OBSBasic *window = static_cast<OBSBasic*>(data);
  411. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  412. QMetaObject::invokeMethod(window, "AddSceneItem",
  413. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  414. }
  415. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  416. {
  417. OBSBasic *window = static_cast<OBSBasic*>(data);
  418. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  419. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  420. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  421. }
  422. void OBSBasic::SourceAdded(void *data, calldata_t params)
  423. {
  424. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  425. if (obs_scene_fromsource(source) != NULL)
  426. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  427. "AddScene",
  428. Q_ARG(OBSSource, OBSSource(source)));
  429. }
  430. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  431. {
  432. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  433. if (obs_scene_fromsource(source) != NULL)
  434. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  435. "RemoveScene",
  436. Q_ARG(OBSSource, OBSSource(source)));
  437. }
  438. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  439. {
  440. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  441. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  442. if (channel == 0)
  443. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  444. "UpdateSceneSelection",
  445. Q_ARG(OBSSource, OBSSource(source)));
  446. }
  447. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  448. {
  449. OBSBasic *window = static_cast<OBSBasic*>(data);
  450. obs_video_info ovi;
  451. int newCX, newCY;
  452. obs_get_video_info(&ovi);
  453. newCX = int(window->previewScale * float(ovi.base_width));
  454. newCY = int(window->previewScale * float(ovi.base_height));
  455. gs_viewport_push();
  456. gs_projection_push();
  457. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  458. -100.0f, 100.0f);
  459. gs_setviewport(window->previewX, window->previewY, newCX, newCY);
  460. obs_render_main_view();
  461. gs_projection_pop();
  462. gs_viewport_pop();
  463. UNUSED_PARAMETER(cx);
  464. UNUSED_PARAMETER(cy);
  465. }
  466. /* Main class functions */
  467. obs_service_t OBSBasic::GetService()
  468. {
  469. if (!service)
  470. service = obs_service_create("rtmp_common", NULL, NULL);
  471. return service;
  472. }
  473. void OBSBasic::SetService(obs_service_t newService)
  474. {
  475. if (newService) {
  476. if (service)
  477. obs_service_destroy(service);
  478. service = newService;
  479. }
  480. }
  481. bool OBSBasic::ResetVideo()
  482. {
  483. struct obs_video_info ovi;
  484. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  485. ovi.graphics_module = App()->GetRenderModule();
  486. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  487. "Video", "BaseCX");
  488. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  489. "Video", "BaseCY");
  490. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  491. "Video", "OutputCX");
  492. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  493. "Video", "OutputCY");
  494. ovi.output_format = VIDEO_FORMAT_NV12;
  495. ovi.adapter = 0;
  496. ovi.gpu_conversion = true;
  497. QTToGSWindow(ui->preview->winId(), ovi.window);
  498. //required to make opengl display stuff on osx(?)
  499. ResizePreview(ovi.base_width, ovi.base_height);
  500. QSize size = GetPixelSize(ui->preview);
  501. ovi.window_width = size.width();
  502. ovi.window_height = size.height();
  503. if (!obs_reset_video(&ovi))
  504. return false;
  505. obs_add_draw_callback(OBSBasic::RenderMain, this);
  506. return true;
  507. }
  508. bool OBSBasic::ResetAudio()
  509. {
  510. struct audio_output_info ai;
  511. ai.name = "Main Audio Track";
  512. ai.format = AUDIO_FORMAT_FLOAT;
  513. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  514. "SampleRate");
  515. const char *channelSetupStr = config_get_string(basicConfig,
  516. "Audio", "ChannelSetup");
  517. if (strcmp(channelSetupStr, "Mono") == 0)
  518. ai.speakers = SPEAKERS_MONO;
  519. else
  520. ai.speakers = SPEAKERS_STEREO;
  521. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  522. return obs_reset_audio(&ai);
  523. }
  524. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  525. int channel)
  526. {
  527. const char *deviceId = config_get_string(basicConfig, "Audio",
  528. deviceName);
  529. obs_source_t source;
  530. obs_data_t settings;
  531. bool same = false;
  532. source = obs_get_output_source(channel);
  533. if (source) {
  534. settings = obs_source_getsettings(source);
  535. const char *curId = obs_data_getstring(settings, "device_id");
  536. same = (strcmp(curId, deviceId) == 0);
  537. obs_data_release(settings);
  538. obs_source_release(source);
  539. }
  540. if (!same)
  541. obs_set_output_source(channel, nullptr);
  542. if (!same && strcmp(deviceId, "disabled") != 0) {
  543. obs_data_t settings = obs_data_create();
  544. obs_data_setstring(settings, "device_id", deviceId);
  545. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  546. sourceId, deviceName, settings);
  547. obs_data_release(settings);
  548. obs_set_output_source(channel, source);
  549. obs_source_release(source);
  550. }
  551. }
  552. void OBSBasic::ResetAudioDevices()
  553. {
  554. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1", 1);
  555. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2", 2);
  556. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1", 3);
  557. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2", 4);
  558. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3", 5);
  559. }
  560. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  561. {
  562. QSize targetSize;
  563. /* resize preview panel to fix to the top section of the window */
  564. targetSize = GetPixelSize(ui->preview);
  565. GetScaleAndCenterPos(int(cx), int(cy),
  566. targetSize.width(), targetSize.height(),
  567. previewX, previewY, previewScale);
  568. if (isVisible()) {
  569. if (resizeTimer)
  570. killTimer(resizeTimer);
  571. resizeTimer = startTimer(100);
  572. }
  573. }
  574. void OBSBasic::closeEvent(QCloseEvent *event)
  575. {
  576. QWidget::closeEvent(event);
  577. if (!event->isAccepted())
  578. return;
  579. // remove draw callback in case our drawable surfaces go away before
  580. // the destructor gets called
  581. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  582. }
  583. void OBSBasic::changeEvent(QEvent *event)
  584. {
  585. /* TODO */
  586. UNUSED_PARAMETER(event);
  587. }
  588. void OBSBasic::resizeEvent(QResizeEvent *event)
  589. {
  590. struct obs_video_info ovi;
  591. if (obs_get_video_info(&ovi))
  592. ResizePreview(ovi.base_width, ovi.base_height);
  593. UNUSED_PARAMETER(event);
  594. }
  595. void OBSBasic::timerEvent(QTimerEvent *event)
  596. {
  597. if (event->timerId() == resizeTimer) {
  598. killTimer(resizeTimer);
  599. resizeTimer = 0;
  600. QSize size = GetPixelSize(ui->preview);
  601. obs_resize(size.width(), size.height());
  602. }
  603. }
  604. void OBSBasic::on_action_New_triggered()
  605. {
  606. /* TODO */
  607. }
  608. void OBSBasic::on_action_Open_triggered()
  609. {
  610. /* TODO */
  611. }
  612. void OBSBasic::on_action_Save_triggered()
  613. {
  614. /* TODO */
  615. }
  616. void OBSBasic::on_action_Settings_triggered()
  617. {
  618. OBSBasicSettings settings(this);
  619. settings.exec();
  620. }
  621. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  622. QListWidgetItem *prev)
  623. {
  624. obs_source_t source = NULL;
  625. if (sceneChanging)
  626. return;
  627. if (current) {
  628. obs_scene_t scene;
  629. scene = current->data(Qt::UserRole).value<OBSScene>();
  630. source = obs_scene_getsource(scene);
  631. }
  632. /* TODO: allow transitions */
  633. obs_set_output_source(0, source);
  634. UNUSED_PARAMETER(prev);
  635. }
  636. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  637. {
  638. /* TODO */
  639. UNUSED_PARAMETER(pos);
  640. }
  641. void OBSBasic::on_actionAddScene_triggered()
  642. {
  643. string name;
  644. bool accepted = NameDialog::AskForName(this,
  645. QTStr("MainWindow.AddSceneDlg.Title"),
  646. QTStr("MainWindow.AddSceneDlg.Text"),
  647. name);
  648. if (accepted) {
  649. if (name.empty()) {
  650. QMessageBox::information(this,
  651. QTStr("MainWindow.NoNameEntered"),
  652. QTStr("MainWindow.NoNameEntered"));
  653. on_actionAddScene_triggered();
  654. return;
  655. }
  656. obs_source_t source = obs_get_source_by_name(name.c_str());
  657. if (source) {
  658. QMessageBox::information(this,
  659. QTStr("MainWindow.NameExists.Title"),
  660. QTStr("MainWindow.NameExists.Text"));
  661. obs_source_release(source);
  662. on_actionAddScene_triggered();
  663. return;
  664. }
  665. obs_scene_t scene = obs_scene_create(name.c_str());
  666. source = obs_scene_getsource(scene);
  667. obs_add_source(source);
  668. obs_scene_release(scene);
  669. obs_set_output_source(0, source);
  670. }
  671. }
  672. void OBSBasic::on_actionRemoveScene_triggered()
  673. {
  674. QListWidgetItem *item = ui->scenes->currentItem();
  675. if (!item)
  676. return;
  677. QVariant userData = item->data(Qt::UserRole);
  678. obs_scene_t scene = userData.value<OBSScene>();
  679. obs_source_t source = obs_scene_getsource(scene);
  680. obs_source_remove(source);
  681. }
  682. void OBSBasic::on_actionSceneProperties_triggered()
  683. {
  684. /* TODO */
  685. }
  686. void OBSBasic::on_actionSceneUp_triggered()
  687. {
  688. /* TODO */
  689. }
  690. void OBSBasic::on_actionSceneDown_triggered()
  691. {
  692. /* TODO */
  693. }
  694. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  695. QListWidgetItem *prev)
  696. {
  697. /* TODO */
  698. UNUSED_PARAMETER(current);
  699. UNUSED_PARAMETER(prev);
  700. }
  701. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  702. {
  703. /* TODO */
  704. UNUSED_PARAMETER(pos);
  705. }
  706. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  707. {
  708. string name;
  709. bool success = false;
  710. while (!success) {
  711. bool accepted = NameDialog::AskForName(this,
  712. Str("MainWindow.AddSourceDlg.Title"),
  713. Str("MainWindow.AddSourceDlg.Text"),
  714. name);
  715. if (!accepted)
  716. break;
  717. if (name.empty()) {
  718. QMessageBox::information(this,
  719. QTStr("MainWindow.NoNameEntered"),
  720. QTStr("MainWindow.NoNameEntered"));
  721. continue;
  722. }
  723. obs_source_t source = obs_get_source_by_name(name.c_str());
  724. if (!source) {
  725. success = true;
  726. break;
  727. } else {
  728. QMessageBox::information(this,
  729. QTStr("MainWindow.NameExists.Title"),
  730. QTStr("MainWindow.NameExists.Text"));
  731. obs_source_release(source);
  732. }
  733. }
  734. if (success) {
  735. obs_source_t source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  736. id, name.c_str(), NULL);
  737. sourceSceneRefs[source] = 0;
  738. obs_add_source(source);
  739. obs_scene_add(scene, source);
  740. obs_source_release(source);
  741. }
  742. }
  743. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  744. {
  745. OBSScene scene = GetCurrentScene();
  746. const char *type;
  747. bool foundValues = false;
  748. size_t idx = 0;
  749. if (!scene)
  750. return;
  751. QMenu popup;
  752. while (obs_enum_input_types(idx++, &type)) {
  753. const char *name = obs_source_getdisplayname(
  754. OBS_SOURCE_TYPE_INPUT,
  755. type, App()->GetLocale());
  756. if (strcmp(type, "scene") == 0)
  757. continue;
  758. QAction *popupItem = new QAction(QT_UTF8(name), this);
  759. popupItem->setData(QT_UTF8(type));
  760. popup.addAction(popupItem);
  761. foundValues = true;
  762. }
  763. if (foundValues) {
  764. QAction *ret = popup.exec(pos);
  765. if (ret)
  766. AddSource(scene, ret->data().toString().toUtf8());
  767. }
  768. }
  769. void OBSBasic::on_actionAddSource_triggered()
  770. {
  771. AddSourcePopupMenu(QCursor::pos());
  772. }
  773. void OBSBasic::on_actionRemoveSource_triggered()
  774. {
  775. OBSSceneItem item = GetCurrentSceneItem();
  776. if (item)
  777. obs_sceneitem_remove(item);
  778. }
  779. void OBSBasic::on_actionSourceProperties_triggered()
  780. {
  781. OBSSceneItem item = GetCurrentSceneItem();
  782. OBSSource source = obs_sceneitem_getsource(item);
  783. if (source) {
  784. delete properties;
  785. properties = new OBSBasicProperties(this, source);
  786. properties->Init();
  787. }
  788. }
  789. void OBSBasic::on_actionSourceUp_triggered()
  790. {
  791. }
  792. void OBSBasic::on_actionSourceDown_triggered()
  793. {
  794. }
  795. void OBSBasic::StreamingStart()
  796. {
  797. ui->streamButton->setText("Stop Streaming");
  798. }
  799. void OBSBasic::StreamingStop(int errorcode)
  800. {
  801. UNUSED_PARAMETER(errorcode);
  802. ui->streamButton->setText("Start Streaming");
  803. }
  804. void OBSBasic::on_streamButton_clicked()
  805. {
  806. if (obs_output_active(streamOutput)) {
  807. obs_output_stop(streamOutput);
  808. } else {
  809. obs_data_t x264Settings = obs_data_create();
  810. obs_data_t aacSettings = obs_data_create();
  811. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  812. "VBitrate");
  813. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  814. "ABitrate");
  815. SaveService();
  816. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  817. obs_data_setbool(x264Settings, "cbr", true);
  818. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  819. obs_encoder_update(x264, x264Settings);
  820. obs_encoder_update(aac, aacSettings);
  821. obs_data_release(x264Settings);
  822. obs_data_release(aacSettings);
  823. obs_encoder_set_video(x264, obs_video());
  824. obs_encoder_set_audio(aac, obs_audio());
  825. obs_output_set_video_encoder(streamOutput, x264);
  826. obs_output_set_audio_encoder(streamOutput, aac);
  827. obs_output_set_service(streamOutput, service);
  828. obs_output_start(streamOutput);
  829. }
  830. }
  831. void OBSBasic::on_settingsButton_clicked()
  832. {
  833. OBSBasicSettings settings(this);
  834. settings.exec();
  835. }
  836. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  837. {
  838. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  839. if (strcmp(val, "10") == 0) {
  840. num = 10;
  841. den = 1;
  842. } else if (strcmp(val, "20") == 0) {
  843. num = 20;
  844. den = 1;
  845. } else if (strcmp(val, "25") == 0) {
  846. num = 25;
  847. den = 1;
  848. } else if (strcmp(val, "29.97") == 0) {
  849. num = 30000;
  850. den = 1001;
  851. } else if (strcmp(val, "48") == 0) {
  852. num = 48;
  853. den = 1;
  854. } else if (strcmp(val, "59.94") == 0) {
  855. num = 60000;
  856. den = 1001;
  857. } else if (strcmp(val, "60") == 0) {
  858. num = 60;
  859. den = 1;
  860. } else {
  861. num = 30;
  862. den = 1;
  863. }
  864. }
  865. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  866. {
  867. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  868. den = 1;
  869. }
  870. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  871. {
  872. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  873. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  874. }
  875. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  876. {
  877. num = 1000000000;
  878. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  879. }
  880. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  881. {
  882. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  883. if (type == 1) //"Integer"
  884. GetFPSInteger(num, den);
  885. else if (type == 2) //"Fraction"
  886. GetFPSFraction(num, den);
  887. else if (false) //"Nanoseconds", currently not implemented
  888. GetFPSNanoseconds(num, den);
  889. else
  890. GetFPSCommon(num, den);
  891. }
  892. config_t OBSBasic::Config() const
  893. {
  894. return basicConfig;
  895. }