Skip to content

util: Carry a whole second in os_nstime_to_timespec - #1508

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/nstime-whole-second
Oct 20, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/nstime-whole-second

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1505

What and why

os_nstime_to_timespec now returns a normalized struct timespec for every timestamp. Before, a timestamp above the base whose wall time landed on a whole second came back as tv_nsec == 1000000000 with tv_sec one short, which is not a valid timespec.

  • Above the base, the nanoseconds are the base's tv_nsec plus offset % 1000000000, which can be anything from 0 to 1999999998. The carry was if (nsecs > nsecs_per_sec), so a sum of exactly 1000000000 was not carried. It is >= now, in the C (libobs/util/platform-time.c.h) and in the Rust core (obs-util::platform_time, ported in util: Port os_nstime_to_timespec to obs-util #1507). Below the base the result was already normalized.
  • The only caller in the tree is shared/bpm (Broadcast Performance Metrics, sent with Enhanced Broadcasting multitrack video). render_metrics_time() prints tv_sec with %T and tv_nsec / 1000000 with .%03ld, so a frame at …:33.000 was sent as …:32.1000Z, which reads as 900 ms early. A plugin passing the result to pthread_cond_timedwait or nanosleep would get EINVAL.
  • Upstream OBS has the same code in libobs/util/platform.c. Nobody has reported it there, so there is no upstream issue.
  • This deliberately changes the characterization tests from util: Move os_nstime_to_timespec into platform-time.c #1506/util: Port os_nstime_to_timespec to obs-util #1507. They pinned the uncarried second as "characterized, not endorsed" so the port would keep the C behavior; now that the C is fixed, the RED commit makes them expect the normalized {sec, 0}: test_whole_second_above_the_base and its Tier 1 mirror, check() (no whole-second special case above the base), test_around_the_base (no longer accepts either form), and the Tier 1 fixed-base cases. Each also pins one nanosecond below and above the edge.
  • The Tier 3 parity test compares the Rust core against the same C file, so both sides change together and it needs no exclusion. whole_second_edge now expects {101, 0} from both. The rust/README.md row no longer says the port keeps the C carry.

Headless (private Xvfb, private D-Bus, fresh HOME):

Build Case Result
master 847d4db test_platform_time with the RED tests, libobs and libobs-with-rust, both lanes test_whole_second_above_the_base fails (tv_sec one short), 8/9 pass
master 847d4db obs-util platform_time tests with the RED tests 3 of 21 fail (whole_second_above_the_base, carry_and_borrow_with_a_fixed_base, negative_wall_seconds)
This branch test_platform_time, libobs and libobs-with-rust, both lanes 9/9 pass, 20 of 20 repeats in each lane and library
This branch obs-util platform_time tests (Tiers 1 and 3) 21/21 pass, parity with no exclusion

RED → GREEN

  • RED commit: 1df576f test: Expect a normalized timespec on a whole second above the base. On master, ctest -R test_platform fails 1 of 5 (test_platform_time: test_whole_second_above_the_base) in the C lane (ENABLE_RUST_LIBOBS=OFF) and the libobs-with-rust lane (ON), each against both libobs and libobs-with-rust. cargo test -p obs-util --lib platform_time: 18 passed, 3 failed.
  • GREEN: 303d764. ctest -R test_platform 5/5 in both lanes, against both libraries, none skipped; exported symbols of the two libraries identical (1766). cargo test -p obs-util: 637/637. cargo clippy --workspace --all-targets -- -D warnings: clean. uv run ci/local_gate.py --lane lint: ok.
  • Real app: no UI or log change. The BPM string is covered by the table in bug: util: os_nstime_to_timespec can return tv_nsec == 1000000000 #1505; the fix makes tv_nsec stay below 1000000000, which is what render_metrics_time() assumes.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • User-visible change: its line in docs/features.md is added (Fixed upstream bugs, Encoding, streaming and recording)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): built with scripting ON (a flag change under cmake/ or build-aux/ runs the full builds) and cross-checked with clang and MSVC (not applicable: no flag change)
  • Intentional differences from C listed in the parity test header and rust/README.md (ports) (none now: the README row drops the kept carry)

@rdoupe
rdoupe requested a review from zackees as a code owner October 20, 2026 07:32
@zackees
zackees enabled auto-merge October 20, 2026 07:41
@zackees
zackees disabled auto-merge October 20, 2026 07:41
@zackees

zackees commented Oct 20, 2026

Copy link
Copy Markdown
Member

Coordinator: not queueing this one myself. It changes the expectation of the existing C test test_whole_second_above_the_base, which AGENTS.md reserves for the maintainer's sign-off. @zackees, the change itself looks deliberate and contained (carry a whole second with >= in both C and Rust); queue it when you approve.

@rdoupe

rdoupe commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Confirming: the test change is deliberate. test_whole_second_above_the_base was added by #1506 to pin the current carry as characterized, not endorsed (#1505). This PR flips that one expectation to the normalized {sec, 0} and adds the ±1 ns neighbours, together with the matching Tier 1 cases. No other existing test changes, and C and Rust change together, so Tier 3 parity needs no exclusion. Holding for maintainer sign-off as noted above.

The characterization tests for os_nstime_to_timespec (obs-rust#262 slice 6) pinned
an odd case as "characterized, not endorsed": above the base, a result that
lands on a whole second comes back as tv_nsec == 1000000000 with tv_sec one
short, because the carry tests nsecs > 1000000000 where >= was meant. That is
not a valid struct timespec (tv_nsec must be below 1000000000).

This deliberately changes those characterization tests to expect the
normalized result, {sec, 0}, since the next commit fixes the carry:

- test_whole_second_above_the_base (and its Tier 1 mirror
  whole_second_above_the_base) expect {sec, 0} and {sec + 7, 0}, and pin
  one nanosecond below ({sec - 1, 999999999}) and above ({sec, 1}).
- check() no longer special-cases a whole second above the base, and
  test_around_the_base no longer accepts either form.
- The Tier 1 fixed-base cases (carry_and_borrow_with_a_fixed_base,
  negative_wall_seconds) expect the carried second, and the fixed base
  also pins one nanosecond below the edge.

On master, test_platform_time fails test_whole_second_above_the_base
(8 of 9 pass) against libobs and libobs-with-rust in both lanes, and the
obs-util platform_time tests fail 3 of 21.

Refs obs-rust#1505
Above the base, os_nstime_to_timespec adds the base's tv_nsec and the
offset's nanoseconds, which sum to anything from 0 to 1999999998, and
carried only when the sum was greater than 1000000000. A sum of exactly
1000000000 was returned as tv_nsec == 1000000000 with tv_sec one short,
which is not a valid struct timespec. That happens for every timestamp
whose wall time is a whole second, unless the base's own wall time was one.

Carry with >= in the C (libobs/util/platform-time.c.h) and in the Rust
core (obs-util::platform_time), so the result is always normalized, as it
already was below the base.

The only caller in the tree is shared/bpm (Broadcast Performance Metrics
on Enhanced Broadcasting multitrack video). It prints tv_sec with %T and
tv_nsec / 1000000 with .%03ld, so a frame at ...:33.000 was sent as
...:32.1000Z, 900 ms early. A plugin passing the result to
pthread_cond_timedwait or nanosleep would get EINVAL.

Upstream OBS has the same code in libobs/util/platform.c (no upstream
issue). The Tier 3 parity test compares the Rust core with the same C
file, so the C and the Rust change together and need no exclusion; the
whole_second_edge case now expects {101, 0} from both and checks one
nanosecond either side. rust/README.md no longer lists the carry as kept.

Fixes obs-rust#1505
@zackees
zackees force-pushed the fix/nstime-whole-second branch from 303d764 to 1361e61 Compare October 20, 2026 10:12
@zackees
zackees added this pull request to the merge queue Oct 20, 2026
@zackees

zackees commented Oct 20, 2026

Copy link
Copy Markdown
Member

Coordinator, approved on the maintainer's instruction: this fixes a real off-by-one (a whole-second result comes back as tv_nsec == 1000000000, an invalid timespec; BPM SEI timestamps print 900 ms early). The only C test it changes is the characterization from #1506, marked "characterized, not endorsed", and it gets stricter (every result normalized), with C and Rust changed together. Queued.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: util: os_nstime_to_timespec can return tv_nsec == 1000000000

2 participants