obs-scene.c 48.3 KB
Newer Older
J
jp9000 已提交
1
/******************************************************************************
S
Socapex 已提交
2 3
    Copyright (C) 2013-2015 by Hugh Bailey <obs.jim@gmail.com>
                               Philippe Groarke <philippe.groarke@gmail.com>
J
jp9000 已提交
4 5 6

    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
7
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
8 9 10 11 12 13 14 15 16 17 18
    (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/>.
******************************************************************************/

J
jp9000 已提交
19
#include "util/threading.h"
J
jp9000 已提交
20 21 22
#include "graphics/math-defs.h"
#include "obs-scene.h"

23 24 25 26 27 28 29 30 31 32 33
/* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
 * lock the graphics mutex inside either of the scene mutexes.
 *
 * Another thing that must be done to prevent that cross-lock (and improve
 * performance), is to not create/release/update sources within the scene
 * mutexes.
 *
 * It's okay to lock the graphics mutex before locking either of the scene
 * mutexes, but not after.
 */

34 35 36
static const char *obs_scene_signals[] = {
	"void item_add(ptr scene, ptr item)",
	"void item_remove(ptr scene, ptr item)",
S
Socapex 已提交
37
	"void reorder(ptr scene)",
38
	"void item_visible(ptr scene, ptr item, bool visible)",
39 40 41
	"void item_select(ptr scene, ptr item)",
	"void item_deselect(ptr scene, ptr item)",
	"void item_transform(ptr scene, ptr item)",
42 43 44
	NULL
};

J
jp9000 已提交
45
static inline void signal_item_remove(struct obs_scene_item *item)
J
jp9000 已提交
46
{
47 48 49 50
	struct calldata params;
	uint8_t stack[128];

	calldata_init_fixed(&params, stack, sizeof(stack));
51 52
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item", item);
J
jp9000 已提交
53

54 55
	signal_handler_signal(item->parent->source->context.signals,
			"item_remove", &params);
J
jp9000 已提交
56 57
}

58
static const char *scene_getname(void *unused)
59
{
60
	UNUSED_PARAMETER(unused);
61
	return "Scene";
62 63
}

64
static void *scene_create(obs_data_t *settings, struct obs_source *source)
J
jp9000 已提交
65
{
P
Palana 已提交
66
	pthread_mutexattr_t attr;
J
jp9000 已提交
67
	struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
68 69 70
	scene->source     = source;
	scene->first_item = NULL;

71
	signal_handler_add_array(obs_source_get_signal_handler(source),
72 73
			obs_scene_signals);

J
jp9000 已提交
74 75
	scene->id_counter = 0;

P
Palana 已提交
76 77 78 79
	if (pthread_mutexattr_init(&attr) != 0)
		goto fail;
	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
		goto fail;
80 81 82 83 84 85 86 87
	if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
		blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
				"mutex");
		goto fail;
	}
	if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
		blog(LOG_ERROR, "scene_create: Couldn't initialize video "
				"mutex");
P
Palana 已提交
88
		goto fail;
89
	}
J
jp9000 已提交
90

J
jp9000 已提交
91
	UNUSED_PARAMETER(settings);
J
jp9000 已提交
92
	return scene;
P
Palana 已提交
93 94 95 96 97

fail:
	pthread_mutexattr_destroy(&attr);
	bfree(scene);
	return NULL;
J
jp9000 已提交
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
#define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
#define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
#define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)

static inline void full_lock(struct obs_scene *scene)
{
	video_lock(scene);
	audio_lock(scene);
}

static inline void full_unlock(struct obs_scene *scene)
{
	audio_unlock(scene);
	video_unlock(scene);
}

117 118 119 120 121 122 123 124 125 126 127
static void set_visibility(struct obs_scene_item *item, bool vis);
static inline void detach_sceneitem(struct obs_scene_item *item);

static inline void remove_without_release(struct obs_scene_item *item)
{
	item->removed = true;
	set_visibility(item, false);
	signal_item_remove(item);
	detach_sceneitem(item);
}

128
static void remove_all_items(struct obs_scene *scene)
J
jp9000 已提交
129
{
J
jp9000 已提交
130
	struct obs_scene_item *item;
131 132 133
	DARRAY(struct obs_scene_item*) items;

	da_init(items);
J
jp9000 已提交
134

135
	full_lock(scene);
J
jp9000 已提交
136

J
jp9000 已提交
137 138
	item = scene->first_item;

139 140 141 142
	while (item) {
		struct obs_scene_item *del_item = item;
		item = item->next;

143 144
		remove_without_release(del_item);
		da_push_back(items, &del_item);
145
	}
J
jp9000 已提交
146

147
	full_unlock(scene);
148 149 150 151

	for (size_t i = 0; i < items.num; i++)
		obs_sceneitem_release(items.array[i]);
	da_free(items);
152 153 154 155 156
}

static void scene_destroy(void *data)
{
	struct obs_scene *scene = data;
J
jp9000 已提交
157

158
	remove_all_items(scene);
159

160 161
	pthread_mutex_destroy(&scene->video_mutex);
	pthread_mutex_destroy(&scene->audio_mutex);
J
jp9000 已提交
162 163 164
	bfree(scene);
}

165 166
static void scene_enum_sources(void *data,
		obs_source_enum_proc_t enum_callback,
167
		void *param, bool active)
168 169 170
{
	struct obs_scene *scene = data;
	struct obs_scene_item *item;
171
	struct obs_scene_item *next;
172

173
	full_lock(scene);
174
	item = scene->first_item;
175

176
	while (item) {
177
		next = item->next;
178 179

		obs_sceneitem_addref(item);
180
		if (!active || os_atomic_load_long(&item->active_refs) > 0)
181
			enum_callback(scene->source, item->source, param);
182 183 184 185 186
		obs_sceneitem_release(item);

		item = next;
	}

187
	full_unlock(scene);
188 189
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203
static void scene_enum_active_sources(void *data,
		obs_source_enum_proc_t enum_callback,
		void *param)
{
	scene_enum_sources(data, enum_callback, param, true);
}

static void scene_enum_all_sources(void *data,
		obs_source_enum_proc_t enum_callback,
		void *param)
{
	scene_enum_sources(data, enum_callback, param, false);
}

204 205 206 207 208 209 210 211 212
static inline void detach_sceneitem(struct obs_scene_item *item)
{
	if (item->prev)
		item->prev->next = item->next;
	else
		item->parent->first_item = item->next;

	if (item->next)
		item->next->prev = item->prev;
J
jp9000 已提交
213 214

	item->parent = NULL;
215 216
}

J
jp9000 已提交
217 218
static inline void attach_sceneitem(struct obs_scene *parent,
		struct obs_scene_item *item, struct obs_scene_item *prev)
219
{
J
jp9000 已提交
220 221
	item->prev   = prev;
	item->parent = parent;
222 223 224 225 226 227 228

	if (prev) {
		item->next = prev->next;
		if (prev->next)
			prev->next->prev = item;
		prev->next = item;
	} else {
229 230 231 232
		item->next = parent->first_item;
		if (parent->first_item)
			parent->first_item->prev = item;
		parent->first_item = item;
233 234 235
	}
}

236
void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
{
	if (align & OBS_ALIGN_RIGHT)
		v->x += (float)cx;
	else if ((align & OBS_ALIGN_LEFT) == 0)
		v->x += (float)(cx / 2);

	if (align & OBS_ALIGN_BOTTOM)
		v->y += (float)cy;
	else if ((align & OBS_ALIGN_TOP) == 0)
		v->y += (float)(cy / 2);
}

static void calculate_bounds_data(struct obs_scene_item *item,
		struct vec2 *origin, struct vec2 *scale,
		uint32_t *cx, uint32_t *cy)
{
J
jp9000 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
	float    width         = (float)(*cx) * fabsf(scale->x);
	float    height        = (float)(*cy) * fabsf(scale->y);
	float    item_aspect   = width / height;
	float    bounds_aspect = item->bounds.x / item->bounds.y;
	uint32_t bounds_type   = item->bounds_type;
	float    width_diff, height_diff;

	if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
		if (width > item->bounds.x || height > item->bounds.y)
			bounds_type = OBS_BOUNDS_SCALE_INNER;

	if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
	    bounds_type == OBS_BOUNDS_SCALE_OUTER) {
266 267 268 269 270 271 272 273 274 275 276 277
		bool  use_width = (bounds_aspect < item_aspect);
		float mul;

		if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
			use_width = !use_width;

		mul = use_width ?
			item->bounds.x / width :
			item->bounds.y / height;

		vec2_mulf(scale, scale, mul);

J
jp9000 已提交
278
	} else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
279 280
		vec2_mulf(scale, scale, item->bounds.x / width);

J
jp9000 已提交
281
	} else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
282 283
		vec2_mulf(scale, scale, item->bounds.y / height);

J
jp9000 已提交
284
	} else if (bounds_type == OBS_BOUNDS_STRETCH) {
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		scale->x = item->bounds.x / (float)(*cx);
		scale->y = item->bounds.y / (float)(*cy);
	}

	width       = (float)(*cx) * scale->x;
	height      = (float)(*cy) * scale->y;
	width_diff  = item->bounds.x - width;
	height_diff = item->bounds.y - height;
	*cx         = (uint32_t)item->bounds.x;
	*cy         = (uint32_t)item->bounds.y;

	add_alignment(origin, item->bounds_align,
			(int)-width_diff, (int)-height_diff);
}

300 301 302 303 304 305 306 307 308 309 310 311 312 313
static inline uint32_t calc_cx(const struct obs_scene_item *item,
		uint32_t width)
{
	uint32_t crop_cx = item->crop.left + item->crop.right;
	return (crop_cx > width) ? 2 : (width - crop_cx);
}

static inline uint32_t calc_cy(const struct obs_scene_item *item,
		uint32_t height)
{
	uint32_t crop_cy = item->crop.top + item->crop.bottom;
	return (crop_cy > height) ? 2 : (height - crop_cy);
}

314 315
static void update_item_transform(struct obs_scene_item *item)
{
316 317
	uint32_t        width         = obs_source_get_width(item->source);
	uint32_t        height        = obs_source_get_height(item->source);
318 319
	uint32_t        cx            = calc_cx(item, width);
	uint32_t        cy            = calc_cy(item, height);
J
jp9000 已提交
320 321
	struct vec2     base_origin;
	struct vec2     origin;
322
	struct vec2     scale         = item->scale;
323 324
	struct calldata params;
	uint8_t         stack[128];
325

326 327 328
	if (os_atomic_load_long(&item->defer_update) > 0)
		return;

329 330 331
	width = cx;
	height = cy;

J
jp9000 已提交
332 333 334
	vec2_zero(&base_origin);
	vec2_zero(&origin);

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	/* ----------------------- */

	if (item->bounds_type != OBS_BOUNDS_NONE) {
		calculate_bounds_data(item, &origin, &scale, &cx, &cy);
	} else {
		cx = (uint32_t)((float)cx * scale.x);
		cy = (uint32_t)((float)cy * scale.y);
	}

	add_alignment(&origin, item->align, (int)cx, (int)cy);

	matrix4_identity(&item->draw_transform);
	matrix4_scale3f(&item->draw_transform, &item->draw_transform,
			scale.x, scale.y, 1.0f);
	matrix4_translate3f(&item->draw_transform, &item->draw_transform,
			-origin.x, -origin.y, 0.0f);
	matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
			0.0f, 0.0f, 1.0f, RAD(item->rot));
	matrix4_translate3f(&item->draw_transform, &item->draw_transform,
			item->pos.x, item->pos.y, 0.0f);

356 357
	item->output_scale = scale;

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	/* ----------------------- */

	if (item->bounds_type != OBS_BOUNDS_NONE) {
		vec2_copy(&scale, &item->bounds);
	} else {
		scale.x = (float)width  * item->scale.x;
		scale.y = (float)height * item->scale.y;
	}

	add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);

	matrix4_identity(&item->box_transform);
	matrix4_scale3f(&item->box_transform, &item->box_transform,
			scale.x, scale.y, 1.0f);
	matrix4_translate3f(&item->box_transform, &item->box_transform,
			-base_origin.x, -base_origin.y, 0.0f);
	matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
			0.0f, 0.0f, 1.0f, RAD(item->rot));
	matrix4_translate3f(&item->box_transform, &item->box_transform,
			item->pos.x, item->pos.y, 0.0f);

	/* ----------------------- */

	item->last_width  = width;
	item->last_height = height;

384
	calldata_init_fixed(&params, stack, sizeof(stack));
385 386
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item", item);
387 388 389 390 391 392
	signal_handler_signal(item->parent->source->context.signals,
			"item_transform", &params);
}

static inline bool source_size_changed(struct obs_scene_item *item)
{
393 394
	uint32_t width  = obs_source_get_width(item->source);
	uint32_t height = obs_source_get_height(item->source);
395 396 397 398

	return item->last_width != width || item->last_height != height;
}

399 400 401 402 403
static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
{
	return crop->left || crop->right || crop->top || crop->bottom;
}

404 405 406 407 408
static inline bool scale_filter_enabled(const struct obs_scene_item *item)
{
	return item->scale_filter != OBS_SCALE_DISABLE;
}

409 410 411 412 413
static inline bool item_is_scene(const struct obs_scene_item *item)
{
	return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
}

414 415
static inline bool item_texture_enabled(const struct obs_scene_item *item)
{
416 417
	return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
		item_is_scene(item);
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
}

static void render_item_texture(struct obs_scene_item *item)
{
	gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
	gs_effect_t *effect = obs->video.default_effect;
	enum obs_scale_type type = item->scale_filter;
	uint32_t cx = gs_texture_get_width(tex);
	uint32_t cy = gs_texture_get_height(tex);

	if (type != OBS_SCALE_DISABLE) {
		if (type == OBS_SCALE_POINT) {
			gs_eparam_t *image = gs_effect_get_param_by_name(
					effect, "image");
			gs_effect_set_next_sampler(image,
					obs->video.point_sampler);

		} else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
		           !close_float(item->output_scale.y, 1.0f, EPSILON)) {
			gs_eparam_t *scale_param;

			if (item->output_scale.x < 0.5f ||
			    item->output_scale.y < 0.5f) {
				effect = obs->video.bilinear_lowres_effect;
			} else if (type == OBS_SCALE_BICUBIC) {
				effect = obs->video.bicubic_effect;
			} else if (type == OBS_SCALE_LANCZOS) {
				effect = obs->video.lanczos_effect;
			}

			scale_param = gs_effect_get_param_by_name(effect,
					"base_dimension_i");
			if (scale_param) {
				struct vec2 base_res_i = {
					1.0f / (float)cx,
					1.0f / (float)cy
				};

				gs_effect_set_vec2(scale_param, &base_res_i);
			}
		}
	}

	while (gs_effect_loop(effect, "Draw"))
		obs_source_draw(tex, 0, 0, 0, 0, 0);
}

465 466
static inline void render_item(struct obs_scene_item *item)
{
467
	if (item->item_render) {
468 469 470 471 472
		uint32_t width  = obs_source_get_width(item->source);
		uint32_t height = obs_source_get_height(item->source);
		uint32_t cx = calc_cx(item, width);
		uint32_t cy = calc_cy(item, height);

473
		if (cx && cy && gs_texrender_begin(item->item_render, cx, cy)) {
474 475
			float cx_scale = (float)width  / (float)cx;
			float cy_scale = (float)height / (float)cy;
476 477 478 479 480 481 482
			struct vec4 clear_color;

			vec4_zero(&clear_color);
			gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
			gs_ortho(0.0f, (float)width, 0.0f, (float)height,
					-100.0f, 100.0f);

483 484 485 486 487 488
			gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
			gs_matrix_translate3f(
					-(float)item->crop.left,
					-(float)item->crop.top,
					0.0f);

489 490
			gs_blend_state_push();
			gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
491
			obs_source_video_render(item->source);
492
			gs_blend_state_pop();
493
			gs_texrender_end(item->item_render);
494 495 496 497 498
		}
	}

	gs_matrix_push();
	gs_matrix_mul(&item->draw_transform);
499 500
	if (item->item_render) {
		render_item_texture(item);
501 502 503 504 505 506 507 508 509 510 511 512 513 514
	} else {
		obs_source_video_render(item->source);
	}
	gs_matrix_pop();
}

static void scene_video_tick(void *data, float seconds)
{
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

	video_lock(scene);
	item = scene->first_item;
	while (item) {
515 516
		if (item->item_render)
			gs_texrender_reset(item->item_render);
517 518 519 520 521 522 523
		item = item->next;
	}
	video_unlock(scene);

	UNUSED_PARAMETER(seconds);
}

524
static void scene_video_render(void *data, gs_effect_t *effect)
J
jp9000 已提交
525
{
526
	DARRAY(struct obs_scene_item*) remove_items;
527 528 529
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

530 531
	da_init(remove_items);

532
	video_lock(scene);
533
	item = scene->first_item;
J
jp9000 已提交
534

J
jp9000 已提交
535
	gs_blend_state_push();
536
	gs_reset_blend_state();
J
jp9000 已提交
537

538
	while (item) {
539
		if (obs_source_removed(item->source)) {
540 541 542
			struct obs_scene_item *del_item = item;
			item = item->next;

543 544
			remove_without_release(del_item);
			da_push_back(remove_items, &del_item);
545 546 547
			continue;
		}

548 549
		if (source_size_changed(item))
			update_item_transform(item);
J
jp9000 已提交
550

551 552
		if (item->user_visible)
			render_item(item);
553 554

		item = item->next;
J
jp9000 已提交
555
	}
556

J
jp9000 已提交
557 558
	gs_blend_state_pop();

559
	video_unlock(scene);
J
jp9000 已提交
560

561 562 563 564
	for (size_t i = 0; i < remove_items.num; i++)
		obs_sceneitem_release(remove_items.array[i]);
	da_free(remove_items);

J
jp9000 已提交
565 566 567
	UNUSED_PARAMETER(effect);
}

568
static void set_visibility(struct obs_scene_item *item, bool vis)
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
{
	pthread_mutex_lock(&item->actions_mutex);

	da_resize(item->audio_actions, 0);

	if (os_atomic_load_long(&item->active_refs) > 0) {
		if (!vis)
			obs_source_remove_active_child(item->parent->source,
					item->source);
	} else if (vis) {
		obs_source_add_active_child(item->parent->source, item->source);
	}

	os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
	item->visible = vis;
	item->user_visible = vis;

	pthread_mutex_unlock(&item->actions_mutex);
}

589
static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
590
{
J
jp9000 已提交
591
	const char            *name = obs_data_get_string(item_data, "name");
592
	obs_source_t          *source = obs_get_source_by_name(name);
593
	const char            *scale_filter_str;
594
	struct obs_scene_item *item;
595
	bool visible;
596
	bool lock;
597 598 599 600 601 602 603 604

	if (!source) {
		blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
				name);
		return;
	}

	item = obs_scene_add(scene, source);
P
Palana 已提交
605 606 607 608 609 610 611 612
	if (!item) {
		blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
		                  "to scene '%s'!",
		                  name, obs_source_get_name(scene->source));
		
		obs_source_release(source);
		return;
	}
613

614 615 616
	obs_data_set_default_int(item_data, "align",
			OBS_ALIGN_TOP | OBS_ALIGN_LEFT);

J
jp9000 已提交
617 618 619
	if (obs_data_has_user_value(item_data, "id"))
		item->id = obs_data_get_int(item_data, "id");

J
jp9000 已提交
620 621
	item->rot     = (float)obs_data_get_double(item_data, "rot");
	item->align   = (uint32_t)obs_data_get_int(item_data, "align");
622
	visible = obs_data_get_bool(item_data, "visible");
623
	lock = obs_data_get_bool(item_data, "locked");
624 625
	obs_data_get_vec2(item_data, "pos",    &item->pos);
	obs_data_get_vec2(item_data, "scale",  &item->scale);
626

627 628 629 630 631 632
	obs_data_release(item->private_settings);
	item->private_settings =
		obs_data_get_obj(item_data, "private_settings");
	if (!item->private_settings)
		item->private_settings = obs_data_create();

633
	set_visibility(item, visible);
634
	obs_sceneitem_set_locked(item, lock);
635

636
	item->bounds_type =
J
jp9000 已提交
637 638
		(enum obs_bounds_type)obs_data_get_int(item_data,
				"bounds_type");
639
	item->bounds_align =
J
jp9000 已提交
640
		(uint32_t)obs_data_get_int(item_data, "bounds_align");
641 642
	obs_data_get_vec2(item_data, "bounds", &item->bounds);

643 644 645 646 647
	item->crop.left   = (uint32_t)obs_data_get_int(item_data, "crop_left");
	item->crop.top    = (uint32_t)obs_data_get_int(item_data, "crop_top");
	item->crop.right  = (uint32_t)obs_data_get_int(item_data, "crop_right");
	item->crop.bottom = (uint32_t)obs_data_get_int(item_data, "crop_bottom");

648 649 650 651 652 653 654 655 656 657 658 659 660
	scale_filter_str = obs_data_get_string(item_data, "scale_filter");
	item->scale_filter = OBS_SCALE_DISABLE;

	if (scale_filter_str) {
		if (astrcmpi(scale_filter_str, "point") == 0)
			item->scale_filter = OBS_SCALE_POINT;
		else if (astrcmpi(scale_filter_str, "bilinear") == 0)
			item->scale_filter = OBS_SCALE_BILINEAR;
		else if (astrcmpi(scale_filter_str, "bicubic") == 0)
			item->scale_filter = OBS_SCALE_BICUBIC;
		else if (astrcmpi(scale_filter_str, "lanczos") == 0)
			item->scale_filter = OBS_SCALE_LANCZOS;
	}
661 662

	if (item->item_render && !item_texture_enabled(item)) {
663
		obs_enter_graphics();
664 665
		gs_texrender_destroy(item->item_render);
		item->item_render = NULL;
666 667
		obs_leave_graphics();

668
	} else if (!item->item_render && item_texture_enabled(item)) {
669
		obs_enter_graphics();
670
		item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
671 672 673
		obs_leave_graphics();
	}

674
	obs_source_release(source);
675 676

	update_item_transform(item);
677 678
}

J
jp9000 已提交
679
static void scene_load(void *data, obs_data_t *settings)
680
{
J
jp9000 已提交
681
	struct obs_scene *scene = data;
682
	obs_data_array_t *items = obs_data_get_array(settings, "items");
683 684 685 686 687 688 689 690 691
	size_t           count, i;

	remove_all_items(scene);

	if (!items) return;

	count = obs_data_array_count(items);

	for (i = 0; i < count; i++) {
692
		obs_data_t *item_data = obs_data_array_item(items, i);
693 694 695 696
		scene_load_item(scene, item_data);
		obs_data_release(item_data);
	}

J
jp9000 已提交
697 698 699
	if (obs_data_has_user_value(settings, "id_counter"))
		scene->id_counter = obs_data_get_int(settings, "id_counter");

700 701 702
	obs_data_array_release(items);
}

703 704
static void scene_save_item(obs_data_array_t *array,
		struct obs_scene_item *item)
705
{
706
	obs_data_t *item_data = obs_data_create();
707
	const char *name     = obs_source_get_name(item->source);
708
	const char *scale_filter;
709

J
jp9000 已提交
710
	obs_data_set_string(item_data, "name",         name);
711
	obs_data_set_bool  (item_data, "visible",      item->user_visible);
712
	obs_data_set_bool  (item_data, "locked",       item->locked);
J
jp9000 已提交
713
	obs_data_set_double(item_data, "rot",          item->rot);
714 715
	obs_data_set_vec2 (item_data, "pos",          &item->pos);
	obs_data_set_vec2 (item_data, "scale",        &item->scale);
J
jp9000 已提交
716 717 718
	obs_data_set_int   (item_data, "align",        (int)item->align);
	obs_data_set_int   (item_data, "bounds_type",  (int)item->bounds_type);
	obs_data_set_int   (item_data, "bounds_align", (int)item->bounds_align);
719
	obs_data_set_vec2 (item_data, "bounds",       &item->bounds);
720 721 722 723
	obs_data_set_int  (item_data, "crop_left",    (int)item->crop.left);
	obs_data_set_int  (item_data, "crop_top",     (int)item->crop.top);
	obs_data_set_int  (item_data, "crop_right",   (int)item->crop.right);
	obs_data_set_int  (item_data, "crop_bottom",  (int)item->crop.bottom);
J
jp9000 已提交
724
	obs_data_set_int  (item_data, "id",           item->id);
725 726 727 728 729 730 731 732 733 734 735 736 737

	if (item->scale_filter == OBS_SCALE_POINT)
		scale_filter = "point";
	else if (item->scale_filter == OBS_SCALE_BILINEAR)
		scale_filter = "bilinear";
	else if (item->scale_filter == OBS_SCALE_BICUBIC)
		scale_filter = "bicubic";
	else if (item->scale_filter == OBS_SCALE_LANCZOS)
		scale_filter = "lanczos";
	else
		scale_filter = "disable";

	obs_data_set_string(item_data, "scale_filter", scale_filter);
738

739 740 741
	obs_data_set_obj(item_data, "private_settings",
			item->private_settings);

742 743 744 745
	obs_data_array_push_back(array, item_data);
	obs_data_release(item_data);
}

746
static void scene_save(void *data, obs_data_t *settings)
747 748
{
	struct obs_scene      *scene = data;
749
	obs_data_array_t      *array  = obs_data_array_create();
750 751
	struct obs_scene_item *item;

752
	full_lock(scene);
753 754 755

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
756
		scene_save_item(array, item);
757 758 759
		item = item->next;
	}

J
jp9000 已提交
760 761
	obs_data_set_int(settings, "id_counter", scene->id_counter);

762
	full_unlock(scene);
763

J
jp9000 已提交
764
	obs_data_set_array(settings, "items", array);
765 766 767
	obs_data_array_release(array);
}

J
jp9000 已提交
768 769 770 771
static uint32_t scene_getwidth(void *data)
{
	UNUSED_PARAMETER(data);
	return obs->video.base_width;
J
jp9000 已提交
772 773
}

J
jp9000 已提交
774
static uint32_t scene_getheight(void *data)
J
jp9000 已提交
775
{
J
jp9000 已提交
776 777
	UNUSED_PARAMETER(data);
	return obs->video.base_height;
J
jp9000 已提交
778 779
}

780 781 782 783 784 785
static void apply_scene_item_audio_actions(struct obs_scene_item *item,
		float **p_buf, uint64_t ts, size_t sample_rate)
{
	bool cur_visible = item->visible;
	uint64_t frame_num = 0;
	size_t deref_count = 0;
786
	float *buf = NULL;
787

788 789 790 791 792
	if (p_buf) {
		if (!*p_buf)
			*p_buf = malloc(AUDIO_OUTPUT_FRAMES * sizeof(float));
		buf = *p_buf;
	}
793 794 795 796 797 798 799 800 801 802 803 804 805 806

	pthread_mutex_lock(&item->actions_mutex);

	for (size_t i = 0; i < item->audio_actions.num; i++) {
		struct item_action action = item->audio_actions.array[i];
		uint64_t timestamp = action.timestamp;
		uint64_t new_frame_num;

		if (timestamp < ts)
			timestamp = ts;

		new_frame_num = (timestamp - ts) * (uint64_t)sample_rate /
			1000000000ULL;

807
		if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
808 809 810 811 812 813 814 815
			break;

		da_erase(item->audio_actions, i--);

		item->visible = action.visible;
		if (!item->visible)
			deref_count++;

816
		if (buf && new_frame_num > frame_num) {
817 818 819 820 821 822 823
			for (; frame_num < new_frame_num; frame_num++)
				buf[frame_num] = cur_visible ? 1.0f : 0.0f;
		}

		cur_visible = item->visible;
	}

824 825 826 827
	if (buf) {
		for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
			buf[frame_num] = cur_visible ? 1.0f : 0.0f;
	}
828 829 830 831 832 833 834 835 836 837 838

	pthread_mutex_unlock(&item->actions_mutex);

	while (deref_count--) {
		if (os_atomic_dec_long(&item->active_refs) == 0) {
			obs_source_remove_active_child(item->parent->source,
					item->source);
		}
	}
}

839
static bool apply_scene_item_volume(struct obs_scene_item *item,
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
		float **buf, uint64_t ts, size_t sample_rate)
{
	bool actions_pending;
	struct item_action action;

	pthread_mutex_lock(&item->actions_mutex);

	actions_pending = item->audio_actions.num > 0;
	if (actions_pending)
		action = item->audio_actions.array[0];

	pthread_mutex_unlock(&item->actions_mutex);

	if (actions_pending) {
		uint64_t duration = (uint64_t)AUDIO_OUTPUT_FRAMES *
			1000000000ULL / (uint64_t)sample_rate;

857
		if (!ts || action.timestamp < (ts + duration)) {
858 859 860 861 862 863 864 865 866
			apply_scene_item_audio_actions(item, buf, ts,
					sample_rate);
			return true;
		}
	}

	return false;
}

867 868 869 870 871 872
static void process_all_audio_actions(struct obs_scene_item *item,
		size_t sample_rate)
{
	while (apply_scene_item_volume(item, NULL, 0, sample_rate));
}

873 874 875 876 877 878 879 880 881 882 883 884
static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
		size_t pos, size_t count)
{
	register float *out = p_out;
	register float *buf = buf_in + pos;
	register float *in = p_in + pos;
	register float *end = in + count;

	while (in < end)
		*(out++) += *(in++) * *(buf++);
}

885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
static inline void mix_audio(float *p_out, float *p_in,
		size_t pos, size_t count)
{
	register float *out = p_out;
	register float *in = p_in + pos;
	register float *end = in + count;

	while (in < end)
		*(out++) += *(in++);
}

static bool scene_audio_render(void *data, uint64_t *ts_out,
		struct obs_source_audio_mix *audio_output, uint32_t mixers,
		size_t channels, size_t sample_rate)
{
	uint64_t timestamp = 0;
901
	float *buf = NULL;
902 903 904 905 906 907 908 909 910 911 912 913
	struct obs_source_audio_mix child_audio;
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

	audio_lock(scene);

	item = scene->first_item;
	while (item) {
		if (!obs_source_audio_pending(item->source)) {
			uint64_t source_ts =
				obs_source_get_audio_timestamp(item->source);

914
			if (source_ts && (!timestamp || source_ts < timestamp))
915 916 917 918 919 920 921
				timestamp = source_ts;
		}

		item = item->next;
	}

	if (!timestamp) {
922 923 924 925 926 927 928 929
		/* just process all pending audio actions if no audio playing,
		 * otherwise audio actions will just never be processed */
		item = scene->first_item;
		while (item) {
			process_all_audio_actions(item, sample_rate);
			item = item->next;
		}

930 931 932 933 934 935 936 937
		audio_unlock(scene);
		return false;
	}

	item = scene->first_item;
	while (item) {
		uint64_t source_ts;
		size_t pos, count;
938 939 940 941
		bool apply_buf;

		apply_buf = apply_scene_item_volume(item, &buf, timestamp,
				sample_rate);
942 943 944 945 946 947 948

		if (obs_source_audio_pending(item->source)) {
			item = item->next;
			continue;
		}

		source_ts = obs_source_get_audio_timestamp(item->source);
949 950 951 952 953
		if (!source_ts) {
			item = item->next;
			continue;
		}

954 955 956 957
		pos = (size_t)ns_to_audio_frames(sample_rate,
				source_ts - timestamp);
		count = AUDIO_OUTPUT_FRAMES - pos;

958 959 960 961 962
		if (!apply_buf && !item->visible) {
			item = item->next;
			continue;
		}

963 964
		obs_source_get_audio_mix(item->source, &child_audio);
		for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
965 966 967
			if ((mixers & (1 << mix)) == 0)
				continue;

968 969 970 971
			for (size_t ch = 0; ch < channels; ch++) {
				float *out = audio_output->output[mix].data[ch];
				float *in = child_audio.output[mix].data[ch];

972 973 974 975 976
				if (apply_buf)
					mix_audio_with_buf(out, in, buf, pos,
							count);
				else
					mix_audio(out, in, pos, count);
977 978 979 980 981 982 983 984
			}
		}

		item = item->next;
	}

	*ts_out = timestamp;
	audio_unlock(scene);
985 986

	free(buf);
987 988 989
	return true;
}

990
const struct obs_source_info scene_info =
J
jp9000 已提交
991
{
992
	.id            = "scene",
993
	.type          = OBS_SOURCE_TYPE_SCENE,
994 995 996
	.output_flags  = OBS_SOURCE_VIDEO |
	                 OBS_SOURCE_CUSTOM_DRAW |
	                 OBS_SOURCE_COMPOSITE,
997 998 999
	.get_name      = scene_getname,
	.create        = scene_create,
	.destroy       = scene_destroy,
1000
	.video_tick    = scene_video_tick,
1001
	.video_render  = scene_video_render,
1002
	.audio_render  = scene_audio_render,
1003 1004 1005 1006
	.get_width     = scene_getwidth,
	.get_height    = scene_getheight,
	.load          = scene_load,
	.save          = scene_save,
1007 1008
	.enum_active_sources = scene_enum_active_sources,
	.enum_all_sources = scene_enum_all_sources
J
jp9000 已提交
1009 1010
};

1011
obs_scene_t *obs_scene_create(const char *name)
J
jp9000 已提交
1012
{
1013 1014
	struct obs_source *source = obs_source_create("scene", name, NULL,
			NULL);
1015
	return source->context.data;
J
jp9000 已提交
1016 1017
}

1018 1019 1020 1021 1022 1023 1024
obs_scene_t *obs_scene_create_private(const char *name)
{
	struct obs_source *source = obs_source_create_private("scene", name,
			NULL);
	return source->context.data;
}

1025
static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
1026 1027 1028
{
	struct obs_scene_item *item = scene->first_item;

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	while (item && idx--)
		item = item->next;
	return item ? item->source : NULL;
}

static inline obs_source_t *dup_child(struct darray *array, size_t idx,
		obs_scene_t *new_scene, bool private)
{
	DARRAY(struct obs_scene_item*) old_items;
	obs_source_t *source;

	old_items.da = *array;

	source = old_items.array[idx]->source;

	/* if the old item is referenced more than once in the old scene,
	 * make sure they're referenced similarly in the new scene to reduce
	 * load times */
	for (size_t i = 0; i < idx; i++) {
		struct obs_scene_item *item = old_items.array[i];
		if (item->source == source) {
			source = get_child_at_idx(new_scene, i);
			obs_source_addref(source);
			return source;
		}
	}

	return obs_source_duplicate(source, NULL, private);
}

static inline obs_source_t *new_ref(obs_source_t *source)
{
	obs_source_addref(source);
	return source;
}

obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
		enum obs_scene_duplicate_type type)
{
	bool make_unique  = ((int)type & (1<<0)) != 0;
	bool make_private = ((int)type & (1<<1)) != 0;
	DARRAY(struct obs_scene_item*) items;
	struct obs_scene *new_scene;
	struct obs_scene_item *item;
	struct obs_source *source;

	da_init(items);

	if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
		return NULL;

	/* --------------------------------- */

1082
	full_lock(scene);
1083

1084
	item = scene->first_item;
1085
	while (item) {
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
		da_push_back(items, &item);
		obs_sceneitem_addref(item);
		item = item->next;
	}

	full_unlock(scene);

	/* --------------------------------- */

	new_scene = make_private ?
		obs_scene_create_private(name) : obs_scene_create(name);

S
SuslikV 已提交
1098 1099
	obs_source_copy_filters(new_scene->source, scene->source);

1100 1101 1102
	obs_data_apply(new_scene->source->private_settings,
			scene->source->private_settings);

1103 1104 1105 1106 1107
	for (size_t i = 0; i < items.num; i++) {
		item = items.array[i];
		source = make_unique ?
			dup_child(&items.da, i, new_scene, make_private) :
			new_ref(item->source);
1108 1109 1110 1111 1112

		if (source) {
			struct obs_scene_item *new_item =
				obs_scene_add(new_scene, source);

1113
			if (!new_item) {
1114
				obs_source_release(source);
1115 1116 1117 1118 1119
				continue;
			}

			if (!item->user_visible)
				set_visibility(new_item, false);
1120

1121 1122
			new_item->selected = item->selected;
			new_item->pos = item->pos;
1123
			new_item->rot = item->rot;
1124 1125 1126 1127
			new_item->scale = item->scale;
			new_item->align = item->align;
			new_item->last_width = item->last_width;
			new_item->last_height = item->last_height;
1128 1129
			new_item->output_scale = item->output_scale;
			new_item->scale_filter = item->scale_filter;
1130 1131 1132 1133 1134 1135
			new_item->box_transform = item->box_transform;
			new_item->draw_transform = item->draw_transform;
			new_item->bounds_type = item->bounds_type;
			new_item->bounds_align = item->bounds_align;
			new_item->bounds = item->bounds;

1136 1137 1138
			new_item->toggle_visibility =
					OBS_INVALID_HOTKEY_PAIR_ID;

1139 1140
			obs_sceneitem_set_crop(new_item, &item->crop);

1141 1142 1143 1144 1145 1146 1147 1148
			if (!new_item->item_render &&
			    item_texture_enabled(new_item)) {
				obs_enter_graphics();
				new_item->item_render = gs_texrender_create(
						GS_RGBA, GS_ZS_NONE);
				obs_leave_graphics();
			}

1149 1150
			obs_source_release(source);
		}
1151 1152
	}

1153 1154
	for (size_t i = 0; i < items.num; i++)
		obs_sceneitem_release(items.array[i]);
1155

1156
	da_free(items);
1157 1158 1159
	return new_scene;
}

1160
void obs_scene_addref(obs_scene_t *scene)
1161
{
P
Palana 已提交
1162 1163
	if (scene)
		obs_source_addref(scene->source);
1164 1165
}

1166
void obs_scene_release(obs_scene_t *scene)
J
jp9000 已提交
1167
{
P
Palana 已提交
1168 1169
	if (scene)
		obs_source_release(scene->source);
J
jp9000 已提交
1170 1171
}

1172
obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
J
jp9000 已提交
1173
{
J
jp9000 已提交
1174
	return scene ? scene->source : NULL;
J
jp9000 已提交
1175 1176
}

1177
obs_scene_t *obs_scene_from_source(const obs_source_t *source)
1178
{
1179
	if (!source || source->info.id != scene_info.id)
1180 1181
		return NULL;

1182
	return source->context.data;
1183 1184
}

1185
obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
1186 1187 1188
{
	struct obs_scene_item *item;

J
jp9000 已提交
1189 1190 1191
	if (!scene)
		return NULL;

1192
	full_lock(scene);
1193 1194 1195

	item = scene->first_item;
	while (item) {
1196
		if (strcmp(item->source->context.name, name) == 0)
1197 1198 1199 1200 1201
			break;

		item = item->next;
	}

1202
	full_unlock(scene);
1203 1204 1205 1206

	return item;
}

J
jp9000 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
obs_sceneitem_t *obs_scene_find_sceneitem_by_id(obs_scene_t *scene, int64_t id)
{
	struct obs_scene_item *item;

	if (!scene)
		return NULL;

	full_lock(scene);

	item = scene->first_item;
	while (item) {
		if (item->id == id)
			break;

		item = item->next;
	}

	full_unlock(scene);

	return item;
}

1229 1230
void obs_scene_enum_items(obs_scene_t *scene,
		bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
1231 1232 1233 1234
		void *param)
{
	struct obs_scene_item *item;

J
jp9000 已提交
1235 1236 1237
	if (!scene || !callback)
		return;

1238
	full_lock(scene);
1239 1240 1241

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
1242 1243 1244 1245
		struct obs_scene_item *next = item->next;

		obs_sceneitem_addref(item);

1246 1247
		if (!callback(scene, item, param)) {
			obs_sceneitem_release(item);
1248
			break;
1249
		}
1250

J
jp9000 已提交
1251 1252 1253
		obs_sceneitem_release(item);

		item = next;
1254 1255
	}

1256
	full_unlock(scene);
1257 1258
}

P
Palana 已提交
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
{
	long owners = si->ref;
	while (owners > 0) {
		if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
			return si;

		owners = si->ref;
	}
	return NULL;
}

static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
		obs_hotkey_t *hotkey, bool pressed)
{
	UNUSED_PARAMETER(id);
	UNUSED_PARAMETER(hotkey);

	obs_sceneitem_t *si = sceneitem_get_ref(data);
1278
	if (pressed && si && !si->user_visible) {
P
Palana 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
		obs_sceneitem_set_visible(si, true);
		obs_sceneitem_release(si);
		return true;
	}

	obs_sceneitem_release(si);
	return false;
}

static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
		obs_hotkey_t *hotkey, bool pressed)
{
	UNUSED_PARAMETER(id);
	UNUSED_PARAMETER(hotkey);

	obs_sceneitem_t *si = sceneitem_get_ref(data);
1295
	if (pressed && si && si->user_visible) {
P
Palana 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
		obs_sceneitem_set_visible(si, false);
		obs_sceneitem_release(si);
		return true;
	}

	obs_sceneitem_release(si);
	return false;
}

static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
		const char *name)
{
	struct dstr show = {0};
	struct dstr hide = {0};
	struct dstr show_desc = {0};
	struct dstr hide_desc = {0};

	dstr_copy(&show, "libobs.show_scene_item.%1");
	dstr_replace(&show, "%1", name);
	dstr_copy(&hide, "libobs.hide_scene_item.%1");
	dstr_replace(&hide, "%1", name);

	dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
	dstr_replace(&show_desc, "%1", name);
	dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
	dstr_replace(&hide_desc, "%1", name);

	item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
			show.array, show_desc.array,
			hide.array, hide_desc.array,
			hotkey_show_sceneitem, hotkey_hide_sceneitem,
			item, item);

	dstr_free(&show);
	dstr_free(&hide);
	dstr_free(&show_desc);
	dstr_free(&hide_desc);
}

1335 1336 1337 1338 1339 1340
static inline bool source_has_audio(obs_source_t *source)
{
	return (source->info.output_flags &
		(OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
}

1341
obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
J
jp9000 已提交
1342
{
1343
	struct obs_scene_item *last;
1344
	struct obs_scene_item *item;
1345 1346
	struct calldata params;
	uint8_t stack[128];
1347 1348 1349 1350 1351 1352
	pthread_mutex_t mutex;

	struct item_action action = {
		.visible = true,
		.timestamp = os_gettime_ns()
	};
1353

1354 1355 1356 1357
	if (!scene)
		return NULL;

	if (!source) {
J
jp9000 已提交
1358
		blog(LOG_ERROR, "Tried to add a NULL source to a scene");
1359 1360 1361
		return NULL;
	}

1362 1363 1364 1365 1366
	if (pthread_mutex_init(&mutex, NULL) != 0) {
		blog(LOG_WARNING, "Failed to create scene item mutex");
		return NULL;
	}

1367
	if (!obs_source_add_active_child(scene->source, source)) {
J
jp9000 已提交
1368 1369
		blog(LOG_WARNING, "Failed to add source to scene due to "
		                  "infinite source recursion");
1370
		pthread_mutex_destroy(&mutex);
J
jp9000 已提交
1371 1372 1373
		return NULL;
	}

1374
	item = bzalloc(sizeof(struct obs_scene_item));
J
jp9000 已提交
1375
	item->source  = source;
J
jp9000 已提交
1376
	item->id      = ++scene->id_counter;
J
jp9000 已提交
1377
	item->parent  = scene;
J
jp9000 已提交
1378
	item->ref     = 1;
1379
	item->align   = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
1380 1381
	item->actions_mutex = mutex;
	item->user_visible = true;
1382
	item->locked = false;
1383
	item->private_settings = obs_data_create();
1384
	os_atomic_set_long(&item->active_refs, 1);
J
jp9000 已提交
1385
	vec2_set(&item->scale, 1.0f, 1.0f);
1386 1387
	matrix4_identity(&item->draw_transform);
	matrix4_identity(&item->box_transform);
J
jp9000 已提交
1388

1389
	obs_source_addref(source);
1390

1391 1392 1393 1394 1395 1396 1397
	if (source_has_audio(source)) {
		item->visible = false;
		da_push_back(item->audio_actions, &action);
	} else {
		item->visible = true;
	}

1398 1399 1400 1401 1402 1403
	if (item_texture_enabled(item)) {
		obs_enter_graphics();
		item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
		obs_leave_graphics();
	}

1404
	full_lock(scene);
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415

	last = scene->first_item;
	if (!last) {
		scene->first_item = item;
	} else {
		while (last->next)
			last = last->next;

		last->next = item;
		item->prev = last;
	}
J
jp9000 已提交
1416

1417
	full_unlock(scene);
1418

1419 1420
	if (!scene->source->context.private)
		init_hotkeys(scene, item, obs_source_get_name(source));
P
Palana 已提交
1421

1422
	calldata_init_fixed(&params, stack, sizeof(stack));
1423 1424
	calldata_set_ptr(&params, "scene", scene);
	calldata_set_ptr(&params, "item", item);
1425 1426
	signal_handler_signal(scene->source->context.signals, "item_add",
			&params);
J
jp9000 已提交
1427

J
jp9000 已提交
1428 1429 1430
	return item;
}

1431
static void obs_sceneitem_destroy(obs_sceneitem_t *item)
J
jp9000 已提交
1432
{
1433
	if (item) {
1434
		if (item->item_render) {
1435
			obs_enter_graphics();
1436
			gs_texrender_destroy(item->item_render);
1437 1438
			obs_leave_graphics();
		}
1439
		obs_data_release(item->private_settings);
P
Palana 已提交
1440
		obs_hotkey_pair_unregister(item->toggle_visibility);
1441
		pthread_mutex_destroy(&item->actions_mutex);
1442
		if (item->source)
J
jp9000 已提交
1443
			obs_source_release(item->source);
1444
		da_free(item->audio_actions);
J
jp9000 已提交
1445
		bfree(item);
J
jp9000 已提交
1446 1447 1448
	}
}

1449
void obs_sceneitem_addref(obs_sceneitem_t *item)
J
jp9000 已提交
1450
{
P
Palana 已提交
1451
	if (item)
J
jp9000 已提交
1452
		os_atomic_inc_long(&item->ref);
J
jp9000 已提交
1453
}
1454

1455
void obs_sceneitem_release(obs_sceneitem_t *item)
J
jp9000 已提交
1456
{
P
Palana 已提交
1457 1458
	if (!item)
		return;
P
Palana 已提交
1459

J
jp9000 已提交
1460
	if (os_atomic_dec_long(&item->ref) == 0)
P
Palana 已提交
1461
		obs_sceneitem_destroy(item);
1462 1463
}

1464
void obs_sceneitem_remove(obs_sceneitem_t *item)
J
jp9000 已提交
1465
{
1466
	obs_scene_t *scene;
J
jp9000 已提交
1467

1468
	if (!item)
J
jp9000 已提交
1469 1470
		return;

1471
	scene = item->parent;
J
jp9000 已提交
1472

1473
	full_lock(scene);
J
jp9000 已提交
1474

J
jp9000 已提交
1475
	if (item->removed) {
1476
		if (scene)
1477
			full_unlock(scene);
1478 1479 1480
		return;
	}

J
jp9000 已提交
1481 1482
	item->removed = true;

1483 1484
	assert(scene != NULL);
	assert(scene->source != NULL);
1485 1486

	set_visibility(item, false);
1487

1488
	signal_item_remove(item);
J
jp9000 已提交
1489
	detach_sceneitem(item);
1490

1491
	full_unlock(scene);
J
jp9000 已提交
1492 1493 1494 1495

	obs_sceneitem_release(item);
}

1496
obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
J
jp9000 已提交
1497
{
J
jp9000 已提交
1498
	return item ? item->parent : NULL;
J
jp9000 已提交
1499 1500
}

1501
obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
1502
{
J
jp9000 已提交
1503
	return item ? item->source : NULL;
J
jp9000 已提交
1504 1505
}

1506
void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
1507
{
1508 1509
	struct calldata params;
	uint8_t stack[128];
1510 1511
	const char *command = select ? "item_select" : "item_deselect";

1512
	if (!item || item->selected == select || !item->parent)
1513 1514 1515 1516
		return;

	item->selected = select;

1517
	calldata_init_fixed(&params, stack, sizeof(stack));
1518 1519
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item",  item);
1520 1521 1522 1523
	signal_handler_signal(item->parent->source->context.signals,
			command, &params);
}

1524
bool obs_sceneitem_selected(const obs_sceneitem_t *item)
1525 1526 1527 1528
{
	return item ? item->selected : false;
}

1529
void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
J
jp9000 已提交
1530
{
1531
	if (item) {
J
jp9000 已提交
1532
		vec2_copy(&item->pos, pos);
1533 1534
		update_item_transform(item);
	}
J
jp9000 已提交
1535 1536
}

1537
void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
J
jp9000 已提交
1538
{
1539
	if (item) {
J
jp9000 已提交
1540
		item->rot = rot;
1541 1542
		update_item_transform(item);
	}
J
jp9000 已提交
1543 1544
}

1545
void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
J
jp9000 已提交
1546
{
1547 1548 1549 1550
	if (item) {
		vec2_copy(&item->scale, scale);
		update_item_transform(item);
	}
J
jp9000 已提交
1551 1552
}

1553
void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
J
jp9000 已提交
1554
{
1555 1556 1557 1558
	if (item) {
		item->align = alignment;
		update_item_transform(item);
	}
J
jp9000 已提交
1559 1560
}

S
Socapex 已提交
1561
static inline void signal_reorder(struct obs_scene_item *item)
J
jp9000 已提交
1562
{
1563
	const char *command = NULL;
1564 1565
	struct calldata params;
	uint8_t stack[128];
J
jp9000 已提交
1566

S
Socapex 已提交
1567
	command = "reorder";
J
jp9000 已提交
1568

1569
	calldata_init_fixed(&params, stack, sizeof(stack));
1570
	calldata_set_ptr(&params, "scene", item->parent);
J
jp9000 已提交
1571 1572 1573 1574 1575

	signal_handler_signal(item->parent->source->context.signals,
			command, &params);
}

1576
void obs_sceneitem_set_order(obs_sceneitem_t *item,
J
jp9000 已提交
1577
		enum obs_order_movement movement)
J
jp9000 已提交
1578
{
J
jp9000 已提交
1579 1580
	if (!item) return;

J
jp9000 已提交
1581
	struct obs_scene_item *next, *prev;
J
jp9000 已提交
1582 1583
	struct obs_scene *scene = item->parent;

J
jp9000 已提交
1584
	obs_scene_addref(scene);
1585
	full_lock(scene);
1586

J
jp9000 已提交
1587 1588 1589
	next = item->next;
	prev = item->prev;

1590 1591
	detach_sceneitem(item);

J
jp9000 已提交
1592
	if (movement == OBS_ORDER_MOVE_DOWN) {
J
jp9000 已提交
1593
		attach_sceneitem(scene, item, prev ? prev->prev : NULL);
J
jp9000 已提交
1594

J
jp9000 已提交
1595
	} else if (movement == OBS_ORDER_MOVE_UP) {
J
jp9000 已提交
1596
		attach_sceneitem(scene, item, next ? next : prev);
J
jp9000 已提交
1597

J
jp9000 已提交
1598
	} else if (movement == OBS_ORDER_MOVE_TOP) {
J
jp9000 已提交
1599
		struct obs_scene_item *last = next;
1600
		if (!last) {
J
jp9000 已提交
1601
			last = prev;
1602 1603 1604 1605
		} else {
			while (last->next)
				last = last->next;
		}
J
jp9000 已提交
1606

J
jp9000 已提交
1607
		attach_sceneitem(scene, item, last);
1608

J
jp9000 已提交
1609
	} else if (movement == OBS_ORDER_MOVE_BOTTOM) {
J
jp9000 已提交
1610
		attach_sceneitem(scene, item, NULL);
J
jp9000 已提交
1611
	}
1612

S
Socapex 已提交
1613
	signal_reorder(item);
J
jp9000 已提交
1614

1615
	full_unlock(scene);
1616
	obs_scene_release(scene);
J
jp9000 已提交
1617 1618
}

1619 1620 1621 1622 1623 1624 1625 1626 1627
void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
		int position)
{
	if (!item) return;

	struct obs_scene *scene = item->parent;
	struct obs_scene_item *next;

	obs_scene_addref(scene);
1628
	full_lock(scene);
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646

	detach_sceneitem(item);
	next = scene->first_item;

	if (position == 0) {
		attach_sceneitem(scene, item, NULL);
	} else {
		for (int i = position; i > 1; --i) {
			if (next->next == NULL)
				break;
			next = next->next;
		}

		attach_sceneitem(scene, item, next);
	}

	signal_reorder(item);

1647
	full_unlock(scene);
1648 1649 1650
	obs_scene_release(scene);
}

1651
void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
1652 1653 1654 1655 1656 1657 1658 1659
		enum obs_bounds_type type)
{
	if (item) {
		item->bounds_type = type;
		update_item_transform(item);
	}
}

1660
void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
1661 1662 1663 1664 1665 1666 1667 1668
		uint32_t alignment)
{
	if (item) {
		item->bounds_align = alignment;
		update_item_transform(item);
	}
}

1669
void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
1670 1671 1672 1673 1674 1675 1676
{
	if (item) {
		item->bounds = *bounds;
		update_item_transform(item);
	}
}

1677
void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
J
jp9000 已提交
1678
{
J
jp9000 已提交
1679 1680
	if (item)
		vec2_copy(pos, &item->pos);
J
jp9000 已提交
1681 1682
}

1683
float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
J
jp9000 已提交
1684
{
J
jp9000 已提交
1685
	return item ? item->rot : 0.0f;
J
jp9000 已提交
1686 1687
}

1688
void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
J
jp9000 已提交
1689
{
J
jp9000 已提交
1690
	if (item)
1691
		vec2_copy(scale, &item->scale);
J
jp9000 已提交
1692 1693
}

1694
uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
1695 1696 1697 1698
{
	return item ? item->align : 0;
}

1699
enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
1700 1701 1702 1703
{
	return item ? item->bounds_type : OBS_BOUNDS_NONE;
}

1704
uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
1705 1706 1707 1708
{
	return item ? item->bounds_align : 0;
}

1709
void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
J
jp9000 已提交
1710
{
J
jp9000 已提交
1711
	if (item)
1712 1713 1714
		*bounds = item->bounds;
}

1715
void obs_sceneitem_get_info(const obs_sceneitem_t *item,
1716
		struct obs_transform_info *info)
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
{
	if (item && info) {
		info->pos              = item->pos;
		info->rot              = item->rot;
		info->scale            = item->scale;
		info->alignment        = item->align;
		info->bounds_type      = item->bounds_type;
		info->bounds_alignment = item->bounds_align;
		info->bounds           = item->bounds;
	}
}

1729
void obs_sceneitem_set_info(obs_sceneitem_t *item,
1730
		const struct obs_transform_info *info)
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
{
	if (item && info) {
		item->pos          = info->pos;
		item->rot          = info->rot;
		item->scale        = info->scale;
		item->align        = info->alignment;
		item->bounds_type  = info->bounds_type;
		item->bounds_align = info->bounds_alignment;
		item->bounds       = info->bounds;
		update_item_transform(item);
	}
}

1744
void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
1745 1746 1747 1748 1749 1750
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->draw_transform);
}

1751
void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
1752 1753 1754 1755
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->box_transform);
J
jp9000 已提交
1756
}
1757 1758 1759

bool obs_sceneitem_visible(const obs_sceneitem_t *item)
{
1760
	return item ? item->user_visible : false;
1761 1762
}

1763
bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
1764
{
1765 1766
	struct calldata cd;
	uint8_t stack[256];
1767 1768 1769 1770
	struct item_action action = {
		.visible = visible,
		.timestamp = os_gettime_ns()
	};
1771 1772

	if (!item)
1773
		return false;
1774

1775
	if (item->user_visible == visible)
1776
		return false;
1777 1778

	if (!item->parent)
1779 1780 1781
		return false;

	if (visible) {
1782 1783 1784 1785 1786 1787 1788
		if (os_atomic_inc_long(&item->active_refs) == 1) {
			if (!obs_source_add_active_child(item->parent->source,
						item->source)) {
				os_atomic_dec_long(&item->active_refs);
				return false;
			}
		}
1789 1790
	}

1791
	item->user_visible = visible;
1792

1793
	calldata_init_fixed(&cd, stack, sizeof(stack));
1794 1795 1796 1797 1798 1799
	calldata_set_ptr(&cd, "scene", item->parent);
	calldata_set_ptr(&cd, "item", item);
	calldata_set_bool(&cd, "visible", visible);

	signal_handler_signal(item->parent->source->context.signals,
			"item_visible", &cd);
1800 1801 1802 1803 1804 1805 1806 1807

	if (source_has_audio(item->source)) {
		pthread_mutex_lock(&item->actions_mutex);
		da_push_back(item->audio_actions, &action);
		pthread_mutex_unlock(&item->actions_mutex);
	} else {
		set_visibility(item, visible);
	}
1808
	return true;
1809
}
P
Palana 已提交
1810

1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
bool obs_sceneitem_locked(const obs_sceneitem_t *item)
{
	return item ? item->locked : false;
}

bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
{
	if (!item)
		return false;

	if (item->locked == lock)
		return false;

	if (!item->parent)
		return false;

	item->locked = lock;

	return true;
}

P
Palana 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
		size_t size, bool *order_matches)
{
	obs_sceneitem_t *item = scene->first_item;

	size_t count = 0;
	while (item) {
		bool found = false;
		for (size_t i = 0; i < size; i++) {
			if (items[i] != item)
				continue;

			if (count != i)
				*order_matches = false;

			found = true;
			break;
		}

		if (!found)
			return false;

		item = item->next;
		count += 1;
	}

	return count == size;
}

bool obs_scene_reorder_items(obs_scene_t *scene,
		obs_sceneitem_t * const *item_order, size_t item_order_size)
{
	if (!scene || !item_order_size)
		return false;

	obs_scene_addref(scene);
1868
	full_lock(scene);
P
Palana 已提交
1869 1870 1871 1872

	bool order_matches = true;
	if (!sceneitems_match(scene, item_order, item_order_size,
				&order_matches) || order_matches) {
1873
		full_unlock(scene);
P
Palana 已提交
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
		obs_scene_release(scene);
		return false;
	}

	scene->first_item = item_order[0];

	obs_sceneitem_t *prev = NULL;
	for (size_t i = 0; i < item_order_size; i++) {
		item_order[i]->prev = prev;
		item_order[i]->next = NULL;

		if (prev)
			prev->next = item_order[i];

		prev = item_order[i];
	}

	signal_reorder(scene->first_item);

1893
	full_unlock(scene);
P
Palana 已提交
1894 1895 1896 1897
	obs_scene_release(scene);
	return true;
}

P
Palana 已提交
1898 1899 1900 1901 1902 1903 1904
void obs_scene_atomic_update(obs_scene_t *scene,
		obs_scene_atomic_update_func func, void *data)
{
	if (!scene)
		return;

	obs_scene_addref(scene);
1905
	full_lock(scene);
P
Palana 已提交
1906
	func(data, scene);
1907
	full_unlock(scene);
P
Palana 已提交
1908 1909
	obs_scene_release(scene);
}
1910

1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
		const struct obs_sceneitem_crop *crop2)
{
	return crop1->left   == crop2->left  &&
	       crop1->right  == crop2->right &&
	       crop1->top    == crop2->top   &&
	       crop1->bottom == crop2->bottom;
}

void obs_sceneitem_set_crop(obs_sceneitem_t *item,
		const struct obs_sceneitem_crop *crop)
{
1923
	bool item_tex_now_enabled;
1924 1925 1926 1927 1928 1929 1930 1931

	if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
		return;
	if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
		return;
	if (crop_equal(crop, &item->crop))
		return;

1932 1933
	item_tex_now_enabled = crop_enabled(crop) ||
		scale_filter_enabled(item) || item_is_scene(item);
1934 1935 1936

	obs_enter_graphics();

1937 1938 1939
	if (!item_tex_now_enabled) {
		gs_texrender_destroy(item->item_render);
		item->item_render = NULL;
1940

1941 1942
	} else if (!item->item_render) {
		item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
	}

	memcpy(&item->crop, crop, sizeof(*crop));

	if (item->crop.left < 0) item->crop.left = 0;
	if (item->crop.right < 0) item->crop.right = 0;
	if (item->crop.top < 0) item->crop.top = 0;
	if (item->crop.bottom < 0) item->crop.bottom = 0;
	obs_leave_graphics();

	update_item_transform(item);
}

void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
		struct obs_sceneitem_crop *crop)
{
	if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
		return;
	if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
		return;

	memcpy(crop, &item->crop, sizeof(*crop));
}

1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
		enum obs_scale_type filter)
{
	if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
		return;

	item->scale_filter = filter;

	obs_enter_graphics();

	if (!item_texture_enabled(item)) {
		gs_texrender_destroy(item->item_render);
		item->item_render = NULL;

	} else if (!item->item_render) {
		item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
	}

	obs_leave_graphics();

	update_item_transform(item);
}

enum obs_scale_type obs_sceneitem_get_scale_filter(
		obs_sceneitem_t *item)
{
	return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter") ?
		item->scale_filter : OBS_SCALE_DISABLE;
}

1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
{
	if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
		return;

	os_atomic_inc_long(&item->defer_update);
}

void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
{
	if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
		return;

	if (os_atomic_dec_long(&item->defer_update) == 0)
		update_item_transform(item);
}
J
jp9000 已提交
2013 2014 2015 2016 2017 2018 2019 2020

int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
{
	if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
		return 0;

	return item->id;
}
2021 2022 2023 2024 2025 2026 2027 2028 2029

obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
{
	if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
		return NULL;

	obs_data_addref(item->private_settings);
	return item->private_settings;
}