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