obs-internal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. uint64_t video_time;
  170. video_t *video;
  171. pthread_t video_thread;
  172. bool thread_initialized;
  173. bool gpu_conversion;
  174. const char *conversion_tech;
  175. uint32_t conversion_height;
  176. uint32_t plane_offsets[3];
  177. uint32_t plane_sizes[3];
  178. uint32_t plane_linewidth[3];
  179. uint32_t output_width;
  180. uint32_t output_height;
  181. uint32_t base_width;
  182. uint32_t base_height;
  183. float color_matrix[16];
  184. enum obs_scale_type scale_type;
  185. struct obs_display main_display;
  186. };
  187. struct obs_core_audio {
  188. /* TODO: sound output subsystem */
  189. audio_t *audio;
  190. float user_volume;
  191. float present_volume;
  192. };
  193. /* user sources, output channels, and displays */
  194. struct obs_core_data {
  195. pthread_mutex_t user_sources_mutex;
  196. DARRAY(struct obs_source*) user_sources;
  197. struct obs_source *first_source;
  198. struct obs_display *first_display;
  199. struct obs_output *first_output;
  200. struct obs_encoder *first_encoder;
  201. struct obs_service *first_service;
  202. pthread_mutex_t sources_mutex;
  203. pthread_mutex_t displays_mutex;
  204. pthread_mutex_t outputs_mutex;
  205. pthread_mutex_t encoders_mutex;
  206. pthread_mutex_t services_mutex;
  207. struct obs_view main_view;
  208. volatile long active_transitions;
  209. long long unnamed_index;
  210. volatile bool valid;
  211. };
  212. /* user hotkeys */
  213. struct obs_core_hotkeys {
  214. pthread_mutex_t mutex;
  215. DARRAY(obs_hotkey_t) hotkeys;
  216. obs_hotkey_id next_id;
  217. DARRAY(obs_hotkey_pair_t) hotkey_pairs;
  218. obs_hotkey_pair_id next_pair_id;
  219. pthread_t hotkey_thread;
  220. bool hotkey_thread_initialized;
  221. os_event_t *stop_event;
  222. bool thread_disable_press : 1;
  223. bool strict_modifiers : 1;
  224. bool reroute_hotkeys : 1;
  225. DARRAY(obs_hotkey_binding_t) bindings;
  226. obs_hotkey_callback_router_func router_func;
  227. void *router_func_data;
  228. obs_hotkeys_platform_t *platform_context;
  229. pthread_once_t name_map_init_token;
  230. struct obs_hotkey_name_map *name_map;
  231. signal_handler_t *signals;
  232. char *translations[OBS_KEY_LAST_VALUE];
  233. char *mute;
  234. char *unmute;
  235. char *push_to_mute;
  236. char *push_to_talk;
  237. char *sceneitem_show;
  238. char *sceneitem_hide;
  239. };
  240. struct obs_core {
  241. struct obs_module *first_module;
  242. DARRAY(struct obs_module_path) module_paths;
  243. DARRAY(struct obs_source_info) input_types;
  244. DARRAY(struct obs_source_info) filter_types;
  245. DARRAY(struct obs_source_info) transition_types;
  246. DARRAY(struct obs_output_info) output_types;
  247. DARRAY(struct obs_encoder_info) encoder_types;
  248. DARRAY(struct obs_service_info) service_types;
  249. DARRAY(struct obs_modal_ui) modal_ui_callbacks;
  250. DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
  251. signal_handler_t *signals;
  252. proc_handler_t *procs;
  253. char *locale;
  254. /* segmented into multiple sub-structures to keep things a bit more
  255. * clean and organized */
  256. struct obs_core_video video;
  257. struct obs_core_audio audio;
  258. struct obs_core_data data;
  259. struct obs_core_hotkeys hotkeys;
  260. };
  261. extern struct obs_core *obs;
  262. extern void *obs_video_thread(void *param);
  263. /* ------------------------------------------------------------------------- */
  264. /* obs shared context data */
  265. struct obs_context_data {
  266. char *name;
  267. void *data;
  268. obs_data_t *settings;
  269. signal_handler_t *signals;
  270. proc_handler_t *procs;
  271. DARRAY(obs_hotkey_id) hotkeys;
  272. DARRAY(obs_hotkey_pair_id) hotkey_pairs;
  273. obs_data_t *hotkey_data;
  274. DARRAY(char*) rename_cache;
  275. pthread_mutex_t rename_cache_mutex;
  276. pthread_mutex_t *mutex;
  277. struct obs_context_data *next;
  278. struct obs_context_data **prev_next;
  279. };
  280. extern bool obs_context_data_init(
  281. struct obs_context_data *context,
  282. obs_data_t *settings,
  283. const char *name,
  284. obs_data_t *hotkey_data);
  285. extern void obs_context_data_free(struct obs_context_data *context);
  286. extern void obs_context_data_insert(struct obs_context_data *context,
  287. pthread_mutex_t *mutex, void *first);
  288. extern void obs_context_data_remove(struct obs_context_data *context);
  289. extern void obs_context_data_setname(struct obs_context_data *context,
  290. const char *name);
  291. /* ------------------------------------------------------------------------- */
  292. /* ref-counting */
  293. struct obs_weak_ref {
  294. volatile long refs;
  295. volatile long weak_refs;
  296. };
  297. static inline void obs_ref_addref(struct obs_weak_ref *ref)
  298. {
  299. os_atomic_inc_long(&ref->refs);
  300. }
  301. static inline bool obs_ref_release(struct obs_weak_ref *ref)
  302. {
  303. return os_atomic_dec_long(&ref->refs) == -1;
  304. }
  305. static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
  306. {
  307. os_atomic_inc_long(&ref->weak_refs);
  308. }
  309. static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
  310. {
  311. return os_atomic_dec_long(&ref->weak_refs) == -1;
  312. }
  313. static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
  314. {
  315. long owners = ref->refs;
  316. while (owners > -1) {
  317. if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
  318. return true;
  319. owners = ref->refs;
  320. }
  321. return false;
  322. }
  323. /* ------------------------------------------------------------------------- */
  324. /* sources */
  325. struct async_frame {
  326. struct obs_source_frame *frame;
  327. long unused_count;
  328. bool used;
  329. };
  330. struct obs_weak_source {
  331. struct obs_weak_ref ref;
  332. struct obs_source *source;
  333. };
  334. struct obs_source {
  335. struct obs_context_data context;
  336. struct obs_source_info info;
  337. struct obs_weak_source *control;
  338. /* general exposed flags that can be set for the source */
  339. uint32_t flags;
  340. uint32_t default_flags;
  341. /* indicates ownership of the info.id buffer */
  342. bool owns_info_id;
  343. /* signals to call the source update in the video thread */
  344. bool defer_update;
  345. /* ensures show/hide are only called once */
  346. volatile long show_refs;
  347. /* ensures activate/deactivate are only called once */
  348. volatile long activate_refs;
  349. /* used to indicate that the source has been removed and all
  350. * references to it should be released (not exactly how I would prefer
  351. * to handle things but it's the best option) */
  352. bool removed;
  353. bool active;
  354. bool showing;
  355. /* used to temporarily disable sources if needed */
  356. bool enabled;
  357. /* timing (if video is present, is based upon video) */
  358. volatile bool timing_set;
  359. volatile uint64_t timing_adjust;
  360. uint64_t next_audio_ts_min;
  361. uint64_t last_frame_ts;
  362. uint64_t last_sys_timestamp;
  363. bool async_rendered;
  364. /* audio */
  365. bool audio_failed;
  366. bool muted;
  367. struct resample_info sample_info;
  368. audio_resampler_t *resampler;
  369. audio_line_t *audio_line;
  370. pthread_mutex_t audio_mutex;
  371. struct obs_audio_data audio_data;
  372. size_t audio_storage_size;
  373. float base_volume;
  374. float user_volume;
  375. float present_volume;
  376. int64_t sync_offset;
  377. /* async video data */
  378. gs_texture_t *async_texture;
  379. gs_texrender_t *async_convert_texrender;
  380. struct obs_source_frame *cur_async_frame;
  381. bool async_gpu_conversion;
  382. enum video_format async_format;
  383. enum video_format async_cache_format;
  384. enum gs_color_format async_texture_format;
  385. float async_color_matrix[16];
  386. bool async_full_range;
  387. float async_color_range_min[3];
  388. float async_color_range_max[3];
  389. int async_plane_offset[2];
  390. bool async_flip;
  391. bool async_active;
  392. DARRAY(struct async_frame) async_cache;
  393. DARRAY(struct obs_source_frame*)async_frames;
  394. pthread_mutex_t async_mutex;
  395. uint32_t async_width;
  396. uint32_t async_height;
  397. uint32_t async_cache_width;
  398. uint32_t async_cache_height;
  399. uint32_t async_convert_width;
  400. uint32_t async_convert_height;
  401. /* filters */
  402. struct obs_source *filter_parent;
  403. struct obs_source *filter_target;
  404. DARRAY(struct obs_source*) filters;
  405. pthread_mutex_t filter_mutex;
  406. gs_texrender_t *filter_texrender;
  407. enum obs_allow_direct_render allow_direct;
  408. bool rendering_filter;
  409. /* sources specific hotkeys */
  410. obs_hotkey_pair_id mute_unmute_key;
  411. obs_hotkey_id push_to_mute_key;
  412. obs_hotkey_id push_to_talk_key;
  413. bool push_to_mute_enabled : 1;
  414. bool push_to_mute_pressed : 1;
  415. bool push_to_talk_enabled : 1;
  416. bool push_to_talk_pressed : 1;
  417. uint64_t push_to_mute_delay;
  418. uint64_t push_to_mute_stop_time;
  419. uint64_t push_to_talk_delay;
  420. uint64_t push_to_talk_stop_time;
  421. };
  422. extern const struct obs_source_info *find_source(struct darray *list,
  423. const char *id);
  424. extern bool obs_source_init_context(struct obs_source *source,
  425. obs_data_t *settings, const char *name,
  426. obs_data_t *hotkey_data);
  427. extern bool obs_source_init(struct obs_source *source,
  428. const struct obs_source_info *info);
  429. extern void obs_source_destroy(struct obs_source *source);
  430. enum view_type {
  431. MAIN_VIEW,
  432. AUX_VIEW
  433. };
  434. extern void obs_source_activate(obs_source_t *source, enum view_type type);
  435. extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
  436. extern void obs_source_video_tick(obs_source_t *source, float seconds);
  437. extern float obs_source_get_target_volume(obs_source_t *source,
  438. obs_source_t *target);
  439. /* ------------------------------------------------------------------------- */
  440. /* outputs */
  441. struct obs_weak_output {
  442. struct obs_weak_ref ref;
  443. struct obs_output *output;
  444. };
  445. struct obs_output {
  446. struct obs_context_data context;
  447. struct obs_output_info info;
  448. struct obs_weak_output *control;
  449. bool received_video;
  450. bool received_audio;
  451. int64_t video_offset;
  452. int64_t audio_offsets[MAX_AUDIO_MIXES];
  453. int64_t highest_audio_ts;
  454. int64_t highest_video_ts;
  455. pthread_mutex_t interleaved_mutex;
  456. DARRAY(struct encoder_packet) interleaved_packets;
  457. int reconnect_retry_sec;
  458. int reconnect_retry_max;
  459. int reconnect_retries;
  460. int reconnect_retry_cur_sec;
  461. bool reconnecting;
  462. pthread_t reconnect_thread;
  463. os_event_t *reconnect_stop_event;
  464. volatile bool reconnect_thread_active;
  465. uint32_t starting_frame_count;
  466. uint32_t starting_skipped_frame_count;
  467. int total_frames;
  468. bool active;
  469. volatile bool stopped;
  470. video_t *video;
  471. audio_t *audio;
  472. obs_encoder_t *video_encoder;
  473. obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
  474. obs_service_t *service;
  475. size_t mixer_idx;
  476. uint32_t scaled_width;
  477. uint32_t scaled_height;
  478. bool video_conversion_set;
  479. bool audio_conversion_set;
  480. struct video_scale_info video_conversion;
  481. struct audio_convert_info audio_conversion;
  482. bool valid;
  483. };
  484. extern const struct obs_output_info *find_output(const char *id);
  485. extern void obs_output_remove_encoder(struct obs_output *output,
  486. struct obs_encoder *encoder);
  487. void obs_output_destroy(obs_output_t *output);
  488. /* ------------------------------------------------------------------------- */
  489. /* encoders */
  490. struct obs_weak_encoder {
  491. struct obs_weak_ref ref;
  492. struct obs_encoder *encoder;
  493. };
  494. struct encoder_callback {
  495. bool sent_first_packet;
  496. void (*new_packet)(void *param, struct encoder_packet *packet);
  497. void *param;
  498. };
  499. struct obs_encoder {
  500. struct obs_context_data context;
  501. struct obs_encoder_info info;
  502. struct obs_weak_encoder *control;
  503. uint32_t samplerate;
  504. size_t planes;
  505. size_t blocksize;
  506. size_t framesize;
  507. size_t framesize_bytes;
  508. size_t mixer_idx;
  509. uint32_t scaled_width;
  510. uint32_t scaled_height;
  511. enum video_format preferred_format;
  512. bool active;
  513. uint32_t timebase_num;
  514. uint32_t timebase_den;
  515. int64_t cur_pts;
  516. struct circlebuf audio_input_buffer[MAX_AV_PLANES];
  517. uint8_t *audio_output_buffer[MAX_AV_PLANES];
  518. /* if a video encoder is paired with an audio encoder, make it start
  519. * up at the specific timestamp. if this is the audio encoder,
  520. * wait_for_video makes it wait until it's ready to sync up with
  521. * video */
  522. bool wait_for_video;
  523. struct obs_encoder *paired_encoder;
  524. uint64_t start_ts;
  525. pthread_mutex_t outputs_mutex;
  526. DARRAY(obs_output_t*) outputs;
  527. bool destroy_on_stop;
  528. /* stores the video/audio media output pointer. video_t *or audio_t **/
  529. void *media;
  530. pthread_mutex_t callbacks_mutex;
  531. DARRAY(struct encoder_callback) callbacks;
  532. };
  533. extern struct obs_encoder_info *find_encoder(const char *id);
  534. extern bool obs_encoder_initialize(obs_encoder_t *encoder);
  535. extern void obs_encoder_start(obs_encoder_t *encoder,
  536. void (*new_packet)(void *param, struct encoder_packet *packet),
  537. void *param);
  538. extern void obs_encoder_stop(obs_encoder_t *encoder,
  539. void (*new_packet)(void *param, struct encoder_packet *packet),
  540. void *param);
  541. extern void obs_encoder_add_output(struct obs_encoder *encoder,
  542. struct obs_output *output);
  543. extern void obs_encoder_remove_output(struct obs_encoder *encoder,
  544. struct obs_output *output);
  545. void obs_encoder_destroy(obs_encoder_t *encoder);
  546. /* ------------------------------------------------------------------------- */
  547. /* services */
  548. struct obs_weak_service {
  549. struct obs_weak_ref ref;
  550. struct obs_service *service;
  551. };
  552. struct obs_service {
  553. struct obs_context_data context;
  554. struct obs_service_info info;
  555. struct obs_weak_service *control;
  556. bool active;
  557. bool destroy;
  558. struct obs_output *output;
  559. };
  560. extern const struct obs_service_info *find_service(const char *id);
  561. extern void obs_service_activate(struct obs_service *service);
  562. extern void obs_service_deactivate(struct obs_service *service, bool remove);
  563. extern bool obs_service_initialize(struct obs_service *service,
  564. struct obs_output *output);
  565. void obs_service_destroy(obs_service_t *service);