feat(libwebrtc): implement rtc::RateTracker #16381

Merged
AlexWhitehouse merged 2 commits from upstream-pr/16381 into main 2026-11-10 04:36:00 +00:00
AlexWhitehouse commented 2026-11-06 13:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-06T13:57:55Z.


What

Implements libwebrtc rtc::RateTracker's four exports, which threw through NotImplemented_nid_no_patch:

Export NID
rtc::RateTracker::RateTracker(int64_t, size_t) (_ZN3rtc11RateTrackerC1Elm) o-7JqHJukNc
rtc::RateTracker::~RateTracker() (_ZN3rtc11RateTrackerD1Ev) 03MioCxwz-g
rtc::RateTracker::AddSamples(int64_t) (_ZN3rtc11RateTracker10AddSamplesEl) BMS8p-tBcTU
rtc::RateTracker::ComputeRateForInterval(int64_t) const (_ZNK3rtc11RateTracker22ComputeRateForIntervalEl) HVudf31CXkc

Source: rtc_base/rate_tracker.cc and rate_tracker.h at M134. Both files are byte-identical at M127, the other end of the revision range in the debt file's "Source revision" entry. src/RateTracker.cpp starts with rate_tracker.cc's notice copied byte for byte, plus the WebRTC BSD licence, as the other libwebrtc ports do. The bodies follow upstream line for line:

  • AddSamples reads Time() and calls EnsureInitialized, then advances and clears at most bucket_count + 1 buckets, realigns the bucket start after a full expiry, and adds to the current bucket and the total.
  • ComputeRateForInterval caps the interval at the window, skips expired buckets, counts the oldest bucket in proportion (with upstream's rounding), and returns 0 during the first bucket after initialization.

ABI, from the header:

  • Layout: a 64-byte object: the vptr, then bucket_milliseconds_, bucket_count_, sample_buckets_, total_sample_count_, current_bucket_, bucket_start_time_milliseconds_ and initialization_time_milliseconds_ at offsets 8 to 56. include/RateTracker.hpp checks every offset with a static_assert.
  • Allocation: sample_buckets_ is new int64_t[bucket_count + 1] upstream. It is allocated and freed through the guest's operator new[] and operator delete[] (_Znam, _ZdaPv), as libc's StdioExtras.cpp calls them, so guest code that owns or frees the object stays consistent. libwebrtc now links libc for them.
  • Vtable: Time() is a protected virtual that upstream calls through the vptr, so a guest subclass can override it. The constructor installs a vtable with the Itanium slot order of the header's virtuals: ~RateTracker (complete and deleting), then Time, which defaults to this module's existing rtc::TimeMillis. Time is called through the object's vptr with APS5_VABI.
    • The module's own RateTracker vtable and typeinfo are not among the 31 libwebrtc data exports, so the table is local. Its typeinfo slot is null, so RTTI on these objects is unsupported, as in libSceNpCommon.
  • Checks: upstream's RTC_CHECKs on a non-positive bucket length or a zero bucket count abort, so those throw here. So does a bucket count whose array size overflows. RTC_DCHECK_LE(0, sample_count) is debug-only and is left out, as in a release build.
  • Debt: a new entry under "Public source implementations" in docs/dev/debt/libwebrtc.md records these choices and that nothing is compared with the console. The inventory count drops from 339 to 335; all four exports had names, so the unnamed figure stays 60.

Tested

  • New registered ctest guest_webrtc_rate_tracker (core/libs/prx/libwebrtc/tests/RateTracker.cpp). A test vtable whose Time slot returns a controlled clock drives the exports, so there are no sleeps.
    • Upstream cases: it ports rate_tracker_unittest.cc: 30 and 60 FPS, basics (0 before a full bucket, 12340 after 1234 samples in 100 ms, partial-window and full-expiry values), a long period between samples, rolloff, the first second after an initial value, and counts above 2^32.
    • ABI checks: the guest allocator receives the 88-byte array for 10 buckets; fields the constructor leaves unset stay untouched; guard bytes on both sides of the object survive; the default Time reads rtc::TimeMillis; and both destructors free the array (and the object) through the guest allocator. Invalid constructor parameters throw.
  • Linux x86-64 (Ubuntu 26.04, GCC 15, Release), the libwebrtc group:
    • against main's library, 17 existing tests pass and the new test fails with _ZN3rtc11RateTrackerC1Elm_nid_postfix not implemented;
    • with this PR (533e376f51), 18/18 pass, and public_headers passes.
  • Mutation check: removing the partial-bucket rounding term fails with libwebrtc RateTracker check failed at line 279.
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): ctest -R webrtc passes 18/18 at 533e376f51, with no warnings in the new files.
    • The new test can't be built against main there: the header it uses doesn't exist on main, and main declares the stubs as (void).
    • The Linux negative control above ran the test against main's library.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed.
  • python3 tools/pr_overlap.py --branch feat/libwebrtc-rate-tracker: no open PR implements these exports. #15154 lists RateTracker among the families it leaves unimplemented, and merges cleanly with this branch.
    • Shared lines: #15265 (NetworkManager declarations right after these in Export.cpp/Api.hpp) and my #15766 merge cleanly in code. They conflict only on the inventory count line in docs/dev/debt/libwebrtc.md.
    • Count line: that line is also shared with #15189–#15197, #15261–#15272, #15275–#15277 and #15609. Whichever merges second recounts.
    • Uncomparable PRs: #15211, #15285 and #14212 already conflict with main and keep only those conflicts.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5188928419 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16381). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-06T13:57:55Z. --- ### What Implements libwebrtc `rtc::RateTracker`'s four exports, which threw through `NotImplemented_nid_no_patch`: | Export | NID | |---|---| | `rtc::RateTracker::RateTracker(int64_t, size_t)` (`_ZN3rtc11RateTrackerC1Elm`) | `o-7JqHJukNc` | | `rtc::RateTracker::~RateTracker()` (`_ZN3rtc11RateTrackerD1Ev`) | `03MioCxwz-g` | | `rtc::RateTracker::AddSamples(int64_t)` (`_ZN3rtc11RateTracker10AddSamplesEl`) | `BMS8p-tBcTU` | | `rtc::RateTracker::ComputeRateForInterval(int64_t) const` (`_ZNK3rtc11RateTracker22ComputeRateForIntervalEl`) | `HVudf31CXkc` | **Source:** [`rtc_base/rate_tracker.cc`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/rate_tracker.cc) and [`rate_tracker.h`](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/rate_tracker.h) at M134. Both files are byte-identical at [M127](https://webrtc.googlesource.com/src/+/e0b28a6a81a989c1f5c89e30fcd247870047390d/rtc_base/rate_tracker.cc), the other end of the revision range in the debt file's "Source revision" entry. `src/RateTracker.cpp` starts with `rate_tracker.cc`'s notice copied byte for byte, plus the WebRTC BSD licence, as the other libwebrtc ports do. The bodies follow upstream line for line: - `AddSamples` reads `Time()` and calls `EnsureInitialized`, then advances and clears at most `bucket_count + 1` buckets, realigns the bucket start after a full expiry, and adds to the current bucket and the total. - `ComputeRateForInterval` caps the interval at the window, skips expired buckets, counts the oldest bucket in proportion (with upstream's rounding), and returns 0 during the first bucket after initialization. **ABI**, from the header: - **Layout:** a 64-byte object: the vptr, then `bucket_milliseconds_`, `bucket_count_`, `sample_buckets_`, `total_sample_count_`, `current_bucket_`, `bucket_start_time_milliseconds_` and `initialization_time_milliseconds_` at offsets 8 to 56. `include/RateTracker.hpp` checks every offset with a `static_assert`. - **Allocation:** `sample_buckets_` is `new int64_t[bucket_count + 1]` upstream. It is allocated and freed through the guest's `operator new[]` and `operator delete[]` (`_Znam`, `_ZdaPv`), as libc's `StdioExtras.cpp` calls them, so guest code that owns or frees the object stays consistent. libwebrtc now links libc for them. - **Vtable:** `Time()` is a protected virtual that upstream calls through the vptr, so a guest subclass can override it. The constructor installs a vtable with the Itanium slot order of the header's virtuals: `~RateTracker` (complete and deleting), then `Time`, which defaults to this module's existing `rtc::TimeMillis`. `Time` is called through the object's vptr with `APS5_VABI`. - The module's own `RateTracker` vtable and typeinfo are not among the 31 libwebrtc data exports, so the table is local. Its typeinfo slot is null, so RTTI on these objects is unsupported, as in libSceNpCommon. - **Checks:** upstream's `RTC_CHECK`s on a non-positive bucket length or a zero bucket count abort, so those throw here. So does a bucket count whose array size overflows. `RTC_DCHECK_LE(0, sample_count)` is debug-only and is left out, as in a release build. - **Debt:** a new entry under "Public source implementations" in `docs/dev/debt/libwebrtc.md` records these choices and that nothing is compared with the console. The inventory count drops from 339 to 335; all four exports had names, so the unnamed figure stays 60. ### Tested - **New registered ctest `guest_webrtc_rate_tracker`** (`core/libs/prx/libwebrtc/tests/RateTracker.cpp`). A test vtable whose `Time` slot returns a controlled clock drives the exports, so there are no sleeps. - **Upstream cases:** it ports `rate_tracker_unittest.cc`: 30 and 60 FPS, basics (0 before a full bucket, 12340 after 1234 samples in 100 ms, partial-window and full-expiry values), a long period between samples, rolloff, the first second after an initial value, and counts above 2^32. - **ABI checks:** the guest allocator receives the 88-byte array for 10 buckets; fields the constructor leaves unset stay untouched; guard bytes on both sides of the object survive; the default `Time` reads `rtc::TimeMillis`; and both destructors free the array (and the object) through the guest allocator. Invalid constructor parameters throw. - **Linux x86-64** (Ubuntu 26.04, GCC 15, Release), the libwebrtc group: - against `main`'s library, 17 existing tests pass and the new test fails with `_ZN3rtc11RateTrackerC1Elm_nid_postfix not implemented`; - with this PR (533e376f51), 18/18 pass, and `public_headers` passes. - **Mutation check:** removing the partial-bucket rounding term fails with `libwebrtc RateTracker check failed at line 279`. - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release): `ctest -R webrtc` passes 18/18 at 533e376f51, with no warnings in the new files. - The new test can't be built against `main` there: the header it uses doesn't exist on `main`, and `main` declares the stubs as `(void)`. - The Linux negative control above ran the test against `main`'s library. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. - **`python3 tools/pr_overlap.py --branch feat/libwebrtc-rate-tracker`:** no open PR implements these exports. #15154 lists `RateTracker` among the families it leaves unimplemented, and merges cleanly with this branch. - **Shared lines:** #15265 (NetworkManager declarations right after these in `Export.cpp`/`Api.hpp`) and my #15766 merge cleanly in code. They conflict only on the inventory count line in `docs/dev/debt/libwebrtc.md`. - **Count line:** that line is also shared with #15189–#15197, #15261–#15272, #15275–#15277 and #15609. Whichever merges second recounts. - **Uncomparable PRs:** #15211, #15285 and #14212 already conflict with `main` and keep only those conflicts. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ahmadmuj commented 2026-11-06 15:32:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-11-06T15:32:13Z.

Review: COMMENTED


Checked head 533e376f517a7a54372bdaa2057cb5a77bf5bf42, PR base d60ebec0c292807398de2130294cac8ae0c1b862, and main 492ed5c5fe5d476257ab52fa8e09cf7c343c8200.

The odd-width path matches WebRTC's pinned source. I checked this through the actual exact-head CMake libwebrtc and guest libc, with an isolated RateTrackerOddWindow044.cpp probe.

Setup: Ubuntu 24.04.2 under WSL2, x86-64 i7-13700K, GCC 13.3.0, Release. The probe uses the declared exports, APS5_VABI, and a controlled Time slot. dladdr confirms all tracker and allocator calls resolve to the built modules.

With a 3 ms bucket, count 1, and one sample at time 0, interval 3 returns:

Time (ms) Exact expected double
0, 1, 2 0.0
3, 4 1000.0 / 3.0
5, 6 0.0

Adding seven samples at time 6 gives 7000.0 / 3.0 at time 9. All assertions pass. The probe verifies binary64 and FE_TONEAREST, uses no fast-math, and compares exact divisions rather than decimal approximations.

For a control, I changed only the production rounding term from bucketMilliseconds >> 1 to (bucketMilliseconds + 1) >> 1. The new probe fails at time 5: it gets 1000.0 / 3.0 instead of zero. The unchanged guest_webrtc_rate_tracker fixture still passes with that mutation. Its valid constructions use 100 ms buckets, where both terms equal 50.

Object/allocation guards, initialized bucket state, one 16-byte allocation, one matching free, and direct D1 cleanup pass, including the failing control. Each run uses a fresh process. After restoring production bytes and rebuilding, both tests pass again. Source and artifact hashes match the original build. The mutant build also refreshes libc/libkernel's generated modified-state metadata.

Commands used, with W the exact-head worktree, B its build, E the probe directory, and L=$B/core/libs/libs/unpatched:

cmake -S "$W" -B "$B" -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DFFMPEG_PREBUILT_DIR=/home/ahmad/work/anyps5/builds/main/externals/ffmpeg-9ac4cfd195f1
cmake --build "$B" --target libwebrtc guest_webrtc_rate_tracker_tests -j 16
g++ -std=c++20 -O2 -fno-fast-math -fno-reciprocal-math -frounding-math -fPIE -pie -I"$W/core/libs" "$E/RateTrackerOddWindow044.cpp" -Wl,--no-as-needed "$L/libwebrtc.prx" "$L/libc.prx" -Wl,-rpath,"$L" -Wl,-rpath-link,"$L" -ldl -o "$B/RateTrackerOddWindow044"
ctest --test-dir "$B" -R '^guest_webrtc_rate_tracker$' -V
"$B/RateTrackerOddWindow044" "$L"
python3 tools/check_conventions.py --base 492ed5c5fe5d476257ab52fa8e09cf7c343c8200

Conventions pass. Each fixture/probe invocation finishes below 0.12 seconds. Builds and tests run without credential variables; builds hold the shared build lock. No full product build runs.

This adds coverage beyond removing the rounding term or repeating the even-width fixture. The current head passes; I found no defect in this boundary. Main's four exports remain throwing stubs by source inspection; no main runtime result is claimed. Console layout, dispatch, and behavior remain unverified. GPUs tested: n/a. AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5802512854 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16381#pullrequestreview-5802512854). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-11-06T15:32:13Z. **Review:** COMMENTED --- Checked head `533e376f517a7a54372bdaa2057cb5a77bf5bf42`, PR base `d60ebec0c292807398de2130294cac8ae0c1b862`, and main `492ed5c5fe5d476257ab52fa8e09cf7c343c8200`. The odd-width path matches [WebRTC's pinned source](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/rate_tracker.cc). I checked this through the actual exact-head CMake `libwebrtc` and guest `libc`, with an isolated `RateTrackerOddWindow044.cpp` probe. Setup: Ubuntu 24.04.2 under WSL2, x86-64 i7-13700K, GCC 13.3.0, Release. The probe uses the declared exports, `APS5_VABI`, and a controlled Time slot. `dladdr` confirms all tracker and allocator calls resolve to the built modules. With a 3 ms bucket, count 1, and one sample at time 0, interval 3 returns: | Time (ms) | Exact expected double | | --- | --- | | 0, 1, 2 | `0.0` | | 3, 4 | `1000.0 / 3.0` | | 5, 6 | `0.0` | Adding seven samples at time 6 gives `7000.0 / 3.0` at time 9. All assertions pass. The probe verifies binary64 and `FE_TONEAREST`, uses no fast-math, and compares exact divisions rather than decimal approximations. For a control, I changed only the production rounding term from `bucketMilliseconds >> 1` to `(bucketMilliseconds + 1) >> 1`. The new probe fails at time 5: it gets `1000.0 / 3.0` instead of zero. The unchanged `guest_webrtc_rate_tracker` fixture still passes with that mutation. Its valid constructions use 100 ms buckets, where both terms equal 50. Object/allocation guards, initialized bucket state, one 16-byte allocation, one matching free, and direct D1 cleanup pass, including the failing control. Each run uses a fresh process. After restoring production bytes and rebuilding, both tests pass again. Source and artifact hashes match the original build. The mutant build also refreshes libc/libkernel's generated modified-state metadata. Commands used, with `W` the exact-head worktree, `B` its build, `E` the probe directory, and `L=$B/core/libs/libs/unpatched`: ```sh cmake -S "$W" -B "$B" -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DFFMPEG_PREBUILT_DIR=/home/ahmad/work/anyps5/builds/main/externals/ffmpeg-9ac4cfd195f1 cmake --build "$B" --target libwebrtc guest_webrtc_rate_tracker_tests -j 16 g++ -std=c++20 -O2 -fno-fast-math -fno-reciprocal-math -frounding-math -fPIE -pie -I"$W/core/libs" "$E/RateTrackerOddWindow044.cpp" -Wl,--no-as-needed "$L/libwebrtc.prx" "$L/libc.prx" -Wl,-rpath,"$L" -Wl,-rpath-link,"$L" -ldl -o "$B/RateTrackerOddWindow044" ctest --test-dir "$B" -R '^guest_webrtc_rate_tracker$' -V "$B/RateTrackerOddWindow044" "$L" python3 tools/check_conventions.py --base 492ed5c5fe5d476257ab52fa8e09cf7c343c8200 ``` Conventions pass. Each fixture/probe invocation finishes below 0.12 seconds. Builds and tests run without credential variables; builds hold the shared build lock. No full product build runs. This adds coverage beyond removing the rounding term or repeating the even-width fixture. The current head passes; I found no defect in this boundary. Main's four exports remain throwing stubs by source inspection; no main runtime result is claimed. Console layout, dispatch, and behavior remain unverified. GPUs tested: n/a. AI-assisted: yes.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!16381
No description provided.