Prefer NVENC for WebRTC H264 encoding#2329
Open
balthazur wants to merge 7 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.
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.Note
Medium Risk
Medium risk because it monkey-patches
aiortc.codecs.h264.H264Encoder._encode_frame, which can affect all WebRTC H.264 sessions and may behave differently across aiortc/PyAV/FFmpeg builds and GPU availability.Overview
WebRTC H.264 encoding now prefers NVIDIA hardware encode when available. A new
prefer_h264_nvenc_encoder()patch overridesaiortc’sH264Encoder._encode_frameto tryh264_nvencfirst (with several option presets) and transparently fall back to the originallibx264path if NVENC can’t be opened or errors during encode.The patch also handles runtime changes by recreating/resetting the encoder on resolution changes, attempting bitrate updates on significant target bitrate shifts, and emitting scoped
[WEBRTC_NVENC]logs for selection and fallback. Both the stream manager and worker WebRTC entrypoints now callprefer_h264_nvenc_encoder()at import time so the behavior applies to all H.264 WebRTC outputs.Reviewed by Cursor Bugbot for commit 2ed2809. Bugbot is set up for automated code reviews on this repo. Configure here.