logo
On this page

Traffic Control

2024-11-13

Feature overview

Traffic 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, automatically adapting to the current network environment and network fluctuations, thereby ensuring smooth audio and video publishing.

The principle of traffic control is to model the user's network environment and estimate the uplink bandwidth based on the current network situation. If the current uplink bandwidth is less than the set publishing bitrate, it will progressively reduce from video bitrate, resolution, frame rate, and audio bitrate through the configured options to reduce the final uplink bitrate of publishing, 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 Publishing stream, only one RTC Playing stream or only one L3 Playing stream, the SDK will automatically enable downstream traffic control. The playing end notifies the publishing end of its network situation, 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 connection.

Usage steps

1 Enable traffic control

Before publishing stream, call the enableTrafficControl interface, enable traffic control through the "enable" parameter, and set adjustable traffic control properties (bitrate, frame rate, resolution) through the property parameter. Multiple selections are supported, the default value is ADAPTIVE_FPS, which is adaptive (reduce) video frame rate. When the uplink bandwidth is insufficient, the SDK will reduce the final uplink bitrate of publishing according to the current network environment and the set property parameter to adapt to the uplink bandwidth.

Warning
  • Usage limitation: Only supports RTC publishing stream.
  • When the traffic control switch is turned off, the set traffic control properties property will also become invalid.
  • When the traffic control properties contain adaptive resolution ADAPTIVE_RESOLUTION, only initial resolutions with "16:9" or "4:3" ratios are supported. If the initial resolution is other values, adaptive resolution cannot take effect, and the SDK will downgrade to directly reducing the encoding bitrate. If you need to record local media during traffic control, adaptive resolution will affect file recording in "MP4" format. 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 adaptive (reduce) video frame rate and adaptive (reduce) video resolution at the same time
zgEngine.enableTrafficControl(true, zgDefines.ZegoTrafficControlProperty.AdaptiveFPS + zgDefines.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 engine. For example, the show live streaming scenario needs to ensure video smoothness, it is recommended to choose adaptive (reduce) video resolution; the education scenario needs to ensure clarity, it is recommended to choose adaptive (reduce) video frame rate.

Warning

The SDK determines the current traffic control properties based on the order of interface calls, and the later call takes effect. That is, if traffic control properties are set first through enableTrafficControl, and then setRoomScenario is called to set scenario, the traffic control properties will change according to 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 can make the SDK adopt the video sending mode set by this method (do not send video or send at a very low frame rate) when the network does not meet the minimum bitrate for sending video.

Warning

You can call setMinVideoBitrateForTrafficControl at any time after initializing the SDK and before publishing stream startPublishingStream, but this setting only takes effect after traffic control is enabled.

// After enabling traffic control, when the uplink and downlink bandwidth is lower than 200 kbps, continue sending video data at a very low frame rate
zgEngine.setMinVideoBitrateForTrafficControl(200, zgDefines.ZegoTrafficControlMinVideoBitrateMode.UltraLowFPS)

Previous

Multi-source Capture

Next

Geofencing

On this page

Back to top