obs-internal.h 33 KB

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