Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libobs/build/libobs.core-output+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "libobs/obs-encoder-clear-roi-fields.c.h"
#include "libobs/obs-encoder-configuration-fields.c.h"
#include "libobs/obs-encoder-counter-fields.c.h"
#include "libobs/obs-encoder-create.c.h"
#include "libobs/obs-encoder-defaults-fields.c.h"
#include "libobs/obs-encoder-dimensions-fields.c.h"
#include "libobs/obs-encoder-error-ownership-fields.c.h"
Expand Down
20 changes: 20 additions & 0 deletions libobs/obs-encoder-create.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "libobs/build/libobs.pch.h"

/* Private constructor engine; allocation and context ownership remain in C. */
extern struct obs_encoder *create_encoder(const char *id, enum obs_encoder_type type, const char *name,
obs_data_t *settings, size_t mixer_idx, obs_data_t *hotkey_data);

obs_encoder_t *obs_video_encoder_create(const char *id, const char *name, obs_data_t *settings, obs_data_t *hotkey_data)
{
if (!name || !id)
return NULL;
return create_encoder(id, OBS_ENCODER_VIDEO, name, settings, 0, hotkey_data);
}

obs_encoder_t *obs_audio_encoder_create(const char *id, const char *name, obs_data_t *settings, size_t mixer_idx,
obs_data_t *hotkey_data)
{
if (!name || !id)
return NULL;
return create_encoder(id, OBS_ENCODER_AUDIO, name, settings, mixer_idx, hotkey_data);
}
19 changes: 2 additions & 17 deletions libobs/obs-encoder.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct obs_encoder_info *find_encoder(const char *id)
return NULL;
}

static struct obs_encoder *create_encoder(const char *id, enum obs_encoder_type type, const char *name,
obs_data_t *settings, size_t mixer_idx, obs_data_t *hotkey_data)
struct obs_encoder *create_encoder(const char *id, enum obs_encoder_type type, const char *name, obs_data_t *settings,
size_t mixer_idx, obs_data_t *hotkey_data)
{
struct obs_encoder *encoder;
struct obs_encoder_info *ei = find_encoder(id);
Expand Down Expand Up @@ -91,21 +91,6 @@ static struct obs_encoder *create_encoder(const char *id, enum obs_encoder_type
return encoder;
}

obs_encoder_t *obs_video_encoder_create(const char *id, const char *name, obs_data_t *settings, obs_data_t *hotkey_data)
{
if (!name || !id)
return NULL;
return create_encoder(id, OBS_ENCODER_VIDEO, name, settings, 0, hotkey_data);
}

obs_encoder_t *obs_audio_encoder_create(const char *id, const char *name, obs_data_t *settings, size_t mixer_idx,
obs_data_t *hotkey_data)
{
if (!name || !id)
return NULL;
return create_encoder(id, OBS_ENCODER_AUDIO, name, settings, mixer_idx, hotkey_data);
}

static void receive_video(void *param, struct video_data *frame);
static void receive_audio(void *param, size_t mix_idx, struct audio_data *data);

Expand Down
30 changes: 30 additions & 0 deletions test/cmocka/build/test-graph.d/test_encoder_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"schema": 1,
"targets": {
"test_encoder_create": {
"sources": [
"test/cmocka/test_encoder_create.c"
],
"libraries": [
"OBS::libobs",
"OBS::caption",
"Uthash::Uthash",
"SIMDe::SIMDe",
"OBS::native-test-cmocka",
{
"name": "OBS::w32-pthreads",
"requires": "OS_WINDOWS"
}
]
}
},
"tests": {
"test_encoder_create": {
"target": "test_encoder_create",
"command": [
"${target}"
],
"timeout_seconds": 30
}
}
}
159 changes: 159 additions & 0 deletions test/cmocka/test_encoder_create.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/* Public constructor wrappers, characterized against each real libobs backend. */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdint.h>
#include <string.h>
#include <cmocka.h>
#include "obs-internal.h"

static const char *type_name(void *data)
{
(void)data;
return "Constructor characterization";
}

static void *codec_create(obs_data_t *settings, obs_encoder_t *encoder)
{
(void)settings;
(void)encoder;
/* Construction must remain lazy: no codec instance is created. */
fail();
return NULL;
}

static void codec_destroy(void *data)
{
(void)data;
fail();
}

static bool codec_encode(void *data, struct encoder_frame *frame, struct encoder_packet *packet, bool *received)
{
(void)data;
(void)frame;
(void)packet;
(void)received;
fail();
return false;
}

static size_t frame_size(void *data)
{
(void)data;
return 1024;
}

static int setup(void **state)
{
(void)state;
assert_true(obs_startup("en-US", NULL, NULL));
struct obs_encoder_info info = {.id = "constructor-video",
.type = OBS_ENCODER_VIDEO,
.codec = "h264",
.get_name = type_name,
.create = codec_create,
.destroy = codec_destroy,
.encode = codec_encode,
.get_frame_size = frame_size};
obs_register_encoder(&info);
info.id = "constructor-audio";
info.type = OBS_ENCODER_AUDIO;
info.codec = "aac";
obs_register_encoder(&info);
return 0;
}

static int teardown(void **state)
{
(void)state;
obs_shutdown();
return 0;
}

static void null_admission(void **state)
{
(void)state;
assert_null(obs_video_encoder_create(NULL, "video", NULL, NULL));
assert_null(obs_video_encoder_create("constructor-video", NULL, NULL, NULL));
assert_null(obs_video_encoder_create(NULL, NULL, NULL, NULL));
assert_null(obs_audio_encoder_create(NULL, "audio", NULL, 0, NULL));
assert_null(obs_audio_encoder_create("constructor-audio", NULL, NULL, 0, NULL));
assert_null(obs_audio_encoder_create(NULL, NULL, NULL, SIZE_MAX, NULL));
}

static void wrong_type(void **state)
{
(void)state;
assert_null(obs_video_encoder_create("constructor-audio", "wrong-video", NULL, NULL));
assert_null(obs_audio_encoder_create("constructor-video", "wrong-audio", NULL, 3, NULL));
assert_null(obs_get_encoder_by_name("wrong-video"));
assert_null(obs_get_encoder_by_name("wrong-audio"));
}

static void check_created(const char *id, bool audio, size_t mixer, bool supplied, bool unknown)
{
obs_data_t *settings = supplied ? obs_data_create() : NULL;
obs_data_t *hotkeys = supplied ? obs_data_create() : NULL;
if (supplied) {
obs_data_set_int(settings, "marker", 73);
obs_data_set_string(hotkeys, "marker", "hotkeys");
}
char name[] = "constructor-instance";
obs_encoder_t *encoder = audio ? obs_audio_encoder_create(id, name, settings, mixer, hotkeys)
: obs_video_encoder_create(id, name, settings, hotkeys);
assert_non_null(encoder);
assert_int_equal(encoder->info.type, audio ? OBS_ENCODER_AUDIO : OBS_ENCODER_VIDEO);
assert_string_equal(encoder->info.id, id);
assert_string_equal(encoder->orig_info.id, id);
assert_int_equal(encoder->owns_info_id, unknown);
assert_true(encoder->mixer_idx == (audio ? mixer : 0));
assert_int_equal(encoder->frame_rate_divisor, audio ? 0 : 1);
assert_null(encoder->context.data);
assert_false(encoder->active);
assert_non_null(encoder->context.control);
if (supplied) {
assert_ptr_equal(encoder->context.settings, settings);
assert_ptr_equal(encoder->context.hotkey_data, hotkeys);
}
obs_data_release(settings);
obs_data_release(hotkeys);
assert_int_equal(obs_data_get_int(encoder->context.settings, "marker"), supplied ? 73 : 0);
assert_string_equal(obs_data_get_string(encoder->context.hotkey_data, "marker"), supplied ? "hotkeys" : "");
name[0] = 'X';
assert_string_equal(obs_encoder_get_name(encoder), "constructor-instance");
obs_encoder_t *found = obs_get_encoder_by_name("constructor-instance");
assert_ptr_equal(found, encoder);
obs_encoder_release(found);
obs_encoder_release(encoder);
assert_null(obs_get_encoder_by_name("constructor-instance"));
}

static void registered_creation(void **state)
{
(void)state;
const size_t mixers[] = {0, 3, MAX_AUDIO_MIXES - 1, MAX_AUDIO_MIXES, SIZE_MAX};
for (unsigned supplied = 0; supplied < 2; supplied++) {
check_created("constructor-video", false, 0, supplied, false);
for (size_t i = 0; i < sizeof(mixers) / sizeof(mixers[0]); i++)
/* Out-of-range mixer storage is characterized, not endorsed. */
check_created("constructor-audio", true, mixers[i], supplied, false);
}
}

static void unknown_creation(void **state)
{
(void)state;
for (unsigned supplied = 0; supplied < 2; supplied++) {
/* Unknown IDs produce usable placeholder handles, not NULL. */
check_created("constructor-missing-video", false, 0, supplied, true);
check_created("constructor-missing-audio", true, SIZE_MAX, supplied, true);
}
}

int main(void)
{
const struct CMUnitTest tests[] = {cmocka_unit_test(null_admission), cmocka_unit_test(wrong_type),
cmocka_unit_test(registered_creation), cmocka_unit_test(unknown_creation)};
return cmocka_run_group_tests(tests, setup, teardown);
}
Loading