yolobs-studio/plugins/rtmp-services/rtmp-common.c

667 lines
16 KiB
C
Raw Normal View History

2016-02-23 23:16:51 +00:00
#include <util/platform.h>
2016-05-24 19:53:01 +00:00
#include <util/dstr.h>
2016-02-23 23:16:51 +00:00
#include <obs-module.h>
#include <jansson.h>
2020-10-01 20:15:25 +00:00
#include <obs-config.h>
2016-02-23 23:16:51 +00:00
#include "rtmp-format-ver.h"
2018-02-19 19:54:37 +00:00
#include "twitch.h"
2019-12-10 19:31:54 +00:00
#include "younow.h"
2020-10-01 20:15:25 +00:00
#include "nimotv.h"
#include "showroom.h"
2016-02-23 23:16:51 +00:00
struct rtmp_common {
char *service;
char *server;
char *key;
2018-02-19 19:54:37 +00:00
char *output;
2020-10-01 20:15:25 +00:00
bool supports_additional_audio_track;
2016-02-23 23:16:51 +00:00
};
static const char *rtmp_common_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("StreamingServices");
}
2018-02-19 19:54:37 +00:00
static json_t *open_services_file(void);
2019-07-27 12:47:10 +00:00
static inline json_t *find_service(json_t *root, const char *name,
2019-09-22 21:19:10 +00:00
const char **p_new_name);
2020-10-01 20:15:25 +00:00
static inline bool get_bool_val(json_t *service, const char *key);
2018-02-19 19:54:37 +00:00
static inline const char *get_string_val(json_t *service, const char *key);
extern void twitch_ingests_refresh(int seconds);
2018-12-16 16:14:58 +00:00
static void ensure_valid_url(struct rtmp_common *service, json_t *json,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2018-12-16 16:14:58 +00:00
{
json_t *servers = json_object_get(json, "servers");
const char *top_url = NULL;
json_t *server;
size_t index;
if (!service->server || !servers || !json_is_array(servers))
return;
if (astrstri(service->service, "Facebook") == NULL)
return;
json_array_foreach (servers, index, server) {
const char *url = get_string_val(server, "url");
if (!url)
continue;
if (!top_url)
top_url = url;
if (astrcmpi(service->server, url) == 0)
return;
}
/* server was not found in server list, use first server instead */
if (top_url) {
bfree(service->server);
service->server = bstrdup(top_url);
obs_data_set_string(settings, "server", top_url);
}
}
2016-02-23 23:16:51 +00:00
static void rtmp_common_update(void *data, obs_data_t *settings)
{
struct rtmp_common *service = data;
bfree(service->service);
bfree(service->server);
2018-02-19 19:54:37 +00:00
bfree(service->output);
2016-02-23 23:16:51 +00:00
bfree(service->key);
service->service = bstrdup(obs_data_get_string(settings, "service"));
2019-09-22 21:19:10 +00:00
service->server = bstrdup(obs_data_get_string(settings, "server"));
service->key = bstrdup(obs_data_get_string(settings, "key"));
2020-10-01 20:15:25 +00:00
service->supports_additional_audio_track = false;
2019-09-22 21:19:10 +00:00
service->output = NULL;
2018-02-19 19:54:37 +00:00
json_t *root = open_services_file();
if (root) {
2019-07-27 12:47:10 +00:00
const char *new_name;
json_t *serv = find_service(root, service->service, &new_name);
if (new_name) {
bfree(service->service);
service->service = bstrdup(new_name);
}
2018-02-19 19:54:37 +00:00
if (serv) {
json_t *rec = json_object_get(serv, "recommended");
2019-09-22 21:19:10 +00:00
if (json_is_object(rec)) {
2018-02-19 19:54:37 +00:00
const char *out = get_string_val(rec, "output");
if (out)
service->output = bstrdup(out);
}
2018-12-16 16:14:58 +00:00
2020-10-01 20:15:25 +00:00
service->supports_additional_audio_track = get_bool_val(
serv, "supports_additional_audio_track");
2018-12-16 16:14:58 +00:00
ensure_valid_url(service, serv, settings);
2018-02-19 19:54:37 +00:00
}
}
json_decref(root);
if (!service->output)
service->output = bstrdup("rtmp_output");
2016-02-23 23:16:51 +00:00
}
static void rtmp_common_destroy(void *data)
{
struct rtmp_common *service = data;
bfree(service->service);
bfree(service->server);
2018-02-19 19:54:37 +00:00
bfree(service->output);
2016-02-23 23:16:51 +00:00
bfree(service->key);
bfree(service);
}
static void *rtmp_common_create(obs_data_t *settings, obs_service_t *service)
{
struct rtmp_common *data = bzalloc(sizeof(struct rtmp_common));
rtmp_common_update(data, settings);
UNUSED_PARAMETER(service);
return data;
}
static inline const char *get_string_val(json_t *service, const char *key)
{
json_t *str_val = json_object_get(service, key);
if (!str_val || !json_is_string(str_val))
return NULL;
return json_string_value(str_val);
}
static inline int get_int_val(json_t *service, const char *key)
{
json_t *integer_val = json_object_get(service, key);
if (!integer_val || !json_is_integer(integer_val))
return 0;
return (int)json_integer_value(integer_val);
}
static inline bool get_bool_val(json_t *service, const char *key)
{
json_t *bool_val = json_object_get(service, key);
if (!bool_val || !json_is_boolean(bool_val))
return false;
return json_is_true(bool_val);
}
static void add_service(obs_property_t *list, json_t *service, bool show_all,
2019-09-22 21:19:10 +00:00
const char *cur_service)
2016-02-23 23:16:51 +00:00
{
json_t *servers;
const char *name;
bool common;
if (!json_is_object(service)) {
blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
2019-09-22 21:19:10 +00:00
"is not an object");
2016-02-23 23:16:51 +00:00
return;
}
name = get_string_val(service, "name");
if (!name) {
blog(LOG_WARNING, "rtmp-common.c: [add_service] service "
2019-09-22 21:19:10 +00:00
"has no name");
2016-02-23 23:16:51 +00:00
return;
}
common = get_bool_val(service, "common");
if (!show_all && !common && strcmp(cur_service, name) != 0) {
return;
}
servers = json_object_get(service, "servers");
if (!servers || !json_is_array(servers)) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"rtmp-common.c: [add_service] service "
"'%s' has no servers",
name);
2016-02-23 23:16:51 +00:00
return;
}
obs_property_list_add_string(list, name, name);
}
static void add_services(obs_property_t *list, json_t *root, bool show_all,
2019-09-22 21:19:10 +00:00
const char *cur_service)
2016-02-23 23:16:51 +00:00
{
json_t *service;
size_t index;
if (!json_is_array(root)) {
blog(LOG_WARNING, "rtmp-common.c: [add_services] JSON file "
2019-09-22 21:19:10 +00:00
"root is not an array");
2016-02-23 23:16:51 +00:00
return;
}
json_array_foreach (root, index, service) {
add_service(list, service, show_all, cur_service);
}
2017-06-29 19:01:10 +00:00
2019-07-27 12:47:10 +00:00
service = find_service(root, cur_service, NULL);
2017-06-29 19:01:10 +00:00
if (!service && cur_service && *cur_service) {
obs_property_list_insert_string(list, 0, cur_service,
2019-09-22 21:19:10 +00:00
cur_service);
2017-06-29 19:01:10 +00:00
obs_property_list_item_disable(list, 0, true);
}
2016-02-23 23:16:51 +00:00
}
static json_t *open_json_file(const char *file)
{
2019-09-22 21:19:10 +00:00
char *file_data = os_quick_read_utf8_file(file);
2016-02-23 23:16:51 +00:00
json_error_t error;
2019-09-22 21:19:10 +00:00
json_t *root;
json_t *list;
int format_ver;
2016-02-23 23:16:51 +00:00
if (!file_data)
return NULL;
root = json_loads(file_data, JSON_REJECT_DUPLICATES, &error);
bfree(file_data);
if (!root) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"rtmp-common.c: [open_json_file] "
"Error reading JSON file (%d): %s",
error.line, error.text);
2016-02-23 23:16:51 +00:00
return NULL;
}
format_ver = get_int_val(root, "format_version");
if (format_ver != RTMP_SERVICES_FORMAT_VERSION) {
2019-09-22 21:19:10 +00:00
blog(LOG_DEBUG,
"rtmp-common.c: [open_json_file] "
"Wrong format version (%d), expected %d",
format_ver, RTMP_SERVICES_FORMAT_VERSION);
2016-02-23 23:16:51 +00:00
json_decref(root);
return NULL;
}
list = json_object_get(root, "services");
if (list)
json_incref(list);
json_decref(root);
if (!list) {
blog(LOG_WARNING, "rtmp-common.c: [open_json_file] "
2019-09-22 21:19:10 +00:00
"No services list");
2016-02-23 23:16:51 +00:00
return NULL;
}
return list;
}
static json_t *open_services_file(void)
{
char *file;
json_t *root = NULL;
file = obs_module_config_path("services.json");
if (file) {
root = open_json_file(file);
bfree(file);
}
if (!root) {
file = obs_module_file("services.json");
if (file) {
root = open_json_file(file);
bfree(file);
}
}
return root;
}
static void build_service_list(obs_property_t *list, json_t *root,
2019-09-22 21:19:10 +00:00
bool show_all, const char *cur_service)
2016-02-23 23:16:51 +00:00
{
obs_property_list_clear(list);
add_services(list, root, show_all, cur_service);
}
static void properties_data_destroy(void *data)
{
json_t *root = data;
if (root)
json_decref(root);
}
2018-02-19 19:54:37 +00:00
static bool fill_twitch_servers_locked(obs_property_t *servers_prop)
{
size_t count = twitch_ingest_count();
obs_property_list_add_string(servers_prop,
2019-09-22 21:19:10 +00:00
obs_module_text("Server.Auto"), "auto");
2018-02-19 19:54:37 +00:00
if (count <= 1)
return false;
for (size_t i = 0; i < count; i++) {
struct twitch_ingest ing = twitch_ingest(i);
obs_property_list_add_string(servers_prop, ing.name, ing.url);
}
return true;
}
static inline bool fill_twitch_servers(obs_property_t *servers_prop)
{
bool success;
twitch_ingests_lock();
success = fill_twitch_servers_locked(servers_prop);
twitch_ingests_unlock();
return success;
}
2016-02-23 23:16:51 +00:00
static void fill_servers(obs_property_t *servers_prop, json_t *service,
2019-09-22 21:19:10 +00:00
const char *name)
2016-02-23 23:16:51 +00:00
{
json_t *servers, *server;
size_t index;
obs_property_list_clear(servers_prop);
servers = json_object_get(service, "servers");
if (!json_is_array(servers)) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"rtmp-common.c: [fill_servers] "
"Servers for service '%s' not a valid object",
name);
2016-02-23 23:16:51 +00:00
return;
}
2018-12-16 16:14:58 +00:00
if (strcmp(name, "Twitch") == 0) {
2018-02-19 19:54:37 +00:00
if (fill_twitch_servers(servers_prop))
return;
}
2020-10-01 20:15:25 +00:00
if (strcmp(name, "Nimo TV") == 0) {
obs_property_list_add_string(
servers_prop, obs_module_text("Server.Auto"), "auto");
}
2016-02-23 23:16:51 +00:00
json_array_foreach (servers, index, server) {
const char *server_name = get_string_val(server, "name");
2019-09-22 21:19:10 +00:00
const char *url = get_string_val(server, "url");
2016-02-23 23:16:51 +00:00
if (!server_name || !url)
continue;
obs_property_list_add_string(servers_prop, server_name, url);
}
}
2019-07-27 12:47:10 +00:00
static inline json_t *find_service(json_t *root, const char *name,
2019-09-22 21:19:10 +00:00
const char **p_new_name)
2016-02-23 23:16:51 +00:00
{
size_t index;
json_t *service;
2019-09-22 21:19:10 +00:00
if (p_new_name)
*p_new_name = NULL;
2019-07-27 12:47:10 +00:00
2016-02-23 23:16:51 +00:00
json_array_foreach (root, index, service) {
const char *cur_name = get_string_val(service, "name");
if (strcmp(name, cur_name) == 0)
return service;
2019-07-27 12:47:10 +00:00
/* check for alternate names */
json_t *alt_names = json_object_get(service, "alt_names");
size_t alt_name_idx;
json_t *alt_name_obj;
json_array_foreach (alt_names, alt_name_idx, alt_name_obj) {
const char *alt_name = json_string_value(alt_name_obj);
if (alt_name && strcmp(name, alt_name) == 0) {
2019-09-22 21:19:10 +00:00
if (p_new_name)
*p_new_name = cur_name;
2019-07-27 12:47:10 +00:00
return service;
}
}
2016-02-23 23:16:51 +00:00
}
return NULL;
}
static bool service_selected(obs_properties_t *props, obs_property_t *p,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
const char *name = obs_data_get_string(settings, "service");
2019-09-22 21:19:10 +00:00
json_t *root = obs_properties_get_param(props);
2016-02-23 23:16:51 +00:00
json_t *service;
2019-07-27 12:47:10 +00:00
const char *new_name;
2016-02-23 23:16:51 +00:00
if (!name || !*name)
return false;
2019-07-27 12:47:10 +00:00
service = find_service(root, name, &new_name);
2017-06-29 19:01:10 +00:00
if (!service) {
const char *server = obs_data_get_string(settings, "server");
obs_property_list_insert_string(p, 0, name, name);
obs_property_list_item_disable(p, 0, true);
p = obs_properties_get(props, "server");
obs_property_list_insert_string(p, 0, server, server);
obs_property_list_item_disable(p, 0, true);
return true;
}
2019-07-27 12:47:10 +00:00
if (new_name) {
name = new_name;
obs_data_set_string(settings, "service", name);
}
2016-02-23 23:16:51 +00:00
fill_servers(obs_properties_get(props, "server"), service, name);
return true;
}
2019-09-22 21:19:10 +00:00
static bool show_all_services_toggled(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
const char *cur_service = obs_data_get_string(settings, "service");
bool show_all = obs_data_get_bool(settings, "show_all");
json_t *root = obs_properties_get_param(ppts);
if (!root)
return false;
build_service_list(obs_properties_get(ppts, "service"), root, show_all,
2019-09-22 21:19:10 +00:00
cur_service);
2016-02-23 23:16:51 +00:00
UNUSED_PARAMETER(p);
return true;
}
static obs_properties_t *rtmp_common_properties(void *unused)
{
UNUSED_PARAMETER(unused);
obs_properties_t *ppts = obs_properties_create();
2019-09-22 21:19:10 +00:00
obs_property_t *p;
json_t *root;
2016-02-23 23:16:51 +00:00
root = open_services_file();
if (root)
obs_properties_set_param(ppts, root, properties_data_destroy);
2019-09-22 21:19:10 +00:00
p = obs_properties_add_list(ppts, "service", obs_module_text("Service"),
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(p, service_selected);
p = obs_properties_add_bool(ppts, "show_all",
2019-09-22 21:19:10 +00:00
obs_module_text("ShowAll"));
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(p, show_all_services_toggled);
obs_properties_add_list(ppts, "server", obs_module_text("Server"),
2019-09-22 21:19:10 +00:00
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
2016-02-23 23:16:51 +00:00
obs_properties_add_text(ppts, "key", obs_module_text("StreamKey"),
2019-09-22 21:19:10 +00:00
OBS_TEXT_PASSWORD);
2016-02-23 23:16:51 +00:00
return ppts;
}
static void apply_video_encoder_settings(obs_data_t *settings,
2019-09-22 21:19:10 +00:00
json_t *recommended)
2016-02-23 23:16:51 +00:00
{
json_t *item = json_object_get(recommended, "keyint");
2019-09-22 21:19:10 +00:00
if (json_is_integer(item)) {
2016-02-23 23:16:51 +00:00
int keyint = (int)json_integer_value(item);
obs_data_set_int(settings, "keyint_sec", keyint);
}
2016-05-24 19:53:01 +00:00
obs_data_set_string(settings, "rate_control", "CBR");
2016-02-23 23:16:51 +00:00
item = json_object_get(recommended, "profile");
2019-12-10 19:31:54 +00:00
obs_data_item_t *enc_item = obs_data_item_byname(settings, "profile");
if (json_is_string(item) &&
obs_data_item_gettype(enc_item) == OBS_DATA_STRING) {
2016-02-23 23:16:51 +00:00
const char *profile = json_string_value(item);
obs_data_set_string(settings, "profile", profile);
}
2020-10-01 20:15:25 +00:00
obs_data_item_release(&enc_item);
2016-02-23 23:16:51 +00:00
item = json_object_get(recommended, "max video bitrate");
2019-09-22 21:19:10 +00:00
if (json_is_integer(item)) {
2016-02-23 23:16:51 +00:00
int max_bitrate = (int)json_integer_value(item);
if (obs_data_get_int(settings, "bitrate") > max_bitrate) {
obs_data_set_int(settings, "bitrate", max_bitrate);
obs_data_set_int(settings, "buffer_size", max_bitrate);
}
}
2016-05-24 19:53:01 +00:00
2018-02-19 19:54:37 +00:00
item = json_object_get(recommended, "bframes");
2019-09-22 21:19:10 +00:00
if (json_is_integer(item))
2018-02-19 19:54:37 +00:00
obs_data_set_int(settings, "bf", 0);
2016-05-24 19:53:01 +00:00
item = json_object_get(recommended, "x264opts");
2019-09-22 21:19:10 +00:00
if (json_is_string(item)) {
2016-05-24 19:53:01 +00:00
const char *x264_settings = json_string_value(item);
const char *cur_settings =
obs_data_get_string(settings, "x264opts");
struct dstr opts;
dstr_init_copy(&opts, cur_settings);
if (!dstr_is_empty(&opts))
dstr_cat(&opts, " ");
dstr_cat(&opts, x264_settings);
obs_data_set_string(settings, "x264opts", opts.array);
dstr_free(&opts);
}
2016-02-23 23:16:51 +00:00
}
static void apply_audio_encoder_settings(obs_data_t *settings,
2019-09-22 21:19:10 +00:00
json_t *recommended)
2016-02-23 23:16:51 +00:00
{
json_t *item = json_object_get(recommended, "max audio bitrate");
2019-09-22 21:19:10 +00:00
if (json_is_integer(item)) {
2016-02-23 23:16:51 +00:00
int max_bitrate = (int)json_integer_value(item);
if (obs_data_get_int(settings, "bitrate") > max_bitrate)
obs_data_set_int(settings, "bitrate", max_bitrate);
}
}
static void initialize_output(struct rtmp_common *service, json_t *root,
2019-09-22 21:19:10 +00:00
obs_data_t *video_settings,
obs_data_t *audio_settings)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
json_t *json_service = find_service(root, service->service, NULL);
json_t *recommended;
2016-02-23 23:16:51 +00:00
if (!json_service) {
2018-02-19 19:54:37 +00:00
if (service->service && *service->service)
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"rtmp-common.c: [initialize_output] "
"Could not find service '%s'",
service->service);
2016-02-23 23:16:51 +00:00
return;
}
recommended = json_object_get(json_service, "recommended");
if (!recommended)
return;
if (video_settings)
apply_video_encoder_settings(video_settings, recommended);
if (audio_settings)
apply_audio_encoder_settings(audio_settings, recommended);
}
2019-09-22 21:19:10 +00:00
static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings,
obs_data_t *audio_settings)
2016-02-23 23:16:51 +00:00
{
struct rtmp_common *service = data;
2019-09-22 21:19:10 +00:00
json_t *root = open_services_file();
2016-02-23 23:16:51 +00:00
if (root) {
initialize_output(service, root, video_settings,
2019-09-22 21:19:10 +00:00
audio_settings);
2016-02-23 23:16:51 +00:00
json_decref(root);
}
}
2018-02-19 19:54:37 +00:00
static const char *rtmp_common_get_output_type(void *data)
{
struct rtmp_common *service = data;
return service->output;
}
2016-02-23 23:16:51 +00:00
static const char *rtmp_common_url(void *data)
{
struct rtmp_common *service = data;
2018-02-19 19:54:37 +00:00
if (service->service && strcmp(service->service, "Twitch") == 0) {
if (service->server && strcmp(service->server, "auto") == 0) {
struct twitch_ingest ing;
twitch_ingests_refresh(3);
twitch_ingests_lock();
ing = twitch_ingest(0);
twitch_ingests_unlock();
return ing.url;
}
}
2019-12-10 19:31:54 +00:00
if (service->service && strcmp(service->service, "YouNow") == 0) {
if (service->server && service->key) {
return younow_get_ingest(service->server, service->key);
}
}
2020-10-01 20:15:25 +00:00
if (service->service && strcmp(service->service, "Nimo TV") == 0) {
if (service->server && strcmp(service->server, "auto") == 0) {
return nimotv_get_ingest(service->key);
}
}
if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
if (service->server && service->key) {
struct showroom_ingest *ingest;
ingest = showroom_get_ingest(service->server,
service->key);
return ingest->url;
}
}
2016-02-23 23:16:51 +00:00
return service->server;
}
static const char *rtmp_common_key(void *data)
{
struct rtmp_common *service = data;
2020-10-01 20:15:25 +00:00
if (service->service && strcmp(service->service, "SHOWROOM") == 0) {
if (service->server && service->key) {
struct showroom_ingest *ingest;
ingest = showroom_get_ingest(service->server,
service->key);
return ingest->key;
}
}
2016-02-23 23:16:51 +00:00
return service->key;
}
2020-10-01 20:15:25 +00:00
static bool supports_multitrack(void *data)
{
struct rtmp_common *service = data;
return service->supports_additional_audio_track;
}
2016-02-23 23:16:51 +00:00
struct obs_service_info rtmp_common_service = {
2019-09-22 21:19:10 +00:00
.id = "rtmp_common",
.get_name = rtmp_common_getname,
.create = rtmp_common_create,
.destroy = rtmp_common_destroy,
.update = rtmp_common_update,
2016-02-23 23:16:51 +00:00
.get_properties = rtmp_common_properties,
2019-09-22 21:19:10 +00:00
.get_url = rtmp_common_url,
.get_key = rtmp_common_key,
2016-02-23 23:16:51 +00:00
.apply_encoder_settings = rtmp_common_apply_settings,
2018-02-19 19:54:37 +00:00
.get_output_type = rtmp_common_get_output_type,
2016-02-23 23:16:51 +00:00
};