rtmp-common.c 22 KB

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