rtmp-common.c 16 KB

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