import { CallingState, videoLoggerSystem, type StreamVideoClient, } from '@stream-io/video-client'; import type { StreamVideoConfig } from '../StreamVideoRN/types'; import { clearPushWSEventSubscriptions, processCallFromPushInBackground, } from './internal/utils'; import { getCallingxLib, type EventData, type EventParams, } from './libs/callingx'; import { Platform } from 'react-native'; import { onRingNotificationReceived } from './internal/android'; type PushConfig = NonNullable; const logger = videoLoggerSystem.getLogger('callingx'); /** * Sets up callingx event listeners for handling call actions from the native calling UI. */ export function setupCallingExpEvents(pushConfig: NonNullable) { const hasPushProvider = (Platform.OS === 'android' && pushConfig.android?.pushProviderName) || (Platform.OS === 'ios' && pushConfig.ios?.pushProviderName); const hasOngoingCalls = (Platform.OS === 'android' && pushConfig.android?.enableOngoingCalls) || (Platform.OS === 'ios' && pushConfig.ios?.enableOngoingCalls); if (!hasPushProvider && !hasOngoingCalls) { return; } const callingx = getCallingxLib(); callingx.addEventListener('answerCall', (params) => { onAcceptCall(pushConfig)(params); }); callingx.addEventListener('endCall', (params) => { onEndCall(pushConfig)(params); }); callingx.addEventListener( 'didActivateAudioSession', onDidActivateAudioSession, ); callingx.addEventListener( 'didDeactivateAudioSession', onDidDeactivateAudioSession, ); callingx.addEventListener('providerReset', (params) => { onProviderReset(pushConfig)(params); }); //NOTE: until getInitialEvents invocation, events are delayed and won't be sent to event listeners, this is a way to make sure none of required events are missed //in most cases there will be no delayed answers or ends, but if so we don't want to miss any of them const events = callingx.getInitialEvents(); events.forEach((event: EventData) => { const { eventName, params } = event; logger.debug(`${eventName} delayed event`); if (eventName === 'answerCall') { onAcceptCall(pushConfig)(params as EventParams['answerCall']); } else if (eventName === 'endCall') { onEndCall(pushConfig)(params as EventParams['endCall']); } else if (eventName === 'didActivateAudioSession') { onDidActivateAudioSession(); } else if (eventName === 'didDeactivateAudioSession') { onDidDeactivateAudioSession(); } else if (eventName === 'providerReset') { onProviderReset(pushConfig)(params as EventParams['providerReset']); } else if (eventName === 'ringCallPushReceived') { onRingNotificationReceived(params).catch((error) => { logger.error(`Error in onRingNotificationReceived: ${error}`); }); } }); } const onDidActivateAudioSession = () => { logger.debug('callingExpDidActivateAudioSession'); }; const onDidDeactivateAudioSession = () => { logger.debug('callingExpDidDeactivateAudioSession'); }; const onProviderReset = (pushConfig: PushConfig) => async ({ callCids }: EventParams['providerReset']) => { logger.debug(`onProviderReset event callCids: ${callCids?.join(', ')}`); if (!callCids?.length) { return; } let videoClient: StreamVideoClient | undefined; try { videoClient = await pushConfig.createStreamVideoClient(); } catch (e) { logger.error('onProviderReset: failed to resolve video client', e); return; } if (!videoClient) { return; } for (const cid of callCids) { const call = videoClient.state.calls.find((c) => c.cid === cid); if (call && call.state.callingState !== CallingState.LEFT) { call?.leave().catch((error: unknown) => { logger.error(`onProviderReset: error leaving call ${cid}`, error); }); } } }; const onAcceptCall = (pushConfig: PushConfig) => ({ callId: call_cid, source }: EventParams['answerCall']) => { logger.debug(`onAcceptCall event call_cid: ${call_cid} source: ${source}`); if (source === 'app' || !call_cid) { // App-initiated actions are fulfilled on the native side immediately — nothing to do here return; } const callingx = getCallingxLib(); clearPushWSEventSubscriptions(call_cid); processCallFromPushInBackground( pushConfig, call_cid, 'accept', (didFail) => { callingx.fulfillAnswerCallAction(call_cid, didFail); }, ); }; const onEndCall = (pushConfig: PushConfig) => ({ callId: call_cid, source }: EventParams['endCall']) => { logger.debug(`onEndCall event call_cid: ${call_cid} source: ${source}`); if (source === 'app' || !call_cid) { // App-initiated actions are fulfilled on the native side immediately — nothing to do here return; } const callingx = getCallingxLib(); clearPushWSEventSubscriptions(call_cid); processCallFromPushInBackground( pushConfig, call_cid, 'decline', (didFail) => { callingx.fulfillEndCallAction(call_cid, didFail); }, ); };