obs-internal.h 33 KB

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