studio.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /******************************************************************************
  2. Copyright (C) 2019-2020 by Dillon Pentz <[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 "importers.hpp"
  15. using namespace std;
  16. using namespace json11;
  17. void TranslateOSStudio(Json &res)
  18. {
  19. Json::object out = res.object_items();
  20. Json::array sources = out["sources"].array_items();
  21. for (size_t i = 0; i < sources.size(); i++) {
  22. Json::object source = sources[i].object_items();
  23. Json::object settings = source["settings"].object_items();
  24. string id = source["id"].string_value();
  25. #define DirectTranslation(before, after) \
  26. if (id == before) { \
  27. source["id"] = after; \
  28. source["versioned_id"] = obs_get_latest_input_type_id(after); \
  29. }
  30. #define ClearTranslation(before, after) \
  31. if (id == before) { \
  32. source["id"] = after; \
  33. source["settings"] = Json::object{}; \
  34. source["versioned_id"] = obs_get_latest_input_type_id(after); \
  35. }
  36. #ifdef __APPLE__
  37. DirectTranslation("text_gdiplus", "text_ft2_source");
  38. ClearTranslation("game_capture", "syphon-input");
  39. ClearTranslation("wasapi_input_capture",
  40. "coreaudio_input_capture");
  41. ClearTranslation("wasapi_output_capture",
  42. "coreaudio_output_capture");
  43. ClearTranslation("pulse_input_capture",
  44. "coreaudio_input_capture");
  45. ClearTranslation("pulse_output_capture",
  46. "coreaudio_output_capture");
  47. ClearTranslation("jack_output_capture",
  48. "coreaudio_output_capture");
  49. ClearTranslation("alsa_input_capture",
  50. "coreaudio_input_capture");
  51. ClearTranslation("dshow_input", "av_capture_input");
  52. ClearTranslation("v4l2_input", "av_capture_input");
  53. ClearTranslation("xcomposite_input", "window_capture");
  54. if (id == "monitor_capture") {
  55. if (settings["show_cursor"].is_null() &&
  56. !settings["capture_cursor"].is_null()) {
  57. bool cursor =
  58. settings["capture_cursor"].bool_value();
  59. settings["show_cursor"] = cursor;
  60. }
  61. }
  62. DirectTranslation("xshm_input", "monitor_capture");
  63. #elif defined(_WIN32)
  64. DirectTranslation("text_ft2_source", "text_gdiplus");
  65. ClearTranslation("syphon-input", "game_capture");
  66. ClearTranslation("coreaudio_input_capture",
  67. "wasapi_input_capture");
  68. ClearTranslation("coreaudio_output_capture",
  69. "wasapi_output_capture");
  70. ClearTranslation("pulse_input_capture", "wasapi_input_capture");
  71. ClearTranslation("pulse_output_capture",
  72. "wasapi_output_capture");
  73. ClearTranslation("jack_output_capture",
  74. "wasapi_output_capture");
  75. ClearTranslation("alsa_input_capture", "wasapi_input_capture");
  76. ClearTranslation("av_capture_input", "dshow_input");
  77. ClearTranslation("v4l2_input", "dshow_input");
  78. ClearTranslation("xcomposite_input", "window_capture");
  79. if (id == "monitor_capture" || id == "xshm_input") {
  80. if (!settings["show_cursor"].is_null()) {
  81. bool cursor =
  82. settings["show_cursor"].bool_value();
  83. settings["capture_cursor"] = cursor;
  84. }
  85. source["id"] = "monitor_capture";
  86. }
  87. #else
  88. DirectTranslation("text_gdiplus", "text_ft2_source");
  89. ClearTranslation("coreaudio_input_capture",
  90. "pulse_input_capture");
  91. ClearTranslation("coreaudio_output_capture",
  92. "pulse_output_capture");
  93. ClearTranslation("wasapi_input_capture", "pulse_input_capture");
  94. ClearTranslation("wasapi_output_capture",
  95. "pulse_output_capture");
  96. ClearTranslation("av_capture_input", "v4l2_input");
  97. ClearTranslation("dshow_input", "v4l2_input");
  98. ClearTranslation("window_capture", "xcomposite_input");
  99. if (id == "monitor_capture") {
  100. source["id"] = "xshm_input";
  101. if (settings["show_cursor"].is_null() &&
  102. !settings["capture_cursor"].is_null()) {
  103. bool cursor =
  104. settings["capture_cursor"].bool_value();
  105. settings["show_cursor"] = cursor;
  106. }
  107. }
  108. #endif
  109. source["settings"] = settings;
  110. sources[i] = source;
  111. #undef DirectTranslation
  112. #undef ClearTranslation
  113. }
  114. out["sources"] = sources;
  115. res = out;
  116. }
  117. static string CheckPath(const string &path, const string &rootDir)
  118. {
  119. char root[512];
  120. *root = 0;
  121. size_t rootLen = os_get_abs_path(rootDir.c_str(), root, sizeof(root));
  122. char absPath[512];
  123. *absPath = 0;
  124. size_t len = os_get_abs_path((rootDir + path).c_str(), absPath,
  125. sizeof(absPath));
  126. if (len == 0)
  127. return path;
  128. if (strstr(absPath, root) != absPath)
  129. return path;
  130. if (*(absPath + rootLen) != QDir::separator().toLatin1())
  131. return path;
  132. return absPath;
  133. }
  134. void TranslatePaths(Json &res, const string &rootDir)
  135. {
  136. if (res.is_object()) {
  137. Json::object out = res.object_items();
  138. for (auto it = out.begin(); it != out.end(); it++) {
  139. Json val = it->second;
  140. if (val.is_string()) {
  141. if (val.string_value().rfind("./", 0) != 0)
  142. continue;
  143. out[it->first] =
  144. CheckPath(val.string_value(), rootDir);
  145. } else if (val.is_array() || val.is_object()) {
  146. TranslatePaths(val, rootDir);
  147. out[it->first] = val;
  148. }
  149. }
  150. res = out;
  151. } else if (res.is_array()) {
  152. Json::array out = res.array_items();
  153. for (size_t i = 0; i != out.size(); i++) {
  154. Json val = out[i];
  155. if (val.is_string()) {
  156. if (val.string_value().rfind("./", 0) != 0)
  157. continue;
  158. out[i] = CheckPath(val.string_value(), rootDir);
  159. } else if (val.is_array() || val.is_object()) {
  160. TranslatePaths(val, rootDir);
  161. out[i] = val;
  162. }
  163. }
  164. res = out;
  165. }
  166. }
  167. bool StudioImporter::Check(const string &path)
  168. {
  169. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  170. string err;
  171. Json collection = Json::parse(file_data, err);
  172. if (err != "")
  173. return false;
  174. if (collection.is_null())
  175. return false;
  176. if (collection["sources"].is_null())
  177. return false;
  178. if (collection["name"].is_null())
  179. return false;
  180. if (collection["current_scene"].is_null())
  181. return false;
  182. return true;
  183. }
  184. string StudioImporter::Name(const string &path)
  185. {
  186. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  187. string err;
  188. Json d = Json::parse(file_data, err);
  189. string name = d["name"].string_value();
  190. return name;
  191. }
  192. int StudioImporter::ImportScenes(const string &path, string &name, Json &res)
  193. {
  194. if (!os_file_exists(path.c_str()))
  195. return IMPORTER_FILE_NOT_FOUND;
  196. if (!Check(path.c_str()))
  197. return IMPORTER_FILE_NOT_RECOGNISED;
  198. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  199. string err;
  200. Json d = Json::parse(file_data, err);
  201. if (err != "")
  202. return IMPORTER_ERROR_DURING_CONVERSION;
  203. QDir dir(path.c_str());
  204. TranslateOSStudio(d);
  205. TranslatePaths(d, QDir::cleanPath(dir.filePath("..")).toStdString());
  206. Json::object obj = d.object_items();
  207. if (name != "")
  208. obj["name"] = name;
  209. else
  210. obj["name"] = "OBS Studio Import";
  211. res = obj;
  212. return IMPORTER_SUCCESS;
  213. }