obs.c 37.8 KB
Newer Older
J
jp9000 已提交
1
/******************************************************************************
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 19
#include <inttypes.h>

20
#include "graphics/matrix4.h"
21 22
#include "callback/calldata.h"

J
jp9000 已提交
23
#include "obs.h"
J
jp9000 已提交
24
#include "obs-internal.h"
J
jp9000 已提交
25

26
struct obs_core *obs = NULL;
27

J
jp9000 已提交
28
extern void add_default_module_paths(void);
J
jp9000 已提交
29 30
extern char *find_libobs_data_file(const char *file);

31
static inline void make_gs_init_data(struct gs_init_data *gid,
32
		const struct obs_video_info *ovi)
J
jp9000 已提交
33
{
34
	memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
35 36
	gid->cx              = ovi->window_width;
	gid->cy              = ovi->window_height;
37 38 39 40 41 42
	gid->num_backbuffers = 2;
	gid->format          = GS_RGBA;
	gid->zsformat        = GS_ZS_NONE;
	gid->adapter         = ovi->adapter;
}

J
jp9000 已提交
43
static inline void make_video_info(struct video_output_info *vi,
44 45 46
		struct obs_video_info *ovi)
{
	vi->name    = "video";
47
	vi->format  = ovi->output_format;
48 49 50 51
	vi->fps_num = ovi->fps_num;
	vi->fps_den = ovi->fps_den;
	vi->width   = ovi->output_width;
	vi->height  = ovi->output_height;
52 53
	vi->range   = ovi->range;
	vi->colorspace = ovi->colorspace;
54
	vi->cache_size = 6;
55 56
}

J
jp9000 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#define PIXEL_SIZE 4

#define GET_ALIGN(val, align) \
	(((val) + (align-1)) & ~(align-1))

static inline void set_420p_sizes(const struct obs_video_info *ovi)
{
	struct obs_core_video *video = &obs->video;
	uint32_t chroma_pixels;
	uint32_t total_bytes;

	chroma_pixels = (ovi->output_width * ovi->output_height / 4);
	chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);

	video->plane_offsets[0] = 0;
	video->plane_offsets[1] = ovi->output_width * ovi->output_height;
	video->plane_offsets[2] = video->plane_offsets[1] + chroma_pixels;

	video->plane_linewidth[0] = ovi->output_width;
	video->plane_linewidth[1] = ovi->output_width/2;
	video->plane_linewidth[2] = ovi->output_width/2;

	video->plane_sizes[0] = video->plane_offsets[1];
	video->plane_sizes[1] = video->plane_sizes[0]/4;
	video->plane_sizes[2] = video->plane_sizes[1];

	total_bytes = video->plane_offsets[2] + chroma_pixels;

	video->conversion_height =
		(total_bytes/PIXEL_SIZE + ovi->output_width-1) /
		ovi->output_width;

	video->conversion_height = GET_ALIGN(video->conversion_height, 2);
	video->conversion_tech = "Planar420";
}

P
Palana 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
static inline void set_nv12_sizes(const struct obs_video_info *ovi)
{
	struct obs_core_video *video = &obs->video;
	uint32_t chroma_pixels;
	uint32_t total_bytes;

	chroma_pixels = (ovi->output_width * ovi->output_height / 2);
	chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);

	video->plane_offsets[0] = 0;
	video->plane_offsets[1] = ovi->output_width * ovi->output_height;

	video->plane_linewidth[0] = ovi->output_width;
	video->plane_linewidth[1] = ovi->output_width;

	video->plane_sizes[0] = video->plane_offsets[1];
	video->plane_sizes[1] = video->plane_sizes[0]/2;

	total_bytes = video->plane_offsets[1] + chroma_pixels;

	video->conversion_height =
		(total_bytes/PIXEL_SIZE + ovi->output_width-1) /
		ovi->output_width;

	video->conversion_height = GET_ALIGN(video->conversion_height, 2);
	video->conversion_tech = "NV12";
}

J
jp9000 已提交
121 122 123 124 125 126 127 128 129 130 131
static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
{
	obs->video.conversion_height = 0;
	memset(obs->video.plane_offsets, 0, sizeof(obs->video.plane_offsets));
	memset(obs->video.plane_sizes, 0, sizeof(obs->video.plane_sizes));
	memset(obs->video.plane_linewidth, 0,
		sizeof(obs->video.plane_linewidth));

	switch ((uint32_t)ovi->output_format) {
	case VIDEO_FORMAT_I420:
		set_420p_sizes(ovi);
P
Palana 已提交
132 133 134 135
		break;
	case VIDEO_FORMAT_NV12:
		set_nv12_sizes(ovi);
		break;
J
jp9000 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	}
}

static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
{
	struct obs_core_video *video = &obs->video;

	calc_gpu_conversion_sizes(ovi);

	if (!video->conversion_height) {
		blog(LOG_INFO, "GPU conversion not available for format: %u",
				(unsigned int)ovi->output_format);
		video->gpu_conversion = false;
		return true;
	}

	for (size_t i = 0; i < NUM_TEXTURES; i++) {
153
		video->convert_textures[i] = gs_texture_create(
J
jp9000 已提交
154
				ovi->output_width, video->conversion_height,
155
				GS_RGBA, 1, NULL, GS_RENDER_TARGET);
J
jp9000 已提交
156 157 158 159 160 161 162 163

		if (!video->convert_textures[i])
			return false;
	}

	return true;
}

164 165
static bool obs_init_textures(struct obs_video_info *ovi)
{
166
	struct obs_core_video *video = &obs->video;
J
jp9000 已提交
167 168
	uint32_t output_height = video->gpu_conversion ?
		video->conversion_height : ovi->output_height;
169 170 171
	size_t i;

	for (i = 0; i < NUM_TEXTURES; i++) {
172
		video->copy_surfaces[i] = gs_stagesurface_create(
J
jp9000 已提交
173
				ovi->output_width, output_height, GS_RGBA);
174 175 176 177

		if (!video->copy_surfaces[i])
			return false;

178
		video->render_textures[i] = gs_texture_create(
179
				ovi->base_width, ovi->base_height,
180
				GS_RGBA, 1, NULL, GS_RENDER_TARGET);
181 182 183 184

		if (!video->render_textures[i])
			return false;

185
		video->output_textures[i] = gs_texture_create(
186
				ovi->output_width, ovi->output_height,
187
				GS_RGBA, 1, NULL, GS_RENDER_TARGET);
188 189 190 191 192 193 194 195

		if (!video->output_textures[i])
			return false;
	}

	return true;
}

196
static int obs_init_graphics(struct obs_video_info *ovi)
197
{
198
	struct obs_core_video *video = &obs->video;
199
	struct gs_init_data graphics_data;
200
	bool success = true;
201
	int errorcode;
J
jp9000 已提交
202

203 204 205 206
	make_gs_init_data(&graphics_data, ovi);

	errorcode = gs_create(&video->graphics, ovi->graphics_module,
			&graphics_data);
J
jp9000 已提交
207
	if (errorcode != GS_SUCCESS) {
208 209 210 211 212 213 214 215
		switch (errorcode) {
		case GS_ERROR_MODULE_NOT_FOUND:
			return OBS_VIDEO_MODULE_NOT_FOUND;
		case GS_ERROR_NOT_SUPPORTED:
			return OBS_VIDEO_NOT_SUPPORTED;
		default:
			return OBS_VIDEO_FAIL;
		}
J
jp9000 已提交
216 217
	}

218
	gs_enter_context(video->graphics);
J
jp9000 已提交
219

220
	char *filename = find_libobs_data_file("default.effect");
221
	video->default_effect = gs_effect_create_from_file(filename,
222 223 224
			NULL);
	bfree(filename);

225 226 227 228 229 230
	if (gs_get_device_type() == GS_DEVICE_OPENGL) {
		filename = find_libobs_data_file("default_rect.effect");
		video->default_rect_effect = gs_effect_create_from_file(
				filename, NULL);
		bfree(filename);
	}
P
Palana 已提交
231

232 233 234 235 236
	filename = find_libobs_data_file("opaque.effect");
	video->opaque_effect = gs_effect_create_from_file(filename,
			NULL);
	bfree(filename);

237
	filename = find_libobs_data_file("solid.effect");
238
	video->solid_effect = gs_effect_create_from_file(filename,
239 240 241 242
			NULL);
	bfree(filename);

	filename = find_libobs_data_file("format_conversion.effect");
243
	video->conversion_effect = gs_effect_create_from_file(filename,
244 245 246
			NULL);
	bfree(filename);

247 248 249 250 251 252 253 254 255 256
	filename = find_libobs_data_file("bicubic_scale.effect");
	video->bicubic_effect = gs_effect_create_from_file(filename,
			NULL);
	bfree(filename);

	filename = find_libobs_data_file("lanczos_scale.effect");
	video->lanczos_effect = gs_effect_create_from_file(filename,
			NULL);
	bfree(filename);

257 258 259 260 261
	filename = find_libobs_data_file("bilinear_lowres_scale.effect");
	video->bilinear_lowres_effect = gs_effect_create_from_file(filename,
			NULL);
	bfree(filename);

262 263
	if (!video->default_effect)
		success = false;
264 265 266 267
	if (gs_get_device_type() == GS_DEVICE_OPENGL) {
		if (!video->default_rect_effect)
			success = false;
	}
268 269
	if (!video->opaque_effect)
		success = false;
270 271 272 273
	if (!video->solid_effect)
		success = false;
	if (!video->conversion_effect)
		success = false;
J
jp9000 已提交
274

275
	gs_leave_context();
276
	return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
J
jp9000 已提交
277 278
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
static inline void set_video_matrix(struct obs_core_video *video,
		struct obs_video_info *ovi)
{
	struct matrix4 mat;
	struct vec4 r_row;

	if (format_is_yuv(ovi->output_format)) {
		video_format_get_parameters(ovi->colorspace, ovi->range,
				(float*)&mat, NULL, NULL);
		matrix4_inv(&mat, &mat);

		/* swap R and G */
		r_row = mat.x;
		mat.x = mat.y;
		mat.y = r_row;
	} else {
		matrix4_identity(&mat);
	}

	memcpy(video->color_matrix, &mat, sizeof(float) * 16);
}

301
static int obs_init_video(struct obs_video_info *ovi)
J
jp9000 已提交
302
{
303
	struct obs_core_video *video = &obs->video;
J
jp9000 已提交
304
	struct video_output_info vi;
305 306 307
	int errorcode;

	make_video_info(&vi, ovi);
308 309 310 311 312
	video->base_width     = ovi->base_width;
	video->base_height    = ovi->base_height;
	video->output_width   = ovi->output_width;
	video->output_height  = ovi->output_height;
	video->gpu_conversion = ovi->gpu_conversion;
313
	video->scale_type     = ovi->scale_type;
314

315 316
	set_video_matrix(video, ovi);

J
jp9000 已提交
317
	errorcode = video_output_open(&video->video, &vi);
318 319

	if (errorcode != VIDEO_OUTPUT_SUCCESS) {
320
		if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
321
			blog(LOG_ERROR, "Invalid video parameters specified");
322 323
			return OBS_VIDEO_INVALID_PARAM;
		} else {
324
			blog(LOG_ERROR, "Could not open video output");
325 326
		}
		return OBS_VIDEO_FAIL;
327 328
	}

329
	if (!obs_display_init(&video->main_display, NULL))
330
		return OBS_VIDEO_FAIL;
331 332 333 334

	video->main_display.cx = ovi->window_width;
	video->main_display.cy = ovi->window_height;

335
	gs_enter_context(video->graphics);
336 337

	if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
338
		return OBS_VIDEO_FAIL;
339
	if (!obs_init_textures(ovi))
340
		return OBS_VIDEO_FAIL;
341

342
	gs_leave_context();
343

344 345 346
	errorcode = pthread_create(&video->video_thread, NULL,
			obs_video_thread, obs);
	if (errorcode != 0)
347
		return OBS_VIDEO_FAIL;
J
jp9000 已提交
348

349
	video->thread_initialized = true;
350
	return OBS_VIDEO_SUCCESS;
J
jp9000 已提交
351 352
}

353
static void stop_video(void)
J
jp9000 已提交
354
{
355
	struct obs_core_video *video = &obs->video;
356
	void *thread_retval;
J
jp9000 已提交
357

358 359
	if (video->video) {
		video_output_stop(video->video);
360
		if (video->thread_initialized) {
361
			pthread_join(video->video_thread, &thread_retval);
362 363
			video->thread_initialized = false;
		}
364
	}
365

366 367 368 369 370 371 372 373
}

static void obs_free_video(void)
{
	struct obs_core_video *video = &obs->video;

	if (video->video) {
		obs_display_free(&video->main_display);
374

375
		video_output_close(video->video);
376 377
		video->video = NULL;

378 379
		if (!video->graphics)
			return;
380

381
		gs_enter_context(video->graphics);
382

383
		if (video->mapped_surface) {
384
			gs_stagesurface_unmap(video->mapped_surface);
385 386
			video->mapped_surface = NULL;
		}
387

388
		for (size_t i = 0; i < NUM_TEXTURES; i++) {
389 390 391 392
			gs_stagesurface_destroy(video->copy_surfaces[i]);
			gs_texture_destroy(video->render_textures[i]);
			gs_texture_destroy(video->convert_textures[i]);
			gs_texture_destroy(video->output_textures[i]);
393

J
jp9000 已提交
394 395 396 397
			video->copy_surfaces[i]    = NULL;
			video->render_textures[i]  = NULL;
			video->convert_textures[i] = NULL;
			video->output_textures[i]  = NULL;
398
		}
399

400
		gs_leave_context();
401

402
		circlebuf_free(&video->vframe_info_buffer);
403

404 405 406 407 408 409 410 411 412
		memset(&video->textures_rendered, 0,
				sizeof(video->textures_rendered));
		memset(&video->textures_output, 0,
				sizeof(video->textures_output));
		memset(&video->textures_copied, 0,
				sizeof(video->textures_copied));
		memset(&video->textures_converted, 0,
				sizeof(video->textures_converted));

413 414 415 416 417 418 419 420 421
		video->cur_texture = 0;
	}
}

static void obs_free_graphics(void)
{
	struct obs_core_video *video = &obs->video;

	if (video->graphics) {
422
		gs_enter_context(video->graphics);
423

424
		gs_effect_destroy(video->default_effect);
P
Palana 已提交
425
		gs_effect_destroy(video->default_rect_effect);
426
		gs_effect_destroy(video->opaque_effect);
427 428
		gs_effect_destroy(video->solid_effect);
		gs_effect_destroy(video->conversion_effect);
429 430
		gs_effect_destroy(video->bicubic_effect);
		gs_effect_destroy(video->lanczos_effect);
431
		gs_effect_destroy(video->bilinear_lowres_effect);
432
		video->default_effect = NULL;
433

434
		gs_leave_context();
435 436

		gs_destroy(video->graphics);
437
		video->graphics = NULL;
438
	}
J
jp9000 已提交
439 440
}

J
jp9000 已提交
441
static bool obs_init_audio(struct audio_output_info *ai)
J
jp9000 已提交
442
{
443
	struct obs_core_audio *audio = &obs->audio;
444
	int errorcode;
J
jp9000 已提交
445

446
	/* TODO: sound subsystem */
J
jp9000 已提交
447

J
jp9000 已提交
448 449 450
	audio->user_volume    = 1.0f;
	audio->present_volume = 1.0f;

J
jp9000 已提交
451
	errorcode = audio_output_open(&audio->audio, ai);
452 453
	if (errorcode == AUDIO_OUTPUT_SUCCESS)
		return true;
P
Palana 已提交
454
	else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
455 456 457 458 459 460 461 462 463
		blog(LOG_ERROR, "Invalid audio parameters specified");
	else
		blog(LOG_ERROR, "Could not open audio output");

	return false;
}

static void obs_free_audio(void)
{
464
	struct obs_core_audio *audio = &obs->audio;
465 466 467
	if (audio->audio)
		audio_output_close(audio->audio);

468
	memset(audio, 0, sizeof(struct obs_core_audio));
469 470 471 472
}

static bool obs_init_data(void)
{
473
	struct obs_core_data *data = &obs->data;
474
	pthread_mutexattr_t attr;
475

476 477
	assert(data != NULL);

478 479
	pthread_mutex_init_value(&obs->data.displays_mutex);

480
	if (pthread_mutexattr_init(&attr) != 0)
481
		return false;
482 483
	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
		goto fail;
484 485
	if (pthread_mutex_init(&data->user_sources_mutex, &attr) != 0)
		goto fail;
486 487 488 489
	if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
		goto fail;
	if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
		goto fail;
490 491 492 493
	if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
		goto fail;
	if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
		goto fail;
494 495
	if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
		goto fail;
J
jp9000 已提交
496
	if (!obs_view_init(&data->main_view))
497
		goto fail;
J
jp9000 已提交
498

499
	data->valid = true;
500 501 502

fail:
	pthread_mutexattr_destroy(&attr);
503
	return data->valid;
J
jp9000 已提交
504 505
}

506 507 508 509 510 511 512 513 514 515 516
void obs_main_view_free(struct obs_view *view)
{
	if (!view) return;

	for (size_t i = 0; i < MAX_CHANNELS; i++)
		obs_source_release(view->channels[i]);

	memset(view->channels, 0, sizeof(view->channels));
	pthread_mutex_destroy(&view->channels_mutex);
}

517 518 519 520 521 522 523 524 525 526 527 528
#define FREE_OBS_LINKED_LIST(type) \
	do { \
		int unfreed = 0; \
		while (data->first_ ## type ) { \
			obs_ ## type ## _destroy(data->first_ ## type ); \
			unfreed++; \
		} \
		if (unfreed) \
			blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
					unfreed); \
	} while (false)

529
static void obs_free_data(void)
J
jp9000 已提交
530
{
531
	struct obs_core_data *data = &obs->data;
J
jp9000 已提交
532

533 534
	data->valid = false;

535
	obs_main_view_free(&data->main_view);
536

537
	blog(LOG_INFO, "Freeing OBS context data");
538

539 540 541 542 543 544 545
	if (data->user_sources.num)
		blog(LOG_INFO, "\t%d user source(s) were remaining",
				(int)data->user_sources.num);

	while (data->user_sources.num)
		obs_source_remove(data->user_sources.array[0]);
	da_free(data->user_sources);
546

547 548 549 550 551 552 553
	FREE_OBS_LINKED_LIST(source);
	FREE_OBS_LINKED_LIST(output);
	FREE_OBS_LINKED_LIST(encoder);
	FREE_OBS_LINKED_LIST(display);
	FREE_OBS_LINKED_LIST(service);

	pthread_mutex_destroy(&data->user_sources_mutex);
554 555 556 557
	pthread_mutex_destroy(&data->sources_mutex);
	pthread_mutex_destroy(&data->displays_mutex);
	pthread_mutex_destroy(&data->outputs_mutex);
	pthread_mutex_destroy(&data->encoders_mutex);
558
	pthread_mutex_destroy(&data->services_mutex);
559
}
J
jp9000 已提交
560

561 562 563 564 565 566 567 568 569
static const char *obs_signals[] = {
	"void source_create(ptr source)",
	"void source_destroy(ptr source)",
	"void source_add(ptr source)",
	"void source_remove(ptr source)",
	"void source_activate(ptr source)",
	"void source_deactivate(ptr source)",
	"void source_show(ptr source)",
	"void source_hide(ptr source)",
J
jp9000 已提交
570
	"void source_rename(ptr source, string new_name, string prev_name)",
571
	"void source_volume(ptr source, in out float volume)",
572 573
	"void source_volume_level(ptr source, float level, float magnitude, "
		"float peak)",
574 575 576 577 578 579 580

	"void channel_change(int channel, in out ptr source, ptr prev_source)",
	"void master_volume(in out float volume)",

	NULL
};

581 582 583 584 585 586 587
static inline bool obs_init_handlers(void)
{
	obs->signals = signal_handler_create();
	if (!obs->signals)
		return false;

	obs->procs   = proc_handler_create();
588 589 590 591
	if (!obs->procs)
		return false;

	return signal_handler_add_array(obs->signals, obs_signals);
592 593
}

594 595
extern const struct obs_source_info scene_info;

J
jp9000 已提交
596 597
extern void log_system_info(void);

598
static bool obs_init(const char *locale)
599
{
600
	obs = bzalloc(sizeof(struct obs_core));
601

J
jp9000 已提交
602 603
	log_system_info();

604 605 606 607 608
	if (!obs_init_data())
		return false;
	if (!obs_init_handlers())
		return false;

609
	obs->locale = bstrdup(locale);
610
	obs_register_source(&scene_info);
J
jp9000 已提交
611
	add_default_module_paths();
612
	return true;
J
jp9000 已提交
613 614
}

J
jp9000 已提交
615 616 617 618
#ifdef _WIN32
extern void initialize_crash_handler(void);
#endif

619
bool obs_startup(const char *locale)
J
jp9000 已提交
620
{
621
	bool success;
J
jp9000 已提交
622

623
	if (obs) {
J
jp9000 已提交
624
		blog(LOG_WARNING, "Tried to call obs_startup more than once");
625 626 627
		return false;
	}

J
jp9000 已提交
628 629 630 631
#ifdef _WIN32
	initialize_crash_handler();
#endif

632
	success = obs_init(locale);
633 634 635 636
	if (!success)
		obs_shutdown();

	return success;
J
jp9000 已提交
637 638
}

639
void obs_shutdown(void)
J
jp9000 已提交
640
{
J
jp9000 已提交
641 642
	struct obs_module *module;

J
jp9000 已提交
643 644 645 646 647
	if (!obs)
		return;

	da_free(obs->input_types);
	da_free(obs->filter_types);
648
	da_free(obs->encoder_types);
J
jp9000 已提交
649 650
	da_free(obs->transition_types);
	da_free(obs->output_types);
651
	da_free(obs->service_types);
J
jp9000 已提交
652 653
	da_free(obs->modal_ui_callbacks);
	da_free(obs->modeless_ui_callbacks);
J
jp9000 已提交
654

655 656
	stop_video();

657 658
	obs_free_data();
	obs_free_video();
659
	obs_free_graphics();
660
	obs_free_audio();
661 662
	proc_handler_destroy(obs->procs);
	signal_handler_destroy(obs->signals);
J
jp9000 已提交
663

J
jp9000 已提交
664 665 666 667 668 669 670 671 672 673 674
	module = obs->first_module;
	while (module) {
		struct obs_module *next = module->next;
		free_module(module);
		module = next;
	}
	obs->first_module = NULL;

	for (size_t i = 0; i < obs->module_paths.num; i++)
		free_module_path(obs->module_paths.array+i);
	da_free(obs->module_paths);
J
jp9000 已提交
675

676
	bfree(obs->locale);
J
jp9000 已提交
677
	bfree(obs);
678 679 680
	obs = NULL;
}

681 682 683 684 685
bool obs_initialized(void)
{
	return obs != NULL;
}

686 687 688 689 690
uint32_t obs_get_version(void)
{
	return LIBOBS_API_VER;
}

691 692
void obs_set_locale(const char *locale)
{
J
jp9000 已提交
693
	struct obs_module *module;
694 695 696 697 698 699
	if (!obs)
		return;

	if (obs->locale)
		bfree(obs->locale);
	obs->locale = bstrdup(locale);
700

J
jp9000 已提交
701 702
	module = obs->first_module;
	while (module) {
703 704
		if (module->set_locale)
			module->set_locale(locale);
J
jp9000 已提交
705 706

		module = module->next;
707
	}
708 709 710 711 712 713 714
}

const char *obs_get_locale(void)
{
	return obs ? obs->locale : NULL;
}

715 716 717 718
#define OBS_SIZE_MIN 2
#define OBS_SIZE_MAX (32 * 1024)

static inline bool size_valid(uint32_t width, uint32_t height)
719
{
720 721 722 723 724 725 726
	return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
	        width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
}

int obs_reset_video(struct obs_video_info *ovi)
{
	if (!obs) return OBS_VIDEO_FAIL;
727 728 729

	/* don't allow changing of video settings if active. */
	if (obs->video.video && video_output_active(obs->video.video))
730 731 732 733 734
		return OBS_VIDEO_CURRENTLY_ACTIVE;

	if (!size_valid(ovi->output_width, ovi->output_height) ||
	    !size_valid(ovi->base_width,   ovi->base_height))
		return OBS_VIDEO_INVALID_PARAM;
735

736
	struct obs_core_video *video = &obs->video;
737

738
	stop_video();
739
	obs_free_video();
740

741 742
	if (!ovi) {
		obs_free_graphics();
743
		return OBS_VIDEO_SUCCESS;
744 745
	}

746 747 748 749
	/* align to multiple-of-two and SSE alignment sizes */
	ovi->output_width  &= 0xFFFFFFFC;
	ovi->output_height &= 0xFFFFFFFE;

750 751
	if (!video->graphics) {
		int errorcode = obs_init_graphics(ovi);
752 753
		if (errorcode != OBS_VIDEO_SUCCESS) {
			obs_free_graphics();
754
			return errorcode;
755
		}
756
	}
757

J
jp9000 已提交
758 759 760 761 762 763 764 765
	blog(LOG_INFO, "video settings reset:\n"
	               "\tbase resolution:   %dx%d\n"
	               "\toutput resolution: %dx%d\n"
	               "\tfps:               %d/%d",
	               ovi->base_width, ovi->base_height,
	               ovi->output_width, ovi->output_height,
	               ovi->fps_num, ovi->fps_den);

766
	return obs_init_video(ovi);
767 768
}

769
bool obs_reset_audio(const struct obs_audio_info *oai)
J
jp9000 已提交
770
{
771 772
	struct audio_output_info ai;

773 774 775 776 777 778
	if (!obs) return false;

	/* don't allow changing of audio settings if active. */
	if (obs->audio.audio && audio_output_active(obs->audio.audio))
		return false;

J
jp9000 已提交
779
	obs_free_audio();
780
	if (!oai)
781 782
		return true;

783 784 785 786 787 788
	ai.name = "Audio";
	ai.samples_per_sec = oai->samples_per_sec;
	ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
	ai.speakers = oai->speakers;
	ai.buffer_ms = oai->buffer_ms;

J
jp9000 已提交
789 790 791 792
	blog(LOG_INFO, "audio settings reset:\n"
	               "\tsamples per sec: %d\n"
	               "\tspeakers:        %d\n"
	               "\tbuffering (ms):  %d\n",
793 794 795
	               (int)ai.samples_per_sec,
	               (int)ai.speakers,
	               (int)ai.buffer_ms);
J
jp9000 已提交
796

797
	return obs_init_audio(&ai);
J
jp9000 已提交
798 799
}

800 801
bool obs_get_video_info(struct obs_video_info *ovi)
{
802
	struct obs_core_video *video = &obs->video;
J
jp9000 已提交
803
	const struct video_output_info *info;
804

J
jp9000 已提交
805
	if (!obs || !video->graphics)
806 807
		return false;

808
	info = video_output_get_info(video->video);
809 810
	if (!info)
		return false;
811 812 813 814

	memset(ovi, 0, sizeof(struct obs_video_info));
	ovi->base_width    = video->base_width;
	ovi->base_height   = video->base_height;
815
	ovi->gpu_conversion= video->gpu_conversion;
816
	ovi->scale_type    = video->scale_type;
817 818
	ovi->colorspace    = info->colorspace;
	ovi->range         = info->range;
819 820
	ovi->output_width  = info->width;
	ovi->output_height = info->height;
821
	ovi->output_format = info->format;
822 823 824 825 826 827
	ovi->fps_num       = info->fps_num;
	ovi->fps_den       = info->fps_den;

	return true;
}

828
bool obs_get_audio_info(struct obs_audio_info *oai)
829
{
830
	struct obs_core_audio *audio = &obs->audio;
J
jp9000 已提交
831
	const struct audio_output_info *info;
832

833
	if (!obs || !oai || !audio->audio)
834 835
		return false;

836
	info = audio_output_get_info(audio->audio);
837

838 839 840
	oai->samples_per_sec = info->samples_per_sec;
	oai->speakers = info->speakers;
	oai->buffer_ms = info->buffer_ms;
841 842 843
	return true;
}

844
bool obs_enum_input_types(size_t idx, const char **id)
J
jp9000 已提交
845
{
J
jp9000 已提交
846 847
	if (!obs) return false;

J
jp9000 已提交
848 849
	if (idx >= obs->input_types.num)
		return false;
850
	*id = obs->input_types.array[idx].id;
J
jp9000 已提交
851 852 853
	return true;
}

854
bool obs_enum_filter_types(size_t idx, const char **id)
J
jp9000 已提交
855
{
J
jp9000 已提交
856 857
	if (!obs) return false;

J
jp9000 已提交
858 859
	if (idx >= obs->filter_types.num)
		return false;
860
	*id = obs->filter_types.array[idx].id;
J
jp9000 已提交
861 862 863
	return true;
}

864
bool obs_enum_transition_types(size_t idx, const char **id)
J
jp9000 已提交
865
{
J
jp9000 已提交
866 867
	if (!obs) return false;

J
jp9000 已提交
868 869
	if (idx >= obs->transition_types.num)
		return false;
870
	*id = obs->transition_types.array[idx].id;
J
jp9000 已提交
871 872 873
	return true;
}

874
bool obs_enum_output_types(size_t idx, const char **id)
J
jp9000 已提交
875
{
J
jp9000 已提交
876 877
	if (!obs) return false;

J
jp9000 已提交
878 879
	if (idx >= obs->output_types.num)
		return false;
880
	*id = obs->output_types.array[idx].id;
J
jp9000 已提交
881 882 883
	return true;
}

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
bool obs_enum_encoder_types(size_t idx, const char **id)
{
	if (!obs) return false;

	if (idx >= obs->encoder_types.num)
		return false;
	*id = obs->encoder_types.array[idx].id;
	return true;
}

bool obs_enum_service_types(size_t idx, const char **id)
{
	if (!obs) return false;

	if (idx >= obs->service_types.num)
		return false;
	*id = obs->service_types.array[idx].id;
	return true;
}

J
jp9000 已提交
904
void obs_enter_graphics(void)
J
jp9000 已提交
905
{
J
jp9000 已提交
906
	if (obs && obs->video.graphics)
907
		gs_enter_context(obs->video.graphics);
J
jp9000 已提交
908 909 910 911 912
}

void obs_leave_graphics(void)
{
	if (obs && obs->video.graphics)
913
		gs_leave_context();
J
jp9000 已提交
914 915
}

916
audio_t *obs_get_audio(void)
J
jp9000 已提交
917 918 919 920
{
	return (obs != NULL) ? obs->audio.audio : NULL;
}

921
video_t *obs_get_video(void)
J
jp9000 已提交
922
{
J
jp9000 已提交
923
	return (obs != NULL) ? obs->video.video : NULL;
J
jp9000 已提交
924 925
}

J
jp9000 已提交
926
/* TODO: optimize this later so it's not just O(N) string lookups */
J
jp9000 已提交
927
static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
J
jp9000 已提交
928 929
		const char *task, const char *target)
{
J
jp9000 已提交
930 931
	for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
		struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
J
jp9000 已提交
932

J
jp9000 已提交
933
		if (strcmp(callback->id,     id)     == 0 &&
934 935
		    strcmp(callback->task,   task)   == 0 &&
		    strcmp(callback->target, target) == 0)
J
jp9000 已提交
936 937 938 939 940 941
			return callback;
	}

	return NULL;
}

J
jp9000 已提交
942
static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
J
jp9000 已提交
943 944
		const char *task, const char *target)
{
J
jp9000 已提交
945
	for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
946
		struct obs_modeless_ui *callback;
J
jp9000 已提交
947
		callback = obs->modeless_ui_callbacks.array+i;
J
jp9000 已提交
948

J
jp9000 已提交
949
		if (strcmp(callback->id,     id)     == 0 &&
950 951
		    strcmp(callback->task,   task)   == 0 &&
		    strcmp(callback->target, target) == 0)
J
jp9000 已提交
952 953 954 955 956 957
			return callback;
	}

	return NULL;
}

958
int obs_exec_ui(const char *name, const char *task, const char *target,
J
jp9000 已提交
959 960
		void *data, void *ui_data)
{
961
	struct obs_modal_ui *callback;
J
jp9000 已提交
962 963
	int errorcode = OBS_UI_NOTFOUND;

J
jp9000 已提交
964 965
	if (!obs) return errorcode;

966
	callback = get_modal_ui_callback(name, task, target);
J
jp9000 已提交
967
	if (callback) {
J
jp9000 已提交
968
		bool success = callback->exec(data, ui_data);
J
jp9000 已提交
969 970 971 972 973 974
		errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
	}

	return errorcode;
}

J
jp9000 已提交
975 976 977
void *obs_create_ui(const char *name, const char *task, const char *target,
		void *data, void *ui_data)
{
978
	struct obs_modeless_ui *callback;
J
jp9000 已提交
979

J
jp9000 已提交
980 981
	if (!obs) return NULL;

J
jp9000 已提交
982
	callback = get_modeless_ui_callback(name, task, target);
J
jp9000 已提交
983
	return callback ? callback->create(data, ui_data) : NULL;
J
jp9000 已提交
984 985
}

986
bool obs_add_source(obs_source_t *source)
987
{
988 989
	struct calldata params = {0};

J
jp9000 已提交
990
	if (!obs) return false;
991
	if (!source) return false;
J
jp9000 已提交
992

993
	pthread_mutex_lock(&obs->data.sources_mutex);
994
	da_push_back(obs->data.user_sources, &source);
995 996 997
	obs_source_addref(source);
	pthread_mutex_unlock(&obs->data.sources_mutex);

998
	calldata_set_ptr(&params, "source", source);
999
	signal_handler_signal(obs->signals, "source_add", &params);
1000 1001
	calldata_free(&params);

1002 1003 1004
	return true;
}

1005
obs_source_t *obs_get_output_source(uint32_t channel)
1006
{
J
jp9000 已提交
1007
	if (!obs) return NULL;
1008
	return obs_view_get_source(&obs->data.main_view, channel);
J
jp9000 已提交
1009 1010
}

1011
void obs_set_output_source(uint32_t channel, obs_source_t *source)
J
jp9000 已提交
1012
{
1013 1014 1015 1016 1017
	assert(channel < MAX_CHANNELS);

	if (!obs) return;
	if (channel >= MAX_CHANNELS) return;

1018
	struct obs_source *prev_source;
J
jp9000 已提交
1019
	struct obs_view *view = &obs->data.main_view;
J
jp9000 已提交
1020
	struct calldata params = {0};
1021

J
jp9000 已提交
1022
	pthread_mutex_lock(&view->channels_mutex);
1023

J
jp9000 已提交
1024 1025
	obs_source_addref(source);

J
jp9000 已提交
1026
	prev_source = view->channels[channel];
J
jp9000 已提交
1027

1028 1029 1030
	calldata_set_int(&params, "channel", channel);
	calldata_set_ptr(&params, "prev_source", prev_source);
	calldata_set_ptr(&params, "source", source);
1031
	signal_handler_signal(obs->signals, "channel_change", &params);
1032
	calldata_get_ptr(&params, "source", &source);
J
jp9000 已提交
1033 1034
	calldata_free(&params);

J
jp9000 已提交
1035
	view->channels[channel] = source;
1036

J
jp9000 已提交
1037 1038 1039
	pthread_mutex_unlock(&view->channels_mutex);

	if (source)
1040
		obs_source_activate(source, MAIN_VIEW);
1041 1042

	if (prev_source) {
1043
		obs_source_deactivate(prev_source, MAIN_VIEW);
1044
		obs_source_release(prev_source);
1045
	}
J
jp9000 已提交
1046
}
1047

1048
void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
1049
{
J
jp9000 已提交
1050 1051
	if (!obs) return;

1052
	pthread_mutex_lock(&obs->data.user_sources_mutex);
1053

1054 1055 1056
	for (size_t i = 0; i < obs->data.user_sources.num; i++) {
		struct obs_source *source = obs->data.user_sources.array[i];
		if (!enum_proc(param, source))
1057
			break;
1058
	}
1059

1060
	pthread_mutex_unlock(&obs->data.user_sources_mutex);
1061 1062
}

1063 1064
static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
		void *param)
1065
{
1066 1067
	struct obs_context_data **start = pstart, *context;
	bool (*enum_proc)(void*, void*) = proc;
1068

1069 1070 1071
	assert(start);
	assert(mutex);
	assert(enum_proc);
J
jp9000 已提交
1072

1073
	pthread_mutex_lock(mutex);
1074

1075 1076 1077
	context = *start;
	while (context) {
		if (!enum_proc(param, context))
1078 1079
			break;

1080 1081 1082 1083
		context = context->next;
	}

	pthread_mutex_unlock(mutex);
1084 1085
}

1086
void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
1087
{
J
jp9000 已提交
1088
	if (!obs) return;
1089 1090 1091
	obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
			enum_proc, param);
}
J
jp9000 已提交
1092

1093
void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
1094 1095 1096 1097 1098
{
	if (!obs) return;
	obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
			enum_proc, param);
}
1099

1100
void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
1101 1102 1103 1104
{
	if (!obs) return;
	obs_enum(&obs->data.first_service, &obs->data.services_mutex,
			enum_proc, param);
1105
}
1106

1107
obs_source_t *obs_get_source_by_name(const char *name)
1108
{
1109
	struct obs_core_data *data = &obs->data;
1110 1111 1112
	struct obs_source *source = NULL;
	size_t i;

J
jp9000 已提交
1113 1114
	if (!obs) return NULL;

1115
	pthread_mutex_lock(&data->user_sources_mutex);
1116

1117 1118 1119
	for (i = 0; i < data->user_sources.num; i++) {
		struct obs_source *cur_source = data->user_sources.array[i];
		if (strcmp(cur_source->context.name, name) == 0) {
1120
			source = cur_source;
1121
			obs_source_addref(source);
1122 1123 1124 1125
			break;
		}
	}

1126
	pthread_mutex_unlock(&data->user_sources_mutex);
1127 1128
	return source;
}
1129

1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
static inline void *get_context_by_name(void *vfirst, const char *name,
		pthread_mutex_t *mutex)
{
	struct obs_context_data **first = vfirst;
	struct obs_context_data *context;

	pthread_mutex_lock(mutex);

	context = *first;
	while (context) {
		if (strcmp(context->name, name) == 0)
			break;
		context = context->next;
	}

	pthread_mutex_unlock(mutex);
	return context;
}

1149
obs_output_t *obs_get_output_by_name(const char *name)
1150 1151 1152 1153 1154 1155
{
	if (!obs) return NULL;
	return get_context_by_name(&obs->data.first_output, name,
			&obs->data.outputs_mutex);
}

1156
obs_encoder_t *obs_get_encoder_by_name(const char *name)
1157 1158 1159 1160 1161 1162
{
	if (!obs) return NULL;
	return get_context_by_name(&obs->data.first_encoder, name,
			&obs->data.encoders_mutex);
}

1163
obs_service_t *obs_get_service_by_name(const char *name)
1164 1165 1166 1167 1168 1169
{
	if (!obs) return NULL;
	return get_context_by_name(&obs->data.first_service, name,
			&obs->data.services_mutex);
}

1170
gs_effect_t *obs_get_default_effect(void)
1171
{
1172
	if (!obs) return NULL;
1173 1174
	return obs->video.default_effect;
}
1175

P
Palana 已提交
1176 1177 1178 1179 1180 1181
gs_effect_t *obs_get_default_rect_effect(void)
{
	if (!obs) return NULL;
	return obs->video.default_rect_effect;
}

1182 1183 1184 1185 1186 1187
gs_effect_t *obs_get_opaque_effect(void)
{
	if (!obs) return NULL;
	return obs->video.opaque_effect;
}

1188
gs_effect_t *obs_get_solid_effect(void)
1189 1190 1191 1192 1193
{
	if (!obs) return NULL;
	return obs->video.solid_effect;
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
gs_effect_t *obs_get_bicubic_effect(void)
{
	if (!obs) return NULL;
	return obs->video.bicubic_effect;
}

gs_effect_t *obs_get_lanczos_effect(void)
{
	if (!obs) return NULL;
	return obs->video.lanczos_effect;
}

1206 1207 1208 1209 1210 1211
gs_effect_t *obs_get_bilinear_lowres_effect(void)
{
	if (!obs) return NULL;
	return obs->video.bilinear_lowres_effect;
}

1212
signal_handler_t *obs_get_signal_handler(void)
1213
{
1214
	if (!obs) return NULL;
1215 1216 1217
	return obs->signals;
}

1218
proc_handler_t *obs_get_proc_handler(void)
1219
{
1220
	if (!obs) return NULL;
1221 1222
	return obs->procs;
}
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247

void obs_add_draw_callback(
		void (*draw)(void *param, uint32_t cx, uint32_t cy),
		void *param)
{
	if (!obs) return;

	obs_display_add_draw_callback(&obs->video.main_display, draw, param);
}

void obs_remove_draw_callback(
		void (*draw)(void *param, uint32_t cx, uint32_t cy),
		void *param)
{
	if (!obs) return;

	obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
}

void obs_resize(uint32_t cx, uint32_t cy)
{
	if (!obs || !obs->video.video || !obs->video.graphics) return;
	obs_display_resize(&obs->video.main_display, cx, cy);
}

J
jp9000 已提交
1248
void obs_render_main_view(void)
1249 1250
{
	if (!obs) return;
J
jp9000 已提交
1251
	obs_view_render(&obs->data.main_view);
1252
}
J
jp9000 已提交
1253 1254 1255

void obs_set_master_volume(float volume)
{
J
jp9000 已提交
1256
	struct calldata data = {0};
J
jp9000 已提交
1257

J
jp9000 已提交
1258
	if (!obs) return;
J
jp9000 已提交
1259

1260
	calldata_set_float(&data, "volume", volume);
1261 1262
	signal_handler_signal(obs->signals, "master_volume", &data);
	volume = (float)calldata_float(&data, "volume");
J
jp9000 已提交
1263 1264
	calldata_free(&data);

J
jp9000 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	obs->audio.user_volume = volume;
}

void obs_set_present_volume(float volume)
{
	if (!obs) return;
	obs->audio.present_volume = volume;
}

float obs_get_master_volume(void)
{
	return obs ? obs->audio.user_volume : 0.0f;
}

float obs_get_present_volume(void)
{
	return obs ? obs->audio.present_volume : 0.0f;
}
1283

1284 1285
static obs_source_t *obs_load_source_type(obs_data_t *source_data,
		enum obs_source_type type)
1286
{
1287
	obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
1288
	obs_source_t *source;
J
jp9000 已提交
1289 1290
	const char   *name    = obs_data_get_string(source_data, "name");
	const char   *id      = obs_data_get_string(source_data, "id");
1291
	obs_data_t   *settings = obs_data_get_obj(source_data, "settings");
1292
	double       volume;
1293 1294
	int64_t      sync;
	uint32_t     flags;
1295
	uint32_t     mixers;
1296

1297
	source = obs_source_create(type, id, name, settings);
1298 1299

	obs_data_set_default_double(source_data, "volume", 1.0);
J
jp9000 已提交
1300
	volume = obs_data_get_double(source_data, "volume");
1301
	obs_source_set_volume(source, (float)volume);
1302

1303 1304 1305
	sync = obs_data_get_int(source_data, "sync");
	obs_source_set_sync_offset(source, sync);

1306 1307 1308 1309
	obs_data_set_default_int(source_data, "mixers", 0xF);
	mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
	obs_source_set_audio_mixers(source, mixers);

1310
	obs_data_set_default_int(source_data, "flags", source->default_flags);
1311 1312 1313
	flags = (uint32_t)obs_data_get_int(source_data, "flags");
	obs_source_set_flags(source, flags);

1314 1315 1316 1317
	obs_data_set_default_bool(source_data, "enabled", true);
	obs_source_set_enabled(source,
			obs_data_get_bool(source_data, "enabled"));

J
jp9000 已提交
1318 1319 1320
	obs_data_set_default_bool(source_data, "muted", false);
	obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
	if (filters) {
		size_t count = obs_data_array_count(filters);

		for (size_t i = 0; i < count; i++) {
			obs_data_t *filter_data =
				obs_data_array_item(filters, i);

			obs_source_t *filter = obs_load_source_type(
					filter_data, OBS_SOURCE_TYPE_FILTER);
			if (filter) {
				obs_source_filter_add(source, filter);
				obs_source_release(filter);
			}

			obs_data_release(filter_data);
		}

		obs_data_array_release(filters);
	}

1341 1342 1343 1344 1345
	obs_data_release(settings);

	return source;
}

1346 1347 1348 1349 1350
obs_source_t *obs_load_source(obs_data_t *source_data)
{
	return obs_load_source_type(source_data, OBS_SOURCE_TYPE_INPUT);
}

1351
void obs_load_sources(obs_data_array_t *array)
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
{
	size_t count;
	size_t i;

	if (!obs) return;

	count = obs_data_array_count(array);

	pthread_mutex_lock(&obs->data.user_sources_mutex);

	for (i = 0; i < count; i++) {
1363 1364
		obs_data_t   *source_data = obs_data_array_item(array, i);
		obs_source_t *source      = obs_load_source(source_data);
1365 1366 1367

		obs_add_source(source);

1368
		obs_source_release(source);
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
		obs_data_release(source_data);
	}

	/* tell sources that we want to load */
	for (i = 0; i < obs->data.user_sources.num; i++)
		obs_source_load(obs->data.user_sources.array[i]);

	pthread_mutex_unlock(&obs->data.user_sources_mutex);
}

1379
obs_data_t *obs_save_source(obs_source_t *source)
1380
{
1381
	obs_data_array_t *filters = obs_data_array_create();
1382 1383
	obs_data_t *source_data = obs_data_create();
	obs_data_t *settings    = obs_source_get_settings(source);
1384
	float      volume      = obs_source_get_volume(source);
1385
	uint32_t   mixers      = obs_source_get_audio_mixers(source);
1386 1387
	int64_t    sync        = obs_source_get_sync_offset(source);
	uint32_t   flags       = obs_source_get_flags(source);
1388
	const char *name       = obs_source_get_name(source);
J
jp9000 已提交
1389
	const char *id         = obs_source_get_id(source);
1390
	bool       enabled     = obs_source_enabled(source);
J
jp9000 已提交
1391
	bool       muted       = obs_source_muted(source);
1392 1393

	obs_source_save(source);
1394

J
jp9000 已提交
1395 1396 1397
	obs_data_set_string(source_data, "name",     name);
	obs_data_set_string(source_data, "id",       id);
	obs_data_set_obj   (source_data, "settings", settings);
1398
	obs_data_set_int   (source_data, "mixers",   mixers);
1399 1400
	obs_data_set_int   (source_data, "sync",     sync);
	obs_data_set_int   (source_data, "flags",    flags);
J
jp9000 已提交
1401
	obs_data_set_double(source_data, "volume",   volume);
1402
	obs_data_set_bool  (source_data, "enabled",  enabled);
J
jp9000 已提交
1403
	obs_data_set_bool  (source_data, "muted",    muted);
1404

1405 1406 1407
	pthread_mutex_lock(&source->filter_mutex);

	if (source->filters.num) {
1408 1409
		for (size_t i = source->filters.num; i > 0; i--) {
			obs_source_t *filter = source->filters.array[i - 1];
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
			obs_data_t *filter_data = obs_save_source(filter);
			obs_data_array_push_back(filters, filter_data);
			obs_data_release(filter_data);
		}

		obs_data_set_array(source_data, "filters", filters);
	}

	pthread_mutex_unlock(&source->filter_mutex);

1420
	obs_data_release(settings);
1421
	obs_data_array_release(filters);
1422 1423

	return source_data;
1424 1425
}

1426
obs_data_array_t *obs_save_sources(void)
1427
{
1428
	obs_data_array_t *array;
1429 1430 1431 1432 1433 1434 1435 1436 1437
	size_t i;

	if (!obs) return NULL;

	array = obs_data_array_create();

	pthread_mutex_lock(&obs->data.user_sources_mutex);

	for (i = 0; i < obs->data.user_sources.num; i++) {
1438 1439
		obs_source_t *source      = obs->data.user_sources.array[i];
		obs_data_t   *source_data = obs_save_source(source);
1440 1441 1442

		obs_data_array_push_back(array, source_data);
		obs_data_release(source_data);
1443 1444 1445 1446 1447 1448 1449
	}

	pthread_mutex_unlock(&obs->data.user_sources_mutex);

	return array;
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
/* ensures that names are never blank */
static inline char *dup_name(const char *name)
{
	if (!name || !*name) {
		struct dstr unnamed = {0};
		dstr_printf(&unnamed, "__unnamed%004lld",
				obs->data.unnamed_index++);

		return unnamed.array;
	} else {
		return bstrdup(name);
	}
}

static inline bool obs_context_data_init_wrap(
		struct obs_context_data *context,
1466
		obs_data_t              *settings,
1467 1468 1469
		const char              *name)
{
	assert(context);
1470
	memset(context, 0, sizeof(*context));
1471

J
jp9000 已提交
1472 1473 1474 1475
	pthread_mutex_init_value(&context->rename_cache_mutex);
	if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
		return false;

1476
	context->signals = signal_handler_create();
P
Palana 已提交
1477
	if (!context->signals)
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
		return false;

	context->procs = proc_handler_create();
	if (!context->procs)
		return false;

	context->name     = dup_name(name);
	context->settings = obs_data_newref(settings);
	return true;
}

bool obs_context_data_init(
		struct obs_context_data *context,
1491
		obs_data_t              *settings,
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
		const char              *name)
{
	if (obs_context_data_init_wrap(context, settings, name)) {
		return true;
	} else {
		obs_context_data_free(context);
		return false;
	}
}

void obs_context_data_free(struct obs_context_data *context)
{
	signal_handler_destroy(context->signals);
	proc_handler_destroy(context->procs);
	obs_data_release(context->settings);
	obs_context_data_remove(context);
J
jp9000 已提交
1508
	pthread_mutex_destroy(&context->rename_cache_mutex);
1509 1510
	bfree(context->name);

J
jp9000 已提交
1511 1512 1513 1514
	for (size_t i = 0; i < context->rename_cache.num; i++)
		bfree(context->rename_cache.array[i]);
	da_free(context->rename_cache);

1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	memset(context, 0, sizeof(*context));
}

void obs_context_data_insert(struct obs_context_data *context,
		pthread_mutex_t *mutex, void *pfirst)
{
	struct obs_context_data **first = pfirst;

	assert(context);
	assert(mutex);
	assert(first);

	context->mutex = mutex;

	pthread_mutex_lock(mutex);
	context->prev_next  = first;
	context->next       = *first;
	*first              = context;
	if (context->next)
		context->next->prev_next = &context->next;
	pthread_mutex_unlock(mutex);
}

void obs_context_data_remove(struct obs_context_data *context)
{
	if (context && context->mutex) {
		pthread_mutex_lock(context->mutex);
1542 1543
		if (context->prev_next)
			*context->prev_next = context->next;
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
		if (context->next)
			context->next->prev_next = context->prev_next;
		pthread_mutex_unlock(context->mutex);

		context->mutex = NULL;
	}
}

void obs_context_data_setname(struct obs_context_data *context,
		const char *name)
{
J
jp9000 已提交
1555 1556 1557 1558
	pthread_mutex_lock(&context->rename_cache_mutex);

	if (context->name)
		da_push_back(context->rename_cache, &context->name);
1559
	context->name = dup_name(name);
J
jp9000 已提交
1560 1561

	pthread_mutex_unlock(&context->rename_cache_mutex);
1562
}
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573

void obs_preview_set_enabled(bool enable)
{
	if (obs)
		obs->video.main_display.enabled = enable;
}

bool obs_preview_enabled(void)
{
	return obs ? obs->video.main_display.enabled : false;
}