obs-internal.h 30 KB

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