image-source.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <obs-module.h>
  2. #include <graphics/image-file.h>
  3. #include <util/platform.h>
  4. #include <util/dstr.h>
  5. #include <sys/stat.h>
  6. #define blog(log_level, format, ...) \
  7. blog(log_level, "[image_source: '%s'] " format, \
  8. obs_source_get_name(context->source), ##__VA_ARGS__)
  9. #define debug(format, ...) blog(LOG_DEBUG, format, ##__VA_ARGS__)
  10. #define info(format, ...) blog(LOG_INFO, format, ##__VA_ARGS__)
  11. #define warn(format, ...) blog(LOG_WARNING, format, ##__VA_ARGS__)
  12. struct image_source {
  13. obs_source_t *source;
  14. char *file;
  15. bool persistent;
  16. bool linear_alpha;
  17. time_t file_timestamp;
  18. float update_time_elapsed;
  19. uint64_t last_time;
  20. bool active;
  21. gs_image_file2_t if2;
  22. };
  23. static time_t get_modified_timestamp(const char *filename)
  24. {
  25. struct stat stats;
  26. if (os_stat(filename, &stats) != 0)
  27. return -1;
  28. return stats.st_mtime;
  29. }
  30. static const char *image_source_get_name(void *unused)
  31. {
  32. UNUSED_PARAMETER(unused);
  33. return obs_module_text("ImageInput");
  34. }
  35. static void image_source_load(struct image_source *context)
  36. {
  37. char *file = context->file;
  38. obs_enter_graphics();
  39. gs_image_file2_free(&context->if2);
  40. obs_leave_graphics();
  41. if (file && *file) {
  42. debug("loading texture '%s'", file);
  43. context->file_timestamp = get_modified_timestamp(file);
  44. gs_image_file2_init(&context->if2, file);
  45. context->update_time_elapsed = 0;
  46. obs_enter_graphics();
  47. gs_image_file2_init_texture(&context->if2);
  48. obs_leave_graphics();
  49. if (!context->if2.image.loaded)
  50. warn("failed to load texture '%s'", file);
  51. }
  52. }
  53. static void image_source_unload(struct image_source *context)
  54. {
  55. obs_enter_graphics();
  56. gs_image_file2_free(&context->if2);
  57. obs_leave_graphics();
  58. }
  59. static void image_source_update(void *data, obs_data_t *settings)
  60. {
  61. struct image_source *context = data;
  62. const char *file = obs_data_get_string(settings, "file");
  63. const bool unload = obs_data_get_bool(settings, "unload");
  64. const bool linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  65. if (context->file)
  66. bfree(context->file);
  67. context->file = bstrdup(file);
  68. context->persistent = !unload;
  69. context->linear_alpha = linear_alpha;
  70. /* Load the image if the source is persistent or showing */
  71. if (context->persistent || obs_source_showing(context->source))
  72. image_source_load(data);
  73. else
  74. image_source_unload(data);
  75. }
  76. static void image_source_defaults(obs_data_t *settings)
  77. {
  78. obs_data_set_default_bool(settings, "unload", false);
  79. obs_data_set_default_bool(settings, "linear_alpha", false);
  80. }
  81. static void image_source_show(void *data)
  82. {
  83. struct image_source *context = data;
  84. if (!context->persistent)
  85. image_source_load(context);
  86. }
  87. static void image_source_hide(void *data)
  88. {
  89. struct image_source *context = data;
  90. if (!context->persistent)
  91. image_source_unload(context);
  92. }
  93. static void *image_source_create(obs_data_t *settings, obs_source_t *source)
  94. {
  95. struct image_source *context = bzalloc(sizeof(struct image_source));
  96. context->source = source;
  97. image_source_update(context, settings);
  98. return context;
  99. }
  100. static void image_source_destroy(void *data)
  101. {
  102. struct image_source *context = data;
  103. image_source_unload(context);
  104. if (context->file)
  105. bfree(context->file);
  106. bfree(context);
  107. }
  108. static uint32_t image_source_getwidth(void *data)
  109. {
  110. struct image_source *context = data;
  111. return context->if2.image.cx;
  112. }
  113. static uint32_t image_source_getheight(void *data)
  114. {
  115. struct image_source *context = data;
  116. return context->if2.image.cy;
  117. }
  118. static void image_source_render(void *data, gs_effect_t *effect)
  119. {
  120. struct image_source *context = data;
  121. if (!context->if2.image.texture)
  122. return;
  123. const char *tech_name = context->linear_alpha ? "DrawAlphaBlend"
  124. : "DrawNonlinearAlpha";
  125. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  126. gs_technique_t *tech = gs_effect_get_technique(effect, tech_name);
  127. const bool previous = gs_framebuffer_srgb_enabled();
  128. gs_enable_framebuffer_srgb(true);
  129. gs_blend_state_push();
  130. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  131. gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image");
  132. gs_effect_set_texture_srgb(param, context->if2.image.texture);
  133. size_t passes = gs_technique_begin(tech);
  134. for (size_t i = 0; i < passes; i++) {
  135. gs_technique_begin_pass(tech, i);
  136. gs_draw_sprite(context->if2.image.texture, 0,
  137. context->if2.image.cx, context->if2.image.cy);
  138. gs_technique_end_pass(tech);
  139. }
  140. gs_technique_end(tech);
  141. gs_blend_state_pop();
  142. gs_enable_framebuffer_srgb(previous);
  143. }
  144. static void image_source_tick(void *data, float seconds)
  145. {
  146. struct image_source *context = data;
  147. uint64_t frame_time = obs_get_video_frame_time();
  148. context->update_time_elapsed += seconds;
  149. if (obs_source_showing(context->source)) {
  150. if (context->update_time_elapsed >= 1.0f) {
  151. time_t t = get_modified_timestamp(context->file);
  152. context->update_time_elapsed = 0.0f;
  153. if (context->file_timestamp != t) {
  154. image_source_load(context);
  155. }
  156. }
  157. }
  158. if (obs_source_active(context->source)) {
  159. if (!context->active) {
  160. if (context->if2.image.is_animated_gif)
  161. context->last_time = frame_time;
  162. context->active = true;
  163. }
  164. } else {
  165. if (context->active) {
  166. if (context->if2.image.is_animated_gif) {
  167. context->if2.image.cur_frame = 0;
  168. context->if2.image.cur_loop = 0;
  169. context->if2.image.cur_time = 0;
  170. obs_enter_graphics();
  171. gs_image_file2_update_texture(&context->if2);
  172. obs_leave_graphics();
  173. }
  174. context->active = false;
  175. }
  176. return;
  177. }
  178. if (context->last_time && context->if2.image.is_animated_gif) {
  179. uint64_t elapsed = frame_time - context->last_time;
  180. bool updated = gs_image_file2_tick(&context->if2, elapsed);
  181. if (updated) {
  182. obs_enter_graphics();
  183. gs_image_file2_update_texture(&context->if2);
  184. obs_leave_graphics();
  185. }
  186. }
  187. context->last_time = frame_time;
  188. }
  189. static const char *image_filter =
  190. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd *.webp);;"
  191. "BMP Files (*.bmp);;"
  192. "Targa Files (*.tga);;"
  193. "PNG Files (*.png);;"
  194. "JPEG Files (*.jpeg *.jpg);;"
  195. "GIF Files (*.gif);;"
  196. "PSD Files (*.psd);;"
  197. "WebP Files (*.webp);;"
  198. "All Files (*.*)";
  199. static obs_properties_t *image_source_properties(void *data)
  200. {
  201. struct image_source *s = data;
  202. struct dstr path = {0};
  203. obs_properties_t *props = obs_properties_create();
  204. if (s && s->file && *s->file) {
  205. const char *slash;
  206. dstr_copy(&path, s->file);
  207. dstr_replace(&path, "\\", "/");
  208. slash = strrchr(path.array, '/');
  209. if (slash)
  210. dstr_resize(&path, slash - path.array + 1);
  211. }
  212. obs_properties_add_path(props, "file", obs_module_text("File"),
  213. OBS_PATH_FILE, image_filter, path.array);
  214. obs_properties_add_bool(props, "unload",
  215. obs_module_text("UnloadWhenNotShowing"));
  216. obs_properties_add_bool(props, "linear_alpha",
  217. obs_module_text("LinearAlpha"));
  218. dstr_free(&path);
  219. return props;
  220. }
  221. uint64_t image_source_get_memory_usage(void *data)
  222. {
  223. struct image_source *s = data;
  224. return s->if2.mem_usage;
  225. }
  226. static void missing_file_callback(void *src, const char *new_path, void *data)
  227. {
  228. struct image_source *s = src;
  229. obs_source_t *source = s->source;
  230. obs_data_t *settings = obs_source_get_settings(source);
  231. obs_data_set_string(settings, "file", new_path);
  232. obs_source_update(source, settings);
  233. obs_data_release(settings);
  234. UNUSED_PARAMETER(data);
  235. }
  236. static obs_missing_files_t *image_source_missingfiles(void *data)
  237. {
  238. struct image_source *s = data;
  239. obs_missing_files_t *files = obs_missing_files_create();
  240. if (strcmp(s->file, "") != 0) {
  241. if (!os_file_exists(s->file)) {
  242. obs_missing_file_t *file = obs_missing_file_create(
  243. s->file, missing_file_callback,
  244. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  245. obs_missing_files_add_file(files, file);
  246. }
  247. }
  248. return files;
  249. }
  250. static struct obs_source_info image_source_info = {
  251. .id = "image_source",
  252. .type = OBS_SOURCE_TYPE_INPUT,
  253. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  254. OBS_SOURCE_SRGB,
  255. .get_name = image_source_get_name,
  256. .create = image_source_create,
  257. .destroy = image_source_destroy,
  258. .update = image_source_update,
  259. .get_defaults = image_source_defaults,
  260. .show = image_source_show,
  261. .hide = image_source_hide,
  262. .get_width = image_source_getwidth,
  263. .get_height = image_source_getheight,
  264. .video_render = image_source_render,
  265. .video_tick = image_source_tick,
  266. .missing_files = image_source_missingfiles,
  267. .get_properties = image_source_properties,
  268. .icon_type = OBS_ICON_TYPE_IMAGE,
  269. };
  270. OBS_DECLARE_MODULE()
  271. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  272. MODULE_EXPORT const char *obs_module_description(void)
  273. {
  274. return "Image/color/slideshow sources";
  275. }
  276. extern struct obs_source_info slideshow_info;
  277. extern struct obs_source_info color_source_info_v1;
  278. extern struct obs_source_info color_source_info_v2;
  279. extern struct obs_source_info color_source_info_v3;
  280. bool obs_module_load(void)
  281. {
  282. obs_register_source(&image_source_info);
  283. obs_register_source(&color_source_info_v1);
  284. obs_register_source(&color_source_info_v2);
  285. obs_register_source(&color_source_info_v3);
  286. obs_register_source(&slideshow_info);
  287. return true;
  288. }