obs-app.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <sstream>
  15. #include <util/bmem.h>
  16. #include <util/dstr.h>
  17. #include <util/platform.h>
  18. #include <obs.hpp>
  19. #include "obs-app.hpp"
  20. #include "window-main-basic.hpp"
  21. #include "wx-wrappers.hpp"
  22. #include "platform.hpp"
  23. using namespace std;
  24. IMPLEMENT_APP(OBSApp);
  25. OBSAppBase::~OBSAppBase()
  26. {
  27. blog(LOG_INFO, "Number of memory leaks: %llu", bnum_allocs());
  28. }
  29. static void do_log(enum log_type type, const char *msg, va_list args)
  30. {
  31. #ifdef _WIN32
  32. char bla[4096];
  33. vsnprintf(bla, 4095, msg, args);
  34. OutputDebugStringA(bla);
  35. OutputDebugStringA("\n");
  36. if (type >= LOG_WARNING)
  37. __debugbreak();
  38. #else
  39. vprintf(msg, args);
  40. #endif
  41. }
  42. bool OBSApp::InitGlobalConfigDefaults()
  43. {
  44. config_set_default_string(globalConfig, "General", "Language", "en");
  45. config_set_default_int(globalConfig, "Window", "PosX", -1);
  46. config_set_default_int(globalConfig, "Window", "PosY", -1);
  47. config_set_default_int(globalConfig, "Window", "SizeX", -1);
  48. config_set_default_int(globalConfig, "Window", "SizeY", -1);
  49. vector<MonitorInfo> monitors;
  50. GetMonitors(monitors);
  51. if (!monitors.size()) {
  52. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  53. "technically shouldn't be possible.");
  54. return false;
  55. }
  56. uint32_t cx = monitors[0].cx;
  57. uint32_t cy = monitors[0].cy;
  58. #if _WIN32
  59. config_set_default_string(globalConfig, "Video", "Renderer",
  60. "Direct3D 11");
  61. #else
  62. config_set_default_string(globalConfig, "Video", "Renderer",
  63. "OpenGL");
  64. #endif
  65. config_set_default_uint(globalConfig, "Video", "BaseCX", cx);
  66. config_set_default_uint(globalConfig, "Video", "BaseCY", cy);
  67. cx = cx * 10 / 15;
  68. cy = cy * 10 / 15;
  69. config_set_default_uint(globalConfig, "Video", "OutputCX", cx);
  70. config_set_default_uint(globalConfig, "Video", "OutputCY", cy);
  71. config_set_default_string(globalConfig, "Video", "FPSType", "Common");
  72. config_set_default_string(globalConfig, "Video", "FPSCommon", "30");
  73. config_set_default_uint(globalConfig, "Video", "FPSInt", 30);
  74. config_set_default_uint(globalConfig, "Video", "FPSNum", 30);
  75. config_set_default_uint(globalConfig, "Video", "FPSDen", 1);
  76. config_set_default_uint(globalConfig, "Video", "FPSNS", 33333333);
  77. return true;
  78. }
  79. static bool do_mkdir(const char *path)
  80. {
  81. if (os_mkdir(path) == MKDIR_ERROR) {
  82. OBSErrorBox(NULL, "Failed to create directory %s", path);
  83. return false;
  84. }
  85. return true;
  86. }
  87. static bool MakeUserDirs()
  88. {
  89. BPtr<char> configPath(os_get_config_path("obs-studio"));
  90. return do_mkdir(configPath);
  91. }
  92. bool OBSApp::InitGlobalConfig()
  93. {
  94. BPtr<char> path(os_get_config_path("obs-studio/global.ini"));
  95. int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
  96. if (errorcode != CONFIG_SUCCESS) {
  97. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  98. return false;
  99. }
  100. return InitGlobalConfigDefaults();
  101. }
  102. #define DEFAULT_LANG "en"
  103. bool OBSApp::InitLocale()
  104. {
  105. const char *lang = config_get_string(globalConfig, "General",
  106. "Language");
  107. stringstream file;
  108. file << "locale/" << lang << ".txt";
  109. string englishPath;
  110. if (!GetDataFilePath("locale/" DEFAULT_LANG ".txt", englishPath)) {
  111. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".txt");
  112. return false;
  113. }
  114. textLookup = text_lookup_create(englishPath.c_str());
  115. if (!textLookup) {
  116. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  117. englishPath.c_str());
  118. return false;
  119. }
  120. if (astrcmpi(lang, DEFAULT_LANG) == 0)
  121. return true;
  122. string path;
  123. if (GetDataFilePath(file.str().c_str(), path)) {
  124. if (!text_lookup_add(textLookup, path.c_str()))
  125. blog(LOG_WARNING, "Failed to add locale file '%s'",
  126. path.c_str());
  127. } else {
  128. blog(LOG_WARNING, "Could not find locale file '%s'",
  129. file.str().c_str());
  130. }
  131. return true;
  132. }
  133. bool OBSApp::InitOBSBasic()
  134. {
  135. OBSBasic *obsBasic = new OBSBasic();
  136. obsBasic->Show();
  137. mainWindow = obsBasic;
  138. return obsBasic->Init();
  139. }
  140. bool OBSApp::OnInit()
  141. {
  142. base_set_log_handler(do_log);
  143. if (!wxApp::OnInit())
  144. return false;
  145. wxInitAllImageHandlers();
  146. if (!MakeUserDirs())
  147. return false;
  148. if (!InitGlobalConfig())
  149. return false;
  150. if (!InitLocale())
  151. return false;
  152. if (!InitOBSBasic())
  153. return false;
  154. return true;
  155. }
  156. int OBSApp::OnExit()
  157. {
  158. return wxApp::OnExit();
  159. }
  160. void OBSApp::GetFPSCommon(uint32_t &num, uint32_t &den) const
  161. {
  162. const char *val = config_get_string(globalConfig, "Video", "FPSCommon");
  163. if (strcmp(val, "10") == 0) {
  164. num = 30;
  165. den = 1;
  166. } else if (strcmp(val, "20") == 0) {
  167. num = 20;
  168. den = 1;
  169. } else if (strcmp(val, "25") == 0) {
  170. num = 25;
  171. den = 1;
  172. } else if (strcmp(val, "29.97") == 0) {
  173. num = 30000;
  174. den = 1001;
  175. } else if (strcmp(val, "48") == 0) {
  176. num = 48;
  177. den = 1;
  178. } else if (strcmp(val, "59.94") == 0) {
  179. num = 60000;
  180. den = 1001;
  181. } else if (strcmp(val, "60") == 0) {
  182. num = 60;
  183. den = 1;
  184. } else {
  185. num = 30;
  186. den = 1;
  187. }
  188. }
  189. void OBSApp::GetFPSInteger(uint32_t &num, uint32_t &den) const
  190. {
  191. num = (uint32_t)config_get_uint(globalConfig, "Video", "FPSInt");
  192. den = 1;
  193. }
  194. void OBSApp::GetFPSFraction(uint32_t &num, uint32_t &den) const
  195. {
  196. num = (uint32_t)config_get_uint(globalConfig, "Video", "FPSNum");
  197. den = (uint32_t)config_get_uint(globalConfig, "Video", "FPSDen");
  198. }
  199. void OBSApp::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  200. {
  201. num = 1000000000;
  202. den = (uint32_t)config_get_uint(globalConfig, "Video", "FPSNS");
  203. }
  204. void OBSApp::GetConfigFPS(uint32_t &num, uint32_t &den) const
  205. {
  206. const char *type = config_get_string(globalConfig, "Video", "FPSType");
  207. if (astrcmpi(type, "Integer") == 0)
  208. GetFPSInteger(num, den);
  209. else if (astrcmpi(type, "Fraction") == 0)
  210. GetFPSFraction(num, den);
  211. else if (astrcmpi(type, "Nanoseconds") == 0)
  212. GetFPSNanoseconds(num, den);
  213. else
  214. GetFPSCommon(num, den);
  215. }
  216. const char *OBSApp::GetRenderModule() const
  217. {
  218. const char *renderer = config_get_string(globalConfig, "Video",
  219. "Renderer");
  220. if (astrcmpi(renderer, "Direct3D 11") == 0)
  221. return "libobs-d3d11";
  222. else
  223. return "libobs-opengl";
  224. }