image-source.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  120. gs_technique_t *tech =
  121. gs_effect_get_technique(effect, "DrawNonlinearAlpha");
  122. const bool previous = gs_framebuffer_srgb_enabled();
  123. gs_enable_framebuffer_srgb(true);
  124. gs_blend_state_push();
  125. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  126. gs_eparam_t *const param = gs_effect_get_param_by_name(effect, "image");
  127. gs_effect_set_texture_srgb(param, context->if2.image.texture);
  128. size_t passes = gs_technique_begin(tech);
  129. for (size_t i = 0; i < passes; i++) {
  130. gs_technique_begin_pass(tech, i);
  131. gs_draw_sprite(context->if2.image.texture, 0,
  132. context->if2.image.cx, context->if2.image.cy);
  133. gs_technique_end_pass(tech);
  134. }
  135. gs_technique_end(tech);
  136. gs_blend_state_pop();
  137. gs_enable_framebuffer_srgb(previous);
  138. }
  139. static void image_source_tick(void *data, float seconds)
  140. {
  141. struct image_source *context = data;
  142. uint64_t frame_time = obs_get_video_frame_time();
  143. context->update_time_elapsed += seconds;
  144. if (obs_source_showing(context->source)) {
  145. if (context->update_time_elapsed >= 1.0f) {
  146. time_t t = get_modified_timestamp(context->file);
  147. context->update_time_elapsed = 0.0f;
  148. if (context->file_timestamp != t) {
  149. image_source_load(context);
  150. }
  151. }
  152. }
  153. if (obs_source_active(context->source)) {
  154. if (!context->active) {
  155. if (context->if2.image.is_animated_gif)
  156. context->last_time = frame_time;
  157. context->active = true;
  158. }
  159. } else {
  160. if (context->active) {
  161. if (context->if2.image.is_animated_gif) {
  162. context->if2.image.cur_frame = 0;
  163. context->if2.image.cur_loop = 0;
  164. context->if2.image.cur_time = 0;
  165. obs_enter_graphics();
  166. gs_image_file2_update_texture(&context->if2);
  167. obs_leave_graphics();
  168. }
  169. context->active = false;
  170. }
  171. return;
  172. }
  173. if (context->last_time && context->if2.image.is_animated_gif) {
  174. uint64_t elapsed = frame_time - context->last_time;
  175. bool updated = gs_image_file2_tick(&context->if2, elapsed);
  176. if (updated) {
  177. obs_enter_graphics();
  178. gs_image_file2_update_texture(&context->if2);
  179. obs_leave_graphics();
  180. }
  181. }
  182. context->last_time = frame_time;
  183. }
  184. static const char *image_filter =
  185. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd *.webp);;"
  186. "BMP Files (*.bmp);;"
  187. "Targa Files (*.tga);;"
  188. "PNG Files (*.png);;"
  189. "JPEG Files (*.jpeg *.jpg);;"
  190. "GIF Files (*.gif);;"
  191. "PSD Files (*.psd);;"
  192. "WebP Files (*.webp);;"
  193. "All Files (*.*)";
  194. static obs_properties_t *image_source_properties(void *data)
  195. {
  196. struct image_source *s = data;
  197. struct dstr path = {0};
  198. obs_properties_t *props = obs_properties_create();
  199. if (s && s->file && *s->file) {
  200. const char *slash;
  201. dstr_copy(&path, s->file);
  202. dstr_replace(&path, "\\", "/");
  203. slash = strrchr(path.array, '/');
  204. if (slash)
  205. dstr_resize(&path, slash - path.array + 1);
  206. }
  207. obs_properties_add_path(props, "file", obs_module_text("File"),
  208. OBS_PATH_FILE, image_filter, path.array);
  209. obs_properties_add_bool(props, "unload",
  210. obs_module_text("UnloadWhenNotShowing"));
  211. dstr_free(&path);
  212. return props;
  213. }
  214. uint64_t image_source_get_memory_usage(void *data)
  215. {
  216. struct image_source *s = data;
  217. return s->if2.mem_usage;
  218. }
  219. static void missing_file_callback(void *src, const char *new_path, void *data)
  220. {
  221. struct image_source *s = src;
  222. obs_source_t *source = s->source;
  223. obs_data_t *settings = obs_source_get_settings(source);
  224. obs_data_set_string(settings, "file", new_path);
  225. obs_source_update(source, settings);
  226. obs_data_release(settings);
  227. UNUSED_PARAMETER(data);
  228. }
  229. static obs_missing_files_t *image_source_missingfiles(void *data)
  230. {
  231. struct image_source *s = data;
  232. obs_missing_files_t *files = obs_missing_files_create();
  233. if (strcmp(s->file, "") != 0) {
  234. if (!os_file_exists(s->file)) {
  235. obs_missing_file_t *file = obs_missing_file_create(
  236. s->file, missing_file_callback,
  237. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  238. obs_missing_files_add_file(files, file);
  239. }
  240. }
  241. return files;
  242. }
  243. static struct obs_source_info image_source_info = {
  244. .id = "image_source",
  245. .type = OBS_SOURCE_TYPE_INPUT,
  246. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW,
  247. .get_name = image_source_get_name,
  248. .create = image_source_create,
  249. .destroy = image_source_destroy,
  250. .update = image_source_update,
  251. .get_defaults = image_source_defaults,
  252. .show = image_source_show,
  253. .hide = image_source_hide,
  254. .get_width = image_source_getwidth,
  255. .get_height = image_source_getheight,
  256. .video_render = image_source_render,
  257. .video_tick = image_source_tick,
  258. .missing_files = image_source_missingfiles,
  259. .get_properties = image_source_properties,
  260. .icon_type = OBS_ICON_TYPE_IMAGE,
  261. };
  262. OBS_DECLARE_MODULE()
  263. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  264. MODULE_EXPORT const char *obs_module_description(void)
  265. {
  266. return "Image/color/slideshow sources";
  267. }
  268. extern struct obs_source_info slideshow_info;
  269. extern struct obs_source_info color_source_info_v1;
  270. extern struct obs_source_info color_source_info_v2;
  271. extern struct obs_source_info color_source_info_v3;
  272. bool obs_module_load(void)
  273. {
  274. obs_register_source(&image_source_info);
  275. obs_register_source(&color_source_info_v1);
  276. obs_register_source(&color_source_info_v2);
  277. obs_register_source(&color_source_info_v3);
  278. obs_register_source(&slideshow_info);
  279. return true;
  280. }