This topic describes how to test network quality before a call.
Feature introduction
The ARTC SDK supports testing local network quality before joining a channel. This helps identify potential network issues in advance so you can optimize your network and avoid stuttering, frame drops, and audio interruptions during calls, ensuring stable and smooth real-time voice and video communication.
Sample code
Network quality detection on Android: Android/ARTCExample/AdvancedUsage/src/main/java/com/aliyun/artc/api/advancedusage/PreJoinChannelTest/PreJoinChannelTestActivity.java.
Perform network quality detection on iOS: iOS/ARTCExample/AdvancedUsage/PreJoinChannelTest/PreJoinChannelTestVC.swift.
How it works
Taking the iOS platform as an example, the ARTC SDK provides the startLastmileDetect method to detect local network quality before joining a channel. It returns network quality scores and statistics. Follow these steps:
Before joining a channel, call
startLastmileDetectto start the test. InAliRtcNetworkQualityProbeConfig, set the target for testing and the expected maximum bitrate.After calling this method, the SDK triggers the following callbacks:
onLastmileDetectResultWithQuality: Triggered 2 seconds afterstartLastmileDetect. It returns scores for upstream and downstream network quality, reflecting a subjective assessment.onLastmileDetectResultWithBandWidth: Triggered 30 seconds afterstartLastmileDetect. It returns real-time network statistics, including packet loss rate, network jitter, and available bandwidth, reflecting an objective assessment.
After you obtain the network quality statistics, call
stopLastmileDetectto stop the test.
Code examples
Initiate a network quality test
Android
Call the startNetworkQualityProbeTest operation:
public abstract int startNetworkQualityProbeTest(AlirtcNetworkQualityProbeConfig config);iOS
startLastmileDetect - Starts the last-mile network quality detection
- (int)startLastmileDetect:(AliRtcNetworkQualityProbeConfig *_Nonnull)config;Windows
AliRtcNetworkProbeConfig config;
config.probeUplink = true; // Probe upstream network
config.probeDownlink = true; // Probe downstream network
rtcEngine.startLastmileDetect(config);II. Receive network quality detection callback
Android
onNetworkQualityProbeTest: triggered about 3 seconds after the test starts, providing initial insights into the network quality.
public void onNetworkQualityProbeTest(AliRtcNetworkQuality quality){}
public void onNetworkQualityProbeTestResult(int code, AliRtcEngine.AlirtcNetworkQualityProbeResult result){}iOS
onLastmileDetectResultWithQuality - Callback for the network quality detection result
onLastmileDetectResultWithBandWidth: triggered about 30 seconds after the test starts, providing details of the network quality.
- (void)onLastmileDetectResultWithQuality:(AliRtcNetworkQuality)networkQuality;
- (void)onLastmileDetectResultWithBandWidth:(int)code result:(AliRtcNetworkQualityProbeResult* _Nonnull)result;Windows
// Handle network quality detection callback (replace onNetworkQualityChanged)
void OnLastmileDetectResultWithQuality(AliEngineNetworkQuality networkQuality) override {
std::string message = getNetworkQualityString(networkQuality);
networkTestResultText += "Network detection quality: " + message + "\n";
std::cout << "=== Network quality detection callback ===" << std::endl;
std::cout << networkTestResultText << std::endl;
}
// Handle lastmile network detection result callback
void onLastmileDetectResultWithBandWidth(int code,
const AliRtcNetworkProbeResult& result) override {
std::string message = buildNetworkTestResultMessage(code, result);
networkTestResultText += message + "\n";
std::cout << "=== Lastmile detection result callback ===" << std::endl;
std::cout << networkTestResultText << std::endl;
}Stop network quality detection
Android
stopNetworkQualityProbeTest - Stop network quality probe test
public abstract int stopNetworkQualityProbeTest();iOS
Call the stopLastmileDetect operation:
- (int)stopLastmileDetect;Windows
rtcEngine.stopLastmileDetect();