obs-internal.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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_textures[NUM_TEXTURES];
  188. gs_texture_t *output_textures[NUM_TEXTURES];
  189. gs_texture_t *convert_textures[NUM_TEXTURES];
  190. gs_texture_t *convert_uv_textures[NUM_TEXTURES];
  191. bool textures_rendered[NUM_TEXTURES];
  192. bool textures_output[NUM_TEXTURES];
  193. bool textures_copied[NUM_TEXTURES];
  194. bool textures_converted[NUM_TEXTURES];
  195. bool using_nv12_tex;
  196. struct circlebuf vframe_info_buffer;
  197. struct circlebuf vframe_info_buffer_gpu;
  198. gs_effect_t *default_effect;
  199. gs_effect_t *default_rect_effect;
  200. gs_effect_t *opaque_effect;
  201. gs_effect_t *solid_effect;
  202. gs_effect_t *repeat_effect;
  203. gs_effect_t *conversion_effect;
  204. gs_effect_t *bicubic_effect;
  205. gs_effect_t *lanczos_effect;
  206. gs_effect_t *area_effect;
  207. gs_effect_t *bilinear_lowres_effect;
  208. gs_effect_t *premultiplied_alpha_effect;
  209. gs_samplerstate_t *point_sampler;
  210. gs_stagesurf_t *mapped_surface;
  211. int cur_texture;
  212. long raw_active;
  213. long gpu_encoder_active;
  214. pthread_mutex_t gpu_encoder_mutex;
  215. struct circlebuf gpu_encoder_queue;
  216. struct circlebuf gpu_encoder_avail_queue;
  217. DARRAY(obs_encoder_t *) gpu_encoders;
  218. os_sem_t *gpu_encode_semaphore;
  219. os_event_t *gpu_encode_inactive;
  220. pthread_t gpu_encode_thread;
  221. bool gpu_encode_thread_initialized;
  222. volatile bool gpu_encode_stop;
  223. uint64_t video_time;
  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,
  348. uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts,
  349. uint32_t mixers, struct audio_output_data *mixes);
  350. extern void start_raw_video(video_t *video,
  351. 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, struct video_data *frame),
  356. void *param);
  357. /* ------------------------------------------------------------------------- */
  358. /* obs shared context data */
  359. struct obs_context_data {
  360. char *name;
  361. void *data;
  362. obs_data_t *settings;
  363. signal_handler_t *signals;
  364. proc_handler_t *procs;
  365. enum obs_obj_type type;
  366. DARRAY(obs_hotkey_id) hotkeys;
  367. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  368. obs_data_t *hotkey_data;
  369. DARRAY(char*) rename_cache;
  370. pthread_mutex_t rename_cache_mutex;
  371. pthread_mutex_t *mutex;
  372. struct obs_context_data *next;
  373. struct obs_context_data **prev_next;
  374. bool private;
  375. };
  376. extern bool obs_context_data_init(
  377. struct obs_context_data *context,
  378. enum obs_obj_type type,
  379. obs_data_t *settings,
  380. const char *name,
  381. obs_data_t *hotkey_data,
  382. bool private);
  383. extern void obs_context_data_free(struct obs_context_data *context);
  384. extern void obs_context_data_insert(struct obs_context_data *context,
  385. pthread_mutex_t *mutex, void *first);
  386. extern void obs_context_data_remove(struct obs_context_data *context);
  387. extern void obs_context_data_setname(struct obs_context_data *context,
  388. const char *name);
  389. /* ------------------------------------------------------------------------- */
  390. /* ref-counting */
  391. struct obs_weak_ref {
  392. volatile long refs;
  393. volatile long weak_refs;
  394. };
  395. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  396. {
  397. os_atomic_inc_long(&ref->refs);
  398. }
  399. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  400. {
  401. return os_atomic_dec_long(&ref->refs) == -1;
  402. }
  403. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  404. {
  405. os_atomic_inc_long(&ref->weak_refs);
  406. }
  407. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  408. {
  409. return os_atomic_dec_long(&ref->weak_refs) == -1;
  410. }
  411. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  412. {
  413. long owners = ref->refs;
  414. while (owners > -1) {
  415. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  416. return true;
  417. owners = ref->refs;
  418. }
  419. return false;
  420. }
  421. /* ------------------------------------------------------------------------- */
  422. /* sources */
  423. struct async_frame {
  424. struct obs_source_frame *frame;
  425. long unused_count;
  426. bool used;
  427. };
  428. enum audio_action_type {
  429. AUDIO_ACTION_VOL,
  430. AUDIO_ACTION_MUTE,
  431. AUDIO_ACTION_PTT,
  432. AUDIO_ACTION_PTM,
  433. };
  434. struct audio_action {
  435. uint64_t timestamp;
  436. enum audio_action_type type;
  437. union {
  438. float vol;
  439. bool set;
  440. };
  441. };
  442. struct obs_weak_source {
  443. struct obs_weak_ref ref;
  444. struct obs_source *source;
  445. };
  446. struct audio_cb_info {
  447. obs_source_audio_capture_t callback;
  448. void *param;
  449. };
  450. struct obs_source {
  451. struct obs_context_data context;
  452. struct obs_source_info info;
  453. struct obs_weak_source *control;
  454. /* general exposed flags that can be set for the source */
  455. uint32_t flags;
  456. uint32_t default_flags;
  457. /* indicates ownership of the info.id buffer */
  458. bool owns_info_id;
  459. /* signals to call the source update in the video thread */
  460. bool defer_update;
  461. /* ensures show/hide are only called once */
  462. volatile long show_refs;
  463. /* ensures activate/deactivate are only called once */
  464. volatile long activate_refs;
  465. /* used to indicate that the source has been removed and all
  466. * references to it should be released (not exactly how I would prefer
  467. * to handle things but it's the best option) */
  468. bool removed;
  469. bool active;
  470. bool showing;
  471. /* used to temporarily disable sources if needed */
  472. bool enabled;
  473. /* timing (if video is present, is based upon video) */
  474. volatile bool timing_set;
  475. volatile uint64_t timing_adjust;
  476. uint64_t resample_offset;
  477. uint64_t last_audio_ts;
  478. uint64_t next_audio_ts_min;
  479. uint64_t next_audio_sys_ts_min;
  480. uint64_t last_frame_ts;
  481. uint64_t last_sys_timestamp;
  482. bool async_rendered;
  483. /* audio */
  484. bool audio_failed;
  485. bool audio_pending;
  486. bool pending_stop;
  487. bool user_muted;
  488. bool muted;
  489. struct obs_source *next_audio_source;
  490. struct obs_source **prev_next_audio_source;
  491. uint64_t audio_ts;
  492. struct circlebuf audio_input_buf[MAX_AUDIO_CHANNELS];
  493. size_t last_audio_input_buf_size;
  494. DARRAY(struct audio_action) audio_actions;
  495. float *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
  496. struct resample_info sample_info;
  497. audio_resampler_t *resampler;
  498. pthread_mutex_t audio_actions_mutex;
  499. pthread_mutex_t audio_buf_mutex;
  500. pthread_mutex_t audio_mutex;
  501. pthread_mutex_t audio_cb_mutex;
  502. DARRAY(struct audio_cb_info) audio_cb_list;
  503. struct obs_audio_data audio_data;
  504. size_t audio_storage_size;
  505. uint32_t audio_mixers;
  506. float user_volume;
  507. float volume;
  508. int64_t sync_offset;
  509. int64_t last_sync_offset;
  510. float balance;
  511. /* async video data */
  512. gs_texture_t *async_texture;
  513. gs_texrender_t *async_texrender;
  514. struct obs_source_frame *cur_async_frame;
  515. bool async_gpu_conversion;
  516. enum video_format async_format;
  517. bool async_full_range;
  518. enum video_format async_cache_format;
  519. bool async_cache_full_range;
  520. enum gs_color_format async_texture_format;
  521. int async_plane_offset[2];
  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;
  536. uint32_t async_convert_height;
  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_texture;
  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. bool transitioning_video;
  578. bool transitioning_audio;
  579. bool transition_source_active[2];
  580. uint32_t transition_alignment;
  581. uint32_t transition_actual_cx;
  582. uint32_t transition_actual_cy;
  583. uint32_t transition_cx;
  584. uint32_t transition_cy;
  585. uint32_t transition_fixed_duration;
  586. bool transition_use_fixed_duration;
  587. enum obs_transition_mode transition_mode;
  588. enum obs_transition_scale_type transition_scale_type;
  589. struct matrix4 transition_matrices[2];
  590. struct audio_monitor *monitor;
  591. enum obs_monitoring_type monitoring_type;
  592. obs_data_t *private_settings;
  593. };
  594. extern struct obs_source_info *get_source_info(const char *id);
  595. extern bool obs_source_init_context(struct obs_source *source,
  596. obs_data_t *settings, const char *name,
  597. obs_data_t *hotkey_data, bool private);
  598. extern bool obs_transition_init(obs_source_t *transition);
  599. extern void obs_transition_free(obs_source_t *transition);
  600. extern void obs_transition_tick(obs_source_t *transition);
  601. extern void obs_transition_enum_sources(obs_source_t *transition,
  602. obs_source_enum_proc_t enum_callback, void *param);
  603. extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
  604. extern void obs_transition_load(obs_source_t *source, obs_data_t *data);
  605. struct audio_monitor *audio_monitor_create(obs_source_t *source);
  606. void audio_monitor_reset(struct audio_monitor *monitor);
  607. extern void audio_monitor_destroy(struct audio_monitor *monitor);
  608. extern void obs_source_destroy(struct obs_source *source);
  609. enum view_type {
  610. MAIN_VIEW,
  611. AUX_VIEW
  612. };
  613. static inline void obs_source_dosignal(struct obs_source *source,
  614. const char *signal_obs, 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 convert_video_format(
  636. 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, size_t size);
  651. extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);
  652. extern struct obs_source_frame *filter_async_video(obs_source_t *source,
  653. struct obs_source_frame *in);
  654. extern bool update_async_texture(struct obs_source *source,
  655. const struct obs_source_frame *frame,
  656. gs_texture_t *tex, gs_texrender_t *texrender);
  657. extern bool set_async_texture_size(struct obs_source *source,
  658. const struct obs_source_frame *frame);
  659. extern void remove_async_frame(obs_source_t *source,
  660. struct obs_source_frame *frame);
  661. extern void set_deinterlace_texture_size(obs_source_t *source);
  662. extern void deinterlace_process_last_frame(obs_source_t *source,
  663. uint64_t sys_time);
  664. extern void deinterlace_update_async_video(obs_source_t *source);
  665. extern void deinterlace_render(obs_source_t *s);
  666. /* ------------------------------------------------------------------------- */
  667. /* outputs */
  668. enum delay_msg {
  669. DELAY_MSG_PACKET,
  670. DELAY_MSG_START,
  671. DELAY_MSG_STOP,
  672. };
  673. struct delay_data {
  674. enum delay_msg msg;
  675. uint64_t ts;
  676. struct encoder_packet packet;
  677. };
  678. typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);
  679. struct obs_weak_output {
  680. struct obs_weak_ref ref;
  681. struct obs_output *output;
  682. };
  683. #define CAPTION_LINE_CHARS (32)
  684. #define CAPTION_LINE_BYTES (4*CAPTION_LINE_CHARS)
  685. struct caption_text {
  686. char text[CAPTION_LINE_BYTES+1];
  687. double display_duration;
  688. struct caption_text *next;
  689. };
  690. struct obs_output {
  691. struct obs_context_data context;
  692. struct obs_output_info info;
  693. struct obs_weak_output *control;
  694. /* indicates ownership of the info.id buffer */
  695. bool owns_info_id;
  696. bool received_video;
  697. bool received_audio;
  698. volatile bool data_active;
  699. volatile bool end_data_capture_thread_active;
  700. int64_t video_offset;
  701. int64_t audio_offsets[MAX_AUDIO_MIXES];
  702. int64_t highest_audio_ts;
  703. int64_t highest_video_ts;
  704. pthread_t end_data_capture_thread;
  705. os_event_t *stopping_event;
  706. pthread_mutex_t interleaved_mutex;
  707. DARRAY(struct encoder_packet) interleaved_packets;
  708. int stop_code;
  709. int reconnect_retry_sec;
  710. int reconnect_retry_max;
  711. int reconnect_retries;
  712. int reconnect_retry_cur_sec;
  713. pthread_t reconnect_thread;
  714. os_event_t *reconnect_stop_event;
  715. volatile bool reconnecting;
  716. volatile bool reconnect_thread_active;
  717. uint32_t starting_drawn_count;
  718. uint32_t starting_lagged_count;
  719. uint32_t starting_frame_count;
  720. int total_frames;
  721. volatile bool active;
  722. video_t *video;
  723. audio_t *audio;
  724. obs_encoder_t *video_encoder;
  725. obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
  726. obs_service_t *service;
  727. size_t mixer_mask;
  728. uint32_t scaled_width;
  729. uint32_t scaled_height;
  730. bool video_conversion_set;
  731. bool audio_conversion_set;
  732. struct video_scale_info video_conversion;
  733. struct audio_convert_info audio_conversion;
  734. pthread_mutex_t caption_mutex;
  735. double caption_timestamp;
  736. struct caption_text *caption_head;
  737. struct caption_text *caption_tail;
  738. bool valid;
  739. uint64_t active_delay_ns;
  740. encoded_callback_t delay_callback;
  741. struct circlebuf delay_data; /* struct delay_data */
  742. pthread_mutex_t delay_mutex;
  743. uint32_t delay_sec;
  744. uint32_t delay_flags;
  745. uint32_t delay_cur_flags;
  746. volatile long delay_restart_refs;
  747. volatile bool delay_active;
  748. volatile bool delay_capturing;
  749. char *last_error_message;
  750. };
  751. static inline void do_output_signal(struct obs_output *output,
  752. const char *signal)
  753. {
  754. struct calldata params = {0};
  755. calldata_set_ptr(&params, "output", output);
  756. signal_handler_signal(output->context.signals, signal, &params);
  757. calldata_free(&params);
  758. }
  759. extern void process_delay(void *data, struct encoder_packet *packet);
  760. extern void obs_output_cleanup_delay(obs_output_t *output);
  761. extern bool obs_output_delay_start(obs_output_t *output);
  762. extern void obs_output_delay_stop(obs_output_t *output);
  763. extern bool obs_output_actual_start(obs_output_t *output);
  764. extern void obs_output_actual_stop(obs_output_t *output, bool force,
  765. uint64_t ts);
  766. extern const struct obs_output_info *find_output(const char *id);
  767. extern void obs_output_remove_encoder(struct obs_output *output,
  768. struct obs_encoder *encoder);
  769. extern void obs_encoder_packet_create_instance(struct encoder_packet *dst,
  770. const struct encoder_packet *src);
  771. void obs_output_destroy(obs_output_t *output);
  772. /* ------------------------------------------------------------------------- */
  773. /* encoders */
  774. struct obs_weak_encoder {
  775. struct obs_weak_ref ref;
  776. struct obs_encoder *encoder;
  777. };
  778. struct encoder_callback {
  779. bool sent_first_packet;
  780. void (*new_packet)(void *param, struct encoder_packet *packet);
  781. void *param;
  782. };
  783. struct obs_encoder {
  784. struct obs_context_data context;
  785. struct obs_encoder_info info;
  786. struct obs_weak_encoder *control;
  787. /* allows re-routing to another encoder */
  788. struct obs_encoder_info orig_info;
  789. pthread_mutex_t init_mutex;
  790. uint32_t samplerate;
  791. size_t planes;
  792. size_t blocksize;
  793. size_t framesize;
  794. size_t framesize_bytes;
  795. size_t mixer_idx;
  796. uint32_t scaled_width;
  797. uint32_t scaled_height;
  798. enum video_format preferred_format;
  799. volatile bool active;
  800. bool initialized;
  801. /* indicates ownership of the info.id buffer */
  802. bool owns_info_id;
  803. uint32_t timebase_num;
  804. uint32_t timebase_den;
  805. int64_t cur_pts;
  806. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  807. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  808. /* if a video encoder is paired with an audio encoder, make it start
  809. * up at the specific timestamp. if this is the audio encoder,
  810. * wait_for_video makes it wait until it's ready to sync up with
  811. * video */
  812. bool wait_for_video;
  813. bool first_received;
  814. struct obs_encoder *paired_encoder;
  815. int64_t offset_usec;
  816. uint64_t first_raw_ts;
  817. uint64_t start_ts;
  818. pthread_mutex_t outputs_mutex;
  819. DARRAY(obs_output_t*) outputs;
  820. bool destroy_on_stop;
  821. /* stores the video/audio media output pointer. video_t *or audio_t **/
  822. void *media;
  823. pthread_mutex_t callbacks_mutex;
  824. DARRAY(struct encoder_callback) callbacks;
  825. const char *profile_encoder_encode_name;
  826. };
  827. extern struct obs_encoder_info *find_encoder(const char *id);
  828. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  829. extern void obs_encoder_shutdown(obs_encoder_t *encoder);
  830. extern void obs_encoder_start(obs_encoder_t *encoder,
  831. void (*new_packet)(void *param, struct encoder_packet *packet),
  832. void *param);
  833. extern void obs_encoder_stop(obs_encoder_t *encoder,
  834. void (*new_packet)(void *param, struct encoder_packet *packet),
  835. void *param);
  836. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  837. struct obs_output *output);
  838. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  839. struct obs_output *output);
  840. extern bool start_gpu_encode(obs_encoder_t *encoder);
  841. extern void stop_gpu_encode(obs_encoder_t *encoder);
  842. extern void do_encode(struct obs_encoder *encoder, struct encoder_frame *frame);
  843. extern void send_off_encoder_packet(obs_encoder_t *encoder, bool success,
  844. bool received, struct encoder_packet *pkt);
  845. void obs_encoder_destroy(obs_encoder_t *encoder);
  846. /* ------------------------------------------------------------------------- */
  847. /* services */
  848. struct obs_weak_service {
  849. struct obs_weak_ref ref;
  850. struct obs_service *service;
  851. };
  852. struct obs_service {
  853. struct obs_context_data context;
  854. struct obs_service_info info;
  855. struct obs_weak_service *control;
  856. /* indicates ownership of the info.id buffer */
  857. bool owns_info_id;
  858. bool active;
  859. bool destroy;
  860. struct obs_output *output;
  861. };
  862. extern const struct obs_service_info *find_service(const char *id);
  863. extern void obs_service_activate(struct obs_service *service);
  864. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  865. extern bool obs_service_initialize(struct obs_service *service,
  866. struct obs_output *output);
  867. void obs_service_destroy(obs_service_t *service);