image-source.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <obs-module.h>
  2. #include <graphics/image-file.h>
  3. #include <util/platform.h>
  4. #include <sys/stat.h>
  5. #define blog(log_level, format, ...) \
  6. blog(log_level, "[image_source: '%s'] " format, \
  7. obs_source_get_name(context->source), ##__VA_ARGS__)
  8. #define debug(format, ...) \
  9. blog(LOG_DEBUG, format, ##__VA_ARGS__)
  10. #define info(format, ...) \
  11. blog(LOG_INFO, format, ##__VA_ARGS__)
  12. #define warn(format, ...) \
  13. blog(LOG_WARNING, format, ##__VA_ARGS__)
  14. struct image_source {
  15. obs_source_t *source;
  16. char *file;
  17. bool persistent;
  18. time_t file_timestamp;
  19. float update_time_elapsed;
  20. uint64_t last_time;
  21. gs_image_file_t image;
  22. };
  23. static time_t get_modified_timestamp(const char *filename)
  24. {
  25. struct stat stats;
  26. stat(filename, &stats);
  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_file_free(&context->image);
  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_file_init(&context->image, file);
  44. context->update_time_elapsed = 0;
  45. obs_enter_graphics();
  46. gs_image_file_init_texture(&context->image);
  47. obs_leave_graphics();
  48. if (!context->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_file_free(&context->image);
  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->image.cx;
  108. }
  109. static uint32_t image_source_getheight(void *data)
  110. {
  111. struct image_source *context = data;
  112. return context->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->image.texture)
  118. return;
  119. gs_reset_blend_state();
  120. gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
  121. context->image.texture);
  122. gs_draw_sprite(context->image.texture, 0,
  123. context->image.cx, context->image.cy);
  124. }
  125. static void image_source_tick(void *data, float seconds)
  126. {
  127. struct image_source *context = data;
  128. uint64_t frame_time = obs_get_video_frame_time();
  129. if (context->last_time && context->image.is_animated_gif) {
  130. uint64_t elapsed = frame_time - context->last_time;
  131. bool updated = gs_image_file_tick(&context->image, elapsed);
  132. if (updated) {
  133. obs_enter_graphics();
  134. gs_image_file_update_texture(&context->image);
  135. obs_leave_graphics();
  136. }
  137. }
  138. context->last_time = frame_time;
  139. if (!obs_source_showing(context->source)) return;
  140. context->update_time_elapsed += seconds;
  141. if (context->update_time_elapsed >= 1.0f) {
  142. time_t t = get_modified_timestamp(context->file);
  143. context->update_time_elapsed = 0.0f;
  144. if (context->file_timestamp < t) {
  145. image_source_load(context);
  146. }
  147. }
  148. }
  149. static const char *image_filter =
  150. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif);;"
  151. "BMP Files (*.bmp);;"
  152. "Targa Files (*.tga);;"
  153. "PNG Files (*.png);;"
  154. "JPEG Files (*.jpeg *.jpg);;"
  155. "GIF Files (*.gif)";
  156. static obs_properties_t *image_source_properties(void *unused)
  157. {
  158. UNUSED_PARAMETER(unused);
  159. obs_properties_t *props = obs_properties_create();
  160. obs_properties_add_path(props,
  161. "file", obs_module_text("File"),
  162. OBS_PATH_FILE, image_filter, NULL);
  163. obs_properties_add_bool(props,
  164. "unload", obs_module_text("UnloadWhenNotShowing"));
  165. return props;
  166. }
  167. static struct obs_source_info image_source_info = {
  168. .id = "image_source",
  169. .type = OBS_SOURCE_TYPE_INPUT,
  170. .output_flags = OBS_SOURCE_VIDEO,
  171. .get_name = image_source_get_name,
  172. .create = image_source_create,
  173. .destroy = image_source_destroy,
  174. .update = image_source_update,
  175. .get_defaults = image_source_defaults,
  176. .show = image_source_show,
  177. .hide = image_source_hide,
  178. .get_width = image_source_getwidth,
  179. .get_height = image_source_getheight,
  180. .video_render = image_source_render,
  181. .video_tick = image_source_tick,
  182. .get_properties = image_source_properties
  183. };
  184. OBS_DECLARE_MODULE()
  185. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  186. bool obs_module_load(void)
  187. {
  188. obs_register_source(&image_source_info);
  189. return true;
  190. }