image-source.c 9.0 KB

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