obs-internal.h 18.6 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"
J
jp9000 已提交
26 27
#include "callback/signal.h"
#include "callback/proc.h"
J
jp9000 已提交
28 29 30

#include "graphics/graphics.h"

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

#include "obs.h"
36

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

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

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

J
jp9000 已提交
50
/* ------------------------------------------------------------------------- */
51
/* modules */
J
jp9000 已提交
52 53

struct obs_module {
J
jp9000 已提交
54 55 56
	const char *file;
	char *bin_path;
	char *data_path;
J
jp9000 已提交
57
	void *module;
J
jp9000 已提交
58 59
	bool loaded;

60
	bool        (*load)(void);
J
jp9000 已提交
61 62 63 64
	void        (*unload)(void);
	void        (*set_locale)(const char *locale);
	void        (*free_locale)(void);
	uint32_t    (*ver)(void);
65
	void        (*set_pointer)(obs_module_t *module);
J
jp9000 已提交
66 67 68 69 70
	const char *(*name)(void);
	const char *(*description)(void);
	const char *(*author)(void);

	struct obs_module *next;
J
jp9000 已提交
71 72
};

J
jp9000 已提交
73 74
extern void free_module(struct obs_module *mod);

J
jp9000 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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 已提交
97

98
/* ------------------------------------------------------------------------- */
J
jp9000 已提交
99
/* views */
100

J
jp9000 已提交
101
struct obs_view {
102
	pthread_mutex_t                 channels_mutex;
103
	obs_source_t                    *channels[MAX_CHANNELS];
104 105
};

J
jp9000 已提交
106 107
extern bool obs_view_init(struct obs_view *view);
extern void obs_view_free(struct obs_view *view);
108 109 110 111 112 113 114


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

struct obs_display {
	bool                            size_changed;
J
jp9000 已提交
115
	bool                            enabled;
116
	uint32_t                        cx, cy;
117
	uint32_t                        background_color;
118
	gs_swapchain_t                  *swap;
119 120
	pthread_mutex_t                 draw_callbacks_mutex;
	DARRAY(struct draw_callback)    draw_callbacks;
121 122 123

	struct obs_display              *next;
	struct obs_display              **prev_next;
124 125 126
};

extern bool obs_display_init(struct obs_display *display,
127
		const struct gs_init_data *graphics_data);
128 129 130
extern void obs_display_free(struct obs_display *display);


131
/* ------------------------------------------------------------------------- */
J
jp9000 已提交
132
/* core */
133

134 135 136 137 138
struct obs_vframe_info {
	uint64_t timestamp;
	int count;
};

139
struct obs_core_video {
140 141 142 143 144
	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];
145 146 147
	bool                            textures_rendered[NUM_TEXTURES];
	bool                            textures_output[NUM_TEXTURES];
	bool                            textures_copied[NUM_TEXTURES];
J
jp9000 已提交
148
	bool                            textures_converted[NUM_TEXTURES];
149
	struct circlebuf                vframe_info_buffer;
150
	gs_effect_t                     *default_effect;
P
Palana 已提交
151
	gs_effect_t                     *default_rect_effect;
152
	gs_effect_t                     *opaque_effect;
153 154
	gs_effect_t                     *solid_effect;
	gs_effect_t                     *conversion_effect;
155 156
	gs_effect_t                     *bicubic_effect;
	gs_effect_t                     *lanczos_effect;
157
	gs_effect_t                     *bilinear_lowres_effect;
158
	gs_stagesurf_t                  *mapped_surface;
159 160
	int                             cur_texture;

161
	video_t                         *video;
162 163 164
	pthread_t                       video_thread;
	bool                            thread_initialized;

J
jp9000 已提交
165 166 167 168 169 170 171 172 173
	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;
174 175
	uint32_t                        base_width;
	uint32_t                        base_height;
176
	float                           color_matrix[16];
177
	enum obs_scale_type             scale_type;
178 179

	struct obs_display              main_display;
180 181
};

182
struct obs_core_audio {
J
jp9000 已提交
183
	/* TODO: sound output subsystem */
184
	audio_t                         *audio;
J
jp9000 已提交
185 186 187

	float                           user_volume;
	float                           present_volume;
188 189
};

190
/* user sources, output channels, and displays */
191
struct obs_core_data {
192 193 194 195 196 197 198 199
	pthread_mutex_t                 user_sources_mutex;
	DARRAY(struct obs_source*)      user_sources;

	struct obs_source               *first_source;
	struct obs_display              *first_display;
	struct obs_output               *first_output;
	struct obs_encoder              *first_encoder;
	struct obs_service              *first_service;
200 201 202 203 204

	pthread_mutex_t                 sources_mutex;
	pthread_mutex_t                 displays_mutex;
	pthread_mutex_t                 outputs_mutex;
	pthread_mutex_t                 encoders_mutex;
205
	pthread_mutex_t                 services_mutex;
206

J
jp9000 已提交
207
	struct obs_view                 main_view;
208

J
jp9000 已提交
209 210
	volatile long                   active_transitions;

J
jp9000 已提交
211 212
	long long                       unnamed_index;

213
	volatile bool                   valid;
214 215
};

216
struct obs_core {
J
jp9000 已提交
217 218 219
	struct obs_module               *first_module;
	DARRAY(struct obs_module_path)  module_paths;

J
jp9000 已提交
220 221 222 223 224 225 226 227 228
	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;

229 230
	signal_handler_t                *signals;
	proc_handler_t                  *procs;
231

232 233
	char                            *locale;

234 235
	/* segmented into multiple sub-structures to keep things a bit more
	 * clean and organized */
J
jp9000 已提交
236 237 238
	struct obs_core_video           video;
	struct obs_core_audio           audio;
	struct obs_core_data            data;
J
jp9000 已提交
239 240
};

241
extern struct obs_core *obs;
242

J
jp9000 已提交
243
extern void *obs_video_thread(void *param);
J
jp9000 已提交
244 245 246


/* ------------------------------------------------------------------------- */
247
/* obs shared context data */
J
jp9000 已提交
248

249 250
struct obs_context_data {
	char                            *name;
251
	void                            *data;
252 253 254
	obs_data_t                      *settings;
	signal_handler_t                *signals;
	proc_handler_t                  *procs;
J
jp9000 已提交
255

J
jp9000 已提交
256 257 258
	DARRAY(char*)                   rename_cache;
	pthread_mutex_t                 rename_cache_mutex;

259 260 261 262 263 264 265
	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,
266
		obs_data_t              *settings,
267 268 269 270 271 272 273 274 275 276 277
		const char              *name);
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 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 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
/* ------------------------------------------------------------------------- */
/* 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;
}


320 321 322
/* ------------------------------------------------------------------------- */
/* sources  */

323 324
struct async_frame {
	struct obs_source_frame *frame;
325
	long unused_count;
326 327 328
	bool used;
};

329 330 331 332 333
struct obs_source {
	struct obs_context_data         context;
	struct obs_source_info          info;
	volatile long                   refs;

J
jp9000 已提交
334 335
	/* general exposed flags that can be set for the source */
	uint32_t                        flags;
336
	uint32_t                        default_flags;
J
jp9000 已提交
337

338 339 340
	/* indicates ownership of the info.id buffer */
	bool                            owns_info_id;

341
	/* signals to call the source update in the video thread */
342 343
	bool                            defer_update;

344
	/* ensures show/hide are only called once */
J
jp9000 已提交
345
	volatile long                   show_refs;
346

347
	/* ensures activate/deactivate are only called once */
J
jp9000 已提交
348
	volatile long                   activate_refs;
349

J
jp9000 已提交
350 351 352
	/* 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) */
353
	bool                            removed;
J
jp9000 已提交
354

355 356 357
	bool                            active;
	bool                            showing;

358 359 360
	/* used to temporarily disable sources if needed */
	bool                            enabled;

J
jp9000 已提交
361
	/* timing (if video is present, is based upon video) */
362 363 364 365 366
	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;
367
	bool                            async_rendered;
J
jp9000 已提交
368 369

	/* audio */
370
	bool                            audio_failed;
J
jp9000 已提交
371
	bool                            muted;
372
	struct resample_info            sample_info;
373 374
	audio_resampler_t               *resampler;
	audio_line_t                    *audio_line;
375
	pthread_mutex_t                 audio_mutex;
376
	struct obs_audio_data           audio_data;
377
	size_t                          audio_storage_size;
378
	float                           base_volume;
J
jp9000 已提交
379 380
	float                           user_volume;
	float                           present_volume;
J
jp9000 已提交
381
	int64_t                         sync_offset;
J
jp9000 已提交
382 383

	/* async video data */
384 385
	gs_texture_t                    *async_texture;
	gs_texrender_t                  *async_convert_texrender;
386
	struct obs_source_frame         *cur_async_frame;
387
	bool                            async_gpu_conversion;
388
	enum video_format               async_format;
J
jp9000 已提交
389
	enum video_format               async_cache_format;
390
	enum gs_color_format            async_texture_format;
391
	float                           async_color_matrix[16];
392 393 394
	bool                            async_full_range;
	float                           async_color_range_min[3];
	float                           async_color_range_max[3];
395
	int                             async_plane_offset[2];
396
	bool                            async_flip;
397
	bool                            async_active;
398
	DARRAY(struct async_frame)      async_cache;
399 400
	DARRAY(struct obs_source_frame*)async_frames;
	pthread_mutex_t                 async_mutex;
401 402
	uint32_t                        async_width;
	uint32_t                        async_height;
J
jp9000 已提交
403 404
	uint32_t                        async_cache_width;
	uint32_t                        async_cache_height;
405 406
	uint32_t                        async_convert_width;
	uint32_t                        async_convert_height;
J
jp9000 已提交
407 408

	/* filters */
409 410 411 412
	struct obs_source               *filter_parent;
	struct obs_source               *filter_target;
	DARRAY(struct obs_source*)      filters;
	pthread_mutex_t                 filter_mutex;
413
	gs_texrender_t                  *filter_texrender;
414
	enum obs_allow_direct_render    allow_direct;
415
	bool                            rendering_filter;
J
jp9000 已提交
416 417
};

418 419
extern const struct obs_source_info *find_source(struct darray *list,
		const char *id);
420
extern bool obs_source_init_context(struct obs_source *source,
421
		obs_data_t *settings, const char *name);
J
jp9000 已提交
422 423 424
extern bool obs_source_init(struct obs_source *source,
		const struct obs_source_info *info);

425 426
extern void obs_source_destroy(struct obs_source *source);

427 428 429 430 431
enum view_type {
	MAIN_VIEW,
	AUX_VIEW
};

432 433 434
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);
435 436
extern float obs_source_get_target_volume(obs_source_t *source,
		obs_source_t *target);
J
jp9000 已提交
437 438 439 440 441 442


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

struct obs_output {
443
	struct obs_context_data         context;
444
	struct obs_output_info          info;
445

446 447 448
	bool                            received_video;
	bool                            received_audio;
	int64_t                         video_offset;
449
	int64_t                         audio_offsets[MAX_AUDIO_MIXES];
J
jp9000 已提交
450 451
	int64_t                         highest_audio_ts;
	int64_t                         highest_video_ts;
J
jp9000 已提交
452
	pthread_mutex_t                 interleaved_mutex;
453
	DARRAY(struct encoder_packet)   interleaved_packets;
J
jp9000 已提交
454

J
jp9000 已提交
455 456 457 458 459
	int                             reconnect_retry_sec;
	int                             reconnect_retry_max;
	int                             reconnect_retries;
	bool                            reconnecting;
	pthread_t                       reconnect_thread;
460
	os_event_t                      *reconnect_stop_event;
J
jp9000 已提交
461 462
	volatile bool                   reconnect_thread_active;

J
jp9000 已提交
463 464 465
	uint32_t                        starting_frame_count;
	uint32_t                        starting_skipped_frame_count;

466 467
	int                             total_frames;

J
jp9000 已提交
468
	bool                            active;
469
	volatile bool                   stopped;
470 471 472
	video_t                         *video;
	audio_t                         *audio;
	obs_encoder_t                   *video_encoder;
473
	obs_encoder_t                   *audio_encoders[MAX_AUDIO_MIXES];
474
	obs_service_t                   *service;
475
	size_t                          mixer_idx;
J
jp9000 已提交
476

477 478 479
	uint32_t                        scaled_width;
	uint32_t                        scaled_height;

J
jp9000 已提交
480 481 482 483 484
	bool                            video_conversion_set;
	bool                            audio_conversion_set;
	struct video_scale_info         video_conversion;
	struct audio_convert_info       audio_conversion;

485
	bool                            valid;
J
jp9000 已提交
486 487
};

488 489
extern const struct obs_output_info *find_output(const char *id);

J
jp9000 已提交
490 491 492
extern void obs_output_remove_encoder(struct obs_output *output,
		struct obs_encoder *encoder);

J
jp9000 已提交
493 494 495 496

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

497
struct encoder_callback {
498
	bool sent_first_packet;
J
jp9000 已提交
499 500 501 502 503
	void (*new_packet)(void *param, struct encoder_packet *packet);
	void *param;
};

struct obs_encoder {
504
	struct obs_context_data         context;
505
	struct obs_encoder_info         info;
J
jp9000 已提交
506

507 508 509 510 511 512
	uint32_t                        samplerate;
	size_t                          planes;
	size_t                          blocksize;
	size_t                          framesize;
	size_t                          framesize_bytes;

513 514
	size_t                          mixer_idx;

515 516
	uint32_t                        scaled_width;
	uint32_t                        scaled_height;
517
	enum video_format               preferred_format;
518

J
jp9000 已提交
519 520
	bool                            active;

521 522 523 524 525
	uint32_t                        timebase_num;
	uint32_t                        timebase_den;

	int64_t                         cur_pts;

526 527 528 529 530 531 532 533 534 535 536
	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;
	struct obs_encoder              *paired_encoder;
	uint64_t                        start_ts;

J
jp9000 已提交
537
	pthread_mutex_t                 outputs_mutex;
538
	DARRAY(obs_output_t*)            outputs;
J
jp9000 已提交
539 540 541

	bool                            destroy_on_stop;

542
	/* stores the video/audio media output pointer.  video_t *or audio_t **/
J
jp9000 已提交
543
	void                            *media;
544 545 546

	pthread_mutex_t                 callbacks_mutex;
	DARRAY(struct encoder_callback) callbacks;
J
jp9000 已提交
547
};
J
jp9000 已提交
548

549 550
extern struct obs_encoder_info *find_encoder(const char *id);

551
extern bool obs_encoder_initialize(obs_encoder_t *encoder);
552

553
extern void obs_encoder_start(obs_encoder_t *encoder,
554 555
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param);
556
extern void obs_encoder_stop(obs_encoder_t *encoder,
557 558 559
		void (*new_packet)(void *param, struct encoder_packet *packet),
		void *param);

J
jp9000 已提交
560 561 562 563
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);
564 565 566 567 568 569 570

/* ------------------------------------------------------------------------- */
/* services */

struct obs_service {
	struct obs_context_data         context;
	struct obs_service_info         info;
571 572 573 574

	bool                            active;
	bool                            destroy;
	struct obs_output               *output;
575
};
576

577 578
extern const struct obs_service_info *find_service(const char *id);

579 580 581 582
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);