2021-09-29 20:23:06 +00:00
|
|
|
// Copyright 2016-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-27 16:16:09 +00:00
|
|
|
import { requestMicrophonePermissions } from '../util/requestMicrophonePermissions';
|
2021-09-29 20:23:06 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { WebAudioRecorderClass } from '../window.d';
|
2021-09-29 20:23:06 +00:00
|
|
|
|
|
|
|
export class RecorderClass {
|
|
|
|
private context?: AudioContext;
|
|
|
|
private input?: GainNode;
|
|
|
|
private recorder?: WebAudioRecorderClass;
|
|
|
|
private source?: MediaStreamAudioSourceNode;
|
2021-10-15 18:51:33 +00:00
|
|
|
private stream?: MediaStream;
|
2021-09-29 20:23:06 +00:00
|
|
|
private blob?: Blob;
|
|
|
|
private resolve?: (blob: Blob) => void;
|
|
|
|
|
|
|
|
clear(): void {
|
|
|
|
this.blob = undefined;
|
|
|
|
this.resolve = undefined;
|
|
|
|
|
|
|
|
if (this.source) {
|
|
|
|
this.source.disconnect();
|
|
|
|
this.source = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.recorder) {
|
|
|
|
if (this.recorder.isRecording()) {
|
|
|
|
this.recorder.cancelRecording();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reach in and terminate the web worker used by WebAudioRecorder, otherwise
|
|
|
|
// it gets leaked due to a reference cycle with its onmessage listener
|
|
|
|
this.recorder.worker.terminate();
|
|
|
|
this.recorder = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.input = undefined;
|
2021-10-15 18:51:33 +00:00
|
|
|
this.stream = undefined;
|
2021-09-29 20:23:06 +00:00
|
|
|
|
|
|
|
if (this.context) {
|
|
|
|
this.context.close();
|
|
|
|
this.context = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 16:16:09 +00:00
|
|
|
async start(): Promise<boolean> {
|
|
|
|
const hasMicrophonePermission = await requestMicrophonePermissions();
|
|
|
|
if (!hasMicrophonePermission) {
|
|
|
|
log.info(
|
|
|
|
'Recorder/start: Microphone permission was denied, new audio recording not allowed.'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:23:06 +00:00
|
|
|
this.clear();
|
|
|
|
|
|
|
|
this.context = new AudioContext();
|
|
|
|
this.input = this.context.createGain();
|
|
|
|
|
|
|
|
this.recorder = new window.WebAudioRecorder(this.input, {
|
|
|
|
encoding: 'mp3',
|
|
|
|
workerDir: 'js/', // must end with slash
|
|
|
|
options: {
|
|
|
|
timeLimit: 360, // one minute more than our UI-imposed limit
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.recorder.onComplete = this.onComplete.bind(this);
|
|
|
|
this.recorder.onError = this.onError.bind(this);
|
|
|
|
|
|
|
|
try {
|
2021-12-07 08:21:55 +00:00
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
2021-12-17 01:14:21 +00:00
|
|
|
// TypeScript doesn't know about these options.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
audio: { mandatory: { googAutoGainControl: false } } as any,
|
2021-12-07 08:21:55 +00:00
|
|
|
});
|
2021-09-29 20:23:06 +00:00
|
|
|
if (!this.context || !this.input) {
|
2021-10-27 16:16:09 +00:00
|
|
|
const err = new Error(
|
|
|
|
'Recorder/getUserMedia/stream: Missing context or input!'
|
2021-09-29 20:23:06 +00:00
|
|
|
);
|
2021-10-27 16:16:09 +00:00
|
|
|
this.onError(this.recorder, err);
|
|
|
|
throw err;
|
2021-09-29 20:23:06 +00:00
|
|
|
}
|
|
|
|
this.source = this.context.createMediaStreamSource(stream);
|
|
|
|
this.source.connect(this.input);
|
2021-10-15 18:51:33 +00:00
|
|
|
this.stream = stream;
|
2021-09-29 20:23:06 +00:00
|
|
|
} catch (err) {
|
2021-09-30 20:13:47 +00:00
|
|
|
log.error(
|
|
|
|
'Recorder.onGetUserMediaError:',
|
|
|
|
err && err.stack ? err.stack : err
|
|
|
|
);
|
2021-09-29 20:23:06 +00:00
|
|
|
this.clear();
|
2021-09-30 20:13:47 +00:00
|
|
|
throw err;
|
2021-09-29 20:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.recorder) {
|
|
|
|
this.recorder.startRecording();
|
2021-10-27 16:16:09 +00:00
|
|
|
return true;
|
2021-09-29 20:23:06 +00:00
|
|
|
}
|
2021-10-27 16:16:09 +00:00
|
|
|
|
|
|
|
return false;
|
2021-09-29 20:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop(): Promise<Blob | undefined> {
|
|
|
|
if (!this.recorder) {
|
|
|
|
log.warn('Recorder/stop: Called with no recorder');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:51:33 +00:00
|
|
|
if (this.stream) {
|
|
|
|
this.stream.getTracks().forEach(track => track.stop());
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:23:06 +00:00
|
|
|
if (this.blob) {
|
|
|
|
return this.blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
const promise = new Promise<Blob>(resolve => {
|
|
|
|
this.resolve = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.recorder.finishRecording();
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
onComplete(_recorder: WebAudioRecorderClass, blob: Blob): void {
|
|
|
|
this.blob = blob;
|
|
|
|
this.resolve?.(blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
onError(_recorder: WebAudioRecorderClass, error: Error): void {
|
|
|
|
if (!this.recorder) {
|
|
|
|
log.warn('Recorder/onError: Called with no recorder');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clear();
|
|
|
|
|
2021-10-27 16:16:09 +00:00
|
|
|
log.error('Recorder/onError:', error && error.stack ? error.stack : error);
|
2021-09-29 20:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getBlob(): Blob {
|
|
|
|
if (!this.blob) {
|
|
|
|
throw new Error('no blob found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.blob;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const recorder = new RecorderClass();
|