From fc1baa97cb59716738d2e149f5e68900116ab3ee Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Wed, 25 Nov 2026 11:14:25 -0800 Subject: [PATCH 1/4] libobs: extract obs_source_enum_active_sources and obs_source_enum_active_tree verbatim (Refs #5729) Move the two bodies and the static enum_source_active_tree_callback() unchanged out of obs-source.c.h into obs-source-enum-active.c.h, still compiled in the live core-source group. obs-source.c.h's data_valid() and struct source_enum_data are file-local, so the extracted file carries copies under scoped names mapped onto the originals with macros it undefines at its end; the bodies stay verbatim. No Rust, no behavior change. Co-Authored-By: Claude Opus 5.5 --- libobs/build/libobs.core-source+.h.c | 1 + libobs/obs-source-enum-active.c.h | 85 ++++++++++++++++++++++++++++ libobs/obs-source.c.h | 62 -------------------- 3 files changed, 86 insertions(+), 62 deletions(-) create mode 100644 libobs/obs-source-enum-active.c.h diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index e48e40891f..23a37d0a42 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -73,6 +73,7 @@ #include "libobs/obs-source-deinterlace-seam.c.h" #include "libobs/obs-source-deinterlace-texture-fields.c.h" #include "libobs/obs-source-enabled-setter-fields.c.h" +#include "libobs/obs-source-enum-active.c.h" #include "libobs/obs-source-enum-filters-fields.c.h" #include "libobs/obs-source-filter-backup-fields.c.h" #include "libobs/obs-source-filter-count-fields.c.h" diff --git a/libobs/obs-source-enum-active.c.h b/libobs/obs-source-enum-active.c.h new file mode 100644 index 0000000000..b0d793c47c --- /dev/null +++ b/libobs/obs-source-enum-active.c.h @@ -0,0 +1,85 @@ +#include "libobs/build/libobs.pch.h" + +/* obs_source_enum_active_sources() and obs_source_enum_active_tree(), moved + * verbatim out of obs-source.c.h (#5729). obs-source.c.h's data_valid() and + * struct source_enum_data are file-local there, so this file carries its own + * copies under scoped names; the bodies keep the original names and the file + * also builds in a translation unit of its own. */ +struct source_enum_active_data { + obs_source_enum_proc_t enum_callback; + void *param; + obs_source_t *stop_source; +}; + +static inline bool source_enum_active_data_valid(const struct obs_source *source, const char *f) +{ + return obs_source_valid(source, f) && source->context.data; +} + +#define source_enum_data source_enum_active_data +#define data_valid source_enum_active_data_valid + +static void enum_source_active_tree_callback(obs_source_t *parent, obs_source_t *child, void *param) +{ + struct source_enum_data *data = param; + bool is_transition = child->info.type == OBS_SOURCE_TYPE_TRANSITION; + + if (is_transition) + obs_transition_enum_sources(child, enum_source_active_tree_callback, param); + if (child->info.enum_active_sources) { + if (child->context.data) { + child->info.enum_active_sources(child->context.data, enum_source_active_tree_callback, data); + } + } + + data->enum_callback(parent, child, data->param); +} + +void obs_source_enum_active_sources(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param) +{ + bool is_transition; + if (!data_valid(source, "obs_source_enum_active_sources")) + return; + + is_transition = source->info.type == OBS_SOURCE_TYPE_TRANSITION; + if (!is_transition && !source->info.enum_active_sources) + return; + + source = obs_source_get_ref(source); + if (!data_valid(source, "obs_source_enum_active_sources")) + return; + + if (is_transition) + obs_transition_enum_sources(source, enum_callback, param); + if (source->info.enum_active_sources) + source->info.enum_active_sources(source->context.data, enum_callback, param); + + obs_source_release(source); +} + +void obs_source_enum_active_tree(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param) +{ + struct source_enum_data data = {enum_callback, param}; + bool is_transition; + + if (!data_valid(source, "obs_source_enum_active_tree")) + return; + + is_transition = source->info.type == OBS_SOURCE_TYPE_TRANSITION; + if (!is_transition && !source->info.enum_active_sources) + return; + + source = obs_source_get_ref(source); + if (!data_valid(source, "obs_source_enum_active_tree")) + return; + + if (source->info.type == OBS_SOURCE_TYPE_TRANSITION) + obs_transition_enum_sources(source, enum_source_active_tree_callback, &data); + if (source->info.enum_active_sources) + source->info.enum_active_sources(source->context.data, enum_source_active_tree_callback, &data); + + obs_source_release(source); +} + +#undef data_valid +#undef source_enum_data diff --git a/libobs/obs-source.c.h b/libobs/obs-source.c.h index a8cb9fab92..d04aaff909 100644 --- a/libobs/obs-source.c.h +++ b/libobs/obs-source.c.h @@ -2819,68 +2819,6 @@ struct source_enum_data { obs_source_t *stop_source; }; -static void enum_source_active_tree_callback(obs_source_t *parent, obs_source_t *child, void *param) -{ - struct source_enum_data *data = param; - bool is_transition = child->info.type == OBS_SOURCE_TYPE_TRANSITION; - - if (is_transition) - obs_transition_enum_sources(child, enum_source_active_tree_callback, param); - if (child->info.enum_active_sources) { - if (child->context.data) { - child->info.enum_active_sources(child->context.data, enum_source_active_tree_callback, data); - } - } - - data->enum_callback(parent, child, data->param); -} - -void obs_source_enum_active_sources(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param) -{ - bool is_transition; - if (!data_valid(source, "obs_source_enum_active_sources")) - return; - - is_transition = source->info.type == OBS_SOURCE_TYPE_TRANSITION; - if (!is_transition && !source->info.enum_active_sources) - return; - - source = obs_source_get_ref(source); - if (!data_valid(source, "obs_source_enum_active_sources")) - return; - - if (is_transition) - obs_transition_enum_sources(source, enum_callback, param); - if (source->info.enum_active_sources) - source->info.enum_active_sources(source->context.data, enum_callback, param); - - obs_source_release(source); -} - -void obs_source_enum_active_tree(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param) -{ - struct source_enum_data data = {enum_callback, param}; - bool is_transition; - - if (!data_valid(source, "obs_source_enum_active_tree")) - return; - - is_transition = source->info.type == OBS_SOURCE_TYPE_TRANSITION; - if (!is_transition && !source->info.enum_active_sources) - return; - - source = obs_source_get_ref(source); - if (!data_valid(source, "obs_source_enum_active_tree")) - return; - - if (source->info.type == OBS_SOURCE_TYPE_TRANSITION) - obs_transition_enum_sources(source, enum_source_active_tree_callback, &data); - if (source->info.enum_active_sources) - source->info.enum_active_sources(source->context.data, enum_source_active_tree_callback, &data); - - obs_source_release(source); -} - static void enum_source_full_tree_callback(obs_source_t *parent, obs_source_t *child, void *param) { struct source_enum_data *data = param; From dbb6499b2e2f0341a87cbf82a492e6fa88e9c78c Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Wed, 25 Nov 2026 11:14:25 -0800 Subject: [PATCH 2/4] test: characterize obs_source_enum_active_sources and obs_source_enum_active_tree (Refs #5729) New cmocka test test_source_enum_active (tests.d + test-graph.json) on private sources of a started core, calling the real libobs exports: NULL source logs one LOG_DEBUG line per function; a type without enum_active_sources is a silent no-op; enum_active_sources reports direct children only in plugin order; enum_active_tree walks depth first, post-order, reporting (edge parent, child, param), not descending into a child without the callback, and walking a shared child once per edge. Co-Authored-By: Claude Opus 5.5 --- test/cmocka/build/test-graph.json | 28 ++ test/cmocka/test_source_enum_active.c | 354 ++++++++++++++++++ .../tests.d/test_source_enum_active.cmake | 7 + 3 files changed, 389 insertions(+) create mode 100644 test/cmocka/test_source_enum_active.c create mode 100644 test/cmocka/tests.d/test_source_enum_active.cmake diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 43a53f0b66..772b26b769 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -17957,6 +17957,21 @@ "SIMDe::SIMDe" ] }, + "test_source_enum_active": { + "sources": [ + "test/cmocka/test_source_enum_active.c" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ] + }, "test_source_enum_filters": { "sources": [ "test/cmocka/test_source_enum_filters.c" @@ -29987,6 +30002,19 @@ "${target}" ] }, + "test_source_enum_active": { + "target": "test_source_enum_active", + "command": [ + "${target}" + ], + "suite": "core", + "optional_prefixes": [ + [ + "xvfb-run", + "-a" + ] + ] + }, "test_source_enum_filters": { "target": "test_source_enum_filters", "command": [ diff --git a/test/cmocka/test_source_enum_active.c b/test/cmocka/test_source_enum_active.c new file mode 100644 index 0000000000..0111f2e344 --- /dev/null +++ b/test/cmocka/test_source_enum_active.c @@ -0,0 +1,354 @@ +/* Characterization of obs_source_enum_active_sources() and + * obs_source_enum_active_tree() (#5729): show and activate propagation, + * scenes and the audio tick walk a source's active children through them. + * + * What it pins, on private sources of a started core, calling the exported + * functions of the real libobs: + * + * - NULL source: one LOG_DEBUG line, ": Null 'source' + * parameter", and the callback never runs. + * - A source whose type has no enum_active_sources: nothing is called and + * nothing is logged. + * - enum_active_sources reports the source's direct active children only, + * in the plugin's order, as (source, child, param). + * - enum_active_tree walks depth first and reports each edge after the + * child's own subtree (post-order), as (edge parent, child, param); a + * child without enum_active_sources is reported but not descended into; + * a shared child is reported once per edge. + * - The plugin's enum_active_sources runs once per walked node with that + * node's own data. + * + * obs_startup() needs a display on Linux, so the test runs under xvfb-run + * when it is installed. */ +#include +#include +#include +#include +#include +#include +#include +#include "obs-internal.h" + +#define NODE_ID "test_source_enum_active_node" +#define LEAF_ID "test_source_enum_active_leaf" +#define MAX_CHILDREN 4 +#define MAX_EDGES 32 + +/* ------------------------------------------------------------------------- */ +/* log capture */ + +static unsigned log_count; +static int log_level; +static char log_message[160]; +#if defined(__GNUC__) || defined(__clang__) +static void capture_log(int level, const char *format, va_list args, void *param) __attribute__((format(printf, 2, 0))); +#endif +static void capture_log(int level, const char *format, va_list args, void *param) +{ + (void)param; + log_count++; + log_level = level; + vsnprintf(log_message, sizeof(log_message), format, args); +} + +static log_handler_t saved_handler; +static void *saved_param; + +static void capture_start(void) +{ + base_get_log_handler(&saved_handler, &saved_param); + base_set_log_handler(capture_log, NULL); + log_count = 0; + log_level = -1; + log_message[0] = '\0'; +} + +static void capture_stop(void) +{ + base_set_log_handler(saved_handler, saved_param); +} + +/* ------------------------------------------------------------------------- */ +/* source types */ + +/* A node lists `children` through enum_active_sources and counts the calls. */ +struct node { + obs_source_t *self; + obs_source_t *children[MAX_CHILDREN]; + size_t child_count; + unsigned calls; +}; + +static const char *node_name(void *type_data) +{ + (void)type_data; + return "enum active node"; +} + +static void *node_create(obs_data_t *settings, obs_source_t *source) +{ + (void)settings; + struct node *node = bzalloc(sizeof(*node)); + node->self = source; + return node; +} + +static void node_destroy(void *data) +{ + bfree(data); +} + +static uint32_t node_size(void *data) +{ + (void)data; + return 16; +} + +static void node_enum_active(void *data, obs_source_enum_proc_t enum_callback, void *param) +{ + struct node *node = data; + node->calls++; + for (size_t i = 0; i < node->child_count; i++) + enum_callback(node->self, node->children[i], param); +} + +static void register_types(void) +{ + struct obs_source_info node = { + .id = NODE_ID, + .type = OBS_SOURCE_TYPE_INPUT, + .output_flags = OBS_SOURCE_VIDEO, + .get_name = node_name, + .create = node_create, + .destroy = node_destroy, + .get_width = node_size, + .get_height = node_size, + .enum_active_sources = node_enum_active, + }; + obs_register_source(&node); + + struct obs_source_info leaf = node; + leaf.id = LEAF_ID; + leaf.enum_active_sources = NULL; + obs_register_source(&leaf); +} + +/* ------------------------------------------------------------------------- */ +/* fixture: nodes 0..3, leaf 4 */ + +#define NODES 4 +#define LEAF 4 +#define ALL 5 + +static obs_source_t *src[ALL]; +static struct node *nodes[ALL]; + +static int fixture_setup(void **state) +{ + (void)state; + static const char *names[ALL] = {"a", "b", "c", "d", "leaf"}; + for (size_t i = 0; i < ALL; i++) { + src[i] = obs_source_create_private(i == LEAF ? LEAF_ID : NODE_ID, names[i], NULL); + assert_non_null(src[i]); + nodes[i] = obs_obj_get_data(src[i]); + assert_non_null(nodes[i]); + } + return 0; +} + +static int fixture_teardown(void **state) +{ + (void)state; + for (size_t i = 0; i < ALL; i++) + obs_source_release(src[i]); + /* Sources are destroyed on the destruction thread, which logs. Let it + * finish here, so those lines never land in the next test's capture. */ + obs_wait_for_destroy_queue(); + memset(src, 0, sizeof(src)); + memset(nodes, 0, sizeof(nodes)); + return 0; +} + +static void list(size_t parent, size_t child) +{ + struct node *node = nodes[parent]; + assert_true(node->child_count < MAX_CHILDREN); + node->children[node->child_count++] = src[child]; +} + +/* ------------------------------------------------------------------------- */ +/* recorded edges */ + +struct edges { + size_t count; + obs_source_t *parent[MAX_EDGES]; + obs_source_t *child[MAX_EDGES]; +}; + +static void record(obs_source_t *parent, obs_source_t *child, void *param) +{ + struct edges *edges = param; + if (edges->count < MAX_EDGES) { + edges->parent[edges->count] = parent; + edges->child[edges->count] = child; + } + edges->count++; +} + +static void assert_edges(const struct edges *edges, const size_t (*want)[2], size_t count) +{ + assert_int_equal(edges->count, count); + for (size_t i = 0; i < count; i++) { + assert_ptr_equal(edges->parent[i], src[want[i][0]]); + assert_ptr_equal(edges->child[i], src[want[i][1]]); + } +} + +/* ------------------------------------------------------------------------- */ +/* cases */ + +static void null_source_logs_once(void **state) +{ + (void)state; + struct edges edges = {0}; + + capture_start(); + obs_source_enum_active_sources(NULL, record, &edges); + capture_stop(); + assert_int_equal(log_count, 1); + assert_int_equal(log_level, LOG_DEBUG); + assert_string_equal(log_message, "obs_source_enum_active_sources: Null 'source' parameter"); + + capture_start(); + obs_source_enum_active_tree(NULL, record, &edges); + capture_stop(); + assert_int_equal(log_count, 1); + assert_int_equal(log_level, LOG_DEBUG); + assert_string_equal(log_message, "obs_source_enum_active_tree: Null 'source' parameter"); + + assert_int_equal(edges.count, 0); +} + +static void source_without_callback_does_nothing(void **state) +{ + (void)state; + struct edges edges = {0}; + + capture_start(); + obs_source_enum_active_sources(src[LEAF], record, &edges); + obs_source_enum_active_tree(src[LEAF], record, &edges); + capture_stop(); + assert_int_equal(log_count, 0); + assert_int_equal(edges.count, 0); +} + +static void node_without_children_calls_plugin_once(void **state) +{ + (void)state; + struct edges edges = {0}; + + obs_source_enum_active_sources(src[0], record, &edges); + assert_int_equal(nodes[0]->calls, 1); + obs_source_enum_active_tree(src[0], record, &edges); + assert_int_equal(nodes[0]->calls, 2); + assert_int_equal(edges.count, 0); +} + +/* a -> b -> c, a -> leaf, b -> d */ +static void build_tree(void) +{ + list(0, 1); + list(0, LEAF); + list(1, 2); + list(1, 3); +} + +static void sources_reports_direct_children_only(void **state) +{ + (void)state; + struct edges edges = {0}; + build_tree(); + + obs_source_enum_active_sources(src[0], record, &edges); + static const size_t want[][2] = {{0, 1}, {0, LEAF}}; + assert_edges(&edges, want, 2); + assert_int_equal(nodes[0]->calls, 1); + assert_int_equal(nodes[1]->calls, 0); +} + +static void tree_is_depth_first_post_order(void **state) +{ + (void)state; + struct edges edges = {0}; + build_tree(); + + obs_source_enum_active_tree(src[0], record, &edges); + static const size_t want[][2] = {{1, 2}, {1, 3}, {0, 1}, {0, LEAF}}; + assert_edges(&edges, want, 4); + assert_int_equal(nodes[0]->calls, 1); + assert_int_equal(nodes[1]->calls, 1); + assert_int_equal(nodes[2]->calls, 1); + assert_int_equal(nodes[3]->calls, 1); +} + +/* a -> b, a -> c, b -> d, c -> d: d is walked and reported once per edge. */ +static void shared_child_is_reported_per_edge(void **state) +{ + (void)state; + struct edges edges = {0}; + list(0, 1); + list(0, 2); + list(1, 3); + list(2, 3); + + obs_source_enum_active_tree(src[0], record, &edges); + static const size_t want[][2] = {{1, 3}, {0, 1}, {2, 3}, {0, 2}}; + assert_edges(&edges, want, 4); + assert_int_equal(nodes[3]->calls, 2); +} + +static void tree_from_inner_node(void **state) +{ + (void)state; + struct edges edges = {0}; + build_tree(); + + obs_source_enum_active_tree(src[1], record, &edges); + static const size_t want[][2] = {{1, 2}, {1, 3}}; + assert_edges(&edges, want, 2); + assert_int_equal(nodes[0]->calls, 0); +} + +/* ------------------------------------------------------------------------- */ + +static int group_setup(void **state) +{ + (void)state; + if (!obs_startup("en-US", NULL, NULL)) + return -1; + register_types(); + return 0; +} + +static int group_teardown(void **state) +{ + (void)state; + obs_shutdown(); + return 0; +} + +#define CASE(f) cmocka_unit_test_setup_teardown(f, fixture_setup, fixture_teardown) + +int main(void) +{ + const struct CMUnitTest tests[] = { + CASE(null_source_logs_once), + CASE(source_without_callback_does_nothing), + CASE(node_without_children_calls_plugin_once), + CASE(sources_reports_direct_children_only), + CASE(tree_is_depth_first_post_order), + CASE(shared_child_is_reported_per_edge), + CASE(tree_from_inner_node), + }; + return cmocka_run_group_tests(tests, group_setup, group_teardown); +} diff --git a/test/cmocka/tests.d/test_source_enum_active.cmake b/test/cmocka/tests.d/test_source_enum_active.cmake new file mode 100644 index 0000000000..9d55bbd2df --- /dev/null +++ b/test/cmocka/tests.d/test_source_enum_active.cmake @@ -0,0 +1,7 @@ +# obs_source_enum_active_sources and obs_source_enum_active_tree (#5729): +# NULL source logs one LOG_DEBUG line; a type without enum_active_sources is +# a no-op; the first reports direct active children in plugin order, the +# second walks depth first and reports each edge after the child's subtree, +# descending only into children with the callback. Private sources under a +# started core. +# Registered once in build/test-graph.json, consumed by CMake and Cargo. From 587bab794da72171aca961947bff6581a65b0d9c Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Wed, 25 Nov 2026 11:19:47 -0800 Subject: [PATCH 3/4] test: RED enum active sources and tree safe core (Refs #5729) Tier 1 mirrors of test_source_enum_active in the existing obs-tests-core binary (rust/obs-source/tests/source/enum_active.rs), on a model of what the core reaches through obs-source::enum_active's Libobs trait: nodes listing active children, transitions reporting their slots, the reference calls, and the caller's and the tree walk's sinks. They also cover the dying-source, data-less and transition arms. They run against todo!() cores: test result: FAILED. 0 passed; 14 failed panicked at rust/obs-source/src/enum_active.rs:67:5: not yet implemented: #5729 Co-Authored-By: Claude Opus 5.5 --- rust/obs-source/src/enum_active.rs | 77 +++++ rust/obs-source/src/lib.rs | 1 + rust/obs-source/tests/source/enum_active.rs | 330 ++++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 4 files changed, 409 insertions(+) create mode 100644 rust/obs-source/src/enum_active.rs create mode 100644 rust/obs-source/tests/source/enum_active.rs diff --git a/rust/obs-source/src/enum_active.rs b/rust/obs-source/src/enum_active.rs new file mode 100644 index 0000000000..1f1a2aae5c --- /dev/null +++ b/rust/obs-source/src/enum_active.rs @@ -0,0 +1,77 @@ +//! `obs_source_enum_active_sources()` and `obs_source_enum_active_tree()`, +//! which show and activate propagation, scenes and the audio tick use to +//! walk a source's active children. Both refuse a NULL source (logged) or +//! one without context data (silently), return early for a non-transition +//! type without `enum_active_sources`, then hold a reference for the call: +//! a transition reports its slots first, then the type's +//! `enum_active_sources` runs. The tree walk wraps the caller's callback so +//! each child's own subtree is reported before the edge into it +//! ([`tree_edge`]). The plugin callbacks, `obs_transition_enum_sources()` +//! and the reference calls stay C; the core reaches them through +//! [`Libobs`]. + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::unimplemented, + clippy::unreachable +)] + +use core::ffi::CStr; + +/// `obs_source_enum_active_sources`, named in its NULL diagnostic. +pub const SOURCES_FUNCTION: &CStr = c"obs_source_enum_active_sources"; +/// `obs_source_enum_active_tree`, named in its NULL diagnostic. +pub const TREE_FUNCTION: &CStr = c"obs_source_enum_active_tree"; + +/// What the two calls reach in libobs. `Source` is a non-NULL source; +/// `Sink` is the callback and parameter handed on to the C (the caller's +/// for the direct call, the tree walk's own for the tree). +pub trait Libobs { + /// A source handle. + type Source: Copy; + /// A callback with its parameter. + type Sink: Copy; + /// `source->context.data != NULL`. + fn has_data(&mut self, source: Self::Source) -> bool; + /// `source->info.type == OBS_SOURCE_TYPE_TRANSITION`. + fn is_transition(&mut self, source: Self::Source) -> bool; + /// `source->info.enum_active_sources != NULL`. + fn has_enum_active(&mut self, source: Self::Source) -> bool; + /// `obs_source_get_ref(source)`; `None` while it is being destroyed. + fn get_ref(&mut self, source: Self::Source) -> Option; + /// `obs_source_release(source)`. + fn release(&mut self, source: Self::Source); + /// `obs_transition_enum_sources(source, sink)`. + fn transition_enum(&mut self, source: Self::Source, sink: Self::Sink); + /// `source->info.enum_active_sources(source->context.data, sink)`. + fn enum_active(&mut self, source: Self::Source, sink: Self::Sink); + /// `LOG_DEBUG` `": Null 'source' parameter"`. + fn log_null(&mut self, function: &'static CStr); +} + +/// The body both calls share, `function` naming the call in diagnostics: +/// `sink` is the caller's callback for `obs_source_enum_active_sources()` +/// and the tree walk's for `obs_source_enum_active_tree()`. +pub fn enumerate( + libobs: &mut L, + function: &'static CStr, + source: Option, + sink: L::Sink, +) { + let _ = (libobs, function, source, sink); + todo!("#5729") +} + +/// `enum_source_active_tree_callback()` up to the caller's callback: a +/// transition child reports its slots and a child with `enum_active_sources` +/// and data reports its own children, both through the tree walk's `sink`. +/// The caller then reports the edge itself. +pub fn tree_edge(libobs: &mut L, child: L::Source, sink: L::Sink) { + let _ = (libobs, child, sink); + todo!("#5729") +} diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index 82b48615c6..337fdb137d 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -226,6 +226,7 @@ pub mod deinterlace_texture; pub mod draw_plan; pub mod draw_technique; pub mod enabled_setter; +pub mod enum_active; pub mod enum_filters; pub mod filter_backup; pub mod filter_count; diff --git a/rust/obs-source/tests/source/enum_active.rs b/rust/obs-source/tests/source/enum_active.rs new file mode 100644 index 0000000000..c718be1cb7 --- /dev/null +++ b/rust/obs-source/tests/source/enum_active.rs @@ -0,0 +1,330 @@ +//! Tier 1 mirrors of test_source_enum_active.c, plus the transition, dying +//! and data-less arms the C test reaches only through the parity oracle. +//! The C the core reaches is modelled here: nodes whose +//! `enum_active_sources` lists children, transitions whose two slots +//! `obs_transition_enum_sources()` reports, and the reference calls. The +//! caller's callback and the tree walk's are the model's two sinks; the +//! tree sink runs [`tree_edge`] and then reports the edge, as +//! `enum_source_active_tree_callback()` does. +use core::ffi::CStr; +use obs_source::enum_active::{Libobs, SOURCES_FUNCTION, TREE_FUNCTION, enumerate, tree_edge}; + +const NODES: usize = 6; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Sink { + User, + Tree, +} + +/// One C call, report or log line, in order. +#[derive(Debug, Clone, PartialEq, Eq)] +enum Event { + GetRef(usize), + Release(usize), + Transition(usize, Sink), + EnumActive(usize, Sink), + Edge(usize, usize), + Log(&'static CStr), +} + +struct Model { + children: [Vec; NODES], + slots: [Vec; NODES], + transition: [bool; NODES], + enum_active: [bool; NODES], + data: [bool; NODES], + dying: [bool; NODES], + events: Vec, +} + +impl Default for Model { + fn default() -> Self { + Self { + children: Default::default(), + slots: Default::default(), + transition: [false; NODES], + enum_active: [true; NODES], + data: [true; NODES], + dying: [false; NODES], + events: Vec::new(), + } + } +} + +impl Model { + fn report(&mut self, parent: usize, child: usize, sink: Sink) { + if sink == Sink::Tree { + tree_edge(self, child, Sink::Tree); + } + self.events.push(Event::Edge(parent, child)); + } + + fn sources(&mut self, source: Option) { + enumerate(self, SOURCES_FUNCTION, source, Sink::User); + } + + fn tree(&mut self, source: Option) { + enumerate(self, TREE_FUNCTION, source, Sink::Tree); + } + + fn edges(&self) -> Vec<(usize, usize)> { + self.events + .iter() + .filter_map(|e| match e { + Event::Edge(p, c) => Some((*p, *c)), + _ => None, + }) + .collect() + } +} + +impl Libobs for Model { + type Source = usize; + type Sink = Sink; + + fn has_data(&mut self, source: usize) -> bool { + self.data[source] + } + + fn is_transition(&mut self, source: usize) -> bool { + self.transition[source] + } + + fn has_enum_active(&mut self, source: usize) -> bool { + self.enum_active[source] + } + + fn get_ref(&mut self, source: usize) -> Option { + self.events.push(Event::GetRef(source)); + (!self.dying[source]).then_some(source) + } + + fn release(&mut self, source: usize) { + self.events.push(Event::Release(source)); + } + + fn transition_enum(&mut self, source: usize, sink: Sink) { + self.events.push(Event::Transition(source, sink)); + for slot in self.slots[source].clone() { + self.report(source, slot, sink); + } + } + + fn enum_active(&mut self, source: usize, sink: Sink) { + assert!(self.enum_active[source], "called a missing callback"); + self.events.push(Event::EnumActive(source, sink)); + for child in self.children[source].clone() { + self.report(source, child, sink); + } + } + + fn log_null(&mut self, function: &'static CStr) { + self.events.push(Event::Log(function)); + } +} + +const LEAF: usize = 4; + +/// a(0) -> b(1) -> c(2), a -> leaf, b -> d(3); the leaf has no callback. +fn tree_model() -> Model { + let mut m = Model::default(); + m.enum_active[LEAF] = false; + m.children[0] = vec![1, LEAF]; + m.children[1] = vec![2, 3]; + m +} + +#[test] +fn null_source_logs_once() { + let mut m = Model::default(); + m.sources(None); + m.tree(None); + assert_eq!( + m.events, + [Event::Log(SOURCES_FUNCTION), Event::Log(TREE_FUNCTION)] + ); +} + +#[test] +fn source_without_callback_does_nothing() { + let mut m = tree_model(); + m.sources(Some(LEAF)); + m.tree(Some(LEAF)); + assert!(m.events.is_empty()); +} + +#[test] +fn source_without_data_does_nothing() { + let mut m = tree_model(); + m.data[0] = false; + m.sources(Some(0)); + m.tree(Some(0)); + assert!(m.events.is_empty()); +} + +#[test] +fn node_without_children_calls_plugin_once() { + let mut m = Model::default(); + m.sources(Some(0)); + assert_eq!( + m.events, + [ + Event::GetRef(0), + Event::EnumActive(0, Sink::User), + Event::Release(0) + ] + ); +} + +#[test] +fn sources_reports_direct_children_only() { + let mut m = tree_model(); + m.sources(Some(0)); + assert_eq!(m.edges(), [(0, 1), (0, LEAF)]); + assert!(!m.events.contains(&Event::EnumActive(1, Sink::User))); +} + +#[test] +fn tree_is_depth_first_post_order() { + let mut m = tree_model(); + m.tree(Some(0)); + assert_eq!( + m.events, + [ + Event::GetRef(0), + Event::EnumActive(0, Sink::Tree), + Event::EnumActive(1, Sink::Tree), + Event::EnumActive(2, Sink::Tree), + Event::Edge(1, 2), + Event::EnumActive(3, Sink::Tree), + Event::Edge(1, 3), + Event::Edge(0, 1), + Event::Edge(0, LEAF), + Event::Release(0), + ] + ); +} + +#[test] +fn shared_child_is_reported_per_edge() { + let mut m = Model::default(); + m.children[0] = vec![1, 2]; + m.children[1] = vec![3]; + m.children[2] = vec![3]; + m.tree(Some(0)); + assert_eq!(m.edges(), [(1, 3), (0, 1), (2, 3), (0, 2)]); +} + +#[test] +fn tree_from_inner_node() { + let mut m = tree_model(); + m.tree(Some(1)); + assert_eq!(m.edges(), [(1, 2), (1, 3)]); +} + +#[test] +fn child_without_data_is_reported_but_not_walked() { + let mut m = tree_model(); + m.data[1] = false; + m.tree(Some(0)); + assert_eq!(m.edges(), [(0, 1), (0, LEAF)]); + assert!(!m.events.contains(&Event::EnumActive(1, Sink::Tree))); +} + +#[test] +fn dying_source_logs_after_taking_no_reference() { + let mut m = tree_model(); + m.dying[0] = true; + m.sources(Some(0)); + m.tree(Some(0)); + assert_eq!( + m.events, + [ + Event::GetRef(0), + Event::Log(SOURCES_FUNCTION), + Event::GetRef(0), + Event::Log(TREE_FUNCTION), + ] + ); +} + +#[test] +fn transition_reports_slots_before_its_own_children() { + let mut m = Model::default(); + m.transition[0] = true; + m.slots[0] = vec![1, 2]; + m.children[0] = vec![3]; + m.sources(Some(0)); + assert_eq!( + m.events, + [ + Event::GetRef(0), + Event::Transition(0, Sink::User), + Event::Edge(0, 1), + Event::Edge(0, 2), + Event::EnumActive(0, Sink::User), + Event::Edge(0, 3), + Event::Release(0), + ] + ); +} + +#[test] +fn transition_without_callback_still_reports_slots() { + let mut m = Model::default(); + m.transition[0] = true; + m.enum_active[0] = false; + m.slots[0] = vec![1]; + m.sources(Some(0)); + assert_eq!( + m.events, + [ + Event::GetRef(0), + Event::Transition(0, Sink::User), + Event::Edge(0, 1), + Event::Release(0), + ] + ); +} + +#[test] +fn tree_descends_into_transition_children() { + // a -> t(1, transition, slots 2 and 3, no callback); 2 -> 5. + let mut m = Model::default(); + m.children[0] = vec![1]; + m.transition[1] = true; + m.enum_active[1] = false; + m.slots[1] = vec![2, 3]; + m.children[2] = vec![5]; + m.tree(Some(0)); + assert_eq!( + m.events, + [ + Event::GetRef(0), + Event::EnumActive(0, Sink::Tree), + Event::Transition(1, Sink::Tree), + Event::EnumActive(2, Sink::Tree), + Event::EnumActive(5, Sink::Tree), + Event::Edge(2, 5), + Event::Edge(1, 2), + Event::EnumActive(3, Sink::Tree), + Event::Edge(1, 3), + Event::Edge(0, 1), + Event::Release(0), + ] + ); +} + +#[test] +fn transition_child_without_data_still_reports_slots() { + let mut m = Model::default(); + m.children[0] = vec![1]; + m.transition[1] = true; + m.data[1] = false; + m.slots[1] = vec![2]; + m.tree(Some(0)); + assert_eq!(m.edges(), [(1, 2), (0, 1)]); + assert!(m.events.contains(&Event::Transition(1, Sink::Tree))); + assert!(!m.events.contains(&Event::EnumActive(1, Sink::Tree))); +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 720094de45..1122aae18c 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -72,6 +72,7 @@ mod draw_plan; mod draw_plan_parity; mod enabled_setter; mod enabled_setter_parity; +mod enum_active; mod enum_filters; mod enum_filters_parity; mod filter_backup; From 7c39166aa6098493fb5415167b485ea20d2dec7d Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Wed, 25 Nov 2026 11:26:43 -0800 Subject: [PATCH 4/4] obs-source: port obs_source_enum_active_sources and obs_source_enum_active_tree to Rust (Fixes #5729) - Safe core obs-source::enum_active::{enumerate, tree_edge} over a Libobs trait: NULL source logged with the function's name, no context data or (non-transition) no enum_active_sources returns silently; then obs_source_get_ref() (a dying source logs the NULL line), transition slots first, the type's enum_active_sources, release. tree_edge is enum_source_active_tree_callback() up to the caller's callback: a transition child's slots, then (with callback and data) its own children, through the tree sink; the edge is reported after. - C ABI shims in ffi/source_enum_active.rs: fields and the plugin call through the new libobs/obs-source-enum-active-fields.c.h (live core-source group, no EXPORT); obs_transition_enum_sources(), obs_source_get_ref() and obs_source_release() through Calls; the tree callback is a generic extern "C" fn over the same Calls. - The extracted file moves to the reference-only group libobs.ported-source-enum-active (+ ported-source-sets JSON); both names are EXPORT in obs.h, so they go into rust-exports.d/obs-source-source_enum_active.txt. - Oracle: oracle.d/source-enum-active.list and oracle/source_enum_active.c (the original C renamed oracle_*, the production field adapters, a scripted plugin callback and recording get_ref/release/transition stand-ins). The obs_source_enum_active_tree() stand-in in oracle/audio_callback_links.c is removed: the Rust export defines it. - Tier 3 enum_active_parity.rs: every plugin call (with whether it got the caller's callback and param), reference, transition enum and reported edge in order, the log lines and every byte, over a grid, a proptest and pinned answers; four shim mutants rejected. - obs-build registration test and a rust/README.md row. test result: ok. 15 passed; 0 failed (obs-tests-core enum_active) Co-Authored-By: Claude Opus 5.5 Ci-Attestation: {"at":1795692230,"gate":"data/all/compatibility","host":"linux-x86_64","key":"","lane":"client-checks","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":84,"stamp":"ff5f97f4eefad89b44bf6750939420f8","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692230,"gate":"data/all/flatpak-manifest","host":"linux-x86_64","key":"","lane":"client-checks","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":84,"stamp":"e3b9427b067c4fa17966e696380cc571","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692230,"gate":"data/all/qt-xml","host":"linux-x86_64","key":"","lane":"client-checks","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":84,"stamp":"9aeb521a002ec1a31fab7522a6c5f9c8","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692230,"gate":"data/all/services-schema","host":"linux-x86_64","key":"","lane":"client-checks","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":84,"stamp":"1544ec4b6e0e19b9272cd865a3a4bf18","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692144,"gate":"general/all/client-lint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"74ba567af3d06b7d83237d1978a2e018","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692230,"gate":"python/all/self-test","host":"linux-x86_64","key":"","lane":"client-checks","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":84,"stamp":"77c96206fc9cc46e459e01ee80fa9aff","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/aarch64-apple-darwin/client-clippy","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"e4bc0f43eab929b994500b421014d9c3","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/aarch64-apple-darwin/client-dylint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"5fef64a7109ce9d3ee0955a140309d2f","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/aarch64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"9a87fe9fc603c8526f5c4f5c111572d8","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/aarch64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"799e15d6f9c09d4f1362ae12dacbb6f1","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692144,"gate":"rust/aarch64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"6c6ea1b9fad46cec630feb1aa70006eb","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/aarch64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"72e1567cbb921723d85c5a335809b126","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/x86_64-apple-darwin/client-clippy","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"8df17712065d4bf5162b892170b87dc4","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/x86_64-apple-darwin/client-dylint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"c50fe0a5243e7ce45be66feee5829ce6","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/x86_64-pc-windows-msvc/client-clippy","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"b565de30958451fff70f5669e719c8b1","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692145,"gate":"rust/x86_64-pc-windows-msvc/client-dylint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"332d1a7ffd6d36da46075dde5c070f51","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692144,"gate":"rust/x86_64-unknown-linux-gnu/client-clippy","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"163a65bf981695f0d935846fad134b3f","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692144,"gate":"rust/x86_64-unknown-linux-gnu/client-dylint","host":"linux-x86_64","key":"b33f96c72c795bb7614ba8d145ff6d6487152ed48fe6ab04ac401b80943edc17","lane":"client-lint","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":317,"stamp":"577fbd0183784e1dc5e970b007eec7c4","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Ci-Attestation: {"at":1795692592,"gate":"rust/x86_64-unknown-linux-gnu/client-test","host":"linux-x86_64","key":"9b99fde274313fc3b9a22ae1d56415def455383eb6dd3a28d3a98fc10349c0e8","lane":"client-test-x86_64-unknown-linux-gnu","parents":["587bab794da72171aca961947bff6581a65b0d9c"],"secs":361,"stamp":"d2d38917bcfc1f4cc2b76b578f4974c9","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66","v":1,"via":"run"} Client-Checks: {"checks":{"base":"e8cbea4dd0142b45229389ff871e402fda40050a","coverage":"all-tracked-applicable-files","diff":["libobs/build/libobs.core-source+.h.c","libobs/build/libobs.ported-source-enum-active+.h.c","libobs/build/ported-source-sets/source-enum-active.json","libobs/cmake/rust-exports.d/obs-source-source_enum_active.txt","libobs/obs-source-enum-active-fields.c.h","libobs/obs-source-enum-active.c.h","libobs/obs-source.c.h","rust/README.md","rust/obs-build/tests/native.rs","rust/obs-build/tests/native/registration/canonical_source_enum_active_swap_is_registered.rs","rust/obs-c-oracle/oracle.d/source-enum-active.list","rust/obs-c-oracle/oracle/audio_callback_links.c","rust/obs-c-oracle/oracle/source_enum_active.c","rust/obs-source/src/enum_active.rs","rust/obs-source/src/ffi/mod.rs","rust/obs-source/src/ffi/source_enum_active.rs","rust/obs-source/src/lib.rs","rust/obs-source/tests/source/enum_active.rs","rust/obs-source/tests/source/enum_active_parity.rs","rust/obs-source/tests/source/main.rs","test/cmocka/build/test-graph.json","test/cmocka/test_source_enum_active.c","test/cmocka/tests.d/test_source_enum_active.cmake"],"head":"eb9d29f1ef877d7cf2585bb33c630b80bd865a7c","tools":{"orchestrator-python":"3.13.15 (main, Aug 14 2026, 15:34:59) [Clang 22.1.3 ]","schema-dependencies":"3.13.15 (main, Aug 14 2026, 15:34:59) [Clang 22.1.3 ]\n{'jsonschema': '4.26.0', 'json_source_map': '1.0.5'}","uv":"uv 0.13.5 (x86_64-unknown-linux-gnu)","xmllint":"xmllint: using libxml version 21503\n compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 DTDValid HTML C14N Catalog XPath XPointer XInclude Iconv ISO8859X Regexps Automata RelaxNG Schemas Modules Debug"},"tracked-digest":"23cd197c720122a65e8651063775476e44cdd1ee07236721caf5a4aaecfdce42","tree":"41e0ec99e986d891e09a26d0a61d31876ab95a66"}} --- libobs/build/libobs.core-source+.h.c | 2 +- .../libobs.ported-source-enum-active+.h.c | 4 + .../source-enum-active.json | 5 + .../obs-source-source_enum_active.txt | 2 + libobs/obs-source-enum-active-fields.c.h | 29 ++ rust/README.md | 1 + rust/obs-build/tests/native.rs | 2 + ...l_source_enum_active_swap_is_registered.rs | 44 ++ .../oracle.d/source-enum-active.list | 1 + .../oracle/audio_callback_links.c | 17 +- rust/obs-c-oracle/oracle/source_enum_active.c | 229 ++++++++++ rust/obs-source/src/enum_active.rs | 42 +- rust/obs-source/src/ffi/mod.rs | 1 + rust/obs-source/src/ffi/source_enum_active.rs | 247 +++++++++++ .../tests/source/enum_active_parity.rs | 411 ++++++++++++++++++ rust/obs-source/tests/source/main.rs | 1 + 16 files changed, 1022 insertions(+), 16 deletions(-) create mode 100644 libobs/build/libobs.ported-source-enum-active+.h.c create mode 100644 libobs/build/ported-source-sets/source-enum-active.json create mode 100644 libobs/cmake/rust-exports.d/obs-source-source_enum_active.txt create mode 100644 libobs/obs-source-enum-active-fields.c.h create mode 100644 rust/obs-build/tests/native/registration/canonical_source_enum_active_swap_is_registered.rs create mode 100644 rust/obs-c-oracle/oracle.d/source-enum-active.list create mode 100644 rust/obs-c-oracle/oracle/source_enum_active.c create mode 100644 rust/obs-source/src/ffi/source_enum_active.rs create mode 100644 rust/obs-source/tests/source/enum_active_parity.rs diff --git a/libobs/build/libobs.core-source+.h.c b/libobs/build/libobs.core-source+.h.c index 23a37d0a42..0e3acbbbdd 100644 --- a/libobs/build/libobs.core-source+.h.c +++ b/libobs/build/libobs.core-source+.h.c @@ -73,7 +73,7 @@ #include "libobs/obs-source-deinterlace-seam.c.h" #include "libobs/obs-source-deinterlace-texture-fields.c.h" #include "libobs/obs-source-enabled-setter-fields.c.h" -#include "libobs/obs-source-enum-active.c.h" +#include "libobs/obs-source-enum-active-fields.c.h" #include "libobs/obs-source-enum-filters-fields.c.h" #include "libobs/obs-source-filter-backup-fields.c.h" #include "libobs/obs-source-filter-count-fields.c.h" diff --git a/libobs/build/libobs.ported-source-enum-active+.h.c b/libobs/build/libobs.ported-source-enum-active+.h.c new file mode 100644 index 0000000000..2a34b6e290 --- /dev/null +++ b/libobs/build/libobs.ported-source-enum-active+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): original source enum active sources and tree for C reference. */ +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-source-enum-active.c.h" diff --git a/libobs/build/ported-source-sets/source-enum-active.json b/libobs/build/ported-source-sets/source-enum-active.json new file mode 100644 index 0000000000..ab4c9f5e6d --- /dev/null +++ b/libobs/build/ported-source-sets/source-enum-active.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-source-enum-active+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-source-source_enum_active.txt b/libobs/cmake/rust-exports.d/obs-source-source_enum_active.txt new file mode 100644 index 0000000000..903f030e3b --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-source-source_enum_active.txt @@ -0,0 +1,2 @@ +obs_source_enum_active_sources +obs_source_enum_active_tree diff --git a/libobs/obs-source-enum-active-fields.c.h b/libobs/obs-source-enum-active-fields.c.h new file mode 100644 index 0000000000..c41f6c43b4 --- /dev/null +++ b/libobs/obs-source-enum-active-fields.c.h @@ -0,0 +1,29 @@ +#include "libobs/build/libobs.pch.h" + +/* What obs_source_enum_active_sources() and obs_source_enum_active_tree() + * read and call on a source (the obs-source shims, #5729): its context + * data, whether its type is a transition and has enum_active_sources, and + * that callback itself. The caller rejects NULL and keeps the source alive. */ +bool obs_source_enum_active_has_data(const obs_source_t *source); +bool obs_source_enum_active_has_data(const obs_source_t *source) +{ + return source->context.data != NULL; +} + +bool obs_source_enum_active_is_transition(const obs_source_t *source); +bool obs_source_enum_active_is_transition(const obs_source_t *source) +{ + return source->info.type == OBS_SOURCE_TYPE_TRANSITION; +} + +bool obs_source_enum_active_has_callback(const obs_source_t *source); +bool obs_source_enum_active_has_callback(const obs_source_t *source) +{ + return source->info.enum_active_sources != NULL; +} + +void obs_source_enum_active_call(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param); +void obs_source_enum_active_call(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param) +{ + source->info.enum_active_sources(source->context.data, enum_callback, param); +} diff --git a/rust/README.md b/rust/README.md index 4a4897cd30..a138776a7f 100644 --- a/rust/README.md +++ b/rust/README.md @@ -460,6 +460,7 @@ The private output starting/encoder-demand queries use lazy safe Rust decisions | `libobs/obs-source-draw-plan.c.h` | `obs-source::draw_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-draw-plan`). The source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`, as eight functions on plain values: `obs_source_draw_plan_rotation_x()` and `_y()` (the translate that moves a rotated async frame back into its box: height or width for 270 and 90, both for 180, nothing for any other angle; the rotation is in 0-359 since #2136), `obs_source_draw_plan_async_linear_srgb()` (the graphics flag, or an SDR frame into anything but `GS_CS_SRGB`, or an HDR frame tonemapped into SDR), `obs_source_draw_plan_async_nonlinear_alpha()` (an SDR frame drawn linear whose alpha is not linear), `obs_source_draw_plan_technique()` (the technique for each pair of source and current colour spaces, with or without non-linear alpha; `source_render()`'s conversion uses the same table without it), `obs_source_draw_plan_reads_white_level()` and `_multiplier()` (into or out of scRGB, the level over 80 or 80 over the level, `obs_get_video_sdr_white_level()` read only then, the same for both callers) and `obs_source_draw_plan_convert()` (whether `source_render()` draws through `color_space_texrender`: the two spaces in different classes, SDR, 709_EXTENDED and scRGB). The techniques are `enum obs_source_draw_technique` (`obs-source-draw-technique.h`, `obs-source::draw_technique`), shared with the deinterlace draw decisions; C maps them to names. They are declared in `obs-source-draw-plan.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `gs_*` call in its order and under its conditions (the decisions are separate calls, not one plan, so neither `gs_get_linear_srgb()` nor the white level read is hoisted), `convert_video_space()`, `obs_get_video_sdr_white_level()`, `gs_get_format_from_space()`, the texrenders and the technique names. Colour spaces cross as `int` and the rotation as `long` (`c_long`), so values outside the enum or 0-359 take the C's paths. The multiplier is one `f32` division, the C's, bit for bit (NaN payloads and infinities included), and the size converts to `f32` as `(float)uint32_t` does. Kept, characterized, not endorsed: a white level of 0 gives a multiplier of 0 into scRGB and +inf out of it (-0 gives -inf, a NaN stays a NaN); a rotation outside 0-359 is not moved; an SDR frame into a current space outside the enum is drawn linear with non-linear alpha, with `Draw`. No intentional differences. `test_source_draw_plan_steps` compiles the C into itself, and `test_source_draw_plan_steps_rust` runs the same test source against `libobs_rust-static`; `test_async_rotation_normalize`, `test_filter_color_space`, `test_source_color_space_steps`, `test_color_correction_16f`, `test_pipewire_cursor_hdr` and `test_texture_render` see the swap through the real libobs. Tier 3 compares the result bytes of every function: rotations around 0-359, a stride over all of `c_long` and past 32 bits, sizes around 2^24 and `u32::MAX`, every pair of 649 spaces with every flag, every pair of 12 spaces at 26 special white levels, both callers' decisions in their order, and proptests over any rotation, size, space and `f32` white level bit pattern. | | `libobs/obs-source-duplicate.c.h` | `obs-source::source_duplicate` | Swapped in hybrid `libobs` (group `libobs.ported-source-duplicate`). The duplicate decisions of `obs_source_duplicate()` and `get_new_filter_name()` in `obs-source.c.h`, and the scene duplicate decisions of `obs_scene_duplicate()`, `dup_child()` and `dup_group()` in `obs-scene.c.h`, as fifteen functions: the route of `obs_source_duplicate()` one step after each lookup or read (`obs_source_duplicate_type_step`, `_scene_step`, `_group_step`, `_flags_step` and `_created_step`: a public scene gives a new reference, a private copy of a scene or either copy of a group gives a scene copy, any other scene-typed source NULL, a `DO_NOT_DUPLICATE` source a new reference, anything else a new source), the duplicate type of a scene copy (`_scene_type`), the filter copy (`_copies_filters`: not for a filter) and the number `get_new_filter_name()` tries next (`_filter_number`: "Color", "Color 2", ...); the flags of `obs_scene_duplicate()` (`obs_scene_duplicate_unique`, `_private`), its group override (`_keeps_unique`: a public copy of a group keeps its sources), its per-item route (`_item_route`: group, unique child or reference), the reuse step of `dup_child()` for a repeated source (`_reuse`), the name of a copy (`_keeps_name`: only when private) and the group resize (`_resizes`). They are declared in `obs-source-duplicate.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` and `obs-scene.c.h` keep every lookup (`obs_scene_from_source()`, `obs_group_from_source()` only when no scene was found, the output flags only for a source that is not scene-typed, `obs_source_get_filter_by_name()`), every create, reference and release, `obs_data_apply()`, `obs_scene_add()`, `duplicate_filters()` and `duplicate_filter()` with their reverse add order, `filter_compatible()`, `duplicate_item_data()` (with `obs_sceneitem_set_transition()`), `get_child_at_idx()`, `resize_scene()`, every lock, `dstr_printf()` and every log; `obs_scene_duplicate()` keeps each old item's new source (NULL where none was added) for the reuse step, holding a reference to each until it returns, so a handler of the new scene's `item_add` signal that removes an earlier item cannot free a copy a later item reuses. The two source pointers of the reuse step are compared by address, never followed; every other input is a flag or an integer, and the enum values cross as `int`. `obs-source.c.h` checks the source types, `OBS_SOURCE_DO_NOT_DUPLICATE` and the four `OBS_SCENE_DUP_*` values at compile time. Intentional differences (#2905), each a fix: `obs_source_duplicate()` releases the settings and returns NULL when the create returned no source (the C wrote the audio mixers, sync offset, volume, mute and flags through it); a repeated source reuses the first earlier item with the same source whose copy was added to the new scene, and is copied afresh when there is none (the C took the new scene's item at the old index, another item's source, or none, once an earlier item was not added). `obs_source_duplicate_filter_number()` stays at `INT_MAX`, where the C's `int` would overflow. `test_source_duplicate_steps` compiles the C into itself, and `test_source_duplicate_steps_rust` runs the same test source against `libobs_rust-static`; `test_scene_dup_group_children`, `test_scene_duplicate_scale_ref`, `test_scene_private_canvas` and `test_scene_dup_copy_lifetime` (a copy removed by an `item_add` handler stays alive until the duplicate returns) run the callers through the real libobs. Tier 3 compares the header's values, every flag, type and duplicate type from -1000 to 1000 and the ends of `int`, every output flag bit, every filter number from -100,000 to 100,000 and near the ends, every reuse step, and a model of the item loop over every scene of up to 6 items over 3 sources with each item copied, refused by its duplicate or refused by `obs_scene_add()`, plus proptests; the two fixes are the only differences. | | `libobs/obs-source-enabled-setter.c.h` | `obs-source::enabled_setter` | Swapped in hybrid `libobs`; the C reference keeps the unchanged extracted setter. Rejects NULL with its original debug diagnostic, stores the enabled bool before synchronous `enable` dispatch, and emits even for repeated values. Calldata retains the source pointer and C bool representation on a fixed 128-byte stack. Callback payload edits do not rewrite source state; reentrant setters retain their final write. Tier 1 covers state, order, repeats and NULL; Tier 3 checks the real private layout, unchanged C setter, full callback traces, payload mutation, reentrancy and diagnostics. Existing C audio/filter callers remain unchanged Tier 2 coverage. No intentional differences or new test binary. | +| `libobs/obs-source-enum-active.c.h` | `obs-source::enum_active`, shims in `obs-source::ffi::source_enum_active` | #5729: swapped in hybrid `libobs` (group `libobs.ported-source-enum-active`). `obs_source_enum_active_sources()` and `obs_source_enum_active_tree()`, which show and activate propagation, scenes and the audio tick use to walk a source's active children. A NULL source logs one `LOG_DEBUG` line naming the function; a source without context data, or a non-transition whose type has no `enum_active_sources`, returns silently. Otherwise the source is held with `obs_source_get_ref()` (a dying source logs the NULL line and returns), a transition reports its two slots through `obs_transition_enum_sources()`, the type's `enum_active_sources` runs, and the reference is released. The tree walk wraps the caller's callback (`enum_source_active_tree_callback()`): for each child, a transition's slots and, when it has the callback and data, its own active children are walked first, then the edge is reported (depth first, post-order). Safe core over a `Libobs` trait; the shims read the context data, type and callback and make the plugin call through `obs-source-enum-active-fields.c.h` (live group `libobs.core-source`, no `EXPORT`) and call `obs_transition_enum_sources()`, `obs_source_get_ref()` and `obs_source_release()` through `Calls`. The new `test_source_enum_active` (plus the existing transition, slideshow and scene visibility tests) runs against hybrid and reference libraries. Tier 3 runs the original C and the Rust shims on noise-filled real-header sources over a scripted plugin callback and recording reference and transition stand-ins (every call and reported edge in order, the log lines, every byte), a grid, a proptest, and rejects four mutants. The test-only `obs_source_enum_active_tree()` stand-in in `oracle/audio_callback_links.c` is gone: the Rust export defines it in the test binaries now. | Not covered: a NULL child reported to the tree walk (the C dereferences it; the Rust skips the walk and reports the edge). | | `libobs/obs-source-enum-filters.c.h` | `obs-source::enum_filters`, shims in `obs-source::ffi::source_enum_filters` | #5605: swapped in hybrid `libobs` (group `libobs.ported-source-enum-filters`). `obs_source_enum_filters()` calls back once per filter, last to first, with `(source, filter, param)` and `filter_mutex` held across each call; the count is read once and each entry again at its index when visited, so a callback that changes the list (the mutex is recursive) is seen as the C saw it. It writes nothing and takes no reference. A NULL source or callback returns and logs one `LOG_DEBUG` line; a NULL callback is named `'callback_obj.obj'`, the union member upstream's `obs_ptr_valid()` stringizes (kept). Safe Rust core over a `FilterList` trait; the shim rejects NULL before calling the adapters of `obs-source-enum-filters-fields.c.h` (live group `libobs.core-source`, no `EXPORT`). The unchanged `test_source_enum_filters` runs against hybrid and reference libraries through the real libobs. Tier 3 runs the original C and the Rust export on noise-filled real-header sources over every count to 8, three fills, four callback list changes and a proptest (call order, parent and param, lock state, NULL log lines, every byte), and rejects four mutants. | None. | | `libobs/obs-source-filter-backup.c.h` | `obs-source::filter_backup` | Swapped in hybrid `libobs` (group `libobs.ported-source-filter-backup`). `obs_source_backup_filters()` (`EXPORT` in `obs.h`, so the shim is exported): a NULL source logs `obs_source_backup_filters: Null 'source' parameter` at debug level and returns NULL; otherwise it creates the array, takes `filter_mutex`, and for each filter in list order saves it with `obs_save_source()`, pushes it and releases the save's reference, then unlocks. The count and the filter are read again after every save, as the C did, since a save callback runs plugin code under the lock; a NULL save is passed to the push, which skips it. `obs-source-filter-backup-fields.c.h` keeps the lock, the unlock and the list reads in C, and the shim calls `obs_save_source()` and the `obs_data` array calls by symbol. No intentional differences. `test_source_filter_backup` runs against the C, hybrid and C reference builds. Tier 3 runs the C loop and the shim over real private layouts and compares the saved JSON and the reference count of every item, the lock held across each save, a list grown by a save callback, NULL saves, the NULL diagnostic and repeated backups, plus a proptest over lists of up to 16 filters. | | `libobs/obs-source-filter-chain.c.h` | `obs-source::source_filter_chain` | Swapped in hybrid `libobs` (group `libobs.ported-source-filter-chain`). The filter chain decisions of `obs-source.c.h`, as thirteen functions: `filter_compatible()` (`obs_source_filter_chain_compatible`: the filter's video, audio and async bits a subset of the source's, without the async bit for an audio filter with no video); the gates of `obs_source_filter_add()` (`_add_allowed` after the list check, `_checks_compatible`: not for a source that owns its info id, `_ref_step` after the reference) and the new filter's target (`_add_target`: the source, or the old first filter); the filter `obs_source_filter_remove_refless()` retargets (`_remove_retarget`: the one before); the movements of `move_filter_dir()` (`_move_route`), with the walks of `find_next_filter()` and `find_prev_filter()` one filter at a time (`_walk_step`, `_same_kind`: to the nearest filter of the same async kind) and the top and bottom moves (`_edge_move`: whatever the kind); each filter's target in `reorder_filter_targets()` (`_target_of`: the next filter, the last one the source); and the name match of `obs_source_restore_filters()` (`_restore_matches`: an unnamed filter never matches) and how it gets a backed-up filter (`_restore_step`). They are declared in `obs-source-filter-chain.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps every `filter_mutex` lock, `da_find()`, `da_insert()`, `da_erase()`, `da_move_item()` and `da_move()`, every `filter_parent` and `filter_target` write, every reference and release, the loads, every `obs_data` call, the `filter_add` and `filter_remove` callbacks, every signal and every log, and reads each flag where it did (the walks one neighbour per step); `obs_source_filter_remove()`, `obs_source_filter_get_index()`, `obs_source_filter_set_index()`, `duplicate_filters()` and `duplicate_filter()` stay C, and the index setter and `duplicate_filter()` call the ported rules. The names cross as C strings that are only read; every other input is a flag, an index or a count, the out-parameters are written only on true, and the enum values cross as `int`. `obs-source.c.h` checks the output flags and the four movements at compile time. Intentional differences (#2981), each a fix: `obs_source_filter_add()` unlocks `filter_mutex` before returning when the filter can't be referenced (the C returned with it locked, so every later filter call on the source blocked); it refuses a filter that has a parent, with the existing warning (the C took a filter on another source into a second chain); `obs_source_filter_set_order()` with a movement outside `enum obs_order_movement` moves and signals nothing (the C retargeted every filter and signalled `reorder_filters`); `obs_source_restore_filters()` loads an unmatched backed-up filter with `obs_load_private_source()` (the C loaded it public, so a filter whose plugin is missing came back as a public source). `test_source_filter_chain_steps` compiles the C into itself, and `test_source_filter_chain_steps_rust` runs the same test source against `libobs_rust-static`; `test_filter_clipboard`, `test_filter_skip_removed`, `test_filter_size_no_target`, `test_filter_color_space`, `test_scroll_filter_wide_source`, `test_missing_files_list`, `test_ws_filter_settings_event`, `test_async_delay_audio` and `test_color_correction_16f` run the callers through the real libobs. Tier 3 compares the header's values, every pair of 49 flag words, every bool, every count, index and route over lists of up to 20 and near the ends of `usize`, every movement from -1000 to 1000 and the ends of `int`, every pair of 17 names with an unnamed filter, a model of `move_filter_dir()` with its walks and retarget over every chain of up to 7 filters with every async pattern, index and movement from -1 to 5, and a model of the restore loop, plus proptests; the four fixes are the only differences. | diff --git a/rust/obs-build/tests/native.rs b/rust/obs-build/tests/native.rs index 646b87c353..b1a63bcef0 100644 --- a/rust/obs-build/tests/native.rs +++ b/rust/obs-build/tests/native.rs @@ -49,6 +49,8 @@ mod canonical_source_configurable_swap_is_registered; mod canonical_source_defaults_swap_is_registered; #[path = "native/registration/canonical_source_draw_swap_is_registered.rs"] mod canonical_source_draw_swap_is_registered; +#[path = "native/registration/canonical_source_enum_active_swap_is_registered.rs"] +mod canonical_source_enum_active_swap_is_registered; #[path = "native/registration/canonical_source_enum_filters_swap_is_registered.rs"] mod canonical_source_enum_filters_swap_is_registered; #[path = "native/registration/canonical_source_flags_mixers_swap_is_registered.rs"] diff --git a/rust/obs-build/tests/native/registration/canonical_source_enum_active_swap_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_source_enum_active_swap_is_registered.rs new file mode 100644 index 0000000000..638e4bee19 --- /dev/null +++ b/rust/obs-build/tests/native/registration/canonical_source_enum_active_swap_is_registered.rs @@ -0,0 +1,44 @@ +use super::*; + +#[test] +fn canonical_source_enum_active_swap_is_registered() { + use obs_build::validation::graph::{Graph, Source}; + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let mut graph = Graph::load(&root.join("test/cmocka/build/test-graph.json")).unwrap(); + let name = "test_source_enum_active"; + graph.targets.retain(|key, _| key == name); + graph.tests.retain(|key, _| key == name); + for os in ["OS_LINUX", "OS_MACOS", "OS_WINDOWS"] { + let selected = graph.select(&[(os.into(), true)].into()).unwrap(); + // The test compiles no copy of the two calls: it calls the exported + // symbols of the real libobs (Rust in hybrid, the original C in the + // reference). + assert!( + matches!(selected.targets[name].sources.as_slice(), [Source::Path(path)] + if path == "test/cmocka/test_source_enum_active.c") + ); + assert!(selected.targets[name].definitions.is_empty()); + assert_eq!(selected.tests[name].target, name); + } + let ported = + fs::read_to_string(root.join("libobs/build/ported-source-sets/source-enum-active.json")) + .unwrap(); + assert!(ported.contains("build/libobs.ported-source-enum-active+.h.c")); + let group = fs::read_to_string(root.join("libobs/build/libobs.ported-source-enum-active+.h.c")) + .unwrap(); + assert!(group.contains("#include \"libobs/obs-source-enum-active.c.h\"")); + let hybrid = fs::read_to_string(root.join("libobs/build/libobs.core-source+.h.c")).unwrap(); + assert!(!hybrid.contains("#include \"libobs/obs-source-enum-active.c.h\"")); + assert!(hybrid.contains("#include \"libobs/obs-source-enum-active-fields.c.h\"")); + let exports = fs::read_to_string( + root.join("libobs/cmake/rust-exports.d/obs-source-source_enum_active.txt"), + ) + .unwrap(); + assert_eq!( + exports.lines().collect::>(), + [ + "obs_source_enum_active_sources", + "obs_source_enum_active_tree", + ] + ); +} diff --git a/rust/obs-c-oracle/oracle.d/source-enum-active.list b/rust/obs-c-oracle/oracle.d/source-enum-active.list new file mode 100644 index 0000000000..ed81c0b985 --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/source-enum-active.list @@ -0,0 +1 @@ +source-enum-active.inc|libobs/obs-source-enum-active.c.h|auto diff --git a/rust/obs-c-oracle/oracle/audio_callback_links.c b/rust/obs-c-oracle/oracle/audio_callback_links.c index 0ff3a0323b..c3b160cf92 100644 --- a/rust/obs-c-oracle/oracle/audio_callback_links.c +++ b/rust/obs-c-oracle/oracle/audio_callback_links.c @@ -1,15 +1,17 @@ -/* Test-only link stand-ins: obs_source_audio_render() and - * obs_source_enum_active_tree() under libobs's own names, for the test +/* Test-only link stand-in: obs_source_audio_render() under libobs's own + * name, for the test * binaries that link the obs-core shims without libobs. * * The audio tick's shim (rust/obs-core/src/ffi/audio_callback.rs) exports - * audio_callback(), whose libobs calls name them. The MSVC, MinGW and macOS + * audio_callback(), whose libobs calls name it. The MSVC, MinGW and macOS * linkers want every symbol an object names resolved, used or not, so * obs-tests-core needs a definition; libobs has the real ones. * obs_source_active() is the real Rust export of obs-source (#5401), whose * counter adapters oracle/source_showing_active.c compiles, and * obs_source_removed() is its export too (#5587), whose flag adapter - * oracle/source_removed.c compiles. + * oracle/source_removed.c compiles. obs_source_enum_active_tree() is its + * export too (#5729), whose field adapters oracle/source_enum_active.c + * compiles. * obs_source_get_ref() and obs_source_release() (oracle/view.c), * obs_source_get_name() (the obs-missing-files tests), * audio_output_get_sample_rate() and audio_output_get_channels() @@ -29,10 +31,3 @@ void obs_source_audio_render(obs_source_t *source, uint32_t mixers, size_t chann UNUSED_PARAMETER(sample_rate); UNUSED_PARAMETER(size); } - -void obs_source_enum_active_tree(obs_source_t *source, obs_source_enum_proc_t enum_callback, void *param) -{ - UNUSED_PARAMETER(source); - UNUSED_PARAMETER(enum_callback); - UNUSED_PARAMETER(param); -} diff --git a/rust/obs-c-oracle/oracle/source_enum_active.c b/rust/obs-c-oracle/oracle/source_enum_active.c new file mode 100644 index 0000000000..093edeba36 --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_enum_active.c @@ -0,0 +1,229 @@ +/* Tier 3: the unchanged extracted obs_source_enum_active_sources() and + * obs_source_enum_active_tree(), renamed oracle_*, and the production field + * adapters the Rust shims go through (obs-source-enum-active-fields.c.h, + * defined once, here), over the real private layout. + * + * Both sides reach the same recording stand-ins for obs_source_get_ref(), + * obs_source_release() and obs_transition_enum_sources() (the original C + * through macros, the Rust shims through their Calls), and the same scripted + * enum_active_sources callback, which reports a case's children in order. + * The real calls are covered by test_source_enum_active. */ +#define _NO_CRT_STDIO_INLINE +#include +#include +#include +#define obs_source_enum_active_sources oracle_obs_source_enum_active_sources +#define obs_source_enum_active_tree oracle_obs_source_enum_active_tree +#define obs_source_get_ref oracle_enum_active_get_ref +#define obs_source_release oracle_enum_active_release +#define obs_transition_enum_sources oracle_enum_active_transition_enum +#include "libobs/build/libobs.pch.h" +#include "source-enum-active.inc" +#undef obs_transition_enum_sources +#undef obs_source_release +#undef obs_source_get_ref +#undef obs_source_enum_active_tree +#undef obs_source_enum_active_sources +#include "source_enum_active_fields.inc" + +#define ENUM_ACTIVE_NODES 5 +#define ENUM_ACTIVE_CHILDREN 3 +#define ENUM_ACTIVE_EVENTS 64 +#define ENUM_ACTIVE_NULL 0xfe +#define ENUM_ACTIVE_OTHER 0xff + +/* Node flags. */ +#define ENUM_ACTIVE_TRANSITION 1 +#define ENUM_ACTIVE_CALLBACK 2 +#define ENUM_ACTIVE_DATA 4 +#define ENUM_ACTIVE_DYING 8 + +/* One call: obs_source_enum_active_sources (which 0) or _tree (1) on node + * `root` (-1 for NULL). Each node has flags, up to three active children and + * two transition slots (-1 for none). A child or slot at or below its node's + * index is skipped, so every walk ends. */ +struct enum_active_case { + uint8_t which; + int8_t root; + uint8_t noise; + uint8_t flags[ENUM_ACTIVE_NODES]; + int8_t children[ENUM_ACTIVE_NODES][ENUM_ACTIVE_CHILDREN]; + int8_t slots[ENUM_ACTIVE_NODES][2]; +}; + +/* What one side did: its events, three words each (1 enum_active_sources: + * node, whether the callback is the caller's; 2 get_ref: node; 3 release: + * node; 4 transition enum: node, whether the callback is the caller's; 5 the + * caller's callback: parent, child), its log lines and whether every byte of + * the nodes is as before. */ +struct enum_active_result { + uint32_t events; + uint64_t trace[3 * ENUM_ACTIVE_EVENTS]; + uint32_t log_count; + int32_t log_level; + char log[160]; + bool untouched; +}; + +struct enum_active_fixture { + struct obs_source nodes[ENUM_ACTIVE_NODES]; + uint8_t script[ENUM_ACTIVE_NODES]; + const struct enum_active_case *c; + struct enum_active_result *out; +}; + +static _Thread_local struct enum_active_fixture *enum_active_current; +static int enum_active_user_param; + +static uint64_t enum_active_index(const void *source) +{ + struct enum_active_fixture *f = enum_active_current; + if (!source) + return ENUM_ACTIVE_NULL; + for (size_t i = 0; f && i < ENUM_ACTIVE_NODES; i++) { + if (source == &f->nodes[i]) + return i; + } + return ENUM_ACTIVE_OTHER; +} + +static void enum_active_record(uint64_t kind, uint64_t a, uint64_t b) +{ + struct enum_active_result *out = enum_active_current->out; + if (out->events < ENUM_ACTIVE_EVENTS) { + out->trace[3 * out->events] = kind; + out->trace[3 * out->events + 1] = a; + out->trace[3 * out->events + 2] = b; + } + out->events++; +} + +static void enum_active_user(obs_source_t *parent, obs_source_t *child, void *param) +{ + enum_active_record(5, enum_active_index(parent), + enum_active_index(child) | (param == &enum_active_user_param ? 0 : 0x100)); +} + +static uint64_t enum_active_is_user(obs_source_enum_proc_t cb, void *param) +{ + return (cb == enum_active_user ? 1 : 0) | (param == &enum_active_user_param ? 2 : 0); +} + +static void enum_active_plugin(void *data, obs_source_enum_proc_t cb, void *param) +{ + struct enum_active_fixture *f = enum_active_current; + size_t i = (size_t)((uint8_t *)data - f->script); + enum_active_record(1, i, enum_active_is_user(cb, param)); + for (size_t k = 0; k < ENUM_ACTIVE_CHILDREN; k++) { + int8_t child = f->c->children[i][k]; + if (child > (int8_t)i && child < ENUM_ACTIVE_NODES) + cb(&f->nodes[i], &f->nodes[child], param); + } +} + +obs_source_t *oracle_enum_active_get_ref(obs_source_t *source); +obs_source_t *oracle_enum_active_get_ref(obs_source_t *source) +{ + uint64_t i = enum_active_index(source); + enum_active_record(2, i, 0); + if (i < ENUM_ACTIVE_NODES && (enum_active_current->c->flags[i] & ENUM_ACTIVE_DYING)) + return NULL; + return source; +} + +void oracle_enum_active_release(obs_source_t *source); +void oracle_enum_active_release(obs_source_t *source) +{ + enum_active_record(3, enum_active_index(source), 0); +} + +void oracle_enum_active_transition_enum(obs_source_t *source, obs_source_enum_proc_t cb, void *param); +void oracle_enum_active_transition_enum(obs_source_t *source, obs_source_enum_proc_t cb, void *param) +{ + struct enum_active_fixture *f = enum_active_current; + uint64_t i = enum_active_index(source); + enum_active_record(4, i, enum_active_is_user(cb, param)); + if (i >= ENUM_ACTIVE_NODES) + return; + for (size_t k = 0; k < 2; k++) { + int8_t slot = f->c->slots[i][k]; + if (slot > (int8_t)i && slot < ENUM_ACTIVE_NODES) + cb(source, &f->nodes[slot], param); + } +} + +#if defined(__GNUC__) || defined(__clang__) +static void enum_active_capture(int level, const char *format, va_list args, void *param) + __attribute__((format(printf, 2, 0))); +#endif +static void enum_active_capture(int level, const char *format, va_list args, void *param) +{ + struct enum_active_result *out = param; + out->log_count++; + out->log_level = level; + vsnprintf(out->log, sizeof(out->log), format, args); +} + +typedef void (*enum_active_fn)(obs_source_t *, obs_source_enum_proc_t, void *); +typedef void (*enum_active_get_log)(log_handler_t *, void **); +typedef void (*enum_active_set_log)(log_handler_t, void *); + +/* The original calls, for the parity test's other side. */ +void oracle_source_enum_active_originals(enum_active_fn *sources, enum_active_fn *tree); +void oracle_source_enum_active_originals(enum_active_fn *sources, enum_active_fn *tree) +{ + *sources = oracle_obs_source_enum_active_sources; + *tree = oracle_obs_source_enum_active_tree; +} + +/* Runs one case through either call on fresh noise-filled nodes. False when + * the fixture could not be built or an adapter does not reach its field. */ +bool oracle_source_enum_active_run(enum_active_fn sources, enum_active_fn tree, enum_active_get_log get, + enum_active_set_log set, const struct enum_active_case *c, + struct enum_active_result *out); +bool oracle_source_enum_active_run(enum_active_fn sources, enum_active_fn tree, enum_active_get_log get, + enum_active_set_log set, const struct enum_active_case *c, + struct enum_active_result *out) +{ + struct enum_active_fixture *f = malloc(sizeof(*f)); + struct obs_source *before = malloc(sizeof(f->nodes)); + if (!f || !before) { + free(f); + free(before); + return false; + } + memset(f, c->noise, sizeof(*f)); + memset(out, 0, sizeof(*out)); + out->log_level = -1; + f->c = c; + f->out = out; + bool valid = true; + for (size_t i = 0; i < ENUM_ACTIVE_NODES; i++) { + uint8_t flags = c->flags[i]; + f->nodes[i].context.data = (flags & ENUM_ACTIVE_DATA) ? &f->script[i] : NULL; + f->nodes[i].info.type = (flags & ENUM_ACTIVE_TRANSITION) + ? OBS_SOURCE_TYPE_TRANSITION + : (enum obs_source_type)(c->noise % 3 == 0 ? OBS_SOURCE_TYPE_INPUT + : OBS_SOURCE_TYPE_FILTER); + f->nodes[i].info.enum_active_sources = (flags & ENUM_ACTIVE_CALLBACK) ? enum_active_plugin : NULL; + valid = valid && obs_source_enum_active_has_data(&f->nodes[i]) == ((flags & ENUM_ACTIVE_DATA) != 0) && + obs_source_enum_active_is_transition(&f->nodes[i]) == ((flags & ENUM_ACTIVE_TRANSITION) != 0) && + obs_source_enum_active_has_callback(&f->nodes[i]) == ((flags & ENUM_ACTIVE_CALLBACK) != 0); + } + memcpy(before, f->nodes, sizeof(f->nodes)); + + log_handler_t old_handler; + void *old_param; + get(&old_handler, &old_param); + enum_active_current = f; + set(enum_active_capture, out); + obs_source_t *root = c->root < 0 || c->root >= ENUM_ACTIVE_NODES ? NULL : &f->nodes[c->root]; + (c->which == 0 ? sources : tree)(root, enum_active_user, &enum_active_user_param); + set(old_handler, old_param); + enum_active_current = NULL; + + out->untouched = memcmp(before, f->nodes, sizeof(f->nodes)) == 0; + free(before); + free(f); + return valid; +} diff --git a/rust/obs-source/src/enum_active.rs b/rust/obs-source/src/enum_active.rs index 1f1a2aae5c..4905f9c55d 100644 --- a/rust/obs-source/src/enum_active.rs +++ b/rust/obs-source/src/enum_active.rs @@ -63,8 +63,38 @@ pub fn enumerate( source: Option, sink: L::Sink, ) { - let _ = (libobs, function, source, sink); - todo!("#5729") + let Some(source) = valid(libobs, function, source) else { + return; + }; + let is_transition = libobs.is_transition(source); + if !is_transition && !libobs.has_enum_active(source) { + return; + } + let held = libobs.get_ref(source); + let Some(source) = valid(libobs, function, held) else { + return; + }; + if is_transition { + libobs.transition_enum(source, sink); + } + if libobs.has_enum_active(source) { + libobs.enum_active(source, sink); + } + libobs.release(source); +} + +/// `data_valid()`: a NULL source is logged; one without context data is +/// refused silently. +fn valid( + libobs: &mut L, + function: &'static CStr, + source: Option, +) -> Option { + let Some(source) = source else { + libobs.log_null(function); + return None; + }; + libobs.has_data(source).then_some(source) } /// `enum_source_active_tree_callback()` up to the caller's callback: a @@ -72,6 +102,10 @@ pub fn enumerate( /// and data reports its own children, both through the tree walk's `sink`. /// The caller then reports the edge itself. pub fn tree_edge(libobs: &mut L, child: L::Source, sink: L::Sink) { - let _ = (libobs, child, sink); - todo!("#5729") + if libobs.is_transition(child) { + libobs.transition_enum(child, sink); + } + if libobs.has_enum_active(child) && libobs.has_data(child) { + libobs.enum_active(child, sink); + } } diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index 6d626a7d93..87e15ce4a7 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -38,6 +38,7 @@ pub mod source_draw_plan; pub mod source_draw_technique; pub mod source_duplicate; pub mod source_enabled_setter; +pub mod source_enum_active; pub mod source_enum_filters; pub mod source_filter_backup; pub mod source_filter_chain; diff --git a/rust/obs-source/src/ffi/source_enum_active.rs b/rust/obs-source/src/ffi/source_enum_active.rs new file mode 100644 index 0000000000..6e3d361dfe --- /dev/null +++ b/rust/obs-source/src/ffi/source_enum_active.rs @@ -0,0 +1,247 @@ +//! C ABI of `obs_source_enum_active_sources()` and +//! `obs_source_enum_active_tree()`. The source fields and the plugin's +//! `enum_active_sources` call go through +//! `obs-source-enum-active-fields.c.h` (no `EXPORT`); +//! `obs_transition_enum_sources()`, `obs_source_get_ref()` and +//! `obs_source_release()` are called directly. +use crate::enum_active; +use core::ffi::{CStr, c_char, c_int, c_void}; +use core::ptr::NonNull; + +/// `obs_source_enum_proc_t`. +pub type EnumProc = + unsafe extern "C" fn(parent: *mut c_void, child: *mut c_void, param: *mut c_void); + +unsafe extern "C" { + fn obs_source_enum_active_has_data(source: *const c_void) -> bool; + fn obs_source_enum_active_is_transition(source: *const c_void) -> bool; + fn obs_source_enum_active_has_callback(source: *const c_void) -> bool; + fn obs_source_enum_active_call( + source: *mut c_void, + enum_callback: EnumProc, + param: *mut c_void, + ); + fn obs_transition_enum_sources( + source: *mut c_void, + enum_callback: EnumProc, + param: *mut c_void, + ); + fn obs_source_get_ref(source: *mut c_void) -> *mut c_void; + fn obs_source_release(source: *mut c_void); + fn blog(level: c_int, format: *const c_char, ...); +} + +/// `LOG_DEBUG`, the level of `obs_ptr_valid()`'s NULL line. +const LOG_DEBUG: c_int = 400; + +/// A callback with its parameter, as handed to the C. +#[derive(Debug, Clone, Copy)] +pub struct Sink { + /// The callback. + pub callback: EnumProc, + /// Its parameter. + pub param: *mut c_void, +} + +/// The tree walk's parameter (`struct source_enum_data`): the caller's +/// callback and parameter. +#[repr(C)] +struct TreeData { + enum_callback: EnumProc, + param: *mut c_void, +} + +/// The fields and the plugin call, through the production adapters; the +/// rest of [`enum_active::Libobs`] is provided by [`Calls`]. +pub trait Calls: Default { + /// `obs_transition_enum_sources()`. + /// + /// # Safety + /// A live source and a callback that takes `param`. + unsafe fn transition_enum(source: *mut c_void, callback: EnumProc, param: *mut c_void); + /// `obs_source_get_ref()`. + /// + /// # Safety + /// A live source. + unsafe fn get_ref(source: *mut c_void) -> *mut c_void; + /// `obs_source_release()`. + /// + /// # Safety + /// A reference taken by [`Calls::get_ref`]. + unsafe fn release(source: *mut c_void); +} + +/// What the shims reach: libobs's C. Each source is non-NULL and, under the +/// libobs contract, live for the call. +#[derive(Debug, Default, Clone, Copy)] +pub struct Libobs; + +impl Calls for Libobs { + unsafe fn transition_enum(source: *mut c_void, callback: EnumProc, param: *mut c_void) { + // SAFETY: the caller's contract. + unsafe { obs_transition_enum_sources(source, callback, param) } + } + + unsafe fn get_ref(source: *mut c_void) -> *mut c_void { + // SAFETY: the caller's contract. + unsafe { obs_source_get_ref(source) } + } + + unsafe fn release(source: *mut c_void) { + // SAFETY: the caller's contract. + unsafe { obs_source_release(source) } + } +} + +/// [`enum_active::Libobs`] over the production field adapters and `C`'s +/// calls. +#[derive(Debug, Default, Clone, Copy)] +pub struct With(pub C); + +impl enum_active::Libobs for With { + type Source = NonNull; + type Sink = Sink; + + fn has_data(&mut self, source: Self::Source) -> bool { + // SAFETY: a live source; the adapter reads one field. + unsafe { obs_source_enum_active_has_data(source.as_ptr()) } + } + + fn is_transition(&mut self, source: Self::Source) -> bool { + // SAFETY: as `has_data`. + unsafe { obs_source_enum_active_is_transition(source.as_ptr()) } + } + + fn has_enum_active(&mut self, source: Self::Source) -> bool { + // SAFETY: as `has_data`. + unsafe { obs_source_enum_active_has_callback(source.as_ptr()) } + } + + fn get_ref(&mut self, source: Self::Source) -> Option { + // SAFETY: a live source. + NonNull::new(unsafe { C::get_ref(source.as_ptr()) }) + } + + fn release(&mut self, source: Self::Source) { + // SAFETY: the reference `get_ref` took. + unsafe { C::release(source.as_ptr()) } + } + + fn transition_enum(&mut self, source: Self::Source, sink: Sink) { + // SAFETY: a live transition; the sink's callback takes its param. + unsafe { C::transition_enum(source.as_ptr(), sink.callback, sink.param) } + } + + fn enum_active(&mut self, source: Self::Source, sink: Sink) { + // SAFETY: a live source with the callback and data (the core + // checked both); the sink's callback takes its param. + unsafe { obs_source_enum_active_call(source.as_ptr(), sink.callback, sink.param) } + } + + fn log_null(&mut self, function: &'static CStr) { + // SAFETY: static strings match the two %s arguments of the format. + unsafe { + blog( + LOG_DEBUG, + c"%s: Null '%s' parameter".as_ptr(), + function.as_ptr(), + c"source".as_ptr(), + ); + } + } +} + +/// `enum_source_active_tree_callback()`. +/// +/// # Safety +/// `param` is the [`TreeData`] of a running tree walk; `child` is live. +unsafe extern "C" fn tree_callback( + parent: *mut c_void, + child: *mut c_void, + param: *mut c_void, +) { + let sink = Sink { + callback: tree_callback::, + param, + }; + if let Some(child_source) = NonNull::new(child) { + enum_active::tree_edge(&mut With(C::default()), child_source, sink); + } + // SAFETY: `param` is the walk's `TreeData`, alive until it returns. + let data = unsafe { &*param.cast::() }; + // SAFETY: the caller's callback with its own parameter. + unsafe { (data.enum_callback)(parent, child, data.param) } +} + +/// `obs_source_enum_active_sources()` over `C`'s calls (the export uses +/// [`Libobs`]; the parity test passes stand-ins). +/// +/// # Safety +/// `source` is NULL or live; `enum_callback` takes `param`. +pub unsafe fn enum_active_sources_with( + source: *mut c_void, + enum_callback: EnumProc, + param: *mut c_void, +) { + let sink = Sink { + callback: enum_callback, + param, + }; + enum_active::enumerate( + &mut With(C::default()), + enum_active::SOURCES_FUNCTION, + NonNull::new(source), + sink, + ); +} + +/// `obs_source_enum_active_tree()` over `C`'s calls, as +/// [`enum_active_sources_with`]. +/// +/// # Safety +/// As [`enum_active_sources_with`]. +pub unsafe fn enum_active_tree_with( + source: *mut c_void, + enum_callback: EnumProc, + param: *mut c_void, +) { + let mut data = TreeData { + enum_callback, + param, + }; + let sink = Sink { + callback: tree_callback::, + param: (&raw mut data).cast(), + }; + enum_active::enumerate( + &mut With(C::default()), + enum_active::TREE_FUNCTION, + NonNull::new(source), + sink, + ); +} + +/// # Safety +/// `source` is NULL or live under the original libobs ownership contract; +/// `enum_callback` takes `param`. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_enum_active_sources( + source: *mut c_void, + enum_callback: EnumProc, + param: *mut c_void, +) { + // SAFETY: the caller's contract. + unsafe { enum_active_sources_with::(source, enum_callback, param) } +} + +/// # Safety +/// As [`obs_source_enum_active_sources`]. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_source_enum_active_tree( + source: *mut c_void, + enum_callback: EnumProc, + param: *mut c_void, +) { + // SAFETY: the caller's contract. + unsafe { enum_active_tree_with::(source, enum_callback, param) } +} diff --git a/rust/obs-source/tests/source/enum_active_parity.rs b/rust/obs-source/tests/source/enum_active_parity.rs new file mode 100644 index 0000000000..9f4b149267 --- /dev/null +++ b/rust/obs-source/tests/source/enum_active_parity.rs @@ -0,0 +1,411 @@ +//! Tier 3: the original C `obs_source_enum_active_sources()` and +//! `obs_source_enum_active_tree()` and the Rust shims, on noise-filled +//! real-header sources, each reaching the same recording stand-ins +//! (oracle/source_enum_active.c) for `obs_source_get_ref()`, +//! `obs_source_release()` and `obs_transition_enum_sources()`, the +//! production field adapters and a scripted `enum_active_sources`. +//! Compares every plugin call (with whether it got the caller's callback +//! and parameter), reference, transition enum and reported edge in order, +//! the log lines and every byte of the sources. The exported symbols are +//! the same shims over libobs's own calls. No intentional differences; +//! a NULL child reported to the tree walk (the C dereferences it) is left +//! out. +use core::ffi::{CStr, c_char, c_void}; +use obs_source::ffi::source_enum_active::{ + Calls, EnumProc, enum_active_sources_with, enum_active_tree_with, +}; +use obs_util::ffi::base::{base_get_log_handler, base_set_log_handler}; +use proptest::prelude::*; + +const NODES: usize = 5; +const CHILDREN: usize = 3; +const EVENTS: usize = 64; + +const TRANSITION: u8 = 1; +const CALLBACK: u8 = 2; +const DATA: u8 = 4; +const DYING: u8 = 8; + +#[repr(C)] +#[derive(Clone, Copy, Debug, Default)] +struct Case { + which: u8, + root: i8, + noise: u8, + flags: [u8; NODES], + children: [[i8; CHILDREN]; NODES], + slots: [[i8; 2]; NODES], +} + +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Outcome { + events: u32, + trace: [u64; 3 * EVENTS], + log_count: u32, + log_level: i32, + log: [c_char; 160], + untouched: bool, +} + +impl Default for Outcome { + fn default() -> Self { + Self { + events: 0, + trace: [0; 3 * EVENTS], + log_count: 0, + log_level: 0, + log: [0; 160], + untouched: false, + } + } +} + +impl Outcome { + fn events(&self) -> Vec<[u64; 3]> { + self.trace + .chunks(3) + .take(usize::try_from(self.events).unwrap().min(EVENTS)) + .map(|e| [e[0], e[1], e[2]]) + .collect() + } + + fn log(&self) -> &str { + // SAFETY: the oracle wrote a NUL-terminated string (or left zeros). + unsafe { CStr::from_ptr(self.log.as_ptr()) } + .to_str() + .unwrap() + } +} + +type Enum = unsafe extern "C" fn(*mut c_void, EnumProc, *mut c_void); +type GetLog = unsafe extern "C" fn(*mut *mut c_void, *mut *mut c_void); +type SetLog = unsafe extern "C" fn(*mut c_void, *mut c_void); + +unsafe extern "C" { + fn oracle_source_enum_active_originals(sources: *mut Option, tree: *mut Option); + fn oracle_source_enum_active_run( + sources: Enum, + tree: Enum, + get: GetLog, + set: SetLog, + case: *const Case, + out: *mut Outcome, + ) -> bool; + fn oracle_enum_active_get_ref(source: *mut c_void) -> *mut c_void; + fn oracle_enum_active_release(source: *mut c_void); + fn oracle_enum_active_transition_enum(source: *mut c_void, cb: EnumProc, param: *mut c_void); +} + +/// The oracle's stand-ins, as the Rust shims' calls. +#[derive(Default)] +struct Stand; + +impl Calls for Stand { + unsafe fn transition_enum(source: *mut c_void, callback: EnumProc, param: *mut c_void) { + // SAFETY: the shim's contract, forwarded to the recording stand-in. + unsafe { oracle_enum_active_transition_enum(source, callback, param) } + } + + unsafe fn get_ref(source: *mut c_void) -> *mut c_void { + // SAFETY: as above. + unsafe { oracle_enum_active_get_ref(source) } + } + + unsafe fn release(source: *mut c_void) { + // SAFETY: as above. + unsafe { oracle_enum_active_release(source) } + } +} + +/// A stand-in that never reports a transition's slots (mutant). +#[derive(Default)] +struct NoSlots; + +impl Calls for NoSlots { + unsafe fn transition_enum(source: *mut c_void, callback: EnumProc, param: *mut c_void) { + let _ = (source, callback, param); + } + + unsafe fn get_ref(source: *mut c_void) -> *mut c_void { + // SAFETY: as `Stand`. + unsafe { oracle_enum_active_get_ref(source) } + } + + unsafe fn release(source: *mut c_void) { + // SAFETY: as `Stand`. + unsafe { oracle_enum_active_release(source) } + } +} + +/// A stand-in that never releases (mutant). +#[derive(Default)] +struct Leaks; + +impl Calls for Leaks { + unsafe fn transition_enum(source: *mut c_void, callback: EnumProc, param: *mut c_void) { + // SAFETY: as `Stand`. + unsafe { oracle_enum_active_transition_enum(source, callback, param) } + } + + unsafe fn get_ref(source: *mut c_void) -> *mut c_void { + // SAFETY: as `Stand`. + unsafe { oracle_enum_active_get_ref(source) } + } + + unsafe fn release(source: *mut c_void) { + let _ = source; + } +} + +macro_rules! shims { + ($sources:ident, $tree:ident, $calls:ty) => { + unsafe extern "C" fn $sources(source: *mut c_void, cb: EnumProc, param: *mut c_void) { + // SAFETY: the oracle's fixture keeps the sources alive. + unsafe { enum_active_sources_with::<$calls>(source, cb, param) } + } + unsafe extern "C" fn $tree(source: *mut c_void, cb: EnumProc, param: *mut c_void) { + // SAFETY: as above. + unsafe { enum_active_tree_with::<$calls>(source, cb, param) } + } + }; +} + +shims!(rust_sources, rust_tree, Stand); +shims!(no_slots_sources, no_slots_tree, NoSlots); +shims!(leaks_sources, leaks_tree, Leaks); + +/// Mutant: the tree walk reports the edge before the child's subtree. +unsafe extern "C" fn pre_order_tree(source: *mut c_void, cb: EnumProc, param: *mut c_void) { + #[repr(C)] + struct Data { + cb: EnumProc, + param: *mut c_void, + } + unsafe extern "C" fn walk(parent: *mut c_void, child: *mut c_void, param: *mut c_void) { + // SAFETY: `param` is the `Data` below, alive for the walk. + let data = unsafe { &*param.cast::() }; + // SAFETY: the caller's callback with its parameter. + unsafe { (data.cb)(parent, child, data.param) }; + // SAFETY: a live child under the oracle's fixture. + unsafe { enum_active_sources_with::(child, walk, param) }; + } + let mut data = Data { cb, param }; + // SAFETY: as `rust_sources`. + unsafe { enum_active_sources_with::(source, walk, (&raw mut data).cast()) } +} + +/// Mutant: the direct call walks the tree. +unsafe extern "C" fn sources_as_tree(source: *mut c_void, cb: EnumProc, param: *mut c_void) { + // SAFETY: as `rust_sources`. + unsafe { enum_active_tree_with::(source, cb, param) } +} + +fn originals() -> (Enum, Enum) { + let mut sources = None; + let mut tree = None; + // SAFETY: two writable slots for function pointers. + unsafe { oracle_source_enum_active_originals(&raw mut sources, &raw mut tree) }; + (sources.unwrap(), tree.unwrap()) +} + +fn run(sources: Enum, tree: Enum, case: &Case) -> Outcome { + let mut out = Outcome::default(); + // SAFETY: the isolated child owns the log handler; the case and the + // outcome are valid for the call. + let valid = unsafe { + oracle_source_enum_active_run( + sources, + tree, + base_get_log_handler, + base_set_log_handler, + case, + &mut out, + ) + }; + assert!(valid, "the oracle fixture or an adapter failed: {case:?}"); + out +} + +fn check_with(sources: Enum, tree: Enum, case: &Case) -> Option { + let (original_sources, original_tree) = originals(); + let original = run(original_sources, original_tree, case); + let ported = run(sources, tree, case); + (ported == original).then_some(original) +} + +fn check(case: &Case) -> Outcome { + check_with(rust_sources, rust_tree, case) + .unwrap_or_else(|| panic!("the Rust shims disagree with the original C: {case:?}")) +} + +/// Every node a full, data-holding node without children or slots. +fn base(which: u8, root: i8) -> Case { + Case { + which, + root, + flags: [CALLBACK | DATA; NODES], + children: [[-1; CHILDREN]; NODES], + slots: [[-1; 2]; NODES], + ..Case::default() + } +} + +/// Node 0 lists 1 and 2, node 1 lists 3 and 4; node 2 is a transition with +/// slots 3 and 4. +fn shaped(which: u8, root: i8, flags: [u8; NODES]) -> Case { + let mut c = base(which, root); + c.flags = flags; + c.children[0] = [1, 2, -1]; + c.children[1] = [3, 4, -1]; + c.children[2] = [4, -1, -1]; + c.slots[2] = [3, 4]; + c +} + +const FLAG_SETS: [u8; 6] = [ + CALLBACK | DATA, + DATA, + CALLBACK, + TRANSITION | DATA, + TRANSITION | CALLBACK | DATA, + CALLBACK | DATA | DYING, +]; + +fn grid() -> impl Iterator { + let mut cases = Vec::new(); + for which in 0..2u8 { + for root in -1i8..3 { + for &root_flags in &FLAG_SETS { + for &inner in &FLAG_SETS { + let mut flags = [CALLBACK | DATA; NODES]; + if root >= 0 { + flags[usize::try_from(root).unwrap()] = root_flags; + } + flags[2] = if root == 2 { root_flags } else { inner }; + flags[1] = if root == 1 { root_flags } else { inner }; + cases.push(shaped(which, root, flags)); + } + } + } + } + cases.into_iter() +} + +#[test] +fn enum_active_matches_c() { + if !crate::output::isolated(concat!(module_path!(), "::enum_active_matches_c")) { + return; + } + pinned(); + grid_and_mutants(); + random_cases(); +} + +fn pinned() { + // Event 1 is a plugin call (node, 3 when it got the caller's callback + // and parameter), 2 get_ref, 3 release, 4 a transition enum, 5 an edge + // reported to the caller (parent, child). + let direct = check(&shaped(0, 0, [CALLBACK | DATA; NODES])); + assert!(direct.untouched && direct.log_count == 0); + assert_eq!( + direct.events(), + [[2, 0, 0], [1, 0, 3], [5, 0, 1], [5, 0, 2], [3, 0, 0]] + ); + let tree = check(&shaped(1, 0, [CALLBACK | DATA; NODES])); + assert_eq!( + tree.events(), + [ + [2, 0, 0], + [1, 0, 0], + [1, 1, 0], + [1, 3, 0], + [5, 1, 3], + [1, 4, 0], + [5, 1, 4], + [5, 0, 1], + [1, 2, 0], + [1, 4, 0], + [5, 2, 4], + [5, 0, 2], + [3, 0, 0], + ] + ); + let mut flags = [CALLBACK | DATA; NODES]; + flags[2] = TRANSITION | DATA; + let transition = check(&shaped(1, 0, flags)); + assert!(transition.events().contains(&[4, 2, 0])); + for (which, function) in [ + (0, "obs_source_enum_active_sources"), + (1, "obs_source_enum_active_tree"), + ] { + let null = check(&base(which, -1)); + assert!(null.events().is_empty()); + assert_eq!((null.log_count, null.log_level), (1, 400)); + assert_eq!(null.log(), format!("{function}: Null 'source' parameter")); + let mut dying = base(which, 0); + dying.flags[0] |= DYING; + let dying = check(&dying); + assert_eq!(dying.events(), [[2, 0, 0]]); + assert_eq!(dying.log(), format!("{function}: Null 'source' parameter")); + let mut bare = base(which, 0); + bare.flags[0] = DATA; + assert!(check(&bare).events().is_empty()); + bare.flags[0] = CALLBACK; + assert!(check(&bare).events().is_empty()); + } +} + +fn grid_and_mutants() { + let mut count = 0usize; + for c in grid() { + let _ = check(&c); + count += 1; + } + assert_eq!(count, 2 * 4 * FLAG_SETS.len() * FLAG_SETS.len()); + + let mutants: [(&str, Enum, Enum); 4] = [ + ("transition slots dropped", no_slots_sources, no_slots_tree), + ("never released", leaks_sources, leaks_tree), + ("pre-order tree", rust_sources, pre_order_tree), + ("direct call walks the tree", sources_as_tree, rust_tree), + ]; + for (name, sources, tree) in mutants { + assert!( + grid().any(|c| check_with(sources, tree, &c).is_none()), + "the mutant '{name}' agreed with the original C" + ); + } +} + +fn random_cases() { + let mut runner = proptest::test_runner::TestRunner::new(ProptestConfig { + cases: obs_c_oracle::parity::cases(512), + failure_persistence: None, + ..ProptestConfig::default() + }); + runner + .run( + &( + (0u8..2, -1i8..5, any::()), + [0u8..16, 0u8..16, 0u8..16, 0u8..16, 0u8..16], + proptest::collection::vec(-1i8..5, NODES * CHILDREN), + proptest::collection::vec(-1i8..5, NODES * 2), + ), + |((which, root, noise), flags, children, slots)| { + let mut c = base(which, root); + c.noise = noise; + c.flags = flags; + for (i, row) in c.children.iter_mut().enumerate() { + row.copy_from_slice(&children[i * CHILDREN..(i + 1) * CHILDREN]); + } + for (i, row) in c.slots.iter_mut().enumerate() { + row.copy_from_slice(&slots[i * 2..(i + 1) * 2]); + } + let out = check(&c); + prop_assert!(out.untouched); + Ok(()) + }, + ) + .unwrap(); +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 1122aae18c..e99467f1ae 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -73,6 +73,7 @@ mod draw_plan_parity; mod enabled_setter; mod enabled_setter_parity; mod enum_active; +mod enum_active_parity; mod enum_filters; mod enum_filters_parity; mod filter_backup;