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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ci/test_bench_libobs_stand_ins.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
# Identity getters are absent: #3924 exports their real Rust shims.
# The encoder packet copy and release are absent: #4650 exports them from
# obs-encoder, which the bench links through obs-data.
# The encoder name getter is absent too: #5253 exports it from obs-encoder;
# the synthetic interleaver replaces only its private name seam.
SYMBOLS = (
"obs_encoder_get_name",
"obs_encoder_group_num_started",
"obs_encoder_interleave_info",
"obs_hotkey_binding_fields",
Expand Down Expand Up @@ -70,6 +71,7 @@
"obs_output_initialize_encoders",
"obs_output_interleave_active",
"obs_output_interleave_add_caption",
"obs_output_interleave_encoder_name",
"obs_output_interleave_fields",
"obs_output_interleave_first_audio_encoder_index",
"obs_output_interleave_first_video_encoder_index",
Expand Down
3 changes: 2 additions & 1 deletion libobs/build/libobs.core-output+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "libobs/obs-encoder-error-ownership-fields.c.h"
#include "libobs/obs-encoder-extra-data-fields.c.h"
#include "libobs/obs-encoder-frame-rate-divisor-fields.c.h"
#include "libobs/obs-encoder-get-name.c.h"
#include "libobs/obs-encoder-get-name-fields.c.h"
#include "libobs/obs-encoder-gpu-scaling-getters-fields.c.h"
#include "libobs/obs-encoder-group-lifecycle-fields.c.h"
#include "libobs/obs-encoder-init-fields.c.h"
Expand Down Expand Up @@ -51,6 +51,7 @@
#include "libobs/obs-output-frames-dropped-fields.c.h"
#include "libobs/obs-output-handler-getters-fields.c.h"
#include "libobs/obs-output-identity-fields.c.h"
#include "libobs/obs-output-interleave-name-fields.c.h"
#include "libobs/obs-output-last-error-fields.c.h"
#include "libobs/obs-output-mixer-getters-fields.c.h"
#include "libobs/obs-output-mixer-setters-fields.c.h"
Expand Down
5 changes: 5 additions & 0 deletions libobs/build/libobs.ported-encoder-get-name+.h.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Unity group (BUILD.md): original encoder name query, reference only. */

#include "libobs/build/libobs.pch.h"

#include "libobs/obs-encoder-get-name.c.h"
5 changes: 5 additions & 0 deletions libobs/build/ported-source-sets/encoder-get-name.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"LIBOBS_PORTED_SOURCES": [
"build/libobs.ported-encoder-get-name+.h.c"
]
}
1 change: 1 addition & 0 deletions libobs/cmake/rust-exports.d/obs-encoder-get_name.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obs_encoder_get_name
8 changes: 8 additions & 0 deletions libobs/obs-encoder-get-name-fields.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "libobs/build/libobs.pch.h"

const char *obs_encoder_get_name_context_name(const obs_encoder_t *encoder);

const char *obs_encoder_get_name_context_name(const obs_encoder_t *encoder)
{
return encoder->context.name;
}
10 changes: 10 additions & 0 deletions libobs/obs-output-interleave-name-fields.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "libobs/build/libobs.pch.h"

/* A private seam lets synthetic interleaver fixtures supply their own names
* without replacing the public encoder getter or its native-layout projection. */
const char *obs_output_interleave_encoder_name(const obs_encoder_t *encoder);

const char *obs_output_interleave_encoder_name(const obs_encoder_t *encoder)
{
return obs_encoder_get_name(encoder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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)
foreach(adapter IN ITEMS encoder-video-tex-active-fields encoder-video-tex-active-mix encoder-get-name output-interleave-name)
if(NOT "{root}/test/cmocka/${{adapter}}-link-support.c" IN_LIST adapter_sources)
message(FATAL_ERROR "missing real encoder adapter source: ${{adapter}}")
endif()
Expand Down
4 changes: 2 additions & 2 deletions rust/obs-c-oracle/benches/port_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ mod libobs_stand_ins {
}

#[unsafe(no_mangle)]
pub extern "C" fn obs_encoder_get_name() -> ! {
called("obs_encoder_get_name")
pub extern "C" fn obs_output_interleave_encoder_name() -> ! {
called("obs_output_interleave_encoder_name")
}

#[unsafe(no_mangle)]
Expand Down
1 change: 1 addition & 0 deletions rust/obs-c-oracle/oracle.d/encoder-get-name.list
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
encoder-get-name.inc|libobs/obs-encoder-get-name.c.h|auto
63 changes: 63 additions & 0 deletions rust/obs-c-oracle/oracle/encoder_get_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Original C body and production adapter, with the real private encoder layout. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "obs-internal.h"
const char *oracle_obs_encoder_get_name(const obs_encoder_t *encoder);
#define obs_encoder_get_name oracle_obs_encoder_get_name
#include "encoder-get-name.inc"
#undef obs_encoder_get_name
#include "encoder-get-name-fields.inc"

struct encoder_name_log {
unsigned count;
int level;
char message[256];
};
#if defined(__GNUC__) || defined(__clang__)
static void encoder_name_capture(int level, const char *format, va_list args, void *param)
__attribute__((format(printf, 2, 0)));
#endif
static void encoder_name_capture(int level, const char *format, va_list args, void *param)
{
struct encoder_name_log *log = param;
log->count++;
log->level = level;
vsnprintf(log->message, sizeof(log->message), format, args);
}

typedef const char *(*encoder_name_getter)(const obs_encoder_t *);
static bool encoder_name_compare(const obs_encoder_t *encoder, encoder_name_getter rust)
{
struct encoder_name_log r = {0}, c = {0};
base_set_log_handler(encoder_name_capture, &r);
const char *actual = rust(encoder);
base_set_log_handler(encoder_name_capture, &c);
const char *expected = oracle_obs_encoder_get_name(encoder);
return actual == expected && r.count == c.count && r.level == c.level && strcmp(r.message, c.message) == 0 &&
(encoder ? r.count == 0
: r.count == 1 && r.level == LOG_DEBUG &&
strcmp(r.message, "obs_encoder_get_name: Null 'encoder' parameter") == 0);
}

bool oracle_encoder_get_name_probe(encoder_name_getter rust, const char *name, const char *renamed, bool null_name);
bool oracle_encoder_get_name_probe(encoder_name_getter rust, const char *name, const char *renamed, bool null_name)
{
log_handler_t saved_handler;
void *saved_param;
base_get_log_handler(&saved_handler, &saved_param);
struct obs_encoder encoder = {0};
char first[128], second[128];
snprintf(first, sizeof(first), "%s", name);
snprintf(second, sizeof(second), "%s", renamed);
encoder.context.name = null_name ? NULL : first;
unsigned char before[sizeof(encoder)];
memcpy(before, &encoder, sizeof(encoder));
bool equal = encoder_name_compare(NULL, rust) && encoder_name_compare(&encoder, rust) &&
encoder_name_compare(&encoder, rust) && memcmp(before, &encoder, sizeof(encoder)) == 0;
encoder.context.name = second;
memcpy(before, &encoder, sizeof(encoder));
equal = equal && encoder_name_compare(&encoder, rust) && memcmp(before, &encoder, sizeof(encoder)) == 0;
base_set_log_handler(saved_handler, saved_param);
return equal;
}
4 changes: 3 additions & 1 deletion rust/obs-c-oracle/oracle/output_interleave.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* obs-output.c wrappers (obs_output_interleave_active(), the two first
* encoder index helpers and obs_output_interleave_add_caption()),
* obs_encoder_packet_create_instance() and _release(), obs_output_get_name()
* and obs_encoder_get_name(). The cargo test that links this file defines
* and the private obs_output_interleave_encoder_name() seam. The cargo test that links this file defines
* them, recording each call, and the Rust shim calls the same ones, so both
* sides are seen through the same stubs; the packet copy and release are
* obs-encoder's real exports. pthread, blog and the darray come from the
Expand All @@ -30,6 +30,7 @@
#define get_encoder_index oracle_get_encoder_index
#define free_packets oracle_free_packets
#define os_gettime_ns oracle_output_interleave_clock_ns
#define obs_encoder_get_name obs_output_interleave_encoder_name

/* send_interleaved() names itself with __FUNCTION__, a GCC, Clang and MSVC
* extension that -Wpedantic rejects, and the file converts between signed
Expand All @@ -46,6 +47,7 @@
#pragma GCC diagnostic ignored "-Wformat"
#endif
#include "obs-output-interleave.c.h"
#undef obs_encoder_get_name
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
Expand Down
24 changes: 24 additions & 0 deletions rust/obs-encoder/src/ffi/get_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Opaque encoder projection follows the public NULL guard.
use core::ffi::{c_char, c_int, c_void};

unsafe extern "C" {
fn obs_encoder_get_name_context_name(encoder: *const c_void) -> *const c_char;
fn blog(level: c_int, format: *const c_char, ...);
}

/// # Safety
/// A non-NULL encoder must be live; the result borrows its current context name.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn obs_encoder_get_name(encoder: *const c_void) -> *const c_char {
crate::get_name::get(
(!encoder.is_null()).then_some(|| {
// SAFETY: get invokes the projection only for a non-NULL live encoder.
unsafe { obs_encoder_get_name_context_name(encoder) }
}),
|level, message| {
// SAFETY: the static diagnostic is NUL-terminated without substitutions.
unsafe { blog(level, message.as_ptr()) }
},
)
.unwrap_or(core::ptr::null())
}
1 change: 1 addition & 0 deletions rust/obs-encoder/src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod encoder_module;
pub mod get_name;
pub mod last_error;
pub mod media_getters;
pub mod packet_ownership;
Expand Down
16 changes: 16 additions & 0 deletions rust/obs-encoder/src/get_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Borrow the current context name without copying or caching it.
use std::ffi::CStr;

/// Reject a missing encoder before projecting its C-owned name.
pub fn get<T>(
encoder: Option<impl FnOnce() -> T>,
log: impl FnOnce(i32, &'static CStr),
) -> Option<T> {
match encoder {
Some(project) => Some(project()),
None => {
log(400, c"obs_encoder_get_name: Null 'encoder' parameter");
None
}
}
}
1 change: 1 addition & 0 deletions rust/obs-encoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod encoder_module;
#[allow(unsafe_code)]
pub mod ffi;
pub mod get_name;
pub mod last_error;
pub mod media_getters;
pub mod packet_ownership;
Expand Down
63 changes: 63 additions & 0 deletions rust/obs-encoder/tests/encoder/get_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Tier 1 mirrors test_encoder_get_name.c without a native encoder layout.
use obs_encoder::get_name::get;
use std::cell::Cell;
use std::ffi::CStr;

#[test]
fn null_encoder_logs_and_returns_null() {
let mut logs = Vec::new();
assert_eq!(
get::<usize>(None::<fn() -> usize>, |level, text| logs
.push((level, text))),
None
);
assert_eq!(
logs,
[(400, c"obs_encoder_get_name: Null 'encoder' parameter")]
);
}

fn name<'a>(value: &'a Cell<&'a CStr>) -> &'a CStr {
get(Some(|| value.get()), |_, _| panic!("valid encoder logged")).unwrap()
}

#[test]
fn returns_the_context_name_pointer_silently() {
let value = Cell::new(c"get name");
let first = name(&value);
assert_eq!(first.as_ptr(), value.get().as_ptr());
assert_eq!(name(&value).as_ptr(), first.as_ptr());
assert_eq!(first, c"get name");
}

#[test]
fn unnamed_encoder_returns_its_generated_name() {
let value = Cell::new(c"__unnamed0017");
let current = name(&value);
assert_eq!(current.as_ptr(), value.get().as_ptr());
assert!(current.to_bytes().starts_with(b"__unnamed"));
}

#[test]
fn follows_a_rename() {
let value = Cell::new(c"before");
assert_eq!(name(&value), c"before");
value.set(c"after");
let current = name(&value);
assert_eq!(current.as_ptr(), value.get().as_ptr());
assert_eq!(current, c"after");
}

#[test]
fn projects_exactly_once_even_for_an_absent_name() {
let calls = Cell::new(0);
let current = get(
Some(|| {
calls.set(calls.get() + 1);
None::<usize>
}),
|_, _| panic!("valid encoder logged"),
);
assert_eq!(current, Some(None));
assert_eq!(calls.get(), 1);
}
78 changes: 78 additions & 0 deletions rust/obs-encoder/tests/encoder/get_name_parity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Tier 3: untouched C body and real private projection. No intentional differences.
use core::ffi::{c_char, c_void};
use obs_encoder::ffi::get_name::obs_encoder_get_name;
use proptest::prelude::*;
use std::ffi::CString;

type Getter = unsafe extern "C" fn(*const c_void) -> *const c_char;
unsafe extern "C" {
fn oracle_encoder_get_name_probe(
get: Getter,
name: *const c_char,
renamed: *const c_char,
null_name: bool,
) -> bool;
}

fn check(name: &[u8], renamed: &[u8], null_name: bool) {
let name = CString::new(name).unwrap();
let renamed = CString::new(renamed).unwrap();
// SAFETY: C borrows the bounded strings and passes live real-layout encoders
// (or NULL) to both getters. The isolated process owns its log handler.
unsafe {
assert!(oracle_encoder_get_name_probe(
obs_encoder_get_name,
name.as_ptr(),
renamed.as_ptr(),
null_name
));
}
}

#[test]
fn original_c_characterization_and_edges() {
if !super::isolated(concat!(
module_path!(),
"::original_c_characterization_and_edges"
)) {
return;
}
for name in [
b"get name".as_slice(),
b"__unnamed0017",
b"before",
b"",
b"%s-%n",
b"\xff\x80",
] {
for null_name in [false, true] {
check(name, b"after", null_name);
}
}
}

#[test]
fn randomized_original_c_parity() {
if !super::isolated(concat!(module_path!(), "::randomized_original_c_parity")) {
return;
}
use proptest::test_runner::{Config, TestRunner};
let mut runner = TestRunner::new(Config {
cases: obs_c_oracle::parity::cases(128),
failure_persistence: None,
..Config::default()
});
runner
.run(
&(
proptest::collection::vec(1_u8..=255, 0..128),
proptest::collection::vec(1_u8..=255, 0..128),
any::<bool>(),
),
|(name, renamed, null_name)| {
check(&name, &renamed, null_name);
Ok(())
},
)
.unwrap();
}
2 changes: 2 additions & 0 deletions rust/obs-encoder/tests/encoder/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod get_name;
mod get_name_parity;
mod last_error;
mod last_error_parity;
mod media_getters;
Expand Down
Loading
Loading