obs-internal.h 35 KB

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