rtmp-common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 "service-specific/twitch.h"
  8. #include "service-specific/nimotv.h"
  9. #include "service-specific/showroom.h"
  10. #include "service-specific/dacast.h"
  11. struct rtmp_common {
  12. char *service;
  13. char *protocol;
  14. char *server;
  15. char *key;
  16. struct obs_service_resolution *supported_resolutions;
  17. size_t supported_resolutions_count;
  18. int max_fps;
  19. char **video_codecs;
  20. char **audio_codecs;
  21. bool supports_additional_audio_track;
  22. };
  23. static const char *rtmp_common_getname(void *unused)
  24. {
  25. UNUSED_PARAMETER(unused);
  26. return obs_module_text("StreamingServices");
  27. }
  28. static json_t *open_services_file(void);
  29. static inline json_t *find_service(json_t *root, const char *name,
  30. const char **p_new_name);
  31. static inline bool get_bool_val(json_t *service, const char *key);
  32. static inline const char *get_string_val(json_t *service, const char *key);
  33. static inline int get_int_val(json_t *service, const char *key);
  34. extern void twitch_ingests_refresh(int seconds);
  35. static void ensure_valid_url(struct rtmp_common *service, json_t *json,
  36. obs_data_t *settings)
  37. {
  38. json_t *servers = json_object_get(json, "servers");
  39. const char *top_url = NULL;
  40. json_t *server;
  41. size_t index;
  42. if (!service->server || !servers || !json_is_array(servers))
  43. return;
  44. if (astrstri(service->service, "Facebook") == NULL)
  45. return;
  46. json_array_foreach (servers, index, server) {
  47. const char *url = get_string_val(server, "url");
  48. if (!url)
  49. continue;
  50. if (!top_url)
  51. top_url = url;
  52. if (astrcmpi(service->server, url) == 0)
  53. return;
  54. }
  55. /* server was not found in server list, use first server instead */
  56. if (top_url) {
  57. bfree(service->server);
  58. service->server = bstrdup(top_url);
  59. obs_data_set_string(settings, "server", top_url);
  60. }
  61. }
  62. static void update_recommendations(struct rtmp_common *service, json_t *rec)
  63. {
  64. json_t *sr = json_object_get(rec, "supported resolutions");
  65. if (sr && json_is_array(sr)) {
  66. DARRAY(struct obs_service_resolution) res_list;
  67. json_t *res_obj;
  68. size_t index;
  69. da_init(res_list);
  70. json_array_foreach (sr, index, res_obj) {
  71. if (!json_is_string(res_obj))
  72. continue;
  73. const char *res_str = json_string_value(res_obj);
  74. struct obs_service_resolution res;
  75. if (sscanf(res_str, "%dx%d", &res.cx, &res.cy) != 2)
  76. continue;
  77. if (res.cx <= 0 || res.cy <= 0)
  78. continue;
  79. da_push_back(res_list, &res);
  80. }
  81. if (res_list.num) {
  82. service->supported_resolutions = res_list.array;
  83. service->supported_resolutions_count = res_list.num;
  84. }
  85. }
  86. service->max_fps = get_int_val(rec, "max fps");
  87. }
  88. #define RTMP_PREFIX "rtmp://"
  89. #define RTMPS_PREFIX "rtmps://"
  90. static const char *get_protocol(json_t *service, obs_data_t *settings)
  91. {
  92. const char *protocol = get_string_val(service, "protocol");
  93. if (protocol) {
  94. return protocol;
  95. }
  96. json_t *servers = json_object_get(service, "servers");
  97. if (!json_is_array(servers))
  98. return "RTMP";
  99. json_t *server = json_array_get(servers, 0);
  100. const char *url = get_string_val(server, "url");
  101. if (strncmp(url, RTMPS_PREFIX, strlen(RTMPS_PREFIX)) == 0) {
  102. obs_data_set_string(settings, "protocol", "RTMPS");
  103. return "RTMPS";
  104. }
  105. return "RTMP";
  106. }
  107. static void copy_info_to_settings(json_t *service, obs_data_t *settings);
  108. static void rtmp_common_update(void *data, obs_data_t *settings)
  109. {
  110. struct rtmp_common *service = data;
  111. bfree(service->supported_resolutions);
  112. if (service->video_codecs)
  113. bfree(service->video_codecs);
  114. if (service->audio_codecs)
  115. bfree(service->audio_codecs);
  116. bfree(service->service);
  117. bfree(service->protocol);
  118. bfree(service->server);
  119. bfree(service->key);
  120. service->service = bstrdup(obs_data_get_string(settings, "service"));
  121. service->protocol = bstrdup(obs_data_get_string(settings, "protocol"));
  122. service->server = bstrdup(obs_data_get_string(settings, "server"));
  123. service->key = bstrdup(obs_data_get_string(settings, "key"));
  124. service->supports_additional_audio_track = false;
  125. service->video_codecs = NULL;
  126. service->audio_codecs = NULL;
  127. service->supported_resolutions = NULL;
  128. service->supported_resolutions_count = 0;
  129. service->max_fps = 0;
  130. json_t *root = open_services_file();
  131. if (root) {
  132. const char *new_name;
  133. json_t *serv = find_service(root, service->service, &new_name);
  134. if (new_name) {
  135. bfree(service->service);
  136. service->service = bstrdup(new_name);
  137. }
  138. if ((service->protocol == NULL ||
  139. service->protocol[0] == '\0')) {
  140. bfree(service->protocol);
  141. service->protocol =
  142. bstrdup(get_protocol(serv, settings));
  143. }
  144. if (serv) {
  145. copy_info_to_settings(serv, settings);
  146. json_t *rec = json_object_get(serv, "recommended");
  147. if (json_is_object(rec)) {
  148. update_recommendations(service, rec);
  149. }
  150. service->supports_additional_audio_track = get_bool_val(
  151. serv, "supports_additional_audio_track");
  152. ensure_valid_url(service, serv, settings);
  153. }
  154. }
  155. json_decref(root);
  156. }
  157. static void rtmp_common_destroy(void *data)
  158. {
  159. struct rtmp_common *service = data;
  160. bfree(service->supported_resolutions);
  161. if (service->video_codecs)
  162. bfree(service->video_codecs);
  163. if (service->audio_codecs)
  164. bfree(service->audio_codecs);
  165. bfree(service->service);
  166. bfree(service->protocol);
  167. bfree(service->server);
  168. bfree(service->key);
  169. bfree(service);
  170. }
  171. static void *rtmp_common_create(obs_data_t *settings, obs_service_t *service)
  172. {
  173. struct rtmp_common *data = bzalloc(sizeof(struct rtmp_common));
  174. rtmp_common_update(data, settings);
  175. UNUSED_PARAMETER(service);
  176. return data;
  177. }
  178. static inline const char *get_string_val(json_t *service, const char *key)
  179. {
  180. json_t *str_val = json_object_get(service, key);
  181. if (!str_val || !json_is_string(str_val))
  182. return NULL;
  183. return json_string_value(str_val);
  184. }
  185. static inline int get_int_val(json_t *service, const char *key)
  186. {
  187. json_t *integer_val = json_object_get(service, key);
  188. if (!integer_val || !json_is_integer(integer_val))
  189. return 0;
  190. return (int)json_integer_value(integer_val);
  191. }
  192. static inline bool get_bool_val(json_t *service, const char *key)
  193. {
  194. json_t *bool_val = json_object_get(service, key);
  195. if (!bool_val || !json_is_boolean(bool_val))
  196. return false;
  197. return json_is_true(bool_val);
  198. }
  199. static bool is_protocol_available(json_t *service)
  200. {
  201. const char *protocol = get_string_val(service, "protocol");
  202. if (protocol)
  203. return obs_is_output_protocol_registered(protocol);
  204. /* Test RTMP and RTMPS if no protocol found */
  205. json_t *servers;
  206. size_t index;
  207. json_t *server;
  208. const char *url;
  209. bool ret = false;
  210. servers = json_object_get(service, "servers");
  211. json_array_foreach (servers, index, server) {
  212. url = get_string_val(server, "url");
  213. if (strncmp(url, RTMP_PREFIX, strlen(RTMP_PREFIX)) == 0)
  214. ret |= obs_is_output_protocol_registered("RTMP");
  215. else if (strncmp(url, RTMPS_PREFIX, strlen(RTMPS_PREFIX)) == 0)
  216. ret |= obs_is_output_protocol_registered("RTMPS");
  217. }
  218. return ret;
  219. }
  220. static void add_service(obs_property_t *list, json_t *service, bool show_all,
  221. const char *cur_service)
  222. {
  223. json_t *servers;
  224. const char *name;
  225. bool common;
  226. if (!json_is_object(service)) {
  227. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  228. "is not an object");
  229. return;
  230. }
  231. name = get_string_val(service, "name");
  232. if (!name) {
  233. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  234. "has no name");
  235. return;
  236. }
  237. common = get_bool_val(service, "common");
  238. if (!show_all && !common && strcmp(cur_service, name) != 0) {
  239. return;
  240. }
  241. servers = json_object_get(service, "servers");
  242. if (!servers || !json_is_array(servers)) {
  243. blog(LOG_WARNING,
  244. "rtmp-common.c: [add_service] service "
  245. "'%s' has no servers",
  246. name);
  247. return;
  248. }
  249. obs_property_list_add_string(list, name, name);
  250. }
  251. static void add_services(obs_property_t *list, json_t *root, bool show_all,
  252. const char *cur_service)
  253. {
  254. json_t *service;
  255. size_t index;
  256. if (!json_is_array(root)) {
  257. blog(LOG_WARNING, "rtmp-common.c: [add_services] JSON file "
  258. "root is not an array");
  259. return;
  260. }
  261. json_array_foreach (root, index, service) {
  262. /* Skip service with non-available protocol */
  263. if (!is_protocol_available(service))
  264. continue;
  265. add_service(list, service, show_all, cur_service);
  266. }
  267. service = find_service(root, cur_service, NULL);
  268. if (!service && cur_service && *cur_service) {
  269. obs_property_list_insert_string(list, 0, cur_service,
  270. cur_service);
  271. obs_property_list_item_disable(list, 0, true);
  272. }
  273. }
  274. static json_t *open_json_file(const char *file)
  275. {
  276. char *file_data = os_quick_read_utf8_file(file);
  277. json_error_t error;
  278. json_t *root;
  279. json_t *list;
  280. int format_ver;
  281. if (!file_data)
  282. return NULL;
  283. root = json_loads(file_data, JSON_REJECT_DUPLICATES, &error);
  284. bfree(file_data);
  285. if (!root) {
  286. blog(LOG_WARNING,
  287. "rtmp-common.c: [open_json_file] "
  288. "Error reading JSON file (%d): %s",
  289. error.line, error.text);
  290. return NULL;
  291. }
  292. format_ver = get_int_val(root, "format_version");
  293. if (format_ver != RTMP_SERVICES_FORMAT_VERSION) {
  294. blog(LOG_DEBUG,
  295. "rtmp-common.c: [open_json_file] "
  296. "Wrong format version (%d), expected %d",
  297. format_ver, RTMP_SERVICES_FORMAT_VERSION);
  298. json_decref(root);
  299. return NULL;
  300. }
  301. list = json_object_get(root, "services");
  302. if (list)
  303. json_incref(list);
  304. json_decref(root);
  305. if (!list) {
  306. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  307. "No services list");
  308. return NULL;
  309. }
  310. return list;
  311. }
  312. static json_t *open_services_file(void)
  313. {
  314. char *file;
  315. json_t *root = NULL;
  316. file = obs_module_config_path("services.json");
  317. if (file) {
  318. root = open_json_file(file);
  319. bfree(file);
  320. }
  321. if (!root) {
  322. file = obs_module_file("services.json");
  323. if (file) {
  324. root = open_json_file(file);
  325. bfree(file);
  326. }
  327. }
  328. return root;
  329. }
  330. static void build_service_list(obs_property_t *list, json_t *root,
  331. bool show_all, const char *cur_service)
  332. {
  333. obs_property_list_clear(list);
  334. add_services(list, root, show_all, cur_service);
  335. }
  336. static void properties_data_destroy(void *data)
  337. {
  338. json_t *root = data;
  339. if (root)
  340. json_decref(root);
  341. }
  342. static bool fill_twitch_servers_locked(obs_property_t *servers_prop)
  343. {
  344. size_t count = twitch_ingest_count();
  345. obs_property_list_add_string(servers_prop,
  346. obs_module_text("Server.Auto"), "auto");
  347. if (count <= 1)
  348. return false;
  349. for (size_t i = 0; i < count; i++) {
  350. struct twitch_ingest ing = twitch_ingest(i);
  351. obs_property_list_add_string(servers_prop, ing.name, ing.url);
  352. }
  353. return true;
  354. }
  355. static inline bool fill_twitch_servers(obs_property_t *servers_prop)
  356. {
  357. bool success;
  358. twitch_ingests_lock();
  359. success = fill_twitch_servers_locked(servers_prop);
  360. twitch_ingests_unlock();
  361. return success;
  362. }
  363. static void fill_servers(obs_property_t *servers_prop, json_t *service,
  364. const char *name)
  365. {
  366. json_t *servers, *server;
  367. size_t index;
  368. obs_property_list_clear(servers_prop);
  369. servers = json_object_get(service, "servers");
  370. if (!json_is_array(servers)) {
  371. blog(LOG_WARNING,
  372. "rtmp-common.c: [fill_servers] "
  373. "Servers for service '%s' not a valid object",
  374. name);
  375. return;
  376. }
  377. /* Assumption: Twitch should be RTMP only, so no RTMPS check */
  378. if (strcmp(name, "Twitch") == 0) {
  379. if (fill_twitch_servers(servers_prop))
  380. return;
  381. }
  382. /* Assumption: Nimo TV should be RTMP only, so no RTMPS check in the ingest */
  383. if (strcmp(name, "Nimo TV") == 0) {
  384. obs_property_list_add_string(
  385. servers_prop, obs_module_text("Server.Auto"), "auto");
  386. }
  387. json_array_foreach (servers, index, server) {
  388. const char *server_name = get_string_val(server, "name");
  389. const char *url = get_string_val(server, "url");
  390. if (!server_name || !url)
  391. continue;
  392. /* Skip RTMPS server if protocol not registered */
  393. if ((!obs_is_output_protocol_registered("RTMPS")) &&
  394. (strncmp(url, "rtmps://", 8) == 0))
  395. continue;
  396. obs_property_list_add_string(servers_prop, server_name, url);
  397. }
  398. }
  399. static void copy_string_from_json_if_available(json_t *service,
  400. obs_data_t *settings,
  401. const char *name)
  402. {
  403. const char *string = get_string_val(service, name);
  404. if (string)
  405. obs_data_set_string(settings, name, string);
  406. }
  407. static void fill_more_info_link(json_t *service, obs_data_t *settings)
  408. {
  409. copy_string_from_json_if_available(service, settings, "more_info_link");
  410. }
  411. static void fill_stream_key_link(json_t *service, obs_data_t *settings)
  412. {
  413. copy_string_from_json_if_available(service, settings,
  414. "stream_key_link");
  415. }
  416. static void update_protocol(json_t *service, obs_data_t *settings)
  417. {
  418. const char *protocol = get_string_val(service, "protocol");
  419. if (protocol) {
  420. obs_data_set_string(settings, "protocol", protocol);
  421. return;
  422. }
  423. json_t *servers = json_object_get(service, "servers");
  424. if (!json_is_array(servers))
  425. return;
  426. json_t *server = json_array_get(servers, 0);
  427. const char *url = get_string_val(server, "url");
  428. if (strncmp(url, RTMPS_PREFIX, strlen(RTMPS_PREFIX)) == 0) {
  429. obs_data_set_string(settings, "protocol", "RTMPS");
  430. return;
  431. }
  432. obs_data_set_string(settings, "protocol", "RTMP");
  433. }
  434. static void copy_info_to_settings(json_t *service, obs_data_t *settings)
  435. {
  436. const char *name = obs_data_get_string(settings, "service");
  437. fill_more_info_link(service, settings);
  438. fill_stream_key_link(service, settings);
  439. copy_string_from_json_if_available(
  440. service, settings, "multitrack_video_configuration_url");
  441. copy_string_from_json_if_available(service, settings,
  442. "multitrack_video_name");
  443. if (!obs_data_has_user_value(settings, "multitrack_video_name")) {
  444. obs_data_set_string(settings, "multitrack_video_name",
  445. "Multitrack Video");
  446. }
  447. const char *learn_more_link_url =
  448. get_string_val(service, "multitrack_video_learn_more_link");
  449. struct dstr learn_more_link = {0};
  450. if (learn_more_link_url) {
  451. dstr_init_copy(
  452. &learn_more_link,
  453. obs_module_text("MultitrackVideo.LearnMoreLink"));
  454. dstr_replace(&learn_more_link, "%1", learn_more_link_url);
  455. }
  456. struct dstr str;
  457. dstr_init_copy(&str, obs_module_text("MultitrackVideo.Disclaimer"));
  458. dstr_replace(&str, "%1",
  459. obs_data_get_string(settings, "multitrack_video_name"));
  460. dstr_replace(&str, "%2", name);
  461. if (learn_more_link.array) {
  462. dstr_cat(&str, learn_more_link.array);
  463. }
  464. obs_data_set_string(settings, "multitrack_video_disclaimer", str.array);
  465. dstr_free(&learn_more_link);
  466. dstr_free(&str);
  467. update_protocol(service, settings);
  468. }
  469. static inline json_t *find_service(json_t *root, const char *name,
  470. const char **p_new_name)
  471. {
  472. size_t index;
  473. json_t *service;
  474. if (p_new_name)
  475. *p_new_name = NULL;
  476. json_array_foreach (root, index, service) {
  477. /* skip service with non-available protocol */
  478. if (!is_protocol_available(service))
  479. continue;
  480. const char *cur_name = get_string_val(service, "name");
  481. if (strcmp(name, cur_name) == 0)
  482. return service;
  483. /* check for alternate names */
  484. json_t *alt_names = json_object_get(service, "alt_names");
  485. size_t alt_name_idx;
  486. json_t *alt_name_obj;
  487. json_array_foreach (alt_names, alt_name_idx, alt_name_obj) {
  488. const char *alt_name = json_string_value(alt_name_obj);
  489. if (alt_name && strcmp(name, alt_name) == 0) {
  490. if (p_new_name)
  491. *p_new_name = cur_name;
  492. return service;
  493. }
  494. }
  495. }
  496. return NULL;
  497. }
  498. static bool service_selected(obs_properties_t *props, obs_property_t *p,
  499. obs_data_t *settings)
  500. {
  501. const char *name = obs_data_get_string(settings, "service");
  502. json_t *root = obs_properties_get_param(props);
  503. json_t *service;
  504. const char *new_name;
  505. if (!name || !*name)
  506. return false;
  507. service = find_service(root, name, &new_name);
  508. if (!service) {
  509. const char *server = obs_data_get_string(settings, "server");
  510. obs_property_list_insert_string(p, 0, name, name);
  511. obs_property_list_item_disable(p, 0, true);
  512. p = obs_properties_get(props, "server");
  513. obs_property_list_insert_string(p, 0, server, server);
  514. obs_property_list_item_disable(p, 0, true);
  515. return true;
  516. }
  517. if (new_name) {
  518. name = new_name;
  519. obs_data_set_string(settings, "service", name);
  520. }
  521. fill_servers(obs_properties_get(props, "server"), service, name);
  522. copy_info_to_settings(service, settings);
  523. return true;
  524. }
  525. static bool show_all_services_toggled(obs_properties_t *ppts, obs_property_t *p,
  526. obs_data_t *settings)
  527. {
  528. const char *cur_service = obs_data_get_string(settings, "service");
  529. bool show_all = obs_data_get_bool(settings, "show_all");
  530. json_t *root = obs_properties_get_param(ppts);
  531. if (!root)
  532. return false;
  533. build_service_list(obs_properties_get(ppts, "service"), root, show_all,
  534. cur_service);
  535. UNUSED_PARAMETER(p);
  536. return true;
  537. }
  538. static obs_properties_t *rtmp_common_properties(void *unused)
  539. {
  540. UNUSED_PARAMETER(unused);
  541. obs_properties_t *ppts = obs_properties_create();
  542. obs_property_t *p;
  543. json_t *root;
  544. root = open_services_file();
  545. if (root)
  546. obs_properties_set_param(ppts, root, properties_data_destroy);
  547. p = obs_properties_add_list(ppts, "service", obs_module_text("Service"),
  548. OBS_COMBO_TYPE_LIST,
  549. OBS_COMBO_FORMAT_STRING);
  550. obs_property_set_modified_callback(p, service_selected);
  551. p = obs_properties_add_bool(ppts, "show_all",
  552. obs_module_text("ShowAll"));
  553. obs_property_set_modified_callback(p, show_all_services_toggled);
  554. obs_properties_add_list(ppts, "server", obs_module_text("Server"),
  555. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  556. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  557. OBS_TEXT_PASSWORD);
  558. return ppts;
  559. }
  560. static int get_bitrate_matrix_max(json_t *array);
  561. static void apply_video_encoder_settings(obs_data_t *settings,
  562. json_t *recommended)
  563. {
  564. json_t *item = json_object_get(recommended, "keyint");
  565. if (json_is_integer(item)) {
  566. int keyint = (int)json_integer_value(item);
  567. obs_data_set_int(settings, "keyint_sec", keyint);
  568. }
  569. obs_data_set_string(settings, "rate_control", "CBR");
  570. item = json_object_get(recommended, "profile");
  571. obs_data_item_t *enc_item = obs_data_item_byname(settings, "profile");
  572. if (json_is_string(item) &&
  573. obs_data_item_gettype(enc_item) == OBS_DATA_STRING) {
  574. const char *profile = json_string_value(item);
  575. obs_data_set_string(settings, "profile", profile);
  576. }
  577. obs_data_item_release(&enc_item);
  578. int max_bitrate = 0;
  579. item = json_object_get(recommended, "bitrate matrix");
  580. if (json_is_array(item)) {
  581. max_bitrate = get_bitrate_matrix_max(item);
  582. }
  583. item = json_object_get(recommended, "max video bitrate");
  584. if (!max_bitrate && json_is_integer(item)) {
  585. max_bitrate = (int)json_integer_value(item);
  586. }
  587. if (max_bitrate &&
  588. obs_data_get_int(settings, "bitrate") > max_bitrate) {
  589. obs_data_set_int(settings, "bitrate", max_bitrate);
  590. obs_data_set_int(settings, "buffer_size", max_bitrate);
  591. }
  592. item = json_object_get(recommended, "bframes");
  593. if (json_is_integer(item)) {
  594. int bframes = (int)json_integer_value(item);
  595. obs_data_set_int(settings, "bf", bframes);
  596. }
  597. item = json_object_get(recommended, "x264opts");
  598. if (json_is_string(item)) {
  599. const char *x264_settings = json_string_value(item);
  600. const char *cur_settings =
  601. obs_data_get_string(settings, "x264opts");
  602. struct dstr opts;
  603. dstr_init_copy(&opts, cur_settings);
  604. if (!dstr_is_empty(&opts))
  605. dstr_cat(&opts, " ");
  606. dstr_cat(&opts, x264_settings);
  607. obs_data_set_string(settings, "x264opts", opts.array);
  608. dstr_free(&opts);
  609. }
  610. }
  611. static void apply_audio_encoder_settings(obs_data_t *settings,
  612. json_t *recommended)
  613. {
  614. json_t *item = json_object_get(recommended, "max audio bitrate");
  615. if (json_is_integer(item)) {
  616. int max_bitrate = (int)json_integer_value(item);
  617. if (obs_data_get_int(settings, "bitrate") > max_bitrate)
  618. obs_data_set_int(settings, "bitrate", max_bitrate);
  619. }
  620. }
  621. static void initialize_output(struct rtmp_common *service, json_t *root,
  622. obs_data_t *video_settings,
  623. obs_data_t *audio_settings)
  624. {
  625. json_t *json_service = find_service(root, service->service, NULL);
  626. json_t *recommended;
  627. if (!json_service) {
  628. if (service->service && *service->service)
  629. blog(LOG_WARNING,
  630. "rtmp-common.c: [initialize_output] "
  631. "Could not find service '%s'",
  632. service->service);
  633. return;
  634. }
  635. recommended = json_object_get(json_service, "recommended");
  636. if (!recommended)
  637. return;
  638. if (video_settings)
  639. apply_video_encoder_settings(video_settings, recommended);
  640. if (audio_settings)
  641. apply_audio_encoder_settings(audio_settings, recommended);
  642. }
  643. static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings,
  644. obs_data_t *audio_settings)
  645. {
  646. struct rtmp_common *service = data;
  647. json_t *root = open_services_file();
  648. if (root) {
  649. initialize_output(service, root, video_settings,
  650. audio_settings);
  651. json_decref(root);
  652. }
  653. }
  654. static const char *rtmp_common_url(void *data)
  655. {
  656. struct rtmp_common *service = data;
  657. if (service->service && strcmp(service->service, "Twitch") == 0) {
  658. if (service->server && strcmp(service->server, "auto") == 0) {
  659. struct twitch_ingest ing;
  660. twitch_ingests_refresh(3);
  661. twitch_ingests_lock();
  662. ing = twitch_ingest(0);
  663. twitch_ingests_unlock();
  664. return ing.url;
  665. }
  666. }
  667. if (service->service && strcmp(service->service, "Nimo TV") == 0) {
  668. if (service->server && strcmp(service->server, "auto") == 0) {
  669. return nimotv_get_ingest(service->key);
  670. }
  671. }
  672. if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
  673. if (service->server && service->key) {
  674. struct showroom_ingest *ingest;
  675. ingest = showroom_get_ingest(service->server,
  676. service->key);
  677. return ingest->url;
  678. }
  679. }
  680. if (service->service && strcmp(service->service, "Dacast") == 0) {
  681. if (service->server && service->key) {
  682. dacast_ingests_load_data(service->server, service->key);
  683. struct dacast_ingest *ingest;
  684. ingest = dacast_ingest(service->key);
  685. return ingest->url;
  686. }
  687. }
  688. return service->server;
  689. }
  690. static const char *rtmp_common_key(void *data)
  691. {
  692. struct rtmp_common *service = data;
  693. if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
  694. if (service->server && service->key) {
  695. struct showroom_ingest *ingest;
  696. ingest = showroom_get_ingest(service->server,
  697. service->key);
  698. return ingest->key;
  699. }
  700. }
  701. if (service->service && strcmp(service->service, "Dacast") == 0) {
  702. if (service->key) {
  703. struct dacast_ingest *ingest;
  704. ingest = dacast_ingest(service->key);
  705. return ingest->streamkey;
  706. }
  707. }
  708. return service->key;
  709. }
  710. static void rtmp_common_get_supported_resolutions(
  711. void *data, struct obs_service_resolution **resolutions, size_t *count)
  712. {
  713. struct rtmp_common *service = data;
  714. if (service->supported_resolutions_count) {
  715. *count = service->supported_resolutions_count;
  716. *resolutions =
  717. bmemdup(service->supported_resolutions,
  718. *count * sizeof(struct obs_service_resolution));
  719. } else {
  720. *count = 0;
  721. *resolutions = NULL;
  722. }
  723. }
  724. static void rtmp_common_get_max_fps(void *data, int *fps)
  725. {
  726. struct rtmp_common *service = data;
  727. *fps = service->max_fps;
  728. }
  729. static int get_bitrate_matrix_max(json_t *array)
  730. {
  731. size_t index;
  732. json_t *item;
  733. struct obs_video_info ovi;
  734. if (!obs_get_video_info(&ovi))
  735. return 0;
  736. double cur_fps = (double)ovi.fps_num / (double)ovi.fps_den;
  737. json_array_foreach (array, index, item) {
  738. if (!json_is_object(item))
  739. continue;
  740. const char *res = get_string_val(item, "res");
  741. double fps = (double)get_int_val(item, "fps") + 0.0000001;
  742. int bitrate = get_int_val(item, "max bitrate");
  743. if (!res)
  744. continue;
  745. int cx, cy;
  746. int c = sscanf(res, "%dx%d", &cx, &cy);
  747. if (c != 2)
  748. continue;
  749. if ((int)ovi.output_width == cx &&
  750. (int)ovi.output_height == cy && cur_fps <= fps)
  751. return bitrate;
  752. }
  753. return 0;
  754. }
  755. static void rtmp_common_get_max_bitrate(void *data, int *video_bitrate,
  756. int *audio_bitrate)
  757. {
  758. struct rtmp_common *service = data;
  759. json_t *root = open_services_file();
  760. json_t *item;
  761. if (!root)
  762. return;
  763. json_t *json_service = find_service(root, service->service, NULL);
  764. if (!json_service) {
  765. goto fail;
  766. }
  767. json_t *recommended = json_object_get(json_service, "recommended");
  768. if (!recommended) {
  769. goto fail;
  770. }
  771. if (audio_bitrate) {
  772. item = json_object_get(recommended, "max audio bitrate");
  773. if (json_is_integer(item))
  774. *audio_bitrate = (int)json_integer_value(item);
  775. }
  776. if (video_bitrate) {
  777. int bitrate = 0;
  778. item = json_object_get(recommended, "bitrate matrix");
  779. if (json_is_array(item)) {
  780. bitrate = get_bitrate_matrix_max(item);
  781. }
  782. if (!bitrate) {
  783. item = json_object_get(recommended,
  784. "max video bitrate");
  785. if (json_is_integer(item))
  786. bitrate = (int)json_integer_value(item);
  787. }
  788. *video_bitrate = bitrate;
  789. }
  790. fail:
  791. json_decref(root);
  792. }
  793. static const char **rtmp_common_get_supported_video_codecs(void *data)
  794. {
  795. struct rtmp_common *service = data;
  796. if (service->video_codecs)
  797. return (const char **)service->video_codecs;
  798. struct dstr codecs = {0};
  799. json_t *root = open_services_file();
  800. if (!root)
  801. return NULL;
  802. json_t *json_service = find_service(root, service->service, NULL);
  803. if (!json_service) {
  804. goto fail;
  805. }
  806. json_t *json_video_codecs =
  807. json_object_get(json_service, "supported video codecs");
  808. if (!json_is_array(json_video_codecs)) {
  809. goto fail;
  810. }
  811. size_t index;
  812. json_t *item;
  813. json_array_foreach (json_video_codecs, index, item) {
  814. char codec[16];
  815. snprintf(codec, sizeof(codec), "%s", json_string_value(item));
  816. if (codecs.len)
  817. dstr_cat(&codecs, ";");
  818. dstr_cat(&codecs, codec);
  819. }
  820. service->video_codecs = strlist_split(codecs.array, ';', false);
  821. dstr_free(&codecs);
  822. fail:
  823. json_decref(root);
  824. return (const char **)service->video_codecs;
  825. }
  826. static const char **rtmp_common_get_supported_audio_codecs(void *data)
  827. {
  828. struct rtmp_common *service = data;
  829. if (service->audio_codecs)
  830. return (const char **)service->audio_codecs;
  831. struct dstr codecs = {0};
  832. json_t *root = open_services_file();
  833. if (!root)
  834. return NULL;
  835. json_t *json_service = find_service(root, service->service, NULL);
  836. if (!json_service) {
  837. goto fail;
  838. }
  839. json_t *json_audio_codecs =
  840. json_object_get(json_service, "supported audio codecs");
  841. if (!json_is_array(json_audio_codecs)) {
  842. goto fail;
  843. }
  844. size_t index;
  845. json_t *item;
  846. json_array_foreach (json_audio_codecs, index, item) {
  847. char codec[16];
  848. snprintf(codec, sizeof(codec), "%s", json_string_value(item));
  849. if (codecs.len)
  850. dstr_cat(&codecs, ";");
  851. dstr_cat(&codecs, codec);
  852. }
  853. service->audio_codecs = strlist_split(codecs.array, ';', false);
  854. dstr_free(&codecs);
  855. fail:
  856. json_decref(root);
  857. return (const char **)service->audio_codecs;
  858. }
  859. static const char *rtmp_common_username(void *data)
  860. {
  861. struct rtmp_common *service = data;
  862. if (service->service && strcmp(service->service, "Dacast") == 0) {
  863. if (service->key) {
  864. struct dacast_ingest *ingest;
  865. ingest = dacast_ingest(service->key);
  866. return ingest->username;
  867. }
  868. }
  869. return NULL;
  870. }
  871. static const char *rtmp_common_password(void *data)
  872. {
  873. struct rtmp_common *service = data;
  874. if (service->service && strcmp(service->service, "Dacast") == 0) {
  875. if (service->key) {
  876. struct dacast_ingest *ingest;
  877. ingest = dacast_ingest(service->key);
  878. return ingest->password;
  879. }
  880. }
  881. return NULL;
  882. }
  883. static const char *rtmp_common_get_protocol(void *data)
  884. {
  885. struct rtmp_common *service = data;
  886. return service->protocol ? service->protocol : "RTMP";
  887. }
  888. static const char *rtmp_common_get_connect_info(void *data, uint32_t type)
  889. {
  890. switch ((enum obs_service_connect_info)type) {
  891. case OBS_SERVICE_CONNECT_INFO_SERVER_URL:
  892. return rtmp_common_url(data);
  893. case OBS_SERVICE_CONNECT_INFO_STREAM_ID:
  894. return rtmp_common_key(data);
  895. case OBS_SERVICE_CONNECT_INFO_USERNAME:
  896. return rtmp_common_username(data);
  897. case OBS_SERVICE_CONNECT_INFO_PASSWORD:
  898. return rtmp_common_password(data);
  899. case OBS_SERVICE_CONNECT_INFO_ENCRYPT_PASSPHRASE: {
  900. const char *protocol = rtmp_common_get_protocol(data);
  901. if ((strcmp(protocol, "SRT") == 0))
  902. return rtmp_common_password(data);
  903. else if ((strcmp(protocol, "RIST") == 0))
  904. return rtmp_common_key(data);
  905. break;
  906. }
  907. case OBS_SERVICE_CONNECT_INFO_BEARER_TOKEN:
  908. return NULL;
  909. }
  910. return NULL;
  911. }
  912. static bool rtmp_common_can_try_to_connect(void *data)
  913. {
  914. struct rtmp_common *service = data;
  915. const char *key = rtmp_common_key(data);
  916. if (service->service && strcmp(service->service, "Dacast") == 0)
  917. return (key != NULL && key[0] != '\0');
  918. const char *url = rtmp_common_url(data);
  919. return (url != NULL && url[0] != '\0') &&
  920. (key != NULL && key[0] != '\0');
  921. }
  922. struct obs_service_info rtmp_common_service = {
  923. .id = "rtmp_common",
  924. .get_name = rtmp_common_getname,
  925. .create = rtmp_common_create,
  926. .destroy = rtmp_common_destroy,
  927. .update = rtmp_common_update,
  928. .get_properties = rtmp_common_properties,
  929. .get_protocol = rtmp_common_get_protocol,
  930. .get_url = rtmp_common_url,
  931. .get_key = rtmp_common_key,
  932. .get_username = rtmp_common_username,
  933. .get_password = rtmp_common_password,
  934. .get_connect_info = rtmp_common_get_connect_info,
  935. .apply_encoder_settings = rtmp_common_apply_settings,
  936. .get_supported_resolutions = rtmp_common_get_supported_resolutions,
  937. .get_max_fps = rtmp_common_get_max_fps,
  938. .get_max_bitrate = rtmp_common_get_max_bitrate,
  939. .get_supported_video_codecs = rtmp_common_get_supported_video_codecs,
  940. .get_supported_audio_codecs = rtmp_common_get_supported_audio_codecs,
  941. .can_try_to_connect = rtmp_common_can_try_to_connect,
  942. };