Repository navigation
feat(stream): add RTT-based adaptive bitrate (MACCA) - #6181
Open
cesarpmorais wants to merge 1 commit into
Open
cesarpmorais wants to merge 1 commit into
cesarpmorais wants to merge 1 commit into
Conversation
Author
|
cesarpmorais
force-pushed
the
feat/macca-adaptive-bitrate
branch
from
November 23, 2026 15:32
c694f1f to
7a185bf
Compare
Add an opt-in `bitrate_mode = adaptive` that adjusts the encoder bitrate from the client's ENet round-trip time, with no client changes. The controller cuts the bitrate by beta after sustained congestion and recovers along a CUBIC curve, with fast convergence, at most one decision per second. Its parameters are exposed as `abr_*` options, defaulting to the values evaluated in the MACCA paper (SBESC 2026). The control thread publishes the RTT through an atomic, and the last bitrate the encoder accepted is restored after encoder reinitialization. FFmpeg-based encoders rebuild the codec context at the new bitrate, reusing the hardware frame pool; VideoToolbox changes it in place. Native NVENC keeps a fixed bitrate for now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cesarpmorais
force-pushed
the
feat/macca-adaptive-bitrate
branch
from
November 23, 2026 15:44
7a185bf to
8cbce28
Compare
|
This branch has not been deployed
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.



Description
This PR adds an opt-in adaptive bitrate mode,
bitrate_mode = adaptive. The default staysfixed, so nothing changes unless a user turns it on.The controller is MACCA (Multi-state AIMD with Cubic Convergence Adaptation), from our paper accepted at SBESC 2026. There we evaluated it against fixed bitrate and a baseline AIMD controller on a Sunshine/Moonlight testbed.
MACCA only uses the ENet round-trip time Sunshine already measures on the control stream. It works with any Moonlight client and adds nothing to the protocol. When the bandwidth to the client drops, packets queue up and the RTT rises; MACCA lowers the bitrate before that queue turns into stutter or a disconnect, and raises it again once the path is clear:
abr_rtt_threshold_ms(100 ms) forabr_hysteresis_ms(2 s), the bitrate is multiplied byabr_beta(0.7). While congestion lasts, it decreases again after each further window, down toabr_min_kbps. Shorter spikes are ignored.abr_margin_ms(20 ms) forabr_stable_time_ms(1 s), the bitrate follows the CUBIC curve from the last decrease. It rises fast at first, levels off near the rate held before the cut afterabr_cubic_recovery_ms(15 s), then probes above it.abr_fast_convergence, on by default).max_bitrateorabr_max_kbps.The defaults are the parameters we evaluated. In our Linux testbed (VA-API, Celeste, six
tcbandwidth-degradation scenarios), compared with the baseline AIMD controller, MACCA:Compared with fixed bitrate, both adaptive controllers cut stall time by 90–100%.
Usage
That's all that is needed; it can also be set in the Web UI under Audio/Video → Bitrate Mode. The tuning options appear there when Adaptive is selected. All options are read when a stream starts:
abr_rtt_threshold_msabr_margin_msabr_hysteresis_msabr_stable_time_msabr_betaabr_cubic_recovery_msabr_fast_convergenceabr_min_kbpsabr_max_kbpsWith
min_log_level = debug, each change is logged asAdaptive bitrate: target … kbps (RTT … ms).docs/configuration.mddocuments all options, anddocs/troubleshooting.mdcovers the VideoToolbox issue.Encoder support
kVTCompressionPropertyKey_AverageBitRate, plusDataRateLimitswhen the session uses standard rate control.set_bitratereturns false, so the stream keeps a fixed bitrate. Runtime reconfiguration for NVENC is what feat(stream): add adaptive NVENC bitrate control #5460 implements.Known issue: VideoToolbox drops frames at low bitrates (also with fixed bitrate)
While testing on macOS (Apple M5, macOS 26.6.2, HEVC 1080p60, high-motion video), we found that VideoToolbox's low-latency rate control drops frames when the bitrate is too low for the content. Sunshine treats a dropped frame as fatal and recreates the encoder, so every frame is sent as an IDR frame at about 30 fps, and the stream stutters. The log repeats
VideoToolbox dropped a frame/Creating encoder [hevc_vt]on every frame.This is not caused by this PR. With
bitrate_mode = fixedand Moonlight set to 4 Mbps (2708 kbps negotiated), the same content produced 1296 dropped frames in 44 s, every frame an IDR, while the RTT stayed at 5–8 ms. Adaptive mode makes it more likely to hit, though: under congestion it can lower the bitrate into that range. In our run, the drops started when MACCA reached 4558 kbps and stopped once its recovery passed ~5150 kbps.This PR documents it rather than working around it:
docs/troubleshooting.mdhas a new macOS entry ("Stream stutters at low bitrates with VideoToolbox"), and the docs and UI text forbitrate_modeandabr_min_kbpspoint to it;abr_min_kbpsabove that level on macOS, e.g.abr_min_kbps = 6000for 1080p60.A real fix belongs in the VideoToolbox encoder, separately from this PR: for example, falling back to standard rate control instead of recreating the session in a loop. I'm happy to open an issue for it.
Relation to #5460
#5460 adapts the bitrate from packet loss and FEC reports, for native NVENC on Windows. This PR uses RTT only and works across encoders. The two signals are complementary, and #5460's NVENC reconfiguration would also give this controller NVENC support.
Changes
src/bitrate_controller.{h,cpp}: the controller. It is policy only, with no I/O.src/stream.cpp:make_bitrate_controllercreates one controller per session when adaptive mode is on. The control thread publishes the peer's ENet RTT to an atomic, so the video thread never touches the ENet peer. The video thread feeds the controller one sample per sent frame and raises abitrate_changemail event when the target changes.src/video.{h,cpp},src/platform/common.h,src/platform/macos/vt_encoder.mm:encode_session_t::set_bitrate, and handling of the event in both encode loops.make_avcodec_encode_sessionintobuild_avcodec_ctx, so it can be rebuilt at a new bitrate. To keep it within the quality gate's complexity limits, it is split into small helpers (select_avcodec_profile,set_avcodec_gop,make_avcodec_hw_device,attach_avcodec_hw_frames,make_avcodec_options,set_avcodec_rate_control,configure_avcodec_ctx), with the encoder inputs grouped inavcodec_ctx_inputs_t. The settings applied are unchanged. One side fix: the options dictionary is now freed afteravcodec_open2; it leaked before.src/config.{h,cpp}: thebitrate_modeandabr_*options, with range checks. An unknownbitrate_modefalls back tofixedwith a warning.configuration.md,troubleshooting.md), Web UI (Audio/Video → Bitrate Mode) anden.json.tests/software_encode_test_utils.h: the CPU test display and BGRA frame used bytest_legacy_codec_roundtrip.cpp, now shared with the newtest_video_bitrate.cppinstead of being duplicated.Screenshot
Issues Fixed or Closed
Roadmap Issues
Testing
Automated (macOS 26.6.2, Apple M5,
BUILD_WERROR=ON; formatted with clang-format 23.1.0):tests/unit/test_bitrate_controller.cpp: hysteresis, ignored short spikes, cascaded decreases and the floor, fast convergence on and off, the cubic recovery values, probing up to the ceiling, the dead band between the thresholds, one decision per interval, ceilings, and no overflow long after a decrease.tests/unit/test_video_bitrate.cpp:max_bitrate), restarts with an IDR frame, and every frame still decodes;tests/unit/platform/macos/test_vt_encoder.mm: a VideoToolbox H.264 session accepts runtime bitrate changes (skipped without a hardware encoder; it ran on the M5).tests/unit/test_stream.cpp: the controller is only created in adaptive mode, starts at the negotiated bitrate ormax_bitrate, and uses the configured parameters.ConfigPersistenceTest.ParsesAdaptiveBitrateOptionscovers parsing and the rejection of invalid values.test_config_consistencycovers the new options in the docs, the UI defaults anden.json.Manual, macOS → Moonlight Android on the local network (VideoToolbox HEVC 1080p60, Moonlight slider 25 Mbps → 18988 kbps negotiated, high-motion video). The bandwidth to the client was capped at 8 Mbit/s for 30 s with dummynet:
The controller behaved as designed: the RTT went from 457 ms back under 25 ms within 4 s of the last decrease. Peak RTT 470 ms, median 8 ms, no stalls. Below ~5 Mbps the VideoToolbox frame-drop issue described above appeared (it reproduces with fixed bitrate too).
Earlier evaluation: the same controller logic, before this port, was evaluated on Linux with VA-API and
tcbandwidth caps (six scenarios, against fixed bitrate and a baseline AIMD). All 787 rate changes logged in those runs match the controller's rules.Not tested: Windows, and FFmpeg hardware encoders other than VA-API on current
master.Type of Change
Checklist
AI Usage
See our AI usage policy.
Claude Code ported the controller from our research branch onto current master, wrote the unit tests and docs, checked the logged rate changes from our campaign against the controller's rules, and helped run and analyze the macOS tests.
🤖 Generated with Claude Code