Skip to content

Fix/video codecs patching#245

Merged
dangusev merged 4 commits into
mainfrom
fix/video-codecs-patching
May 15, 2026
Merged

Fix/video codecs patching#245
dangusev merged 4 commits into
mainfrom
fix/video-codecs-patching

Conversation

@dangusev
Copy link
Copy Markdown
Contributor

@dangusev dangusev commented May 15, 2026

  1. Fixes publishing codec preferences not being applied, so tracks were still able to use vp8.
    Now, only h264 or av1.

  2. Sets "ultrafast" h264 codec preset by default for lowest latency.
    To use the default "medium", pass STREAM_PATCH_AIORTC_H264_PRESET=medium or STREAM_PATCH_AIORTC_H264_PRESET= (empty) env variable.

Summary by CodeRabbit

Release Notes

  • New Features

    • H.264 video encoder presets are now configurable through environment variables, defaulting to "ultrafast" for enhanced performance tuning.
  • Refactoring

    • Improved codec preference handling timing and encoder initialization for more precise video quality configuration.
  • Tests

    • Added test coverage for H.264 preset functionality, including environment variable handling and default value verification.

Review Change Stack

dangusev added 2 commits May 15, 2026 13:08
In PublisherPeerConnection, publish_codec_preferences() was called on init, when the list of transceivers was still empty, so it was not applied, and vp8 codec was still used.
Moved the func call to `addTrack()` to fix that.
- Updated StreamH264Encoder to pass "preset" to codec options
- Added STREAM_PATCH_AIORTC_H264_PRESET env variable with "ultrafast" by default. To reset to default "medium", pass "medium" or empty value
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 15, 2026

Warning

Rate limit exceeded

@dangusev has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 34 minutes and 9 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1ee843b1-e3ae-49c3-b6d8-09674389dc31

📥 Commits

Reviewing files that changed from the base of the PR and between 5d03f8a and 141cb03.

📒 Files selected for processing (1)
  • getstream/video/rtc/encoders_patches.py
📝 Walkthrough

Walkthrough

This PR introduces environment-driven H.264 encoder preset configuration, refactors when codec preferences are applied to video transceivers, updates related documentation and warnings, and adds test coverage for the new preset behavior. The changes affect encoder patching, transceiver management, and video configuration logic.

Changes

Video Encoding Configuration and Management

Layer / File(s) Summary
H.264 Encoder Preset Configuration and Documentation
getstream/video/rtc/encoders_patches.py
Imports av, fractions, and H264_MAX_FRAME_RATE; adds STREAM_H264_PRESET constant from STREAM_PATCH_AIORTC_H264_PRESET environment variable (default "ultrafast"); extends StreamH264Encoder.__init__ to set preset; overrides _encode_frame to lazily create and configure libx264 CodecContext with frame geometry, bitrate, pixel format, framerate, timebase, preset option, and profile; updates patch_sender_encoder docstring and warning message to reference "Stream values" generically.
Codec Preference Application Timing Refactoring
getstream/video/rtc/pc.py
Removes codec preference iteration from PublisherPeerConnection constructor and moves it to addTrack() method, applying preferences only when adding sender-backed video tracks by matching the transceiver whose sender equals the returned RTCRtpSender; encoder bitrate patching is now performed within the same track.kind == "video" block, after codec preferences are set.
H.264 Preset Configuration Tests
tests/rtc/test_encoders_patches.py
Adds TestStreamH264Preset test suite verifying that STREAM_H264_PRESET and StreamH264Encoder().preset are controlled by the STREAM_PATCH_AIORTC_H264_PRESET environment variable; asserts default "ultrafast" preset, env-driven preset values, and whitespace-stripped parsing after module reload.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A preset hops into the encoder's den,
libx264 configs flow like a stream again,
transceivers find their timing, just right,
video tracks dance through the night—
ultrafast by default, no fuss, no flight! 🎥✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Fix/video codecs patching' is vague and uses a generic 'Fix/' prefix without clearly specifying what the main changes accomplish. Consider a more specific title like 'Apply codec preferences for video tracks and set H.264 preset to ultrafast' that conveys the primary changes to reviewers.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/video-codecs-patching

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@getstream/video/rtc/encoders_patches.py`:
- Around line 81-101: H264Encoder._encode_frame currently only sets up
self.codec when it is None, so if the parent reassigns a new CodecContext (e.g.,
on resolution change) your preset/options get lost; update _encode_frame to
detect a freshly-created parent codec (check self.codec is not None and
self.codec.name == "libx264" but self.codec.options.get("preset") != self.preset
or missing expected keys) and re-apply the STREAM_H264_PRESET-related settings
(preset, tune, level and any missing fields like pix_fmt, framerate, time_base,
bit_rate, profile) by merging only missing/incorrect keys into
self.codec.options before calling yield from super()._encode_frame(frame,
force_keyframe) so you preserve parent intent while restoring your latency
optimizations.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 97cd3af5-f2dd-4dda-9b5b-220812d483b5

📥 Commits

Reviewing files that changed from the base of the PR and between 031dc16 and 5d03f8a.

📒 Files selected for processing (3)
  • getstream/video/rtc/encoders_patches.py
  • getstream/video/rtc/pc.py
  • tests/rtc/test_encoders_patches.py

Comment thread getstream/video/rtc/encoders_patches.py
@dangusev dangusev merged commit 1526ccf into main May 15, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants