obs-internal.h 29 KB

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