1
0

obs-internal.h 35 KB

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