window-basic-main.cpp 26 KB

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