obs-scene.c 33.1 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
static const char *obs_scene_signals[] = {
	"void item_add(ptr scene, ptr item)",
	"void item_remove(ptr scene, ptr item)",
S
Socapex 已提交
26
	"void reorder(ptr scene)",
27
	"void item_visible(ptr scene, ptr item, bool visible)",
28 29 30
	"void item_select(ptr scene, ptr item)",
	"void item_deselect(ptr scene, ptr item)",
	"void item_transform(ptr scene, ptr item)",
31 32 33
	NULL
};

J
jp9000 已提交
34
static inline void signal_item_remove(struct obs_scene_item *item)
J
jp9000 已提交
35
{
J
jp9000 已提交
36
	struct calldata params = {0};
37 38
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item", item);
J
jp9000 已提交
39

40 41
	signal_handler_signal(item->parent->source->context.signals,
			"item_remove", &params);
J
jp9000 已提交
42
	calldata_free(&params);
J
jp9000 已提交
43 44
}

45
static const char *scene_getname(void *unused)
46
{
47
	UNUSED_PARAMETER(unused);
48
	return "Scene";
49 50
}

51
static void *scene_create(obs_data_t *settings, struct obs_source *source)
J
jp9000 已提交
52
{
P
Palana 已提交
53
	pthread_mutexattr_t attr;
J
jp9000 已提交
54
	struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
55 56 57
	scene->source     = source;
	scene->first_item = NULL;

58
	signal_handler_add_array(obs_source_get_signal_handler(source),
59 60
			obs_scene_signals);

P
Palana 已提交
61 62 63 64
	if (pthread_mutexattr_init(&attr) != 0)
		goto fail;
	if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
		goto fail;
65 66 67 68 69 70 71 72
	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 已提交
73
		goto fail;
74
	}
J
jp9000 已提交
75

J
jp9000 已提交
76
	UNUSED_PARAMETER(settings);
J
jp9000 已提交
77
	return scene;
P
Palana 已提交
78 79 80 81 82

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

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#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);
}

102
static void remove_all_items(struct obs_scene *scene)
J
jp9000 已提交
103
{
J
jp9000 已提交
104
	struct obs_scene_item *item;
J
jp9000 已提交
105

106
	full_lock(scene);
J
jp9000 已提交
107

J
jp9000 已提交
108 109
	item = scene->first_item;

110 111 112 113
	while (item) {
		struct obs_scene_item *del_item = item;
		item = item->next;

J
jp9000 已提交
114
		obs_sceneitem_remove(del_item);
115
	}
J
jp9000 已提交
116

117
	full_unlock(scene);
118 119 120 121 122
}

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

124
	remove_all_items(scene);
125 126
	pthread_mutex_destroy(&scene->video_mutex);
	pthread_mutex_destroy(&scene->audio_mutex);
J
jp9000 已提交
127 128 129
	bfree(scene);
}

130 131 132 133 134 135
static void scene_enum_sources(void *data,
		obs_source_enum_proc_t enum_callback,
		void *param)
{
	struct obs_scene *scene = data;
	struct obs_scene_item *item;
136
	struct obs_scene_item *next;
137

138
	full_lock(scene);
139
	item = scene->first_item;
140

141
	while (item) {
142
		next = item->next;
143 144

		obs_sceneitem_addref(item);
145
		if (os_atomic_load_long(&item->active_refs) > 0)
146
			enum_callback(scene->source, item->source, param);
147 148 149 150 151
		obs_sceneitem_release(item);

		item = next;
	}

152
	full_unlock(scene);
153 154
}

155 156 157 158 159 160 161 162 163
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 已提交
164 165

	item->parent = NULL;
166 167
}

J
jp9000 已提交
168 169
static inline void attach_sceneitem(struct obs_scene *parent,
		struct obs_scene_item *item, struct obs_scene_item *prev)
170
{
J
jp9000 已提交
171 172
	item->prev   = prev;
	item->parent = parent;
173 174 175 176 177 178 179

	if (prev) {
		item->next = prev->next;
		if (prev->next)
			prev->next->prev = item;
		prev->next = item;
	} else {
180 181 182 183
		item->next = parent->first_item;
		if (parent->first_item)
			parent->first_item->prev = item;
		parent->first_item = item;
184 185 186
	}
}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
{
	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 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
	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) {
217 218 219 220 221 222 223 224 225 226 227 228
		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 已提交
229
	} else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
230 231
		vec2_mulf(scale, scale, item->bounds.x / width);

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

J
jp9000 已提交
235
	} else if (bounds_type == OBS_BOUNDS_STRETCH) {
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
		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);
}

static void update_item_transform(struct obs_scene_item *item)
{
253 254
	uint32_t        width         = obs_source_get_width(item->source);
	uint32_t        height        = obs_source_get_height(item->source);
255 256
	uint32_t        cx            = width;
	uint32_t        cy            = height;
J
jp9000 已提交
257 258
	struct vec2     base_origin;
	struct vec2     origin;
259 260 261
	struct vec2     scale         = item->scale;
	struct calldata params        = {0};

J
jp9000 已提交
262 263 264
	vec2_zero(&base_origin);
	vec2_zero(&origin);

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	/* ----------------------- */

	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);

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

	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;

312 313
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item", item);
314 315 316 317 318 319 320
	signal_handler_signal(item->parent->source->context.signals,
			"item_transform", &params);
	calldata_free(&params);
}

static inline bool source_size_changed(struct obs_scene_item *item)
{
321 322
	uint32_t width  = obs_source_get_width(item->source);
	uint32_t height = obs_source_get_height(item->source);
323 324 325 326

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

327
static void scene_video_render(void *data, gs_effect_t *effect)
J
jp9000 已提交
328
{
329 330 331
	struct obs_scene *scene = data;
	struct obs_scene_item *item;

332
	video_lock(scene);
333
	item = scene->first_item;
J
jp9000 已提交
334

J
jp9000 已提交
335
	gs_blend_state_push();
336
	gs_reset_blend_state();
J
jp9000 已提交
337

338
	while (item) {
339
		if (obs_source_removed(item->source)) {
340 341 342
			struct obs_scene_item *del_item = item;
			item = item->next;

J
jp9000 已提交
343
			obs_sceneitem_remove(del_item);
344 345 346
			continue;
		}

347 348
		if (source_size_changed(item))
			update_item_transform(item);
J
jp9000 已提交
349

350
		if (item->user_visible) {
351 352 353 354 355
			gs_matrix_push();
			gs_matrix_mul(&item->draw_transform);
			obs_source_video_render(item->source);
			gs_matrix_pop();
		}
356 357

		item = item->next;
J
jp9000 已提交
358
	}
359

J
jp9000 已提交
360 361
	gs_blend_state_pop();

362
	video_unlock(scene);
J
jp9000 已提交
363 364 365 366

	UNUSED_PARAMETER(effect);
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
static inline void set_visibility(struct obs_scene_item *item, bool vis)
{
	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);
}

388
static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
389
{
J
jp9000 已提交
390
	const char            *name = obs_data_get_string(item_data, "name");
391
	obs_source_t          *source = obs_get_source_by_name(name);
392
	struct obs_scene_item *item;
393
	bool visible;
394 395 396 397 398 399 400 401

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

	item = obs_scene_add(scene, source);
P
Palana 已提交
402 403 404 405 406 407 408 409
	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;
	}
410

411 412 413
	obs_data_set_default_int(item_data, "align",
			OBS_ALIGN_TOP | OBS_ALIGN_LEFT);

J
jp9000 已提交
414 415
	item->rot     = (float)obs_data_get_double(item_data, "rot");
	item->align   = (uint32_t)obs_data_get_int(item_data, "align");
416
	visible = obs_data_get_bool(item_data, "visible");
417 418
	obs_data_get_vec2(item_data, "pos",    &item->pos);
	obs_data_get_vec2(item_data, "scale",  &item->scale);
419

420
	set_visibility(item, visible);
421

422
	item->bounds_type =
J
jp9000 已提交
423 424
		(enum obs_bounds_type)obs_data_get_int(item_data,
				"bounds_type");
425
	item->bounds_align =
J
jp9000 已提交
426
		(uint32_t)obs_data_get_int(item_data, "bounds_align");
427 428
	obs_data_get_vec2(item_data, "bounds", &item->bounds);

429
	obs_source_release(source);
430 431

	update_item_transform(item);
432 433
}

434
static void scene_load(void *scene, obs_data_t *settings)
435
{
436
	obs_data_array_t *items = obs_data_get_array(settings, "items");
437 438 439 440 441 442 443 444 445
	size_t           count, i;

	remove_all_items(scene);

	if (!items) return;

	count = obs_data_array_count(items);

	for (i = 0; i < count; i++) {
446
		obs_data_t *item_data = obs_data_array_item(items, i);
447 448 449 450 451 452 453
		scene_load_item(scene, item_data);
		obs_data_release(item_data);
	}

	obs_data_array_release(items);
}

454 455
static void scene_save_item(obs_data_array_t *array,
		struct obs_scene_item *item)
456
{
457
	obs_data_t *item_data = obs_data_create();
458
	const char *name     = obs_source_get_name(item->source);
459

J
jp9000 已提交
460
	obs_data_set_string(item_data, "name",         name);
461
	obs_data_set_bool  (item_data, "visible",      item->user_visible);
J
jp9000 已提交
462
	obs_data_set_double(item_data, "rot",          item->rot);
463 464
	obs_data_set_vec2 (item_data, "pos",          &item->pos);
	obs_data_set_vec2 (item_data, "scale",        &item->scale);
J
jp9000 已提交
465 466 467
	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);
468
	obs_data_set_vec2 (item_data, "bounds",       &item->bounds);
469 470 471 472 473

	obs_data_array_push_back(array, item_data);
	obs_data_release(item_data);
}

474
static void scene_save(void *data, obs_data_t *settings)
475 476
{
	struct obs_scene      *scene = data;
477
	obs_data_array_t      *array  = obs_data_array_create();
478 479
	struct obs_scene_item *item;

480
	full_lock(scene);
481 482 483

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
484
		scene_save_item(array, item);
485 486 487
		item = item->next;
	}

488
	full_unlock(scene);
489

J
jp9000 已提交
490
	obs_data_set_array(settings, "items", array);
491 492 493
	obs_data_array_release(array);
}

J
jp9000 已提交
494 495 496 497
static uint32_t scene_getwidth(void *data)
{
	UNUSED_PARAMETER(data);
	return obs->video.base_width;
J
jp9000 已提交
498 499
}

J
jp9000 已提交
500
static uint32_t scene_getheight(void *data)
J
jp9000 已提交
501
{
J
jp9000 已提交
502 503
	UNUSED_PARAMETER(data);
	return obs->video.base_height;
J
jp9000 已提交
504 505
}

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
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;
	float *buf;

	if (!*p_buf)
		*p_buf = malloc(AUDIO_OUTPUT_FRAMES * sizeof(float));
	buf = *p_buf;

	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;

		if (new_frame_num >= AUDIO_OUTPUT_FRAMES)
			break;

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

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

		if (new_frame_num > frame_num) {
			for (; frame_num < new_frame_num; frame_num++)
				buf[frame_num] = cur_visible ? 1.0f : 0.0f;
		}

		cur_visible = item->visible;
	}

	for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
		buf[frame_num] = cur_visible ? 1.0f : 0.0f;

	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);
		}
	}
}

static inline bool apply_scene_item_volume(struct obs_scene_item *item,
		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;

		if (action.timestamp < (ts + duration)) {
			apply_scene_item_audio_actions(item, buf, ts,
					sample_rate);
			return true;
		}
	}

	return false;
}

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++);
}

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
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;
617
	float *buf = NULL;
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	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);

			if (!timestamp || source_ts < timestamp)
				timestamp = source_ts;
		}

		item = item->next;
	}

	if (!timestamp) {
		audio_unlock(scene);
		return false;
	}

	item = scene->first_item;
	while (item) {
		uint64_t source_ts;
		size_t pos, count;
646 647 648 649
		bool apply_buf;

		apply_buf = apply_scene_item_volume(item, &buf, timestamp,
				sample_rate);
650 651 652 653 654 655 656 657 658 659 660

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

		source_ts = obs_source_get_audio_timestamp(item->source);
		pos = (size_t)ns_to_audio_frames(sample_rate,
				source_ts - timestamp);
		count = AUDIO_OUTPUT_FRAMES - pos;

661 662 663 664 665
		if (!apply_buf && !item->visible) {
			item = item->next;
			continue;
		}

666 667
		obs_source_get_audio_mix(item->source, &child_audio);
		for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
668 669 670
			if ((mixers & (1 << mix)) == 0)
				continue;

671 672 673 674
			for (size_t ch = 0; ch < channels; ch++) {
				float *out = audio_output->output[mix].data[ch];
				float *in = child_audio.output[mix].data[ch];

675 676 677 678 679
				if (apply_buf)
					mix_audio_with_buf(out, in, buf, pos,
							count);
				else
					mix_audio(out, in, pos, count);
680 681 682 683 684 685 686 687
			}
		}

		item = item->next;
	}

	*ts_out = timestamp;
	audio_unlock(scene);
688 689

	free(buf);
690 691 692
	return true;
}

693
const struct obs_source_info scene_info =
J
jp9000 已提交
694
{
695 696
	.id            = "scene",
	.type          = OBS_SOURCE_TYPE_INPUT,
697 698 699
	.output_flags  = OBS_SOURCE_VIDEO |
	                 OBS_SOURCE_CUSTOM_DRAW |
	                 OBS_SOURCE_COMPOSITE,
700 701 702 703
	.get_name      = scene_getname,
	.create        = scene_create,
	.destroy       = scene_destroy,
	.video_render  = scene_video_render,
704
	.audio_render  = scene_audio_render,
705 706 707 708
	.get_width     = scene_getwidth,
	.get_height    = scene_getheight,
	.load          = scene_load,
	.save          = scene_save,
709
	.enum_active_sources = scene_enum_sources
J
jp9000 已提交
710 711
};

712
obs_scene_t *obs_scene_create(const char *name)
J
jp9000 已提交
713
{
714 715
	struct obs_source *source = obs_source_create("scene", name, NULL,
			NULL);
716
	return source->context.data;
J
jp9000 已提交
717 718
}

719 720 721 722 723
obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name)
{
	struct obs_scene *new_scene = obs_scene_create(name);
	struct obs_scene_item *item = scene->first_item;

724
	full_lock(scene);
725 726 727 728 729 730 731 732

	while (item) {
		struct obs_source *source = item->source;

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

733 734 735 736 737 738 739
			if (!new_item) {
				item = item->next;
				continue;
			}

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

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
			new_item->selected = item->selected;
			new_item->pos = item->pos;
			new_item->scale = item->scale;
			new_item->align = item->align;
			new_item->last_width = item->last_width;
			new_item->last_height = item->last_height;
			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;
		}

		item = item->next;
	}

757
	full_unlock(scene);
758 759 760 761

	return new_scene;
}

762
void obs_scene_addref(obs_scene_t *scene)
763
{
P
Palana 已提交
764 765
	if (scene)
		obs_source_addref(scene->source);
766 767
}

768
void obs_scene_release(obs_scene_t *scene)
J
jp9000 已提交
769
{
P
Palana 已提交
770 771
	if (scene)
		obs_source_release(scene->source);
J
jp9000 已提交
772 773
}

774
obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
J
jp9000 已提交
775
{
J
jp9000 已提交
776
	return scene ? scene->source : NULL;
J
jp9000 已提交
777 778
}

779
obs_scene_t *obs_scene_from_source(const obs_source_t *source)
780
{
781
	if (!source || source->info.id != scene_info.id)
782 783
		return NULL;

784
	return source->context.data;
785 786
}

787
obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
788 789 790
{
	struct obs_scene_item *item;

J
jp9000 已提交
791 792 793
	if (!scene)
		return NULL;

794
	full_lock(scene);
795 796 797

	item = scene->first_item;
	while (item) {
798
		if (strcmp(item->source->context.name, name) == 0)
799 800 801 802 803
			break;

		item = item->next;
	}

804
	full_unlock(scene);
805 806 807 808

	return item;
}

809 810
void obs_scene_enum_items(obs_scene_t *scene,
		bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
811 812 813 814
		void *param)
{
	struct obs_scene_item *item;

J
jp9000 已提交
815 816 817
	if (!scene || !callback)
		return;

818
	full_lock(scene);
819 820 821

	item = scene->first_item;
	while (item) {
J
jp9000 已提交
822 823 824 825
		struct obs_scene_item *next = item->next;

		obs_sceneitem_addref(item);

826 827
		if (!callback(scene, item, param)) {
			obs_sceneitem_release(item);
828
			break;
829
		}
830

J
jp9000 已提交
831 832 833
		obs_sceneitem_release(item);

		item = next;
834 835
	}

836
	full_unlock(scene);
837 838
}

P
Palana 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
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);
858
	if (pressed && si && !si->user_visible) {
P
Palana 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
		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);
875
	if (pressed && si && si->user_visible) {
P
Palana 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
		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);
}

915 916 917 918 919 920
static inline bool source_has_audio(obs_source_t *source)
{
	return (source->info.output_flags &
		(OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
}

921
obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
J
jp9000 已提交
922
{
923
	struct obs_scene_item *last;
924
	struct obs_scene_item *item;
J
jp9000 已提交
925
	struct calldata params = {0};
926 927 928 929 930 931
	pthread_mutex_t mutex;

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

933 934 935 936
	if (!scene)
		return NULL;

	if (!source) {
J
jp9000 已提交
937
		blog(LOG_ERROR, "Tried to add a NULL source to a scene");
938 939 940
		return NULL;
	}

941 942 943 944 945
	if (pthread_mutex_init(&mutex, NULL) != 0) {
		blog(LOG_WARNING, "Failed to create scene item mutex");
		return NULL;
	}

946
	if (!obs_source_add_active_child(scene->source, source)) {
J
jp9000 已提交
947 948
		blog(LOG_WARNING, "Failed to add source to scene due to "
		                  "infinite source recursion");
949
		pthread_mutex_destroy(&mutex);
J
jp9000 已提交
950 951 952
		return NULL;
	}

953
	item = bzalloc(sizeof(struct obs_scene_item));
J
jp9000 已提交
954 955
	item->source  = source;
	item->parent  = scene;
J
jp9000 已提交
956
	item->ref     = 1;
957
	item->align   = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
958 959 960
	item->actions_mutex = mutex;
	item->user_visible = true;
	os_atomic_set_long(&item->active_refs, 1);
J
jp9000 已提交
961
	vec2_set(&item->scale, 1.0f, 1.0f);
962 963
	matrix4_identity(&item->draw_transform);
	matrix4_identity(&item->box_transform);
J
jp9000 已提交
964

965
	obs_source_addref(source);
966

967 968 969 970 971 972 973
	if (source_has_audio(source)) {
		item->visible = false;
		da_push_back(item->audio_actions, &action);
	} else {
		item->visible = true;
	}

974
	full_lock(scene);
975 976 977 978 979 980 981 982 983 984 985

	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 已提交
986

987
	full_unlock(scene);
988

P
Palana 已提交
989 990
	init_hotkeys(scene, item, obs_source_get_name(source));

991 992
	calldata_set_ptr(&params, "scene", scene);
	calldata_set_ptr(&params, "item", item);
993 994
	signal_handler_signal(scene->source->context.signals, "item_add",
			&params);
J
jp9000 已提交
995 996
	calldata_free(&params);

J
jp9000 已提交
997 998 999
	return item;
}

1000
static void obs_sceneitem_destroy(obs_sceneitem_t *item)
J
jp9000 已提交
1001
{
1002
	if (item) {
P
Palana 已提交
1003
		obs_hotkey_pair_unregister(item->toggle_visibility);
1004
		pthread_mutex_destroy(&item->actions_mutex);
1005
		if (item->source)
J
jp9000 已提交
1006
			obs_source_release(item->source);
1007
		da_free(item->audio_actions);
J
jp9000 已提交
1008
		bfree(item);
J
jp9000 已提交
1009 1010 1011
	}
}

1012
void obs_sceneitem_addref(obs_sceneitem_t *item)
J
jp9000 已提交
1013
{
P
Palana 已提交
1014
	if (item)
J
jp9000 已提交
1015
		os_atomic_inc_long(&item->ref);
J
jp9000 已提交
1016
}
1017

1018
void obs_sceneitem_release(obs_sceneitem_t *item)
J
jp9000 已提交
1019
{
P
Palana 已提交
1020 1021
	if (!item)
		return;
P
Palana 已提交
1022

J
jp9000 已提交
1023
	if (os_atomic_dec_long(&item->ref) == 0)
P
Palana 已提交
1024
		obs_sceneitem_destroy(item);
1025 1026
}

1027
void obs_sceneitem_remove(obs_sceneitem_t *item)
J
jp9000 已提交
1028
{
1029
	obs_scene_t *scene;
J
jp9000 已提交
1030

1031
	if (!item)
J
jp9000 已提交
1032 1033
		return;

1034
	scene = item->parent;
J
jp9000 已提交
1035

1036
	if (scene)
1037
		full_lock(scene);
J
jp9000 已提交
1038

J
jp9000 已提交
1039
	if (item->removed) {
1040
		if (scene)
1041
			full_unlock(scene);
1042 1043 1044
		return;
	}

J
jp9000 已提交
1045 1046
	item->removed = true;

1047 1048
	assert(scene != NULL);
	assert(scene->source != NULL);
1049 1050

	set_visibility(item, false);
1051

1052
	signal_item_remove(item);
J
jp9000 已提交
1053
	detach_sceneitem(item);
1054

1055
	full_unlock(scene);
J
jp9000 已提交
1056 1057 1058 1059

	obs_sceneitem_release(item);
}

1060
obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
J
jp9000 已提交
1061
{
J
jp9000 已提交
1062
	return item ? item->parent : NULL;
J
jp9000 已提交
1063 1064
}

1065
obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
1066
{
J
jp9000 已提交
1067
	return item ? item->source : NULL;
J
jp9000 已提交
1068 1069
}

1070
void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
1071 1072 1073 1074 1075 1076 1077 1078 1079
{
	struct calldata params = {0};
	const char *command = select ? "item_select" : "item_deselect";

	if (!item || item->selected == select)
		return;

	item->selected = select;

1080 1081
	calldata_set_ptr(&params, "scene", item->parent);
	calldata_set_ptr(&params, "item",  item);
1082 1083 1084 1085 1086 1087
	signal_handler_signal(item->parent->source->context.signals,
			command, &params);

	calldata_free(&params);
}

1088
bool obs_sceneitem_selected(const obs_sceneitem_t *item)
1089 1090 1091 1092
{
	return item ? item->selected : false;
}

1093
void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
J
jp9000 已提交
1094
{
1095
	if (item) {
J
jp9000 已提交
1096
		vec2_copy(&item->pos, pos);
1097 1098
		update_item_transform(item);
	}
J
jp9000 已提交
1099 1100
}

1101
void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
J
jp9000 已提交
1102
{
1103
	if (item) {
J
jp9000 已提交
1104
		item->rot = rot;
1105 1106
		update_item_transform(item);
	}
J
jp9000 已提交
1107 1108
}

1109
void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
J
jp9000 已提交
1110
{
1111 1112 1113 1114
	if (item) {
		vec2_copy(&item->scale, scale);
		update_item_transform(item);
	}
J
jp9000 已提交
1115 1116
}

1117
void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
J
jp9000 已提交
1118
{
1119 1120 1121 1122
	if (item) {
		item->align = alignment;
		update_item_transform(item);
	}
J
jp9000 已提交
1123 1124
}

S
Socapex 已提交
1125
static inline void signal_reorder(struct obs_scene_item *item)
J
jp9000 已提交
1126
{
1127
	const char *command = NULL;
J
jp9000 已提交
1128 1129
	struct calldata params = {0};

S
Socapex 已提交
1130
	command = "reorder";
J
jp9000 已提交
1131

1132
	calldata_set_ptr(&params, "scene", item->parent);
J
jp9000 已提交
1133 1134 1135 1136 1137 1138 1139

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

	calldata_free(&params);
}

1140
void obs_sceneitem_set_order(obs_sceneitem_t *item,
J
jp9000 已提交
1141
		enum obs_order_movement movement)
J
jp9000 已提交
1142
{
J
jp9000 已提交
1143 1144
	if (!item) return;

J
jp9000 已提交
1145
	struct obs_scene_item *next, *prev;
J
jp9000 已提交
1146 1147
	struct obs_scene *scene = item->parent;

J
jp9000 已提交
1148
	obs_scene_addref(scene);
1149
	full_lock(scene);
1150

J
jp9000 已提交
1151 1152 1153
	next = item->next;
	prev = item->prev;

1154 1155
	detach_sceneitem(item);

J
jp9000 已提交
1156
	if (movement == OBS_ORDER_MOVE_DOWN) {
J
jp9000 已提交
1157
		attach_sceneitem(scene, item, prev ? prev->prev : NULL);
J
jp9000 已提交
1158

J
jp9000 已提交
1159
	} else if (movement == OBS_ORDER_MOVE_UP) {
J
jp9000 已提交
1160
		attach_sceneitem(scene, item, next ? next : prev);
J
jp9000 已提交
1161

J
jp9000 已提交
1162
	} else if (movement == OBS_ORDER_MOVE_TOP) {
J
jp9000 已提交
1163
		struct obs_scene_item *last = next;
1164
		if (!last) {
J
jp9000 已提交
1165
			last = prev;
1166 1167 1168 1169
		} else {
			while (last->next)
				last = last->next;
		}
J
jp9000 已提交
1170

J
jp9000 已提交
1171
		attach_sceneitem(scene, item, last);
1172

J
jp9000 已提交
1173
	} else if (movement == OBS_ORDER_MOVE_BOTTOM) {
J
jp9000 已提交
1174
		attach_sceneitem(scene, item, NULL);
J
jp9000 已提交
1175
	}
1176

S
Socapex 已提交
1177
	signal_reorder(item);
J
jp9000 已提交
1178

1179
	full_unlock(scene);
1180
	obs_scene_release(scene);
J
jp9000 已提交
1181 1182
}

1183 1184 1185 1186 1187 1188 1189 1190 1191
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);
1192
	full_lock(scene);
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

	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);

1211
	full_unlock(scene);
1212 1213 1214
	obs_scene_release(scene);
}

1215
void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
1216 1217 1218 1219 1220 1221 1222 1223
		enum obs_bounds_type type)
{
	if (item) {
		item->bounds_type = type;
		update_item_transform(item);
	}
}

1224
void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
1225 1226 1227 1228 1229 1230 1231 1232
		uint32_t alignment)
{
	if (item) {
		item->bounds_align = alignment;
		update_item_transform(item);
	}
}

1233
void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
1234 1235 1236 1237 1238 1239 1240
{
	if (item) {
		item->bounds = *bounds;
		update_item_transform(item);
	}
}

1241
void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
J
jp9000 已提交
1242
{
J
jp9000 已提交
1243 1244
	if (item)
		vec2_copy(pos, &item->pos);
J
jp9000 已提交
1245 1246
}

1247
float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
J
jp9000 已提交
1248
{
J
jp9000 已提交
1249
	return item ? item->rot : 0.0f;
J
jp9000 已提交
1250 1251
}

1252
void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
J
jp9000 已提交
1253
{
J
jp9000 已提交
1254
	if (item)
1255
		vec2_copy(scale, &item->scale);
J
jp9000 已提交
1256 1257
}

1258
uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
1259 1260 1261 1262
{
	return item ? item->align : 0;
}

1263
enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
1264 1265 1266 1267
{
	return item ? item->bounds_type : OBS_BOUNDS_NONE;
}

1268
uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
1269 1270 1271 1272
{
	return item ? item->bounds_align : 0;
}

1273
void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
J
jp9000 已提交
1274
{
J
jp9000 已提交
1275
	if (item)
1276 1277 1278
		*bounds = item->bounds;
}

1279
void obs_sceneitem_get_info(const obs_sceneitem_t *item,
1280
		struct obs_transform_info *info)
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
{
	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;
	}
}

1293
void obs_sceneitem_set_info(obs_sceneitem_t *item,
1294
		const struct obs_transform_info *info)
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
{
	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);
	}
}

1308
void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
1309 1310 1311 1312 1313 1314
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->draw_transform);
}

1315
void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
1316 1317 1318 1319
		struct matrix4 *transform)
{
	if (item)
		matrix4_copy(transform, &item->box_transform);
J
jp9000 已提交
1320
}
1321 1322 1323

bool obs_sceneitem_visible(const obs_sceneitem_t *item)
{
1324
	return item ? item->user_visible : false;
1325 1326
}

1327
bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
1328 1329
{
	struct calldata cd = {0};
1330 1331 1332 1333
	struct item_action action = {
		.visible = visible,
		.timestamp = os_gettime_ns()
	};
1334 1335

	if (!item)
1336
		return false;
1337

1338
	if (item->user_visible == visible)
1339
		return false;
1340 1341

	if (!item->parent)
1342 1343 1344
		return false;

	if (visible) {
1345 1346 1347 1348 1349 1350 1351
		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;
			}
		}
1352 1353
	}

1354
	item->user_visible = visible;
1355 1356 1357 1358 1359 1360 1361 1362

	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);
	calldata_free(&cd);
1363 1364 1365 1366 1367 1368 1369 1370

	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);
	}
1371
	return true;
1372
}
P
Palana 已提交
1373

P
Palana 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
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);
1410
	full_lock(scene);
P
Palana 已提交
1411 1412 1413 1414

	bool order_matches = true;
	if (!sceneitems_match(scene, item_order, item_order_size,
				&order_matches) || order_matches) {
1415
		full_unlock(scene);
P
Palana 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
		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);

1435
	full_unlock(scene);
P
Palana 已提交
1436 1437 1438 1439
	obs_scene_release(scene);
	return true;
}

P
Palana 已提交
1440 1441 1442 1443 1444 1445 1446
void obs_scene_atomic_update(obs_scene_t *scene,
		obs_scene_atomic_update_func func, void *data)
{
	if (!scene)
		return;

	obs_scene_addref(scene);
1447
	full_lock(scene);
P
Palana 已提交
1448
	func(data, scene);
1449
	full_unlock(scene);
P
Palana 已提交
1450 1451
	obs_scene_release(scene);
}