settings-basic-video.cpp 9.9 KB

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