image-source.c 6.9 KB

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