From e288f61f0a9abd0ce2652ef4c43b8c9e53e84b3f Mon Sep 17 00:00:00 2001 From: SuperKenVery <39673849+SuperKenVery@users.noreply.github.com> Date: Fri, 5 Jul 2024 02:08:06 +0800 Subject: [PATCH] Prevent Element appearing in system media controls (#10995) * Use WebAudio API to play notification sound So that it won't appear in system media control. * Run prettier * Chosse from mp3 and ogg * Run prettier * Use WebAudioAPI everywhere There's still one remoteAudio. I'm not sure what it does. It seems it's only used in tests... * Run prettier * Eliminate a stupid error * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Update setupManualMocks.ts * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * delint Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * mocks Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * mocks Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Simplify Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * covg Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- jest.config.ts | 2 +- package.json | 3 +- src/LegacyCallHandler.tsx | 130 ++++-------------- src/Notifier.ts | 27 ++-- src/audio/BackgroundAudio.ts | 74 ++++++++++ .../models/VoiceBroadcastRecording.ts | 12 +- test/LegacyCallHandler-test.ts | 79 +++++++---- test/Notifier-test.ts | 21 +++ test/setup/mocks.ts | 35 +++++ test/setup/setupManualMocks.ts | 6 + .../models/VoiceBroadcastRecording-test.ts | 25 +++- yarn.lock | 5 + 12 files changed, 255 insertions(+), 164 deletions(-) create mode 100644 src/audio/BackgroundAudio.ts create mode 100644 test/setup/mocks.ts diff --git a/jest.config.ts b/jest.config.ts index 182c28f68a..7293e5b3be 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -22,7 +22,7 @@ const config: Config = { testEnvironment: "jsdom", testMatch: ["/test/**/*-test.[jt]s?(x)"], globalSetup: "/test/globalSetup.ts", - setupFiles: ["jest-canvas-mock"], + setupFiles: ["jest-canvas-mock", "web-streams-polyfill/polyfill"], setupFilesAfterEnv: ["/test/setupTests.ts"], moduleNameMapper: { "\\.(gif|png|ttf|woff2)$": "/__mocks__/imageMock.js", diff --git a/package.json b/package.json index 621cd74104..e3a4e7d4ce 100644 --- a/package.json +++ b/package.json @@ -227,7 +227,8 @@ "stylelint-config-standard": "^36.0.0", "stylelint-scss": "^6.0.0", "ts-node": "^10.9.1", - "typescript": "5.5.2" + "typescript": "5.5.2", + "web-streams-polyfill": "^4.0.0" }, "peerDependencies": { "postcss": "^8.4.19", diff --git a/src/LegacyCallHandler.tsx b/src/LegacyCallHandler.tsx index 7abdb236ae..7e5f23fc3f 100644 --- a/src/LegacyCallHandler.tsx +++ b/src/LegacyCallHandler.tsx @@ -66,6 +66,7 @@ import { localNotificationsAreSilenced } from "./utils/notifications"; import { SdkContextClass } from "./contexts/SDKContext"; import { showCantStartACallDialog } from "./voice-broadcast/utils/showCantStartACallDialog"; import { isNotNull } from "./Typeguards"; +import { BackgroundAudio } from "./audio/BackgroundAudio"; export const PROTOCOL_PSTN = "m.protocol.pstn"; export const PROTOCOL_PSTN_PREFIXED = "im.vector.protocol.pstn"; @@ -157,8 +158,6 @@ export default class LegacyCallHandler extends EventEmitter { // Calls started as an attended transfer, ie. with the intention of transferring another // call with a different party to this one. private transferees = new Map(); // callId (target) -> call (transferee) - private audioPromises = new Map>(); - private audioElementsWithListeners = new Map(); private supportsPstnProtocol: boolean | null = null; private pstnSupportPrefixed: boolean | null = null; // True if the server only support the prefixed pstn protocol private supportsSipNativeVirtual: boolean | null = null; // im.vector.protocol.sip_virtual and im.vector.protocol.sip_native @@ -170,6 +169,9 @@ export default class LegacyCallHandler extends EventEmitter { private silencedCalls = new Set(); // callIds + private backgroundAudio = new BackgroundAudio(); + private playingSources: Record = {}; // Record them for stopping + public static get instance(): LegacyCallHandler { if (!window.mxLegacyCallHandler) { window.mxLegacyCallHandler = new LegacyCallHandler(); @@ -199,33 +201,11 @@ export default class LegacyCallHandler extends EventEmitter { } public start(): void { - // add empty handlers for media actions, otherwise the media keys - // end up causing the audio elements with our ring/ringback etc - // audio clips in to play. - if (navigator.mediaSession) { - navigator.mediaSession.setActionHandler("play", function () {}); - navigator.mediaSession.setActionHandler("pause", function () {}); - navigator.mediaSession.setActionHandler("seekbackward", function () {}); - navigator.mediaSession.setActionHandler("seekforward", function () {}); - navigator.mediaSession.setActionHandler("previoustrack", function () {}); - navigator.mediaSession.setActionHandler("nexttrack", function () {}); - } - if (SettingsStore.getValue(UIFeature.Voip)) { MatrixClientPeg.safeGet().on(CallEventHandlerEvent.Incoming, this.onCallIncoming); } this.checkProtocols(CHECK_PROTOCOLS_ATTEMPTS); - - // Add event listeners for the