obs-internal.h 36 KB

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