obs-internal.h 35 KB

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