logo
On this page

Traffic Control

2024-11-13

Introduction

Traffic control refers to the SDK dynamically adjusting the bitrate, frame rate, and resolution of the video stream, as well as the audio bitrate, based on the current network status of the local and remote ends, automatically adapting 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, modeling the user's network environment and estimating their uplink bandwidth. If the current uplink bandwidth is less than the set streaming bitrate, the SDK will progressively reduce it from video bitrate, resolution, frame rate, and audio bitrate through the configured options to reduce the final uplink bitrate of the stream, ensuring the smoothness of the live broadcast. 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 end notifies the publishing end of its network situation, and the publishing end estimates its own uplink bandwidth and the downlink bandwidth of the playing end, 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.

Usage Steps

1 Enable traffic control

Before streaming, 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 can be selected, and the default value is ADAPTIVE_FPS, which is adaptive (reducing) video frame rate. When uplink bandwidth is insufficient, the SDK will reduce the final uplink bitrate of the stream according to the current network environment and the set property parameter to adapt to the uplink bandwidth.

Warning
  • Usage restriction: Only supports RTC streaming.
  • When traffic control is disabled, the set traffic control property property will also become invalid.
  • When the traffic control property contains adaptive (reducing) video resolution ADAPTIVE_RESOLUTION, only initial resolutions with "16:9" or "4:3" aspect ratios are supported. If the initial resolution is other values, adaptive (reducing) video resolution will not take effect, and the SDK will directly reduce the encoding bitrate. If you need to record local media while using the traffic control function, adaptive (reducing) video 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 traffic control and enable both adaptive (reducing) video frame rate and adaptive (reducing) video resolution
ZegoExpressSDK::getEngine()->enableTrafficControl(true, ZEGO_TRAFFIC_CONTROL_PROPERTY_ADAPTIVE_FPS | ZEGO_TRAFFIC_CONTROL_PROPERTY_ADAPTIVE_RESOLUTION);

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, 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.

Warning

The SDK will determine the current traffic control properties based on the order of interface calls, and the later call will take 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 according to scenario.

3 Set the 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 the video sending mode, which allows the SDK to use the video sending mode set by this method (not sending video or sending at a very low frame rate) when the network does not reach the minimum bitrate for sending video.

Warning

setMinVideoBitrateForTrafficControl can be called at any time after initializing the SDK and before starting to publish streams startPublishingStream, but the setting will only take effect after traffic control is enabled.

// After enabling traffic control, when the uplink and downlink bandwidth is lower than 200 kbps, continue to send video data at a very low frame rate
ZegoExpressSDK::getEngine()->setMinVideoBitrateForTrafficControl(200, ZEGO_TRAFFIC_CONTROL_MIN_VIDEO_BITRATE_MODE_ULTRA_LOW_FPS);

Previous

Multi-source Capture

Next

Cloud Proxy

On this page

Back to top