streaming-helpers.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "streaming-helpers.hpp"
  2. #include "../plugins/rtmp-services/rtmp-format-ver.h"
  3. #include <util/platform.h>
  4. #include <util/util.hpp>
  5. #include <obs.h>
  6. using namespace json11;
  7. static Json open_json_file(const char *path)
  8. {
  9. BPtr<char> file_data = os_quick_read_utf8_file(path);
  10. if (!file_data)
  11. return Json();
  12. std::string err;
  13. Json json = Json::parse(file_data, err);
  14. if (json["format_version"].int_value() != RTMP_SERVICES_FORMAT_VERSION)
  15. return Json();
  16. return json;
  17. }
  18. static inline bool name_matches(const Json &service, const char *name)
  19. {
  20. if (service["name"].string_value() == name)
  21. return true;
  22. auto &alt_names = service["alt_names"].array_items();
  23. for (const Json &alt_name : alt_names) {
  24. if (alt_name.string_value() == name) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. Json get_services_json()
  31. {
  32. obs_module_t *mod = obs_get_module("rtmp-services");
  33. Json root;
  34. BPtr<char> file = obs_module_get_config_path(mod, "services.json");
  35. if (file)
  36. root = open_json_file(file);
  37. if (root.is_null()) {
  38. file = obs_find_module_file(mod, "services.json");
  39. if (file)
  40. root = open_json_file(file);
  41. }
  42. return root;
  43. }
  44. Json get_service_from_json(Json &root, const char *name)
  45. {
  46. auto &services = root["services"].array_items();
  47. for (const Json &service : services) {
  48. if (name_matches(service, name)) {
  49. return service;
  50. }
  51. }
  52. return Json();
  53. }