2014-06-27 15:37:04 -07:00
|
|
|
#include <obs-module.h>
|
|
|
|
|
2015-02-21 11:41:47 -08:00
|
|
|
#define blog(log_level, format, ...) \
|
|
|
|
blog(log_level, "[image_source: '%s'] " format, \
|
2014-08-04 08:41:15 -07:00
|
|
|
obs_source_get_name(context->source), ##__VA_ARGS__)
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2015-02-21 11:41:47 -08: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__)
|
|
|
|
|
2014-06-27 15:37:04 -07:00
|
|
|
struct image_source {
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_source_t *source;
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2015-02-20 06:31:49 -08:00
|
|
|
char *file;
|
|
|
|
bool persistent;
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
gs_texture_t *tex;
|
2014-06-27 15:37:04 -07:00
|
|
|
uint32_t cx;
|
|
|
|
uint32_t cy;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *image_source_get_name(void)
|
|
|
|
{
|
2014-07-14 08:06:00 -07:00
|
|
|
return obs_module_text("ImageInput");
|
2014-06-27 15:37:04 -07:00
|
|
|
}
|
|
|
|
|
2015-02-20 06:31:49 -08:00
|
|
|
static void image_source_load(struct image_source *context)
|
2014-06-27 15:37:04 -07:00
|
|
|
{
|
2015-02-20 06:31:49 -08:00
|
|
|
char *file = context->file;
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2014-08-04 05:48:58 -07:00
|
|
|
obs_enter_graphics();
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2015-02-20 06:31:49 -08:00
|
|
|
if (context->tex)
|
2014-08-07 23:42:07 -07:00
|
|
|
gs_texture_destroy(context->tex);
|
2015-02-20 06:31:49 -08:00
|
|
|
context->tex = NULL;
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2014-07-24 00:25:05 -07:00
|
|
|
if (file && *file) {
|
2015-02-20 06:31:49 -08:00
|
|
|
debug("loading texture '%s'", file);
|
|
|
|
|
2014-08-07 23:42:07 -07:00
|
|
|
context->tex = gs_texture_create_from_file(file);
|
2014-06-27 15:37:04 -07:00
|
|
|
if (context->tex) {
|
2014-08-07 23:42:07 -07:00
|
|
|
context->cx = gs_texture_get_width(context->tex);
|
|
|
|
context->cy = gs_texture_get_height(context->tex);
|
2014-06-27 15:37:04 -07:00
|
|
|
} else {
|
|
|
|
warn("failed to load texture '%s'", file);
|
|
|
|
context->cx = 0;
|
|
|
|
context->cy = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:58 -07:00
|
|
|
obs_leave_graphics();
|
2014-06-27 15:37:04 -07:00
|
|
|
}
|
|
|
|
|
2015-02-20 06:31:49 -08:00
|
|
|
static void image_source_unload(struct image_source *context)
|
|
|
|
{
|
|
|
|
obs_enter_graphics();
|
|
|
|
|
|
|
|
if (context->tex)
|
|
|
|
gs_texture_destroy(context->tex);
|
|
|
|
context->tex = NULL;
|
|
|
|
|
|
|
|
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", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void *image_source_create(obs_data_t *settings, obs_source_t *source)
|
2014-06-27 15:37:04 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2015-02-20 06:31:49 -08:00
|
|
|
image_source_unload(context);
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2015-02-20 06:31:49 -08:00
|
|
|
if (context->file)
|
|
|
|
bfree(context->file);
|
2014-06-27 15:37:04 -07:00
|
|
|
bfree(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t image_source_getwidth(void *data)
|
|
|
|
{
|
|
|
|
struct image_source *context = data;
|
|
|
|
return context->cx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t image_source_getheight(void *data)
|
|
|
|
{
|
|
|
|
struct image_source *context = data;
|
|
|
|
return context->cy;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void image_source_render(void *data, gs_effect_t *effect)
|
2014-06-27 15:37:04 -07:00
|
|
|
{
|
|
|
|
struct image_source *context = data;
|
2015-02-20 06:31:49 -08:00
|
|
|
|
2014-06-27 15:37:04 -07:00
|
|
|
if (!context->tex)
|
|
|
|
return;
|
|
|
|
|
2014-07-16 14:51:59 -07:00
|
|
|
gs_reset_blend_state();
|
2014-08-07 23:42:07 -07:00
|
|
|
gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
|
|
|
|
context->tex);
|
2014-06-27 15:37:04 -07:00
|
|
|
gs_draw_sprite(context->tex, 0, context->cx, context->cy);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *image_filter =
|
|
|
|
"All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif);;"
|
|
|
|
"BMP Files (*.bmp);;"
|
|
|
|
"Targa Files (*.tga);;"
|
|
|
|
"PNG Files (*.png);;"
|
|
|
|
"JPEG Files (*.jpeg *.jpg);;"
|
|
|
|
"GIF Files (*.gif)";
|
|
|
|
|
2014-09-29 08:36:13 -07:00
|
|
|
static obs_properties_t *image_source_properties(void *unused)
|
2014-06-27 15:37:04 -07:00
|
|
|
{
|
2014-09-29 08:36:13 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_properties_t *props = obs_properties_create();
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2014-07-09 22:12:57 -07:00
|
|
|
obs_properties_add_path(props,
|
|
|
|
"file", obs_module_text("File"),
|
|
|
|
OBS_PATH_FILE, image_filter, NULL);
|
2015-02-20 06:31:49 -08:00
|
|
|
obs_properties_add_bool(props,
|
|
|
|
"unload", obs_module_text("UnloadWhenNotShowing"));
|
2014-06-27 15:37:04 -07:00
|
|
|
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct obs_source_info image_source_info = {
|
2014-08-04 21:27:52 -07: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,
|
2015-02-20 06:31:49 -08:00
|
|
|
.get_defaults = image_source_defaults,
|
|
|
|
.show = image_source_show,
|
|
|
|
.hide = image_source_hide,
|
2014-08-04 21:27:52 -07:00
|
|
|
.get_width = image_source_getwidth,
|
|
|
|
.get_height = image_source_getheight,
|
|
|
|
.video_render = image_source_render,
|
|
|
|
.get_properties = image_source_properties
|
2014-06-27 15:37:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
OBS_DECLARE_MODULE()
|
2014-07-09 22:12:57 -07:00
|
|
|
OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
|
2014-06-27 15:37:04 -07:00
|
|
|
|
2014-07-27 12:42:43 -07:00
|
|
|
bool obs_module_load(void)
|
2014-06-27 15:37:04 -07:00
|
|
|
{
|
|
|
|
obs_register_source(&image_source_info);
|
|
|
|
return true;
|
|
|
|
}
|