obs-internal.h 16 KB

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