image-source.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. time_t file_timestamp;
  17. float update_time_elapsed;
  18. uint64_t last_time;
  19. bool active;
  20. gs_image_file2_t if2;
  21. };
  22. static time_t get_modified_timestamp(const char *filename)
  23. {
  24. struct stat stats;
  25. if (os_stat(filename, &stats) != 0)
  26. return -1;
  27. return stats.st_mtime;
  28. }
  29. static const char *image_source_get_name(void *unused)
  30. {
  31. UNUSED_PARAMETER(unused);
  32. return obs_module_text("ImageInput");
  33. }
  34. static void image_source_load(struct image_source *context)
  35. {
  36. char *file = context->file;
  37. obs_enter_graphics();
  38. gs_image_file2_free(&context->if2);
  39. obs_leave_graphics();
  40. if (file && *file) {
  41. debug("loading texture '%s'", file);
  42. context->file_timestamp = get_modified_timestamp(file);
  43. gs_image_file2_init(&context->if2, file);
  44. context->update_time_elapsed = 0;
  45. obs_enter_graphics();
  46. gs_image_file2_init_texture(&context->if2);
  47. obs_leave_graphics();
  48. if (!context->if2.image.loaded)
  49. warn("failed to load texture '%s'", file);
  50. }
  51. }
  52. static void image_source_unload(struct image_source *context)
  53. {
  54. obs_enter_graphics();
  55. gs_image_file2_free(&context->if2);
  56. obs_leave_graphics();
  57. }
  58. static void image_source_update(void *data, obs_data_t *settings)
  59. {
  60. struct image_source *context = data;
  61. const char *file = obs_data_get_string(settings, "file");
  62. const bool unload = obs_data_get_bool(settings, "unload");
  63. if (context->file)
  64. bfree(context->file);
  65. context->file = bstrdup(file);
  66. context->persistent = !unload;
  67. /* Load the image if the source is persistent or showing */
  68. if (context->persistent || obs_source_showing(context->source))
  69. image_source_load(data);
  70. else
  71. image_source_unload(data);
  72. }
  73. static void image_source_defaults(obs_data_t *settings)
  74. {
  75. obs_data_set_default_bool(settings, "unload", false);
  76. }
  77. static void image_source_show(void *data)
  78. {
  79. struct image_source *context = data;
  80. if (!context->persistent)
  81. image_source_load(context);
  82. }
  83. static void image_source_hide(void *data)
  84. {
  85. struct image_source *context = data;
  86. if (!context->persistent)
  87. image_source_unload(context);
  88. }
  89. static void *image_source_create(obs_data_t *settings, obs_source_t *source)
  90. {
  91. struct image_source *context = bzalloc(sizeof(struct image_source));
  92. context->source = source;
  93. image_source_update(context, settings);
  94. return context;
  95. }
  96. static void image_source_destroy(void *data)
  97. {
  98. struct image_source *context = data;
  99. image_source_unload(context);
  100. if (context->file)
  101. bfree(context->file);
  102. bfree(context);
  103. }
  104. static uint32_t image_source_getwidth(void *data)
  105. {
  106. struct image_source *context = data;
  107. return context->if2.image.cx;
  108. }
  109. static uint32_t image_source_getheight(void *data)
  110. {
  111. struct image_source *context = data;
  112. return context->if2.image.cy;
  113. }
  114. static void image_source_render(void *data, gs_effect_t *effect)
  115. {
  116. struct image_source *context = data;
  117. if (!context->if2.image.texture)
  118. return;
  119. const bool linear_srgb = gs_get_linear_srgb();
  120. const bool previous = gs_framebuffer_srgb_enabled();
  121. gs_enable_framebuffer_srgb(linear_srgb);
  122. gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image");
  123. if (linear_srgb)
  124. gs_effect_set_texture_srgb(param, context->if2.image.texture);
  125. else
  126. gs_effect_set_texture(param, context->if2.image.texture);
  127. gs_draw_sprite(context->if2.image.texture, 0, context->if2.image.cx,
  128. context->if2.image.cy);
  129. gs_enable_framebuffer_srgb(previous);
  130. }
  131. static void image_source_tick(void *data, float seconds)
  132. {
  133. struct image_source *context = data;
  134. uint64_t frame_time = obs_get_video_frame_time();
  135. context->update_time_elapsed += seconds;
  136. if (obs_source_showing(context->source)) {
  137. if (context->update_time_elapsed >= 1.0f) {
  138. time_t t = get_modified_timestamp(context->file);
  139. context->update_time_elapsed = 0.0f;
  140. if (context->file_timestamp != t) {
  141. image_source_load(context);
  142. }
  143. }
  144. }
  145. if (obs_source_active(context->source)) {
  146. if (!context->active) {
  147. if (context->if2.image.is_animated_gif)
  148. context->last_time = frame_time;
  149. context->active = true;
  150. }
  151. } else {
  152. if (context->active) {
  153. if (context->if2.image.is_animated_gif) {
  154. context->if2.image.cur_frame = 0;
  155. context->if2.image.cur_loop = 0;
  156. context->if2.image.cur_time = 0;
  157. obs_enter_graphics();
  158. gs_image_file2_update_texture(&context->if2);
  159. obs_leave_graphics();
  160. }
  161. context->active = false;
  162. }
  163. return;
  164. }
  165. if (context->last_time && context->if2.image.is_animated_gif) {
  166. uint64_t elapsed = frame_time - context->last_time;
  167. bool updated = gs_image_file2_tick(&context->if2, elapsed);
  168. if (updated) {
  169. obs_enter_graphics();
  170. gs_image_file2_update_texture(&context->if2);
  171. obs_leave_graphics();
  172. }
  173. }
  174. context->last_time = frame_time;
  175. }
  176. static const char *image_filter =
  177. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd *.webp);;"
  178. "BMP Files (*.bmp);;"
  179. "Targa Files (*.tga);;"
  180. "PNG Files (*.png);;"
  181. "JPEG Files (*.jpeg *.jpg);;"
  182. "GIF Files (*.gif);;"
  183. "PSD Files (*.psd);;"
  184. "WebP Files (*.webp);;"
  185. "All Files (*.*)";
  186. static obs_properties_t *image_source_properties(void *data)
  187. {
  188. struct image_source *s = data;
  189. struct dstr path = {0};
  190. obs_properties_t *props = obs_properties_create();
  191. if (s && s->file && *s->file) {
  192. const char *slash;
  193. dstr_copy(&path, s->file);
  194. dstr_replace(&path, "\\", "/");
  195. slash = strrchr(path.array, '/');
  196. if (slash)
  197. dstr_resize(&path, slash - path.array + 1);
  198. }
  199. obs_properties_add_path(props, "file", obs_module_text("File"),
  200. OBS_PATH_FILE, image_filter, path.array);
  201. obs_properties_add_bool(props, "unload",
  202. obs_module_text("UnloadWhenNotShowing"));
  203. dstr_free(&path);
  204. return props;
  205. }
  206. uint64_t image_source_get_memory_usage(void *data)
  207. {
  208. struct image_source *s = data;
  209. return s->if2.mem_usage;
  210. }
  211. static struct obs_source_info image_source_info = {
  212. .id = "image_source",
  213. .type = OBS_SOURCE_TYPE_INPUT,
  214. .output_flags = OBS_SOURCE_VIDEO,
  215. .get_name = image_source_get_name,
  216. .create = image_source_create,
  217. .destroy = image_source_destroy,
  218. .update = image_source_update,
  219. .get_defaults = image_source_defaults,
  220. .show = image_source_show,
  221. .hide = image_source_hide,
  222. .get_width = image_source_getwidth,
  223. .get_height = image_source_getheight,
  224. .video_render = image_source_render,
  225. .video_tick = image_source_tick,
  226. .get_properties = image_source_properties,
  227. .icon_type = OBS_ICON_TYPE_IMAGE,
  228. };
  229. OBS_DECLARE_MODULE()
  230. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  231. MODULE_EXPORT const char *obs_module_description(void)
  232. {
  233. return "Image/color/slideshow sources";
  234. }
  235. extern struct obs_source_info slideshow_info;
  236. extern struct obs_source_info color_source_info_v1;
  237. extern struct obs_source_info color_source_info_v2;
  238. extern struct obs_source_info color_source_info_v3;
  239. bool obs_module_load(void)
  240. {
  241. obs_register_source(&image_source_info);
  242. obs_register_source(&color_source_info_v1);
  243. obs_register_source(&color_source_info_v2);
  244. obs_register_source(&color_source_info_v3);
  245. obs_register_source(&slideshow_info);
  246. return true;
  247. }