rtmp-common.c 28 KB

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