feat(libwebrtc): implement webrtc::AudioEncoder::Encode #17703

Merged
LeandroLP23 merged 1 commit from upstream-pr/17703 into main 2026-11-12 04:57:38 +00:00
LeandroLP23 commented 2026-11-09 10:57:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-11-09T10:57:56Z.


What

Implements webrtc::AudioEncoder::Encode in libwebrtc, adapted from audio_encoder.cc. The WebRTC notice and BSD licence head the file. Encode is the non-virtual entry point a title calls on its own AudioEncoder subclass, so this reaches titles that implement EncodeImpl themselves.

  • Same code at both bounding revisions.
    • Encode is identical at M127 (e0b28a6a81) and M134 (8d78f5de6c).
    • The virtual order of audio_encoder.h is the same at both; the headers differ only in absl::optional versus std::optional and commented-out parameter names.
    • array_view.h differs only by an added <cstddef> include and commented-out parameter names.
  • Virtual slots: NumChannels is slot 3, SampleRateHz slot 2 and EncodeImpl slot 29, counted after the two destructor entries. #17269 uses slot 21 for OnReceivedUplinkBandwidth by the same count.
  • What Encode does:
    • Calls NumChannels and then SampleRateHz, the left-to-right order Clang uses for NumChannels() * SampleRateHz().
    • Calls EncodeImpl, passing the caller's result storage, this, the timestamp, the ArrayView as pointer and size (as RmsLevel::Analyze takes it) and the rtc::Buffer*.
    • Returns that storage. EncodedInfo holds a std::vector, so it is returned through hidden storage and its vector is never touched here.
  • Where upstream's RTC_CHECK_EQs stop the process, this throws std::runtime_error:
    • before encoding, when the audio is not NumChannels() * SampleRateHz() / 100 samples;
    • after encoding, when the bytes EncodeImpl appended to the buffer differ from encoded_bytes, the size_t at offset 0 of the result.
  • Null arguments: a null result, encoder, vtable, virtual function or output buffer throws std::invalid_argument.
  • Not emitted: TRACE_EVENT0.

rtc::Buffer is modelled in include/Buffer.hpp, byte-identical to the file #17698 adds, so the two merge cleanly in either order.

Tested

Windows 11, MinGW GCC Release build. guest_webrtc_audio_encoder_encode is new; guest_webrtc_audio_encoder and guest_webrtc_rms_level still pass.

The new test drives a fake encoder with a 30-slot vtable. It checks:

  • the call order, and every argument forwarded to EncodeImpl;
  • an empty packet;
  • a wrong sample count, which throws before EncodeImpl and before the null output buffer is reached;
  • a size mismatch measured from a non-empty buffer;
  • every null case.

Negative controls: moving EncodeImpl to slot 28, or dividing by 1000, both fail the test.

tools/check_conventions.py --base origin/main passes.

tools/pr_overlap.py --branch reported #15609 and #16381 next to my CMake and include lines. I moved both, and the branch now merges cleanly with each of the 29 open libwebrtc pull requests that merge with main, and with #17698 in either order.

#14212, #15189 and #17691 already conflict with main:

  • #14212 respaces the debt file.
  • #15189 implements ColorSpace.
  • #17691 deletes the whole tree.

None touches Encode.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5222761148 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17703). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-11-09T10:57:56Z. --- ### What Implements `webrtc::AudioEncoder::Encode` in libwebrtc, adapted from [audio_encoder.cc](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100/api/audio_codecs/audio_encoder.cc). The WebRTC notice and BSD licence head the file. `Encode` is the non-virtual entry point a title calls on its own `AudioEncoder` subclass, so this reaches titles that implement `EncodeImpl` themselves. - **Same code at both bounding revisions.** - `Encode` is identical at M127 ([e0b28a6a81](https://webrtc.googlesource.com/src/+/e0b28a6a81a989c1f5c89e30fcd247870047390d)) and M134 ([8d78f5de6c](https://webrtc.googlesource.com/src/+/8d78f5de6c27b2c793039989ea381f1428fb0100)). - The virtual order of `audio_encoder.h` is the same at both; the headers differ only in `absl::optional` versus `std::optional` and commented-out parameter names. - `array_view.h` differs only by an added `<cstddef>` include and commented-out parameter names. - **Virtual slots:** `NumChannels` is slot 3, `SampleRateHz` slot 2 and `EncodeImpl` slot 29, counted after the two destructor entries. #17269 uses slot 21 for `OnReceivedUplinkBandwidth` by the same count. - **What `Encode` does:** - Calls `NumChannels` and then `SampleRateHz`, the left-to-right order Clang uses for `NumChannels() * SampleRateHz()`. - Calls `EncodeImpl`, passing the caller's result storage, `this`, the timestamp, the `ArrayView` as pointer and size (as `RmsLevel::Analyze` takes it) and the `rtc::Buffer*`. - Returns that storage. `EncodedInfo` holds a `std::vector`, so it is returned through hidden storage and its vector is never touched here. - **Where upstream's `RTC_CHECK_EQ`s stop the process, this throws `std::runtime_error`:** - before encoding, when the audio is not `NumChannels() * SampleRateHz() / 100` samples; - after encoding, when the bytes `EncodeImpl` appended to the buffer differ from `encoded_bytes`, the `size_t` at offset 0 of the result. - **Null arguments:** a null result, encoder, vtable, virtual function or output buffer throws `std::invalid_argument`. - **Not emitted:** `TRACE_EVENT0`. `rtc::Buffer` is modelled in `include/Buffer.hpp`, byte-identical to the file #17698 adds, so the two merge cleanly in either order. ### Tested Windows 11, MinGW GCC Release build. `guest_webrtc_audio_encoder_encode` is new; `guest_webrtc_audio_encoder` and `guest_webrtc_rms_level` still pass. The new test drives a fake encoder with a 30-slot vtable. It checks: - the call order, and every argument forwarded to `EncodeImpl`; - an empty packet; - a wrong sample count, which throws before `EncodeImpl` and before the null output buffer is reached; - a size mismatch measured from a non-empty buffer; - every null case. Negative controls: moving `EncodeImpl` to slot 28, or dividing by 1000, both fail the test. `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` reported #15609 and #16381 next to my CMake and include lines. I moved both, and the branch now merges cleanly with each of the 29 open libwebrtc pull requests that merge with `main`, and with #17698 in either order. #14212, #15189 and #17691 already conflict with `main`: - #14212 respaces the debt file. - #15189 implements ColorSpace. - #17691 deletes the whole tree. None touches `Encode`. ### 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 (Claude Code)
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!17703
No description provided.