Skip to content

Commit 2f5225f

Browse files
committed
Fix racy ringing sound
1 parent 85af9f7 commit 2f5225f

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/LegacyCallHandler.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ export default class LegacyCallHandler extends TypedEventEmitter<LegacyCallHandl
390390

391391
const [urlPrefix, loop] = audioInfo[audioId];
392392
const source = await this.backgroundAudio.pickFormatAndPlay(urlPrefix, ["mp3", "ogg"], loop);
393+
if (this.playingSources[audioId]) {
394+
logger.warn(`${logPrefix} Already playing audio ${audioId}!`);
395+
}
393396
this.playingSources[audioId] = source;
394397
logger.debug(`${logPrefix} playing audio successfully`);
395398
}

src/toasts/IncomingCallToast.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
66
Please see LICENSE files in the repository root for full details.
77
*/
88

9-
import React, { type JSX, useCallback, useEffect, useState } from "react";
9+
import React, { type JSX, useCallback, useEffect, useRef, useState } from "react";
1010
import { type Room, type MatrixEvent, type RoomMember, RoomEvent, EventType } from "matrix-js-sdk/src/matrix";
1111
import { Button, ToggleInput, Tooltip, TooltipProvider } from "@vector-im/compound-web";
1212
import VideoCallIcon from "@vector-im/compound-design-tokens/assets/web/icons/video-call-solid";
@@ -133,13 +133,15 @@ export function IncomingCallToast({ notificationEvent }: Props): JSX.Element {
133133
setConnectedCalls(Array.from(CallStore.instance.connectedCalls));
134134
});
135135
const otherCallIsOngoing = connectedCalls.find((call) => call.roomId !== roomId);
136-
// Start ringing if not already.
136+
const playInstance = useRef<Promise<void>>(null);
137137
useEffect(() => {
138+
// Start ringing if not already.
139+
// This section can race, so we use a ref to keep track of whether we have started trying to play.
138140
const isRingToast = notificationContent.notification_type == "ring";
139-
if (isRingToast && !LegacyCallHandler.instance.isPlaying(AudioID.Ring)) {
140-
LegacyCallHandler.instance.play(AudioID.Ring);
141+
if (isRingToast && !playInstance.current && !LegacyCallHandler.instance.isPlaying(AudioID.Ring)) {
142+
playInstance.current = LegacyCallHandler.instance.play(AudioID.Ring);
141143
}
142-
}, [notificationContent.notification_type]);
144+
}, [notificationContent.notification_type, playInstance]);
143145

144146
// Stop ringing on dismiss.
145147
const dismissToast = useCallback((): void => {

0 commit comments

Comments
 (0)