|
@@ -5,13 +5,26 @@ import { getVideo } from './videoElement';
|
|
|
import { EventEmitter } from 'eventemitter3';
|
|
|
|
|
|
export type ResolutionType = '1080p' | '2k' | '4k';
|
|
|
+
|
|
|
+export type PlatformType = 'web' | 'electron' | 'canvas';
|
|
|
+
|
|
|
+export interface PlatformConfigType {
|
|
|
+ chromeMediaSourceId?: string | null;
|
|
|
+ canvasId?: string;
|
|
|
+ minWidth?: number;
|
|
|
+ maxWidth?: number;
|
|
|
+ minHeight?: number;
|
|
|
+ maxHeight?: number;
|
|
|
+ frameRate?: number;
|
|
|
+}
|
|
|
+
|
|
|
export interface InitConfigType extends DisplayMediaStreamConstraints {
|
|
|
- uploadUrl: '';
|
|
|
+ uploadUrl?: string;
|
|
|
resolution: ResolutionType;
|
|
|
autoDownload?: boolean;
|
|
|
isElectron?: boolean;
|
|
|
- chromeMediaSourceId?: null;
|
|
|
- chromeAudioSourceId?: null;
|
|
|
+ platform?: PlatformType;
|
|
|
+ config?: PlatformConfigType;
|
|
|
debug?: boolean;
|
|
|
}
|
|
|
export enum RecorderStatusType {
|
|
@@ -35,22 +48,59 @@ export class BasicSimaqRecorder extends EventEmitter {
|
|
|
private recordChunks: Blob[] = [];
|
|
|
private autoDownload = false;
|
|
|
private passiveEnd = false;
|
|
|
- private isElectron: boolean;
|
|
|
+ private platform: string;
|
|
|
+ private uploadUrl: string;
|
|
|
+ private canvasId: string;
|
|
|
+ private platformConfig: PlatformConfigType;
|
|
|
private chromeMediaSourceId: string | null;
|
|
|
- private chromeAudioSourceId: string | null;
|
|
|
+
|
|
|
constructor(arg: InitConfigType) {
|
|
|
super();
|
|
|
console.log('arg', arg);
|
|
|
this.autoDownload = arg.autoDownload;
|
|
|
- this.isElectron = arg.isElectron;
|
|
|
- this.chromeMediaSourceId = arg.chromeMediaSourceId;
|
|
|
- this.chromeAudioSourceId = arg.chromeAudioSourceId;
|
|
|
+ this.platform = arg.platform;
|
|
|
+ this.platformConfig = arg.config;
|
|
|
+ this.uploadUrl = arg.uploadUrl;
|
|
|
+ this.initParams(arg);
|
|
|
videoConstraints.subscribe((value) => {
|
|
|
console.log('subscribe', value);
|
|
|
});
|
|
|
}
|
|
|
private sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
|
|
|
|
+ private get isElectron(): boolean {
|
|
|
+ return this.platform === 'electron';
|
|
|
+ }
|
|
|
+ private get isWeb(): boolean {
|
|
|
+ return this.platform === 'web';
|
|
|
+ }
|
|
|
+ private get isCanvas(): boolean {
|
|
|
+ return this.platform === 'canvas';
|
|
|
+ }
|
|
|
+
|
|
|
+ private get canvasElement(): HTMLCanvasElement {
|
|
|
+ // return document.getElementById(this.canvasId);
|
|
|
+ return document.querySelector(this.canvasId);
|
|
|
+ }
|
|
|
+ private set canvasElement(canvas) {
|
|
|
+ this.canvasElement = canvas;
|
|
|
+ }
|
|
|
+
|
|
|
+ private initParams(arg: InitConfigType): void {
|
|
|
+ switch (arg.platform) {
|
|
|
+ case 'web':
|
|
|
+ break;
|
|
|
+ case 'electron':
|
|
|
+ this.chromeMediaSourceId = arg.config.chromeMediaSourceId;
|
|
|
+ break;
|
|
|
+ case 'canvas':
|
|
|
+ this.canvasId = arg.config.canvasId;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public async startRecord(): Promise<void> {
|
|
|
try {
|
|
|
if (!this.isStartRecoding) {
|
|
@@ -60,9 +110,12 @@ export class BasicSimaqRecorder extends EventEmitter {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- const media = this.isElectron
|
|
|
- ? await this.getEletronDisplayMedia()
|
|
|
- : await this.getDisplayMedia();
|
|
|
+ // const media = this.isElectron
|
|
|
+ // ? await this.getEletronDisplayMedia()
|
|
|
+ // : await this.getDisplayMedia();
|
|
|
+
|
|
|
+ const media = await this.getDefaultMedia();
|
|
|
+
|
|
|
console.log('media', media);
|
|
|
if (media) {
|
|
|
this.emit('startRecord');
|
|
@@ -92,6 +145,41 @@ export class BasicSimaqRecorder extends EventEmitter {
|
|
|
console.error('startRecord::', error);
|
|
|
}
|
|
|
}
|
|
|
+ private getDefaultMedia(): Promise<MediaStream | null> {
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
+ switch (this.platform) {
|
|
|
+ case 'web':
|
|
|
+ return resolve(await this.getDisplayMedia());
|
|
|
+ case 'canvas':
|
|
|
+ return resolve(await this.getCanvasSteam());
|
|
|
+ case 'electron':
|
|
|
+ return resolve(await this.getEletronDisplayMedia());
|
|
|
+ default:
|
|
|
+ return resolve(await this.getDisplayMedia());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private getCanvasSteam(): Promise<MediaStream | null> {
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
+ try {
|
|
|
+ const audioInput = await this.getDeaultAudio();
|
|
|
+ if (audioInput) {
|
|
|
+ this.audioInput = audioInput;
|
|
|
+ }
|
|
|
+ console.log('audioInput', audioInput);
|
|
|
+ console.log('this.canvasElement', this.canvasElement);
|
|
|
+ const stream = this.canvasElement.captureStream(30);
|
|
|
+ if (stream) {
|
|
|
+ return resolve(stream);
|
|
|
+ }
|
|
|
+ return resolve(null);
|
|
|
+ } catch (error) {
|
|
|
+ return resolve(null);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
private getEletronDisplayMedia(): Promise<MediaStream | null> {
|
|
|
return new Promise(async (resolve) => {
|
|
|
try {
|
|
@@ -101,18 +189,21 @@ export class BasicSimaqRecorder extends EventEmitter {
|
|
|
}
|
|
|
console.log('eletron-audioInput', audioInput);
|
|
|
if (navigator.mediaDevices.getDisplayMedia) {
|
|
|
+
|
|
|
+ const videoConfig = {
|
|
|
+ mandatory: {
|
|
|
+ chromeMediaSource: 'desktop',
|
|
|
+ chromeMediaSourceId: this.chromeMediaSourceId,
|
|
|
+ minWidth: this.platformConfig.minWidth || 1280,
|
|
|
+ maxWidth: this.platformConfig.maxWidth || 3840,
|
|
|
+ minHeight: this.platformConfig.minHeight || 720,
|
|
|
+ maxHeight: this.platformConfig.maxHeight || 2160,
|
|
|
+ },
|
|
|
+ };
|
|
|
+ console.log('videoConfig', videoConfig);
|
|
|
const res = await navigator.mediaDevices.getUserMedia({
|
|
|
audio: false,
|
|
|
- video: {
|
|
|
- mandatory: {
|
|
|
- chromeMediaSource: 'desktop',
|
|
|
- chromeMediaSourceId: this.chromeMediaSourceId,
|
|
|
- minWidth: 1280,
|
|
|
- maxWidth: 3840,
|
|
|
- minHeight: 720,
|
|
|
- maxHeight: 2160,
|
|
|
- },
|
|
|
- },
|
|
|
+ video: videoConfig,
|
|
|
} as any as MediaStreamConstraints);
|
|
|
return resolve(res);
|
|
|
}
|
|
@@ -260,5 +351,11 @@ export class BasicSimaqRecorder extends EventEmitter {
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
}
|
|
|
|
|
|
+ public updateCanvas(canvas: HTMLCanvasElement) {
|
|
|
+ if (this.isCanvas) {
|
|
|
+ this.canvasElement = canvas;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private uploadToServer(): void { }
|
|
|
}
|