Prefer NVENC for WebRTC H264 encoding#2329
Open
balthazur wants to merge 8 commits into
Open
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 6a9096f. Configure here.
Collaborator
|
lets measure this, CPU->GPU->CPU round trip might make this actually slower |
Contributor
Author
@grzegorz-roboflow what exactly do you want to measure? So far, I measured the raw encoding time. Let me know, I can run a comparison |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
h264_nvencfor aiortc H.264 WebRTC encoding when available.libx264encoder when NVENC is unavailable or fails.[WEBRTC_NVENC]logs for patch activation, encoder selection, fallback, and resolution-triggered encoder recreation.Why
Modal GPU workers run on NVIDIA GPUs, so H.264 hardware encoding can reduce server-side encode latency without changing the negotiated browser codec. H.264 stays the WebRTC-compatible path; this does not introduce H.265/HEVC or bitrate/congestion-control changes.
Testing
python3 -m py_compile inference/core/interfaces/webrtc_worker/h264_nvenc.py inference/core/interfaces/webrtc_worker/webrtc.py inference/core/interfaces/stream_manager/manager_app/webrtc.pyvenvandaiortc 1.14.0: confirmedh264_nvencfallback tolibx264on a non-NVIDIA Mac still produces H.264 packets.git diff --checkStaging benchmark
libx264h264_nvencIn staging, software
libx264encoding took roughly 14-17 ms per 1080p frame. Withh264_nvenc, sampled encode time dropped to about 2.6 ms per frame, roughly a 5-6x improvement. This does not by itself solve all WebRTC congestion/ramp-up behavior, but it removes server-side H.264 encoding as a major bottleneck on NVIDIA GPU Modal workers.For comparison, I also tested VP8: backend VP8 encode sampled around 10-17 ms/frame, while browser-side encode/decode stayed in the same low-ms range as H.264, so VP8 did not look like the better backend output codec.
Note
Medium Risk
Monkey-patches
aiortc'sH264Encoderto prefer GPU NVENC, which can affect WebRTC video stability/compatibility and may surface runtime/driver-specific failures despite the fallback path.Overview
Prefers NVIDIA NVENC for WebRTC H.264 video encoding when available.
Adds
h264_nvencsupport via a runtime patch (prefer_h264_nvenc_encoder) that overridesaiortc.codecs.h264.H264Encoder._encode_frameto try opening an NVENCav.CodecContext, update bitrate, recreate the encoder on resolution change, and fall back to the originallibx264path on unavailability or encode errors (with[WEBRTC_NVENC]scoped logs).Enables this behavior by invoking
prefer_h264_nvenc_encoder()at import time in both the stream manager WebRTC app and the WebRTC worker entrypoints.Reviewed by Cursor Bugbot for commit 8805940. Bugbot is set up for automated code reviews on this repo. Configure here.