obs-scene.c 33.2 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
	struct obs_source *source =
715 716
		obs_source_create(OBS_SOURCE_TYPE_INPUT, "scene", name, NULL,
				NULL);
717
	return source->context.data;
J
jp9000 已提交
718 719
}

720 721 722 723 724
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;

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

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

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

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

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

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
			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;
	}

758
	full_unlock(scene);
759 760 761 762

	return new_scene;
}

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

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

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

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

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

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

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

795
	full_lock(scene);
796 797 798

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

		item = item->next;
	}

805
	full_unlock(scene);
806 807 808 809

	return item;
}

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

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

819
	full_lock(scene);
820 821 822

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

		obs_sceneitem_addref(item);

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

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

		item = next;
835 836
	}

837
	full_unlock(scene);
838 839
}

P
Palana 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
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);
859
	if (pressed && si && !si->user_visible) {
P
Palana 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
		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);
876
	if (pressed && si && si->user_visible) {
P
Palana 已提交
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 915
		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);
}

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

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

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

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

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

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

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

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

966
	obs_source_addref(source);
967

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

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

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

988
	full_unlock(scene);
989

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	set_visibility(item, false);
1052

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

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

	obs_sceneitem_release(item);
}

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

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

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

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

	item->selected = select;

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

	calldata_free(&params);
}

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

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

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

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

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

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

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

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

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

	calldata_free(&params);
}

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

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

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

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

1155 1156
	detach_sceneitem(item);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	if (!item)
1337
		return false;
1338

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

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

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

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

	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);
1364 1365 1366 1367 1368 1369 1370 1371

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

P
Palana 已提交
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 1410
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);
1411
	full_lock(scene);
P
Palana 已提交
1412 1413 1414 1415

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

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

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

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