yolobs-studio/test/test-input/test-random.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

108 lines
2.1 KiB
C
Raw Normal View History

2016-02-23 23:16:51 +00:00
#include <stdlib.h>
#include <util/threading.h>
#include <util/platform.h>
#include <obs.h>
struct random_tex {
obs_source_t *source;
2019-09-22 21:19:10 +00:00
os_event_t *stop_signal;
pthread_t thread;
bool initialized;
2016-02-23 23:16:51 +00:00
};
2016-08-28 12:07:43 +00:00
static const char *random_getname(void *unused)
2016-02-23 23:16:51 +00:00
{
2016-08-28 12:07:43 +00:00
UNUSED_PARAMETER(unused);
2016-02-23 23:16:51 +00:00
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;
2019-09-22 21:19:10 +00:00
pixel |= (rand() % 256);
pixel |= (rand() % 256) << 8;
pixel |= (rand() % 256) << 16;
2016-02-23 23:16:51 +00:00
//pixel |= (rand()%256) << 24;
//pixel |= 0xFFFFFFFF;
2019-09-22 21:19:10 +00:00
pixels[y * 20 + x] = pixel;
2016-02-23 23:16:51 +00:00
}
}
}
static void *video_thread(void *data)
{
2019-09-22 21:19:10 +00:00
struct random_tex *rt = data;
uint32_t pixels[20 * 20];
uint64_t cur_time = os_gettime_ns();
2016-02-23 23:16:51 +00:00
struct obs_source_frame frame = {
2019-09-22 21:19:10 +00:00
.data = {[0] = (uint8_t *)pixels},
.linesize = {[0] = 20 * 4},
.width = 20,
.height = 20,
.format = VIDEO_FORMAT_BGRX,
2016-02-23 23:16:51 +00:00
};
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 = {
2019-09-22 21:19:10 +00:00
.id = "random",
.type = OBS_SOURCE_TYPE_INPUT,
2016-02-23 23:16:51 +00:00
.output_flags = OBS_SOURCE_ASYNC_VIDEO,
2019-09-22 21:19:10 +00:00
.get_name = random_getname,
.create = random_create,
.destroy = random_destroy,
2016-02-23 23:16:51 +00:00
};