Network Speed Test
Feature Overview
ZEGOCLOUD provides network speed test functionality to detect uplink and downlink network speeds before users publish/play streams, determining the suitable bitrate for audio/video streams under current network conditions.
When the uplink speed test results show a high packet loss rate, it is recommended to reduce the publish stream bitrate by lowering resolution or frame rate to ensure normal streaming; when the downlink speed test results show a high packet loss rate, it is recommended to use the Layered Video Encoding feature provided by the SDK to play low-bitrate streams to ensure normal stream playing.
ZEGOCLOUD recommends using the SDK's network speed test feature in the following business scenarios:
-
Call scenarios: Network quality assessment is required.
-
Education scenarios: Pre-class network detection is required.
-
Live streaming scenarios: Network connection speed testing is required.
The basic principle of network speed test is shown below:

Example Source Code Download
Please refer to Download Example Source Code to get the source code.
For related source code, please check files in the "/ZegoExpressExample/Examples/Others/NetworkAndPerformance" directory.
Prerequisites
Before implementing the network speed test feature, ensure that:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, see Console - Project Information.
- You have integrated the ZEGO Express SDK into your project and implemented basic audio/video streaming functionality. For details, see Quick Start - Integration and Quick Start - Implementation Flow.
Usage Steps
1 Listen to Speed Test Callbacks
Before starting the speed test, you can set up speed test related callbacks first.
- Under normal speed test conditions, the onNetworkSpeedTestQualityUpdate callback will be triggered when network speed quality updates.
- When an error occurs during the speed test, the onNetworkSpeedTestError callback will be triggered.
// Network speed test quality callback
- (void)onNetworkSpeedTestQualityUpdate:(ZegoNetworkSpeedTestQuality *)quality type:(ZegoNetworkSpeedTestType)type {
NSLog(@"onNetworkSpeedTestQualityUpdate rtt=%d packetLostRate=%f connectCost=%d type=%d", quality.rtt, quality.packetLostRate, quality.connectCost, type);
}
// Network speed test error callback
- (void)onNetworkSpeedTestError:(int)errorCode type:(ZegoNetworkSpeedTestType)type {
NSLog(@"onNetworkSpeedTestError errorCode=%d, type=%d", errorCode, type);
}2 Start Speed Test
Create an instance of ZegoNetworkSpeedTestConfig network speed test configuration, set whether to perform uplink/downlink speed tests and expected bitrate according to the actual situation, and call the startNetworkSpeedTest interface to start the network speed test.
After calling the startNetworkSpeedTest interface to start the speed test, it supports speed testing for 30 seconds by default (if you need to adjust the duration, please contact ZEGOCLOUD Technical Support to modify the dynamic configuration). After timeout, the speed test will be forcibly terminated. If you need to continue the speed test, please call this interface again.
ZegoNetworkSpeedTestConfig *config = [[ZegoNetworkSpeedTestConfig alloc] init];
ZegoVideoConfig *videoConfig = [[ZegoExpressEngine sharedEngine] getVideoConfig];
// Perform uplink speed test, specify expected publish stream bitrate
config.testUplink = YES;
config.expectedUplinkBitrate = videoConfig.bitrate;
// Perform downlink speed test, specify expected play stream bitrate
config.testDownlink = YES;
config.expectedDownlinkBitrate = videoConfig.bitrate;
// Start speed test, default callback interval is 3 seconds
[[ZegoExpressEngine sharedEngine] startNetworkSpeedTest:config];
// If you need to set the callback interval, refer to the following call (1.5 seconds as an example)
[[ZegoExpressEngine sharedEngine] startNetworkSpeedTest:config interval:1500];Speed test results will be returned in the network speed test quality ZegoNetworkSpeedTestQuality in onNetworkSpeedTestQualityUpdate. By analyzing each parameter value, you can know whether the current network quality is good.
Parameters in ZegoNetworkSpeedTestQuality are as follows:
| Parameter | Description | Note |
|---|---|---|
| connectCost | Time to connect to server, in milliseconds. | If the network connection is disconnected during the speed test, reconnection will be initiated automatically. This variable will be updated accordingly. The smaller the value, the better. |
| rtt | Network latency, in milliseconds. | Represents the time consumed between the SDK and the server round trip. The smaller the value, the better. |
| packetLostRate | Packet loss rate, in percentage. | The value range is 0.0 - 1.0. For example, 0.5 means that for every 10 packets sent to the server, 5 of them may be lost on the way. |
| quality | Network quality | Represents the current network quality level. For details, see ZegoStreamQualityLevel.
|
3 Stop Speed Test
Call the stopNetworkSpeedTest interface to stop the network speed test.
After stopping the speed test, you will no longer receive onNetworkSpeedTestQualityUpdate or onNetworkSpeedTestError callbacks.
[[ZegoExpressEngine sharedEngine] stopNetworkSpeedTest];