1
0

image-source.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_file3_t if3;
  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_file3_free(&context->if3);
  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_file3_init(&context->if3, file,
  45. context->linear_alpha
  46. ? GS_IMAGE_ALPHA_PREMULTIPLY_SRGB
  47. : GS_IMAGE_ALPHA_PREMULTIPLY);
  48. context->update_time_elapsed = 0;
  49. obs_enter_graphics();
  50. gs_image_file3_init_texture(&context->if3);
  51. obs_leave_graphics();
  52. if (!context->if3.image2.image.loaded)
  53. warn("failed to load texture '%s'", file);
  54. }
  55. }
  56. static void image_source_unload(struct image_source *context)
  57. {
  58. obs_enter_graphics();
  59. gs_image_file3_free(&context->if3);
  60. obs_leave_graphics();
  61. }
  62. static void image_source_update(void *data, obs_data_t *settings)
  63. {
  64. struct image_source *context = data;
  65. const char *file = obs_data_get_string(settings, "file");
  66. const bool unload = obs_data_get_bool(settings, "unload");
  67. const bool linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  68. if (context->file)
  69. bfree(context->file);
  70. context->file = bstrdup(file);
  71. context->persistent = !unload;
  72. context->linear_alpha = linear_alpha;
  73. /* Load the image if the source is persistent or showing */
  74. if (context->persistent || obs_source_showing(context->source))
  75. image_source_load(data);
  76. else
  77. image_source_unload(data);
  78. }
  79. static void image_source_defaults(obs_data_t *settings)
  80. {
  81. obs_data_set_default_bool(settings, "unload", false);
  82. obs_data_set_default_bool(settings, "linear_alpha", false);
  83. }
  84. static void image_source_show(void *data)
  85. {
  86. struct image_source *context = data;
  87. if (!context->persistent)
  88. image_source_load(context);
  89. }
  90. static void image_source_hide(void *data)
  91. {
  92. struct image_source *context = data;
  93. if (!context->persistent)
  94. image_source_unload(context);
  95. }
  96. static void *image_source_create(obs_data_t *settings, obs_source_t *source)
  97. {
  98. struct image_source *context = bzalloc(sizeof(struct image_source));
  99. context->source = source;
  100. image_source_update(context, settings);
  101. return context;
  102. }
  103. static void image_source_destroy(void *data)
  104. {
  105. struct image_source *context = data;
  106. image_source_unload(context);
  107. if (context->file)
  108. bfree(context->file);
  109. bfree(context);
  110. }
  111. static uint32_t image_source_getwidth(void *data)
  112. {
  113. struct image_source *context = data;
  114. return context->if3.image2.image.cx;
  115. }
  116. static uint32_t image_source_getheight(void *data)
  117. {
  118. struct image_source *context = data;
  119. return context->if3.image2.image.cy;
  120. }
  121. static void image_source_render(void *data, gs_effect_t *effect)
  122. {
  123. struct image_source *context = data;
  124. if (!context->if3.image2.image.texture)
  125. return;
  126. const bool previous = gs_framebuffer_srgb_enabled();
  127. gs_enable_framebuffer_srgb(true);
  128. gs_blend_state_push();
  129. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  130. gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image");
  131. gs_effect_set_texture_srgb(param, context->if3.image2.image.texture);
  132. gs_draw_sprite(context->if3.image2.image.texture, 0,
  133. context->if3.image2.image.cx,
  134. context->if3.image2.image.cy);
  135. gs_blend_state_pop();
  136. gs_enable_framebuffer_srgb(previous);
  137. }
  138. static void image_source_tick(void *data, float seconds)
  139. {
  140. struct image_source *context = data;
  141. uint64_t frame_time = obs_get_video_frame_time();
  142. context->update_time_elapsed += seconds;
  143. if (obs_source_showing(context->source)) {
  144. if (context->update_time_elapsed >= 1.0f) {
  145. time_t t = get_modified_timestamp(context->file);
  146. context->update_time_elapsed = 0.0f;
  147. if (context->file_timestamp != t) {
  148. image_source_load(context);
  149. }
  150. }
  151. }
  152. if (obs_source_active(context->source)) {
  153. if (!context->active) {
  154. if (context->if3.image2.image.is_animated_gif)
  155. context->last_time = frame_time;
  156. context->active = true;
  157. }
  158. } else {
  159. if (context->active) {
  160. if (context->if3.image2.image.is_animated_gif) {
  161. context->if3.image2.image.cur_frame = 0;
  162. context->if3.image2.image.cur_loop = 0;
  163. context->if3.image2.image.cur_time = 0;
  164. obs_enter_graphics();
  165. gs_image_file3_update_texture(&context->if3);
  166. obs_leave_graphics();
  167. }
  168. context->active = false;
  169. }
  170. return;
  171. }
  172. if (context->last_time && context->if3.image2.image.is_animated_gif) {
  173. uint64_t elapsed = frame_time - context->last_time;
  174. bool updated = gs_image_file3_tick(&context->if3, elapsed);
  175. if (updated) {
  176. obs_enter_graphics();
  177. gs_image_file3_update_texture(&context->if3);
  178. obs_leave_graphics();
  179. }
  180. }
  181. context->last_time = frame_time;
  182. }
  183. static const char *image_filter =
  184. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd *.webp);;"
  185. "BMP Files (*.bmp);;"
  186. "Targa Files (*.tga);;"
  187. "PNG Files (*.png);;"
  188. "JPEG Files (*.jpeg *.jpg);;"
  189. "GIF Files (*.gif);;"
  190. "PSD Files (*.psd);;"
  191. "WebP Files (*.webp);;"
  192. "All Files (*.*)";
  193. static obs_properties_t *image_source_properties(void *data)
  194. {
  195. struct image_source *s = data;
  196. struct dstr path = {0};
  197. obs_properties_t *props = obs_properties_create();
  198. if (s && s->file && *s->file) {
  199. const char *slash;
  200. dstr_copy(&path, s->file);
  201. dstr_replace(&path, "\\", "/");
  202. slash = strrchr(path.array, '/');
  203. if (slash)
  204. dstr_resize(&path, slash - path.array + 1);
  205. }
  206. obs_properties_add_path(props, "file", obs_module_text("File"),
  207. OBS_PATH_FILE, image_filter, path.array);
  208. obs_properties_add_bool(props, "unload",
  209. obs_module_text("UnloadWhenNotShowing"));
  210. obs_properties_add_bool(props, "linear_alpha",
  211. obs_module_text("LinearAlpha"));
  212. dstr_free(&path);
  213. return props;
  214. }
  215. uint64_t image_source_get_memory_usage(void *data)
  216. {
  217. struct image_source *s = data;
  218. return s->if3.image2.mem_usage;
  219. }
  220. static void missing_file_callback(void *src, const char *new_path, void *data)
  221. {
  222. struct image_source *s = src;
  223. obs_source_t *source = s->source;
  224. obs_data_t *settings = obs_source_get_settings(source);
  225. obs_data_set_string(settings, "file", new_path);
  226. obs_source_update(source, settings);
  227. obs_data_release(settings);
  228. UNUSED_PARAMETER(data);
  229. }
  230. static obs_missing_files_t *image_source_missingfiles(void *data)
  231. {
  232. struct image_source *s = data;
  233. obs_missing_files_t *files = obs_missing_files_create();
  234. if (strcmp(s->file, "") != 0) {
  235. if (!os_file_exists(s->file)) {
  236. obs_missing_file_t *file = obs_missing_file_create(
  237. s->file, missing_file_callback,
  238. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  239. obs_missing_files_add_file(files, file);
  240. }
  241. }
  242. return files;
  243. }
  244. static struct obs_source_info image_source_info = {
  245. .id = "image_source",
  246. .type = OBS_SOURCE_TYPE_INPUT,
  247. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_SRGB,
  248. .get_name = image_source_get_name,
  249. .create = image_source_create,
  250. .destroy = image_source_destroy,
  251. .update = image_source_update,
  252. .get_defaults = image_source_defaults,
  253. .show = image_source_show,
  254. .hide = image_source_hide,
  255. .get_width = image_source_getwidth,
  256. .get_height = image_source_getheight,
  257. .video_render = image_source_render,
  258. .video_tick = image_source_tick,
  259. .missing_files = image_source_missingfiles,
  260. .get_properties = image_source_properties,
  261. .icon_type = OBS_ICON_TYPE_IMAGE,
  262. };
  263. OBS_DECLARE_MODULE()
  264. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  265. MODULE_EXPORT const char *obs_module_description(void)
  266. {
  267. return "Image/color/slideshow sources";
  268. }
  269. extern struct obs_source_info slideshow_info;
  270. extern struct obs_source_info color_source_info_v1;
  271. extern struct obs_source_info color_source_info_v2;
  272. extern struct obs_source_info color_source_info_v3;
  273. bool obs_module_load(void)
  274. {
  275. obs_register_source(&image_source_info);
  276. obs_register_source(&color_source_info_v1);
  277. obs_register_source(&color_source_info_v2);
  278. obs_register_source(&color_source_info_v3);
  279. obs_register_source(&slideshow_info);
  280. return true;
  281. }