obs-internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. bool async_rendered;
  228. /* audio */
  229. bool audio_failed;
  230. struct resample_info sample_info;
  231. audio_resampler_t *resampler;
  232. audio_line_t *audio_line;
  233. pthread_mutex_t audio_mutex;
  234. struct obs_audio_data audio_data;
  235. size_t audio_storage_size;
  236. float user_volume;
  237. float present_volume;
  238. int64_t sync_offset;
  239. /* audio levels*/
  240. float vol_mag;
  241. float vol_max;
  242. float vol_peak;
  243. size_t vol_update_count;
  244. /* transition volume is meant to store the sum of transitioning volumes
  245. * of a source, i.e. if a source is within both the "to" and "from"
  246. * targets of a transition, it would add both volumes to this variable,
  247. * and then when the transition frame is complete, is applies the value
  248. * to the presentation volume. */
  249. float transition_volume;
  250. /* async video data */
  251. gs_texture_t *async_texture;
  252. gs_texrender_t *async_convert_texrender;
  253. bool async_gpu_conversion;
  254. enum video_format async_format;
  255. enum gs_color_format async_texture_format;
  256. float async_color_matrix[16];
  257. bool async_full_range;
  258. float async_color_range_min[3];
  259. float async_color_range_max[3];
  260. int async_plane_offset[2];
  261. bool async_flip;
  262. DARRAY(struct obs_source_frame*)video_frames;
  263. pthread_mutex_t video_mutex;
  264. uint32_t async_width;
  265. uint32_t async_height;
  266. uint32_t async_convert_width;
  267. uint32_t async_convert_height;
  268. /* filters */
  269. struct obs_source *filter_parent;
  270. struct obs_source *filter_target;
  271. DARRAY(struct obs_source*) filters;
  272. pthread_mutex_t filter_mutex;
  273. gs_texrender_t *filter_texrender;
  274. bool rendering_filter;
  275. };
  276. extern const struct obs_source_info *find_source(struct darray *list,
  277. const char *id);
  278. extern bool obs_source_init_context(struct obs_source *source,
  279. obs_data_t *settings, const char *name);
  280. extern bool obs_source_init(struct obs_source *source,
  281. const struct obs_source_info *info);
  282. extern void obs_source_destroy(struct obs_source *source);
  283. enum view_type {
  284. MAIN_VIEW,
  285. AUX_VIEW
  286. };
  287. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  288. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  289. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  290. /* ------------------------------------------------------------------------- */
  291. /* outputs */
  292. struct obs_output {
  293. struct obs_context_data context;
  294. struct obs_output_info info;
  295. bool received_video;
  296. bool received_audio;
  297. int64_t first_video_ts;
  298. int64_t video_offset;
  299. int64_t audio_offset;
  300. int64_t highest_audio_ts;
  301. int64_t highest_video_ts;
  302. pthread_mutex_t interleaved_mutex;
  303. DARRAY(struct encoder_packet) interleaved_packets;
  304. int reconnect_retry_sec;
  305. int reconnect_retry_max;
  306. int reconnect_retries;
  307. bool reconnecting;
  308. pthread_t reconnect_thread;
  309. os_event_t *reconnect_stop_event;
  310. volatile bool reconnect_thread_active;
  311. uint32_t starting_frame_count;
  312. uint32_t starting_skipped_frame_count;
  313. int total_frames;
  314. bool active;
  315. video_t *video;
  316. audio_t *audio;
  317. obs_encoder_t *video_encoder;
  318. obs_encoder_t *audio_encoder;
  319. obs_service_t *service;
  320. uint32_t scaled_width;
  321. uint32_t scaled_height;
  322. bool video_conversion_set;
  323. bool audio_conversion_set;
  324. struct video_scale_info video_conversion;
  325. struct audio_convert_info audio_conversion;
  326. bool valid;
  327. };
  328. extern const struct obs_output_info *find_output(const char *id);
  329. extern void obs_output_remove_encoder(struct obs_output *output,
  330. struct obs_encoder *encoder);
  331. /* ------------------------------------------------------------------------- */
  332. /* encoders */
  333. struct encoder_callback {
  334. bool sent_first_packet;
  335. void (*new_packet)(void *param, struct encoder_packet *packet);
  336. void *param;
  337. };
  338. struct obs_encoder {
  339. struct obs_context_data context;
  340. struct obs_encoder_info info;
  341. uint32_t samplerate;
  342. size_t planes;
  343. size_t blocksize;
  344. size_t framesize;
  345. size_t framesize_bytes;
  346. uint32_t scaled_width;
  347. uint32_t scaled_height;
  348. bool active;
  349. uint32_t timebase_num;
  350. uint32_t timebase_den;
  351. int64_t cur_pts;
  352. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  353. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  354. /* if a video encoder is paired with an audio encoder, make it start
  355. * up at the specific timestamp. if this is the audio encoder,
  356. * wait_for_video makes it wait until it's ready to sync up with
  357. * video */
  358. bool wait_for_video;
  359. struct obs_encoder *paired_encoder;
  360. uint64_t start_ts;
  361. pthread_mutex_t outputs_mutex;
  362. DARRAY(obs_output_t*) outputs;
  363. bool destroy_on_stop;
  364. /* stores the video/audio media output pointer. video_t *or audio_t **/
  365. void *media;
  366. pthread_mutex_t callbacks_mutex;
  367. DARRAY(struct encoder_callback) callbacks;
  368. };
  369. extern struct obs_encoder_info *find_encoder(const char *id);
  370. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  371. extern void obs_encoder_start(obs_encoder_t *encoder,
  372. void (*new_packet)(void *param, struct encoder_packet *packet),
  373. void *param);
  374. extern void obs_encoder_stop(obs_encoder_t *encoder,
  375. void (*new_packet)(void *param, struct encoder_packet *packet),
  376. void *param);
  377. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  378. struct obs_output *output);
  379. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  380. struct obs_output *output);
  381. /* ------------------------------------------------------------------------- */
  382. /* services */
  383. struct obs_service {
  384. struct obs_context_data context;
  385. struct obs_service_info info;
  386. bool active;
  387. bool destroy;
  388. struct obs_output *output;
  389. };
  390. extern const struct obs_service_info *find_service(const char *id);
  391. extern void obs_service_activate(struct obs_service *service);
  392. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  393. extern bool obs_service_initialize(struct obs_service *service,
  394. struct obs_output *output);