Network Speed Test
Feature Overview
ZEGOCLOUD provides a network speed test feature that allows users to detect uplink and downlink network speeds before starting to publish or play streams, helping to determine the suitable bitrate for audio and video streams under current network conditions.
When the uplink speed test shows a high packet loss rate, it is recommended to reduce resolution or frame rate to lower the publishing bitrate and ensure smooth stream publishing. When the downlink speed test shows a high packet loss rate, it is recommended to use the layered video encoding feature provided by the SDK to play streams with lower bitrate to ensure smooth stream playing.
ZEGOCLOUD recommends using the SDK's network speed test feature in the following business scenarios:
-
In call scenarios, network quality assessment is required.
-
In education scenarios, pre-class network detection is required.
-
In live streaming scenarios, network connection speed testing is required.
The basic principle of network speed test is shown in the following diagram:

Download Sample Source Code
Please refer to Download Sample Source Code to get the source code.
For related source code, please check the files in the "/ZegoExpressExample/Examples/Others/NetworkAndPerformance" directory.
Prerequisites
Before implementing the network speed test feature, ensure that:
- A project has been created in the ZEGOCLOUD Console, and a valid AppID and AppSign have been obtained. For details, please refer to Console - Project Information.
- ZEGO Express SDK has been integrated into the project, and basic audio/video stream publishing and playing functionality has been implemented. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
Implementation Steps
1 Listen for 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
virtual void onNetworkSpeedTestQualityUpdate(const ZegoNetworkSpeedTestQuality& quality, ZegoNetworkSpeedTestType type)
{
printf("onNetworkSpeedTestQualityUpdate rtt=%d packetLostRate=%f connectCost=%d type=%d", quality.rtt, quality.packetLostRate, quality.connectCost, type);
}
// Network speed test error callback
virtual void onNetworkSpeedTestError(int errorCode, ZegoNetworkSpeedTestType type)
{
printf("onNetworkSpeedTestError errorCode=%d type=%d", errorCode, type);
}2 Start speed test
Create an instance of ZegoNetworkSpeedTestConfig for network speed test configuration, set whether to perform uplink and downlink speed tests and the 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 the timeout, the speed test will be forcibly ended. If you need to continue the speed test, please call this interface again.
ZegoNetworkSpeedTestConfig config;
// Perform uplink speed test and specify expected publishing bitrate
config.testUplink = true;
config.expectedUplinkBitrate = videoConfig.bitrate;
// Perform downlink speed test and specify expected playing bitrate
config.testDownlink = true;
config.expectedDownlinkBitrate = videoConfig.bitrate;
// Start speed test, default callback interval is 3 seconds
engine->startNetworkSpeedTest(config);
// If you need to set the callback interval, you can refer to the following call (taking 1.5 seconds as an example)
engine->startNetworkSpeedTest(config, 1500);The speed test results will be returned in onNetworkSpeedTestQualityUpdate through the network speed test quality ZegoNetworkSpeedTestQuality. By analyzing the parameter values, you can know whether the current network quality is good.
The parameters in ZegoNetworkSpeedTestQuality are as follows:
| Parameter | Description | Note |
|---|---|---|
| connectCost | Time to connect to the server, in milliseconds. | If the network connection is disconnected during the speed test, reconnection will be automatically initiated. 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 out of every 10 data packets sent to the server, 5 may be lost in transit. |
quality | Network quality | Represents the current network quality level. For details, please refer to 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.
engine->stopNetworkSpeedTest();