obs-internal.h 34 KB

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