image-source.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <obs-module.h>
  2. #define warn(format, ...) \
  3. blog(LOG_WARNING, "[image_source: '%s'] " format, \
  4. obs_source_get_name(context->source), ##__VA_ARGS__)
  5. struct image_source {
  6. obs_source_t source;
  7. gs_texture_t tex;
  8. uint32_t cx;
  9. uint32_t cy;
  10. };
  11. static const char *image_source_get_name(void)
  12. {
  13. return obs_module_text("ImageInput");
  14. }
  15. static void image_source_update(void *data, obs_data_t settings)
  16. {
  17. struct image_source *context = data;
  18. const char *file = obs_data_get_string(settings, "file");
  19. obs_enter_graphics();
  20. if (context->tex) {
  21. gs_texture_destroy(context->tex);
  22. context->tex = NULL;
  23. }
  24. if (file && *file) {
  25. context->tex = gs_texture_create_from_file(file);
  26. if (context->tex) {
  27. context->cx = gs_texture_get_width(context->tex);
  28. context->cy = gs_texture_get_height(context->tex);
  29. } else {
  30. warn("failed to load texture '%s'", file);
  31. context->cx = 0;
  32. context->cy = 0;
  33. }
  34. }
  35. obs_leave_graphics();
  36. }
  37. static void *image_source_create(obs_data_t settings, obs_source_t source)
  38. {
  39. struct image_source *context = bzalloc(sizeof(struct image_source));
  40. context->source = source;
  41. image_source_update(context, settings);
  42. return context;
  43. }
  44. static void image_source_destroy(void *data)
  45. {
  46. struct image_source *context = data;
  47. obs_enter_graphics();
  48. gs_texture_destroy(context->tex);
  49. obs_leave_graphics();
  50. bfree(context);
  51. }
  52. static uint32_t image_source_getwidth(void *data)
  53. {
  54. struct image_source *context = data;
  55. return context->cx;
  56. }
  57. static uint32_t image_source_getheight(void *data)
  58. {
  59. struct image_source *context = data;
  60. return context->cy;
  61. }
  62. static void image_source_render(void *data, gs_effect_t effect)
  63. {
  64. struct image_source *context = data;
  65. if (!context->tex)
  66. return;
  67. gs_reset_blend_state();
  68. gs_effect_set_texture(gs_effect_get_param_by_name(effect, "image"),
  69. context->tex);
  70. gs_draw_sprite(context->tex, 0, context->cx, context->cy);
  71. }
  72. static const char *image_filter =
  73. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif);;"
  74. "BMP Files (*.bmp);;"
  75. "Targa Files (*.tga);;"
  76. "PNG Files (*.png);;"
  77. "JPEG Files (*.jpeg *.jpg);;"
  78. "GIF Files (*.gif)";
  79. static obs_properties_t image_source_properties(void)
  80. {
  81. obs_properties_t props = obs_properties_create();
  82. obs_properties_add_path(props,
  83. "file", obs_module_text("File"),
  84. OBS_PATH_FILE, image_filter, NULL);
  85. return props;
  86. }
  87. static struct obs_source_info image_source_info = {
  88. .id = "image_source",
  89. .type = OBS_SOURCE_TYPE_INPUT,
  90. .output_flags = OBS_SOURCE_VIDEO,
  91. .get_name = image_source_get_name,
  92. .create = image_source_create,
  93. .destroy = image_source_destroy,
  94. .update = image_source_update,
  95. .get_width = image_source_getwidth,
  96. .get_height = image_source_getheight,
  97. .video_render = image_source_render,
  98. .get_properties = image_source_properties
  99. };
  100. OBS_DECLARE_MODULE()
  101. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  102. bool obs_module_load(void)
  103. {
  104. obs_register_source(&image_source_info);
  105. return true;
  106. }