mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 09:01:17 -08:00
refactor ringtones and cleanup call controls
This commit is contained in:
1 parent
2e5fd525f1
commit
f012f4326b
10 files changed
+211
-529
No files matched your search
@@ -0,0 +1,120 @@
|
||||
import * as Sentry from '@sentry/react';
|
||||
import { callRingtoneVolumeToGain } from './callRingtone';
|
||||
import { resolveCallToneSources } from './callToneSources';
|
||||
import type { Settings, CallRingtoneId } from '$state/settings';
|
||||
|
||||
export type PreviewTone = 'incoming' | 'outgoing';
|
||||
|
||||
class CallRingtoneManager {
|
||||
private incomingAudio: HTMLAudioElement;
|
||||
private outgoingAudio: HTMLAudioElement;
|
||||
private previewAudio: HTMLAudioElement;
|
||||
private revokeToneUrls: (() => void) | undefined;
|
||||
private currentPreviewUrl: string | null = null;
|
||||
|
||||
constructor() {
|
||||
this.incomingAudio = new Audio();
|
||||
this.incomingAudio.loop = true;
|
||||
|
||||
this.outgoingAudio = new Audio();
|
||||
this.outgoingAudio.loop = true;
|
||||
|
||||
this.previewAudio = new Audio();
|
||||
this.previewAudio.loop = true;
|
||||
}
|
||||
|
||||
public async syncSources(
|
||||
callRingtoneId: CallRingtoneId,
|
||||
callRingbackTone: CallRingtoneId,
|
||||
callRingtoneVolume: number
|
||||
) {
|
||||
const resolved = await resolveCallToneSources({ callRingtoneId, callRingbackTone });
|
||||
|
||||
this.revokeToneUrls?.();
|
||||
this.revokeToneUrls = resolved.revoke;
|
||||
|
||||
const gain = callRingtoneVolumeToGain(callRingtoneVolume);
|
||||
|
||||
if (resolved.incomingUrl) {
|
||||
this.incomingAudio.src = resolved.incomingUrl;
|
||||
} else {
|
||||
this.incomingAudio.removeAttribute('src');
|
||||
}
|
||||
|
||||
if (resolved.outgoingUrl) {
|
||||
this.outgoingAudio.src = resolved.outgoingUrl;
|
||||
} else {
|
||||
this.outgoingAudio.removeAttribute('src');
|
||||
}
|
||||
|
||||
this.incomingAudio.volume = gain;
|
||||
this.outgoingAudio.volume = gain;
|
||||
}
|
||||
|
||||
public playIncoming(): Promise<void> {
|
||||
if (!this.incomingAudio.src) return Promise.resolve();
|
||||
return this.incomingAudio.play().catch((err) => {
|
||||
if (err.name === 'AbortError') return;
|
||||
Sentry.metrics.count('sable.call.ringtone.blocked', 1);
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
public stopIncoming() {
|
||||
this.incomingAudio.pause();
|
||||
this.incomingAudio.currentTime = 0;
|
||||
}
|
||||
|
||||
public playOutgoing() {
|
||||
if (!this.outgoingAudio.src) return;
|
||||
this.outgoingAudio.play().catch((err) => {
|
||||
if (err.name === 'AbortError') return;
|
||||
Sentry.metrics.count('sable.call.ringback.blocked', 1);
|
||||
});
|
||||
}
|
||||
|
||||
public stopOutgoing() {
|
||||
this.outgoingAudio.pause();
|
||||
this.outgoingAudio.currentTime = 0;
|
||||
}
|
||||
|
||||
public async playPreview(
|
||||
tone: PreviewTone,
|
||||
settings: Pick<Settings, 'callRingtoneId' | 'callRingbackTone' | 'callRingtoneVolume'>
|
||||
) {
|
||||
this.stopPreview();
|
||||
|
||||
const resolved = await resolveCallToneSources({
|
||||
callRingtoneId: settings.callRingtoneId,
|
||||
callRingbackTone: settings.callRingbackTone,
|
||||
});
|
||||
const source = tone === 'incoming' ? resolved.incomingUrl : resolved.outgoingUrl;
|
||||
|
||||
if (tone === 'incoming' && resolved.outgoingUrl?.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(resolved.outgoingUrl);
|
||||
} else if (tone === 'outgoing' && resolved.incomingUrl?.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(resolved.incomingUrl);
|
||||
}
|
||||
|
||||
if (!source) return;
|
||||
|
||||
this.currentPreviewUrl = source;
|
||||
this.previewAudio.src = source;
|
||||
this.previewAudio.volume = callRingtoneVolumeToGain(settings.callRingtoneVolume);
|
||||
|
||||
await this.previewAudio.play();
|
||||
}
|
||||
|
||||
public stopPreview() {
|
||||
this.previewAudio.pause();
|
||||
this.previewAudio.currentTime = 0;
|
||||
this.previewAudio.removeAttribute('src');
|
||||
|
||||
if (this.currentPreviewUrl?.startsWith('blob:')) {
|
||||
URL.revokeObjectURL(this.currentPreviewUrl);
|
||||
}
|
||||
this.currentPreviewUrl = null;
|
||||
}
|
||||
}
|
||||
|
||||
export const ringtoneManager = new CallRingtoneManager();
|
||||
@@ -9,8 +9,6 @@ import {
|
||||
VideoCamera,
|
||||
VideoCameraSlash,
|
||||
ScreenShare,
|
||||
SquaresFour,
|
||||
Presentation,
|
||||
} from '$components/icons/phosphor';
|
||||
import { useAtom } from 'jotai';
|
||||
import * as css from './styles.css';
|
||||
@@ -179,35 +177,3 @@ export function ChatButton() {
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
|
||||
type LayoutButtonProps = {
|
||||
spotlight: boolean;
|
||||
onToggle: () => void;
|
||||
};
|
||||
export function LayoutButton({ spotlight, onToggle }: LayoutButtonProps) {
|
||||
return (
|
||||
<TooltipProvider
|
||||
position="Top"
|
||||
delay={500}
|
||||
tooltip={
|
||||
<Tooltip>
|
||||
<Text size="T200">{spotlight ? 'Switch to Grid View' : 'Switch to Spotlight View'}</Text>
|
||||
</Tooltip>
|
||||
}
|
||||
>
|
||||
{(anchorRef) => (
|
||||
<IconButton
|
||||
ref={anchorRef}
|
||||
variant="Surface"
|
||||
fill="Soft"
|
||||
radii="400"
|
||||
size="400"
|
||||
onClick={() => onToggle()}
|
||||
outlined
|
||||
>
|
||||
{sizedIcon(spotlight ? SquaresFour : Presentation, '300', { filled: true })}
|
||||
</IconButton>
|
||||
)}
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { IncomingCall } from '$state/callEmbed';
|
||||
import {
|
||||
evaluateIncomingCallFallback,
|
||||
evaluateOutgoingRingbackFallback,
|
||||
type OutgoingRingbackState,
|
||||
} from './callSignalingFallback';
|
||||
import { evaluateIncomingCallFallback } from './callSignalingFallback';
|
||||
import type { SessionDescription } from './callMembershipState';
|
||||
import { INCOMING_MEMBERSHIP_GRACE_MS, OUTGOING_RING_TIMEOUT_MS } from './callSignalingPolicy';
|
||||
import { INCOMING_MEMBERSHIP_GRACE_MS } from './callSignalingPolicy';
|
||||
|
||||
const NOW = 1_700_000_000_000;
|
||||
const EMPTY_SESSION = {} as SessionDescription;
|
||||
@@ -59,76 +55,3 @@ describe('evaluateIncomingCallFallback', () => {
|
||||
expect(action).toEqual({ kind: 'clear', reason: 'membership_dropped' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('evaluateOutgoingRingbackFallback', () => {
|
||||
const baseContext = {
|
||||
myUserId: '@self:example.org',
|
||||
activeCallRoomId: '!room:example.org',
|
||||
outgoingRingbackAllowed: true,
|
||||
declinedRoomId: null,
|
||||
hasCallBeenActive: false,
|
||||
getRoom: () => ({ roomId: '!room:example.org' }) as never,
|
||||
isDirectRoom: () => true,
|
||||
getSessionDescription: () => EMPTY_SESSION,
|
||||
};
|
||||
|
||||
it('stops ringback when outgoing call is no longer pending', () => {
|
||||
const state: OutgoingRingbackState = {
|
||||
ringRoomId: '!room:example.org',
|
||||
ringStartedAt: NOW,
|
||||
};
|
||||
const action = evaluateOutgoingRingbackFallback(state, NOW, {
|
||||
...baseContext,
|
||||
getRoom: () => undefined,
|
||||
});
|
||||
expect(action.kind).toBe('stop');
|
||||
expect(action.nextState).toEqual({ ringRoomId: null, ringStartedAt: null });
|
||||
});
|
||||
|
||||
it('plays ringback for pending outgoing calls and tracks start time', () => {
|
||||
const action = evaluateOutgoingRingbackFallback(
|
||||
{ ringRoomId: null, ringStartedAt: null },
|
||||
NOW,
|
||||
{
|
||||
...baseContext,
|
||||
isOutgoingPending: () => true,
|
||||
isCallActive: () => false,
|
||||
}
|
||||
);
|
||||
expect(action).toMatchObject({
|
||||
kind: 'play',
|
||||
roomId: '!room:example.org',
|
||||
started: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('stops ringback after timeout', () => {
|
||||
const action = evaluateOutgoingRingbackFallback(
|
||||
{
|
||||
ringRoomId: '!room:example.org',
|
||||
ringStartedAt: NOW - OUTGOING_RING_TIMEOUT_MS,
|
||||
},
|
||||
NOW,
|
||||
{
|
||||
...baseContext,
|
||||
isOutgoingPending: () => true,
|
||||
isCallActive: () => false,
|
||||
}
|
||||
);
|
||||
expect(action.kind).toBe('stop');
|
||||
});
|
||||
|
||||
it('stops ringback if call has been active previously', () => {
|
||||
const action = evaluateOutgoingRingbackFallback(
|
||||
{ ringRoomId: null, ringStartedAt: null },
|
||||
NOW,
|
||||
{
|
||||
...baseContext,
|
||||
hasCallBeenActive: true,
|
||||
isOutgoingPending: () => true,
|
||||
isCallActive: () => false,
|
||||
}
|
||||
);
|
||||
expect(action.kind).toBe('stop');
|
||||
});
|
||||
});
|
||||
@@ -1,12 +1,8 @@
|
||||
import type { IncomingCall } from '$state/callEmbed';
|
||||
import type { Room } from '$types/matrix-sdk';
|
||||
import {
|
||||
isCallActive,
|
||||
isIncomingCallActive,
|
||||
isOutgoingCallPending,
|
||||
type SessionDescription,
|
||||
} from './callMembershipState';
|
||||
import { INCOMING_MEMBERSHIP_GRACE_MS, OUTGOING_RING_TIMEOUT_MS } from './callSignalingPolicy';
|
||||
import type { SessionDescription } from './callMembershipState';
|
||||
import { isIncomingCallActive } from './callMembershipState';
|
||||
import { INCOMING_MEMBERSHIP_GRACE_MS } from './callSignalingPolicy';
|
||||
|
||||
export type IncomingFallbackAction =
|
||||
| { kind: 'none' }
|
||||
@@ -43,84 +39,3 @@ export const evaluateIncomingCallFallback = (
|
||||
|
||||
return { kind: 'clear', reason: 'membership_dropped' };
|
||||
};
|
||||
|
||||
export type OutgoingRingbackState = {
|
||||
ringRoomId: string | null;
|
||||
ringStartedAt: number | null;
|
||||
};
|
||||
|
||||
export type OutgoingRingbackAction =
|
||||
| { kind: 'stop'; nextState: OutgoingRingbackState }
|
||||
| { kind: 'play'; roomId: string; nextState: OutgoingRingbackState; started: boolean };
|
||||
|
||||
export type OutgoingRingbackContext = {
|
||||
myUserId: string;
|
||||
activeCallRoomId: string | undefined;
|
||||
outgoingRingbackAllowed: boolean;
|
||||
declinedRoomId: string | null;
|
||||
hasCallBeenActive: boolean;
|
||||
getRoom: (roomId: string) => Room | null | undefined;
|
||||
isDirectRoom: (roomId: string) => boolean;
|
||||
getSessionDescription: (room: Room) => SessionDescription;
|
||||
isOutgoingPending?: typeof isOutgoingCallPending;
|
||||
isCallActive?: typeof isCallActive;
|
||||
};
|
||||
|
||||
const clearedRingbackState = (): OutgoingRingbackState => ({
|
||||
ringRoomId: null,
|
||||
ringStartedAt: null,
|
||||
});
|
||||
|
||||
export const evaluateOutgoingRingbackFallback = (
|
||||
state: OutgoingRingbackState,
|
||||
now: number,
|
||||
context: OutgoingRingbackContext
|
||||
): OutgoingRingbackAction => {
|
||||
const stop = (): OutgoingRingbackAction => ({
|
||||
kind: 'stop',
|
||||
nextState: clearedRingbackState(),
|
||||
});
|
||||
|
||||
if (!context.activeCallRoomId || !context.outgoingRingbackAllowed) {
|
||||
return stop();
|
||||
}
|
||||
if (context.declinedRoomId === context.activeCallRoomId) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
// Only direct (1:1) calls should play an outgoing ringback tone. Voice rooms should not ring.
|
||||
if (!context.isDirectRoom(context.activeCallRoomId)) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
const outgoingRoom = context.getRoom(context.activeCallRoomId);
|
||||
if (!outgoingRoom) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
const sessionDescription = context.getSessionDescription(outgoingRoom);
|
||||
const isOutgoingPending = context.isOutgoingPending ?? isOutgoingCallPending;
|
||||
const isActive = context.isCallActive ?? isCallActive;
|
||||
const pendingOutgoing = isOutgoingPending(context.myUserId, outgoingRoom, sessionDescription);
|
||||
const activeCall = isActive(context.myUserId, outgoingRoom, sessionDescription);
|
||||
|
||||
if (!pendingOutgoing || activeCall || context.hasCallBeenActive) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
const started = state.ringRoomId !== context.activeCallRoomId;
|
||||
const nextState: OutgoingRingbackState = started
|
||||
? { ringRoomId: context.activeCallRoomId, ringStartedAt: now }
|
||||
: state;
|
||||
|
||||
if (nextState.ringStartedAt && now - nextState.ringStartedAt >= OUTGOING_RING_TIMEOUT_MS) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
return {
|
||||
kind: 'play',
|
||||
roomId: context.activeCallRoomId,
|
||||
nextState,
|
||||
started,
|
||||
};
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { Box, Icons, Input, Switch, Text, toRem } from 'folds';
|
||||
import { SequenceCard } from '$components/sequence-card';
|
||||
import { SettingTile } from '$components/setting-tile';
|
||||
@@ -8,12 +8,11 @@ import { settingsAtom, type CallRingtoneId } from '$state/settings';
|
||||
import {
|
||||
CALL_RINGBACK_OPTIONS,
|
||||
CALL_RINGTONE_OPTIONS,
|
||||
callRingtoneVolumeToGain,
|
||||
clampCallRingtoneVolume,
|
||||
readAudioDurationMs,
|
||||
validateCustomCallRingtone,
|
||||
} from '$features/call/callRingtone';
|
||||
import { resolveCallToneSources, revokeUnusedCustomToneUrls } from '$features/call/callToneSources';
|
||||
import { ringtoneManager } from '$features/call/CallRingtoneManager';
|
||||
import {
|
||||
clearCustomCallRingback,
|
||||
clearCustomCallRingtone,
|
||||
@@ -66,8 +65,6 @@ export function CallSoundSettings() {
|
||||
const [customRingtoneMeta, setCustomRingtoneMeta] = useState<CustomToneMetadata | null>(null);
|
||||
const [customRingbackMeta, setCustomRingbackMeta] = useState<CustomToneMetadata | null>(null);
|
||||
const [customError, setCustomError] = useState<string | null>(null);
|
||||
const previewAudioRef = useRef<HTMLAudioElement | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
Promise.all([getCustomCallRingtone(), getCustomCallRingback()])
|
||||
@@ -85,8 +82,7 @@ export function CallSoundSettings() {
|
||||
|
||||
return () => {
|
||||
mounted = false;
|
||||
previewAudioRef.current?.pause();
|
||||
previewAudioRef.current = null;
|
||||
ringtoneManager.stopPreview();
|
||||
};
|
||||
}, []);
|
||||
|
||||
@@ -136,45 +132,28 @@ export function CallSoundSettings() {
|
||||
[customRingbackMeta, loadingCustomState]
|
||||
);
|
||||
|
||||
const resolveToneForPreview = useCallback(
|
||||
async (tone: PreviewTone): Promise<string | null> => {
|
||||
const resolved = await resolveCallToneSources({ callRingtoneId, callRingbackTone });
|
||||
const source = tone === 'incoming' ? resolved.incomingUrl : resolved.outgoingUrl;
|
||||
revokeUnusedCustomToneUrls(resolved, source);
|
||||
return source;
|
||||
},
|
||||
[callRingtoneId, callRingbackTone]
|
||||
);
|
||||
|
||||
const playPreviewTone = useCallback(
|
||||
async (tone: PreviewTone) => {
|
||||
setCustomError(null);
|
||||
setPreviewing(true);
|
||||
try {
|
||||
const source = await resolveToneForPreview(tone);
|
||||
if (!source) return;
|
||||
const revokeSource = source.startsWith('blob:');
|
||||
await ringtoneManager.playPreview(tone, {
|
||||
callRingtoneId,
|
||||
callRingbackTone,
|
||||
callRingtoneVolume,
|
||||
});
|
||||
|
||||
previewAudioRef.current?.pause();
|
||||
const audio = new Audio(source);
|
||||
audio.loop = true;
|
||||
audio.volume = callRingtoneVolumeToGain(callRingtoneVolume);
|
||||
previewAudioRef.current = audio;
|
||||
await audio.play();
|
||||
window.setTimeout(() => {
|
||||
if (previewAudioRef.current === audio) {
|
||||
audio.pause();
|
||||
audio.currentTime = 0;
|
||||
}
|
||||
if (revokeSource) URL.revokeObjectURL(source);
|
||||
ringtoneManager.stopPreview();
|
||||
}, 2500);
|
||||
} catch {
|
||||
} catch (err) {
|
||||
if (err instanceof Error && err.name === 'AbortError') return;
|
||||
setCustomError('Unable to preview this ringtone in your browser.');
|
||||
} finally {
|
||||
setPreviewing(false);
|
||||
}
|
||||
},
|
||||
[callRingtoneVolume, resolveToneForPreview]
|
||||
[callRingtoneId, callRingbackTone, callRingtoneVolume]
|
||||
);
|
||||
|
||||
const importCustomTone = useCallback(
|
||||
|
||||
@@ -29,15 +29,17 @@ import {
|
||||
type OutgoingDeclineEvent,
|
||||
} from '$features/call/outgoingDeclineHandler';
|
||||
import { parseRtcDeclineFromTimelineEvent } from '$features/call/rtcTimelineDecline';
|
||||
import {
|
||||
evaluateIncomingCallFallback,
|
||||
evaluateOutgoingRingbackFallback,
|
||||
} from '$features/call/callSignalingFallback';
|
||||
import { callRingtoneVolumeToGain, canPlayCallAudio } from '$features/call/callRingtone';
|
||||
import { evaluateIncomingCallFallback } from '$features/call/callSignalingFallback';
|
||||
import { canPlayCallAudio } from '$features/call/callRingtone';
|
||||
import { dismissSystemCallNotifications } from '$features/call/callNotificationBridge';
|
||||
import { resolveCallToneSources } from '$features/call/callToneSources';
|
||||
import { isIncomingCallSuppressed } from '$features/call/callIncomingIngress';
|
||||
import { getRemoteRtcMemberUserIds, isCallActive } from '$features/call/callMembershipState';
|
||||
import {
|
||||
getRemoteRtcMemberUserIds,
|
||||
isCallActive,
|
||||
isOutgoingCallPending,
|
||||
} from '$features/call/callMembershipState';
|
||||
import { ringtoneManager } from '$features/call/CallRingtoneManager';
|
||||
import { OUTGOING_RING_TIMEOUT_MS } from '$features/call/callSignalingPolicy';
|
||||
import { useMatrixClient } from './useMatrixClient';
|
||||
import { createDebugLogger } from '../utils/debugLogger';
|
||||
|
||||
@@ -59,8 +61,6 @@ export function useIncomingCallSignaling() {
|
||||
const setCallSoundBlocked = useSetAtom(callSoundBlockedAtom);
|
||||
const setCallEmbed = useSetAtom(callEmbedAtom);
|
||||
|
||||
const incomingAudioRef = useRef<HTMLAudioElement | null>(null);
|
||||
const outgoingAudioRef = useRef<HTMLAudioElement | null>(null);
|
||||
const incomingCallRef = useRef<IncomingCall | null>(incomingCall);
|
||||
const mutedRoomIdRef = useRef<string | null>(mutedRoomId);
|
||||
const seenNotificationIdsRef = useRef<Set<string>>(new Set());
|
||||
@@ -118,83 +118,20 @@ export function useIncomingCallSignaling() {
|
||||
}, [callEmbed]);
|
||||
|
||||
useEffect(() => {
|
||||
const incoming = new Audio();
|
||||
incoming.loop = true;
|
||||
incomingAudioRef.current = incoming;
|
||||
|
||||
const outgoing = new Audio();
|
||||
outgoing.loop = true;
|
||||
outgoingAudioRef.current = outgoing;
|
||||
|
||||
return () => {
|
||||
incoming.pause();
|
||||
outgoing.pause();
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let canceled = false;
|
||||
let revokeToneUrls: (() => void) | undefined;
|
||||
|
||||
const incoming = incomingAudioRef.current;
|
||||
const outgoing = outgoingAudioRef.current;
|
||||
if (!incoming || !outgoing) return undefined;
|
||||
|
||||
const syncSources = async () => {
|
||||
const resolved = await resolveCallToneSources({
|
||||
callRingtoneId: settings.callRingtoneId,
|
||||
callRingbackTone: settings.callRingbackTone,
|
||||
});
|
||||
|
||||
if (canceled) {
|
||||
resolved.revoke();
|
||||
return;
|
||||
}
|
||||
|
||||
revokeToneUrls?.();
|
||||
revokeToneUrls = resolved.revoke;
|
||||
|
||||
incoming.pause();
|
||||
incoming.currentTime = 0;
|
||||
outgoing.pause();
|
||||
outgoing.currentTime = 0;
|
||||
|
||||
const gain = callRingtoneVolumeToGain(settings.callRingtoneVolume);
|
||||
|
||||
if (resolved.incomingUrl) {
|
||||
incoming.src = resolved.incomingUrl;
|
||||
} else {
|
||||
incoming.removeAttribute('src');
|
||||
}
|
||||
if (resolved.outgoingUrl) {
|
||||
outgoing.src = resolved.outgoingUrl;
|
||||
} else {
|
||||
outgoing.removeAttribute('src');
|
||||
}
|
||||
|
||||
incoming.volume = gain;
|
||||
outgoing.volume = gain;
|
||||
};
|
||||
|
||||
syncSources();
|
||||
|
||||
return () => {
|
||||
canceled = true;
|
||||
revokeToneUrls?.();
|
||||
};
|
||||
ringtoneManager.syncSources(
|
||||
settings.callRingtoneId,
|
||||
settings.callRingbackTone,
|
||||
settings.callRingtoneVolume
|
||||
);
|
||||
}, [settings.callRingtoneId, settings.callRingbackTone, settings.callRingtoneVolume]);
|
||||
|
||||
const stopIncomingRing = useCallback(() => {
|
||||
incomingAudioRef.current?.pause();
|
||||
if (incomingAudioRef.current) incomingAudioRef.current.currentTime = 0;
|
||||
ringtoneManager.stopIncoming();
|
||||
setCallSoundBlocked(false);
|
||||
}, [setCallSoundBlocked]);
|
||||
|
||||
const stopOutgoingRing = useCallback(() => {
|
||||
outgoingAudioRef.current?.pause();
|
||||
if (outgoingAudioRef.current) outgoingAudioRef.current.currentTime = 0;
|
||||
outgoingRingRoomIdRef.current = null;
|
||||
outgoingStartRef.current = null;
|
||||
ringtoneManager.stopOutgoing();
|
||||
}, []);
|
||||
|
||||
const clearIncomingCall = useCallback(() => {
|
||||
@@ -340,34 +277,16 @@ export function useIncomingCallSignaling() {
|
||||
return;
|
||||
}
|
||||
|
||||
const audio = incomingAudioRef.current;
|
||||
if (!audio?.src) {
|
||||
stopIncomingRing();
|
||||
return;
|
||||
}
|
||||
|
||||
if (callEmbed && incomingCall && callEmbed.roomId !== incomingCall.roomId) {
|
||||
stopIncomingRing();
|
||||
return;
|
||||
}
|
||||
|
||||
audio
|
||||
.play()
|
||||
.then(() => {
|
||||
ringtoneManager
|
||||
.playIncoming()
|
||||
?.then(() => {
|
||||
setCallSoundBlocked(false);
|
||||
})
|
||||
.catch(() => {
|
||||
// AbortError is handled in ringtoneManager, any other error comes here
|
||||
setCallSoundBlocked(true);
|
||||
Sentry.metrics.count('sable.call.ringtone.blocked', 1);
|
||||
});
|
||||
}, [
|
||||
callEmbed,
|
||||
incomingCall,
|
||||
incomingRingtoneAllowed,
|
||||
incomingToneIsSilent,
|
||||
setCallSoundBlocked,
|
||||
stopIncomingRing,
|
||||
]);
|
||||
}, [incomingRingtoneAllowed, incomingToneIsSilent, setCallSoundBlocked, stopIncomingRing]);
|
||||
|
||||
signalingHandlerRefs.current = {
|
||||
callEmbed,
|
||||
@@ -581,49 +500,61 @@ export function useIncomingCallSignaling() {
|
||||
handlers().clearIncomingCall();
|
||||
};
|
||||
|
||||
let outgoingRingTimeoutId: number | undefined;
|
||||
|
||||
const evaluateOutgoingFallback = () => {
|
||||
const activeCallRoomId = handlers().callEmbed?.roomId;
|
||||
const outgoingRoom = activeCallRoomId ? mx.getRoom(activeCallRoomId) : null;
|
||||
if (outgoingRoom) {
|
||||
const session = mx.matrixRTC.getRoomSession(outgoingRoom).sessionDescription;
|
||||
if (isCallActive(myUserId, outgoingRoom, session)) {
|
||||
hasCallBeenActiveRef.current = true;
|
||||
}
|
||||
}
|
||||
|
||||
const ringAction = evaluateOutgoingRingbackFallback(
|
||||
{
|
||||
ringRoomId: outgoingRingRoomIdRef.current,
|
||||
ringStartedAt: outgoingStartRef.current,
|
||||
},
|
||||
Date.now(),
|
||||
{
|
||||
...fallbackContext,
|
||||
activeCallRoomId,
|
||||
isDirectRoom: (roomId: string) => handlers().mDirects.has(roomId),
|
||||
outgoingRingbackAllowed: handlers().outgoingRingbackAllowed,
|
||||
declinedRoomId: declinedOutgoingRoomIdRef.current,
|
||||
hasCallBeenActive: hasCallBeenActiveRef.current,
|
||||
}
|
||||
);
|
||||
|
||||
outgoingRingRoomIdRef.current = ringAction.nextState.ringRoomId;
|
||||
outgoingStartRef.current = ringAction.nextState.ringStartedAt;
|
||||
|
||||
if (ringAction.kind === 'stop') {
|
||||
const stop = () => {
|
||||
handlers().stopOutgoingRing();
|
||||
return;
|
||||
window.clearTimeout(outgoingRingTimeoutId);
|
||||
outgoingRingTimeoutId = undefined;
|
||||
};
|
||||
|
||||
if (
|
||||
!activeCallRoomId ||
|
||||
!handlers().outgoingRingbackAllowed ||
|
||||
declinedOutgoingRoomIdRef.current === activeCallRoomId
|
||||
) {
|
||||
outgoingRingRoomIdRef.current = null;
|
||||
outgoingStartRef.current = null;
|
||||
return stop();
|
||||
}
|
||||
|
||||
if (ringAction.started) {
|
||||
debugLog.info('call', 'Outgoing ringing fallback started', { roomId: ringAction.roomId });
|
||||
if (!handlers().mDirects.has(activeCallRoomId)) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
const outgoingAudio = outgoingAudioRef.current;
|
||||
if (outgoingAudio && (ringAction.started || outgoingAudio.paused)) {
|
||||
outgoingAudio.play().catch(() => {
|
||||
Sentry.metrics.count('sable.call.ringback.blocked', 1);
|
||||
});
|
||||
const outgoingRoom = mx.getRoom(activeCallRoomId);
|
||||
if (!outgoingRoom) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
const session = mx.matrixRTC.getRoomSession(outgoingRoom).sessionDescription;
|
||||
|
||||
if (isCallActive(myUserId, outgoingRoom, session)) {
|
||||
hasCallBeenActiveRef.current = true;
|
||||
}
|
||||
|
||||
if (hasCallBeenActiveRef.current) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
const isPending = isOutgoingCallPending(myUserId, outgoingRoom, session);
|
||||
if (!isPending) {
|
||||
return stop();
|
||||
}
|
||||
|
||||
if (!outgoingStartRef.current || outgoingRingRoomIdRef.current !== activeCallRoomId) {
|
||||
outgoingStartRef.current = Date.now();
|
||||
outgoingRingRoomIdRef.current = activeCallRoomId;
|
||||
debugLog.info('call', 'Outgoing ringing fallback started', { roomId: activeCallRoomId });
|
||||
ringtoneManager.playOutgoing();
|
||||
|
||||
window.clearTimeout(outgoingRingTimeoutId);
|
||||
outgoingRingTimeoutId = window.setTimeout(() => {
|
||||
stop();
|
||||
}, OUTGOING_RING_TIMEOUT_MS);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,14 +3,7 @@ import EventEmitter from 'eventemitter3';
|
||||
import { CallControlState } from './CallControlState';
|
||||
import type { ElementMediaStateDetail, ElementMediaStatePayload } from './types';
|
||||
import { ElementWidgetActions } from './types';
|
||||
import {
|
||||
getGridControl,
|
||||
getReactionsButton,
|
||||
getScreenshareButton,
|
||||
getSettingsButton,
|
||||
getSpotlightControl,
|
||||
isElementToggledOn,
|
||||
} from './elementCallDomAdapter';
|
||||
import { getScreenshareButton, isElementToggledOn } from './elementCallDomAdapter';
|
||||
|
||||
export enum CallControlEvent {
|
||||
StateUpdate = 'state_update',
|
||||
@@ -33,22 +26,6 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
return getScreenshareButton(this.document);
|
||||
}
|
||||
|
||||
private get settingsButton(): HTMLElement | undefined {
|
||||
return getSettingsButton(this.document);
|
||||
}
|
||||
|
||||
private get reactionsButton(): HTMLElement | undefined {
|
||||
return getReactionsButton(this.document);
|
||||
}
|
||||
|
||||
private get spotlightControl(): HTMLElement | undefined {
|
||||
return getSpotlightControl(this.document);
|
||||
}
|
||||
|
||||
private get gridControl(): HTMLElement | undefined {
|
||||
return getGridControl(this.document);
|
||||
}
|
||||
|
||||
constructor(state: CallControlState, call: ClientWidgetApi, iframe: HTMLIFrameElement) {
|
||||
super();
|
||||
|
||||
@@ -79,10 +56,6 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
return this.state.screenshare;
|
||||
}
|
||||
|
||||
public get spotlight(): boolean {
|
||||
return this.state.spotlight;
|
||||
}
|
||||
|
||||
public async applyState() {
|
||||
await this.setMediaState({
|
||||
audio_enabled: this.microphone,
|
||||
@@ -102,13 +75,6 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
attributeFilter: ['data-kind', 'aria-pressed', 'aria-checked', 'class'],
|
||||
});
|
||||
}
|
||||
const spotlightControl = this.spotlightControl;
|
||||
if (spotlightControl) {
|
||||
this.controlMutationObserver.observe(spotlightControl, {
|
||||
attributes: true,
|
||||
attributeFilter: ['checked', 'aria-pressed', 'aria-checked', 'data-kind', 'class'],
|
||||
});
|
||||
}
|
||||
|
||||
this.onControlMutation();
|
||||
}
|
||||
@@ -143,8 +109,7 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
data.audio_enabled ?? this.microphone,
|
||||
data.video_enabled ?? this.video,
|
||||
this.sound,
|
||||
this.screenshare,
|
||||
this.spotlight
|
||||
this.screenshare
|
||||
);
|
||||
|
||||
this.state = state;
|
||||
@@ -157,15 +122,8 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
|
||||
public onControlMutation() {
|
||||
const screenshare: boolean = isElementToggledOn(this.screenshareButton);
|
||||
const spotlight: boolean = isElementToggledOn(this.spotlightControl);
|
||||
|
||||
this.state = new CallControlState(
|
||||
this.microphone,
|
||||
this.video,
|
||||
this.sound,
|
||||
screenshare,
|
||||
spotlight
|
||||
);
|
||||
this.state = new CallControlState(this.microphone, this.video, this.sound, screenshare);
|
||||
this.emitStateUpdate();
|
||||
}
|
||||
|
||||
@@ -190,13 +148,7 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
|
||||
this.setSound(sound);
|
||||
|
||||
const state = new CallControlState(
|
||||
this.microphone,
|
||||
this.video,
|
||||
sound,
|
||||
this.screenshare,
|
||||
this.spotlight
|
||||
);
|
||||
const state = new CallControlState(this.microphone, this.video, sound, this.screenshare);
|
||||
this.state = state;
|
||||
this.emitStateUpdate();
|
||||
|
||||
@@ -209,22 +161,6 @@ export class CallControl extends EventEmitter implements CallControlState {
|
||||
this.screenshareButton?.click();
|
||||
}
|
||||
|
||||
public toggleSpotlight() {
|
||||
if (this.spotlight) {
|
||||
this.gridControl?.click();
|
||||
return;
|
||||
}
|
||||
this.spotlightControl?.click();
|
||||
}
|
||||
|
||||
public toggleReactions() {
|
||||
this.reactionsButton?.click();
|
||||
}
|
||||
|
||||
public toggleSettings() {
|
||||
this.settingsButton?.click();
|
||||
}
|
||||
|
||||
public dispose() {
|
||||
this.controlMutationObserver.disconnect();
|
||||
}
|
||||
|
||||
@@ -7,19 +7,10 @@ export class CallControlState {
|
||||
|
||||
public readonly screenshare: boolean;
|
||||
|
||||
public readonly spotlight: boolean;
|
||||
|
||||
constructor(
|
||||
microphone: boolean,
|
||||
video: boolean,
|
||||
sound: boolean,
|
||||
screenshare = false,
|
||||
spotlight = false
|
||||
) {
|
||||
constructor(microphone: boolean, video: boolean, sound: boolean, screenshare = false) {
|
||||
this.microphone = microphone;
|
||||
this.video = video;
|
||||
this.sound = sound;
|
||||
this.screenshare = screenshare;
|
||||
this.spotlight = spotlight;
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,7 @@ vi.mock('../../utils/debugLogger', () => ({
|
||||
debug: vi.fn<(...args: unknown[]) => void>(),
|
||||
}),
|
||||
}));
|
||||
import {
|
||||
getInCallControlsContainer,
|
||||
getLeaveButton,
|
||||
getScreenshareButton,
|
||||
isElementToggledOn,
|
||||
} from './elementCallDomAdapter';
|
||||
import { getScreenshareButton, isElementToggledOn } from './elementCallDomAdapter';
|
||||
|
||||
type FakeElement = {
|
||||
checked?: boolean;
|
||||
@@ -31,31 +26,15 @@ const createFakeElement = (
|
||||
});
|
||||
|
||||
describe('elementCallDomAdapter', () => {
|
||||
it('finds call controls container from leave button ancestry', () => {
|
||||
const container = createFakeElement();
|
||||
const row = createFakeElement({}, { parentElement: container });
|
||||
const leave = createFakeElement({}, { parentElement: row });
|
||||
const doc = {
|
||||
querySelector: (selector: string) =>
|
||||
selector === '[data-testid="incall_leave"]' ? leave : null,
|
||||
} as Document;
|
||||
|
||||
expect(getLeaveButton(doc)).toBe(leave);
|
||||
expect(getInCallControlsContainer(doc)).toBe(container);
|
||||
});
|
||||
|
||||
it('falls back to aria-label selectors when test ids are missing', () => {
|
||||
const leave = createFakeElement();
|
||||
const screenshare = createFakeElement();
|
||||
const doc = {
|
||||
querySelector: (selector: string) => {
|
||||
if (selector === 'button[aria-label*="Leave" i]') return leave;
|
||||
if (selector === 'button[aria-label*="screen" i]') return screenshare;
|
||||
return null;
|
||||
},
|
||||
} as Document;
|
||||
|
||||
expect(getLeaveButton(doc)).toBe(leave);
|
||||
expect(getScreenshareButton(doc)).toBe(screenshare);
|
||||
});
|
||||
|
||||
|
||||
@@ -25,70 +25,12 @@ const queryFirst = (doc: Document | undefined, { key, selectors }: SelectorQuery
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export const getLeaveButton = (doc: Document | undefined): HTMLElement | undefined =>
|
||||
queryFirst(doc, {
|
||||
key: 'leave_button',
|
||||
selectors: ['[data-testid="incall_leave"]', 'button[aria-label*="Leave" i]'],
|
||||
});
|
||||
|
||||
export const getScreenshareButton = (doc: Document | undefined): HTMLElement | undefined =>
|
||||
queryFirst(doc, {
|
||||
key: 'screenshare_button',
|
||||
selectors: ['[data-testid="incall_screenshare"]', 'button[aria-label*="screen" i]'],
|
||||
});
|
||||
|
||||
export const getSettingsButton = (doc: Document | undefined): HTMLElement | undefined => {
|
||||
return queryFirst(doc, {
|
||||
key: 'settings_button',
|
||||
selectors: [
|
||||
'[data-testid="settings-bottom-left"]',
|
||||
'[data-testid="settings-bottom-center"]',
|
||||
'button[aria-label*="Settings" i]',
|
||||
],
|
||||
});
|
||||
};
|
||||
|
||||
export const getReactionsButton = (doc: Document | undefined): HTMLElement | undefined => {
|
||||
return queryFirst(doc, {
|
||||
key: 'reactions_button',
|
||||
selectors: ['[data-testid="incall_reactions"]', 'button[aria-label*="Reaction" i]'],
|
||||
});
|
||||
};
|
||||
|
||||
export const getSpotlightControl = (doc: Document | undefined): HTMLElement | undefined =>
|
||||
queryFirst(doc, {
|
||||
key: 'spotlight_control',
|
||||
selectors: [
|
||||
'input[value="spotlight"]',
|
||||
'button[value="spotlight"]',
|
||||
'[data-testid="layout_spotlight"]',
|
||||
'button[aria-label*="spotlight" i]',
|
||||
],
|
||||
});
|
||||
|
||||
export const getGridControl = (doc: Document | undefined): HTMLElement | undefined =>
|
||||
queryFirst(doc, {
|
||||
key: 'grid_control',
|
||||
selectors: [
|
||||
'input[value="grid"]',
|
||||
'button[value="grid"]',
|
||||
'[data-testid="layout_grid"]',
|
||||
'button[aria-label*="grid" i]',
|
||||
],
|
||||
});
|
||||
|
||||
export const getInCallControlsContainer = (doc: Document | undefined): HTMLElement | undefined => {
|
||||
const leaveButton = getLeaveButton(doc);
|
||||
|
||||
const container = leaveButton?.parentElement?.parentElement;
|
||||
if (container) return container;
|
||||
|
||||
return queryFirst(doc, {
|
||||
key: 'incall_controls_container',
|
||||
selectors: ['[data-testid="incall_controls"]', '[data-testid="incall_toolbar"]'],
|
||||
});
|
||||
};
|
||||
|
||||
export const isElementToggledOn = (element: HTMLElement | undefined): boolean => {
|
||||
if (!element) return false;
|
||||
if ('checked' in element && typeof (element as HTMLInputElement).checked === 'boolean') {
|
||||
|
||||
Reference in new issue
Block a user