obs-internal.h 33 KB

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