obs-internal.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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 "callback/signal.h"
  23. #include "callback/proc.h"
  24. #include "graphics/graphics.h"
  25. #include "graphics/matrix4.h"
  26. #include "media-io/audio-resampler.h"
  27. #include "media-io/video-io.h"
  28. #include "media-io/audio-io.h"
  29. #include "obs.h"
  30. #define NUM_TEXTURES 2
  31. #define MICROSECOND_DEN 1000000
  32. static inline int64_t packet_dts_usec(struct encoder_packet *packet)
  33. {
  34. return packet->dts * MICROSECOND_DEN / packet->timebase_den;
  35. }
  36. struct draw_callback {
  37. void (*draw)(void *param, uint32_t cx, uint32_t cy);
  38. void *param;
  39. };
  40. /* ------------------------------------------------------------------------- */
  41. /* validity checks */
  42. static inline bool obs_object_valid(const void *obj, const char *f,
  43. const char *t)
  44. {
  45. if (!obj) {
  46. blog(LOG_DEBUG, "%s: Null '%s' parameter", f, t);
  47. return false;
  48. }
  49. return true;
  50. }
  51. #define obs_ptr_valid(ptr, func) obs_object_valid(ptr, func, #ptr)
  52. #define obs_source_valid obs_ptr_valid
  53. #define obs_output_valid obs_ptr_valid
  54. #define obs_encoder_valid obs_ptr_valid
  55. #define obs_service_valid obs_ptr_valid
  56. /* ------------------------------------------------------------------------- */
  57. /* modules */
  58. struct obs_module {
  59. char *mod_name;
  60. const char *file;
  61. char *bin_path;
  62. char *data_path;
  63. void *module;
  64. bool loaded;
  65. bool (*load)(void);
  66. void (*unload)(void);
  67. void (*set_locale)(const char *locale);
  68. void (*free_locale)(void);
  69. uint32_t (*ver)(void);
  70. void (*set_pointer)(obs_module_t *module);
  71. const char *(*name)(void);
  72. const char *(*description)(void);
  73. const char *(*author)(void);
  74. struct obs_module *next;
  75. };
  76. extern void free_module(struct obs_module *mod);
  77. struct obs_module_path {
  78. char *bin;
  79. char *data;
  80. };
  81. static inline void free_module_path(struct obs_module_path *omp)
  82. {
  83. if (omp) {
  84. bfree(omp->bin);
  85. bfree(omp->data);
  86. }
  87. }
  88. static inline bool check_path(const char *data, const char *path,
  89. struct dstr *output)
  90. {
  91. dstr_copy(output, path);
  92. dstr_cat(output, data);
  93. return os_file_exists(output->array);
  94. }
  95. /* ------------------------------------------------------------------------- */
  96. /* hotkeys */
  97. struct obs_hotkey {
  98. obs_hotkey_id id;
  99. char *name;
  100. char *description;
  101. obs_hotkey_func func;
  102. void *data;
  103. int pressed;
  104. obs_hotkey_registerer_t registerer_type;
  105. void *registerer;
  106. obs_hotkey_id pair_partner_id;
  107. };
  108. struct obs_hotkey_pair {
  109. obs_hotkey_pair_id pair_id;
  110. obs_hotkey_id id[2];
  111. obs_hotkey_active_func func[2];
  112. bool pressed0 : 1;
  113. bool pressed1 : 1;
  114. void *data[2];
  115. };
  116. typedef struct obs_hotkey_pair obs_hotkey_pair_t;
  117. typedef struct obs_hotkeys_platform obs_hotkeys_platform_t;
  118. void *obs_hotkey_thread(void *param);
  119. struct obs_core_hotkeys;
  120. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys);
  121. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys);
  122. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  123. obs_key_t key);
  124. const char *obs_get_hotkey_translation(obs_key_t key, const char *def);
  125. struct obs_context_data;
  126. void obs_hotkeys_context_release(struct obs_context_data *context);
  127. void obs_hotkeys_free(void);
  128. struct obs_hotkey_binding {
  129. obs_key_combination_t key;
  130. bool pressed : 1;
  131. bool modifiers_match : 1;
  132. obs_hotkey_id hotkey_id;
  133. obs_hotkey_t *hotkey;
  134. };
  135. struct obs_hotkey_name_map;
  136. void obs_hotkey_name_map_free(void);
  137. /* ------------------------------------------------------------------------- */
  138. /* views */
  139. struct obs_view {
  140. pthread_mutex_t channels_mutex;
  141. obs_source_t *channels[MAX_CHANNELS];
  142. };
  143. extern bool obs_view_init(struct obs_view *view);
  144. extern void obs_view_free(struct obs_view *view);
  145. /* ------------------------------------------------------------------------- */
  146. /* displays */
  147. struct obs_display {
  148. bool size_changed;
  149. bool enabled;
  150. uint32_t cx, cy;
  151. uint32_t background_color;
  152. gs_swapchain_t *swap;
  153. pthread_mutex_t draw_callbacks_mutex;
  154. DARRAY(struct draw_callback) draw_callbacks;
  155. struct obs_display *next;
  156. struct obs_display **prev_next;
  157. };
  158. extern bool obs_display_init(struct obs_display *display,
  159. const struct gs_init_data *graphics_data);
  160. extern void obs_display_free(struct obs_display *display);
  161. /* ------------------------------------------------------------------------- */
  162. /* core */
  163. struct obs_vframe_info {
  164. uint64_t timestamp;
  165. int count;
  166. };
  167. struct obs_core_video {
  168. graphics_t *graphics;
  169. gs_stagesurf_t *copy_surfaces[NUM_TEXTURES];
  170. gs_texture_t *render_textures[NUM_TEXTURES];
  171. gs_texture_t *output_textures[NUM_TEXTURES];
  172. gs_texture_t *convert_textures[NUM_TEXTURES];
  173. bool textures_rendered[NUM_TEXTURES];
  174. bool textures_output[NUM_TEXTURES];
  175. bool textures_copied[NUM_TEXTURES];
  176. bool textures_converted[NUM_TEXTURES];
  177. struct circlebuf vframe_info_buffer;
  178. gs_effect_t *default_effect;
  179. gs_effect_t *default_rect_effect;
  180. gs_effect_t *opaque_effect;
  181. gs_effect_t *solid_effect;
  182. gs_effect_t *conversion_effect;
  183. gs_effect_t *bicubic_effect;
  184. gs_effect_t *lanczos_effect;
  185. gs_effect_t *bilinear_lowres_effect;
  186. gs_stagesurf_t *mapped_surface;
  187. int cur_texture;
  188. uint64_t video_time;
  189. video_t *video;
  190. pthread_t video_thread;
  191. uint32_t total_frames;
  192. uint32_t lagged_frames;
  193. bool thread_initialized;
  194. bool gpu_conversion;
  195. const char *conversion_tech;
  196. uint32_t conversion_height;
  197. uint32_t plane_offsets[3];
  198. uint32_t plane_sizes[3];
  199. uint32_t plane_linewidth[3];
  200. uint32_t output_width;
  201. uint32_t output_height;
  202. uint32_t base_width;
  203. uint32_t base_height;
  204. float color_matrix[16];
  205. enum obs_scale_type scale_type;
  206. gs_texture_t *transparent_texture;
  207. };
  208. struct obs_core_audio {
  209. /* TODO: sound output subsystem */
  210. audio_t *audio;
  211. DARRAY(struct obs_source*) render_order;
  212. DARRAY(struct obs_source*) root_nodes;
  213. uint64_t buffered_ts;
  214. struct circlebuf buffered_timestamps;
  215. int buffering_wait_ticks;
  216. int total_buffering_ticks;
  217. float user_volume;
  218. };
  219. /* user sources, output channels, and displays */
  220. struct obs_core_data {
  221. struct obs_source *first_source;
  222. struct obs_source *first_audio_source;
  223. struct obs_display *first_display;
  224. struct obs_output *first_output;
  225. struct obs_encoder *first_encoder;
  226. struct obs_service *first_service;
  227. pthread_mutex_t sources_mutex;
  228. pthread_mutex_t displays_mutex;
  229. pthread_mutex_t outputs_mutex;
  230. pthread_mutex_t encoders_mutex;
  231. pthread_mutex_t services_mutex;
  232. pthread_mutex_t audio_sources_mutex;
  233. struct obs_view main_view;
  234. long long unnamed_index;
  235. volatile bool valid;
  236. };
  237. /* user hotkeys */
  238. struct obs_core_hotkeys {
  239. pthread_mutex_t mutex;
  240. DARRAY(obs_hotkey_t) hotkeys;
  241. obs_hotkey_id next_id;
  242. DARRAY(obs_hotkey_pair_t) hotkey_pairs;
  243. obs_hotkey_pair_id next_pair_id;
  244. pthread_t hotkey_thread;
  245. bool hotkey_thread_initialized;
  246. os_event_t *stop_event;
  247. bool thread_disable_press : 1;
  248. bool strict_modifiers : 1;
  249. bool reroute_hotkeys : 1;
  250. DARRAY(obs_hotkey_binding_t) bindings;
  251. obs_hotkey_callback_router_func router_func;
  252. void *router_func_data;
  253. obs_hotkeys_platform_t *platform_context;
  254. pthread_once_t name_map_init_token;
  255. struct obs_hotkey_name_map *name_map;
  256. signal_handler_t *signals;
  257. char *translations[OBS_KEY_LAST_VALUE];
  258. char *mute;
  259. char *unmute;
  260. char *push_to_mute;
  261. char *push_to_talk;
  262. char *sceneitem_show;
  263. char *sceneitem_hide;
  264. };
  265. struct obs_core {
  266. struct obs_module *first_module;
  267. DARRAY(struct obs_module_path) module_paths;
  268. DARRAY(struct obs_source_info) source_types;
  269. DARRAY(struct obs_source_info) input_types;
  270. DARRAY(struct obs_source_info) filter_types;
  271. DARRAY(struct obs_source_info) transition_types;
  272. DARRAY(struct obs_output_info) output_types;
  273. DARRAY(struct obs_encoder_info) encoder_types;
  274. DARRAY(struct obs_service_info) service_types;
  275. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  276. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  277. signal_handler_t *signals;
  278. proc_handler_t *procs;
  279. char *locale;
  280. char *module_config_path;
  281. bool name_store_owned;
  282. profiler_name_store_t *name_store;
  283. /* segmented into multiple sub-structures to keep things a bit more
  284. * clean and organized */
  285. struct obs_core_video video;
  286. struct obs_core_audio audio;
  287. struct obs_core_data data;
  288. struct obs_core_hotkeys hotkeys;
  289. };
  290. extern struct obs_core *obs;
  291. extern void *obs_video_thread(void *param);
  292. extern gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file);
  293. extern bool audio_callback(void *param,
  294. uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts,
  295. uint32_t mixers, struct audio_output_data *mixes);
  296. /* ------------------------------------------------------------------------- */
  297. /* obs shared context data */
  298. struct obs_context_data {
  299. char *name;
  300. void *data;
  301. obs_data_t *settings;
  302. signal_handler_t *signals;
  303. proc_handler_t *procs;
  304. enum obs_obj_type type;
  305. DARRAY(obs_hotkey_id) hotkeys;
  306. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  307. obs_data_t *hotkey_data;
  308. DARRAY(char*) rename_cache;
  309. pthread_mutex_t rename_cache_mutex;
  310. pthread_mutex_t *mutex;
  311. struct obs_context_data *next;
  312. struct obs_context_data **prev_next;
  313. bool private;
  314. };
  315. extern bool obs_context_data_init(
  316. struct obs_context_data *context,
  317. enum obs_obj_type type,
  318. obs_data_t *settings,
  319. const char *name,
  320. obs_data_t *hotkey_data,
  321. bool private);
  322. extern void obs_context_data_free(struct obs_context_data *context);
  323. extern void obs_context_data_insert(struct obs_context_data *context,
  324. pthread_mutex_t *mutex, void *first);
  325. extern void obs_context_data_remove(struct obs_context_data *context);
  326. extern void obs_context_data_setname(struct obs_context_data *context,
  327. const char *name);
  328. /* ------------------------------------------------------------------------- */
  329. /* ref-counting */
  330. struct obs_weak_ref {
  331. volatile long refs;
  332. volatile long weak_refs;
  333. };
  334. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  335. {
  336. os_atomic_inc_long(&ref->refs);
  337. }
  338. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  339. {
  340. return os_atomic_dec_long(&ref->refs) == -1;
  341. }
  342. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  343. {
  344. os_atomic_inc_long(&ref->weak_refs);
  345. }
  346. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  347. {
  348. return os_atomic_dec_long(&ref->weak_refs) == -1;
  349. }
  350. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  351. {
  352. long owners = ref->refs;
  353. while (owners > -1) {
  354. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  355. return true;
  356. owners = ref->refs;
  357. }
  358. return false;
  359. }
  360. /* ------------------------------------------------------------------------- */
  361. /* sources */
  362. struct async_frame {
  363. struct obs_source_frame *frame;
  364. long unused_count;
  365. bool used;
  366. };
  367. enum audio_action_type {
  368. AUDIO_ACTION_VOL,
  369. AUDIO_ACTION_MUTE,
  370. AUDIO_ACTION_PTT,
  371. AUDIO_ACTION_PTM,
  372. };
  373. struct audio_action {
  374. uint64_t timestamp;
  375. enum audio_action_type type;
  376. union {
  377. float vol;
  378. bool set;
  379. };
  380. };
  381. struct obs_weak_source {
  382. struct obs_weak_ref ref;
  383. struct obs_source *source;
  384. };
  385. struct audio_cb_info {
  386. obs_source_audio_capture_t callback;
  387. void *param;
  388. };
  389. struct obs_source {
  390. struct obs_context_data context;
  391. struct obs_source_info info;
  392. struct obs_weak_source *control;
  393. /* general exposed flags that can be set for the source */
  394. uint32_t flags;
  395. uint32_t default_flags;
  396. /* indicates ownership of the info.id buffer */
  397. bool owns_info_id;
  398. /* signals to call the source update in the video thread */
  399. bool defer_update;
  400. /* ensures show/hide are only called once */
  401. volatile long show_refs;
  402. /* ensures activate/deactivate are only called once */
  403. volatile long activate_refs;
  404. /* used to indicate that the source has been removed and all
  405. * references to it should be released (not exactly how I would prefer
  406. * to handle things but it's the best option) */
  407. bool removed;
  408. bool active;
  409. bool showing;
  410. /* used to temporarily disable sources if needed */
  411. bool enabled;
  412. /* timing (if video is present, is based upon video) */
  413. volatile bool timing_set;
  414. volatile uint64_t timing_adjust;
  415. uint64_t resample_offset;
  416. uint64_t next_audio_ts_min;
  417. uint64_t next_audio_sys_ts_min;
  418. uint64_t last_frame_ts;
  419. uint64_t last_sys_timestamp;
  420. bool async_rendered;
  421. /* audio */
  422. bool audio_failed;
  423. bool audio_pending;
  424. bool user_muted;
  425. bool muted;
  426. struct obs_source *next_audio_source;
  427. struct obs_source **prev_next_audio_source;
  428. uint64_t audio_ts;
  429. struct circlebuf audio_input_buf[MAX_AUDIO_CHANNELS];
  430. size_t last_audio_input_buf_size;
  431. DARRAY(struct audio_action) audio_actions;
  432. float *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
  433. struct resample_info sample_info;
  434. audio_resampler_t *resampler;
  435. pthread_mutex_t audio_actions_mutex;
  436. pthread_mutex_t audio_buf_mutex;
  437. pthread_mutex_t audio_mutex;
  438. pthread_mutex_t audio_cb_mutex;
  439. DARRAY(struct audio_cb_info) audio_cb_list;
  440. struct obs_audio_data audio_data;
  441. size_t audio_storage_size;
  442. uint32_t audio_mixers;
  443. float user_volume;
  444. float volume;
  445. int64_t sync_offset;
  446. int64_t last_sync_offset;
  447. /* async video data */
  448. gs_texture_t *async_texture;
  449. gs_texrender_t *async_convert_texrender;
  450. struct obs_source_frame *cur_async_frame;
  451. bool async_gpu_conversion;
  452. enum video_format async_format;
  453. enum video_format async_cache_format;
  454. enum gs_color_format async_texture_format;
  455. float async_color_matrix[16];
  456. bool async_full_range;
  457. float async_color_range_min[3];
  458. float async_color_range_max[3];
  459. int async_plane_offset[2];
  460. bool async_flip;
  461. bool async_active;
  462. DARRAY(struct async_frame) async_cache;
  463. DARRAY(struct obs_source_frame*)async_frames;
  464. pthread_mutex_t async_mutex;
  465. uint32_t async_width;
  466. uint32_t async_height;
  467. uint32_t async_cache_width;
  468. uint32_t async_cache_height;
  469. uint32_t async_convert_width;
  470. uint32_t async_convert_height;
  471. /* filters */
  472. struct obs_source *filter_parent;
  473. struct obs_source *filter_target;
  474. DARRAY(struct obs_source*) filters;
  475. pthread_mutex_t filter_mutex;
  476. gs_texrender_t *filter_texrender;
  477. enum obs_allow_direct_render allow_direct;
  478. bool rendering_filter;
  479. /* sources specific hotkeys */
  480. obs_hotkey_pair_id mute_unmute_key;
  481. obs_hotkey_id push_to_mute_key;
  482. obs_hotkey_id push_to_talk_key;
  483. bool push_to_mute_enabled : 1;
  484. bool push_to_mute_pressed : 1;
  485. bool user_push_to_mute_pressed : 1;
  486. bool push_to_talk_enabled : 1;
  487. bool push_to_talk_pressed : 1;
  488. bool user_push_to_talk_pressed : 1;
  489. uint64_t push_to_mute_delay;
  490. uint64_t push_to_mute_stop_time;
  491. uint64_t push_to_talk_delay;
  492. uint64_t push_to_talk_stop_time;
  493. /* transitions */
  494. uint64_t transition_start_time;
  495. uint64_t transition_duration;
  496. pthread_mutex_t transition_tex_mutex;
  497. gs_texrender_t *transition_texrender[2];
  498. pthread_mutex_t transition_mutex;
  499. obs_source_t *transition_sources[2];
  500. bool transitioning_video;
  501. bool transitioning_audio;
  502. bool transition_source_active[2];
  503. uint32_t transition_alignment;
  504. uint32_t transition_actual_cx;
  505. uint32_t transition_actual_cy;
  506. uint32_t transition_cx;
  507. uint32_t transition_cy;
  508. uint32_t transition_fixed_duration;
  509. bool transition_use_fixed_duration : 1;
  510. enum obs_transition_mode transition_mode;
  511. enum obs_transition_scale_type transition_scale_type;
  512. struct matrix4 transition_matrices[2];
  513. };
  514. extern const struct obs_source_info *get_source_info(const char *id);
  515. extern bool obs_source_init_context(struct obs_source *source,
  516. obs_data_t *settings, const char *name,
  517. obs_data_t *hotkey_data, bool private);
  518. extern void obs_source_save(obs_source_t *source);
  519. extern void obs_source_load(obs_source_t *source);
  520. extern bool obs_transition_init(obs_source_t *transition);
  521. extern void obs_transition_free(obs_source_t *transition);
  522. extern void obs_transition_tick(obs_source_t *transition);
  523. extern void obs_transition_enum_sources(obs_source_t *transition,
  524. obs_source_enum_proc_t enum_callback, void *param);
  525. extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
  526. extern void obs_transition_load(obs_source_t *source, obs_data_t *data);
  527. extern void obs_source_destroy(struct obs_source *source);
  528. enum view_type {
  529. MAIN_VIEW,
  530. AUX_VIEW
  531. };
  532. static inline void obs_source_dosignal(struct obs_source *source,
  533. const char *signal_obs, const char *signal_source)
  534. {
  535. struct calldata data;
  536. uint8_t stack[128];
  537. calldata_init_fixed(&data, stack, sizeof(stack));
  538. calldata_set_ptr(&data, "source", source);
  539. if (signal_obs && !source->context.private)
  540. signal_handler_signal(obs->signals, signal_obs, &data);
  541. if (signal_source)
  542. signal_handler_signal(source->context.signals, signal_source,
  543. &data);
  544. }
  545. /* maximum timestamp variance in nanoseconds */
  546. #define MAX_TS_VAR 2000000000ULL
  547. static inline bool frame_out_of_bounds(const obs_source_t *source, uint64_t ts)
  548. {
  549. if (ts < source->last_frame_ts)
  550. return ((source->last_frame_ts - ts) > MAX_TS_VAR);
  551. else
  552. return ((ts - source->last_frame_ts) > MAX_TS_VAR);
  553. }
  554. static inline enum gs_color_format convert_video_format(
  555. enum video_format format)
  556. {
  557. if (format == VIDEO_FORMAT_RGBA)
  558. return GS_RGBA;
  559. else if (format == VIDEO_FORMAT_BGRA)
  560. return GS_BGRA;
  561. return GS_BGRX;
  562. }
  563. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  564. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  565. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  566. extern float obs_source_get_target_volume(obs_source_t *source,
  567. obs_source_t *target);
  568. extern void obs_source_audio_render(obs_source_t *source, uint32_t mixers,
  569. size_t channels, size_t sample_rate, size_t size);
  570. extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);
  571. extern struct obs_source_frame *filter_async_video(obs_source_t *source,
  572. struct obs_source_frame *in);
  573. extern bool set_async_texture_size(struct obs_source *source,
  574. const struct obs_source_frame *frame);
  575. extern void remove_async_frame(obs_source_t *source,
  576. struct obs_source_frame *frame);
  577. /* ------------------------------------------------------------------------- */
  578. /* outputs */
  579. enum delay_msg {
  580. DELAY_MSG_PACKET,
  581. DELAY_MSG_START,
  582. DELAY_MSG_STOP,
  583. };
  584. struct delay_data {
  585. enum delay_msg msg;
  586. uint64_t ts;
  587. struct encoder_packet packet;
  588. };
  589. typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);
  590. struct obs_weak_output {
  591. struct obs_weak_ref ref;
  592. struct obs_output *output;
  593. };
  594. struct obs_output {
  595. struct obs_context_data context;
  596. struct obs_output_info info;
  597. struct obs_weak_output *control;
  598. /* indicates ownership of the info.id buffer */
  599. bool owns_info_id;
  600. bool received_video;
  601. bool received_audio;
  602. int64_t video_offset;
  603. int64_t audio_offsets[MAX_AUDIO_MIXES];
  604. int64_t highest_audio_ts;
  605. int64_t highest_video_ts;
  606. pthread_mutex_t interleaved_mutex;
  607. DARRAY(struct encoder_packet) interleaved_packets;
  608. int reconnect_retry_sec;
  609. int reconnect_retry_max;
  610. int reconnect_retries;
  611. int reconnect_retry_cur_sec;
  612. bool reconnecting;
  613. pthread_t reconnect_thread;
  614. os_event_t *reconnect_stop_event;
  615. volatile bool reconnect_thread_active;
  616. uint32_t starting_drawn_count;
  617. uint32_t starting_lagged_count;
  618. uint32_t starting_frame_count;
  619. uint32_t starting_skipped_frame_count;
  620. int total_frames;
  621. bool active;
  622. volatile bool stopped;
  623. video_t *video;
  624. audio_t *audio;
  625. obs_encoder_t *video_encoder;
  626. obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
  627. obs_service_t *service;
  628. size_t mixer_idx;
  629. uint32_t scaled_width;
  630. uint32_t scaled_height;
  631. bool video_conversion_set;
  632. bool audio_conversion_set;
  633. struct video_scale_info video_conversion;
  634. struct audio_convert_info audio_conversion;
  635. bool valid;
  636. uint64_t active_delay_ns;
  637. encoded_callback_t delay_callback;
  638. struct circlebuf delay_data; /* struct delay_data */
  639. pthread_mutex_t delay_mutex;
  640. uint32_t delay_sec;
  641. uint32_t delay_flags;
  642. uint32_t delay_cur_flags;
  643. volatile long delay_restart_refs;
  644. bool delay_active;
  645. bool delay_capturing;
  646. };
  647. static inline void do_output_signal(struct obs_output *output,
  648. const char *signal)
  649. {
  650. struct calldata params = {0};
  651. calldata_set_ptr(&params, "output", output);
  652. signal_handler_signal(output->context.signals, signal, &params);
  653. calldata_free(&params);
  654. }
  655. extern void process_delay(void *data, struct encoder_packet *packet);
  656. extern void obs_output_cleanup_delay(obs_output_t *output);
  657. extern bool obs_output_delay_start(obs_output_t *output);
  658. extern void obs_output_delay_stop(obs_output_t *output);
  659. extern bool obs_output_actual_start(obs_output_t *output);
  660. extern void obs_output_actual_stop(obs_output_t *output, bool force);
  661. extern const struct obs_output_info *find_output(const char *id);
  662. extern void obs_output_remove_encoder(struct obs_output *output,
  663. struct obs_encoder *encoder);
  664. void obs_output_destroy(obs_output_t *output);
  665. /* ------------------------------------------------------------------------- */
  666. /* encoders */
  667. struct obs_weak_encoder {
  668. struct obs_weak_ref ref;
  669. struct obs_encoder *encoder;
  670. };
  671. struct encoder_callback {
  672. bool sent_first_packet;
  673. void (*new_packet)(void *param, struct encoder_packet *packet);
  674. void *param;
  675. };
  676. struct obs_encoder {
  677. struct obs_context_data context;
  678. struct obs_encoder_info info;
  679. struct obs_weak_encoder *control;
  680. pthread_mutex_t init_mutex;
  681. uint32_t samplerate;
  682. size_t planes;
  683. size_t blocksize;
  684. size_t framesize;
  685. size_t framesize_bytes;
  686. size_t mixer_idx;
  687. uint32_t scaled_width;
  688. uint32_t scaled_height;
  689. enum video_format preferred_format;
  690. volatile bool active;
  691. bool initialized;
  692. /* indicates ownership of the info.id buffer */
  693. bool owns_info_id;
  694. uint32_t timebase_num;
  695. uint32_t timebase_den;
  696. int64_t cur_pts;
  697. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  698. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  699. /* if a video encoder is paired with an audio encoder, make it start
  700. * up at the specific timestamp. if this is the audio encoder,
  701. * wait_for_video makes it wait until it's ready to sync up with
  702. * video */
  703. bool wait_for_video;
  704. bool first_received;
  705. struct obs_encoder *paired_encoder;
  706. int64_t offset_usec;
  707. uint64_t first_raw_ts;
  708. uint64_t start_ts;
  709. pthread_mutex_t outputs_mutex;
  710. DARRAY(obs_output_t*) outputs;
  711. bool destroy_on_stop;
  712. /* stores the video/audio media output pointer. video_t *or audio_t **/
  713. void *media;
  714. pthread_mutex_t callbacks_mutex;
  715. DARRAY(struct encoder_callback) callbacks;
  716. const char *profile_encoder_encode_name;
  717. };
  718. extern struct obs_encoder_info *find_encoder(const char *id);
  719. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  720. extern void obs_encoder_shutdown(obs_encoder_t *encoder);
  721. extern void obs_encoder_start(obs_encoder_t *encoder,
  722. void (*new_packet)(void *param, struct encoder_packet *packet),
  723. void *param);
  724. extern void obs_encoder_stop(obs_encoder_t *encoder,
  725. void (*new_packet)(void *param, struct encoder_packet *packet),
  726. void *param);
  727. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  728. struct obs_output *output);
  729. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  730. struct obs_output *output);
  731. void obs_encoder_destroy(obs_encoder_t *encoder);
  732. /* ------------------------------------------------------------------------- */
  733. /* services */
  734. struct obs_weak_service {
  735. struct obs_weak_ref ref;
  736. struct obs_service *service;
  737. };
  738. struct obs_service {
  739. struct obs_context_data context;
  740. struct obs_service_info info;
  741. struct obs_weak_service *control;
  742. /* indicates ownership of the info.id buffer */
  743. bool owns_info_id;
  744. bool active;
  745. bool destroy;
  746. struct obs_output *output;
  747. };
  748. extern const struct obs_service_info *find_service(const char *id);
  749. extern void obs_service_activate(struct obs_service *service);
  750. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  751. extern bool obs_service_initialize(struct obs_service *service,
  752. struct obs_output *output);
  753. void obs_service_destroy(obs_service_t *service);