obs-internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 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 "callback/signal.h"
  20. #include "callback/proc.h"
  21. #include "graphics/graphics.h"
  22. #include "media-io/audio-resampler.h"
  23. #include "media-io/video-io.h"
  24. #include "media-io/audio-io.h"
  25. #include "obs.h"
  26. #define NUM_TEXTURES 2
  27. struct draw_callback {
  28. void (*draw)(void *param, uint32_t cx, uint32_t cy);
  29. void *param;
  30. };
  31. /* ------------------------------------------------------------------------- */
  32. /* modules */
  33. struct obs_module {
  34. char *name;
  35. void *module;
  36. };
  37. extern void free_module(struct obs_module *mod);
  38. /* ------------------------------------------------------------------------- */
  39. /* views */
  40. struct obs_view {
  41. pthread_mutex_t channels_mutex;
  42. obs_source_t channels[MAX_CHANNELS];
  43. };
  44. extern bool obs_view_init(struct obs_view *view);
  45. extern void obs_view_free(struct obs_view *view);
  46. /* ------------------------------------------------------------------------- */
  47. /* displays */
  48. struct obs_display {
  49. bool size_changed;
  50. uint32_t cx, cy;
  51. swapchain_t swap;
  52. pthread_mutex_t draw_callbacks_mutex;
  53. DARRAY(struct draw_callback) draw_callbacks;
  54. };
  55. extern bool obs_display_init(struct obs_display *display,
  56. struct gs_init_data *graphics_data);
  57. extern void obs_display_free(struct obs_display *display);
  58. /* ------------------------------------------------------------------------- */
  59. /* core */
  60. struct obs_core_video {
  61. graphics_t graphics;
  62. stagesurf_t copy_surfaces[NUM_TEXTURES];
  63. texture_t render_textures[NUM_TEXTURES];
  64. texture_t output_textures[NUM_TEXTURES];
  65. texture_t convert_textures[NUM_TEXTURES];
  66. bool textures_rendered[NUM_TEXTURES];
  67. bool textures_output[NUM_TEXTURES];
  68. bool textures_copied[NUM_TEXTURES];
  69. bool textures_converted[NUM_TEXTURES];
  70. struct source_frame convert_frames[NUM_TEXTURES];
  71. effect_t default_effect;
  72. effect_t conversion_effect;
  73. stagesurf_t mapped_surface;
  74. int cur_texture;
  75. video_t video;
  76. pthread_t video_thread;
  77. bool thread_initialized;
  78. bool gpu_conversion;
  79. const char *conversion_tech;
  80. uint32_t conversion_height;
  81. uint32_t plane_offsets[3];
  82. uint32_t plane_sizes[3];
  83. uint32_t plane_linewidth[3];
  84. uint32_t output_width;
  85. uint32_t output_height;
  86. uint32_t base_width;
  87. uint32_t base_height;
  88. struct obs_display main_display;
  89. };
  90. struct obs_core_audio {
  91. /* TODO: sound output subsystem */
  92. audio_t audio;
  93. float user_volume;
  94. float present_volume;
  95. };
  96. /* user sources, output channels, and displays */
  97. struct obs_core_data {
  98. /* arrays of pointers jim? you should really stop being lazy and use
  99. * linked lists. */
  100. DARRAY(struct obs_display*) displays;
  101. DARRAY(struct obs_source*) sources;
  102. DARRAY(struct obs_output*) outputs;
  103. DARRAY(struct obs_encoder*) encoders;
  104. pthread_mutex_t sources_mutex;
  105. pthread_mutex_t displays_mutex;
  106. pthread_mutex_t outputs_mutex;
  107. pthread_mutex_t encoders_mutex;
  108. struct obs_view main_view;
  109. long long unnamed_index;
  110. volatile bool valid;
  111. };
  112. struct obs_core {
  113. DARRAY(struct obs_module) modules;
  114. DARRAY(struct obs_source_info) input_types;
  115. DARRAY(struct obs_source_info) filter_types;
  116. DARRAY(struct obs_source_info) transition_types;
  117. DARRAY(struct obs_output_info) output_types;
  118. DARRAY(struct obs_encoder_info) encoder_types;
  119. DARRAY(struct obs_service_info) service_types;
  120. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  121. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  122. signal_handler_t signals;
  123. proc_handler_t procs;
  124. /* segmented into multiple sub-structures to keep things a bit more
  125. * clean and organized */
  126. struct obs_core_video video;
  127. struct obs_core_audio audio;
  128. struct obs_core_data data;
  129. };
  130. extern struct obs_core *obs;
  131. extern void *obs_video_thread(void *param);
  132. /* ------------------------------------------------------------------------- */
  133. /* sources */
  134. struct obs_source {
  135. volatile long refs;
  136. struct obs_source_info info;
  137. /* source-specific data */
  138. char *name; /* user-defined name */
  139. obs_data_t settings;
  140. void *data;
  141. signal_handler_t signals;
  142. proc_handler_t procs;
  143. /* ensures show/hide are only called once */
  144. volatile long show_refs;
  145. /* ensures activate/deactivate are only called once */
  146. volatile long activate_refs;
  147. /* prevents infinite recursion when enumerating sources */
  148. volatile long enum_refs;
  149. /* used to indicate that the source has been removed and all
  150. * references to it should be released (not exactly how I would prefer
  151. * to handle things but it's the best option) */
  152. bool removed;
  153. /* timing (if video is present, is based upon video) */
  154. volatile bool timing_set;
  155. volatile uint64_t timing_adjust;
  156. volatile long audio_reset_ref;
  157. uint64_t next_audio_ts_min;
  158. uint64_t last_frame_ts;
  159. uint64_t last_sys_timestamp;
  160. /* audio */
  161. bool audio_failed;
  162. struct resample_info sample_info;
  163. audio_resampler_t resampler;
  164. audio_line_t audio_line;
  165. pthread_mutex_t audio_mutex;
  166. struct filtered_audio audio_data;
  167. size_t audio_storage_size;
  168. float user_volume;
  169. float present_volume;
  170. int64_t sync_offset;
  171. /* transition volume is meant to store the sum of transitioning volumes
  172. * of a source, i.e. if a source is within both the "to" and "from"
  173. * targets of a transition, it would add both volumes to this variable,
  174. * and then when the transition frame is complete, is applies the value
  175. * to the presentation volume. */
  176. float transition_volume;
  177. /* async video data */
  178. texture_t output_texture;
  179. DARRAY(struct source_frame*) video_frames;
  180. pthread_mutex_t video_mutex;
  181. /* filters */
  182. struct obs_source *filter_parent;
  183. struct obs_source *filter_target;
  184. DARRAY(struct obs_source*) filters;
  185. pthread_mutex_t filter_mutex;
  186. texrender_t filter_texrender;
  187. bool rendering_filter;
  188. };
  189. bool obs_source_init_handlers(struct obs_source *source);
  190. extern bool obs_source_init(struct obs_source *source,
  191. const struct obs_source_info *info);
  192. enum view_type {
  193. MAIN_VIEW,
  194. AUX_VIEW
  195. };
  196. extern void obs_source_activate(obs_source_t source, enum view_type type);
  197. extern void obs_source_deactivate(obs_source_t source, enum view_type type);
  198. extern void obs_source_video_tick(obs_source_t source, float seconds);
  199. /* ------------------------------------------------------------------------- */
  200. /* outputs */
  201. struct il_packet {
  202. int64_t input_ts_us;
  203. int64_t output_ts_us;
  204. struct encoder_packet packet;
  205. };
  206. struct obs_output {
  207. char *name;
  208. void *data;
  209. struct obs_output_info info;
  210. obs_data_t settings;
  211. signal_handler_t signals;
  212. proc_handler_t procs;
  213. bool received_video;
  214. bool received_audio;
  215. int64_t first_video_ts;
  216. int64_t video_offset;
  217. int64_t audio_offset;
  218. pthread_mutex_t interleaved_mutex;
  219. DARRAY(struct il_packet) interleaved_packets;
  220. bool active;
  221. video_t video;
  222. audio_t audio;
  223. obs_encoder_t video_encoder;
  224. obs_encoder_t audio_encoder;
  225. bool video_conversion_set;
  226. bool audio_conversion_set;
  227. struct video_scale_info video_conversion;
  228. struct audio_convert_info audio_conversion;
  229. bool valid;
  230. };
  231. extern void obs_output_remove_encoder(struct obs_output *output,
  232. struct obs_encoder *encoder);
  233. /* ------------------------------------------------------------------------- */
  234. /* encoders */
  235. struct encoder_callback {
  236. bool sent_first_packet;
  237. void (*new_packet)(void *param, struct encoder_packet *packet);
  238. void *param;
  239. };
  240. struct obs_encoder {
  241. char *name;
  242. void *data;
  243. struct obs_encoder_info info;
  244. obs_data_t settings;
  245. bool active;
  246. uint32_t timebase_num;
  247. uint32_t timebase_den;
  248. int64_t cur_pts;
  249. pthread_mutex_t outputs_mutex;
  250. DARRAY(obs_output_t) outputs;
  251. bool destroy_on_stop;
  252. /* stores the video/audio media output pointer. video_t or audio_t */
  253. void *media;
  254. pthread_mutex_t callbacks_mutex;
  255. DARRAY(struct encoder_callback) callbacks;
  256. };
  257. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  258. struct obs_output *output);
  259. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  260. struct obs_output *output);