obs-internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. const 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. struct circlebuf timestamp_buffer;
  112. gs_effect_t *default_effect;
  113. gs_effect_t *default_rect_effect;
  114. gs_effect_t *solid_effect;
  115. gs_effect_t *conversion_effect;
  116. gs_effect_t *bicubic_effect;
  117. gs_effect_t *lanczos_effect;
  118. gs_stagesurf_t *mapped_surface;
  119. int cur_texture;
  120. video_t *video;
  121. pthread_t video_thread;
  122. bool thread_initialized;
  123. bool gpu_conversion;
  124. const char *conversion_tech;
  125. uint32_t conversion_height;
  126. uint32_t plane_offsets[3];
  127. uint32_t plane_sizes[3];
  128. uint32_t plane_linewidth[3];
  129. uint32_t output_width;
  130. uint32_t output_height;
  131. uint32_t base_width;
  132. uint32_t base_height;
  133. float color_matrix[16];
  134. enum obs_scale_type scale_type;
  135. struct obs_display main_display;
  136. };
  137. struct obs_core_audio {
  138. /* TODO: sound output subsystem */
  139. audio_t *audio;
  140. float user_volume;
  141. float present_volume;
  142. };
  143. /* user sources, output channels, and displays */
  144. struct obs_core_data {
  145. pthread_mutex_t user_sources_mutex;
  146. DARRAY(struct obs_source*) user_sources;
  147. struct obs_source *first_source;
  148. struct obs_display *first_display;
  149. struct obs_output *first_output;
  150. struct obs_encoder *first_encoder;
  151. struct obs_service *first_service;
  152. pthread_mutex_t sources_mutex;
  153. pthread_mutex_t displays_mutex;
  154. pthread_mutex_t outputs_mutex;
  155. pthread_mutex_t encoders_mutex;
  156. pthread_mutex_t services_mutex;
  157. struct obs_view main_view;
  158. volatile long active_transitions;
  159. long long unnamed_index;
  160. volatile bool valid;
  161. };
  162. struct obs_core {
  163. struct obs_module *first_module;
  164. DARRAY(struct obs_module_path) module_paths;
  165. DARRAY(struct obs_source_info) input_types;
  166. DARRAY(struct obs_source_info) filter_types;
  167. DARRAY(struct obs_source_info) transition_types;
  168. DARRAY(struct obs_output_info) output_types;
  169. DARRAY(struct obs_encoder_info) encoder_types;
  170. DARRAY(struct obs_service_info) service_types;
  171. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  172. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  173. signal_handler_t *signals;
  174. proc_handler_t *procs;
  175. char *locale;
  176. /* segmented into multiple sub-structures to keep things a bit more
  177. * clean and organized */
  178. struct obs_core_video video;
  179. struct obs_core_audio audio;
  180. struct obs_core_data data;
  181. };
  182. extern struct obs_core *obs;
  183. extern void *obs_video_thread(void *param);
  184. /* ------------------------------------------------------------------------- */
  185. /* obs shared context data */
  186. struct obs_context_data {
  187. char *name;
  188. void *data;
  189. obs_data_t *settings;
  190. signal_handler_t *signals;
  191. proc_handler_t *procs;
  192. DARRAY(char*) rename_cache;
  193. pthread_mutex_t rename_cache_mutex;
  194. pthread_mutex_t *mutex;
  195. struct obs_context_data *next;
  196. struct obs_context_data **prev_next;
  197. };
  198. extern bool obs_context_data_init(
  199. struct obs_context_data *context,
  200. obs_data_t *settings,
  201. const char *name);
  202. extern void obs_context_data_free(struct obs_context_data *context);
  203. extern void obs_context_data_insert(struct obs_context_data *context,
  204. pthread_mutex_t *mutex, void *first);
  205. extern void obs_context_data_remove(struct obs_context_data *context);
  206. extern void obs_context_data_setname(struct obs_context_data *context,
  207. const char *name);
  208. /* ------------------------------------------------------------------------- */
  209. /* sources */
  210. struct obs_source {
  211. struct obs_context_data context;
  212. struct obs_source_info info;
  213. volatile long refs;
  214. /* general exposed flags that can be set for the source */
  215. uint32_t flags;
  216. /* indicates ownership of the info.id buffer */
  217. bool owns_info_id;
  218. /* signals to call the source update in the video thread */
  219. bool defer_update;
  220. /* ensures show/hide are only called once */
  221. volatile long show_refs;
  222. /* ensures activate/deactivate are only called once */
  223. volatile long activate_refs;
  224. /* used to indicate that the source has been removed and all
  225. * references to it should be released (not exactly how I would prefer
  226. * to handle things but it's the best option) */
  227. bool removed;
  228. /* timing (if video is present, is based upon video) */
  229. volatile bool timing_set;
  230. volatile uint64_t timing_adjust;
  231. uint64_t next_audio_ts_min;
  232. uint64_t last_frame_ts;
  233. uint64_t last_sys_timestamp;
  234. bool async_rendered;
  235. /* audio */
  236. bool audio_failed;
  237. struct resample_info sample_info;
  238. audio_resampler_t *resampler;
  239. audio_line_t *audio_line;
  240. pthread_mutex_t audio_mutex;
  241. struct obs_audio_data audio_data;
  242. size_t audio_storage_size;
  243. float base_volume;
  244. float user_volume;
  245. float present_volume;
  246. int64_t sync_offset;
  247. /* async video data */
  248. gs_texture_t *async_texture;
  249. gs_texrender_t *async_convert_texrender;
  250. bool async_gpu_conversion;
  251. enum video_format async_format;
  252. enum gs_color_format async_texture_format;
  253. float async_color_matrix[16];
  254. bool async_full_range;
  255. float async_color_range_min[3];
  256. float async_color_range_max[3];
  257. int async_plane_offset[2];
  258. bool async_flip;
  259. DARRAY(struct obs_source_frame*)video_frames;
  260. pthread_mutex_t video_mutex;
  261. uint32_t async_width;
  262. uint32_t async_height;
  263. uint32_t async_convert_width;
  264. uint32_t async_convert_height;
  265. /* filters */
  266. struct obs_source *filter_parent;
  267. struct obs_source *filter_target;
  268. DARRAY(struct obs_source*) filters;
  269. pthread_mutex_t filter_mutex;
  270. gs_texrender_t *filter_texrender;
  271. bool rendering_filter;
  272. };
  273. extern const struct obs_source_info *find_source(struct darray *list,
  274. const char *id);
  275. extern bool obs_source_init_context(struct obs_source *source,
  276. obs_data_t *settings, const char *name);
  277. extern bool obs_source_init(struct obs_source *source,
  278. const struct obs_source_info *info);
  279. extern void obs_source_destroy(struct obs_source *source);
  280. enum view_type {
  281. MAIN_VIEW,
  282. AUX_VIEW
  283. };
  284. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  285. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  286. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  287. extern float obs_source_get_target_volume(obs_source_t *source,
  288. obs_source_t *target);
  289. /* ------------------------------------------------------------------------- */
  290. /* outputs */
  291. struct obs_output {
  292. struct obs_context_data context;
  293. struct obs_output_info info;
  294. bool received_video;
  295. bool received_audio;
  296. int64_t video_offset;
  297. int64_t audio_offset;
  298. int64_t highest_audio_ts;
  299. int64_t highest_video_ts;
  300. pthread_mutex_t interleaved_mutex;
  301. DARRAY(struct encoder_packet) interleaved_packets;
  302. int reconnect_retry_sec;
  303. int reconnect_retry_max;
  304. int reconnect_retries;
  305. bool reconnecting;
  306. pthread_t reconnect_thread;
  307. os_event_t *reconnect_stop_event;
  308. volatile bool reconnect_thread_active;
  309. uint32_t starting_frame_count;
  310. uint32_t starting_skipped_frame_count;
  311. int total_frames;
  312. bool active;
  313. video_t *video;
  314. audio_t *audio;
  315. obs_encoder_t *video_encoder;
  316. obs_encoder_t *audio_encoder;
  317. obs_service_t *service;
  318. uint32_t scaled_width;
  319. uint32_t scaled_height;
  320. bool video_conversion_set;
  321. bool audio_conversion_set;
  322. struct video_scale_info video_conversion;
  323. struct audio_convert_info audio_conversion;
  324. bool valid;
  325. };
  326. extern const struct obs_output_info *find_output(const char *id);
  327. extern void obs_output_remove_encoder(struct obs_output *output,
  328. struct obs_encoder *encoder);
  329. /* ------------------------------------------------------------------------- */
  330. /* encoders */
  331. struct encoder_callback {
  332. bool sent_first_packet;
  333. void (*new_packet)(void *param, struct encoder_packet *packet);
  334. void *param;
  335. };
  336. struct obs_encoder {
  337. struct obs_context_data context;
  338. struct obs_encoder_info info;
  339. uint32_t samplerate;
  340. size_t planes;
  341. size_t blocksize;
  342. size_t framesize;
  343. size_t framesize_bytes;
  344. uint32_t scaled_width;
  345. uint32_t scaled_height;
  346. bool active;
  347. uint32_t timebase_num;
  348. uint32_t timebase_den;
  349. int64_t cur_pts;
  350. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  351. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  352. /* if a video encoder is paired with an audio encoder, make it start
  353. * up at the specific timestamp. if this is the audio encoder,
  354. * wait_for_video makes it wait until it's ready to sync up with
  355. * video */
  356. bool wait_for_video;
  357. struct obs_encoder *paired_encoder;
  358. uint64_t start_ts;
  359. pthread_mutex_t outputs_mutex;
  360. DARRAY(obs_output_t*) outputs;
  361. bool destroy_on_stop;
  362. /* stores the video/audio media output pointer. video_t *or audio_t **/
  363. void *media;
  364. pthread_mutex_t callbacks_mutex;
  365. DARRAY(struct encoder_callback) callbacks;
  366. };
  367. extern struct obs_encoder_info *find_encoder(const char *id);
  368. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  369. extern void obs_encoder_start(obs_encoder_t *encoder,
  370. void (*new_packet)(void *param, struct encoder_packet *packet),
  371. void *param);
  372. extern void obs_encoder_stop(obs_encoder_t *encoder,
  373. void (*new_packet)(void *param, struct encoder_packet *packet),
  374. void *param);
  375. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  376. struct obs_output *output);
  377. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  378. struct obs_output *output);
  379. /* ------------------------------------------------------------------------- */
  380. /* services */
  381. struct obs_service {
  382. struct obs_context_data context;
  383. struct obs_service_info info;
  384. bool active;
  385. bool destroy;
  386. struct obs_output *output;
  387. };
  388. extern const struct obs_service_info *find_service(const char *id);
  389. extern void obs_service_activate(struct obs_service *service);
  390. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  391. extern bool obs_service_initialize(struct obs_service *service,
  392. struct obs_output *output);