settings-basic-video.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 <util/lexer.h>
  15. #include "obs-app.hpp"
  16. #include "settings-basic.hpp"
  17. #include "window-settings-basic.hpp"
  18. #include "wx-wrappers.hpp"
  19. #include "platform.hpp"
  20. #include <sstream>
  21. #include <unordered_set>
  22. using namespace std;
  23. class BasicVideoData : public BasicSettingsData {
  24. ConnectorList connections;
  25. int AddRes(uint32_t cx, uint32_t cy);
  26. void LoadOther();
  27. void LoadResolutionData();
  28. void LoadFPSData();
  29. void LoadFPSCommon();
  30. void LoadFPSInteger();
  31. void LoadFPSFraction();
  32. void LoadFPSNanoseconds();
  33. void ResetScaleList(uint32_t cx, uint32_t cy);
  34. void BaseResListChanged(wxCommandEvent &event);
  35. void OutputResListChanged(wxCommandEvent &event);
  36. void SaveOther();
  37. void SaveFPSData();
  38. void SaveFPSCommon();
  39. void SaveFPSInteger();
  40. void SaveFPSFraction();
  41. void SaveFPSNanoseconds();
  42. virtual void SetChanged()
  43. {
  44. BasicSettingsData::SetChanged();
  45. window->videoChangedText->Show();
  46. }
  47. public:
  48. BasicVideoData(OBSBasicSettings *window);
  49. virtual void Apply();
  50. };
  51. struct BaseLexer {
  52. lexer lex;
  53. public:
  54. inline BaseLexer() {lexer_init(&lex);}
  55. inline ~BaseLexer() {lexer_free(&lex);}
  56. operator lexer*() {return &lex;}
  57. };
  58. /* parses "[width]x[height]", string, i.e. 1024x768 */
  59. static bool ConvertTextRes(wxComboBox *combo, uint32_t &cx, uint32_t &cy)
  60. {
  61. string str = combo->GetValue().ToStdString();
  62. BaseLexer lex;
  63. lexer_start(lex, str.c_str());
  64. base_token token;
  65. /* parse width */
  66. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  67. return false;
  68. if (token.type != BASETOKEN_DIGIT)
  69. return false;
  70. cx = std::stoul(token.text.array);
  71. /* parse 'x' */
  72. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  73. return false;
  74. if (strref_cmpi(&token.text, "x") != 0)
  75. return false;
  76. /* parse height */
  77. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  78. return false;
  79. if (token.type != BASETOKEN_DIGIT)
  80. return false;
  81. cy = std::stoul(token.text.array);
  82. /* shouldn't be any more tokens after this */
  83. if (lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  84. return false;
  85. return true;
  86. }
  87. int BasicVideoData::AddRes(uint32_t cx, uint32_t cy)
  88. {
  89. stringstream res;
  90. res << cx << "x" << cy;
  91. return window->baseResList->Append(res.str());
  92. }
  93. void BasicVideoData::LoadOther()
  94. {
  95. const char *renderer = config_get_string(GetGlobalConfig(), "Video",
  96. "Renderer");
  97. window->rendererList->Clear();
  98. window->rendererList->Append("OpenGL");
  99. #ifdef _WIN32
  100. window->rendererList->Append("Direct3D 11");
  101. #endif
  102. int sel = window->rendererList->FindString(renderer);
  103. if (sel == wxNOT_FOUND)
  104. sel = 0;
  105. window->rendererList->SetSelection(sel);
  106. }
  107. static uint64_t append_uint32_t(uint64_t first, uint64_t second)
  108. {
  109. return (first << 32) | second;
  110. }
  111. void BasicVideoData::LoadResolutionData()
  112. {
  113. window->baseResList->Clear();
  114. uint32_t cx = config_get_uint(GetGlobalConfig(), "Video", "BaseCX");
  115. uint32_t cy = config_get_uint(GetGlobalConfig(), "Video", "BaseCY");
  116. vector<MonitorInfo> monitors;
  117. GetMonitors(monitors);
  118. unordered_set<uint64_t> resolutions;
  119. for (size_t i = 0; i < monitors.size(); i++) {
  120. uint64_t res = append_uint32_t(monitors[i].cx, monitors[i].cy);
  121. if(resolutions.emplace(res).second)
  122. AddRes(monitors[i].cx, monitors[i].cy);
  123. }
  124. stringstream res;
  125. res << cx << "x" << cy;
  126. window->baseResList->SetValue(res.str());
  127. ResetScaleList(cx, cy);
  128. cx = config_get_uint(GetGlobalConfig(), "Video", "OutputCX");
  129. cy = config_get_uint(GetGlobalConfig(), "Video", "OutputCY");
  130. res.str(string());
  131. res.clear();
  132. res << cx << "x" << cy;
  133. window->outputResList->SetValue(res.str());
  134. }
  135. void BasicVideoData::LoadFPSData()
  136. {
  137. const char *fpsType = config_get_string(GetGlobalConfig(), "Video",
  138. "FPSType");
  139. LoadFPSCommon();
  140. LoadFPSInteger();
  141. LoadFPSFraction();
  142. LoadFPSNanoseconds();
  143. if (!fpsType)
  144. return;
  145. else if (strcmp(fpsType, "Common") == 0)
  146. window->fpsTypeList->SetSelection(0);
  147. else if (strcmp(fpsType, "Integer") == 0)
  148. window->fpsTypeList->SetSelection(1);
  149. else if (strcmp(fpsType, "Fraction") == 0)
  150. window->fpsTypeList->SetSelection(2);
  151. else if (strcmp(fpsType, "Nanoseconds") == 0)
  152. window->fpsTypeList->SetSelection(3);
  153. }
  154. void BasicVideoData::LoadFPSCommon()
  155. {
  156. const char *str = config_get_string(GetGlobalConfig(), "Video",
  157. "FPSCommon");
  158. int val = 3;
  159. if (strcmp(str, "10") == 0)
  160. val = 0;
  161. else if (strcmp(str, "20") == 0)
  162. val = 1;
  163. else if (strcmp(str, "29.97") == 0)
  164. val = 2;
  165. else if (strcmp(str, "30") == 0)
  166. val = 3;
  167. else if (strcmp(str, "48") == 0)
  168. val = 4;
  169. else if (strcmp(str, "59.94") == 0)
  170. val = 5;
  171. else if (strcmp(str, "60") == 0)
  172. val = 6;
  173. window->fpsCommonList->SetSelection(val);
  174. }
  175. void BasicVideoData::LoadFPSInteger()
  176. {
  177. window->fpsTypeList->SetSelection(1);
  178. int val = config_get_int(GetGlobalConfig(), "Video", "FPSInt");
  179. window->fpsIntegerScroller->SetValue(val);
  180. }
  181. void BasicVideoData::LoadFPSFraction()
  182. {
  183. window->fpsTypeList->SetSelection(2);
  184. int num = config_get_int(GetGlobalConfig(), "Video", "FPSNum");
  185. int den = config_get_int(GetGlobalConfig(), "Video", "FPSDen");
  186. window->fpsNumeratorScroller->SetValue(num);
  187. window->fpsDenominatorScroller->SetValue(den);
  188. }
  189. void BasicVideoData::LoadFPSNanoseconds()
  190. {
  191. window->fpsTypeList->SetSelection(3);
  192. int val = config_get_int(GetGlobalConfig(), "Video", "FPSNS");
  193. window->fpsNanosecondsScroller->SetValue(val);
  194. }
  195. /* some nice default output resolution vals */
  196. static const double vals[] =
  197. {
  198. 1.0,
  199. 1.25,
  200. (1.0/0.75),
  201. 1.5,
  202. (1.0/0.6),
  203. 1.75,
  204. 2.0,
  205. 2.25,
  206. 2.5,
  207. 2.75,
  208. 3.0
  209. };
  210. static const size_t numVals = sizeof(vals)/sizeof(double);
  211. void BasicVideoData::ResetScaleList(uint32_t cx, uint32_t cy)
  212. {
  213. window->outputResList->Clear();
  214. for (size_t i = 0; i < numVals; i++) {
  215. stringstream res;
  216. res << uint32_t(double(cx) / vals[i]);
  217. res << "x";
  218. res << uint32_t(double(cy) / vals[i]);
  219. window->outputResList->Append(res.str());
  220. }
  221. if (numVals) {
  222. stringstream str;
  223. str << cx << "x" << cy;
  224. window->outputResList->SetValue(str.str());
  225. }
  226. }
  227. BasicVideoData::BasicVideoData(OBSBasicSettings *window)
  228. : BasicSettingsData(window)
  229. {
  230. LoadResolutionData();
  231. LoadFPSData();
  232. LoadOther();
  233. /* load connectors after loading data to prevent them from triggering */
  234. connections.Add(window->baseResList, wxEVT_TEXT,
  235. wxCommandEventHandler(
  236. BasicVideoData::BaseResListChanged),
  237. NULL, this);
  238. connections.Add(window->outputResList, wxEVT_TEXT,
  239. wxCommandEventHandler(
  240. BasicVideoData::OutputResListChanged),
  241. NULL, this);
  242. window->videoChangedText->Hide();
  243. }
  244. void BasicVideoData::BaseResListChanged(wxCommandEvent &event)
  245. {
  246. uint32_t cx, cy;
  247. if (!ConvertTextRes(window->baseResList, cx, cy)) {
  248. window->videoChangedText->SetLabel(
  249. WXStr("Settings.Video.InvalidResolution"));
  250. window->videoChangedText->Show();
  251. return;
  252. }
  253. SetChanged();
  254. window->videoChangedText->SetLabel(WXStr("Settings.StreamRestart"));
  255. window->videoChangedText->Show();
  256. ResetScaleList(cx, cy);
  257. }
  258. void BasicVideoData::OutputResListChanged(wxCommandEvent &event)
  259. {
  260. uint32_t cx, cy;
  261. if (!ConvertTextRes(window->outputResList, cx, cy)) {
  262. window->videoChangedText->SetLabel(
  263. WXStr("Settings.Video.InvalidResolution"));
  264. window->videoChangedText->Show();
  265. return;
  266. }
  267. SetChanged();
  268. window->videoChangedText->SetLabel(WXStr("Settings.StreamRestart"));
  269. window->videoChangedText->Show();
  270. }
  271. void BasicVideoData::SaveOther()
  272. {
  273. int sel = window->rendererList->GetSelection();
  274. if (sel == wxNOT_FOUND)
  275. return;
  276. wxString renderer = window->rendererList->GetString(sel);
  277. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  278. renderer.c_str());
  279. }
  280. void BasicVideoData::SaveFPSData()
  281. {
  282. int id = window->fpsTypeList->GetCurrentPage()->GetId();
  283. const char *type;
  284. switch (id) {
  285. case ID_FPSPANEL_COMMON: type = "Common"; break;
  286. case ID_FPSPANEL_INTEGER: type = "Integer"; break;
  287. case ID_FPSPANEL_FRACTION: type = "Fraction"; break;
  288. case ID_FPSPANEL_NANOSECONDS: type = "Nanoseconds"; break;
  289. }
  290. config_set_string(GetGlobalConfig(), "Video", "FPSType", type);
  291. SaveFPSCommon();
  292. SaveFPSInteger();
  293. SaveFPSFraction();
  294. SaveFPSNanoseconds();
  295. }
  296. void BasicVideoData::SaveFPSCommon()
  297. {
  298. int sel = window->fpsCommonList->GetSelection();
  299. wxString str = window->fpsCommonList->GetString(sel);
  300. config_set_string(GetGlobalConfig(), "Video", "FPSCommon", str.c_str());
  301. }
  302. void BasicVideoData::SaveFPSInteger()
  303. {
  304. int val = window->fpsIntegerScroller->GetValue();
  305. config_set_int(GetGlobalConfig(), "Video", "FPSInt", val);
  306. }
  307. void BasicVideoData::SaveFPSFraction()
  308. {
  309. int num = window->fpsNumeratorScroller->GetValue();
  310. int den = window->fpsDenominatorScroller->GetValue();
  311. config_set_int(GetGlobalConfig(), "Video", "FPSNum", num);
  312. config_set_int(GetGlobalConfig(), "Video", "FPSDen", den);
  313. }
  314. void BasicVideoData::SaveFPSNanoseconds()
  315. {
  316. int val = window->fpsNanosecondsScroller->GetValue();
  317. config_set_int(GetGlobalConfig(), "Video", "FPSNS", val);
  318. }
  319. void BasicVideoData::Apply()
  320. {
  321. uint32_t cx, cy;
  322. if (!ConvertTextRes(window->baseResList, cx, cy)) {
  323. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  324. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  325. }
  326. if (ConvertTextRes(window->outputResList, cx, cy)) {
  327. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  328. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  329. }
  330. SaveOther();
  331. SaveFPSData();
  332. config_save(GetGlobalConfig());
  333. SetSaved();
  334. }
  335. BasicSettingsData *CreateBasicVideoSettings(OBSBasicSettings *window)
  336. {
  337. return new BasicVideoData(window);
  338. }