obs-internal.h 35.9 KB
Newer Older
J
jp9000 已提交
1
/******************************************************************************
J
jp9000 已提交
2
    Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
J
jp9000 已提交
3 4 5

    This program is free software: you can redistribute it and/or modify
    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
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

18
#pragma once
J
jp9000 已提交
19

J
jp9000 已提交
20
#include "util/c99defs.h"
J
jp9000 已提交
21
#include "util/darray.h"
22
#include "util/circlebuf.h"
J
jp9000 已提交
23
#include "util/dstr.h"
J
jp9000 已提交
24
#include "util/threading.h"
J
jp9000 已提交
25
#include "util/platform.h"
P
Palana 已提交
26
#include "util/profiler.h"
J
jp9000 已提交
27 28
#include "callback/signal.h"
#include "callback/proc.h"
J
jp9000 已提交
29 30

#include "graphics/graphics.h"
J
jp9000 已提交
31
#include "graphics/matrix4.h"
J
jp9000 已提交
32

J
jp9000 已提交
33
#include "media-io/audio-resampler.h"
J
jp9000 已提交
34 35 36 37
#include "media-io/video-io.h"
#include "media-io/audio-io.h"

#include "obs.h"
38

J
jp9000 已提交
39
#define NUM_TEXTURES 2
40
#define MICROSECOND_DEN 1000000
41 42
#define NUM_ENCODE_TEXTURES 3
#define NUM_ENCODE_TEXTURE_FRAMES_TO_WAIT 1
J
jp9000 已提交
43

44 45 46 47
static inline int64_t packet_dts_usec(struct encoder_packet *packet)
{
	return packet->dts * MICROSECOND_DEN / packet->timebase_den;
}
48

49 50 51 52 53
struct tick_callback {
	void (*tick)(void *param, float seconds);
	void *param;
};

54 55 56 57 58
struct draw_callback {
	void (*draw)(void *param, uint32_t cx, uint32_t cy);
	void *param;
};

59 60 61 62 63 64 65
/* ------------------------------------------------------------------------- */
/* validity checks */

static inline bool obs_object_valid(const void *obj, const char *f,
		const char *t)
{
	if (!obj) {
66
		blog(LOG_DEBUG, "%s: Null '%s' parameter", f, t);
67 68 69 70 71 72
		return false;
	}

	return true;
}

73 74 75 76 77
#define obs_ptr_valid(ptr, func) obs_object_valid(ptr, func, #ptr)
#define obs_source_valid  obs_ptr_valid
#define obs_output_valid  obs_ptr_valid
#define obs_encoder_valid obs_ptr_valid
#define obs_service_valid obs_ptr_valid
78

J
jp9000 已提交
79
/* ------------------------------------------------------------------------- */
80
/* modules */
J
jp9000 已提交
81 82

struct obs_module {
83
	char *mod_name;
J
jp9000 已提交
84 85 86
	const char *file;
	char *bin_path;
	char *data_path;
J
jp9000 已提交
87
	void *module;
J
jp9000 已提交
88 89
	bool loaded;

90
	bool        (*load)(void);
J
jp9000 已提交
91
	void        (*unload)(void);
92
	void        (*post_load)(void);
J
jp9000 已提交
93 94 95
	void        (*set_locale)(const char *locale);
	void        (*free_locale)(void);
	uint32_t    (*ver)(void);
96
	void        (*set_pointer)(obs_module_t *module);
J
jp9000 已提交
97 98 99 100 101
	const char *(*name)(void);
	const char *(*description)(void);
	const char *(*author)(void);

	struct obs_module *next;
J
jp9000 已提交
102 103
};

J
jp9000 已提交
104 105
extern void free_module(struct obs_module *mod);

J
jp9000 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
struct obs_module_path {
	char *bin;
	char *data;
};

static inline void free_module_path(struct obs_module_path *omp)
{
	if (omp) {
		bfree(omp->bin);
		bfree(omp->data);
	}
}

static inline bool check_path(const char *data, const char *path,
		struct dstr *output)
{
	dstr_copy(output, path);
	dstr_cat(output, data);

	return os_file_exists(output->array);
}

J
jp9000 已提交
128

P
Palana 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/* ------------------------------------------------------------------------- */
/* hotkeys */

struct obs_hotkey {
	obs_hotkey_id               id;
	char                        *name;
	char                        *description;

	obs_hotkey_func             func;
	void                        *data;
	int                         pressed;

	obs_hotkey_registerer_t     registerer_type;
	void                        *registerer;

	obs_hotkey_id               pair_partner_id;
};

struct obs_hotkey_pair {
	obs_hotkey_pair_id          pair_id;
	obs_hotkey_id               id[2];
	obs_hotkey_active_func      func[2];
151 152
	bool                        pressed0;
	bool                        pressed1;
P
Palana 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	void                        *data[2];
};

typedef struct obs_hotkey_pair obs_hotkey_pair_t;

typedef struct obs_hotkeys_platform obs_hotkeys_platform_t;

void *obs_hotkey_thread(void *param);

struct obs_core_hotkeys;
bool obs_hotkeys_platform_init(struct obs_core_hotkeys *hotkeys);
void obs_hotkeys_platform_free(struct obs_core_hotkeys *hotkeys);
bool obs_hotkeys_platform_is_pressed(obs_hotkeys_platform_t *context,
		obs_key_t key);

const char *obs_get_hotkey_translation(obs_key_t key, const char *def);

struct obs_context_data;
void obs_hotkeys_context_release(struct obs_context_data *context);

void obs_hotkeys_free(void);

struct obs_hotkey_binding {
	obs_key_combination_t       key;
177 178
	bool                        pressed;
	bool                        modifiers_match;
P
Palana 已提交
179 180 181 182 183 184 185 186 187

	obs_hotkey_id               hotkey_id;
	obs_hotkey_t                *hotkey;
};

struct obs_hotkey_name_map;
void obs_hotkey_name_map_free(void);


188
/* ------------------------------------------------------------------------- */
J
jp9000 已提交
189
/* views */
190

J
jp9000 已提交
191
struct obs_view {
192
	pthread_mutex_t                 channels_mutex;
193
	obs_source_t                    *channels[MAX_CHANNELS];
194 195
};

J
jp9000 已提交
196 197
extern bool obs_view_init(struct obs_view *view);
extern void obs_view_free(struct obs_view *view);
198 199 200 201 202 203 204


/* ------------------------------------------------------------------------- */
/* displays */

struct obs_display {
	bool                            size_changed;
J
jp9000 已提交
205
	bool                            enabled;
206
	uint32_t                        cx, cy;
207
	uint32_t                        background_color;
208
	gs_swapchain_t                  *swap;
209
	pthread_mutex_t                 draw_callbacks_mutex;
210
	pthread_mutex_t                 draw_info_mutex;
211
	DARRAY(struct draw_callback)    draw_callbacks;
212 213 214

	struct obs_display              *next;
	struct obs_display              **prev_next;
215 216 217
};

extern bool obs_display_init(struct obs_display *display,
218
		const struct gs_init_data *graphics_data);
219 220 221
extern void obs_display_free(struct obs_display *display);


222
/* ------------------------------------------------------------------------- */
J
jp9000 已提交
223
/* core */
224

225 226 227 228 229
struct obs_vframe_info {
	uint64_t timestamp;
	int count;
};

230 231 232 233 234 235 236 237 238 239
struct obs_tex_frame {
	gs_texture_t *tex;
	gs_texture_t *tex_uv;
	uint32_t handle;
	uint64_t timestamp;
	uint64_t lock_key;
	int count;
	bool released;
};

240
struct obs_core_video {
241 242 243 244 245
	graphics_t                      *graphics;
	gs_stagesurf_t                  *copy_surfaces[NUM_TEXTURES];
	gs_texture_t                    *render_textures[NUM_TEXTURES];
	gs_texture_t                    *output_textures[NUM_TEXTURES];
	gs_texture_t                    *convert_textures[NUM_TEXTURES];
246
	gs_texture_t                    *convert_uv_textures[NUM_TEXTURES];
247 248 249
	bool                            textures_rendered[NUM_TEXTURES];
	bool                            textures_output[NUM_TEXTURES];
	bool                            textures_copied[NUM_TEXTURES];
J
jp9000 已提交
250
	bool                            textures_converted[NUM_TEXTURES];
251
	bool                            using_nv12_tex;
252
	struct circlebuf                vframe_info_buffer;
253
	struct circlebuf                vframe_info_buffer_gpu;
254
	gs_effect_t                     *default_effect;
P
Palana 已提交
255
	gs_effect_t                     *default_rect_effect;
256
	gs_effect_t                     *opaque_effect;
257
	gs_effect_t                     *solid_effect;
258
	gs_effect_t                     *repeat_effect;
259
	gs_effect_t                     *conversion_effect;
260 261
	gs_effect_t                     *bicubic_effect;
	gs_effect_t                     *lanczos_effect;
262
	gs_effect_t                     *bilinear_lowres_effect;
263
	gs_effect_t                     *premultiplied_alpha_effect;
264
	gs_samplerstate_t               *point_sampler;
265
	gs_stagesurf_t                  *mapped_surface;
266
	int                             cur_texture;
267
	long                            raw_active;
268 269 270 271 272 273
	long                            gpu_encoder_active;
	pthread_mutex_t                 gpu_encoder_mutex;
	struct circlebuf                gpu_encoder_queue;
	struct circlebuf                gpu_encoder_avail_queue;
	DARRAY(obs_encoder_t *)         gpu_encoders;
	os_sem_t                        *gpu_encode_semaphore;
274
	os_event_t                      *gpu_encode_inactive;
275 276 277
	pthread_t                       gpu_encode_thread;
	bool                            gpu_encode_thread_initialized;
	volatile bool                   gpu_encode_stop;
278

J
jp9000 已提交
279
	uint64_t                        video_time;
280
	uint64_t                        video_avg_frame_time_ns;
J
jp9000 已提交
281
	double                          video_fps;
282
	video_t                         *video;
283
	pthread_t                       video_thread;
284 285
	uint32_t                        total_frames;
	uint32_t                        lagged_frames;
286 287
	bool                            thread_initialized;

J
jp9000 已提交
288 289 290 291 292 293 294 295 296
	bool                            gpu_conversion;
	const char                      *conversion_tech;
	uint32_t                        conversion_height;
	uint32_t                        plane_offsets[3];
	uint32_t                        plane_sizes[3];
	uint32_t                        plane_linewidth[3];

	uint32_t                        output_width;
	uint32_t                        output_height;
297 298
	uint32_t                        base_width;
	uint32_t                        base_height;
299
	float                           color_matrix[16];
300
	enum obs_scale_type             scale_type;
301 302

	gs_texture_t                    *transparent_texture;
J
jp9000 已提交
303 304 305 306 307 308 309 310 311

	gs_effect_t                     *deinterlace_discard_effect;
	gs_effect_t                     *deinterlace_discard_2x_effect;
	gs_effect_t                     *deinterlace_linear_effect;
	gs_effect_t                     *deinterlace_linear_2x_effect;
	gs_effect_t                     *deinterlace_blend_effect;
	gs_effect_t                     *deinterlace_blend_2x_effect;
	gs_effect_t                     *deinterlace_yadif_effect;
	gs_effect_t                     *deinterlace_yadif_2x_effect;
312 313

	struct obs_video_info           ovi;
314 315
};

J
jp9000 已提交
316 317
struct audio_monitor;

318
struct obs_core_audio {
319
	audio_t                         *audio;
J
jp9000 已提交
320

J
jp9000 已提交
321 322 323 324 325 326 327 328
	DARRAY(struct obs_source*)      render_order;
	DARRAY(struct obs_source*)      root_nodes;

	uint64_t                        buffered_ts;
	struct circlebuf                buffered_timestamps;
	int                             buffering_wait_ticks;
	int                             total_buffering_ticks;

J
jp9000 已提交
329
	float                           user_volume;
J
jp9000 已提交
330 331 332 333 334

	pthread_mutex_t                 monitoring_mutex;
	DARRAY(struct audio_monitor*)   monitors;
	char                            *monitoring_device_name;
	char                            *monitoring_device_id;
335 336
};

337
/* user sources, output channels, and displays */
338
struct obs_core_data {
339
	struct obs_source               *first_source;
340
	struct obs_source               *first_audio_source;
341 342 343 344
	struct obs_display              *first_display;
	struct obs_output               *first_output;
	struct obs_encoder              *first_encoder;
	struct obs_service              *first_service;
345 346 347 348 349

	pthread_mutex_t                 sources_mutex;
	pthread_mutex_t                 displays_mutex;
	pthread_mutex_t                 outputs_mutex;
	pthread_mutex_t                 encoders_mutex;
350
	pthread_mutex_t                 services_mutex;
351
	pthread_mutex_t                 audio_sources_mutex;
352 353
	pthread_mutex_t                 draw_callbacks_mutex;
	DARRAY(struct draw_callback)    draw_callbacks;
354
	DARRAY(struct tick_callback)    tick_callbacks;
355

J
jp9000 已提交
356
	struct obs_view                 main_view;
357

J
jp9000 已提交
358 359
	long long                       unnamed_index;

360 361
	obs_data_t                      *private_data;

362
	volatile bool                   valid;
363 364
};

P
Palana 已提交
365 366 367 368 369 370 371 372 373 374 375
/* user hotkeys */
struct obs_core_hotkeys {
	pthread_mutex_t                 mutex;
	DARRAY(obs_hotkey_t)            hotkeys;
	obs_hotkey_id                   next_id;
	DARRAY(obs_hotkey_pair_t)       hotkey_pairs;
	obs_hotkey_pair_id              next_pair_id;

	pthread_t                       hotkey_thread;
	bool                            hotkey_thread_initialized;
	os_event_t                      *stop_event;
376 377 378
	bool                            thread_disable_press;
	bool                            strict_modifiers;
	bool                            reroute_hotkeys;
P
Palana 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391
	DARRAY(obs_hotkey_binding_t)    bindings;

	obs_hotkey_callback_router_func router_func;
	void                            *router_func_data;

	obs_hotkeys_platform_t          *platform_context;

	pthread_once_t                  name_map_init_token;
	struct obs_hotkey_name_map      *name_map;

	signal_handler_t                *signals;

	char                            *translations[OBS_KEY_LAST_VALUE];
P
Palana 已提交
392 393 394 395
	char                            *mute;
	char                            *unmute;
	char                            *push_to_mute;
	char                            *push_to_talk;
P
Palana 已提交
396 397
	char                            *sceneitem_show;
	char                            *sceneitem_hide;
P
Palana 已提交
398 399
};

400
struct obs_core {
J
jp9000 已提交
401 402 403
	struct obs_module               *first_module;
	DARRAY(struct obs_module_path)  module_paths;

404
	DARRAY(struct obs_source_info)  source_types;
J
jp9000 已提交
405 406 407 408 409 410 411 412 413
	DARRAY(struct obs_source_info)  input_types;
	DARRAY(struct obs_source_info)  filter_types;
	DARRAY(struct obs_source_info)  transition_types;
	DARRAY(struct obs_output_info)  output_types;
	DARRAY(struct obs_encoder_info) encoder_types;
	DARRAY(struct obs_service_info) service_types;
	DARRAY(struct obs_modal_ui)     modal_ui_callbacks;
	DARRAY(struct obs_modeless_ui)  modeless_ui_callbacks;

414 415
	signal_handler_t                *signals;
	proc_handler_t                  *procs;
416

417
	char                            *locale;
418
	char                            *module_config_path;
419 420
	bool                            name_store_owned;
	profiler_name_store_t           *name_store;
421

422 423
	/* segmented into multiple sub-structures to keep things a bit more
	 * clean and organized */
J
jp9000 已提交
424 425 426
	struct obs_core_video           video;
	struct obs_core_audio           audio;
	struct obs_core_data            data;
P
Palana 已提交
427
	struct obs_core_hotkeys         hotkeys;
J
jp9000 已提交
428 429
};

430
extern struct obs_core *obs;
431

432
extern void *obs_graphics_thread(void *param);
J
jp9000 已提交
433

J
jp9000 已提交
434 435
extern gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file);

J
jp9000 已提交
436 437 438 439
extern bool audio_callback(void *param,
		uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts,
		uint32_t mixers, struct audio_output_data *mixes);

440 441 442 443 444 445 446
extern void start_raw_video(video_t *video,
		const struct video_scale_info *conversion,
		void (*callback)(void *param, struct video_data *frame),
		void *param);
extern void stop_raw_video(video_t *video,
		void (*callback)(void *param, struct video_data *frame),
		void *param);
J
jp9000 已提交
447 448

/* ------------------------------------------------------------------------- */
449
/* obs shared context data */
J
jp9000 已提交
450

451 452
struct obs_context_data {
	char                            *name;
453
	void                            *data;
454 455 456
	obs_data_t                      *settings;
	signal_handler_t                *signals;
	proc_handler_t                  *procs;
457
	enum obs_obj_type               type;
J
jp9000 已提交
458

P
Palana 已提交
459 460 461 462
	DARRAY(obs_hotkey_id)           hotkeys;
	DARRAY(obs_hotkey_pair_id)      hotkey_pairs;
	obs_data_t                      *hotkey_data;

J
jp9000 已提交
463 464 465
	DARRAY(char*)                   rename_cache;
	pthread_mutex_t                 rename_cache_mutex;

466 467 468
	pthread_mutex_t                 *mutex;
	struct obs_context_data         *next;
	struct obs_context_data         **prev_next;
J
jp9000 已提交
469 470

	bool                            private;
471 472 473 474
};

extern bool obs_context_data_init(
		struct obs_context_data *context,
475
		enum obs_obj_type       type,
476
		obs_data_t              *settings,
P
Palana 已提交
477
		const char              *name,
J
jp9000 已提交
478 479
		obs_data_t              *hotkey_data,
		bool                    private);
480 481 482 483 484 485 486 487 488 489
extern void obs_context_data_free(struct obs_context_data *context);

extern void obs_context_data_insert(struct obs_context_data *context,
		pthread_mutex_t *mutex, void *first);
extern void obs_context_data_remove(struct obs_context_data *context);

extern void obs_context_data_setname(struct obs_context_data *context,
		const char *name);


P
Palana 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
/* ------------------------------------------------------------------------- */
/* ref-counting  */

struct obs_weak_ref {
	volatile long refs;
	volatile long weak_refs;
};

static inline void obs_ref_addref(struct obs_weak_ref *ref)
{
	os_atomic_inc_long(&ref->refs);
}

static inline bool obs_ref_release(struct obs_weak_ref *ref)
{
	return os_atomic_dec_long(&ref->refs) == -1;
}

static inline void obs_weak_ref_addref(struct obs_weak_ref *ref)
{
	os_atomic_inc_long(&ref->weak_refs);
}

static inline bool obs_weak_ref_release(struct obs_weak_ref *ref)
{
	return os_atomic_dec_long(&ref->weak_refs) == -1;
}

static inline bool obs_weak_ref_get_ref(struct obs_weak_ref *ref)
{
	long owners = ref->refs;
	while (owners > -1) {
		if (os_atomic_compare_swap_long(&ref->refs, owners, owners + 1))
			return true;

		owners = ref->refs;
	}

	return false;
}


532 533 534
/* ------------------------------------------------------------------------- */
/* sources  */

535 536
struct async_frame {
	struct obs_source_frame *frame;
537
	long unused_count;
538 539 540
	bool used;
};

J
jp9000 已提交
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
enum audio_action_type {
	AUDIO_ACTION_VOL,
	AUDIO_ACTION_MUTE,
	AUDIO_ACTION_PTT,
	AUDIO_ACTION_PTM,
};

struct audio_action {
	uint64_t timestamp;
	enum audio_action_type type;
	union {
		float vol;
		bool  set;
	};
};

557 558 559 560 561
struct obs_weak_source {
	struct obs_weak_ref ref;
	struct obs_source *source;
};

562 563 564 565 566
struct audio_cb_info {
	obs_source_audio_capture_t callback;
	void *param;
};

567 568 569
struct obs_source {
	struct obs_context_data         context;
	struct obs_source_info          info;
570
	struct obs_weak_source          *control;
571

J
jp9000 已提交
572 573
	/* general exposed flags that can be set for the source */
	uint32_t                        flags;
574
	uint32_t                        default_flags;
J
jp9000 已提交
575

576 577 578
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

579
	/* signals to call the source update in the video thread */
580 581
	bool                            defer_update;

582
	/* ensures show/hide are only called once */
J
jp9000 已提交
583
	volatile long                   show_refs;
584

585
	/* ensures activate/deactivate are only called once */
J
jp9000 已提交
586
	volatile long                   activate_refs;
587

J
jp9000 已提交
588 589 590
	/* used to indicate that the source has been removed and all
	 * references to it should be released (not exactly how I would prefer
	 * to handle things but it's the best option) */
591
	bool                            removed;
J
jp9000 已提交
592

593 594 595
	bool                            active;
	bool                            showing;

596 597 598
	/* used to temporarily disable sources if needed */
	bool                            enabled;

J
jp9000 已提交
599
	/* timing (if video is present, is based upon video) */
600 601
	volatile bool                   timing_set;
	volatile uint64_t               timing_adjust;
602
	uint64_t                        resample_offset;
J
jp9000 已提交
603
	uint64_t                        last_audio_ts;
604
	uint64_t                        next_audio_ts_min;
605
	uint64_t                        next_audio_sys_ts_min;
606 607
	uint64_t                        last_frame_ts;
	uint64_t                        last_sys_timestamp;
608
	bool                            async_rendered;
J
jp9000 已提交
609 610

	/* audio */
611
	bool                            audio_failed;
612
	bool                            audio_pending;
613
	bool                            pending_stop;
J
jp9000 已提交
614
	bool                            user_muted;
J
jp9000 已提交
615
	bool                            muted;
616 617
	struct obs_source               *next_audio_source;
	struct obs_source               **prev_next_audio_source;
618 619
	uint64_t                        audio_ts;
	struct circlebuf                audio_input_buf[MAX_AUDIO_CHANNELS];
620
	size_t                          last_audio_input_buf_size;
J
jp9000 已提交
621
	DARRAY(struct audio_action)     audio_actions;
622
	float                           *audio_output_buf[MAX_AUDIO_MIXES][MAX_AUDIO_CHANNELS];
623
	struct resample_info            sample_info;
624
	audio_resampler_t               *resampler;
J
jp9000 已提交
625
	pthread_mutex_t                 audio_actions_mutex;
626
	pthread_mutex_t                 audio_buf_mutex;
627
	pthread_mutex_t                 audio_mutex;
628 629
	pthread_mutex_t                 audio_cb_mutex;
	DARRAY(struct audio_cb_info)    audio_cb_list;
630
	struct obs_audio_data           audio_data;
631
	size_t                          audio_storage_size;
632
	uint32_t                        audio_mixers;
J
jp9000 已提交
633
	float                           user_volume;
J
jp9000 已提交
634
	float                           volume;
J
jp9000 已提交
635
	int64_t                         sync_offset;
636
	int64_t                         last_sync_offset;
C
cg2121 已提交
637
	float                           balance;
J
jp9000 已提交
638 639

	/* async video data */
640
	gs_texture_t                    *async_texture;
641
	gs_texrender_t                  *async_texrender;
642
	struct obs_source_frame         *cur_async_frame;
643
	bool                            async_gpu_conversion;
644
	enum video_format               async_format;
J
jp9000 已提交
645
	enum video_format               async_cache_format;
646
	enum gs_color_format            async_texture_format;
647
	float                           async_color_matrix[16];
648 649 650
	bool                            async_full_range;
	float                           async_color_range_min[3];
	float                           async_color_range_max[3];
651
	int                             async_plane_offset[2];
652
	bool                            async_flip;
653
	bool                            async_active;
654
	bool                            async_update_texture;
655
	bool                            async_unbuffered;
656
	bool                            async_decoupled;
657
	struct obs_source_frame         *async_preload_frame;
658
	DARRAY(struct async_frame)      async_cache;
659 660
	DARRAY(struct obs_source_frame*)async_frames;
	pthread_mutex_t                 async_mutex;
661 662
	uint32_t                        async_width;
	uint32_t                        async_height;
J
jp9000 已提交
663 664
	uint32_t                        async_cache_width;
	uint32_t                        async_cache_height;
665 666
	uint32_t                        async_convert_width;
	uint32_t                        async_convert_height;
J
jp9000 已提交
667

J
jp9000 已提交
668 669 670 671 672 673 674 675 676 677 678 679
	/* async video deinterlacing */
	uint64_t                        deinterlace_offset;
	uint64_t                        deinterlace_frame_ts;
	gs_effect_t                     *deinterlace_effect;
	struct obs_source_frame         *prev_async_frame;
	gs_texture_t                    *async_prev_texture;
	gs_texrender_t                  *async_prev_texrender;
	uint32_t                        deinterlace_half_duration;
	enum obs_deinterlace_mode       deinterlace_mode;
	bool                            deinterlace_top_first;
	bool                            deinterlace_rendered;

J
jp9000 已提交
680
	/* filters */
681 682 683 684
	struct obs_source               *filter_parent;
	struct obs_source               *filter_target;
	DARRAY(struct obs_source*)      filters;
	pthread_mutex_t                 filter_mutex;
685
	gs_texrender_t                  *filter_texrender;
686
	enum obs_allow_direct_render    allow_direct;
687
	bool                            rendering_filter;
P
Palana 已提交
688 689 690 691 692

	/* sources specific hotkeys */
	obs_hotkey_pair_id              mute_unmute_key;
	obs_hotkey_id                   push_to_mute_key;
	obs_hotkey_id                   push_to_talk_key;
693 694 695 696 697 698
	bool                            push_to_mute_enabled;
	bool                            push_to_mute_pressed;
	bool                            user_push_to_mute_pressed;
	bool                            push_to_talk_enabled;
	bool                            push_to_talk_pressed;
	bool                            user_push_to_talk_pressed;
P
Palana 已提交
699 700 701 702
	uint64_t                        push_to_mute_delay;
	uint64_t                        push_to_mute_stop_time;
	uint64_t                        push_to_talk_delay;
	uint64_t                        push_to_talk_stop_time;
J
jp9000 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

	/* transitions */
	uint64_t                        transition_start_time;
	uint64_t                        transition_duration;
	pthread_mutex_t                 transition_tex_mutex;
	gs_texrender_t                  *transition_texrender[2];
	pthread_mutex_t                 transition_mutex;
	obs_source_t                    *transition_sources[2];
	bool                            transitioning_video;
	bool                            transitioning_audio;
	bool                            transition_source_active[2];
	uint32_t                        transition_alignment;
	uint32_t                        transition_actual_cx;
	uint32_t                        transition_actual_cy;
	uint32_t                        transition_cx;
	uint32_t                        transition_cy;
	uint32_t                        transition_fixed_duration;
720
	bool                            transition_use_fixed_duration;
J
jp9000 已提交
721 722 723
	enum obs_transition_mode        transition_mode;
	enum obs_transition_scale_type  transition_scale_type;
	struct matrix4                  transition_matrices[2];
J
jp9000 已提交
724 725 726

	struct audio_monitor            *monitor;
	enum obs_monitoring_type        monitoring_type;
727 728

	obs_data_t                      *private_settings;
J
jp9000 已提交
729 730
};

731
extern struct obs_source_info *get_source_info(const char *id);
732
extern bool obs_source_init_context(struct obs_source *source,
733
		obs_data_t *settings, const char *name,
J
jp9000 已提交
734
		obs_data_t *hotkey_data, bool private);
J
jp9000 已提交
735

J
jp9000 已提交
736 737 738 739 740 741 742 743
extern bool obs_transition_init(obs_source_t *transition);
extern void obs_transition_free(obs_source_t *transition);
extern void obs_transition_tick(obs_source_t *transition);
extern void obs_transition_enum_sources(obs_source_t *transition,
		obs_source_enum_proc_t enum_callback, void *param);
extern void obs_transition_save(obs_source_t *source, obs_data_t *data);
extern void obs_transition_load(obs_source_t *source, obs_data_t *data);

J
jp9000 已提交
744 745 746 747
struct audio_monitor *audio_monitor_create(obs_source_t *source);
void audio_monitor_reset(struct audio_monitor *monitor);
extern void audio_monitor_destroy(struct audio_monitor *monitor);

748 749
extern void obs_source_destroy(struct obs_source *source);

750 751 752 753 754
enum view_type {
	MAIN_VIEW,
	AUX_VIEW
};

755 756 757 758
static inline void obs_source_dosignal(struct obs_source *source,
		const char *signal_obs, const char *signal_source)
{
	struct calldata data;
759
	uint8_t stack[128];
760

761
	calldata_init_fixed(&data, stack, sizeof(stack));
762
	calldata_set_ptr(&data, "source", source);
J
jp9000 已提交
763
	if (signal_obs && !source->context.private)
764 765 766 767 768 769
		signal_handler_signal(obs->signals, signal_obs, &data);
	if (signal_source)
		signal_handler_signal(source->context.signals, signal_source,
				&data);
}

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
/* maximum timestamp variance in nanoseconds */
#define MAX_TS_VAR          2000000000ULL

static inline bool frame_out_of_bounds(const obs_source_t *source, uint64_t ts)
{
	if (ts < source->last_frame_ts)
		return ((source->last_frame_ts - ts) > MAX_TS_VAR);
	else
		return ((ts - source->last_frame_ts) > MAX_TS_VAR);
}

static inline enum gs_color_format convert_video_format(
		enum video_format format)
{
	if (format == VIDEO_FORMAT_RGBA)
		return GS_RGBA;
	else if (format == VIDEO_FORMAT_BGRA)
		return GS_BGRA;

	return GS_BGRX;
}

792 793 794
extern void obs_source_activate(obs_source_t *source, enum view_type type);
extern void obs_source_deactivate(obs_source_t *source, enum view_type type);
extern void obs_source_video_tick(obs_source_t *source, float seconds);
795 796
extern float obs_source_get_target_volume(obs_source_t *source,
		obs_source_t *target);
J
jp9000 已提交
797

798 799 800
extern void obs_source_audio_render(obs_source_t *source, uint32_t mixers,
		size_t channels, size_t sample_rate, size_t size);

801 802
extern void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy);

803 804
extern struct obs_source_frame *filter_async_video(obs_source_t *source,
		struct obs_source_frame *in);
805 806 807
extern bool update_async_texture(struct obs_source *source,
		const struct obs_source_frame *frame,
		gs_texture_t *tex, gs_texrender_t *texrender);
808 809 810 811 812
extern bool set_async_texture_size(struct obs_source *source,
		const struct obs_source_frame *frame);
extern void remove_async_frame(obs_source_t *source,
		struct obs_source_frame *frame);

J
jp9000 已提交
813 814 815 816 817 818
extern void set_deinterlace_texture_size(obs_source_t *source);
extern void deinterlace_process_last_frame(obs_source_t *source,
		uint64_t sys_time);
extern void deinterlace_update_async_video(obs_source_t *source);
extern void deinterlace_render(obs_source_t *s);

J
jp9000 已提交
819 820 821 822

/* ------------------------------------------------------------------------- */
/* outputs  */

823 824 825 826 827 828 829 830 831 832 833 834
enum delay_msg {
	DELAY_MSG_PACKET,
	DELAY_MSG_START,
	DELAY_MSG_STOP,
};

struct delay_data {
	enum delay_msg msg;
	uint64_t ts;
	struct encoder_packet packet;
};

835 836
typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);

837 838 839 840 841
struct obs_weak_output {
	struct obs_weak_ref ref;
	struct obs_output *output;
};

842 843 844 845
#define CAPTION_LINE_CHARS (32)
#define CAPTION_LINE_BYTES (4*CAPTION_LINE_CHARS)
struct caption_text {
	char text[CAPTION_LINE_BYTES+1];
846
	double display_duration;
847 848 849
	struct caption_text *next;
};

J
jp9000 已提交
850
struct obs_output {
851
	struct obs_context_data         context;
852
	struct obs_output_info          info;
853
	struct obs_weak_output          *control;
854

855 856 857
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

858 859
	bool                            received_video;
	bool                            received_audio;
860
	volatile bool                   data_active;
861
	volatile bool                   end_data_capture_thread_active;
862
	int64_t                         video_offset;
863
	int64_t                         audio_offsets[MAX_AUDIO_MIXES];
J
jp9000 已提交
864 865
	int64_t                         highest_audio_ts;
	int64_t                         highest_video_ts;
866
	pthread_t                       end_data_capture_thread;
867
	os_event_t                      *stopping_event;
J
jp9000 已提交
868
	pthread_mutex_t                 interleaved_mutex;
869
	DARRAY(struct encoder_packet)   interleaved_packets;
870
	int                             stop_code;
J
jp9000 已提交
871

J
jp9000 已提交
872 873 874
	int                             reconnect_retry_sec;
	int                             reconnect_retry_max;
	int                             reconnect_retries;
875
	int                             reconnect_retry_cur_sec;
J
jp9000 已提交
876
	pthread_t                       reconnect_thread;
877
	os_event_t                      *reconnect_stop_event;
878
	volatile bool                   reconnecting;
J
jp9000 已提交
879 880
	volatile bool                   reconnect_thread_active;

881 882
	uint32_t                        starting_drawn_count;
	uint32_t                        starting_lagged_count;
J
jp9000 已提交
883 884
	uint32_t                        starting_frame_count;

885 886
	int                             total_frames;

887
	volatile bool                   active;
888 889 890
	video_t                         *video;
	audio_t                         *audio;
	obs_encoder_t                   *video_encoder;
891
	obs_encoder_t                   *audio_encoders[MAX_AUDIO_MIXES];
892
	obs_service_t                   *service;
893
	size_t                          mixer_mask;
J
jp9000 已提交
894

895 896 897
	uint32_t                        scaled_width;
	uint32_t                        scaled_height;

J
jp9000 已提交
898 899 900 901 902
	bool                            video_conversion_set;
	bool                            audio_conversion_set;
	struct video_scale_info         video_conversion;
	struct audio_convert_info       audio_conversion;

903 904 905 906 907
	pthread_mutex_t                 caption_mutex;
	double                          caption_timestamp;
	struct caption_text             *caption_head;
	struct caption_text             *caption_tail;

908
	bool                            valid;
909 910 911 912 913 914 915 916 917

	uint64_t                        active_delay_ns;
	encoded_callback_t              delay_callback;
	struct circlebuf                delay_data; /* struct delay_data */
	pthread_mutex_t                 delay_mutex;
	uint32_t                        delay_sec;
	uint32_t                        delay_flags;
	uint32_t                        delay_cur_flags;
	volatile long                   delay_restart_refs;
918 919
	volatile bool                   delay_active;
	volatile bool                   delay_capturing;
920 921

	char                            *last_error_message;
J
jp9000 已提交
922 923
};

924 925 926 927 928 929 930 931 932 933 934 935 936 937
static inline void do_output_signal(struct obs_output *output,
		const char *signal)
{
	struct calldata params = {0};
	calldata_set_ptr(&params, "output", output);
	signal_handler_signal(output->context.signals, signal, &params);
	calldata_free(&params);
}

extern void process_delay(void *data, struct encoder_packet *packet);
extern void obs_output_cleanup_delay(obs_output_t *output);
extern bool obs_output_delay_start(obs_output_t *output);
extern void obs_output_delay_stop(obs_output_t *output);
extern bool obs_output_actual_start(obs_output_t *output);
938 939
extern void obs_output_actual_stop(obs_output_t *output, bool force,
		uint64_t ts);
940

941 942
extern const struct obs_output_info *find_output(const char *id);

J
jp9000 已提交
943 944 945
extern void obs_output_remove_encoder(struct obs_output *output,
		struct obs_encoder *encoder);

946 947
extern void obs_encoder_packet_create_instance(struct encoder_packet *dst,
		const struct encoder_packet *src);
948 949
void obs_output_destroy(obs_output_t *output);

J
jp9000 已提交
950 951 952 953

/* ------------------------------------------------------------------------- */
/* encoders  */

954 955 956 957 958
struct obs_weak_encoder {
	struct obs_weak_ref ref;
	struct obs_encoder *encoder;
};

959
struct encoder_callback {
960
	bool sent_first_packet;
J
jp9000 已提交
961 962 963 964 965
	void (*new_packet)(void *param, struct encoder_packet *packet);
	void *param;
};

struct obs_encoder {
966
	struct obs_context_data         context;
967
	struct obs_encoder_info         info;
968
	struct obs_weak_encoder         *control;
J
jp9000 已提交
969

J
jp9000 已提交
970 971 972
	/* allows re-routing to another encoder */
	struct obs_encoder_info         orig_info;

J
jp9000 已提交
973 974
	pthread_mutex_t                 init_mutex;

975 976 977 978 979 980
	uint32_t                        samplerate;
	size_t                          planes;
	size_t                          blocksize;
	size_t                          framesize;
	size_t                          framesize_bytes;

981 982
	size_t                          mixer_idx;

983 984
	uint32_t                        scaled_width;
	uint32_t                        scaled_height;
985
	enum video_format               preferred_format;
986

J
jp9000 已提交
987 988
	volatile bool                   active;
	bool                            initialized;
J
jp9000 已提交
989

990 991 992
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

993 994 995 996 997
	uint32_t                        timebase_num;
	uint32_t                        timebase_den;

	int64_t                         cur_pts;

998 999 1000 1001 1002 1003 1004 1005
	struct circlebuf                audio_input_buffer[MAX_AV_PLANES];
	uint8_t                         *audio_output_buffer[MAX_AV_PLANES];

	/* if a video encoder is paired with an audio encoder, make it start
	 * up at the specific timestamp.  if this is the audio encoder,
	 * wait_for_video makes it wait until it's ready to sync up with
	 * video */
	bool                            wait_for_video;
1006
	bool                            first_received;
1007
	struct obs_encoder              *paired_encoder;
1008
	int64_t                         offset_usec;
1009
	uint64_t                        first_raw_ts;
1010 1011
	uint64_t                        start_ts;

J
jp9000 已提交
1012
	pthread_mutex_t                 outputs_mutex;
1013
	DARRAY(obs_output_t*)            outputs;
J
jp9000 已提交
1014 1015 1016

	bool                            destroy_on_stop;

1017
	/* stores the video/audio media output pointer.  video_t *or audio_t **/
J
jp9000 已提交
1018
	void                            *media;
1019 1020 1021

	pthread_mutex_t                 callbacks_mutex;
	DARRAY(struct encoder_callback) callbacks;
P
Palana 已提交
1022 1023

	const char                      *profile_encoder_encode_name;
J
jp9000 已提交
1024
};
J
jp9000 已提交
1025

1026 1027
extern struct obs_encoder_info *find_encoder(const char *id);

1028
extern bool obs_encoder_initialize(obs_encoder_t *encoder);
1029
extern void obs_encoder_shutdown(obs_encoder_t *encoder);
1030

1031
extern void obs_encoder_start(obs_encoder_t *encoder,
1032 1033
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param);
1034
extern void obs_encoder_stop(obs_encoder_t *encoder,
1035 1036 1037
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param);

J
jp9000 已提交
1038 1039 1040 1041
extern void obs_encoder_add_output(struct obs_encoder *encoder,
		struct obs_output *output);
extern void obs_encoder_remove_output(struct obs_encoder *encoder,
		struct obs_output *output);
1042

1043 1044 1045
extern bool start_gpu_encode(obs_encoder_t *encoder);
extern void stop_gpu_encode(obs_encoder_t *encoder);

J
jp9000 已提交
1046 1047 1048 1049
extern void do_encode(struct obs_encoder *encoder, struct encoder_frame *frame);
extern void send_off_encoder_packet(obs_encoder_t *encoder, bool success,
		bool received, struct encoder_packet *pkt);

1050 1051
void obs_encoder_destroy(obs_encoder_t *encoder);

1052 1053 1054
/* ------------------------------------------------------------------------- */
/* services */

1055 1056 1057 1058 1059
struct obs_weak_service {
	struct obs_weak_ref ref;
	struct obs_service *service;
};

1060 1061 1062
struct obs_service {
	struct obs_context_data         context;
	struct obs_service_info         info;
1063
	struct obs_weak_service         *control;
1064

1065 1066 1067
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

1068 1069 1070
	bool                            active;
	bool                            destroy;
	struct obs_output               *output;
1071
};
1072

1073 1074
extern const struct obs_service_info *find_service(const char *id);

1075 1076 1077 1078
extern void obs_service_activate(struct obs_service *service);
extern void obs_service_deactivate(struct obs_service *service, bool remove);
extern bool obs_service_initialize(struct obs_service *service,
		struct obs_output *output);
1079 1080 1081

void obs_service_destroy(obs_service_t *service);