image-source.c 6.6 KB

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