Flow Control
Function Overview
Flow control refers to the SDK dynamically adjusting the bitrate, frame rate, and resolution of video stream publishing, as well as the audio bitrate, based on the current network status of the local and remote ends, to automatically adapt to the current network environment and fluctuations, thereby ensuring smooth audio and video streaming.
The principle of flow control is to model the user's network environment based on current network conditions and estimate the uplink bandwidth. If the current uplink bandwidth is less than the set publishing bitrate, the SDK will progressively reduce it from video bitrate, resolution, frame rate, and audio bitrate through configured options to reduce the final publishing uplink bitrate and ensure live streaming smoothness. When the network environment returns to normal, the uplink bitrate will also recover to the initial set value.
When there is only one RTC stream publishing, only one RTC stream playing, or only one L3 stream playing, the SDK will automatically enable downlink flow control. The playing end notifies the publishing end of its network status, the publishing end estimates its own uplink bandwidth and the playing end's downlink bandwidth, and takes the minimum value from the uplink and downlink bandwidth estimates to adjust its own uplink bitrate to ensure the smoothness of the co-hosting.
This feature does not support running in WebGL environment.
Usage Steps
1 Enable Flow Control
Before publishing stream, call the EnableTrafficControl interface to enable flow control through the enable parameter, and set adjustable flow control properties (bitrate, frame rate, resolution) through the property parameter. Multiple selections are supported, the default value is AdaptiveFPS, which means adaptive (reducing) video frame rate. When uplink bandwidth is insufficient, the SDK will reduce the final publishing uplink bitrate according to the current network environment and the set property parameter to adapt to the uplink bandwidth.
- Usage limitation: Only supports RTC stream publishing.
- When the flow control switch is turned off, the set flow control properties
propertywill also become invalid. - When the flow control properties include adaptive resolution "AdaptiveResolution", only initial resolutions with "16:9" or "4:3" ratios are supported. If the initial resolution is other values, the adaptive resolution will not take effect, and the SDK will degrade to directly reducing the encoding bitrate. If you need to record local media while using the flow control feature, the adaptive resolution will affect "MP4" format file recording. In this case, you need to change the format to "FLV". For detailed operations, please refer to Audio and Video Recording.
// Enable flow control, and simultaneously enable adaptive (reducing) video frame rate and adaptive (reducing) video resolution
ZegoExpressEngine.GetEngine().EnableTrafficControl(true, AdaptiveResolution | AdaptiveFPS);2 (Optional) Automatically Adjust Flow Control Properties
The SDK will automatically set flow control properties suitable for the scenario based on the scenario selected by the developer when creating engine. For example, the show live streaming scenario needs to ensure video smoothness, so adaptive (reducing) video resolution is recommended; the education scenario needs to ensure clarity, so adaptive (reducing) video frame rate is recommended.
The SDK will determine the current flow control properties based on the interface call order, and the later call takes effect. That is, if flow control properties are first set through enableTrafficControl, and then setRoomScenario is called to set scenario, the flow control properties will change according to scenario.
3 Set Minimum Video Bitrate for Flow Control (Can be skipped for audio-only scenarios)
After enabling flow control, call the SetMinVideoBitrateForTrafficControl method to set the minimum video bitrate (default value is "0") and video sending mode, which can make the SDK adopt the video sending mode set by this method (not sending video or sending at an extremely low frame rate) when the network does not reach the minimum bitrate for sending video.
SetMinVideoBitrateForTrafficControl can be called at any time after initializing the SDK and before starting to publish stream startPublishingStream, but this setting only takes effect after flow control is enabled.
// After enabling flow control, when the uplink and downlink bandwidth is lower than 200 kbps, continue to send video data at an extremely low frame rate
ZegoExpressEngine.GetEngine().SetMinVideoBitrateForTrafficControl(200, ZegoTrafficControlMinVideoBitrateMode.UltraLowFPS);