File: test-random.c

package info (click to toggle)
obs-studio 30.2.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,852 kB
  • sloc: ansic: 202,137; cpp: 112,402; makefile: 868; python: 599; sh: 275; javascript: 19
file content (107 lines) | stat: -rw-r--r-- 2,126 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdlib.h>
#include <util/threading.h>
#include <util/platform.h>
#include <obs.h>

struct random_tex {
	obs_source_t *source;
	os_event_t *stop_signal;
	pthread_t thread;
	bool initialized;
};

static const char *random_getname(void *unused)
{
	UNUSED_PARAMETER(unused);
	return "20x20 Random Pixel Texture Source (Test)";
}

static void random_destroy(void *data)
{
	struct random_tex *rt = data;

	if (rt) {
		if (rt->initialized) {
			os_event_signal(rt->stop_signal);
			pthread_join(rt->thread, NULL);
		}

		os_event_destroy(rt->stop_signal);
		bfree(rt);
	}
}

static inline void fill_texture(uint32_t *pixels)
{
	size_t x, y;

	for (y = 0; y < 20; y++) {
		for (x = 0; x < 20; x++) {
			uint32_t pixel = 0;
			pixel |= (rand() % 256);
			pixel |= (rand() % 256) << 8;
			pixel |= (rand() % 256) << 16;
			//pixel |= (rand()%256) << 24;
			//pixel |= 0xFFFFFFFF;
			pixels[y * 20 + x] = pixel;
		}
	}
}

static void *video_thread(void *data)
{
	struct random_tex *rt = data;
	uint32_t pixels[20 * 20];
	uint64_t cur_time = os_gettime_ns();

	struct obs_source_frame frame = {
		.data = {[0] = (uint8_t *)pixels},
		.linesize = {[0] = 20 * 4},
		.width = 20,
		.height = 20,
		.format = VIDEO_FORMAT_BGRX,
	};

	while (os_event_try(rt->stop_signal) == EAGAIN) {
		fill_texture(pixels);

		frame.timestamp = cur_time;

		obs_source_output_video(rt->source, &frame);

		os_sleepto_ns(cur_time += 250000000);
	}

	return NULL;
}

static void *random_create(obs_data_t *settings, obs_source_t *source)
{
	struct random_tex *rt = bzalloc(sizeof(struct random_tex));
	rt->source = source;

	if (os_event_init(&rt->stop_signal, OS_EVENT_TYPE_MANUAL) != 0) {
		random_destroy(rt);
		return NULL;
	}

	if (pthread_create(&rt->thread, NULL, video_thread, rt) != 0) {
		random_destroy(rt);
		return NULL;
	}

	rt->initialized = true;

	UNUSED_PARAMETER(settings);
	UNUSED_PARAMETER(source);
	return rt;
}

struct obs_source_info test_random = {
	.id = "random",
	.type = OBS_SOURCE_TYPE_INPUT,
	.output_flags = OBS_SOURCE_ASYNC_VIDEO,
	.get_name = random_getname,
	.create = random_create,
	.destroy = random_destroy,
};