window-basic-main.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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-xcomposite");
  351. obs_load_module("linux-pulseaudio");
  352. #endif
  353. if (!InitOutputs())
  354. throw "Failed to initialize outputs";
  355. if (!InitEncoders())
  356. throw "Failed to initialize encoders";
  357. if (!InitService())
  358. throw "Failed to initialize service";
  359. Load(savePath);
  360. ResetAudioDevices();
  361. }
  362. OBSBasic::~OBSBasic()
  363. {
  364. BPtr<char> savePath(os_get_config_path("obs-studio/basic/scenes.json"));
  365. SaveService();
  366. Save(savePath);
  367. if (properties)
  368. delete properties;
  369. /* free the lists before shutting down to remove the scene/item
  370. * references */
  371. ClearVolumeControls();
  372. ui->sources->clear();
  373. ui->scenes->clear();
  374. obs_shutdown();
  375. }
  376. OBSScene OBSBasic::GetCurrentScene()
  377. {
  378. QListWidgetItem *item = ui->scenes->currentItem();
  379. return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
  380. }
  381. OBSSceneItem OBSBasic::GetCurrentSceneItem()
  382. {
  383. QListWidgetItem *item = ui->sources->currentItem();
  384. return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
  385. }
  386. void OBSBasic::UpdateSources(OBSScene scene)
  387. {
  388. ui->sources->clear();
  389. obs_scene_enum_items(scene,
  390. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  391. {
  392. OBSBasic *window = static_cast<OBSBasic*>(p);
  393. window->InsertSceneItem(item);
  394. UNUSED_PARAMETER(scene);
  395. return true;
  396. }, this);
  397. }
  398. void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
  399. {
  400. obs_source_t source = obs_sceneitem_getsource(item);
  401. const char *name = obs_source_getname(source);
  402. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  403. listItem->setData(Qt::UserRole,
  404. QVariant::fromValue(OBSSceneItem(item)));
  405. ui->sources->insertItem(0, listItem);
  406. }
  407. /* Qt callbacks for invokeMethod */
  408. void OBSBasic::AddScene(OBSSource source)
  409. {
  410. const char *name = obs_source_getname(source);
  411. obs_scene_t scene = obs_scene_fromsource(source);
  412. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  413. item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
  414. ui->scenes->addItem(item);
  415. signal_handler_t handler = obs_source_signalhandler(source);
  416. signal_handler_connect(handler, "item_add",
  417. OBSBasic::SceneItemAdded, this);
  418. signal_handler_connect(handler, "item_remove",
  419. OBSBasic::SceneItemRemoved, this);
  420. }
  421. void OBSBasic::RemoveScene(OBSSource source)
  422. {
  423. const char *name = obs_source_getname(source);
  424. QListWidgetItem *sel = ui->scenes->currentItem();
  425. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  426. Qt::MatchExactly);
  427. if (sel != nullptr) {
  428. if (items.contains(sel))
  429. ui->sources->clear();
  430. delete sel;
  431. }
  432. }
  433. void OBSBasic::AddSceneItem(OBSSceneItem item)
  434. {
  435. obs_scene_t scene = obs_sceneitem_getscene(item);
  436. obs_source_t source = obs_sceneitem_getsource(item);
  437. if (GetCurrentScene() == scene)
  438. InsertSceneItem(item);
  439. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  440. }
  441. void OBSBasic::RemoveSceneItem(OBSSceneItem item)
  442. {
  443. obs_scene_t scene = obs_sceneitem_getscene(item);
  444. if (GetCurrentScene() == scene) {
  445. for (int i = 0; i < ui->sources->count(); i++) {
  446. QListWidgetItem *listItem = ui->sources->item(i);
  447. QVariant userData = listItem->data(Qt::UserRole);
  448. if (userData.value<OBSSceneItem>() == item) {
  449. delete listItem;
  450. break;
  451. }
  452. }
  453. }
  454. obs_source_t source = obs_sceneitem_getsource(item);
  455. int scenes = sourceSceneRefs[source] - 1;
  456. sourceSceneRefs[source] = scenes;
  457. if (scenes == 0) {
  458. obs_source_remove(source);
  459. sourceSceneRefs.erase(source);
  460. }
  461. }
  462. void OBSBasic::UpdateSceneSelection(OBSSource source)
  463. {
  464. if (source) {
  465. obs_source_type type;
  466. obs_source_gettype(source, &type, NULL);
  467. obs_scene_t scene = obs_scene_fromsource(source);
  468. const char *name = obs_source_getname(source);
  469. if (!scene)
  470. return;
  471. QList<QListWidgetItem*> items =
  472. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  473. if (items.count()) {
  474. sceneChanging = true;
  475. ui->scenes->setCurrentItem(items.first());
  476. sceneChanging = false;
  477. UpdateSources(scene);
  478. }
  479. }
  480. }
  481. void OBSBasic::ActivateAudioSource(OBSSource source)
  482. {
  483. VolControl *vol = new VolControl(source);
  484. volumes.push_back(vol);
  485. ui->volumeWidgets->layout()->addWidget(vol);
  486. }
  487. void OBSBasic::DeactivateAudioSource(OBSSource source)
  488. {
  489. for (size_t i = 0; i < volumes.size(); i++) {
  490. if (volumes[i]->GetSource() == source) {
  491. delete volumes[i];
  492. volumes.erase(volumes.begin() + i);
  493. break;
  494. }
  495. }
  496. }
  497. /* OBS Callbacks */
  498. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  499. {
  500. OBSBasic *window = static_cast<OBSBasic*>(data);
  501. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  502. QMetaObject::invokeMethod(window, "AddSceneItem",
  503. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  504. }
  505. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  506. {
  507. OBSBasic *window = static_cast<OBSBasic*>(data);
  508. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  509. QMetaObject::invokeMethod(window, "RemoveSceneItem",
  510. Q_ARG(OBSSceneItem, OBSSceneItem(item)));
  511. }
  512. void OBSBasic::SourceAdded(void *data, calldata_t params)
  513. {
  514. OBSBasic *window = static_cast<OBSBasic*>(data);
  515. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  516. if (obs_scene_fromsource(source) != NULL)
  517. QMetaObject::invokeMethod(window,
  518. "AddScene",
  519. Q_ARG(OBSSource, OBSSource(source)));
  520. else
  521. window->sourceSceneRefs[source] = 0;
  522. }
  523. void OBSBasic::SourceRemoved(void *data, calldata_t params)
  524. {
  525. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  526. if (obs_scene_fromsource(source) != NULL)
  527. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  528. "RemoveScene",
  529. Q_ARG(OBSSource, OBSSource(source)));
  530. }
  531. void OBSBasic::SourceActivated(void *data, calldata_t params)
  532. {
  533. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  534. uint32_t flags = obs_source_get_output_flags(source);
  535. if (flags & OBS_SOURCE_AUDIO)
  536. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  537. "ActivateAudioSource",
  538. Q_ARG(OBSSource, OBSSource(source)));
  539. }
  540. void OBSBasic::SourceDeactivated(void *data, calldata_t params)
  541. {
  542. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  543. uint32_t flags = obs_source_get_output_flags(source);
  544. if (flags & OBS_SOURCE_AUDIO)
  545. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  546. "DeactivateAudioSource",
  547. Q_ARG(OBSSource, OBSSource(source)));
  548. }
  549. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  550. {
  551. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  552. uint32_t channel = (uint32_t)calldata_int(params, "channel");
  553. if (channel == 0)
  554. QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
  555. "UpdateSceneSelection",
  556. Q_ARG(OBSSource, OBSSource(source)));
  557. }
  558. void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
  559. {
  560. OBSBasic *window = static_cast<OBSBasic*>(data);
  561. obs_video_info ovi;
  562. int newCX, newCY;
  563. obs_get_video_info(&ovi);
  564. newCX = int(window->previewScale * float(ovi.base_width));
  565. newCY = int(window->previewScale * float(ovi.base_height));
  566. gs_viewport_push();
  567. gs_projection_push();
  568. gs_ortho(0.0f, float(ovi.base_width), 0.0f, float(ovi.base_height),
  569. -100.0f, 100.0f);
  570. gs_setviewport(window->previewX, window->previewY, newCX, newCY);
  571. obs_render_main_view();
  572. gs_projection_pop();
  573. gs_viewport_pop();
  574. UNUSED_PARAMETER(cx);
  575. UNUSED_PARAMETER(cy);
  576. }
  577. /* Main class functions */
  578. obs_service_t OBSBasic::GetService()
  579. {
  580. if (!service)
  581. service = obs_service_create("rtmp_common", NULL, NULL);
  582. return service;
  583. }
  584. void OBSBasic::SetService(obs_service_t newService)
  585. {
  586. if (newService) {
  587. if (service)
  588. obs_service_destroy(service);
  589. service = newService;
  590. }
  591. }
  592. bool OBSBasic::ResetVideo()
  593. {
  594. struct obs_video_info ovi;
  595. GetConfigFPS(ovi.fps_num, ovi.fps_den);
  596. ovi.graphics_module = App()->GetRenderModule();
  597. ovi.base_width = (uint32_t)config_get_uint(basicConfig,
  598. "Video", "BaseCX");
  599. ovi.base_height = (uint32_t)config_get_uint(basicConfig,
  600. "Video", "BaseCY");
  601. ovi.output_width = (uint32_t)config_get_uint(basicConfig,
  602. "Video", "OutputCX");
  603. ovi.output_height = (uint32_t)config_get_uint(basicConfig,
  604. "Video", "OutputCY");
  605. ovi.output_format = VIDEO_FORMAT_NV12;
  606. ovi.adapter = 0;
  607. ovi.gpu_conversion = true;
  608. QTToGSWindow(ui->preview->winId(), ovi.window);
  609. //required to make opengl display stuff on osx(?)
  610. ResizePreview(ovi.base_width, ovi.base_height);
  611. QSize size = GetPixelSize(ui->preview);
  612. ovi.window_width = size.width();
  613. ovi.window_height = size.height();
  614. if (!obs_reset_video(&ovi))
  615. return false;
  616. obs_add_draw_callback(OBSBasic::RenderMain, this);
  617. return true;
  618. }
  619. bool OBSBasic::ResetAudio()
  620. {
  621. struct audio_output_info ai;
  622. ai.name = "Main Audio Track";
  623. ai.format = AUDIO_FORMAT_FLOAT;
  624. ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
  625. "SampleRate");
  626. const char *channelSetupStr = config_get_string(basicConfig,
  627. "Audio", "ChannelSetup");
  628. if (strcmp(channelSetupStr, "Mono") == 0)
  629. ai.speakers = SPEAKERS_MONO;
  630. else
  631. ai.speakers = SPEAKERS_STEREO;
  632. ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
  633. return obs_reset_audio(&ai);
  634. }
  635. void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceName,
  636. const char *deviceDesc, int channel)
  637. {
  638. const char *deviceId = config_get_string(basicConfig, "Audio",
  639. deviceName);
  640. obs_source_t source;
  641. obs_data_t settings;
  642. bool same = false;
  643. source = obs_get_output_source(channel);
  644. if (source) {
  645. settings = obs_source_getsettings(source);
  646. const char *curId = obs_data_getstring(settings, "device_id");
  647. same = (strcmp(curId, deviceId) == 0);
  648. obs_data_release(settings);
  649. obs_source_release(source);
  650. }
  651. if (!same)
  652. obs_set_output_source(channel, nullptr);
  653. if (!same && strcmp(deviceId, "disabled") != 0) {
  654. obs_data_t settings = obs_data_create();
  655. obs_data_setstring(settings, "device_id", deviceId);
  656. source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
  657. sourceId, deviceDesc, settings);
  658. obs_data_release(settings);
  659. obs_set_output_source(channel, source);
  660. obs_source_release(source);
  661. }
  662. }
  663. void OBSBasic::ResetAudioDevices()
  664. {
  665. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice1",
  666. Str("Basic.DesktopDevice1"), 1);
  667. ResetAudioDevice(App()->OutputAudioSource(), "DesktopDevice2",
  668. Str("Basic.DesktopDevice2"), 2);
  669. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice1",
  670. Str("Basic.AuxDevice1"), 3);
  671. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice2",
  672. Str("Basic.AuxDevice2"), 4);
  673. ResetAudioDevice(App()->InputAudioSource(), "AuxDevice3",
  674. Str("Basic.AuxDevice3"), 5);
  675. }
  676. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  677. {
  678. QSize targetSize;
  679. /* resize preview panel to fix to the top section of the window */
  680. targetSize = GetPixelSize(ui->preview);
  681. GetScaleAndCenterPos(int(cx), int(cy),
  682. targetSize.width(), targetSize.height(),
  683. previewX, previewY, previewScale);
  684. if (isVisible()) {
  685. if (resizeTimer)
  686. killTimer(resizeTimer);
  687. resizeTimer = startTimer(100);
  688. }
  689. }
  690. void OBSBasic::closeEvent(QCloseEvent *event)
  691. {
  692. QWidget::closeEvent(event);
  693. if (!event->isAccepted())
  694. return;
  695. // remove draw callback in case our drawable surfaces go away before
  696. // the destructor gets called
  697. obs_remove_draw_callback(OBSBasic::RenderMain, this);
  698. }
  699. void OBSBasic::changeEvent(QEvent *event)
  700. {
  701. /* TODO */
  702. UNUSED_PARAMETER(event);
  703. }
  704. void OBSBasic::resizeEvent(QResizeEvent *event)
  705. {
  706. struct obs_video_info ovi;
  707. if (obs_get_video_info(&ovi))
  708. ResizePreview(ovi.base_width, ovi.base_height);
  709. UNUSED_PARAMETER(event);
  710. }
  711. void OBSBasic::timerEvent(QTimerEvent *event)
  712. {
  713. if (event->timerId() == resizeTimer) {
  714. killTimer(resizeTimer);
  715. resizeTimer = 0;
  716. QSize size = GetPixelSize(ui->preview);
  717. obs_resize(size.width(), size.height());
  718. }
  719. }
  720. void OBSBasic::on_action_New_triggered()
  721. {
  722. /* TODO */
  723. }
  724. void OBSBasic::on_action_Open_triggered()
  725. {
  726. /* TODO */
  727. }
  728. void OBSBasic::on_action_Save_triggered()
  729. {
  730. /* TODO */
  731. }
  732. void OBSBasic::on_action_Settings_triggered()
  733. {
  734. OBSBasicSettings settings(this);
  735. settings.exec();
  736. }
  737. void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
  738. QListWidgetItem *prev)
  739. {
  740. obs_source_t source = NULL;
  741. if (sceneChanging)
  742. return;
  743. if (current) {
  744. obs_scene_t scene;
  745. scene = current->data(Qt::UserRole).value<OBSScene>();
  746. source = obs_scene_getsource(scene);
  747. }
  748. /* TODO: allow transitions */
  749. obs_set_output_source(0, source);
  750. UNUSED_PARAMETER(prev);
  751. }
  752. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  753. {
  754. /* TODO */
  755. UNUSED_PARAMETER(pos);
  756. }
  757. void OBSBasic::on_actionAddScene_triggered()
  758. {
  759. string name;
  760. QString format{QTStr("Basic.Main.DefaultSceneName.Text")};
  761. int i = 1;
  762. QString placeHolderText = format.arg(i);
  763. obs_source_t source = nullptr;
  764. while ((source = obs_get_source_by_name(QT_TO_UTF8(placeHolderText)))) {
  765. obs_source_release(source);
  766. placeHolderText = format.arg(++i);
  767. }
  768. bool accepted = NameDialog::AskForName(this,
  769. QTStr("Basic.Main.AddSceneDlg.Title"),
  770. QTStr("Basic.Main.AddSceneDlg.Text"),
  771. name,
  772. placeHolderText);
  773. if (accepted) {
  774. if (name.empty()) {
  775. QMessageBox::information(this,
  776. QTStr("NoNameEntered"),
  777. QTStr("NoNameEntered"));
  778. on_actionAddScene_triggered();
  779. return;
  780. }
  781. obs_source_t source = obs_get_source_by_name(name.c_str());
  782. if (source) {
  783. QMessageBox::information(this,
  784. QTStr("NameExists.Title"),
  785. QTStr("NameExists.Text"));
  786. obs_source_release(source);
  787. on_actionAddScene_triggered();
  788. return;
  789. }
  790. obs_scene_t scene = obs_scene_create(name.c_str());
  791. source = obs_scene_getsource(scene);
  792. obs_add_source(source);
  793. obs_scene_release(scene);
  794. obs_set_output_source(0, source);
  795. }
  796. }
  797. void OBSBasic::on_actionRemoveScene_triggered()
  798. {
  799. QListWidgetItem *item = ui->scenes->currentItem();
  800. if (!item)
  801. return;
  802. QVariant userData = item->data(Qt::UserRole);
  803. obs_scene_t scene = userData.value<OBSScene>();
  804. obs_source_t source = obs_scene_getsource(scene);
  805. obs_source_remove(source);
  806. }
  807. void OBSBasic::on_actionSceneProperties_triggered()
  808. {
  809. /* TODO */
  810. }
  811. void OBSBasic::on_actionSceneUp_triggered()
  812. {
  813. /* TODO */
  814. }
  815. void OBSBasic::on_actionSceneDown_triggered()
  816. {
  817. /* TODO */
  818. }
  819. void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
  820. QListWidgetItem *prev)
  821. {
  822. /* TODO */
  823. UNUSED_PARAMETER(current);
  824. UNUSED_PARAMETER(prev);
  825. }
  826. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  827. {
  828. /* TODO */
  829. UNUSED_PARAMETER(pos);
  830. }
  831. void OBSBasic::AddSource(const char *id)
  832. {
  833. OBSBasicSourceSelect sourceSelect(this, id);
  834. sourceSelect.exec();
  835. }
  836. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  837. {
  838. const char *type;
  839. bool foundValues = false;
  840. size_t idx = 0;
  841. if (!GetCurrentScene()) {
  842. // Tell the user he needs a scene first (help beginners).
  843. QMessageBox::information(this,
  844. QTStr("Basic.Main.AddSourceHelp.Title"),
  845. QTStr("Basic.Main.AddSourceHelp.Text"));
  846. return;
  847. }
  848. QMenu popup;
  849. while (obs_enum_input_types(idx++, &type)) {
  850. const char *name = obs_source_getdisplayname(
  851. OBS_SOURCE_TYPE_INPUT,
  852. type, App()->GetLocale());
  853. if (strcmp(type, "scene") == 0)
  854. continue;
  855. QAction *popupItem = new QAction(QT_UTF8(name), this);
  856. popupItem->setData(QT_UTF8(type));
  857. popup.addAction(popupItem);
  858. foundValues = true;
  859. }
  860. if (foundValues) {
  861. QAction *ret = popup.exec(pos);
  862. if (ret)
  863. AddSource(ret->data().toString().toUtf8());
  864. }
  865. }
  866. void OBSBasic::on_actionAddSource_triggered()
  867. {
  868. AddSourcePopupMenu(QCursor::pos());
  869. }
  870. void OBSBasic::on_actionRemoveSource_triggered()
  871. {
  872. OBSSceneItem item = GetCurrentSceneItem();
  873. if (item)
  874. obs_sceneitem_remove(item);
  875. }
  876. void OBSBasic::on_actionSourceProperties_triggered()
  877. {
  878. OBSSceneItem item = GetCurrentSceneItem();
  879. OBSSource source = obs_sceneitem_getsource(item);
  880. if (source) {
  881. delete properties;
  882. properties = new OBSBasicProperties(this, source);
  883. properties->Init();
  884. }
  885. }
  886. void OBSBasic::on_actionSourceUp_triggered()
  887. {
  888. }
  889. void OBSBasic::on_actionSourceDown_triggered()
  890. {
  891. }
  892. void OBSBasic::StreamingStart()
  893. {
  894. ui->streamButton->setText("Stop Streaming");
  895. }
  896. void OBSBasic::StreamingStop(int code)
  897. {
  898. const char *errorMessage;
  899. switch (code) {
  900. case OBS_OUTPUT_BAD_PATH:
  901. errorMessage = Str("Output.ConnectFail.BadPath");
  902. break;
  903. case OBS_OUTPUT_CONNECT_FAILED:
  904. errorMessage = Str("Output.ConnectFail.ConnectFailed");
  905. break;
  906. case OBS_OUTPUT_INVALID_STREAM:
  907. errorMessage = Str("Output.ConnectFail.InvalidStream");
  908. break;
  909. case OBS_OUTPUT_ERROR:
  910. errorMessage = Str("Output.ConnectFail.Error");
  911. break;
  912. case OBS_OUTPUT_DISCONNECTED:
  913. /* doesn't happen if output is set to reconnect. note that
  914. * reconnects are handled in the output, not in the UI */
  915. errorMessage = Str("Output.ConnectFail.Disconnected");
  916. }
  917. ui->streamButton->setText(QTStr("Basic.Main.StartStreaming"));
  918. if (code != OBS_OUTPUT_SUCCESS)
  919. QMessageBox::information(this,
  920. QTStr("Output.ConnectFail.Title"),
  921. QT_UTF8(errorMessage));
  922. }
  923. void OBSBasic::on_streamButton_clicked()
  924. {
  925. if (obs_output_active(streamOutput)) {
  926. obs_output_stop(streamOutput);
  927. } else {
  928. obs_data_t x264Settings = obs_data_create();
  929. obs_data_t aacSettings = obs_data_create();
  930. int videoBitrate = config_get_uint(basicConfig, "SimpleOutput",
  931. "VBitrate");
  932. int audioBitrate = config_get_uint(basicConfig, "SimpleOutput",
  933. "ABitrate");
  934. SaveService();
  935. obs_data_setint(x264Settings, "bitrate", videoBitrate);
  936. obs_data_setbool(x264Settings, "cbr", true);
  937. obs_data_setint(aacSettings, "bitrate", audioBitrate);
  938. obs_encoder_update(x264, x264Settings);
  939. obs_encoder_update(aac, aacSettings);
  940. obs_data_release(x264Settings);
  941. obs_data_release(aacSettings);
  942. obs_encoder_set_video(x264, obs_video());
  943. obs_encoder_set_audio(aac, obs_audio());
  944. obs_output_set_video_encoder(streamOutput, x264);
  945. obs_output_set_audio_encoder(streamOutput, aac);
  946. obs_output_set_service(streamOutput, service);
  947. obs_output_start(streamOutput);
  948. }
  949. }
  950. void OBSBasic::on_settingsButton_clicked()
  951. {
  952. OBSBasicSettings settings(this);
  953. settings.exec();
  954. }
  955. void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
  956. {
  957. const char *val = config_get_string(basicConfig, "Video", "FPSCommon");
  958. if (strcmp(val, "10") == 0) {
  959. num = 10;
  960. den = 1;
  961. } else if (strcmp(val, "20") == 0) {
  962. num = 20;
  963. den = 1;
  964. } else if (strcmp(val, "25") == 0) {
  965. num = 25;
  966. den = 1;
  967. } else if (strcmp(val, "29.97") == 0) {
  968. num = 30000;
  969. den = 1001;
  970. } else if (strcmp(val, "48") == 0) {
  971. num = 48;
  972. den = 1;
  973. } else if (strcmp(val, "59.94") == 0) {
  974. num = 60000;
  975. den = 1001;
  976. } else if (strcmp(val, "60") == 0) {
  977. num = 60;
  978. den = 1;
  979. } else {
  980. num = 30;
  981. den = 1;
  982. }
  983. }
  984. void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
  985. {
  986. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
  987. den = 1;
  988. }
  989. void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
  990. {
  991. num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
  992. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
  993. }
  994. void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  995. {
  996. num = 1000000000;
  997. den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
  998. }
  999. void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
  1000. {
  1001. uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");
  1002. if (type == 1) //"Integer"
  1003. GetFPSInteger(num, den);
  1004. else if (type == 2) //"Fraction"
  1005. GetFPSFraction(num, den);
  1006. else if (false) //"Nanoseconds", currently not implemented
  1007. GetFPSNanoseconds(num, den);
  1008. else
  1009. GetFPSCommon(num, den);
  1010. }
  1011. config_t OBSBasic::Config() const
  1012. {
  1013. return basicConfig;
  1014. }