image-source.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
  120. context->if2.image.texture);
  121. gs_draw_sprite(context->if2.image.texture, 0, context->if2.image.cx,
  122. context->if2.image.cy);
  123. }
  124. static void image_source_tick(void *data, float seconds)
  125. {
  126. struct image_source *context = data;
  127. uint64_t frame_time = obs_get_video_frame_time();
  128. context->update_time_elapsed += seconds;
  129. if (context->update_time_elapsed >= 1.0f) {
  130. time_t t = get_modified_timestamp(context->file);
  131. context->update_time_elapsed = 0.0f;
  132. if (context->file_timestamp != t) {
  133. image_source_load(context);
  134. }
  135. }
  136. if (obs_source_active(context->source)) {
  137. if (!context->active) {
  138. if (context->if2.image.is_animated_gif)
  139. context->last_time = frame_time;
  140. context->active = true;
  141. }
  142. } else {
  143. if (context->active) {
  144. if (context->if2.image.is_animated_gif) {
  145. context->if2.image.cur_frame = 0;
  146. context->if2.image.cur_loop = 0;
  147. context->if2.image.cur_time = 0;
  148. obs_enter_graphics();
  149. gs_image_file2_update_texture(&context->if2);
  150. obs_leave_graphics();
  151. }
  152. context->active = false;
  153. }
  154. return;
  155. }
  156. if (context->last_time && context->if2.image.is_animated_gif) {
  157. uint64_t elapsed = frame_time - context->last_time;
  158. bool updated = gs_image_file2_tick(&context->if2, elapsed);
  159. if (updated) {
  160. obs_enter_graphics();
  161. gs_image_file2_update_texture(&context->if2);
  162. obs_leave_graphics();
  163. }
  164. }
  165. context->last_time = frame_time;
  166. }
  167. static const char *image_filter =
  168. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif *.psd);;"
  169. "BMP Files (*.bmp);;"
  170. "Targa Files (*.tga);;"
  171. "PNG Files (*.png);;"
  172. "JPEG Files (*.jpeg *.jpg);;"
  173. "GIF Files (*.gif);;"
  174. "PSD Files (*.psd);;"
  175. "All Files (*.*)";
  176. static obs_properties_t *image_source_properties(void *data)
  177. {
  178. struct image_source *s = data;
  179. struct dstr path = {0};
  180. obs_properties_t *props = obs_properties_create();
  181. if (s && s->file && *s->file) {
  182. const char *slash;
  183. dstr_copy(&path, s->file);
  184. dstr_replace(&path, "\\", "/");
  185. slash = strrchr(path.array, '/');
  186. if (slash)
  187. dstr_resize(&path, slash - path.array + 1);
  188. }
  189. obs_properties_add_path(props, "file", obs_module_text("File"),
  190. OBS_PATH_FILE, image_filter, path.array);
  191. obs_properties_add_bool(props, "unload",
  192. obs_module_text("UnloadWhenNotShowing"));
  193. dstr_free(&path);
  194. return props;
  195. }
  196. uint64_t image_source_get_memory_usage(void *data)
  197. {
  198. struct image_source *s = data;
  199. return s->if2.mem_usage;
  200. }
  201. static struct obs_source_info image_source_info = {
  202. .id = "image_source",
  203. .type = OBS_SOURCE_TYPE_INPUT,
  204. .output_flags = OBS_SOURCE_VIDEO,
  205. .get_name = image_source_get_name,
  206. .create = image_source_create,
  207. .destroy = image_source_destroy,
  208. .update = image_source_update,
  209. .get_defaults = image_source_defaults,
  210. .show = image_source_show,
  211. .hide = image_source_hide,
  212. .get_width = image_source_getwidth,
  213. .get_height = image_source_getheight,
  214. .video_render = image_source_render,
  215. .video_tick = image_source_tick,
  216. .get_properties = image_source_properties,
  217. .icon_type = OBS_ICON_TYPE_IMAGE,
  218. };
  219. OBS_DECLARE_MODULE()
  220. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  221. MODULE_EXPORT const char *obs_module_description(void)
  222. {
  223. return "Image/color/slideshow sources";
  224. }
  225. extern struct obs_source_info slideshow_info;
  226. extern struct obs_source_info color_source_info_v1;
  227. extern struct obs_source_info color_source_info_v2;
  228. bool obs_module_load(void)
  229. {
  230. obs_register_source(&image_source_info);
  231. obs_register_source(&color_source_info_v1);
  232. obs_register_source(&color_source_info_v2);
  233. obs_register_source(&slideshow_info);
  234. return true;
  235. }