rtmp-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include <util/platform.h>
  2. #include <obs-module.h>
  3. #include <jansson.h>
  4. #include "rtmp-format-ver.h"
  5. struct rtmp_common {
  6. char *service;
  7. char *server;
  8. char *key;
  9. };
  10. static const char *rtmp_common_getname(void *unused)
  11. {
  12. UNUSED_PARAMETER(unused);
  13. return obs_module_text("StreamingServices");
  14. }
  15. static void rtmp_common_update(void *data, obs_data_t *settings)
  16. {
  17. struct rtmp_common *service = data;
  18. bfree(service->service);
  19. bfree(service->server);
  20. bfree(service->key);
  21. service->service = bstrdup(obs_data_get_string(settings, "service"));
  22. service->server = bstrdup(obs_data_get_string(settings, "server"));
  23. service->key = bstrdup(obs_data_get_string(settings, "key"));
  24. }
  25. static void rtmp_common_destroy(void *data)
  26. {
  27. struct rtmp_common *service = data;
  28. bfree(service->service);
  29. bfree(service->server);
  30. bfree(service->key);
  31. bfree(service);
  32. }
  33. static void *rtmp_common_create(obs_data_t *settings, obs_service_t *service)
  34. {
  35. struct rtmp_common *data = bzalloc(sizeof(struct rtmp_common));
  36. rtmp_common_update(data, settings);
  37. UNUSED_PARAMETER(service);
  38. return data;
  39. }
  40. static inline const char *get_string_val(json_t *service, const char *key)
  41. {
  42. json_t *str_val = json_object_get(service, key);
  43. if (!str_val || !json_is_string(str_val))
  44. return NULL;
  45. return json_string_value(str_val);
  46. }
  47. static inline int get_int_val(json_t *service, const char *key)
  48. {
  49. json_t *integer_val = json_object_get(service, key);
  50. if (!integer_val || !json_is_integer(integer_val))
  51. return 0;
  52. return (int)json_integer_value(integer_val);
  53. }
  54. static inline bool get_bool_val(json_t *service, const char *key)
  55. {
  56. json_t *bool_val = json_object_get(service, key);
  57. if (!bool_val || !json_is_boolean(bool_val))
  58. return false;
  59. return json_is_true(bool_val);
  60. }
  61. static void add_service(obs_property_t *list, json_t *service, bool show_all,
  62. const char *cur_service)
  63. {
  64. json_t *servers;
  65. const char *name;
  66. bool common;
  67. if (!json_is_object(service)) {
  68. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  69. "is not an object");
  70. return;
  71. }
  72. name = get_string_val(service, "name");
  73. if (!name) {
  74. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  75. "has no name");
  76. return;
  77. }
  78. common = get_bool_val(service, "common");
  79. if (!show_all && !common && strcmp(cur_service, name) != 0) {
  80. return;
  81. }
  82. servers = json_object_get(service, "servers");
  83. if (!servers || !json_is_array(servers)) {
  84. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  85. "'%s' has no servers", name);
  86. return;
  87. }
  88. obs_property_list_add_string(list, name, name);
  89. }
  90. static void add_services(obs_property_t *list, json_t *root, bool show_all,
  91. const char *cur_service)
  92. {
  93. json_t *service;
  94. size_t index;
  95. if (!json_is_array(root)) {
  96. blog(LOG_WARNING, "rtmp-common.c: [add_services] JSON file "
  97. "root is not an array");
  98. return;
  99. }
  100. json_array_foreach (root, index, service) {
  101. add_service(list, service, show_all, cur_service);
  102. }
  103. }
  104. static json_t *open_json_file(const char *file)
  105. {
  106. char *file_data = os_quick_read_utf8_file(file);
  107. json_error_t error;
  108. json_t *root;
  109. json_t *list;
  110. int format_ver;
  111. if (!file_data)
  112. return NULL;
  113. root = json_loads(file_data, JSON_REJECT_DUPLICATES, &error);
  114. bfree(file_data);
  115. if (!root) {
  116. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  117. "Error reading JSON file (%d): %s",
  118. error.line, error.text);
  119. return NULL;
  120. }
  121. format_ver = get_int_val(root, "format_version");
  122. if (format_ver != RTMP_SERVICES_FORMAT_VERSION) {
  123. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  124. "Wrong format version (%d), expected %d",
  125. format_ver, RTMP_SERVICES_FORMAT_VERSION);
  126. json_decref(root);
  127. return NULL;
  128. }
  129. list = json_object_get(root, "services");
  130. if (list)
  131. json_incref(list);
  132. json_decref(root);
  133. if (!list) {
  134. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  135. "No services list");
  136. return NULL;
  137. }
  138. return list;
  139. }
  140. static json_t *open_services_file(void)
  141. {
  142. char *file;
  143. json_t *root = NULL;
  144. file = obs_module_config_path("services.json");
  145. if (file) {
  146. root = open_json_file(file);
  147. bfree(file);
  148. }
  149. if (!root) {
  150. file = obs_module_file("services.json");
  151. if (file) {
  152. root = open_json_file(file);
  153. bfree(file);
  154. }
  155. }
  156. return root;
  157. }
  158. static void build_service_list(obs_property_t *list, json_t *root,
  159. bool show_all, const char *cur_service)
  160. {
  161. obs_property_list_clear(list);
  162. add_services(list, root, show_all, cur_service);
  163. }
  164. static void properties_data_destroy(void *data)
  165. {
  166. json_t *root = data;
  167. if (root)
  168. json_decref(root);
  169. }
  170. static void fill_servers(obs_property_t *servers_prop, json_t *service,
  171. const char *name)
  172. {
  173. json_t *servers, *server;
  174. size_t index;
  175. obs_property_list_clear(servers_prop);
  176. servers = json_object_get(service, "servers");
  177. if (!json_is_array(servers)) {
  178. blog(LOG_WARNING, "rtmp-common.c: [fill_servers] "
  179. "Servers for service '%s' not a valid object",
  180. name);
  181. return;
  182. }
  183. json_array_foreach (servers, index, server) {
  184. const char *server_name = get_string_val(server, "name");
  185. const char *url = get_string_val(server, "url");
  186. if (!server_name || !url)
  187. continue;
  188. obs_property_list_add_string(servers_prop, server_name, url);
  189. }
  190. }
  191. static inline json_t *find_service(json_t *root, const char *name)
  192. {
  193. size_t index;
  194. json_t *service;
  195. json_array_foreach (root, index, service) {
  196. const char *cur_name = get_string_val(service, "name");
  197. if (strcmp(name, cur_name) == 0)
  198. return service;
  199. }
  200. return NULL;
  201. }
  202. static bool service_selected(obs_properties_t *props, obs_property_t *p,
  203. obs_data_t *settings)
  204. {
  205. const char *name = obs_data_get_string(settings, "service");
  206. json_t *root = obs_properties_get_param(props);
  207. json_t *service;
  208. if (!name || !*name)
  209. return false;
  210. service = find_service(root, name);
  211. if (!service)
  212. return false;
  213. fill_servers(obs_properties_get(props, "server"), service, name);
  214. UNUSED_PARAMETER(p);
  215. return true;
  216. }
  217. static bool show_all_services_toggled(obs_properties_t *ppts,
  218. obs_property_t *p, obs_data_t *settings)
  219. {
  220. const char *cur_service = obs_data_get_string(settings, "service");
  221. bool show_all = obs_data_get_bool(settings, "show_all");
  222. json_t *root = obs_properties_get_param(ppts);
  223. if (!root)
  224. return false;
  225. build_service_list(obs_properties_get(ppts, "service"), root, show_all,
  226. cur_service);
  227. UNUSED_PARAMETER(p);
  228. return true;
  229. }
  230. static obs_properties_t *rtmp_common_properties(void *unused)
  231. {
  232. UNUSED_PARAMETER(unused);
  233. obs_properties_t *ppts = obs_properties_create();
  234. obs_property_t *p;
  235. json_t *root;
  236. root = open_services_file();
  237. if (root)
  238. obs_properties_set_param(ppts, root, properties_data_destroy);
  239. p = obs_properties_add_list(ppts, "service",
  240. obs_module_text("Service"),
  241. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  242. obs_property_set_modified_callback(p, service_selected);
  243. p = obs_properties_add_bool(ppts, "show_all",
  244. obs_module_text("ShowAll"));
  245. obs_property_set_modified_callback(p, show_all_services_toggled);
  246. obs_properties_add_list(ppts, "server", obs_module_text("Server"),
  247. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  248. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  249. OBS_TEXT_PASSWORD);
  250. return ppts;
  251. }
  252. static void apply_video_encoder_settings(obs_data_t *settings,
  253. json_t *recommended)
  254. {
  255. json_t *item = json_object_get(recommended, "keyint");
  256. if (item && json_is_integer(item)) {
  257. int keyint = (int)json_integer_value(item);
  258. obs_data_set_int(settings, "keyint_sec", keyint);
  259. }
  260. obs_data_set_bool(settings, "cbr", true);
  261. item = json_object_get(recommended, "profile");
  262. if (item && json_is_string(item)) {
  263. const char *profile = json_string_value(item);
  264. obs_data_set_string(settings, "profile", profile);
  265. }
  266. item = json_object_get(recommended, "max video bitrate");
  267. if (item && json_is_integer(item)) {
  268. int max_bitrate = (int)json_integer_value(item);
  269. if (obs_data_get_int(settings, "bitrate") > max_bitrate) {
  270. obs_data_set_int(settings, "bitrate", max_bitrate);
  271. obs_data_set_int(settings, "buffer_size", max_bitrate);
  272. }
  273. }
  274. }
  275. static void apply_audio_encoder_settings(obs_data_t *settings,
  276. json_t *recommended)
  277. {
  278. json_t *item = json_object_get(recommended, "max audio bitrate");
  279. if (item && json_is_integer(item)) {
  280. int max_bitrate = (int)json_integer_value(item);
  281. if (obs_data_get_int(settings, "bitrate") > max_bitrate)
  282. obs_data_set_int(settings, "bitrate", max_bitrate);
  283. }
  284. }
  285. static void initialize_output(struct rtmp_common *service, json_t *root,
  286. obs_data_t *video_settings, obs_data_t *audio_settings)
  287. {
  288. json_t *json_service = find_service(root, service->service);
  289. json_t *recommended;
  290. if (!json_service) {
  291. blog(LOG_WARNING, "rtmp-common.c: [initialize_output] "
  292. "Could not find service '%s'",
  293. service->service);
  294. return;
  295. }
  296. recommended = json_object_get(json_service, "recommended");
  297. if (!recommended)
  298. return;
  299. if (video_settings)
  300. apply_video_encoder_settings(video_settings, recommended);
  301. if (audio_settings)
  302. apply_audio_encoder_settings(audio_settings, recommended);
  303. }
  304. static void rtmp_common_apply_settings(void *data,
  305. obs_data_t *video_settings, obs_data_t *audio_settings)
  306. {
  307. struct rtmp_common *service = data;
  308. json_t *root = open_services_file();
  309. if (root) {
  310. initialize_output(service, root, video_settings,
  311. audio_settings);
  312. json_decref(root);
  313. }
  314. }
  315. static const char *rtmp_common_url(void *data)
  316. {
  317. struct rtmp_common *service = data;
  318. return service->server;
  319. }
  320. static const char *rtmp_common_key(void *data)
  321. {
  322. struct rtmp_common *service = data;
  323. return service->key;
  324. }
  325. struct obs_service_info rtmp_common_service = {
  326. .id = "rtmp_common",
  327. .get_name = rtmp_common_getname,
  328. .create = rtmp_common_create,
  329. .destroy = rtmp_common_destroy,
  330. .update = rtmp_common_update,
  331. .get_properties = rtmp_common_properties,
  332. .get_url = rtmp_common_url,
  333. .get_key = rtmp_common_key,
  334. .apply_encoder_settings = rtmp_common_apply_settings,
  335. };