studio.cpp 7.3 KB

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