feat: enhance call handling by prioritizing homeservers based on active call members

This commit is contained in:
2026-04-20 21:15:46 +10:00
parent 9161c4bde2
commit 13a0c98d1d
3 changed files with 62 additions and 17 deletions

View File

@@ -30,6 +30,7 @@ import {
fetchLiveKitJWTFromServers,
getLiveKitHomeserverPriority,
} from './useRtcConfig';
import { getActiveCallMembers } from './useRoomCallMembers';
import { getAudioSettings, SCREEN_SHARE_RESOLUTIONS, SCREEN_SHARE_BITRATES } from '../settings/audio/Audio';
import { getCallSounds, CallSoundType } from './CallSounds';
@@ -306,6 +307,16 @@ export class CallService {
throw new Error(`Room ${roomId} not found`);
}
// Check if there's already an active call
const activeCallMembers = getActiveCallMembers(room);
const isJoiningExistingCall = activeCallMembers.length > 0;
if (isJoiningExistingCall) {
console.log(`Joining existing call with ${activeCallMembers.length} participant(s)`);
} else {
console.log('Starting new call');
}
const homeserverPriority = getLiveKitHomeserverPriority(
room,
this.config.homeserverBaseUrl,

View File

@@ -19,7 +19,7 @@ export interface CallMember {
* @param room - The Matrix room to check
* @returns Array of active call members
*/
function getActiveCallMembers(room: Room): CallMember[] {
export function getActiveCallMembers(room: Room): CallMember[] {
const members: CallMember[] = [];
const now = Date.now();

View File

@@ -4,6 +4,7 @@ import { useMatrixClient } from '../../hooks/useMatrixClient';
import { LiveKitFocus, LiveKitJWTResponse, OpenIdToken, WellKnownWithRTC } from './types';
import { getMxIdServer } from '../../utils/matrix';
import { guessDmRoomUserId } from '../../utils/matrix';
import { getActiveCallMembers } from './useRoomCallMembers';
/**
* Get room server from room ID (format: !roomid:server.com)
@@ -14,7 +15,8 @@ function getRoomServer(roomId: string): string | undefined {
/**
* Determine which homeservers to try for LiveKit JWT, in priority order
* @param room - Room to start call in
* Prioritizes servers where active call members are located when joining existing calls
* @param room - Room to start/join call in
* @param userHomeserver - User's homeserver
* @param myUserId - Current user's Matrix ID
* @returns Array of homeserver base URLs to try in order
@@ -28,28 +30,60 @@ export function getLiveKitHomeserverPriority(
const memberCount = room.getMembers().length;
const isDM = memberCount <= 2;
if (isDM) {
// For DMs: user's homeserver first, then other member's homeserver
servers.push(userHomeserver);
// Check for active call members
const activeCallMembers = getActiveCallMembers(room);
const hasActiveCall = activeCallMembers.length > 0;
const otherUserId = guessDmRoomUserId(room, myUserId);
if (otherUserId && otherUserId !== myUserId) {
const otherServer = getMxIdServer(otherUserId);
if (otherServer && otherServer !== userHomeserver) {
servers.push(`https://${otherServer}`);
}
}
} else {
// For channels: room's homeserver first, then user's homeserver
if (hasActiveCall) {
// Joining existing call - prioritize room's homeserver (where call is hosted)
const roomServer = getRoomServer(room.roomId);
if (roomServer) {
servers.push(`https://${roomServer}`);
}
// Fallback to user's homeserver if different
if (!roomServer || roomServer !== userHomeserver.replace('https://', '').replace('http://', '')) {
// Add user's homeserver as fallback
const userDomain = userHomeserver.replace('https://', '').replace('http://', '');
if (!roomServer || roomServer !== userDomain) {
servers.push(userHomeserver);
}
// For DMs, also try other member's homeserver
if (isDM) {
const otherUserId = guessDmRoomUserId(room, myUserId);
if (otherUserId && otherUserId !== myUserId) {
const otherServer = getMxIdServer(otherUserId);
if (otherServer && otherServer !== userDomain && otherServer !== roomServer) {
servers.push(`https://${otherServer}`);
}
}
}
} else {
// Starting new call - use original priority logic
if (isDM) {
// For DMs: user's homeserver first, then other member's homeserver
servers.push(userHomeserver);
const otherUserId = guessDmRoomUserId(room, myUserId);
if (otherUserId && otherUserId !== myUserId) {
const otherServer = getMxIdServer(otherUserId);
const userDomain = userHomeserver.replace('https://', '').replace('http://', '');
if (otherServer && otherServer !== userDomain) {
servers.push(`https://${otherServer}`);
}
}
} else {
// For channels: room's homeserver first, then user's homeserver
const roomServer = getRoomServer(room.roomId);
if (roomServer) {
servers.push(`https://${roomServer}`);
}
// Fallback to user's homeserver if different
const userDomain = userHomeserver.replace('https://', '').replace('http://', '');
if (!roomServer || roomServer !== userDomain) {
servers.push(userHomeserver);
}
}
}
return servers;