rtmp-common.c 31 KB

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