window-basic-main.cpp 12 KB

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