obs-internal.h 30 KB

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