obs-internal.h 35 KB

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