image-source.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <obs-module.h>
  2. #include <util/platform.h>
  3. #include <sys/stat.h>
  4. #define blog(log_level, format, ...) \
  5. blog(log_level, "[image_source: '%s'] " format, \
  6. obs_source_get_name(context->source), ##__VA_ARGS__)
  7. #define debug(format, ...) \
  8. blog(LOG_DEBUG, format, ##__VA_ARGS__)
  9. #define info(format, ...) \
  10. blog(LOG_INFO, format, ##__VA_ARGS__)
  11. #define warn(format, ...) \
  12. blog(LOG_WARNING, format, ##__VA_ARGS__)
  13. struct image_source {
  14. obs_source_t *source;
  15. char *file;
  16. bool persistent;
  17. time_t file_timestamp;
  18. float update_time_elapsed;
  19. gs_texture_t *tex;
  20. uint32_t cx;
  21. uint32_t cy;
  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. if (context->tex)
  39. gs_texture_destroy(context->tex);
  40. context->tex = NULL;
  41. if (file && *file) {
  42. debug("loading texture '%s'", file);
  43. context->file_timestamp = get_modified_timestamp(file);
  44. context->tex = gs_texture_create_from_file(file);
  45. context->update_time_elapsed = 0;
  46. if (context->tex) {
  47. context->cx = gs_texture_get_width(context->tex);
  48. context->cy = gs_texture_get_height(context->tex);
  49. } else {
  50. warn("failed to load texture '%s'", file);
  51. context->cx = 0;
  52. context->cy = 0;
  53. }
  54. }
  55. obs_leave_graphics();
  56. }
  57. static void image_source_unload(struct image_source *context)
  58. {
  59. obs_enter_graphics();
  60. if (context->tex)
  61. gs_texture_destroy(context->tex);
  62. context->tex = NULL;
  63. obs_leave_graphics();
  64. }
  65. static void image_source_update(void *data, obs_data_t *settings)
  66. {
  67. struct image_source *context = data;
  68. const char *file = obs_data_get_string(settings, "file");
  69. const bool unload = obs_data_get_bool(settings, "unload");
  70. if (context->file)
  71. bfree(context->file);
  72. context->file = bstrdup(file);
  73. context->persistent = !unload;
  74. /* Load the image if the source is persistent or showing */
  75. if (context->persistent || obs_source_showing(context->source))
  76. image_source_load(data);
  77. else
  78. image_source_unload(data);
  79. }
  80. static void image_source_defaults(obs_data_t *settings)
  81. {
  82. obs_data_set_default_bool(settings, "unload", false);
  83. }
  84. static void image_source_show(void *data)
  85. {
  86. struct image_source *context = data;
  87. if (!context->persistent)
  88. image_source_load(context);
  89. }
  90. static void image_source_hide(void *data)
  91. {
  92. struct image_source *context = data;
  93. if (!context->persistent)
  94. image_source_unload(context);
  95. }
  96. static void *image_source_create(obs_data_t *settings, obs_source_t *source)
  97. {
  98. struct image_source *context = bzalloc(sizeof(struct image_source));
  99. context->source = source;
  100. image_source_update(context, settings);
  101. return context;
  102. }
  103. static void image_source_destroy(void *data)
  104. {
  105. struct image_source *context = data;
  106. image_source_unload(context);
  107. if (context->file)
  108. bfree(context->file);
  109. bfree(context);
  110. }
  111. static uint32_t image_source_getwidth(void *data)
  112. {
  113. struct image_source *context = data;
  114. return context->cx;
  115. }
  116. static uint32_t image_source_getheight(void *data)
  117. {
  118. struct image_source *context = data;
  119. return context->cy;
  120. }
  121. static void image_source_render(void *data, gs_effect_t *effect)
  122. {
  123. struct image_source *context = data;
  124. if (!context->tex)
  125. return;
  126. gs_reset_blend_state();
  127. gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
  128. context->tex);
  129. gs_draw_sprite(context->tex, 0, context->cx, context->cy);
  130. }
  131. static void image_source_tick(void *data, float seconds)
  132. {
  133. struct image_source *context = data;
  134. if (!obs_source_showing(context->source)) return;
  135. context->update_time_elapsed += seconds;
  136. if (context->update_time_elapsed >= 1.0f) {
  137. time_t t = get_modified_timestamp(context->file);
  138. context->update_time_elapsed = 0.0f;
  139. if (context->file_timestamp < t) {
  140. image_source_load(context);
  141. }
  142. }
  143. }
  144. static const char *image_filter =
  145. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif);;"
  146. "BMP Files (*.bmp);;"
  147. "Targa Files (*.tga);;"
  148. "PNG Files (*.png);;"
  149. "JPEG Files (*.jpeg *.jpg);;"
  150. "GIF Files (*.gif)";
  151. static obs_properties_t *image_source_properties(void *unused)
  152. {
  153. UNUSED_PARAMETER(unused);
  154. obs_properties_t *props = obs_properties_create();
  155. obs_properties_add_path(props,
  156. "file", obs_module_text("File"),
  157. OBS_PATH_FILE, image_filter, NULL);
  158. obs_properties_add_bool(props,
  159. "unload", obs_module_text("UnloadWhenNotShowing"));
  160. return props;
  161. }
  162. static struct obs_source_info image_source_info = {
  163. .id = "image_source",
  164. .type = OBS_SOURCE_TYPE_INPUT,
  165. .output_flags = OBS_SOURCE_VIDEO,
  166. .get_name = image_source_get_name,
  167. .create = image_source_create,
  168. .destroy = image_source_destroy,
  169. .update = image_source_update,
  170. .get_defaults = image_source_defaults,
  171. .show = image_source_show,
  172. .hide = image_source_hide,
  173. .get_width = image_source_getwidth,
  174. .get_height = image_source_getheight,
  175. .video_render = image_source_render,
  176. .video_tick = image_source_tick,
  177. .get_properties = image_source_properties
  178. };
  179. OBS_DECLARE_MODULE()
  180. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  181. bool obs_module_load(void)
  182. {
  183. obs_register_source(&image_source_info);
  184. return true;
  185. }