rtmp-common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 (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,
  144. "rtmp-common.c: [add_service] service "
  145. "'%s' has no servers",
  146. name);
  147. return;
  148. }
  149. obs_property_list_add_string(list, name, name);
  150. }
  151. static void add_services(obs_property_t *list, json_t *root, bool show_all,
  152. const char *cur_service)
  153. {
  154. json_t *service;
  155. size_t index;
  156. if (!json_is_array(root)) {
  157. blog(LOG_WARNING, "rtmp-common.c: [add_services] JSON file "
  158. "root is not an array");
  159. return;
  160. }
  161. json_array_foreach (root, index, service) {
  162. add_service(list, service, show_all, cur_service);
  163. }
  164. service = find_service(root, cur_service, NULL);
  165. if (!service && cur_service && *cur_service) {
  166. obs_property_list_insert_string(list, 0, cur_service,
  167. cur_service);
  168. obs_property_list_item_disable(list, 0, true);
  169. }
  170. }
  171. static json_t *open_json_file(const char *file)
  172. {
  173. char *file_data = os_quick_read_utf8_file(file);
  174. json_error_t error;
  175. json_t *root;
  176. json_t *list;
  177. int format_ver;
  178. if (!file_data)
  179. return NULL;
  180. root = json_loads(file_data, JSON_REJECT_DUPLICATES, &error);
  181. bfree(file_data);
  182. if (!root) {
  183. blog(LOG_WARNING,
  184. "rtmp-common.c: [open_json_file] "
  185. "Error reading JSON file (%d): %s",
  186. error.line, error.text);
  187. return NULL;
  188. }
  189. format_ver = get_int_val(root, "format_version");
  190. if (format_ver != RTMP_SERVICES_FORMAT_VERSION) {
  191. blog(LOG_DEBUG,
  192. "rtmp-common.c: [open_json_file] "
  193. "Wrong format version (%d), expected %d",
  194. format_ver, RTMP_SERVICES_FORMAT_VERSION);
  195. json_decref(root);
  196. return NULL;
  197. }
  198. list = json_object_get(root, "services");
  199. if (list)
  200. json_incref(list);
  201. json_decref(root);
  202. if (!list) {
  203. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  204. "No services list");
  205. return NULL;
  206. }
  207. return list;
  208. }
  209. static json_t *open_services_file(void)
  210. {
  211. char *file;
  212. json_t *root = NULL;
  213. file = obs_module_config_path("services.json");
  214. if (file) {
  215. root = open_json_file(file);
  216. bfree(file);
  217. }
  218. if (!root) {
  219. file = obs_module_file("services.json");
  220. if (file) {
  221. root = open_json_file(file);
  222. bfree(file);
  223. }
  224. }
  225. return root;
  226. }
  227. static void build_service_list(obs_property_t *list, json_t *root,
  228. bool show_all, const char *cur_service)
  229. {
  230. obs_property_list_clear(list);
  231. add_services(list, root, show_all, cur_service);
  232. }
  233. static void properties_data_destroy(void *data)
  234. {
  235. json_t *root = data;
  236. if (root)
  237. json_decref(root);
  238. }
  239. static bool fill_twitch_servers_locked(obs_property_t *servers_prop)
  240. {
  241. size_t count = twitch_ingest_count();
  242. obs_property_list_add_string(servers_prop,
  243. obs_module_text("Server.Auto"), "auto");
  244. if (count <= 1)
  245. return false;
  246. for (size_t i = 0; i < count; i++) {
  247. struct twitch_ingest ing = twitch_ingest(i);
  248. obs_property_list_add_string(servers_prop, ing.name, ing.url);
  249. }
  250. return true;
  251. }
  252. static inline bool fill_twitch_servers(obs_property_t *servers_prop)
  253. {
  254. bool success;
  255. twitch_ingests_lock();
  256. success = fill_twitch_servers_locked(servers_prop);
  257. twitch_ingests_unlock();
  258. return success;
  259. }
  260. static void fill_servers(obs_property_t *servers_prop, json_t *service,
  261. const char *name)
  262. {
  263. json_t *servers, *server;
  264. size_t index;
  265. obs_property_list_clear(servers_prop);
  266. servers = json_object_get(service, "servers");
  267. if (!json_is_array(servers)) {
  268. blog(LOG_WARNING,
  269. "rtmp-common.c: [fill_servers] "
  270. "Servers for service '%s' not a valid object",
  271. name);
  272. return;
  273. }
  274. if (strcmp(name, "Mixer.com - FTL") == 0) {
  275. obs_property_list_add_string(
  276. servers_prop, obs_module_text("Server.Auto"), "auto");
  277. }
  278. if (strcmp(name, "Twitch") == 0) {
  279. if (fill_twitch_servers(servers_prop))
  280. return;
  281. }
  282. json_array_foreach (servers, index, server) {
  283. const char *server_name = get_string_val(server, "name");
  284. const char *url = get_string_val(server, "url");
  285. if (!server_name || !url)
  286. continue;
  287. obs_property_list_add_string(servers_prop, server_name, url);
  288. }
  289. }
  290. static inline json_t *find_service(json_t *root, const char *name,
  291. const char **p_new_name)
  292. {
  293. size_t index;
  294. json_t *service;
  295. if (p_new_name)
  296. *p_new_name = NULL;
  297. json_array_foreach (root, index, service) {
  298. const char *cur_name = get_string_val(service, "name");
  299. if (strcmp(name, cur_name) == 0)
  300. return service;
  301. /* check for alternate names */
  302. json_t *alt_names = json_object_get(service, "alt_names");
  303. size_t alt_name_idx;
  304. json_t *alt_name_obj;
  305. json_array_foreach (alt_names, alt_name_idx, alt_name_obj) {
  306. const char *alt_name = json_string_value(alt_name_obj);
  307. if (alt_name && strcmp(name, alt_name) == 0) {
  308. if (p_new_name)
  309. *p_new_name = cur_name;
  310. return service;
  311. }
  312. }
  313. }
  314. return NULL;
  315. }
  316. static bool service_selected(obs_properties_t *props, obs_property_t *p,
  317. obs_data_t *settings)
  318. {
  319. const char *name = obs_data_get_string(settings, "service");
  320. json_t *root = obs_properties_get_param(props);
  321. json_t *service;
  322. const char *new_name;
  323. if (!name || !*name)
  324. return false;
  325. service = find_service(root, name, &new_name);
  326. if (!service) {
  327. const char *server = obs_data_get_string(settings, "server");
  328. obs_property_list_insert_string(p, 0, name, name);
  329. obs_property_list_item_disable(p, 0, true);
  330. p = obs_properties_get(props, "server");
  331. obs_property_list_insert_string(p, 0, server, server);
  332. obs_property_list_item_disable(p, 0, true);
  333. return true;
  334. }
  335. if (new_name) {
  336. name = new_name;
  337. obs_data_set_string(settings, "service", name);
  338. }
  339. fill_servers(obs_properties_get(props, "server"), service, name);
  340. return true;
  341. }
  342. static bool show_all_services_toggled(obs_properties_t *ppts, obs_property_t *p,
  343. obs_data_t *settings)
  344. {
  345. const char *cur_service = obs_data_get_string(settings, "service");
  346. bool show_all = obs_data_get_bool(settings, "show_all");
  347. json_t *root = obs_properties_get_param(ppts);
  348. if (!root)
  349. return false;
  350. build_service_list(obs_properties_get(ppts, "service"), root, show_all,
  351. cur_service);
  352. UNUSED_PARAMETER(p);
  353. return true;
  354. }
  355. static obs_properties_t *rtmp_common_properties(void *unused)
  356. {
  357. UNUSED_PARAMETER(unused);
  358. obs_properties_t *ppts = obs_properties_create();
  359. obs_property_t *p;
  360. json_t *root;
  361. root = open_services_file();
  362. if (root)
  363. obs_properties_set_param(ppts, root, properties_data_destroy);
  364. p = obs_properties_add_list(ppts, "service", obs_module_text("Service"),
  365. OBS_COMBO_TYPE_LIST,
  366. OBS_COMBO_FORMAT_STRING);
  367. obs_property_set_modified_callback(p, service_selected);
  368. p = obs_properties_add_bool(ppts, "show_all",
  369. obs_module_text("ShowAll"));
  370. obs_property_set_modified_callback(p, show_all_services_toggled);
  371. obs_properties_add_list(ppts, "server", obs_module_text("Server"),
  372. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  373. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  374. OBS_TEXT_PASSWORD);
  375. return ppts;
  376. }
  377. static void apply_video_encoder_settings(obs_data_t *settings,
  378. json_t *recommended)
  379. {
  380. json_t *item = json_object_get(recommended, "keyint");
  381. if (json_is_integer(item)) {
  382. int keyint = (int)json_integer_value(item);
  383. obs_data_set_int(settings, "keyint_sec", keyint);
  384. }
  385. obs_data_set_string(settings, "rate_control", "CBR");
  386. item = json_object_get(recommended, "profile");
  387. obs_data_item_t *enc_item = obs_data_item_byname(settings, "profile");
  388. if (json_is_string(item) &&
  389. obs_data_item_gettype(enc_item) == OBS_DATA_STRING) {
  390. const char *profile = json_string_value(item);
  391. obs_data_set_string(settings, "profile", profile);
  392. }
  393. item = json_object_get(recommended, "max video bitrate");
  394. if (json_is_integer(item)) {
  395. int max_bitrate = (int)json_integer_value(item);
  396. if (obs_data_get_int(settings, "bitrate") > max_bitrate) {
  397. obs_data_set_int(settings, "bitrate", max_bitrate);
  398. obs_data_set_int(settings, "buffer_size", max_bitrate);
  399. }
  400. }
  401. item = json_object_get(recommended, "bframes");
  402. if (json_is_integer(item))
  403. obs_data_set_int(settings, "bf", 0);
  404. item = json_object_get(recommended, "x264opts");
  405. if (json_is_string(item)) {
  406. const char *x264_settings = json_string_value(item);
  407. const char *cur_settings =
  408. obs_data_get_string(settings, "x264opts");
  409. struct dstr opts;
  410. dstr_init_copy(&opts, cur_settings);
  411. if (!dstr_is_empty(&opts))
  412. dstr_cat(&opts, " ");
  413. dstr_cat(&opts, x264_settings);
  414. obs_data_set_string(settings, "x264opts", opts.array);
  415. dstr_free(&opts);
  416. }
  417. }
  418. static void apply_audio_encoder_settings(obs_data_t *settings,
  419. json_t *recommended)
  420. {
  421. json_t *item = json_object_get(recommended, "max audio bitrate");
  422. if (json_is_integer(item)) {
  423. int max_bitrate = (int)json_integer_value(item);
  424. if (obs_data_get_int(settings, "bitrate") > max_bitrate)
  425. obs_data_set_int(settings, "bitrate", max_bitrate);
  426. }
  427. }
  428. static void initialize_output(struct rtmp_common *service, json_t *root,
  429. obs_data_t *video_settings,
  430. obs_data_t *audio_settings)
  431. {
  432. json_t *json_service = find_service(root, service->service, NULL);
  433. json_t *recommended;
  434. if (!json_service) {
  435. if (service->service && *service->service)
  436. blog(LOG_WARNING,
  437. "rtmp-common.c: [initialize_output] "
  438. "Could not find service '%s'",
  439. service->service);
  440. return;
  441. }
  442. recommended = json_object_get(json_service, "recommended");
  443. if (!recommended)
  444. return;
  445. if (video_settings)
  446. apply_video_encoder_settings(video_settings, recommended);
  447. if (audio_settings)
  448. apply_audio_encoder_settings(audio_settings, recommended);
  449. }
  450. static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings,
  451. obs_data_t *audio_settings)
  452. {
  453. struct rtmp_common *service = data;
  454. json_t *root = open_services_file();
  455. if (root) {
  456. initialize_output(service, root, video_settings,
  457. audio_settings);
  458. json_decref(root);
  459. }
  460. }
  461. static const char *rtmp_common_get_output_type(void *data)
  462. {
  463. struct rtmp_common *service = data;
  464. return service->output;
  465. }
  466. static const char *rtmp_common_url(void *data)
  467. {
  468. struct rtmp_common *service = data;
  469. if (service->service && strcmp(service->service, "Twitch") == 0) {
  470. if (service->server && strcmp(service->server, "auto") == 0) {
  471. struct twitch_ingest ing;
  472. twitch_ingests_refresh(3);
  473. twitch_ingests_lock();
  474. ing = twitch_ingest(0);
  475. twitch_ingests_unlock();
  476. return ing.url;
  477. }
  478. }
  479. return service->server;
  480. }
  481. static const char *rtmp_common_key(void *data)
  482. {
  483. struct rtmp_common *service = data;
  484. return service->key;
  485. }
  486. struct obs_service_info rtmp_common_service = {
  487. .id = "rtmp_common",
  488. .get_name = rtmp_common_getname,
  489. .create = rtmp_common_create,
  490. .destroy = rtmp_common_destroy,
  491. .update = rtmp_common_update,
  492. .get_properties = rtmp_common_properties,
  493. .get_url = rtmp_common_url,
  494. .get_key = rtmp_common_key,
  495. .apply_encoder_settings = rtmp_common_apply_settings,
  496. .get_output_type = rtmp_common_get_output_type,
  497. };