rtmp-common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #include <util/platform.h>
  2. #include <util/dstr.h>
  3. #include <obs-module.h>
  4. #include <jansson.h>
  5. #include "rtmp-format-ver.h"
  6. #include "twitch.h"
  7. struct rtmp_common {
  8. char *service;
  9. char *server;
  10. char *key;
  11. char *output;
  12. };
  13. static const char *rtmp_common_getname(void *unused)
  14. {
  15. UNUSED_PARAMETER(unused);
  16. return obs_module_text("StreamingServices");
  17. }
  18. static json_t *open_services_file(void);
  19. static inline json_t *find_service(json_t *root, const char *name);
  20. static inline const char *get_string_val(json_t *service, const char *key);
  21. extern void twitch_ingests_refresh(int seconds);
  22. static void rtmp_common_update(void *data, obs_data_t *settings)
  23. {
  24. struct rtmp_common *service = data;
  25. bfree(service->service);
  26. bfree(service->server);
  27. bfree(service->output);
  28. bfree(service->key);
  29. service->service = bstrdup(obs_data_get_string(settings, "service"));
  30. service->server = bstrdup(obs_data_get_string(settings, "server"));
  31. service->key = bstrdup(obs_data_get_string(settings, "key"));
  32. service->output = NULL;
  33. json_t *root = open_services_file();
  34. if (root) {
  35. json_t *serv = find_service(root, service->service);
  36. if (serv) {
  37. json_t *rec = json_object_get(serv, "recommended");
  38. if (rec && json_is_object(rec)) {
  39. const char *out = get_string_val(rec, "output");
  40. if (out)
  41. service->output = bstrdup(out);
  42. }
  43. }
  44. }
  45. json_decref(root);
  46. if (!service->output)
  47. service->output = bstrdup("rtmp_output");
  48. }
  49. static void rtmp_common_destroy(void *data)
  50. {
  51. struct rtmp_common *service = data;
  52. bfree(service->service);
  53. bfree(service->server);
  54. bfree(service->output);
  55. bfree(service->key);
  56. bfree(service);
  57. }
  58. static void *rtmp_common_create(obs_data_t *settings, obs_service_t *service)
  59. {
  60. struct rtmp_common *data = bzalloc(sizeof(struct rtmp_common));
  61. rtmp_common_update(data, settings);
  62. UNUSED_PARAMETER(service);
  63. return data;
  64. }
  65. static inline const char *get_string_val(json_t *service, const char *key)
  66. {
  67. json_t *str_val = json_object_get(service, key);
  68. if (!str_val || !json_is_string(str_val))
  69. return NULL;
  70. return json_string_value(str_val);
  71. }
  72. static inline int get_int_val(json_t *service, const char *key)
  73. {
  74. json_t *integer_val = json_object_get(service, key);
  75. if (!integer_val || !json_is_integer(integer_val))
  76. return 0;
  77. return (int)json_integer_value(integer_val);
  78. }
  79. static inline bool get_bool_val(json_t *service, const char *key)
  80. {
  81. json_t *bool_val = json_object_get(service, key);
  82. if (!bool_val || !json_is_boolean(bool_val))
  83. return false;
  84. return json_is_true(bool_val);
  85. }
  86. static void add_service(obs_property_t *list, json_t *service, bool show_all,
  87. const char *cur_service)
  88. {
  89. json_t *servers;
  90. const char *name;
  91. bool common;
  92. if (!json_is_object(service)) {
  93. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  94. "is not an object");
  95. return;
  96. }
  97. name = get_string_val(service, "name");
  98. if (!name) {
  99. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  100. "has no name");
  101. return;
  102. }
  103. common = get_bool_val(service, "common");
  104. if (!show_all && !common && strcmp(cur_service, name) != 0) {
  105. return;
  106. }
  107. servers = json_object_get(service, "servers");
  108. if (!servers || !json_is_array(servers)) {
  109. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  110. "'%s' has no servers", name);
  111. return;
  112. }
  113. obs_property_list_add_string(list, name, name);
  114. }
  115. static void add_services(obs_property_t *list, json_t *root, bool show_all,
  116. const char *cur_service)
  117. {
  118. json_t *service;
  119. size_t index;
  120. if (!json_is_array(root)) {
  121. blog(LOG_WARNING, "rtmp-common.c: [add_services] JSON file "
  122. "root is not an array");
  123. return;
  124. }
  125. json_array_foreach (root, index, service) {
  126. add_service(list, service, show_all, cur_service);
  127. }
  128. service = find_service(root, cur_service);
  129. if (!service && cur_service && *cur_service) {
  130. obs_property_list_insert_string(list, 0, cur_service,
  131. cur_service);
  132. obs_property_list_item_disable(list, 0, true);
  133. }
  134. }
  135. static json_t *open_json_file(const char *file)
  136. {
  137. char *file_data = os_quick_read_utf8_file(file);
  138. json_error_t error;
  139. json_t *root;
  140. json_t *list;
  141. int format_ver;
  142. if (!file_data)
  143. return NULL;
  144. root = json_loads(file_data, JSON_REJECT_DUPLICATES, &error);
  145. bfree(file_data);
  146. if (!root) {
  147. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  148. "Error reading JSON file (%d): %s",
  149. error.line, error.text);
  150. return NULL;
  151. }
  152. format_ver = get_int_val(root, "format_version");
  153. if (format_ver != RTMP_SERVICES_FORMAT_VERSION) {
  154. blog(LOG_DEBUG, "rtmp-common.c: [open_json_file] "
  155. "Wrong format version (%d), expected %d",
  156. format_ver, RTMP_SERVICES_FORMAT_VERSION);
  157. json_decref(root);
  158. return NULL;
  159. }
  160. list = json_object_get(root, "services");
  161. if (list)
  162. json_incref(list);
  163. json_decref(root);
  164. if (!list) {
  165. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  166. "No services list");
  167. return NULL;
  168. }
  169. return list;
  170. }
  171. static json_t *open_services_file(void)
  172. {
  173. char *file;
  174. json_t *root = NULL;
  175. file = obs_module_config_path("services.json");
  176. if (file) {
  177. root = open_json_file(file);
  178. bfree(file);
  179. }
  180. if (!root) {
  181. file = obs_module_file("services.json");
  182. if (file) {
  183. root = open_json_file(file);
  184. bfree(file);
  185. }
  186. }
  187. return root;
  188. }
  189. static void build_service_list(obs_property_t *list, json_t *root,
  190. bool show_all, const char *cur_service)
  191. {
  192. obs_property_list_clear(list);
  193. add_services(list, root, show_all, cur_service);
  194. }
  195. static void properties_data_destroy(void *data)
  196. {
  197. json_t *root = data;
  198. if (root)
  199. json_decref(root);
  200. }
  201. static bool fill_twitch_servers_locked(obs_property_t *servers_prop)
  202. {
  203. size_t count = twitch_ingest_count();
  204. obs_property_list_add_string(servers_prop,
  205. obs_module_text("Server.Auto"), "auto");
  206. if (count <= 1)
  207. return false;
  208. for (size_t i = 0; i < count; i++) {
  209. struct twitch_ingest ing = twitch_ingest(i);
  210. obs_property_list_add_string(servers_prop, ing.name, ing.url);
  211. }
  212. return true;
  213. }
  214. static inline bool fill_twitch_servers(obs_property_t *servers_prop)
  215. {
  216. bool success;
  217. twitch_ingests_lock();
  218. success = fill_twitch_servers_locked(servers_prop);
  219. twitch_ingests_unlock();
  220. return success;
  221. }
  222. static void fill_servers(obs_property_t *servers_prop, json_t *service,
  223. const char *name)
  224. {
  225. json_t *servers, *server;
  226. size_t index;
  227. obs_property_list_clear(servers_prop);
  228. servers = json_object_get(service, "servers");
  229. if (!json_is_array(servers)) {
  230. blog(LOG_WARNING, "rtmp-common.c: [fill_servers] "
  231. "Servers for service '%s' not a valid object",
  232. name);
  233. return;
  234. }
  235. if (strcmp(name, "Mixer.com - FTL") == 0) {
  236. obs_property_list_add_string(servers_prop,
  237. obs_module_text("Server.Auto"), "auto");
  238. }
  239. if (name && strcmp(name, "Twitch") == 0) {
  240. if (fill_twitch_servers(servers_prop))
  241. return;
  242. }
  243. json_array_foreach (servers, index, server) {
  244. const char *server_name = get_string_val(server, "name");
  245. const char *url = get_string_val(server, "url");
  246. if (!server_name || !url)
  247. continue;
  248. obs_property_list_add_string(servers_prop, server_name, url);
  249. }
  250. }
  251. static inline json_t *find_service(json_t *root, const char *name)
  252. {
  253. size_t index;
  254. json_t *service;
  255. json_array_foreach (root, index, service) {
  256. const char *cur_name = get_string_val(service, "name");
  257. if (strcmp(name, cur_name) == 0)
  258. return service;
  259. }
  260. return NULL;
  261. }
  262. static bool service_selected(obs_properties_t *props, obs_property_t *p,
  263. obs_data_t *settings)
  264. {
  265. const char *name = obs_data_get_string(settings, "service");
  266. json_t *root = obs_properties_get_param(props);
  267. json_t *service;
  268. if (!name || !*name)
  269. return false;
  270. service = find_service(root, name);
  271. if (!service) {
  272. const char *server = obs_data_get_string(settings, "server");
  273. obs_property_list_insert_string(p, 0, name, name);
  274. obs_property_list_item_disable(p, 0, true);
  275. p = obs_properties_get(props, "server");
  276. obs_property_list_insert_string(p, 0, server, server);
  277. obs_property_list_item_disable(p, 0, true);
  278. return true;
  279. }
  280. fill_servers(obs_properties_get(props, "server"), service, name);
  281. return true;
  282. }
  283. static bool show_all_services_toggled(obs_properties_t *ppts,
  284. obs_property_t *p, obs_data_t *settings)
  285. {
  286. const char *cur_service = obs_data_get_string(settings, "service");
  287. bool show_all = obs_data_get_bool(settings, "show_all");
  288. json_t *root = obs_properties_get_param(ppts);
  289. if (!root)
  290. return false;
  291. build_service_list(obs_properties_get(ppts, "service"), root, show_all,
  292. cur_service);
  293. UNUSED_PARAMETER(p);
  294. return true;
  295. }
  296. static obs_properties_t *rtmp_common_properties(void *unused)
  297. {
  298. UNUSED_PARAMETER(unused);
  299. obs_properties_t *ppts = obs_properties_create();
  300. obs_property_t *p;
  301. json_t *root;
  302. root = open_services_file();
  303. if (root)
  304. obs_properties_set_param(ppts, root, properties_data_destroy);
  305. p = obs_properties_add_list(ppts, "service",
  306. obs_module_text("Service"),
  307. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  308. obs_property_set_modified_callback(p, service_selected);
  309. p = obs_properties_add_bool(ppts, "show_all",
  310. obs_module_text("ShowAll"));
  311. obs_property_set_modified_callback(p, show_all_services_toggled);
  312. obs_properties_add_list(ppts, "server", obs_module_text("Server"),
  313. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  314. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  315. OBS_TEXT_PASSWORD);
  316. return ppts;
  317. }
  318. static void apply_video_encoder_settings(obs_data_t *settings,
  319. json_t *recommended)
  320. {
  321. json_t *item = json_object_get(recommended, "keyint");
  322. if (item && json_is_integer(item)) {
  323. int keyint = (int)json_integer_value(item);
  324. obs_data_set_int(settings, "keyint_sec", keyint);
  325. }
  326. obs_data_set_string(settings, "rate_control", "CBR");
  327. item = json_object_get(recommended, "profile");
  328. if (item && json_is_string(item)) {
  329. const char *profile = json_string_value(item);
  330. obs_data_set_string(settings, "profile", profile);
  331. }
  332. item = json_object_get(recommended, "max video bitrate");
  333. if (item && json_is_integer(item)) {
  334. int max_bitrate = (int)json_integer_value(item);
  335. if (obs_data_get_int(settings, "bitrate") > max_bitrate) {
  336. obs_data_set_int(settings, "bitrate", max_bitrate);
  337. obs_data_set_int(settings, "buffer_size", max_bitrate);
  338. }
  339. }
  340. item = json_object_get(recommended, "bframes");
  341. if (item && json_is_integer(item))
  342. obs_data_set_int(settings, "bf", 0);
  343. item = json_object_get(recommended, "x264opts");
  344. if (item && json_is_string(item)) {
  345. const char *x264_settings = json_string_value(item);
  346. const char *cur_settings =
  347. obs_data_get_string(settings, "x264opts");
  348. struct dstr opts;
  349. dstr_init_copy(&opts, cur_settings);
  350. if (!dstr_is_empty(&opts))
  351. dstr_cat(&opts, " ");
  352. dstr_cat(&opts, x264_settings);
  353. obs_data_set_string(settings, "x264opts", opts.array);
  354. dstr_free(&opts);
  355. }
  356. }
  357. static void apply_audio_encoder_settings(obs_data_t *settings,
  358. json_t *recommended)
  359. {
  360. json_t *item = json_object_get(recommended, "max audio bitrate");
  361. if (item && json_is_integer(item)) {
  362. int max_bitrate = (int)json_integer_value(item);
  363. if (obs_data_get_int(settings, "bitrate") > max_bitrate)
  364. obs_data_set_int(settings, "bitrate", max_bitrate);
  365. }
  366. }
  367. static void initialize_output(struct rtmp_common *service, json_t *root,
  368. obs_data_t *video_settings, obs_data_t *audio_settings)
  369. {
  370. json_t *json_service = find_service(root, service->service);
  371. json_t *recommended;
  372. if (!json_service) {
  373. if (service->service && *service->service)
  374. blog(LOG_WARNING, "rtmp-common.c: [initialize_output] "
  375. "Could not find service '%s'",
  376. service->service);
  377. return;
  378. }
  379. recommended = json_object_get(json_service, "recommended");
  380. if (!recommended)
  381. return;
  382. if (video_settings)
  383. apply_video_encoder_settings(video_settings, recommended);
  384. if (audio_settings)
  385. apply_audio_encoder_settings(audio_settings, recommended);
  386. }
  387. static void rtmp_common_apply_settings(void *data,
  388. obs_data_t *video_settings, obs_data_t *audio_settings)
  389. {
  390. struct rtmp_common *service = data;
  391. json_t *root = open_services_file();
  392. if (root) {
  393. initialize_output(service, root, video_settings,
  394. audio_settings);
  395. json_decref(root);
  396. }
  397. }
  398. static const char *rtmp_common_get_output_type(void *data)
  399. {
  400. struct rtmp_common *service = data;
  401. return service->output;
  402. }
  403. static const char *rtmp_common_url(void *data)
  404. {
  405. struct rtmp_common *service = data;
  406. if (service->service && strcmp(service->service, "Twitch") == 0) {
  407. if (service->server && strcmp(service->server, "auto") == 0) {
  408. struct twitch_ingest ing;
  409. twitch_ingests_refresh(3);
  410. twitch_ingests_lock();
  411. ing = twitch_ingest(0);
  412. twitch_ingests_unlock();
  413. return ing.url;
  414. }
  415. }
  416. return service->server;
  417. }
  418. static const char *rtmp_common_key(void *data)
  419. {
  420. struct rtmp_common *service = data;
  421. return service->key;
  422. }
  423. struct obs_service_info rtmp_common_service = {
  424. .id = "rtmp_common",
  425. .get_name = rtmp_common_getname,
  426. .create = rtmp_common_create,
  427. .destroy = rtmp_common_destroy,
  428. .update = rtmp_common_update,
  429. .get_properties = rtmp_common_properties,
  430. .get_url = rtmp_common_url,
  431. .get_key = rtmp_common_key,
  432. .apply_encoder_settings = rtmp_common_apply_settings,
  433. .get_output_type = rtmp_common_get_output_type,
  434. };