diff --git a/libobs/build/libobs.core+.h.c b/libobs/build/libobs.core+.h.c index f6461dd683..f0b6aed5ba 100644 --- a/libobs/build/libobs.core+.h.c +++ b/libobs/build/libobs.core+.h.c @@ -13,6 +13,7 @@ #include "libobs/obs-core-state.c.h" #include "libobs/obs-core-texture-activity-fields.c.h" #include "libobs/obs-core-tick-callbacks-fields.c.h" +#include "libobs/obs-encoder-video-tex-active-mix.c.h" #include "libobs/obs-enum-scenes-fields.c.h" #include "libobs/obs-get-audio-fields.c.h" #include "libobs/obs-get-main-canvas-fields.c.h" diff --git a/libobs/build/libobs.core-output+.h.c b/libobs/build/libobs.core-output+.h.c index ad2821422e..3efdd9c0f7 100644 --- a/libobs/build/libobs.core-output+.h.c +++ b/libobs/build/libobs.core-output+.h.c @@ -37,7 +37,7 @@ #include "libobs/obs-encoder-settings-fields.c.h" #include "libobs/obs-encoder-state-queries-fields.c.h" #include "libobs/obs-encoder-type-info-fields.c.h" -#include "libobs/obs-encoder-video-tex-active.c.h" +#include "libobs/obs-encoder-video-tex-active-fields.c.h" #include "libobs/obs-encoder.c.h" #include "libobs/obs-output-active-fields.c.h" #include "libobs/obs-output-can-pause-fields.c.h" diff --git a/libobs/build/libobs.ported-encoder-video-tex-active+.h.c b/libobs/build/libobs.ported-encoder-video-tex-active+.h.c new file mode 100644 index 0000000000..dca1e46d00 --- /dev/null +++ b/libobs/build/libobs.ported-encoder-video-tex-active+.h.c @@ -0,0 +1,4 @@ +/* Unity group (BUILD.md): original texture-input activity query, reference only. */ +#include "libobs/build/libobs.pch.h" + +#include "libobs/obs-encoder-video-tex-active.c.h" diff --git a/libobs/build/ported-source-sets/encoder-video-tex-active.json b/libobs/build/ported-source-sets/encoder-video-tex-active.json new file mode 100644 index 0000000000..bdeb43ce39 --- /dev/null +++ b/libobs/build/ported-source-sets/encoder-video-tex-active.json @@ -0,0 +1,5 @@ +{ + "LIBOBS_PORTED_SOURCES": [ + "build/libobs.ported-encoder-video-tex-active+.h.c" + ] +} diff --git a/libobs/cmake/rust-exports.d/obs-encoder-video_tex_active.txt b/libobs/cmake/rust-exports.d/obs-encoder-video_tex_active.txt new file mode 100644 index 0000000000..7cac7386ec --- /dev/null +++ b/libobs/cmake/rust-exports.d/obs-encoder-video_tex_active.txt @@ -0,0 +1 @@ +obs_encoder_video_tex_active diff --git a/libobs/cmake/rust-hidden.d/obs-encoder-video_tex_active.txt b/libobs/cmake/rust-hidden.d/obs-encoder-video_tex_active.txt new file mode 100644 index 0000000000..0bbed21edf --- /dev/null +++ b/libobs/cmake/rust-hidden.d/obs-encoder-video_tex_active.txt @@ -0,0 +1 @@ +obs_encoder_video_tex_active_rust diff --git a/libobs/obs-encoder-video-tex-active-fields.c.h b/libobs/obs-encoder-video-tex-active-fields.c.h new file mode 100644 index 0000000000..1bbf06cbac --- /dev/null +++ b/libobs/obs-encoder-video-tex-active-fields.c.h @@ -0,0 +1,20 @@ +#include "libobs/build/libobs.pch.h" + +const struct obs_core_video_mix *obs_encoder_video_tex_active_mix(const obs_encoder_t *encoder); +bool obs_encoder_video_tex_active_nv12(const struct obs_core_video_mix *mix); +bool obs_encoder_video_tex_active_p010(const struct obs_core_video_mix *mix); + +const struct obs_core_video_mix *obs_encoder_video_tex_active_mix(const obs_encoder_t *encoder) +{ + return get_mix_for_video(encoder->media); +} + +bool obs_encoder_video_tex_active_nv12(const struct obs_core_video_mix *mix) +{ + return mix->using_nv12_tex; +} + +bool obs_encoder_video_tex_active_p010(const struct obs_core_video_mix *mix) +{ + return mix->using_p010_tex; +} diff --git a/libobs/obs-encoder-video-tex-active-mix.c.h b/libobs/obs-encoder-video-tex-active-mix.c.h new file mode 100644 index 0000000000..06083265ea --- /dev/null +++ b/libobs/obs-encoder-video-tex-active-mix.c.h @@ -0,0 +1,19 @@ +#include "libobs/build/libobs.pch.h" + +struct obs_core_video_mix *get_mix_for_video(video_t *v) +{ + struct obs_core_video_mix *result = NULL; + + pthread_mutex_lock(&obs->video.mixes_mutex); + for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) { + struct obs_core_video_mix *mix = obs->video.mixes.array[i]; + + if (v == mix->video) { + result = mix; + break; + } + } + pthread_mutex_unlock(&obs->video.mixes_mutex); + + return result; +} diff --git a/libobs/obs.c.h b/libobs/obs.c.h index adbc67e9ad..2d61887617 100644 --- a/libobs/obs.c.h +++ b/libobs/obs.c.h @@ -2922,24 +2922,6 @@ void obs_get_video_frame_counts(uint32_t *total, uint32_t *skipped) *skipped = skipped_frames; } -struct obs_core_video_mix *get_mix_for_video(video_t *v) -{ - struct obs_core_video_mix *result = NULL; - - pthread_mutex_lock(&obs->video.mixes_mutex); - for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) { - struct obs_core_video_mix *mix = obs->video.mixes.array[i]; - - if (v == mix->video) { - result = mix; - break; - } - } - pthread_mutex_unlock(&obs->video.mixes_mutex); - - return result; -} - void start_raw_video(video_t *v, const struct video_scale_info *conversion, uint32_t frame_rate_divisor, void (*callback)(void *param, struct video_data *frame), void *param) { diff --git a/rust/README.md b/rust/README.md index 48fc13db21..78f75abecf 100644 --- a/rust/README.md +++ b/rust/README.md @@ -279,6 +279,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/obs-encoder-frame-rate-divisor.c.h` (extracted from `obs-encoder.c.h`) | `obs-output::encoder_frame_rate_divisor` | Public frame-rate-divisor getter. Preserves NULL and audio-type defaults, historical setter diagnostics, current-info type selection, full-width divisor and no mutation. Borrowed private fields; real-header layout and callback-based randomized Tier 3 parity; unchanged actual-library Tier 2 on both libraries. No intended differences; Tier 4 inapplicable. | | `libobs/obs-encoder-packet-ownership.c.h` | `obs-encoder::packet_ownership` | Swapped in hybrid `libobs` (group `libobs.ported-encoder-packet-ownership`). `obs_encoder_packet_create_instance` (hidden), `obs_encoder_packet_ref` and `obs_encoder_packet_release` keep the C layout: one bmem block per payload, a native `long` count in front of the data, atomic increments and decrements, the block freed on the last release, then the whole packet zeroed. create copies every field, then allocates `size + sizeof(long)` and copies the payload (an owned source is copied too, never shared); ref of NULL does nothing, and a packet with no data is copied without touching a count. The safe core holds the order behind a host trait; the FFI keeps the pointers. Tier 1 checks the order against a recording host and mirrors every assertion of `test_encoder_packet_ownership` through the exports; Tier 3 runs random create/ref/release/write sequences on four packets per side against the original C, comparing fields, block aliasing, counts, bytes and `bnum_allocs()` after every step. The delay and interleave parity tests now go through these exports instead of their own copies. Excluded as in C: NULL-data or self copies, and refs of buffers with no count. No intentional differences; Tier 4 inapplicable. | | `libobs/obs-encoder-media-getters.c.h` | `obs-encoder::media_getters` | Video, parent-video and audio getters preserve NULL diagnostics, wrong-type warnings, borrowed media identity and FPS override priority, without active/initialized gates or mutation. Real private-layout snapshot adapter, header-derived layout checks, Tier 1 characterization mirrors and original-C randomized differential coverage in obs-tests-core; unchanged public C characterization runs against both libraries. No intentional behavior differences. | +| `libobs/obs-encoder-video-tex-active.c.h` | `obs-encoder::video_tex_active` | Texture-input activity preserves one C mutex-protected mix lookup before the absent-mix guard and only the requested NV12/P010 flag read. Opaque C-owned adapters retain private layouts; safe mirrors, unchanged C characterization, real-walk original-C differential and all Rust-static links cover the swap. No behavior change; Tier 4 inapplicable. | | `libobs/obs-encoder-metadata-getters.c.h` (extracted from `obs-encoder.c.h`) | `obs-output::encoder_metadata` | Five public instance metadata getters: codec/type from current info, ID/type data/caps from original info. No intended differences: NULL defaults and exact diagnostics, borrowed pointer identity, full-width caps, no locks, reference changes or plugin callbacks. Private field-pointer seam avoids mirroring the encoder layout; real-header view/enum layout checks, exact differential parity and unchanged actual-library `test_encoder_metadata_getters` cover both libraries. | | `libobs/obs-encoder-dimensions.c.h` | `obs-output::encoder_dimensions` | Public `obs_encoder_get_width` and `obs_encoder_get_height` swapped in hybrid libobs (group `libobs.ported-encoder-dimensions`). Reuses `encoder_size::video_size_route`; preserves NULL debug logs, wrong-type warnings including the encoder name, media absence before stored axes, independent full-width scaled axes and lazy video fallback. A borrowed field-address adapter keeps the private encoder layout in C. `test_encoder_dimensions` characterizes actual libraries and remains unchanged after its C-only commit. Tier 1 mirrors its assertions; real-header checks cover every view field and encoder type layout. Tier 3 compares both getters, full diagnostics and whole-encoder immutability for edges and randomized fields, using callbacks for media reads and the oracle C size helper. No intentional behavior differences. Tier 4 is inapplicable. | | `libobs/obs-encoder-group-lifecycle.c.h` | `obs-output::encoder_group_lifecycle` | Encoder-group allocation, deferred destruction and ordered locked teardown swapped in hybrid libobs. Native allocation, private fields and mutex primitives stay behind C adapters; the safe core clears each encoder back-pointer before releasing its reference, frees the list, unlocks and destroys the mutex, then frees the group. Tier 1 mirrors the source characterization; real-header view layout and bounded randomized C/Rust operation traces include init failure and the locked stop-path handoff. Actual-library Tier 2 checks zero initialization, deferred requests, back-pointers and strong-reference ownership. No intentional differences; Tier 4 is inapplicable. | diff --git a/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs b/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs index ae0a43454c..7ad18c9f39 100644 --- a/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs +++ b/rust/obs-build/tests/native/registration/canonical_video_info_query_is_registered.rs @@ -75,6 +75,12 @@ target_link_libraries(with_shared PRIVATE OBS::libobs) target_link_libraries(reference PRIVATE OBS::libobs) include("{root}/test/cmocka/tests.d/video_info_query_link_support.cmake") function(check_support) + get_target_property(adapter_sources video_info_query_link_support SOURCES) + foreach(adapter IN ITEMS encoder-video-tex-active-fields encoder-video-tex-active-mix) + if(NOT "{root}/test/cmocka/${{adapter}}-link-support.c" IN_LIST adapter_sources) + message(FATAL_ERROR "missing real encoder adapter source: ${{adapter}}") + endif() + endforeach() foreach(name IN ITEMS standalone with_shared future_consumer reference) get_target_property(links ${{name}} LINK_LIBRARIES) if(name STREQUAL "reference") diff --git a/rust/obs-build/tests/native/registration/rust-static/cases/header/test_encoder_video_tex_active.json b/rust/obs-build/tests/native/registration/rust-static/cases/header/test_encoder_video_tex_active.json new file mode 100644 index 0000000000..f14204c92f --- /dev/null +++ b/rust/obs-build/tests/native/registration/rust-static/cases/header/test_encoder_video_tex_active.json @@ -0,0 +1,40 @@ +{ + "schema": 1, + "targets": { + "test_encoder_video_tex_active_rust": { + "sources": [ + "test/cmocka/test_encoder_video_tex_active.c", + "test/cmocka/encoder-video-tex-active-fields-link-support.c" + ], + "definitions": [ + "get_mix_for_video=tex_active_recording_mix_lookup", + "obs_encoder_video_tex_active=obs_encoder_video_tex_active_rust" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "includes": [ + ".", + "libobs" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + } + }, + "tests": { + "test_encoder_video_tex_active_rust": { + "target": "test_encoder_video_tex_active_rust", + "command": [ + "${target}" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + } + } +} diff --git a/rust/obs-c-oracle/oracle.d/encoder-video-tex-active-mix.list b/rust/obs-c-oracle/oracle.d/encoder-video-tex-active-mix.list new file mode 100644 index 0000000000..e8016e1a3f --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/encoder-video-tex-active-mix.list @@ -0,0 +1 @@ +encoder-video-tex-active-mix.inc|libobs/obs-encoder-video-tex-active-mix.c.h|auto diff --git a/rust/obs-c-oracle/oracle.d/encoder-video-tex-active.list b/rust/obs-c-oracle/oracle.d/encoder-video-tex-active.list new file mode 100644 index 0000000000..38741b08f2 --- /dev/null +++ b/rust/obs-c-oracle/oracle.d/encoder-video-tex-active.list @@ -0,0 +1 @@ +encoder-video-tex-active.inc|libobs/obs-encoder-video-tex-active.c.h|auto diff --git a/rust/obs-c-oracle/oracle/encoder_video_tex_active.c b/rust/obs-c-oracle/oracle/encoder_video_tex_active.c new file mode 100644 index 0000000000..2ddea16937 --- /dev/null +++ b/rust/obs-c-oracle/oracle/encoder_video_tex_active.c @@ -0,0 +1,131 @@ +/* Original query, unchanged mutex walk and real private field adapters. */ +#include +#include +#include "obs-internal.h" + +struct encoder_tex_fixture { + struct obs_core *core; + video_t *media; + bool flags[3][2]; + unsigned lookups; + bool lookup_ok; +}; +#if defined(_MSC_VER) +#define ENCODER_TEX_THREAD_LOCAL __declspec(thread) +#else +#define ENCODER_TEX_THREAD_LOCAL __thread +#endif +static ENCODER_TEX_THREAD_LOCAL struct encoder_tex_fixture *encoder_tex_current; +#undef ENCODER_TEX_THREAD_LOCAL + +#define obs (encoder_tex_current->core) +struct obs_core_video_mix *oracle_encoder_tex_mix_walk(video_t *video); +#define get_mix_for_video oracle_encoder_tex_mix_walk +#include "encoder-video-tex-active-mix.inc" +#undef get_mix_for_video +#undef obs + +static struct obs_core_video_mix *encoder_tex_lookup(video_t *video) +{ + struct encoder_tex_fixture *fixture = encoder_tex_current; + fixture->lookups++; + fixture->lookup_ok = fixture->lookup_ok && video == fixture->media; + struct obs_core_video_mix *selected = oracle_encoder_tex_mix_walk(video); + for (size_t i = 0; i < fixture->core->video.mixes.num; i++) { + struct obs_core_video_mix *mix = fixture->core->video.mixes.array[i]; + if (mix == selected) { + mix->using_nv12_tex = fixture->flags[i][0]; + mix->using_p010_tex = fixture->flags[i][1]; + break; + } + } + return selected; +} + +#define get_mix_for_video encoder_tex_lookup +bool oracle_encoder_video_tex_active(const obs_encoder_t *encoder, enum video_format format); +#define obs_encoder_video_tex_active oracle_encoder_video_tex_active +#include "encoder-video-tex-active.inc" +#undef obs_encoder_video_tex_active +#include "encoder-video-tex-active-fields.inc" +#undef get_mix_for_video + +void oracle_encoder_tex_format_values(int *values); +void oracle_encoder_tex_format_values(int *values) +{ + values[0] = VIDEO_FORMAT_NV12; + values[1] = VIDEO_FORMAT_P010; + values[2] = VIDEO_FORMAT_R10L; +} + +typedef bool (*encoder_tex_query)(const obs_encoder_t *, enum video_format); +bool oracle_encoder_tex_check(encoder_tex_query candidate, int format, uint8_t media, uint8_t mask, + const uint8_t *videos, const uint8_t *flags); +bool oracle_encoder_tex_check(encoder_tex_query candidate, int format, uint8_t media, uint8_t mask, + const uint8_t *videos, const uint8_t *flags) +{ + struct obs_core *core = calloc(1, sizeof(*core)); + struct obs_core *before = malloc(sizeof(*core)); + struct obs_core *after = malloc(sizeof(*core)); + if (!core || !before || !after) { + free(core); + free(before); + free(after); + return false; + } + if (pthread_mutex_init(&core->video.mixes_mutex, NULL) != 0) { + free(core); + free(before); + free(after); + return false; + } + unsigned char tokens[3] = {11, 22, 33}; + video_t *identities[] = {NULL, (video_t *)&tokens[0], (video_t *)&tokens[1], (video_t *)&tokens[2]}; + struct obs_encoder encoder = {0}, encoder_before; + struct obs_core_video_mix mixes[3] = {0}, initial[3], c_after[3]; + struct obs_core_video_mix *pointers[3] = {0}; + struct encoder_tex_fixture fixture = {.core = core, .media = identities[media % 4], .lookup_ok = true}; + encoder.media = fixture.media; + memcpy(&encoder_before, &encoder, sizeof(encoder)); + for (unsigned i = 0; i < 3; i++) { + mixes[i].video = identities[videos[i] % 4]; + /* Only a matching lookup changes these to the requested values. */ + mixes[i].using_nv12_tex = !(flags[i] & 1); + mixes[i].using_p010_tex = !(flags[i] & 2); + if (mask & (1u << i)) { + size_t slot = core->video.mixes.num++; + pointers[slot] = &mixes[i]; + fixture.flags[slot][0] = (flags[i] & 1) != 0; + fixture.flags[slot][1] = (flags[i] & 2) != 0; + } + } + core->video.mixes.array = pointers; + core->video.mixes.capacity = 3; + memcpy(initial, mixes, sizeof(mixes)); + memcpy(before, core, sizeof(*core)); + /* Mutex internals may change on any platform; compare all other storage. */ + memset(&before->video.mixes_mutex, 0, sizeof(before->video.mixes_mutex)); + encoder_tex_current = &fixture; + bool c = oracle_encoder_video_tex_active(&encoder, (enum video_format)format); + bool equal = fixture.lookups == 1 && fixture.lookup_ok; + memcpy(c_after, mixes, sizeof(mixes)); + memcpy(mixes, initial, sizeof(mixes)); + fixture.lookups = 0; + fixture.lookup_ok = true; + bool rust = candidate(&encoder, (enum video_format)format); + equal = equal && c == rust && fixture.lookups == 1 && fixture.lookup_ok && + memcmp(&encoder, &encoder_before, sizeof(encoder)) == 0 && memcmp(mixes, c_after, sizeof(mixes)) == 0; + memcpy(after, core, sizeof(*core)); + memset(&after->video.mixes_mutex, 0, sizeof(after->video.mixes_mutex)); + equal = equal && memcmp(before, after, sizeof(*core)) == 0; + int locked = pthread_mutex_trylock(&core->video.mixes_mutex); + equal = equal && locked == 0; + if (locked == 0) + pthread_mutex_unlock(&core->video.mixes_mutex); + encoder_tex_current = NULL; + pthread_mutex_destroy(&core->video.mixes_mutex); + free(after); + free(before); + free(core); + return equal; +} diff --git a/rust/obs-encoder/src/ffi/mod.rs b/rust/obs-encoder/src/ffi/mod.rs index 71ff06545c..bdd7d1f88e 100644 --- a/rust/obs-encoder/src/ffi/mod.rs +++ b/rust/obs-encoder/src/ffi/mod.rs @@ -3,3 +3,4 @@ pub mod last_error; pub mod media_getters; pub mod packet_ownership; pub mod type_info; +pub mod video_tex_active; diff --git a/rust/obs-encoder/src/ffi/video_tex_active.rs b/rust/obs-encoder/src/ffi/video_tex_active.rs new file mode 100644 index 0000000000..1a81940b79 --- /dev/null +++ b/rust/obs-encoder/src/ffi/video_tex_active.rs @@ -0,0 +1,52 @@ +//! Borrow opaque C owners; never reproduce the encoder or mix layout in Rust. +use core::ffi::{c_int, c_void}; +use core::ptr::NonNull; + +unsafe extern "C" { + fn obs_encoder_video_tex_active_mix(encoder: *const c_void) -> *const c_void; + fn obs_encoder_video_tex_active_nv12(mix: *const c_void) -> bool; + fn obs_encoder_video_tex_active_p010(mix: *const c_void) -> bool; +} + +/// # Safety +/// As in C, the encoder and OBS core must be live and synchronized, and the +/// selected mix must remain readable through the query. NULL encoder is outside +/// the C API's contract; NULL media and an absent mix are valid. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_encoder_video_tex_active( + encoder: *const c_void, + format: c_int, +) -> bool { + // SAFETY: the private entry has the same ownership contract as this API. + unsafe { obs_encoder_video_tex_active_rust(encoder, format) } +} + +/// Private static-link entry: shared libobs must never satisfy a fixture's +/// Rust query before its archive member is selected (notably with frameworks). +/// +/// # Safety +/// Identical live-encoder/core/mix contract to obs_encoder_video_tex_active. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn obs_encoder_video_tex_active_rust( + encoder: *const c_void, + format: c_int, +) -> bool { + // Reject an invalid owner before the C adapter can project encoder->media. + let encoder = NonNull::new(encoder.cast_mut()).expect("the C API requires a live encoder"); + crate::video_tex_active::video_tex_active( + format, + || { + // SAFETY: the owner was checked above; C retains the synchronized + // get_mix_for_video walk and reads the real encoder media field. + NonNull::new(unsafe { obs_encoder_video_tex_active_mix(encoder.as_ptr()) }.cast_mut()) + }, + |mix| { + // SAFETY: the safe core selects only a present mix and the NV12 flag. + unsafe { obs_encoder_video_tex_active_nv12(mix.as_ptr()) } + }, + |mix| { + // SAFETY: the safe core selects only a present mix and the P010 flag. + unsafe { obs_encoder_video_tex_active_p010(mix.as_ptr()) } + }, + ) +} diff --git a/rust/obs-encoder/src/lib.rs b/rust/obs-encoder/src/lib.rs index 58a34036fe..f729bea9a2 100644 --- a/rust/obs-encoder/src/lib.rs +++ b/rust/obs-encoder/src/lib.rs @@ -7,3 +7,4 @@ pub mod last_error; pub mod media_getters; pub mod packet_ownership; pub mod type_info; +pub mod video_tex_active; diff --git a/rust/obs-encoder/src/video_tex_active.rs b/rust/obs-encoder/src/video_tex_active.rs new file mode 100644 index 0000000000..035805a9bb --- /dev/null +++ b/rust/obs-encoder/src/video_tex_active.rs @@ -0,0 +1,21 @@ +//! Query C-owned texture-input state after the synchronized mix lookup. + +pub const NV12: i32 = 2; +pub const P010: i32 = 18; + +/// Look up exactly once, then read only the flag matching the requested format. +pub fn video_tex_active( + format: i32, + lookup: impl FnOnce() -> Option, + nv12: impl FnOnce(M) -> bool, + p010: impl FnOnce(M) -> bool, +) -> bool { + let Some(mix) = lookup() else { + return false; + }; + match format { + NV12 => nv12(mix), + P010 => p010(mix), + _ => false, + } +} diff --git a/rust/obs-encoder/tests/encoder/main.rs b/rust/obs-encoder/tests/encoder/main.rs index 05112357e9..c71aca1cb3 100644 --- a/rust/obs-encoder/tests/encoder/main.rs +++ b/rust/obs-encoder/tests/encoder/main.rs @@ -10,3 +10,5 @@ mod encoder_module; mod encoder_module_parity; mod type_info; mod type_info_parity; +mod video_tex_active; +mod video_tex_active_parity; diff --git a/rust/obs-encoder/tests/encoder/video_tex_active.rs b/rust/obs-encoder/tests/encoder/video_tex_active.rs new file mode 100644 index 0000000000..9863bc10ae --- /dev/null +++ b/rust/obs-encoder/tests/encoder/video_tex_active.rs @@ -0,0 +1,100 @@ +//! Tier 1 mirrors test_encoder_video_tex_active.c without private C layouts. +use obs_encoder::video_tex_active::{NV12, P010, video_tex_active}; +use std::cell::{Cell, RefCell}; + +fn check(format: i32, media: Option, mixed: bool, flags: [bool; 2]) { + let encoder = (media, 0x5a_u8); + let before = encoder; + let mix = Cell::new([!flags[0], !flags[1]]); + let events = RefCell::new(Vec::new()); + let result = video_tex_active( + format, + || { + assert_eq!(encoder.0, media); + events.borrow_mut().push("lookup"); + // Like the characterization, make pre-lookup reads observably wrong. + mix.set(flags); + mixed.then_some(&mix) + }, + |selected| { + assert!(std::ptr::eq(selected, &mix)); + events.borrow_mut().push("nv12"); + selected.get()[0] + }, + |selected| { + assert!(std::ptr::eq(selected, &mix)); + events.borrow_mut().push("p010"); + selected.get()[1] + }, + ); + assert_eq!( + result, + mixed && ((format == NV12 && flags[0]) || (format == P010 && flags[1])) + ); + let mut expected = vec!["lookup"]; + if mixed && format == NV12 { + expected.push("nv12"); + } else if mixed && format == P010 { + expected.push("p010"); + } + assert_eq!(*events.borrow(), expected); + assert_eq!(encoder, before); + assert_eq!(mix.get(), flags); +} + +#[test] +fn test_encoder_video_tex_active_missing_mix() { + for media in [None, Some(11)] { + for format in [NV12, P010, 6, 0] { + check(format, media, false, [true, true]); + } + } +} + +#[test] +fn test_encoder_video_tex_active_format_and_flag_matrix() { + for flags in [[false, false], [true, false], [false, true], [true, true]] { + for format in 0..=25 { + check(format, Some(11), true, flags); + } + } +} + +#[test] +fn test_encoder_video_tex_active_reads_selected_mix_after_lookup() { + for flags in [[false, false], [true, false], [false, true], [true, true]] { + for format in [NV12, P010] { + check(format, Some(11), true, flags); + } + } +} + +#[test] +fn test_encoder_tex_active_null_no_video_and_canvas_gone() { + // A live encoder before/after video reset, or after its canvas mix is gone. + for media in [None, Some(11)] { + for format in [NV12, P010, 1] { + check(format, media, false, [false, false]); + } + } +} + +#[test] +fn test_encoder_tex_active_null_main_video_capabilities() { + // Model capabilities; make no assertion about any real device's support. + for available in [false, true] { + for (flags, format) in [([available, false], NV12), ([false, available], P010)] { + for requested in [NV12, P010, 1] { + check(requested, Some(11), true, flags); + } + check(format, Some(11), true, flags); + } + } +} + +#[test] +fn other_formats_still_look_up_without_reading_flags() { + for format in [i32::MIN, -1, 26, i32::MAX] { + check(format, None, true, [true, true]); + } +} diff --git a/rust/obs-encoder/tests/encoder/video_tex_active_parity.rs b/rust/obs-encoder/tests/encoder/video_tex_active_parity.rs new file mode 100644 index 0000000000..e0fc096c98 --- /dev/null +++ b/rust/obs-encoder/tests/encoder/video_tex_active_parity.rs @@ -0,0 +1,80 @@ +//! Tier 3: original C query and real mutex walk, using actual private layouts. +//! NULL encoders/cores and dangling mixes are excluded as undefined in C. +use core::ffi::{c_int, c_void}; +use obs_encoder::ffi::video_tex_active::obs_encoder_video_tex_active; +use obs_encoder::video_tex_active::{NV12, P010}; +use proptest::prelude::*; + +type Query = unsafe extern "C" fn(*const c_void, c_int) -> bool; +unsafe extern "C" { + fn oracle_encoder_tex_format_values(values: *mut c_int); + fn oracle_encoder_tex_check( + candidate: Query, + format: c_int, + media: u8, + mask: u8, + videos: *const u8, + flags: *const u8, + ) -> bool; +} + +#[test] +fn real_header_format_values() { + let mut values = [0; 3]; + // SAFETY: C writes exactly three integers measured from the real enum. + unsafe { oracle_encoder_tex_format_values(values.as_mut_ptr()) }; + assert_eq!(values, [NV12, P010, 25]); +} + +fn compare(format: i32, media: u8, mask: u8, videos: [u8; 3], flags: [u8; 3]) { + // SAFETY: C creates and snapshots live real-header encoder/core/mix owners, + // initializes the real mutex, calls both implementations, then destroys it. + // Both three-byte inputs remain live; pointers are compared, never dereferenced. + assert!( + unsafe { + oracle_encoder_tex_check( + obs_encoder_video_tex_active, + format, + media, + mask, + videos.as_ptr(), + flags.as_ptr(), + ) + }, + "format={format}, media={media}, mask={mask}, videos={videos:?}, flags={flags:?}" + ); +} + +#[test] +fn characterized_edges_and_first_matching_mix() { + for format in 0..=25 { + for flags in 0..4 { + for media in 0..4 { + for mask in [0, 1, 2, 4, 7] { + compare(format, media, mask, [1, 2, 3], [flags; 3]); + // Duplicate NULL/non-NULL videos select the first present mix. + compare( + format, + media, + mask, + [media; 3], + [flags, flags ^ 3, flags ^ 1], + ); + } + } + } + } +} + +proptest! { + #![proptest_config(ProptestConfig { cases: obs_c_oracle::parity::cases(512), failure_persistence: None, ..ProptestConfig::default() })] + #[test] + fn result_lookup_and_storage_match_original_c( + format in prop_oneof![0i32..=25, any::()], + media in 0u8..4, mask in 0u8..8, + videos in prop::array::uniform3(0u8..4), + flags in prop::array::uniform3(0u8..4), + ) { + compare(format, media, mask, videos, flags); + } +} diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 8a3dffd34e..4983f85a56 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -4124,6 +4124,32 @@ } ] }, + "test_encoder_video_tex_active_rust": { + "sources": [ + "test/cmocka/test_encoder_video_tex_active.c", + "test/cmocka/encoder-video-tex-active-fields-link-support.c" + ], + "definitions": [ + "get_mix_for_video=tex_active_recording_mix_lookup", + "obs_encoder_video_tex_active=obs_encoder_video_tex_active_rust" + ], + "libraries": [ + "libobs_rust-static", + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "OBS::w32-pthreads", + "requires": "OS_WINDOWS" + } + ], + "requires": "HAVE_LIBOBS_RUST_STATIC", + "includes": [ + ".", + "libobs" + ] + }, "test_encoding_lag_rescale": { "sources": [ "test/cmocka/test_encoding_lag_rescale.c" @@ -23461,6 +23487,13 @@ "${target}" ] }, + "test_encoder_video_tex_active_rust": { + "target": "test_encoder_video_tex_active_rust", + "command": [ + "${target}" + ], + "requires": "HAVE_LIBOBS_RUST_STATIC" + }, "test_encoding_lag_rescale": { "target": "test_encoding_lag_rescale", "command": [ diff --git a/test/cmocka/encoder-video-tex-active-fields-link-support.c b/test/cmocka/encoder-video-tex-active-fields-link-support.c new file mode 100644 index 0000000000..6d66beaf1a --- /dev/null +++ b/test/cmocka/encoder-video-tex-active-fields-link-support.c @@ -0,0 +1,2 @@ +/* Real private adapters; the characterization may own the mix lookup. */ +#include "libobs/obs-encoder-video-tex-active-fields.c.h" diff --git a/test/cmocka/encoder-video-tex-active-mix-link-support.c b/test/cmocka/encoder-video-tex-active-mix-link-support.c new file mode 100644 index 0000000000..a3a150564c --- /dev/null +++ b/test/cmocka/encoder-video-tex-active-mix-link-support.c @@ -0,0 +1,2 @@ +/* Real synchronized C lookup, demand-linked alongside the shared core owner. */ +#include "libobs/obs-encoder-video-tex-active-mix.c.h" diff --git a/test/cmocka/tests.d/video_info_query_link_support.cmake b/test/cmocka/tests.d/video_info_query_link_support.cmake index 3dafa2a2d8..8bd1579776 100644 --- a/test/cmocka/tests.d/video_info_query_link_support.cmake +++ b/test/cmocka/tests.d/video_info_query_link_support.cmake @@ -7,6 +7,8 @@ function(_video_info_query_link_support) STATIC "${CMAKE_CURRENT_SOURCE_DIR}/video-info-query-link-support.c" "${CMAKE_CURRENT_SOURCE_DIR}/core-state-link-support.c" + "${CMAKE_CURRENT_SOURCE_DIR}/encoder-video-tex-active-fields-link-support.c" + "${CMAKE_CURRENT_SOURCE_DIR}/encoder-video-tex-active-mix-link-support.c" ) target_include_directories( video_info_query_link_support