obs-internal.h 34 KB

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