obs-internal.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 "callback/signal.h"
  22. #include "callback/proc.h"
  23. #include "graphics/graphics.h"
  24. #include "media-io/audio-resampler.h"
  25. #include "media-io/video-io.h"
  26. #include "media-io/audio-io.h"
  27. #include "obs.h"
  28. #define NUM_TEXTURES 2
  29. #define MICROSECOND_DEN 1000000
  30. static inline int64_t packet_dts_usec(struct encoder_packet *packet)
  31. {
  32. return packet->dts * MICROSECOND_DEN / packet->timebase_den;
  33. }
  34. struct draw_callback {
  35. void (*draw)(void *param, uint32_t cx, uint32_t cy);
  36. void *param;
  37. };
  38. /* ------------------------------------------------------------------------- */
  39. /* modules */
  40. struct obs_module {
  41. const char *file;
  42. char *bin_path;
  43. char *data_path;
  44. void *module;
  45. bool loaded;
  46. bool (*load)(void);
  47. void (*unload)(void);
  48. void (*set_locale)(const char *locale);
  49. void (*free_locale)(void);
  50. uint32_t (*ver)(void);
  51. void (*set_pointer)(obs_module_t *module);
  52. const char *(*name)(void);
  53. const char *(*description)(void);
  54. const char *(*author)(void);
  55. struct obs_module *next;
  56. };
  57. extern void free_module(struct obs_module *mod);
  58. struct obs_module_path {
  59. char *bin;
  60. char *data;
  61. };
  62. static inline void free_module_path(struct obs_module_path *omp)
  63. {
  64. if (omp) {
  65. bfree(omp->bin);
  66. bfree(omp->data);
  67. }
  68. }
  69. static inline bool check_path(const char *data, const char *path,
  70. struct dstr *output)
  71. {
  72. dstr_copy(output, path);
  73. dstr_cat(output, data);
  74. return os_file_exists(output->array);
  75. }
  76. /* ------------------------------------------------------------------------- */
  77. /* hotkeys */
  78. struct obs_hotkey {
  79. obs_hotkey_id id;
  80. char *name;
  81. char *description;
  82. obs_hotkey_func func;
  83. void *data;
  84. int pressed;
  85. obs_hotkey_registerer_t registerer_type;
  86. void *registerer;
  87. obs_hotkey_id pair_partner_id;
  88. };
  89. struct obs_hotkey_pair {
  90. obs_hotkey_pair_id pair_id;
  91. obs_hotkey_id id[2];
  92. obs_hotkey_active_func func[2];
  93. bool pressed0 : 1;
  94. bool pressed1 : 1;
  95. void *data[2];
  96. };
  97. typedef struct obs_hotkey_pair obs_hotkey_pair_t;
  98. typedef struct obs_hotkeys_platform obs_hotkeys_platform_t;
  99. void *obs_hotkey_thread(void *param);
  100. struct obs_core_hotkeys;
  101. bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys);
  102. void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys);
  103. bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
  104. obs_key_t key);
  105. const char *obs_get_hotkey_translation(obs_key_t key, const char *def);
  106. struct obs_context_data;
  107. void obs_hotkeys_context_release(struct obs_context_data *context);
  108. void obs_hotkeys_free(void);
  109. struct obs_hotkey_binding {
  110. obs_key_combination_t key;
  111. bool pressed : 1;
  112. bool modifiers_match : 1;
  113. obs_hotkey_id hotkey_id;
  114. obs_hotkey_t *hotkey;
  115. };
  116. struct obs_hotkey_name_map;
  117. void obs_hotkey_name_map_free(void);
  118. /* ------------------------------------------------------------------------- */
  119. /* views */
  120. struct obs_view {
  121. pthread_mutex_t channels_mutex;
  122. obs_source_t *channels[MAX_CHANNELS];
  123. };
  124. extern bool obs_view_init(struct obs_view *view);
  125. extern void obs_view_free(struct obs_view *view);
  126. /* ------------------------------------------------------------------------- */
  127. /* displays */
  128. struct obs_display {
  129. bool size_changed;
  130. bool enabled;
  131. uint32_t cx, cy;
  132. uint32_t background_color;
  133. gs_swapchain_t *swap;
  134. pthread_mutex_t draw_callbacks_mutex;
  135. DARRAY(struct draw_callback) draw_callbacks;
  136. struct obs_display *next;
  137. struct obs_display **prev_next;
  138. };
  139. extern bool obs_display_init(struct obs_display *display,
  140. const struct gs_init_data *graphics_data);
  141. extern void obs_display_free(struct obs_display *display);
  142. /* ------------------------------------------------------------------------- */
  143. /* core */
  144. struct obs_vframe_info {
  145. uint64_t timestamp;
  146. int count;
  147. };
  148. struct obs_core_video {
  149. graphics_t *graphics;
  150. gs_stagesurf_t *copy_surfaces[NUM_TEXTURES];
  151. gs_texture_t *render_textures[NUM_TEXTURES];
  152. gs_texture_t *output_textures[NUM_TEXTURES];
  153. gs_texture_t *convert_textures[NUM_TEXTURES];
  154. bool textures_rendered[NUM_TEXTURES];
  155. bool textures_output[NUM_TEXTURES];
  156. bool textures_copied[NUM_TEXTURES];
  157. bool textures_converted[NUM_TEXTURES];
  158. struct circlebuf vframe_info_buffer;
  159. gs_effect_t *default_effect;
  160. gs_effect_t *default_rect_effect;
  161. gs_effect_t *opaque_effect;
  162. gs_effect_t *solid_effect;
  163. gs_effect_t *conversion_effect;
  164. gs_effect_t *bicubic_effect;
  165. gs_effect_t *lanczos_effect;
  166. gs_effect_t *bilinear_lowres_effect;
  167. gs_stagesurf_t *mapped_surface;
  168. int cur_texture;
  169. video_t *video;
  170. pthread_t video_thread;
  171. bool thread_initialized;
  172. bool gpu_conversion;
  173. const char *conversion_tech;
  174. uint32_t conversion_height;
  175. uint32_t plane_offsets[3];
  176. uint32_t plane_sizes[3];
  177. uint32_t plane_linewidth[3];
  178. uint32_t output_width;
  179. uint32_t output_height;
  180. uint32_t base_width;
  181. uint32_t base_height;
  182. float color_matrix[16];
  183. enum obs_scale_type scale_type;
  184. struct obs_display main_display;
  185. };
  186. struct obs_core_audio {
  187. /* TODO: sound output subsystem */
  188. audio_t *audio;
  189. float user_volume;
  190. float present_volume;
  191. };
  192. /* user sources, output channels, and displays */
  193. struct obs_core_data {
  194. pthread_mutex_t user_sources_mutex;
  195. DARRAY(struct obs_source*) user_sources;
  196. struct obs_source *first_source;
  197. struct obs_display *first_display;
  198. struct obs_output *first_output;
  199. struct obs_encoder *first_encoder;
  200. struct obs_service *first_service;
  201. pthread_mutex_t sources_mutex;
  202. pthread_mutex_t displays_mutex;
  203. pthread_mutex_t outputs_mutex;
  204. pthread_mutex_t encoders_mutex;
  205. pthread_mutex_t services_mutex;
  206. struct obs_view main_view;
  207. volatile long active_transitions;
  208. long long unnamed_index;
  209. volatile bool valid;
  210. };
  211. /* user hotkeys */
  212. struct obs_core_hotkeys {
  213. pthread_mutex_t mutex;
  214. DARRAY(obs_hotkey_t) hotkeys;
  215. obs_hotkey_id next_id;
  216. DARRAY(obs_hotkey_pair_t) hotkey_pairs;
  217. obs_hotkey_pair_id next_pair_id;
  218. pthread_t hotkey_thread;
  219. bool hotkey_thread_initialized;
  220. os_event_t *stop_event;
  221. bool thread_disable_press : 1;
  222. bool strict_modifiers : 1;
  223. bool reroute_hotkeys : 1;
  224. DARRAY(obs_hotkey_binding_t) bindings;
  225. obs_hotkey_callback_router_func router_func;
  226. void *router_func_data;
  227. obs_hotkeys_platform_t *platform_context;
  228. pthread_once_t name_map_init_token;
  229. struct obs_hotkey_name_map *name_map;
  230. signal_handler_t *signals;
  231. char *translations[OBS_KEY_LAST_VALUE];
  232. };
  233. struct obs_core {
  234. struct obs_module *first_module;
  235. DARRAY(struct obs_module_path) module_paths;
  236. DARRAY(struct obs_source_info) input_types;
  237. DARRAY(struct obs_source_info) filter_types;
  238. DARRAY(struct obs_source_info) transition_types;
  239. DARRAY(struct obs_output_info) output_types;
  240. DARRAY(struct obs_encoder_info) encoder_types;
  241. DARRAY(struct obs_service_info) service_types;
  242. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  243. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  244. signal_handler_t *signals;
  245. proc_handler_t *procs;
  246. char *locale;
  247. /* segmented into multiple sub-structures to keep things a bit more
  248. * clean and organized */
  249. struct obs_core_video video;
  250. struct obs_core_audio audio;
  251. struct obs_core_data data;
  252. struct obs_core_hotkeys hotkeys;
  253. };
  254. extern struct obs_core *obs;
  255. extern void *obs_video_thread(void *param);
  256. /* ------------------------------------------------------------------------- */
  257. /* obs shared context data */
  258. struct obs_context_data {
  259. char *name;
  260. void *data;
  261. obs_data_t *settings;
  262. signal_handler_t *signals;
  263. proc_handler_t *procs;
  264. DARRAY(obs_hotkey_id) hotkeys;
  265. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  266. obs_data_t *hotkey_data;
  267. DARRAY(char*) rename_cache;
  268. pthread_mutex_t rename_cache_mutex;
  269. pthread_mutex_t *mutex;
  270. struct obs_context_data *next;
  271. struct obs_context_data **prev_next;
  272. };
  273. extern bool obs_context_data_init(
  274. struct obs_context_data *context,
  275. obs_data_t *settings,
  276. const char *name,
  277. obs_data_t *hotkey_data);
  278. extern void obs_context_data_free(struct obs_context_data *context);
  279. extern void obs_context_data_insert(struct obs_context_data *context,
  280. pthread_mutex_t *mutex, void *first);
  281. extern void obs_context_data_remove(struct obs_context_data *context);
  282. extern void obs_context_data_setname(struct obs_context_data *context,
  283. const char *name);
  284. /* ------------------------------------------------------------------------- */
  285. /* ref-counting */
  286. struct obs_weak_ref {
  287. volatile long refs;
  288. volatile long weak_refs;
  289. };
  290. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  291. {
  292. os_atomic_inc_long(&ref->refs);
  293. }
  294. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  295. {
  296. return os_atomic_dec_long(&ref->refs) == -1;
  297. }
  298. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  299. {
  300. os_atomic_inc_long(&ref->weak_refs);
  301. }
  302. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  303. {
  304. return os_atomic_dec_long(&ref->weak_refs) == -1;
  305. }
  306. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  307. {
  308. long owners = ref->refs;
  309. while (owners > -1) {
  310. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  311. return true;
  312. owners = ref->refs;
  313. }
  314. return false;
  315. }
  316. /* ------------------------------------------------------------------------- */
  317. /* sources */
  318. struct async_frame {
  319. struct obs_source_frame *frame;
  320. long unused_count;
  321. bool used;
  322. };
  323. struct obs_weak_source {
  324. struct obs_weak_ref ref;
  325. struct obs_source *source;
  326. };
  327. struct obs_source {
  328. struct obs_context_data context;
  329. struct obs_source_info info;
  330. struct obs_weak_source *control;
  331. /* general exposed flags that can be set for the source */
  332. uint32_t flags;
  333. uint32_t default_flags;
  334. /* indicates ownership of the info.id buffer */
  335. bool owns_info_id;
  336. /* signals to call the source update in the video thread */
  337. bool defer_update;
  338. /* ensures show/hide are only called once */
  339. volatile long show_refs;
  340. /* ensures activate/deactivate are only called once */
  341. volatile long activate_refs;
  342. /* used to indicate that the source has been removed and all
  343. * references to it should be released (not exactly how I would prefer
  344. * to handle things but it's the best option) */
  345. bool removed;
  346. bool active;
  347. bool showing;
  348. /* used to temporarily disable sources if needed */
  349. bool enabled;
  350. /* timing (if video is present, is based upon video) */
  351. volatile bool timing_set;
  352. volatile uint64_t timing_adjust;
  353. uint64_t next_audio_ts_min;
  354. uint64_t last_frame_ts;
  355. uint64_t last_sys_timestamp;
  356. bool async_rendered;
  357. /* audio */
  358. bool audio_failed;
  359. bool muted;
  360. struct resample_info sample_info;
  361. audio_resampler_t *resampler;
  362. audio_line_t *audio_line;
  363. pthread_mutex_t audio_mutex;
  364. struct obs_audio_data audio_data;
  365. size_t audio_storage_size;
  366. float base_volume;
  367. float user_volume;
  368. float present_volume;
  369. int64_t sync_offset;
  370. /* async video data */
  371. gs_texture_t *async_texture;
  372. gs_texrender_t *async_convert_texrender;
  373. struct obs_source_frame *cur_async_frame;
  374. bool async_gpu_conversion;
  375. enum video_format async_format;
  376. enum video_format async_cache_format;
  377. enum gs_color_format async_texture_format;
  378. float async_color_matrix[16];
  379. bool async_full_range;
  380. float async_color_range_min[3];
  381. float async_color_range_max[3];
  382. int async_plane_offset[2];
  383. bool async_flip;
  384. bool async_active;
  385. DARRAY(struct async_frame) async_cache;
  386. DARRAY(struct obs_source_frame*)async_frames;
  387. pthread_mutex_t async_mutex;
  388. uint32_t async_width;
  389. uint32_t async_height;
  390. uint32_t async_cache_width;
  391. uint32_t async_cache_height;
  392. uint32_t async_convert_width;
  393. uint32_t async_convert_height;
  394. /* filters */
  395. struct obs_source *filter_parent;
  396. struct obs_source *filter_target;
  397. DARRAY(struct obs_source*) filters;
  398. pthread_mutex_t filter_mutex;
  399. gs_texrender_t *filter_texrender;
  400. enum obs_allow_direct_render allow_direct;
  401. bool rendering_filter;
  402. };
  403. extern const struct obs_source_info *find_source(struct darray *list,
  404. const char *id);
  405. extern bool obs_source_init_context(struct obs_source *source,
  406. obs_data_t *settings, const char *name,
  407. obs_data_t *hotkey_data);
  408. extern bool obs_source_init(struct obs_source *source,
  409. const struct obs_source_info *info);
  410. extern void obs_source_destroy(struct obs_source *source);
  411. enum view_type {
  412. MAIN_VIEW,
  413. AUX_VIEW
  414. };
  415. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  416. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  417. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  418. extern float obs_source_get_target_volume(obs_source_t *source,
  419. obs_source_t *target);
  420. /* ------------------------------------------------------------------------- */
  421. /* outputs */
  422. struct obs_weak_output {
  423. struct obs_weak_ref ref;
  424. struct obs_output *output;
  425. };
  426. struct obs_output {
  427. struct obs_context_data context;
  428. struct obs_output_info info;
  429. struct obs_weak_output *control;
  430. bool received_video;
  431. bool received_audio;
  432. int64_t video_offset;
  433. int64_t audio_offsets[MAX_AUDIO_MIXES];
  434. int64_t highest_audio_ts;
  435. int64_t highest_video_ts;
  436. pthread_mutex_t interleaved_mutex;
  437. DARRAY(struct encoder_packet) interleaved_packets;
  438. int reconnect_retry_sec;
  439. int reconnect_retry_max;
  440. int reconnect_retries;
  441. int reconnect_retry_cur_sec;
  442. bool reconnecting;
  443. pthread_t reconnect_thread;
  444. os_event_t *reconnect_stop_event;
  445. volatile bool reconnect_thread_active;
  446. uint32_t starting_frame_count;
  447. uint32_t starting_skipped_frame_count;
  448. int total_frames;
  449. bool active;
  450. volatile bool stopped;
  451. video_t *video;
  452. audio_t *audio;
  453. obs_encoder_t *video_encoder;
  454. obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
  455. obs_service_t *service;
  456. size_t mixer_idx;
  457. uint32_t scaled_width;
  458. uint32_t scaled_height;
  459. bool video_conversion_set;
  460. bool audio_conversion_set;
  461. struct video_scale_info video_conversion;
  462. struct audio_convert_info audio_conversion;
  463. bool valid;
  464. };
  465. extern const struct obs_output_info *find_output(const char *id);
  466. extern void obs_output_remove_encoder(struct obs_output *output,
  467. struct obs_encoder *encoder);
  468. void obs_output_destroy(obs_output_t *output);
  469. /* ------------------------------------------------------------------------- */
  470. /* encoders */
  471. struct obs_weak_encoder {
  472. struct obs_weak_ref ref;
  473. struct obs_encoder *encoder;
  474. };
  475. struct encoder_callback {
  476. bool sent_first_packet;
  477. void (*new_packet)(void *param, struct encoder_packet *packet);
  478. void *param;
  479. };
  480. struct obs_encoder {
  481. struct obs_context_data context;
  482. struct obs_encoder_info info;
  483. struct obs_weak_encoder *control;
  484. uint32_t samplerate;
  485. size_t planes;
  486. size_t blocksize;
  487. size_t framesize;
  488. size_t framesize_bytes;
  489. size_t mixer_idx;
  490. uint32_t scaled_width;
  491. uint32_t scaled_height;
  492. enum video_format preferred_format;
  493. bool active;
  494. uint32_t timebase_num;
  495. uint32_t timebase_den;
  496. int64_t cur_pts;
  497. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  498. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  499. /* if a video encoder is paired with an audio encoder, make it start
  500. * up at the specific timestamp. if this is the audio encoder,
  501. * wait_for_video makes it wait until it's ready to sync up with
  502. * video */
  503. bool wait_for_video;
  504. struct obs_encoder *paired_encoder;
  505. uint64_t start_ts;
  506. pthread_mutex_t outputs_mutex;
  507. DARRAY(obs_output_t*) outputs;
  508. bool destroy_on_stop;
  509. /* stores the video/audio media output pointer. video_t *or audio_t **/
  510. void *media;
  511. pthread_mutex_t callbacks_mutex;
  512. DARRAY(struct encoder_callback) callbacks;
  513. };
  514. extern struct obs_encoder_info *find_encoder(const char *id);
  515. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  516. extern void obs_encoder_start(obs_encoder_t *encoder,
  517. void (*new_packet)(void *param, struct encoder_packet *packet),
  518. void *param);
  519. extern void obs_encoder_stop(obs_encoder_t *encoder,
  520. void (*new_packet)(void *param, struct encoder_packet *packet),
  521. void *param);
  522. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  523. struct obs_output *output);
  524. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  525. struct obs_output *output);
  526. void obs_encoder_destroy(obs_encoder_t *encoder);
  527. /* ------------------------------------------------------------------------- */
  528. /* services */
  529. struct obs_weak_service {
  530. struct obs_weak_ref ref;
  531. struct obs_service *service;
  532. };
  533. struct obs_service {
  534. struct obs_context_data context;
  535. struct obs_service_info info;
  536. struct obs_weak_service *control;
  537. bool active;
  538. bool destroy;
  539. struct obs_output *output;
  540. };
  541. extern const struct obs_service_info *find_service(const char *id);
  542. extern void obs_service_activate(struct obs_service *service);
  543. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  544. extern bool obs_service_initialize(struct obs_service *service,
  545. struct obs_output *output);
  546. void obs_service_destroy(obs_service_t *service);