@videosdk.live/react-native-sdk
Version:
<h1 align="center"> <img src="https://static.videosdk.live/videosdk_logo_website_black.png"/><br/> <p align="center"> Video SDK React Native App <br/> <a href="https://videosdk.live/">videosdk.live</a> </p> </h1>
88 lines (71 loc) • 2.84 kB
JavaScript
import { useRef, useMemo, useCallback, useEffect } from 'react';
import { RTCFrameCryptorFactory } from "@videosdk.live/react-native-webrtc";
const useKeyProvider = ({discardFrameWhenCryptorNotReady = false}) => {
// const latestSetIndexRef = useRef(new Map());
const options = useMemo(() => ({
sharedKey: true,
ratchetSalt: "VideoSDKEncryptionRatchetSalt",
ratchetWindowSize: 0,
failureTolerance: -1,
keyRingSize: 16,
discardFrameWhenCryptorNotReady,
uncryptedMagicBytes: "VideoSDK-Encryption-Magic",
}), [discardFrameWhenCryptorNotReady]);
const nativeKeyProviderRef = useRef(null);
useEffect(() => {
try {
nativeKeyProviderRef.current = RTCFrameCryptorFactory.createDefaultKeyProvider(options);
console.log("RNKeyProvider initialized successfully");
} catch (error) {
console.error("Error initializing RNKeyProvider:", error);
}
return () => {
if (nativeKeyProviderRef.current && typeof nativeKeyProviderRef.current.dispose === 'function') {
nativeKeyProviderRef.current.dispose();
}
};
}, [options]);
// const getOptions = useCallback(() => options, [options]);
// const getLatestKeyIndex = useCallback((participantId) => {
// return latestSetIndexRef.current.get(participantId) ?? 0;
// }, []);
const setSharedKey = useCallback(async (key) => {
if (nativeKeyProviderRef.current) {
return await nativeKeyProviderRef.current.setSharedKey(key);
}
}, []);
// const ratchetSharedKey = useCallback(async (keyIndex) => {
// if (nativeKeyProviderRef.current) {
// await nativeKeyProviderRef.current.ratchetSharedKey(keyIndex);
// }
// }, []);
// const setKey = useCallback(async (participantId, key, keyIndex) => {
// if (options.sharedKey) {
// return await setSharedKey(key, keyIndex);
// }
// latestSetIndexRef.current.set(participantId, keyIndex ?? 0);
// if (nativeKeyProviderRef.current) {
// return await nativeKeyProviderRef.current.setKey(participantId, key, keyIndex);
// }
// }, [options, setSharedKey]);
// const ratchetKey = useCallback(async (participantIdentity, keyIndex) => {
// if (!options.sharedKey && participantIdentity) {
// if (nativeKeyProviderRef.current) {
// await nativeKeyProviderRef.current.ratchetKey(participantIdentity, keyIndex);
// }
// } else {
// await ratchetSharedKey(keyIndex);
// }
// }, [options, ratchetSharedKey]);
// const setSifTrailer = useCallback(async (trailer) => {
// if (nativeKeyProviderRef.current) {
// return await nativeKeyProviderRef.current.setSifTrailer(trailer);
// }
// }, []);
const rtcKeyProvider = nativeKeyProviderRef.current;
return {
setSharedKey,
rtcKeyProvider,
};
};
export default useKeyProvider;