window-basic-main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs.hpp>
  15. #include <wx/msgdlg.h>
  16. #include "obs-app.hpp"
  17. #include "wx-wrappers.hpp"
  18. #include "window-basic-settings.hpp"
  19. #include "window-basic-main.hpp"
  20. #include "window-namedialog.hpp"
  21. using namespace std;
  22. obs_scene_t OBSBasic::GetCurrentScene()
  23. {
  24. int sel = scenes->GetSelection();
  25. if (sel == wxNOT_FOUND)
  26. return NULL;
  27. return (obs_scene_t)scenes->GetClientData(sel);
  28. }
  29. void OBSBasic::AddScene(obs_source_t source)
  30. {
  31. const char *name = obs_source_getname(source);
  32. obs_scene_t scene = obs_scene_fromsource(source);
  33. scenes->Append(WX_UTF8(name), scene);
  34. signal_handler_t handler = obs_source_signalhandler(source);
  35. signal_handler_connect(handler, "add", OBSBasic::SceneItemAdded,
  36. this);
  37. signal_handler_connect(handler, "remove", OBSBasic::SceneItemRemoved,
  38. this);
  39. }
  40. void OBSBasic::RemoveScene(obs_source_t source)
  41. {
  42. const char *name = obs_source_getname(source);
  43. int idx = scenes->FindString(name);
  44. int sel = scenes->GetSelection();
  45. if (idx != wxNOT_FOUND) {
  46. if (sel == idx)
  47. sources->Clear();
  48. scenes->Delete(idx);
  49. }
  50. }
  51. void OBSBasic::AddSceneItem(obs_sceneitem_t item)
  52. {
  53. obs_scene_t scene = obs_sceneitem_getscene(item);
  54. obs_source_t source = obs_sceneitem_getsource(item);
  55. const char *name = obs_source_getname(source);
  56. if (GetCurrentScene() == scene)
  57. sources->Insert(WX_UTF8(name), 0, item);
  58. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  59. }
  60. void OBSBasic::RemoveSceneItem(obs_sceneitem_t item)
  61. {
  62. obs_scene_t scene = obs_sceneitem_getscene(item);
  63. if (GetCurrentScene() == scene) {
  64. for (unsigned int idx = 0; idx < sources->GetCount(); idx++) {
  65. obs_sceneitem_t curItem;
  66. curItem = (obs_sceneitem_t)sources->GetClientData(idx);
  67. if (item == curItem) {
  68. sources->Delete(idx);
  69. break;
  70. }
  71. }
  72. }
  73. obs_source_t source = obs_sceneitem_getsource(item);
  74. obs_source_addref(source);
  75. obs_source_release(source);
  76. int scenes = sourceSceneRefs[source] - 1;
  77. if (scenes == 0) {
  78. obs_source_remove(source);
  79. sourceSceneRefs.erase(source);
  80. }
  81. }
  82. void OBSBasic::UpdateSources(obs_scene_t scene)
  83. {
  84. sources->Clear();
  85. obs_scene_enum_items(scene,
  86. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  87. {
  88. OBSBasic *window = static_cast<OBSBasic*>(p);
  89. window->AddSceneItem(item);
  90. return true;
  91. }, this);
  92. }
  93. void OBSBasic::UpdateSceneSelection(obs_source_t source)
  94. {
  95. if (source) {
  96. obs_source_type type;
  97. obs_source_gettype(source, &type, NULL);
  98. if (type == SOURCE_SCENE) {
  99. obs_scene_t scene = obs_scene_fromsource(source);
  100. const char *name = obs_source_getname(source);
  101. int idx = scenes->FindString(WX_UTF8(name));
  102. int sel = scenes->GetSelection();
  103. if (idx != sel) {
  104. scenes->SetSelection(idx);
  105. UpdateSources(scene);
  106. }
  107. }
  108. } else {
  109. scenes->SetSelection(wxNOT_FOUND);
  110. }
  111. }
  112. /* OBS Callbacks */
  113. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  114. {
  115. OBSBasic *window = static_cast<OBSBasic*>(data);
  116. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  117. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  118. window->AddSceneItem(item);
  119. }
  120. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  121. {
  122. OBSBasic *window = static_cast<OBSBasic*>(data);
  123. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  124. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  125. window->RemoveSceneItem(item);
  126. }
  127. void OBSBasic::SourceAdded(void *data, calldata_t params)
  128. {
  129. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  130. obs_source_type type;
  131. obs_source_gettype(source, &type, NULL);
  132. if (type == SOURCE_SCENE)
  133. static_cast<OBSBasic*>(data)->AddScene(source);
  134. }
  135. void OBSBasic::SourceDestroyed(void *data, calldata_t params)
  136. {
  137. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  138. obs_source_type type;
  139. obs_source_gettype(source, &type, NULL);
  140. if (type == SOURCE_SCENE)
  141. static_cast<OBSBasic*>(data)->RemoveScene(source);
  142. }
  143. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  144. {
  145. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  146. uint32_t channel = calldata_uint32(params, "channel");
  147. if (channel == 0)
  148. static_cast<OBSBasic*>(data)->UpdateSceneSelection(source);
  149. }
  150. /* Main class functions */
  151. bool OBSBasic::Init()
  152. {
  153. if (!obs_startup())
  154. return false;
  155. if (!InitGraphics())
  156. return false;
  157. if (!InitAudio())
  158. return false;
  159. signal_handler_connect(obs_signalhandler(), "source-add",
  160. OBSBasic::SourceAdded, this);
  161. signal_handler_connect(obs_signalhandler(), "source-destroy",
  162. OBSBasic::SourceDestroyed, this);
  163. signal_handler_connect(obs_signalhandler(), "channel-change",
  164. OBSBasic::ChannelChanged, this);
  165. /* TODO: this is a test */
  166. obs_load_module("test-input");
  167. return true;
  168. }
  169. OBSBasic::~OBSBasic()
  170. {
  171. obs_shutdown();
  172. }
  173. bool OBSBasic::InitGraphics()
  174. {
  175. struct obs_video_info ovi;
  176. wxGetApp().GetConfigFPS(ovi.fps_num, ovi.fps_den);
  177. ovi.graphics_module = wxGetApp().GetRenderModule();
  178. ovi.base_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  179. "Video", "BaseCX");
  180. ovi.base_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  181. "Video", "BaseCY");
  182. ovi.output_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  183. "Video", "OutputCX");
  184. ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  185. "Video", "OutputCY");
  186. ovi.output_format = VIDEO_FORMAT_RGBA;
  187. ovi.adapter = 0;
  188. ovi.window = WxToGSWindow(previewPanel);
  189. //required to make opengl display stuff on osx(?)
  190. ResizePreview(ovi.base_width, ovi.base_height);
  191. SendSizeEvent();
  192. wxSize size = previewPanel->GetClientSize();
  193. ovi.window_width = size.x;
  194. ovi.window_height = size.y;
  195. return obs_reset_video(&ovi);
  196. }
  197. bool OBSBasic::InitAudio()
  198. {
  199. /* TODO: load audio settings from config */
  200. struct audio_info ai;
  201. ai.name = "test";
  202. ai.samples_per_sec = 44100;
  203. ai.format = AUDIO_FORMAT_16BIT;
  204. ai.speakers = SPEAKERS_STEREO;
  205. ai.buffer_ms = 700;
  206. return obs_reset_audio(&ai);
  207. }
  208. void OBSBasic::OnClose(wxCloseEvent &event)
  209. {
  210. wxGetApp().ExitMainLoop();
  211. event.Skip();
  212. }
  213. void OBSBasic::OnMinimize(wxIconizeEvent &event)
  214. {
  215. event.Skip();
  216. }
  217. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  218. {
  219. /* resize preview panel to fix to the top section of the window */
  220. wxSize targetSize = GetPreviewContainer()->GetSize();
  221. double targetAspect = double(targetSize.x) / double(targetSize.y);
  222. double baseAspect = double(cx) / double(cy);
  223. wxSize newSize;
  224. if (targetAspect > baseAspect)
  225. newSize = wxSize(targetSize.y * baseAspect, targetSize.y);
  226. else
  227. newSize = wxSize(targetSize.x, targetSize.x / baseAspect);
  228. GetPreviewPanel()->SetMinSize(newSize);
  229. }
  230. void OBSBasic::OnSize(wxSizeEvent &event)
  231. {
  232. struct obs_video_info ovi;
  233. event.Skip();
  234. if (!obs_get_video_info(&ovi))
  235. return;
  236. ResizePreview(ovi.base_width, ovi.base_height);
  237. }
  238. void OBSBasic::OnResizePreview(wxSizeEvent &event)
  239. {
  240. event.Skip();
  241. wxSize newSize = previewPanel->GetClientSize();
  242. graphics_t graphics = obs_graphics();
  243. if (graphics) {
  244. gs_entercontext(graphics);
  245. gs_resize(newSize.x, newSize.y);
  246. gs_leavecontext();
  247. }
  248. }
  249. void OBSBasic::fileNewClicked(wxCommandEvent &event)
  250. {
  251. }
  252. void OBSBasic::fileOpenClicked(wxCommandEvent &event)
  253. {
  254. }
  255. void OBSBasic::fileSaveClicked(wxCommandEvent &event)
  256. {
  257. }
  258. void OBSBasic::fileExitClicked(wxCommandEvent &event)
  259. {
  260. wxGetApp().ExitMainLoop();
  261. }
  262. void OBSBasic::scenesClicked(wxCommandEvent &event)
  263. {
  264. int sel = scenes->GetSelection();
  265. obs_source_t source = NULL;
  266. if (sel != wxNOT_FOUND) {
  267. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  268. source = obs_scene_getsource(scene);
  269. UpdateSources(scene);
  270. }
  271. /* TODO: allow transitions */
  272. obs_set_output_source(0, source);
  273. }
  274. void OBSBasic::scenesRDown(wxMouseEvent &event)
  275. {
  276. }
  277. void OBSBasic::sceneAddClicked(wxCommandEvent &event)
  278. {
  279. string name;
  280. int ret = NameDialog::AskForName(this,
  281. Str("MainWindow.AddSceneDlg.Title"),
  282. Str("MainWindow.AddSceneDlg.Text"),
  283. name);
  284. if (ret == wxID_OK) {
  285. obs_source_t source = obs_get_source_by_name(name.c_str());
  286. if (source) {
  287. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  288. WXStr("MainWindow.NameExists.Title"),
  289. wxOK|wxCENTRE, this);
  290. obs_source_release(source);
  291. sceneAddClicked(event);
  292. return;
  293. }
  294. obs_scene_t scene = obs_scene_create(name.c_str());
  295. source = obs_scene_getsource(scene);
  296. obs_add_source(source);
  297. obs_scene_release(scene);
  298. obs_set_output_source(0, source);
  299. }
  300. }
  301. void OBSBasic::sceneRemoveClicked(wxCommandEvent &event)
  302. {
  303. int sel = scenes->GetSelection();
  304. if (sel == wxNOT_FOUND)
  305. return;
  306. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  307. obs_source_t source = obs_scene_getsource(scene);
  308. obs_source_remove(source);
  309. }
  310. void OBSBasic::scenePropertiesClicked(wxCommandEvent &event)
  311. {
  312. }
  313. void OBSBasic::sceneUpClicked(wxCommandEvent &event)
  314. {
  315. }
  316. void OBSBasic::sceneDownClicked(wxCommandEvent &event)
  317. {
  318. }
  319. void OBSBasic::sourcesClicked(wxCommandEvent &event)
  320. {
  321. }
  322. void OBSBasic::sourcesToggled(wxCommandEvent &event)
  323. {
  324. }
  325. void OBSBasic::sourcesRDown(wxMouseEvent &event)
  326. {
  327. }
  328. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  329. {
  330. string name;
  331. bool success = false;
  332. while (!success) {
  333. int ret = NameDialog::AskForName(this,
  334. Str("MainWindow.AddSourceDlg.Title"),
  335. Str("MainWindow.AddSourceDlg.Text"),
  336. name);
  337. if (ret == wxID_CANCEL)
  338. break;
  339. obs_source_t source = obs_get_source_by_name(
  340. name.c_str());
  341. if (!source) {
  342. success = true;
  343. } else {
  344. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  345. WXStr("MainWindow.NameExists.Title"),
  346. wxOK|wxCENTRE, this);
  347. obs_source_release(source);
  348. }
  349. }
  350. if (success) {
  351. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  352. name.c_str(), NULL);
  353. sourceSceneRefs[source] = 0;
  354. obs_add_source(source);
  355. obs_sceneitem_t item = obs_scene_add(scene, source);
  356. obs_source_release(source);
  357. }
  358. }
  359. void OBSBasic::AddSourcePopupMenu()
  360. {
  361. int sceneSel = scenes->GetSelection();
  362. size_t idx = 0;
  363. const char *type;
  364. vector<const char *> types;
  365. if (sceneSel == wxNOT_FOUND)
  366. return;
  367. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sceneSel);
  368. obs_scene_addref(scene);
  369. unique_ptr<wxMenu> popup(new wxMenu());
  370. while (obs_enum_input_types(idx, &type)) {
  371. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  372. type, wxGetApp().GetLocale());
  373. types.push_back(type);
  374. popup->Append((int)idx+1, wxString(name, wxConvUTF8));
  375. idx++;
  376. }
  377. if (idx) {
  378. int id = WXDoPopupMenu(this, popup.get());
  379. if (id != -1)
  380. AddSource(scene, types[id-1]);
  381. }
  382. obs_scene_release(scene);
  383. }
  384. void OBSBasic::sourceAddClicked(wxCommandEvent &event)
  385. {
  386. AddSourcePopupMenu();
  387. }
  388. void OBSBasic::sourceRemoveClicked(wxCommandEvent &event)
  389. {
  390. int sel = sources->GetSelection();
  391. if (sel == wxNOT_FOUND)
  392. return;
  393. obs_sceneitem_t item = (obs_sceneitem_t)sources->GetClientData(sel);
  394. obs_source_t source = obs_sceneitem_getsource(item);
  395. obs_sceneitem_destroy(item);
  396. }
  397. void OBSBasic::sourcePropertiesClicked(wxCommandEvent &event)
  398. {
  399. }
  400. void OBSBasic::sourceUpClicked(wxCommandEvent &event)
  401. {
  402. }
  403. void OBSBasic::sourceDownClicked(wxCommandEvent &event)
  404. {
  405. }
  406. void OBSBasic::settingsClicked(wxCommandEvent &event)
  407. {
  408. OBSBasicSettings test(this);
  409. test.ShowModal();
  410. }
  411. void OBSBasic::exitClicked(wxCommandEvent &event)
  412. {
  413. wxGetApp().ExitMainLoop();
  414. }