Enhance RTMP stack: O(1) Queue, ABR, Zero-copy serialization, Drop P-…#385
Enhance RTMP stack: O(1) Queue, ABR, Zero-copy serialization, Drop P-…#385paraisospelosul wants to merge 1 commit into
Conversation
4bde5d6 to
7063ac9
Compare
…Frames on congestion
bac1f77 to
089db72
Compare
|
looks very complicated |
|
Okay, if there's anything usable, go ahead and use it; otherwise, we'll wrap it up and scrap the idea. What actually works and is worth keeping AdaptiveBitrateRtmp.swift — The ABR logic is the most valuable part. It’s clean, sensible, and integrates well with the existing architecture. It deserves a dedicated PR. Strategy moving forward |
Description
This PR introduces a comprehensive modernization of Moblin's RTMP stack, focusing on zero-copy optimization, predictable congestion management, and deterministic performance under poor mobile network conditions.
The architecture has been refined to eliminate CPU bottlenecks during high framerate broadcasting (e.g., 60fps/120fps) and introduces a robust ABR state machine to prevent P-frame accumulation and Head-of-Line (HOL) blocking.
Key Architectural Improvements
1. O(1) Priority Queue (
DequeModule)RtmpSendQueuepreviously relied on anArray.sort()triggered on everyenqueue(), creating anO(n log n)CPU hotspot during congestion.Deque<Data>(viaswift-collections), one for each RTMP priority.dequeue()now runs in strictlyO(1)constant time, naturally preserving insertion stability without any CPU sorting overhead.2. Active Interframe Dropping (Zero-Cost Serialization)
RtmpSocket(priorityFor(chunk:)) with full support for Enhanced RTMP extended headers. WhenestimatedSendPressure() > 0.85, the socket entersisDropModeActiveand completely bypasses the serialization of new P-frames, discarding them instantly before they burn CPU cycles or memory.3. Outbound Pipeline Pressure Heuristic
1MBthreshold to estimate congestion fails to account for streams of varying bitrates (e.g., 500Kbps vs 12Mbps).estimatedSendPressure(). The threshold (maxBacklog) is now dynamically calculated based on the stream's recent throughput (recentSendRateBytesPerSecond * 0.6s), providing a mathematically sound and adaptive estimation of the TCP/Kernel backlog. Hysteresis (0.85enter,0.65leave) was also added to prevent drop-mode flapping.4. Chunk Size & Metrics Fixes
64KB(for >4Mbps) and32KB(for <4Mbps) to drastically reduce TCP Head-of-Line blocking over unstable LTE/5G connections.RtmpStreamMetrics.healthScorecalculations to safely clamp between0.0and1.0.Testing & Validation
O(1).firstByte & 0x80).