yolobs-studio/plugins/image-source/image-source.c

291 lines
6.9 KiB
C
Raw Normal View History

2016-02-23 23:16:51 +00:00
#include <obs-module.h>
#include <graphics/image-file.h>
#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 <sys/stat.h>
2019-09-22 21:19:10 +00:00
#define blog(log_level, format, ...) \
2016-02-23 23:16:51 +00:00
blog(log_level, "[image_source: '%s'] " format, \
2019-09-22 21:19:10 +00:00
obs_source_get_name(context->source), ##__VA_ARGS__)
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
#define debug(format, ...) blog(LOG_DEBUG, format, ##__VA_ARGS__)
#define info(format, ...) blog(LOG_INFO, format, ##__VA_ARGS__)
#define warn(format, ...) blog(LOG_WARNING, format, ##__VA_ARGS__)
2016-02-23 23:16:51 +00:00
struct image_source {
obs_source_t *source;
2019-09-22 21:19:10 +00:00
char *file;
bool persistent;
time_t file_timestamp;
float update_time_elapsed;
uint64_t last_time;
bool active;
2016-02-23 23:16:51 +00:00
2019-07-27 12:47:10 +00:00
gs_image_file2_t if2;
2016-02-23 23:16:51 +00:00
};
static time_t get_modified_timestamp(const char *filename)
{
struct stat stats;
2016-05-24 19:53:01 +00:00
if (os_stat(filename, &stats) != 0)
return -1;
2016-02-23 23:16:51 +00:00
return stats.st_mtime;
}
static const char *image_source_get_name(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("ImageInput");
}
static void image_source_load(struct image_source *context)
{
char *file = context->file;
obs_enter_graphics();
2019-07-27 12:47:10 +00:00
gs_image_file2_free(&context->if2);
2016-02-23 23:16:51 +00:00
obs_leave_graphics();
if (file && *file) {
debug("loading texture '%s'", file);
context->file_timestamp = get_modified_timestamp(file);
2019-07-27 12:47:10 +00:00
gs_image_file2_init(&context->if2, file);
2016-02-23 23:16:51 +00:00
context->update_time_elapsed = 0;
obs_enter_graphics();
2019-07-27 12:47:10 +00:00
gs_image_file2_init_texture(&context->if2);
2016-02-23 23:16:51 +00:00
obs_leave_graphics();
2019-07-27 12:47:10 +00:00
if (!context->if2.image.loaded)
2016-02-23 23:16:51 +00:00
warn("failed to load texture '%s'", file);
}
}
static void image_source_unload(struct image_source *context)
{
obs_enter_graphics();
2019-07-27 12:47:10 +00:00
gs_image_file2_free(&context->if2);
2016-02-23 23:16:51 +00:00
obs_leave_graphics();
}
static void image_source_update(void *data, obs_data_t *settings)
{
struct image_source *context = data;
const char *file = obs_data_get_string(settings, "file");
const bool unload = obs_data_get_bool(settings, "unload");
if (context->file)
bfree(context->file);
context->file = bstrdup(file);
context->persistent = !unload;
/* Load the image if the source is persistent or showing */
if (context->persistent || obs_source_showing(context->source))
image_source_load(data);
else
image_source_unload(data);
}
static void image_source_defaults(obs_data_t *settings)
{
obs_data_set_default_bool(settings, "unload", false);
}
static void image_source_show(void *data)
{
struct image_source *context = data;
if (!context->persistent)
image_source_load(context);
}
static void image_source_hide(void *data)
{
struct image_source *context = data;
if (!context->persistent)
image_source_unload(context);
}
static void *image_source_create(obs_data_t *settings, obs_source_t *source)
{
struct image_source *context = bzalloc(sizeof(struct image_source));
context->source = source;
image_source_update(context, settings);
return context;
}
static void image_source_destroy(void *data)
{
struct image_source *context = data;
image_source_unload(context);
if (context->file)
bfree(context->file);
bfree(context);
}
static uint32_t image_source_getwidth(void *data)
{
struct image_source *context = data;
2019-07-27 12:47:10 +00:00
return context->if2.image.cx;
2016-02-23 23:16:51 +00:00
}
static uint32_t image_source_getheight(void *data)
{
struct image_source *context = data;
2019-07-27 12:47:10 +00:00
return context->if2.image.cy;
2016-02-23 23:16:51 +00:00
}
static void image_source_render(void *data, gs_effect_t *effect)
{
struct image_source *context = data;
2019-07-27 12:47:10 +00:00
if (!context->if2.image.texture)
2016-02-23 23:16:51 +00:00
return;
gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
2019-09-22 21:19:10 +00:00
context->if2.image.texture);
gs_draw_sprite(context->if2.image.texture, 0, context->if2.image.cx,
context->if2.image.cy);
2016-02-23 23:16:51 +00:00
}
static void image_source_tick(void *data, float seconds)
{
struct image_source *context = data;
uint64_t frame_time = obs_get_video_frame_time();
2017-06-29 19:01:10 +00:00
context->update_time_elapsed += seconds;
if (context->update_time_elapsed >= 1.0f) {
time_t t = get_modified_timestamp(context->file);
context->update_time_elapsed = 0.0f;
if (context->file_timestamp != t) {
image_source_load(context);
}
}
2016-08-28 12:07:43 +00:00
if (obs_source_active(context->source)) {
if (!context->active) {
2019-07-27 12:47:10 +00:00
if (context->if2.image.is_animated_gif)
2016-08-28 12:07:43 +00:00
context->last_time = frame_time;
context->active = true;
}
} else {
if (context->active) {
2019-07-27 12:47:10 +00:00
if (context->if2.image.is_animated_gif) {
context->if2.image.cur_frame = 0;
context->if2.image.cur_loop = 0;
context->if2.image.cur_time = 0;
2016-08-28 12:07:43 +00:00
obs_enter_graphics();
2019-07-27 12:47:10 +00:00
gs_image_file2_update_texture(&context->if2);
2016-08-28 12:07:43 +00:00
obs_leave_graphics();
}
context->active = false;
}
return;
}
2019-07-27 12:47:10 +00:00
if (context->last_time && context->if2.image.is_animated_gif) {
2016-02-23 23:16:51 +00:00
uint64_t elapsed = frame_time - context->last_time;
2019-07-27 12:47:10 +00:00
bool updated = gs_image_file2_tick(&context->if2, elapsed);
2016-02-23 23:16:51 +00:00
if (updated) {
obs_enter_graphics();
2019-07-27 12:47:10 +00:00
gs_image_file2_update_texture(&context->if2);
2016-02-23 23:16:51 +00:00
obs_leave_graphics();
}
}
context->last_time = frame_time;
}
static const char *image_filter =
2019-07-27 12:47:10 +00:00
"All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd);;"
2016-02-23 23:16:51 +00:00
"BMP Files (*.bmp);;"
"Targa Files (*.tga);;"
"PNG Files (*.png);;"
"JPEG Files (*.jpeg *.jpg);;"
2019-07-27 12:47:10 +00:00
"GIF Files (*.gif);;"
"PSD Files (*.psd);;"
"All Files (*.*)";
2016-02-23 23:16:51 +00:00
2016-05-24 19:53:01 +00:00
static obs_properties_t *image_source_properties(void *data)
2016-02-23 23:16:51 +00:00
{
2016-05-24 19:53:01 +00:00
struct image_source *s = data;
struct dstr path = {0};
2016-02-23 23:16:51 +00:00
obs_properties_t *props = obs_properties_create();
2016-05-24 19:53:01 +00:00
if (s && s->file && *s->file) {
const char *slash;
dstr_copy(&path, s->file);
dstr_replace(&path, "\\", "/");
slash = strrchr(path.array, '/');
if (slash)
dstr_resize(&path, slash - path.array + 1);
}
2019-09-22 21:19:10 +00:00
obs_properties_add_path(props, "file", obs_module_text("File"),
OBS_PATH_FILE, image_filter, path.array);
obs_properties_add_bool(props, "unload",
obs_module_text("UnloadWhenNotShowing"));
2016-05-24 19:53:01 +00:00
dstr_free(&path);
2016-02-23 23:16:51 +00:00
return props;
}
2019-07-27 12:47:10 +00:00
uint64_t image_source_get_memory_usage(void *data)
{
struct image_source *s = data;
return s->if2.mem_usage;
}
2016-02-23 23:16:51 +00:00
static struct obs_source_info image_source_info = {
2019-09-22 21:19:10 +00:00
.id = "image_source",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_VIDEO,
.get_name = image_source_get_name,
.create = image_source_create,
.destroy = image_source_destroy,
.update = image_source_update,
.get_defaults = image_source_defaults,
.show = image_source_show,
.hide = image_source_hide,
.get_width = image_source_getwidth,
.get_height = image_source_getheight,
.video_render = image_source_render,
.video_tick = image_source_tick,
2020-03-25 08:07:22 +00:00
.get_properties = image_source_properties,
.icon_type = OBS_ICON_TYPE_IMAGE,
};
2016-02-23 23:16:51 +00:00
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
2019-07-27 12:47:10 +00:00
MODULE_EXPORT const char *obs_module_description(void)
{
return "Image/color/slideshow sources";
}
2016-02-23 23:16:51 +00:00
2016-08-28 12:07:43 +00:00
extern struct obs_source_info slideshow_info;
2020-03-25 08:07:22 +00:00
extern struct obs_source_info color_source_info_v1;
extern struct obs_source_info color_source_info_v2;
2016-08-28 12:07:43 +00:00
2016-02-23 23:16:51 +00:00
bool obs_module_load(void)
{
obs_register_source(&image_source_info);
2020-03-25 08:07:22 +00:00
obs_register_source(&color_source_info_v1);
obs_register_source(&color_source_info_v2);
2016-08-28 12:07:43 +00:00
obs_register_source(&slideshow_info);
2016-02-23 23:16:51 +00:00
return true;
}