obs-source.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * [type] can be one of the following:
  33. * + input
  34. * + filter
  35. * + transition
  36. *
  37. * input: A source used for directly playing video and/or sound.
  38. * filter: A source that is used for filtering other sources, modifying
  39. * the audio/video data before it is actually played.
  40. * transition: A source used for transitioning between two different sources
  41. * on screen.
  42. *
  43. * Each individual source is then exported by it's name. For example, a
  44. * source named "mysource" would have the following exports:
  45. * + mysource_getname
  46. * + mysource_create
  47. * + mysource_destroy
  48. * + mysource_get_output_flags
  49. *
  50. * [and optionally]
  51. * + mysource_properties
  52. * + mysource_update
  53. * + mysource_activate
  54. * + mysource_deactivate
  55. * + mysource_video_tick
  56. * + mysource_video_render
  57. * + mysource_getwidth
  58. * + mysource_getheight
  59. * + mysource_getparam
  60. * + mysource_setparam
  61. * + mysource_filtervideo
  62. * + mysource_filteraudio
  63. *
  64. * ===========================================
  65. * Primary Exports
  66. * ===========================================
  67. * bool enum_[type](size_t idx, const char **name);
  68. * idx: index of the enumeration.
  69. * name: pointer to variable that receives the type of the source
  70. * Return value: false when no more available.
  71. *
  72. * ===========================================
  73. * Source Exports
  74. * ===========================================
  75. * const char *[name]_getname(const char *locale);
  76. * Returns the full name of the source type (seen by the user).
  77. *
  78. * ---------------------------------------------------------
  79. * void *[name]_create(obs_data_t settings, obs_source_t source);
  80. * Creates a source.
  81. *
  82. * settings: Settings of the source.
  83. * source: pointer to main source
  84. * Return value: Internal source pointer, or NULL if failed.
  85. *
  86. * ---------------------------------------------------------
  87. * void [name]_destroy(void *data);
  88. * Destroys the source.
  89. *
  90. * ---------------------------------------------------------
  91. * uint32_t [name]_get_output_flags(void *data);
  92. * Returns a combination of one of the following values:
  93. * + SOURCE_VIDEO: source has video
  94. * + SOURCE_AUDIO: source has audio
  95. * + SOURCE_ASYNC_VIDEO: video is sent asynchronously via RAM
  96. * + SOURCE_DEFAULT_EFFECT: source uses default effect
  97. * + SOURCE_YUV: source is in YUV color space
  98. *
  99. * ===========================================
  100. * Optional Source Exports
  101. * ===========================================
  102. * obs_properties_t [name]_properties(const char *locale);
  103. * Returns the properties of this particular source type, if any.
  104. *
  105. * ---------------------------------------------------------
  106. * void [name]_update(void *data, obs_data_t settings);
  107. * Called to update the settings of the source.
  108. *
  109. * ---------------------------------------------------------
  110. * void [name]_video_activate(void *data);
  111. * Called when the source is being displayed.
  112. *
  113. * ---------------------------------------------------------
  114. * void [name]_video_deactivate(void *data);
  115. * Called when the source is no longer being displayed.
  116. *
  117. * ---------------------------------------------------------
  118. * void [name]_video_tick(void *data, float seconds);
  119. * Called each video frame with the time taken between the last and
  120. * current frame, in seconds.
  121. *
  122. * ---------------------------------------------------------
  123. * void [name]_video_render(void *data);
  124. * Called to render the source.
  125. *
  126. * ---------------------------------------------------------
  127. * uint32_t [name]_getwidth(void *data);
  128. * Returns the width of a source, or -1 for maximum width. If you render
  129. * video, this is required.
  130. *
  131. * ---------------------------------------------------------
  132. * uint32_t [name]_getheight(void *data);
  133. * Returns the height of a source, or -1 for maximum height. If you
  134. * render video, this is required.
  135. *
  136. * ---------------------------------------------------------
  137. * void [name]_getparam(void *data, const char *param, void *buf,
  138. * size_t buf_size);
  139. * Gets a source parameter value.
  140. *
  141. * param: Name of parameter.
  142. * Return value: Value of parameter.
  143. *
  144. * ---------------------------------------------------------
  145. * void [name]_setparam(void *data, const char *param, const void *buf,
  146. * size_t size);
  147. * Sets a source parameter value.
  148. *
  149. * param: Name of parameter.
  150. * val: Value of parameter to set.
  151. *
  152. * ---------------------------------------------------------
  153. * struct source_frame *[name]_filter_video(void *data,
  154. * const struct source_frame *frame);
  155. * Filters audio data. Used with audio filters.
  156. *
  157. * frame: Video frame data.
  158. * returns: New video frame data (or NULL if pending)
  159. *
  160. * ---------------------------------------------------------
  161. * struct filter_audio [name]_filter_audio(void *data,
  162. * struct filter_audio *audio);
  163. * Filters video data. Used with async video data.
  164. *
  165. * audio: Audio data.
  166. * reutrns New audio data (or NULL if pending)
  167. */
  168. struct obs_source;
  169. struct source_info {
  170. const char *id;
  171. /* ----------------------------------------------------------------- */
  172. /* required implementations */
  173. const char *(*getname)(const char *locale);
  174. void *(*create)(obs_data_t settings, obs_source_t source);
  175. void (*destroy)(void *data);
  176. uint32_t (*get_output_flags)(void *data);
  177. /* ----------------------------------------------------------------- */
  178. /* optional implementations */
  179. obs_properties_t (*properties)(const char *locale);
  180. void (*update)(void *data, obs_data_t settings);
  181. void (*activate)(void *data);
  182. void (*deactivate)(void *data);
  183. void (*video_tick)(void *data, float seconds);
  184. void (*video_render)(void *data);
  185. uint32_t (*getwidth)(void *data);
  186. uint32_t (*getheight)(void *data);
  187. struct source_frame *(*filter_video)(void *data,
  188. const struct source_frame *frame);
  189. struct filtered_audio *(*filter_audio)(void *data,
  190. struct filtered_audio *audio);
  191. };
  192. struct obs_source {
  193. volatile int refs;
  194. /* source-specific data */
  195. char *name; /* user-defined name */
  196. enum obs_source_type type;
  197. obs_data_t settings;
  198. void *data;
  199. struct source_info callbacks;
  200. signal_handler_t signals;
  201. proc_handler_t procs;
  202. /* used to indicate that the source has been removed and all
  203. * references to it should be released (not exactly how I would prefer
  204. * to handle things but it's the best option) */
  205. bool removed;
  206. /* timing (if video is present, is based upon video) */
  207. volatile bool timing_set;
  208. volatile uint64_t timing_adjust;
  209. volatile int audio_reset_ref;
  210. uint64_t next_audio_ts_min;
  211. uint64_t last_frame_ts;
  212. uint64_t last_sys_timestamp;
  213. /* audio */
  214. bool audio_failed;
  215. struct resample_info sample_info;
  216. audio_resampler_t resampler;
  217. audio_line_t audio_line;
  218. pthread_mutex_t audio_mutex;
  219. struct filtered_audio audio_data;
  220. size_t audio_storage_size;
  221. float volume;
  222. /* async video data */
  223. texture_t output_texture;
  224. DARRAY(struct source_frame*) video_frames;
  225. pthread_mutex_t video_mutex;
  226. /* filters */
  227. struct obs_source *filter_parent;
  228. struct obs_source *filter_target;
  229. DARRAY(struct obs_source*) filters;
  230. pthread_mutex_t filter_mutex;
  231. bool rendering_filter;
  232. };
  233. extern bool load_source_info(void *module, const char *module_name,
  234. const char *source_name, struct source_info *info);
  235. bool obs_source_init_handlers(struct obs_source *source);
  236. extern bool obs_source_init(struct obs_source *source,
  237. const struct source_info *info);
  238. extern void obs_source_activate(obs_source_t source);
  239. extern void obs_source_deactivate(obs_source_t source);
  240. extern void obs_source_video_tick(obs_source_t source, float seconds);