Extra worklet module loading and mock it in tests

pull/28217/head
Johannes Marbach 2023-11-13 19:52:15 +01:00
parent 64ba0b4130
commit 5855930221
3 changed files with 31 additions and 12 deletions

View File

@ -33,7 +33,7 @@ const config: Config = {
"waveWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js", "waveWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js",
"workers/(.+)Factory": "<rootDir>/__mocks__/workerFactoryMock.js", "workers/(.+)Factory": "<rootDir>/__mocks__/workerFactoryMock.js",
"^!!raw-loader!.*": "jest-raw-loader", "^!!raw-loader!.*": "jest-raw-loader",
"RecorderWorklet": "<rootDir>/__mocks__/empty.js", "recorderWorkletFactory": "<rootDir>/__mocks__/empty.js",
}, },
transformIgnorePatterns: ["/node_modules/(?!matrix-js-sdk).+$"], transformIgnorePatterns: ["/node_modules/(?!matrix-js-sdk).+$"],
collectCoverageFrom: [ collectCoverageFrom: [

View File

@ -28,11 +28,7 @@ import { UPDATE_EVENT } from "../stores/AsyncStore";
import { createAudioContext } from "./compat"; import { createAudioContext } from "./compat";
import { FixedRollingArray } from "../utils/FixedRollingArray"; import { FixedRollingArray } from "../utils/FixedRollingArray";
import { clamp } from "../utils/numbers"; import { clamp } from "../utils/numbers";
// This import is needed for dead code analysis but not actually used because the import recorderWorkletFactory from "./recorderWorkletFactory";
// built-in worker / worklet handling in Webpack 5 only supports static paths
// @ts-ignore no-unused-locals
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import mxRecorderWorkletPath from "./RecorderWorklet";
const CHANNELS = 1; // stereo isn't important const CHANNELS = 1; // stereo isn't important
export const SAMPLE_RATE = 48000; // 48khz is what WebRTC uses. 12khz is where we lose quality. export const SAMPLE_RATE = 48000; // 48khz is what WebRTC uses. 12khz is where we lose quality.
@ -133,12 +129,7 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
if (this.recorderContext.audioWorklet) { if (this.recorderContext.audioWorklet) {
// Set up our worklet. We use this for timing information and waveform analysis: the // Set up our worklet. We use this for timing information and waveform analysis: the
// web audio API prefers this be done async to avoid holding the main thread with math. // web audio API prefers this be done async to avoid holding the main thread with math.
await recorderWorkletFactory(this.recorderContext);
// The audioWorklet.addModule syntax is required for Webpack 5 to correctly recognise
// this as a worklet rather than an asset. This also requires the parser.javascript.worker
// configuration described in https://github.com/webpack/webpack.js.org/issues/6869.
const audioWorklet = this.recorderContext.audioWorklet;
await audioWorklet.addModule(new URL("./RecorderWorklet.ts", import.meta.url));
this.recorderWorklet = new AudioWorkletNode(this.recorderContext, WORKLET_NAME); this.recorderWorklet = new AudioWorkletNode(this.recorderContext, WORKLET_NAME);
this.recorderSource.connect(this.recorderWorklet); this.recorderSource.connect(this.recorderWorklet);

View File

@ -0,0 +1,28 @@
/*
Copyright 2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This import is needed for dead code analysis but not actually used because the
// built-in worker / worklet handling in Webpack 5 only supports static paths
// @ts-ignore no-unused-locals
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import mxRecorderWorkletPath from "./RecorderWorklet";
export default function recorderWorkletFactory(context: AudioContext): Promise<void> {
// The context.audioWorklet.addModule syntax is required for Webpack 5 to correctly recognise
// this as a worklet rather than an asset. This also requires the parser.javascript.worker
// configuration described in https://github.com/webpack/webpack.js.org/issues/6869.
return context.audioWorklet.addModule(new URL("./RecorderWorklet.ts", import.meta.url));
}