Pre-call Detection
Feature Overview
To ensure a real-time call experience, you can perform network and device detection before the call to identify and resolve issues in advance.
- Network detection: Detects the network environment to determine or predict whether it is suitable for publishing/playing streams at a specified bitrate.
- Device detection: Detects whether the local microphone, camera, and speaker are functioning properly.
This document describes how to use ZEGOCLOUD SDK interfaces to implement the above two aspects of detection.
Prerequisites
Before implementing pre-call network/device detection, 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.
Network Detection
Please refer to Network and Performance for operations.
Device Detection
Microphone Detection
Detection Logic
The microphone device detection flow is shown below:
Corresponding Interfaces
1. Start Microphone
Call the startPreview interface to start audio capture without streaming.
[[ZegoExpressEngine sharedEngine] startPreview:nil];2. Check Microphone Permission
ZEGOCLOUD SDK automatically checks microphone permission. If the user has not authorized, it will request user consent; if the user refuses, the user needs to manually enable permission in system settings.
3. Check if Microphone is Available
Detect device exceptions through the following callbacks. If no exception feedback is detected (you can start "4. Check Microphone Audio Data" synchronously), and the microphone audio data detection is normal, the microphone device is available.
- Version 2.15.0 and later: Listen to the onLocalDeviceExceptionOccurred callback to detect device exceptions.
- Before version 2.15.0: Listen to the
onDeviceErrorcallback to detect device exceptions.
// Local device exception notification.
//
// Supported versions: 2.15.0 and later.
// Description: Local device exception.
// Trigger timing: This callback is triggered when local audio or video device functions encounter exceptions.
//
// @param exceptionType Device exception type.
// @param deviceType The device type where the exception occurred.
// @param deviceID Device ID. Currently only supports desktop devices for identifying specific devices; for mobile devices, this parameter returns an empty string.
- (void)onLocalDeviceExceptionOccurred:(ZegoDeviceExceptionType)exceptionType deviceType:(ZegoDeviceType)deviceType deviceID:(NSString *)deviceID;4. Check Microphone Audio Data
Call the startSoundLevelMonitor interface to obtain the energy value of sound captured by the microphone. If the data is normal, the microphone is functioning properly and can be used for calls.
[[ZegoExpressEngine sharedEngine] startSoundLevelMonitor];Camera Detection
Detection Logic
The camera device detection flow is shown below:
Corresponding Interfaces
1. Start Camera
Call the startPreview interface to bind the view for camera preview, start video capture and preview without streaming.
ZegoCanvas *previewCanvas = [ZegoCanvas canvasWithView:self.previewView];
[[ZegoExpressEngine sharedEngine] startPreview:previewCanvas];2. Check Camera Permission
ZEGOCLOUD SDK automatically checks camera permission. If the user has not authorized, it will request user consent; if the user refuses, the user needs to manually enable permission in system settings.
3. Check if Camera is Available
Detect device exceptions through the following callbacks. If no exception feedback is detected (you can start "4. Check if Display is Normal" synchronously), and the display is normal, the device is available.
- Version 2.15.0 and later: Listen to the onLocalDeviceExceptionOccurred callback to detect device exceptions.
- Before version 2.15.0: Listen to the
onDeviceErrorcallback to detect device exceptions.
// Local device exception notification.
//
// Supported versions: 2.15.0 and later.
// Description: Local device exception.
// Trigger timing: This callback is triggered when local audio or video device functions encounter exceptions.
//
// @param exceptionType Device exception type.
// @param deviceType The device type where the exception occurred.
// @param deviceID Device ID. Currently only supports desktop devices for identifying specific devices; for mobile devices, this parameter returns an empty string.
- (void)onLocalDeviceExceptionOccurred:(ZegoDeviceExceptionType)exceptionType deviceType:(ZegoDeviceType)deviceType deviceID:(NSString *)deviceID;4. Check if Display is Normal
If the display is normal at this time, the camera is functioning properly and can be used for calls.
Speaker Detection
Detection Logic
The playback device detection flow is shown below:
Corresponding Interfaces
1. Use Media Player to Play Audio File
Call the ZegoMediaPlayer interface to play your audio file for testing.
// 1. Create media player.
ZegoMediaPlayer *mediaPlayer = [[ZegoExpressEngine sharedEngine] createMediaPlayer];
// 2. Load media resource.
NSString *resourcePath = "xxx";
[mediaPlayer loadResource: resourcePath callback:^(int errorCode) {
NSLog(@"Media Player load resource. errorCode: %d", errorCode);
}];
// 3. Start playing media.
[mediaPlayer start];2. Check if Sound is Heard
If you can hear the corresponding audio, the playback device is functioning properly and can be used for calls. Call the mediaPlayer:stateUpdate:errorCode: callback to check the media player status:
// Player playback status callback.
// @param mediaPlayer The callback player instance.
// @param state Player status.
// @param errorCode Error code. For details, see common error codes: /real-time-video-ios-oc/client-sdk/error-code.
- (void)mediaPlayer:(ZegoMediaPlayer *)mediaPlayer stateUpdate:(ZegoMediaPlayerState)state errorCode:(int)errorCode;