import type { RingingCallLifecycleHooks, StreamVideoConfig } from './types'; import { setRingingCallLifecycleHooks as storeRingingCallLifecycleHooks } from '../internal/ringingCallLifecycle'; import pushLogoutCallbacks from '../internal/pushLogoutCallback'; import { setupIosVoipPushEvents } from '../push/setupIosVoipPushEvents'; import { setupAndroidPushEvents } from '../push/setupAndroidPushEvents'; import { setupCallingExpEvents } from '../push/setupCallingExpEvents'; import { extractCallingExpOptions, getCallingxLib, } from '../push/libs/callingx'; import { NativeModules, Platform } from 'react-native'; import { videoLoggerSystem } from '@stream-io/video-client'; // Utility type for deep partial type DeepPartial = { [P in keyof T]?: T[P] extends object ? DeepPartial : T[P]; }; // Helper function for deep merging function deepMerge>( target: T, source: DeepPartial, ): T { const result = { ...target }; for (const key in source) { if (source[key] !== undefined) { if ( typeof source[key] === 'object' && source[key] !== null && !Array.isArray(source[key]) && typeof target[key] === 'object' && target[key] !== null && !Array.isArray(target[key]) ) { result[key] = deepMerge( target[key], source[key] as DeepPartial, ); } else { result[key] = source[key] as T[typeof key]; } } } return result; } const DEFAULT_STREAM_VIDEO_CONFIG: StreamVideoConfig = { foregroundService: { android: { channel: { id: 'stream_call_foreground_service', name: 'Ongoing calls', }, notificationTexts: { title: 'Call in progress', body: 'Tap to return to the call', }, taskToRun: () => new Promise(() => {}), }, }, }; export class StreamVideoRN { private static config = DEFAULT_STREAM_VIDEO_CONFIG; /** * Update the global config for StreamVideoRN except for push config. * To set push config use `StreamVideoRN.setPushConfig` instead. * This function accepts a partial config object that will be deeply merged with the default config. */ static updateConfig( updateConfig: DeepPartial>, ) { this.config = deepMerge(this.config, updateConfig); } /** * Set the push config for StreamVideoRN. * This method must be called **outside** of your application lifecycle, e.g. alongside your * `AppRegistry.registerComponent()` method call at the entry point of your application code. * Since the app can be opened from a dead state through a push notification * Note: This function must be called only once. Further calls will be ignored. * @example // in index.js * import { AppRegistry } from 'react-native'; * import { StreamVideoRN } from '@stream-io/video-react-native-sdk'; * import App from './App'; * // Set push config * const pushConfig = {}; // construct your config * // Set CallKit/Android Telecom API integration options. All params are optional. If not provided, the default values will be used. * const callingExpOptions = { * ios: { * callsHistory: true, * displayCallTimeout: 60000, * sound: 'ringtone', * imageName: 'callkit_icon', * }, * android: { * incomingChannel: { * id: 'stream_incoming_call_notifications', * name: 'Call notifications', * vibration: true, * sound: 'default', * }, * titleTransformer: (memberName: string, incoming: boolean) => * incoming * ? `${memberName} is calling you` * : `You are calling ${memberName}`, * }, * }; * StreamVideoRN.setPushConfig(pushConfig, callingExpOptions); * AppRegistry.registerComponent('app', () => App); */ static setPushConfig(pushConfig: NonNullable) { if (this.config.push) { // Ignoring this config as push config was already set return; } this.config.push = pushConfig; try { const callingx = getCallingxLib(); videoLoggerSystem .getLogger('StreamVideoRN.setPushConfig') .info(JSON.stringify(this.config)); const options = extractCallingExpOptions(this.config); callingx.setup(options); } catch { throw new Error( 'react-native-callingx library is not installed. Please check our migration instructions: https://getstream.io/video/docs/react-native/migration-guides/1.32.0/.', ); } setupCallingExpEvents(pushConfig); setupIosVoipPushEvents(pushConfig); setupAndroidPushEvents(pushConfig); } /** * Register hooks that run around a ringing call's join, for every ringing path: * accepted from the CallKit/Telecom UI, accepted inside the app, and outgoing. * * Call this at your application's entry point, alongside * {@link StreamVideoRN.setPushConfig} and **outside** the React tree. A call * accepted from a push notification can be created and joined before any * component mounts, so hooks registered from inside React would be missed on * exactly the path that most needs them. * * Calling it again replaces the previously registered hooks. * * @example // in index.js * StreamVideoRN.setRingingCallLifecycleHooks({ * onBeforeCallJoin: (call) => attachE2EEIfConfigured(call), * onAfterCallLeave: (call) => disposeE2EEManager(call), * }); */ static setRingingCallLifecycleHooks = (hooks: RingingCallLifecycleHooks) => { storeRingingCallLifecycleHooks(hooks); }; static getConfig() { return this.config; } /** * This is the function to be called when the push token must be removed. * Typically used on user logout. */ static onPushLogout() { const callbacks = pushLogoutCallbacks.current; if (!callbacks) { return Promise.resolve(); } pushLogoutCallbacks.current = []; return Promise.all(callbacks.map((callback) => callback())).then(() => {}); } static clearPushLogoutCallbacks() { pushLogoutCallbacks.current = []; } /** * Android only. Opt out of the Android 11+ communication-mode keep-alive. * * By default the SDK plays a silent voice-communication track for the duration of a * communicator-role call, so Android does not reset `MODE_IN_COMMUNICATION` ~6s after it is * set (which breaks audio routing and echo cancellation). * See {@link https://issuetracker.google.com/issues/209493718} * * Call this at app start, alongside {@link setPushConfig} — the native audio manager * rejects the change once it has been activated for a call. No-op on iOS and on * Android below API 30. */ static setDisableCommunicationModeWorkaround(disabled: boolean) { if (Platform.OS !== 'android') { return; } try { NativeModules.StreamInCallManager?.setDisableCommunicationModeWorkaround( disabled, ); } catch (error) { videoLoggerSystem .getLogger('StreamVideoRN') .warn('setDisableCommunicationModeWorkaround failed', error); } } /** * Play native busy tone for call rejection */ static async playBusyTone() { return NativeModules.StreamVideoReactNative.playBusyTone(); } /** * Stop native busy tone */ static async stopBusyTone() { return NativeModules.StreamVideoReactNative.stopBusyTone(); } /** * Check if the device has audio output hardware * @returns True if the device has audio output hardware */ static async androidHasAudioOutputHardware(): Promise { if (Platform.OS !== 'android') throw new Error( 'androidHasAudioOutputHardware function is only available on Android', ); return NativeModules.StreamVideoReactNative.hasAudioOutputHardware(); } /** * Check if the device has microphone hardware * @returns True if the device has microphone hardware */ static async androidHasMicrophoneHardware(): Promise { if (Platform.OS !== 'android') throw new Error( 'androidHasMicrophoneHardware function is only available on Android', ); return NativeModules.StreamVideoReactNative.hasMicrophoneHardware(); } /** * Check if the device has camera hardware * @returns True if the device has camera hardware */ static async androidHasCameraHardware(): Promise { if (Platform.OS !== 'android') throw new Error( 'androidHasCameraHardware function is only available on Android', ); return NativeModules.StreamVideoReactNative.hasCameraHardware(); } }