obs-internal.h 37 KB

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