rtmp-common.c 15 KB

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