Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(call): stability fixes to prevent call drops (#3370)

Will Hutchinson3a30daacc7a7
diff --git a/js/app/packages/channel/Call/CallContext.tsx b/js/app/packages/channel/Call/CallContext.tsx
index 6faf60428b..7db7630479 100644
--- a/js/app/packages/channel/Call/CallContext.tsx
+++ b/js/app/packages/channel/Call/CallContext.tsx
@@ -69,6 +69,27 @@ type NativeAudioProcessingSettings = MediaTrackSettings & {
voiceIsolation?: boolean;
};
+type NativeAudioProcessingSupportedConstraints =
+ MediaTrackSupportedConstraints & {
+ voiceIsolation?: boolean;
+ };
+
+function supportedNativeAudioProcessingConstraints(): NativeAudioProcessingSupportedConstraints | null {
+ return (
+ (navigator.mediaDevices?.getSupportedConstraints?.() as
+ | NativeAudioProcessingSupportedConstraints
+ | undefined) ?? null
+ );
+}
+
+function supportsNativeAudioProcessingConstraint(
+ name: keyof NativeAudioProcessingSupportedConstraints
+): boolean {
+ const supported = supportedNativeAudioProcessingConstraints();
+ if (!supported) return name !== 'voiceIsolation';
+ return supported[name] === true;
+}
+
function normalizeNoiseSuppressionMode(
value: unknown
): MicNoiseSuppressionMode {
@@ -98,10 +119,16 @@ function microphoneCaptureOptions(
// AGC can raise residual background artifacts during quiet speech/silence.
// Keep it disabled for every mode; Krisp/browser NS should not be stacked
// with a gain stage that makes suppressed noise audible again.
- autoGainControl: false,
+ ...(supportsNativeAudioProcessingConstraint('autoGainControl')
+ ? { autoGainControl: false }
+ : {}),
echoCancellation: true,
- noiseSuppression: useBrowserProcessing,
- voiceIsolation: useBrowserProcessing,
+ ...(supportsNativeAudioProcessingConstraint('noiseSuppression')
+ ? { noiseSuppression: useBrowserProcessing }
+ : {}),
+ ...(supportsNativeAudioProcessingConstraint('voiceIsolation')
+ ? { voiceIsolation: useBrowserProcessing }
+ : {}),
...(deviceId ? { deviceId: { exact: deviceId } } : {}),
};
}
@@ -112,9 +139,15 @@ function nativeAudioProcessingConstraints(
const useBrowserProcessing = mode === 'browser';
return {
- autoGainControl: false,
- noiseSuppression: useBrowserProcessing,
- voiceIsolation: useBrowserProcessing,
+ ...(supportsNativeAudioProcessingConstraint('autoGainControl')
+ ? { autoGainControl: false }
+ : {}),
+ ...(supportsNativeAudioProcessingConstraint('noiseSuppression')
+ ? { noiseSuppression: useBrowserProcessing }
+ : {}),
+ ...(supportsNativeAudioProcessingConstraint('voiceIsolation')
+ ? { voiceIsolation: useBrowserProcessing }
+ : {}),
};
}
@@ -123,6 +156,21 @@ function getLocalMicTrack(r: Room): LocalTrack | undefined {
?.track as LocalTrack | undefined;
}
+function isActiveCallConnectionState(state: ConnectionState): boolean {
+ return (
+ state === ConnectionState.Connecting ||
+ state === ConnectionState.Connected ||
+ state === ConnectionState.Reconnecting ||
+ state === ConnectionState.SignalReconnecting
+ );
+}
+
+function isLiveLocalTrack(
+ track: LocalTrack | null | undefined
+): track is LocalTrack {
+ return track?.mediaStreamTrack?.readyState === 'live';
+}
+
async function applyNativeAudioProcessingToMicTrack(
r: Room,
mode: MicNoiseSuppressionMode
@@ -356,6 +404,21 @@ function createCallState() {
const [blurProcessor, setBlurProcessor] =
createSignal<BackgroundProcessorWrapper | null>(null);
+ let mediaSetupVersion = 0;
+
+ function nextMediaSetupVersion() {
+ mediaSetupVersion += 1;
+ return mediaSetupVersion;
+ }
+
+ function cancelPendingMediaSetup() {
+ mediaSetupVersion += 1;
+ }
+
+ function isCurrentMediaSetup(targetRoom: Room, setupVersion: number) {
+ return room() === targetRoom && mediaSetupVersion === setupVersion;
+ }
+
function currentMicrophoneCaptureOptions(
deviceId?: string | null
): AudioCaptureOptions {
@@ -409,23 +472,29 @@ function createCallState() {
/** Apply the preferred mic noise suppression mode to the current mic track. */
async function ensureNoiseSuppressionOnMicTrack(r: Room) {
+ if (room() !== r) return;
+
const preferredMode = persistedNoiseSuppressionMode();
if (preferredMode === 'off') {
setStore('noiseSuppressionMode', 'off');
await detachKrispFromMicTrack(r);
+ if (room() !== r) return;
await applyNativeAudioProcessingToMicTrack(r, 'off');
+ if (room() !== r) return;
logMicAudioProcessing('noise-suppression-off', r);
return;
}
const micTrack = getLocalMicTrack(r);
- if (!micTrack) return;
+ if (!isLiveLocalTrack(micTrack)) return;
if (preferredMode === 'browser' || !isKrispNoiseFilterSupported()) {
setStore('noiseSuppressionMode', 'browser');
await detachKrispFromMicTrack(r);
+ if (room() !== r) return;
await applyNativeAudioProcessingToMicTrack(r, 'browser');
+ if (room() !== r) return;
logMicAudioProcessing('browser-noise-suppression-enabled', r, {
reason: preferredMode === 'browser' ? 'preferred' : 'krisp-unsupported',
});
@@ -435,6 +504,7 @@ function createCallState() {
const existing = krispFilter();
if (existing && micTrack.getProcessor() === existing) {
await applyNativeAudioProcessingToMicTrack(r, 'krisp');
+ if (room() !== r || !isLiveLocalTrack(micTrack)) return;
await existing.setEnabled(true);
setStore('noiseSuppressionMode', 'krisp');
logMicAudioProcessing('krisp-noise-suppression-reused', r);
@@ -444,9 +514,11 @@ function createCallState() {
// Krisp should be the only noise suppression layer. Disable native browser
// NS / voice isolation / AGC before attaching it to avoid cascaded filters.
await applyNativeAudioProcessingToMicTrack(r, 'krisp');
+ if (room() !== r || !isLiveLocalTrack(micTrack)) return;
try {
await detachKrispFromMicTrack(r);
+ if (room() !== r || !isLiveLocalTrack(micTrack)) return;
// `quality` is model size/CPU cost, not suppression strength. The default
// medium model avoids the CPU pressure/dropouts that made voices sound
@@ -454,15 +526,23 @@ function createCallState() {
const krisp = KrispNoiseFilter({ quality: 'medium' });
setKrispFilter(krisp);
await micTrack.setProcessor(krisp);
+ if (room() !== r || !isLiveLocalTrack(micTrack)) {
+ await krisp.destroy();
+ if (krispFilter() === krisp) setKrispFilter(null);
+ return;
+ }
await krisp.setEnabled(true);
setStore('noiseSuppressionMode', 'krisp');
logMicAudioProcessing('krisp-noise-suppression-attached', r);
} catch (e) {
+ if (room() !== r) return;
console.error('failed to re-attach Krisp noise filter', e);
// Fall back to a single browser-native layer if Krisp fails.
await detachKrispFromMicTrack(r);
+ if (room() !== r) return;
setStore('noiseSuppressionMode', 'browser');
await applyNativeAudioProcessingToMicTrack(r, 'browser');
+ if (room() !== r) return;
logMicAudioProcessing('browser-noise-suppression-fallback', r, {
reason: 'krisp-attach-failed',
});
@@ -511,7 +591,8 @@ function createCallState() {
if (effect.type === 'none') return true;
const camPub = r.localParticipant.getTrackPublication(Track.Source.Camera);
- if (!camPub?.track) return true;
+ const camTrack = camPub?.track as LocalTrack | undefined;
+ if (!isLiveLocalTrack(camTrack)) return true;
const processorOptions =
effect.type === 'blur'
@@ -522,6 +603,8 @@ function createCallState() {
: { mode: 'virtual-background' as const, imagePath: effect.path };
try {
+ if (room() !== r || !isLiveLocalTrack(camTrack)) return true;
+
const existing = blurProcessor();
// If we have a processor and the track hasn't changed, use switchTo()
@@ -534,20 +617,28 @@ function createCallState() {
// Destroy old processor if it exists (track changed or force recreate)
if (existing) {
await existing.destroy();
- setBlurProcessor(null);
+ if (blurProcessor() === existing) setBlurProcessor(null);
}
+ if (room() !== r || !isLiveLocalTrack(camTrack)) return true;
+
// Create and attach a new processor.
const { BackgroundProcessor, ProcessorWrapper } = await import(
'@livekit/track-processors'
);
if (!ProcessorWrapper.isSupported) return false;
+ if (room() !== r || !isLiveLocalTrack(camTrack)) return true;
const processor = BackgroundProcessor(processorOptions);
- await (camPub.track as LocalTrack).setProcessor(processor);
+ await camTrack.setProcessor(processor);
+ if (room() !== r || !isLiveLocalTrack(camTrack)) {
+ await processor.destroy();
+ return true;
+ }
setBlurProcessor(processor);
return true;
} catch (e) {
+ if (room() !== r) return true;
console.error('failed to attach background effect processor', e);
return false;
}
@@ -594,6 +685,11 @@ function createCallState() {
function attachRoomListeners(r: Room) {
r.on(RoomEvent.ConnectionStateChanged, (state: ConnectionState) => {
+ console.debug('[call] connection state changed', {
+ state,
+ room: store.activeChannelId,
+ call: store.activeCallId,
+ });
setStore('connectionState', state);
});
@@ -618,10 +714,18 @@ function createCallState() {
}
bumpTrackVersion();
});
- r.on(RoomEvent.Disconnected, resetState);
+ r.on(RoomEvent.Disconnected, (reason?: unknown) => {
+ console.warn('[call] room disconnected', {
+ reason,
+ room: store.activeChannelId,
+ call: store.activeCallId,
+ });
+ resetState();
+ });
}
function destroyRoom() {
+ cancelPendingMediaSetup();
const krisp = krispFilter();
if (krisp) {
krisp.destroy().catch((e) => {
@@ -797,23 +901,39 @@ function createCallState() {
async function connect(tokenResponse: CallTokenResponse) {
const existingRoom = room();
- // If switching to a different channel, tear down the old room entirely
- if (existingRoom && store.activeChannelId !== tokenResponse.channelId) {
+ if (
+ existingRoom &&
+ store.activeChannelId === tokenResponse.channelId &&
+ isActiveCallConnectionState(store.connectionState)
+ ) {
+ // A duplicate join can arrive while LiveKit is already connected or
+ // recovering its signaling connection. Do not call room.connect() again;
+ // that replaces the SDK's reconnection attempt and can wedge the peer
+ // connection until the user manually leaves/rejoins.
+ console.debug('[call] ignoring duplicate connect for active room', {
+ channelId: tokenResponse.channelId,
+ state: store.connectionState,
+ });
+ setStore('activeCallId', tokenResponse.callId);
+ setStore('optimisticJoinChannelId', null);
+ setStore('joinError', null);
+ return;
+ }
+
+ // If switching channels, or if a previous disconnected room instance is
+ // still hanging around after a failed reconnect, tear it down and build a
+ // fresh Room. This gives retry/auto-rejoin the same clean slate as a manual
+ // leave + join.
+ if (existingRoom) {
await existingRoom.disconnect();
destroyRoom();
}
- let targetRoom: Room;
- if (room()) {
- // Reuse existing room instance (same channel, e.g. leave then rejoin)
- targetRoom = room()!;
- } else {
- targetRoom = new Room({
- audioCaptureDefaults: currentMicrophoneCaptureOptions(),
- });
- attachRoomListeners(targetRoom);
- setRoom(targetRoom);
- }
+ const targetRoom = new Room({
+ audioCaptureDefaults: currentMicrophoneCaptureOptions(),
+ });
+ attachRoomListeners(targetRoom);
+ setRoom(targetRoom);
setStore('activeChannelId', tokenResponse.channelId);
setStore('activeCallId', tokenResponse.callId);
@@ -844,13 +964,14 @@ function createCallState() {
// join mutation timeout can fire after the user is already in the room and
// run failed-join cleanup, which calls DELETE /call/:channel and kicks the
// user out. Run the non-critical setup in the background instead.
- void finishLocalMediaSetup(targetRoom).catch((e) => {
+ const setupVersion = nextMediaSetupVersion();
+ void finishLocalMediaSetup(targetRoom, setupVersion).catch((e) => {
console.error('failed to finish local call media setup', e);
});
}
- async function finishLocalMediaSetup(targetRoom: Room) {
- if (room() !== targetRoom) return;
+ async function finishLocalMediaSetup(targetRoom: Room, setupVersion: number) {
+ if (!isCurrentMediaSetup(targetRoom, setupVersion)) return;
// Enable microphone by default.
try {
@@ -861,7 +982,7 @@ function createCallState() {
} catch (e) {
console.error('failed to enable microphone', e);
}
- if (room() !== targetRoom) return;
+ if (!isCurrentMediaSetup(targetRoom, setupVersion)) return;
if (store.isAudioMuted) {
// The user muted while the initial enable was in flight; honor their
// latest intent instead of letting background setup re-open the mic.
@@ -875,17 +996,18 @@ function createCallState() {
// ensureNoiseSuppressionOnMicTrack is a no-op when the user's pref is off.
await ensureNoiseSuppressionOnMicTrack(targetRoom);
}
- if (room() !== targetRoom) return;
+ if (!isCurrentMediaSetup(targetRoom, setupVersion)) return;
// Enumerate available devices and track active ones.
await enumerateDevices();
- if (room() !== targetRoom) return;
+ if (!isCurrentMediaSetup(targetRoom, setupVersion)) return;
trackActiveDevices(targetRoom);
}
async function disconnect() {
const r = room();
if (r) {
+ cancelPendingMediaSetup();
try {
await r.disconnect();
} finally {
@@ -1045,6 +1167,7 @@ function createCallState() {
const handleBeforeUnload = () => {
const r = room();
if (r) {
+ cancelPendingMediaSetup();
r.disconnect();
}
};
@@ -1056,6 +1179,7 @@ function createCallState() {
'devicechange',
handleDeviceChange
);
+ cancelPendingMediaSetup();
const r = room();
if (r) {
r.disconnect();
@@ -1070,7 +1194,7 @@ function createCallState() {
room,
connectionState: () => store.connectionState,
isInCall: () =>
- store.connectionState === ConnectionState.Connected ||
+ isActiveCallConnectionState(store.connectionState) ||
store.optimisticJoinChannelId !== null,
activeChannelId: () =>
store.activeChannelId ?? store.optimisticJoinChannelId,
@@ -1096,7 +1220,11 @@ function createCallState() {
activeAudioInputDeviceId: () => store.activeAudioInputDeviceId,
activeAudioOutputDeviceId: () => store.activeAudioOutputDeviceId,
activeVideoInputDeviceId: () => store.activeVideoInputDeviceId,
- isConnecting: () => store.optimisticJoinChannelId !== null,
+ isConnecting: () =>
+ store.optimisticJoinChannelId !== null ||
+ store.connectionState === ConnectionState.Connecting ||
+ store.connectionState === ConnectionState.Reconnecting ||
+ store.connectionState === ConnectionState.SignalReconnecting,
// mutations
connect,
diff --git a/js/app/packages/channel/Call/use-call.ts b/js/app/packages/channel/Call/use-call.ts
index 8f1b71b4d9..aac10ed609 100644
--- a/js/app/packages/channel/Call/use-call.ts
+++ b/js/app/packages/channel/Call/use-call.ts
@@ -3,7 +3,7 @@ import { useLeaveCallMutation } from '@queries/call/call';
import { queryClient } from '@queries/client';
import { callServiceClient } from '@service-call/client';
import { useMutation } from '@tanstack/solid-query';
-import { RoomEvent } from 'livekit-client';
+import { DisconnectReason, RoomEvent } from 'livekit-client';
import { createEffect, createSignal, onCleanup } from 'solid-js';
import { useCallContext } from './CallContext';
import { endCallKitCall, registerCallKitCallEndedHandler } from './use-callkit';
@@ -20,6 +20,32 @@ type JoinCallContext = {
};
const JOIN_TIMEOUT_MS = 15_000;
+const AUTO_REJOIN_DELAY_MS = 750;
+const MAX_AUTO_REJOIN_ATTEMPTS = 1;
+
+type ActiveJoinAttempt = {
+ channelId: string;
+ promise: Promise<void>;
+};
+
+function shouldAutoRejoin(reason?: DisconnectReason) {
+ switch (reason) {
+ case DisconnectReason.CLIENT_INITIATED:
+ case DisconnectReason.DUPLICATE_IDENTITY:
+ case DisconnectReason.PARTICIPANT_REMOVED:
+ case DisconnectReason.ROOM_DELETED:
+ case DisconnectReason.ROOM_CLOSED:
+ return false;
+ default:
+ return true;
+ }
+}
+
+// Module-level guard: only one join can be in flight at a time across all
+// useCall() instances. The call button, call tab, auto-join flow, and in-call
+// panel can all mount their own hook; without this, two components can race and
+// call room.connect() while LiveKit is already reconnecting.
+let activeJoinAttempt: ActiveJoinAttempt | null = null;
// Module-level guard: only one leave can be in flight at a time across all
// useCall() instances. Prevents a user-initiated leave and a concurrent
@@ -39,6 +65,35 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
// Track the disconnect listener so we can swap it when the room changes.
let cleanupDisconnectListener: (() => void) | null = null;
+ let autoRejoinAttempts = 0;
+ let autoRejoinTimer: ReturnType<typeof globalThis.setTimeout> | null = null;
+
+ function clearAutoRejoinTimer() {
+ if (!autoRejoinTimer) return;
+ globalThis.clearTimeout(autoRejoinTimer);
+ autoRejoinTimer = null;
+ }
+
+ function scheduleAutoRejoin(reason?: DisconnectReason) {
+ if (leaveInFlight) return;
+
+ if (!shouldAutoRejoin(reason)) {
+ options?.onLeave?.();
+ return;
+ }
+
+ if (autoRejoinAttempts >= MAX_AUTO_REJOIN_ATTEMPTS) {
+ options?.onLeave?.();
+ return;
+ }
+
+ autoRejoinAttempts += 1;
+ callCtx.setJoinError('Call disconnected. Reconnecting…');
+ autoRejoinTimer = globalThis.setTimeout(() => {
+ autoRejoinTimer = null;
+ joinCall().catch((e) => console.error('auto-rejoin call failed', e));
+ }, AUTO_REJOIN_DELAY_MS);
+ }
function attachDisconnectListener() {
cleanupDisconnectListener?.();
@@ -47,7 +102,14 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
const room = callCtx.room();
if (!room) return;
- const handleDisconnect = () => options?.onLeave?.();
+ const handleDisconnect = (reason?: DisconnectReason) => {
+ // This listener is detached before explicit leave, so reaching this path
+ // means LiveKit gave up on recovery. Try one hard rejoin with a fresh
+ // token/Room instead of immediately dumping the user out of the call UI.
+ cleanupDisconnectListener?.();
+ cleanupDisconnectListener = null;
+ scheduleAutoRejoin(reason);
+ };
room.on(RoomEvent.Disconnected, handleDisconnect);
cleanupDisconnectListener = () =>
room.off(RoomEvent.Disconnected, handleDisconnect);
@@ -60,7 +122,10 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
attachDisconnectListener();
}
- onCleanup(() => cleanupDisconnectListener?.());
+ onCleanup(() => {
+ cleanupDisconnectListener?.();
+ clearAutoRejoinTimer();
+ });
createEffect(() => {
if (!callCtx.isInCall() || callCtx.activeChannelId() !== channelId())
@@ -114,6 +179,8 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
return { channelId: id };
},
onSuccess: () => {
+ autoRejoinAttempts = 0;
+ clearAutoRejoinTimer();
void queryClient.invalidateQueries({ queryKey: ['call', 'active'] });
attachDisconnectListener();
},
@@ -144,6 +211,18 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
}));
const joinCall = async () => {
+ clearAutoRejoinTimer();
+ const id = channelId();
+ const existing = activeJoinAttempt;
+ if (existing && existing.channelId !== id) {
+ throw new Error('Already joining another call');
+ }
+
+ const joinPromise = existing?.promise ?? joinCallMutation.mutateAsync(id);
+ if (!existing) {
+ activeJoinAttempt = { channelId: id, promise: joinPromise };
+ }
+
setJoinUiPending(true);
const safetyMs = JOIN_TIMEOUT_MS + 5_000;
const safetyTimer = globalThis.setTimeout(
@@ -151,8 +230,11 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
safetyMs
);
try {
- await joinCallMutation.mutateAsync(channelId());
+ await joinPromise;
} finally {
+ if (activeJoinAttempt?.promise === joinPromise) {
+ activeJoinAttempt = null;
+ }
globalThis.clearTimeout(safetyTimer);
setJoinUiPending(false);
}
@@ -166,6 +248,7 @@ export function useCall(channelId: () => string, options?: UseCallOptions) {
// doesn't double-fire onLeave.
cleanupDisconnectListener?.();
cleanupDisconnectListener = null;
+ clearAutoRejoinTimer();
// Dismiss the native CallKit call sheet if the user left from within the app.
// When the leave is initiated by CXEndCallAction, the native sheet is already
// ending, so avoid sending a second native end request back to CallKit.
diff --git a/js/app/packages/queries/call/call.ts b/js/app/packages/queries/call/call.ts
index e8de04659b..0076dc5f28 100644
--- a/js/app/packages/queries/call/call.ts
+++ b/js/app/packages/queries/call/call.ts
@@ -38,11 +38,27 @@ export function useJoinCallMutation() {
}));
}
+function isNotFoundResultError(error: unknown) {
+ return (
+ error instanceof ThrownResultError &&
+ error.errors.some((err) => err.code === 'NOT_FOUND')
+ );
+}
+
export function useLeaveCallMutation() {
return useMutation(() => ({
gcTime: 0,
- mutationFn: (channelId: string) =>
- throwOnErr(() => callServiceClient.leaveCall(channelId)),
+ mutationFn: async (channelId: string) => {
+ try {
+ return await throwOnErr(() => callServiceClient.leaveCall(channelId));
+ } catch (error) {
+ // Leaving a call should be idempotent. If LiveKit/server cleanup already
+ // removed us, the UI should still finish disconnecting instead of
+ // surfacing a noisy "Resource not found" control failure.
+ if (isNotFoundResultError(error)) return undefined;
+ throw error;
+ }
+ },
onSuccess() {
queryClient.invalidateQueries({ queryKey: ['call', 'active'] });
},