obs-internal.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 "util/profiler.h"
  22. #include "callback/signal.h"
  23. #include "callback/proc.h"
  24. #include "graphics/graphics.h"
  25. #include "graphics/matrix4.h"
  26. #include "media-io/audio-resampler.h"
  27. #include "media-io/video-io.h"
  28. #include "media-io/audio-io.h"
  29. #include "obs.h"
  30. #define NUM_TEXTURES 2
  31. #define MICROSECOND_DEN 1000000
  32. static inline int64_t packet_dts_usec(struct encoder_packet *packet)
  33. {
  34. return packet->dts * MICROSECOND_DEN / packet->timebase_den;
  35. }
  36. struct tick_callback {
  37. void (*tick)(void *param, float seconds);
  38. void *param;
  39. };
  40. struct draw_callback {
  41. void (*draw)(void *param, uint32_t cx, uint32_t cy);
  42. void *param;
  43. };
  44. /* ------------------------------------------------------------------------- */
  45. /* validity checks */
  46. static inline bool obs_object_valid(const void *obj, const char *f,
  47. const char *t)
  48. {
  49. if (!obj) {
  50. blog(LOG_DEBUG, "%s: Null '%s' parameter", f, t);
  51. return false;
  52. }
  53. return true;
  54. }
  55. #define obs_ptr_valid(ptr, func) obs_object_valid(ptr, func, #ptr)
  56. #define obs_source_valid obs_ptr_valid
  57. #define obs_output_valid obs_ptr_valid
  58. #define obs_encoder_valid obs_ptr_valid
  59. #define obs_service_valid obs_ptr_valid
  60. /* ------------------------------------------------------------------------- */
  61. /* modules */
  62. struct obs_module {
  63. char *mod_name;
  64. const char *file;
  65. char *bin_path;
  66. char *data_path;
  67. void *module;
  68. bool loaded;
  69. bool (*load)(void);
  70. void (*unload)(void);
  71. void (*post_load)(void);
  72. void (*set_locale)(const char *locale);
  73. void (*free_locale)(void);
  74. uint32_t (*ver)(void);
  75. void (*set_pointer)(obs_module_t *module);
  76. const char *(*name)(void);
  77. const char *(*description)(void);
  78. const char *(*author)(void);
  79. struct obs_module *next;
  80. };
  81. extern void free_module(struct obs_module *mod);
  82. struct obs_module_path {
  83. char *bin;
  84. char *data;
  85. };
  86. static inline void free_module_path(struct obs_module_path *omp)
  87. {
  88. if (omp) {
  89. bfree(omp->bin);
  90. bfree(omp->data);
  91. }
  92. }
  93. static inline bool check_path(const char *data, const char *path,
  94. struct dstr *output)
  95. {
  96. dstr_copy(output, path);
  97. dstr_cat(output, data);
  98. return os_file_exists(output->array);
  99. }
  100. /* ------------------------------------------------------------------------- */
  101. /* hotkeys */
  102. struct obs_hotkey {
  103. obs_hotkey_id id;
  104. char *name;
  105. char *description;
  106. obs_hotkey_func func;
  107. void *data;
  108. int pressed;
  109. obs_hotkey_registerer_t registerer_type;
  110. void *registerer;
  111. obs_hotkey_id pair_partner_id;
  112. };
  113. struct obs_hotkey_pair {
  114. obs_hotkey_pair_id pair_id;
  115. obs_hotkey_id id[2];
  116. obs_hotkey_active_func func[2];
  117. bool pressed0;
  118. bool pressed1;
  119. void *data[2];
  120. };
  121. typedef struct obs_hotkey_pair obs_hotkey_pair_t;
  122. typedef struct obs_hotkeys_platform obs_hotkeys_platform_t;
  123. void *obs_hotkey_thread(void *param);
  124. struct obs_core_hotkeys;
  125. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys);
  126. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys);
  127. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  128. obs_key_t key);
  129. const char *obs_get_hotkey_translation(obs_key_t key, const char *def);
  130. struct obs_context_data;
  131. void obs_hotkeys_context_release(struct obs_context_data *context);
  132. void obs_hotkeys_free(void);
  133. struct obs_hotkey_binding {
  134. obs_key_combination_t key;
  135. bool pressed;
  136. bool modifiers_match;
  137. obs_hotkey_id hotkey_id;
  138. obs_hotkey_t *hotkey;
  139. };
  140. struct obs_hotkey_name_map;
  141. void obs_hotkey_name_map_free(void);
  142. /* ------------------------------------------------------------------------- */
  143. /* views */
  144. struct obs_view {
  145. pthread_mutex_t channels_mutex;
  146. obs_source_t *channels[MAX_CHANNELS];
  147. };
  148. extern bool obs_view_init(struct obs_view *view);
  149. extern void obs_view_free(struct obs_view *view);
  150. /* ------------------------------------------------------------------------- */
  151. /* displays */
  152. struct obs_display {
  153. bool size_changed;
  154. bool enabled;
  155. uint32_t cx, cy;
  156. uint32_t background_color;
  157. gs_swapchain_t *swap;
  158. pthread_mutex_t draw_callbacks_mutex;
  159. pthread_mutex_t draw_info_mutex;
  160. DARRAY(struct draw_callback) draw_callbacks;
  161. struct obs_display *next;
  162. struct obs_display **prev_next;
  163. };
  164. extern bool obs_display_init(struct obs_display *display,
  165. const struct gs_init_data *graphics_data);
  166. extern void obs_display_free(struct obs_display *display);
  167. /* ------------------------------------------------------------------------- */
  168. /* core */
  169. struct obs_vframe_info {
  170. uint64_t timestamp;
  171. int count;
  172. };
  173. struct obs_core_video {
  174. graphics_t *graphics;
  175. gs_stagesurf_t *copy_surfaces[NUM_TEXTURES];
  176. gs_texture_t *render_textures[NUM_TEXTURES];
  177. gs_texture_t *output_textures[NUM_TEXTURES];
  178. gs_texture_t *convert_textures[NUM_TEXTURES];
  179. bool textures_rendered[NUM_TEXTURES];
  180. bool textures_output[NUM_TEXTURES];
  181. bool textures_copied[NUM_TEXTURES];
  182. bool textures_converted[NUM_TEXTURES];
  183. struct circlebuf vframe_info_buffer;
  184. gs_effect_t *default_effect;
  185. gs_effect_t *default_rect_effect;
  186. gs_effect_t *opaque_effect;
  187. gs_effect_t *solid_effect;
  188. gs_effect_t *conversion_effect;
  189. gs_effect_t *bicubic_effect;
  190. gs_effect_t *lanczos_effect;
  191. gs_effect_t *bilinear_lowres_effect;
  192. gs_effect_t *premultiplied_alpha_effect;
  193. gs_samplerstate_t *point_sampler;
  194. gs_stagesurf_t *mapped_surface;
  195. int cur_texture;
  196. uint64_t video_time;
  197. uint64_t video_avg_frame_time_ns;
  198. double video_fps;
  199. video_t *video;
  200. pthread_t video_thread;
  201. uint32_t total_frames;
  202. uint32_t lagged_frames;
  203. bool thread_initialized;
  204. bool gpu_conversion;
  205. const char *conversion_tech;
  206. uint32_t conversion_height;
  207. uint32_t plane_offsets[3];
  208. uint32_t plane_sizes[3];
  209. uint32_t plane_linewidth[3];
  210. uint32_t output_width;
  211. uint32_t output_height;
  212. uint32_t base_width;
  213. uint32_t base_height;
  214. float color_matrix[16];
  215. enum obs_scale_type scale_type;
  216. gs_texture_t *transparent_texture;
  217. gs_effect_t *deinterlace_discard_effect;
  218. gs_effect_t *deinterlace_discard_2x_effect;
  219. gs_effect_t *deinterlace_linear_effect;
  220. gs_effect_t *deinterlace_linear_2x_effect;
  221. gs_effect_t *deinterlace_blend_effect;
  222. gs_effect_t *deinterlace_blend_2x_effect;
  223. gs_effect_t *deinterlace_yadif_effect;
  224. gs_effect_t *deinterlace_yadif_2x_effect;
  225. struct obs_video_info ovi;
  226. };
  227. struct audio_monitor;
  228. struct obs_core_audio {
  229. audio_t *audio;
  230. DARRAY(struct obs_source*) render_order;
  231. DARRAY(struct obs_source*) root_nodes;
  232. uint64_t buffered_ts;
  233. struct circlebuf buffered_timestamps;
  234. int buffering_wait_ticks;
  235. int total_buffering_ticks;
  236. float user_volume;
  237. pthread_mutex_t monitoring_mutex;
  238. DARRAY(struct audio_monitor*) monitors;
  239. char *monitoring_device_name;
  240. char *monitoring_device_id;
  241. };
  242. /* user sources, output channels, and displays */
  243. struct obs_core_data {
  244. struct obs_source *first_source;
  245. struct obs_source *first_audio_source;
  246. struct obs_display *first_display;
  247. struct obs_output *first_output;
  248. struct obs_encoder *first_encoder;
  249. struct obs_service *first_service;
  250. pthread_mutex_t sources_mutex;
  251. pthread_mutex_t displays_mutex;
  252. pthread_mutex_t outputs_mutex;
  253. pthread_mutex_t encoders_mutex;
  254. pthread_mutex_t services_mutex;
  255. pthread_mutex_t audio_sources_mutex;
  256. pthread_mutex_t draw_callbacks_mutex;
  257. DARRAY(struct draw_callback) draw_callbacks;
  258. DARRAY(struct tick_callback) tick_callbacks;
  259. struct obs_view main_view;
  260. long long unnamed_index;
  261. volatile bool valid;
  262. };
  263. /* user hotkeys */
  264. struct obs_core_hotkeys {
  265. pthread_mutex_t mutex;
  266. DARRAY(obs_hotkey_t) hotkeys;
  267. obs_hotkey_id next_id;
  268. DARRAY(obs_hotkey_pair_t) hotkey_pairs;
  269. obs_hotkey_pair_id next_pair_id;
  270. pthread_t hotkey_thread;
  271. bool hotkey_thread_initialized;
  272. os_event_t *stop_event;
  273. bool thread_disable_press;
  274. bool strict_modifiers;
  275. bool reroute_hotkeys;
  276. DARRAY(obs_hotkey_binding_t) bindings;
  277. obs_hotkey_callback_router_func router_func;
  278. void *router_func_data;
  279. obs_hotkeys_platform_t *platform_context;
  280. pthread_once_t name_map_init_token;
  281. struct obs_hotkey_name_map *name_map;
  282. signal_handler_t *signals;
  283. char *translations[OBS_KEY_LAST_VALUE];
  284. char *mute;
  285. char *unmute;
  286. char *push_to_mute;
  287. char *push_to_talk;
  288. char *sceneitem_show;
  289. char *sceneitem_hide;
  290. };
  291. struct obs_core {
  292. struct obs_module *first_module;
  293. DARRAY(struct obs_module_path) module_paths;
  294. DARRAY(struct obs_source_info) source_types;
  295. DARRAY(struct obs_source_info) input_types;
  296. DARRAY(struct obs_source_info) filter_types;
  297. DARRAY(struct obs_source_info) transition_types;
  298. DARRAY(struct obs_output_info) output_types;
  299. DARRAY(struct obs_encoder_info) encoder_types;
  300. DARRAY(struct obs_service_info) service_types;
  301. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  302. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  303. signal_handler_t *signals;
  304. proc_handler_t *procs;
  305. char *locale;
  306. char *module_config_path;
  307. bool name_store_owned;
  308. profiler_name_store_t *name_store;
  309. /* segmented into multiple sub-structures to keep things a bit more
  310. * clean and organized */
  311. struct obs_core_video video;
  312. struct obs_core_audio audio;
  313. struct obs_core_data data;
  314. struct obs_core_hotkeys hotkeys;
  315. };
  316. extern struct obs_core *obs;
  317. extern void *obs_graphics_thread(void *param);
  318. extern gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file);
  319. extern bool audio_callback(void *param,
  320. uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts,
  321. uint32_t mixers, struct audio_output_data *mixes);
  322. /* ------------------------------------------------------------------------- */
  323. /* obs shared context data */
  324. struct obs_context_data {
  325. char *name;
  326. void *data;
  327. obs_data_t *settings;
  328. signal_handler_t *signals;
  329. proc_handler_t *procs;
  330. enum obs_obj_type type;
  331. DARRAY(obs_hotkey_id) hotkeys;
  332. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  333. obs_data_t *hotkey_data;
  334. DARRAY(char*) rename_cache;
  335. pthread_mutex_t rename_cache_mutex;
  336. pthread_mutex_t *mutex;
  337. struct obs_context_data *next;
  338. struct obs_context_data **prev_next;
  339. bool private;
  340. };
  341. extern bool obs_context_data_init(
  342. struct obs_context_data *context,
  343. enum obs_obj_type type,
  344. obs_data_t *settings,
  345. const char *name,
  346. obs_data_t *hotkey_data,
  347. bool private);
  348. extern void obs_context_data_free(struct obs_context_data *context);
  349. extern void obs_context_data_insert(struct obs_context_data *context,
  350. pthread_mutex_t *mutex, void *first);
  351. extern void obs_context_data_remove(struct obs_context_data *context);
  352. extern void obs_context_data_setname(struct obs_context_data *context,
  353. const char *name);
  354. /* ------------------------------------------------------------------------- */
  355. /* ref-counting */
  356. struct obs_weak_ref {
  357. volatile long refs;
  358. volatile long weak_refs;
  359. };
  360. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  361. {
  362. os_atomic_inc_long(&ref->refs);
  363. }
  364. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  365. {
  366. return os_atomic_dec_long(&ref->refs) == -1;
  367. }
  368. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  369. {
  370. os_atomic_inc_long(&ref->weak_refs);
  371. }
  372. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  373. {
  374. return os_atomic_dec_long(&ref->weak_refs) == -1;
  375. }
  376. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  377. {
  378. long owners = ref->refs;
  379. while (owners > -1) {
  380. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  381. return true;
  382. owners = ref->refs;
  383. }
  384. return false;
  385. }
  386. /* ------------------------------------------------------------------------- */
  387. /* sources */
  388. struct async_frame {
  389. struct obs_source_frame *frame;
  390. long unused_count;
  391. bool used;
  392. };
  393. enum audio_action_type {
  394. AUDIO_ACTION_VOL,
  395. AUDIO_ACTION_MUTE,
  396. AUDIO_ACTION_PTT,
  397. AUDIO_ACTION_PTM,
  398. };
  399. struct audio_action {
  400. uint64_t timestamp;
  401. enum audio_action_type type;
  402. union {
  403. float vol;
  404. bool set;
  405. };
  406. };
  407. struct obs_weak_source {
  408. struct obs_weak_ref ref;
  409. struct obs_source *source;
  410. };
  411. struct audio_cb_info {
  412. obs_source_audio_capture_t callback;
  413. void *param;
  414. };
  415. struct obs_source {
  416. struct obs_context_data context;
  417. struct obs_source_info info;
  418. struct obs_weak_source *control;
  419. /* general exposed flags that can be set for the source */
  420. uint32_t flags;
  421. uint32_t default_flags;
  422. /* indicates ownership of the info.id buffer */
  423. bool owns_info_id;
  424. /* signals to call the source update in the video thread */
  425. bool defer_update;
  426. /* ensures show/hide are only called once */
  427. volatile long show_refs;
  428. /* ensures activate/deactivate are only called once */
  429. volatile long activate_refs;
  430. /* used to indicate that the source has been removed and all
  431. * references to it should be released (not exactly how I would prefer
  432. * to handle things but it's the best option) */
  433. bool removed;
  434. bool active;
  435. bool showing;
  436. /* used to temporarily disable sources if needed */
  437. bool enabled;
  438. /* timing (if video is present, is based upon video) */
  439. volatile bool timing_set;
  440. volatile uint64_t timing_adjust;
  441. uint64_t resample_offset;
  442. uint64_t last_audio_ts;
  443. uint64_t next_audio_ts_min;
  444. uint64_t next_audio_sys_ts_min;
  445. uint64_t last_frame_ts;
  446. uint64_t last_sys_timestamp;
  447. bool async_rendered;
  448. /* audio */
  449. bool audio_failed;
  450. bool audio_pending;
  451. bool pending_stop;
  452. bool user_muted;
  453. bool muted;
  454. struct obs_source *next_audio_source;
  455. struct obs_source **prev_next_audio_source;
  456. uint64_t audio_ts;
  457. struct circlebuf audio_input_buf[MAX_AUDIO_CHANNELS];
  458. size_t last_audio_input_buf_size;
  459. DARRAY(struct audio_action) audio_actions;
  460. float *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
  461. struct resample_info sample_info;
  462. audio_resampler_t *resampler;
  463. pthread_mutex_t audio_actions_mutex;
  464. pthread_mutex_t audio_buf_mutex;
  465. pthread_mutex_t audio_mutex;
  466. pthread_mutex_t audio_cb_mutex;
  467. DARRAY(struct audio_cb_info) audio_cb_list;
  468. struct obs_audio_data audio_data;
  469. size_t audio_storage_size;
  470. uint32_t audio_mixers;
  471. float user_volume;
  472. float volume;
  473. int64_t sync_offset;
  474. int64_t last_sync_offset;
  475. /* async video data */
  476. gs_texture_t *async_texture;
  477. gs_texrender_t *async_texrender;
  478. struct obs_source_frame *cur_async_frame;
  479. bool async_gpu_conversion;
  480. enum video_format async_format;
  481. enum video_format async_cache_format;
  482. enum gs_color_format async_texture_format;
  483. float async_color_matrix[16];
  484. bool async_full_range;
  485. float async_color_range_min[3];
  486. float async_color_range_max[3];
  487. int async_plane_offset[2];
  488. bool async_flip;
  489. bool async_active;
  490. bool async_update_texture;
  491. bool async_unbuffered;
  492. bool async_decoupled;
  493. struct obs_source_frame *async_preload_frame;
  494. DARRAY(struct async_frame) async_cache;
  495. DARRAY(struct obs_source_frame*)async_frames;
  496. pthread_mutex_t async_mutex;
  497. uint32_t async_width;
  498. uint32_t async_height;
  499. uint32_t async_cache_width;
  500. uint32_t async_cache_height;
  501. uint32_t async_convert_width;
  502. uint32_t async_convert_height;
  503. /* async video deinterlacing */
  504. uint64_t deinterlace_offset;
  505. uint64_t deinterlace_frame_ts;
  506. gs_effect_t *deinterlace_effect;
  507. struct obs_source_frame *prev_async_frame;
  508. gs_texture_t *async_prev_texture;
  509. gs_texrender_t *async_prev_texrender;
  510. uint32_t deinterlace_half_duration;
  511. enum obs_deinterlace_mode deinterlace_mode;
  512. bool deinterlace_top_first;
  513. bool deinterlace_rendered;
  514. /* filters */
  515. struct obs_source *filter_parent;
  516. struct obs_source *filter_target;
  517. DARRAY(struct obs_source*) filters;
  518. pthread_mutex_t filter_mutex;
  519. gs_texrender_t *filter_texrender;
  520. enum obs_allow_direct_render allow_direct;
  521. bool rendering_filter;
  522. /* sources specific hotkeys */
  523. obs_hotkey_pair_id mute_unmute_key;
  524. obs_hotkey_id push_to_mute_key;
  525. obs_hotkey_id push_to_talk_key;
  526. bool push_to_mute_enabled;
  527. bool push_to_mute_pressed;
  528. bool user_push_to_mute_pressed;
  529. bool push_to_talk_enabled;
  530. bool push_to_talk_pressed;
  531. bool user_push_to_talk_pressed;
  532. uint64_t push_to_mute_delay;
  533. uint64_t push_to_mute_stop_time;
  534. uint64_t push_to_talk_delay;
  535. uint64_t push_to_talk_stop_time;
  536. /* transitions */
  537. uint64_t transition_start_time;
  538. uint64_t transition_duration;
  539. pthread_mutex_t transition_tex_mutex;
  540. gs_texrender_t *transition_texrender[2];
  541. pthread_mutex_t transition_mutex;
  542. obs_source_t *transition_sources[2];
  543. bool transitioning_video;
  544. bool transitioning_audio;
  545. bool transition_source_active[2];
  546. uint32_t transition_alignment;
  547. uint32_t transition_actual_cx;
  548. uint32_t transition_actual_cy;
  549. uint32_t transition_cx;
  550. uint32_t transition_cy;
  551. uint32_t transition_fixed_duration;
  552. bool transition_use_fixed_duration;
  553. enum obs_transition_mode transition_mode;
  554. enum obs_transition_scale_type transition_scale_type;
  555. struct matrix4 transition_matrices[2];
  556. struct audio_monitor *monitor;
  557. enum obs_monitoring_type monitoring_type;
  558. obs_data_t *private_settings;
  559. };
  560. extern struct obs_source_info *get_source_info(const char *id);
  561. extern bool obs_source_init_context(struct obs_source *source,
  562. obs_data_t *settings, const char *name,
  563. obs_data_t *hotkey_data, bool private);
  564. extern void obs_source_save(obs_source_t *source);
  565. extern void obs_source_load(obs_source_t *source);
  566. extern bool obs_transition_init(obs_source_t *transition);
  567. extern void obs_transition_free(obs_source_t *transition);
  568. extern void obs_transition_tick(obs_source_t *transition);
  569. extern void obs_transition_enum_sources(obs_source_t *transition,
  570. obs_source_enum_proc_t enum_callback, void *param);
  571. extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
  572. extern void obs_transition_load(obs_source_t *source, obs_data_t *data);
  573. struct audio_monitor *audio_monitor_create(obs_source_t *source);
  574. void audio_monitor_reset(struct audio_monitor *monitor);
  575. extern void audio_monitor_destroy(struct audio_monitor *monitor);
  576. extern void obs_source_destroy(struct obs_source *source);
  577. enum view_type {
  578. MAIN_VIEW,
  579. AUX_VIEW
  580. };
  581. static inline void obs_source_dosignal(struct obs_source *source,
  582. const char *signal_obs, const char *signal_source)
  583. {
  584. struct calldata data;
  585. uint8_t stack[128];
  586. calldata_init_fixed(&data, stack, sizeof(stack));
  587. calldata_set_ptr(&data, "source", source);
  588. if (signal_obs && !source->context.private)
  589. signal_handler_signal(obs->signals, signal_obs, &data);
  590. if (signal_source)
  591. signal_handler_signal(source->context.signals, signal_source,
  592. &data);
  593. }
  594. /* maximum timestamp variance in nanoseconds */
  595. #define MAX_TS_VAR 2000000000ULL
  596. static inline bool frame_out_of_bounds(const obs_source_t *source, uint64_t ts)
  597. {
  598. if (ts < source->last_frame_ts)
  599. return ((source->last_frame_ts - ts) > MAX_TS_VAR);
  600. else
  601. return ((ts - source->last_frame_ts) > MAX_TS_VAR);
  602. }
  603. static inline enum gs_color_format convert_video_format(
  604. enum video_format format)
  605. {
  606. if (format == VIDEO_FORMAT_RGBA)
  607. return GS_RGBA;
  608. else if (format == VIDEO_FORMAT_BGRA)
  609. return GS_BGRA;
  610. return GS_BGRX;
  611. }
  612. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  613. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  614. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  615. extern float obs_source_get_target_volume(obs_source_t *source,
  616. obs_source_t *target);
  617. extern void obs_source_audio_render(obs_source_t *source, uint32_t mixers,
  618. size_t channels, size_t sample_rate, size_t size);
  619. extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);
  620. extern struct obs_source_frame *filter_async_video(obs_source_t *source,
  621. struct obs_source_frame *in);
  622. extern bool update_async_texture(struct obs_source *source,
  623. const struct obs_source_frame *frame,
  624. gs_texture_t *tex, gs_texrender_t *texrender);
  625. extern bool set_async_texture_size(struct obs_source *source,
  626. const struct obs_source_frame *frame);
  627. extern void remove_async_frame(obs_source_t *source,
  628. struct obs_source_frame *frame);
  629. extern void set_deinterlace_texture_size(obs_source_t *source);
  630. extern void deinterlace_process_last_frame(obs_source_t *source,
  631. uint64_t sys_time);
  632. extern void deinterlace_update_async_video(obs_source_t *source);
  633. extern void deinterlace_render(obs_source_t *s);
  634. /* ------------------------------------------------------------------------- */
  635. /* outputs */
  636. enum delay_msg {
  637. DELAY_MSG_PACKET,
  638. DELAY_MSG_START,
  639. DELAY_MSG_STOP,
  640. };
  641. struct delay_data {
  642. enum delay_msg msg;
  643. uint64_t ts;
  644. struct encoder_packet packet;
  645. };
  646. typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);
  647. struct obs_weak_output {
  648. struct obs_weak_ref ref;
  649. struct obs_output *output;
  650. };
  651. #define CAPTION_LINE_CHARS (32)
  652. #define CAPTION_LINE_BYTES (4*CAPTION_LINE_CHARS)
  653. struct caption_text {
  654. char text[CAPTION_LINE_BYTES+1];
  655. struct caption_text *next;
  656. };
  657. struct obs_output {
  658. struct obs_context_data context;
  659. struct obs_output_info info;
  660. struct obs_weak_output *control;
  661. /* indicates ownership of the info.id buffer */
  662. bool owns_info_id;
  663. bool received_video;
  664. bool received_audio;
  665. volatile bool data_active;
  666. volatile bool end_data_capture_thread_active;
  667. int64_t video_offset;
  668. int64_t audio_offsets[MAX_AUDIO_MIXES];
  669. int64_t highest_audio_ts;
  670. int64_t highest_video_ts;
  671. pthread_t end_data_capture_thread;
  672. os_event_t *stopping_event;
  673. pthread_mutex_t interleaved_mutex;
  674. DARRAY(struct encoder_packet) interleaved_packets;
  675. int stop_code;
  676. int reconnect_retry_sec;
  677. int reconnect_retry_max;
  678. int reconnect_retries;
  679. int reconnect_retry_cur_sec;
  680. pthread_t reconnect_thread;
  681. os_event_t *reconnect_stop_event;
  682. volatile bool reconnecting;
  683. volatile bool reconnect_thread_active;
  684. uint32_t starting_drawn_count;
  685. uint32_t starting_lagged_count;
  686. uint32_t starting_frame_count;
  687. int total_frames;
  688. volatile bool active;
  689. video_t *video;
  690. audio_t *audio;
  691. obs_encoder_t *video_encoder;
  692. obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
  693. obs_service_t *service;
  694. size_t mixer_idx;
  695. uint32_t scaled_width;
  696. uint32_t scaled_height;
  697. bool video_conversion_set;
  698. bool audio_conversion_set;
  699. struct video_scale_info video_conversion;
  700. struct audio_convert_info audio_conversion;
  701. pthread_mutex_t caption_mutex;
  702. double caption_timestamp;
  703. struct caption_text *caption_head;
  704. struct caption_text *caption_tail;
  705. bool valid;
  706. uint64_t active_delay_ns;
  707. encoded_callback_t delay_callback;
  708. struct circlebuf delay_data; /* struct delay_data */
  709. pthread_mutex_t delay_mutex;
  710. uint32_t delay_sec;
  711. uint32_t delay_flags;
  712. uint32_t delay_cur_flags;
  713. volatile long delay_restart_refs;
  714. volatile bool delay_active;
  715. volatile bool delay_capturing;
  716. char *last_error_message;
  717. };
  718. static inline void do_output_signal(struct obs_output *output,
  719. const char *signal)
  720. {
  721. struct calldata params = {0};
  722. calldata_set_ptr(&params, "output", output);
  723. signal_handler_signal(output->context.signals, signal, &params);
  724. calldata_free(&params);
  725. }
  726. extern void process_delay(void *data, struct encoder_packet *packet);
  727. extern void obs_output_cleanup_delay(obs_output_t *output);
  728. extern bool obs_output_delay_start(obs_output_t *output);
  729. extern void obs_output_delay_stop(obs_output_t *output);
  730. extern bool obs_output_actual_start(obs_output_t *output);
  731. extern void obs_output_actual_stop(obs_output_t *output, bool force,
  732. uint64_t ts);
  733. extern const struct obs_output_info *find_output(const char *id);
  734. extern void obs_output_remove_encoder(struct obs_output *output,
  735. struct obs_encoder *encoder);
  736. extern void obs_encoder_packet_create_instance(struct encoder_packet *dst,
  737. const struct encoder_packet *src);
  738. void obs_output_destroy(obs_output_t *output);
  739. /* ------------------------------------------------------------------------- */
  740. /* encoders */
  741. struct obs_weak_encoder {
  742. struct obs_weak_ref ref;
  743. struct obs_encoder *encoder;
  744. };
  745. struct encoder_callback {
  746. bool sent_first_packet;
  747. void (*new_packet)(void *param, struct encoder_packet *packet);
  748. void *param;
  749. };
  750. struct obs_encoder {
  751. struct obs_context_data context;
  752. struct obs_encoder_info info;
  753. struct obs_weak_encoder *control;
  754. pthread_mutex_t init_mutex;
  755. uint32_t samplerate;
  756. size_t planes;
  757. size_t blocksize;
  758. size_t framesize;
  759. size_t framesize_bytes;
  760. size_t mixer_idx;
  761. uint32_t scaled_width;
  762. uint32_t scaled_height;
  763. enum video_format preferred_format;
  764. volatile bool active;
  765. bool initialized;
  766. /* indicates ownership of the info.id buffer */
  767. bool owns_info_id;
  768. uint32_t timebase_num;
  769. uint32_t timebase_den;
  770. int64_t cur_pts;
  771. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  772. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  773. /* if a video encoder is paired with an audio encoder, make it start
  774. * up at the specific timestamp. if this is the audio encoder,
  775. * wait_for_video makes it wait until it's ready to sync up with
  776. * video */
  777. bool wait_for_video;
  778. bool first_received;
  779. struct obs_encoder *paired_encoder;
  780. int64_t offset_usec;
  781. uint64_t first_raw_ts;
  782. uint64_t start_ts;
  783. pthread_mutex_t outputs_mutex;
  784. DARRAY(obs_output_t*) outputs;
  785. bool destroy_on_stop;
  786. /* stores the video/audio media output pointer. video_t *or audio_t **/
  787. void *media;
  788. pthread_mutex_t callbacks_mutex;
  789. DARRAY(struct encoder_callback) callbacks;
  790. const char *profile_encoder_encode_name;
  791. };
  792. extern struct obs_encoder_info *find_encoder(const char *id);
  793. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  794. extern void obs_encoder_shutdown(obs_encoder_t *encoder);
  795. extern void obs_encoder_start(obs_encoder_t *encoder,
  796. void (*new_packet)(void *param, struct encoder_packet *packet),
  797. void *param);
  798. extern void obs_encoder_stop(obs_encoder_t *encoder,
  799. void (*new_packet)(void *param, struct encoder_packet *packet),
  800. void *param);
  801. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  802. struct obs_output *output);
  803. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  804. struct obs_output *output);
  805. void obs_encoder_destroy(obs_encoder_t *encoder);
  806. /* ------------------------------------------------------------------------- */
  807. /* services */
  808. struct obs_weak_service {
  809. struct obs_weak_ref ref;
  810. struct obs_service *service;
  811. };
  812. struct obs_service {
  813. struct obs_context_data context;
  814. struct obs_service_info info;
  815. struct obs_weak_service *control;
  816. /* indicates ownership of the info.id buffer */
  817. bool owns_info_id;
  818. bool active;
  819. bool destroy;
  820. struct obs_output *output;
  821. };
  822. extern const struct obs_service_info *find_service(const char *id);
  823. extern void obs_service_activate(struct obs_service *service);
  824. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  825. extern bool obs_service_initialize(struct obs_service *service,
  826. struct obs_output *output);
  827. void obs_service_destroy(obs_service_t *service);