signal-desktop/ts/util/callingTones.ts

87 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-01-03 11:55:46 -08:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 15:34:04 -05:00
// SPDX-License-Identifier: AGPL-3.0-only
2020-08-26 20:03:42 -04:00
import PQueue from 'p-queue';
import { MINUTE } from './durations';
import { Sound, SoundType } from './Sound';
2020-06-04 11:16:19 -07:00
const ringtoneEventQueue = new PQueue({
concurrency: 1,
timeout: MINUTE * 30,
2021-11-23 23:01:03 +01:00
throwOnTimeout: true,
});
2020-06-04 11:16:19 -07:00
class CallingTones {
private ringtone?: Sound;
async handRaised() {
const canPlayTone = window.Events.getCallRingtoneNotification();
if (!canPlayTone) {
return;
}
const tone = new Sound({
soundType: SoundType.CallingHandRaised,
});
await tone.play();
}
async playEndCall(): Promise<void> {
2021-08-18 16:08:14 -04:00
const canPlayTone = window.Events.getCallRingtoneNotification();
2020-08-26 20:03:42 -04:00
if (!canPlayTone) {
return;
}
const tone = new Sound({
soundType: SoundType.CallingHangUp,
2020-06-04 11:16:19 -07:00
});
2020-08-26 20:03:42 -04:00
await tone.play();
2020-06-04 11:16:19 -07:00
}
async playRingtone() {
2020-08-26 20:03:42 -04:00
await ringtoneEventQueue.add(async () => {
if (this.ringtone) {
this.ringtone.stop();
this.ringtone = undefined;
}
2021-08-18 16:08:14 -04:00
const canPlayTone = window.Events.getCallRingtoneNotification();
2020-08-26 20:03:42 -04:00
if (!canPlayTone) {
return;
}
this.ringtone = new Sound({
loop: true,
soundType: SoundType.Ringtone,
2020-08-26 20:03:42 -04:00
});
await this.ringtone.play();
2020-06-04 11:16:19 -07:00
});
}
2020-08-26 20:03:42 -04:00
async stopRingtone() {
await ringtoneEventQueue.add(async () => {
if (this.ringtone) {
this.ringtone.stop();
this.ringtone = undefined;
}
});
2020-06-04 11:16:19 -07:00
}
async someonePresenting() {
2021-08-18 16:08:14 -04:00
const canPlayTone = window.Events.getCallRingtoneNotification();
if (!canPlayTone) {
return;
}
const tone = new Sound({
soundType: SoundType.CallingPresenting,
});
await tone.play();
}
2020-06-04 11:16:19 -07:00
}
export const callingTones = new CallingTones();