rtmp-common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 inline json_t *find_service(json_t *root, const char *name,
  344. const char **p_new_name)
  345. {
  346. size_t index;
  347. json_t *service;
  348. if (p_new_name)
  349. *p_new_name = NULL;
  350. json_array_foreach (root, index, service) {
  351. const char *cur_name = get_string_val(service, "name");
  352. if (strcmp(name, cur_name) == 0)
  353. return service;
  354. /* check for alternate names */
  355. json_t *alt_names = json_object_get(service, "alt_names");
  356. size_t alt_name_idx;
  357. json_t *alt_name_obj;
  358. json_array_foreach (alt_names, alt_name_idx, alt_name_obj) {
  359. const char *alt_name = json_string_value(alt_name_obj);
  360. if (alt_name && strcmp(name, alt_name) == 0) {
  361. if (p_new_name)
  362. *p_new_name = cur_name;
  363. return service;
  364. }
  365. }
  366. }
  367. return NULL;
  368. }
  369. static bool service_selected(obs_properties_t *props, obs_property_t *p,
  370. obs_data_t *settings)
  371. {
  372. const char *name = obs_data_get_string(settings, "service");
  373. json_t *root = obs_properties_get_param(props);
  374. json_t *service;
  375. const char *new_name;
  376. if (!name || !*name)
  377. return false;
  378. service = find_service(root, name, &new_name);
  379. if (!service) {
  380. const char *server = obs_data_get_string(settings, "server");
  381. obs_property_list_insert_string(p, 0, name, name);
  382. obs_property_list_item_disable(p, 0, true);
  383. p = obs_properties_get(props, "server");
  384. obs_property_list_insert_string(p, 0, server, server);
  385. obs_property_list_item_disable(p, 0, true);
  386. return true;
  387. }
  388. if (new_name) {
  389. name = new_name;
  390. obs_data_set_string(settings, "service", name);
  391. }
  392. fill_servers(obs_properties_get(props, "server"), service, name);
  393. fill_more_info_link(service, settings);
  394. return true;
  395. }
  396. static bool show_all_services_toggled(obs_properties_t *ppts, obs_property_t *p,
  397. obs_data_t *settings)
  398. {
  399. const char *cur_service = obs_data_get_string(settings, "service");
  400. bool show_all = obs_data_get_bool(settings, "show_all");
  401. json_t *root = obs_properties_get_param(ppts);
  402. if (!root)
  403. return false;
  404. build_service_list(obs_properties_get(ppts, "service"), root, show_all,
  405. cur_service);
  406. UNUSED_PARAMETER(p);
  407. return true;
  408. }
  409. static obs_properties_t *rtmp_common_properties(void *unused)
  410. {
  411. UNUSED_PARAMETER(unused);
  412. obs_properties_t *ppts = obs_properties_create();
  413. obs_property_t *p;
  414. json_t *root;
  415. root = open_services_file();
  416. if (root)
  417. obs_properties_set_param(ppts, root, properties_data_destroy);
  418. p = obs_properties_add_list(ppts, "service", obs_module_text("Service"),
  419. OBS_COMBO_TYPE_LIST,
  420. OBS_COMBO_FORMAT_STRING);
  421. obs_property_set_modified_callback(p, service_selected);
  422. p = obs_properties_add_bool(ppts, "show_all",
  423. obs_module_text("ShowAll"));
  424. obs_property_set_modified_callback(p, show_all_services_toggled);
  425. obs_properties_add_list(ppts, "server", obs_module_text("Server"),
  426. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  427. obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
  428. OBS_TEXT_PASSWORD);
  429. return ppts;
  430. }
  431. static void apply_video_encoder_settings(obs_data_t *settings,
  432. json_t *recommended)
  433. {
  434. json_t *item = json_object_get(recommended, "keyint");
  435. if (json_is_integer(item)) {
  436. int keyint = (int)json_integer_value(item);
  437. obs_data_set_int(settings, "keyint_sec", keyint);
  438. }
  439. obs_data_set_string(settings, "rate_control", "CBR");
  440. item = json_object_get(recommended, "profile");
  441. obs_data_item_t *enc_item = obs_data_item_byname(settings, "profile");
  442. if (json_is_string(item) &&
  443. obs_data_item_gettype(enc_item) == OBS_DATA_STRING) {
  444. const char *profile = json_string_value(item);
  445. obs_data_set_string(settings, "profile", profile);
  446. }
  447. obs_data_item_release(&enc_item);
  448. item = json_object_get(recommended, "max video bitrate");
  449. if (json_is_integer(item)) {
  450. int max_bitrate = (int)json_integer_value(item);
  451. if (obs_data_get_int(settings, "bitrate") > max_bitrate) {
  452. obs_data_set_int(settings, "bitrate", max_bitrate);
  453. obs_data_set_int(settings, "buffer_size", max_bitrate);
  454. }
  455. }
  456. item = json_object_get(recommended, "bframes");
  457. if (json_is_integer(item)) {
  458. int bframes = (int)json_integer_value(item);
  459. obs_data_set_int(settings, "bf", bframes);
  460. }
  461. item = json_object_get(recommended, "x264opts");
  462. if (json_is_string(item)) {
  463. const char *x264_settings = json_string_value(item);
  464. const char *cur_settings =
  465. obs_data_get_string(settings, "x264opts");
  466. struct dstr opts;
  467. dstr_init_copy(&opts, cur_settings);
  468. if (!dstr_is_empty(&opts))
  469. dstr_cat(&opts, " ");
  470. dstr_cat(&opts, x264_settings);
  471. obs_data_set_string(settings, "x264opts", opts.array);
  472. dstr_free(&opts);
  473. }
  474. }
  475. static void apply_audio_encoder_settings(obs_data_t *settings,
  476. json_t *recommended)
  477. {
  478. json_t *item = json_object_get(recommended, "max audio bitrate");
  479. if (json_is_integer(item)) {
  480. int max_bitrate = (int)json_integer_value(item);
  481. if (obs_data_get_int(settings, "bitrate") > max_bitrate)
  482. obs_data_set_int(settings, "bitrate", max_bitrate);
  483. }
  484. }
  485. static void initialize_output(struct rtmp_common *service, json_t *root,
  486. obs_data_t *video_settings,
  487. obs_data_t *audio_settings)
  488. {
  489. json_t *json_service = find_service(root, service->service, NULL);
  490. json_t *recommended;
  491. if (!json_service) {
  492. if (service->service && *service->service)
  493. blog(LOG_WARNING,
  494. "rtmp-common.c: [initialize_output] "
  495. "Could not find service '%s'",
  496. service->service);
  497. return;
  498. }
  499. recommended = json_object_get(json_service, "recommended");
  500. if (!recommended)
  501. return;
  502. if (video_settings)
  503. apply_video_encoder_settings(video_settings, recommended);
  504. if (audio_settings)
  505. apply_audio_encoder_settings(audio_settings, recommended);
  506. }
  507. static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings,
  508. obs_data_t *audio_settings)
  509. {
  510. struct rtmp_common *service = data;
  511. json_t *root = open_services_file();
  512. if (root) {
  513. initialize_output(service, root, video_settings,
  514. audio_settings);
  515. json_decref(root);
  516. }
  517. }
  518. static const char *rtmp_common_get_output_type(void *data)
  519. {
  520. struct rtmp_common *service = data;
  521. return service->output;
  522. }
  523. static const char *rtmp_common_url(void *data)
  524. {
  525. struct rtmp_common *service = data;
  526. if (service->service && strcmp(service->service, "Twitch") == 0) {
  527. if (service->server && strcmp(service->server, "auto") == 0) {
  528. struct twitch_ingest ing;
  529. twitch_ingests_refresh(3);
  530. twitch_ingests_lock();
  531. ing = twitch_ingest(0);
  532. twitch_ingests_unlock();
  533. return ing.url;
  534. }
  535. }
  536. if (service->service && strcmp(service->service, "YouNow") == 0) {
  537. if (service->server && service->key) {
  538. return younow_get_ingest(service->server, service->key);
  539. }
  540. }
  541. if (service->service && strcmp(service->service, "Nimo TV") == 0) {
  542. if (service->server && strcmp(service->server, "auto") == 0) {
  543. return nimotv_get_ingest(service->key);
  544. }
  545. }
  546. if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
  547. if (service->server && service->key) {
  548. struct showroom_ingest *ingest;
  549. ingest = showroom_get_ingest(service->server,
  550. service->key);
  551. return ingest->url;
  552. }
  553. }
  554. if (service->service && strcmp(service->service, "Dacast") == 0) {
  555. if (service->server && service->key) {
  556. dacast_ingests_load_data(service->server, service->key);
  557. struct dacast_ingest *ingest;
  558. ingest = dacast_ingest(service->key);
  559. return ingest->url;
  560. }
  561. }
  562. return service->server;
  563. }
  564. static const char *rtmp_common_key(void *data)
  565. {
  566. struct rtmp_common *service = data;
  567. if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
  568. if (service->server && service->key) {
  569. struct showroom_ingest *ingest;
  570. ingest = showroom_get_ingest(service->server,
  571. service->key);
  572. return ingest->key;
  573. }
  574. }
  575. if (service->service && strcmp(service->service, "Dacast") == 0) {
  576. if (service->key) {
  577. struct dacast_ingest *ingest;
  578. ingest = dacast_ingest(service->key);
  579. return ingest->streamkey;
  580. }
  581. }
  582. return service->key;
  583. }
  584. static bool supports_multitrack(void *data)
  585. {
  586. struct rtmp_common *service = data;
  587. return service->supports_additional_audio_track;
  588. }
  589. static void rtmp_common_get_supported_resolutions(
  590. void *data, struct obs_service_resolution **resolutions, size_t *count)
  591. {
  592. struct rtmp_common *service = data;
  593. *count = service->supported_resolutions_count;
  594. *resolutions = bmemdup(service->supported_resolutions,
  595. *count * sizeof(struct obs_service_resolution));
  596. }
  597. static void rtmp_common_get_max_fps(void *data, int *fps)
  598. {
  599. struct rtmp_common *service = data;
  600. *fps = service->max_fps;
  601. }
  602. static void rtmp_common_get_max_bitrate(void *data, int *video_bitrate,
  603. int *audio_bitrate)
  604. {
  605. struct rtmp_common *service = data;
  606. json_t *root = open_services_file();
  607. json_t *item;
  608. if (!root)
  609. return;
  610. json_t *json_service = find_service(root, service->service, NULL);
  611. if (!json_service) {
  612. goto fail;
  613. }
  614. json_t *recommended = json_object_get(json_service, "recommended");
  615. if (!recommended) {
  616. goto fail;
  617. }
  618. if (audio_bitrate) {
  619. item = json_object_get(recommended, "max audio bitrate");
  620. if (json_is_integer(item))
  621. *audio_bitrate = (int)json_integer_value(item);
  622. }
  623. if (video_bitrate) {
  624. item = json_object_get(recommended, "max video bitrate");
  625. if (json_is_integer(item))
  626. *video_bitrate = (int)json_integer_value(item);
  627. }
  628. fail:
  629. json_decref(root);
  630. }
  631. static const char *rtmp_common_username(void *data)
  632. {
  633. struct rtmp_common *service = data;
  634. if (service->service && strcmp(service->service, "Dacast") == 0) {
  635. if (service->key) {
  636. struct dacast_ingest *ingest;
  637. ingest = dacast_ingest(service->key);
  638. return ingest->username;
  639. }
  640. }
  641. return NULL;
  642. }
  643. static const char *rtmp_common_password(void *data)
  644. {
  645. struct rtmp_common *service = data;
  646. if (service->service && strcmp(service->service, "Dacast") == 0) {
  647. if (service->key) {
  648. struct dacast_ingest *ingest;
  649. ingest = dacast_ingest(service->key);
  650. return ingest->password;
  651. }
  652. }
  653. return NULL;
  654. }
  655. struct obs_service_info rtmp_common_service = {
  656. .id = "rtmp_common",
  657. .get_name = rtmp_common_getname,
  658. .create = rtmp_common_create,
  659. .destroy = rtmp_common_destroy,
  660. .update = rtmp_common_update,
  661. .get_properties = rtmp_common_properties,
  662. .get_url = rtmp_common_url,
  663. .get_key = rtmp_common_key,
  664. .get_username = rtmp_common_username,
  665. .get_password = rtmp_common_password,
  666. .apply_encoder_settings = rtmp_common_apply_settings,
  667. .get_output_type = rtmp_common_get_output_type,
  668. .get_supported_resolutions = rtmp_common_get_supported_resolutions,
  669. .get_max_fps = rtmp_common_get_max_fps,
  670. .get_max_bitrate = rtmp_common_get_max_bitrate,
  671. };