import AgoraRTC, { CameraVideoTrackInitConfig, ClientConfig, IAgoraRTCClient, IAgoraRTCRemoteUser, ICameraVideoTrack, ILocalAudioTrack, ILocalVideoTrack, IMicrophoneAudioTrack, MicrophoneAudioTrackInitConfig, ScreenVideoTrackInitConfig } from "agora-rtc-sdk-ng"; import { resolve } from "path"; import { IResources } from "../interface/IResources"; import { Listeners } from "../listeners/Listeners"; import { DeviceT } from "../type/DeviceT"; import { TResources } from "../type/TResources"; export class Resources implements IResources{ public _configClient : ClientConfig = {codec:"vp8", mode:"live"}; public _client!: IAgoraRTCClient; public _listeners!: Listeners; public _audioTrack!: IMicrophoneAudioTrack; public _videoTrack!: ICameraVideoTrack; public _screeanTrack!: ILocalVideoTrack; public _resourcesCallback!:Function; public _volumenCallback!:Function; public _volumenInterval!: Function; constructor(resources?: TResources) { if(resources){ if(resources._configClient) this._configClient = resources._configClient; if(resources._client) this._client = resources._client; if(resources._listeners) this._listeners = resources._listeners; if(resources._audioTrack) this._audioTrack = resources._audioTrack; if(resources._videoTrack) this._videoTrack = resources._videoTrack; if(resources._screeanTrack) this._screeanTrack = resources._screeanTrack; if(resources._resourcesCallback) this._resourcesCallback = resources._resourcesCallback; if(resources._volumenCallback) this._resourcesCallback = resources._volumenCallback; if(resources._volumenInterval) this._volumenInterval = resources._volumenInterval; } } public doResourcesCallback(){ if(this._resourcesCallback){ this.getResourcesCallback()(this); } } public async getMicrophones() : Promise{ let devices = await AgoraRTC.getDevices(); if(devices.length == 0 ) return []; let microphones = devices.filter( (device) => device.kind === "audioinput" ); return microphones; } public async getSpeakers() : Promise{ let devices = await AgoraRTC.getDevices(); if(devices.length == 0 ) return []; let speakers = devices.filter( (device) => device.kind === "audiooutput" ); return speakers; } public async getCameras() : Promise{ let devices = await AgoraRTC.getDevices(); if(devices.length == 0 ) return []; let cameras = devices.filter( (device) => device.kind === "videoinput" ); return cameras; } public getAudioTrack(): IMicrophoneAudioTrack { return this._audioTrack; } public async createAudioTrack(microphoneSelected : DeviceT, encoderConfig?:any){ let audioParams : MicrophoneAudioTrackInitConfig = { AEC:true, AGC:false, ANS:true, encoderConfig: "music_standard", microphoneId: microphoneSelected.deviceId, }; let track = await AgoraRTC.createMicrophoneAudioTrack(audioParams); this.setAudioTrack(track); return track; } public async hasDevicePermissions() : Promise{ return new Promise(async ()=>{ let client!: IAgoraRTCClient; if(this._client){ client = this._client; }else{ client = await AgoraRTC.createClient(this.getConfigClient()); } try { let microphones = await this.getMicrophones(); if(microphones && microphones.length > 0){ await this.createAudioTrack(microphones[0]) }else{ resolve(false); } } catch (error) { resolve(false); } resolve(true); }); } public setAudioTrack(value: IMicrophoneAudioTrack) { this._audioTrack = value; this.doResourcesCallback(); } public getVideoTrack(): ICameraVideoTrack { return this._videoTrack; } public async createVideoTrack(cameraSelected : DeviceT, encoderConfig?:any){ let cameras = await this.getCameras(); let videoParams : CameraVideoTrackInitConfig = { facingMode:"user", encoderConfig: { width: 640, height:480, frameRate:30, bitrateMin:400, bitrateMax:990 }, optimizationMode: "motion", cameraId: cameraSelected ? cameraSelected.deviceId : cameras && cameras.length > 0? cameras[0].deviceId : undefined, }; let track = await AgoraRTC.createCameraVideoTrack(videoParams); this.setVideoTrack(track); return track; } public setVideoTrack(value: ICameraVideoTrack) : void{ this._videoTrack = value; this.doResourcesCallback(); } public getScreeanTrack(): ILocalVideoTrack { return this._screeanTrack; } public async createScreenTrack(encoderConfig?:any){ let videoParams : ScreenVideoTrackInitConfig = { encoderConfig: { width: 1920, height:1080, frameRate:30, bitrateMin:2080, bitrateMax:3160 }, optimizationMode: "detail", }; let track = await AgoraRTC.createScreenVideoTrack(videoParams, "disable"); this.setScreeanTrack(track); return track; } public setScreeanTrack(value: ILocalVideoTrack) : void{ this._screeanTrack = value; this.doResourcesCallback(); } public getClient(): IAgoraRTCClient { return this._client; } public setClient(value: IAgoraRTCClient) : void{ this._client = value; this.doResourcesCallback(); } public getListeners(): Listeners { return this._listeners; } public setListeners(value : Listeners): void { this._listeners = value; this.doResourcesCallback(); } public getConfigClient(): ClientConfig { return this._configClient; } public setConfigClient(value: ClientConfig): void { this._configClient = value; this.doResourcesCallback(); } public getResourcesCallback(): Function { return this._resourcesCallback; } public setResourcesCallback(value: Function): void { this._resourcesCallback = value; } public getVolumenCallback(): Function { return this._volumenCallback; } public setVolumenCallback(value: Function): void { this._volumenCallback = value; } public startRecordVolumen(volumenCallback : Function): void { this._volumenInterval = setInterval(()=>{ if(volumenCallback && this._audioTrack){ volumenCallback(this._audioTrack.getVolumeLevel()); } }, 500); } public stopRecordVolumen(): void { clearInterval(this._volumenInterval); } }