obs-internal.h 35 KB

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