rtmp-common.c 15 KB

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