obs-source.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "util/c99defs.h"
  16. #include "util/darray.h"
  17. #include "util/dstr.h"
  18. #include "util/threading.h"
  19. #include "media-io/audio-resampler.h"
  20. #include "callback/signal.h"
  21. #include "callback/proc.h"
  22. /*
  23. * ===========================================
  24. * Sources
  25. * ===========================================
  26. *
  27. * A source is literally a "source" of audio and/or video.
  28. *
  29. * A module with sources needs to export these functions:
  30. * + enum_[type]
  31. *
  32. * Each individual source is then exported by it's name. For example, a
  33. * source named "mysource" would have the following exports:
  34. * + mysource_getname
  35. * + mysource_create
  36. * + mysource_destroy
  37. * + mysource_get_output_flags
  38. *
  39. * [and optionally]
  40. * + mysource_update
  41. * + mysource_activate
  42. * + mysource_deactivate
  43. * + mysource_video_tick
  44. * + mysource_video_render
  45. * + mysource_getwidth
  46. * + mysource_getheight
  47. * + mysource_getparam
  48. * + mysource_setparam
  49. * + mysource_filtervideo
  50. * + mysource_filteraudio
  51. *
  52. * ===========================================
  53. * Primary Exports
  54. * ===========================================
  55. * const bool enum_[type](size_t idx, const char **name);
  56. * idx: index of the source.
  57. * type: pointer to variable that receives the type of the source
  58. * Return value: false when no more available.
  59. *
  60. * ===========================================
  61. * Source Exports
  62. * ===========================================
  63. * const char *[name]_getname(const char *locale);
  64. * Returns the full name of the source type (seen by the user).
  65. *
  66. * ---------------------------------------------------------
  67. * void *[name]_create(const char *settings, obs_source_t source);
  68. * Creates a source.
  69. *
  70. * settings: Settings of the source.
  71. * source: pointer to main source
  72. * Return value: Internal source pointer, or NULL if failed.
  73. *
  74. * ---------------------------------------------------------
  75. * void [name]_destroy(void *data);
  76. * Destroys the source.
  77. *
  78. * ---------------------------------------------------------
  79. * uint32_t [name]_get_output_flags(void *data);
  80. * Returns a combination of one of the following values:
  81. * + SOURCE_VIDEO: source has video
  82. * + SOURCE_AUDIO: source has audio
  83. * + SOURCE_ASYNC_VIDEO: video is sent asynchronously via RAM
  84. * + SOURCE_DEFAULT_EFFECT: source uses default effect
  85. * + SOURCE_YUV: source is in YUV color space
  86. *
  87. * ===========================================
  88. * Optional Source Exports
  89. * ===========================================
  90. * void [name]_update(void *data, const char *settings);
  91. * Called to update the settings of the source.
  92. *
  93. * ---------------------------------------------------------
  94. * void [name]_video_activate(void *data);
  95. * Called when the source is being displayed.
  96. *
  97. * ---------------------------------------------------------
  98. * void [name]_video_deactivate(void *data);
  99. * Called when the source is no longer being displayed.
  100. *
  101. * ---------------------------------------------------------
  102. * void [name]_video_tick(void *data, float seconds);
  103. * Called each video frame with the time taken between the last and
  104. * current frame, in seconds.
  105. *
  106. * ---------------------------------------------------------
  107. * void [name]_video_render(void *data);
  108. * Called to render the source.
  109. *
  110. * ---------------------------------------------------------
  111. * uint32_t [name]_getwidth(void *data);
  112. * Returns the width of a source, or -1 for maximum width. If you render
  113. * video, this is required.
  114. *
  115. * ---------------------------------------------------------
  116. * uint32_t [name]_getheight(void *data);
  117. * Returns the height of a source, or -1 for maximum height. If you
  118. * render video, this is required.
  119. *
  120. * ---------------------------------------------------------
  121. * void [name]_getparam(void *data, const char *param, void *buf,
  122. * size_t buf_size);
  123. * Gets a source parameter value.
  124. *
  125. * param: Name of parameter.
  126. * Return value: Value of parameter.
  127. *
  128. * ---------------------------------------------------------
  129. * void [name]_setparam(void *data, const char *param, const void *buf,
  130. * size_t size);
  131. * Sets a source parameter value.
  132. *
  133. * param: Name of parameter.
  134. * val: Value of parameter to set.
  135. *
  136. * ---------------------------------------------------------
  137. * struct source_frame *[name]_filter_video(void *data,
  138. * const struct source_frame *frame);
  139. * Filters audio data. Used with audio filters.
  140. *
  141. * frame: Video frame data.
  142. * returns: New video frame data (or NULL if pending)
  143. *
  144. * ---------------------------------------------------------
  145. * struct filter_audio [name]_filter_audio(void *data,
  146. * struct filter_audio *audio);
  147. * Filters video data. Used with async video data.
  148. *
  149. * audio: Audio data.
  150. * reutrns New audio data (or NULL if pending)
  151. */
  152. struct obs_source;
  153. struct source_info {
  154. const char *id;
  155. /* ----------------------------------------------------------------- */
  156. /* required implementations */
  157. const char *(*getname)(const char *locale);
  158. void *(*create)(const char *settings, obs_source_t source);
  159. void (*destroy)(void *data);
  160. uint32_t (*get_output_flags)(void *data);
  161. /* ----------------------------------------------------------------- */
  162. /* optional implementations */
  163. void (*update)(void *data, const char *settings);
  164. void (*activate)(void *data);
  165. void (*deactivate)(void *data);
  166. void (*video_tick)(void *data, float seconds);
  167. void (*video_render)(void *data);
  168. uint32_t (*getwidth)(void *data);
  169. uint32_t (*getheight)(void *data);
  170. size_t (*getparam)(void *data, const char *param, void *data_out,
  171. size_t buf_size);
  172. void (*setparam)(void *data, const char *param, const void *data_in,
  173. size_t size);
  174. struct source_frame *(*filter_video)(void *data,
  175. const struct source_frame *frame);
  176. struct filtered_audio *(*filter_audio)(void *data,
  177. struct filtered_audio *audio);
  178. };
  179. struct obs_source {
  180. volatile int refs;
  181. /* source-specific data */
  182. char *name; /* user-defined name */
  183. enum obs_source_type type;
  184. struct dstr settings;
  185. void *data;
  186. struct source_info callbacks;
  187. signal_handler_t signals;
  188. proc_handler_t procs;
  189. /* used to indicate that the source has been removed and all
  190. * references to it should be released (not exactly how I would prefer
  191. * to handle things but it's the best option) */
  192. bool removed;
  193. /* timing (if video is present, is based upon video) */
  194. volatile bool timing_set;
  195. volatile uint64_t timing_adjust;
  196. volatile int audio_reset_ref;
  197. uint64_t next_audio_ts_min;
  198. uint64_t last_frame_ts;
  199. uint64_t last_sys_timestamp;
  200. /* audio */
  201. bool audio_failed;
  202. struct resample_info sample_info;
  203. audio_resampler_t resampler;
  204. audio_line_t audio_line;
  205. pthread_mutex_t audio_mutex;
  206. struct filtered_audio audio_data;
  207. size_t audio_storage_size;
  208. float volume;
  209. /* async video data */
  210. texture_t output_texture;
  211. DARRAY(struct source_frame*) video_frames;
  212. pthread_mutex_t video_mutex;
  213. /* filters */
  214. struct obs_source *filter_parent;
  215. struct obs_source *filter_target;
  216. DARRAY(struct obs_source*) filters;
  217. pthread_mutex_t filter_mutex;
  218. bool rendering_filter;
  219. };
  220. extern bool load_source_info(void *module, const char *module_name,
  221. const char *source_name, struct source_info *info);
  222. bool obs_source_init_handlers(struct obs_source *source);
  223. extern bool obs_source_init(struct obs_source *source, const char *settings,
  224. const struct source_info *info);
  225. extern void obs_source_activate(obs_source_t source);
  226. extern void obs_source_deactivate(obs_source_t source);
  227. extern void obs_source_video_tick(obs_source_t source, float seconds);