Skip to content

rust: Port the context name dedup and unnamed names in obs.c to obs-core #1787

Closed

Description

@rdoupe

Port the two context name helpers of libobs/obs.c.h to rust/obs-core (#1773) as a new module, context_names. They decide the name every source, scene, canvas, encoder, output and service gets: a duplicate name on insert or rename becomes "<name> 2", "<name> 3", … (the first free number from 2 up), and a NULL or empty name becomes "__unnamed%04lld" from a global counter. If they are wrong, a scene collection loads with its sources renamed, and hotkeys, filters and obs-websocket clients that find sources by name stop finding them. The video output plan in the same file was ported in #1773.

The unit, about 30 lines:

  • obs_context_deduplicate_name() (master obs.c.h 2749-2766): NULL when the name is free in the given uthash table, else dstr_printf("%s %d", name, suffix++) from 2 until HASH_FIND_STR finds nothing. The suffix is a C int.
  • dup_name() (2646-2659): NULL for a private context without a name; "__unnamed%04lld" with obs->data.unnamed_index++ for a NULL or empty name; else bstrdup.

These stay C: the callers obs_context_data_init_wrap(), obs_context_data_insert_name() (2768-2797), obs_context_data_setname() (2876) and obs_context_data_setname_ht() (2887-2915), the uthash tables and their locks, the rename cache, the two warnings, and the counter field itself.

Phase and position

Phase 6 (#444), libobs core through opaque seams. It joins rust/obs-core, which holds the video output plan (#1773); it is the second unit extracted from obs.c.h. It is part of a large file, so it goes in through function extraction (testing-policy.md, "Extracting a function before porting it"), as the video plan did (#1714). Its Rust tests join the crate's existing tests/obs_core/ binary. No new test binary.

Dependencies and existing tests

  • The moved code needs no libobs state. The lookup becomes a callback, bool (*exists)(void *param, const char *name), which obs.c implements with the same HASH_FIND_STR on the same table, so the loop asks the same names in the same order. The counter stays obs->data.unnamed_index; obs.c passes its address and the helper increments it. Both helpers return a bmalloc'd string, so the port returns it through obs-util::bmem (or C copies it with bstrdup); no darray or uthash crosses the boundary.
  • Today: many tests create sources, but none pins the names. PR 1 adds tests (below).
  • Input: names come from scene collections (importable), obs-websocket (CreateInput, SetInputName, CreateScene, which refuse an existing or empty name but take any other bytes), scripts and plugins. They are untrusted bytes: any byte but NUL, not necessarily UTF-8, any length. The port must keep the "%s %d" formatting byte-exact, never use a name as a format, never go through str, and never panic.

Plan

  1. Characterization and extraction PR (behaviour-neutral, two commits):
    • test_context_names (real libobs, public API only, so it also runs the hybrid libobs; each case starts its own libobs and needs no video). It pins insert and rename dedup, the first free number and gaps, names already ending in a number or " (2)", % sequences, bytes that are not UTF-8, Unicode normalization, a 64 KiB name, 300 copies of one name, the unnamed names and their four-digit minimum, the counter shared by sources, encoders, outputs and canvases, private sources, canvas-bound sources (scenes) made unique within their canvas, canvas names, the warnings and the rename signal.
    • The move: both helpers go into libobs/obs-context-names.c.h as hidden entry points (declared in obs-internal.h without EXPORT), obs_context_name_dup(name, private, &unnamed_index) and obs_context_name_deduplicate(name, exists, param). dup_name() and obs_context_deduplicate_name() stay as one-line wrappers, so the callers do not change. The file joins libobs.core with libobs.pch.h.
    • test_context_name_helpers, which compiles the moved file (no libobs core) and calls only the entry points with a scripted exists(): the questions asked and their order, 100000 taken numbers, a 1 MiB name, an empty name that is taken, a NULL name, and the counter at its edges.
  2. Fix PR, if wanted (spec-following, RED then GREEN): the rename warning names the requested name (below). Possibly also the NULL rename crash, through the unnamed path dup_name() already has.
  3. Port PR (RED, then GREEN):
    • obs_core::context_names, a safe core over &CStr with an exists(&CStr) -> bool closure, and src/ffi/context_names.rs for the two entry points. The C file moves to a ported group, libobs.ported-context-names, with its .pch.h, source-sets.json entry and export fragment (hidden, as obs-video-plan.c is in rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt); ci/test_ported_sources.py gets the object.
    • The suffix is a C int. Reaching its end needs 2^31 taken names, which is not reachable, but the Rust core must not panic or wrap there: it saturates and stops (a documented intentional difference). The same for the counter at LLONG_MAX (C overflows a signed integer there).
    • Tests:
      • Tier 1 ports every test_context_name_helpers expectation 1:1.
      • Tier 3 checks parity against the moved C through obs-c-oracle (oracle/context_names.c): a proptest over names (arbitrary bytes, %, trailing digits, " (2)", empty, long) and random taken sets (the name, "<name> <k>" for random k, other names), comparing the result and every name exists() was asked, in order; and the unnamed name for random counters, private or not.
      • Tier 2: test_context_name_helpers_rust, the unchanged test_context_name_helpers.c linked against libobs_rust-static, as test_scene_bounds_place_rust does; test_context_names runs the hybrid libobs unchanged.
      • Tier 4 does not apply (no protocol change).
    • Docs: the rust/README.md row, and the docs/rust-port/naming.md row for rust/obs-core (add obs-context-names.c.h).

For the reviewer to decide

  • Seam shape. The lookup is a callback and the counter a pointer, so C keeps the tables, the locks and the counter, and the Rust core is pure. The alternative, passing the table, would need uthash across the boundary for no gain.
  • What reaches the hybrid build. The entry points are hidden, so a test linking libobs.so cannot call them. test_context_name_helpers calls nothing else, so the port can link the same file against the Rust staticlib. test_context_names drives the callers through the public API, so it covers the swap end to end.
  • Behaviours the characterization pins, not endorses (none is changed by the move):
    • The rename warning (obs_context_data_setname_ht) prints the old name as the "duplicate name", not the requested one (round 3 N4).
    • Renaming "A 2" to "A" while "A" is taken leaves it "A 2" (its own name is free during the rename), with the warning and a rename signal from "A 2" to "A 2".
    • A rename to an empty name takes the next unnamed name without making it unique, so a source can get the name another source already has (a source loaded as "__unnamed0001" from a collection, for example). obs-websocket refuses an empty name; scripts and plugins can pass one.
    • obs_source_set_name(source, NULL) on a public source passes NULL to HASH_FIND_STR, which crashes (API or script misuse only; N4).
    • Names are not made unique for encoders, outputs and services, and obs_encoder_set_name() does not do it. A scene and an input may share a name (the scene is unique only within its canvas).
    • unnamed_index is read and incremented without a lock by every thread that creates a context.
    • A taken empty name would become " 2" (no table holds "", so libobs never asks).

Activity

  1. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1787

    1. Characterization and move PR (next, ready): dup_name and obs_context_deduplicate_name moved into libobs/obs-context-names.c.h behind hidden entry points, with test_context_names (real libobs, 14 cases) and test_context_name_helpers (pure, 12 cases). No behaviour change.
    2. Port to rust/obs-core (module context_names) with oracle parity tests.
  2. added
    claimed
    on Oct 22, 2026
  3. rdoupe commented on Oct 22, 2026

    @rdoupe
    CollaboratorAuthor

    Port is up in #1813. Intentional differences from C, both for null pointers obs.c never passes (listed in the parity test header and rust/README.md): a null exists callback returns NULL without a call, and a null unnamed_index returns NULL where an unnamed name is needed. The behaviours pinned as not endorsed in #1788 are kept, and the exists() call sequence matches C one to one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions