call-engine-wx 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.mjs +2 -2
- package/dist/index.mp.js +1 -1
- package/dist/index.umd.js +5 -3546
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +1 -3
package/dist/index.cjs.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
|
|
6
6
|
var TRTCCloud = require('@tencentcloud/trtc-component-wx');
|
|
7
|
-
var TencentCloudChat = require('@tencentcloud/lite-chat');
|
|
7
|
+
var TencentCloudChat = require('@tencentcloud/lite-chat/basic');
|
|
8
8
|
|
|
9
9
|
class EventEmitter {
|
|
10
10
|
constructor() {
|
package/dist/index.esm.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*! call-engine-wx | (c) 2024 Tencent | MIT */
|
|
2
2
|
import TRTCCloud from '@tencentcloud/trtc-component-wx';
|
|
3
|
-
import TencentCloudChat from '@tencentcloud/lite-chat';
|
|
4
|
-
export { default as TencentCloudChat } from '@tencentcloud/lite-chat';
|
|
3
|
+
import TencentCloudChat from '@tencentcloud/lite-chat/basic';
|
|
4
|
+
export { default as TencentCloudChat } from '@tencentcloud/lite-chat/basic';
|
|
5
5
|
|
|
6
6
|
class EventEmitter {
|
|
7
7
|
constructor() {
|
package/dist/index.mp.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! call-engine-wx | (c) 2024 Tencent | MIT */
|
|
2
|
-
"use strict";function t(t,e){const s={Title:(t=t||{}).title||"",Sound:t.iosSound||"",BadgeMode:t.ignoreIosBadge?1:0,IsVoipPush:t.iosPushType||0,ContentAvailable:t.enableIosBackgroundNotification?1:0,InterruptionLevel:t.iosInterruptionLevel||"",Image:t.iosImage||""},i={Sound:t.androidSound||"",Title:t.title||"",OPPOChannelID:t.androidOppoChannelId||"",OPPOCategory:t.oppoCategory||"IM",OPPONotifyLevel:t.oppoNotifyLevel||0,GoogleChannelID:t.androidFcmChannelId||"",GoogleImage:t.fcmImage||"",XiaoMiChannelID:t.androidXiaomiChannelId||"",VIVOClassification:t.androidVivoClassification||0,VIVOCategory:t.vivoCategory||"IM",HuaWeiCategory:t.androidHuaweiCategory||"IM",HuaWeiImage:t.huaweiImage||"",HonorImportance:t.honorImportance||"NORMAL",HonorImage:t.honorImage||""},r={entity:{action:2},timPushFeatures:{fcmPushType:0,fcmNotificationType:1},voip_ext:{voip_type:"call",voip_media_type:e===exports.CallMediaType.VIDEO?"video":"audio"}};return{Title:t.title||"",Description:t.description||"",Ext:JSON.stringify(r),AndroidInfo:i,ApnsInfo:s}}function e(){B&&(clearInterval(B),B=null),k="",j=0}async function s(t,e){const s=t||{},i=s.chat;if(!i||!R(i.callExperimentalAPI))return{errCode:H,errMsg:"chat.callExperimentalAPI is not available"};if(!s.callId)return{errCode:H,errMsg:"callId is required"};if(!s.terminalId)return{errCode:H,errMsg:"terminalId is required"};let r=b;e===_.Hangup&&(r=P);const o=((t,e,s,i)=>({Body:{CallId:t||"",TerminalId:e||"",ActionType:s>>>0},Head:T(i)}))(s.callId,s.terminalId,e,r);try{const t={serviceCommand:r,data:JSON.stringify(o)},e=await i.callExperimentalAPI(S,t),s=e&&e.data||e||{},n=L(s.ErrorCode)?s.ErrorCode:L(s.errorCode)?s.errorCode:0;return 0!==n?{errCode:n,errMsg:s.ErrorInfo||s.errorInfo||""}:{errCode:0,errMsg:""}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed"}}}function i(t){switch(t){case exports.LayoutTemplate.FLOAT:return"float";case exports.LayoutTemplate.GRID:return"grid";default:return"float"}}function r(){return{template:exports.LayoutTemplate.FLOAT,canvas:{w:Y,h:q},primaryUser:{userId:"",isLocal:0},secondaryUser:null,showRemoteInfo:0}}function o(t){const e=C&&C.EVENT||{};switch(t){case"SDK_READY":return e.SDK_READY||"sdkStateReady";case"KICKED_OUT":return e.KICKED_OUT||"kickedOut";case"USER_SIG_EXPIRED":return e.USER_SIG_EXPIRED||"userSigExpired";case"ROOM_CUSTOM_DATA_RECEIVED":return e.ROOM_CUSTOM_DATA_RECEIVED;default:return null}}function n(t,e){const s=Error(e);return s.code=t,s}Object.defineProperty(exports,"__esModule",{value:1});var a,c,h,l,u,d,f,p,I,y,m=require("@tencentcloud/trtc-component-wx"),C=require("@tencentcloud/lite-chat");class w{constructor(){this.t=new Map}on(t,e){if("function"!=typeof e)return this;let s=this.t.get(t);return s||(s=[],this.t.set(t,s)),s.push({fn:e,once:0}),this}once(t,e){if("function"!=typeof e)return this;let s=this.t.get(t);return s||(s=[],this.t.set(t,s)),s.push({fn:e,once:1}),this}off(t,e){if(!t)return this.t.clear(),this;const s=this.t.get(t);if(!s)return this;if(!e)return this.t.delete(t),this;const i=s.filter(t=>t.fn!==e);return 0===i.length?this.t.delete(t):this.t.set(t,i),this}emit(t,...e){const s=this.t.get(t);if(!s||0===s.length)return 0;const i=s.slice();for(const s of i){try{s.fn.apply(null,e)}catch(t){}s.once&&this.off(t,s.fn)}return 1}removeAllListeners(t){return t?this.t.delete(t):this.t.clear(),this}listenerCount(t){const e=this.t.get(t);return e?e.length:0}}exports.CallStatus=void 0,(a=exports.CallStatus||(exports.CallStatus={}))[a.NONE=0]="NONE",a[a.WAITING=1]="WAITING",a[a.CALLING=2]="CALLING",exports.CallRole=void 0,(c=exports.CallRole||(exports.CallRole={}))[c.NONE=0]="NONE",c[c.CALLER=1]="CALLER",c[c.CALLEE=2]="CALLEE",exports.CallMediaType=void 0,(h=exports.CallMediaType||(exports.CallMediaType={}))[h.UNKNOWN=0]="UNKNOWN",h[h.AUDIO=1]="AUDIO",h[h.VIDEO=2]="VIDEO",exports.CallScene=void 0,(l=exports.CallScene||(exports.CallScene={}))[l.NONE=0]="NONE",l[l.GROUP=1]="GROUP",l[l.MULTI=2]="MULTI",l[l.SINGLE=3]="SINGLE",exports.IOSOfflinePushType=void 0,(u=exports.IOSOfflinePushType||(exports.IOSOfflinePushType={}))[u.APNs=0]="APNs",u[u.VoIP=1]="VoIP",exports.CallInvitationResultCodeType=void 0,(d=exports.CallInvitationResultCodeType||(exports.CallInvitationResultCodeType={}))[d.SUCCESS=0]="SUCCESS",d[d.INVITED=1]="INVITED",d[d.LINE_BUSY=2]="LINE_BUSY",exports.CallEndReason=void 0,(f=exports.CallEndReason||(exports.CallEndReason={}))[f.UNKNOWN=0]="UNKNOWN",f[f.HANGUP=1]="HANGUP",f[f.REJECT=2]="REJECT",f[f.NO_RESPONSE=3]="NO_RESPONSE",f[f.OFFLINE=4]="OFFLINE",f[f.LINE_BUSY=5]="LINE_BUSY",f[f.CANCELED=6]="CANCELED",f[f.OTHER_DEVICE_ACCEPTED=7]="OTHER_DEVICE_ACCEPTED",f[f.OTHER_DEVICE_REJECT=8]="OTHER_DEVICE_REJECT",f[f.END_BY_SERVER=9]="END_BY_SERVER",exports.LOG_LEVEL=void 0,(p=exports.LOG_LEVEL||(exports.LOG_LEVEL={}))[p.DEBUG=0]="DEBUG",p[p.INFO=1]="INFO",p[p.WARN=2]="WARN",p[p.ERROR=3]="ERROR",p[p.NONE=4]="NONE",exports.CameraPosition=void 0,(I=exports.CameraPosition||(exports.CameraPosition={}))[I.FRONT=1]="FRONT",I[I.BACK=0]="BACK",exports.AudioPlayBackDevice=void 0,(y=exports.AudioPlayBackDevice||(exports.AudioPlayBackDevice={}))[y.SPEAKER=0]="SPEAKER",y[y.EAR=1]="EAR";const _=Object.freeze({None:0,AcceptedByInvitee:1,RejectedByInvitee:2,NoResponseUntilTimeout:3,Hangup:4,Join:5,Offline:6,InviteUser:7}),x=Object.freeze({ERR_FAILED:-1}),E=Object.freeze({ERROR:"onError",SDK_READY:"sdkReady",KICKED_OUT:"onKickedOffline",onUserSigExpired:"onUserSigExpired",ON_CALL_BEGIN:"onCallBegin",ON_CALL_END:"onCallEnd",ON_CALL_RECEIVED:"onCallReceived",ON_CALL_CANCELED:"onCallCanceled",ON_CALL_NOT_CONNECTED:"onCallNotConnected",INVITED:"onInvited",USER_ENTER:"onUserJoin",USER_LEAVE:"onUserLeave",VIEW_LAYOUT_CHANGED:"viewLayoutChanged"}),g="function",L=t=>"number"==typeof t,v=t=>typeof Array.isArray===g?Array.isArray(t):"array"===A(t),R=t=>typeof t===g,A=t=>({}.toString.call(t).match(/^\[object (.*)\]$/)[1].toLowerCase());let N=null;const T=t=>{const e=(()=>{if(N)return N;const t={osName:"Unknown",osVersion:"0.0.0",deviceName:"Unknown"};try{const e="undefined"!=typeof wx?wx:null,s="undefined"!=typeof uni?uni:null;if(e&&"function"==typeof e.getSystemInfoSync){const s=e.getSystemInfoSync()||{},[i="Unknown",r="0.0.0"]=((s.system||"")+"").split(" ");t.osName=i,t.osVersion=r,t.deviceName=s.model||s.brand||"Unknown"}else if(s&&"function"==typeof s.getSystemInfoSync){const e=s.getSystemInfoSync()||{},[i="Unknown",r="0.0.0"]=((e.system||"")+"").split(" ");t.osName=i,t.osVersion=r,t.deviceName=e.model||e.brand||"Unknown"}else if("undefined"!=typeof navigator&&navigator.userAgent){const e=navigator.userAgent;if(/Mac OS X/i.test(e)){t.osName="MacOS";const s=e.match(/Mac OS X ([\d_\.]+)/);t.osVersion=s?s[1].replace(/_/g,"."):"0.0.0",t.deviceName="Mac"}else if(/Windows/i.test(e)){t.osName="Windows";const s=e.match(/Windows NT ([\d\.]+)/);t.osVersion=s?s[1]:"0.0.0",t.deviceName="PC"}else if(/Android/i.test(e)){t.osName="Android";const s=e.match(/Android ([\d\.]+)/);t.osVersion=s?s[1]:"0.0.0",t.deviceName="Android"}else if(/iPhone|iPad|iPod/i.test(e)){t.osName="iOS";const s=e.match(/OS ([\d_]+)/);t.osVersion=s?s[1].replace(/_/g,"."):"0.0.0",t.deviceName=/iPad/i.test(e)?"iPad":"iPhone"}}}catch(t){}return N=t,t})();return{Command:t,DeviceName:e.deviceName,LongPollingKey:"",OSName:e.osName,OSVersion:e.osVersion,RoomId:"",SdkVersion:"1.0.0.0",Version:2}},O=(t={})=>{let e=exports.CallScene.NONE;const s=t.userIDList||[];return s.length>1&&(e=exports.CallScene.MULTI),1===s.length&&(e=exports.CallScene.SINGLE),{callId:t.callId||"",roomId:{strRoomId:t.strRoomID||t.strRoomId||"",intRoomId:t.roomID||t.roomId||0},inviter:t.inviter||"",inviteeList:s,groupId:t.chatGroupID||"",mediaType:t.type,status:t.status||exports.CallStatus.NONE,role:t.role||exports.CallRole.NONE,scene:e,callStartTime:0}},D=Object.freeze({IntRoomId:1,StrRoomId:2}),S="sendTRTCCustomData",M="call_engine_srv.start_call",U="call_engine_srv.heart_beat",b="call_engine_srv.handle_call",P="call_engine_srv.hangup",V="call_engine_srv.cancel_invite",G="call_engine_srv.get_invitation",H=-1,K={errCode:0,errMsg:"",inviteId:"",callResultList:[]};let B=null,k="",j=0;const F={CMD_START_CALL:M,CMD_HEART_BEAT:U,CMD_HANDLE_CALL:b,CMD_HANGUP_CALL:P,CMD_CANCEL_CALL:V,CMD_GET_INVITATION:G,HEARTBEAT_INTERVAL_MS:2e3,HEARTBEAT_FAIL_RETRY_DEFAULT:5,HEARTBEAT_FAIL_INTERVAL_MS:2e3,async startCall(e){const s=e||{},i=s.chat;if(!i||!R(i.callExperimentalAPI))return{...K,errCode:H,errMsg:"chat.callExperimentalAPI is not available"};if(!s.inviter)return{...K,errCode:H,errMsg:"inviter is required"};if(!v(s.userIdList)||0===s.userIdList.length)return{...K,errCode:H,errMsg:"userIdList cannot be empty"};if(s.mediaType!==exports.CallMediaType.AUDIO&&s.mediaType!==exports.CallMediaType.VIDEO)return{...K,errCode:H,errMsg:"invalid mediaType"};const r={roomId:s.roomId||{},timeout:s.timeout,userData:s.userData,isEphemeralCall:!!s.isEphemeralCall,offlinePushInfo:s.offlinePushInfo},o=((e,s,i,r,o)=>{const n=r||{},a=n.roomId||{},c={CallId:"",CallMediaType:i>>>0};"number"==typeof a.intRoomId&&a.intRoomId>0?(c.RoomId=a.intRoomId+"",c.RoomIdType=D.IntRoomId):a.intRoomId&&0!==a.intRoomId||"string"!=typeof a.strRoomId||0>=a.strRoomId.length||(c.RoomId=a.strRoomId,c.RoomIdType=D.StrRoomId),c.ExcludeFromHistoryMessage=0,c.GroupId=o||"",c.IsEphemeralCall=!!n.isEphemeralCall;const h="number"==typeof n.timeout?n.timeout:30;return{Body:{Caller_Account:e,CalleeList_Account:(s||[]).slice(),CallInfo:c,Timeout:1e3*h,UserData:n.userData||"",OfflinePushInfo:t(n.offlinePushInfo,i)},Head:T("call_engine_srv.start_call")}})(s.inviter,s.userIdList,s.mediaType,r,s.groupId||"");let n;try{const t={serviceCommand:M,data:JSON.stringify(o)};n=await i.callExperimentalAPI(S,t)}catch(t){const e=t&&(t.code||t.errorCode)||H,s=t&&(t.message||t.errorInfo)||"send failed";return{...K,errCode:e,errMsg:s}}const a=n&&n.data||n||{};return 0!==a.ErrorCode?{...K,errCode:a.ErrorCode,errMsg:a.ErrorInfo}:{errCode:a?.ErrorCode||0,errMsg:a?.errMsg||"",inviteId:a?.Response?.CallId||"",callResultList:a?.Response?.CallResult||[]}},startHeartBeat(t){const s=t||{},{chat:i,callId:r,userId:o}=s;if(!i||!R(i.callExperimentalAPI))return;if(!r||!o)return;e();const n=L(s.intervalMs)&&s.intervalMs>0?s.intervalMs:2e3,a=L(s.retryCount)&&s.retryCount>0?s.retryCount:5;k=r,B=setInterval(async()=>{if(!k||k!==r)return;const t=await(async(t,e,s)=>{const i={Body:{User_Account:s,CallId:e},Head:T(U)};try{const e={serviceCommand:U,data:JSON.stringify(i)},s=await t.callExperimentalAPI(S,e),r=s&&s.data||s||{},o=L(r.ErrorCode)?r.ErrorCode:L(r.errorCode)?r.errorCode:0;return 0!==o?{errCode:o,errMsg:r.ErrorInfo||r.errorInfo||""}:{errCode:0,errMsg:""}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed"}}})(i,r,o);if(k&&k===r)if(0!==t.errCode){if(j+=1,j>=a){const t=k;if(e(),"function"==typeof s.onTimeout)try{s.onTimeout(t)}catch(t){}}}else j=0},n)},stopHeartBeat:e,async accept(t){return s(t,_.AcceptedByInvitee)},async reject(t){return s(t,_.RejectedByInvitee)},async hangup(t){return s(t,_.Hangup)},async cancel(t){const e=t||{},s=e.chat;if(!s||!R(s.callExperimentalAPI))return{errCode:H,errMsg:"chat.callExperimentalAPI is not available"};if(!e.callId)return{errCode:H,errMsg:"callId is required"};if(!e.inviter)return{errCode:H,errMsg:"inviter is required"};if(!v(e.calleeList)||0===e.calleeList.length)return{errCode:H,errMsg:"calleeList cannot be empty"};const i=(r=e.inviter,o=e.callId,n=e.calleeList,a=V,{Body:{CallId:o||"",Operator_Account:r||"",CalleeList_Account:Array.isArray(n)?n.filter(Boolean):[]},Head:T(a)});var r,o,n,a;try{const t={serviceCommand:V,data:JSON.stringify(i)},e=await s.callExperimentalAPI(S,t),r=e&&e.data||e||{},o=L(r.ErrorCode)?r.ErrorCode:L(r.ErrorCode)?r.errorCode:0;return 0!==o?{errCode:o,errMsg:r.ErrorInfo||r.errorInfo||""}:{errCode:0,errMsg:""}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed"}}},async getInvitation(t){const e=t||{},s=e.chat;if(!s||!R(s.callExperimentalAPI))return{errCode:H,errMsg:"chat.callExperimentalAPI is not available",data:null};if(!e.userId)return{errCode:H,errMsg:"userId is required",data:null};const i={Body:{User_Account:e.userId||""},Head:T(G)};var r;try{const t={serviceCommand:G,data:JSON.stringify(i)},e=await s.callExperimentalAPI(S,t),o=e&&e.data||e||{},n=L(o.ErrorCode)?o.ErrorCode:L(o.ErrorCode)?o.errorCode:0;if(0!==n)return{errCode:n,errMsg:o.ErrorInfo||o.errorInfo||"",data:null};const a=o?.Response;return a&&null!==(r=a)&&"object"==typeof r&&0!==Object.keys(a).length?{errCode:0,errMsg:"",data:a}:{errCode:0,errMsg:"",data:null}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed",data:null}}}};var W,J;exports.LayoutTemplate=void 0,(W=exports.LayoutTemplate||(exports.LayoutTemplate={}))[W.FLOAT=0]="FLOAT",W[W.GRID=1]="GRID",(t=>{t[t.NONE=0]="NONE",t[t.WAITING=1]="WAITING",t[t.CALLING=2]="CALLING"})(J||(J={}));const Y=720,q=1280,$={x:480,y:80,w:200,h:300};class X{constructor(){this.currentTemplate=exports.LayoutTemplate.FLOAT,this.largeViewUserId="",this.isViewReversed=0,this.currentLayout=r(),this.previousLayout=r()}setLayoutTemplate(t){this.currentTemplate=t,this.largeViewUserId="",this.isViewReversed=0}setLargeViewUser(t){this.largeViewUserId===t?this.largeViewUserId="":this.largeViewUserId=t}toggleViewReversed(){this.isViewReversed=!this.isViewReversed}getCurrentTemplate(){return this.currentTemplate}getCurrentLayout(){return this.currentLayout}recalculate(t){switch(this.previousLayout=this.currentLayout,this.currentTemplate){case exports.LayoutTemplate.FLOAT:this.currentLayout=this.calculateFloatLayout(t);break;case exports.LayoutTemplate.GRID:this.currentLayout=this.calculateGridLayout(t)}return this.isLayoutChanged()}toJson(){return JSON.stringify(this.toJsonObject())}toJsonObject(){const t=this.currentLayout,e={template:i(t.template)};return t.template===exports.LayoutTemplate.FLOAT?(e.canvas={w:t.canvas.w,h:t.canvas.h},e.primaryUser={userId:t.primaryUser.userId,isLocal:t.primaryUser.isLocal},t.secondaryUser?e.secondaryUser={userId:t.secondaryUser.userId,isLocal:t.secondaryUser.isLocal,frame:{...t.secondaryUser.frame}}:e.secondaryUser=null,e.showRemoteInfo=t.showRemoteInfo):(e.canvas={w:t.canvas.w,h:t.canvas.h},e.participants=t.participants.map(t=>({userId:t.userId,frame:{...t.frame},isLocal:t.isLocal,showName:t.showName}))),e}isLayoutChanged(){const t=this.previousLayout,e=this.currentLayout;if(t.template!==e.template)return 1;if(e.template===exports.LayoutTemplate.FLOAT&&t.template===exports.LayoutTemplate.FLOAT)return t.primaryUser.userId!==e.primaryUser.userId||(t.secondaryUser?t.secondaryUser.userId:"")!==(e.secondaryUser?e.secondaryUser.userId:"")||t.showRemoteInfo!==e.showRemoteInfo?1:0;if(e.template===exports.LayoutTemplate.GRID&&t.template===exports.LayoutTemplate.GRID){if(t.participants.length!==e.participants.length)return 1;for(let s=0;s<e.participants.length;s++){const i=t.participants[s],r=e.participants[s];if(i.userId!==r.userId)return 1;if(i.frame.x!==r.frame.x)return 1;if(i.frame.y!==r.frame.y)return 1;if(i.frame.w!==r.frame.w)return 1;if(i.frame.h!==r.frame.h)return 1;if(i.showName!==r.showName)return 1}return 0}return 1}calculateFloatLayout(t){const e=t.selfId;let s="";for(const i of t.allParticipants)if(i.id!==e){s=i.id;break}const i=t.selfStatus===J.CALLING,r=t.mediaType===exports.CallMediaType.AUDIO;let o,n,a,c;return i||r?!i&&r||r?(o=s,n=0,a=null,c=1):(this.isViewReversed?(o=e,n=1,a={userId:s,isLocal:0,frame:{...$}}):(o=s,n=0,a={userId:e,isLocal:1,frame:{...$}}),c=0):(o=e,n=1,a=null,c=1),{template:exports.LayoutTemplate.FLOAT,canvas:{w:Y,h:q},primaryUser:{userId:o,isLocal:n},secondaryUser:a,showRemoteInfo:c}}calculateGridLayout(t){const e=new Map;for(const s of t.allParticipants)e.set(s.id,s.status);const s=[];if(t.inviter){const i=e.get(t.inviter);void 0!==i&&i!==J.NONE&&s.push(t.inviter)}for(const i of t.inviteeList){const t=e.get(i);void 0!==t&&t!==J.NONE&&s.push(i)}const i=this.calculateGridPositions(s.length,s,t.selfId);return{template:exports.LayoutTemplate.GRID,canvas:{w:Y,h:q},participants:i}}calculateGridPositions(t,e,s){const i=[];if(0>=t)return i;const r=240,o=360;let n=-1;if(this.largeViewUserId)for(let s=0;t>s;s++)if(e[s]===this.largeViewUserId){n=s;break}if(n>=0&&4>=t){const o=[n];for(let e=0;t>e;e++)e!==n&&o.push(e);const a=r,c=t-1,h=[{x:0,y:0,w:Y,h:Y}],l=Y;for(let t=0;c>t;t++)h.push({x:t*a,y:l,w:a,h:a});for(let t=0;t<o.length&&t<h.length;t++){const r=e[o[t]];i.push({userId:r,frame:h[t],isLocal:r===s,showName:0===t})}return i}if(n>=0&&t>4){const o=480,a=480,c=[n];for(let e=0;t>e;e++)e!==n&&c.push(e);{const t=e[c[0]];i.push({userId:t,frame:{x:0,y:0,w:o,h:a},isLocal:t===s,showName:1})}let h=1;if(h<c.length){const t=e[c[h]];i.push({userId:t,frame:{x:o,y:0,w:r,h:r},isLocal:t===s,showName:0}),h++}if(h<c.length){const t=e[c[h]];i.push({userId:t,frame:{x:o,y:r,w:r,h:r},isLocal:t===s,showName:0}),h++}let l=a;for(;h<c.length;){for(let t=0;3>t&&h<c.length;t++){const o=e[c[h]];i.push({userId:o,frame:{x:t*r,y:l,w:r,h:r},isLocal:o===s,showName:0}),h++}l+=r}return i}if(1===t)i.push({userId:e[0],frame:{x:0,y:0,w:Y,h:q},isLocal:e[0]===s,showName:0});else if(2===t)i.push({userId:e[0],frame:{x:0,y:0,w:o,h:o},isLocal:e[0]===s,showName:0}),i.push({userId:e[1],frame:{x:o,y:0,w:o,h:o},isLocal:e[1]===s,showName:0});else if(3===t)i.push({userId:e[0],frame:{x:0,y:0,w:o,h:o},isLocal:e[0]===s,showName:0}),i.push({userId:e[1],frame:{x:o,y:0,w:o,h:o},isLocal:e[1]===s,showName:0}),i.push({userId:e[2],frame:{x:180,y:o,w:o,h:o},isLocal:e[2]===s,showName:0});else if(4===t)i.push({userId:e[0],frame:{x:0,y:0,w:o,h:o},isLocal:e[0]===s,showName:0}),i.push({userId:e[1],frame:{x:o,y:0,w:o,h:o},isLocal:e[1]===s,showName:0}),i.push({userId:e[2],frame:{x:0,y:o,w:o,h:o},isLocal:e[2]===s,showName:0}),i.push({userId:e[3],frame:{x:o,y:o,w:o,h:o},isLocal:e[3]===s,showName:0});else{let o=0;for(let n=0;t>n;n+=3){const a=Math.min(3,t-n);for(let t=0;a>t;t++){const a=n+t;i.push({userId:e[a],frame:{x:t*r,y:o,w:r,h:r},isLocal:e[a]===s,showName:0})}o+=r}}return i}}const Q=new w,z=Object.freeze(["onEnterRoom","onExitRoom","onSwitchRole","onError","onWarnning","onRemoteUserEnterRoom","onRemoteUserLeaveRoom","onUserVideoAvailable","onUserSubStreamAvailable","onUserAudioAvailable","onUserVoiceVolume","onNetworkQuality","onFirstVideoFrame","onSendFirstLocalVideoFrame","onSendFirstLocalAudioFrame","onMicDidReady"]);class Z{constructor(t){const e=t||{};this.i=new w,this.o=e.SDKAppID||e.sdkAppID||0,this.l="",this.u="",this.p=0,this.I=0,this.m=null,this.C=0,this._=null,this.L=O(),this.v=0,this.R="",this.A=new X;const s=e.chat||e.tim;s&&(this.p=1,this.N=s)}static createInstance(t){return Z.instance||(Z.instance=new Z(t)),Z.instance}static once(t,e){"ready"===t?"function"==typeof e&&Promise.resolve().then(()=>{try{e()}catch(t){}}):Q.once(t,e)}async login(t){const e=t||{},s=e.userID,i=e.userSig,r=e.sdkAppID||e.SDKAppID||this.o;if(!s||!i)throw n(x.ERR_FAILED,"login: userID and userSig are required");!this.o&&r&&(this.o=r),this.l=s,this.u=i,this.T();try{if(this.N=C.create({SDKAppID:this.o,devMode:1}),this.O())return void this.D();let t;try{t=await this.N.login({userID:s,userSig:i});try{this.getTRTCCloudInstance()}catch(t){}}catch(t){const e=t&&t.code,s=t&&t.message||"";if(2024===e||2025===e)return void this.D();if(this.S&&(/timeout/i.test(s)||"NETWORK_TIMEOUT"===e))return void this.D();throw t}if(t&&t.data&&t.data.repeatLogin)return void this.D();this.D()}catch(t){const e=t&&t.code||x.ERR_FAILED,s=t&&t.message||"login failed";throw this.i.emit(E.ERROR,{code:e,message:s}),n(e,s)}}async logout(){this.M();try{this.N&&"function"==typeof this.N.logout&&await this.N.logout()}finally{this.l="",this.u=""}}async destroyInstance(){Z.instance=null;try{await this.logout()}catch(t){}this.U(),this.P(),this.i.removeAllListeners(),this.m=null}on(t,e){this.i.on(t,e)}off(t,e){this.i.off(t,e)}once(t,e){this.i.once(t,e)}getTim(){return this.N}getTRTCCloudInstance(){if(this.m)return this.m;if(!m||"function"!=typeof m.getTRTCShareInstance)throw n(x.ERR_FAILED,"[call-engine-wx] @tencentcloud/trtc-component-wx is not installed or does not expose getTRTCShareInstance().");return this.m=m.getTRTCShareInstance(),this.V(),this.m}setLogLevel(t){try{this.N&&"function"==typeof this.N.setLogLevel&&this.N.setLogLevel(t)}catch(t){}}async calls(t){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"calls: not logged in. Call login() first and wait for SDK_READY.");if(this.v)throw n(x.ERR_FAILED,"calls: a previous calls() is still in progress");if(this.L.status!==exports.CallStatus.NONE)throw n(x.ERR_FAILED,"calls: there is an active call already; hang up before starting a new one");if(!v(t?.userIDList)||0===t?.userIDList?.length)throw n(x.ERR_FAILED,"calls: userIDList cannot be empty");if(t?.type!==exports.CallMediaType.AUDIO&&t?.type!==exports.CallMediaType.VIDEO)throw n(x.ERR_FAILED,"calls: invalid mediaType");const e=this.G();if(!e)throw n(x.ERR_FAILED,"calls: cannot resolve login user id");var s;this.L=O({...t,strRoomId:(s=t?.strRoomID,s||"roomId_"+Math.floor(1e6*Math.random())),status:exports.CallStatus.WAITING,role:exports.CallRole.CALLER,inviter:e}),this.v=1;let i=0;try{try{await this.H(),i=1}catch(t){this.K();const e=t&&t.code||x.ERR_FAILED,s=t&&t.message||"enter TRTC room failed";throw this.i.emit(E.ERROR,{code:e,message:s}),n(e,s)}const t=await F.startCall({chat:this.N,...this.L,userIdList:this.L.inviteeList});if(0!==t.errCode)throw this.K(),i=0,this.i.emit(E.ERROR,{code:t.errCode,message:t.errMsg}),n(t.errCode,t.errMsg||"start_call failed");this.L.callId=t.inviteId,this.L.callStartTime=Date.now();let e=0;for(const s of t.callResultList)switch(s.resultCode){case exports.CallInvitationResultCodeType.INVITED:this.i.emit(E.USER_ENTER,s.userId);break;case exports.CallInvitationResultCodeType.SUCCESS:this.i.emit("onUserInviting",s.userId);break;case exports.CallInvitationResultCodeType.LINE_BUSY:this.i.emit("onUserLineBusy",s.userId),e++}if(e>=this.L.inviteeList.length){const e=t.callResultList[t.callResultList.length-1];return this.K(),i=0,this.i.emit(E.ON_CALL_NOT_CONNECTED,{callInfo:{...this.L},userId:e?e.userId:"",reason:exports.CallEndReason.LINE_BUSY}),this.M(),{inviteId:t.inviteId,roomId:this.L,callResultList:t.callResultList}}return F.startHeartBeat({chat:this.N,callId:this.L.callId,userId:this.L.inviter,onTimeout:t=>{this.L.callId===t&&(this.i.emit(E.ON_CALL_NOT_CONNECTED,{callInfo:{...this.L},userId:this.L.inviter,reason:exports.CallEndReason.LINE_BUSY}),this.K(),this.M())}}),{inviteId:t.inviteId,roomId:this.L,callResultList:t.callResultList}}catch(t){throw i&&this.K(),this.M(),t}finally{this.v=0}}async accept(){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"accept: not logged in. Call login() first and wait for SDK_READY.");if(this.L.role!==exports.CallRole.CALLEE)throw n(x.ERR_FAILED,"accept: current role is not CALLEE");if(this.L.status!==exports.CallStatus.WAITING)throw n(x.ERR_FAILED,`accept: invalid call status ${this.L.status}; expected WAITING`);const t=this.L.callId;if(!t)throw n(x.ERR_FAILED,"accept: no active invitation (callId is empty)");let e=0;try{await this.H(),e=1;const s=this.B(),i=await F.accept({chat:this.N,callId:t,terminalId:s});if(0!==i.errCode)throw n(i.errCode,i.errMsg||"handle_call(accept) failed");return this.L.status=exports.CallStatus.CALLING,this.L.callStartTime=Date.now(),this.i.emit(E.ON_CALL_BEGIN,{callInfo:{...this.L}}),F.startHeartBeat({chat:this.N,callId:this.L.callId,userId:this.G(),onTimeout:t=>{this.L.callId===t&&(this.i.emit(E.ON_CALL_NOT_CONNECTED,{callInfo:{...this.L},userId:this.G(),reason:exports.CallEndReason.LINE_BUSY}),this.K(),this.M())}}),{callId:t,roomId:this.L.roomId}}catch(t){e&&this.K(),this.M();const s=t&&t.code||x.ERR_FAILED,i=t&&t.message||"accept failed";throw this.i.emit(E.ERROR,{code:s,message:i}),n(s,i)}}async reject(){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"reject: not logged in. Call login() first and wait for SDK_READY.");if(this.L.role!==exports.CallRole.CALLEE)throw n(x.ERR_FAILED,"reject: current role is not CALLEE");if(this.L.status!==exports.CallStatus.WAITING)throw n(x.ERR_FAILED,`reject: invalid call status ${this.L.status}; expected WAITING`);const t=this.L.callId;if(!t)throw n(x.ERR_FAILED,"reject: no active invitation (callId is empty)");const e={...this.L};try{const s=this.B();return(await F.reject({chat:this.N,callId:t,terminalId:s})).errCode,this.i.emit(E.ON_CALL_END,{callInfo:e,reason:exports.CallEndReason.REJECT,userId:this.G()}),{callId:t}}finally{this.M()}}async hangup(){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"hangup: not logged in. Call login() first and wait for SDK_READY.");if(this.L.status!==exports.CallStatus.CALLING&&this.L.status!==exports.CallStatus.WAITING)throw n(x.ERR_FAILED,`hangup: invalid call status ${this.L.status}; expected CALLING or WAITING`);const t=this.L.callId;if(!t)throw n(x.ERR_FAILED,"hangup: no active call (callId is empty)");const e=this.L.role===exports.CallRole.CALLER&&this.L.status===exports.CallStatus.WAITING,s={...this.L};try{if(e){const e=this.L.inviter||this.G(),s=Array.isArray(this.L.inviteeList)?this.L.inviteeList.slice():[];s.length>0&&(await F.cancel({chat:this.N,inviter:e,callId:t,calleeList:s})).errCode}else{const e=this.B();(await F.hangup({chat:this.N,callId:t,terminalId:e})).errCode}return this.K(),this.i.emit(E.ON_CALL_END,{callInfo:s,reason:e?exports.CallEndReason.CANCELED:exports.CallEndReason.HANGUP,userId:this.G()}),{callId:t}}finally{this.M()}}setLayoutTemplate(t){this.A.setLayoutTemplate(t),this.k()}setLargeViewUser(t){this.A.setLargeViewUser(t),this.k()}toggleViewReversed(){this.A.toggleViewReversed(),this.k()}j(){const t=this.G()||"",e=this.L||{},s=e.inviter||"",i=Array.isArray(e.inviteeList)?e.inviteeList:[],r="number"==typeof e.status?e.status:J.NONE,o=new Set,n=[],a=(t,e)=>{t&&!o.has(t)&&(o.add(t),n.push({id:t,status:e}))};a(t,r),s&&a(s,r);for(const t of i)a(t,r);return{selfId:t,selfStatus:r,mediaType:"number"==typeof e.mediaType?e.mediaType:exports.CallMediaType.VIDEO,allParticipants:n,inviter:s,inviteeList:i}}k(){try{this.A.recalculate(this.j())&&this.i.emit(E.VIEW_LAYOUT_CHANGED,this.A.toJsonObject())}catch(t){}}async openCamera(t,e){let s;if(s=void 0===e?-1:e?1:0,R(this?.m?.startLocalPreview))return this.m.startLocalPreview(t,s)}async closeCamera(){if(R(this?.m?.stopLocalPreview))return this.m.stopLocalPreview()}async switchCamera(t){R(this?.m?.switchCamera)&&await this.m.switchCamera(!!t)}async openMicrophone(){R(this?.m?.startLocalAudio)&&await this.m.startLocalAudio()}async closeMicrophone(){R(this?.m?.stopLocalAudio)&&await this.m.stopLocalAudio()}async selectAudioPlaybackDevice(t){if(!R(this?.m?.setAudioRoute))return;const e=t===exports.AudioPlayBackDevice.EAR||1===t?exports.AudioPlayBackDevice.EAR:exports.AudioPlayBackDevice.SPEAKER;await this.m.setAudioRoute(e)}async startRemoteView(t){if(!this.m||"function"!=typeof this.m.startRemoteView)return;const{userId:e,view:s,streamType:i=0}=t||{};return this.m.startRemoteView(e,s,i)}async stopRemoteView(t){if(!this.m||"function"!=typeof this.m.stopRemoteView)return;const{userId:e,streamType:s=0}=t||{};return this.m.stopRemoteView(e,s)}T(){if(this.I||!this.N||"function"!=typeof this.N.on)return;const t=o("SDK_READY"),e=o("KICKED_OUT"),s=o("USER_SIG_EXPIRED"),i=o("ROOM_CUSTOM_DATA_RECEIVED");this.F=()=>{this.S=1,this.D()},this.W=t=>{this.i.emit(E.KICKED_OUT,t||{})},this.J=()=>{this.i.emit(E.onUserSigExpired,{})},this.Y=t=>{const{Data:e={},Head:s={}}=JSON.parse(t.data);if("call_engine_srv.invite_user_notify"===s?.Command){const{CallInfo:t={}}=e;let s={intRoomId:0,strRoomId:""};if(1===t.RoomIdType)s.intRoomId=+t.RoomId;else{if(2!==t.RoomIdType)return;s.strRoomId=t.RoomId}this.L=O({userIDList:e?.CalleeList_Account||[],callId:e?.CallInfo?.CallId||"",inviter:e?.Caller_Account||"",roomId:s.intRoomId||0,strRoomId:s.strRoomId||"",groupId:e?.CallInfo?.GroupId||"",type:e?.CallInfo?.CallMediaType,status:exports.CallStatus.WAITING,role:exports.CallRole.CALLEE}),this.i.emit(E.ON_CALL_RECEIVED,{callId:this.L.callId,inviter:this.L.inviter,inviteeList:this.L.inviteeList,groupId:this.L.groupId,mediaType:this.L.mediaType,roomId:this.L.roomId,userData:e?.UserData||"",callInfo:{...this.L}})}},t&&this.N.on(t,this.F),e&&this.N.on(e,this.W),s&&this.N.on(s,this.J),i&&this.N.on(i,this.Y),this.I=1}U(){if(!this.I||!this.N||"function"!=typeof this.N.off)return;const t=o("SDK_READY"),e=o("KICKED_OUT"),s=o("USER_SIG_EXPIRED"),i=o("ROOM_CUSTOM_DATA_RECEIVED");try{t&&this.F&&this.N.off(t,this.F)}catch(t){}try{e&&this.W&&this.N.off(e,this.W)}catch(t){}try{s&&this.J&&this.N.off(s,this.J)}catch(t){}try{i&&this.Y&&this.N.off(i,this.Y)}catch(t){}this.I=0}V(){if(this.C||!this.m)return;const t=this.m;if("function"!=typeof t.on)return;const e={},s=t=>(...e)=>{try{this.i.emit(t,...e)}catch(t){}};for(const i of z){let r;if("onRemoteUserEnterRoom"===i){const t=s(i);r=(...e)=>{try{this.L&&this.L.callId&&this.L.status===exports.CallStatus.WAITING&&(this.L.status=exports.CallStatus.CALLING,this.L.callStartTime||(this.L.callStartTime=Date.now()),this.i.emit(E.ON_CALL_BEGIN,{callInfo:{...this.L}}))}catch(t){}t(...e)}}else r=s(i);e[i]=r;try{t.on(i,r)}catch(t){}}this._=e,this.C=1}P(){if(!this.C)return;const t=this.m,e=this._||{};if(t&&"function"==typeof t.off)for(const s of Object.keys(e))try{t.off(s,e[s])}catch(t){}this._=null,this.C=0}D(){this.q||(this.q=1,this.i.emit(E.SDK_READY,{name:"sdk_ready"}),this.$().catch(t=>{}))}async $(){if(!this.N||!this.O())return;if(this.L&&this.L.callId)return;const t=this.G();if(!t)return;let e;try{e=await F.getInvitation({chat:this.N,userId:t})}catch(t){return}if(e&&0===e.errCode&&e.data)try{const t={data:JSON.stringify({Data:e.data,Head:{Command:"call_engine_srv.invite_user_notify"}})};R(this.Y)&&this.Y(t)}catch(t){}}O(){try{if(!this.N)return 0;if("function"==typeof this.N.getLoginUser)return!!this.N.getLoginUser()}catch(t){}return 0}G(){return this.l?this.l:R(this?.N?.getLoginUser)&&this.N.getLoginUser()||""}async H(){try{F.stopHeartBeat()}catch(t){}if(this.L.callType===exports.CallMediaType.UNKNOWN)throw n(-1,"call type is none");const{roomId:t}=this.L;if(0===t.intRoomId&&""===t.strRoomId)throw n(-1,"room id is empty");const e={sdkAppId:this.o,userId:this.G(),userSig:this.u,role:20};t.intRoomId>0?e.roomId=t.intRoomId:e.strRoomId=t.strRoomId;const s=this.L.mediaType===exports.CallMediaType.VIDEO?0:2,i=this.getTRTCCloudInstance();await new Promise((t,r)=>{let o=0,a=null;const c=()=>{a&&(clearTimeout(a),a=null);try{this.i.off("onEnterRoom",h)}catch(t){}},h=e=>{o||(o=1,c(),"number"==typeof e&&e>0?t():r(n("number"==typeof e?e:-1,"enterRoom failed: "+e)))};this.i.on("onEnterRoom",h),a=setTimeout(()=>{o||(o=1,c(),r(n(-1,"enterRoom timeout")))},1e4);try{const t=i.enterRoom(e,s);t&&"function"==typeof t.catch&&t.catch(t=>{o||(o=1,c(),r(t))})}catch(t){if(o)return;o=1,c(),r(t)}})}K(){try{this.m&&"function"==typeof this.m.exitRoom&&this.m.exitRoom()}catch(t){}}M(){try{F.stopHeartBeat()}catch(t){}this.L=O()}B(){return this.R||(this.R=(()=>{const t=Array(16),e="undefined"!=typeof crypto&&crypto||null;if(e&&"function"==typeof e.getRandomValues){const s=new Uint8Array(16);e.getRandomValues(s);for(let e=0;16>e;e++)t[e]=s[e]}else for(let e=0;16>e;e++)t[e]=Math.floor(256*Math.random());const s=t.map(t=>t.toString(16).padStart(2,"0")).join("").toUpperCase();return`${s.slice(0,8)}-${s.slice(8,12)}-${s.slice(12,16)}-${s.slice(16,20)}-${s.slice(20,32)}`})()),this.R}}Z.instance=null,exports.TencentCloudChat=C,exports.CallInvitationRespondedType=_,exports.TUICallEngine=Z,exports.TUICallEvent=E,exports.TUIErrorCode=x,exports.default=Z;
|
|
2
|
+
"use strict";function t(t,e){const s={Title:(t=t||{}).title||"",Sound:t.iosSound||"",BadgeMode:t.ignoreIosBadge?1:0,IsVoipPush:t.iosPushType||0,ContentAvailable:t.enableIosBackgroundNotification?1:0,InterruptionLevel:t.iosInterruptionLevel||"",Image:t.iosImage||""},i={Sound:t.androidSound||"",Title:t.title||"",OPPOChannelID:t.androidOppoChannelId||"",OPPOCategory:t.oppoCategory||"IM",OPPONotifyLevel:t.oppoNotifyLevel||0,GoogleChannelID:t.androidFcmChannelId||"",GoogleImage:t.fcmImage||"",XiaoMiChannelID:t.androidXiaomiChannelId||"",VIVOClassification:t.androidVivoClassification||0,VIVOCategory:t.vivoCategory||"IM",HuaWeiCategory:t.androidHuaweiCategory||"IM",HuaWeiImage:t.huaweiImage||"",HonorImportance:t.honorImportance||"NORMAL",HonorImage:t.honorImage||""},r={entity:{action:2},timPushFeatures:{fcmPushType:0,fcmNotificationType:1},voip_ext:{voip_type:"call",voip_media_type:e===exports.CallMediaType.VIDEO?"video":"audio"}};return{Title:t.title||"",Description:t.description||"",Ext:JSON.stringify(r),AndroidInfo:i,ApnsInfo:s}}function e(){B&&(clearInterval(B),B=null),k="",j=0}async function s(t,e){const s=t||{},i=s.chat;if(!i||!R(i.callExperimentalAPI))return{errCode:H,errMsg:"chat.callExperimentalAPI is not available"};if(!s.callId)return{errCode:H,errMsg:"callId is required"};if(!s.terminalId)return{errCode:H,errMsg:"terminalId is required"};let r=b;e===_.Hangup&&(r=P);const o=((t,e,s,i)=>({Body:{CallId:t||"",TerminalId:e||"",ActionType:s>>>0},Head:T(i)}))(s.callId,s.terminalId,e,r);try{const t={serviceCommand:r,data:JSON.stringify(o)},e=await i.callExperimentalAPI(S,t),s=e&&e.data||e||{},n=L(s.ErrorCode)?s.ErrorCode:L(s.errorCode)?s.errorCode:0;return 0!==n?{errCode:n,errMsg:s.ErrorInfo||s.errorInfo||""}:{errCode:0,errMsg:""}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed"}}}function i(t){switch(t){case exports.LayoutTemplate.FLOAT:return"float";case exports.LayoutTemplate.GRID:return"grid";default:return"float"}}function r(){return{template:exports.LayoutTemplate.FLOAT,canvas:{w:Y,h:q},primaryUser:{userId:"",isLocal:0},secondaryUser:null,showRemoteInfo:0}}function o(t){const e=C&&C.EVENT||{};switch(t){case"SDK_READY":return e.SDK_READY||"sdkStateReady";case"KICKED_OUT":return e.KICKED_OUT||"kickedOut";case"USER_SIG_EXPIRED":return e.USER_SIG_EXPIRED||"userSigExpired";case"ROOM_CUSTOM_DATA_RECEIVED":return e.ROOM_CUSTOM_DATA_RECEIVED;default:return null}}function n(t,e){const s=Error(e);return s.code=t,s}Object.defineProperty(exports,"__esModule",{value:1});var a,c,h,l,u,d,f,p,I,y,m=require("@tencentcloud/trtc-component-wx"),C=require("@tencentcloud/lite-chat/basic");class w{constructor(){this.t=new Map}on(t,e){if("function"!=typeof e)return this;let s=this.t.get(t);return s||(s=[],this.t.set(t,s)),s.push({fn:e,once:0}),this}once(t,e){if("function"!=typeof e)return this;let s=this.t.get(t);return s||(s=[],this.t.set(t,s)),s.push({fn:e,once:1}),this}off(t,e){if(!t)return this.t.clear(),this;const s=this.t.get(t);if(!s)return this;if(!e)return this.t.delete(t),this;const i=s.filter(t=>t.fn!==e);return 0===i.length?this.t.delete(t):this.t.set(t,i),this}emit(t,...e){const s=this.t.get(t);if(!s||0===s.length)return 0;const i=s.slice();for(const s of i){try{s.fn.apply(null,e)}catch(t){}s.once&&this.off(t,s.fn)}return 1}removeAllListeners(t){return t?this.t.delete(t):this.t.clear(),this}listenerCount(t){const e=this.t.get(t);return e?e.length:0}}exports.CallStatus=void 0,(a=exports.CallStatus||(exports.CallStatus={}))[a.NONE=0]="NONE",a[a.WAITING=1]="WAITING",a[a.CALLING=2]="CALLING",exports.CallRole=void 0,(c=exports.CallRole||(exports.CallRole={}))[c.NONE=0]="NONE",c[c.CALLER=1]="CALLER",c[c.CALLEE=2]="CALLEE",exports.CallMediaType=void 0,(h=exports.CallMediaType||(exports.CallMediaType={}))[h.UNKNOWN=0]="UNKNOWN",h[h.AUDIO=1]="AUDIO",h[h.VIDEO=2]="VIDEO",exports.CallScene=void 0,(l=exports.CallScene||(exports.CallScene={}))[l.NONE=0]="NONE",l[l.GROUP=1]="GROUP",l[l.MULTI=2]="MULTI",l[l.SINGLE=3]="SINGLE",exports.IOSOfflinePushType=void 0,(u=exports.IOSOfflinePushType||(exports.IOSOfflinePushType={}))[u.APNs=0]="APNs",u[u.VoIP=1]="VoIP",exports.CallInvitationResultCodeType=void 0,(d=exports.CallInvitationResultCodeType||(exports.CallInvitationResultCodeType={}))[d.SUCCESS=0]="SUCCESS",d[d.INVITED=1]="INVITED",d[d.LINE_BUSY=2]="LINE_BUSY",exports.CallEndReason=void 0,(f=exports.CallEndReason||(exports.CallEndReason={}))[f.UNKNOWN=0]="UNKNOWN",f[f.HANGUP=1]="HANGUP",f[f.REJECT=2]="REJECT",f[f.NO_RESPONSE=3]="NO_RESPONSE",f[f.OFFLINE=4]="OFFLINE",f[f.LINE_BUSY=5]="LINE_BUSY",f[f.CANCELED=6]="CANCELED",f[f.OTHER_DEVICE_ACCEPTED=7]="OTHER_DEVICE_ACCEPTED",f[f.OTHER_DEVICE_REJECT=8]="OTHER_DEVICE_REJECT",f[f.END_BY_SERVER=9]="END_BY_SERVER",exports.LOG_LEVEL=void 0,(p=exports.LOG_LEVEL||(exports.LOG_LEVEL={}))[p.DEBUG=0]="DEBUG",p[p.INFO=1]="INFO",p[p.WARN=2]="WARN",p[p.ERROR=3]="ERROR",p[p.NONE=4]="NONE",exports.CameraPosition=void 0,(I=exports.CameraPosition||(exports.CameraPosition={}))[I.FRONT=1]="FRONT",I[I.BACK=0]="BACK",exports.AudioPlayBackDevice=void 0,(y=exports.AudioPlayBackDevice||(exports.AudioPlayBackDevice={}))[y.SPEAKER=0]="SPEAKER",y[y.EAR=1]="EAR";const _=Object.freeze({None:0,AcceptedByInvitee:1,RejectedByInvitee:2,NoResponseUntilTimeout:3,Hangup:4,Join:5,Offline:6,InviteUser:7}),x=Object.freeze({ERR_FAILED:-1}),E=Object.freeze({ERROR:"onError",SDK_READY:"sdkReady",KICKED_OUT:"onKickedOffline",onUserSigExpired:"onUserSigExpired",ON_CALL_BEGIN:"onCallBegin",ON_CALL_END:"onCallEnd",ON_CALL_RECEIVED:"onCallReceived",ON_CALL_CANCELED:"onCallCanceled",ON_CALL_NOT_CONNECTED:"onCallNotConnected",INVITED:"onInvited",USER_ENTER:"onUserJoin",USER_LEAVE:"onUserLeave",VIEW_LAYOUT_CHANGED:"viewLayoutChanged"}),g="function",L=t=>"number"==typeof t,v=t=>typeof Array.isArray===g?Array.isArray(t):"array"===A(t),R=t=>typeof t===g,A=t=>({}.toString.call(t).match(/^\[object (.*)\]$/)[1].toLowerCase());let N=null;const T=t=>{const e=(()=>{if(N)return N;const t={osName:"Unknown",osVersion:"0.0.0",deviceName:"Unknown"};try{const e="undefined"!=typeof wx?wx:null,s="undefined"!=typeof uni?uni:null;if(e&&"function"==typeof e.getSystemInfoSync){const s=e.getSystemInfoSync()||{},[i="Unknown",r="0.0.0"]=((s.system||"")+"").split(" ");t.osName=i,t.osVersion=r,t.deviceName=s.model||s.brand||"Unknown"}else if(s&&"function"==typeof s.getSystemInfoSync){const e=s.getSystemInfoSync()||{},[i="Unknown",r="0.0.0"]=((e.system||"")+"").split(" ");t.osName=i,t.osVersion=r,t.deviceName=e.model||e.brand||"Unknown"}else if("undefined"!=typeof navigator&&navigator.userAgent){const e=navigator.userAgent;if(/Mac OS X/i.test(e)){t.osName="MacOS";const s=e.match(/Mac OS X ([\d_\.]+)/);t.osVersion=s?s[1].replace(/_/g,"."):"0.0.0",t.deviceName="Mac"}else if(/Windows/i.test(e)){t.osName="Windows";const s=e.match(/Windows NT ([\d\.]+)/);t.osVersion=s?s[1]:"0.0.0",t.deviceName="PC"}else if(/Android/i.test(e)){t.osName="Android";const s=e.match(/Android ([\d\.]+)/);t.osVersion=s?s[1]:"0.0.0",t.deviceName="Android"}else if(/iPhone|iPad|iPod/i.test(e)){t.osName="iOS";const s=e.match(/OS ([\d_]+)/);t.osVersion=s?s[1].replace(/_/g,"."):"0.0.0",t.deviceName=/iPad/i.test(e)?"iPad":"iPhone"}}}catch(t){}return N=t,t})();return{Command:t,DeviceName:e.deviceName,LongPollingKey:"",OSName:e.osName,OSVersion:e.osVersion,RoomId:"",SdkVersion:"1.0.0.0",Version:2}},O=(t={})=>{let e=exports.CallScene.NONE;const s=t.userIDList||[];return s.length>1&&(e=exports.CallScene.MULTI),1===s.length&&(e=exports.CallScene.SINGLE),{callId:t.callId||"",roomId:{strRoomId:t.strRoomID||t.strRoomId||"",intRoomId:t.roomID||t.roomId||0},inviter:t.inviter||"",inviteeList:s,groupId:t.chatGroupID||"",mediaType:t.type,status:t.status||exports.CallStatus.NONE,role:t.role||exports.CallRole.NONE,scene:e,callStartTime:0}},D=Object.freeze({IntRoomId:1,StrRoomId:2}),S="sendTRTCCustomData",M="call_engine_srv.start_call",U="call_engine_srv.heart_beat",b="call_engine_srv.handle_call",P="call_engine_srv.hangup",V="call_engine_srv.cancel_invite",G="call_engine_srv.get_invitation",H=-1,K={errCode:0,errMsg:"",inviteId:"",callResultList:[]};let B=null,k="",j=0;const F={CMD_START_CALL:M,CMD_HEART_BEAT:U,CMD_HANDLE_CALL:b,CMD_HANGUP_CALL:P,CMD_CANCEL_CALL:V,CMD_GET_INVITATION:G,HEARTBEAT_INTERVAL_MS:2e3,HEARTBEAT_FAIL_RETRY_DEFAULT:5,HEARTBEAT_FAIL_INTERVAL_MS:2e3,async startCall(e){const s=e||{},i=s.chat;if(!i||!R(i.callExperimentalAPI))return{...K,errCode:H,errMsg:"chat.callExperimentalAPI is not available"};if(!s.inviter)return{...K,errCode:H,errMsg:"inviter is required"};if(!v(s.userIdList)||0===s.userIdList.length)return{...K,errCode:H,errMsg:"userIdList cannot be empty"};if(s.mediaType!==exports.CallMediaType.AUDIO&&s.mediaType!==exports.CallMediaType.VIDEO)return{...K,errCode:H,errMsg:"invalid mediaType"};const r={roomId:s.roomId||{},timeout:s.timeout,userData:s.userData,isEphemeralCall:!!s.isEphemeralCall,offlinePushInfo:s.offlinePushInfo},o=((e,s,i,r,o)=>{const n=r||{},a=n.roomId||{},c={CallId:"",CallMediaType:i>>>0};"number"==typeof a.intRoomId&&a.intRoomId>0?(c.RoomId=a.intRoomId+"",c.RoomIdType=D.IntRoomId):a.intRoomId&&0!==a.intRoomId||"string"!=typeof a.strRoomId||0>=a.strRoomId.length||(c.RoomId=a.strRoomId,c.RoomIdType=D.StrRoomId),c.ExcludeFromHistoryMessage=0,c.GroupId=o||"",c.IsEphemeralCall=!!n.isEphemeralCall;const h="number"==typeof n.timeout?n.timeout:30;return{Body:{Caller_Account:e,CalleeList_Account:(s||[]).slice(),CallInfo:c,Timeout:1e3*h,UserData:n.userData||"",OfflinePushInfo:t(n.offlinePushInfo,i)},Head:T("call_engine_srv.start_call")}})(s.inviter,s.userIdList,s.mediaType,r,s.groupId||"");let n;try{const t={serviceCommand:M,data:JSON.stringify(o)};n=await i.callExperimentalAPI(S,t)}catch(t){const e=t&&(t.code||t.errorCode)||H,s=t&&(t.message||t.errorInfo)||"send failed";return{...K,errCode:e,errMsg:s}}const a=n&&n.data||n||{};return 0!==a.ErrorCode?{...K,errCode:a.ErrorCode,errMsg:a.ErrorInfo}:{errCode:a?.ErrorCode||0,errMsg:a?.errMsg||"",inviteId:a?.Response?.CallId||"",callResultList:a?.Response?.CallResult||[]}},startHeartBeat(t){const s=t||{},{chat:i,callId:r,userId:o}=s;if(!i||!R(i.callExperimentalAPI))return;if(!r||!o)return;e();const n=L(s.intervalMs)&&s.intervalMs>0?s.intervalMs:2e3,a=L(s.retryCount)&&s.retryCount>0?s.retryCount:5;k=r,B=setInterval(async()=>{if(!k||k!==r)return;const t=await(async(t,e,s)=>{const i={Body:{User_Account:s,CallId:e},Head:T(U)};try{const e={serviceCommand:U,data:JSON.stringify(i)},s=await t.callExperimentalAPI(S,e),r=s&&s.data||s||{},o=L(r.ErrorCode)?r.ErrorCode:L(r.errorCode)?r.errorCode:0;return 0!==o?{errCode:o,errMsg:r.ErrorInfo||r.errorInfo||""}:{errCode:0,errMsg:""}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed"}}})(i,r,o);if(k&&k===r)if(0!==t.errCode){if(j+=1,j>=a){const t=k;if(e(),"function"==typeof s.onTimeout)try{s.onTimeout(t)}catch(t){}}}else j=0},n)},stopHeartBeat:e,async accept(t){return s(t,_.AcceptedByInvitee)},async reject(t){return s(t,_.RejectedByInvitee)},async hangup(t){return s(t,_.Hangup)},async cancel(t){const e=t||{},s=e.chat;if(!s||!R(s.callExperimentalAPI))return{errCode:H,errMsg:"chat.callExperimentalAPI is not available"};if(!e.callId)return{errCode:H,errMsg:"callId is required"};if(!e.inviter)return{errCode:H,errMsg:"inviter is required"};if(!v(e.calleeList)||0===e.calleeList.length)return{errCode:H,errMsg:"calleeList cannot be empty"};const i=(r=e.inviter,o=e.callId,n=e.calleeList,a=V,{Body:{CallId:o||"",Operator_Account:r||"",CalleeList_Account:Array.isArray(n)?n.filter(Boolean):[]},Head:T(a)});var r,o,n,a;try{const t={serviceCommand:V,data:JSON.stringify(i)},e=await s.callExperimentalAPI(S,t),r=e&&e.data||e||{},o=L(r.ErrorCode)?r.ErrorCode:L(r.ErrorCode)?r.errorCode:0;return 0!==o?{errCode:o,errMsg:r.ErrorInfo||r.errorInfo||""}:{errCode:0,errMsg:""}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed"}}},async getInvitation(t){const e=t||{},s=e.chat;if(!s||!R(s.callExperimentalAPI))return{errCode:H,errMsg:"chat.callExperimentalAPI is not available",data:null};if(!e.userId)return{errCode:H,errMsg:"userId is required",data:null};const i={Body:{User_Account:e.userId||""},Head:T(G)};var r;try{const t={serviceCommand:G,data:JSON.stringify(i)},e=await s.callExperimentalAPI(S,t),o=e&&e.data||e||{},n=L(o.ErrorCode)?o.ErrorCode:L(o.ErrorCode)?o.errorCode:0;if(0!==n)return{errCode:n,errMsg:o.ErrorInfo||o.errorInfo||"",data:null};const a=o?.Response;return a&&null!==(r=a)&&"object"==typeof r&&0!==Object.keys(a).length?{errCode:0,errMsg:"",data:a}:{errCode:0,errMsg:"",data:null}}catch(t){return{errCode:t&&(t.code||t.errorCode)||H,errMsg:t&&(t.message||t.errorInfo)||"send failed",data:null}}}};var W,J;exports.LayoutTemplate=void 0,(W=exports.LayoutTemplate||(exports.LayoutTemplate={}))[W.FLOAT=0]="FLOAT",W[W.GRID=1]="GRID",(t=>{t[t.NONE=0]="NONE",t[t.WAITING=1]="WAITING",t[t.CALLING=2]="CALLING"})(J||(J={}));const Y=720,q=1280,$={x:480,y:80,w:200,h:300};class X{constructor(){this.currentTemplate=exports.LayoutTemplate.FLOAT,this.largeViewUserId="",this.isViewReversed=0,this.currentLayout=r(),this.previousLayout=r()}setLayoutTemplate(t){this.currentTemplate=t,this.largeViewUserId="",this.isViewReversed=0}setLargeViewUser(t){this.largeViewUserId===t?this.largeViewUserId="":this.largeViewUserId=t}toggleViewReversed(){this.isViewReversed=!this.isViewReversed}getCurrentTemplate(){return this.currentTemplate}getCurrentLayout(){return this.currentLayout}recalculate(t){switch(this.previousLayout=this.currentLayout,this.currentTemplate){case exports.LayoutTemplate.FLOAT:this.currentLayout=this.calculateFloatLayout(t);break;case exports.LayoutTemplate.GRID:this.currentLayout=this.calculateGridLayout(t)}return this.isLayoutChanged()}toJson(){return JSON.stringify(this.toJsonObject())}toJsonObject(){const t=this.currentLayout,e={template:i(t.template)};return t.template===exports.LayoutTemplate.FLOAT?(e.canvas={w:t.canvas.w,h:t.canvas.h},e.primaryUser={userId:t.primaryUser.userId,isLocal:t.primaryUser.isLocal},t.secondaryUser?e.secondaryUser={userId:t.secondaryUser.userId,isLocal:t.secondaryUser.isLocal,frame:{...t.secondaryUser.frame}}:e.secondaryUser=null,e.showRemoteInfo=t.showRemoteInfo):(e.canvas={w:t.canvas.w,h:t.canvas.h},e.participants=t.participants.map(t=>({userId:t.userId,frame:{...t.frame},isLocal:t.isLocal,showName:t.showName}))),e}isLayoutChanged(){const t=this.previousLayout,e=this.currentLayout;if(t.template!==e.template)return 1;if(e.template===exports.LayoutTemplate.FLOAT&&t.template===exports.LayoutTemplate.FLOAT)return t.primaryUser.userId!==e.primaryUser.userId||(t.secondaryUser?t.secondaryUser.userId:"")!==(e.secondaryUser?e.secondaryUser.userId:"")||t.showRemoteInfo!==e.showRemoteInfo?1:0;if(e.template===exports.LayoutTemplate.GRID&&t.template===exports.LayoutTemplate.GRID){if(t.participants.length!==e.participants.length)return 1;for(let s=0;s<e.participants.length;s++){const i=t.participants[s],r=e.participants[s];if(i.userId!==r.userId)return 1;if(i.frame.x!==r.frame.x)return 1;if(i.frame.y!==r.frame.y)return 1;if(i.frame.w!==r.frame.w)return 1;if(i.frame.h!==r.frame.h)return 1;if(i.showName!==r.showName)return 1}return 0}return 1}calculateFloatLayout(t){const e=t.selfId;let s="";for(const i of t.allParticipants)if(i.id!==e){s=i.id;break}const i=t.selfStatus===J.CALLING,r=t.mediaType===exports.CallMediaType.AUDIO;let o,n,a,c;return i||r?!i&&r||r?(o=s,n=0,a=null,c=1):(this.isViewReversed?(o=e,n=1,a={userId:s,isLocal:0,frame:{...$}}):(o=s,n=0,a={userId:e,isLocal:1,frame:{...$}}),c=0):(o=e,n=1,a=null,c=1),{template:exports.LayoutTemplate.FLOAT,canvas:{w:Y,h:q},primaryUser:{userId:o,isLocal:n},secondaryUser:a,showRemoteInfo:c}}calculateGridLayout(t){const e=new Map;for(const s of t.allParticipants)e.set(s.id,s.status);const s=[];if(t.inviter){const i=e.get(t.inviter);void 0!==i&&i!==J.NONE&&s.push(t.inviter)}for(const i of t.inviteeList){const t=e.get(i);void 0!==t&&t!==J.NONE&&s.push(i)}const i=this.calculateGridPositions(s.length,s,t.selfId);return{template:exports.LayoutTemplate.GRID,canvas:{w:Y,h:q},participants:i}}calculateGridPositions(t,e,s){const i=[];if(0>=t)return i;const r=240,o=360;let n=-1;if(this.largeViewUserId)for(let s=0;t>s;s++)if(e[s]===this.largeViewUserId){n=s;break}if(n>=0&&4>=t){const o=[n];for(let e=0;t>e;e++)e!==n&&o.push(e);const a=r,c=t-1,h=[{x:0,y:0,w:Y,h:Y}],l=Y;for(let t=0;c>t;t++)h.push({x:t*a,y:l,w:a,h:a});for(let t=0;t<o.length&&t<h.length;t++){const r=e[o[t]];i.push({userId:r,frame:h[t],isLocal:r===s,showName:0===t})}return i}if(n>=0&&t>4){const o=480,a=480,c=[n];for(let e=0;t>e;e++)e!==n&&c.push(e);{const t=e[c[0]];i.push({userId:t,frame:{x:0,y:0,w:o,h:a},isLocal:t===s,showName:1})}let h=1;if(h<c.length){const t=e[c[h]];i.push({userId:t,frame:{x:o,y:0,w:r,h:r},isLocal:t===s,showName:0}),h++}if(h<c.length){const t=e[c[h]];i.push({userId:t,frame:{x:o,y:r,w:r,h:r},isLocal:t===s,showName:0}),h++}let l=a;for(;h<c.length;){for(let t=0;3>t&&h<c.length;t++){const o=e[c[h]];i.push({userId:o,frame:{x:t*r,y:l,w:r,h:r},isLocal:o===s,showName:0}),h++}l+=r}return i}if(1===t)i.push({userId:e[0],frame:{x:0,y:0,w:Y,h:q},isLocal:e[0]===s,showName:0});else if(2===t)i.push({userId:e[0],frame:{x:0,y:0,w:o,h:o},isLocal:e[0]===s,showName:0}),i.push({userId:e[1],frame:{x:o,y:0,w:o,h:o},isLocal:e[1]===s,showName:0});else if(3===t)i.push({userId:e[0],frame:{x:0,y:0,w:o,h:o},isLocal:e[0]===s,showName:0}),i.push({userId:e[1],frame:{x:o,y:0,w:o,h:o},isLocal:e[1]===s,showName:0}),i.push({userId:e[2],frame:{x:180,y:o,w:o,h:o},isLocal:e[2]===s,showName:0});else if(4===t)i.push({userId:e[0],frame:{x:0,y:0,w:o,h:o},isLocal:e[0]===s,showName:0}),i.push({userId:e[1],frame:{x:o,y:0,w:o,h:o},isLocal:e[1]===s,showName:0}),i.push({userId:e[2],frame:{x:0,y:o,w:o,h:o},isLocal:e[2]===s,showName:0}),i.push({userId:e[3],frame:{x:o,y:o,w:o,h:o},isLocal:e[3]===s,showName:0});else{let o=0;for(let n=0;t>n;n+=3){const a=Math.min(3,t-n);for(let t=0;a>t;t++){const a=n+t;i.push({userId:e[a],frame:{x:t*r,y:o,w:r,h:r},isLocal:e[a]===s,showName:0})}o+=r}}return i}}const Q=new w,z=Object.freeze(["onEnterRoom","onExitRoom","onSwitchRole","onError","onWarnning","onRemoteUserEnterRoom","onRemoteUserLeaveRoom","onUserVideoAvailable","onUserSubStreamAvailable","onUserAudioAvailable","onUserVoiceVolume","onNetworkQuality","onFirstVideoFrame","onSendFirstLocalVideoFrame","onSendFirstLocalAudioFrame","onMicDidReady"]);class Z{constructor(t){const e=t||{};this.i=new w,this.o=e.SDKAppID||e.sdkAppID||0,this.l="",this.u="",this.p=0,this.I=0,this.m=null,this.C=0,this._=null,this.L=O(),this.v=0,this.R="",this.A=new X;const s=e.chat||e.tim;s&&(this.p=1,this.N=s)}static createInstance(t){return Z.instance||(Z.instance=new Z(t)),Z.instance}static once(t,e){"ready"===t?"function"==typeof e&&Promise.resolve().then(()=>{try{e()}catch(t){}}):Q.once(t,e)}async login(t){const e=t||{},s=e.userID,i=e.userSig,r=e.sdkAppID||e.SDKAppID||this.o;if(!s||!i)throw n(x.ERR_FAILED,"login: userID and userSig are required");!this.o&&r&&(this.o=r),this.l=s,this.u=i,this.T();try{if(this.N=C.create({SDKAppID:this.o,devMode:1}),this.O())return void this.D();let t;try{t=await this.N.login({userID:s,userSig:i});try{this.getTRTCCloudInstance()}catch(t){}}catch(t){const e=t&&t.code,s=t&&t.message||"";if(2024===e||2025===e)return void this.D();if(this.S&&(/timeout/i.test(s)||"NETWORK_TIMEOUT"===e))return void this.D();throw t}if(t&&t.data&&t.data.repeatLogin)return void this.D();this.D()}catch(t){const e=t&&t.code||x.ERR_FAILED,s=t&&t.message||"login failed";throw this.i.emit(E.ERROR,{code:e,message:s}),n(e,s)}}async logout(){this.M();try{this.N&&"function"==typeof this.N.logout&&await this.N.logout()}finally{this.l="",this.u=""}}async destroyInstance(){Z.instance=null;try{await this.logout()}catch(t){}this.U(),this.P(),this.i.removeAllListeners(),this.m=null}on(t,e){this.i.on(t,e)}off(t,e){this.i.off(t,e)}once(t,e){this.i.once(t,e)}getTim(){return this.N}getTRTCCloudInstance(){if(this.m)return this.m;if(!m||"function"!=typeof m.getTRTCShareInstance)throw n(x.ERR_FAILED,"[call-engine-wx] @tencentcloud/trtc-component-wx is not installed or does not expose getTRTCShareInstance().");return this.m=m.getTRTCShareInstance(),this.V(),this.m}setLogLevel(t){try{this.N&&"function"==typeof this.N.setLogLevel&&this.N.setLogLevel(t)}catch(t){}}async calls(t){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"calls: not logged in. Call login() first and wait for SDK_READY.");if(this.v)throw n(x.ERR_FAILED,"calls: a previous calls() is still in progress");if(this.L.status!==exports.CallStatus.NONE)throw n(x.ERR_FAILED,"calls: there is an active call already; hang up before starting a new one");if(!v(t?.userIDList)||0===t?.userIDList?.length)throw n(x.ERR_FAILED,"calls: userIDList cannot be empty");if(t?.type!==exports.CallMediaType.AUDIO&&t?.type!==exports.CallMediaType.VIDEO)throw n(x.ERR_FAILED,"calls: invalid mediaType");const e=this.G();if(!e)throw n(x.ERR_FAILED,"calls: cannot resolve login user id");var s;this.L=O({...t,strRoomId:(s=t?.strRoomID,s||"roomId_"+Math.floor(1e6*Math.random())),status:exports.CallStatus.WAITING,role:exports.CallRole.CALLER,inviter:e}),this.v=1;let i=0;try{try{await this.H(),i=1}catch(t){this.K();const e=t&&t.code||x.ERR_FAILED,s=t&&t.message||"enter TRTC room failed";throw this.i.emit(E.ERROR,{code:e,message:s}),n(e,s)}const t=await F.startCall({chat:this.N,...this.L,userIdList:this.L.inviteeList});if(0!==t.errCode)throw this.K(),i=0,this.i.emit(E.ERROR,{code:t.errCode,message:t.errMsg}),n(t.errCode,t.errMsg||"start_call failed");this.L.callId=t.inviteId,this.L.callStartTime=Date.now();let e=0;for(const s of t.callResultList)switch(s.resultCode){case exports.CallInvitationResultCodeType.INVITED:this.i.emit(E.USER_ENTER,s.userId);break;case exports.CallInvitationResultCodeType.SUCCESS:this.i.emit("onUserInviting",s.userId);break;case exports.CallInvitationResultCodeType.LINE_BUSY:this.i.emit("onUserLineBusy",s.userId),e++}if(e>=this.L.inviteeList.length){const e=t.callResultList[t.callResultList.length-1];return this.K(),i=0,this.i.emit(E.ON_CALL_NOT_CONNECTED,{callInfo:{...this.L},userId:e?e.userId:"",reason:exports.CallEndReason.LINE_BUSY}),this.M(),{inviteId:t.inviteId,roomId:this.L,callResultList:t.callResultList}}return F.startHeartBeat({chat:this.N,callId:this.L.callId,userId:this.L.inviter,onTimeout:t=>{this.L.callId===t&&(this.i.emit(E.ON_CALL_NOT_CONNECTED,{callInfo:{...this.L},userId:this.L.inviter,reason:exports.CallEndReason.LINE_BUSY}),this.K(),this.M())}}),{inviteId:t.inviteId,roomId:this.L,callResultList:t.callResultList}}catch(t){throw i&&this.K(),this.M(),t}finally{this.v=0}}async accept(){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"accept: not logged in. Call login() first and wait for SDK_READY.");if(this.L.role!==exports.CallRole.CALLEE)throw n(x.ERR_FAILED,"accept: current role is not CALLEE");if(this.L.status!==exports.CallStatus.WAITING)throw n(x.ERR_FAILED,`accept: invalid call status ${this.L.status}; expected WAITING`);const t=this.L.callId;if(!t)throw n(x.ERR_FAILED,"accept: no active invitation (callId is empty)");let e=0;try{await this.H(),e=1;const s=this.B(),i=await F.accept({chat:this.N,callId:t,terminalId:s});if(0!==i.errCode)throw n(i.errCode,i.errMsg||"handle_call(accept) failed");return this.L.status=exports.CallStatus.CALLING,this.L.callStartTime=Date.now(),this.i.emit(E.ON_CALL_BEGIN,{callInfo:{...this.L}}),F.startHeartBeat({chat:this.N,callId:this.L.callId,userId:this.G(),onTimeout:t=>{this.L.callId===t&&(this.i.emit(E.ON_CALL_NOT_CONNECTED,{callInfo:{...this.L},userId:this.G(),reason:exports.CallEndReason.LINE_BUSY}),this.K(),this.M())}}),{callId:t,roomId:this.L.roomId}}catch(t){e&&this.K(),this.M();const s=t&&t.code||x.ERR_FAILED,i=t&&t.message||"accept failed";throw this.i.emit(E.ERROR,{code:s,message:i}),n(s,i)}}async reject(){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"reject: not logged in. Call login() first and wait for SDK_READY.");if(this.L.role!==exports.CallRole.CALLEE)throw n(x.ERR_FAILED,"reject: current role is not CALLEE");if(this.L.status!==exports.CallStatus.WAITING)throw n(x.ERR_FAILED,`reject: invalid call status ${this.L.status}; expected WAITING`);const t=this.L.callId;if(!t)throw n(x.ERR_FAILED,"reject: no active invitation (callId is empty)");const e={...this.L};try{const s=this.B();return(await F.reject({chat:this.N,callId:t,terminalId:s})).errCode,this.i.emit(E.ON_CALL_END,{callInfo:e,reason:exports.CallEndReason.REJECT,userId:this.G()}),{callId:t}}finally{this.M()}}async hangup(){if(!this.O()&&!this.S)throw n(x.ERR_FAILED,"hangup: not logged in. Call login() first and wait for SDK_READY.");if(this.L.status!==exports.CallStatus.CALLING&&this.L.status!==exports.CallStatus.WAITING)throw n(x.ERR_FAILED,`hangup: invalid call status ${this.L.status}; expected CALLING or WAITING`);const t=this.L.callId;if(!t)throw n(x.ERR_FAILED,"hangup: no active call (callId is empty)");const e=this.L.role===exports.CallRole.CALLER&&this.L.status===exports.CallStatus.WAITING,s={...this.L};try{if(e){const e=this.L.inviter||this.G(),s=Array.isArray(this.L.inviteeList)?this.L.inviteeList.slice():[];s.length>0&&(await F.cancel({chat:this.N,inviter:e,callId:t,calleeList:s})).errCode}else{const e=this.B();(await F.hangup({chat:this.N,callId:t,terminalId:e})).errCode}return this.K(),this.i.emit(E.ON_CALL_END,{callInfo:s,reason:e?exports.CallEndReason.CANCELED:exports.CallEndReason.HANGUP,userId:this.G()}),{callId:t}}finally{this.M()}}setLayoutTemplate(t){this.A.setLayoutTemplate(t),this.k()}setLargeViewUser(t){this.A.setLargeViewUser(t),this.k()}toggleViewReversed(){this.A.toggleViewReversed(),this.k()}j(){const t=this.G()||"",e=this.L||{},s=e.inviter||"",i=Array.isArray(e.inviteeList)?e.inviteeList:[],r="number"==typeof e.status?e.status:J.NONE,o=new Set,n=[],a=(t,e)=>{t&&!o.has(t)&&(o.add(t),n.push({id:t,status:e}))};a(t,r),s&&a(s,r);for(const t of i)a(t,r);return{selfId:t,selfStatus:r,mediaType:"number"==typeof e.mediaType?e.mediaType:exports.CallMediaType.VIDEO,allParticipants:n,inviter:s,inviteeList:i}}k(){try{this.A.recalculate(this.j())&&this.i.emit(E.VIEW_LAYOUT_CHANGED,this.A.toJsonObject())}catch(t){}}async openCamera(t,e){let s;if(s=void 0===e?-1:e?1:0,R(this?.m?.startLocalPreview))return this.m.startLocalPreview(t,s)}async closeCamera(){if(R(this?.m?.stopLocalPreview))return this.m.stopLocalPreview()}async switchCamera(t){R(this?.m?.switchCamera)&&await this.m.switchCamera(!!t)}async openMicrophone(){R(this?.m?.startLocalAudio)&&await this.m.startLocalAudio()}async closeMicrophone(){R(this?.m?.stopLocalAudio)&&await this.m.stopLocalAudio()}async selectAudioPlaybackDevice(t){if(!R(this?.m?.setAudioRoute))return;const e=t===exports.AudioPlayBackDevice.EAR||1===t?exports.AudioPlayBackDevice.EAR:exports.AudioPlayBackDevice.SPEAKER;await this.m.setAudioRoute(e)}async startRemoteView(t){if(!this.m||"function"!=typeof this.m.startRemoteView)return;const{userId:e,view:s,streamType:i=0}=t||{};return this.m.startRemoteView(e,s,i)}async stopRemoteView(t){if(!this.m||"function"!=typeof this.m.stopRemoteView)return;const{userId:e,streamType:s=0}=t||{};return this.m.stopRemoteView(e,s)}T(){if(this.I||!this.N||"function"!=typeof this.N.on)return;const t=o("SDK_READY"),e=o("KICKED_OUT"),s=o("USER_SIG_EXPIRED"),i=o("ROOM_CUSTOM_DATA_RECEIVED");this.F=()=>{this.S=1,this.D()},this.W=t=>{this.i.emit(E.KICKED_OUT,t||{})},this.J=()=>{this.i.emit(E.onUserSigExpired,{})},this.Y=t=>{const{Data:e={},Head:s={}}=JSON.parse(t.data);if("call_engine_srv.invite_user_notify"===s?.Command){const{CallInfo:t={}}=e;let s={intRoomId:0,strRoomId:""};if(1===t.RoomIdType)s.intRoomId=+t.RoomId;else{if(2!==t.RoomIdType)return;s.strRoomId=t.RoomId}this.L=O({userIDList:e?.CalleeList_Account||[],callId:e?.CallInfo?.CallId||"",inviter:e?.Caller_Account||"",roomId:s.intRoomId||0,strRoomId:s.strRoomId||"",groupId:e?.CallInfo?.GroupId||"",type:e?.CallInfo?.CallMediaType,status:exports.CallStatus.WAITING,role:exports.CallRole.CALLEE}),this.i.emit(E.ON_CALL_RECEIVED,{callId:this.L.callId,inviter:this.L.inviter,inviteeList:this.L.inviteeList,groupId:this.L.groupId,mediaType:this.L.mediaType,roomId:this.L.roomId,userData:e?.UserData||"",callInfo:{...this.L}})}},t&&this.N.on(t,this.F),e&&this.N.on(e,this.W),s&&this.N.on(s,this.J),i&&this.N.on(i,this.Y),this.I=1}U(){if(!this.I||!this.N||"function"!=typeof this.N.off)return;const t=o("SDK_READY"),e=o("KICKED_OUT"),s=o("USER_SIG_EXPIRED"),i=o("ROOM_CUSTOM_DATA_RECEIVED");try{t&&this.F&&this.N.off(t,this.F)}catch(t){}try{e&&this.W&&this.N.off(e,this.W)}catch(t){}try{s&&this.J&&this.N.off(s,this.J)}catch(t){}try{i&&this.Y&&this.N.off(i,this.Y)}catch(t){}this.I=0}V(){if(this.C||!this.m)return;const t=this.m;if("function"!=typeof t.on)return;const e={},s=t=>(...e)=>{try{this.i.emit(t,...e)}catch(t){}};for(const i of z){let r;if("onRemoteUserEnterRoom"===i){const t=s(i);r=(...e)=>{try{this.L&&this.L.callId&&this.L.status===exports.CallStatus.WAITING&&(this.L.status=exports.CallStatus.CALLING,this.L.callStartTime||(this.L.callStartTime=Date.now()),this.i.emit(E.ON_CALL_BEGIN,{callInfo:{...this.L}}))}catch(t){}t(...e)}}else r=s(i);e[i]=r;try{t.on(i,r)}catch(t){}}this._=e,this.C=1}P(){if(!this.C)return;const t=this.m,e=this._||{};if(t&&"function"==typeof t.off)for(const s of Object.keys(e))try{t.off(s,e[s])}catch(t){}this._=null,this.C=0}D(){this.q||(this.q=1,this.i.emit(E.SDK_READY,{name:"sdk_ready"}),this.$().catch(t=>{}))}async $(){if(!this.N||!this.O())return;if(this.L&&this.L.callId)return;const t=this.G();if(!t)return;let e;try{e=await F.getInvitation({chat:this.N,userId:t})}catch(t){return}if(e&&0===e.errCode&&e.data)try{const t={data:JSON.stringify({Data:e.data,Head:{Command:"call_engine_srv.invite_user_notify"}})};R(this.Y)&&this.Y(t)}catch(t){}}O(){try{if(!this.N)return 0;if("function"==typeof this.N.getLoginUser)return!!this.N.getLoginUser()}catch(t){}return 0}G(){return this.l?this.l:R(this?.N?.getLoginUser)&&this.N.getLoginUser()||""}async H(){try{F.stopHeartBeat()}catch(t){}if(this.L.callType===exports.CallMediaType.UNKNOWN)throw n(-1,"call type is none");const{roomId:t}=this.L;if(0===t.intRoomId&&""===t.strRoomId)throw n(-1,"room id is empty");const e={sdkAppId:this.o,userId:this.G(),userSig:this.u,role:20};t.intRoomId>0?e.roomId=t.intRoomId:e.strRoomId=t.strRoomId;const s=this.L.mediaType===exports.CallMediaType.VIDEO?0:2,i=this.getTRTCCloudInstance();await new Promise((t,r)=>{let o=0,a=null;const c=()=>{a&&(clearTimeout(a),a=null);try{this.i.off("onEnterRoom",h)}catch(t){}},h=e=>{o||(o=1,c(),"number"==typeof e&&e>0?t():r(n("number"==typeof e?e:-1,"enterRoom failed: "+e)))};this.i.on("onEnterRoom",h),a=setTimeout(()=>{o||(o=1,c(),r(n(-1,"enterRoom timeout")))},1e4);try{const t=i.enterRoom(e,s);t&&"function"==typeof t.catch&&t.catch(t=>{o||(o=1,c(),r(t))})}catch(t){if(o)return;o=1,c(),r(t)}})}K(){try{this.m&&"function"==typeof this.m.exitRoom&&this.m.exitRoom()}catch(t){}}M(){try{F.stopHeartBeat()}catch(t){}this.L=O()}B(){return this.R||(this.R=(()=>{const t=Array(16),e="undefined"!=typeof crypto&&crypto||null;if(e&&"function"==typeof e.getRandomValues){const s=new Uint8Array(16);e.getRandomValues(s);for(let e=0;16>e;e++)t[e]=s[e]}else for(let e=0;16>e;e++)t[e]=Math.floor(256*Math.random());const s=t.map(t=>t.toString(16).padStart(2,"0")).join("").toUpperCase();return`${s.slice(0,8)}-${s.slice(8,12)}-${s.slice(12,16)}-${s.slice(16,20)}-${s.slice(20,32)}`})()),this.R}}Z.instance=null,exports.TencentCloudChat=C,exports.CallInvitationRespondedType=_,exports.TUICallEngine=Z,exports.TUICallEvent=E,exports.TUIErrorCode=x,exports.default=Z;
|