obs-internal.h 33 KB

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