obs-internal.h 35 KB

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