obs-internal.h 34 KB

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