rtmp-common.c 14 KB

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