Traffic Control
Feature Overview
Traffic control refers to the SDK dynamically adjusting the video stream's bitrate, frame rate, and resolution, as well as the audio bitrate, based on the current network status of the local and remote endpoints, to automatically adapt to the current network environment and fluctuations, thereby ensuring smooth audio and video streaming.
The principle of traffic control is based on the current network situation to model the user's network environment and estimate their uplink bandwidth. If the current uplink bandwidth is less than the set stream publishing bitrate, the SDK will progressively reduce the video bitrate, resolution, frame rate, and audio bitrate according to the configured options to reduce the final uplink bitrate of the published stream, ensuring live streaming smoothness. After 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 traffic control. The playing endpoint notifies the publishing endpoint of its network situation, and the publishing endpoint estimates its own uplink bandwidth and the playing endpoint's downlink bandwidth, and takes the minimum value from the uplink and downlink bandwidth estimates to adjust its own uplink bitrate to ensure smooth co-hosting.
Usage Steps
1 Enable traffic control
Before publishing a stream, call the enableTrafficControl interface to enable traffic control through the enable parameter, and set the adjustable traffic control properties (bitrate, frame rate, resolution) through the property parameter. Multiple options are supported, with the default value being AdaptiveFPS, which means adaptive (reducing) video frame rate. When uplink bandwidth is insufficient, the SDK will reduce the final uplink bitrate of the published stream based on the current network environment and the set property parameter to adapt to the uplink bandwidth.
- Usage limitation: Only supports RTC stream publishing.
- When the traffic control switch is turned off, the set traffic control properties
propertywill also become invalid. - When the traffic control properties contain adaptive resolution
AdaptiveResolution, only initial resolutions with "16:9" or "4:3" aspect ratios are supported. If the initial resolution is other values, adaptive resolution will not take effect, and the SDK will downgrade to directly reducing the encoding bitrate.
// Enable traffic control and simultaneously enable adaptive (reducing) video frame rate and adaptive (reducing) video resolution
ZegoExpressEngine.instance.enableTrafficControl(true, ZegoTrafficControlProperty.AdaptiveFPS | ZegoTrafficControlProperty.AdaptiveResolution)2 (Optional) Automatically adjust traffic control properties
The SDK will automatically set traffic control properties suitable for the scenario based on the scenario selected by the developer when creating the engine. For example, show live streaming scenarios need to ensure video smoothness, so adaptive (reducing) video resolution is recommended; education scenarios need to ensure clarity, so adaptive (reducing) video frame rate is recommended.
The SDK determines the current traffic control properties based on the interface calling order, with the later call taking effect. That is, if traffic control properties are first set through enableTrafficControl, and then setRoomScenario is called to set scenario, the traffic control properties will change based on scenario.
3 Set minimum video bitrate for traffic control (can be skipped for audio-only scenarios)
After enabling traffic control, call the setMinVideoBitrateForTrafficControl method to set the minimum video bitrate (default value is "0") and video sending mode, which allows the SDK to use the video sending mode set by this method (not sending video or sending at an extremely low frame rate) when the network does not meet the minimum bitrate for sending video.
You can call setMinVideoBitrateForTrafficControl at any time after initializing the SDK and before starting stream publishing startPublishingStream, but this setting only takes effect after traffic control is enabled.
// After enabling flow control, when the uplink and downlink bandwidth is lower than 200 kbps, continue sending video data at an extremely low frame rate
ZegoExpressEngine.instance.setMinVideoBitrateForTrafficControl(200, ZegoTrafficControlMinVideoBitrateMode.UltraLowFPS)