window-basic-main.cpp 30 KB

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