1
0

window-basic-main.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. QString placeHolderText = format.arg(ui->scenes->count() + 1);
  761. bool accepted = NameDialog::AskForName(this,
  762. QTStr("Basic.Main.AddSceneDlg.Title"),
  763. QTStr("Basic.Main.AddSceneDlg.Text"),
  764. name,
  765. placeHolderText);
  766. if (accepted) {
  767. if (name.empty()) {
  768. QMessageBox::information(this,
  769. QTStr("NoNameEntered"),
  770. QTStr("NoNameEntered"));
  771. on_actionAddScene_triggered();
  772. return;
  773. }
  774. obs_source_t source = obs_get_source_by_name(name.c_str());
  775. if (source) {
  776. QMessageBox::information(this,
  777. QTStr("NameExists.Title"),
  778. QTStr("NameExists.Text"));
  779. obs_source_release(source);
  780. on_actionAddScene_triggered();
  781. return;
  782. }
  783. obs_scene_t scene = obs_scene_create(name.c_str());
  784. source = obs_scene_getsource(scene);
  785. obs_add_source(source);
  786. obs_scene_release(scene);
  787. obs_set_output_source(0, source);
  788. }
  789. }
  790. void OBSBasic::on_actionRemoveScene_triggered()
  791. {
  792. QListWidgetItem *item = ui->scenes->currentItem();
  793. if (!item)
  794. return;
  795. QVariant userData = item->data(Qt::UserRole);
  796. obs_scene_t scene = userData.value<OBSScene>();
  797. obs_source_t source = obs_scene_getsource(scene);
  798. obs_source_remove(source);
  799. }
  800. void OBSBasic::on_actionSceneProperties_triggered()
  801. {
  802. /* TODO */
  803. }
  804. void OBSBasic::on_actionSceneUp_triggered()
  805. {
  806. /* TODO */
  807. }
  808. void OBSBasic::on_actionSceneDown_triggered()
  809. {
  810. /* TODO */
  811. }
  812. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  813. QListWidgetItem *prev)
  814. {
  815. /* TODO */
  816. UNUSED_PARAMETER(current);
  817. UNUSED_PARAMETER(prev);
  818. }
  819. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  820. {
  821. /* TODO */
  822. UNUSED_PARAMETER(pos);
  823. }
  824. void OBSBasic::AddSource(const char *id)
  825. {
  826. OBSBasicSourceSelect sourceSelect(this, id);
  827. sourceSelect.exec();
  828. }
  829. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  830. {
  831. const char *type;
  832. bool foundValues = false;
  833. size_t idx = 0;
  834. if (!GetCurrentScene()) {
  835. // Tell the user he needs a scene first (help beginners).
  836. QMessageBox::information(this,
  837. QTStr("Basic.Main.AddSourceHelp.Title"),
  838. QTStr("Basic.Main.AddSourceHelp.Text"));
  839. return;
  840. }
  841. QMenu popup;
  842. while (obs_enum_input_types(idx++, &type)) {
  843. const char *name = obs_source_getdisplayname(
  844. OBS_SOURCE_TYPE_INPUT,
  845. type, App()->GetLocale());
  846. if (strcmp(type, "scene") == 0)
  847. continue;
  848. QAction *popupItem = new QAction(QT_UTF8(name), this);
  849. popupItem->setData(QT_UTF8(type));
  850. popup.addAction(popupItem);
  851. foundValues = true;
  852. }
  853. if (foundValues) {
  854. QAction *ret = popup.exec(pos);
  855. if (ret)
  856. AddSource(ret->data().toString().toUtf8());
  857. }
  858. }
  859. void OBSBasic::on_actionAddSource_triggered()
  860. {
  861. AddSourcePopupMenu(QCursor::pos());
  862. }
  863. void OBSBasic::on_actionRemoveSource_triggered()
  864. {
  865. OBSSceneItem item = GetCurrentSceneItem();
  866. if (item)
  867. obs_sceneitem_remove(item);
  868. }
  869. void OBSBasic::on_actionSourceProperties_triggered()
  870. {
  871. OBSSceneItem item = GetCurrentSceneItem();
  872. OBSSource source = obs_sceneitem_getsource(item);
  873. if (source) {
  874. delete properties;
  875. properties = new OBSBasicProperties(this, source);
  876. properties->Init();
  877. }
  878. }
  879. void OBSBasic::on_actionSourceUp_triggered()
  880. {
  881. }
  882. void OBSBasic::on_actionSourceDown_triggered()
  883. {
  884. }
  885. void OBSBasic::StreamingStart()
  886. {
  887. ui->streamButton->setText("Stop Streaming");
  888. }
  889. void OBSBasic::StreamingStop(int code)
  890. {
  891. const char *errorMessage;
  892. switch (code) {
  893. case OBS_OUTPUT_BAD_PATH:
  894. errorMessage = Str("Output.ConnectFail.BadPath");
  895. break;
  896. case OBS_OUTPUT_CONNECT_FAILED:
  897. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  898. break;
  899. case OBS_OUTPUT_INVALID_STREAM:
  900. errorMessage = Str("Output.ConnectFail.InvalidStream");
  901. break;
  902. case OBS_OUTPUT_ERROR:
  903. errorMessage = Str("Output.ConnectFail.Error");
  904. break;
  905. case OBS_OUTPUT_DISCONNECTED:
  906. /* doesn't happen if output is set to reconnect. note that
  907. * reconnects are handled in the output, not in the UI */
  908. errorMessage = Str("Output.ConnectFail.Disconnected");
  909. }
  910. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  911. if (code != OBS_OUTPUT_SUCCESS)
  912. QMessageBox::information(this,
  913. QTStr("Output.ConnectFail.Title"),
  914. QT_UTF8(errorMessage));
  915. }
  916. void OBSBasic::on_streamButton_clicked()
  917. {
  918. if (obs_output_active(streamOutput)) {
  919. obs_output_stop(streamOutput);
  920. } else {
  921. obs_data_t x264Settings = obs_data_create();
  922. obs_data_t aacSettings = obs_data_create();
  923. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  924. "VBitrate");
  925. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  926. "ABitrate");
  927. SaveService();
  928. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  929. obs_data_setbool(x264Settings, "cbr", true);
  930. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  931. obs_encoder_update(x264, x264Settings);
  932. obs_encoder_update(aac, aacSettings);
  933. obs_data_release(x264Settings);
  934. obs_data_release(aacSettings);
  935. obs_encoder_set_video(x264, obs_video());
  936. obs_encoder_set_audio(aac, obs_audio());
  937. obs_output_set_video_encoder(streamOutput, x264);
  938. obs_output_set_audio_encoder(streamOutput, aac);
  939. obs_output_set_service(streamOutput, service);
  940. obs_output_start(streamOutput);
  941. }
  942. }
  943. void OBSBasic::on_settingsButton_clicked()
  944. {
  945. OBSBasicSettings settings(this);
  946. settings.exec();
  947. }
  948. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  949. {
  950. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  951. if (strcmp(val, "10") == 0) {
  952. num = 10;
  953. den = 1;
  954. } else if (strcmp(val, "20") == 0) {
  955. num = 20;
  956. den = 1;
  957. } else if (strcmp(val, "25") == 0) {
  958. num = 25;
  959. den = 1;
  960. } else if (strcmp(val, "29.97") == 0) {
  961. num = 30000;
  962. den = 1001;
  963. } else if (strcmp(val, "48") == 0) {
  964. num = 48;
  965. den = 1;
  966. } else if (strcmp(val, "59.94") == 0) {
  967. num = 60000;
  968. den = 1001;
  969. } else if (strcmp(val, "60") == 0) {
  970. num = 60;
  971. den = 1;
  972. } else {
  973. num = 30;
  974. den = 1;
  975. }
  976. }
  977. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  978. {
  979. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  980. den = 1;
  981. }
  982. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  983. {
  984. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  985. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  986. }
  987. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  988. {
  989. num = 1000000000;
  990. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  991. }
  992. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  993. {
  994. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  995. if (type == 1) //"Integer"
  996. GetFPSInteger(num, den);
  997. else if (type == 2) //"Fraction"
  998. GetFPSFraction(num, den);
  999. else if (false) //"Nanoseconds", currently not implemented
  1000. GetFPSNanoseconds(num, den);
  1001. else
  1002. GetFPSCommon(num, den);
  1003. }
  1004. config_t OBSBasic::Config() const
  1005. {
  1006. return basicConfig;
  1007. }