rtmp-common.c 14 KB

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