image-source.c 4.5 KB

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