obs-source.h 8.9 KB

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