obs-internal.h 36 KB

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