Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
176 lines
3.9 KiB
TypeScript
176 lines
3.9 KiB
TypeScript
import { Emitter } from '@rocket.chat/emitter';
|
|
import { useCallback, useSyncExternalStore } from 'react';
|
|
|
|
type VideoRecorderEvents = {
|
|
cameraStartedChange: boolean;
|
|
};
|
|
|
|
class VideoRecorder extends Emitter<VideoRecorderEvents> {
|
|
private _cameraStarted = false;
|
|
|
|
private started = false;
|
|
|
|
private recordingAvailable = false;
|
|
|
|
private videoel: HTMLVideoElement | undefined;
|
|
|
|
private chunks: Blob[] = [];
|
|
|
|
private stream: MediaStream | undefined;
|
|
|
|
private mediaRecorder: MediaRecorder | undefined;
|
|
|
|
private sessionId = 0;
|
|
|
|
public get cameraStarted(): boolean {
|
|
return this._cameraStarted;
|
|
}
|
|
|
|
private setCameraStarted(value: boolean) {
|
|
if (this._cameraStarted === value) {
|
|
return;
|
|
}
|
|
this._cameraStarted = value;
|
|
this.emit('cameraStartedChange', value);
|
|
}
|
|
|
|
public getSupportedMimeTypes() {
|
|
if (window.MediaRecorder.isTypeSupported('video/webm')) {
|
|
return 'video/webm; codecs=vp8,opus';
|
|
}
|
|
if (window.MediaRecorder.isTypeSupported('video/mp4')) {
|
|
return 'video/mp4';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public start(videoel?: HTMLVideoElement, cb?: (this: this, success: boolean) => void) {
|
|
this.videoel = videoel;
|
|
const currentSessionId = ++this.sessionId;
|
|
|
|
const handleSuccess = (stream: MediaStream) => {
|
|
if (this.isStaleSession(currentSessionId)) {
|
|
this.stopStreamTracks(stream);
|
|
return;
|
|
}
|
|
this.startUserMedia(stream);
|
|
cb?.call(this, true);
|
|
};
|
|
|
|
const handleError = (error: any) => {
|
|
console.error(error);
|
|
cb?.call(this, false);
|
|
};
|
|
|
|
if (navigator.mediaDevices) {
|
|
navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then(handleSuccess, handleError);
|
|
return;
|
|
}
|
|
|
|
const oldGetUserMedia = navigator.getUserMedia ?? navigator.webkitGetUserMedia ?? navigator.mozGetUserMedia ?? navigator.msGetUserMedia;
|
|
|
|
if (oldGetUserMedia) {
|
|
oldGetUserMedia.call(navigator, { audio: true, video: true }, handleSuccess, handleError);
|
|
return;
|
|
}
|
|
|
|
cb?.call(this, false);
|
|
}
|
|
|
|
public record() {
|
|
this.chunks = [];
|
|
if (!this.stream) {
|
|
return;
|
|
}
|
|
|
|
this.mediaRecorder = new MediaRecorder(this.stream, { mimeType: this.getSupportedMimeTypes() });
|
|
this.mediaRecorder.ondataavailable = (blobev) => {
|
|
this.chunks.push(blobev.data);
|
|
if (!this.recordingAvailable) {
|
|
this.recordingAvailable = true;
|
|
}
|
|
};
|
|
this.mediaRecorder.start();
|
|
}
|
|
|
|
private stopStreamTracks(stream: MediaStream) {
|
|
const vtracks = stream.getVideoTracks();
|
|
for (const vtrack of Array.from(vtracks)) {
|
|
vtrack.stop();
|
|
}
|
|
|
|
const atracks = stream.getAudioTracks();
|
|
for (const atrack of Array.from(atracks)) {
|
|
atrack.stop();
|
|
}
|
|
}
|
|
|
|
private isStaleSession(sessionId: number): boolean {
|
|
return this.sessionId !== sessionId;
|
|
}
|
|
|
|
private startUserMedia(stream: MediaStream) {
|
|
if (!this.videoel) {
|
|
return;
|
|
}
|
|
this.stream = stream;
|
|
|
|
try {
|
|
this.videoel.srcObject = stream;
|
|
} catch (error) {
|
|
const URL = window.URL || window.webkitURL;
|
|
this.videoel.src = URL.createObjectURL(stream as unknown as MediaSource | Blob);
|
|
}
|
|
|
|
this.started = true;
|
|
this.setCameraStarted(true);
|
|
}
|
|
|
|
public stop(cb?: (blob: Blob) => void) {
|
|
this.sessionId++;
|
|
|
|
this.stopRecording();
|
|
|
|
if (this.stream) {
|
|
this.stopStreamTracks(this.stream);
|
|
}
|
|
|
|
if (this.videoel) {
|
|
this.videoel.pause();
|
|
this.videoel.src = '';
|
|
}
|
|
|
|
const wasStarted = this.started;
|
|
this.started = false;
|
|
this.setCameraStarted(false);
|
|
this.recordingAvailable = false;
|
|
|
|
if (cb && this.chunks && wasStarted) {
|
|
const blob = new Blob(this.chunks);
|
|
cb(blob);
|
|
}
|
|
|
|
delete this.stream;
|
|
delete this.videoel;
|
|
}
|
|
|
|
public stopRecording() {
|
|
if (!this.started || !this.mediaRecorder) {
|
|
return;
|
|
}
|
|
|
|
this.mediaRecorder.stop();
|
|
delete this.mediaRecorder;
|
|
}
|
|
}
|
|
|
|
const instance = new VideoRecorder();
|
|
|
|
export { instance as VideoRecorder };
|
|
|
|
export const useVideoRecorderCameraStarted = (): boolean =>
|
|
useSyncExternalStore(
|
|
useCallback((onStoreChange) => instance.on('cameraStartedChange', onStoreChange), []),
|
|
() => instance.cameraStarted,
|
|
);
|