A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import assert from 'node:assert/strict';
import Authentication from '@app/features/auth/state/Authentication';
import {Logger} from '@app/features/platform/utils/AppLogger';
import {
createVoiceMeshRoom,
holdVoiceMeshConversion,
isVoiceMeshRoom,
} from '@app/features/voice/engine/mesh/VoiceMeshRoom';
import {Store} from '@app/features/voice/engine/Store';
import {sendVoiceStateDisconnect} from '@app/features/voice/engine/VoiceChannelConnector';
import {
createVoiceConnectionSnapshot,
getVoiceConnectionFailedTarget,
isLatestVoiceConnectionAttempt,
isVoiceConnectionFailed,
selectVoiceConnectionServerUpdateDecision,
transitionVoiceConnectionSnapshot,
type VoiceConnectionEvent,
type VoiceConnectionLocalDisconnectReason,
type VoiceConnectionSnapshot,
} from '@app/features/voice/engine/VoiceConnectionStateMachine';
import {VoiceConnectionThrottle} from '@app/features/voice/engine/VoiceConnectionThrottle';
import {
createE2EEKeyProvider,
createE2EEWorker,
ownE2EEWorker,
releaseE2EEWorker,
} from '@app/features/voice/engine/VoiceE2EEKeyProvider';
import {getSharedVoiceAudioContext} from '@app/features/voice/engine/VoiceSharedAudioContext';
import {
releaseCaptureWithRepublishedTrack,
selectLocalMediaPublicationsForConnectionRepublish,
} from '@app/features/voice/engine/VoiceTrackPublicationUtils';
import {
assertDisconnectReason,
assertNonEmptyString,
assertOptionalNonEmptyString,
assertVoiceServerUpdateShape,
hasAnyTerminalTransport,
isReadyToRepublishTrack,
} from '@app/features/voice/engine/v2/VoiceEngineV2AppAdapterAssertions';
import {VoiceEngineV2AppReconnectPolicy} from '@app/features/voice/engine/v2/VoiceEngineV2AppReconnectPolicy';
import VoiceRegionTeleport from '@app/features/voice/state/VoiceRegionTeleport';
import {
findVideoPublishCodecPolicyViolation,
getRoomVideoPublishDefaults,
} from '@app/features/voice/utils/CodecCapabilityDetector';
import {getH264HardwareProfilesSync} from '@app/features/voice/utils/GpuEncoderCapabilities';
import {
clearScreenShareDecodeFailures,
getVideoDecoderExclusionsSync,
loadVideoDecoderExclusions,
} from '@app/features/voice/utils/VideoDecoderCapabilities';
import {getScreenShareBitrateCeilingBps} from '@app/features/voice/utils/VideoQualityEntitlement';
import type {
ExternalE2EEKeyProvider,
LocalTrack,
Room,
RoomConnectOptions,
RoomOptions,
TrackPublishOptions,
} from 'livekit-client';
import {Room as LiveKitRoom, Track} from 'livekit-client';
import {makeObservable, observableRef} from 'mobx';
import type {Subscription} from 'rxjs';
import {timer} from 'rxjs';
const logger = new Logger('VoiceEngineV2AppConnectionHostAdapter');
const VOICE_SERVER_TIMEOUT_MS = 5000;
const VIDEO_DECODER_EXCLUSION_TIMEOUT_MS = 500;
export interface VoiceServerUpdateData {
token: string;
endpoint: string;
connection_id: string;
guild_id?: string;
channel_id?: string;
e2ee_key?: string | null;
ice_servers?: Array<Pick<RTCIceServer, 'urls'>>;
}
interface VoiceConnectionState {
room: Room | null;
guildId: string | null;
channelId: string | null;
connecting: boolean;
connected: boolean;
reconnecting: boolean;
voiceServerEndpoint: string | null;
connectionId: string | null;
}
interface RegionHotSwapState {
pendingRoom: Room | null;
previousRoom: Room | null;
inProgress: boolean;
}
type HotSwapQueuedOperation = () => void | Promise<void>;
type VoiceConnectFailureHandler = (
guildId: string | null,
channelId: string,
connectionId: string | null,
attemptId: number,
error: unknown,
) => void | Promise<void>;
const initialConnectionState: VoiceConnectionState = {
room: null,
guildId: null,
channelId: null,
connecting: false,
connected: false,
reconnecting: false,
voiceServerEndpoint: null,
connectionId: null,
};
const initialHotSwapState: RegionHotSwapState = {
pendingRoom: null,
previousRoom: null,
inProgress: false,
};
const REGION_HOT_SWAP_TIMEOUT_MS = 10000;
async function getRoomVideoDecoderExclusions(): Promise<RoomOptions['subscriberVideoCodecExclusions']> {
let timeoutId: NodeJS.Timeout | undefined;
const timeout = new Promise<null>((resolve) => {
timeoutId = setTimeout(() => resolve(null), VIDEO_DECODER_EXCLUSION_TIMEOUT_MS);
});
try {
await Promise.race([loadVideoDecoderExclusions(), timeout]);
} finally {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
}
const exclusions = getVideoDecoderExclusionsSync();
return exclusions && exclusions.length > 0 ? exclusions : undefined;
}
function createWebAudioMixOption(): RoomOptions['webAudioMix'] {
const audioContext = getSharedVoiceAudioContext();
if (audioContext) {
assert.notEqual(audioContext.state, 'closed', 'shared voice AudioContext handed to LiveKit must not be closed');
return {audioContext};
}
return true;
}
function createRoomPublishDefaults(): RoomOptions['publishDefaults'] {
return {
screenShareEncoding: {
maxBitrate: getScreenShareBitrateCeilingBps(),
maxFramerate: 30,
priority: 'high',
},
...getRoomVideoPublishDefaults(),
};
}
function createRoomOptions(
e2eeKey: string | null,
subscriberVideoCodecExclusions: RoomOptions['subscriberVideoCodecExclusions'],
): {
roomOptions: RoomOptions;
e2eeKeyProvider: ExternalE2EEKeyProvider | null;
e2eeWorker: Worker | null;
} {
const roomOptions: RoomOptions = {
adaptiveStream: false,
dynacast: true,
webAudioMix: createWebAudioMixOption(),
publishDefaults: createRoomPublishDefaults(),
subscriberVideoCodecExclusions,
h264HardwareProfiles: getH264HardwareProfilesSync()?.profiles,
};
let e2eeKeyProvider: ExternalE2EEKeyProvider | null = null;
let e2eeWorker: Worker | null = null;
if (e2eeKey) {
try {
e2eeKeyProvider = createE2EEKeyProvider();
e2eeWorker = createE2EEWorker();
roomOptions.e2ee = {keyProvider: e2eeKeyProvider, worker: e2eeWorker};
} catch (error) {
logger.error('Failed to construct E2EE key provider/worker', error);
e2eeWorker?.terminate();
e2eeKeyProvider = null;
e2eeWorker = null;
}
}
return {roomOptions, e2eeKeyProvider, e2eeWorker};
}
function requireCurrentUserId(): string {
const userId = Authentication.currentUserId;
assertNonEmptyString(userId, 'mesh room current user id');
return userId;
}
function createRoomConnectOptions(): RoomConnectOptions {
const connectOptions: RoomConnectOptions = {
autoSubscribe: false,
};
assert.equal(connectOptions.autoSubscribe, false, 'LiveKit connect options must not auto-subscribe');
return connectOptions;
}
class VoiceEngineV2AppConnectionHostAdapter extends Store {
connectionState: VoiceConnectionState = initialConnectionState;
hotSwapState: RegionHotSwapState = initialHotSwapState;
private connectionSnapshot: VoiceConnectionSnapshot = createVoiceConnectionSnapshot();
private throttle = new VoiceConnectionThrottle();
private reconnect = new VoiceEngineV2AppReconnectPolicy();
private voiceServerTimeoutSub: Subscription | null = null;
private hotSwapTimeoutSub: Subscription | null = null;
private isLocalDisconnecting = false;
private hotSwapOperationQueue: Array<HotSwapQueuedOperation> = [];
constructor() {
super();
makeObservable(this, {
connectionState: observableRef,
hotSwapState: observableRef,
});
this.throttle.subscribe(() => this.emitChange());
this.reconnect.subscribe(() => this.emitChange());
}
get room(): Room | null {
return this.connectionState.room;
}
get guildId(): string | null {
return this.connectionState.guildId;
}
get channelId(): string | null {
return this.connectionState.channelId;
}
get connected(): boolean {
return this.connectionState.connected;
}
get connecting(): boolean {
return this.connectionState.connecting;
}
get reconnecting(): boolean {
return this.connectionState.reconnecting;
}
get connectionId(): string | null {
return this.connectionState.connectionId;
}
get voiceServerEndpoint(): string | null {
return this.connectionState.voiceServerEndpoint;
}
get shouldAutoReconnect(): boolean {
return this.reconnect.shouldAutoReconnect;
}
get disconnecting(): boolean {
return this.isLocalDisconnecting;
}
get localDisconnectReason(): VoiceConnectionLocalDisconnectReason {
return this.connectionSnapshot.context.localDisconnectReason;
}
get connectFailed(): boolean {
return isVoiceConnectionFailed(this.connectionSnapshot);
}
get connectFailedTarget(): {guildId: string | null; channelId: string} | null {
return getVoiceConnectionFailedTarget(this.connectionSnapshot);
}
private async drainHotSwapQueue(): Promise<void> {
const ops = this.hotSwapOperationQueue.splice(0);
if (ops.length === 0) return;
this.update(() => {
this.transitionConnection({type: 'hotSwap.drainQueue'});
});
logger.info('Draining hot-swap operation queue', {count: ops.length});
for (const op of ops) {
try {
await op();
} catch (error) {
logger.warn('Queued hot-swap operation failed during drain', {error});
}
}
}
private clearHotSwapQueue(): void {
if (this.hotSwapOperationQueue.length > 0) {
logger.info('Clearing hot-swap operation queue', {discarded: this.hotSwapOperationQueue.length});
}
this.hotSwapOperationQueue.length = 0;
this.update(() => {
this.transitionConnection({type: 'hotSwap.clearQueue'});
});
}
get lastConnectedChannel(): {
guildId: string;
channelId: string;
} | null {
return this.reconnect.lastConnectedChannel;
}
private transitionConnection(event: VoiceConnectionEvent): void {
const wasTeleporting = this.hotSwapState.inProgress;
this.connectionSnapshot = transitionVoiceConnectionSnapshot(this.connectionSnapshot, event);
const {context} = this.connectionSnapshot;
this.connectionState = {
room: context.room as Room | null,
guildId: context.guildId,
channelId: context.channelId,
connecting: context.connecting,
connected: context.connected,
reconnecting: context.reconnecting,
voiceServerEndpoint: context.voiceServerEndpoint,
connectionId: context.connectionId,
};
this.hotSwapState = {
pendingRoom: context.hotSwap.pendingRoom as Room | null,
previousRoom: context.hotSwap.previousRoom as Room | null,
inProgress: context.hotSwap.inProgress,
};
const isTeleporting = this.hotSwapState.inProgress;
if (isTeleporting && !wasTeleporting) {
VoiceRegionTeleport.beginTeleport();
}
if (!isTeleporting && wasTeleporting) {
VoiceRegionTeleport.endTeleport();
}
}
private isLatestConnectionAttempt(attemptId: number): boolean {
return (
isLatestVoiceConnectionAttempt(this.connectionSnapshot, attemptId) && this.throttle.isLatestAttempt(attemptId)
);
}
private syncThrottleToCurrentAttempt(): void {
this.throttle.setLatestAttemptId(this.connectionSnapshot.context.connectionAttemptId);
}
private invalidateThrottleAttempt(): void {
this.throttle.setLatestAttemptId(this.connectionSnapshot.context.connectionAttemptId + 1);
}
startConnection(guildId: string | null, channelId: string): boolean {
assertOptionalNonEmptyString(guildId, 'startConnection.guildId');
assertNonEmptyString(channelId, 'startConnection.channelId');
if (this.throttle.shouldThrottle()) {
logger.warn('Connection throttled');
return false;
}
this.throttle.recordConnectRequest();
this.update(() => {
this.transitionConnection({type: 'connection.start', guildId, channelId});
});
this.syncThrottleToCurrentAttempt();
this.throttle.setInFlightConnect(true);
this.scheduleVoiceServerTimeout(guildId, channelId);
logger.info('Connection started', {guildId, channelId});
return true;
}
recoverConnectionExpectation(guildId: string | null, channelId: string): void {
assertOptionalNonEmptyString(guildId, 'recoverConnectionExpectation.guildId');
assertNonEmptyString(channelId, 'recoverConnectionExpectation.channelId');
this.throttle.recordConnectRequest();
this.update(() => {
this.transitionConnection({type: 'connection.recoverExpectation', guildId, channelId});
});
this.syncThrottleToCurrentAttempt();
this.throttle.setInFlightConnect(true);
this.scheduleVoiceServerTimeout(guildId, channelId);
logger.info('Connection expectation recovered', {guildId, channelId});
}
handleVoiceServerUpdate(
raw: VoiceServerUpdateData,
onRoomCreated: (room: Room, attemptId: number, guildId: string | null, channelId: string) => void,
onBeforeReconnect?: (isChannelMove: boolean, previousRoom: Room) => boolean | undefined,
onRoomClosed?: (room: Room, attemptId: number) => void,
onHotSwapComplete?: (newRoom: Room, attemptId: number, guildId: string | null, channelId: string) => void,
onConnectFailed?: VoiceConnectFailureHandler,
): void {
assertVoiceServerUpdateShape(raw, 'handleVoiceServerUpdate.raw');
assert.equal(typeof onRoomCreated, 'function', 'handleVoiceServerUpdate.onRoomCreated must be a function');
void this.handleVoiceServerUpdateAsync(
raw,
onRoomCreated,
onBeforeReconnect,
onRoomClosed,
onHotSwapComplete,
onConnectFailed,
);
}
private async handleVoiceServerUpdateAsync(
raw: VoiceServerUpdateData,
onRoomCreated: (room: Room, attemptId: number, guildId: string | null, channelId: string) => void,
onBeforeReconnect?: (isChannelMove: boolean, previousRoom: Room) => boolean | undefined,
onRoomClosed?: (room: Room, attemptId: number) => void,
onHotSwapComplete?: (newRoom: Room, attemptId: number, guildId: string | null, channelId: string) => void,
onConnectFailed?: VoiceConnectFailureHandler,
): Promise<void> {
const decision = selectVoiceConnectionServerUpdateDecision(this.connectionSnapshot, raw);
const {
guildId: expectedGuildId,
channelId: expectedChannelId,
connected,
room: existingRoom,
voiceServerEndpoint: currentEndpoint,
} = this.connectionState;
const guildId = raw.guild_id ?? null;
const endpoint = raw.endpoint ?? null;
const token = raw.token ?? null;
const connectionId = raw.connection_id ?? null;
const incomingChannelId = raw.channel_id ?? null;
const attemptId = decision.attemptId;
logger.debug('handleVoiceServerUpdate called', {
incomingGuildId: guildId,
expectedGuildId,
incomingChannelId,
expectedChannelId,
endpoint,
hasToken: !!token,
connectionId,
attemptId,
});
if (decision.type === 'ignore' && decision.reason === 'guild-or-channel-mismatch') {
logger.warn('Ignoring VOICE_SERVER_UPDATE: guild or channel mismatch', {
expectedGuildId: decision.expectedGuildId,
incomingGuildId: decision.incomingGuildId,
expectedChannelId: decision.expectedChannelId,
incomingChannelId: decision.incomingChannelId,
});
return;
}
if (decision.type === 'ignore' && decision.reason === 'stale-channel-update') {
logger.warn('Ignoring VOICE_SERVER_UPDATE: stale channel update', {
expectedChannelId: decision.expectedChannelId,
incomingChannelId: decision.incomingChannelId,
connectionId,
});
return;
}
if (decision.type === 'ignore' && decision.reason === 'stale-attempt') {
logger.warn('Ignoring VOICE_SERVER_UPDATE: not latest attempt', {attemptId});
return;
}
if (decision.type !== 'accept') return;
const isChannelMove = decision.isChannelMove;
if (isChannelMove) {
if (connected) {
logger.info('VOICE_SERVER_UPDATE: server-initiated channel move', {
expectedChannelId,
incomingChannelId,
connectionId,
});
}
}
if (decision.isRegionChange) {
logger.info('VOICE_SERVER_UPDATE: region change detected, attempting hot-swap', {
previousEndpoint: currentEndpoint,
newEndpoint: endpoint,
guildId,
channelId: decision.resolvedChannelId,
});
await this.handleRegionHotSwap(
raw,
decision.currentRoom as Room,
attemptId,
decision.guildId,
decision.resolvedChannelId,
onRoomCreated,
onRoomClosed,
onHotSwapComplete,
);
return;
}
const resolvedChannelId = decision.resolvedChannelId;
this.clearVoiceServerTimeout();
let previousRoom = connected && existingRoom ? existingRoom : null;
let shouldStopPreviousRoomTracks = true;
if (previousRoom) {
shouldStopPreviousRoomTracks = isChannelMove || onBeforeReconnect?.(isChannelMove, previousRoom) !== true;
previousRoom.removeAllListeners();
holdVoiceMeshConversion(previousRoom, true);
if (isChannelMove) {
this.disconnectPreviousRoom(previousRoom);
previousRoom = null;
}
}
this.update(() => {
this.transitionConnection({
type: 'voiceServer.accepted',
guildId,
channelId: resolvedChannelId,
endpoint,
connectionId,
isChannelMove,
});
});
this.throttle.setInFlightConnect(true);
const e2eeKey = raw.e2ee_key ?? null;
clearScreenShareDecodeFailures();
const subscriberVideoCodecExclusions = await getRoomVideoDecoderExclusions();
if (
!this.isLatestConnectionAttempt(attemptId) ||
this.connectionState.guildId !== guildId ||
this.connectionState.channelId !== resolvedChannelId
) {
logger.warn('Aborting LiveKit room creation after codec probing because attempt is stale', {attemptId});
return;
}
const {roomOptions, e2eeKeyProvider, e2eeWorker} = createRoomOptions(e2eeKey, subscriberVideoCodecExclusions);
const room = raw.ice_servers
? createVoiceMeshRoom(roomOptions, {
iceServers: raw.ice_servers,
guildId,
channelId: resolvedChannelId,
connectionId: raw.connection_id,
userId: requireCurrentUserId(),
})
: new LiveKitRoom(roomOptions);
ownE2EEWorker(room, e2eeWorker);
let roomClosed = false;
const closeRoom = () => {
if (roomClosed) return;
roomClosed = true;
releaseE2EEWorker(room);
onRoomClosed?.(room, attemptId);
};
const failConnectBeforeRoomConnect = (message: string, error?: unknown) => {
logger.error(message, error);
closeRoom();
this.disconnectPreviousRoom(previousRoom);
if (!this.isLatestConnectionAttempt(attemptId)) return;
this.update(() => {
this.transitionConnection({type: 'connection.failed', reason: 'error'});
});
this.throttle.setInFlightConnect(false);
this.reconnect.setReconnectState('error');
void onConnectFailed?.(guildId, resolvedChannelId, connectionId, attemptId, error ?? new Error(message));
};
const connectRoom = () => {
if (!this.isLatestConnectionAttempt(attemptId)) {
closeRoom();
return;
}
onRoomCreated(room, attemptId, guildId, resolvedChannelId);
if (!endpoint || !token) {
failConnectBeforeRoomConnect('Missing endpoint or token', {endpoint, hasToken: !!token});
return;
}
logger.info('Attempting to connect to LiveKit', {endpoint, guildId, channelId: resolvedChannelId});
const connectOptions = createRoomConnectOptions();
room
.connect(endpoint, token, connectOptions)
.then(() => {
this.disconnectPreviousRoom(previousRoom, shouldStopPreviousRoomTracks);
logger.info('LiveKit connection succeeded');
const connectionState = this.connectionState;
const connectedEventAlreadyApplied = connectionState.connected && connectionState.room == null;
if (
!this.isLatestConnectionAttempt(attemptId) ||
connectionState.guildId !== guildId ||
connectionState.channelId !== resolvedChannelId ||
(!connectionState.connecting && !connectedEventAlreadyApplied)
) {
logger.warn('Connection succeeded but attempt is stale, disconnecting');
closeRoom();
try {
room.removeAllListeners();
room.disconnect();
} catch (error) {
logger.warn('Failed to disconnect stale room', error);
}
return;
}
logger.info('Initializing voice connection');
this.update(() => {
this.transitionConnection({type: 'connection.roomReady', room, attemptId});
});
})
.catch((error) => {
closeRoom();
this.disconnectPreviousRoom(previousRoom);
logger.error('LiveKit connection failed', {error, endpoint});
if (this.isLatestConnectionAttempt(attemptId)) {
this.update(() => {
this.transitionConnection({type: 'connection.failed', reason: 'error'});
});
this.throttle.setInFlightConnect(false);
this.reconnect.setReconnectState('error');
void onConnectFailed?.(guildId, resolvedChannelId, connectionId, attemptId, error);
}
});
};
if (e2eeKey) {
if (!e2eeKeyProvider) {
failConnectBeforeRoomConnect('Cannot join E2EE voice channel because E2EE setup failed');
return;
}
void e2eeKeyProvider
.setKey(e2eeKey)
.then(() => room.setE2EEEnabled(true))
.then(() => {
connectRoom();
})
.catch((error) => {
failConnectBeforeRoomConnect(
'Cannot join E2EE voice channel because the E2EE key failed to initialize',
error,
);
});
return;
}
connectRoom();
}
private handleRegionHotSwap(
raw: VoiceServerUpdateData,
existingRoom: Room,
attemptId: number,
guildId: string | null,
channelId: string,
onRoomCreated: (room: Room, attemptId: number, guildId: string | null, channelId: string) => void,
onRoomClosed?: (room: Room, attemptId: number) => void,
onHotSwapComplete?: (newRoom: Room, attemptId: number, guildId: string | null, channelId: string) => void,
): Promise<void> {
return this.handleRegionHotSwapAsync(
raw,
existingRoom,
attemptId,
guildId,
channelId,
onRoomCreated,
onRoomClosed,
onHotSwapComplete,
);
}
private async handleRegionHotSwapAsync(
raw: VoiceServerUpdateData,
existingRoom: Room,
attemptId: number,
guildId: string | null,
channelId: string,
onRoomCreated: (room: Room, attemptId: number, guildId: string | null, channelId: string) => void,
onRoomClosed?: (room: Room, attemptId: number) => void,
onHotSwapComplete?: (newRoom: Room, attemptId: number, guildId: string | null, channelId: string) => void,
): Promise<void> {
const endpoint = raw.endpoint!;
const token = raw.token!;
const connectionId = raw.connection_id ?? null;
this.abortHotSwap();
const cachedExclusions = getVideoDecoderExclusionsSync();
const roomOptions: RoomOptions = {
adaptiveStream: false,
dynacast: true,
webAudioMix: createWebAudioMixOption(),
publishDefaults: createRoomPublishDefaults(),
subscriberVideoCodecExclusions: cachedExclusions && cachedExclusions.length > 0 ? cachedExclusions : undefined,
};
if (!this.isLatestConnectionAttempt(attemptId) || this.connectionState.room !== existingRoom) {
logger.warn('Region hot-swap: aborted before room creation because attempt is stale', {attemptId});
return;
}
const newRoom = new LiveKitRoom(roomOptions);
this.update(() => {
this.transitionConnection({type: 'hotSwap.start', pendingRoom: newRoom, previousRoom: existingRoom});
});
holdVoiceMeshConversion(existingRoom, true);
this.clearHotSwapTimeout();
this.hotSwapTimeoutSub = timer(REGION_HOT_SWAP_TIMEOUT_MS).subscribe(() => {
if (this.hotSwapState.inProgress && this.hotSwapState.pendingRoom === newRoom) {
logger.warn('Region hot-swap timed out, aborting', {endpoint});
this.abortHotSwap();
}
});
const connectOptions = createRoomConnectOptions();
logger.info('Region hot-swap: connecting to new endpoint', {endpoint, guildId, channelId});
newRoom
.connect(endpoint, token, connectOptions)
.then(async () => {
if (
!this.hotSwapState.inProgress ||
this.hotSwapState.pendingRoom !== newRoom ||
!this.isLatestConnectionAttempt(attemptId)
) {
logger.warn('Region hot-swap: new room connected but hot-swap was cancelled');
try {
newRoom.removeAllListeners();
newRoom.disconnect(false);
} catch (error) {
logger.warn('Region hot-swap: cancelled cleanup disconnect failed', {error});
}
return;
}
logger.info('Region hot-swap: new room connected, republishing tracks');
try {
await this.republishLocalTracks(existingRoom, newRoom);
} catch (error) {
logger.warn('Region hot-swap: failed to republish tracks, aborting', {error});
if (
!this.hotSwapState.inProgress ||
this.hotSwapState.pendingRoom !== newRoom ||
!this.isLatestConnectionAttempt(attemptId)
) {
return;
}
this.abortHotSwap();
return;
}
if (
!this.hotSwapState.inProgress ||
this.hotSwapState.pendingRoom !== newRoom ||
!this.isLatestConnectionAttempt(attemptId)
) {
logger.warn('Region hot-swap: cancelled during track republishing');
try {
newRoom.removeAllListeners();
newRoom.disconnect(false);
} catch (error) {
logger.warn('Region hot-swap: cancelled-during-republish disconnect failed', {error});
}
return;
}
logger.info('Region hot-swap: swapping room pointer');
const previousRoom = existingRoom;
onRoomCreated(newRoom, attemptId, guildId, channelId);
previousRoom.removeAllListeners();
this.update(() => {
this.transitionConnection({type: 'hotSwap.complete', room: newRoom, endpoint, connectionId});
});
this.clearHotSwapTimeout();
onHotSwapComplete?.(newRoom, attemptId, guildId, channelId);
await this.drainHotSwapQueue();
try {
previousRoom.disconnect(false);
} catch (error) {
logger.warn('Region hot-swap: failed to disconnect old room', {error});
}
releaseE2EEWorker(previousRoom);
logger.info('Region hot-swap: complete', {
previousEndpoint: this.connectionState.voiceServerEndpoint,
newEndpoint: endpoint,
});
})
.catch((error) => {
logger.error('Region hot-swap: failed to connect to new endpoint', {error, endpoint});
onRoomClosed?.(newRoom, attemptId);
try {
newRoom.removeAllListeners();
newRoom.disconnect();
} catch (cleanupError) {
logger.warn('Region hot-swap: failed disconnect after connect failure', {cleanupError});
}
if (
!this.hotSwapState.inProgress ||
this.hotSwapState.pendingRoom !== newRoom ||
!this.isLatestConnectionAttempt(attemptId)
) {
return;
}
this.update(() => {
this.transitionConnection({type: 'hotSwap.reset'});
});
holdVoiceMeshConversion(existingRoom, false);
this.clearHotSwapTimeout();
this.clearHotSwapQueue();
logger.info('Region hot-swap: keeping old room on previous endpoint');
});
}
private async republishLocalTracks(oldRoom: Room, newRoom: Room): Promise<void> {
const oldParticipant = oldRoom.localParticipant;
const newParticipant = newRoom.localParticipant;
if (!oldParticipant || !newParticipant) {
logger.warn('Region hot-swap: missing participant for track republishing');
return;
}
const isMeshConversion = isVoiceMeshRoom(oldRoom);
const publications = selectLocalMediaPublicationsForConnectionRepublish(
Array.from(oldParticipant.trackPublications.values()),
).filter(
(publication) =>
!isMeshConversion ||
(publication.source !== Track.Source.ScreenShare && publication.source !== Track.Source.ScreenShareAudio),
);
const errors: Array<{source: string; error: unknown}> = [];
let codecPolicyFailure: Error | null = null;
for (const publication of publications) {
const track = publication.track as LocalTrack | undefined;
if (!isReadyToRepublishTrack(track)) {
logger.debug('Region hot-swap: skipping ended or missing track', {
source: publication.source,
trackSid: publication.trackSid,
});
continue;
}
try {
logger.debug('Region hot-swap: republishing track', {
source: publication.source,
kind: track.kind,
});
const publishOptions = {
...((publication as {options?: TrackPublishOptions}).options ?? {}),
source: publication.source,
name: publication.trackName,
};
const republished = await newParticipant.publishTrack(track.mediaStreamTrack, publishOptions);
releaseCaptureWithRepublishedTrack(newParticipant, republished, track);
const violation = publishOptions.videoCodec
? findVideoPublishCodecPolicyViolation(publishOptions.videoCodec, republished.options?.videoCodec)
: null;
if (violation) {
codecPolicyFailure = new Error(
`Region hot-swap: ${publication.source} negotiated ${violation.negotiated} after requesting ${violation.requested}`,
);
break;
}
} catch (error) {
errors.push({source: publication.source ?? 'unknown', error});
logger.warn('Region hot-swap: failed to republish track', {
source: publication.source,
error,
});
}
}
if (codecPolicyFailure) throw codecPolicyFailure;
const screenShareFailure = errors.find(
(error) => error.source === Track.Source.ScreenShare || error.source === Track.Source.ScreenShareAudio,
);
if (screenShareFailure) {
throw new Error(`Screen share track republish failed for ${screenShareFailure.source}`);
}
if (errors.length > 0 && errors.length === publications.length) {
throw new Error(`All ${errors.length} track republications failed`);
}
if (errors.length > 0) {
logger.warn('Region hot-swap: some tracks failed to republish', {
total: publications.length,
failed: errors.length,
});
}
}
abortHotSwap(): void {
assert.ok(this.hotSwapOperationQueue.length <= 4096, 'abortHotSwap pre-condition: queue under cap');
assert.ok(this.connectionSnapshot !== null, 'abortHotSwap pre-condition: connection snapshot present');
if (!this.hotSwapState.inProgress) return;
const {pendingRoom, previousRoom} = this.hotSwapState;
logger.info('Aborting region hot-swap');
if (pendingRoom) {
try {
pendingRoom.removeAllListeners();
pendingRoom.disconnect(false);
} catch (error) {
logger.warn('Failed to disconnect pending hot-swap room', {error});
}
}
this.update(() => {
this.transitionConnection({type: 'hotSwap.abort'});
});
holdVoiceMeshConversion(previousRoom, false);
this.clearHotSwapTimeout();
this.clearHotSwapQueue();
}
markConnected(): void {
assert.ok(this.connectionSnapshot !== null, 'markConnected pre-condition: connection snapshot present');
const {guildId, channelId} = this.connectionState;
assertNonEmptyString(channelId, 'markConnected pre-condition: channelId present');
this.update(() => {
this.transitionConnection({type: 'connection.connected'});
});
this.reconnect.setLastConnectedChannel(guildId, channelId);
this.throttle.setInFlightConnect(false);
this.reconnect.resetOnConnection();
assert.ok(this.connectionState.connected, 'markConnected post-condition: connection state reflects connected');
logger.info('Connection established');
}
markDisconnected(reason: 'user' | 'error' | 'server' = 'user'): void {
assertDisconnectReason(reason, 'markDisconnected.reason');
this.update(() => {
this.transitionConnection({type: 'connection.disconnected', reason});
});
this.invalidateThrottleAttempt();
this.throttle.setInFlightConnect(false);
this.reconnect.setReconnectState(reason);
logger.info('Connection terminated', {reason});
}
markReconnecting(): void {
assert.ok(this.connectionSnapshot !== null, 'markReconnecting pre-condition: connection snapshot present');
this.update(() => {
this.transitionConnection({type: 'connection.reconnecting'});
});
logger.info('Connection reconnecting');
}
markReconnected(): void {
assert.ok(this.connectionSnapshot !== null, 'markReconnected pre-condition: connection snapshot present');
this.update(() => {
this.transitionConnection({type: 'connection.reconnected'});
});
this.reconnect.resetOnConnection();
logger.info('Connection reconnected');
}
disconnectFromVoiceChannel(reason: 'user' | 'error' | 'server' = 'user'): void {
assertDisconnectReason(reason, 'disconnectFromVoiceChannel.reason');
const {room} = this.connectionState;
this.update(() => {
this.isLocalDisconnecting = reason === 'user';
});
this.clearVoiceServerTimeout();
this.abortHotSwap();
if (room) {
room.removeAllListeners();
room.disconnect();
releaseE2EEWorker(room);
}
this.update(() => {
this.transitionConnection({type: 'connection.disconnected', reason});
});
this.invalidateThrottleAttempt();
this.reconnect.setReconnectState(reason);
this.update(() => {
this.isLocalDisconnecting = false;
});
logger.info('Disconnected from voice channel', {reason});
}
disconnectForChannelMove(): void {
assert.ok(this.connectionSnapshot !== null, 'disconnectForChannelMove pre-condition: connection snapshot present');
const {room} = this.connectionState;
this.clearVoiceServerTimeout();
this.abortHotSwap();
if (room) {
room.removeAllListeners();
room.disconnect();
releaseE2EEWorker(room);
}
this.update(() => {
this.transitionConnection({type: 'connection.disconnectForChannelMove'});
});
this.invalidateThrottleAttempt();
logger.info('Disconnected for channel move (preserving connectionId)');
}
private disconnectRoomForTerminalUnload(room: Room | null, label: string): void {
if (!room) return;
try {
room.removeAllListeners();
room.disconnect();
logger.debug('Terminal unload LiveKit room disconnect requested', {label});
} catch (error) {
logger.warn('Terminal unload LiveKit room disconnect failed', {label, error});
}
releaseE2EEWorker(room);
}
hasTerminalUnloadTransports(): boolean {
assert.ok(this.connectionSnapshot !== null, 'hasTerminalUnloadTransports pre-condition: snapshot present');
return hasAnyTerminalTransport({current: this.connectionState, hotSwap: this.hotSwapState});
}
disconnectTransportsForTerminalUnload(): void {
assert.ok(
this.connectionSnapshot !== null,
'disconnectTransportsForTerminalUnload pre-condition: snapshot present',
);
const {room} = this.connectionState;
const {pendingRoom, previousRoom} = this.hotSwapState;
this.clearVoiceServerTimeout();
this.clearHotSwapTimeout();
this.clearHotSwapQueue();
this.disconnectRoomForTerminalUnload(pendingRoom, 'pending-hot-swap');
if (previousRoom && previousRoom !== room && previousRoom !== pendingRoom) {
this.disconnectRoomForTerminalUnload(previousRoom, 'previous-hot-swap');
}
this.disconnectRoomForTerminalUnload(room, 'current');
this.throttle.setInFlightConnect(false);
this.update(() => {
this.isLocalDisconnecting = false;
this.transitionConnection({type: 'connection.cleanup'});
});
this.invalidateThrottleAttempt();
}
markReconnectionAttempted(): void {
assert.ok(this.reconnect !== null, 'markReconnectionAttempted pre-condition: reconnect policy present');
this.reconnect.markAttempted();
}
forgetReconnectChannel(channelId: string): void {
assertNonEmptyString(channelId, 'forgetReconnectChannel.channelId');
this.reconnect.forgetChannel(channelId);
}
createGuardedHandler<T extends ReadonlyArray<unknown>>(
attemptId: number,
handler: (...args: T) => void | Promise<void>,
): (...args: T) => void {
assert.equal(typeof attemptId, 'number', 'createGuardedHandler.attemptId must be a number');
assert.ok(Number.isFinite(attemptId), 'createGuardedHandler.attemptId must be finite');
assert.equal(typeof handler, 'function', 'createGuardedHandler.handler must be a function');
return (...args: T) => {
if (!this.isLatestConnectionAttempt(attemptId)) {
return;
}
try {
const result = handler(...args);
if (result && typeof (result as Promise<void>).catch === 'function') {
void (result as Promise<void>).catch((error) => {
logger.error('Guarded voice handler failed', {attemptId, error});
});
}
} catch (error) {
logger.error('Guarded voice handler failed', {attemptId, error});
}
};
}
resetConnectionState(): void {
assert.ok(this.connectionSnapshot !== null, 'resetConnectionState pre-condition: snapshot present');
this.abortHotSwap();
this.update(() => {
this.isLocalDisconnecting = false;
this.transitionConnection({type: 'connection.reset'});
});
this.invalidateThrottleAttempt();
this.throttle.setInFlightConnect(false);
}
clearInFlightConnect(): void {
assert.ok(this.throttle !== null, 'clearInFlightConnect pre-condition: throttle present');
this.throttle.setInFlightConnect(false);
}
abortConnection(): void {
assert.ok(this.connectionSnapshot !== null, 'abortConnection pre-condition: snapshot present');
this.clearVoiceServerTimeout();
this.abortHotSwap();
this.update(() => {
this.isLocalDisconnecting = false;
this.transitionConnection({type: 'connection.abort'});
});
this.invalidateThrottleAttempt();
this.throttle.setInFlightConnect(false);
logger.info('Connection aborted due to gateway error');
}
private scheduleVoiceServerTimeout(guildId: string | null, channelId: string): void {
this.clearVoiceServerTimeout();
this.voiceServerTimeoutSub = timer(VOICE_SERVER_TIMEOUT_MS).subscribe(() => {
let didTimeout = false;
let abandonedConnectionId: string | null = null;
this.update(() => {
if (
this.connectionState.guildId === guildId &&
this.connectionState.channelId === channelId &&
!this.connectionState.connected
) {
logger.warn('Voice server timeout', {guildId, channelId});
didTimeout = true;
abandonedConnectionId = this.connectionState.connectionId;
this.transitionConnection({type: 'voiceServer.timeout', guildId, channelId});
this.invalidateThrottleAttempt();
this.throttle.setInFlightConnect(false);
this.reconnect.setReconnectState('error');
}
});
if (didTimeout) {
logger.info('Sending voice state disconnect for abandoned voice connection after timeout', {
guildId,
channelId,
connectionId: abandonedConnectionId,
});
sendVoiceStateDisconnect(guildId, abandonedConnectionId);
}
});
}
private disconnectPreviousRoom(previousRoom: Room | null, stopTracks = true): void {
if (!previousRoom) return;
try {
const tracksToStop = stopTracks ? [] : this.getPreviousRoomNonScreenShareTracks(previousRoom);
previousRoom.disconnect(stopTracks);
this.stopPreviousRoomTracks(tracksToStop);
logger.debug('Previous room disconnected', {stopTracks});
} catch (error) {
logger.warn('Failed to disconnect previous room', error);
}
releaseE2EEWorker(previousRoom);
}
private getPreviousRoomNonScreenShareTracks(previousRoom: Room): Array<LocalTrack> {
const tracks: Array<LocalTrack> = [];
previousRoom.localParticipant.trackPublications.forEach((publication) => {
if (publication.source === Track.Source.ScreenShare || publication.source === Track.Source.ScreenShareAudio) {
return;
}
if (publication.track) {
tracks.push(publication.track);
}
});
return tracks;
}
private stopPreviousRoomTracks(tracks: Array<LocalTrack>): void {
for (const track of tracks) {
try {
track.stop();
} catch (error) {
logger.warn('Failed to stop previous room local track', {error});
}
}
}
private clearVoiceServerTimeout(): void {
this.voiceServerTimeoutSub?.unsubscribe();
this.voiceServerTimeoutSub = null;
}
private clearHotSwapTimeout(): void {
this.hotSwapTimeoutSub?.unsubscribe();
this.hotSwapTimeoutSub = null;
}
cleanup(): void {
assert.ok(this.connectionSnapshot !== null, 'cleanup pre-condition: snapshot present');
assert.ok(this.hotSwapOperationQueue.length <= 4096, 'cleanup pre-condition: queue under cap');
const {room} = this.connectionState;
this.clearVoiceServerTimeout();
this.abortHotSwap();
if (room) {
room.removeAllListeners();
room.disconnect();
releaseE2EEWorker(room);
}
this.update(() => {
this.isLocalDisconnecting = false;
this.transitionConnection({type: 'connection.cleanup'});
});
this.throttle.reset();
this.invalidateThrottleAttempt();
this.reconnect.cleanup();
logger.info('Cleanup complete');
}
}
export default new VoiceEngineV2AppConnectionHostAdapter();