obs-internal.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. };
  220. struct audio_monitor;
  221. struct obs_core_audio {
  222. audio_t *audio;
  223. DARRAY(struct obs_source*) render_order;
  224. DARRAY(struct obs_source*) root_nodes;
  225. uint64_t buffered_ts;
  226. struct circlebuf buffered_timestamps;
  227. int buffering_wait_ticks;
  228. int total_buffering_ticks;
  229. float user_volume;
  230. pthread_mutex_t monitoring_mutex;
  231. DARRAY(struct audio_monitor*) monitors;
  232. char *monitoring_device_name;
  233. char *monitoring_device_id;
  234. };
  235. /* user sources, output channels, and displays */
  236. struct obs_core_data {
  237. struct obs_source *first_source;
  238. struct obs_source *first_audio_source;
  239. struct obs_display *first_display;
  240. struct obs_output *first_output;
  241. struct obs_encoder *first_encoder;
  242. struct obs_service *first_service;
  243. pthread_mutex_t sources_mutex;
  244. pthread_mutex_t displays_mutex;
  245. pthread_mutex_t outputs_mutex;
  246. pthread_mutex_t encoders_mutex;
  247. pthread_mutex_t services_mutex;
  248. pthread_mutex_t audio_sources_mutex;
  249. struct obs_view main_view;
  250. long long unnamed_index;
  251. volatile bool valid;
  252. };
  253. /* user hotkeys */
  254. struct obs_core_hotkeys {
  255. pthread_mutex_t mutex;
  256. DARRAY(obs_hotkey_t) hotkeys;
  257. obs_hotkey_id next_id;
  258. DARRAY(obs_hotkey_pair_t) hotkey_pairs;
  259. obs_hotkey_pair_id next_pair_id;
  260. pthread_t hotkey_thread;
  261. bool hotkey_thread_initialized;
  262. os_event_t *stop_event;
  263. bool thread_disable_press : 1;
  264. bool strict_modifiers : 1;
  265. bool reroute_hotkeys : 1;
  266. DARRAY(obs_hotkey_binding_t) bindings;
  267. obs_hotkey_callback_router_func router_func;
  268. void *router_func_data;
  269. obs_hotkeys_platform_t *platform_context;
  270. pthread_once_t name_map_init_token;
  271. struct obs_hotkey_name_map *name_map;
  272. signal_handler_t *signals;
  273. char *translations[OBS_KEY_LAST_VALUE];
  274. char *mute;
  275. char *unmute;
  276. char *push_to_mute;
  277. char *push_to_talk;
  278. char *sceneitem_show;
  279. char *sceneitem_hide;
  280. };
  281. struct obs_core {
  282. struct obs_module *first_module;
  283. DARRAY(struct obs_module_path) module_paths;
  284. DARRAY(struct obs_source_info) source_types;
  285. DARRAY(struct obs_source_info) input_types;
  286. DARRAY(struct obs_source_info) filter_types;
  287. DARRAY(struct obs_source_info) transition_types;
  288. DARRAY(struct obs_output_info) output_types;
  289. DARRAY(struct obs_encoder_info) encoder_types;
  290. DARRAY(struct obs_service_info) service_types;
  291. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  292. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  293. signal_handler_t *signals;
  294. proc_handler_t *procs;
  295. char *locale;
  296. char *module_config_path;
  297. bool name_store_owned;
  298. profiler_name_store_t *name_store;
  299. /* segmented into multiple sub-structures to keep things a bit more
  300. * clean and organized */
  301. struct obs_core_video video;
  302. struct obs_core_audio audio;
  303. struct obs_core_data data;
  304. struct obs_core_hotkeys hotkeys;
  305. };
  306. extern struct obs_core *obs;
  307. extern void *obs_video_thread(void *param);
  308. extern gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file);
  309. extern bool audio_callback(void *param,
  310. uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts,
  311. uint32_t mixers, struct audio_output_data *mixes);
  312. /* ------------------------------------------------------------------------- */
  313. /* obs shared context data */
  314. struct obs_context_data {
  315. char *name;
  316. void *data;
  317. obs_data_t *settings;
  318. signal_handler_t *signals;
  319. proc_handler_t *procs;
  320. enum obs_obj_type type;
  321. DARRAY(obs_hotkey_id) hotkeys;
  322. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  323. obs_data_t *hotkey_data;
  324. DARRAY(char*) rename_cache;
  325. pthread_mutex_t rename_cache_mutex;
  326. pthread_mutex_t *mutex;
  327. struct obs_context_data *next;
  328. struct obs_context_data **prev_next;
  329. bool private;
  330. };
  331. extern bool obs_context_data_init(
  332. struct obs_context_data *context,
  333. enum obs_obj_type type,
  334. obs_data_t *settings,
  335. const char *name,
  336. obs_data_t *hotkey_data,
  337. bool private);
  338. extern void obs_context_data_free(struct obs_context_data *context);
  339. extern void obs_context_data_insert(struct obs_context_data *context,
  340. pthread_mutex_t *mutex, void *first);
  341. extern void obs_context_data_remove(struct obs_context_data *context);
  342. extern void obs_context_data_setname(struct obs_context_data *context,
  343. const char *name);
  344. /* ------------------------------------------------------------------------- */
  345. /* ref-counting */
  346. struct obs_weak_ref {
  347. volatile long refs;
  348. volatile long weak_refs;
  349. };
  350. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  351. {
  352. os_atomic_inc_long(&ref->refs);
  353. }
  354. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  355. {
  356. return os_atomic_dec_long(&ref->refs) == -1;
  357. }
  358. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  359. {
  360. os_atomic_inc_long(&ref->weak_refs);
  361. }
  362. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  363. {
  364. return os_atomic_dec_long(&ref->weak_refs) == -1;
  365. }
  366. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  367. {
  368. long owners = ref->refs;
  369. while (owners > -1) {
  370. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  371. return true;
  372. owners = ref->refs;
  373. }
  374. return false;
  375. }
  376. /* ------------------------------------------------------------------------- */
  377. /* sources */
  378. struct async_frame {
  379. struct obs_source_frame *frame;
  380. long unused_count;
  381. bool used;
  382. };
  383. enum audio_action_type {
  384. AUDIO_ACTION_VOL,
  385. AUDIO_ACTION_MUTE,
  386. AUDIO_ACTION_PTT,
  387. AUDIO_ACTION_PTM,
  388. };
  389. struct audio_action {
  390. uint64_t timestamp;
  391. enum audio_action_type type;
  392. union {
  393. float vol;
  394. bool set;
  395. };
  396. };
  397. struct obs_weak_source {
  398. struct obs_weak_ref ref;
  399. struct obs_source *source;
  400. };
  401. struct audio_cb_info {
  402. obs_source_audio_capture_t callback;
  403. void *param;
  404. };
  405. struct obs_source {
  406. struct obs_context_data context;
  407. struct obs_source_info info;
  408. struct obs_weak_source *control;
  409. /* general exposed flags that can be set for the source */
  410. uint32_t flags;
  411. uint32_t default_flags;
  412. /* indicates ownership of the info.id buffer */
  413. bool owns_info_id;
  414. /* signals to call the source update in the video thread */
  415. bool defer_update;
  416. /* ensures show/hide are only called once */
  417. volatile long show_refs;
  418. /* ensures activate/deactivate are only called once */
  419. volatile long activate_refs;
  420. /* used to indicate that the source has been removed and all
  421. * references to it should be released (not exactly how I would prefer
  422. * to handle things but it's the best option) */
  423. bool removed;
  424. bool active;
  425. bool showing;
  426. /* used to temporarily disable sources if needed */
  427. bool enabled;
  428. /* timing (if video is present, is based upon video) */
  429. volatile bool timing_set;
  430. volatile uint64_t timing_adjust;
  431. uint64_t resample_offset;
  432. uint64_t last_audio_ts;
  433. uint64_t next_audio_ts_min;
  434. uint64_t next_audio_sys_ts_min;
  435. uint64_t last_frame_ts;
  436. uint64_t last_sys_timestamp;
  437. bool async_rendered;
  438. /* audio */
  439. bool audio_failed;
  440. bool audio_pending;
  441. bool pending_stop;
  442. bool user_muted;
  443. bool muted;
  444. struct obs_source *next_audio_source;
  445. struct obs_source **prev_next_audio_source;
  446. uint64_t audio_ts;
  447. struct circlebuf audio_input_buf[MAX_AUDIO_CHANNELS];
  448. size_t last_audio_input_buf_size;
  449. DARRAY(struct audio_action) audio_actions;
  450. float *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
  451. struct resample_info sample_info;
  452. audio_resampler_t *resampler;
  453. pthread_mutex_t audio_actions_mutex;
  454. pthread_mutex_t audio_buf_mutex;
  455. pthread_mutex_t audio_mutex;
  456. pthread_mutex_t audio_cb_mutex;
  457. DARRAY(struct audio_cb_info) audio_cb_list;
  458. struct obs_audio_data audio_data;
  459. size_t audio_storage_size;
  460. uint32_t audio_mixers;
  461. float user_volume;
  462. float volume;
  463. int64_t sync_offset;
  464. int64_t last_sync_offset;
  465. /* async video data */
  466. gs_texture_t *async_texture;
  467. gs_texrender_t *async_texrender;
  468. struct obs_source_frame *cur_async_frame;
  469. bool async_gpu_conversion;
  470. enum video_format async_format;
  471. enum video_format async_cache_format;
  472. enum gs_color_format async_texture_format;
  473. float async_color_matrix[16];
  474. bool async_full_range;
  475. float async_color_range_min[3];
  476. float async_color_range_max[3];
  477. int async_plane_offset[2];
  478. bool async_flip;
  479. bool async_active;
  480. bool async_update_texture;
  481. struct obs_source_frame *async_preload_frame;
  482. DARRAY(struct async_frame) async_cache;
  483. DARRAY(struct obs_source_frame*)async_frames;
  484. pthread_mutex_t async_mutex;
  485. uint32_t async_width;
  486. uint32_t async_height;
  487. uint32_t async_cache_width;
  488. uint32_t async_cache_height;
  489. uint32_t async_convert_width;
  490. uint32_t async_convert_height;
  491. /* async video deinterlacing */
  492. uint64_t deinterlace_offset;
  493. uint64_t deinterlace_frame_ts;
  494. gs_effect_t *deinterlace_effect;
  495. struct obs_source_frame *prev_async_frame;
  496. gs_texture_t *async_prev_texture;
  497. gs_texrender_t *async_prev_texrender;
  498. uint32_t deinterlace_half_duration;
  499. enum obs_deinterlace_mode deinterlace_mode;
  500. bool deinterlace_top_first;
  501. bool deinterlace_rendered;
  502. /* filters */
  503. struct obs_source *filter_parent;
  504. struct obs_source *filter_target;
  505. DARRAY(struct obs_source*) filters;
  506. pthread_mutex_t filter_mutex;
  507. gs_texrender_t *filter_texrender;
  508. enum obs_allow_direct_render allow_direct;
  509. bool rendering_filter;
  510. /* sources specific hotkeys */
  511. obs_hotkey_pair_id mute_unmute_key;
  512. obs_hotkey_id push_to_mute_key;
  513. obs_hotkey_id push_to_talk_key;
  514. bool push_to_mute_enabled : 1;
  515. bool push_to_mute_pressed : 1;
  516. bool user_push_to_mute_pressed : 1;
  517. bool push_to_talk_enabled : 1;
  518. bool push_to_talk_pressed : 1;
  519. bool user_push_to_talk_pressed : 1;
  520. uint64_t push_to_mute_delay;
  521. uint64_t push_to_mute_stop_time;
  522. uint64_t push_to_talk_delay;
  523. uint64_t push_to_talk_stop_time;
  524. /* transitions */
  525. uint64_t transition_start_time;
  526. uint64_t transition_duration;
  527. pthread_mutex_t transition_tex_mutex;
  528. gs_texrender_t *transition_texrender[2];
  529. pthread_mutex_t transition_mutex;
  530. obs_source_t *transition_sources[2];
  531. bool transitioning_video;
  532. bool transitioning_audio;
  533. bool transition_source_active[2];
  534. uint32_t transition_alignment;
  535. uint32_t transition_actual_cx;
  536. uint32_t transition_actual_cy;
  537. uint32_t transition_cx;
  538. uint32_t transition_cy;
  539. uint32_t transition_fixed_duration;
  540. bool transition_use_fixed_duration : 1;
  541. enum obs_transition_mode transition_mode;
  542. enum obs_transition_scale_type transition_scale_type;
  543. struct matrix4 transition_matrices[2];
  544. struct audio_monitor *monitor;
  545. enum obs_monitoring_type monitoring_type;
  546. };
  547. extern const struct obs_source_info *get_source_info(const char *id);
  548. extern bool obs_source_init_context(struct obs_source *source,
  549. obs_data_t *settings, const char *name,
  550. obs_data_t *hotkey_data, bool private);
  551. extern void obs_source_save(obs_source_t *source);
  552. extern void obs_source_load(obs_source_t *source);
  553. extern bool obs_transition_init(obs_source_t *transition);
  554. extern void obs_transition_free(obs_source_t *transition);
  555. extern void obs_transition_tick(obs_source_t *transition);
  556. extern void obs_transition_enum_sources(obs_source_t *transition,
  557. obs_source_enum_proc_t enum_callback, void *param);
  558. extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
  559. extern void obs_transition_load(obs_source_t *source, obs_data_t *data);
  560. struct audio_monitor *audio_monitor_create(obs_source_t *source);
  561. void audio_monitor_reset(struct audio_monitor *monitor);
  562. extern void audio_monitor_destroy(struct audio_monitor *monitor);
  563. extern void obs_source_destroy(struct obs_source *source);
  564. enum view_type {
  565. MAIN_VIEW,
  566. AUX_VIEW
  567. };
  568. static inline void obs_source_dosignal(struct obs_source *source,
  569. const char *signal_obs, const char *signal_source)
  570. {
  571. struct calldata data;
  572. uint8_t stack[128];
  573. calldata_init_fixed(&data, stack, sizeof(stack));
  574. calldata_set_ptr(&data, "source", source);
  575. if (signal_obs && !source->context.private)
  576. signal_handler_signal(obs->signals, signal_obs, &data);
  577. if (signal_source)
  578. signal_handler_signal(source->context.signals, signal_source,
  579. &data);
  580. }
  581. /* maximum timestamp variance in nanoseconds */
  582. #define MAX_TS_VAR 2000000000ULL
  583. static inline bool frame_out_of_bounds(const obs_source_t *source, uint64_t ts)
  584. {
  585. if (ts < source->last_frame_ts)
  586. return ((source->last_frame_ts - ts) > MAX_TS_VAR);
  587. else
  588. return ((ts - source->last_frame_ts) > MAX_TS_VAR);
  589. }
  590. static inline enum gs_color_format convert_video_format(
  591. enum video_format format)
  592. {
  593. if (format == VIDEO_FORMAT_RGBA)
  594. return GS_RGBA;
  595. else if (format == VIDEO_FORMAT_BGRA)
  596. return GS_BGRA;
  597. return GS_BGRX;
  598. }
  599. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  600. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  601. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  602. extern float obs_source_get_target_volume(obs_source_t *source,
  603. obs_source_t *target);
  604. extern void obs_source_audio_render(obs_source_t *source, uint32_t mixers,
  605. size_t channels, size_t sample_rate, size_t size);
  606. extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);
  607. extern struct obs_source_frame *filter_async_video(obs_source_t *source,
  608. struct obs_source_frame *in);
  609. extern bool update_async_texture(struct obs_source *source,
  610. const struct obs_source_frame *frame,
  611. gs_texture_t *tex, gs_texrender_t *texrender);
  612. extern bool set_async_texture_size(struct obs_source *source,
  613. const struct obs_source_frame *frame);
  614. extern void remove_async_frame(obs_source_t *source,
  615. struct obs_source_frame *frame);
  616. extern void set_deinterlace_texture_size(obs_source_t *source);
  617. extern void deinterlace_process_last_frame(obs_source_t *source,
  618. uint64_t sys_time);
  619. extern void deinterlace_update_async_video(obs_source_t *source);
  620. extern void deinterlace_render(obs_source_t *s);
  621. /* ------------------------------------------------------------------------- */
  622. /* outputs */
  623. enum delay_msg {
  624. DELAY_MSG_PACKET,
  625. DELAY_MSG_START,
  626. DELAY_MSG_STOP,
  627. };
  628. struct delay_data {
  629. enum delay_msg msg;
  630. uint64_t ts;
  631. struct encoder_packet packet;
  632. };
  633. typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);
  634. struct obs_weak_output {
  635. struct obs_weak_ref ref;
  636. struct obs_output *output;
  637. };
  638. #define CAPTION_LINE_CHARS (32)
  639. #define CAPTION_LINE_BYTES (4*CAPTION_LINE_CHARS)
  640. struct caption_text {
  641. char text[CAPTION_LINE_BYTES+1];
  642. struct caption_text *next;
  643. };
  644. struct obs_output {
  645. struct obs_context_data context;
  646. struct obs_output_info info;
  647. struct obs_weak_output *control;
  648. /* indicates ownership of the info.id buffer */
  649. bool owns_info_id;
  650. bool received_video;
  651. bool received_audio;
  652. volatile bool data_active;
  653. volatile bool end_data_capture_thread_active;
  654. int64_t video_offset;
  655. int64_t audio_offsets[MAX_AUDIO_MIXES];
  656. int64_t highest_audio_ts;
  657. int64_t highest_video_ts;
  658. pthread_t end_data_capture_thread;
  659. os_event_t *stopping_event;
  660. pthread_mutex_t interleaved_mutex;
  661. DARRAY(struct encoder_packet) interleaved_packets;
  662. int stop_code;
  663. int reconnect_retry_sec;
  664. int reconnect_retry_max;
  665. int reconnect_retries;
  666. int reconnect_retry_cur_sec;
  667. pthread_t reconnect_thread;
  668. os_event_t *reconnect_stop_event;
  669. volatile bool reconnecting;
  670. volatile bool reconnect_thread_active;
  671. uint32_t starting_drawn_count;
  672. uint32_t starting_lagged_count;
  673. uint32_t starting_frame_count;
  674. uint32_t starting_skipped_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);