diff --git a/libobs/build/libobs.core-output+.h.c b/libobs/build/libobs.core-output+.h.c index 48aabb5dc5..aa1608dca4 100644 --- a/libobs/build/libobs.core-output+.h.c +++ b/libobs/build/libobs.core-output+.h.c @@ -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" diff --git a/libobs/obs-encoder-group-num-started.c.h b/libobs/obs-encoder-group-num-started.c.h new file mode 100644 index 0000000000..e09d3e7f69 --- /dev/null +++ b/libobs/obs-encoder-group-num-started.c.h @@ -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; +} diff --git a/libobs/obs-encoder.c.h b/libobs/obs-encoder.c.h index a66e984d60..f80807034c 100644 --- a/libobs/obs-encoder.c.h +++ b/libobs/obs-encoder.c.h @@ -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")) diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index e2cffe4267..3129ce52a4 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -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", @@ -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": [ diff --git a/test/cmocka/test_encoder_group_num_started.c b/test/cmocka/test_encoder_group_num_started.c new file mode 100644 index 0000000000..6de909b87f --- /dev/null +++ b/test/cmocka/test_encoder_group_num_started.c @@ -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 +#include +#include +#include +#include +#include +#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); +} diff --git a/test/cmocka/tests.d/test_encoder_group_num_started.cmake b/test/cmocka/tests.d/test_encoder_group_num_started.cmake new file mode 100644 index 0000000000..d85a840326 --- /dev/null +++ b/test/cmocka/tests.d/test_encoder_group_num_started.cmake @@ -0,0 +1 @@ +# Registered in the canonical native test graph (test/cmocka/build/test-graph.json).