image-source.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <obs-module.h>
  2. #define warn(format, ...) \
  3. blog(LOG_WARNING, "[image_source: '%s'] " format, \
  4. obs_source_getname(context->source), ##__VA_ARGS__)
  5. struct image_source {
  6. obs_source_t source;
  7. 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_getstring(settings, "file");
  19. gs_entercontext(obs_graphics());
  20. if (context->tex) {
  21. texture_destroy(context->tex);
  22. context->tex = NULL;
  23. }
  24. if (file) {
  25. context->tex = gs_create_texture_from_file(file);
  26. if (context->tex) {
  27. context->cx = texture_getwidth(context->tex);
  28. context->cy = texture_getheight(context->tex);
  29. } else {
  30. warn("failed to load texture '%s'", file);
  31. context->cx = 0;
  32. context->cy = 0;
  33. }
  34. }
  35. gs_leavecontext();
  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. gs_entercontext(obs_graphics());
  48. texture_destroy(context->tex);
  49. gs_leavecontext();
  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, effect_t effect)
  63. {
  64. struct image_source *context = data;
  65. if (!context->tex)
  66. return;
  67. gs_reset_blend_state();
  68. effect_settexture(effect_getparambyname(effect, "image"), context->tex);
  69. gs_draw_sprite(context->tex, 0, context->cx, context->cy);
  70. }
  71. static const char *image_filter =
  72. "All formats (*.bmp *.tga *.png *.jpeg *.jpg *.gif);;"
  73. "BMP Files (*.bmp);;"
  74. "Targa Files (*.tga);;"
  75. "PNG Files (*.png);;"
  76. "JPEG Files (*.jpeg *.jpg);;"
  77. "GIF Files (*.gif)";
  78. static obs_properties_t image_source_properties(void)
  79. {
  80. obs_properties_t props = obs_properties_create();
  81. obs_properties_add_path(props,
  82. "file", obs_module_text("File"),
  83. OBS_PATH_FILE, image_filter, NULL);
  84. return props;
  85. }
  86. static struct obs_source_info image_source_info = {
  87. .id = "image_source",
  88. .type = OBS_SOURCE_TYPE_INPUT,
  89. .output_flags = OBS_SOURCE_VIDEO,
  90. .getname = image_source_get_name,
  91. .create = image_source_create,
  92. .destroy = image_source_destroy,
  93. .update = image_source_update,
  94. .getwidth = image_source_getwidth,
  95. .getheight = image_source_getheight,
  96. .video_render = image_source_render,
  97. .properties = image_source_properties
  98. };
  99. OBS_DECLARE_MODULE()
  100. OBS_MODULE_USE_DEFAULT_LOCALE("image-source", "en-US")
  101. bool obs_module_load(uint32_t libobs_version)
  102. {
  103. obs_register_source(&image_source_info);
  104. UNUSED_PARAMETER(libobs_version);
  105. return true;
  106. }
  107. void obs_module_unload(void)
  108. {
  109. OBS_MODULE_FREE_DEFAULT_LOCALE();
  110. }