window-basic-main.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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-source-select.hpp"
  26. #include "window-basic-main.hpp"
  27. #include "window-basic-properties.hpp"
  28. #include "qt-wrappers.hpp"
  29. #include "display-helpers.hpp"
  30. #include "volume-control.hpp"
  31. #include "ui_OBSBasic.h"
  32. #include <sstream>
  33. #include <QScreen>
  34. #include <QWindow>
  35. using namespace std;
  36. Q_DECLARE_METATYPE(OBSScene);
  37. Q_DECLARE_METATYPE(OBSSceneItem);
  38. OBSBasic::OBSBasic(QWidget *parent)
  39. : OBSMainWindow (parent),
  40. properties (nullptr),
  41. streamOutput (nullptr),
  42. service (nullptr),
  43. aac (nullptr),
  44. x264 (nullptr),
  45. sceneChanging (false),
  46. resizeTimer (0),
  47. ui (new Ui::OBSBasic)
  48. {
  49. ui->setupUi(this);
  50. connect(windowHandle(), &QWindow::screenChanged, [this]() {
  51. struct obs_video_info ovi;
  52. if (obs_get_video_info(&ovi))
  53. ResizePreview(ovi.base_width, ovi.base_height);
  54. });
  55. }
  56. static void SaveAudioDevice(const char *name, int channel, obs_data_t parent)
  57. {
  58. obs_source_t source = obs_get_output_source(channel);
  59. if (!source)
  60. return;
  61. obs_data_t data = obs_save_source(source);
  62. obs_data_setobj(parent, name, data);
  63. obs_data_release(data);
  64. obs_source_release(source);
  65. }
  66. static obs_data_t GenerateSaveData()
  67. {
  68. obs_data_t saveData = obs_data_create();
  69. obs_data_array_t sourcesArray = obs_save_sources();
  70. obs_source_t currentScene = obs_get_output_source(0);
  71. const char *sceneName = obs_source_getname(currentScene);
  72. SaveAudioDevice(DESKTOP_AUDIO_1, 1, saveData);
  73. SaveAudioDevice(DESKTOP_AUDIO_2, 2, saveData);
  74. SaveAudioDevice(AUX_AUDIO_1, 3, saveData);
  75. SaveAudioDevice(AUX_AUDIO_2, 4, saveData);
  76. SaveAudioDevice(AUX_AUDIO_3, 5, saveData);
  77. obs_data_setstring(saveData, "current_scene", sceneName);
  78. obs_data_setarray(saveData, "sources", sourcesArray);
  79. obs_data_array_release(sourcesArray);
  80. obs_source_release(currentScene);
  81. return saveData;
  82. }
  83. void OBSBasic::ClearVolumeControls()
  84. {
  85. VolControl *control;
  86. for (size_t i = 0; i < volumes.size(); i++) {
  87. control = volumes[i];
  88. delete control;
  89. }
  90. volumes.clear();
  91. }
  92. void OBSBasic::Save(const char *file)
  93. {
  94. obs_data_t saveData = GenerateSaveData();
  95. const char *jsonData = obs_data_getjson(saveData);
  96. /* TODO maybe a message box here? */
  97. if (!os_quick_write_utf8_file(file, jsonData, strlen(jsonData), false))
  98. blog(LOG_ERROR, "Could not save scene data to %s", file);
  99. obs_data_release(saveData);
  100. }
  101. static void LoadAudioDevice(const char *name, int channel, obs_data_t parent)
  102. {
  103. obs_data_t data = obs_data_getobj(parent, name);
  104. if (!data)
  105. return;
  106. obs_source_t source = obs_load_source(data);
  107. if (source) {
  108. obs_set_output_source(channel, source);
  109. obs_source_release(source);
  110. }
  111. obs_data_release(data);
  112. }
  113. void OBSBasic::CreateDefaultScene()
  114. {
  115. obs_scene_t scene = obs_scene_create(Str("Basic.Scene"));
  116. obs_source_t source = obs_scene_getsource(scene);
  117. obs_add_source(source);
  118. #ifdef __APPLE__
  119. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, "display_capture",
  120. Str("Basic.DisplayCapture"), NULL);
  121. if (source) {
  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, "code");
  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 code = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
  301. if (code != CONFIG_SUCCESS) {
  302. OBSErrorBox(NULL, "Failed to open basic.ini: %d", code);
  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. sourceSceneRefs[source] = scenes;
  456. if (scenes == 0) {
  457. obs_source_remove(source);
  458. sourceSceneRefs.erase(source);
  459. }
  460. }
  461. void OBSBasic::UpdateSceneSelection(OBSSource source)
  462. {
  463. if (source) {
  464. obs_source_type type;
  465. obs_source_gettype(source, &type, NULL);
  466. obs_scene_t scene = obs_scene_fromsource(source);
  467. const char *name = obs_source_getname(source);
  468. if (!scene)
  469. return;
  470. QList<QListWidgetItem*> items =
  471. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  472. if (items.count()) {
  473. sceneChanging = true;
  474. ui->scenes->setCurrentItem(items.first());
  475. sceneChanging = false;
  476. UpdateSources(scene);
  477. }
  478. }
  479. }
  480. void OBSBasic::ActivateAudioSource(OBSSource source)
  481. {
  482. VolControl *vol = new VolControl(source);
  483. volumes.push_back(vol);
  484. ui->volumeWidgets->layout()->addWidget(vol);
  485. }
  486. void OBSBasic::DeactivateAudioSource(OBSSource source)
  487. {
  488. for (size_t i = 0; i < volumes.size(); i++) {
  489. if (volumes[i]->GetSource() == source) {
  490. delete volumes[i];
  491. volumes.erase(volumes.begin() + i);
  492. break;
  493. }
  494. }
  495. }
  496. /* OBS Callbacks */
  497. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  498. {
  499. OBSBasic *window = static_cast<OBSBasic*>(data);
  500. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  501. QMetaObject::invokeMethod(window, "AddSceneItem",
  502. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  503. }
  504. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  505. {
  506. OBSBasic *window = static_cast<OBSBasic*>(data);
  507. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  508. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  509. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  510. }
  511. void OBSBasic::SourceAdded(void *data, calldata_t params)
  512. {
  513. OBSBasic *window = static_cast<OBSBasic*>(data);
  514. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  515. if (obs_scene_fromsource(source) != NULL)
  516. QMetaObject::invokeMethod(window,
  517. "AddScene",
  518. Q_ARG(OBSSource, OBSSource(source)));
  519. else
  520. window->sourceSceneRefs[source] = 0;
  521. }
  522. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  523. {
  524. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  525. if (obs_scene_fromsource(source) != NULL)
  526. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  527. "RemoveScene",
  528. Q_ARG(OBSSource, OBSSource(source)));
  529. }
  530. void OBSBasic::SourceActivated(void *data, calldata_t params)
  531. {
  532. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  533. uint32_t flags = obs_source_get_output_flags(source);
  534. if (flags & OBS_SOURCE_AUDIO)
  535. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  536. "ActivateAudioSource",
  537. Q_ARG(OBSSource, OBSSource(source)));
  538. }
  539. void OBSBasic::SourceDeactivated(void *data, calldata_t params)
  540. {
  541. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  542. uint32_t flags = obs_source_get_output_flags(source);
  543. if (flags & OBS_SOURCE_AUDIO)
  544. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  545. "DeactivateAudioSource",
  546. Q_ARG(OBSSource, OBSSource(source)));
  547. }
  548. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  549. {
  550. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  551. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  552. if (channel == 0)
  553. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  554. "UpdateSceneSelection",
  555. Q_ARG(OBSSource, OBSSource(source)));
  556. }
  557. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  558. {
  559. OBSBasic *window = static_cast<OBSBasic*>(data);
  560. obs_video_info ovi;
  561. int newCX, newCY;
  562. obs_get_video_info(&ovi);
  563. newCX = int(window->previewScale * float(ovi.base_width));
  564. newCY = int(window->previewScale * float(ovi.base_height));
  565. gs_viewport_push();
  566. gs_projection_push();
  567. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  568. -100.0f, 100.0f);
  569. gs_setviewport(window->previewX, window->previewY, newCX, newCY);
  570. obs_render_main_view();
  571. gs_projection_pop();
  572. gs_viewport_pop();
  573. UNUSED_PARAMETER(cx);
  574. UNUSED_PARAMETER(cy);
  575. }
  576. /* Main class functions */
  577. obs_service_t OBSBasic::GetService()
  578. {
  579. if (!service)
  580. service = obs_service_create("rtmp_common", NULL, NULL);
  581. return service;
  582. }
  583. void OBSBasic::SetService(obs_service_t newService)
  584. {
  585. if (newService) {
  586. if (service)
  587. obs_service_destroy(service);
  588. service = newService;
  589. }
  590. }
  591. bool OBSBasic::ResetVideo()
  592. {
  593. struct obs_video_info ovi;
  594. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  595. ovi.graphics_module = App()->GetRenderModule();
  596. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  597. "Video", "BaseCX");
  598. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  599. "Video", "BaseCY");
  600. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  601. "Video", "OutputCX");
  602. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  603. "Video", "OutputCY");
  604. ovi.output_format = VIDEO_FORMAT_NV12;
  605. ovi.adapter = 0;
  606. ovi.gpu_conversion = true;
  607. QTToGSWindow(ui->preview->winId(), ovi.window);
  608. //required to make opengl display stuff on osx(?)
  609. ResizePreview(ovi.base_width, ovi.base_height);
  610. QSize size = GetPixelSize(ui->preview);
  611. ovi.window_width = size.width();
  612. ovi.window_height = size.height();
  613. if (!obs_reset_video(&ovi))
  614. return false;
  615. obs_add_draw_callback(OBSBasic::RenderMain, this);
  616. return true;
  617. }
  618. bool OBSBasic::ResetAudio()
  619. {
  620. struct audio_output_info ai;
  621. ai.name = "Main Audio Track";
  622. ai.format = AUDIO_FORMAT_FLOAT;
  623. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  624. "SampleRate");
  625. const char *channelSetupStr = config_get_string(basicConfig,
  626. "Audio", "ChannelSetup");
  627. if (strcmp(channelSetupStr, "Mono") == 0)
  628. ai.speakers = SPEAKERS_MONO;
  629. else
  630. ai.speakers = SPEAKERS_STEREO;
  631. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  632. return obs_reset_audio(&ai);
  633. }
  634. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  635. const char *deviceDesc, int channel)
  636. {
  637. const char *deviceId = config_get_string(basicConfig, "Audio",
  638. deviceName);
  639. obs_source_t source;
  640. obs_data_t settings;
  641. bool same = false;
  642. source = obs_get_output_source(channel);
  643. if (source) {
  644. settings = obs_source_getsettings(source);
  645. const char *curId = obs_data_getstring(settings, "device_id");
  646. same = (strcmp(curId, deviceId) == 0);
  647. obs_data_release(settings);
  648. obs_source_release(source);
  649. }
  650. if (!same)
  651. obs_set_output_source(channel, nullptr);
  652. if (!same && strcmp(deviceId, "disabled") != 0) {
  653. obs_data_t settings = obs_data_create();
  654. obs_data_setstring(settings, "device_id", deviceId);
  655. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  656. sourceId, deviceDesc, settings);
  657. obs_data_release(settings);
  658. obs_set_output_source(channel, source);
  659. obs_source_release(source);
  660. }
  661. }
  662. void OBSBasic::ResetAudioDevices()
  663. {
  664. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1",
  665. Str("Basic.DesktopDevice1"), 1);
  666. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2",
  667. Str("Basic.DesktopDevice2"), 2);
  668. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1",
  669. Str("Basic.AuxDevice1"), 3);
  670. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2",
  671. Str("Basic.AuxDevice2"), 4);
  672. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3",
  673. Str("Basic.AuxDevice3"), 5);
  674. }
  675. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  676. {
  677. QSize targetSize;
  678. /* resize preview panel to fix to the top section of the window */
  679. targetSize = GetPixelSize(ui->preview);
  680. GetScaleAndCenterPos(int(cx), int(cy),
  681. targetSize.width(), targetSize.height(),
  682. previewX, previewY, previewScale);
  683. if (isVisible()) {
  684. if (resizeTimer)
  685. killTimer(resizeTimer);
  686. resizeTimer = startTimer(100);
  687. }
  688. }
  689. void OBSBasic::closeEvent(QCloseEvent *event)
  690. {
  691. QWidget::closeEvent(event);
  692. if (!event->isAccepted())
  693. return;
  694. // remove draw callback in case our drawable surfaces go away before
  695. // the destructor gets called
  696. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  697. }
  698. void OBSBasic::changeEvent(QEvent *event)
  699. {
  700. /* TODO */
  701. UNUSED_PARAMETER(event);
  702. }
  703. void OBSBasic::resizeEvent(QResizeEvent *event)
  704. {
  705. struct obs_video_info ovi;
  706. if (obs_get_video_info(&ovi))
  707. ResizePreview(ovi.base_width, ovi.base_height);
  708. UNUSED_PARAMETER(event);
  709. }
  710. void OBSBasic::timerEvent(QTimerEvent *event)
  711. {
  712. if (event->timerId() == resizeTimer) {
  713. killTimer(resizeTimer);
  714. resizeTimer = 0;
  715. QSize size = GetPixelSize(ui->preview);
  716. obs_resize(size.width(), size.height());
  717. }
  718. }
  719. void OBSBasic::on_action_New_triggered()
  720. {
  721. /* TODO */
  722. }
  723. void OBSBasic::on_action_Open_triggered()
  724. {
  725. /* TODO */
  726. }
  727. void OBSBasic::on_action_Save_triggered()
  728. {
  729. /* TODO */
  730. }
  731. void OBSBasic::on_action_Settings_triggered()
  732. {
  733. OBSBasicSettings settings(this);
  734. settings.exec();
  735. }
  736. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  737. QListWidgetItem *prev)
  738. {
  739. obs_source_t source = NULL;
  740. if (sceneChanging)
  741. return;
  742. if (current) {
  743. obs_scene_t scene;
  744. scene = current->data(Qt::UserRole).value<OBSScene>();
  745. source = obs_scene_getsource(scene);
  746. }
  747. /* TODO: allow transitions */
  748. obs_set_output_source(0, source);
  749. UNUSED_PARAMETER(prev);
  750. }
  751. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  752. {
  753. /* TODO */
  754. UNUSED_PARAMETER(pos);
  755. }
  756. void OBSBasic::on_actionAddScene_triggered()
  757. {
  758. string name;
  759. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  760. int i = 1;
  761. QString placeHolderText = format.arg(i);
  762. obs_source_t source = nullptr;
  763. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  764. obs_source_release(source);
  765. placeHolderText = format.arg(++i);
  766. }
  767. bool accepted = NameDialog::AskForName(this,
  768. QTStr("Basic.Main.AddSceneDlg.Title"),
  769. QTStr("Basic.Main.AddSceneDlg.Text"),
  770. name,
  771. placeHolderText);
  772. if (accepted) {
  773. if (name.empty()) {
  774. QMessageBox::information(this,
  775. QTStr("NoNameEntered"),
  776. QTStr("NoNameEntered"));
  777. on_actionAddScene_triggered();
  778. return;
  779. }
  780. obs_source_t source = obs_get_source_by_name(name.c_str());
  781. if (source) {
  782. QMessageBox::information(this,
  783. QTStr("NameExists.Title"),
  784. QTStr("NameExists.Text"));
  785. obs_source_release(source);
  786. on_actionAddScene_triggered();
  787. return;
  788. }
  789. obs_scene_t scene = obs_scene_create(name.c_str());
  790. source = obs_scene_getsource(scene);
  791. obs_add_source(source);
  792. obs_scene_release(scene);
  793. obs_set_output_source(0, source);
  794. }
  795. }
  796. void OBSBasic::on_actionRemoveScene_triggered()
  797. {
  798. QListWidgetItem *item = ui->scenes->currentItem();
  799. if (!item)
  800. return;
  801. QVariant userData = item->data(Qt::UserRole);
  802. obs_scene_t scene = userData.value<OBSScene>();
  803. obs_source_t source = obs_scene_getsource(scene);
  804. obs_source_remove(source);
  805. }
  806. void OBSBasic::on_actionSceneProperties_triggered()
  807. {
  808. /* TODO */
  809. }
  810. void OBSBasic::on_actionSceneUp_triggered()
  811. {
  812. /* TODO */
  813. }
  814. void OBSBasic::on_actionSceneDown_triggered()
  815. {
  816. /* TODO */
  817. }
  818. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  819. QListWidgetItem *prev)
  820. {
  821. /* TODO */
  822. UNUSED_PARAMETER(current);
  823. UNUSED_PARAMETER(prev);
  824. }
  825. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  826. {
  827. /* TODO */
  828. UNUSED_PARAMETER(pos);
  829. }
  830. void OBSBasic::AddSource(const char *id)
  831. {
  832. OBSBasicSourceSelect sourceSelect(this, id);
  833. sourceSelect.exec();
  834. }
  835. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  836. {
  837. const char *type;
  838. bool foundValues = false;
  839. size_t idx = 0;
  840. if (!GetCurrentScene()) {
  841. // Tell the user he needs a scene first (help beginners).
  842. QMessageBox::information(this,
  843. QTStr("Basic.Main.AddSourceHelp.Title"),
  844. QTStr("Basic.Main.AddSourceHelp.Text"));
  845. return;
  846. }
  847. QMenu popup;
  848. while (obs_enum_input_types(idx++, &type)) {
  849. const char *name = obs_source_getdisplayname(
  850. OBS_SOURCE_TYPE_INPUT,
  851. type, App()->GetLocale());
  852. if (strcmp(type, "scene") == 0)
  853. continue;
  854. QAction *popupItem = new QAction(QT_UTF8(name), this);
  855. popupItem->setData(QT_UTF8(type));
  856. popup.addAction(popupItem);
  857. foundValues = true;
  858. }
  859. if (foundValues) {
  860. QAction *ret = popup.exec(pos);
  861. if (ret)
  862. AddSource(ret->data().toString().toUtf8());
  863. }
  864. }
  865. void OBSBasic::on_actionAddSource_triggered()
  866. {
  867. AddSourcePopupMenu(QCursor::pos());
  868. }
  869. void OBSBasic::on_actionRemoveSource_triggered()
  870. {
  871. OBSSceneItem item = GetCurrentSceneItem();
  872. if (item)
  873. obs_sceneitem_remove(item);
  874. }
  875. void OBSBasic::on_actionSourceProperties_triggered()
  876. {
  877. OBSSceneItem item = GetCurrentSceneItem();
  878. OBSSource source = obs_sceneitem_getsource(item);
  879. if (source) {
  880. delete properties;
  881. properties = new OBSBasicProperties(this, source);
  882. properties->Init();
  883. }
  884. }
  885. void OBSBasic::on_actionSourceUp_triggered()
  886. {
  887. }
  888. void OBSBasic::on_actionSourceDown_triggered()
  889. {
  890. }
  891. void OBSBasic::StreamingStart()
  892. {
  893. ui->streamButton->setText("Stop Streaming");
  894. }
  895. void OBSBasic::StreamingStop(int code)
  896. {
  897. const char *errorMessage;
  898. switch (code) {
  899. case OBS_OUTPUT_BAD_PATH:
  900. errorMessage = Str("Output.ConnectFail.BadPath");
  901. break;
  902. case OBS_OUTPUT_CONNECT_FAILED:
  903. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  904. break;
  905. case OBS_OUTPUT_INVALID_STREAM:
  906. errorMessage = Str("Output.ConnectFail.InvalidStream");
  907. break;
  908. case OBS_OUTPUT_ERROR:
  909. errorMessage = Str("Output.ConnectFail.Error");
  910. break;
  911. case OBS_OUTPUT_DISCONNECTED:
  912. /* doesn't happen if output is set to reconnect. note that
  913. * reconnects are handled in the output, not in the UI */
  914. errorMessage = Str("Output.ConnectFail.Disconnected");
  915. }
  916. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  917. if (code != OBS_OUTPUT_SUCCESS)
  918. QMessageBox::information(this,
  919. QTStr("Output.ConnectFail.Title"),
  920. QT_UTF8(errorMessage));
  921. }
  922. void OBSBasic::on_streamButton_clicked()
  923. {
  924. if (obs_output_active(streamOutput)) {
  925. obs_output_stop(streamOutput);
  926. } else {
  927. obs_data_t x264Settings = obs_data_create();
  928. obs_data_t aacSettings = obs_data_create();
  929. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  930. "VBitrate");
  931. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  932. "ABitrate");
  933. SaveService();
  934. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  935. obs_data_setbool(x264Settings, "cbr", true);
  936. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  937. obs_encoder_update(x264, x264Settings);
  938. obs_encoder_update(aac, aacSettings);
  939. obs_data_release(x264Settings);
  940. obs_data_release(aacSettings);
  941. obs_encoder_set_video(x264, obs_video());
  942. obs_encoder_set_audio(aac, obs_audio());
  943. obs_output_set_video_encoder(streamOutput, x264);
  944. obs_output_set_audio_encoder(streamOutput, aac);
  945. obs_output_set_service(streamOutput, service);
  946. obs_output_start(streamOutput);
  947. }
  948. }
  949. void OBSBasic::on_settingsButton_clicked()
  950. {
  951. OBSBasicSettings settings(this);
  952. settings.exec();
  953. }
  954. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  955. {
  956. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  957. if (strcmp(val, "10") == 0) {
  958. num = 10;
  959. den = 1;
  960. } else if (strcmp(val, "20") == 0) {
  961. num = 20;
  962. den = 1;
  963. } else if (strcmp(val, "25") == 0) {
  964. num = 25;
  965. den = 1;
  966. } else if (strcmp(val, "29.97") == 0) {
  967. num = 30000;
  968. den = 1001;
  969. } else if (strcmp(val, "48") == 0) {
  970. num = 48;
  971. den = 1;
  972. } else if (strcmp(val, "59.94") == 0) {
  973. num = 60000;
  974. den = 1001;
  975. } else if (strcmp(val, "60") == 0) {
  976. num = 60;
  977. den = 1;
  978. } else {
  979. num = 30;
  980. den = 1;
  981. }
  982. }
  983. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  984. {
  985. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  986. den = 1;
  987. }
  988. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  989. {
  990. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  991. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  992. }
  993. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  994. {
  995. num = 1000000000;
  996. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  997. }
  998. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  999. {
  1000. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  1001. if (type == 1) //"Integer"
  1002. GetFPSInteger(num, den);
  1003. else if (type == 2) //"Fraction"
  1004. GetFPSFraction(num, den);
  1005. else if (false) //"Nanoseconds", currently not implemented
  1006. GetFPSNanoseconds(num, den);
  1007. else
  1008. GetFPSCommon(num, den);
  1009. }
  1010. config_t OBSBasic::Config() const
  1011. {
  1012. return basicConfig;
  1013. }