obs-internal.h 15 KB

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