Precall Setup - React Native
Picture this: before diving into the depths of a video call, imagine giving your setup a quick check-up, like a tech-savvy doctor ensuring all systems are a go. That's essentially what a precall experience does- it’s like your extensive debug session before the main code execution—a crucial step in ensuring your app's performance is top-notch.
Why is it necessary?
Why invest time and effort into crafting a precall experience, you wonder? Well, picture this scenario: your users eagerly join a video call, only to encounter a myriad of technical difficulties—muted microphones, pixelated cameras, and laggy connections. Not exactly the smooth user experience you had in mind, right?
By integrating a robust precall process into your app, developers become the unsung heroes, preemptively addressing potential pitfalls and ensuring that users step into their video calls with confidence.
Ensure you're using the latest version of @videosdk.live/react-native-incallmanager
and @videosdk.live/react-native-sdk
.
Upgrade with:
npm update @videosdk.live/react-native-sdk @videosdk.live/react-native-incallmanager
Using PreCall Functions
Check Permissions
- Begin by ensuring that your application has the necessary permissions to access user devices such as cameras, microphones
- Utilize the
checkPermission()
andcheckBluetoothPermission()
methods of theuseMediaDevice
hook to verify if permissions are granted.
import { useMediaDevice } from "@videosdk.live/react-native-sdk";
const { checkPermission } = useMediaDevice();
const checkMediaPermission = async () => {
//These methods return a Promise that resolve to a Map<string, boolean> object.
const checkAudioPermission = await checkPermission("audio"); //For getting audio permission
const checkVideoPermission = await checkPermission("video"); //For getting video permission
const checkAudioVideoPermission = await checkPermission("audio_video"); //For getting both audio and video permissions
const checkBTPermission = await checkBlueToothPermission(); // For getting bluetooth permission
// Output: Map object for both audio and video permission:
/*
Map(2)
0 : {"audio" => true}
key: "audio"
value: true
1 : {"video" => true}
key: "video"
value: true
*/
};
Request Permissions (if necessary)
- If permissions are not granted, use the
requestPermission()
andrequestBluetoothPermission
methods of theuseMediaDevice
hook to prompt users to grant access to their devices.
const requestAudioVideoPermission = async () => {
try {
//These methods return a Promise that resolve to a Map<string, boolean> object.
const requestAudioPermission = await requestPermission("audio"); //For Requesting Audio Permission
const requestVideoPermission = await requestPermission("video"); //For Requesting Video Permission
const requestAudioVideoPermission = await requestPermission("audio_video"); //For Requesting Audio and Video Permissions
// Applicable only to Android; not required for iOS
const checkBTPermission = await requestBluetoothPermission(); //For requesting Bluetooth Permission.
} catch (ex) {
console.log("Error in requestPermission ", ex);
}
};
Render Device List
- Once you have the necessary permissions, Fetch and render list of available camera, microphone, and list of all devices using the
getCameras()
,getAudioDeviceList()
andgetDevices()
methods of theuseMediaDevice
hook respectively.
const getMediaDevices = async () => {
try {
//Method to get all available webcams.
//It returns a Promise that is resolved with an array of CameraDeviceInfo objects describing the video input devices.
let webcams = await getCameras();
console.log("List of Devices:", webcams);
//Method to get all available Microphones.
//It returns a Promise that is resolved with an array of MicrophoneDeviceInfo objects describing the audio input devices.
const mics = await getAudioDeviceList();
console.log("List of Microphone:", mics);
//Method to get all available cameras and playback devices.
//It returns a list of the currently available media input and output devices, such as microphones, cameras, headsets, and so forth
let deivces = await getDevices();
console.log("List of Cameras:", devices);
} catch (err) {
console.log("Error in getting audio or video devices", err);
}
};
Handle Device Changes
- Implement the
onAudioDeviceChanged
callback of theuseMediaDevice
hook to dynamically re-render device lists whenever new devices are attached or removed from the system. - Ensure that users can seamlessly interact with newly connected devices without disruptions.
const {
...
} = useMediaDevice({ onAudioDeviceChanged });
//Fetch camera, mic and speaker devices again using this function.
function onAudioDeviceChanged(device) {
console.log("Device Changed", device)
}
Create Media Tracks
- Create media tracks for the selected microphone and camera using the
createMicrophoneAudioTrack()
andcreateCameraVideoTrack()
methods. - Ensure that these tracks originate from the user-selected devices for accurate testing.
import {
createCameraVideoTrack,
createMicrophoneAudioTrack,
} from "@videosdk.live/react-native-sdk";
//For Getting Audio Tracks
const getMediaTracks = async () => {
try {
//Returns a MediaStream object, containing the Audio Stream from the selected Mic Device.
let customTrack = await createMicrophoneAudioTrack({
encoderConfig: "speech_standard",
noiseConfig: {
noiseSuppression: true,
echoCancellation: true,
autoGainControl: true,
},
});
} catch (error) {
console.log("Error in getting Audio Track", error);
}
//For Getting Video Tracks
try {
//Returns a MediaStream object, containing the Video Stream from the selected Webcam Device.
let customVideoTrack = await createCameraVideoTrack({
optimizationMode: "motion",
encoderConfig: "h720p_w1280p",
facingMode: "user",
});
//To retrive video tracks that will be displayed to the user from the stream.
const videoTracks = stream?.getVideoTracks();
const videoTrack = videoTracks.length ? videoTracks[0] : null;
} catch (error) {
console.log("Error in getting Video Track", error);
}
};
Passing States to Meeting
- Ensure that all relevant states, such as microphone and camera status (on/off), and selected devices, are passed into the meeting from the precall screen.
- This can be accomplished by passing these crucial states and media streams onto the VideoSDK
MeetingProvider
. - By ensuring this integration, users can seamlessly transition from the precall setup to the actual meeting while preserving their preferred settings.
<MeetingProvider
config={
{
...
//Status of Mircophone Device as selected by the user (On/Off).
micEnabled: micOn,
//Status of Webcam Device as selected by the user (On/Off).
webcamEnabled: webcamOn,
//customVideoStream has to be the Video Stream of the user's selected Webcam device as created in Step-5.
customCameraVideoTrack: customVideoStream,
//customAudioStream has to be the Audio Stream of the user's selected Microphone device as created in Step-5.
customMicrophoneAudioTrack: customAudioStream
}
} >
</MeetingProvider>
You can explore the complete implementation of the Precall functions in the official React Native SDK example available here.
API Reference
The API references for all the methods utilized in this guide are provided below.
Got a Question? Ask us on discord