image-source.c 4.6 KB

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