obs-internal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. bool enabled;
  89. uint32_t cx, cy;
  90. uint32_t background_color;
  91. gs_swapchain_t *swap;
  92. pthread_mutex_t draw_callbacks_mutex;
  93. DARRAY(struct draw_callback) draw_callbacks;
  94. struct obs_display *next;
  95. struct obs_display **prev_next;
  96. };
  97. extern bool obs_display_init(struct obs_display *display,
  98. const struct gs_init_data *graphics_data);
  99. extern void obs_display_free(struct obs_display *display);
  100. /* ------------------------------------------------------------------------- */
  101. /* core */
  102. struct obs_vframe_info {
  103. uint64_t timestamp;
  104. int count;
  105. };
  106. struct obs_core_video {
  107. graphics_t *graphics;
  108. gs_stagesurf_t *copy_surfaces[NUM_TEXTURES];
  109. gs_texture_t *render_textures[NUM_TEXTURES];
  110. gs_texture_t *output_textures[NUM_TEXTURES];
  111. gs_texture_t *convert_textures[NUM_TEXTURES];
  112. bool textures_rendered[NUM_TEXTURES];
  113. bool textures_output[NUM_TEXTURES];
  114. bool textures_copied[NUM_TEXTURES];
  115. bool textures_converted[NUM_TEXTURES];
  116. struct circlebuf vframe_info_buffer;
  117. gs_effect_t *default_effect;
  118. gs_effect_t *default_rect_effect;
  119. gs_effect_t *opaque_effect;
  120. gs_effect_t *solid_effect;
  121. gs_effect_t *conversion_effect;
  122. gs_effect_t *bicubic_effect;
  123. gs_effect_t *lanczos_effect;
  124. gs_effect_t *bilinear_lowres_effect;
  125. gs_stagesurf_t *mapped_surface;
  126. int cur_texture;
  127. video_t *video;
  128. pthread_t video_thread;
  129. bool thread_initialized;
  130. bool gpu_conversion;
  131. const char *conversion_tech;
  132. uint32_t conversion_height;
  133. uint32_t plane_offsets[3];
  134. uint32_t plane_sizes[3];
  135. uint32_t plane_linewidth[3];
  136. uint32_t output_width;
  137. uint32_t output_height;
  138. uint32_t base_width;
  139. uint32_t base_height;
  140. float color_matrix[16];
  141. enum obs_scale_type scale_type;
  142. struct obs_display main_display;
  143. };
  144. struct obs_core_audio {
  145. /* TODO: sound output subsystem */
  146. audio_t *audio;
  147. float user_volume;
  148. float present_volume;
  149. };
  150. /* user sources, output channels, and displays */
  151. struct obs_core_data {
  152. pthread_mutex_t user_sources_mutex;
  153. DARRAY(struct obs_source*) user_sources;
  154. struct obs_source *first_source;
  155. struct obs_display *first_display;
  156. struct obs_output *first_output;
  157. struct obs_encoder *first_encoder;
  158. struct obs_service *first_service;
  159. pthread_mutex_t sources_mutex;
  160. pthread_mutex_t displays_mutex;
  161. pthread_mutex_t outputs_mutex;
  162. pthread_mutex_t encoders_mutex;
  163. pthread_mutex_t services_mutex;
  164. struct obs_view main_view;
  165. volatile long active_transitions;
  166. long long unnamed_index;
  167. volatile bool valid;
  168. };
  169. struct obs_core {
  170. struct obs_module *first_module;
  171. DARRAY(struct obs_module_path) module_paths;
  172. DARRAY(struct obs_source_info) input_types;
  173. DARRAY(struct obs_source_info) filter_types;
  174. DARRAY(struct obs_source_info) transition_types;
  175. DARRAY(struct obs_output_info) output_types;
  176. DARRAY(struct obs_encoder_info) encoder_types;
  177. DARRAY(struct obs_service_info) service_types;
  178. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  179. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  180. signal_handler_t *signals;
  181. proc_handler_t *procs;
  182. char *locale;
  183. /* segmented into multiple sub-structures to keep things a bit more
  184. * clean and organized */
  185. struct obs_core_video video;
  186. struct obs_core_audio audio;
  187. struct obs_core_data data;
  188. };
  189. extern struct obs_core *obs;
  190. extern void *obs_video_thread(void *param);
  191. /* ------------------------------------------------------------------------- */
  192. /* obs shared context data */
  193. struct obs_context_data {
  194. char *name;
  195. void *data;
  196. obs_data_t *settings;
  197. signal_handler_t *signals;
  198. proc_handler_t *procs;
  199. DARRAY(char*) rename_cache;
  200. pthread_mutex_t rename_cache_mutex;
  201. pthread_mutex_t *mutex;
  202. struct obs_context_data *next;
  203. struct obs_context_data **prev_next;
  204. };
  205. extern bool obs_context_data_init(
  206. struct obs_context_data *context,
  207. obs_data_t *settings,
  208. const char *name);
  209. extern void obs_context_data_free(struct obs_context_data *context);
  210. extern void obs_context_data_insert(struct obs_context_data *context,
  211. pthread_mutex_t *mutex, void *first);
  212. extern void obs_context_data_remove(struct obs_context_data *context);
  213. extern void obs_context_data_setname(struct obs_context_data *context,
  214. const char *name);
  215. /* ------------------------------------------------------------------------- */
  216. /* ref-counting */
  217. struct obs_weak_ref {
  218. volatile long refs;
  219. volatile long weak_refs;
  220. };
  221. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  222. {
  223. os_atomic_inc_long(&ref->refs);
  224. }
  225. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  226. {
  227. return os_atomic_dec_long(&ref->refs) == -1;
  228. }
  229. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  230. {
  231. os_atomic_inc_long(&ref->weak_refs);
  232. }
  233. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  234. {
  235. return os_atomic_dec_long(&ref->weak_refs) == -1;
  236. }
  237. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  238. {
  239. long owners = ref->refs;
  240. while (owners > -1) {
  241. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  242. return true;
  243. owners = ref->refs;
  244. }
  245. return false;
  246. }
  247. /* ------------------------------------------------------------------------- */
  248. /* sources */
  249. struct async_frame {
  250. struct obs_source_frame *frame;
  251. long unused_count;
  252. bool used;
  253. };
  254. struct obs_weak_source {
  255. struct obs_weak_ref ref;
  256. struct obs_source *source;
  257. };
  258. struct obs_source {
  259. struct obs_context_data context;
  260. struct obs_source_info info;
  261. struct obs_weak_source *control;
  262. /* general exposed flags that can be set for the source */
  263. uint32_t flags;
  264. uint32_t default_flags;
  265. /* indicates ownership of the info.id buffer */
  266. bool owns_info_id;
  267. /* signals to call the source update in the video thread */
  268. bool defer_update;
  269. /* ensures show/hide are only called once */
  270. volatile long show_refs;
  271. /* ensures activate/deactivate are only called once */
  272. volatile long activate_refs;
  273. /* used to indicate that the source has been removed and all
  274. * references to it should be released (not exactly how I would prefer
  275. * to handle things but it's the best option) */
  276. bool removed;
  277. bool active;
  278. bool showing;
  279. /* used to temporarily disable sources if needed */
  280. bool enabled;
  281. /* timing (if video is present, is based upon video) */
  282. volatile bool timing_set;
  283. volatile uint64_t timing_adjust;
  284. uint64_t next_audio_ts_min;
  285. uint64_t last_frame_ts;
  286. uint64_t last_sys_timestamp;
  287. bool async_rendered;
  288. /* audio */
  289. bool audio_failed;
  290. bool muted;
  291. struct resample_info sample_info;
  292. audio_resampler_t *resampler;
  293. audio_line_t *audio_line;
  294. pthread_mutex_t audio_mutex;
  295. struct obs_audio_data audio_data;
  296. size_t audio_storage_size;
  297. float base_volume;
  298. float user_volume;
  299. float present_volume;
  300. int64_t sync_offset;
  301. /* async video data */
  302. gs_texture_t *async_texture;
  303. gs_texrender_t *async_convert_texrender;
  304. struct obs_source_frame *cur_async_frame;
  305. bool async_gpu_conversion;
  306. enum video_format async_format;
  307. enum video_format async_cache_format;
  308. enum gs_color_format async_texture_format;
  309. float async_color_matrix[16];
  310. bool async_full_range;
  311. float async_color_range_min[3];
  312. float async_color_range_max[3];
  313. int async_plane_offset[2];
  314. bool async_flip;
  315. bool async_active;
  316. DARRAY(struct async_frame) async_cache;
  317. DARRAY(struct obs_source_frame*)async_frames;
  318. pthread_mutex_t async_mutex;
  319. uint32_t async_width;
  320. uint32_t async_height;
  321. uint32_t async_cache_width;
  322. uint32_t async_cache_height;
  323. uint32_t async_convert_width;
  324. uint32_t async_convert_height;
  325. /* filters */
  326. struct obs_source *filter_parent;
  327. struct obs_source *filter_target;
  328. DARRAY(struct obs_source*) filters;
  329. pthread_mutex_t filter_mutex;
  330. gs_texrender_t *filter_texrender;
  331. enum obs_allow_direct_render allow_direct;
  332. bool rendering_filter;
  333. };
  334. extern const struct obs_source_info *find_source(struct darray *list,
  335. const char *id);
  336. extern bool obs_source_init_context(struct obs_source *source,
  337. obs_data_t *settings, const char *name);
  338. extern bool obs_source_init(struct obs_source *source,
  339. const struct obs_source_info *info);
  340. extern void obs_source_destroy(struct obs_source *source);
  341. enum view_type {
  342. MAIN_VIEW,
  343. AUX_VIEW
  344. };
  345. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  346. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  347. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  348. extern float obs_source_get_target_volume(obs_source_t *source,
  349. obs_source_t *target);
  350. /* ------------------------------------------------------------------------- */
  351. /* outputs */
  352. struct obs_weak_output {
  353. struct obs_weak_ref ref;
  354. struct obs_output *output;
  355. };
  356. struct obs_output {
  357. struct obs_context_data context;
  358. struct obs_output_info info;
  359. struct obs_weak_output *control;
  360. bool received_video;
  361. bool received_audio;
  362. int64_t video_offset;
  363. int64_t audio_offsets[MAX_AUDIO_MIXES];
  364. int64_t highest_audio_ts;
  365. int64_t highest_video_ts;
  366. pthread_mutex_t interleaved_mutex;
  367. DARRAY(struct encoder_packet) interleaved_packets;
  368. int reconnect_retry_sec;
  369. int reconnect_retry_max;
  370. int reconnect_retries;
  371. bool reconnecting;
  372. pthread_t reconnect_thread;
  373. os_event_t *reconnect_stop_event;
  374. volatile bool reconnect_thread_active;
  375. uint32_t starting_frame_count;
  376. uint32_t starting_skipped_frame_count;
  377. int total_frames;
  378. bool active;
  379. volatile bool stopped;
  380. video_t *video;
  381. audio_t *audio;
  382. obs_encoder_t *video_encoder;
  383. obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
  384. obs_service_t *service;
  385. size_t mixer_idx;
  386. uint32_t scaled_width;
  387. uint32_t scaled_height;
  388. bool video_conversion_set;
  389. bool audio_conversion_set;
  390. struct video_scale_info video_conversion;
  391. struct audio_convert_info audio_conversion;
  392. bool valid;
  393. };
  394. extern const struct obs_output_info *find_output(const char *id);
  395. extern void obs_output_remove_encoder(struct obs_output *output,
  396. struct obs_encoder *encoder);
  397. void obs_output_destroy(obs_output_t *output);
  398. /* ------------------------------------------------------------------------- */
  399. /* encoders */
  400. struct encoder_callback {
  401. bool sent_first_packet;
  402. void (*new_packet)(void *param, struct encoder_packet *packet);
  403. void *param;
  404. };
  405. struct obs_encoder {
  406. struct obs_context_data context;
  407. struct obs_encoder_info info;
  408. uint32_t samplerate;
  409. size_t planes;
  410. size_t blocksize;
  411. size_t framesize;
  412. size_t framesize_bytes;
  413. size_t mixer_idx;
  414. uint32_t scaled_width;
  415. uint32_t scaled_height;
  416. enum video_format preferred_format;
  417. bool active;
  418. uint32_t timebase_num;
  419. uint32_t timebase_den;
  420. int64_t cur_pts;
  421. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  422. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  423. /* if a video encoder is paired with an audio encoder, make it start
  424. * up at the specific timestamp. if this is the audio encoder,
  425. * wait_for_video makes it wait until it's ready to sync up with
  426. * video */
  427. bool wait_for_video;
  428. struct obs_encoder *paired_encoder;
  429. uint64_t start_ts;
  430. pthread_mutex_t outputs_mutex;
  431. DARRAY(obs_output_t*) outputs;
  432. bool destroy_on_stop;
  433. /* stores the video/audio media output pointer. video_t *or audio_t **/
  434. void *media;
  435. pthread_mutex_t callbacks_mutex;
  436. DARRAY(struct encoder_callback) callbacks;
  437. };
  438. extern struct obs_encoder_info *find_encoder(const char *id);
  439. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  440. extern void obs_encoder_start(obs_encoder_t *encoder,
  441. void (*new_packet)(void *param, struct encoder_packet *packet),
  442. void *param);
  443. extern void obs_encoder_stop(obs_encoder_t *encoder,
  444. void (*new_packet)(void *param, struct encoder_packet *packet),
  445. void *param);
  446. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  447. struct obs_output *output);
  448. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  449. struct obs_output *output);
  450. /* ------------------------------------------------------------------------- */
  451. /* services */
  452. struct obs_service {
  453. struct obs_context_data context;
  454. struct obs_service_info info;
  455. bool active;
  456. bool destroy;
  457. struct obs_output *output;
  458. };
  459. extern const struct obs_service_info *find_service(const char *id);
  460. extern void obs_service_activate(struct obs_service *service);
  461. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  462. extern bool obs_service_initialize(struct obs_service *service,
  463. struct obs_output *output);