diff --git a/src/app/features/call/CallService.ts b/src/app/features/call/CallService.ts index a323981..adc31a1 100644 --- a/src/app/features/call/CallService.ts +++ b/src/app/features/call/CallService.ts @@ -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, diff --git a/src/app/features/call/useRoomCallMembers.ts b/src/app/features/call/useRoomCallMembers.ts index cf79774..b6ffde3 100644 --- a/src/app/features/call/useRoomCallMembers.ts +++ b/src/app/features/call/useRoomCallMembers.ts @@ -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(); diff --git a/src/app/features/call/useRtcConfig.ts b/src/app/features/call/useRtcConfig.ts index ab5abfd..437913d 100644 --- a/src/app/features/call/useRtcConfig.ts +++ b/src/app/features/call/useRtcConfig.ts @@ -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 @@ -27,29 +29,61 @@ export function getLiveKitHomeserverPriority( const servers: string[] = []; const memberCount = room.getMembers().length; const isDM = memberCount <= 2; + + // Check for active call members + const activeCallMembers = getActiveCallMembers(room); + const hasActiveCall = activeCallMembers.length > 0; - 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); - 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;