obs-internal.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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/deque.h"
  18. #include "util/dstr.h"
  19. #include "util/threading.h"
  20. #include "util/platform.h"
  21. #include "util/profiler.h"
  22. #include "util/task.h"
  23. #include "util/uthash.h"
  24. #include "util/array-serializer.h"
  25. #include "callback/signal.h"
  26. #include "callback/proc.h"
  27. #include "graphics/graphics.h"
  28. #include "graphics/matrix4.h"
  29. #include "media-io/audio-resampler.h"
  30. #include "media-io/video-io.h"
  31. #include "media-io/audio-io.h"
  32. #include "obs.h"
  33. #include <obsversion.h>
  34. #include <caption/caption.h>
  35. /* Custom helpers for the UUID hash table */
  36. #define HASH_FIND_UUID(head, uuid, out) \
  37. HASH_FIND(hh_uuid, head, uuid, UUID_STR_LENGTH, out)
  38. #define HASH_ADD_UUID(head, uuid_field, add) \
  39. HASH_ADD(hh_uuid, head, uuid_field[0], UUID_STR_LENGTH, add)
  40. #define NUM_TEXTURES 2
  41. #define NUM_CHANNELS 3
  42. #define MICROSECOND_DEN 1000000
  43. #define NUM_ENCODE_TEXTURES 10
  44. #define NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT 1
  45. static inline int64_t packet_dts_usec(struct encoder_packet *packet)
  46. {
  47. return packet->dts * MICROSECOND_DEN / packet->timebase_den;
  48. }
  49. struct tick_callback {
  50. void (*tick)(void *param, float seconds);
  51. void *param;
  52. };
  53. struct draw_callback {
  54. void (*draw)(void *param, uint32_t cx, uint32_t cy);
  55. void *param;
  56. };
  57. struct rendered_callback {
  58. void (*rendered)(void *param);
  59. void *param;
  60. };
  61. /* ------------------------------------------------------------------------- */
  62. /* validity checks */
  63. static inline bool obs_object_valid(const void *obj, const char *f,
  64. const char *t)
  65. {
  66. if (!obj) {
  67. blog(LOG_DEBUG, "%s: Null '%s' parameter", f, t);
  68. return false;
  69. }
  70. return true;
  71. }
  72. #define obs_ptr_valid(ptr, func) obs_object_valid(ptr, func, #ptr)
  73. #define obs_source_valid obs_ptr_valid
  74. #define obs_output_valid obs_ptr_valid
  75. #define obs_encoder_valid obs_ptr_valid
  76. #define obs_service_valid obs_ptr_valid
  77. /* ------------------------------------------------------------------------- */
  78. /* modules */
  79. struct obs_module {
  80. char *mod_name;
  81. const char *file;
  82. char *bin_path;
  83. char *data_path;
  84. void *module;
  85. bool loaded;
  86. bool (*load)(void);
  87. void (*unload)(void);
  88. void (*post_load)(void);
  89. void (*set_locale)(const char *locale);
  90. bool (*get_string)(const char *lookup_string,
  91. const char **translated_string);
  92. void (*free_locale)(void);
  93. uint32_t (*ver)(void);
  94. void (*set_pointer)(obs_module_t *module);
  95. const char *(*name)(void);
  96. const char *(*description)(void);
  97. const char *(*author)(void);
  98. struct obs_module *next;
  99. };
  100. extern void free_module(struct obs_module *mod);
  101. struct obs_module_path {
  102. char *bin;
  103. char *data;
  104. };
  105. static inline void free_module_path(struct obs_module_path *omp)
  106. {
  107. if (omp) {
  108. bfree(omp->bin);
  109. bfree(omp->data);
  110. }
  111. }
  112. static inline bool check_path(const char *data, const char *path,
  113. struct dstr *output)
  114. {
  115. dstr_copy(output, path);
  116. dstr_cat(output, data);
  117. return os_file_exists(output->array);
  118. }
  119. /* ------------------------------------------------------------------------- */
  120. /* hotkeys */
  121. struct obs_hotkey {
  122. obs_hotkey_id id;
  123. char *name;
  124. char *description;
  125. obs_hotkey_func func;
  126. void *data;
  127. int pressed;
  128. obs_hotkey_registerer_t registerer_type;
  129. void *registerer;
  130. obs_hotkey_id pair_partner_id;
  131. UT_hash_handle hh;
  132. };
  133. struct obs_hotkey_pair {
  134. obs_hotkey_pair_id pair_id;
  135. obs_hotkey_id id[2];
  136. obs_hotkey_active_func func[2];
  137. bool pressed0;
  138. bool pressed1;
  139. void *data[2];
  140. UT_hash_handle hh;
  141. };
  142. typedef struct obs_hotkey_pair obs_hotkey_pair_t;
  143. typedef struct obs_hotkeys_platform obs_hotkeys_platform_t;
  144. void *obs_hotkey_thread(void *param);
  145. struct obs_core_hotkeys;
  146. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys);
  147. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys);
  148. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  149. obs_key_t key);
  150. const char *obs_get_hotkey_translation(obs_key_t key, const char *def);
  151. struct obs_context_data;
  152. void obs_hotkeys_context_release(struct obs_context_data *context);
  153. void obs_hotkeys_free(void);
  154. struct obs_hotkey_binding {
  155. obs_key_combination_t key;
  156. bool pressed;
  157. bool modifiers_match;
  158. obs_hotkey_id hotkey_id;
  159. obs_hotkey_t *hotkey;
  160. };
  161. struct obs_hotkey_name_map_item;
  162. void obs_hotkey_name_map_free(void);
  163. /* ------------------------------------------------------------------------- */
  164. /* views */
  165. struct obs_view {
  166. pthread_mutex_t channels_mutex;
  167. obs_source_t *channels[MAX_CHANNELS];
  168. };
  169. extern bool obs_view_init(struct obs_view *view);
  170. extern void obs_view_free(struct obs_view *view);
  171. /* ------------------------------------------------------------------------- */
  172. /* displays */
  173. struct obs_display {
  174. bool update_color_space;
  175. bool enabled;
  176. uint32_t cx, cy;
  177. uint32_t next_cx, next_cy;
  178. uint32_t background_color;
  179. gs_swapchain_t *swap;
  180. pthread_mutex_t draw_callbacks_mutex;
  181. pthread_mutex_t draw_info_mutex;
  182. DARRAY(struct draw_callback) draw_callbacks;
  183. bool use_clear_workaround;
  184. struct obs_display *next;
  185. struct obs_display **prev_next;
  186. };
  187. extern bool obs_display_init(struct obs_display *display,
  188. const struct gs_init_data *graphics_data);
  189. extern void obs_display_free(struct obs_display *display);
  190. /* ------------------------------------------------------------------------- */
  191. /* core */
  192. struct obs_vframe_info {
  193. uint64_t timestamp;
  194. int count;
  195. };
  196. struct obs_tex_frame {
  197. gs_texture_t *tex;
  198. gs_texture_t *tex_uv;
  199. uint32_t handle;
  200. uint64_t timestamp;
  201. uint64_t lock_key;
  202. int count;
  203. bool released;
  204. };
  205. struct obs_task_info {
  206. obs_task_t task;
  207. void *param;
  208. };
  209. struct obs_core_video_mix {
  210. struct obs_view *view;
  211. gs_stagesurf_t *active_copy_surfaces[NUM_TEXTURES][NUM_CHANNELS];
  212. gs_stagesurf_t *copy_surfaces[NUM_TEXTURES][NUM_CHANNELS];
  213. gs_texture_t *convert_textures[NUM_CHANNELS];
  214. gs_texture_t *convert_textures_encode[NUM_CHANNELS];
  215. #ifdef _WIN32
  216. gs_stagesurf_t *copy_surfaces_encode[NUM_TEXTURES];
  217. #endif
  218. gs_texture_t *render_texture;
  219. gs_texture_t *output_texture;
  220. enum gs_color_space render_space;
  221. bool texture_rendered;
  222. bool textures_copied[NUM_TEXTURES];
  223. bool texture_converted;
  224. bool using_nv12_tex;
  225. bool using_p010_tex;
  226. struct deque vframe_info_buffer;
  227. struct deque vframe_info_buffer_gpu;
  228. gs_stagesurf_t *mapped_surfaces[NUM_CHANNELS];
  229. int cur_texture;
  230. volatile long raw_active;
  231. volatile long gpu_encoder_active;
  232. bool gpu_was_active;
  233. bool raw_was_active;
  234. bool was_active;
  235. pthread_mutex_t gpu_encoder_mutex;
  236. struct deque gpu_encoder_queue;
  237. struct deque gpu_encoder_avail_queue;
  238. DARRAY(obs_encoder_t *) gpu_encoders;
  239. os_sem_t *gpu_encode_semaphore;
  240. os_event_t *gpu_encode_inactive;
  241. pthread_t gpu_encode_thread;
  242. bool gpu_encode_thread_initialized;
  243. volatile bool gpu_encode_stop;
  244. video_t *video;
  245. struct obs_video_info ovi;
  246. bool gpu_conversion;
  247. const char *conversion_techs[NUM_CHANNELS];
  248. bool conversion_needed;
  249. float conversion_width_i;
  250. float conversion_height_i;
  251. float color_matrix[16];
  252. bool encoder_only_mix;
  253. long encoder_refs;
  254. };
  255. extern struct obs_core_video_mix *
  256. obs_create_video_mix(struct obs_video_info *ovi);
  257. extern void obs_free_video_mix(struct obs_core_video_mix *video);
  258. struct obs_core_video {
  259. graphics_t *graphics;
  260. gs_effect_t *default_effect;
  261. gs_effect_t *default_rect_effect;
  262. gs_effect_t *opaque_effect;
  263. gs_effect_t *solid_effect;
  264. gs_effect_t *repeat_effect;
  265. gs_effect_t *conversion_effect;
  266. gs_effect_t *bicubic_effect;
  267. gs_effect_t *lanczos_effect;
  268. gs_effect_t *area_effect;
  269. gs_effect_t *bilinear_lowres_effect;
  270. gs_effect_t *premultiplied_alpha_effect;
  271. gs_samplerstate_t *point_sampler;
  272. uint64_t video_time;
  273. uint64_t video_frame_interval_ns;
  274. uint64_t video_half_frame_interval_ns;
  275. uint64_t video_avg_frame_time_ns;
  276. double video_fps;
  277. pthread_t video_thread;
  278. uint32_t total_frames;
  279. uint32_t lagged_frames;
  280. bool thread_initialized;
  281. gs_texture_t *transparent_texture;
  282. gs_effect_t *deinterlace_discard_effect;
  283. gs_effect_t *deinterlace_discard_2x_effect;
  284. gs_effect_t *deinterlace_linear_effect;
  285. gs_effect_t *deinterlace_linear_2x_effect;
  286. gs_effect_t *deinterlace_blend_effect;
  287. gs_effect_t *deinterlace_blend_2x_effect;
  288. gs_effect_t *deinterlace_yadif_effect;
  289. gs_effect_t *deinterlace_yadif_2x_effect;
  290. float sdr_white_level;
  291. float hdr_nominal_peak_level;
  292. pthread_mutex_t task_mutex;
  293. struct deque tasks;
  294. pthread_mutex_t encoder_group_mutex;
  295. DARRAY(obs_weak_encoder_t *) ready_encoder_groups;
  296. pthread_mutex_t mixes_mutex;
  297. DARRAY(struct obs_core_video_mix *) mixes;
  298. struct obs_core_video_mix *main_mix;
  299. };
  300. extern void add_ready_encoder_group(obs_encoder_t *encoder);
  301. struct audio_monitor;
  302. struct obs_core_audio {
  303. audio_t *audio;
  304. DARRAY(struct obs_source *) render_order;
  305. DARRAY(struct obs_source *) root_nodes;
  306. uint64_t buffered_ts;
  307. struct deque buffered_timestamps;
  308. uint64_t buffering_wait_ticks;
  309. int total_buffering_ticks;
  310. int max_buffering_ticks;
  311. bool fixed_buffer;
  312. pthread_mutex_t monitoring_mutex;
  313. DARRAY(struct audio_monitor *) monitors;
  314. char *monitoring_device_name;
  315. char *monitoring_device_id;
  316. pthread_mutex_t task_mutex;
  317. struct deque tasks;
  318. };
  319. /* user sources, output channels, and displays */
  320. struct obs_core_data {
  321. /* Hash tables (uthash) */
  322. struct obs_source *sources; /* Lookup by UUID (hh_uuid) */
  323. struct obs_source *public_sources; /* Lookup by name (hh) */
  324. /* Linked lists */
  325. struct obs_source *first_audio_source;
  326. struct obs_display *first_display;
  327. struct obs_output *first_output;
  328. struct obs_encoder *first_encoder;
  329. struct obs_service *first_service;
  330. pthread_mutex_t sources_mutex;
  331. pthread_mutex_t displays_mutex;
  332. pthread_mutex_t outputs_mutex;
  333. pthread_mutex_t encoders_mutex;
  334. pthread_mutex_t services_mutex;
  335. pthread_mutex_t audio_sources_mutex;
  336. pthread_mutex_t draw_callbacks_mutex;
  337. DARRAY(struct draw_callback) draw_callbacks;
  338. DARRAY(struct rendered_callback) rendered_callbacks;
  339. DARRAY(struct tick_callback) tick_callbacks;
  340. struct obs_view main_view;
  341. long long unnamed_index;
  342. obs_data_t *private_data;
  343. volatile bool valid;
  344. DARRAY(char *) protocols;
  345. DARRAY(obs_source_t *) sources_to_tick;
  346. };
  347. /* user hotkeys */
  348. struct obs_core_hotkeys {
  349. pthread_mutex_t mutex;
  350. obs_hotkey_t *hotkeys;
  351. obs_hotkey_id next_id;
  352. obs_hotkey_pair_t *hotkey_pairs;
  353. obs_hotkey_pair_id next_pair_id;
  354. pthread_t hotkey_thread;
  355. bool hotkey_thread_initialized;
  356. os_event_t *stop_event;
  357. bool thread_disable_press;
  358. bool strict_modifiers;
  359. bool reroute_hotkeys;
  360. DARRAY(obs_hotkey_binding_t) bindings;
  361. obs_hotkey_callback_router_func router_func;
  362. void *router_func_data;
  363. obs_hotkeys_platform_t *platform_context;
  364. pthread_once_t name_map_init_token;
  365. struct obs_hotkey_name_map_item *name_map;
  366. signal_handler_t *signals;
  367. char *translations[OBS_KEY_LAST_VALUE];
  368. char *mute;
  369. char *unmute;
  370. char *push_to_mute;
  371. char *push_to_talk;
  372. char *sceneitem_show;
  373. char *sceneitem_hide;
  374. };
  375. typedef DARRAY(struct obs_source_info) obs_source_info_array_t;
  376. struct obs_core {
  377. struct obs_module *first_module;
  378. DARRAY(struct obs_module_path) module_paths;
  379. DARRAY(char *) safe_modules;
  380. obs_source_info_array_t source_types;
  381. obs_source_info_array_t input_types;
  382. obs_source_info_array_t filter_types;
  383. obs_source_info_array_t transition_types;
  384. DARRAY(struct obs_output_info) output_types;
  385. DARRAY(struct obs_encoder_info) encoder_types;
  386. DARRAY(struct obs_service_info) service_types;
  387. signal_handler_t *signals;
  388. proc_handler_t *procs;
  389. char *locale;
  390. char *module_config_path;
  391. bool name_store_owned;
  392. profiler_name_store_t *name_store;
  393. /* segmented into multiple sub-structures to keep things a bit more
  394. * clean and organized */
  395. struct obs_core_video video;
  396. struct obs_core_audio audio;
  397. struct obs_core_data data;
  398. struct obs_core_hotkeys hotkeys;
  399. os_task_queue_t *destruction_task_thread;
  400. obs_task_handler_t ui_task_handler;
  401. };
  402. extern struct obs_core *obs;
  403. struct obs_graphics_context {
  404. uint64_t last_time;
  405. uint64_t interval;
  406. uint64_t frame_time_total_ns;
  407. uint64_t fps_total_ns;
  408. uint32_t fps_total_frames;
  409. const char *video_thread_name;
  410. };
  411. extern void *obs_graphics_thread(void *param);
  412. extern bool obs_graphics_thread_loop(struct obs_graphics_context *context);
  413. #ifdef __APPLE__
  414. extern void *obs_graphics_thread_autorelease(void *param);
  415. extern bool
  416. obs_graphics_thread_loop_autorelease(struct obs_graphics_context *context);
  417. #endif
  418. extern gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file);
  419. extern bool audio_callback(void *param, uint64_t start_ts_in,
  420. uint64_t end_ts_in, uint64_t *out_ts,
  421. uint32_t mixers, struct audio_output_data *mixes);
  422. extern struct obs_core_video_mix *get_mix_for_video(video_t *video);
  423. extern void
  424. start_raw_video(video_t *video, const struct video_scale_info *conversion,
  425. uint32_t frame_rate_divisor,
  426. void (*callback)(void *param, struct video_data *frame),
  427. void *param);
  428. extern void stop_raw_video(video_t *video,
  429. void (*callback)(void *param,
  430. struct video_data *frame),
  431. void *param);
  432. /* ------------------------------------------------------------------------- */
  433. /* obs shared context data */
  434. struct obs_weak_ref {
  435. volatile long refs;
  436. volatile long weak_refs;
  437. };
  438. struct obs_weak_object {
  439. struct obs_weak_ref ref;
  440. struct obs_context_data *object;
  441. };
  442. typedef void (*obs_destroy_cb)(void *obj);
  443. struct obs_context_data {
  444. char *name;
  445. const char *uuid;
  446. void *data;
  447. obs_data_t *settings;
  448. signal_handler_t *signals;
  449. proc_handler_t *procs;
  450. enum obs_obj_type type;
  451. struct obs_weak_object *control;
  452. obs_destroy_cb destroy;
  453. DARRAY(obs_hotkey_id) hotkeys;
  454. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  455. obs_data_t *hotkey_data;
  456. DARRAY(char *) rename_cache;
  457. pthread_mutex_t rename_cache_mutex;
  458. pthread_mutex_t *mutex;
  459. struct obs_context_data *next;
  460. struct obs_context_data **prev_next;
  461. UT_hash_handle hh;
  462. UT_hash_handle hh_uuid;
  463. bool private;
  464. };
  465. extern bool obs_context_data_init(struct obs_context_data *context,
  466. enum obs_obj_type type, obs_data_t *settings,
  467. const char *name, const char *uuid,
  468. obs_data_t *hotkey_data, bool private);
  469. extern void obs_context_init_control(struct obs_context_data *context,
  470. void *object, obs_destroy_cb destroy);
  471. extern void obs_context_data_free(struct obs_context_data *context);
  472. extern void obs_context_data_insert(struct obs_context_data *context,
  473. pthread_mutex_t *mutex, void *first);
  474. extern void obs_context_data_insert_name(struct obs_context_data *context,
  475. pthread_mutex_t *mutex, void *first);
  476. extern void obs_context_data_insert_uuid(struct obs_context_data *context,
  477. pthread_mutex_t *mutex,
  478. void *first_uuid);
  479. extern void obs_context_data_remove(struct obs_context_data *context);
  480. extern void obs_context_data_remove_name(struct obs_context_data *context,
  481. void *phead);
  482. extern void obs_context_data_remove_uuid(struct obs_context_data *context,
  483. void *puuid_head);
  484. extern void obs_context_wait(struct obs_context_data *context);
  485. extern void obs_context_data_setname(struct obs_context_data *context,
  486. const char *name);
  487. extern void obs_context_data_setname_ht(struct obs_context_data *context,
  488. const char *name, void *phead);
  489. /* ------------------------------------------------------------------------- */
  490. /* ref-counting */
  491. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  492. {
  493. os_atomic_inc_long(&ref->refs);
  494. }
  495. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  496. {
  497. return os_atomic_dec_long(&ref->refs) == -1;
  498. }
  499. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  500. {
  501. os_atomic_inc_long(&ref->weak_refs);
  502. }
  503. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  504. {
  505. return os_atomic_dec_long(&ref->weak_refs) == -1;
  506. }
  507. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  508. {
  509. long owners = os_atomic_load_long(&ref->refs);
  510. while (owners > -1) {
  511. if (os_atomic_compare_exchange_long(&ref->refs, &owners,
  512. owners + 1)) {
  513. return true;
  514. }
  515. }
  516. return false;
  517. }
  518. static inline bool obs_weak_ref_expired(struct obs_weak_ref *ref)
  519. {
  520. long owners = os_atomic_load_long(&ref->refs);
  521. return owners < 0;
  522. }
  523. /* ------------------------------------------------------------------------- */
  524. /* sources */
  525. struct async_frame {
  526. struct obs_source_frame *frame;
  527. long unused_count;
  528. bool used;
  529. };
  530. enum audio_action_type {
  531. AUDIO_ACTION_VOL,
  532. AUDIO_ACTION_MUTE,
  533. AUDIO_ACTION_PTT,
  534. AUDIO_ACTION_PTM,
  535. };
  536. struct audio_action {
  537. uint64_t timestamp;
  538. enum audio_action_type type;
  539. union {
  540. float vol;
  541. bool set;
  542. };
  543. };
  544. struct obs_weak_source {
  545. struct obs_weak_ref ref;
  546. struct obs_source *source;
  547. };
  548. struct audio_cb_info {
  549. obs_source_audio_capture_t callback;
  550. void *param;
  551. };
  552. struct caption_cb_info {
  553. obs_source_caption_t callback;
  554. void *param;
  555. };
  556. enum media_action_type {
  557. MEDIA_ACTION_NONE,
  558. MEDIA_ACTION_PLAY_PAUSE,
  559. MEDIA_ACTION_RESTART,
  560. MEDIA_ACTION_STOP,
  561. MEDIA_ACTION_NEXT,
  562. MEDIA_ACTION_PREVIOUS,
  563. MEDIA_ACTION_SET_TIME,
  564. };
  565. struct media_action {
  566. enum media_action_type type;
  567. union {
  568. bool pause;
  569. int64_t ms;
  570. };
  571. };
  572. struct obs_source {
  573. struct obs_context_data context;
  574. struct obs_source_info info;
  575. /* general exposed flags that can be set for the source */
  576. uint32_t flags;
  577. uint32_t default_flags;
  578. uint32_t last_obs_ver;
  579. /* indicates ownership of the info.id buffer */
  580. bool owns_info_id;
  581. /* signals to call the source update in the video thread */
  582. long defer_update_count;
  583. /* ensures show/hide are only called once */
  584. volatile long show_refs;
  585. /* ensures activate/deactivate are only called once */
  586. volatile long activate_refs;
  587. /* source is in the process of being destroyed */
  588. volatile long destroying;
  589. /* used to indicate that the source has been removed and all
  590. * references to it should be released (not exactly how I would prefer
  591. * to handle things but it's the best option) */
  592. bool removed;
  593. /* used to indicate if the source should show up when queried for user ui */
  594. bool temp_removed;
  595. bool active;
  596. bool showing;
  597. /* used to temporarily disable sources if needed */
  598. bool enabled;
  599. /* hint to allow sources to render more quickly */
  600. bool texcoords_centered;
  601. /* timing (if video is present, is based upon video) */
  602. volatile bool timing_set;
  603. volatile uint64_t timing_adjust;
  604. uint64_t resample_offset;
  605. uint64_t next_audio_ts_min;
  606. uint64_t next_audio_sys_ts_min;
  607. uint64_t last_frame_ts;
  608. uint64_t last_sys_timestamp;
  609. bool async_rendered;
  610. /* audio */
  611. bool audio_failed;
  612. bool audio_pending;
  613. bool pending_stop;
  614. bool audio_active;
  615. bool user_muted;
  616. bool muted;
  617. struct obs_source *next_audio_source;
  618. struct obs_source **prev_next_audio_source;
  619. uint64_t audio_ts;
  620. struct deque audio_input_buf[MAX_AUDIO_CHANNELS];
  621. size_t last_audio_input_buf_size;
  622. DARRAY(struct audio_action) audio_actions;
  623. float *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
  624. float *audio_mix_buf[MAX_AUDIO_CHANNELS];
  625. struct resample_info sample_info;
  626. audio_resampler_t *resampler;
  627. pthread_mutex_t audio_actions_mutex;
  628. pthread_mutex_t audio_buf_mutex;
  629. pthread_mutex_t audio_mutex;
  630. pthread_mutex_t audio_cb_mutex;
  631. DARRAY(struct audio_cb_info) audio_cb_list;
  632. struct obs_audio_data audio_data;
  633. size_t audio_storage_size;
  634. uint32_t audio_mixers;
  635. float user_volume;
  636. float volume;
  637. int64_t sync_offset;
  638. int64_t last_sync_offset;
  639. float balance;
  640. /* async video data */
  641. gs_texture_t *async_textures[MAX_AV_PLANES];
  642. gs_texrender_t *async_texrender;
  643. struct obs_source_frame *cur_async_frame;
  644. bool async_gpu_conversion;
  645. enum video_format async_format;
  646. bool async_full_range;
  647. uint8_t async_trc;
  648. enum video_format async_cache_format;
  649. bool async_cache_full_range;
  650. uint8_t async_cache_trc;
  651. enum gs_color_format async_texture_formats[MAX_AV_PLANES];
  652. int async_channel_count;
  653. long async_rotation;
  654. bool async_flip;
  655. bool async_linear_alpha;
  656. bool async_active;
  657. bool async_update_texture;
  658. bool async_unbuffered;
  659. bool async_decoupled;
  660. struct obs_source_frame *async_preload_frame;
  661. DARRAY(struct async_frame) async_cache;
  662. DARRAY(struct obs_source_frame *) async_frames;
  663. pthread_mutex_t async_mutex;
  664. uint32_t async_width;
  665. uint32_t async_height;
  666. uint32_t async_cache_width;
  667. uint32_t async_cache_height;
  668. uint32_t async_convert_width[MAX_AV_PLANES];
  669. uint32_t async_convert_height[MAX_AV_PLANES];
  670. uint64_t async_last_rendered_ts;
  671. pthread_mutex_t caption_cb_mutex;
  672. DARRAY(struct caption_cb_info) caption_cb_list;
  673. /* async video deinterlacing */
  674. uint64_t deinterlace_offset;
  675. uint64_t deinterlace_frame_ts;
  676. gs_effect_t *deinterlace_effect;
  677. struct obs_source_frame *prev_async_frame;
  678. gs_texture_t *async_prev_textures[MAX_AV_PLANES];
  679. gs_texrender_t *async_prev_texrender;
  680. uint32_t deinterlace_half_duration;
  681. enum obs_deinterlace_mode deinterlace_mode;
  682. bool deinterlace_top_first;
  683. bool deinterlace_rendered;
  684. /* filters */
  685. struct obs_source *filter_parent;
  686. struct obs_source *filter_target;
  687. DARRAY(struct obs_source *) filters;
  688. pthread_mutex_t filter_mutex;
  689. gs_texrender_t *filter_texrender;
  690. enum obs_allow_direct_render allow_direct;
  691. bool rendering_filter;
  692. bool filter_bypass_active;
  693. /* sources specific hotkeys */
  694. obs_hotkey_pair_id mute_unmute_key;
  695. obs_hotkey_id push_to_mute_key;
  696. obs_hotkey_id push_to_talk_key;
  697. bool push_to_mute_enabled;
  698. bool push_to_mute_pressed;
  699. bool user_push_to_mute_pressed;
  700. bool push_to_talk_enabled;
  701. bool push_to_talk_pressed;
  702. bool user_push_to_talk_pressed;
  703. uint64_t push_to_mute_delay;
  704. uint64_t push_to_mute_stop_time;
  705. uint64_t push_to_talk_delay;
  706. uint64_t push_to_talk_stop_time;
  707. /* transitions */
  708. uint64_t transition_start_time;
  709. uint64_t transition_duration;
  710. pthread_mutex_t transition_tex_mutex;
  711. gs_texrender_t *transition_texrender[2];
  712. pthread_mutex_t transition_mutex;
  713. obs_source_t *transition_sources[2];
  714. float transition_manual_clamp;
  715. float transition_manual_torque;
  716. float transition_manual_target;
  717. float transition_manual_val;
  718. bool transitioning_video;
  719. bool transitioning_audio;
  720. bool transition_source_active[2];
  721. uint32_t transition_alignment;
  722. uint32_t transition_actual_cx;
  723. uint32_t transition_actual_cy;
  724. uint32_t transition_cx;
  725. uint32_t transition_cy;
  726. uint32_t transition_fixed_duration;
  727. bool transition_use_fixed_duration;
  728. enum obs_transition_mode transition_mode;
  729. enum obs_transition_scale_type transition_scale_type;
  730. struct matrix4 transition_matrices[2];
  731. /* color space */
  732. gs_texrender_t *color_space_texrender;
  733. /* audio monitoring */
  734. struct audio_monitor *monitor;
  735. enum obs_monitoring_type monitoring_type;
  736. /* media action queue */
  737. DARRAY(struct media_action) media_actions;
  738. pthread_mutex_t media_actions_mutex;
  739. /* private data */
  740. obs_data_t *private_settings;
  741. };
  742. extern struct obs_source_info *get_source_info(const char *id);
  743. extern struct obs_source_info *get_source_info2(const char *unversioned_id,
  744. uint32_t ver);
  745. extern bool obs_source_init_context(struct obs_source *source,
  746. obs_data_t *settings, const char *name,
  747. const char *uuid, obs_data_t *hotkey_data,
  748. bool private);
  749. extern bool obs_transition_init(obs_source_t *transition);
  750. extern void obs_transition_free(obs_source_t *transition);
  751. extern void obs_transition_tick(obs_source_t *transition, float t);
  752. extern void obs_transition_enum_sources(obs_source_t *transition,
  753. obs_source_enum_proc_t enum_callback,
  754. void *param);
  755. extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
  756. extern void obs_transition_load(obs_source_t *source, obs_data_t *data);
  757. struct audio_monitor *audio_monitor_create(obs_source_t *source);
  758. void audio_monitor_reset(struct audio_monitor *monitor);
  759. extern void audio_monitor_destroy(struct audio_monitor *monitor);
  760. extern obs_source_t *
  761. obs_source_create_set_last_ver(const char *id, const char *name,
  762. const char *uuid, obs_data_t *settings,
  763. obs_data_t *hotkey_data, uint32_t last_obs_ver,
  764. bool is_private);
  765. extern void obs_source_destroy(struct obs_source *source);
  766. extern void obs_source_addref(obs_source_t *source);
  767. enum view_type {
  768. MAIN_VIEW,
  769. AUX_VIEW,
  770. };
  771. static inline void obs_source_dosignal(struct obs_source *source,
  772. const char *signal_obs,
  773. const char *signal_source)
  774. {
  775. struct calldata data;
  776. uint8_t stack[128];
  777. calldata_init_fixed(&data, stack, sizeof(stack));
  778. calldata_set_ptr(&data, "source", source);
  779. if (signal_obs && !source->context.private)
  780. signal_handler_signal(obs->signals, signal_obs, &data);
  781. if (signal_source)
  782. signal_handler_signal(source->context.signals, signal_source,
  783. &data);
  784. }
  785. /* maximum timestamp variance in nanoseconds */
  786. #define MAX_TS_VAR 2000000000ULL
  787. static inline bool frame_out_of_bounds(const obs_source_t *source, uint64_t ts)
  788. {
  789. if (ts < source->last_frame_ts)
  790. return ((source->last_frame_ts - ts) > MAX_TS_VAR);
  791. else
  792. return ((ts - source->last_frame_ts) > MAX_TS_VAR);
  793. }
  794. static inline enum gs_color_format
  795. convert_video_format(enum video_format format, enum video_trc trc)
  796. {
  797. switch (trc) {
  798. case VIDEO_TRC_PQ:
  799. case VIDEO_TRC_HLG:
  800. return GS_RGBA16F;
  801. default:
  802. switch (format) {
  803. case VIDEO_FORMAT_RGBA:
  804. return GS_RGBA;
  805. case VIDEO_FORMAT_BGRA:
  806. case VIDEO_FORMAT_I40A:
  807. case VIDEO_FORMAT_I42A:
  808. case VIDEO_FORMAT_YUVA:
  809. case VIDEO_FORMAT_AYUV:
  810. return GS_BGRA;
  811. case VIDEO_FORMAT_I010:
  812. case VIDEO_FORMAT_P010:
  813. case VIDEO_FORMAT_I210:
  814. case VIDEO_FORMAT_I412:
  815. case VIDEO_FORMAT_YA2L:
  816. case VIDEO_FORMAT_P216:
  817. case VIDEO_FORMAT_P416:
  818. case VIDEO_FORMAT_V210:
  819. case VIDEO_FORMAT_R10L:
  820. return GS_RGBA16F;
  821. default:
  822. return GS_BGRX;
  823. }
  824. }
  825. }
  826. static inline enum gs_color_space convert_video_space(enum video_format format,
  827. enum video_trc trc)
  828. {
  829. enum gs_color_space space = GS_CS_SRGB;
  830. if (convert_video_format(format, trc) == GS_RGBA16F) {
  831. switch (trc) {
  832. case VIDEO_TRC_DEFAULT:
  833. case VIDEO_TRC_SRGB:
  834. space = GS_CS_SRGB_16F;
  835. break;
  836. case VIDEO_TRC_PQ:
  837. case VIDEO_TRC_HLG:
  838. space = GS_CS_709_EXTENDED;
  839. }
  840. }
  841. return space;
  842. }
  843. extern void obs_source_set_texcoords_centered(obs_source_t *source,
  844. bool centered);
  845. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  846. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  847. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  848. extern float obs_source_get_target_volume(obs_source_t *source,
  849. obs_source_t *target);
  850. extern uint64_t obs_source_get_last_async_ts(const obs_source_t *source);
  851. extern void obs_source_audio_render(obs_source_t *source, uint32_t mixers,
  852. size_t channels, size_t sample_rate,
  853. size_t size);
  854. extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);
  855. extern struct obs_source_frame *filter_async_video(obs_source_t *source,
  856. struct obs_source_frame *in);
  857. extern bool update_async_texture(struct obs_source *source,
  858. const struct obs_source_frame *frame,
  859. gs_texture_t *tex, gs_texrender_t *texrender);
  860. extern bool update_async_textures(struct obs_source *source,
  861. const struct obs_source_frame *frame,
  862. gs_texture_t *tex[MAX_AV_PLANES],
  863. gs_texrender_t *texrender);
  864. extern bool set_async_texture_size(struct obs_source *source,
  865. const struct obs_source_frame *frame);
  866. extern void remove_async_frame(obs_source_t *source,
  867. struct obs_source_frame *frame);
  868. extern void set_deinterlace_texture_size(obs_source_t *source);
  869. extern void deinterlace_process_last_frame(obs_source_t *source,
  870. uint64_t sys_time);
  871. extern void deinterlace_update_async_video(obs_source_t *source);
  872. extern void deinterlace_render(obs_source_t *s);
  873. /* ------------------------------------------------------------------------- */
  874. /* outputs */
  875. enum delay_msg {
  876. DELAY_MSG_PACKET,
  877. DELAY_MSG_START,
  878. DELAY_MSG_STOP,
  879. };
  880. struct delay_data {
  881. enum delay_msg msg;
  882. uint64_t ts;
  883. struct encoder_packet packet;
  884. bool packet_time_valid;
  885. struct encoder_packet_time packet_time;
  886. };
  887. typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet,
  888. struct encoder_packet_time *frame_time);
  889. struct obs_weak_output {
  890. struct obs_weak_ref ref;
  891. struct obs_output *output;
  892. };
  893. #define CAPTION_LINE_CHARS (32)
  894. #define CAPTION_LINE_BYTES (4 * CAPTION_LINE_CHARS)
  895. struct caption_text {
  896. char text[CAPTION_LINE_BYTES + 1];
  897. double display_duration;
  898. struct caption_text *next;
  899. };
  900. struct caption_track_data {
  901. struct caption_text *caption_head;
  902. struct caption_text *caption_tail;
  903. pthread_mutex_t caption_mutex;
  904. double caption_timestamp;
  905. double last_caption_timestamp;
  906. struct deque caption_data;
  907. };
  908. struct pause_data {
  909. pthread_mutex_t mutex;
  910. uint64_t last_video_ts;
  911. uint64_t ts_start;
  912. uint64_t ts_end;
  913. uint64_t ts_offset;
  914. };
  915. extern bool video_pause_check(struct pause_data *pause, uint64_t timestamp);
  916. extern bool audio_pause_check(struct pause_data *pause, struct audio_data *data,
  917. size_t sample_rate);
  918. extern void pause_reset(struct pause_data *pause);
  919. enum keyframe_group_track_status {
  920. KEYFRAME_TRACK_STATUS_NOT_SEEN = 0,
  921. KEYFRAME_TRACK_STATUS_SEEN = 1,
  922. KEYFRAME_TRACK_STATUS_SKIPPED = 2,
  923. };
  924. struct keyframe_group_data {
  925. uintptr_t group_id;
  926. int64_t pts;
  927. uint32_t required_tracks;
  928. enum keyframe_group_track_status
  929. seen_on_track[MAX_OUTPUT_VIDEO_ENCODERS];
  930. };
  931. struct obs_output {
  932. struct obs_context_data context;
  933. struct obs_output_info info;
  934. /* indicates ownership of the info.id buffer */
  935. bool owns_info_id;
  936. bool received_video[MAX_OUTPUT_VIDEO_ENCODERS];
  937. DARRAY(struct keyframe_group_data) keyframe_group_tracking;
  938. bool received_audio;
  939. volatile bool data_active;
  940. volatile bool end_data_capture_thread_active;
  941. int64_t video_offsets[MAX_OUTPUT_VIDEO_ENCODERS];
  942. int64_t audio_offsets[MAX_OUTPUT_AUDIO_ENCODERS];
  943. int64_t highest_audio_ts;
  944. int64_t highest_video_ts[MAX_OUTPUT_VIDEO_ENCODERS];
  945. pthread_t end_data_capture_thread;
  946. os_event_t *stopping_event;
  947. pthread_mutex_t interleaved_mutex;
  948. DARRAY(struct encoder_packet) interleaved_packets;
  949. int stop_code;
  950. int reconnect_retry_sec;
  951. int reconnect_retry_max;
  952. int reconnect_retries;
  953. uint32_t reconnect_retry_cur_msec;
  954. float reconnect_retry_exp;
  955. pthread_t reconnect_thread;
  956. os_event_t *reconnect_stop_event;
  957. volatile bool reconnecting;
  958. volatile bool reconnect_thread_active;
  959. uint32_t starting_drawn_count;
  960. uint32_t starting_lagged_count;
  961. int total_frames;
  962. volatile bool active;
  963. volatile bool paused;
  964. video_t *video;
  965. audio_t *audio;
  966. obs_encoder_t *video_encoders[MAX_OUTPUT_VIDEO_ENCODERS];
  967. obs_encoder_t *audio_encoders[MAX_OUTPUT_AUDIO_ENCODERS];
  968. obs_service_t *service;
  969. size_t mixer_mask;
  970. struct pause_data pause;
  971. struct deque audio_buffer[MAX_AUDIO_MIXES][MAX_AV_PLANES];
  972. uint64_t audio_start_ts;
  973. uint64_t video_start_ts;
  974. size_t audio_size;
  975. size_t planes;
  976. size_t sample_rate;
  977. size_t total_audio_frames;
  978. uint32_t scaled_width;
  979. uint32_t scaled_height;
  980. bool video_conversion_set;
  981. bool audio_conversion_set;
  982. struct video_scale_info video_conversion;
  983. struct audio_convert_info audio_conversion;
  984. // captions are output per track
  985. struct caption_track_data *caption_tracks[MAX_OUTPUT_VIDEO_ENCODERS];
  986. DARRAY(struct encoder_packet_time) encoder_packet_times[MAX_OUTPUT_VIDEO_ENCODERS];
  987. bool valid;
  988. uint64_t active_delay_ns;
  989. encoded_callback_t delay_callback;
  990. struct deque delay_data; /* struct delay_data */
  991. pthread_mutex_t delay_mutex;
  992. uint32_t delay_sec;
  993. uint32_t delay_flags;
  994. uint32_t delay_cur_flags;
  995. volatile long delay_restart_refs;
  996. volatile bool delay_active;
  997. volatile bool delay_capturing;
  998. char *last_error_message;
  999. float audio_data[MAX_AUDIO_CHANNELS][AUDIO_OUTPUT_FRAMES];
  1000. };
  1001. static inline void do_output_signal(struct obs_output *output,
  1002. const char *signal)
  1003. {
  1004. struct calldata params = {0};
  1005. calldata_set_ptr(&params, "output", output);
  1006. signal_handler_signal(output->context.signals, signal, &params);
  1007. calldata_free(&params);
  1008. }
  1009. extern void process_delay(void *data, struct encoder_packet *packet,
  1010. struct encoder_packet_time *packet_time);
  1011. extern void obs_output_cleanup_delay(obs_output_t *output);
  1012. extern bool obs_output_delay_start(obs_output_t *output);
  1013. extern void obs_output_delay_stop(obs_output_t *output);
  1014. extern bool obs_output_actual_start(obs_output_t *output);
  1015. extern void obs_output_actual_stop(obs_output_t *output, bool force,
  1016. uint64_t ts);
  1017. extern const struct obs_output_info *find_output(const char *id);
  1018. extern void obs_output_remove_encoder(struct obs_output *output,
  1019. struct obs_encoder *encoder);
  1020. extern void
  1021. obs_encoder_packet_create_instance(struct encoder_packet *dst,
  1022. const struct encoder_packet *src);
  1023. void obs_output_destroy(obs_output_t *output);
  1024. /* ------------------------------------------------------------------------- */
  1025. /* encoders */
  1026. struct obs_weak_encoder {
  1027. struct obs_weak_ref ref;
  1028. struct obs_encoder *encoder;
  1029. };
  1030. struct encoder_callback {
  1031. bool sent_first_packet;
  1032. encoded_callback_t new_packet;
  1033. void *param;
  1034. };
  1035. struct obs_encoder_group {
  1036. pthread_mutex_t mutex;
  1037. /* allows group to be destroyed even if some encoders are active */
  1038. bool destroy_on_stop;
  1039. /* holds strong references to all encoders */
  1040. DARRAY(struct obs_encoder *) encoders;
  1041. uint32_t num_encoders_started;
  1042. uint64_t start_timestamp;
  1043. };
  1044. struct obs_encoder {
  1045. struct obs_context_data context;
  1046. struct obs_encoder_info info;
  1047. /* allows re-routing to another encoder */
  1048. struct obs_encoder_info orig_info;
  1049. pthread_mutex_t init_mutex;
  1050. uint32_t samplerate;
  1051. size_t planes;
  1052. size_t blocksize;
  1053. size_t framesize;
  1054. size_t framesize_bytes;
  1055. size_t mixer_idx;
  1056. /* OBS_SCALE_DISABLE indicates GPU scaling is disabled */
  1057. enum obs_scale_type gpu_scale_type;
  1058. uint32_t scaled_width;
  1059. uint32_t scaled_height;
  1060. enum video_format preferred_format;
  1061. volatile bool active;
  1062. volatile bool paused;
  1063. bool initialized;
  1064. /* indicates ownership of the info.id buffer */
  1065. bool owns_info_id;
  1066. uint32_t timebase_num;
  1067. uint32_t timebase_den;
  1068. // allow outputting at fractions of main composition FPS,
  1069. // e.g. 60 FPS with frame_rate_divisor = 1 turns into 30 FPS
  1070. //
  1071. // a separate counter is used in favor of using remainder calculations
  1072. // to allow "inputs" started at the same time to start on the same frame
  1073. // whereas with remainder calculation the frame alignment would depend on
  1074. // the total frame count at the time the encoder was started
  1075. uint32_t frame_rate_divisor;
  1076. uint32_t frame_rate_divisor_counter; // only used for GPU encoders
  1077. video_t *fps_override;
  1078. /* Regions of interest to prioritize during encoding */
  1079. pthread_mutex_t roi_mutex;
  1080. DARRAY(struct obs_encoder_roi) roi;
  1081. uint32_t roi_increment;
  1082. int64_t cur_pts;
  1083. struct deque audio_input_buffer[MAX_AV_PLANES];
  1084. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  1085. /* if a video encoder is paired with an audio encoder, make it start
  1086. * up at the specific timestamp. if this is the audio encoder,
  1087. * it waits until it's ready to sync up with video */
  1088. bool first_received;
  1089. DARRAY(struct obs_weak_encoder *) paired_encoders;
  1090. int64_t offset_usec;
  1091. uint64_t first_raw_ts;
  1092. uint64_t start_ts;
  1093. /* track encoders that are part of a gop-aligned multi track group */
  1094. struct obs_encoder_group *encoder_group;
  1095. pthread_mutex_t outputs_mutex;
  1096. DARRAY(obs_output_t *) outputs;
  1097. /* stores the video/audio media output pointer. video_t *or audio_t **/
  1098. void *media;
  1099. pthread_mutex_t callbacks_mutex;
  1100. DARRAY(struct encoder_callback) callbacks;
  1101. DARRAY(struct encoder_packet_time) encoder_packet_times;
  1102. struct pause_data pause;
  1103. const char *profile_encoder_encode_name;
  1104. char *last_error_message;
  1105. /* reconfigure encoder at next possible opportunity */
  1106. bool reconfigure_requested;
  1107. };
  1108. extern struct obs_encoder_info *find_encoder(const char *id);
  1109. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  1110. extern void obs_encoder_shutdown(obs_encoder_t *encoder);
  1111. extern void obs_encoder_start(obs_encoder_t *encoder,
  1112. encoded_callback_t new_packet, void *param);
  1113. extern void obs_encoder_stop(obs_encoder_t *encoder,
  1114. encoded_callback_t new_packet, void *param);
  1115. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  1116. struct obs_output *output);
  1117. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  1118. struct obs_output *output);
  1119. extern bool start_gpu_encode(obs_encoder_t *encoder);
  1120. extern void stop_gpu_encode(obs_encoder_t *encoder);
  1121. extern bool do_encode(struct obs_encoder *encoder, struct encoder_frame *frame,
  1122. const uint64_t *frame_cts);
  1123. extern void send_off_encoder_packet(obs_encoder_t *encoder, bool success,
  1124. bool received, struct encoder_packet *pkt);
  1125. void obs_encoder_destroy(obs_encoder_t *encoder);
  1126. /* ------------------------------------------------------------------------- */
  1127. /* services */
  1128. struct obs_weak_service {
  1129. struct obs_weak_ref ref;
  1130. struct obs_service *service;
  1131. };
  1132. struct obs_service {
  1133. struct obs_context_data context;
  1134. struct obs_service_info info;
  1135. /* indicates ownership of the info.id buffer */
  1136. bool owns_info_id;
  1137. bool active;
  1138. bool destroy;
  1139. struct obs_output *output;
  1140. };
  1141. extern const struct obs_service_info *find_service(const char *id);
  1142. extern void obs_service_activate(struct obs_service *service);
  1143. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  1144. extern bool obs_service_initialize(struct obs_service *service,
  1145. struct obs_output *output);
  1146. void obs_service_destroy(obs_service_t *service);
  1147. void obs_output_remove_encoder_internal(struct obs_output *output,
  1148. struct obs_encoder *encoder);
  1149. /** Internal Source Profiler functions **/
  1150. /* Start of frame in graphics loop */
  1151. extern void source_profiler_frame_begin(void);
  1152. /* Process data collected during frame */
  1153. extern void source_profiler_frame_collect(void);
  1154. /* Start/end of outputs being rendered (GPU timer begin/end) */
  1155. extern void source_profiler_render_begin(void);
  1156. extern void source_profiler_render_end(void);
  1157. /* Reset settings, buffers, and GPU timers when video settings change */
  1158. extern void source_profiler_reset_video(struct obs_video_info *ovi);
  1159. /* Signal that source received an async frame */
  1160. extern void source_profiler_async_frame_received(obs_source_t *source);
  1161. /* Get timestamp for start of tick */
  1162. extern uint64_t source_profiler_source_tick_start(void);
  1163. /* Submit start timestamp for source */
  1164. extern void source_profiler_source_tick_end(obs_source_t *source,
  1165. uint64_t start);
  1166. /* Obtain GPU timer and start timestamp for render start of a source. */
  1167. extern uint64_t source_profiler_source_render_begin(gs_timer_t **timer);
  1168. /* Submit start timestamp and GPU timer after rendering source */
  1169. extern void source_profiler_source_render_end(obs_source_t *source,
  1170. uint64_t start,
  1171. gs_timer_t *timer);
  1172. /* Remove source from profiler hashmaps */
  1173. extern void source_profiler_remove_source(obs_source_t *source);