// Metro module 16324 (f16325). import { NativeEventEmitter, DeviceEventEmitter } from "react-native"; import SoundpackStore from "../../soundpacks/SoundpackStore"; import ApplicationStreamingStore from "../../../stores/ApplicationStreamingStore"; import AuthenticationStore from "../../../stores/AuthenticationStore"; import CallStore from "../../../stores/CallStore"; import ChannelStore from "../../../stores/ChannelStore"; import MediaEngineStore from "../../../stores/MediaEngineStore"; import NotificationSettingsStore from "../../../stores/NotificationSettingsStore"; import RTCConnectionStore from "../../../stores/RTCConnectionStore"; import RelationshipStore from "../../../stores/RelationshipStore"; import StreamerModeStore from "../../../stores/StreamerModeStore"; import UserStore from "../../../stores/UserStore"; import { ApplicationStreamStates } from "../../../Constants"; import { MediaEngineContextTypes } from "../../../_bundle/Module4250"; import Logger from "../../debug/Logger"; import Module10610 from "../../../_bundle/Module10610"; import AutomaticLifecycleManager from "../../../lib/AutomaticLifecycleManager"; import Module16325 from "../../../_bundle/Module16325"; import SelectedChannelActionCreators from "../../../actions/SelectedChannelActionCreators"; import CallActionCreators from "../../../actions/CallActionCreators"; import { navigateToVoiceChannel } from "../../../utils/native/PrivateChannelCallUtils"; import AudioActionCreators from "../../../actions/AudioActionCreators"; import { getVideoPermission } from "../../video_calls/useHasVideoPermission"; import { getOSRequirement, startStream, stopScreenshare } from "../../video_calls/native/useScreenshareUtils"; import { isMetaQuest } from "../../device/MetaQuestUtils.android"; import { computeChannelName } from "../../channel/useChannelName"; import { createSoundForPack } from "../../sound_playback/SoundUtils"; function $mk117270($env) { return (a0) => { c11741_25.warn("Failed to end orphaned call:", a0); }; } let c11741_28; let env11741 = {}; const c11741_18 = "telecom-end-call-requested"; const c11741_19 = "telecom-set-foreground-requested"; const c11741_20 = "telecom-mic-mute-requested"; const c11741_21 = "telecom-screen-share-requested"; const c11741_22 = "telecom-incoming-call-answered"; const c11741_23 = "telecom-incoming-call-rejected"; const c11741_24 = "telecom-answer-call-requested"; const logger = new Logger("TelecomManager"); const c11741_25 = logger; logger.enableNativeLogger(true); const c11741_26 = { Ringing: 0, 0: "Ringing", Connecting: 1, 1: "Connecting", Connected: 2, 2: "Connected" }; const c11741_27 = new NativeEventEmitter(Module10610); class TelecomManager extends AutomaticLifecycleManager { stores = new Map().set(RTCConnectionStore, () => this.reconcileTelecomState()).set(MediaEngineStore, () => this.handleMuteStoreChange()).set(ApplicationStreamingStore, () => this.handleScreenShareStoreChange()).set(CallStore, () => this.handleIncomingCallStoreChange()); actions = { CALL_CREATE: (a0) => this.handleCallCreate(a0), CALL_UPDATE: (a0) => this.handleCallUpdate(a0), CALL_DELETE: (a0) => this.handleCallDelete(a0) }; currentCall = null; isInitialized = false; lastMuteState = null; registeredIncomingCallIds = new Set(); lastScreenShareActive = null; pendingScreenShareOffSyncTimeout = null; reconcilePromise = null; needsReconcile = false; hostDestroySubscription = null; pendingMutePreference = null; ringtone = null; handleHostDestroy = () => { if (this.isEnabled() && this.currentCall != null) { if (this.currentCall.state === c11741_26.Ringing) { c11741_25.info("Activity destroyed with ringing call, cancelling incoming call"); this.cancelIncomingCall(this.currentCall.channelId); return void 0; } c11741_25.info("Activity destroyed with active call, disconnecting from voice channel"); SelectedChannelActionCreators.disconnect(); } }; handleEndCallRequested = (a0) => { c11741_25.info("Received end call request from Call Bar:", a0.callId); if (this.currentCall != null && a0.callId === this.currentCall.channelId) { if (this.currentCall.state === c11741_26.Ringing) { c11741_25.info("Rejecting ringing call from Call Bar:", this.currentCall.channelId); CallActionCreators.stopRinging(this.currentCall.channelId); this.clearCall(this.currentCall.channelId); } else { SelectedChannelActionCreators.disconnect(); } return void 0; } }; handleSetForegroundRequested = (a0) => { c11741_25.info("Received set foreground request from Call Bar"); if (this.currentCall != null && a0.callId === this.currentCall.channelId) { const channel = ChannelStore.getChannel(this.currentCall.channelId); if (channel != null) { navigateToVoiceChannel(channel, "Call Bar"); } } }; handleMicMuteRequested = (a0) => { c11741_25.info("Received mic mute request from Call Bar:", a0.callId, "isMuted:", a0.isMuted); if (this.currentCall != null && a0.callId === this.currentCall.channelId) { const state = this.currentCall.state; if (!(state !== c11741_26.Ringing && this.currentCall.state !== c11741_26.Connecting)) { this.pendingMutePreference = a0.isMuted; } if (MediaEngineStore.isSelfMute() !== a0.isMuted) { c11741_25.info("Updating Call Bar -> Discord mute state:", a0.isMuted); AudioActionCreators.toggleSelfMute(); } } }; handleScreenShareRequested = (a0) => { L16: { c11741_25.info("Received screen share request from Call Bar:", a0.callId, "isEnabled:", a0.isEnabled); if (this.currentCall != null && a0.callId === this.currentCall.channelId) { const channel = ChannelStore.getChannel(this.currentCall.channelId); if (channel != null) { const currentUserActiveStream = ApplicationStreamingStore.getCurrentUserActiveStream(); const r1 = currentUserActiveStream != null && currentUserActiveStream.state === ApplicationStreamStates.ACTIVE; if (a0.isEnabled && !r1) { const videoPermission = getVideoPermission(channel); if (getOSRequirement()) { if (videoPermission) { c11741_25.info("Starting screen share from Call Bar"); startStream(); break L16; } c11741_25.warn("Cannot start screen share from Call Bar: user lacks streaming permission in this channel"); return void 0; } c11741_25.warn("Cannot start screen share from Call Bar: OS version does not meet requirements"); return void 0; } const isEnabled2 = a0.isEnabled; if (isEnabled2 ? !isEnabled2 : r1) { c11741_25.info("Stopping screen share from Call Bar"); stopScreenshare(); } } } } }; handleAnswerCallRequested = (a0) => { c11741_25.info("Received answer call request from Call Bar:", a0.callId); if (this.currentCall != null && a0.callId === this.currentCall.channelId) { const state = this.currentCall.state; if (state === c11741_26.Ringing) { this.stopRingtone(); this.currentCall.state = c11741_26.Connecting; c11741_25.info("Answering incoming call, joining voice channel:", this.currentCall.channelId); SelectedChannelActionCreators.selectVoiceChannel(this.currentCall.channelId); } else { c11741_25.warn("Answer requested but call is not ringing:", this.currentCall.state); } } }; handleCallCreate = (a0) => { if (this.isEnabled() && isMetaQuest()) { this.processIncomingRing(a0.channelId, a0.ongoingRings); } }; handleCallUpdate = (a0) => { if (this.isEnabled() && isMetaQuest()) { this.processIncomingRing(a0.channelId, a0.ongoingRings); } }; handleCallDelete = (a0) => { if (this.isEnabled() && isMetaQuest() && this.currentCall?.channelId === a0.channelId && this.isPendingIncomingCall(this.currentCall)) { c11741_25.info("Pending incoming call deleted, cancelling incoming call:", a0.channelId); this.cancelIncomingCall(a0.channelId); } }; handleIncomingCallAnswered = (a0) => { c11741_25.info("Received incoming call answered from telecom:", a0.callId); this.registeredIncomingCallIds.delete(a0.callId); Module16325.endCall(a0.callId).catch((error) => { c11741_25.warn("Failed to end answered telecom call:", error); }); SelectedChannelActionCreators.selectVoiceChannel(a0.callId); }; handleIncomingCallRejected = (a0) => { c11741_25.info("Received incoming call rejected from telecom:", a0.callId); this.registeredIncomingCallIds.delete(a0.callId); CallActionCreators.stopRinging(a0.callId).catch((error) => { c11741_25.warn("Failed to stop ringing after telecom reject:", error); }); }; _initialize() { if (!this.isInitialized) { c11741_25.info("Initializing CallKitManager using Telecom framework"); const hostDestroySubscription = this.hostDestroySubscription; if (hostDestroySubscription != null) { hostDestroySubscription.remove(); } this.hostDestroySubscription = c11741_27.addListener("onHostDestroy", this.handleHostDestroy); DeviceEventEmitter.addListener(c11741_18, this.handleEndCallRequested); DeviceEventEmitter.addListener(c11741_19, this.handleSetForegroundRequested); DeviceEventEmitter.addListener(c11741_20, this.handleMicMuteRequested); DeviceEventEmitter.addListener(c11741_21, this.handleScreenShareRequested); DeviceEventEmitter.addListener(c11741_22, this.handleIncomingCallAnswered); DeviceEventEmitter.addListener(c11741_23, this.handleIncomingCallRejected); DeviceEventEmitter.addListener(c11741_24, this.handleAnswerCallRequested); this.isInitialized = true; } } _terminate() { if (this.isInitialized) { const hostDestroySubscription = this.hostDestroySubscription; if (hostDestroySubscription != null) { hostDestroySubscription.remove(); } this.hostDestroySubscription = null; DeviceEventEmitter.removeAllListeners(c11741_18); DeviceEventEmitter.removeAllListeners(c11741_19); DeviceEventEmitter.removeAllListeners(c11741_20); DeviceEventEmitter.removeAllListeners(c11741_21); DeviceEventEmitter.removeAllListeners(c11741_22); DeviceEventEmitter.removeAllListeners(c11741_23); DeviceEventEmitter.removeAllListeners(c11741_24); for (const registeredIncomingCallId of this.registeredIncomingCallIds) { Module16325.endCall(registeredIncomingCallId).catch((error) => { c11741_25.warn("Failed to end telecom incoming call on terminate:", error); }); } this.registeredIncomingCallIds.clear(); this.clearPendingScreenShareOffSync(); this.stopRingtone(); this.reportCallEnded(); this.reconcilePromise = null; this.needsReconcile = false; this.isInitialized = false; } } isEnabled() { return this.isInitialized; } processIncomingRing(a0, a1) { const id = AuthenticationStore.getId(); if (id in a1 && a1[id] != null) { if (this.currentCall?.channelId === a0 && this.currentCall.state !== c11741_26.Ringing) { return void 0; } if (RTCConnectionStore.getChannelId() != null) { return void 0; } if (this.currentCall?.channelId === a0 && this.currentCall.state === c11741_26.Ringing) { return void 0; } this.reportIncomingCall(a0); } else if (this.currentCall?.channelId === a0 && this.currentCall.state === c11741_26.Ringing) { c11741_25.info("Call no longer ringing, cancelling incoming call:", a0); this.cancelIncomingCall(a0); } } reportIncomingCall(a0) { const channel = ChannelStore.getChannel(a0); if (channel != null) { if (this.currentCall != null && this.currentCall.channelId !== a0 && this.isPendingIncomingCall(this.currentCall)) { this.cancelIncomingCall(this.currentCall.channelId); } const channelName = computeChannelName(channel, UserStore, RelationshipStore); let guildId2 = channel.getGuildId(); if (guildId2 == null) { guildId2 = null; } this.currentCall = { channelId: a0, guildId: guildId2, channelName, state: c11741_26.Ringing }; c11741_25.info("Reporting incoming call to Telecom:", a0, "callerName:", channelName); this.startRingtone(); Module16325.reportIncomingCall(a0, channelName, guildId2 != null ? { guildId: guildId2 } : null).then((result) => { if (!result) { c11741_25.warn("Failed to report incoming call: resolved false"); this.clearCall(a0); } }).catch((error) => { c11741_25.warn("Failed to report incoming call:", error); this.clearCall(a0); }); } else { c11741_25.warn("Cannot report incoming call: channel not found:", a0); } } cancelIncomingCall(a0) { c11741_25.info("Cancelling incoming call:", a0); return Module16325.cancelIncomingCall(a0).then(() => { this.clearCall(a0); return true; }).catch((error) => { c11741_25.warn("Failed to cancel incoming call:", error); this.clearCall(a0); return false; }); } isPendingIncomingCall(a0) { const state = a0.state; return state === c11741_26.Ringing || a0.state === c11741_26.Connecting; } reconcileTelecomState() { if (this.isEnabled() && isMetaQuest()) { if (this.reconcilePromise == null) { this.reconcilePromise = this.doReconcile().finally(() => { this.reconcilePromise = null; if (this.needsReconcile) { this.needsReconcile = false; this.reconcileTelecomState(); } }); } else { this.needsReconcile = true; } } } async doReconcile() { let r216; const _this = this; let guildId, c28338_1, c28338_2; L64: { L63: { L62: { L77: { L24: { L48: { L35: { let env31031; c28338_2 = void 0; const r14 = env31031 = { $parent: env11741 }; c28338_1 = r14; env31031[0] = void 0; env31031[1] = void 0; env31031[2] = void 0; const channelId2 = RTCConnectionStore.getChannelId(); const isConnected = RTCConnectionStore.isConnected(); const r58 = isConnected ? channelId2 ?? null : null; env31031[0] = r58; c11741_25.info("Reconcile: rtc=", channelId2, "connected=", isConnected, "target=", r58, "current=", _this.currentCall?.channelId, "state=", _this.currentCall?.state); if (r58 != null) { if (_this.currentCall?.channelId === r58 && _this.currentCall.state === c11741_26.Connected) { break L24; } const channelId3 = _this.currentCall?.channelId; L31: { if (channelId3 === r58) { if (_this.currentCall.state !== c11741_26.Ringing && _this.currentCall.state !== c11741_26.Connecting) { break L31; } c11741_25.info("Incoming call answered, transitioning to active:", r58); _this.stopRingtone(); _this.currentCall.state = c11741_26.Connected; _this.setIncomingCallActive(r58); break L35; } } if (_this.currentCall != null && _this.currentCall.channelId !== r58) { if (_this.isPendingIncomingCall(_this.currentCall)) { await _this.cancelIncomingCall(_this.currentCall.channelId); } else { await _this.endCall(_this.currentCall); } if (_this.currentCall?.channelId !== env31031[0]) { env31031[1] = ChannelStore.getChannel(env31031[0]); if (env31031[1] == null) { break L77; } const guildId3 = RTCConnectionStore.getGuildId(); guildId = guildId3; if (guildId3 == null) { guildId = null; } _this.currentCall = { channelId: env31031[0], guildId, channelName: computeChannelName(env31031[1], UserStore, RelationshipStore), state: c11741_26.Connecting }; } if (_this.currentCall.state !== c11741_26.Connecting) { break L64; } env31031[2] = _this.currentCall.channelId; r216 = await _this.startCall({ channelId: _this.currentCall.channelId, guildId: _this.currentCall.guildId }); } else { if (_this.currentCall?.channelId !== env31031[0]) { env31031[1] = ChannelStore.getChannel(env31031[0]); if (env31031[1] == null) { break L48; } const guildId2 = RTCConnectionStore.getGuildId(); guildId = guildId2; if (guildId2 == null) { guildId = null; } _this.currentCall = { channelId: env31031[0], guildId, channelName: computeChannelName(env31031[1], UserStore, RelationshipStore), state: c11741_26.Connecting }; } if (_this.currentCall.state !== c11741_26.Connecting) { break L24; } env31031[2] = _this.currentCall.channelId; r216 = await _this.startCall({ channelId: _this.currentCall.channelId, guildId: _this.currentCall.guildId }); } if (!r216) { break L64; } if (_this.currentCall?.channelId !== env31031[2]) { c11741_25.info("Call state changed during startCall, ending orphaned native call:", env31031[2]); Module16325.endCall(env31031[2]).catch($mk117270(env11741)); break L63; } if (RTCConnectionStore.isConnected()) { if (RTCConnectionStore.getChannelId() === env31031[2]) { _this.currentCall.state = c11741_26.Connected; _this.setCallActive(env31031[2]); break L64; } } c11741_25.info("RTCConnectionStore indicates disconnect after startCall, ending call:", env31031[2]); await _this.reportCallEnded(); break L62; } if (!(_this.currentCall != null && _this.currentCall.state !== c11741_26.Ringing && _this.currentCall.state !== c11741_26.Connecting)) { break L24; } await _this.reportCallEnded(); return void 0; } return void 0; } return void 0; } return void 0; } return void 0; } return void 0; } return void 0; } } handleIncomingCallStoreChange() { let r22; let _this2; L18: { _this2 = this; if (!isMetaQuest() && this.isEnabled()) { const id = AuthenticationStore.getId(); const set = new Set(); let r40 = CallStore.getCalls()[Symbol.iterator](); const next = r40.next; while (true) { const r43 = r40 === void 0 ? void 0 : (r22 = next.call(r40)).done ? r40 = void 0 : r22.value; if (r40 === void 0) { for (const registeredIncomingCallId of this.registeredIncomingCallIds) { const r60 = registeredIncomingCallId; if (!set.has(registeredIncomingCallId)) { const registeredIncomingCallIds = this.registeredIncomingCallIds; registeredIncomingCallIds.delete(r60); Module16325.endCall(r60).catch((error) => { c11741_25.warn("Failed to end telecom call:", error); }); } } const _loop = function _loop(a0) { if (!_this2.registeredIncomingCallIds.has(a0)) { _this2.registeredIncomingCallIds.add(a0); Module16325.registerIncomingCall(a0).then((result) => { if (!result) { c11741_25.warn("Failed to register incoming call with telecom: resolved false"); _this2.registeredIncomingCallIds.delete(a0); } }).catch((error) => { c11741_25.warn("Failed to register incoming call with telecom:", error); _this2.registeredIncomingCallIds.delete(a0); }); } }; for (const item of set) { _loop(item); } break L18; } else { try { if (r43.ringing.includes(id)) { set.add(r43.channelId); } } catch (r52) { if (r40 !== void 0 && typeof r40.return === "function") { try { r40.return(); } catch {} } throw r52; } } } } } } async startCall(c22976_0) { let r82; const _this3 = this; let c28340_2; let c28340_3 = 0; let c31032_0, c31032_1, c31032_2; L19: { L12: { try { c31032_0 = void 0; c31032_1 = void 0; const channelId2 = c22976_0.channelId; c31032_0 = channelId2; const guildId2 = c22976_0.guildId; if (_this3.currentCall?.channelId === channelId2 && _this3.currentCall.state === c11741_26.Connected) { c11741_25.info("Call already active for channel:", channelId2); break L12; } c11741_25.info("Starting Telecom call:", channelId2); c28340_3 = 1; const r64 = await Module16325.startCall(channelId2, guildId2 != null ? { guildId: guildId2 } : null); const r76 = "Native startCall returned false, clearing call state"; c31032_1 = r64; if (!c31032_1) { c11741_25.warn(r76); _this3.clearCall(c31032_0); } r82 = c31032_1; c28340_3 = 0; break L19; } catch ($e) { const r32 = $e; let r8 = r32; c28340_2 = r32; if (c28340_3 !== 0) { while (true) { try { const r100 = c28340_2; c28340_3 = 0; c31032_2 = r100; c11741_25.warn("Failed to register call with Telecom:", c31032_2); _this3.clearCall(c31032_0); } catch (r96) { r8 = r96; c28340_2 = r96; if (c28340_3 === 0) { break; } continue; } return false; } } throw r8; } } return true; } return r82; } endCall(a0) { c11741_25.info("Ending call:", a0.channelId); return Module16325.endCall(a0.channelId).then((result) => { this.clearCall(a0.channelId); return result; }).catch((error) => { c11741_25.warn("Failed to end call:", error); this.clearCall(a0.channelId); return false; }); } reportCallEnded() { c11741_25.info("Reporting call ended"); return this.currentCall == null ? Promise.resolve(true) : this.currentCall.state === c11741_26.Ringing ? this.cancelIncomingCall(this.currentCall.channelId) : this.endCall(this.currentCall); } setCallActive(a0) { if (this.currentCall?.channelId === a0) { c11741_25.info("Setting call active:", a0); const isSelfMute = MediaEngineStore.isSelfMute(); Module16325.setCallActive(a0, isSelfMute); this.lastMuteState = isSelfMute; this.lastScreenShareActive = false; } else { c11741_25.warn("setCallActive called for unknown channel:", a0); } } setIncomingCallActive(a0) { if (this.currentCall?.channelId === a0) { c11741_25.info("Setting incoming call active:", a0); const isSelfMute = MediaEngineStore.isSelfMute(); const pendingMutePreference = this.pendingMutePreference; let lastMuteState = isSelfMute; if (pendingMutePreference != null) { const pendingMutePreference2 = this.pendingMutePreference; this.pendingMutePreference = null; lastMuteState = isSelfMute; if (isSelfMute !== pendingMutePreference2) { c11741_25.info("Re-applying Telecom Bar ringing-state mute preference:", pendingMutePreference2); AudioActionCreators.setSelfMute(MediaEngineContextTypes.DEFAULT, pendingMutePreference2, false); lastMuteState = pendingMutePreference2; } } Module16325.setIncomingCallActive(a0, lastMuteState); this.lastMuteState = lastMuteState; this.lastScreenShareActive = false; } else { c11741_25.warn("setIncomingCallActive called for unknown channel:", a0); } } clearScreenShareState() { this.lastScreenShareActive = null; } clearPendingScreenShareOffSync() { if (this.pendingScreenShareOffSyncTimeout != null) { clearTimeout(this.pendingScreenShareOffSyncTimeout); this.pendingScreenShareOffSyncTimeout = null; } } clearCall(a0) { if (this.currentCall?.channelId === a0) { this.stopRingtone(); this.currentCall = null; this.lastMuteState = null; this.pendingMutePreference = null; this.clearScreenShareState(); this.clearPendingScreenShareOffSync(); } } startRingtone() { if (isMetaQuest() && this.ringtone == null && !(StreamerModeStore.disableSounds || NotificationSettingsStore.isSoundDisabled("call_ringing"))) { this.ringtone = createSoundForPack("call_ringing", SoundpackStore.getSoundpack()); this.ringtone.loop(); } } stopRingtone() { const ringtone = this.ringtone; if (ringtone != null) { this.ringtone = null; ringtone.stop(); } } handleMuteStoreChange() { if (this.isEnabled() && this.currentCall != null && this.currentCall.state === c11741_26.Connected) { const isSelfMute = MediaEngineStore.isSelfMute(); if (this.lastMuteState !== isSelfMute) { this.lastMuteState = isSelfMute; c11741_25.info("Syncing Discord -> Call Bar mute state:", isSelfMute); Module16325.setMicMuted(this.currentCall.channelId, isSelfMute); } } } handleScreenShareStoreChange() { let channelId; if (this.isEnabled() && this.currentCall != null && this.currentCall.state === c11741_26.Connected) { const currentUserActiveStream = ApplicationStreamingStore.getCurrentUserActiveStream(); const lastScreenShareActive = currentUserActiveStream != null && currentUserActiveStream.state === ApplicationStreamStates.ACTIVE; if (this.lastScreenShareActive !== lastScreenShareActive) { this.lastScreenShareActive = lastScreenShareActive; this.clearPendingScreenShareOffSync(); if (lastScreenShareActive) { c11741_25.info("Syncing Discord -> Call Bar screen share state: true"); Module16325.setScreenShareState(this.currentCall.channelId, true, true); } else { channelId = this.currentCall.channelId; this.pendingScreenShareOffSyncTimeout = setTimeout(() => { this.pendingScreenShareOffSyncTimeout = null; if (this.isEnabled() && this.currentCall?.channelId === channelId) { c11741_25.info("Syncing Discord -> Call Bar screen share state: false (delayed)"); Module16325.setScreenShareState(channelId, true, false); } }, 400); } } } } } c11741_28 = TelecomManager; const r128 = new TelecomManager(); export default r128;