import { ShareScreenAction } from "../action/ShareScreenAction"; import { ActionParams } from "../action/type/ActionParams"; import { ShareScreenActionParams } from "../action/type/ShareScreenActionParams"; import { Context } from "../context/Context"; import { CustomError } from "../exception/CustomError"; import { ErrorType } from "../exception/enum/ErrorType"; import { setAction } from "../store/slices/action"; import { BaseOperation } from "./base/BaseOperation"; import { EOperationStatus } from "./enum/EOperationStatus"; import { EOperationType } from "./enum/EOperationType"; import { IOperation } from "./interface/IOperation"; export class ShareScreenOperation extends BaseOperation implements IOperation { constructor(context: Context){ super(context); this.type=EOperationType.SHARE_SCREEN; } /*@overwrite*/ public async handler(params ? : ActionParams) : Promise{ return new Promise( async (resolve, reject)=>{ try { this.context.getDeterminants().setUpdating(true); let track; let shareScreenActionParams : ShareScreenActionParams = Object.assign(params ? params : {}); let client = this.context.getResources().getClient(); if(shareScreenActionParams.active == true){ if(!this.context.getDeterminants().getHasScreen()){ track = await this.context.getResources().createScreenTrack(); await client.unpublish(track); await track.setEnabled(true); await client.publish(track); this.context.getDeterminants().setHasScreen(true); track.on("track-ended", async () => { await client.unpublish(this.context.getResources().getScreeanTrack()); await this.context.getDeterminants().setHasScreen(false); if(this.context.getDeterminants().getActionDispatch()){ let params : ShareScreenActionParams = {active: false}; let action = new ShareScreenAction(params); this.context.getDeterminants().getActionDispatch()(setAction({action: action})); } }); } }else{ track = await this.context.getResources().getScreeanTrack(); if(track){ await client.unpublish(track); await track.stop(); await track.close(); } this.context.getDeterminants().setHasScreen(false); } this.status = EOperationStatus.PERFORMED; } catch (error) { this.addError(error); this.status = EOperationStatus.FAILED; } resolve(this.status); }); } }