obs-internal.h 17 KB

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