settings-basic-video.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. static uint64_t append_uint32_t(uint64_t first, uint64_t second)
  103. {
  104. return (first << 32) | second;
  105. }
  106. void BasicVideoData::LoadResolutionData()
  107. {
  108. window->baseResList->Clear();
  109. uint32_t cx = config_get_uint(GetGlobalConfig(), "Video", "BaseCX");
  110. uint32_t cy = config_get_uint(GetGlobalConfig(), "Video", "BaseCY");
  111. vector<MonitorInfo> monitors;
  112. GetMonitors(monitors);
  113. unordered_set<uint64_t> resolutions;
  114. for (size_t i = 0; i < monitors.size(); i++) {
  115. uint64_t res = append_uint32_t(monitors[i].cx, monitors[i].cy);
  116. if(resolutions.emplace(res).second)
  117. AddRes(monitors[i].cx, monitors[i].cy);
  118. }
  119. stringstream res;
  120. res << cx << "x" << cy;
  121. window->baseResList->SetValue(res.str());
  122. ResetScaleList(cx, cy);
  123. cx = config_get_uint(GetGlobalConfig(), "Video", "OutputCX");
  124. cy = config_get_uint(GetGlobalConfig(), "Video", "OutputCY");
  125. res.str(string());
  126. res.clear();
  127. res << cx << "x" << cy;
  128. window->outputResList->SetValue(res.str());
  129. }
  130. void BasicVideoData::LoadFPSData()
  131. {
  132. const char *fpsType = config_get_string(GetGlobalConfig(), "Video",
  133. "FPSType");
  134. LoadFPSCommon();
  135. LoadFPSInteger();
  136. LoadFPSFraction();
  137. LoadFPSNanoseconds();
  138. if (!fpsType)
  139. return;
  140. else if (strcmp(fpsType, "Common") == 0)
  141. window->fpsTypeList->SetSelection(0);
  142. else if (strcmp(fpsType, "Integer") == 0)
  143. window->fpsTypeList->SetSelection(1);
  144. else if (strcmp(fpsType, "Fraction") == 0)
  145. window->fpsTypeList->SetSelection(2);
  146. else if (strcmp(fpsType, "Nanoseconds") == 0)
  147. window->fpsTypeList->SetSelection(3);
  148. }
  149. void BasicVideoData::LoadFPSCommon()
  150. {
  151. const char *str = config_get_string(GetGlobalConfig(), "Video",
  152. "FPSCommon");
  153. int val = 3;
  154. if (strcmp(str, "10") == 0)
  155. val = 0;
  156. else if (strcmp(str, "20") == 0)
  157. val = 1;
  158. else if (strcmp(str, "29.97") == 0)
  159. val = 2;
  160. else if (strcmp(str, "30") == 0)
  161. val = 3;
  162. else if (strcmp(str, "48") == 0)
  163. val = 4;
  164. else if (strcmp(str, "59.94") == 0)
  165. val = 5;
  166. else if (strcmp(str, "60") == 0)
  167. val = 6;
  168. window->fpsCommonList->SetSelection(val);
  169. }
  170. void BasicVideoData::LoadFPSInteger()
  171. {
  172. window->fpsTypeList->SetSelection(1);
  173. int val = config_get_int(GetGlobalConfig(), "Video", "FPSInt");
  174. window->fpsIntegerScroller->SetValue(val);
  175. }
  176. void BasicVideoData::LoadFPSFraction()
  177. {
  178. window->fpsTypeList->SetSelection(2);
  179. int num = config_get_int(GetGlobalConfig(), "Video", "FPSNum");
  180. int den = config_get_int(GetGlobalConfig(), "Video", "FPSDen");
  181. window->fpsNumeratorScroller->SetValue(num);
  182. window->fpsDenominatorScroller->SetValue(den);
  183. }
  184. void BasicVideoData::LoadFPSNanoseconds()
  185. {
  186. window->fpsTypeList->SetSelection(3);
  187. int val = config_get_int(GetGlobalConfig(), "Video", "FPSNS");
  188. window->fpsNanosecondsScroller->SetValue(val);
  189. }
  190. /* some nice default output resolution vals */
  191. static const double vals[] =
  192. {
  193. 1.0,
  194. 1.25,
  195. (1.0/0.75),
  196. 1.5,
  197. (1.0/0.6),
  198. 1.75,
  199. 2.0,
  200. 2.25,
  201. 2.5,
  202. 2.75,
  203. 3.0
  204. };
  205. static const size_t numVals = sizeof(vals)/sizeof(double);
  206. void BasicVideoData::ResetScaleList(uint32_t cx, uint32_t cy)
  207. {
  208. window->outputResList->Clear();
  209. for (size_t i = 0; i < numVals; i++) {
  210. stringstream res;
  211. res << uint32_t(double(cx) / vals[i]);
  212. res << "x";
  213. res << uint32_t(double(cy) / vals[i]);
  214. window->outputResList->Append(res.str().c_str());
  215. }
  216. }
  217. BasicVideoData::BasicVideoData(OBSBasicSettings *window)
  218. : BasicSettingsData(window)
  219. {
  220. LoadResolutionData();
  221. LoadFPSData();
  222. LoadOther();
  223. /* load connectors after loading data to prevent them from triggering */
  224. connections.Add(window->baseResList, wxEVT_TEXT,
  225. wxCommandEventHandler(
  226. BasicVideoData::BaseResListChanged),
  227. NULL, this);
  228. connections.Add(window->outputResList, wxEVT_TEXT,
  229. wxCommandEventHandler(
  230. BasicVideoData::OutputResListChanged),
  231. NULL, this);
  232. window->videoChangedText->Hide();
  233. }
  234. void BasicVideoData::BaseResListChanged(wxCommandEvent &event)
  235. {
  236. uint32_t cx, cy;
  237. if (!ConvertTextRes(window->baseResList, cx, cy)) {
  238. window->videoChangedText->SetLabel(
  239. WXStr("Settings.Video.InvalidResolution"));
  240. window->videoChangedText->Show();
  241. return;
  242. }
  243. dataChanged = true;
  244. window->videoChangedText->SetLabel(WXStr("Settings.StreamRestart"));
  245. window->videoChangedText->Show();
  246. ResetScaleList(cx, cy);
  247. }
  248. void BasicVideoData::OutputResListChanged(wxCommandEvent &event)
  249. {
  250. uint32_t cx, cy;
  251. if (!ConvertTextRes(window->outputResList, cx, cy)) {
  252. window->videoChangedText->SetLabel(
  253. WXStr("Settings.Video.InvalidResolution"));
  254. window->videoChangedText->Show();
  255. return;
  256. }
  257. dataChanged = true;
  258. window->videoChangedText->SetLabel(WXStr("Settings.StreamRestart"));
  259. window->videoChangedText->Show();
  260. }
  261. void BasicVideoData::SaveOther()
  262. {
  263. int sel = window->rendererList->GetSelection();
  264. if (sel == wxNOT_FOUND)
  265. return;
  266. wxString renderer = window->rendererList->GetString(sel);
  267. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  268. renderer.c_str());
  269. }
  270. void BasicVideoData::SaveFPSData()
  271. {
  272. int id = window->fpsTypeList->GetCurrentPage()->GetId();
  273. const char *type;
  274. switch (id) {
  275. case ID_FPSPANEL_COMMON: type = "Common"; break;
  276. case ID_FPSPANEL_INTEGER: type = "Integer"; break;
  277. case ID_FPSPANEL_FRACTION: type = "Fraction"; break;
  278. case ID_FPSPANEL_NANOSECONDS: type = "Nanoseconds"; break;
  279. }
  280. config_set_string(GetGlobalConfig(), "Video", "FPSType", type);
  281. SaveOther();
  282. SaveFPSCommon();
  283. SaveFPSInteger();
  284. SaveFPSFraction();
  285. SaveFPSNanoseconds();
  286. }
  287. void BasicVideoData::SaveFPSCommon()
  288. {
  289. int sel = window->fpsCommonList->GetSelection();
  290. wxString str = window->fpsCommonList->GetString(sel);
  291. config_set_string(GetGlobalConfig(), "Video", "FPSCommon", str.c_str());
  292. }
  293. void BasicVideoData::SaveFPSInteger()
  294. {
  295. int val = window->fpsIntegerScroller->GetValue();
  296. config_set_int(GetGlobalConfig(), "Video", "FPSInt", val);
  297. }
  298. void BasicVideoData::SaveFPSFraction()
  299. {
  300. int num = window->fpsNumeratorScroller->GetValue();
  301. int den = window->fpsDenominatorScroller->GetValue();
  302. config_set_int(GetGlobalConfig(), "Video", "FPSNum", num);
  303. config_set_int(GetGlobalConfig(), "Video", "FPSDen", den);
  304. }
  305. void BasicVideoData::SaveFPSNanoseconds()
  306. {
  307. int val = window->fpsNanosecondsScroller->GetValue();
  308. config_set_int(GetGlobalConfig(), "Video", "FPSNS", val);
  309. }
  310. void BasicVideoData::Apply()
  311. {
  312. uint32_t cx, cy;
  313. if (!ConvertTextRes(window->baseResList, cx, cy)) {
  314. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  315. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  316. }
  317. if (ConvertTextRes(window->outputResList, cx, cy)) {
  318. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  319. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  320. }
  321. SaveFPSData();
  322. }
  323. BasicSettingsData *CreateBasicVideoSettings(OBSBasicSettings *window)
  324. {
  325. return new BasicVideoData(window);
  326. }