studio.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. }
  29. #define ClearTranslation(before, after) \
  30. if (id == before) { \
  31. source["id"] = after; \
  32. source["settings"] = Json::object{}; \
  33. }
  34. #ifdef __APPLE__
  35. DirectTranslation("text_gdiplus", "text_ft2_source");
  36. ClearTranslation("game_capture", "syphon-input");
  37. ClearTranslation("wasapi_input_capture",
  38. "coreaudio_input_capture");
  39. ClearTranslation("wasapi_output_capture",
  40. "coreaudio_output_capture");
  41. ClearTranslation("pulse_input_capture",
  42. "coreaudio_input_capture");
  43. ClearTranslation("pulse_output_capture",
  44. "coreaudio_output_capture");
  45. ClearTranslation("jack_output_capture",
  46. "coreaudio_output_capture");
  47. ClearTranslation("alsa_input_capture",
  48. "coreaudio_input_capture");
  49. ClearTranslation("dshow_input", "av_capture_input");
  50. ClearTranslation("v4l2_input", "av_capture_input");
  51. ClearTranslation("xcomposite_input", "window_capture");
  52. if (id == "monitor_capture") {
  53. if (settings["show_cursor"].is_null() &&
  54. !settings["capture_cursor"].is_null()) {
  55. bool cursor =
  56. settings["capture_cursor"].bool_value();
  57. settings["show_cursor"] = cursor;
  58. }
  59. }
  60. DirectTranslation("xshm_input", "monitor_capture");
  61. #elif defined(_WIN32)
  62. DirectTranslation("text_ft2_source", "text_gdiplus");
  63. ClearTranslation("syphon-input", "game_capture");
  64. ClearTranslation("coreaudio_input_capture",
  65. "wasapi_input_capture");
  66. ClearTranslation("coreaudio_output_capture",
  67. "wasapi_output_capture");
  68. ClearTranslation("pulse_input_capture", "wasapi_input_capture");
  69. ClearTranslation("pulse_output_capture",
  70. "wasapi_output_capture");
  71. ClearTranslation("jack_output_capture",
  72. "wasapi_output_capture");
  73. ClearTranslation("alsa_input_capture", "wasapi_input_capture");
  74. ClearTranslation("av_capture_input", "dshow_input");
  75. ClearTranslation("v4l2_input", "dshow_input");
  76. ClearTranslation("xcomposite_input", "window_capture");
  77. if (id == "monitor_capture" || id == "xshm_input") {
  78. if (!settings["show_cursor"].is_null()) {
  79. bool cursor =
  80. settings["show_cursor"].bool_value();
  81. settings["capture_cursor"] = cursor;
  82. }
  83. source["id"] = "monitor_capture";
  84. }
  85. #else
  86. DirectTranslation("text_gdiplus", "text_ft2_source");
  87. ClearTranslation("coreaudio_input_capture",
  88. "pulse_input_capture");
  89. ClearTranslation("coreaudio_output_capture",
  90. "pulse_output_capture");
  91. ClearTranslation("wasapi_input_capture", "pulse_input_capture");
  92. ClearTranslation("wasapi_output_capture",
  93. "pulse_output_capture");
  94. ClearTranslation("av_capture_input", "v4l2_input");
  95. ClearTranslation("dshow_input", "v4l2_input");
  96. ClearTranslation("window_capture", "xcomposite_input");
  97. if (id == "monitor_capture") {
  98. source["id"] = "xshm_input";
  99. if (settings["show_cursor"].is_null() &&
  100. !settings["capture_cursor"].is_null()) {
  101. bool cursor =
  102. settings["capture_cursor"].bool_value();
  103. settings["show_cursor"] = cursor;
  104. }
  105. }
  106. #endif
  107. source["settings"] = settings;
  108. sources[i] = source;
  109. #undef DirectTranslation
  110. #undef ClearTranslation
  111. }
  112. res = out;
  113. }
  114. bool StudioImporter::Check(const string &path)
  115. {
  116. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  117. string err;
  118. Json collection = Json::parse(file_data, err);
  119. if (err != "")
  120. return false;
  121. if (collection.is_null())
  122. return false;
  123. if (collection["sources"].is_null())
  124. return false;
  125. if (collection["name"].is_null())
  126. return false;
  127. if (collection["current_scene"].is_null())
  128. return false;
  129. return true;
  130. }
  131. string StudioImporter::Name(const string &path)
  132. {
  133. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  134. string err;
  135. Json d = Json::parse(file_data, err);
  136. string name = d["name"].string_value();
  137. return name;
  138. }
  139. int StudioImporter::ImportScenes(const string &path, string &name, Json &res)
  140. {
  141. if (!os_file_exists(path.c_str()))
  142. return IMPORTER_FILE_NOT_FOUND;
  143. if (!Check(path.c_str()))
  144. return IMPORTER_FILE_NOT_RECOGNISED;
  145. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  146. string err;
  147. Json d = Json::parse(file_data, err);
  148. if (err != "")
  149. return IMPORTER_ERROR_DURING_CONVERSION;
  150. TranslateOSStudio(d);
  151. Json::object obj = d.object_items();
  152. if (name != "")
  153. obj["name"] = name;
  154. else
  155. obj["name"] = "OBS Studio Import";
  156. res = obj;
  157. return IMPORTER_SUCCESS;
  158. }