obs-internal.h 23 KB

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