image-source.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if (os_stat(filename, &stats) != 0)
  27. return -1;
  28. return stats.st_mtime;
  29. }
  30. static const char *image_source_get_name(void *unused)
  31. {
  32. UNUSED_PARAMETER(unused);
  33. return obs_module_text("ImageInput");
  34. }
  35. static void image_source_load(struct image_source *context)
  36. {
  37. char *file = context->file;
  38. obs_enter_graphics();
  39. gs_image_file_free(&context->image);
  40. obs_leave_graphics();
  41. if (file && *file) {
  42. debug("loading texture '%s'", file);
  43. context->file_timestamp = get_modified_timestamp(file);
  44. gs_image_file_init(&context->image, file);
  45. context->update_time_elapsed = 0;
  46. obs_enter_graphics();
  47. gs_image_file_init_texture(&context->image);
  48. obs_leave_graphics();
  49. if (!context->image.loaded)
  50. warn("failed to load texture '%s'", file);
  51. }
  52. }
  53. static void image_source_unload(struct image_source *context)
  54. {
  55. obs_enter_graphics();
  56. gs_image_file_free(&context->image);
  57. obs_leave_graphics();
  58. }
  59. static void image_source_update(void *data, obs_data_t *settings)
  60. {
  61. struct image_source *context = data;
  62. const char *file = obs_data_get_string(settings, "file");
  63. const bool unload = obs_data_get_bool(settings, "unload");
  64. if (context->file)
  65. bfree(context->file);
  66. context->file = bstrdup(file);
  67. context->persistent = !unload;
  68. /* Load the image if the source is persistent or showing */
  69. if (context->persistent || obs_source_showing(context->source))
  70. image_source_load(data);
  71. else
  72. image_source_unload(data);
  73. }
  74. static void image_source_defaults(obs_data_t *settings)
  75. {
  76. obs_data_set_default_bool(settings, "unload", false);
  77. }
  78. static void image_source_show(void *data)
  79. {
  80. struct image_source *context = data;
  81. if (!context->persistent)
  82. image_source_load(context);
  83. }
  84. static void image_source_hide(void *data)
  85. {
  86. struct image_source *context = data;
  87. if (!context->persistent)
  88. image_source_unload(context);
  89. }
  90. static void *image_source_create(obs_data_t *settings, obs_source_t *source)
  91. {
  92. struct image_source *context = bzalloc(sizeof(struct image_source));
  93. context->source = source;
  94. image_source_update(context, settings);
  95. return context;
  96. }
  97. static void image_source_destroy(void *data)
  98. {
  99. struct image_source *context = data;
  100. image_source_unload(context);
  101. if (context->file)
  102. bfree(context->file);
  103. bfree(context);
  104. }
  105. static uint32_t image_source_getwidth(void *data)
  106. {
  107. struct image_source *context = data;
  108. return context->image.cx;
  109. }
  110. static uint32_t image_source_getheight(void *data)
  111. {
  112. struct image_source *context = data;
  113. return context->image.cy;
  114. }
  115. static void image_source_render(void *data, gs_effect_t *effect)
  116. {
  117. struct image_source *context = data;
  118. if (!context->image.texture)
  119. return;
  120. gs_reset_blend_state();
  121. gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
  122. context->image.texture);
  123. gs_draw_sprite(context->image.texture, 0,
  124. context->image.cx, context->image.cy);
  125. }
  126. static void image_source_tick(void *data, float seconds)
  127. {
  128. struct image_source *context = data;
  129. uint64_t frame_time = obs_get_video_frame_time();
  130. if (context->last_time && context->image.is_animated_gif) {
  131. uint64_t elapsed = frame_time - context->last_time;
  132. bool updated = gs_image_file_tick(&context->image, elapsed);
  133. if (updated) {
  134. obs_enter_graphics();
  135. gs_image_file_update_texture(&context->image);
  136. obs_leave_graphics();
  137. }
  138. }
  139. context->last_time = frame_time;
  140. if (!obs_source_showing(context->source)) return;
  141. context->update_time_elapsed += seconds;
  142. if (context->update_time_elapsed >= 1.0f) {
  143. time_t t = get_modified_timestamp(context->file);
  144. context->update_time_elapsed = 0.0f;
  145. if (context->file_timestamp < t) {
  146. image_source_load(context);
  147. }
  148. }
  149. }
  150. static const char *image_filter =
  151. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif);;"
  152. "BMP Files (*.bmp);;"
  153. "Targa Files (*.tga);;"
  154. "PNG Files (*.png);;"
  155. "JPEG Files (*.jpeg *.jpg);;"
  156. "GIF Files (*.gif)";
  157. static obs_properties_t *image_source_properties(void *unused)
  158. {
  159. UNUSED_PARAMETER(unused);
  160. obs_properties_t *props = obs_properties_create();
  161. obs_properties_add_path(props,
  162. "file", obs_module_text("File"),
  163. OBS_PATH_FILE, image_filter, NULL);
  164. obs_properties_add_bool(props,
  165. "unload", obs_module_text("UnloadWhenNotShowing"));
  166. return props;
  167. }
  168. static struct obs_source_info image_source_info = {
  169. .id = "image_source",
  170. .type = OBS_SOURCE_TYPE_INPUT,
  171. .output_flags = OBS_SOURCE_VIDEO,
  172. .get_name = image_source_get_name,
  173. .create = image_source_create,
  174. .destroy = image_source_destroy,
  175. .update = image_source_update,
  176. .get_defaults = image_source_defaults,
  177. .show = image_source_show,
  178. .hide = image_source_hide,
  179. .get_width = image_source_getwidth,
  180. .get_height = image_source_getheight,
  181. .video_render = image_source_render,
  182. .video_tick = image_source_tick,
  183. .get_properties = image_source_properties
  184. };
  185. OBS_DECLARE_MODULE()
  186. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  187. bool obs_module_load(void)
  188. {
  189. obs_register_source(&image_source_info);
  190. return true;
  191. }