1 /******************************************************************************
2     Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 #include "util/c99defs.h"
21 #include "util/darray.h"
22 #include "util/circlebuf.h"
23 #include "util/dstr.h"
24 #include "util/threading.h"
25 #include "util/platform.h"
26 #include "util/profiler.h"
27 #include "callback/signal.h"
28 #include "callback/proc.h"
29 
30 #include "graphics/graphics.h"
31 #include "graphics/matrix4.h"
32 
33 #include "media-io/audio-resampler.h"
34 #include "media-io/video-io.h"
35 #include "media-io/audio-io.h"
36 
37 #include "obs.h"
38 
39 #include <caption/caption.h>
40 
41 #define NUM_TEXTURES 2
42 #define NUM_CHANNELS 3
43 #define MICROSECOND_DEN 1000000
44 #define NUM_ENCODE_TEXTURES 3
45 #define NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT 1
46 
packet_dts_usec(struct encoder_packet * packet)47 static inline int64_t packet_dts_usec(struct encoder_packet *packet)
48 {
49 	return packet->dts * MICROSECOND_DEN / packet->timebase_den;
50 }
51 
52 struct tick_callback {
53 	void (*tick)(void *param, float seconds);
54 	void *param;
55 };
56 
57 struct draw_callback {
58 	void (*draw)(void *param, uint32_t cx, uint32_t cy);
59 	void *param;
60 };
61 
62 /* ------------------------------------------------------------------------- */
63 /* validity checks */
64 
obs_object_valid(const void * obj,const char * f,const char * t)65 static inline bool obs_object_valid(const void *obj, const char *f,
66 				    const char *t)
67 {
68 	if (!obj) {
69 		blog(LOG_DEBUG, "%s: Null '%s' parameter", f, t);
70 		return false;
71 	}
72 
73 	return true;
74 }
75 
76 #define obs_ptr_valid(ptr, func) obs_object_valid(ptr, func, #ptr)
77 #define obs_source_valid obs_ptr_valid
78 #define obs_output_valid obs_ptr_valid
79 #define obs_encoder_valid obs_ptr_valid
80 #define obs_service_valid obs_ptr_valid
81 
82 /* ------------------------------------------------------------------------- */
83 /* modules */
84 
85 struct obs_module {
86 	char *mod_name;
87 	const char *file;
88 	char *bin_path;
89 	char *data_path;
90 	void *module;
91 	bool loaded;
92 
93 	bool (*load)(void);
94 	void (*unload)(void);
95 	void (*post_load)(void);
96 	void (*set_locale)(const char *locale);
97 	bool (*get_string)(const char *lookup_string,
98 			   const char **translated_string);
99 	void (*free_locale)(void);
100 	uint32_t (*ver)(void);
101 	void (*set_pointer)(obs_module_t *module);
102 	const char *(*name)(void);
103 	const char *(*description)(void);
104 	const char *(*author)(void);
105 
106 	struct obs_module *next;
107 };
108 
109 extern void free_module(struct obs_module *mod);
110 
111 struct obs_module_path {
112 	char *bin;
113 	char *data;
114 };
115 
free_module_path(struct obs_module_path * omp)116 static inline void free_module_path(struct obs_module_path *omp)
117 {
118 	if (omp) {
119 		bfree(omp->bin);
120 		bfree(omp->data);
121 	}
122 }
123 
check_path(const char * data,const char * path,struct dstr * output)124 static inline bool check_path(const char *data, const char *path,
125 			      struct dstr *output)
126 {
127 	dstr_copy(output, path);
128 	dstr_cat(output, data);
129 
130 	return os_file_exists(output->array);
131 }
132 
133 /* ------------------------------------------------------------------------- */
134 /* hotkeys */
135 
136 struct obs_hotkey {
137 	obs_hotkey_id id;
138 	char *name;
139 	char *description;
140 
141 	obs_hotkey_func func;
142 	void *data;
143 	int pressed;
144 
145 	obs_hotkey_registerer_t registerer_type;
146 	void *registerer;
147 
148 	obs_hotkey_id pair_partner_id;
149 };
150 
151 struct obs_hotkey_pair {
152 	obs_hotkey_pair_id pair_id;
153 	obs_hotkey_id id[2];
154 	obs_hotkey_active_func func[2];
155 	bool pressed0;
156 	bool pressed1;
157 	void *data[2];
158 };
159 
160 typedef struct obs_hotkey_pair obs_hotkey_pair_t;
161 
162 typedef struct obs_hotkeys_platform obs_hotkeys_platform_t;
163 
164 void *obs_hotkey_thread(void *param);
165 
166 struct obs_core_hotkeys;
167 bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys);
168 void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys);
169 bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
170 				     obs_key_t key);
171 
172 const char *obs_get_hotkey_translation(obs_key_t key, const char *def);
173 
174 struct obs_context_data;
175 void obs_hotkeys_context_release(struct obs_context_data *context);
176 
177 void obs_hotkeys_free(void);
178 
179 struct obs_hotkey_binding {
180 	obs_key_combination_t key;
181 	bool pressed;
182 	bool modifiers_match;
183 
184 	obs_hotkey_id hotkey_id;
185 	obs_hotkey_t *hotkey;
186 };
187 
188 struct obs_hotkey_name_map;
189 void obs_hotkey_name_map_free(void);
190 
191 /* ------------------------------------------------------------------------- */
192 /* views */
193 
194 struct obs_view {
195 	pthread_mutex_t channels_mutex;
196 	obs_source_t *channels[MAX_CHANNELS];
197 };
198 
199 extern bool obs_view_init(struct obs_view *view);
200 extern void obs_view_free(struct obs_view *view);
201 
202 /* ------------------------------------------------------------------------- */
203 /* displays */
204 
205 struct obs_display {
206 	bool size_changed;
207 	bool enabled;
208 	uint32_t cx, cy;
209 	uint32_t background_color;
210 	gs_swapchain_t *swap;
211 	pthread_mutex_t draw_callbacks_mutex;
212 	pthread_mutex_t draw_info_mutex;
213 	DARRAY(struct draw_callback) draw_callbacks;
214 
215 	struct obs_display *next;
216 	struct obs_display **prev_next;
217 };
218 
219 extern bool obs_display_init(struct obs_display *display,
220 			     const struct gs_init_data *graphics_data);
221 extern void obs_display_free(struct obs_display *display);
222 
223 /* ------------------------------------------------------------------------- */
224 /* core */
225 
226 struct obs_vframe_info {
227 	uint64_t timestamp;
228 	int count;
229 };
230 
231 struct obs_tex_frame {
232 	gs_texture_t *tex;
233 	gs_texture_t *tex_uv;
234 	uint32_t handle;
235 	uint64_t timestamp;
236 	uint64_t lock_key;
237 	int count;
238 	bool released;
239 };
240 
241 struct obs_task_info {
242 	obs_task_t task;
243 	void *param;
244 };
245 
246 struct obs_core_video {
247 	graphics_t *graphics;
248 	gs_stagesurf_t *copy_surfaces[NUM_TEXTURES][NUM_CHANNELS];
249 	gs_texture_t *render_texture;
250 	gs_texture_t *output_texture;
251 	gs_texture_t *convert_textures[NUM_CHANNELS];
252 	bool texture_rendered;
253 	bool textures_copied[NUM_TEXTURES];
254 	bool texture_converted;
255 	bool using_nv12_tex;
256 	struct circlebuf vframe_info_buffer;
257 	struct circlebuf vframe_info_buffer_gpu;
258 	gs_effect_t *default_effect;
259 	gs_effect_t *default_rect_effect;
260 	gs_effect_t *opaque_effect;
261 	gs_effect_t *solid_effect;
262 	gs_effect_t *repeat_effect;
263 	gs_effect_t *conversion_effect;
264 	gs_effect_t *bicubic_effect;
265 	gs_effect_t *lanczos_effect;
266 	gs_effect_t *area_effect;
267 	gs_effect_t *bilinear_lowres_effect;
268 	gs_effect_t *premultiplied_alpha_effect;
269 	gs_samplerstate_t *point_sampler;
270 	gs_stagesurf_t *mapped_surfaces[NUM_CHANNELS];
271 	int cur_texture;
272 	long raw_active;
273 	long gpu_encoder_active;
274 	pthread_mutex_t gpu_encoder_mutex;
275 	struct circlebuf gpu_encoder_queue;
276 	struct circlebuf gpu_encoder_avail_queue;
277 	DARRAY(obs_encoder_t *) gpu_encoders;
278 	os_sem_t *gpu_encode_semaphore;
279 	os_event_t *gpu_encode_inactive;
280 	pthread_t gpu_encode_thread;
281 	bool gpu_encode_thread_initialized;
282 	volatile bool gpu_encode_stop;
283 
284 	uint64_t video_time;
285 	uint64_t video_frame_interval_ns;
286 	uint64_t video_avg_frame_time_ns;
287 	double video_fps;
288 	video_t *video;
289 	pthread_t video_thread;
290 	uint32_t total_frames;
291 	uint32_t lagged_frames;
292 	bool thread_initialized;
293 
294 	bool gpu_conversion;
295 	const char *conversion_techs[NUM_CHANNELS];
296 	bool conversion_needed;
297 	float conversion_width_i;
298 
299 	uint32_t output_width;
300 	uint32_t output_height;
301 	uint32_t base_width;
302 	uint32_t base_height;
303 	float color_matrix[16];
304 	enum obs_scale_type scale_type;
305 
306 	gs_texture_t *transparent_texture;
307 
308 	gs_effect_t *deinterlace_discard_effect;
309 	gs_effect_t *deinterlace_discard_2x_effect;
310 	gs_effect_t *deinterlace_linear_effect;
311 	gs_effect_t *deinterlace_linear_2x_effect;
312 	gs_effect_t *deinterlace_blend_effect;
313 	gs_effect_t *deinterlace_blend_2x_effect;
314 	gs_effect_t *deinterlace_yadif_effect;
315 	gs_effect_t *deinterlace_yadif_2x_effect;
316 
317 	struct obs_video_info ovi;
318 
319 	pthread_mutex_t task_mutex;
320 	struct circlebuf tasks;
321 };
322 
323 struct audio_monitor;
324 
325 struct obs_core_audio {
326 	audio_t *audio;
327 
328 	DARRAY(struct obs_source *) render_order;
329 	DARRAY(struct obs_source *) root_nodes;
330 
331 	uint64_t buffered_ts;
332 	struct circlebuf buffered_timestamps;
333 	uint64_t buffering_wait_ticks;
334 	int total_buffering_ticks;
335 
336 	float user_volume;
337 
338 	pthread_mutex_t monitoring_mutex;
339 	DARRAY(struct audio_monitor *) monitors;
340 	char *monitoring_device_name;
341 	char *monitoring_device_id;
342 };
343 
344 /* user sources, output channels, and displays */
345 struct obs_core_data {
346 	struct obs_source *first_source;
347 	struct obs_source *first_audio_source;
348 	struct obs_display *first_display;
349 	struct obs_output *first_output;
350 	struct obs_encoder *first_encoder;
351 	struct obs_service *first_service;
352 
353 	pthread_mutex_t sources_mutex;
354 	pthread_mutex_t displays_mutex;
355 	pthread_mutex_t outputs_mutex;
356 	pthread_mutex_t encoders_mutex;
357 	pthread_mutex_t services_mutex;
358 	pthread_mutex_t audio_sources_mutex;
359 	pthread_mutex_t draw_callbacks_mutex;
360 	DARRAY(struct draw_callback) draw_callbacks;
361 	DARRAY(struct tick_callback) tick_callbacks;
362 
363 	struct obs_view main_view;
364 
365 	long long unnamed_index;
366 
367 	obs_data_t *private_data;
368 
369 	volatile bool valid;
370 };
371 
372 /* user hotkeys */
373 struct obs_core_hotkeys {
374 	pthread_mutex_t mutex;
375 	DARRAY(obs_hotkey_t) hotkeys;
376 	obs_hotkey_id next_id;
377 	DARRAY(obs_hotkey_pair_t) hotkey_pairs;
378 	obs_hotkey_pair_id next_pair_id;
379 
380 	pthread_t hotkey_thread;
381 	bool hotkey_thread_initialized;
382 	os_event_t *stop_event;
383 	bool thread_disable_press;
384 	bool strict_modifiers;
385 	bool reroute_hotkeys;
386 	DARRAY(obs_hotkey_binding_t) bindings;
387 
388 	obs_hotkey_callback_router_func router_func;
389 	void *router_func_data;
390 
391 	obs_hotkeys_platform_t *platform_context;
392 
393 	pthread_once_t name_map_init_token;
394 	struct obs_hotkey_name_map *name_map;
395 
396 	signal_handler_t *signals;
397 
398 	char *translations[OBS_KEY_LAST_VALUE];
399 	char *mute;
400 	char *unmute;
401 	char *push_to_mute;
402 	char *push_to_talk;
403 	char *sceneitem_show;
404 	char *sceneitem_hide;
405 };
406 
407 struct obs_core {
408 	struct obs_module *first_module;
409 	DARRAY(struct obs_module_path) module_paths;
410 
411 	DARRAY(struct obs_source_info) source_types;
412 	DARRAY(struct obs_source_info) input_types;
413 	DARRAY(struct obs_source_info) filter_types;
414 	DARRAY(struct obs_source_info) transition_types;
415 	DARRAY(struct obs_output_info) output_types;
416 	DARRAY(struct obs_encoder_info) encoder_types;
417 	DARRAY(struct obs_service_info) service_types;
418 	DARRAY(struct obs_modal_ui) modal_ui_callbacks;
419 	DARRAY(struct obs_modeless_ui) modeless_ui_callbacks;
420 
421 	signal_handler_t *signals;
422 	proc_handler_t *procs;
423 
424 	char *locale;
425 	char *module_config_path;
426 	bool name_store_owned;
427 	profiler_name_store_t *name_store;
428 
429 	/* segmented into multiple sub-structures to keep things a bit more
430 	 * clean and organized */
431 	struct obs_core_video video;
432 	struct obs_core_audio audio;
433 	struct obs_core_data data;
434 	struct obs_core_hotkeys hotkeys;
435 
436 	obs_task_handler_t ui_task_handler;
437 };
438 
439 extern struct obs_core *obs;
440 
441 struct obs_graphics_context {
442 	uint64_t last_time;
443 	uint64_t interval;
444 	uint64_t frame_time_total_ns;
445 	uint64_t fps_total_ns;
446 	uint32_t fps_total_frames;
447 #ifdef _WIN32
448 	bool gpu_was_active;
449 #endif
450 	bool raw_was_active;
451 	bool was_active;
452 	const char *video_thread_name;
453 };
454 
455 extern void *obs_graphics_thread(void *param);
456 extern bool obs_graphics_thread_loop(struct obs_graphics_context *context);
457 #ifdef __APPLE__
458 extern void *obs_graphics_thread_autorelease(void *param);
459 extern bool
460 obs_graphics_thread_loop_autorelease(struct obs_graphics_context *context);
461 #endif
462 
463 extern gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file);
464 
465 extern bool audio_callback(void *param, uint64_t start_ts_in,
466 			   uint64_t end_ts_in, uint64_t *out_ts,
467 			   uint32_t mixers, struct audio_output_data *mixes);
468 
469 extern void
470 start_raw_video(video_t *video, const struct video_scale_info *conversion,
471 		void (*callback)(void *param, struct video_data *frame),
472 		void *param);
473 extern void stop_raw_video(video_t *video,
474 			   void (*callback)(void *param,
475 					    struct video_data *frame),
476 			   void *param);
477 
478 /* ------------------------------------------------------------------------- */
479 /* obs shared context data */
480 
481 struct obs_context_data {
482 	char *name;
483 	void *data;
484 	obs_data_t *settings;
485 	signal_handler_t *signals;
486 	proc_handler_t *procs;
487 	enum obs_obj_type type;
488 
489 	DARRAY(obs_hotkey_id) hotkeys;
490 	DARRAY(obs_hotkey_pair_id) hotkey_pairs;
491 	obs_data_t *hotkey_data;
492 
493 	DARRAY(char *) rename_cache;
494 	pthread_mutex_t rename_cache_mutex;
495 
496 	pthread_mutex_t *mutex;
497 	struct obs_context_data *next;
498 	struct obs_context_data **prev_next;
499 
500 	bool private;
501 };
502 
503 extern bool obs_context_data_init(struct obs_context_data *context,
504 				  enum obs_obj_type type, obs_data_t *settings,
505 				  const char *name, obs_data_t *hotkey_data,
506 				  bool private);
507 extern void obs_context_data_free(struct obs_context_data *context);
508 
509 extern void obs_context_data_insert(struct obs_context_data *context,
510 				    pthread_mutex_t *mutex, void *first);
511 extern void obs_context_data_remove(struct obs_context_data *context);
512 
513 extern void obs_context_data_setname(struct obs_context_data *context,
514 				     const char *name);
515 
516 /* ------------------------------------------------------------------------- */
517 /* ref-counting  */
518 
519 struct obs_weak_ref {
520 	volatile long refs;
521 	volatile long weak_refs;
522 };
523 
obs_ref_addref(struct obs_weak_ref * ref)524 static inline void obs_ref_addref(struct obs_weak_ref *ref)
525 {
526 	os_atomic_inc_long(&ref->refs);
527 }
528 
obs_ref_release(struct obs_weak_ref * ref)529 static inline bool obs_ref_release(struct obs_weak_ref *ref)
530 {
531 	return os_atomic_dec_long(&ref->refs) == -1;
532 }
533 
obs_weak_ref_addref(struct obs_weak_ref * ref)534 static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
535 {
536 	os_atomic_inc_long(&ref->weak_refs);
537 }
538 
obs_weak_ref_release(struct obs_weak_ref * ref)539 static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
540 {
541 	return os_atomic_dec_long(&ref->weak_refs) == -1;
542 }
543 
obs_weak_ref_get_ref(struct obs_weak_ref * ref)544 static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
545 {
546 	long owners = os_atomic_load_long(&ref->refs);
547 	while (owners > -1) {
548 		if (os_atomic_compare_exchange_long(&ref->refs, &owners,
549 						    owners + 1)) {
550 			return true;
551 		}
552 	}
553 
554 	return false;
555 }
556 
557 /* ------------------------------------------------------------------------- */
558 /* sources  */
559 
560 struct async_frame {
561 	struct obs_source_frame *frame;
562 	long unused_count;
563 	bool used;
564 };
565 
566 enum audio_action_type {
567 	AUDIO_ACTION_VOL,
568 	AUDIO_ACTION_MUTE,
569 	AUDIO_ACTION_PTT,
570 	AUDIO_ACTION_PTM,
571 };
572 
573 struct audio_action {
574 	uint64_t timestamp;
575 	enum audio_action_type type;
576 	union {
577 		float vol;
578 		bool set;
579 	};
580 };
581 
582 struct obs_weak_source {
583 	struct obs_weak_ref ref;
584 	struct obs_source *source;
585 };
586 
587 struct audio_cb_info {
588 	obs_source_audio_capture_t callback;
589 	void *param;
590 };
591 
592 struct caption_cb_info {
593 	obs_source_caption_t callback;
594 	void *param;
595 };
596 
597 struct obs_source {
598 	struct obs_context_data context;
599 	struct obs_source_info info;
600 	struct obs_weak_source *control;
601 
602 	/* general exposed flags that can be set for the source */
603 	uint32_t flags;
604 	uint32_t default_flags;
605 	uint32_t last_obs_ver;
606 
607 	/* indicates ownership of the info.id buffer */
608 	bool owns_info_id;
609 
610 	/* signals to call the source update in the video thread */
611 	long defer_update_count;
612 
613 	/* ensures show/hide are only called once */
614 	volatile long show_refs;
615 
616 	/* ensures activate/deactivate are only called once */
617 	volatile long activate_refs;
618 
619 	/* used to indicate that the source has been removed and all
620 	 * references to it should be released (not exactly how I would prefer
621 	 * to handle things but it's the best option) */
622 	bool removed;
623 
624 	/*  used to indicate if the source should show up when queried for user ui */
625 	bool temp_removed;
626 
627 	bool active;
628 	bool showing;
629 
630 	/* used to temporarily disable sources if needed */
631 	bool enabled;
632 
633 	/* hint to allow sources to render more quickly */
634 	bool texcoords_centered;
635 
636 	/* timing (if video is present, is based upon video) */
637 	volatile bool timing_set;
638 	volatile uint64_t timing_adjust;
639 	uint64_t resample_offset;
640 	uint64_t last_audio_ts;
641 	uint64_t next_audio_ts_min;
642 	uint64_t next_audio_sys_ts_min;
643 	uint64_t last_frame_ts;
644 	uint64_t last_sys_timestamp;
645 	bool async_rendered;
646 
647 	/* audio */
648 	bool audio_failed;
649 	bool audio_pending;
650 	bool pending_stop;
651 	bool audio_active;
652 	bool user_muted;
653 	bool muted;
654 	struct obs_source *next_audio_source;
655 	struct obs_source **prev_next_audio_source;
656 	uint64_t audio_ts;
657 	struct circlebuf audio_input_buf[MAX_AUDIO_CHANNELS];
658 	size_t last_audio_input_buf_size;
659 	DARRAY(struct audio_action) audio_actions;
660 	float *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
661 	float *audio_mix_buf[MAX_AUDIO_CHANNELS];
662 	struct resample_info sample_info;
663 	audio_resampler_t *resampler;
664 	pthread_mutex_t audio_actions_mutex;
665 	pthread_mutex_t audio_buf_mutex;
666 	pthread_mutex_t audio_mutex;
667 	pthread_mutex_t audio_cb_mutex;
668 	DARRAY(struct audio_cb_info) audio_cb_list;
669 	struct obs_audio_data audio_data;
670 	size_t audio_storage_size;
671 	uint32_t audio_mixers;
672 	float user_volume;
673 	float volume;
674 	int64_t sync_offset;
675 	int64_t last_sync_offset;
676 	float balance;
677 
678 	/* async video data */
679 	gs_texture_t *async_textures[MAX_AV_PLANES];
680 	gs_texrender_t *async_texrender;
681 	struct obs_source_frame *cur_async_frame;
682 	bool async_gpu_conversion;
683 	enum video_format async_format;
684 	bool async_full_range;
685 	enum video_format async_cache_format;
686 	bool async_cache_full_range;
687 	enum gs_color_format async_texture_formats[MAX_AV_PLANES];
688 	int async_channel_count;
689 	long async_rotation;
690 	bool async_flip;
691 	bool async_linear_alpha;
692 	bool async_active;
693 	bool async_update_texture;
694 	bool async_unbuffered;
695 	bool async_decoupled;
696 	struct obs_source_frame *async_preload_frame;
697 	DARRAY(struct async_frame) async_cache;
698 	DARRAY(struct obs_source_frame *) async_frames;
699 	pthread_mutex_t async_mutex;
700 	uint32_t async_width;
701 	uint32_t async_height;
702 	uint32_t async_cache_width;
703 	uint32_t async_cache_height;
704 	uint32_t async_convert_width[MAX_AV_PLANES];
705 	uint32_t async_convert_height[MAX_AV_PLANES];
706 
707 	pthread_mutex_t caption_cb_mutex;
708 	DARRAY(struct caption_cb_info) caption_cb_list;
709 
710 	/* async video deinterlacing */
711 	uint64_t deinterlace_offset;
712 	uint64_t deinterlace_frame_ts;
713 	gs_effect_t *deinterlace_effect;
714 	struct obs_source_frame *prev_async_frame;
715 	gs_texture_t *async_prev_textures[MAX_AV_PLANES];
716 	gs_texrender_t *async_prev_texrender;
717 	uint32_t deinterlace_half_duration;
718 	enum obs_deinterlace_mode deinterlace_mode;
719 	bool deinterlace_top_first;
720 	bool deinterlace_rendered;
721 
722 	/* filters */
723 	struct obs_source *filter_parent;
724 	struct obs_source *filter_target;
725 	DARRAY(struct obs_source *) filters;
726 	pthread_mutex_t filter_mutex;
727 	gs_texrender_t *filter_texrender;
728 	enum obs_allow_direct_render allow_direct;
729 	bool rendering_filter;
730 
731 	/* sources specific hotkeys */
732 	obs_hotkey_pair_id mute_unmute_key;
733 	obs_hotkey_id push_to_mute_key;
734 	obs_hotkey_id push_to_talk_key;
735 	bool push_to_mute_enabled;
736 	bool push_to_mute_pressed;
737 	bool user_push_to_mute_pressed;
738 	bool push_to_talk_enabled;
739 	bool push_to_talk_pressed;
740 	bool user_push_to_talk_pressed;
741 	uint64_t push_to_mute_delay;
742 	uint64_t push_to_mute_stop_time;
743 	uint64_t push_to_talk_delay;
744 	uint64_t push_to_talk_stop_time;
745 
746 	/* transitions */
747 	uint64_t transition_start_time;
748 	uint64_t transition_duration;
749 	pthread_mutex_t transition_tex_mutex;
750 	gs_texrender_t *transition_texrender[2];
751 	pthread_mutex_t transition_mutex;
752 	obs_source_t *transition_sources[2];
753 	float transition_manual_clamp;
754 	float transition_manual_torque;
755 	float transition_manual_target;
756 	float transition_manual_val;
757 	bool transitioning_video;
758 	bool transitioning_audio;
759 	bool transition_source_active[2];
760 	uint32_t transition_alignment;
761 	uint32_t transition_actual_cx;
762 	uint32_t transition_actual_cy;
763 	uint32_t transition_cx;
764 	uint32_t transition_cy;
765 	uint32_t transition_fixed_duration;
766 	bool transition_use_fixed_duration;
767 	enum obs_transition_mode transition_mode;
768 	enum obs_transition_scale_type transition_scale_type;
769 	struct matrix4 transition_matrices[2];
770 
771 	struct audio_monitor *monitor;
772 	enum obs_monitoring_type monitoring_type;
773 
774 	obs_data_t *private_settings;
775 };
776 
777 extern struct obs_source_info *get_source_info(const char *id);
778 extern struct obs_source_info *get_source_info2(const char *unversioned_id,
779 						uint32_t ver);
780 extern bool obs_source_init_context(struct obs_source *source,
781 				    obs_data_t *settings, const char *name,
782 				    obs_data_t *hotkey_data, bool private);
783 
784 extern bool obs_transition_init(obs_source_t *transition);
785 extern void obs_transition_free(obs_source_t *transition);
786 extern void obs_transition_tick(obs_source_t *transition, float t);
787 extern void obs_transition_enum_sources(obs_source_t *transition,
788 					obs_source_enum_proc_t enum_callback,
789 					void *param);
790 extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
791 extern void obs_transition_load(obs_source_t *source, obs_data_t *data);
792 
793 struct audio_monitor *audio_monitor_create(obs_source_t *source);
794 void audio_monitor_reset(struct audio_monitor *monitor);
795 extern void audio_monitor_destroy(struct audio_monitor *monitor);
796 
797 extern obs_source_t *obs_source_create_set_last_ver(const char *id,
798 						    const char *name,
799 						    obs_data_t *settings,
800 						    obs_data_t *hotkey_data,
801 						    uint32_t last_obs_ver);
802 extern void obs_source_destroy(struct obs_source *source);
803 
804 enum view_type {
805 	MAIN_VIEW,
806 	AUX_VIEW,
807 };
808 
obs_source_dosignal(struct obs_source * source,const char * signal_obs,const char * signal_source)809 static inline void obs_source_dosignal(struct obs_source *source,
810 				       const char *signal_obs,
811 				       const char *signal_source)
812 {
813 	struct calldata data;
814 	uint8_t stack[128];
815 
816 	calldata_init_fixed(&data, stack, sizeof(stack));
817 	calldata_set_ptr(&data, "source", source);
818 	if (signal_obs && !source->context.private)
819 		signal_handler_signal(obs->signals, signal_obs, &data);
820 	if (signal_source)
821 		signal_handler_signal(source->context.signals, signal_source,
822 				      &data);
823 }
824 
825 /* maximum timestamp variance in nanoseconds */
826 #define MAX_TS_VAR 2000000000ULL
827 
frame_out_of_bounds(const obs_source_t * source,uint64_t ts)828 static inline bool frame_out_of_bounds(const obs_source_t *source, uint64_t ts)
829 {
830 	if (ts < source->last_frame_ts)
831 		return ((source->last_frame_ts - ts) > MAX_TS_VAR);
832 	else
833 		return ((ts - source->last_frame_ts) > MAX_TS_VAR);
834 }
835 
836 static inline enum gs_color_format
convert_video_format(enum video_format format)837 convert_video_format(enum video_format format)
838 {
839 	switch (format) {
840 	case VIDEO_FORMAT_RGBA:
841 		return GS_RGBA;
842 	case VIDEO_FORMAT_BGRA:
843 	case VIDEO_FORMAT_I40A:
844 	case VIDEO_FORMAT_I42A:
845 	case VIDEO_FORMAT_YUVA:
846 	case VIDEO_FORMAT_AYUV:
847 		return GS_BGRA;
848 	default:
849 		return GS_BGRX;
850 	}
851 }
852 
853 extern void obs_source_set_texcoords_centered(obs_source_t *source,
854 					      bool centered);
855 extern void obs_source_activate(obs_source_t *source, enum view_type type);
856 extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
857 extern void obs_source_video_tick(obs_source_t *source, float seconds);
858 extern float obs_source_get_target_volume(obs_source_t *source,
859 					  obs_source_t *target);
860 
861 extern void obs_source_audio_render(obs_source_t *source, uint32_t mixers,
862 				    size_t channels, size_t sample_rate,
863 				    size_t size);
864 
865 extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);
866 
867 extern struct obs_source_frame *filter_async_video(obs_source_t *source,
868 						   struct obs_source_frame *in);
869 extern bool update_async_texture(struct obs_source *source,
870 				 const struct obs_source_frame *frame,
871 				 gs_texture_t *tex, gs_texrender_t *texrender);
872 extern bool update_async_textures(struct obs_source *source,
873 				  const struct obs_source_frame *frame,
874 				  gs_texture_t *tex[MAX_AV_PLANES],
875 				  gs_texrender_t *texrender);
876 extern bool set_async_texture_size(struct obs_source *source,
877 				   const struct obs_source_frame *frame);
878 extern void remove_async_frame(obs_source_t *source,
879 			       struct obs_source_frame *frame);
880 
881 extern void set_deinterlace_texture_size(obs_source_t *source);
882 extern void deinterlace_process_last_frame(obs_source_t *source,
883 					   uint64_t sys_time);
884 extern void deinterlace_update_async_video(obs_source_t *source);
885 extern void deinterlace_render(obs_source_t *s);
886 
887 /* ------------------------------------------------------------------------- */
888 /* outputs  */
889 
890 enum delay_msg {
891 	DELAY_MSG_PACKET,
892 	DELAY_MSG_START,
893 	DELAY_MSG_STOP,
894 };
895 
896 struct delay_data {
897 	enum delay_msg msg;
898 	uint64_t ts;
899 	struct encoder_packet packet;
900 };
901 
902 typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);
903 
904 struct obs_weak_output {
905 	struct obs_weak_ref ref;
906 	struct obs_output *output;
907 };
908 
909 #define CAPTION_LINE_CHARS (32)
910 #define CAPTION_LINE_BYTES (4 * CAPTION_LINE_CHARS)
911 struct caption_text {
912 	char text[CAPTION_LINE_BYTES + 1];
913 	double display_duration;
914 	struct caption_text *next;
915 };
916 
917 struct pause_data {
918 	pthread_mutex_t mutex;
919 	uint64_t last_video_ts;
920 	uint64_t ts_start;
921 	uint64_t ts_end;
922 	uint64_t ts_offset;
923 };
924 
925 extern bool video_pause_check(struct pause_data *pause, uint64_t timestamp);
926 extern bool audio_pause_check(struct pause_data *pause, struct audio_data *data,
927 			      size_t sample_rate);
928 extern void pause_reset(struct pause_data *pause);
929 
930 struct obs_output {
931 	struct obs_context_data context;
932 	struct obs_output_info info;
933 	struct obs_weak_output *control;
934 
935 	/* indicates ownership of the info.id buffer */
936 	bool owns_info_id;
937 
938 	bool received_video;
939 	bool received_audio;
940 	volatile bool data_active;
941 	volatile bool end_data_capture_thread_active;
942 	int64_t video_offset;
943 	int64_t audio_offsets[MAX_AUDIO_MIXES];
944 	int64_t highest_audio_ts;
945 	int64_t highest_video_ts;
946 	pthread_t end_data_capture_thread;
947 	os_event_t *stopping_event;
948 	pthread_mutex_t interleaved_mutex;
949 	DARRAY(struct encoder_packet) interleaved_packets;
950 	int stop_code;
951 
952 	int reconnect_retry_sec;
953 	int reconnect_retry_max;
954 	int reconnect_retries;
955 	int reconnect_retry_cur_sec;
956 	pthread_t reconnect_thread;
957 	os_event_t *reconnect_stop_event;
958 	volatile bool reconnecting;
959 	volatile bool reconnect_thread_active;
960 
961 	uint32_t starting_drawn_count;
962 	uint32_t starting_lagged_count;
963 	uint32_t starting_frame_count;
964 
965 	int total_frames;
966 
967 	volatile bool active;
968 	volatile bool paused;
969 	video_t *video;
970 	audio_t *audio;
971 	obs_encoder_t *video_encoder;
972 	obs_encoder_t *audio_encoders[MAX_AUDIO_MIXES];
973 	obs_service_t *service;
974 	size_t mixer_mask;
975 
976 	struct pause_data pause;
977 
978 	struct circlebuf audio_buffer[MAX_AUDIO_MIXES][MAX_AV_PLANES];
979 	uint64_t audio_start_ts;
980 	uint64_t video_start_ts;
981 	size_t audio_size;
982 	size_t planes;
983 	size_t sample_rate;
984 	size_t total_audio_frames;
985 
986 	uint32_t scaled_width;
987 	uint32_t scaled_height;
988 
989 	bool video_conversion_set;
990 	bool audio_conversion_set;
991 	struct video_scale_info video_conversion;
992 	struct audio_convert_info audio_conversion;
993 
994 	pthread_mutex_t caption_mutex;
995 	double caption_timestamp;
996 	struct caption_text *caption_head;
997 	struct caption_text *caption_tail;
998 
999 	struct circlebuf caption_data;
1000 
1001 	bool valid;
1002 
1003 	uint64_t active_delay_ns;
1004 	encoded_callback_t delay_callback;
1005 	struct circlebuf delay_data; /* struct delay_data */
1006 	pthread_mutex_t delay_mutex;
1007 	uint32_t delay_sec;
1008 	uint32_t delay_flags;
1009 	uint32_t delay_cur_flags;
1010 	volatile long delay_restart_refs;
1011 	volatile bool delay_active;
1012 	volatile bool delay_capturing;
1013 
1014 	char *last_error_message;
1015 
1016 	float audio_data[MAX_AUDIO_CHANNELS][AUDIO_OUTPUT_FRAMES];
1017 };
1018 
do_output_signal(struct obs_output * output,const char * signal)1019 static inline void do_output_signal(struct obs_output *output,
1020 				    const char *signal)
1021 {
1022 	struct calldata params = {0};
1023 	calldata_set_ptr(&params, "output", output);
1024 	signal_handler_signal(output->context.signals, signal, &params);
1025 	calldata_free(&params);
1026 }
1027 
1028 extern void process_delay(void *data, struct encoder_packet *packet);
1029 extern void obs_output_cleanup_delay(obs_output_t *output);
1030 extern bool obs_output_delay_start(obs_output_t *output);
1031 extern void obs_output_delay_stop(obs_output_t *output);
1032 extern bool obs_output_actual_start(obs_output_t *output);
1033 extern void obs_output_actual_stop(obs_output_t *output, bool force,
1034 				   uint64_t ts);
1035 
1036 extern const struct obs_output_info *find_output(const char *id);
1037 
1038 extern void obs_output_remove_encoder(struct obs_output *output,
1039 				      struct obs_encoder *encoder);
1040 
1041 extern void
1042 obs_encoder_packet_create_instance(struct encoder_packet *dst,
1043 				   const struct encoder_packet *src);
1044 void obs_output_destroy(obs_output_t *output);
1045 
1046 /* ------------------------------------------------------------------------- */
1047 /* encoders  */
1048 
1049 struct obs_weak_encoder {
1050 	struct obs_weak_ref ref;
1051 	struct obs_encoder *encoder;
1052 };
1053 
1054 struct encoder_callback {
1055 	bool sent_first_packet;
1056 	void (*new_packet)(void *param, struct encoder_packet *packet);
1057 	void *param;
1058 };
1059 
1060 struct obs_encoder {
1061 	struct obs_context_data context;
1062 	struct obs_encoder_info info;
1063 	struct obs_weak_encoder *control;
1064 
1065 	/* allows re-routing to another encoder */
1066 	struct obs_encoder_info orig_info;
1067 
1068 	pthread_mutex_t init_mutex;
1069 
1070 	uint32_t samplerate;
1071 	size_t planes;
1072 	size_t blocksize;
1073 	size_t framesize;
1074 	size_t framesize_bytes;
1075 
1076 	size_t mixer_idx;
1077 
1078 	uint32_t scaled_width;
1079 	uint32_t scaled_height;
1080 	enum video_format preferred_format;
1081 
1082 	volatile bool active;
1083 	volatile bool paused;
1084 	bool initialized;
1085 
1086 	/* indicates ownership of the info.id buffer */
1087 	bool owns_info_id;
1088 
1089 	uint32_t timebase_num;
1090 	uint32_t timebase_den;
1091 
1092 	int64_t cur_pts;
1093 
1094 	struct circlebuf audio_input_buffer[MAX_AV_PLANES];
1095 	uint8_t *audio_output_buffer[MAX_AV_PLANES];
1096 
1097 	/* if a video encoder is paired with an audio encoder, make it start
1098 	 * up at the specific timestamp.  if this is the audio encoder,
1099 	 * wait_for_video makes it wait until it's ready to sync up with
1100 	 * video */
1101 	bool wait_for_video;
1102 	bool first_received;
1103 	struct obs_encoder *paired_encoder;
1104 	int64_t offset_usec;
1105 	uint64_t first_raw_ts;
1106 	uint64_t start_ts;
1107 
1108 	pthread_mutex_t outputs_mutex;
1109 	DARRAY(obs_output_t *) outputs;
1110 
1111 	bool destroy_on_stop;
1112 
1113 	/* stores the video/audio media output pointer.  video_t *or audio_t **/
1114 	void *media;
1115 
1116 	pthread_mutex_t callbacks_mutex;
1117 	DARRAY(struct encoder_callback) callbacks;
1118 
1119 	struct pause_data pause;
1120 
1121 	const char *profile_encoder_encode_name;
1122 	char *last_error_message;
1123 
1124 	/* reconfigure encoder at next possible opportunity */
1125 	bool reconfigure_requested;
1126 };
1127 
1128 extern struct obs_encoder_info *find_encoder(const char *id);
1129 
1130 extern bool obs_encoder_initialize(obs_encoder_t *encoder);
1131 extern void obs_encoder_shutdown(obs_encoder_t *encoder);
1132 
1133 extern void obs_encoder_start(obs_encoder_t *encoder,
1134 			      void (*new_packet)(void *param,
1135 						 struct encoder_packet *packet),
1136 			      void *param);
1137 extern void obs_encoder_stop(obs_encoder_t *encoder,
1138 			     void (*new_packet)(void *param,
1139 						struct encoder_packet *packet),
1140 			     void *param);
1141 
1142 extern void obs_encoder_add_output(struct obs_encoder *encoder,
1143 				   struct obs_output *output);
1144 extern void obs_encoder_remove_output(struct obs_encoder *encoder,
1145 				      struct obs_output *output);
1146 
1147 extern bool start_gpu_encode(obs_encoder_t *encoder);
1148 extern void stop_gpu_encode(obs_encoder_t *encoder);
1149 
1150 extern bool do_encode(struct obs_encoder *encoder, struct encoder_frame *frame);
1151 extern void send_off_encoder_packet(obs_encoder_t *encoder, bool success,
1152 				    bool received, struct encoder_packet *pkt);
1153 
1154 void obs_encoder_destroy(obs_encoder_t *encoder);
1155 
1156 /* ------------------------------------------------------------------------- */
1157 /* services */
1158 
1159 struct obs_weak_service {
1160 	struct obs_weak_ref ref;
1161 	struct obs_service *service;
1162 };
1163 
1164 struct obs_service {
1165 	struct obs_context_data context;
1166 	struct obs_service_info info;
1167 	struct obs_weak_service *control;
1168 
1169 	/* indicates ownership of the info.id buffer */
1170 	bool owns_info_id;
1171 
1172 	bool active;
1173 	bool destroy;
1174 	struct obs_output *output;
1175 };
1176 
1177 extern const struct obs_service_info *find_service(const char *id);
1178 
1179 extern void obs_service_activate(struct obs_service *service);
1180 extern void obs_service_deactivate(struct obs_service *service, bool remove);
1181 extern bool obs_service_initialize(struct obs_service *service,
1182 				   struct obs_output *output);
1183 
1184 void obs_service_destroy(obs_service_t *service);
1185