obs-internal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/circlebuf.h"
  18. #include "util/dstr.h"
  19. #include "util/threading.h"
  20. #include "callback/signal.h"
  21. #include "callback/proc.h"
  22. #include "graphics/graphics.h"
  23. #include "media-io/audio-resampler.h"
  24. #include "media-io/video-io.h"
  25. #include "media-io/audio-io.h"
  26. #include "obs.h"
  27. #define NUM_TEXTURES 2
  28. #define MICROSECOND_DEN 1000000
  29. static inline int64_t packet_dts_usec(struct encoder_packet *packet)
  30. {
  31. return packet->dts * MICROSECOND_DEN / packet->timebase_den;
  32. }
  33. struct draw_callback {
  34. void (*draw)(void *param, uint32_t cx, uint32_t cy);
  35. void *param;
  36. };
  37. /* ------------------------------------------------------------------------- */
  38. /* modules */
  39. struct obs_module {
  40. char *name;
  41. void *module;
  42. };
  43. extern void free_module(struct obs_module *mod);
  44. /* ------------------------------------------------------------------------- */
  45. /* views */
  46. struct obs_view {
  47. pthread_mutex_t channels_mutex;
  48. obs_source_t channels[MAX_CHANNELS];
  49. };
  50. extern bool obs_view_init(struct obs_view *view);
  51. extern void obs_view_free(struct obs_view *view);
  52. /* ------------------------------------------------------------------------- */
  53. /* displays */
  54. struct obs_display {
  55. bool size_changed;
  56. uint32_t cx, cy;
  57. swapchain_t swap;
  58. pthread_mutex_t draw_callbacks_mutex;
  59. DARRAY(struct draw_callback) draw_callbacks;
  60. struct obs_display *next;
  61. struct obs_display **prev_next;
  62. };
  63. extern bool obs_display_init(struct obs_display *display,
  64. struct gs_init_data *graphics_data);
  65. extern void obs_display_free(struct obs_display *display);
  66. /* ------------------------------------------------------------------------- */
  67. /* core */
  68. struct obs_core_video {
  69. graphics_t graphics;
  70. stagesurf_t copy_surfaces[NUM_TEXTURES];
  71. texture_t render_textures[NUM_TEXTURES];
  72. texture_t output_textures[NUM_TEXTURES];
  73. texture_t convert_textures[NUM_TEXTURES];
  74. bool textures_rendered[NUM_TEXTURES];
  75. bool textures_output[NUM_TEXTURES];
  76. bool textures_copied[NUM_TEXTURES];
  77. bool textures_converted[NUM_TEXTURES];
  78. struct source_frame convert_frames[NUM_TEXTURES];
  79. effect_t default_effect;
  80. effect_t conversion_effect;
  81. stagesurf_t mapped_surface;
  82. int cur_texture;
  83. video_t video;
  84. pthread_t video_thread;
  85. bool thread_initialized;
  86. bool gpu_conversion;
  87. const char *conversion_tech;
  88. uint32_t conversion_height;
  89. uint32_t plane_offsets[3];
  90. uint32_t plane_sizes[3];
  91. uint32_t plane_linewidth[3];
  92. uint32_t output_width;
  93. uint32_t output_height;
  94. uint32_t base_width;
  95. uint32_t base_height;
  96. struct obs_display main_display;
  97. };
  98. struct obs_core_audio {
  99. /* TODO: sound output subsystem */
  100. audio_t audio;
  101. float user_volume;
  102. float present_volume;
  103. };
  104. /* user sources, output channels, and displays */
  105. struct obs_core_data {
  106. pthread_mutex_t user_sources_mutex;
  107. DARRAY(struct obs_source*) user_sources;
  108. struct obs_source *first_source;
  109. struct obs_display *first_display;
  110. struct obs_output *first_output;
  111. struct obs_encoder *first_encoder;
  112. struct obs_service *first_service;
  113. pthread_mutex_t sources_mutex;
  114. pthread_mutex_t displays_mutex;
  115. pthread_mutex_t outputs_mutex;
  116. pthread_mutex_t encoders_mutex;
  117. pthread_mutex_t services_mutex;
  118. struct obs_view main_view;
  119. long long unnamed_index;
  120. volatile bool valid;
  121. };
  122. struct obs_core {
  123. DARRAY(struct obs_module) modules;
  124. DARRAY(struct obs_source_info) input_types;
  125. DARRAY(struct obs_source_info) filter_types;
  126. DARRAY(struct obs_source_info) transition_types;
  127. DARRAY(struct obs_output_info) output_types;
  128. DARRAY(struct obs_encoder_info) encoder_types;
  129. DARRAY(struct obs_service_info) service_types;
  130. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  131. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  132. signal_handler_t signals;
  133. proc_handler_t procs;
  134. /* segmented into multiple sub-structures to keep things a bit more
  135. * clean and organized */
  136. struct obs_core_video video;
  137. struct obs_core_audio audio;
  138. struct obs_core_data data;
  139. };
  140. extern struct obs_core *obs;
  141. extern void *obs_video_thread(void *param);
  142. /* ------------------------------------------------------------------------- */
  143. /* obs shared context data */
  144. struct obs_context_data {
  145. char *name;
  146. void *data;
  147. obs_data_t settings;
  148. signal_handler_t signals;
  149. proc_handler_t procs;
  150. pthread_mutex_t *mutex;
  151. struct obs_context_data *next;
  152. struct obs_context_data **prev_next;
  153. };
  154. extern bool obs_context_data_init(
  155. struct obs_context_data *context,
  156. obs_data_t settings,
  157. const char *name);
  158. extern void obs_context_data_free(struct obs_context_data *context);
  159. extern void obs_context_data_insert(struct obs_context_data *context,
  160. pthread_mutex_t *mutex, void *first);
  161. extern void obs_context_data_remove(struct obs_context_data *context);
  162. extern void obs_context_data_setname(struct obs_context_data *context,
  163. const char *name);
  164. /* ------------------------------------------------------------------------- */
  165. /* sources */
  166. struct obs_source {
  167. struct obs_context_data context;
  168. struct obs_source_info info;
  169. volatile long refs;
  170. /* signals to call the source update in the video thread */
  171. bool defer_update;
  172. /* ensures show/hide are only called once */
  173. volatile long show_refs;
  174. /* ensures activate/deactivate are only called once */
  175. volatile long activate_refs;
  176. /* prevents infinite recursion when enumerating sources */
  177. volatile long enum_refs;
  178. /* used to indicate that the source has been removed and all
  179. * references to it should be released (not exactly how I would prefer
  180. * to handle things but it's the best option) */
  181. bool removed;
  182. /* timing (if video is present, is based upon video) */
  183. volatile bool timing_set;
  184. volatile uint64_t timing_adjust;
  185. uint64_t next_audio_ts_min;
  186. uint64_t last_frame_ts;
  187. uint64_t last_sys_timestamp;
  188. /*
  189. * audio/video timestamp synchronization reference counter
  190. *
  191. * if audio goes outside of expected timing bounds, this number will
  192. * be deremented.
  193. *
  194. * if video goes outside of expecting timing bounds, this number will
  195. * be incremented.
  196. *
  197. * when this reference counter is at 0, it means ths audio is
  198. * synchronized with the video and it is safe to play. when it's not
  199. * 0, it means that audio and video are desynchronized, and thus not
  200. * safe to play. this just generally ensures synchronization between
  201. * audio/video when timing somehow becomes 'reset'.
  202. *
  203. * XXX: may be an overly cautious check
  204. */
  205. volatile long av_sync_ref;
  206. /* audio */
  207. bool audio_failed;
  208. struct resample_info sample_info;
  209. audio_resampler_t resampler;
  210. audio_line_t audio_line;
  211. pthread_mutex_t audio_mutex;
  212. struct filtered_audio audio_data;
  213. size_t audio_storage_size;
  214. float user_volume;
  215. float present_volume;
  216. int64_t sync_offset;
  217. /* transition volume is meant to store the sum of transitioning volumes
  218. * of a source, i.e. if a source is within both the "to" and "from"
  219. * targets of a transition, it would add both volumes to this variable,
  220. * and then when the transition frame is complete, is applies the value
  221. * to the presentation volume. */
  222. float transition_volume;
  223. /* async video data */
  224. texture_t async_texture;
  225. texrender_t async_convert_texrender;
  226. bool async_gpu_conversion;
  227. enum video_format async_format;
  228. enum gs_color_format async_texture_format;
  229. float async_color_matrix[16];
  230. bool async_full_range;
  231. float async_color_range_min[3];
  232. float async_color_range_max[3];
  233. int async_plane_offset[2];
  234. bool async_flip;
  235. DARRAY(struct source_frame*) video_frames;
  236. pthread_mutex_t video_mutex;
  237. uint32_t async_width;
  238. uint32_t async_height;
  239. uint32_t async_convert_width;
  240. uint32_t async_convert_height;
  241. /* filters */
  242. struct obs_source *filter_parent;
  243. struct obs_source *filter_target;
  244. DARRAY(struct obs_source*) filters;
  245. pthread_mutex_t filter_mutex;
  246. texrender_t filter_texrender;
  247. bool rendering_filter;
  248. };
  249. extern bool obs_source_init_context(struct obs_source *source,
  250. obs_data_t settings, const char *name);
  251. extern bool obs_source_init(struct obs_source *source,
  252. const struct obs_source_info *info);
  253. extern void obs_source_destroy(struct obs_source *source);
  254. enum view_type {
  255. MAIN_VIEW,
  256. AUX_VIEW
  257. };
  258. extern void obs_source_activate(obs_source_t source, enum view_type type);
  259. extern void obs_source_deactivate(obs_source_t source, enum view_type type);
  260. extern void obs_source_video_tick(obs_source_t source, float seconds);
  261. /* ------------------------------------------------------------------------- */
  262. /* outputs */
  263. struct obs_output {
  264. struct obs_context_data context;
  265. struct obs_output_info info;
  266. bool received_video;
  267. bool received_audio;
  268. int64_t first_video_ts;
  269. int64_t video_offset;
  270. int64_t audio_offset;
  271. int64_t highest_audio_ts;
  272. int64_t highest_video_ts;
  273. pthread_mutex_t interleaved_mutex;
  274. DARRAY(struct encoder_packet) interleaved_packets;
  275. bool active;
  276. video_t video;
  277. audio_t audio;
  278. obs_encoder_t video_encoder;
  279. obs_encoder_t audio_encoder;
  280. obs_service_t service;
  281. bool video_conversion_set;
  282. bool audio_conversion_set;
  283. struct video_scale_info video_conversion;
  284. struct audio_convert_info audio_conversion;
  285. bool valid;
  286. };
  287. extern void obs_output_remove_encoder(struct obs_output *output,
  288. struct obs_encoder *encoder);
  289. /* ------------------------------------------------------------------------- */
  290. /* encoders */
  291. struct encoder_callback {
  292. bool sent_first_packet;
  293. void (*new_packet)(void *param, struct encoder_packet *packet);
  294. void *param;
  295. };
  296. struct obs_encoder {
  297. struct obs_context_data context;
  298. struct obs_encoder_info info;
  299. uint32_t samplerate;
  300. size_t planes;
  301. size_t blocksize;
  302. size_t framesize;
  303. size_t framesize_bytes;
  304. bool active;
  305. uint32_t timebase_num;
  306. uint32_t timebase_den;
  307. int64_t cur_pts;
  308. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  309. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  310. /* if a video encoder is paired with an audio encoder, make it start
  311. * up at the specific timestamp. if this is the audio encoder,
  312. * wait_for_video makes it wait until it's ready to sync up with
  313. * video */
  314. bool wait_for_video;
  315. struct obs_encoder *paired_encoder;
  316. uint64_t start_ts;
  317. pthread_mutex_t outputs_mutex;
  318. DARRAY(obs_output_t) outputs;
  319. bool destroy_on_stop;
  320. /* stores the video/audio media output pointer. video_t or audio_t */
  321. void *media;
  322. pthread_mutex_t callbacks_mutex;
  323. DARRAY(struct encoder_callback) callbacks;
  324. };
  325. extern bool obs_encoder_initialize(obs_encoder_t encoder);
  326. extern void obs_encoder_start(obs_encoder_t encoder,
  327. void (*new_packet)(void *param, struct encoder_packet *packet),
  328. void *param);
  329. extern void obs_encoder_stop(obs_encoder_t encoder,
  330. void (*new_packet)(void *param, struct encoder_packet *packet),
  331. void *param);
  332. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  333. struct obs_output *output);
  334. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  335. struct obs_output *output);
  336. /* ------------------------------------------------------------------------- */
  337. /* services */
  338. struct obs_service {
  339. struct obs_context_data context;
  340. struct obs_service_info info;
  341. bool active;
  342. bool destroy;
  343. struct obs_output *output;
  344. };
  345. void obs_service_activate(struct obs_service *service);
  346. void obs_service_deactivate(struct obs_service *service, bool remove);