UNPKG

tjc-sdk

Version:

TIANJI IM SDK

2 lines (1 loc) 13.1 kB
import t from"../../client/tjcEvent.js";import{isElectron as e,ipcRenderer as s,BrowserWindow as i,remote as h}from"../../platform2.js";import n from"../../model/conversationType.js";import{MessageContentType as o}from"../../messages/messageContentType.js";import r from"../../client/tjc.js";import l from"../../client/messageConfig.js";import a from"detectrtc";import c from"../../config.js";import{longValue as f,numberValue as u}from"../../util/longUtil.js";import m from"../../model/conversation.js";import d from"./callEndReason.js";import p from"../messages/callByeMessageContent.js";import g from"./avenginekit.js";class v{tjc;queueEvents=[];callWin;isVoipWindowReady=!1;type;conference=!1;conversation;callId;inviteMessageUid;participants=[];isSupportVoip=!1;hasMicrophone=!1;hasSpeaker=!1;hasWebcam=!1;onVoipCallErrorCallback;onVoipCallStatusCallback=(t,e)=>{};setup(s){if(this.tjc!==s&&(this.tjc=s,a.load((()=>{this.isSupportVoip=a.isWebRTCSupported,this.hasMicrophone=a.hasMicrophone,this.hasSpeaker=!0,this.hasWebcam=a.hasWebcam})),this.event=s.eventEmitter,this.event.on(t.ReceiveMessage,this.onReceiveMessage),this.event.on(t.ConferenceEvent,this.onReceiveConferenceEvent),this.event.on(t.ConnectionStatusChanged,this.onConnectionStatusChange),!e())){const t=require("events").EventEmitter;this.events=new t,this.events.on("voip-message",this.sendVoipListener),this.events.on("conference-request",this.sendConferenceRequestListener),this.events.on("update-call-start-message",this.updateCallStartMessageContentListener)}}updateCallStartMessageContentListener=(t,e)=>{let s=e.messageUid,i=e.content,h=r.getMessageByUid(s);if(!h)return;let n=h.messageContent;n.connectTime=i.connectTime?i.connectTime:n.connectTime,n.endTime=i.endTime?i.endTime:n.endTime,n.status=i.status,n.audioOnly=i.audioOnly,r.updateMessageContent(h.messageId,n)};sendConferenceRequestListener=(t,e)=>{r.sendConferenceRequestEx(e.sessionId?e.sessionId:0,e.roomId?e.roomId:"",e.request,e.data,e.advance,((t,s)=>{this.emitToVoip("sendConferenceRequestResult",{error:t,sendConferenceRequestId:e.sendConferenceRequestId,response:s})}))};sendVoipListener=(t,e)=>{let s=new(l.getMessageContentClazz(e.content.type));s.decode(e.content);let i=r.getServerDeltaTime();if(s.type===o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT)this.participants.push(s.participants);else if(s.type===o.VOIP_CONTENT_TYPE_END&&!s.callId)return;let h=new m(e.conversation.type,e.conversation.target,e.conversation.line);r.sendConversationMessage(h,s,e.toUsers,((t,e)=>{}),((t,e)=>{}),((t,h)=>{this.emitToVoip("sendMessageResult",{error:0,sendMessageId:e.sendMessageId,messageUid:t,timestamp:f(u(h)-i)}),s.type===o.VOIP_CONTENT_TYPE_START&&(this.inviteMessageUid=t)}),(t=>{this.emitToVoip("sendMessageResult",{error:t,sendMessageId:e.sendMessageId})}))};onReceiveConferenceEvent=t=>{this.emitToVoip("conferenceEvent",t)};onConnectionStatusChange=t=>{this.emitToVoip("connectionStatus",t)};onReceiveMessage=t=>{if(!c.ENABLE_MULTI_VOIP_CALL&&t.conversation.type===n.Group)return;if(!c.ENABLE_SINGLE_VOIP_CALL&&t.conversation.type===n.Single)return;if(!e()&&t.messageContent===o.VOIP_REMOTE_CONTROL_REQUEST)return;let s=(new Date).valueOf(),i=r.getServerDeltaTime();if(s-(u(t.timestamp)-i)>=9e4)return;let h=t.messageContent;if(!(this.callWin&&this.conference&&h.type!==o.CONFERENCE_CONTENT_TYPE_COMMAND||h.notLoaded))if(1!==t.direction||h.type!==o.VOIP_CONTENT_TYPE_START&&h.type!==o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT||(this.callWin&&(h.type===o.VOIP_CONTENT_TYPE_START||h.type===o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT&&h.participants.indexOf(r.getUserId())>=0)&&this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-1),this.isSupportVoip&&(g.ENABLE_VOIP_WHEN_NO_MIC_AND_SPEAKER||this.hasSpeaker&&this.hasMicrophone))){if((t.conversation.type===n.Single||t.conversation.type===n.Group||this.conference&&t.conversation.type===n.ChatRoom)&&(h.type===o.VOIP_CONTENT_TYPE_START||h.type===o.VOIP_CONTENT_TYPE_END||h.type===o.VOIP_CONTENT_TYPE_ACCEPT||h.type===o.VOIP_CONTENT_TYPE_SIGNAL||h.type===o.VOIP_CONTENT_TYPE_MODIFY||h.type===o.VOIP_CONTENT_TYPE_ACCEPT_T||h.type===o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT||h.type===o.VOIP_CONTENT_TYPE_MUTE_VIDEO||h.type===o.VOIP_Join_Call_Request||h.type===o.VOIP_REMOTE_CONTROL_INPUT_EVENT||h.type===o.CONFERENCE_CONTENT_TYPE_KICKOFF_MEMBER||h.type===o.CONFERENCE_CONTENT_TYPE_CHANGE_MODE||h.type===o.CONFERENCE_CONTENT_TYPE_COMMAND||h.type===o.VOIP_REMOTE_CONTROL_INVITE||h.type===o.VOIP_REMOTE_CONTROL_ACCEPT_INVITE||h.type===o.VOIP_REMOTE_CONTROL_REQUEST||h.type===o.VOIP_REMOTE_CONTROL_ACCEPT_REQUEST||h.type===o.VOIP_REMOTE_CONTROL_END)){if(0===t.direction&&h.type!==o.VOIP_CONTENT_TYPE_END&&h.type!==o.VOIP_CONTENT_TYPE_ACCEPT&&h.type!==o.VOIP_REMOTE_CONTROL_ACCEPT_INVITE&&h.type!==o.VOIP_REMOTE_CONTROL_ACCEPT_REQUEST)return;let e=[],s=r.getUserInfo(r.getUserId());if(h.type===o.VOIP_CONTENT_TYPE_START||h.type===o.VOIP_REMOTE_CONTROL_REQUEST){if(this.conversation=t.conversation,this.callId=h.callId,this.inviteMessageUid=t.messageUid,this.participants.push(...h.targetIds),this.participants.push(t.from),this.participants=this.participants.filter((t=>t!==s.uid)),t.conversation.type===n.Single)e=[r.getUserInfo(t.from)];else{let i=h.targetIds.filter((t=>t!==s.uid));i.push(t.from),e=r.getUserInfos(i,t.conversation.target)}this.callWin||this.conversation&&(t.participantUserInfos=e,t.selfUserInfo=s,t.timestamp=f(u(t.timestamp)-i),this.showCallUI(t.conversation,!1,{event:"message",args:{...t,remoteControl:h.type===o.VOIP_REMOTE_CONTROL_REQUEST}}))}else if(h.type===o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT){let i=[...h.participants];h.existParticipants&&h.existParticipants.forEach((t=>{i.push(t.userId)})),this.conversation=t.conversation,this.callId=h.callId,this.inviteMessageUid=t.messageUid,this.participants.push(...i),i=i.filter((t=>t.uid!==s.uid)),e=r.getUserInfos(i,t.conversation.target),!this.callWin&&h.participants.indexOf(s.uid)>-1&&this.conversation&&this.showCallUI(t.conversation,!1,{event:"message",args:t})}else if(h.type===o.VOIP_CONTENT_TYPE_END&&h.callId!==this.callId)return;if(t.conversation.type===n.Group&&(h.type===o.VOIP_CONTENT_TYPE_START||h.type===o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT)){let e=r.getGroupMemberIds(t.conversation.target);t.groupMemberUserInfos=r.getUserInfos(e,t.conversation.target)}if(t.participantUserInfos=e,t.selfUserInfo=s,t.timestamp=f(u(t.timestamp)-i),this.callWin){let e=!1;(o.VOIP_CONTENT_TYPE_START===t.messageContent.type||o.VOIP_REMOTE_CONTROL_REQUEST===t.messageContent.type||o.VOIP_CONTENT_TYPE_ADD_PARTICIPANT===t.messageContent.type&&h.participants.indexOf(r.getUserId())>=0)&&(e=!0),e||this.emitToVoip("message",t)}}}else this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-2)};emitToVoip(t,s){if(e())if(this.isVoipWindowReady){if(s=JSON.stringify(s),!this.callWin.isDestroyed())try{this.callWin.webContents.send(t,s)}catch(t){}}else this.queueEvents&&this.queueEvents.push({event:t,args:s});else this.events?this.events.emit(t,t,s):this.queueEvents&&this.queueEvents.push({event:t,args:s})}emitToMain(t,i){e()?s.send(t,i):this.events.emit(t,t,i)}listenVoipEvent=(t,i)=>{e()?s.on(t,i):this.events.on(t,i)};requestRemoteControl(t){t.type===n.Single?this.t(t,!1,[t.target],"",!0):this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-1)}startCall(t,e,s,i=""){this.t(t,e,s,i,!1)}t(t,e,s,i="",h=!1){if(this.callWin)return void(this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-1));if(!this.isSupportVoip||!(g.ENABLE_VOIP_WHEN_NO_MIC_AND_SPEAKER||this.hasSpeaker&&this.hasMicrophone))return void(this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-2));delete(t=Object.assign(new m,t)).i;let o=r.getUserInfo(r.getUserId());s=s.filter((t=>t!==o.uid));let l=t.target+Math.floor(1e4*Math.random());this.conversation=t,this.participants.push(...s),this.callId=l;let a,c=r.getUserInfos(s);if(t.type===n.Group){let e=r.getGroupMemberIds(t.target);a=r.getUserInfos(e,t.target)}this.showCallUI(t,!1,{event:"startCall",args:{conversation:t,audioOnly:e,callId:l,selfUserInfo:o,groupMemberUserInfos:a,participantUserInfos:c,callExtra:i,remoteControl:h}})}startConference(t,e,s,i,h,n,o,l,a=!1,c,f,u=!1,m=!1){if(this.callWin)return void(this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-1));if(!this.isSupportVoip||!(g.ENABLE_VOIP_WHEN_NO_MIC_AND_SPEAKER||this.hasSpeaker&&this.hasMicrophone))return void(this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-2));t=t||r.getUserId()+Math.floor(1e4*Math.random()),this.callId=t,this.conversation=null,this.conference=!0,r.joinChatroom(t,(()=>{}),(t=>{}));let d=r.getUserInfo(r.getUserId());this.showCallUI(null,!0,{event:"startConference",args:{audioOnly:e,callId:t,pin:s||Math.ceil(1e6*Math.random())+"",host:i,title:h,desc:n,audience:o,advance:l,record:a,selfUserInfo:d,extra:c,callExtra:f,muteAudio:u,muteVideo:m}})}joinConference(t,e,s,i,h,n,o,l,a,c,f=null,u=null){if(this.callWin)return void(this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-1));if(!this.isSupportVoip||!(g.ENABLE_VOIP_WHEN_NO_MIC_AND_SPEAKER||this.hasSpeaker&&this.hasMicrophone))return void(this.onVoipCallErrorCallback&&this.onVoipCallErrorCallback(-2));this.conversation=null,this.conference=!0,this.callId=t,r.joinChatroom(t,(()=>{}),(t=>{}));let m=r.getUserInfo(r.getUserId());this.showCallUI(null,!0,{event:"joinConference",args:{audioOnly:e,callId:t,pin:s,host:i,title:h,desc:n,audience:o,advance:l,muteAudio:a,muteVideo:c,selfUserInfo:m,extra:f,callExtra:u}})}showCallUI(t,s,o){if(o.args.remoteControl&&!e())return;if(process&&"linux"===process.platform)return;let l=s?"conference":o.args.remoteControl?"single-rc":t.type===n.Single?"single":"multi";this.type=l;let a=360,c=640,f=360,u=640;switch(l){case"single":a=360,c=640;break;case"single-rc":case"multi":case"conference":a=960,c=600,f=800,u=480}if(e()){let t=new i({width:a,height:c,minWidth:f,minHeight:u,resizable:!0,maximizable:!0,transparent:!!s,frame:!s,webPreferences:{scrollBounce:!1,nativeWindowOpen:!0,nodeIntegration:!0,contextIsolation:!1}});this.callWin=t,this.isVoipWindowReady=!1,h.require("@electron/remote/main").enable(t.webContents),t.webContents.on("did-finish-load",(()=>{this.onVoipWindowReady()})),localStorage.getItem("enable_voip_debug")&&t.webContents.openDevTools(),t.on("close",(()=>{this.onVoipWindowClose()}));let e=window.location.hash,n=window.location.origin;e?n=window.location.href.replace(e,"#/voip"):n+="/voip",n+="/"+l+"?t="+(new Date).getTime(),t.loadURL(n),t.show(),t.removeMenu(),this.emitToVoip(o.event,o.args)}else this.callWin=window,this.events.once("close-voip-div",(()=>{this.onVoipCallStatusCallback&&this.onVoipCallStatusCallback(this.conversation,!1),this.callWin=null,this.isVoipWindowReady=!1,this.conference&&(r.quitChatroom(this.callId),this.conference=!1),this.conference=!1,this.callId=null,this.conversation=null,this.participants=[],this.queueEvents=[]})),setTimeout((()=>{this.isVoipWindowReady=!0,this.emitToVoip(o.event,o.args)}),200),this.onVoipCallStatusCallback&&this.onVoipCallStatusCallback(this.conversation,!0)}onVoipWindowClose=t=>{t&&t.srcElement&&"about:blank"===t.srcElement.URL||this.callWin&&setTimeout((()=>{this.onVoipCallStatusCallback&&this.onVoipCallStatusCallback(this.conversation,!1),this.conversation=null,this.conference&&(r.quitChatroom(this.callId),this.conference=!1),this.callId=null,this.participants=[],this.queueEvents=[],this.callWin=null,this.isVoipWindowReady=!1,this.voipEventRemoveAllListeners("voip-message","conference-request","update-call-start-message","start-screen-share")}),2e3)};onVoipWindowReady(){this.callId&&(this.isVoipWindowReady=!0,this.onVoipCallStatusCallback&&this.onVoipCallStatusCallback(this.conversation,!0),e()&&(s.on("voip-message",this.sendVoipListener),s.on("conference-request",this.sendConferenceRequestListener),s.on("update-call-start-message",this.updateCallStartMessageContentListener),s.on("start-screen-share",((t,e)=>{if(this.callWin){let t=e.width;this.callWin.resizable=!0,this.callWin.closable=!0,this.callWin.maximizable=!!e.rc,this.callWin.transparent=!0;let s=(e.rc,800),i=e.rc?200:800;this.callWin.setMinimumSize(s,i),this.callWin.setSize(s,i),e.rc?this.callWin.minimize():this.callWin.setPosition((t-800)/2,0,!0)}})),s.on("stop-screen-share",((t,e)=>{if(this.callWin){let t=360,s=640;switch(e.type){case"single":t=360,s=640;break;case"multi":case"conference":t=1024,s=800}this.callWin.resizable=!0,this.callWin.closable=!0,this.callWin.maximizable=!0,this.callWin.setMinimumSize(t,s),this.callWin.setSize(t,s),this.callWin.center()}}))),this.queueEvents.length>0&&this.queueEvents.forEach((t=>{this.emitToVoip(t.event,t.args)})))}voipEventRemoveAllListeners(...t){e()&&t.forEach((t=>s.removeAllListeners(t)))}forceCloseVoipWindow(){e()&&this.callWin&&this.callWin.close(),this.callId=null,this.callWin=null}forceCloseVoipWindowAndHangup(){if(this.callWin&&!this.conference){let t=new p;t.callId=this.callId,t.inviteMsgUid=this.inviteMessageUid,t.reason=d.REASON_Hangup,r.sendConversationMessage(this.conversation,t),this.onVoipWindowClose()}}}const C=new v;export{v as AvEngineKitProxy,C as default};