xsplit.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. #include <ctype.h>
  16. #include <QDomDocument>
  17. using namespace std;
  18. using namespace json11;
  19. static int hex_string_to_int(string str)
  20. {
  21. int res = 0;
  22. if (str[0] == '#')
  23. str = str.substr(1);
  24. for (size_t i = 0, l = str.size(); i < l; i++) {
  25. res *= 16;
  26. if (str[0] >= '0' && str[0] <= '9')
  27. res += str[0] - '0';
  28. else
  29. res += str[0] - 'A' + 10;
  30. str = str.substr(1);
  31. }
  32. return res;
  33. }
  34. static Json::object parse_text(QString &config)
  35. {
  36. int start = config.indexOf("*{");
  37. config = config.mid(start + 1);
  38. config.replace("\\", "/");
  39. string err;
  40. Json data = Json::parse(config.toStdString(), err);
  41. if (err != "")
  42. return Json::object{};
  43. string outline = data["outline"].string_value();
  44. int out = 0;
  45. if (outline == "thick")
  46. out = 20;
  47. else if (outline == "thicker")
  48. out = 40;
  49. else if (outline == "thinner")
  50. out = 5;
  51. else if (outline == "thin")
  52. out = 10;
  53. string valign = data["vertAlign"].string_value();
  54. if (valign == "middle")
  55. valign = "center";
  56. Json font = Json::object{{"face", data["fontStyle"]}, {"size", 200}};
  57. return Json::object{{"text", data["text"]},
  58. {"font", font},
  59. {"outline", out > 0},
  60. {"outline_size", out},
  61. {"outline_color", hex_string_to_int(data["outlineColor"].string_value())},
  62. {"color", hex_string_to_int(data["color"].string_value())},
  63. {"align", data["textAlign"]},
  64. {"valign", valign},
  65. {"alpha", data["opacity"]}};
  66. }
  67. static Json::array parse_playlist(QString &playlist)
  68. {
  69. Json::array out = Json::array{};
  70. while (true) {
  71. int end = playlist.indexOf('*');
  72. QString path = playlist.left(end);
  73. out.push_back(Json::object{{"value", path.toStdString()}});
  74. int next = playlist.indexOf('|');
  75. if (next == -1)
  76. break;
  77. playlist = playlist.mid(next + 1);
  78. }
  79. return out;
  80. }
  81. static void parse_media_types(QDomNamedNodeMap &attr, Json::object &source, Json::object &settings)
  82. {
  83. QString playlist = attr.namedItem("FilePlaylist").nodeValue();
  84. if (playlist != "") {
  85. source["id"] = "vlc_source";
  86. settings["playlist"] = parse_playlist(playlist);
  87. QString end_op = attr.namedItem("OpWhenFinished").nodeValue();
  88. if (end_op == "2")
  89. settings["loop"] = true;
  90. } else {
  91. QString url = attr.namedItem("item").nodeValue();
  92. int sep = url.indexOf("://");
  93. if (sep != -1) {
  94. QString prot = url.left(sep);
  95. if (prot == "smlndi") {
  96. source["id"] = "ndi_source";
  97. } else {
  98. source["id"] = "ffmpeg_source";
  99. int info = url.indexOf("\\");
  100. QString input;
  101. if (info != -1) {
  102. input = url.left(info);
  103. } else {
  104. input = url;
  105. }
  106. settings["input"] = input.toStdString();
  107. settings["is_local_file"] = false;
  108. }
  109. } else {
  110. source["id"] = "ffmpeg_source";
  111. settings["local_file"] = url.replace("\\", "/").toStdString();
  112. settings["is_local_file"] = true;
  113. }
  114. }
  115. }
  116. static Json::object parse_slideshow(QString &config)
  117. {
  118. int start = config.indexOf("images\":[");
  119. if (start == -1)
  120. return Json::object{};
  121. config = config.mid(start + 8);
  122. config.replace("\\\\", "/");
  123. int end = config.indexOf(']');
  124. if (end == -1)
  125. return Json::object{};
  126. string arr = config.left(end + 1).toStdString();
  127. string err;
  128. Json::array files = Json::parse(arr, err).array_items();
  129. if (err != "")
  130. return Json::object{};
  131. Json::array files_out = Json::array{};
  132. for (size_t i = 0; i < files.size(); i++) {
  133. string file = files[i].string_value();
  134. files_out.push_back(Json::object{{"value", file}});
  135. }
  136. QString options = config.mid(end + 1);
  137. options[0] = '{';
  138. Json opt = Json::parse(options.toStdString(), err);
  139. if (err != "")
  140. return Json::object{};
  141. return Json::object{{"randomize", opt["random"]},
  142. {"slide_time", opt["delay"].number_value() * 1000 + 700},
  143. {"files", files_out}};
  144. }
  145. static bool source_name_exists(const string &name, const Json::array &sources)
  146. {
  147. for (size_t i = 0; i < sources.size(); i++) {
  148. if (sources.at(i)["name"].string_value() == name)
  149. return true;
  150. }
  151. return false;
  152. }
  153. static Json get_source_with_id(const string &src_id, const Json::array &sources)
  154. {
  155. for (size_t i = 0; i < sources.size(); i++) {
  156. if (sources.at(i)["src_id"].string_value() == src_id)
  157. return sources.at(i);
  158. }
  159. return nullptr;
  160. }
  161. static void parse_items(QDomNode &item, Json::array &items, Json::array &sources)
  162. {
  163. while (!item.isNull()) {
  164. QDomNamedNodeMap attr = item.attributes();
  165. QString srcid = attr.namedItem("srcid").nodeValue();
  166. double vol = attr.namedItem("volume").nodeValue().toDouble();
  167. int type = attr.namedItem("type").nodeValue().toInt();
  168. string name;
  169. Json::object settings;
  170. Json::object source;
  171. string temp_name;
  172. int x = 0;
  173. Json exists = get_source_with_id(srcid.toStdString(), sources);
  174. if (!exists.is_null()) {
  175. name = exists["name"].string_value();
  176. goto skip;
  177. }
  178. name = attr.namedItem("cname").nodeValue().toStdString();
  179. if (name.empty() || name[0] == '\0')
  180. name = attr.namedItem("name").nodeValue().toStdString();
  181. temp_name = name;
  182. while (source_name_exists(temp_name, sources)) {
  183. string new_name = name + " " + to_string(x++);
  184. temp_name = new_name;
  185. }
  186. name = temp_name;
  187. settings = Json::object{};
  188. source = Json::object{{"name", name}, {"src_id", srcid.toStdString()}, {"volume", vol}};
  189. /** type=1 means Media of some kind (Video Playlist, RTSP,
  190. RTMP, NDI or Media File).
  191. type=2 means either a DShow or WASAPI source.
  192. type=4 means an Image source.
  193. type=5 means either a Display or Window Capture.
  194. type=7 means a Game Capture.
  195. type=8 means rendered with a browser, which includes:
  196. Web Page, Image Slideshow, Text.
  197. type=11 means another Scene. **/
  198. if (type == 1) {
  199. parse_media_types(attr, source, settings);
  200. } else if (type == 2) {
  201. QString audio = attr.namedItem("itemaudio").nodeValue();
  202. if (audio.isEmpty()) {
  203. source["id"] = "dshow_input";
  204. } else {
  205. source["id"] = "wasapi_input_capture";
  206. int dev = audio.indexOf("\\wave:") + 6;
  207. QString res = "{0.0.1.00000000}." + audio.mid(dev);
  208. res = res.toLower();
  209. settings["device_id"] = res.toStdString();
  210. }
  211. } else if (type == 4) {
  212. source["id"] = "image_source";
  213. QString path = attr.namedItem("item").nodeValue();
  214. path.replace("\\", "/");
  215. settings["file"] = path.toStdString();
  216. } else if (type == 5) {
  217. QString opt = attr.namedItem("item").nodeValue();
  218. QDomDocument options;
  219. options.setContent(opt);
  220. QDomNode el = options.documentElement();
  221. QDomNamedNodeMap o_attr = el.attributes();
  222. QString display = o_attr.namedItem("desktop").nodeValue();
  223. if (!display.isEmpty()) {
  224. source["id"] = "monitor_capture";
  225. int cursor = attr.namedItem("ScrCapShowMouse").nodeValue().toInt();
  226. settings["capture_cursor"] = cursor == 1;
  227. } else {
  228. source["id"] = "window_capture";
  229. QString exec = o_attr.namedItem("module").nodeValue();
  230. QString window = o_attr.namedItem("window").nodeValue();
  231. QString _class = o_attr.namedItem("class").nodeValue();
  232. int pos = exec.lastIndexOf('\\');
  233. if (_class.isEmpty()) {
  234. _class = "class";
  235. }
  236. QString res = window + ":" + _class + ":" + exec.mid(pos + 1);
  237. settings["window"] = res.toStdString();
  238. settings["priority"] = 2;
  239. }
  240. } else if (type == 7) {
  241. QString opt = attr.namedItem("item").nodeValue();
  242. opt.replace("&lt;", "<");
  243. opt.replace("&gt;", ">");
  244. opt.replace("&quot;", "\"");
  245. QDomDocument doc;
  246. doc.setContent(opt);
  247. QDomNode el = doc.documentElement();
  248. QDomNamedNodeMap o_attr = el.attributes();
  249. QString name = o_attr.namedItem("wndname").nodeValue();
  250. QString exec = o_attr.namedItem("imagename").nodeValue();
  251. QString res = name = "::" + exec;
  252. source["id"] = "game_capture";
  253. settings["window"] = res.toStdString();
  254. settings["capture_mode"] = "window";
  255. } else if (type == 8) {
  256. QString plugin = attr.namedItem("item").nodeValue();
  257. if (plugin.startsWith("html:plugin:imageslideshowplg*")) {
  258. source["id"] = "slideshow";
  259. settings = parse_slideshow(plugin);
  260. } else if (plugin.startsWith("html:plugin:titleplg")) {
  261. source["id"] = "text_gdiplus";
  262. settings = parse_text(plugin);
  263. } else if (plugin.startsWith("http")) {
  264. source["id"] = "browser_source";
  265. int end = plugin.indexOf('*');
  266. settings["url"] = plugin.left(end).toStdString();
  267. }
  268. } else if (type == 11) {
  269. QString id = attr.namedItem("item").nodeValue();
  270. Json source = get_source_with_id(id.toStdString(), sources);
  271. name = source["name"].string_value();
  272. goto skip;
  273. }
  274. source["settings"] = settings;
  275. sources.push_back(source);
  276. skip:
  277. struct obs_video_info ovi;
  278. obs_get_video_info(&ovi);
  279. int width = ovi.base_width;
  280. int height = ovi.base_height;
  281. double pos_left = attr.namedItem("pos_left").nodeValue().toDouble();
  282. double pos_right = attr.namedItem("pos_right").nodeValue().toDouble();
  283. double pos_top = attr.namedItem("pos_top").nodeValue().toDouble();
  284. double pos_bottom = attr.namedItem("pos_bottom").nodeValue().toDouble();
  285. bool visible = attr.namedItem("visible").nodeValue() == "1";
  286. Json out_item = Json::object{{"bounds_type", 2},
  287. {"pos", Json::object{{"x", pos_left * width}, {"y", pos_top * height}}},
  288. {"bounds", Json::object{{"x", (pos_right - pos_left) * width},
  289. {"y", (pos_bottom - pos_top) * height}}},
  290. {"name", name},
  291. {"visible", visible}};
  292. items.push_back(out_item);
  293. item = item.nextSibling();
  294. }
  295. }
  296. static Json::object parse_scenes(QDomElement &scenes)
  297. {
  298. Json::array sources = Json::array{};
  299. QString first = "";
  300. QDomNode in_scene = scenes.firstChild();
  301. while (!in_scene.isNull()) {
  302. QString type = in_scene.nodeName();
  303. if (type == "placement") {
  304. QDomNamedNodeMap attr = in_scene.attributes();
  305. QString name = attr.namedItem("name").nodeValue();
  306. QString id = attr.namedItem("id").nodeValue();
  307. if (first.isEmpty())
  308. first = name;
  309. Json out = Json::object{{"id", "scene"},
  310. {"name", name.toStdString().c_str()},
  311. {"src_id", id.toStdString().c_str()}};
  312. sources.push_back(out);
  313. }
  314. in_scene = in_scene.nextSibling();
  315. }
  316. in_scene = scenes.firstChild();
  317. for (size_t i = 0, l = sources.size(); i < l; i++) {
  318. Json::object source = sources[i].object_items();
  319. Json::array items = Json::array{};
  320. QDomNode firstChild = in_scene.firstChild();
  321. parse_items(firstChild, items, sources);
  322. Json settings = Json::object{{"items", items}, {"id_counter", (int)items.size()}};
  323. source["settings"] = settings;
  324. sources[i] = source;
  325. in_scene = in_scene.nextSibling();
  326. }
  327. return Json::object{{"sources", sources},
  328. {"current_scene", first.toStdString()},
  329. {"current_program_scene", first.toStdString()}};
  330. }
  331. int XSplitImporter::ImportScenes(const string &path, string &name, json11::Json &res)
  332. {
  333. if (name == "")
  334. name = "XSplit Import";
  335. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  336. if (!file_data)
  337. return IMPORTER_FILE_WONT_OPEN;
  338. QDomDocument doc;
  339. doc.setContent(QString(file_data));
  340. QDomElement docElem = doc.documentElement();
  341. Json::object r = parse_scenes(docElem);
  342. r["name"] = name;
  343. res = r;
  344. QDir dir(path.c_str());
  345. TranslateOSStudio(res);
  346. TranslatePaths(res, QDir::cleanPath(dir.filePath("..")).toStdString());
  347. return IMPORTER_SUCCESS;
  348. }
  349. bool XSplitImporter::Check(const string &path)
  350. {
  351. bool check = false;
  352. BPtr<char> file_data = os_quick_read_utf8_file(path.c_str());
  353. if (!file_data)
  354. return false;
  355. string pos = file_data.Get();
  356. string line = ReadLine(pos);
  357. while (!line.empty()) {
  358. if (line.substr(0, 5) == "<?xml") {
  359. line = ReadLine(pos);
  360. } else {
  361. if (line.substr(0, 14) == "<configuration") {
  362. check = true;
  363. }
  364. break;
  365. }
  366. }
  367. return check;
  368. }
  369. OBSImporterFiles XSplitImporter::FindFiles()
  370. {
  371. OBSImporterFiles res;
  372. #ifdef _WIN32
  373. char dst[512];
  374. int found = os_get_program_data_path(dst, 512, "SplitMediaLabs\\XSplit\\Presentation2.0\\");
  375. if (found == -1)
  376. return res;
  377. os_dir_t *dir = os_opendir(dst);
  378. struct os_dirent *ent;
  379. while ((ent = os_readdir(dir)) != NULL) {
  380. string name = ent->d_name;
  381. if (ent->directory || name[0] == '.')
  382. continue;
  383. if (name == "Placements.bpres") {
  384. string str = dst + name;
  385. res.push_back(str);
  386. break;
  387. }
  388. }
  389. os_closedir(dir);
  390. #endif
  391. return res;
  392. }