rtmp-common.c 16 KB

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