Skip to content
Merged
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 @@ -16,6 +16,7 @@
#include "libobs/obs-encoder-get-name-fields.c.h"
#include "libobs/obs-encoder-gpu-scaling-getters-fields.c.h"
#include "libobs/obs-encoder-group-lifecycle-fields.c.h"
#include "libobs/obs-encoder-group-num-started.c.h"
#include "libobs/obs-encoder-init-fields.c.h"
#include "libobs/obs-encoder-last-error-fields.c.h"
#include "libobs/obs-encoder-media-getters-fields.c.h"
Expand Down
12 changes: 12 additions & 0 deletions libobs/obs-encoder-group-num-started.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "libobs/build/libobs.pch.h"

uint32_t obs_encoder_group_num_started(struct obs_encoder_group *group)
{
uint32_t num_started;

pthread_mutex_lock(&group->mutex);
num_started = group->num_encoders_started;
pthread_mutex_unlock(&group->mutex);

return num_started;
}
11 changes: 0 additions & 11 deletions libobs/obs-encoder.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,6 @@ struct obs_encoder_interleave_info obs_encoder_interleave_info(const obs_encoder
return info;
}

uint32_t obs_encoder_group_num_started(struct obs_encoder_group *group)
{
uint32_t num_started;

pthread_mutex_lock(&group->mutex);
num_started = group->num_encoders_started;
pthread_mutex_unlock(&group->mutex);

return num_started;
}

void obs_encoder_update(obs_encoder_t *encoder, obs_data_t *settings)
{
if (!obs_encoder_valid(encoder, "obs_encoder_update"))
Expand Down
26 changes: 26 additions & 0 deletions test/cmocka/build/test-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -3754,6 +3754,26 @@
"OBS::native-test-cmocka"
]
},
"test_encoder_group_num_started": {
"sources": [
"test/cmocka/test_encoder_group_num_started.c",
"libobs/obs-encoder-group-num-started.c.h"
],
"definitions": [
"pthread_mutex_lock=encoder_group_count_record_lock",
"pthread_mutex_unlock=encoder_group_count_record_unlock"
],
"libraries": [
"OBS::libobs",
"OBS::caption",
"Uthash::Uthash",
"OBS::native-test-cmocka",
{
"name": "OBS::w32-pthreads",
"requires": "OS_WINDOWS"
}
]
},
"test_encoder_group_reconfig_steps": {
"sources": [
"test/cmocka/test_encoder_group_reconfig_steps.c",
Expand Down Expand Up @@ -23713,6 +23733,12 @@
],
"optional_prefixes_requires": "OS_LINUX"
},
"test_encoder_group_num_started": {
"target": "test_encoder_group_num_started",
"command": [
"${target}"
]
},
"test_encoder_group_reconfig_steps": {
"target": "test_encoder_group_reconfig_steps",
"command": [
Expand Down
104 changes: 104 additions & 0 deletions test/cmocka/test_encoder_group_num_started.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* C-source characterization of the hidden encoder-group count accessor.
* Record mutex calls using the real private layout, without threads or waits.
* NULL groups are outside the C precondition. A Rust swap must relink this
* unchanged fixture to its replacement and real group-field adapter. */
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdint.h>
#include <string.h>
#include "obs-internal.h"

static struct obs_encoder_group groups[2];
static struct obs_encoder_group *selected;
static unsigned lock_calls, unlock_calls;
static bool locked, change_on_lock, change_on_unlock;
static uint32_t locked_value, unlocked_value;

int pthread_mutex_lock(pthread_mutex_t *mutex)
{
assert_ptr_equal(mutex, &selected->mutex);
assert_false(locked);
assert_int_equal(lock_calls, 0);
assert_int_equal(unlock_calls, 0);
lock_calls++;
locked = true;
if (change_on_lock)
selected->num_encoders_started = locked_value;
return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
assert_ptr_equal(mutex, &selected->mutex);
assert_true(locked);
assert_int_equal(lock_calls, 1);
assert_int_equal(unlock_calls, 0);
unlock_calls++;
if (change_on_unlock)
selected->num_encoders_started = unlocked_value;
locked = false;
return 0;
}

static uint32_t query(struct obs_encoder_group *group)
{
selected = group;
lock_calls = unlock_calls = 0;
uint32_t result = obs_encoder_group_num_started(group);
assert_int_equal(lock_calls, 1);
assert_int_equal(unlock_calls, 1);
assert_false(locked);
return result;
}

static int setup(void **state)
{
(void)state;
memset(groups, 0, sizeof(groups));
selected = NULL;
locked = change_on_lock = change_on_unlock = false;
lock_calls = unlock_calls = 0;
locked_value = unlocked_value = 0;
return 0;
}

static void exact_count_and_group(void **state)
{
(void)state;
const uint32_t values[] = {0, 1, 2, 32, UINT32_MAX - 1, UINT32_MAX};
for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
groups[0].num_encoders_started = values[i];
groups[1].num_encoders_started = ~values[i];
/* Unrelated lifecycle state must not clamp or gate the count. */
groups[0].destroy_on_stop = true;
groups[1].frame_rate_divisors_lcm = UINT32_MAX;
unsigned char before[sizeof(groups)];
memcpy(before, groups, sizeof(groups));
assert_int_equal(query(&groups[0]), values[i]);
assert_int_equal(query(&groups[1]), (uint32_t)~values[i]);
assert_memory_equal(groups, before, sizeof(groups));
}
}

static void snapshot_inside_mutex_calls(void **state)
{
(void)state;
const uint32_t values[] = {0, 1, UINT32_MAX};
change_on_lock = change_on_unlock = true;
for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
locked_value = values[i];
unlocked_value = ~locked_value;
groups[0].num_encoders_started = unlocked_value;
assert_int_equal(query(&groups[0]), locked_value);
assert_int_equal(groups[0].num_encoders_started, unlocked_value);
}
}

int main(void)
{
const struct CMUnitTest tests[] = {cmocka_unit_test_setup(exact_count_and_group, setup),
cmocka_unit_test_setup(snapshot_inside_mutex_calls, setup)};
return cmocka_run_group_tests(tests, NULL, NULL);
}
1 change: 1 addition & 0 deletions test/cmocka/tests.d/test_encoder_group_num_started.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).
Loading