goeasy-rtc 0.1.4 → 0.1.8
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/goeasy-rtc.esm.min.js +1 -1
- package/package.json +1 -1
package/goeasy-rtc.esm.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function t(t,e,i,n){return new(i||(i=Promise))((function(s,o){function r(t){try{c(n.next(t))}catch(t){o(t)}}function a(t){try{c(n.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,a)}c((n=n.apply(t,e||[])).next())}))}var e;"function"==typeof SuppressedError&&SuppressedError,function(t){t.RTC_LOST="RTC_LOST",t.RTC_RECONNECT="RTC_RECONNECT",t.RTC_TIMEOUT="RTC_TIMEOUT",t.RTC_ACCEPTED="RTC_ACCEPTED",t.RECEIVED_RTC_REMOTE_FLOW="RECEIVED_RTC_REMOTE_FLOW",t.GOEASY_TIMEOUT="GOEASY_TIMEOUT",t.GOEASY_DISCONNECTED="GOEASY_DISCONNECTED"}(e||(e={}));var i={exports:{}};!function(t){function e(t){if(t)return function(t){for(var i in e.prototype)t[i]=e.prototype[i];return t}(t)}i.exports=e,e.prototype.on=e.prototype.addEventListener=function(t,e){return this._callbacks=this._callbacks||{},(this._callbacks["$"+t]=this._callbacks["$"+t]||[]).push(e),this},e.prototype.once=function(t,e){function i(){this.off(t,i),e.apply(this,arguments)}return i.fn=e,this.on(t,i),this},e.prototype.off=e.prototype.removeListener=e.prototype.removeAllListeners=e.prototype.removeEventListener=function(t,e){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var i,n=this._callbacks["$"+t];if(!n)return this;if(1==arguments.length)return delete this._callbacks["$"+t],this;for(var s=0;s<n.length;s++)if((i=n[s])===e||i.fn===e){n.splice(s,1);break}return this},e.prototype.emit=function(t){this._callbacks=this._callbacks||{};var e=[].slice.call(arguments,1),i=this._callbacks["$"+t];if(i)for(var n=0,s=(i=i.slice(0)).length;n<s;++n)i[n].apply(this,e);return this},e.prototype.listeners=function(t){return this._callbacks=this._callbacks||{},this._callbacks["$"+t]||[]},e.prototype.hasListeners=function(t){return!!this.listeners(t).length}}();var n=i.exports;class s{support(){return!0}getParams(){return this.params}setData(t){this.active=t.a,this.data=t.d}postConnect(){}}const o=n;class r{constructor(){this.emitter=new o}on(t,e){return this.emitter.on(t,e),this}once(t,e){return this.emitter.once(t,e),this}off(t,e){return this.emitter.off(t,e),this}fire(t,e){return this.emitter.emit(t,e),this}}class a{constructor(){this.eventDriver=new r}on(t,e){this.eventDriver.on(t,e)}off(t,e){this.eventDriver.off(t,e)}fire(t,e){this.eventDriver.fire(t,e)}}let c=new class{isDef(t){return!this.isUndef(t)}isUndef(t){return null==t}isPrimitive(t){return"string"==typeof t||"number"==typeof t||"symbol"==typeof t||"boolean"==typeof t}isObject(t){return null!==t&&"object"==typeof t}isPlainObject(t){return"[object Object]"===Object.prototype.toString.call(t)}isRegExp(t){return"[object RegExp]"===Object.prototype.toString.call(t)}isValidArrayIndex(t){let e=parseFloat(String(t));return e>=0&&Math.floor(e)===e&&isFinite(t)}isString(t){return"string"==typeof t}isNumber(t){return"number"==typeof t}isStringOrNumber(t){return this.isString(t)||this.isNumber(t)}isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}isEmpty(t){return this.isArray(t)?0===t.length:this.isObject(t)?!this.isDef(t):!this.isNumber(t)&&(this.isString(t)?""===t.trim():!this.isDef(t))}isNative(t){return"function"==typeof t&&/native code/.test(t.toString())}isFunction(t){return"function"==typeof t}isBoolean(t){return"boolean"==typeof t}isTrue(t){return!0===t}isFalse(t){return!1===t}isNull(t){return null===t}};const l=n;class u{constructor(){this.emitter=new l}on(t,e){if(!c.isString(t))throw Error("event require a string.");if(!c.isFunction(e))throw Error("callback must be a function");this.emitter.on(t,e)}fire(t,e){this.emitter.emit(t,e)}off(t,e){this.emitter.off(t,e)}}class d{static init(t,e,i,n,s,o){this.Socket=t,this.N=e,this.Member=i,this.v=n,this.Platform=s,this.GModules=o}}class h extends a{static init(){this.i=new h}}class E{constructor(){h.i.on(e.RTC_LOST,(()=>this.onRTCLost())),h.i.on(e.RTC_RECONNECT,(()=>this.onRTCReconnect()))}onRTCLost(){this.rtcLostTimeoutId=setTimeout((()=>{h.i.fire(e.RTC_TIMEOUT)}),1e4)}onRTCReconnect(){clearTimeout(this.rtcLostTimeoutId)}onGoEasyLost(){this.goeasyLostTimeoutId=setTimeout((()=>{h.i.fire(e.GOEASY_TIMEOUT)}),2e4)}onGoEasyReconnected(){clearTimeout(this.goeasyLostTimeoutId)}}var C;!function(t){t.RTC_CMD="RTC_CMD",t.RTC_PKT="RTC_PKT"}(C||(C={}));class m{constructor(){if(this.rtcClientPlugin=uni.requireNativePlugin("RTCClientPlugin"),!this.rtcClientPlugin)throw"Please configure GoEasy RTC native plugin first: https://www.goeasy.io"}}var T,p,f,R,g;!function(t){t[t.AUDIO=0]="AUDIO",t[t.VIDEO=1]="VIDEO"}(T||(T={}));class y{constructor(t,e,i){this.name=t,this.callId=e,this.data=i}static createCallCommand(t,e,i){let n={calleeIds:t.callees.map((t=>t.id)),mediaType:i.mediaType,groupId:i.groupId,callId:e};return i.notification&&(n.notification=i.notification),new y(p.CALL,e,n)}}!function(t){t.CALL="CALL",t.QUIT="QUIT",t.BUSY="BUSY",t.ACCEPT="ACCEPT",t.RANG="RANG",t.HANGUP="HANGUP",t.CANCEL="CANCEL",t.REJECT="REJECT",t.RTC_DISCONNECT="RTC_DISCONNECT",t.GET_TOKEN="GET_TOKEN"}(p||(p={})),function(t){t.WRITE="WRITE",t.READ="READ",t.NONE="NONE"}(f||(f={}));class I{constructor(t){this.permission=f.NONE,this.singleTimeout=0,this.totalTimeout=0,this.startTime=0,this.complete=!1,this.retried=0,this.unique=!1,this.name=t.name,this.params=t.params,this.permission=t.permission,this.totalTimeout=t.totalTimeout,this.singleTimeout=t.singleTimeout,t.unique&&(this.unique=t.unique),this.success=e=>{this.complete||(this.end(),t.success(e))},this.fail=e=>{this.complete||(this.end(),t.fail(e))}}start(){this.startTime=Date.now(),this.initAutoTimeout()}end(){this.complete=!0,clearTimeout(this.timeoutHandler)}initAutoTimeout(){this.timeoutHandler=setTimeout((()=>{this.complete||this.fail({resultCode:408,content:"Host unreachable or timeout"})}),this.totalTimeout)}}!function(t){t[t.connect=3e3]="connect",t[t.reconnectionDelayMax=3e3]="reconnectionDelayMax",t[t.commonQuerySingle=2500]="commonQuerySingle",t[t.commonQueryTotal=12e3]="commonQueryTotal",t[t.commonRequestSingle=1700]="commonRequestSingle",t[t.commonRequestTotal=12e3]="commonRequestTotal",t[t.commonInfiniteSingle=1700]="commonInfiniteSingle",t[t.commonInfiniteTotal=864e5]="commonInfiniteTotal"}(R||(R={}));class v extends m{constructor(){super(),this.joined=!1,plus.globalEvent.addEventListener("connectionLost",(()=>{h.i.fire(e.RTC_LOST)})),plus.globalEvent.addEventListener("connectionRecovery",(t=>{h.i.fire(e.RTC_RECONNECT)})),plus.globalEvent.addEventListener("receivedRemoteFlow",(t=>{h.i.fire(e.RECEIVED_RTC_REMOTE_FLOW,t)}))}destroy(){plus.globalEvent.removeEventListener("connectionLost"),plus.globalEvent.removeEventListener("connectionRecovery"),plus.globalEvent.removeEventListener("receivedRemoteFlow"),this.joined&&(this.leave(),this.joined=!1),this.rtcClientPlugin.destroyEngine()}createEngine(e,i){return t(this,void 0,void 0,(function*(){return this.token=yield this.getNewToken(i),this.token.video=e===T.VIDEO,this.token.userId=d.Socket.user().id,this.rtcClientPlugin.createRTCClient({vendor:this.token.vendor,userId:this.token.userId}),this.token.callId}))}join(){return new Promise(((t,e)=>{this.rtcClientPlugin.joinChannel(this.token,(()=>{this.token.userId,this.token.callId,this.joined=!0,t()}),(t=>{this.token.userId,this.token.callId,e({code:400,content:"Failed to join Channel, error code:"+t})}))}))}push(){this.token.video?this.rtcClientPlugin.callVideo():this.rtcClientPlugin.callAudio()}switchViewLevel(t){this.rtcClientPlugin.switchLevel({userId:t})}leave(){this.rtcClientPlugin.leaveChannel()}requestPermission(t){return new Promise(((e,i)=>{T.VIDEO===t?this.rtcClientPlugin.requestVideoCallPermission((t=>{200===t?e():i({code:403,content:"Failed to request video call permission, error code:"+t})})):T.AUDIO===t?this.rtcClientPlugin.requestVoiceCallPermission((t=>{200===t?e():i({code:403,content:"Failed to request voice call permission, error code:"+t})})):i({code:400,content:"Unsupported media type"})}))}accept(){return t(this,void 0,void 0,(function*(){yield this.join(),this.push()}))}getNewToken(e){return t(this,void 0,void 0,(function*(){let t={name:p.GET_TOKEN};return e&&(t.callId=e),new Promise(((e,i)=>{let n=new I({name:C.RTC_CMD,params:t,permission:f.WRITE,singleTimeout:R.commonRequestSingle,totalTimeout:R.commonRequestTotal,success:t=>{e(t.content)},fail:t=>{i(t)}});d.Socket.e(n)}))}))}}class D{constructor(){this.callees=new Array}getDuration(){return Math.floor((Date.now()-this.acceptedAt)/1e3)}}class S{constructor(t,e,i){this.id=t,this.data=e,this.status=i}}class _{constructor(t){this.call=new D,this.cloudRTC=new v,this.call=t}}class N{static init(){this.eventCenter=new u}static on(t,e){this.eventCenter.on(t,e)}static fire(t,e){this.eventCenter.fire(t,e)}static off(t,e){this.eventCenter.off(t,e)}}!function(t){t.RING="RING",t.USER_RANG="USER_RANG",t.USER_QUIT="USER_QUIT",t.USER_ACCEPTED="USER_ACCEPTED",t.CALL_ENDED="CALL_ENDED"}(g||(g={}));class b{static setStatus(t){this.status&&this.status.destroy(),this.status=t,W.callContext.call.status=t.name()}static clearStatus(){this.status.destroy(),this.status=null}}class w extends s{static init(t){t.c(d),this.module=new w,this.module.name=this.RTC_MODULE_NAME;let e=null;if(this.supportApp()){e=uni.requireNativePlugin("RTCClientPlugin").v()}this.module.params={v:{n:"goeasy-rtc",v:"0.1.4",npv:e}},d.GModules.initModule(this.module)}static check(){if(!this.module)throw new Error("Please init GRTC first.");if(!this.module.active)throw new Error("RTC unavailable. Please confirm if your GoEasy application supports RTC or contact GoEasy team.");if(!this.supportApp())throw new Error("GRTC is only supported in Android and iOS apps developed with UniApp");if(!this.supportGoEasyVersion())throw new Error("GRTC is only supported in GoEasy SDK version 2.10.0 or later")}static supportGoEasyVersion(){return d.v.localeCompare("2.10.0",void 0,{numeric:!0,sensitivity:"base"})>=0}static supportApp(){return[d.Platform.type.UNI_ANDROID,d.Platform.type.UNI_IOS].includes(d.Platform.current)}support(){return w.supportGoEasyVersion()&&w.supportApp()}postConnect(){h.init(),N.init(),H.init()}}w.RTC_MODULE_NAME="GRTC";let A=new class{isDef(t){return!this.isUndef(t)}isUndef(t){return null==t}isPrimitive(t){return"string"==typeof t||"number"==typeof t||"symbol"==typeof t||"boolean"==typeof t}isObject(t){return null!==t&&"object"==typeof t}isString(t){return"string"==typeof t}isNumber(t){return"number"==typeof t}isStringOrNumber(t){return this.isString(t)||this.isNumber(t)}isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}isEmpty(t){return this.isArray(t)?0===t.length:this.isObject(t)?!this.isDef(t):!this.isNumber(t)&&(this.isString(t)?""===t.trim():!this.isDef(t))}isNative(t){return"function"==typeof t&&/native code/.test(t.toString())}isFunction(t){return"function"==typeof t}};const O=new class{validateId(t,e){if(A.isEmpty(t))throw{code:400,content:` ${e} is required.`};if(!A.isStringOrNumber(t))throw{code:400,content:`TypeError: ${e} require string or number.`}}validateNotification(t){function e(t,e,i){if(!(A.isString(t[e])&&t[e].length<=i))throw{code:400,content:`notification.${e} must be a string of no more than ${i} characters`}}function i(t,e,i,n){let s=t[e];if(A.isObject(s)&&A.isDef(s[i])){let t={code:400,content:`notification.vendorOptions.${e}.${i} require a ${n}}`},o=s[i];if("string"===n&&!A.isString(o))throw t;if("number"===n&&!A.isNumber(o))throw t}}if(!A.isObject(t))throw{code:400,content:"TypeError: notification requires an object."};{if(e(t,"title",32),e(t,"body",50),A.isDef(t.sound)&&!A.isString(t.sound))throw{code:400,content:"notification.sound must be a string"};if(A.isDef(t.badge)&&!A.isString(t.badge))throw{code:400,content:"notification.badge must be a string"};t.badge=t.badge||"0";let n=t.vendorOptions;A.isObject(n)&&(i(n,"huawei","category","string"),i(n,"xiaomi","channel_id","string"),i(n,"oppo","channel_id","string"),i(n,"vivo","classification","number"),i(n,"vivo","category","string"))}}validateObject(t,e){if(A.isUndef(t)||!A.isObject(t))throw{code:400,content:e+" must be an object."}}validateString(t,e){if(A.isUndef(t)||!A.isString(t))throw{code:400,content:e+" must be a string."}}};var U,P;!function(t){t.ALL_BUSY="ALL_BUSY",t.DIAL_FAILED="DIAL_FAILED",t.CANCELLED="CANCELLED",t.REJECTED="REJECTED",t.HUNG_UP="HUNG_UP",t.RTC_DISCONNECTED="RTC_DISCONNECTED",t.GOEASY_DISCONNECTED="GOEASY_DISCONNECTED",t.HANDLED_ON_ANOTHER_DEVICE="HANDLED_ON_ANOTHER_DEVICE",t.RING_TIMEOUT="RING_TIMEOUT",t.ACCEPT_FAILED="ACCEPT_FAILED",t.DIAL_TIMEOUT="DIAL_TIMEOUT"}(U||(U={}));class L{onUserRang(e){return t(this,void 0,void 0,(function*(){W.updateMemberStatus(e.callerId,"Ringing");const t=yield d.Member.i.getData(e.callerId),i={id:e.callerId,data:t.get(e.callerId)};N.fire(g.USER_RANG,{user:i})}))}onUserQuit(e){return t(this,void 0,void 0,(function*(){W.updateMemberStatus(e.userId,"Quit");const t=yield d.Member.i.getData(e.userId),i={id:e.userId,data:t.get(e.userId)};!0===e.ended?W.destroyWithEvent(e.reason,i):N.fire(g.USER_QUIT,{user:i,reason:e.reason})}))}onRTCTimeout(){}onGoEasyDisconnected(){W.destroyWithEvent(U.GOEASY_DISCONNECTED)}destroy(){}}class G{static sendRequest(t){return new Promise(((e,i)=>{let n=new I({name:C.RTC_CMD,params:t,permission:f.WRITE,singleTimeout:R.commonRequestSingle,totalTimeout:R.commonRequestTotal,success:t=>{e(t)},fail:t=>{i({code:400,content:t.content})}});d.Socket.e(n)}))}}class k extends L{constructor(){super(),W.callContext.call.acceptedAt=Date.now()}name(){return"InCall"}accept(){return Promise.reject("in call state can not accept")}end(){const t=new y(p.HANGUP,W.callContext.call.id);G.sendRequest(t),W.destroyWithEvent(U.HUNG_UP)}onRTCTimeout(){const t=new y(p.RTC_DISCONNECT,W.callContext.call.id);G.sendRequest(t),W.destroyWithEvent(U.RTC_DISCONNECTED)}onRTCRemoteFlow(e){return t(this,void 0,void 0,(function*(){const t=W.callContext.call;W.updateMemberStatus(e.userId,t.status);const i=yield d.Member.i.getData(e.userId),n={id:e.userId,data:i.get(e.userId)};N.fire(g.USER_ACCEPTED,{user:n})}))}onUserAccepted(t){}}class M extends L{constructor(){super(),this.dialTimeoutId=setTimeout((()=>{W.destroyWithEvent(U.DIAL_TIMEOUT)}),45e3)}name(){return"Dialing"}accept(){return Promise.reject("dialing state can not accept")}end(){const t=new y(p.CANCEL,W.callContext.call.id);G.sendRequest(t),W.destroyWithEvent(U.CANCELLED)}onRTCRemoteFlow(e){return t(this,void 0,void 0,(function*(){b.setStatus(new k),W.callContext.cloudRTC.push();const t=W.callContext.call;t.caller.status=t.status,W.updateMemberStatus(e.userId,t.status);const i=yield d.Member.i.getData(e.userId),n={id:e.userId,data:i.get(e.userId)};N.fire(g.USER_ACCEPTED,{user:n})}))}onUserAccepted(t){t.userId,d.Socket.user().id}destroy(){clearTimeout(this.dialTimeoutId)}}class x extends L{name(){return"Ringing"}constructor(){super();const t=Date.now()-W.callContext.call.time;t>0&&(this.ringTimeoutId=setTimeout((()=>{W.destroyWithEvent(U.RING_TIMEOUT)}),45e3-t))}accept(){return new Promise(((i,n)=>t(this,void 0,void 0,(function*(){try{const t=n=>{b.setStatus(new k);let s=W.callContext.call;"Ringing"===s.caller.status&&(s.caller.status=s.status),W.updateMemberStatus(d.Socket.user().id,s.status),W.updateMemberStatus(n.userId,s.status),h.i.off(e.RTC_ACCEPTED,t),i()};h.i.on(e.RTC_ACCEPTED,t),yield W.callContext.cloudRTC.join(),W.callContext.cloudRTC.push();const n=new y(p.ACCEPT,W.callContext.call.id);G.sendRequest(n)}catch(t){W.destroyWithEvent(U.ACCEPT_FAILED),console.warn("accepte failed:",t),n(t)}}))))}onUserAccepted(t){t.userId===d.Socket.user().id&&W.destroyWithEvent(U.HANDLED_ON_ANOTHER_DEVICE)}end(){const t=new y(p.REJECT,W.callContext.call.id);G.sendRequest(t),W.destroyWithEvent(U.REJECTED)}onRTCRemoteFlow(t){h.i.fire(e.RTC_ACCEPTED,t)}onUserRang(e){return t(this,void 0,void 0,(function*(){}))}destroy(){clearTimeout(this.ringTimeoutId)}}class j{static initNotificationAssembler(){d.N.addAssembler({assemble:t=>({callId:t.callId}),support:t=>!!t.callId})}static createLocalNotification(t){const e=d.N.supportNotification();let i=t.notification;if(!A.isObject(i)||!e)return;let n={callId:t.callId};d.N.createLocalNotification(i.title,i.body,n,i.sound,i.badge)}}!function(t){t.RING="RING",t.USER_RANG="U_RANG",t.USER_QUIT="U_QUIT",t.USER_ACCEPTED="U_ACCEPTED",t.ENDED="ENDED"}(P||(P={}));class q{onServerPacket(t,e){switch(t){case P.RING:this.onRing(e);break;case P.USER_RANG:this.onUserRang(e);break;case P.USER_ACCEPTED:this.onUserAccepted(e);break;case P.USER_QUIT:this.onUserQuit(e)}}}class F extends q{name(){return"Busy"}accept(){return b.status.accept()}end(){b.status.end(),Q.setStatus(new V)}call(t){return Promise.reject("Another call is in progress.")}onRing(t){const e=new y(p.BUSY,t.id);G.sendRequest(e)}onUserAccepted(t){b.status.onUserAccepted(t)}onUserRang(t){b.status.onUserRang(t)}onUserQuit(t){b.status.onUserQuit(t)}onRTCTimeout(){b.status.onRTCTimeout()}onRTCRemoteFlow(t){b.status.onRTCRemoteFlow(t)}onGoEasyDisconnected(){b.status.onGoEasyDisconnected()}}class V extends q{name(){return"Idle"}call(e){if(w.check(),O.validateNotification(e.notification),!d.Socket.user().id)throw new Error("Dialing failed: `id` is required when connecting to GoEasy.");return new Promise(((i,n)=>t(this,void 0,void 0,(function*(){try{Q.setStatus(new F),"calleeIds"in e?W.initGroupCall(e):W.initPrivateCall(e),b.setStatus(new M);const t=W.callContext.cloudRTC;yield t.requestPermission(e.mediaType);const s=yield t.createEngine(e.mediaType);t.join();const o=W.callContext.call;o.id=s;const r=b.status.name();o.status=r;const a="calleeIds"in e?e.calleeIds:[e.calleeId],c=yield d.Member.i.getData(...a);o.callees=a.map((t=>new S(t,c.get(t),"Ringing")));const l=y.createCallCommand(o,s,e);const u=(yield G.sendRequest(l)).content.users;o.callees=o.callees.filter((t=>!u.find((e=>e.userId===t.id&&e.busy))));u.every((t=>t.busy))?(W.destroy(),n({code:400,content:"all callees are busy"})):i()}catch(t){W.destroy(),console.warn("Call failed",t),n(t)}}))))}onRing(e){return t(this,void 0,void 0,(function*(){try{Q.setStatus(new F),W.ring(e),b.setStatus(new x),j.createLocalNotification(e);const t=b.status.name(),i=W.callContext.call;i.status=t;const n=e.users.map((t=>t.id)),s=yield d.Member.i.getData(e.callerId,...n);i.caller=new S(e.callerId,s.get(e.callerId),t),i.callees=e.users.map((e=>new S(e.id,s.get(e.id),t)));const o=W.callContext.cloudRTC;o.requestPermission(e.type),yield o.createEngine(e.type,e.callId);const r=new y(p.RANG,i.id);G.sendRequest(r);const a=yield d.Member.i.getData(e.callerId),c={id:e.callerId,data:a.get(e.callerId)};N.fire(g.RING,{user:c})}catch(t){console.warn("Ring failed",t),W.destroy()}}))}accept(){return Promise.reject("idle state can not accept")}end(){}onUserRang(t){}onUserAccepted(t){}onUserQuit(t){}onRTCTimeout(){}onRTCRemoteFlow(t){}onGoEasyDisconnected(){}}class Q{static setStatus(t){this.status=t}}Q.status=new V;class W{static ring(t){const e=new D;e.id=t.callId,e.time=t.time,e.mediaType=t.type,e.groupId=t.groupId,this.callContext=new _(e)}static initPrivateCall(t){const e=new D;e.mediaType=t.mediaType,e.caller=new S(d.Socket.user().id,d.Socket.user().data,"Dialing"),this.callContext=new _(e)}static initGroupCall(t){if(t.calleeIds.length>8)throw new Error("calleeIds can not more than 8");const e=new D;e.groupId=t.groupId,e.mediaType=t.mediaType,e.caller=new S(d.Socket.user().id,d.Socket.user().data,"Dialing"),this.callContext=new _(e)}static updateMemberStatus(t,e){const i=this.callContext.call;[...i.callees,i.caller].find((e=>e.id===t)).status=e}static isCaller(){return this.callContext.call.caller.id===d.Socket.user().id}static destroyWithEvent(t,e=d.Socket.user()){N.fire(g.USER_QUIT,{user:e,reason:t}),N.fire(g.CALL_ENDED),this.destroy()}static destroy(){this.callContext.cloudRTC.destroy(),this.callContext=null,b.clearStatus(),Q.setStatus(new V)}}class H{static init(){this.instance=new H}static i(){if(this.instance)return this.instance;throw new Error("Please connect GoEasy first.")}constructor(){this.rtcStabilizer=new E,j.initNotificationAssembler(),d.Socket.onMessage(C.RTC_PKT,this.onServerPacket.bind(this)),h.i.on(e.RECEIVED_RTC_REMOTE_FLOW,this.onRTCRemoteFlow.bind(this)),h.i.on(e.RTC_TIMEOUT,this.onRTCTimeout.bind(this)),d.Socket.on(d.Socket.EVENT.LOST,this.onGoEasyDisconnected.bind(this)),d.Socket.on(d.Socket.EVENT.DISCONNECT,this.onGoEasyDisconnected.bind(this))}on(t,e){N.on(t.toString(),e)}off(t,e){N.off(t.toString(),e)}call(e){return t(this,void 0,void 0,(function*(){return Q.status.call(e)}))}groupCall(e){return t(this,void 0,void 0,(function*(){return Q.status.call(e)}))}accept(){return Q.status.accept()}getCurrentCall(){return W.callContext.call}end(){Q.status.end()}onServerPacket(t){Q.status.onServerPacket(t.name,t.data)}onRTCTimeout(){Q.status.onRTCTimeout()}onRTCRemoteFlow(t){Q.status.onRTCRemoteFlow(t)}onGoEasyDisconnected(){Q.status.onGoEasyDisconnected()}}class Y{static init(t){w.init(t)}static call(t){return H.i().call(t)}static groupCall(t){return H.i().groupCall(t)}static accept(){return H.i().accept()}static currentCall(){return H.i().getCurrentCall()}static end(){H.i().end()}static on(t,e){H.i().on(t,e)}static off(t,e){H.i().off(t,e)}}Y.EVENT=g;export{Y as default};
|
|
1
|
+
function t(t,e,i,s){return new(i||(i=Promise))((function(n,o){function r(t){try{c(s.next(t))}catch(t){o(t)}}function a(t){try{c(s.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(r,a)}c((s=s.apply(t,e||[])).next())}))}var e;"function"==typeof SuppressedError&&SuppressedError,function(t){t.RTC_LOST="RTC_LOST",t.RTC_RECONNECT="RTC_RECONNECT",t.RTC_TIMEOUT="RTC_TIMEOUT",t.RTC_ACCEPTED="RTC_ACCEPTED",t.RECEIVED_RTC_REMOTE_FLOW="RECEIVED_RTC_REMOTE_FLOW",t.GOEASY_TIMEOUT="GOEASY_TIMEOUT",t.GOEASY_DISCONNECTED="GOEASY_DISCONNECTED"}(e||(e={}));var i={exports:{}};!function(t){function e(t){if(t)return function(t){for(var i in e.prototype)t[i]=e.prototype[i];return t}(t)}i.exports=e,e.prototype.on=e.prototype.addEventListener=function(t,e){return this._callbacks=this._callbacks||{},(this._callbacks["$"+t]=this._callbacks["$"+t]||[]).push(e),this},e.prototype.once=function(t,e){function i(){this.off(t,i),e.apply(this,arguments)}return i.fn=e,this.on(t,i),this},e.prototype.off=e.prototype.removeListener=e.prototype.removeAllListeners=e.prototype.removeEventListener=function(t,e){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var i,s=this._callbacks["$"+t];if(!s)return this;if(1==arguments.length)return delete this._callbacks["$"+t],this;for(var n=0;n<s.length;n++)if((i=s[n])===e||i.fn===e){s.splice(n,1);break}return this},e.prototype.emit=function(t){this._callbacks=this._callbacks||{};var e=[].slice.call(arguments,1),i=this._callbacks["$"+t];if(i)for(var s=0,n=(i=i.slice(0)).length;s<n;++s)i[s].apply(this,e);return this},e.prototype.listeners=function(t){return this._callbacks=this._callbacks||{},this._callbacks["$"+t]||[]},e.prototype.hasListeners=function(t){return!!this.listeners(t).length}}();var s=i.exports,n={exports:{}},o="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)||"undefined"!=typeof msCrypto&&"function"==typeof window.msCrypto.getRandomValues&&msCrypto.getRandomValues.bind(msCrypto);if(o){var r=new Uint8Array(16);n.exports=function(){return o(r),r}}else{var a=new Array(16);n.exports=function(){for(var t,e=0;e<16;e++)0==(3&e)&&(t=4294967296*Math.random()),a[e]=t>>>((3&e)<<3)&255;return a}}for(var c=[],l=0;l<256;++l)c[l]=(l+256).toString(16).substr(1);var u,d,h=function(t,e){var i=e||0,s=c;return[s[t[i++]],s[t[i++]],s[t[i++]],s[t[i++]],"-",s[t[i++]],s[t[i++]],"-",s[t[i++]],s[t[i++]],"-",s[t[i++]],s[t[i++]],"-",s[t[i++]],s[t[i++]],s[t[i++]],s[t[i++]],s[t[i++]],s[t[i++]]].join("")},E=n.exports,p=h,C=0,m=0;var T=function(t,e,i){var s=e&&i||0,n=e||[],o=(t=t||{}).node||u,r=void 0!==t.clockseq?t.clockseq:d;if(null==o||null==r){var a=E();null==o&&(o=u=[1|a[0],a[1],a[2],a[3],a[4],a[5]]),null==r&&(r=d=16383&(a[6]<<8|a[7]))}var c=void 0!==t.msecs?t.msecs:(new Date).getTime(),l=void 0!==t.nsecs?t.nsecs:m+1,h=c-C+(l-m)/1e4;if(h<0&&void 0===t.clockseq&&(r=r+1&16383),(h<0||c>C)&&void 0===t.nsecs&&(l=0),l>=1e4)throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");C=c,m=l,d=r;var T=(1e4*(268435455&(c+=122192928e5))+l)%4294967296;n[s++]=T>>>24&255,n[s++]=T>>>16&255,n[s++]=T>>>8&255,n[s++]=255&T;var f=c/4294967296*1e4&268435455;n[s++]=f>>>8&255,n[s++]=255&f,n[s++]=f>>>24&15|16,n[s++]=f>>>16&255,n[s++]=r>>>8|128,n[s++]=255&r;for(var R=0;R<6;++R)n[s+R]=o[R];return e||p(n)},f=n.exports,R=h;var y=function(t,e,i){var s=e&&i||0;"string"==typeof t&&(e="binary"===t?new Array(16):null,t=null);var n=(t=t||{}).random||(t.rng||f)();if(n[6]=15&n[6]|64,n[8]=63&n[8]|128,e)for(var o=0;o<16;++o)e[s+o]=n[o];return e||R(n)},g=T,v=y,I=v;I.v1=g,I.v4=v;var D=I;class S{support(){return!0}getParams(){return this.params}setData(t){this.active=t.a,this.data=t.d}preConnect(t){}postConnect(){}}const _=s;class b{constructor(){this.emitter=new _}on(t,e){return this.emitter.on(t,e),this}once(t,e){return this.emitter.once(t,e),this}off(t,e){return this.emitter.off(t,e),this}fire(t,e){return this.emitter.emit(t,e),this}}class A{constructor(){this.eventDriver=new b}on(t,e){this.eventDriver.on(t,e)}off(t,e){this.eventDriver.off(t,e)}fire(t,e){this.eventDriver.fire(t,e)}}let N=new class{isDef(t){return!this.isUndef(t)}isUndef(t){return null==t}isPrimitive(t){return"string"==typeof t||"number"==typeof t||"symbol"==typeof t||"boolean"==typeof t}isObject(t){return null!==t&&"object"==typeof t}isPlainObject(t){return"[object Object]"===Object.prototype.toString.call(t)}isRegExp(t){return"[object RegExp]"===Object.prototype.toString.call(t)}isValidArrayIndex(t){let e=parseFloat(String(t));return e>=0&&Math.floor(e)===e&&isFinite(t)}isString(t){return"string"==typeof t}isNumber(t){return"number"==typeof t}isStringOrNumber(t){return this.isString(t)||this.isNumber(t)}isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}isEmpty(t){return this.isArray(t)?0===t.length:this.isObject(t)?!this.isDef(t):!this.isNumber(t)&&(this.isString(t)?""===t.trim():!this.isDef(t))}isNative(t){return"function"==typeof t&&/native code/.test(t.toString())}isFunction(t){return"function"==typeof t}isBoolean(t){return"boolean"==typeof t}isTrue(t){return!0===t}isFalse(t){return!1===t}isNull(t){return null===t}};const w=s;class U{constructor(){this.emitter=new w}on(t,e){if(!N.isString(t))throw Error("event require a string.");if(!N.isFunction(e))throw Error("callback must be a function");this.emitter.on(t,e)}fire(t,e){this.emitter.emit(t,e)}off(t,e){this.emitter.off(t,e)}}class O{static init(t,e,i,s,n,o){this.Socket=t,this.N=e,this.Member=i,this.v=s,this.Platform=n,this.GModules=o}}const P=D;class L{static get(){return P.v1().replace(/-/g,"")}}var k,G,M;(G=k||(k={})).WRITE="WRITE",G.READ="READ",G.NONE="NONE";class x{constructor(t){this.permission=k.NONE,this.singleTimeout=0,this.totalTimeout=0,this.startTime=0,this.complete=!1,this.retried=0,this.unique=!1,this.uuid=L.get(),this.name=t.name,this.params=t.params,this.permission=t.permission,this.totalTimeout=t.totalTimeout,this.singleTimeout=t.singleTimeout,t.unique&&(this.unique=t.unique),this.success=e=>{this.complete||(this.end(),t.success(e))},this.fail=e=>{this.complete||(this.end(),t.fail(e))}}start(){this.startTime=Date.now(),this.initAutoTimeout()}end(){this.complete=!0,clearTimeout(this.timeoutHandler)}initAutoTimeout(){this.timeoutHandler=setTimeout((()=>{this.complete||this.fail({resultCode:408,content:"Host unreachable or timeout"})}),this.totalTimeout)}}class j extends A{static init(){this.i=new j}}class q{constructor(){j.i.on(e.RTC_LOST,(()=>this.onRTCLost())),j.i.on(e.RTC_RECONNECT,(()=>this.onRTCReconnect()))}onRTCLost(){this.rtcLostTimeoutId=setTimeout((()=>{j.i.fire(e.RTC_TIMEOUT)}),1e4)}onRTCReconnect(){clearTimeout(this.rtcLostTimeoutId)}onGoEasyLost(){this.goeasyLostTimeoutId=setTimeout((()=>{j.i.fire(e.GOEASY_TIMEOUT)}),2e4)}onGoEasyReconnected(){clearTimeout(this.goeasyLostTimeoutId)}}!function(t){t.RTC_CMD="RTC_CMD",t.RTC_PKT="RTC_PKT"}(M||(M={}));class F{constructor(){if(this.rtcClientPlugin=uni.requireNativePlugin("RTCClientPlugin"),!this.rtcClientPlugin)throw"Please configure GoEasy RTC native plugin first: https://www.goeasy.io"}}var V,W,Q,H,Y;!function(t){t[t.AUDIO=0]="AUDIO",t[t.VIDEO=1]="VIDEO"}(V||(V={}));class ${constructor(t,e,i){this.name=t,this.callId=e,this.data=i}static createCallCommand(t,e,i){let s={calleeIds:t.callees.map((t=>t.id)),mediaType:i.mediaType,groupId:i.groupId,callId:e};return i.notification&&(s.notification=i.notification),new $(W.CALL,e,s)}}!function(t){t.CALL="CALL",t.QUIT="QUIT",t.BUSY="BUSY",t.ACCEPT="ACCEPT",t.RANG="RANG",t.HANGUP="HANGUP",t.CANCEL="CANCEL",t.REJECT="REJECT",t.RTC_DISCONNECT="RTC_DISCONNECT",t.GET_TOKEN="GET_TOKEN"}(W||(W={})),function(t){t[t.connect=3e3]="connect",t[t.reconnectionDelayMax=3e3]="reconnectionDelayMax",t[t.commonQuerySingle=2500]="commonQuerySingle",t[t.commonQueryTotal=12e3]="commonQueryTotal",t[t.commonRequestSingle=1700]="commonRequestSingle",t[t.commonRequestTotal=12e3]="commonRequestTotal",t[t.commonInfiniteSingle=1700]="commonInfiniteSingle",t[t.commonInfiniteTotal=864e5]="commonInfiniteTotal"}(Q||(Q={}));class K extends F{constructor(){super(),this.joined=!1,plus.globalEvent.addEventListener("connectionLost",(()=>{j.i.fire(e.RTC_LOST)})),plus.globalEvent.addEventListener("connectionRecovery",(t=>{j.i.fire(e.RTC_RECONNECT)})),plus.globalEvent.addEventListener("receivedRemoteFlow",(t=>{j.i.fire(e.RECEIVED_RTC_REMOTE_FLOW,t)}))}destroy(){plus.globalEvent.removeEventListener("connectionLost"),plus.globalEvent.removeEventListener("connectionRecovery"),plus.globalEvent.removeEventListener("receivedRemoteFlow"),this.joined&&(this.leave(),this.joined=!1),this.rtcClientPlugin.destroyEngine()}createEngine(e,i){return t(this,void 0,void 0,(function*(){return this.token=yield this.getNewToken(i),this.token.video=e===V.VIDEO,this.token.userId=O.Socket.user().id,this.rtcClientPlugin.createRTCClient({vendor:this.token.vendor,userId:this.token.userId}),this.token.callId}))}join(){return new Promise(((t,e)=>{this.rtcClientPlugin.joinChannel(this.token,(()=>{this.token.userId,this.token.callId,this.joined=!0,t()}),(t=>{this.token.userId,this.token.callId,e({code:400,content:"Failed to join Channel, error code:"+t})}))}))}push(){this.token.video?this.rtcClientPlugin.callVideo():this.rtcClientPlugin.callAudio()}switchViewLevel(t){this.rtcClientPlugin.switchLevel({userId:t})}leave(){this.rtcClientPlugin.leaveChannel()}requestPermission(t){return new Promise(((e,i)=>{V.VIDEO===t?this.rtcClientPlugin.requestVideoCallPermission((t=>{200===t?e():i({code:403,content:"Failed to request video call permission, error code:"+t})})):V.AUDIO===t?this.rtcClientPlugin.requestVoiceCallPermission((t=>{200===t?e():i({code:403,content:"Failed to request voice call permission, error code:"+t})})):i({code:400,content:"Unsupported media type"})}))}accept(){return t(this,void 0,void 0,(function*(){yield this.join(),this.push()}))}getNewToken(e){return t(this,void 0,void 0,(function*(){let t={name:W.GET_TOKEN};return e&&(t.callId=e),new Promise(((e,i)=>{let s=new x({name:M.RTC_CMD,params:t,permission:k.WRITE,singleTimeout:Q.commonRequestSingle,totalTimeout:Q.commonRequestTotal,success:t=>{e(t.content)},fail:t=>{i(t)}});O.Socket.e(s)}))}))}}class B{static setStatus(t){this.status&&this.status.destroy(),this.status=t,Ct.callContext.call.status=t.name()}static clearStatus(){this.status.destroy(),this.status=null}}!function(t){t.RING="RING",t.USER_RANG="U_RANG",t.USER_QUIT="U_QUIT",t.USER_ACCEPTED="U_ACCEPTED",t.ENDED="ENDED"}(H||(H={}));class J{constructor(){this.callees=new Array}getDuration(){return Math.floor((Date.now()-this.acceptedAt)/1e3)}}class z{constructor(t,e,i){this.id=t,this.data=e,this.status=i}}class X{constructor(t){this.call=new J,this.cloudRTC=new K,this.acceptedUsers=new Z,this.call=t}}class Z{constructor(){this.acceptedUsers=new Map,this.onRTCRemoteFlow=t=>{this.updateUserStatus(t.userId,{remoteFlow:!0}),this.tryFireUserAccepted(t.userId)},this.onUserAcceptPacket=t=>{t.name===H.USER_ACCEPTED&&(this.updateUserStatus(t.data.userId,{acceptPacket:!0}),this.tryFireUserAccepted(t.data.userId))},O.Socket.onMessage(M.RTC_PKT,this.onUserAcceptPacket),j.i.on(e.RECEIVED_RTC_REMOTE_FLOW,this.onRTCRemoteFlow)}updateUserStatus(t,e){let i;const s=this.acceptedUsers;i=s.has(t)?s.get(t):{acceptPacket:!1,remoteFlow:!1},s.set(t,Object.assign(Object.assign({},i),e))}tryFireUserAccepted(t){const e=this.acceptedUsers;if(e.has(t)){const i=e.get(t);i.remoteFlow&&i.acceptPacket&&(e.delete(t),B.status.onUserAccepted({userId:t}))}}destroy(){O.Socket.offMessage(M.RTC_PKT,this.onUserAcceptPacket),j.i.off(e.RECEIVED_RTC_REMOTE_FLOW,this.onRTCRemoteFlow)}}class tt{static init(){this.eventCenter=new U}static on(t,e){this.eventCenter.on(t,e)}static fire(t,e){this.eventCenter.fire(t,e)}static off(t,e){this.eventCenter.off(t,e)}}!function(t){t.RING="RING",t.USER_RANG="USER_RANG",t.USER_QUIT="USER_QUIT",t.USER_ACCEPTED="USER_ACCEPTED",t.CALL_ENDED="CALL_ENDED"}(Y||(Y={}));class et extends S{static init(t){t.c(O),this.module=new et,this.module.name=this.RTC_MODULE_NAME;let e=null;if(this.supportApp()){let t=uni.requireNativePlugin("RTCClientPlugin");t&&(e=t.v())}this.module.params={v:{n:"goeasy-rtc",v:"0.1.8",npv:e}},O.GModules.initModule(this.module)}static check(){if(!this.module)throw new Error("Please init GRTC first.");if(!this.module.active)throw new Error("RTC unavailable. Please confirm if your GoEasy application supports RTC or contact GoEasy team.");if(!this.supportApp())throw new Error("GRTC is only supported in Android and iOS apps developed with UniApp");if(!this.supportGoEasyVersion())throw new Error("GRTC is only supported in GoEasy SDK version 2.10.0 or later")}static supportGoEasyVersion(){return O.v.localeCompare("2.10.0",void 0,{numeric:!0,sensitivity:"base"})>=0}static supportApp(){return[O.Platform.type.UNI_ANDROID,O.Platform.type.UNI_IOS].includes(O.Platform.current)}support(){return et.supportGoEasyVersion()&&et.supportApp()}postConnect(){j.init(),tt.init(),mt.init()}}et.RTC_MODULE_NAME="GRTC";let it=new class{isDef(t){return!this.isUndef(t)}isUndef(t){return null==t}isPrimitive(t){return"string"==typeof t||"number"==typeof t||"symbol"==typeof t||"boolean"==typeof t}isObject(t){return null!==t&&"object"==typeof t}isString(t){return"string"==typeof t}isNumber(t){return"number"==typeof t}isStringOrNumber(t){return this.isString(t)||this.isNumber(t)}isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}isEmpty(t){return this.isArray(t)?0===t.length:this.isObject(t)?!this.isDef(t):!this.isNumber(t)&&(this.isString(t)?""===t.trim():!this.isDef(t))}isNative(t){return"function"==typeof t&&/native code/.test(t.toString())}isFunction(t){return"function"==typeof t}};const st=new class{validateId(t,e){if(it.isEmpty(t))throw{code:400,content:` ${e} is required.`};if(!it.isStringOrNumber(t))throw{code:400,content:`TypeError: ${e} require string or number.`}}validateNotification(t){function e(t,e,i){if(!(it.isString(t[e])&&t[e].length<=i))throw{code:400,content:`notification.${e} must be a string of no more than ${i} characters`}}function i(t,e,i,s){let n=t[e];if(it.isObject(n)&&it.isDef(n[i])){let t={code:400,content:`notification.vendorOptions.${e}.${i} require a ${s}}`},o=n[i];if("string"===s&&!it.isString(o))throw t;if("number"===s&&!it.isNumber(o))throw t}}if(!it.isObject(t))throw{code:400,content:"TypeError: notification requires an object."};{if(e(t,"title",32),e(t,"body",50),it.isDef(t.sound)&&!it.isString(t.sound))throw{code:400,content:"notification.sound must be a string"};if(it.isDef(t.badge)&&!it.isString(t.badge))throw{code:400,content:"notification.badge must be a string"};t.badge=t.badge||"0";let s=t.vendorOptions;it.isObject(s)&&(i(s,"huawei","category","string"),i(s,"xiaomi","channel_id","string"),i(s,"oppo","channel_id","string"),i(s,"vivo","classification","number"),i(s,"vivo","category","string"))}}validateObject(t,e){if(it.isUndef(t)||!it.isObject(t))throw{code:400,content:e+" must be an object."}}validateString(t,e){if(it.isUndef(t)||!it.isString(t))throw{code:400,content:e+" must be a string."}}};var nt;!function(t){t.ALL_BUSY="ALL_BUSY",t.DIAL_FAILED="DIAL_FAILED",t.CANCELLED="CANCELLED",t.REJECTED="REJECTED",t.HUNG_UP="HUNG_UP",t.RTC_DISCONNECTED="RTC_DISCONNECTED",t.GOEASY_DISCONNECTED="GOEASY_DISCONNECTED",t.HANDLED_ON_ANOTHER_DEVICE="HANDLED_ON_ANOTHER_DEVICE",t.RING_TIMEOUT="RING_TIMEOUT",t.ACCEPT_FAILED="ACCEPT_FAILED",t.DIAL_TIMEOUT="DIAL_TIMEOUT"}(nt||(nt={}));class ot{onRTCRemoteFlow(t){}onUserAcceptPacket(t){}onUserRang(e){return t(this,void 0,void 0,(function*(){Ct.updateMemberStatus(e.callerId,"Ringing");const t=yield O.Member.i.getData(e.callerId),i={id:e.callerId,data:t.get(e.callerId)};tt.fire(Y.USER_RANG,{user:i})}))}onUserQuit(e){return t(this,void 0,void 0,(function*(){Ct.updateMemberStatus(e.userId,"Quit");const t=yield O.Member.i.getData(e.userId),i={id:e.userId,data:t.get(e.userId)};e.userId===O.Socket.user().id?Ct.destroyWithEvent(nt.HANDLED_ON_ANOTHER_DEVICE):!0===e.ended?Ct.destroyWithEvent(e.reason,i):tt.fire(Y.USER_QUIT,{user:i,reason:e.reason})}))}onRTCTimeout(){}onGoEasyDisconnected(){Ct.destroyWithEvent(nt.GOEASY_DISCONNECTED)}destroy(){}}class rt{static sendRequest(t){return new Promise(((e,i)=>{let s=new x({name:M.RTC_CMD,params:t,permission:k.WRITE,singleTimeout:Q.commonRequestSingle,totalTimeout:Q.commonRequestTotal,success:t=>{e(t)},fail:t=>{i({code:400,content:t.content})}});O.Socket.e(s)}))}}class at extends ot{constructor(){super(),Ct.callContext.call.acceptedAt=Date.now()}name(){return"InCall"}accept(){return Promise.reject("The call has already been accepted")}end(){const t=new $(W.HANGUP,Ct.callContext.call.id);rt.sendRequest(t),Ct.destroyWithEvent(nt.HUNG_UP)}onRTCTimeout(){const t=new $(W.RTC_DISCONNECT,Ct.callContext.call.id);rt.sendRequest(t),Ct.destroyWithEvent(nt.RTC_DISCONNECTED)}onUserAccepted(e){return t(this,void 0,void 0,(function*(){const t=Ct.callContext.call;Ct.updateMemberStatus(e.userId,t.status);const i=yield O.Member.i.getData(e.userId),s={id:e.userId,data:i.get(e.userId)};tt.fire(Y.USER_ACCEPTED,{user:s})}))}}class ct extends ot{constructor(){super(),this.dialTimeoutId=setTimeout((()=>{Ct.destroyWithEvent(nt.DIAL_TIMEOUT)}),45e3)}name(){return"Dialing"}accept(){return Promise.reject("Cannot accept the call during dialing.")}end(){const t=new $(W.CANCEL,Ct.callContext.call.id);rt.sendRequest(t),Ct.destroyWithEvent(nt.CANCELLED)}onUserAccepted(e){return t(this,void 0,void 0,(function*(){B.setStatus(new at),Ct.callContext.cloudRTC.push();const t=Ct.callContext.call;t.caller.status=t.status,Ct.updateMemberStatus(e.userId,t.status);const i=yield O.Member.i.getData(e.userId),s={id:e.userId,data:i.get(e.userId)};tt.fire(Y.USER_ACCEPTED,{user:s})}))}destroy(){clearTimeout(this.dialTimeoutId)}}class lt extends ot{name(){return"Ringing"}constructor(){super(),this.startAccept=!1;const t=Date.now()-Ct.callContext.call.time;t>0&&(this.ringTimeoutId=setTimeout((()=>{Ct.destroyWithEvent(nt.RING_TIMEOUT)}),45e3-t))}accept(){return new Promise(((i,s)=>t(this,void 0,void 0,(function*(){try{if(this.startAccept)return void s("The call has already been accepted.");this.startAccept=!0;const t=s=>{B.setStatus(new at);let n=Ct.callContext.call;"Quit"!==n.caller.status&&(n.caller.status=n.status),Ct.updateMemberStatus(O.Socket.user().id,n.status),Ct.updateMemberStatus(s.userId,n.status),j.i.off(e.RTC_ACCEPTED,t),i()};j.i.on(e.RTC_ACCEPTED,t);const n=new $(W.ACCEPT,Ct.callContext.call.id);yield rt.sendRequest(n),yield Ct.callContext.cloudRTC.join(),Ct.callContext.cloudRTC.push()}catch(t){Ct.destroyWithEvent(nt.ACCEPT_FAILED),console.warn("accepte failed:",t),s(t)}}))))}onUserAcceptPacket(t){t.userId===O.Socket.user().id?Ct.destroyWithEvent(nt.HANDLED_ON_ANOTHER_DEVICE):Ct.updateMemberStatus(t.userId,"InCall")}end(){const t=new $(W.REJECT,Ct.callContext.call.id);rt.sendRequest(t),Ct.destroyWithEvent(nt.REJECTED)}onRTCRemoteFlow(t){const i=Ct.callContext.call;"Quit"!==[...i.callees,i.caller].find((e=>e.id===t.userId)).status&&j.i.fire(e.RTC_ACCEPTED,t)}onUserRang(e){return t(this,void 0,void 0,(function*(){}))}destroy(){clearTimeout(this.ringTimeoutId)}onUserAccepted(t){}}class ut{static initNotificationAssembler(){O.N.addAssembler({assemble:t=>({callId:t.callId}),support:t=>!!t.callId})}static createLocalNotification(t){const e=O.N.supportAppNotification();let i=t.notification;if(!it.isObject(i)||!e)return;let s={callId:t.callId};O.N.createLocalNotification(i.title,i.body,s,i.sound,i.badge)}}class dt{onServerPacket(t,e){switch(t){case H.RING:this.onRing(e);break;case H.USER_RANG:this.onUserRang(e);break;case H.USER_ACCEPTED:this.onUserAcceptPacket(e);break;case H.USER_QUIT:this.onUserQuit(e)}}}class ht extends dt{name(){return"Busy"}accept(){return B.status.accept()}end(){B.status.end(),pt.setStatus(new Et)}call(t){return Promise.reject("Another call is in progress.")}onRing(t){const e=new $(W.BUSY,t.id);rt.sendRequest(e)}onUserAcceptPacket(t){B.status.onUserAcceptPacket(t)}onUserRang(t){B.status.onUserRang(t)}onUserQuit(t){B.status.onUserQuit(t)}onRTCTimeout(){B.status.onRTCTimeout()}onRTCRemoteFlow(t){B.status.onRTCRemoteFlow(t)}onGoEasyDisconnected(){B.status.onGoEasyDisconnected()}}class Et extends dt{name(){return"Idle"}call(e){if(et.check(),st.validateNotification(e.notification),!O.Socket.user().id)throw new Error("Dialing failed: `id` is required when connecting to GoEasy.");return new Promise(((i,s)=>t(this,void 0,void 0,(function*(){try{pt.setStatus(new ht),"calleeIds"in e?Ct.initGroupCall(e):Ct.initPrivateCall(e),B.setStatus(new ct);const t=Ct.callContext.cloudRTC;yield t.requestPermission(e.mediaType);const n=yield t.createEngine(e.mediaType);t.join();const o=Ct.callContext.call;o.id=n;const r=B.status.name();o.status=r;const a="calleeIds"in e?e.calleeIds:[e.calleeId],c=yield O.Member.i.getData(...a);o.callees=a.map((t=>new z(t,c.get(t),"Ringing")));const l=$.createCallCommand(o,n,e);const u=(yield rt.sendRequest(l)).content.users;o.callees=o.callees.filter((t=>!u.find((e=>e.userId===t.id&&e.busy))));u.every((t=>t.busy))?(Ct.destroy(),s({code:400,content:"all callees are busy"})):i()}catch(t){Ct.destroy(),console.warn("Call failed",t),s(t)}}))))}onRing(e){return t(this,void 0,void 0,(function*(){try{pt.setStatus(new ht),Ct.ring(e),B.setStatus(new lt),ut.createLocalNotification(e);const t=B.status.name(),i=Ct.callContext.call;i.status=t;const s=e.users.map((t=>t.id)),n=yield O.Member.i.getData(e.callerId,...s);i.caller=new z(e.callerId,n.get(e.callerId),t),i.callees=e.users.map((e=>new z(e.id,n.get(e.id),t)));const o=Ct.callContext.cloudRTC;o.requestPermission(e.type),yield o.createEngine(e.type,e.callId);const r=new $(W.RANG,i.id);rt.sendRequest(r);const a=yield O.Member.i.getData(e.callerId),c={id:e.callerId,data:a.get(e.callerId)};tt.fire(Y.RING,{user:c})}catch(t){console.warn("Ring failed",t),Ct.destroy()}}))}accept(){return Promise.reject("No active call to accept.")}end(){}onUserRang(t){}onUserAcceptPacket(t){}onUserQuit(t){}onRTCTimeout(){}onRTCRemoteFlow(t){}onGoEasyDisconnected(){}}class pt{static setStatus(t){this.status=t}}pt.status=new Et;class Ct{static ring(t){const e=new J;e.id=t.callId,e.time=t.time,e.mediaType=t.type,e.groupId=t.groupId,this.callContext=new X(e)}static initPrivateCall(t){const e=new J;e.mediaType=t.mediaType,e.caller=new z(O.Socket.user().id,O.Socket.user().data,"Dialing"),this.callContext=new X(e)}static initGroupCall(t){if(t.calleeIds.length>8)throw new Error("calleeIds can not more than 8");const e=new J;e.groupId=t.groupId,e.mediaType=t.mediaType,e.caller=new z(O.Socket.user().id,O.Socket.user().data,"Dialing"),this.callContext=new X(e)}static updateMemberStatus(t,e){const i=this.callContext.call;[...i.callees,i.caller].find((e=>e.id===t)).status=e}static isCaller(){return this.callContext.call.caller.id===O.Socket.user().id}static destroyWithEvent(t,e=O.Socket.user()){tt.fire(Y.USER_QUIT,{user:e,reason:t}),tt.fire(Y.CALL_ENDED),this.destroy()}static destroy(){this.callContext.cloudRTC.destroy(),this.callContext.acceptedUsers.destroy(),this.callContext=null,B.clearStatus(),pt.setStatus(new Et)}}class mt{static init(){this.instance=new mt}static i(){if(this.instance)return this.instance;throw new Error("Please connect GoEasy first.")}constructor(){this.rtcStabilizer=new q,ut.initNotificationAssembler(),O.Socket.onMessage(M.RTC_PKT,this.onServerPacket.bind(this)),j.i.on(e.RECEIVED_RTC_REMOTE_FLOW,this.onRTCRemoteFlow.bind(this)),j.i.on(e.RTC_TIMEOUT,this.onRTCTimeout.bind(this)),O.Socket.on(O.Socket.EVENT.LOST,this.onGoEasyDisconnected.bind(this)),O.Socket.on(O.Socket.EVENT.DISCONNECT,this.onGoEasyDisconnected.bind(this))}on(t,e){tt.on(t.toString(),e)}off(t,e){tt.off(t.toString(),e)}call(e){return t(this,void 0,void 0,(function*(){return pt.status.call(e)}))}groupCall(e){return t(this,void 0,void 0,(function*(){return pt.status.call(e)}))}accept(){return pt.status.accept()}getCurrentCall(){return Ct.callContext.call}end(){pt.status.end()}onServerPacket(t){pt.status.onServerPacket(t.name,t.data)}onRTCTimeout(){pt.status.onRTCTimeout()}onRTCRemoteFlow(t){pt.status.onRTCRemoteFlow(t)}onGoEasyDisconnected(){pt.status.onGoEasyDisconnected()}}class Tt{static init(t){et.init(t)}static call(t){return mt.i().call(t)}static groupCall(t){return mt.i().groupCall(t)}static accept(){return mt.i().accept()}static currentCall(){return mt.i().getCurrentCall()}static end(){mt.i().end()}static on(t,e){mt.i().on(t,e)}static off(t,e){mt.i().off(t,e)}}Tt.EVENT=Y;export{Tt as default};
|