rtmp-common.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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/younow.h"
  9. #include "service-specific/nimotv.h"
  10. #include "service-specific/showroom.h"
  11. #include "service-specific/dacast.h"
  12. struct rtmp_common {
  13. char *service;
  14. char *server;
  15. char *key;
  16. char *output;
  17. struct obs_service_resolution *supported_resolutions;
  18. size_t supported_resolutions_count;
  19. int max_fps;
  20. char **video_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. const char *out = get_string_val(rec, "output");
  65. if (out)
  66. service->output = bstrdup(out);
  67. json_t *sr = json_object_get(rec, "supported resolutions");
  68. if (sr && json_is_array(sr)) {
  69. DARRAY(struct obs_service_resolution) res_list;
  70. json_t *res_obj;
  71. size_t index;
  72. da_init(res_list);
  73. json_array_foreach (sr, index, res_obj) {
  74. if (!json_is_string(res_obj))
  75. continue;
  76. const char *res_str = json_string_value(res_obj);
  77. struct obs_service_resolution res;
  78. if (sscanf(res_str, "%dx%d", &res.cx, &res.cy) != 2)
  79. continue;
  80. if (res.cx <= 0 || res.cy <= 0)
  81. continue;
  82. da_push_back(res_list, &res);
  83. }
  84. if (res_list.num) {
  85. service->supported_resolutions = res_list.array;
  86. service->supported_resolutions_count = res_list.num;
  87. }
  88. }
  89. service->max_fps = get_int_val(rec, "max fps");
  90. }
  91. static void rtmp_common_update(void *data, obs_data_t *settings)
  92. {
  93. struct rtmp_common *service = data;
  94. bfree(service->supported_resolutions);
  95. bfree(service->video_codecs);
  96. bfree(service->service);
  97. bfree(service->server);
  98. bfree(service->output);
  99. bfree(service->key);
  100. service->service = bstrdup(obs_data_get_string(settings, "service"));
  101. service->server = bstrdup(obs_data_get_string(settings, "server"));
  102. service->key = bstrdup(obs_data_get_string(settings, "key"));
  103. service->supports_additional_audio_track = false;
  104. service->output = NULL;
  105. service->video_codecs = NULL;
  106. service->supported_resolutions = NULL;
  107. service->supported_resolutions_count = 0;
  108. service->max_fps = 0;
  109. json_t *root = open_services_file();
  110. if (root) {
  111. const char *new_name;
  112. json_t *serv = find_service(root, service->service, &new_name);
  113. if (new_name) {
  114. bfree(service->service);
  115. service->service = bstrdup(new_name);
  116. }
  117. if (serv) {
  118. json_t *rec = json_object_get(serv, "recommended");
  119. if (json_is_object(rec)) {
  120. update_recommendations(service, rec);
  121. }
  122. service->supports_additional_audio_track = get_bool_val(
  123. serv, "supports_additional_audio_track");
  124. ensure_valid_url(service, serv, settings);
  125. }
  126. }
  127. json_decref(root);
  128. if (!service->output)
  129. service->output = bstrdup("rtmp_output");
  130. }
  131. static void rtmp_common_destroy(void *data)
  132. {
  133. struct rtmp_common *service = data;
  134. bfree(service->supported_resolutions);
  135. bfree(service->video_codecs);
  136. bfree(service->service);
  137. bfree(service->server);
  138. bfree(service->output);
  139. bfree(service->key);
  140. bfree(service);
  141. }
  142. static void *rtmp_common_create(obs_data_t *settings, obs_service_t *service)
  143. {
  144. struct rtmp_common *data = bzalloc(sizeof(struct rtmp_common));
  145. rtmp_common_update(data, settings);
  146. UNUSED_PARAMETER(service);
  147. return data;
  148. }
  149. static inline const char *get_string_val(json_t *service, const char *key)
  150. {
  151. json_t *str_val = json_object_get(service, key);
  152. if (!str_val || !json_is_string(str_val))
  153. return NULL;
  154. return json_string_value(str_val);
  155. }
  156. static inline int get_int_val(json_t *service, const char *key)
  157. {
  158. json_t *integer_val = json_object_get(service, key);
  159. if (!integer_val || !json_is_integer(integer_val))
  160. return 0;
  161. return (int)json_integer_value(integer_val);
  162. }
  163. static inline bool get_bool_val(json_t *service, const char *key)
  164. {
  165. json_t *bool_val = json_object_get(service, key);
  166. if (!bool_val || !json_is_boolean(bool_val))
  167. return false;
  168. return json_is_true(bool_val);
  169. }
  170. static void add_service(obs_property_t *list, json_t *service, bool show_all,
  171. const char *cur_service)
  172. {
  173. json_t *servers;
  174. const char *name;
  175. bool common;
  176. if (!json_is_object(service)) {
  177. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  178. "is not an object");
  179. return;
  180. }
  181. name = get_string_val(service, "name");
  182. if (!name) {
  183. blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
  184. "has no name");
  185. return;
  186. }
  187. common = get_bool_val(service, "common");
  188. if (!show_all && !common && strcmp(cur_service, name) != 0) {
  189. return;
  190. }
  191. servers = json_object_get(service, "servers");
  192. if (!servers || !json_is_array(servers)) {
  193. blog(LOG_WARNING,
  194. "rtmp-common.c: [add_service] service "
  195. "'%s' has no servers",
  196. name);
  197. return;
  198. }
  199. obs_property_list_add_string(list, name, name);
  200. }
  201. static void add_services(obs_property_t *list, json_t *root, bool show_all,
  202. const char *cur_service)
  203. {
  204. json_t *service;
  205. size_t index;
  206. if (!json_is_array(root)) {
  207. blog(LOG_WARNING, "rtmp-common.c: [add_services] JSON file "
  208. "root is not an array");
  209. return;
  210. }
  211. json_array_foreach (root, index, service) {
  212. add_service(list, service, show_all, cur_service);
  213. }
  214. service = find_service(root, cur_service, NULL);
  215. if (!service && cur_service && *cur_service) {
  216. obs_property_list_insert_string(list, 0, cur_service,
  217. cur_service);
  218. obs_property_list_item_disable(list, 0, true);
  219. }
  220. }
  221. static json_t *open_json_file(const char *file)
  222. {
  223. char *file_data = os_quick_read_utf8_file(file);
  224. json_error_t error;
  225. json_t *root;
  226. json_t *list;
  227. int format_ver;
  228. if (!file_data)
  229. return NULL;
  230. root = json_loads(file_data, JSON_REJECT_DUPLICATES, &error);
  231. bfree(file_data);
  232. if (!root) {
  233. blog(LOG_WARNING,
  234. "rtmp-common.c: [open_json_file] "
  235. "Error reading JSON file (%d): %s",
  236. error.line, error.text);
  237. return NULL;
  238. }
  239. format_ver = get_int_val(root, "format_version");
  240. if (format_ver != RTMP_SERVICES_FORMAT_VERSION) {
  241. blog(LOG_DEBUG,
  242. "rtmp-common.c: [open_json_file] "
  243. "Wrong format version (%d), expected %d",
  244. format_ver, RTMP_SERVICES_FORMAT_VERSION);
  245. json_decref(root);
  246. return NULL;
  247. }
  248. list = json_object_get(root, "services");
  249. if (list)
  250. json_incref(list);
  251. json_decref(root);
  252. if (!list) {
  253. blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
  254. "No services list");
  255. return NULL;
  256. }
  257. return list;
  258. }
  259. static json_t *open_services_file(void)
  260. {
  261. char *file;
  262. json_t *root = NULL;
  263. file = obs_module_config_path("services.json");
  264. if (file) {
  265. root = open_json_file(file);
  266. bfree(file);
  267. }
  268. if (!root) {
  269. file = obs_module_file("services.json");
  270. if (file) {
  271. root = open_json_file(file);
  272. bfree(file);
  273. }
  274. }
  275. return root;
  276. }
  277. static void build_service_list(obs_property_t *list, json_t *root,
  278. bool show_all, const char *cur_service)
  279. {
  280. obs_property_list_clear(list);
  281. add_services(list, root, show_all, cur_service);
  282. }
  283. static void properties_data_destroy(void *data)
  284. {
  285. json_t *root = data;
  286. if (root)
  287. json_decref(root);
  288. }
  289. static bool fill_twitch_servers_locked(obs_property_t *servers_prop)
  290. {
  291. size_t count = twitch_ingest_count();
  292. obs_property_list_add_string(servers_prop,
  293. obs_module_text("Server.Auto"), "auto");
  294. if (count <= 1)
  295. return false;
  296. for (size_t i = 0; i < count; i++) {
  297. struct twitch_ingest ing = twitch_ingest(i);
  298. obs_property_list_add_string(servers_prop, ing.name, ing.url);
  299. }
  300. return true;
  301. }
  302. static inline bool fill_twitch_servers(obs_property_t *servers_prop)
  303. {
  304. bool success;
  305. twitch_ingests_lock();
  306. success = fill_twitch_servers_locked(servers_prop);
  307. twitch_ingests_unlock();
  308. return success;
  309. }
  310. static void fill_servers(obs_property_t *servers_prop, json_t *service,
  311. const char *name)
  312. {
  313. json_t *servers, *server;
  314. size_t index;
  315. obs_property_list_clear(servers_prop);
  316. servers = json_object_get(service, "servers");
  317. if (!json_is_array(servers)) {
  318. blog(LOG_WARNING,
  319. "rtmp-common.c: [fill_servers] "
  320. "Servers for service '%s' not a valid object",
  321. name);
  322. return;
  323. }
  324. if (strcmp(name, "Twitch") == 0) {
  325. if (fill_twitch_servers(servers_prop))
  326. return;
  327. }
  328. if (strcmp(name, "Nimo TV") == 0) {
  329. obs_property_list_add_string(
  330. servers_prop, obs_module_text("Server.Auto"), "auto");
  331. }
  332. json_array_foreach (servers, index, server) {
  333. const char *server_name = get_string_val(server, "name");
  334. const char *url = get_string_val(server, "url");
  335. if (!server_name || !url)
  336. continue;
  337. obs_property_list_add_string(servers_prop, server_name, url);
  338. }
  339. }
  340. static void fill_more_info_link(json_t *service, obs_data_t *settings)
  341. {
  342. const char *more_info_link;
  343. more_info_link = get_string_val(service, "more_info_link");
  344. if (more_info_link)
  345. obs_data_set_string(settings, "more_info_link", more_info_link);
  346. }
  347. static void fill_stream_key_link(json_t *service, obs_data_t *settings)
  348. {
  349. const char *stream_key_link;
  350. stream_key_link = get_string_val(service, "stream_key_link");
  351. if (stream_key_link)
  352. obs_data_set_string(settings, "stream_key_link",
  353. stream_key_link);
  354. }
  355. static inline json_t *find_service(json_t *root, const char *name,
  356. const char **p_new_name)
  357. {
  358. size_t index;
  359. json_t *service;
  360. if (p_new_name)
  361. *p_new_name = NULL;
  362. json_array_foreach (root, index, service) {
  363. const char *cur_name = get_string_val(service, "name");
  364. if (strcmp(name, cur_name) == 0)
  365. return service;
  366. /* check for alternate names */
  367. json_t *alt_names = json_object_get(service, "alt_names");
  368. size_t alt_name_idx;
  369. json_t *alt_name_obj;
  370. json_array_foreach (alt_names, alt_name_idx, alt_name_obj) {
  371. const char *alt_name = json_string_value(alt_name_obj);
  372. if (alt_name && strcmp(name, alt_name) == 0) {
  373. if (p_new_name)
  374. *p_new_name = cur_name;
  375. return service;
  376. }
  377. }
  378. }
  379. return NULL;
  380. }
  381. static bool service_selected(obs_properties_t *props, obs_property_t *p,
  382. obs_data_t *settings)
  383. {
  384. const char *name = obs_data_get_string(settings, "service");
  385. json_t *root = obs_properties_get_param(props);
  386. json_t *service;
  387. const char *new_name;
  388. if (!name || !*name)
  389. return false;
  390. service = find_service(root, name, &new_name);
  391. if (!service) {
  392. const char *server = obs_data_get_string(settings, "server");
  393. obs_property_list_insert_string(p, 0, name, name);
  394. obs_property_list_item_disable(p, 0, true);
  395. p = obs_properties_get(props, "server");
  396. obs_property_list_insert_string(p, 0, server, server);
  397. obs_property_list_item_disable(p, 0, true);
  398. return true;
  399. }
  400. if (new_name) {
  401. name = new_name;
  402. obs_data_set_string(settings, "service", name);
  403. }
  404. fill_servers(obs_properties_get(props, "server"), service, name);
  405. fill_more_info_link(service, settings);
  406. fill_stream_key_link(service, settings);
  407. return true;
  408. }
  409. static bool show_all_services_toggled(obs_properties_t *ppts, obs_property_t *p,
  410. obs_data_t *settings)
  411. {
  412. const char *cur_service = obs_data_get_string(settings, "service");
  413. bool show_all = obs_data_get_bool(settings, "show_all");
  414. json_t *root = obs_properties_get_param(ppts);
  415. if (!root)
  416. return false;
  417. build_service_list(obs_properties_get(ppts, "service"), root, show_all,
  418. cur_service);
  419. UNUSED_PARAMETER(p);
  420. return true;
  421. }
  422. static obs_properties_t *rtmp_common_properties(void *unused)
  423. {
  424. UNUSED_PARAMETER(unused);
  425. obs_properties_t *ppts = obs_properties_create();
  426. obs_property_t *p;
  427. json_t *root;
  428. root = open_services_file();
  429. if (root)
  430. obs_properties_set_param(ppts, root, properties_data_destroy);
  431. p = obs_properties_add_list(ppts, "service", obs_module_text("Service"),
  432. OBS_COMBO_TYPE_LIST,
  433. OBS_COMBO_FORMAT_STRING);
  434. obs_property_set_modified_callback(p, service_selected);
  435. p = obs_properties_add_bool(ppts, "show_all",
  436. obs_module_text("ShowAll"));
  437. obs_property_set_modified_callback(p, show_all_services_toggled);
  438. obs_properties_add_list(ppts, "server", obs_module_text("Server"),
  439. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  440. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  441. OBS_TEXT_PASSWORD);
  442. return ppts;
  443. }
  444. static int get_bitrate_matrix_max(json_t *array);
  445. static void apply_video_encoder_settings(obs_data_t *settings,
  446. json_t *recommended)
  447. {
  448. json_t *item = json_object_get(recommended, "keyint");
  449. if (json_is_integer(item)) {
  450. int keyint = (int)json_integer_value(item);
  451. obs_data_set_int(settings, "keyint_sec", keyint);
  452. }
  453. obs_data_set_string(settings, "rate_control", "CBR");
  454. item = json_object_get(recommended, "profile");
  455. obs_data_item_t *enc_item = obs_data_item_byname(settings, "profile");
  456. if (json_is_string(item) &&
  457. obs_data_item_gettype(enc_item) == OBS_DATA_STRING) {
  458. const char *profile = json_string_value(item);
  459. obs_data_set_string(settings, "profile", profile);
  460. }
  461. obs_data_item_release(&enc_item);
  462. int max_bitrate = 0;
  463. item = json_object_get(recommended, "bitrate matrix");
  464. if (json_is_array(item)) {
  465. max_bitrate = get_bitrate_matrix_max(item);
  466. }
  467. item = json_object_get(recommended, "max video bitrate");
  468. if (!max_bitrate && json_is_integer(item)) {
  469. max_bitrate = (int)json_integer_value(item);
  470. }
  471. if (max_bitrate &&
  472. obs_data_get_int(settings, "bitrate") > max_bitrate) {
  473. obs_data_set_int(settings, "bitrate", max_bitrate);
  474. obs_data_set_int(settings, "buffer_size", max_bitrate);
  475. }
  476. item = json_object_get(recommended, "bframes");
  477. if (json_is_integer(item)) {
  478. int bframes = (int)json_integer_value(item);
  479. obs_data_set_int(settings, "bf", bframes);
  480. }
  481. item = json_object_get(recommended, "x264opts");
  482. if (json_is_string(item)) {
  483. const char *x264_settings = json_string_value(item);
  484. const char *cur_settings =
  485. obs_data_get_string(settings, "x264opts");
  486. struct dstr opts;
  487. dstr_init_copy(&opts, cur_settings);
  488. if (!dstr_is_empty(&opts))
  489. dstr_cat(&opts, " ");
  490. dstr_cat(&opts, x264_settings);
  491. obs_data_set_string(settings, "x264opts", opts.array);
  492. dstr_free(&opts);
  493. }
  494. }
  495. static void apply_audio_encoder_settings(obs_data_t *settings,
  496. json_t *recommended)
  497. {
  498. json_t *item = json_object_get(recommended, "max audio bitrate");
  499. if (json_is_integer(item)) {
  500. int max_bitrate = (int)json_integer_value(item);
  501. if (obs_data_get_int(settings, "bitrate") > max_bitrate)
  502. obs_data_set_int(settings, "bitrate", max_bitrate);
  503. }
  504. }
  505. static void initialize_output(struct rtmp_common *service, json_t *root,
  506. obs_data_t *video_settings,
  507. obs_data_t *audio_settings)
  508. {
  509. json_t *json_service = find_service(root, service->service, NULL);
  510. json_t *recommended;
  511. if (!json_service) {
  512. if (service->service && *service->service)
  513. blog(LOG_WARNING,
  514. "rtmp-common.c: [initialize_output] "
  515. "Could not find service '%s'",
  516. service->service);
  517. return;
  518. }
  519. recommended = json_object_get(json_service, "recommended");
  520. if (!recommended)
  521. return;
  522. if (video_settings)
  523. apply_video_encoder_settings(video_settings, recommended);
  524. if (audio_settings)
  525. apply_audio_encoder_settings(audio_settings, recommended);
  526. }
  527. static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings,
  528. obs_data_t *audio_settings)
  529. {
  530. struct rtmp_common *service = data;
  531. json_t *root = open_services_file();
  532. if (root) {
  533. initialize_output(service, root, video_settings,
  534. audio_settings);
  535. json_decref(root);
  536. }
  537. }
  538. static const char *rtmp_common_get_output_type(void *data)
  539. {
  540. struct rtmp_common *service = data;
  541. return service->output;
  542. }
  543. static const char *rtmp_common_url(void *data)
  544. {
  545. struct rtmp_common *service = data;
  546. if (service->service && strcmp(service->service, "Twitch") == 0) {
  547. if (service->server && strcmp(service->server, "auto") == 0) {
  548. struct twitch_ingest ing;
  549. twitch_ingests_refresh(3);
  550. twitch_ingests_lock();
  551. ing = twitch_ingest(0);
  552. twitch_ingests_unlock();
  553. return ing.url;
  554. }
  555. }
  556. if (service->service && strcmp(service->service, "YouNow") == 0) {
  557. if (service->server && service->key) {
  558. return younow_get_ingest(service->server, service->key);
  559. }
  560. }
  561. if (service->service && strcmp(service->service, "Nimo TV") == 0) {
  562. if (service->server && strcmp(service->server, "auto") == 0) {
  563. return nimotv_get_ingest(service->key);
  564. }
  565. }
  566. if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
  567. if (service->server && service->key) {
  568. struct showroom_ingest *ingest;
  569. ingest = showroom_get_ingest(service->server,
  570. service->key);
  571. return ingest->url;
  572. }
  573. }
  574. if (service->service && strcmp(service->service, "Dacast") == 0) {
  575. if (service->server && service->key) {
  576. dacast_ingests_load_data(service->server, service->key);
  577. struct dacast_ingest *ingest;
  578. ingest = dacast_ingest(service->key);
  579. return ingest->url;
  580. }
  581. }
  582. return service->server;
  583. }
  584. static const char *rtmp_common_key(void *data)
  585. {
  586. struct rtmp_common *service = data;
  587. if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
  588. if (service->server && service->key) {
  589. struct showroom_ingest *ingest;
  590. ingest = showroom_get_ingest(service->server,
  591. service->key);
  592. return ingest->key;
  593. }
  594. }
  595. if (service->service && strcmp(service->service, "Dacast") == 0) {
  596. if (service->key) {
  597. struct dacast_ingest *ingest;
  598. ingest = dacast_ingest(service->key);
  599. return ingest->streamkey;
  600. }
  601. }
  602. return service->key;
  603. }
  604. static bool supports_multitrack(void *data)
  605. {
  606. struct rtmp_common *service = data;
  607. return service->supports_additional_audio_track;
  608. }
  609. static void rtmp_common_get_supported_resolutions(
  610. void *data, struct obs_service_resolution **resolutions, size_t *count)
  611. {
  612. struct rtmp_common *service = data;
  613. if (service->supported_resolutions_count) {
  614. *count = service->supported_resolutions_count;
  615. *resolutions =
  616. bmemdup(service->supported_resolutions,
  617. *count * sizeof(struct obs_service_resolution));
  618. } else {
  619. *count = 0;
  620. *resolutions = NULL;
  621. }
  622. }
  623. static void rtmp_common_get_max_fps(void *data, int *fps)
  624. {
  625. struct rtmp_common *service = data;
  626. *fps = service->max_fps;
  627. }
  628. static int get_bitrate_matrix_max(json_t *array)
  629. {
  630. size_t index;
  631. json_t *item;
  632. struct obs_video_info ovi;
  633. if (!obs_get_video_info(&ovi))
  634. return 0;
  635. double cur_fps = (double)ovi.fps_num / (double)ovi.fps_den;
  636. json_array_foreach (array, index, item) {
  637. if (!json_is_object(item))
  638. continue;
  639. const char *res = get_string_val(item, "res");
  640. double fps = (double)get_int_val(item, "fps") + 0.0000001;
  641. int bitrate = get_int_val(item, "max bitrate");
  642. if (!res)
  643. continue;
  644. int cx, cy;
  645. int c = sscanf(res, "%dx%d", &cx, &cy);
  646. if (c != 2)
  647. continue;
  648. if ((int)ovi.output_width == cx &&
  649. (int)ovi.output_height == cy && cur_fps <= fps)
  650. return bitrate;
  651. }
  652. return 0;
  653. }
  654. static void rtmp_common_get_max_bitrate(void *data, int *video_bitrate,
  655. int *audio_bitrate)
  656. {
  657. struct rtmp_common *service = data;
  658. json_t *root = open_services_file();
  659. json_t *item;
  660. if (!root)
  661. return;
  662. json_t *json_service = find_service(root, service->service, NULL);
  663. if (!json_service) {
  664. goto fail;
  665. }
  666. json_t *recommended = json_object_get(json_service, "recommended");
  667. if (!recommended) {
  668. goto fail;
  669. }
  670. if (audio_bitrate) {
  671. item = json_object_get(recommended, "max audio bitrate");
  672. if (json_is_integer(item))
  673. *audio_bitrate = (int)json_integer_value(item);
  674. }
  675. if (video_bitrate) {
  676. int bitrate = 0;
  677. item = json_object_get(recommended, "bitrate matrix");
  678. if (json_is_array(item)) {
  679. bitrate = get_bitrate_matrix_max(item);
  680. }
  681. if (!bitrate) {
  682. item = json_object_get(recommended,
  683. "max video bitrate");
  684. if (json_is_integer(item))
  685. bitrate = (int)json_integer_value(item);
  686. }
  687. *video_bitrate = bitrate;
  688. }
  689. fail:
  690. json_decref(root);
  691. }
  692. static const char **rtmp_common_get_supported_video_codecs(void *data)
  693. {
  694. struct rtmp_common *service = data;
  695. if (service->video_codecs)
  696. return (const char **)service->video_codecs;
  697. struct dstr codecs = {0};
  698. json_t *root = open_services_file();
  699. if (!root)
  700. return NULL;
  701. json_t *json_service = find_service(root, service->service, NULL);
  702. if (!json_service) {
  703. goto fail;
  704. }
  705. json_t *json_video_codecs =
  706. json_object_get(json_service, "supported video codecs");
  707. if (json_is_array(json_video_codecs)) {
  708. size_t index;
  709. json_t *item;
  710. json_array_foreach (json_video_codecs, index, item) {
  711. char codec[16];
  712. snprintf(codec, sizeof(codec), "%s",
  713. json_string_value(item));
  714. if (codecs.len)
  715. dstr_cat(&codecs, ";");
  716. dstr_cat(&codecs, codec);
  717. }
  718. } else {
  719. dstr_cat(&codecs, "h264;");
  720. }
  721. service->video_codecs = strlist_split(codecs.array, ';', false);
  722. dstr_free(&codecs);
  723. fail:
  724. json_decref(root);
  725. return (const char **)service->video_codecs;
  726. }
  727. static const char *rtmp_common_username(void *data)
  728. {
  729. struct rtmp_common *service = data;
  730. if (service->service && strcmp(service->service, "Dacast") == 0) {
  731. if (service->key) {
  732. struct dacast_ingest *ingest;
  733. ingest = dacast_ingest(service->key);
  734. return ingest->username;
  735. }
  736. }
  737. return NULL;
  738. }
  739. static const char *rtmp_common_password(void *data)
  740. {
  741. struct rtmp_common *service = data;
  742. if (service->service && strcmp(service->service, "Dacast") == 0) {
  743. if (service->key) {
  744. struct dacast_ingest *ingest;
  745. ingest = dacast_ingest(service->key);
  746. return ingest->password;
  747. }
  748. }
  749. return NULL;
  750. }
  751. struct obs_service_info rtmp_common_service = {
  752. .id = "rtmp_common",
  753. .get_name = rtmp_common_getname,
  754. .create = rtmp_common_create,
  755. .destroy = rtmp_common_destroy,
  756. .update = rtmp_common_update,
  757. .get_properties = rtmp_common_properties,
  758. .get_url = rtmp_common_url,
  759. .get_key = rtmp_common_key,
  760. .get_username = rtmp_common_username,
  761. .get_password = rtmp_common_password,
  762. .apply_encoder_settings = rtmp_common_apply_settings,
  763. .get_output_type = rtmp_common_get_output_type,
  764. .get_supported_resolutions = rtmp_common_get_supported_resolutions,
  765. .get_max_fps = rtmp_common_get_max_fps,
  766. .get_max_bitrate = rtmp_common_get_max_bitrate,
  767. .get_supported_video_codecs = rtmp_common_get_supported_video_codecs,
  768. };