feat(libwebrtc): release the frame buffer in the VideoFrame::Builder destructor #17419

Merged
primesoul-web merged 1 commit from upstream-pr/17419 into main 2026-11-13 05:33:59 +00:00
primesoul-web commented 2026-11-08 19:17:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by primesoul-web on 2026-11-08T19:17:43Z.


What

webrtc::VideoFrame::Builder's destructor threw through NotImplemented_nid_no_patch for every builder that held a frame buffer, so a VideoFrame built from a buffer stopped the process. It now releases that reference through the buffer's virtual Release, at slot 1 of the guest vtable: rtc::RefCountInterface declares AddRef and then Release in ref_count.h, and VideoFrameBuffer inherits that pair before its own virtuals. The vtable pointer is read with GuestCopyIn_nid_no_patch, so an unreadable buffer throws instead of faulting, and a buffer whose Release slot is null throws too.

Packet infos still throw: packet_infos_ is a guest std::vector<RtpPacketInfo> whose elements own guest allocations, and the host cannot run the guest destructor of an element. Upstream destroys members in reverse declaration order, so that throw comes before the buffer is released, as it does here. The libwebrtc technical debt entry is cut to that remaining gap.

Tested

No console and no title. The project build was not run here: this machine has no CMake and no C++ toolchain for the project's build, so ctest was not run and the first project build of this change is the reviewer's CI run.

What was run: the changed src/VideoFrameBuilder.cpp and the updated tests/VideoFrameBuilder.cpp compiled as C++20 with -Wall -Wextra -Werror against the repository headers, with GuestCopyIn_nid_no_patch and NotImplemented_nid_no_patch stubbed, on Linux x86-64, and the resulting test binary passed. The new checks cover: a builder holding a buffer calls Release once with the buffer pointer and changes no other field; a builder without a buffer calls nothing; a buffer whose Release slot is null throws std::invalid_argument; packet infos still throw std::runtime_error; a null builder throws std::invalid_argument.

python3 tools/check_conventions.py --base origin/main passes. python3 tools/pr_overlap.py --branch feat/libwebrtc-builder-destructor-release reports no open pull request changing the same lines or exports.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5214316416 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17419). Originally posted by [primesoul-web](https://github.com/primesoul-web) on 2026-11-08T19:17:43Z. --- ### What `webrtc::VideoFrame::Builder`'s destructor threw through `NotImplemented_nid_no_patch` for every builder that held a frame buffer, so a `VideoFrame` built from a buffer stopped the process. It now releases that reference through the buffer's virtual `Release`, at slot 1 of the guest vtable: `rtc::RefCountInterface` declares `AddRef` and then `Release` in [ref_count.h](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/rtc_base/ref_count.h), and `VideoFrameBuffer` inherits that pair before its own virtuals. The vtable pointer is read with `GuestCopyIn_nid_no_patch`, so an unreadable buffer throws instead of faulting, and a buffer whose `Release` slot is null throws too. Packet infos still throw: `packet_infos_` is a guest `std::vector<RtpPacketInfo>` whose elements own guest allocations, and the host cannot run the guest destructor of an element. Upstream destroys members in reverse declaration order, so that throw comes before the buffer is released, as it does here. The `libwebrtc` technical debt entry is cut to that remaining gap. ### Tested No console and no title. The project build was not run here: this machine has no CMake and no C++ toolchain for the project's build, so `ctest` was not run and the first project build of this change is the reviewer's CI run. What was run: the changed `src/VideoFrameBuilder.cpp` and the updated `tests/VideoFrameBuilder.cpp` compiled as C++20 with `-Wall -Wextra -Werror` against the repository headers, with `GuestCopyIn_nid_no_patch` and `NotImplemented_nid_no_patch` stubbed, on Linux x86-64, and the resulting test binary passed. The new checks cover: a builder holding a buffer calls `Release` once with the buffer pointer and changes no other field; a builder without a buffer calls nothing; a buffer whose `Release` slot is null throws `std::invalid_argument`; packet infos still throw `std::runtime_error`; a null builder throws `std::invalid_argument`. `python3 tools/check_conventions.py --base origin/main` passes. `python3 tools/pr_overlap.py --branch feat/libwebrtc-builder-destructor-release` reports no open pull request changing the same lines or exports. ### Checklist - [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
gabichuuuuu commented 2026-11-08 19:39:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T19:39:17Z.

Review: COMMENTED


Windows run of head 704ae38ace, merged onto main 095ff72367. To save builds, this ran in one tree together with 24 other open PRs that touch different code; all 25 merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.

  • guest_webrtc_video_frame_builder with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's VideoFrameBuilder.cpp and this PR's test: 5 / 5 runs fail with VideoFrame::Builder destructor with a buffer or packet infos: releasing guest references not implemented.

From reading: Release is slot 1 of rtc::RefCountInterface, after AddRef, so vtable[1] is the right entry. Throwing for packet infos before the buffer is released also matches the destruction order: packet_infos_ is declared after video_frame_buffer_ in video_frame.h, so it's destroyed first.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5819365348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17419#pullrequestreview-5819365348). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T19:39:17Z. **Review:** COMMENTED --- Windows run of head `704ae38ace`, merged onto `main` `095ff72367`. To save builds, this ran in one tree together with 24 other open PRs that touch different code; all 25 merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (`x86_64-ucrt-posix-seh`), Release, `BUILD_TESTING=ON`. - `guest_webrtc_video_frame_builder` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s `VideoFrameBuilder.cpp` and this PR's test: 5 / 5 runs fail with `VideoFrame::Builder destructor with a buffer or packet infos: releasing guest references not implemented`. From reading: `Release` is slot 1 of `rtc::RefCountInterface`, after `AddRef`, so `vtable[1]` is the right entry. Throwing for packet infos before the buffer is released also matches the destruction order: `packet_infos_` is declared after `video_frame_buffer_` in `video_frame.h`, so it's destroyed first.
PrivacyOnion commented 2026-11-12 06:08:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:08:30Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 704ae38ace:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_webrtc_video_frame_builder_tests built cleanly.
  • CTest (guest_webrtc_video_frame_builder):
    1/1 Test #4314: guest_webrtc_video_frame_builder ...   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libwebrtc/src/VideoFrameBuilder.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4314: guest_webrtc_video_frame_builder ...Subprocess aborted***Exception:   1.81 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libwebrtc/src/VideoFrameBuilder.cpp that ReleaseVideoFrameBuffer handles null buffer pointers cleanly, reads the vtable via GuestCopyIn_nid_no_patch, throws std::invalid_argument if reading fails or if the vtable or slot 1 (ReleaseSlot = 1) is null, and dispatches to the virtual function with APS5_VABI passing buffer.
    • Verified in core/libs/prx/libwebrtc/src/VideoFrameBuilder.cpp that ZN6webrtc10VideoFrame7BuilderD1Ev_nid_postfix checks checked.packetInfos before releasing checked.videoFrameBuffer, matching C++ reverse member destruction order (packet_infos declared after video_frame_buffer_ in WebRTC's video_frame.h).
    • Verified in docs/dev/debt/libwebrtc.md that the technical debt entry for VideoFrame::Builder was accurately narrowed to the unhandled packet_infos_ vector destruction gap.
    • Verified in core/libs/prx/libwebrtc/tests/VideoFrameBuilder.cpp that TestDestructorReleasesBuffer, TestDestructorWithoutBufferReleasesNothing, TestDestructorWithoutReleaseThrows, TestDestructorWithPacketInfosThrows, and TestNullDestructorThrows exercise every success, null, and exception branch and are invoked in main().
    • Verified that BufferRelease and ReleaseBuffer use APS5_VABI to guarantee ABI compatibility across Linux (SysV LP64) and Windows (LLP64).
    • Verified strict AGENTS.md compliance: zero comments added to code, no title-specific workarounds or magic constants, and changes are strictly limited to the source file, unit test, and technical debt documentation.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869882927 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17419#pullrequestreview-5869882927). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:08:30Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `704ae38ace`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_webrtc_video_frame_builder_tests` built cleanly. - **CTest** (`guest_webrtc_video_frame_builder`): ``` 1/1 Test #4314: guest_webrtc_video_frame_builder ... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libwebrtc/src/VideoFrameBuilder.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4314: guest_webrtc_video_frame_builder ...Subprocess aborted***Exception: 1.81 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified in core/libs/prx/libwebrtc/src/VideoFrameBuilder.cpp that ReleaseVideoFrameBuffer handles null buffer pointers cleanly, reads the vtable via GuestCopyIn_nid_no_patch, throws std::invalid_argument if reading fails or if the vtable or slot 1 (ReleaseSlot = 1) is null, and dispatches to the virtual function with APS5_VABI passing buffer. - Verified in core/libs/prx/libwebrtc/src/VideoFrameBuilder.cpp that _ZN6webrtc10VideoFrame7BuilderD1Ev_nid_postfix checks checked.packetInfos before releasing checked.videoFrameBuffer, matching C++ reverse member destruction order (packet_infos_ declared after video_frame_buffer_ in WebRTC's video_frame.h). - Verified in docs/dev/debt/libwebrtc.md that the technical debt entry for VideoFrame::Builder was accurately narrowed to the unhandled packet_infos_ vector destruction gap. - Verified in core/libs/prx/libwebrtc/tests/VideoFrameBuilder.cpp that TestDestructorReleasesBuffer, TestDestructorWithoutBufferReleasesNothing, TestDestructorWithoutReleaseThrows, TestDestructorWithPacketInfosThrows, and TestNullDestructorThrows exercise every success, null, and exception branch and are invoked in main(). - Verified that BufferRelease and ReleaseBuffer use APS5_VABI to guarantee ABI compatibility across Linux (SysV LP64) and Windows (LLP64). - Verified strict AGENTS.md compliance: zero comments added to code, no title-specific workarounds or magic constants, and changes are strictly limited to the source file, unit test, and technical debt documentation. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!17419
No description provided.