obs-internal.h 25.8 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 31

#include "graphics/graphics.h"

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

#include "obs.h"
37

J
jp9000 已提交
38
#define NUM_TEXTURES 2
39
#define MICROSECOND_DEN 1000000
J
jp9000 已提交
40

41 42 43 44
static inline int64_t packet_dts_usec(struct encoder_packet *packet)
{
	return packet->dts * MICROSECOND_DEN / packet->timebase_den;
}
45

46 47 48 49 50
struct draw_callback {
	void (*draw)(void *param, uint32_t cx, uint32_t cy);
	void *param;
};

51 52 53 54 55 56 57
/* ------------------------------------------------------------------------- */
/* validity checks */

static inline bool obs_object_valid(const void *obj, const char *f,
		const char *t)
{
	if (!obj) {
58
		blog(LOG_DEBUG, "%s: Null '%s' parameter", f, t);
59 60 61 62 63 64
		return false;
	}

	return true;
}

65 66 67 68 69
#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
70

J
jp9000 已提交
71
/* ------------------------------------------------------------------------- */
72
/* modules */
J
jp9000 已提交
73 74

struct obs_module {
75
	char *mod_name;
J
jp9000 已提交
76 77 78
	const char *file;
	char *bin_path;
	char *data_path;
J
jp9000 已提交
79
	void *module;
J
jp9000 已提交
80 81
	bool loaded;

82
	bool        (*load)(void);
J
jp9000 已提交
83 84 85 86
	void        (*unload)(void);
	void        (*set_locale)(const char *locale);
	void        (*free_locale)(void);
	uint32_t    (*ver)(void);
87
	void        (*set_pointer)(obs_module_t *module);
J
jp9000 已提交
88 89 90 91 92
	const char *(*name)(void);
	const char *(*description)(void);
	const char *(*author)(void);

	struct obs_module *next;
J
jp9000 已提交
93 94
};

J
jp9000 已提交
95 96
extern void free_module(struct obs_module *mod);

J
jp9000 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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 已提交
119

P
Palana 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/* ------------------------------------------------------------------------- */
/* 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];
	bool                        pressed0 : 1;
	bool                        pressed1 : 1;
	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;
	bool                        pressed : 1;
	bool                        modifiers_match : 1;

	obs_hotkey_id               hotkey_id;
	obs_hotkey_t                *hotkey;
};

struct obs_hotkey_name_map;
void obs_hotkey_name_map_free(void);


179
/* ------------------------------------------------------------------------- */
J
jp9000 已提交
180
/* views */
181

J
jp9000 已提交
182
struct obs_view {
183
	pthread_mutex_t                 channels_mutex;
184
	obs_source_t                    *channels[MAX_CHANNELS];
185 186
};

J
jp9000 已提交
187 188
extern bool obs_view_init(struct obs_view *view);
extern void obs_view_free(struct obs_view *view);
189 190 191 192 193 194 195


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

struct obs_display {
	bool                            size_changed;
J
jp9000 已提交
196
	bool                            enabled;
197
	uint32_t                        cx, cy;
198
	uint32_t                        background_color;
199
	gs_swapchain_t                  *swap;
200 201
	pthread_mutex_t                 draw_callbacks_mutex;
	DARRAY(struct draw_callback)    draw_callbacks;
202 203 204

	struct obs_display              *next;
	struct obs_display              **prev_next;
205 206 207
};

extern bool obs_display_init(struct obs_display *display,
208
		const struct gs_init_data *graphics_data);
209 210 211
extern void obs_display_free(struct obs_display *display);


212
/* ------------------------------------------------------------------------- */
J
jp9000 已提交
213
/* core */
214

215 216 217 218 219
struct obs_vframe_info {
	uint64_t timestamp;
	int count;
};

220
struct obs_core_video {
221 222 223 224 225
	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];
226 227 228
	bool                            textures_rendered[NUM_TEXTURES];
	bool                            textures_output[NUM_TEXTURES];
	bool                            textures_copied[NUM_TEXTURES];
J
jp9000 已提交
229
	bool                            textures_converted[NUM_TEXTURES];
230
	struct circlebuf                vframe_info_buffer;
231
	gs_effect_t                     *default_effect;
P
Palana 已提交
232
	gs_effect_t                     *default_rect_effect;
233
	gs_effect_t                     *opaque_effect;
234 235
	gs_effect_t                     *solid_effect;
	gs_effect_t                     *conversion_effect;
236 237
	gs_effect_t                     *bicubic_effect;
	gs_effect_t                     *lanczos_effect;
238
	gs_effect_t                     *bilinear_lowres_effect;
239
	gs_stagesurf_t                  *mapped_surface;
240 241
	int                             cur_texture;

J
jp9000 已提交
242
	uint64_t                        video_time;
243
	video_t                         *video;
244 245 246
	pthread_t                       video_thread;
	bool                            thread_initialized;

J
jp9000 已提交
247 248 249 250 251 252 253 254 255
	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;
256 257
	uint32_t                        base_width;
	uint32_t                        base_height;
258
	float                           color_matrix[16];
259
	enum obs_scale_type             scale_type;
260 261

	gs_texture_t                    *transparent_texture;
262 263
};

264
struct obs_core_audio {
J
jp9000 已提交
265
	/* TODO: sound output subsystem */
266
	audio_t                         *audio;
J
jp9000 已提交
267 268 269

	float                           user_volume;
	float                           present_volume;
270 271
};

272
/* user sources, output channels, and displays */
273
struct obs_core_data {
274
	struct obs_source               *first_source;
275
	struct obs_source               *first_audio_source;
276 277 278 279
	struct obs_display              *first_display;
	struct obs_output               *first_output;
	struct obs_encoder              *first_encoder;
	struct obs_service              *first_service;
280 281 282 283 284

	pthread_mutex_t                 sources_mutex;
	pthread_mutex_t                 displays_mutex;
	pthread_mutex_t                 outputs_mutex;
	pthread_mutex_t                 encoders_mutex;
285
	pthread_mutex_t                 services_mutex;
286
	pthread_mutex_t                 audio_sources_mutex;
287

J
jp9000 已提交
288
	struct obs_view                 main_view;
289

J
jp9000 已提交
290 291
	long long                       unnamed_index;

292
	volatile bool                   valid;
293 294
};

P
Palana 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/* 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;
	bool                            thread_disable_press : 1;
	bool                            strict_modifiers : 1;
	bool                            reroute_hotkeys : 1;
	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 已提交
322 323 324 325
	char                            *mute;
	char                            *unmute;
	char                            *push_to_mute;
	char                            *push_to_talk;
P
Palana 已提交
326 327
	char                            *sceneitem_show;
	char                            *sceneitem_hide;
P
Palana 已提交
328 329
};

330
struct obs_core {
J
jp9000 已提交
331 332 333
	struct obs_module               *first_module;
	DARRAY(struct obs_module_path)  module_paths;

J
jp9000 已提交
334 335 336 337 338 339 340 341 342
	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;

343 344
	signal_handler_t                *signals;
	proc_handler_t                  *procs;
345

346
	char                            *locale;
347
	char                            *module_config_path;
348 349
	bool                            name_store_owned;
	profiler_name_store_t           *name_store;
350

351 352
	/* segmented into multiple sub-structures to keep things a bit more
	 * clean and organized */
J
jp9000 已提交
353 354 355
	struct obs_core_video           video;
	struct obs_core_audio           audio;
	struct obs_core_data            data;
P
Palana 已提交
356
	struct obs_core_hotkeys         hotkeys;
J
jp9000 已提交
357 358
};

359
extern struct obs_core *obs;
360

J
jp9000 已提交
361
extern void *obs_video_thread(void *param);
J
jp9000 已提交
362 363 364


/* ------------------------------------------------------------------------- */
365
/* obs shared context data */
J
jp9000 已提交
366

367 368
struct obs_context_data {
	char                            *name;
369
	void                            *data;
370 371 372
	obs_data_t                      *settings;
	signal_handler_t                *signals;
	proc_handler_t                  *procs;
J
jp9000 已提交
373

P
Palana 已提交
374 375 376 377
	DARRAY(obs_hotkey_id)           hotkeys;
	DARRAY(obs_hotkey_pair_id)      hotkey_pairs;
	obs_data_t                      *hotkey_data;

J
jp9000 已提交
378 379 380
	DARRAY(char*)                   rename_cache;
	pthread_mutex_t                 rename_cache_mutex;

381 382 383 384 385 386 387
	pthread_mutex_t                 *mutex;
	struct obs_context_data         *next;
	struct obs_context_data         **prev_next;
};

extern bool obs_context_data_init(
		struct obs_context_data *context,
388
		obs_data_t              *settings,
P
Palana 已提交
389 390
		const char              *name,
		obs_data_t              *hotkey_data);
391 392 393 394 395 396 397 398 399 400
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 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/* ------------------------------------------------------------------------- */
/* 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;
}


443 444 445
/* ------------------------------------------------------------------------- */
/* sources  */

446 447
struct async_frame {
	struct obs_source_frame *frame;
448
	long unused_count;
449 450 451
	bool used;
};

452 453 454 455 456
struct obs_weak_source {
	struct obs_weak_ref ref;
	struct obs_source *source;
};

457 458 459
struct obs_source {
	struct obs_context_data         context;
	struct obs_source_info          info;
460
	struct obs_weak_source          *control;
461

J
jp9000 已提交
462 463
	/* general exposed flags that can be set for the source */
	uint32_t                        flags;
464
	uint32_t                        default_flags;
J
jp9000 已提交
465

466 467 468
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

469
	/* signals to call the source update in the video thread */
470 471
	bool                            defer_update;

472
	/* ensures show/hide are only called once */
J
jp9000 已提交
473
	volatile long                   show_refs;
474

475
	/* ensures activate/deactivate are only called once */
J
jp9000 已提交
476
	volatile long                   activate_refs;
477

J
jp9000 已提交
478 479 480
	/* 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) */
481
	bool                            removed;
J
jp9000 已提交
482

483 484 485
	bool                            active;
	bool                            showing;

486 487 488
	/* used to temporarily disable sources if needed */
	bool                            enabled;

J
jp9000 已提交
489
	/* timing (if video is present, is based upon video) */
490 491 492 493 494
	volatile bool                   timing_set;
	volatile uint64_t               timing_adjust;
	uint64_t                        next_audio_ts_min;
	uint64_t                        last_frame_ts;
	uint64_t                        last_sys_timestamp;
495
	bool                            async_rendered;
J
jp9000 已提交
496 497

	/* audio */
498
	bool                            audio_failed;
J
jp9000 已提交
499
	bool                            muted;
500 501
	struct obs_source               *next_audio_source;
	struct obs_source               **prev_next_audio_source;
502
	struct resample_info            sample_info;
503 504
	audio_resampler_t               *resampler;
	audio_line_t                    *audio_line;
505
	pthread_mutex_t                 audio_mutex;
506
	struct obs_audio_data           audio_data;
507
	size_t                          audio_storage_size;
508
	float                           base_volume;
J
jp9000 已提交
509 510
	float                           user_volume;
	float                           present_volume;
J
jp9000 已提交
511
	int64_t                         sync_offset;
J
jp9000 已提交
512 513

	/* async video data */
514 515
	gs_texture_t                    *async_texture;
	gs_texrender_t                  *async_convert_texrender;
516
	struct obs_source_frame         *cur_async_frame;
517
	bool                            async_gpu_conversion;
518
	enum video_format               async_format;
J
jp9000 已提交
519
	enum video_format               async_cache_format;
520
	enum gs_color_format            async_texture_format;
521
	float                           async_color_matrix[16];
522 523 524
	bool                            async_full_range;
	float                           async_color_range_min[3];
	float                           async_color_range_max[3];
525
	int                             async_plane_offset[2];
526
	bool                            async_flip;
527
	bool                            async_active;
528
	DARRAY(struct async_frame)      async_cache;
529 530
	DARRAY(struct obs_source_frame*)async_frames;
	pthread_mutex_t                 async_mutex;
531 532
	uint32_t                        async_width;
	uint32_t                        async_height;
J
jp9000 已提交
533 534
	uint32_t                        async_cache_width;
	uint32_t                        async_cache_height;
535 536
	uint32_t                        async_convert_width;
	uint32_t                        async_convert_height;
J
jp9000 已提交
537 538

	/* filters */
539 540 541 542
	struct obs_source               *filter_parent;
	struct obs_source               *filter_target;
	DARRAY(struct obs_source*)      filters;
	pthread_mutex_t                 filter_mutex;
543
	gs_texrender_t                  *filter_texrender;
544
	enum obs_allow_direct_render    allow_direct;
545
	bool                            rendering_filter;
P
Palana 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558

	/* sources specific hotkeys */
	obs_hotkey_pair_id              mute_unmute_key;
	obs_hotkey_id                   push_to_mute_key;
	obs_hotkey_id                   push_to_talk_key;
	bool                            push_to_mute_enabled : 1;
	bool                            push_to_mute_pressed : 1;
	bool                            push_to_talk_enabled : 1;
	bool                            push_to_talk_pressed : 1;
	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 已提交
559 560
};

561 562
extern const struct obs_source_info *find_source(struct darray *list,
		const char *id);
563
extern bool obs_source_init_context(struct obs_source *source,
564 565
		obs_data_t *settings, const char *name,
		obs_data_t *hotkey_data);
J
jp9000 已提交
566 567 568
extern bool obs_source_init(struct obs_source *source,
		const struct obs_source_info *info);

569 570 571
extern void obs_source_save(obs_source_t *source);
extern void obs_source_load(obs_source_t *source);

572 573
extern void obs_source_destroy(struct obs_source *source);

574 575 576 577 578
enum view_type {
	MAIN_VIEW,
	AUX_VIEW
};

579 580 581
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);
582 583
extern float obs_source_get_target_volume(obs_source_t *source,
		obs_source_t *target);
J
jp9000 已提交
584 585 586 587 588


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

589 590 591 592 593 594 595 596 597 598 599 600
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;
};

601 602
typedef void (*encoded_callback_t)(void *data, struct encoder_packet *packet);

603 604 605 606 607
struct obs_weak_output {
	struct obs_weak_ref ref;
	struct obs_output *output;
};

J
jp9000 已提交
608
struct obs_output {
609
	struct obs_context_data         context;
610
	struct obs_output_info          info;
611
	struct obs_weak_output          *control;
612

613 614 615
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

616 617 618
	bool                            received_video;
	bool                            received_audio;
	int64_t                         video_offset;
619
	int64_t                         audio_offsets[MAX_AUDIO_MIXES];
J
jp9000 已提交
620 621
	int64_t                         highest_audio_ts;
	int64_t                         highest_video_ts;
J
jp9000 已提交
622
	pthread_mutex_t                 interleaved_mutex;
623
	DARRAY(struct encoder_packet)   interleaved_packets;
J
jp9000 已提交
624

J
jp9000 已提交
625 626 627
	int                             reconnect_retry_sec;
	int                             reconnect_retry_max;
	int                             reconnect_retries;
628
	int                             reconnect_retry_cur_sec;
J
jp9000 已提交
629 630
	bool                            reconnecting;
	pthread_t                       reconnect_thread;
631
	os_event_t                      *reconnect_stop_event;
J
jp9000 已提交
632 633
	volatile bool                   reconnect_thread_active;

J
jp9000 已提交
634 635 636
	uint32_t                        starting_frame_count;
	uint32_t                        starting_skipped_frame_count;

637 638
	int                             total_frames;

J
jp9000 已提交
639
	bool                            active;
640
	volatile bool                   stopped;
641 642 643
	video_t                         *video;
	audio_t                         *audio;
	obs_encoder_t                   *video_encoder;
644
	obs_encoder_t                   *audio_encoders[MAX_AUDIO_MIXES];
645
	obs_service_t                   *service;
646
	size_t                          mixer_idx;
J
jp9000 已提交
647

648 649 650
	uint32_t                        scaled_width;
	uint32_t                        scaled_height;

J
jp9000 已提交
651 652 653 654 655
	bool                            video_conversion_set;
	bool                            audio_conversion_set;
	struct video_scale_info         video_conversion;
	struct audio_convert_info       audio_conversion;

656
	bool                            valid;
657 658 659 660 661 662 663 664 665 666 667

	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;
	bool                            delay_active;
	bool                            delay_capturing;
J
jp9000 已提交
668 669
};

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
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);
extern void obs_output_actual_stop(obs_output_t *output, bool force);

686 687
extern const struct obs_output_info *find_output(const char *id);

J
jp9000 已提交
688 689 690
extern void obs_output_remove_encoder(struct obs_output *output,
		struct obs_encoder *encoder);

691 692
void obs_output_destroy(obs_output_t *output);

J
jp9000 已提交
693 694 695 696

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

697 698 699 700 701
struct obs_weak_encoder {
	struct obs_weak_ref ref;
	struct obs_encoder *encoder;
};

702
struct encoder_callback {
703
	bool sent_first_packet;
J
jp9000 已提交
704 705 706 707 708
	void (*new_packet)(void *param, struct encoder_packet *packet);
	void *param;
};

struct obs_encoder {
709
	struct obs_context_data         context;
710
	struct obs_encoder_info         info;
711
	struct obs_weak_encoder         *control;
J
jp9000 已提交
712

J
jp9000 已提交
713 714
	pthread_mutex_t                 init_mutex;

715 716 717 718 719 720
	uint32_t                        samplerate;
	size_t                          planes;
	size_t                          blocksize;
	size_t                          framesize;
	size_t                          framesize_bytes;

721 722
	size_t                          mixer_idx;

723 724
	uint32_t                        scaled_width;
	uint32_t                        scaled_height;
725
	enum video_format               preferred_format;
726

J
jp9000 已提交
727 728
	volatile bool                   active;
	bool                            initialized;
J
jp9000 已提交
729

730 731 732
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

733 734 735 736 737
	uint32_t                        timebase_num;
	uint32_t                        timebase_den;

	int64_t                         cur_pts;

738 739 740 741 742 743 744 745
	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;
746
	bool                            first_received;
747
	struct obs_encoder              *paired_encoder;
748
	int64_t                         offset_usec;
749 750
	uint64_t                        start_ts;

J
jp9000 已提交
751
	pthread_mutex_t                 outputs_mutex;
752
	DARRAY(obs_output_t*)            outputs;
J
jp9000 已提交
753 754 755

	bool                            destroy_on_stop;

756
	/* stores the video/audio media output pointer.  video_t *or audio_t **/
J
jp9000 已提交
757
	void                            *media;
758 759 760

	pthread_mutex_t                 callbacks_mutex;
	DARRAY(struct encoder_callback) callbacks;
P
Palana 已提交
761 762

	const char                      *profile_encoder_encode_name;
J
jp9000 已提交
763
};
J
jp9000 已提交
764

765 766
extern struct obs_encoder_info *find_encoder(const char *id);

767
extern bool obs_encoder_initialize(obs_encoder_t *encoder);
768
extern void obs_encoder_shutdown(obs_encoder_t *encoder);
769

770
extern void obs_encoder_start(obs_encoder_t *encoder,
771 772
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param);
773
extern void obs_encoder_stop(obs_encoder_t *encoder,
774 775 776
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param);

J
jp9000 已提交
777 778 779 780
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);
781

782 783
void obs_encoder_destroy(obs_encoder_t *encoder);

784 785 786
/* ------------------------------------------------------------------------- */
/* services */

787 788 789 790 791
struct obs_weak_service {
	struct obs_weak_ref ref;
	struct obs_service *service;
};

792 793 794
struct obs_service {
	struct obs_context_data         context;
	struct obs_service_info         info;
795
	struct obs_weak_service         *control;
796

797 798 799
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

800 801 802
	bool                            active;
	bool                            destroy;
	struct obs_output               *output;
803
};
804

805 806
extern const struct obs_service_info *find_service(const char *id);

807 808 809 810
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);
811 812 813

void obs_service_destroy(obs_service_t *service);