usc-rtc-client 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).UscRtc={})}(this,(function(e){"use strict";function t(e,t,i,n){return new(i||(i=Promise))((function(s,r){function o(e){try{c(n.next(e))}catch(e){r(e)}}function a(e){try{c(n.throw(e))}catch(e){r(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(o,a)}c((n=n.apply(e,t||[])).next())}))}var i,n,s,r,o;e.RtcSessionEvent=void 0,(i=e.RtcSessionEvent||(e.RtcSessionEvent={})).bye="bye",i.stream="stream",i.reInvite="reInvite",i.localAudioChange="localAudioChange",i.localVideoChange="localVideoChange",i.localStreamChange="localStreamChange",e.RtcClientEvent=void 0,(n=e.RtcClientEvent||(e.RtcClientEvent={})).connected="connected",n.disconnected="disconnected",n.invite="invite",e.PTTEvent=void 0,(s=e.PTTEvent||(e.PTTEvent={})).PTTInfo="PTTInfo",s.FPREInfo="FPREInfo",s.IMSInfo="IMSInfo",s.ERROR="error",s.SPEAKER="speaker",s.TALKING="talking",s.ACTIVE_GROUP="activeGroup",s.JOIN_INFO="joinInfo",function(e){e.MESSAGE_TYPE_REGISTER="REGISTER",e.MESSAGE_TYPE_INVITE="INVITE",e.MESSAGE_TYPE_REINVITE="reINVITE",e.MESSAGE_TYPE_ICE="ICE",e.MESSAGE_TYPE_INFO="INFO",e.MESSAGE_TYPE_BYE="BYE"}(r||(r={})),function(e){e.FPRE="fpre",e.IMS="ims",e.PTT_QUERY_ONLINE_USER="15",e.PTT_ANSWER_ONLINE_USER="115",e.PTT_QUERY_INFO="16",e.PTT_QUERY_INFO_CONFIRM="116"}(o||(o={}));const a=(e,t)=>{void 0!==e&&void 0!==t&&(t.srcObject=e,t.play())},c=e=>{e.onaddtrack=null,e.onremovetrack=null,e.getTracks().forEach((t=>{null==e||e.removeTrack(t),t.onended=null,t.onmute=null,t.onunmute=null,t.stop()}))};let d=!1;const l=(e,...t)=>{d&&console.log(`%c usc-rtc-devtools %c ${e} %c `,"background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)};var h,u,v;function f(){}function p(){p.init.call(this)}function m(e){return void 0===e._maxListeners?p.defaultMaxListeners:e._maxListeners}function g(e,t,i){if(t)e.call(i);else for(var n=e.length,s=S(e,n),r=0;r<n;++r)s[r].call(i)}function k(e,t,i,n){if(t)e.call(i,n);else for(var s=e.length,r=S(e,s),o=0;o<s;++o)r[o].call(i,n)}function y(e,t,i,n,s){if(t)e.call(i,n,s);else for(var r=e.length,o=S(e,r),a=0;a<r;++a)o[a].call(i,n,s)}function T(e,t,i,n,s,r){if(t)e.call(i,n,s,r);else for(var o=e.length,a=S(e,o),c=0;c<o;++c)a[c].call(i,n,s,r)}function C(e,t,i,n){if(t)e.apply(i,n);else for(var s=e.length,r=S(e,s),o=0;o<s;++o)r[o].apply(i,n)}function w(e,t,i,n){var s,r,o,a;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((r=e._events)?(r.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),r=e._events),o=r[t]):(r=e._events=new f,e._eventsCount=0),o){if("function"==typeof o?o=r[t]=n?[i,o]:[o,i]:n?o.unshift(i):o.push(i),!o.warned&&(s=m(e))&&s>0&&o.length>s){o.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=o.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else o=r[t]=i,++e._eventsCount;return e}function b(e,t,i){var n=!1;function s(){e.removeListener(t,s),n||(n=!0,i.apply(e,arguments))}return s.listener=i,s}function E(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function S(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}f.prototype=Object.create(null),p.EventEmitter=p,p.usingDomains=!1,p.prototype.domain=void 0,p.prototype._events=void 0,p.prototype._maxListeners=void 0,p.defaultMaxListeners=10,p.init=function(){this.domain=null,p.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new f,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},p.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},p.prototype.getMaxListeners=function(){return m(this)},p.prototype.emit=function(e){var t,i,n,s,r,o,a,c="error"===e;if(o=this._events)c=c&&null==o.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var d=new Error('Uncaught, unspecified "error" event. ('+t+")");throw d.context=t,d}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(i=o[e]))return!1;var l="function"==typeof i;switch(n=arguments.length){case 1:g(i,l,this);break;case 2:k(i,l,this,arguments[1]);break;case 3:y(i,l,this,arguments[1],arguments[2]);break;case 4:T(i,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(n-1),r=1;r<n;r++)s[r-1]=arguments[r];C(i,l,this,s)}return!0},p.prototype.addListener=function(e,t){return w(this,e,t,!1)},p.prototype.on=p.prototype.addListener,p.prototype.prependListener=function(e,t){return w(this,e,t,!0)},p.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,b(this,e,t)),this},p.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,b(this,e,t)),this},p.prototype.removeListener=function(e,t){var i,n,s,r,o;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(i=n[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new f:(delete n[e],n.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(s=-1,r=i.length;r-- >0;)if(i[r]===t||i[r].listener&&i[r].listener===t){o=i[r].listener,s=r;break}if(s<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new f,this;delete n[e]}else!function(e,t){for(var i=t,n=i+1,s=e.length;n<s;i+=1,n+=1)e[i]=e[n];e.pop()}(i,s);n.removeListener&&this.emit("removeListener",e,o||t)}return this},p.prototype.off=function(e,t){return this.removeListener(e,t)},p.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new f,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new f:delete i[e]),this;if(0===arguments.length){for(var n,s=Object.keys(i),r=0;r<s.length;++r)"removeListener"!==(n=s[r])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new f,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},p.prototype.listeners=function(e){var t,i=this._events;return i&&(t=i[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]},p.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):E.call(e,t)},p.prototype.listenerCount=E,p.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class R extends p{constructor(){super(),this.localMediaStream=new MediaStream,this.videoConstrains={width:{max:1920},height:{max:1080},frameRate:{ideal:20,max:30}},this.audioConstrants={noiseSuppression:!0,echoCancellation:!0,autoGainControl:!0},super.setMaxListeners(0)}releaseTrack(e){var t;void 0!==e&&(null===(t=this.localMediaStream)||void 0===t||t.removeTrack(e),e.stop())}addDeviceChangeListener(e){navigator.mediaDevices.addEventListener("devicechange",e)}removeDeviceChangeListener(e){navigator.mediaDevices.removeEventListener("devicechange",e)}setVideoTrack(t){var i;this.emit(e.RtcSessionEvent.localVideoChange,{from:this.videoTrack,to:t}),this.videoTrack!==t&&(null===(i=this.localMediaStream)||void 0===i||i.addTrack(t),this.localTractChange(this.videoTrack,t)),this.videoTrack=t}setAudioTrack(t){var i;this.emit(e.RtcSessionEvent.localAudioChange,{from:this.audioTrack,to:t}),this.audioTrack!==t&&(null===(i=this.localMediaStream)||void 0===i||i.addTrack(t),this.localTractChange(this.audioTrack,t)),this.audioTrack=t}renderLocalStreamToContainer(e){a(this.localMediaStream,e)}getAllInputResource(){return t(this,void 0,void 0,(function*(){let e=yield navigator.mediaDevices.enumerateDevices();return{audio:e.filter((e=>"audioinput"===e.kind)),video:e.filter((e=>"videoinput"===e.kind))}}))}useDefaultDevice(e){return t(this,void 0,void 0,(function*(){let t=yield navigator.mediaDevices.getUserMedia({audio:!0,video:!0});this.setAudioTrack(t.getAudioTracks()[0]),this.setVideoTrack(t.getVideoTracks()[0]),this.renderLocalStreamToContainer(e)}))}useAudio(e){return t(this,void 0,void 0,(function*(){let t=void 0===e?{audio:Object.assign({},this.audioConstrants),video:!1}:{video:!1,audio:Object.assign({deviceId:e},this.audioConstrants)},i=yield navigator.mediaDevices.getUserMedia(t);return this.setAudioTrack(i.getAudioTracks()[0]),i.getAudioTracks()[0]}))}useDeviceVideo(e){return t(this,void 0,void 0,(function*(){let t=void 0===e?{audio:!1,video:Object.assign({},this.videoConstrains)}:{audio:!1,video:Object.assign({deviceId:e},this.videoConstrains)},i=yield navigator.mediaDevices.getUserMedia(t);return this.setVideoTrack(i.getVideoTracks()[0]),i.getVideoTracks()[0]}))}useDeskTopVideo(){return t(this,void 0,void 0,(function*(){let e=yield navigator.mediaDevices.getDisplayMedia({video:Object.assign({},this.videoConstrains),audio:!1});return this.setVideoTrack(e.getVideoTracks()[0]),e.getVideoTracks()[0]}))}disableAudio(){void 0!==this.audioTrack&&this.audioTrack.enabled&&(l("local audio status change",this.audioTrack),this.audioTrack.enabled=!1,this.setAudioTrack(this.audioTrack))}enableAudio(){void 0===this.audioTrack||this.audioTrack.enabled||(l("local audio status change",this.audioTrack),this.audioTrack.enabled=!0,this.setAudioTrack(this.audioTrack))}disableVideo(){void 0!==this.videoTrack&&this.videoTrack.enabled&&(l("local video status change",this.videoTrack),this.videoTrack.enabled=!1,this.setVideoTrack(this.videoTrack))}enableVideo(){void 0===this.videoTrack||this.videoTrack.enabled||(l("local video status change",this.videoTrack),this.videoTrack.enabled=!0,this.setVideoTrack(this.videoTrack))}close(){this.releaseTrack(this.audioTrack),this.releaseTrack(this.videoTrack),this.audioTrack=void 0,this.videoTrack=void 0,this.localMediaStream=void 0}}class _ extends R{constructor(){super(),this.videoCodecs=[],this.audioCodecs=[],this.audioReciveCodecs=[],this.preferCodec(),this.peerConnection=this.buildConnection()}buildConnection(){let e=new RTCPeerConnection;return e.addEventListener("connectionstatechange",(e=>l("connectionstatechange",e))),e.addEventListener("icecandidate",(e=>{l("icecandidate",e),this.onIceCandidate(e)})),e.addEventListener("datachannel",(e=>l("datachannel",e))),e.addEventListener("icecandidateerror",(e=>l("icecandidateerror",e))),e.addEventListener("iceconnectionstatechange",(e=>l("iceconnectionstatechange",e))),e.addEventListener("icegatheringstatechange",(e=>l("icegatheringstatechange",e))),e.addEventListener("negotiationneeded",(e=>l("negotiationneeded",e))),e.addEventListener("signalingstatechange",(e=>l("signalingstatechange",e))),e.addEventListener("track",(e=>this.reciveTrack(e))),e}localTractChange(e,i){return t(this,void 0,void 0,(function*(){if(void 0!==this.remoteStream&&void 0!==this.peerConnection)if(void 0!==i){let e=this.peerConnection.getSenders().find((e=>{var t;return(null===(t=e.track)||void 0===t?void 0:t.kind)===i.kind}));void 0!==e&&e.replaceTrack(i)}else{let t=this.peerConnection.getSenders().find((t=>{var i;return(null===(i=t.track)||void 0===i?void 0:i.kind)===(null==e?void 0:e.kind)}));void 0!==t&&this.peerConnection.removeTrack(t)}void 0!==i&&(i.onended=e=>l("local track end",e),i.onmute=e=>{l("local mute",e),this.emitLocalStreamChange()},i.onunmute=e=>{l("local unmute",e),this.emitLocalStreamChange()}),this.releaseTrack(e),this.emitLocalStreamChange()}))}isInMeeting(){return void 0!==this.remoteStream}hasRemoteVideo(e){var t;return null!==(t=null==e?void 0:e.getVideoTracks().some((e=>!1===e.muted&&"live"===e.readyState)))&&void 0!==t&&t}emitLocalStreamChange(){this.emit(e.RtcSessionEvent.localStreamChange,{audioOnly:!this.hasRemoteVideo(this.localMediaStream),stream:this.localMediaStream})}emitRemoteStreamChange(){this.emit(e.RtcSessionEvent.stream,{audioOnly:!this.hasRemoteVideo(this.remoteStream),stream:this.remoteStream})}trigerStreamChange(e=100){void 0!==this.emitStreamTimeout&&clearTimeout(this.emitStreamTimeout),this.emitStreamTimeout=setTimeout((()=>{this.emitRemoteStreamChange(),this.emitStreamTimeout=void 0}),e)}reciveTrack(e){l("recive track",e),void 0===this.remoteStream&&(this.remoteStream=new MediaStream),e.track.addEventListener("ended",(()=>this.trigerStreamChange())),e.track.addEventListener("mute",(()=>this.trigerStreamChange(3e3))),e.track.addEventListener("unmute",(()=>this.trigerStreamChange(10))),this.remoteStream.addTrack(e.track),this.trigerStreamChange()}preferCodec(e="H264",t="PCMA"){this.videoCodecs=[];let i=RTCRtpSender.getCapabilities("video");null!==i&&this.videoCodecs.push(...i.codecs.filter((t=>t.mimeType===`video/${e}`)));let n=RTCRtpReceiver.getCapabilities("video");null!==n&&this.videoCodecs.push(...n.codecs.filter((t=>t.mimeType===`video/${e}`))),this.audioCodecs=[];let s=RTCRtpSender.getCapabilities("audio");null!==s&&this.audioCodecs.push(...s.codecs.filter((e=>e.mimeType===`audio/${t}`)));let r=RTCRtpReceiver.getCapabilities("audio");null!==r&&this.audioCodecs.push(...r.codecs.filter((e=>e.mimeType===`audio/${t}`)))}trackIsInPeerConnection(e){var t,i;return null!==(i=null===(t=this.peerConnection)||void 0===t?void 0:t.getSenders().some((t=>{var i;return(null===(i=t.track)||void 0===i?void 0:i.id)===e.id})))&&void 0!==i&&i}updateTracks(){if(void 0===this.peerConnection||"closed"===this.peerConnection.signalingState)throw new Error("session is closed !");if(void 0===this.audioTrack&&void 0===this.videoCodecs)throw new Error("no input found");if(void 0!==this.audioTrack&&!this.trackIsInPeerConnection(this.audioTrack))try{this.peerConnection.addTrack(this.audioTrack)}catch(e){}if(void 0!==this.videoTrack&&!this.trackIsInPeerConnection(this.videoTrack))try{this.peerConnection.addTrack(this.videoTrack)}catch(e){}try{this.peerConnection.getTransceivers().forEach((e=>{null!==e.sender.track&&("video"===e.sender.track.kind?e.setCodecPreferences([...this.videoCodecs]):"audio"===e.sender.track.kind&&e.setCodecPreferences([...this.audioCodecs]))}))}catch(e){((e,...t)=>{d&&console.log(`%c usc-rtc-devtools %c ${e} %c `,"background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#ff0000 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)})("preferCodec error",e,this.audioCodecs,this.videoCodecs)}}createOffer(){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");this.updateTracks();let e=yield this.peerConnection.createOffer();return e=class{static formatOffer(e){let{sdp:t}=e;if(void 0===t)throw new Error("offer sdp is empty");return t=t.split("\r\n").filter((e=>-1===e.indexOf("a=group:BUNDLE"))).join("\r\n"),e.sdp=t,e}}.formatOffer(e),yield this.peerConnection.setLocalDescription(e),e}))}setOffer(e){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");yield this.peerConnection.setRemoteDescription({type:"offer",sdp:e})}))}createAnswer(){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");this.updateTracks();let e=yield this.peerConnection.createAnswer();return this.peerConnection.setLocalDescription(e),e}))}setAnswer(e){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");yield this.peerConnection.setRemoteDescription({type:"answer",sdp:e})}))}setRemoteIce(e){if(void 0===this.peerConnection)throw new Error("session is closed !");let t=new RTCIceCandidate(e);this.peerConnection.addIceCandidate(t)}closeConnection(e){e.close(),e.ontrack=null,e.onicecandidate=null,e.oniceconnectionstatechange=null,e.onsignalingstatechange=null,e.onicegatheringstatechange=null,e.onnegotiationneeded=null,e.onicecandidateerror=null,e.ondatachannel=null,e.onconnectionstatechange=null}close(){super.close(),void 0!==this.peerConnection&&(this.closeConnection(this.peerConnection),this.peerConnection=void 0),void 0!==this.remoteStream&&c(this.remoteStream)}}e.RtcInviteStatus=void 0,(h=e.RtcInviteStatus||(e.RtcInviteStatus={}))[h.timeout=408]="timeout",h[h.reject=488]="reject",h[h.busy=486]="busy",h[h.notfound=404]="notfound",h[h.notAllowedMedia=415]="notAllowedMedia",h[h.ok=200]="ok";class L extends _{constructor(e,t,i){super(),this.sessionId=t,this.rtcClient=e,this.remoteNumber=i}reciveIceRequest({ice:e}){this.setRemoteIce(JSON.parse(e))}getRemoteNumber(){return this.remoteNumber}onIceCandidate(e){}destory(e=!0){this.removeAllListeners(),void 0!==this.rtcClient&&(void 0!==this.remoteNumber&&(e&&this.rtcClient.sendBye(this.remoteNumber,this.sessionId),this.remoteNumber=void 0),this.rtcClient.removeSession(this)),this.rtcClient=void 0,super.close()}rejectCall(){let[t,i,n]=this.checkIsNotUndefined(this.rtcClient,this.remoteNumber,this.inviteSeq);t.answer(e.RtcInviteStatus.reject,n,i,this.sessionId),this.destory()}acceptCall(){return t(this,void 0,void 0,(function*(){if(void 0!==this.inviteOffer){yield this.setOffer(this.inviteOffer);let[t,i,n]=this.checkIsNotUndefined(this.rtcClient,this.remoteNumber,this.inviteSeq),{sdp:s}=yield this.createAnswer();t.answer(e.RtcInviteStatus.ok,n,i,this.sessionId,s)}}))}pickNewConnection(){return t(this,void 0,void 0,(function*(){void 0!==this.peerConnection&&this.closeConnection(this.peerConnection),this.peerConnection=this.buildConnection(),void 0!==this.remoteStream&&(c(this.remoteStream),this.remoteStream=void 0,this.acceptCall())}))}checkIsNotUndefined(...e){for(const t of e)if(void 0===t)throw new Error("someone is undefined");return e}sendDtmf(e){var i;return t(this,void 0,void 0,(function*(){null===(i=this.rtcClient)||void 0===i||i.sendDtmf(this.remoteNumber,this.sessionId,e)}))}}e.RtcCalledType=void 0,(u=e.RtcCalledType||(e.RtcCalledType={})).audio="audio",u.video="video",u.audioVideo="audioVideo",u.unknown="unknown";class I extends L{constructor(){super(...arguments),this.responseCallType=e.RtcCalledType.unknown}start(){return t(this,void 0,void 0,(function*(){let[t,i]=this.checkIsNotUndefined(this.rtcClient,this.remoteNumber),n=yield this.createOffer(),{status:s,sdp:r}=yield t.invite(i,this.sessionId,n.sdp);s===e.RtcInviteStatus.ok&&(yield this.setAnswer(r));const o=r.split("\r\n"),a=o.find((e=>e.startsWith("m=audio"))),c=o.find((e=>e.startsWith("m=video")));return this.responseCallType=a&&c?e.RtcCalledType.audioVideo:a?e.RtcCalledType.audio:c?e.RtcCalledType.video:e.RtcCalledType.unknown,s}))}addRemote(e){this.remoteNumber=e}}class x{constructor(e,t,i,n){this.req=e,this.seq=x.seqNum++,this.from=t,this.to=i,this.registerid=`${x.random}@${t}`,void 0!==n&&Object.assign(this,n)}}x.random=String(Date.now()),x.seqNum=1,function(e){e.connected="connected",e.disconnected="disconnected",e.message="message"}(v||(v={}));var M=null;try{var N="undefined"!=typeof module&&"function"==typeof module.require&&module.require("worker_threads")||"function"==typeof __non_webpack_require__&&__non_webpack_require__("worker_threads")||"function"==typeof require&&require("worker_threads");M=N.Worker}catch(e){}function G(e,t,i){var n=void 0===t?null:t,s=function(e,t){return Buffer.from(e,"base64").toString(t?"utf16":"utf8")}(e,void 0!==i&&i),r=s.indexOf("\n",10)+1,o=s.substring(r)+(n?"//# sourceMappingURL="+n:"");return function(e){return new M(o,Object.assign({},e,{eval:!0}))}}function P(e,t,i){var n=void 0===t?null:t,s=function(e,t){var i=atob(e);if(t){for(var n=new Uint8Array(i.length),s=0,r=i.length;s<r;++s)n[s]=i.charCodeAt(s);return String.fromCharCode.apply(null,new Uint16Array(n.buffer))}return i}(e,void 0!==i&&i),r=s.indexOf("\n",10)+1,o=s.substring(r)+(n?"//# sourceMappingURL="+n:""),a=new Blob([o],{type:"application/javascript"});return URL.createObjectURL(a)}var A="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0);var O,V,Y,W=(O="Lyogcm9sbHVwLXBsdWdpbi13ZWItd29ya2VyLWxvYWRlciAqLwohZnVuY3Rpb24oKXsidXNlIHN0cmljdCI7bGV0IGUsdD0hMTtjb25zdCBzPXNlbGY7c2VsZi5hZGRFdmVudExpc3RlbmVyKCJtZXNzYWdlIiwoZnVuY3Rpb24ocyl7Y29uc3QgYT1zLmRhdGE7c3dpdGNoKGEuY21kKXtjYXNlInN0YXJ0IjpzZWxmLnBvc3RNZXNzYWdlKHt0eXBlOiJ3ZWJXb3JrZXIiLGRhdGE6IuWQr+WKqHdlYldvcmtlciJ9KSxvKGEudXJsKTticmVhaztjYXNlInN0b3AiOnQ9ITAsZS5jbG9zZSgpLHNlbGYucG9zdE1lc3NhZ2Uoe3R5cGU6IndlYldvcmtlciIsZGF0YToi5YGc5q2id2ViV29ya2VyIn0pLHNlbGYucG9zdE1lc3NhZ2Uoe3R5cGU6ImNsb3NlIixkYXRhOiJjbG9zZSIrKG5ldyBEYXRlKS50b1RpbWVTdHJpbmcoKX0pLHNlbGYuY2xvc2UoKTticmVhaztjYXNlIm1zZyI6MT09PWUucmVhZHlTdGF0ZSYmZS5zZW5kKEpTT04uc3RyaW5naWZ5KGEuZGF0YSkpfX0pLCExKTtjb25zdCBvPWZ1bmN0aW9uKHQpe2V8fChlPW5ldyBXZWJTb2NrZXQodCksZS5vbmVycm9yPWZ1bmN0aW9uKGUpe2EodCl9LGUub25jbG9zZT1mdW5jdGlvbihlKXthKHQpfSxlLm9ub3Blbj1lPT57cy5wb3N0TWVzc2FnZSh7dHlwZToib3BlbiIsZGF0YToic3RhcnQiKyhuZXcgRGF0ZSkudG9UaW1lU3RyaW5nKCl9KX0sZS5vbm1lc3NhZ2U9ZT0+e2NvbnN0IHQ9SlNPTi5wYXJzZShlLmRhdGEpO3MucG9zdE1lc3NhZ2Uoe3R5cGU6IndlYlNvY2tldCIsZGF0YTp0fSl9KX07ZnVuY3Rpb24gYShzKXt0fHwodD0hMCxzZXRUaW1lb3V0KChmdW5jdGlvbigpe2U9bnVsbCxvKHMpLHQ9ITF9KSwyZTMpKX19KCk7Cgo=",V=null,Y=!1,A?G(O,V,Y):function(e,t,i){var n;return function(s){return n=n||P(e,t,i),new Worker(n,s)}}(O,V,Y));class j extends p{constructor(e,t){super(),this.server=e,this.ws=new W,this.configHeartBeat(t)}connect(){this.initWebsocket(),this.ws.onmessage=e=>{const{type:t,data:i}=e.data;switch(t){case"webSocket":return l("websocket recive message ",i),void this.emit(v.message,i);case"open":return l("websocket connected ",this.server),void this.emit(v.connected)}}}configHeartBeat(e){this.ws.postMessage({cmd:"config",heartBeat:e})}initWebsocket(){this.ws.postMessage({cmd:"start",url:this.server})}send(e){this.ws.postMessage({cmd:"msg",data:e})}close(){this.ws.postMessage({cmd:"close"})}sendMessage(e){l("websocket send message ",e),void 0!==e&&void 0!==this.ws&&this.ws.postMessage({cmd:"msg",data:e})}}class U extends p{constructor(e,t,i){super(),this.activeServer="",this.server=e,this.localNumber=t,this.expireTime=i,super.setMaxListeners(0)}testWs(e){return t(this,void 0,void 0,(function*(){return e.reduce(((e,t)=>e.then((e=>Promise.resolve(e)),(e=>new Promise(((i,n)=>{const s=new WebSocket(t);s.onopen=()=>{e.push(t),s.close(),i(e)},s.onclose=()=>{n(e)}}))))),Promise.reject([])).then((e=>Promise.resolve(e)),(e=>Promise.resolve(e)))}))}reConnect(){return t(this,void 0,void 0,(function*(){this.destory();let e=[];void 0!==this.server&&(e="string"==typeof this.server?[this.server]:this.server);const t=yield this.testWs(e);return new Promise(((e,i)=>{if(t.length>0){this.activeServer=t[0];let n=new j(this.activeServer,new x(r.MESSAGE_TYPE_REGISTER,this.localNumber,this.localNumber,{expire:this.expireTime}));n.on(v.connected,(()=>{this.socketWorker=n,this.onConnected(),e()})),n.on(v.disconnected,(e=>{this.socketWorker=void 0,this.onDisConnected(e),i("socketWorker connecion failed")})),n.on(v.message,(e=>this.onReviceMessage(e))),n.connect()}else i("server is empty")}))}))}sendMessage(e){if(void 0===this.socketWorker)throw new Error("socketWorker is closed");this.socketWorker.sendMessage(e)}destory(){void 0!==this.socketWorker&&(this.unRegister(),this.socketWorker.close(),this.socketWorker=void 0)}}class Z extends U{constructor(e,t,i){super(e,t,i),this.asyncProducer=new Map}sendRespondableMessage(e){return t(this,void 0,void 0,(function*(){return this.sendMessage(e),new Promise((t=>this.asyncProducer.set(e.seq,t)))}))}getAsyncResolve(e){let t=this.asyncProducer.get(e);return void 0!==t&&this.asyncProducer.delete(e),t}onDisConnected(t){this.asyncProducer.clear(),this.emit(e.RtcClientEvent.disconnected,t)}onReviceMessage(e){var t;let{status:i,seq:n}=e;void 0!==i?null===(t=this.getAsyncResolve(n))||void 0===t||t.call(void 0,e):this.processMessage(e)}}class D extends Z{register(){return t(this,void 0,void 0,(function*(){let e=new x(r.MESSAGE_TYPE_REGISTER,this.localNumber,this.localNumber,{expire:this.expireTime});return this.sendRespondableMessage(e)}))}unRegister(){let e=new x(r.MESSAGE_TYPE_REGISTER,this.localNumber,this.localNumber,{expire:0});this.sendMessage(e)}invite(e,i,n){return t(this,void 0,void 0,(function*(){return this.sendRespondableMessage(new x(r.MESSAGE_TYPE_INVITE,this.localNumber,e,{cid:i,sdp:n}))}))}sendIce(e,t,i){}sendBye(e,t){return this.sendMessage(new x(r.MESSAGE_TYPE_BYE,this.localNumber,e,{cid:t}))}answer(e,t,i,n,s){this.sendMessage({status:e,seq:t,from:i,to:this.localNumber,cid:n,sdp:s})}sendPttMessage(e){this.sendMessage(e)}sendDtmf(e,i,n){return t(this,void 0,void 0,(function*(){return this.sendMessage(new x(r.MESSAGE_TYPE_INFO,this.localNumber,e,{cid:i,info:{msg:"dtmf",dtmf:n}}))}))}}function K(){}function q(){q.init.call(this)}function X(e){return void 0===e._maxListeners?q.defaultMaxListeners:e._maxListeners}function F(e,t,i){if(t)e.call(i);else for(var n=e.length,s=te(e,n),r=0;r<n;++r)s[r].call(i)}function z(e,t,i,n){if(t)e.call(i,n);else for(var s=e.length,r=te(e,s),o=0;o<s;++o)r[o].call(i,n)}function B(e,t,i,n,s){if(t)e.call(i,n,s);else for(var r=e.length,o=te(e,r),a=0;a<r;++a)o[a].call(i,n,s)}function H(e,t,i,n,s,r){if(t)e.call(i,n,s,r);else for(var o=e.length,a=te(e,o),c=0;c<o;++c)a[c].call(i,n,s,r)}function $(e,t,i,n){if(t)e.apply(i,n);else for(var s=e.length,r=te(e,s),o=0;o<s;++o)r[o].apply(i,n)}function J(e,t,i,n){var s,r,o,a;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((r=e._events)?(r.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),r=e._events),o=r[t]):(r=e._events=new K,e._eventsCount=0),o){if("function"==typeof o?o=r[t]=n?[i,o]:[o,i]:n?o.unshift(i):o.push(i),!o.warned&&(s=X(e))&&s>0&&o.length>s){o.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=o.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else o=r[t]=i,++e._eventsCount;return e}function Q(e,t,i){var n=!1;function s(){e.removeListener(t,s),n||(n=!0,i.apply(e,arguments))}return s.listener=i,s}function ee(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function te(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}K.prototype=Object.create(null),q.EventEmitter=q,q.usingDomains=!1,q.prototype.domain=void 0,q.prototype._events=void 0,q.prototype._maxListeners=void 0,q.defaultMaxListeners=10,q.init=function(){this.domain=null,q.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new K,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},q.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},q.prototype.getMaxListeners=function(){return X(this)},q.prototype.emit=function(e){var t,i,n,s,r,o,a,c="error"===e;if(o=this._events)c=c&&null==o.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var d=new Error('Uncaught, unspecified "error" event. ('+t+")");throw d.context=t,d}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(i=o[e]))return!1;var l="function"==typeof i;switch(n=arguments.length){case 1:F(i,l,this);break;case 2:z(i,l,this,arguments[1]);break;case 3:B(i,l,this,arguments[1],arguments[2]);break;case 4:H(i,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(n-1),r=1;r<n;r++)s[r-1]=arguments[r];$(i,l,this,s)}return!0},q.prototype.addListener=function(e,t){return J(this,e,t,!1)},q.prototype.on=q.prototype.addListener,q.prototype.prependListener=function(e,t){return J(this,e,t,!0)},q.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,Q(this,e,t)),this},q.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,Q(this,e,t)),this},q.prototype.removeListener=function(e,t){var i,n,s,r,o;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(i=n[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new K:(delete n[e],n.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(s=-1,r=i.length;r-- >0;)if(i[r]===t||i[r].listener&&i[r].listener===t){o=i[r].listener,s=r;break}if(s<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new K,this;delete n[e]}else!function(e,t){for(var i=t,n=i+1,s=e.length;n<s;i+=1,n+=1)e[i]=e[n];e.pop()}(i,s);n.removeListener&&this.emit("removeListener",e,o||t)}return this},q.prototype.off=function(e,t){return this.removeListener(e,t)},q.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new K,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new K:delete i[e]),this;if(0===arguments.length){for(var n,s=Object.keys(i),r=0;r<s.length;++r)"removeListener"!==(n=s[r])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new K,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},q.prototype.listeners=function(e){var t,i=this._events;return i&&(t=i[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]},q.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):ee.call(e,t)},q.prototype.listenerCount=ee,q.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};const ie="error",ne="speaker",se="talking",re="activeGroup",oe="joinInfo",ae="disConnected",ce={400:"消息不符规范",401:"验证不通过",403:"无权利操作",404:"不存在该用户",415:"错误参数",405:"无法操作",424:"不存在XD群",425:"管理员没有",426:"已存在管理员",427:"以前被拒绝过,或者删除过",428:"超出所配置的下属管理员的数量",429:"群内无用户",430:"管理员的索引号冲突",431:"还有子管理员存在"},de="connected",le="disconnected",he="message",ue=(e,...t)=>{console.log(`%c usc-ptt-devtools %c ${e} %c `,"background:#16aae0 ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#14e04e ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)},ve=(e,...t)=>{console.log(`%c usc-ptt-devtools %c ${e} %c `,"background:#16aae0 ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#e02514 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)};class fe extends q{server="";ws=null;constructor(e){super(),this.server=e}connect(){this.ws=new WebSocket(this.server),this.ws.onopen=()=>{ue("websocket connected ",this.server),this.emit(de)},this.ws.onclose=()=>{ue("websocket disconnected ",this.server),this.ws=void 0,this.emit(le)},this.ws.onmessage=({data:e})=>{let t=JSON.parse(e);ue("websocket recive message ",t),this.emit(he,t)}}close(){void 0!==this.ws&&(this.ws.close(),this.ws=void 0)}sendMessage(e){ue("websocket send message ",e),void 0!==e&&void 0!==this.ws&&1===this.ws.readyState&&this.ws.send(JSON.stringify(e))}}class pe extends q{socket=void 0;timer=void 0;waitinggroup=void 0;speaker="";activeGroup="";talking=!1;constructor(e,t=20,i=!0){if(super(),!e)return ve("服务器地址为空!");this.server=e,this.heartTime=t,this.autoReconnect=i}async initClient(){return this.destorySocket(),new Promise(((e,t)=>{if(void 0!==this.server){let i=new fe(this.server);i.on(de,(()=>{this.socket=i,this.timer=setInterval((()=>{this.sendMessage({req:0})}),1e3*this.heartTime),e()})),i.on(le,(()=>{this.socket=void 0,this.autoReconnect?setTimeout((()=>{this.initClient()}),3e3):this.emit(ae),t("socket connecion failed")})),i.on(he,(e=>this.onReviceMessage(e))),i.connect()}else t("server is empty")}))}onReviceMessage(e){ue(e);const{info:t,msg:i,res:n}=e;if(10===t&&(this.speaker=`${i.speaker}`,ue(`${this.speaker}正在说话`),this.emit(ne,this.speaker)),11===t&&(200===n.code?(this.activeGroup=this.waitinggroup,ue(`加入组${this.activeGroup}`)):(this.activeGroup="",ve(ce[n.code]),this.emit(ie,ce[n.code])),this.emit(re,this.activeGroup)),13===t&&(200===n.code?this.talking=!0:this.talking=!1,ue(`talking:${this.talking}`),this.emit(se,this.talking)),14===t&&(this.speaker="",ue(`${this.speaker}停止说话`),this.emit(ne,this.speaker)),15===t){const e={user:i.number,group:i.g_num,code:i.status};ue(`${e}进入组`),this.emit(oe,e)}}joinGroup(e,t,i,n){this.socket&&(this.waitinggroup=e,this.sendMessage({req:1,group:e,user:t,ptts:i,port:n}))}quitGroup(){this.socket&&(this.activeGroup="",this.emit(re,this.activeGroup),this.sendMessage({req:2}))}talk(){this.socket&&this.sendMessage({req:3})}stop(){this.talking=!1,ue(`talking:${this.talking}`),this.emit(se,this.talking),this.socket&&this.sendMessage({req:4})}sendMessage(e){if(void 0===this.socket)throw new Error("socket is closed");this.socket.sendMessage(e)}destorySocket(){void 0!==this.socket&&(this.socket.close(),this.socket=void 0)}destory(){this.destorySocket()}}class me extends D{constructor(e){let t=e.expireTime||me.defaultExpireTime;super(e.server,e.localNumber,t),this.heartTime=e.heartTime||this.expireTime/4,this.enablePtt=e.enablePtt,this.pttServer=e.pttServer}initPttClient(){return t(this,void 0,void 0,(function*(){if(this.enablePtt){if(!this.pttServer)return Promise.reject("pttServer不存在");{const e=yield(async(e,t=20,i=!0)=>{let n=new pe(e,t,i);return await n.initClient(),n})(this.pttServer);if(!e)return Promise.reject("ptt客户端初始化失败!");e.on(re,(e=>{this.emit(re,e)})),e.on(ne,(e=>{this.emit(ne,e)})),e.on(se,(e=>{this.emit(se,e)})),e.on(oe,(e=>{this.emit(oe,e)})),e.on(ae,(e=>{this.emit(ae,e)})),this.pttClient=e}}}))}joinGroup(e,t,i,n){var s;null===(s=this.pttClient)||void 0===s||s.joinGroup(e,t,i,n)}quitGroup(){var e;null===(e=this.pttClient)||void 0===e||e.quitGroup()}talk(){var e;null===(e=this.pttClient)||void 0===e||e.talk()}stop(){var e;null===(e=this.pttClient)||void 0===e||e.stop()}onConnected(){}onDisConnected(){super.onDisConnected()}}me.defaultExpireTime=60;class ge extends L{constructor(t,i,n,s,r){super(t,i,n),this.callType=e.RtcCalledType.unknown,this.inviteSeq=r,this.inviteOffer=s;const o=s.split("\r\n"),a=o.find((e=>e.startsWith("m=audio"))),c=o.find((e=>e.startsWith("m=video")));this.callType=a&&c?e.RtcCalledType.audioVideo:a?e.RtcCalledType.audio:c?e.RtcCalledType.video:e.RtcCalledType.unknown}}class ke extends me{constructor(){super(...arguments),this.allSession=[]}createSession(e){let t=new I(this,((e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce(((e,t)=>e+((t&=63)<36?t.toString(36):t<62?(t-26).toString(36).toUpperCase():t>62?"-":"_")),""))(),e);return this.allSession.push(t),t}processMessage(t){var i;let{req:n}=t;if(n===r.MESSAGE_TYPE_INVITE){let i=new ge(this,t.cid,t.from,t.sdp,t.seq);this.allSession.push(i),this.emit(e.RtcClientEvent.invite,i)}else if(n===r.MESSAGE_TYPE_REINVITE){let i=this.allSession.find((e=>e.sessionId==t.cid));void 0!==i&&(i.inviteSeq=t.seq,i.inviteOffer=t.sdp,i.acceptCall(),i.emit(e.RtcSessionEvent.reInvite,i))}else if(n===r.MESSAGE_TYPE_ICE)null===(i=this.allSession.find((e=>e.sessionId===t.cid)))||void 0===i||i.reciveIceRequest(t);else if(n===r.MESSAGE_TYPE_BYE){let i=this.allSession.find((e=>e.sessionId===t.cid));void 0!==i&&(i.emit(e.RtcSessionEvent.bye),i.destory(!1))}n==o.FPRE&&this.emit(e.PTTEvent.FPREInfo,t),n==o.IMS&&this.emit(e.PTTEvent.IMSInfo,t)}destory(){void 0!==this.allSession&&this.allSession.forEach((e=>{e.destory()})),super.destory()}removeSession(e){void 0!==this.allSession&&(this.allSession=this.allSession.filter((t=>t.sessionId!==e.sessionId)))}onDisConnected(){super.onDisConnected(),this.destory()}}e.RtcClient=ke,e.authDevice=()=>t(void 0,void 0,void 0,(function*(){yield navigator.mediaDevices.getUserMedia({audio:!0,video:!0})})),e.createRtcClient=i=>t(void 0,void 0,void 0,(function*(){let t=new ke(i);yield t.reConnect(),yield t.initPttClient().catch((e=>{console.error(e)})),x.random=String(Date.now());let n=yield t.register();return n.status===e.RtcInviteStatus.ok?t:(t.destory(),Promise.reject(n))})),e.disableLog=()=>{d=!1},e.enableLog=()=>{d=!0},e.releaseStreamFromContainer=e=>{void 0!==e&&(e.pause(),e.removeAttribute("src"),e.removeAttribute("srcObject"))},e.renderStreamToContainer=a,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).UscRtc={})}(this,(function(e){"use strict";function t(e,t,i,n){return new(i||(i=Promise))((function(s,o){function r(e){try{c(n.next(e))}catch(e){o(e)}}function a(e){try{c(n.throw(e))}catch(e){o(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(r,a)}c((n=n.apply(e,t||[])).next())}))}var i,n,s,o,r;e.RtcSessionEvent=void 0,(i=e.RtcSessionEvent||(e.RtcSessionEvent={})).bye="bye",i.stream="stream",i.reInvite="reInvite",i.localAudioChange="localAudioChange",i.localVideoChange="localVideoChange",i.localStreamChange="localStreamChange",e.RtcClientEvent=void 0,(n=e.RtcClientEvent||(e.RtcClientEvent={})).connected="connected",n.disconnected="disconnected",n.invite="invite",e.PTTEvent=void 0,(s=e.PTTEvent||(e.PTTEvent={})).PTTInfo="PTTInfo",s.FPREInfo="FPREInfo",s.IMSInfo="IMSInfo",s.ERROR="error",s.SPEAKER="speaker",s.TALKING="talking",s.ACTIVE_GROUP="activeGroup",s.JOIN_INFO="joinInfo",function(e){e.MESSAGE_TYPE_REGISTER="REGISTER",e.MESSAGE_TYPE_INVITE="INVITE",e.MESSAGE_TYPE_REINVITE="reINVITE",e.MESSAGE_TYPE_ICE="ICE",e.MESSAGE_TYPE_INFO="INFO",e.MESSAGE_TYPE_BYE="BYE"}(o||(o={})),function(e){e.FPRE="fpre",e.IMS="ims",e.PTT_QUERY_ONLINE_USER="15",e.PTT_ANSWER_ONLINE_USER="115",e.PTT_QUERY_INFO="16",e.PTT_QUERY_INFO_CONFIRM="116"}(r||(r={}));const a=(e,t)=>{void 0!==e&&void 0!==t&&(t.srcObject=e,t.play())},c=e=>{e.onaddtrack=null,e.onremovetrack=null,e.getTracks().forEach((t=>{null==e||e.removeTrack(t),t.onended=null,t.onmute=null,t.onunmute=null,t.stop()}))};let d=!1;const l=(e,...t)=>{d&&console.log(`%c usc-rtc-devtools %c ${e} %c `,"background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)};var h,u,v;function p(){}function f(){f.init.call(this)}function m(e){return void 0===e._maxListeners?f.defaultMaxListeners:e._maxListeners}function g(e,t,i){if(t)e.call(i);else for(var n=e.length,s=S(e,n),o=0;o<n;++o)s[o].call(i)}function k(e,t,i,n){if(t)e.call(i,n);else for(var s=e.length,o=S(e,s),r=0;r<s;++r)o[r].call(i,n)}function y(e,t,i,n,s){if(t)e.call(i,n,s);else for(var o=e.length,r=S(e,o),a=0;a<o;++a)r[a].call(i,n,s)}function T(e,t,i,n,s,o){if(t)e.call(i,n,s,o);else for(var r=e.length,a=S(e,r),c=0;c<r;++c)a[c].call(i,n,s,o)}function C(e,t,i,n){if(t)e.apply(i,n);else for(var s=e.length,o=S(e,s),r=0;r<s;++r)o[r].apply(i,n)}function b(e,t,i,n){var s,o,r,a;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((o=e._events)?(o.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),o=e._events),r=o[t]):(o=e._events=new p,e._eventsCount=0),r){if("function"==typeof r?r=o[t]=n?[i,r]:[r,i]:n?r.unshift(i):r.push(i),!r.warned&&(s=m(e))&&s>0&&r.length>s){r.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+r.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=r.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else r=o[t]=i,++e._eventsCount;return e}function w(e,t,i){var n=!1;function s(){e.removeListener(t,s),n||(n=!0,i.apply(e,arguments))}return s.listener=i,s}function E(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function S(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}p.prototype=Object.create(null),f.EventEmitter=f,f.usingDomains=!1,f.prototype.domain=void 0,f.prototype._events=void 0,f.prototype._maxListeners=void 0,f.defaultMaxListeners=10,f.init=function(){this.domain=null,f.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new p,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},f.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},f.prototype.getMaxListeners=function(){return m(this)},f.prototype.emit=function(e){var t,i,n,s,o,r,a,c="error"===e;if(r=this._events)c=c&&null==r.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var d=new Error('Uncaught, unspecified "error" event. ('+t+")");throw d.context=t,d}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(i=r[e]))return!1;var l="function"==typeof i;switch(n=arguments.length){case 1:g(i,l,this);break;case 2:k(i,l,this,arguments[1]);break;case 3:y(i,l,this,arguments[1],arguments[2]);break;case 4:T(i,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(n-1),o=1;o<n;o++)s[o-1]=arguments[o];C(i,l,this,s)}return!0},f.prototype.addListener=function(e,t){return b(this,e,t,!1)},f.prototype.on=f.prototype.addListener,f.prototype.prependListener=function(e,t){return b(this,e,t,!0)},f.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,w(this,e,t)),this},f.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,w(this,e,t)),this},f.prototype.removeListener=function(e,t){var i,n,s,o,r;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(i=n[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new p:(delete n[e],n.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(s=-1,o=i.length;o-- >0;)if(i[o]===t||i[o].listener&&i[o].listener===t){r=i[o].listener,s=o;break}if(s<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new p,this;delete n[e]}else!function(e,t){for(var i=t,n=i+1,s=e.length;n<s;i+=1,n+=1)e[i]=e[n];e.pop()}(i,s);n.removeListener&&this.emit("removeListener",e,r||t)}return this},f.prototype.off=function(e,t){return this.removeListener(e,t)},f.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new p,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new p:delete i[e]),this;if(0===arguments.length){for(var n,s=Object.keys(i),o=0;o<s.length;++o)"removeListener"!==(n=s[o])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new p,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},f.prototype.listeners=function(e){var t,i=this._events;return i&&(t=i[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]},f.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):E.call(e,t)},f.prototype.listenerCount=E,f.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};class R extends f{constructor(){super(),this.localMediaStream=new MediaStream,this.videoConstrains={width:{max:1920},height:{max:1080},frameRate:{ideal:20,max:30}},this.audioConstrants={noiseSuppression:!0,echoCancellation:!0,autoGainControl:!0},super.setMaxListeners(0)}releaseTrack(e){var t;void 0!==e&&(null===(t=this.localMediaStream)||void 0===t||t.removeTrack(e),e.stop())}addDeviceChangeListener(e){navigator.mediaDevices.addEventListener("devicechange",e)}removeDeviceChangeListener(e){navigator.mediaDevices.removeEventListener("devicechange",e)}setVideoTrack(t){var i;this.emit(e.RtcSessionEvent.localVideoChange,{from:this.videoTrack,to:t}),this.videoTrack!==t&&(null===(i=this.localMediaStream)||void 0===i||i.addTrack(t),this.localTractChange(this.videoTrack,t)),this.videoTrack=t}setAudioTrack(t){var i;this.emit(e.RtcSessionEvent.localAudioChange,{from:this.audioTrack,to:t}),this.audioTrack!==t&&(null===(i=this.localMediaStream)||void 0===i||i.addTrack(t),this.localTractChange(this.audioTrack,t)),this.audioTrack=t}renderLocalStreamToContainer(e){a(this.localMediaStream,e)}getAllInputResource(){return t(this,void 0,void 0,(function*(){let e=yield navigator.mediaDevices.enumerateDevices();return{audio:e.filter((e=>"audioinput"===e.kind)),video:e.filter((e=>"videoinput"===e.kind))}}))}useDefaultDevice(e){return t(this,void 0,void 0,(function*(){let t=yield navigator.mediaDevices.getUserMedia({audio:!0,video:!0});this.setAudioTrack(t.getAudioTracks()[0]),this.setVideoTrack(t.getVideoTracks()[0]),this.renderLocalStreamToContainer(e)}))}useAudio(e){return t(this,void 0,void 0,(function*(){let t=void 0===e?{audio:Object.assign({},this.audioConstrants),video:!1}:{video:!1,audio:Object.assign({deviceId:e},this.audioConstrants)},i=yield navigator.mediaDevices.getUserMedia(t);return this.setAudioTrack(i.getAudioTracks()[0]),i.getAudioTracks()[0]}))}useDeviceVideo(e){return t(this,void 0,void 0,(function*(){let t=void 0===e?{audio:!1,video:Object.assign({},this.videoConstrains)}:{audio:!1,video:Object.assign({deviceId:e},this.videoConstrains)},i=yield navigator.mediaDevices.getUserMedia(t);return this.setVideoTrack(i.getVideoTracks()[0]),i.getVideoTracks()[0]}))}useDeskTopVideo(){return t(this,void 0,void 0,(function*(){let e=yield navigator.mediaDevices.getDisplayMedia({video:Object.assign({},this.videoConstrains),audio:!1});return this.setVideoTrack(e.getVideoTracks()[0]),e.getVideoTracks()[0]}))}disableAudio(){void 0!==this.audioTrack&&this.audioTrack.enabled&&(l("local audio status change",this.audioTrack),this.audioTrack.enabled=!1,this.setAudioTrack(this.audioTrack))}enableAudio(){void 0===this.audioTrack||this.audioTrack.enabled||(l("local audio status change",this.audioTrack),this.audioTrack.enabled=!0,this.setAudioTrack(this.audioTrack))}disableVideo(){void 0!==this.videoTrack&&this.videoTrack.enabled&&(l("local video status change",this.videoTrack),this.videoTrack.enabled=!1,this.setVideoTrack(this.videoTrack))}enableVideo(){void 0===this.videoTrack||this.videoTrack.enabled||(l("local video status change",this.videoTrack),this.videoTrack.enabled=!0,this.setVideoTrack(this.videoTrack))}close(){this.releaseTrack(this.audioTrack),this.releaseTrack(this.videoTrack),this.audioTrack=void 0,this.videoTrack=void 0,this.localMediaStream=void 0}}class _ extends R{constructor(){super(),this.videoCodecs=[],this.audioCodecs=[],this.audioReciveCodecs=[],this.preferCodec(),this.peerConnection=this.buildConnection()}buildConnection(){let e=new RTCPeerConnection;return e.addEventListener("connectionstatechange",(e=>l("connectionstatechange",e))),e.addEventListener("icecandidate",(e=>{l("icecandidate",e),this.onIceCandidate(e)})),e.addEventListener("datachannel",(e=>l("datachannel",e))),e.addEventListener("icecandidateerror",(e=>l("icecandidateerror",e))),e.addEventListener("iceconnectionstatechange",(e=>l("iceconnectionstatechange",e))),e.addEventListener("icegatheringstatechange",(e=>l("icegatheringstatechange",e))),e.addEventListener("negotiationneeded",(e=>l("negotiationneeded",e))),e.addEventListener("signalingstatechange",(e=>l("signalingstatechange",e))),e.addEventListener("track",(e=>this.reciveTrack(e))),e}localTractChange(e,i){return t(this,void 0,void 0,(function*(){if(void 0!==this.remoteStream&&void 0!==this.peerConnection)if(void 0!==i){let e=this.peerConnection.getSenders().find((e=>{var t;return(null===(t=e.track)||void 0===t?void 0:t.kind)===i.kind}));void 0!==e&&e.replaceTrack(i)}else{let t=this.peerConnection.getSenders().find((t=>{var i;return(null===(i=t.track)||void 0===i?void 0:i.kind)===(null==e?void 0:e.kind)}));void 0!==t&&this.peerConnection.removeTrack(t)}void 0!==i&&(i.onended=e=>l("local track end",e),i.onmute=e=>{l("local mute",e),this.emitLocalStreamChange()},i.onunmute=e=>{l("local unmute",e),this.emitLocalStreamChange()}),this.releaseTrack(e),this.emitLocalStreamChange()}))}isInMeeting(){return void 0!==this.remoteStream}hasRemoteVideo(e){var t;return null!==(t=null==e?void 0:e.getVideoTracks().some((e=>!1===e.muted&&"live"===e.readyState)))&&void 0!==t&&t}emitLocalStreamChange(){this.emit(e.RtcSessionEvent.localStreamChange,{audioOnly:!this.hasRemoteVideo(this.localMediaStream),stream:this.localMediaStream})}emitRemoteStreamChange(){this.emit(e.RtcSessionEvent.stream,{audioOnly:!this.hasRemoteVideo(this.remoteStream),stream:this.remoteStream})}trigerStreamChange(e=100){void 0!==this.emitStreamTimeout&&clearTimeout(this.emitStreamTimeout),this.emitStreamTimeout=setTimeout((()=>{this.emitRemoteStreamChange(),this.emitStreamTimeout=void 0}),e)}reciveTrack(e){l("recive track",e),void 0===this.remoteStream&&(this.remoteStream=new MediaStream),e.track.addEventListener("ended",(()=>this.trigerStreamChange())),e.track.addEventListener("mute",(()=>this.trigerStreamChange(3e3))),e.track.addEventListener("unmute",(()=>this.trigerStreamChange(10))),this.remoteStream.addTrack(e.track),this.trigerStreamChange()}preferCodec(e="H264",t="PCMA"){this.videoCodecs=[];let i=RTCRtpSender.getCapabilities("video");null!==i&&this.videoCodecs.push(...i.codecs.filter((t=>t.mimeType===`video/${e}`)));let n=RTCRtpReceiver.getCapabilities("video");null!==n&&this.videoCodecs.push(...n.codecs.filter((t=>t.mimeType===`video/${e}`))),this.audioCodecs=[];let s=RTCRtpSender.getCapabilities("audio");null!==s&&this.audioCodecs.push(...s.codecs.filter((e=>e.mimeType===`audio/${t}`)));let o=RTCRtpReceiver.getCapabilities("audio");null!==o&&this.audioCodecs.push(...o.codecs.filter((e=>e.mimeType===`audio/${t}`)))}trackIsInPeerConnection(e){var t,i;return null!==(i=null===(t=this.peerConnection)||void 0===t?void 0:t.getSenders().some((t=>{var i;return(null===(i=t.track)||void 0===i?void 0:i.id)===e.id})))&&void 0!==i&&i}updateTracks(){if(void 0===this.peerConnection||"closed"===this.peerConnection.signalingState)throw new Error("session is closed !");if(void 0===this.audioTrack&&void 0===this.videoCodecs)throw new Error("no input found");if(void 0!==this.audioTrack&&!this.trackIsInPeerConnection(this.audioTrack))try{this.peerConnection.addTrack(this.audioTrack)}catch(e){}if(void 0!==this.videoTrack&&!this.trackIsInPeerConnection(this.videoTrack))try{this.peerConnection.addTrack(this.videoTrack)}catch(e){}try{this.peerConnection.getTransceivers().forEach((e=>{null!==e.sender.track&&("video"===e.sender.track.kind?e.setCodecPreferences([...this.videoCodecs]):"audio"===e.sender.track.kind&&e.setCodecPreferences([...this.audioCodecs]))}))}catch(e){((e,...t)=>{d&&console.log(`%c usc-rtc-devtools %c ${e} %c `,"background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#ff0000 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)})("preferCodec error",e,this.audioCodecs,this.videoCodecs)}}createOffer(){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");this.updateTracks();let e=yield this.peerConnection.createOffer();return e=class{static formatOffer(e){let{sdp:t}=e;if(void 0===t)throw new Error("offer sdp is empty");return t=t.split("\r\n").filter((e=>-1===e.indexOf("a=group:BUNDLE"))).join("\r\n"),e.sdp=t,e}}.formatOffer(e),yield this.peerConnection.setLocalDescription(e),e}))}setOffer(e){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");yield this.peerConnection.setRemoteDescription({type:"offer",sdp:e})}))}createAnswer(){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");this.updateTracks();let e=yield this.peerConnection.createAnswer();return this.peerConnection.setLocalDescription(e),e}))}setAnswer(e){return t(this,void 0,void 0,(function*(){if(void 0===this.peerConnection)throw new Error("session is closed !");yield this.peerConnection.setRemoteDescription({type:"answer",sdp:e})}))}setRemoteIce(e){if(void 0===this.peerConnection)throw new Error("session is closed !");let t=new RTCIceCandidate(e);this.peerConnection.addIceCandidate(t)}closeConnection(e){e.close(),e.ontrack=null,e.onicecandidate=null,e.oniceconnectionstatechange=null,e.onsignalingstatechange=null,e.onicegatheringstatechange=null,e.onnegotiationneeded=null,e.onicecandidateerror=null,e.ondatachannel=null,e.onconnectionstatechange=null}close(){super.close(),void 0!==this.peerConnection&&(this.closeConnection(this.peerConnection),this.peerConnection=void 0),void 0!==this.remoteStream&&c(this.remoteStream)}}e.RtcInviteStatus=void 0,(h=e.RtcInviteStatus||(e.RtcInviteStatus={}))[h.timeout=408]="timeout",h[h.reject=488]="reject",h[h.busy=486]="busy",h[h.notfound=404]="notfound",h[h.notAllowedMedia=415]="notAllowedMedia",h[h.ok=200]="ok";class L extends _{constructor(e,t,i){super(),this.sessionId=t,this.rtcClient=e,this.remoteNumber=i}reciveIceRequest({ice:e}){this.setRemoteIce(JSON.parse(e))}getRemoteNumber(){return this.remoteNumber}onIceCandidate(e){}destory(e=!0){this.removeAllListeners(),void 0!==this.rtcClient&&(void 0!==this.remoteNumber&&(e&&this.rtcClient.sendBye(this.remoteNumber,this.sessionId),this.remoteNumber=void 0),this.rtcClient.removeSession(this)),this.rtcClient=void 0,super.close()}rejectCall(){let[t,i,n]=this.checkIsNotUndefined(this.rtcClient,this.remoteNumber,this.inviteSeq);t.answer(e.RtcInviteStatus.reject,n,i,this.sessionId),this.destory()}acceptCall(){return t(this,void 0,void 0,(function*(){if(void 0!==this.inviteOffer){yield this.setOffer(this.inviteOffer);let[t,i,n]=this.checkIsNotUndefined(this.rtcClient,this.remoteNumber,this.inviteSeq),{sdp:s}=yield this.createAnswer();t.answer(e.RtcInviteStatus.ok,n,i,this.sessionId,s)}}))}pickNewConnection(){return t(this,void 0,void 0,(function*(){void 0!==this.peerConnection&&this.closeConnection(this.peerConnection),this.peerConnection=this.buildConnection(),void 0!==this.remoteStream&&(c(this.remoteStream),this.remoteStream=void 0,this.acceptCall())}))}checkIsNotUndefined(...e){for(const t of e)if(void 0===t)throw new Error("someone is undefined");return e}sendDtmf(e){var i;return t(this,void 0,void 0,(function*(){null===(i=this.rtcClient)||void 0===i||i.sendDtmf(this.remoteNumber,this.sessionId,e)}))}}e.RtcCalledType=void 0,(u=e.RtcCalledType||(e.RtcCalledType={})).audio="audio",u.video="video",u.audioVideo="audioVideo",u.unknown="unknown";class I extends L{constructor(){super(...arguments),this.responseCallType=e.RtcCalledType.unknown}start(){return t(this,void 0,void 0,(function*(){let[t,i]=this.checkIsNotUndefined(this.rtcClient,this.remoteNumber),n=yield this.createOffer(),{status:s,sdp:o}=yield t.invite(i,this.sessionId,n.sdp);s===e.RtcInviteStatus.ok&&(yield this.setAnswer(o));const r=o.split("\r\n"),a=r.find((e=>e.startsWith("m=audio"))),c=r.find((e=>e.startsWith("m=video")));return this.responseCallType=a&&c?e.RtcCalledType.audioVideo:a?e.RtcCalledType.audio:c?e.RtcCalledType.video:e.RtcCalledType.unknown,s}))}addRemote(e){this.remoteNumber=e}}class x{constructor(e,t,i,n){this.req=e,this.seq=x.seqNum++,this.from=t,this.to=i,this.registerid=`${x.random}@${t}`,void 0!==n&&Object.assign(this,n)}}x.random=String(Date.now()),x.seqNum=1,function(e){e.connected="connected",e.disconnected="disconnected",e.message="message"}(v||(v={}));var M=null;try{var N="undefined"!=typeof module&&"function"==typeof module.require&&module.require("worker_threads")||"function"==typeof __non_webpack_require__&&__non_webpack_require__("worker_threads")||"function"==typeof require&&require("worker_threads");M=N.Worker}catch(e){}function G(e,t,i){var n=void 0===t?null:t,s=function(e,t){return Buffer.from(e,"base64").toString(t?"utf16":"utf8")}(e,void 0!==i&&i),o=s.indexOf("\n",10)+1,r=s.substring(o)+(n?"//# sourceMappingURL="+n:"");return function(e){return new M(r,Object.assign({},e,{eval:!0}))}}function P(e,t,i){var n=void 0===t?null:t,s=function(e,t){var i=atob(e);if(t){for(var n=new Uint8Array(i.length),s=0,o=i.length;s<o;++s)n[s]=i.charCodeAt(s);return String.fromCharCode.apply(null,new Uint16Array(n.buffer))}return i}(e,void 0!==i&&i),o=s.indexOf("\n",10)+1,r=s.substring(o)+(n?"//# sourceMappingURL="+n:""),a=new Blob([r],{type:"application/javascript"});return URL.createObjectURL(a)}var A="[object process]"===Object.prototype.toString.call("undefined"!=typeof process?process:0);var W,Z,V,Y=(W="Lyogcm9sbHVwLXBsdWdpbi13ZWItd29ya2VyLWxvYWRlciAqLwohZnVuY3Rpb24oKXsidXNlIHN0cmljdCI7bGV0IGUsdD0hMTtjb25zdCBzPXNlbGYsYT17fSxuPXt0aW1lb3V0OjVlMyx0aW1lb3V0T2JqOm51bGwscmVzZXQoKXtyZXR1cm4gY2xlYXJJbnRlcnZhbCh0aGlzLnRpbWVvdXRPYmopLHRoaXN9LHJlZ2lzdGVyKCl7MT09PWUucmVhZHlTdGF0ZSYmZS5zZW5kKEpTT04uc3RyaW5naWZ5KFJlZ2lzdGVyKSl9LHNlbmRPbmxpbmUoKXsxPT09ZS5yZWFkeVN0YXRlJiZlLnNlbmQoSlNPTi5zdHJpbmdpZnkoYSkpfSxzdGFydCgpe3RoaXMudGltZW91dE9iaj1zZXRJbnRlcnZhbCgoKCk9Pnt0aGlzLnNlbmRPbmxpbmUoKX0pLHRoaXMudGltZW91dCl9fTtzZWxmLmFkZEV2ZW50TGlzdGVuZXIoIm1lc3NhZ2UiLChmdW5jdGlvbihzKXtjb25zdCByPXMuZGF0YTtzd2l0Y2goci5jbWQpe2Nhc2UiY29uZmlnIjpPYmplY3QuYXNzaWduKGEsci5oZWFydEJlYXQpLG4udGltZW91dD1yLmhlYXJ0VGltZTticmVhaztjYXNlInN0YXJ0IjpzZWxmLnBvc3RNZXNzYWdlKHt0eXBlOiJ3ZWJXb3JrZXIiLGRhdGE6IuWQr+WKqHdlYldvcmtlciJ9KSxvKHIudXJsKTticmVhaztjYXNlInN0b3AiOnQ9ITAsZS5jbG9zZSgpLHNlbGYucG9zdE1lc3NhZ2Uoe3R5cGU6IndlYldvcmtlciIsZGF0YToi5YGc5q2id2ViV29ya2VyIn0pLHNlbGYucG9zdE1lc3NhZ2Uoe3R5cGU6ImNsb3NlIixkYXRhOiJjbG9zZSIrKG5ldyBEYXRlKS50b1RpbWVTdHJpbmcoKX0pLHNlbGYuY2xvc2UoKTticmVhaztjYXNlIm1zZyI6MT09PWUucmVhZHlTdGF0ZSYmZS5zZW5kKEpTT04uc3RyaW5naWZ5KHIuZGF0YSkpfX0pLCExKTtjb25zdCBvPWZ1bmN0aW9uKHQpe2V8fChlPW5ldyBXZWJTb2NrZXQodCksZS5vbmVycm9yPWZ1bmN0aW9uKGUpe3IodCl9LGUub25jbG9zZT1mdW5jdGlvbihlKXtyKHQpfSxlLm9ub3Blbj1lPT57cy5wb3N0TWVzc2FnZSh7dHlwZToib3BlbiIsZGF0YToic3RhcnQiKyhuZXcgRGF0ZSkudG9UaW1lU3RyaW5nKCl9KSxuLnJlc2V0KCkuc3RhcnQoKX0sZS5vbm1lc3NhZ2U9ZT0+e2NvbnN0IHQ9SlNPTi5wYXJzZShlLmRhdGEpO3MucG9zdE1lc3NhZ2Uoe3R5cGU6IndlYlNvY2tldCIsZGF0YTp0fSl9KX07ZnVuY3Rpb24gcihzKXt0fHwodD0hMCxzZXRUaW1lb3V0KChmdW5jdGlvbigpe2U9bnVsbCxvKHMpLHQ9ITF9KSwyZTMpKX19KCk7Cgo=",Z=null,V=!1,A?G(W,Z,V):function(e,t,i){var n;return function(s){return n=n||P(e,t,i),new Worker(n,s)}}(W,Z,V));class O extends f{constructor(e,t,i){super(),this.server=e,this.ws=new Y,this.configHeartBeat(t,i)}connect(){this.initWebsocket(),this.ws.onmessage=e=>{const{type:t,data:i}=e.data;switch(t){case"webSocket":return l("websocket recive message ",i),void this.emit(v.message,i);case"open":return l("websocket connected ",this.server),void this.emit(v.connected)}}}configHeartBeat(e,t){this.ws.postMessage({cmd:"config",heartBeat:e,heartTime:t})}initWebsocket(){this.ws.postMessage({cmd:"start",url:this.server})}send(e){this.ws.postMessage({cmd:"msg",data:e})}close(){this.ws.postMessage({cmd:"close"})}sendMessage(e){l("websocket send message ",e),void 0!==e&&void 0!==this.ws&&this.ws.postMessage({cmd:"msg",data:e})}}class j extends f{constructor(e,t,i,n){super(),this.activeServer="",this.server=e,this.localNumber=t,this.expireTime=i,this.heartTime=n,super.setMaxListeners(0)}testWs(e){return t(this,void 0,void 0,(function*(){return e.reduce(((e,t)=>e.then((e=>Promise.resolve(e)),(e=>new Promise(((i,n)=>{const s=new WebSocket(t);s.onopen=()=>{e.push(t),s.close(),i(e)},s.onclose=()=>{n(e)}}))))),Promise.reject([])).then((e=>Promise.resolve(e)),(e=>Promise.resolve(e)))}))}reConnect(){return t(this,void 0,void 0,(function*(){this.destory();let e=[];void 0!==this.server&&(e="string"==typeof this.server?[this.server]:this.server);const t=yield this.testWs(e);return new Promise(((e,i)=>{if(t.length>0){this.activeServer=t[0];let n=new O(this.activeServer,new x(o.MESSAGE_TYPE_REGISTER,this.localNumber,this.localNumber,{expire:this.expireTime}),this.heartTime);n.on(v.connected,(()=>{this.socketWorker=n,this.onConnected(),e()})),n.on(v.disconnected,(e=>{this.socketWorker=void 0,this.onDisConnected(e),i("socketWorker connecion failed")})),n.on(v.message,(e=>this.onReviceMessage(e))),n.connect()}else i("server is empty")}))}))}sendMessage(e){if(void 0===this.socketWorker)throw new Error("socketWorker is closed");this.socketWorker.sendMessage(e)}destory(){void 0!==this.socketWorker&&(this.unRegister(),this.socketWorker.close(),this.socketWorker=void 0)}}class U extends j{constructor(){super(...arguments),this.asyncProducer=new Map}sendRespondableMessage(e){return t(this,void 0,void 0,(function*(){return this.sendMessage(e),new Promise((t=>this.asyncProducer.set(e.seq,t)))}))}getAsyncResolve(e){let t=this.asyncProducer.get(e);return void 0!==t&&this.asyncProducer.delete(e),t}onDisConnected(t){this.asyncProducer.clear(),this.emit(e.RtcClientEvent.disconnected,t)}onReviceMessage(e){var t;let{status:i,seq:n}=e;void 0!==i?null===(t=this.getAsyncResolve(n))||void 0===t||t.call(void 0,e):this.processMessage(e)}}class X extends U{register(){return t(this,void 0,void 0,(function*(){let e=new x(o.MESSAGE_TYPE_REGISTER,this.localNumber,this.localNumber,{expire:this.expireTime});return this.sendRespondableMessage(e)}))}unRegister(){let e=new x(o.MESSAGE_TYPE_REGISTER,this.localNumber,this.localNumber,{expire:0});this.sendMessage(e)}invite(e,i,n){return t(this,void 0,void 0,(function*(){return this.sendRespondableMessage(new x(o.MESSAGE_TYPE_INVITE,this.localNumber,e,{cid:i,sdp:n}))}))}sendIce(e,t,i){}sendBye(e,t){return this.sendMessage(new x(o.MESSAGE_TYPE_BYE,this.localNumber,e,{cid:t}))}answer(e,t,i,n,s){this.sendMessage({status:e,seq:t,from:i,to:this.localNumber,cid:n,sdp:s})}sendPttMessage(e){this.sendMessage(e)}sendDtmf(e,i,n){return t(this,void 0,void 0,(function*(){return this.sendMessage(new x(o.MESSAGE_TYPE_INFO,this.localNumber,e,{cid:i,info:{msg:"dtmf",dtmf:n}}))}))}}function K(){}function D(){D.init.call(this)}function z(e){return void 0===e._maxListeners?D.defaultMaxListeners:e._maxListeners}function q(e,t,i){if(t)e.call(i);else for(var n=e.length,s=te(e,n),o=0;o<n;++o)s[o].call(i)}function F(e,t,i,n){if(t)e.call(i,n);else for(var s=e.length,o=te(e,s),r=0;r<s;++r)o[r].call(i,n)}function H(e,t,i,n,s){if(t)e.call(i,n,s);else for(var o=e.length,r=te(e,o),a=0;a<o;++a)r[a].call(i,n,s)}function J(e,t,i,n,s,o){if(t)e.call(i,n,s,o);else for(var r=e.length,a=te(e,r),c=0;c<r;++c)a[c].call(i,n,s,o)}function B(e,t,i,n){if(t)e.apply(i,n);else for(var s=e.length,o=te(e,s),r=0;r<s;++r)o[r].apply(i,n)}function Q(e,t,i,n){var s,o,r,a;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((o=e._events)?(o.newListener&&(e.emit("newListener",t,i.listener?i.listener:i),o=e._events),r=o[t]):(o=e._events=new K,e._eventsCount=0),r){if("function"==typeof r?r=o[t]=n?[i,r]:[r,i]:n?r.unshift(i):r.push(i),!r.warned&&(s=z(e))&&s>0&&r.length>s){r.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+r.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=r.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else r=o[t]=i,++e._eventsCount;return e}function $(e,t,i){var n=!1;function s(){e.removeListener(t,s),n||(n=!0,i.apply(e,arguments))}return s.listener=i,s}function ee(e){var t=this._events;if(t){var i=t[e];if("function"==typeof i)return 1;if(i)return i.length}return 0}function te(e,t){for(var i=new Array(t);t--;)i[t]=e[t];return i}K.prototype=Object.create(null),D.EventEmitter=D,D.usingDomains=!1,D.prototype.domain=void 0,D.prototype._events=void 0,D.prototype._maxListeners=void 0,D.defaultMaxListeners=10,D.init=function(){this.domain=null,D.usingDomains&&undefined.active,this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new K,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},D.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},D.prototype.getMaxListeners=function(){return z(this)},D.prototype.emit=function(e){var t,i,n,s,o,r,a,c="error"===e;if(r=this._events)c=c&&null==r.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var d=new Error('Uncaught, unspecified "error" event. ('+t+")");throw d.context=t,d}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(i=r[e]))return!1;var l="function"==typeof i;switch(n=arguments.length){case 1:q(i,l,this);break;case 2:F(i,l,this,arguments[1]);break;case 3:H(i,l,this,arguments[1],arguments[2]);break;case 4:J(i,l,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(n-1),o=1;o<n;o++)s[o-1]=arguments[o];B(i,l,this,s)}return!0},D.prototype.addListener=function(e,t){return Q(this,e,t,!1)},D.prototype.on=D.prototype.addListener,D.prototype.prependListener=function(e,t){return Q(this,e,t,!0)},D.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,$(this,e,t)),this},D.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,$(this,e,t)),this},D.prototype.removeListener=function(e,t){var i,n,s,o,r;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(n=this._events))return this;if(!(i=n[e]))return this;if(i===t||i.listener&&i.listener===t)0==--this._eventsCount?this._events=new K:(delete n[e],n.removeListener&&this.emit("removeListener",e,i.listener||t));else if("function"!=typeof i){for(s=-1,o=i.length;o-- >0;)if(i[o]===t||i[o].listener&&i[o].listener===t){r=i[o].listener,s=o;break}if(s<0)return this;if(1===i.length){if(i[0]=void 0,0==--this._eventsCount)return this._events=new K,this;delete n[e]}else!function(e,t){for(var i=t,n=i+1,s=e.length;n<s;i+=1,n+=1)e[i]=e[n];e.pop()}(i,s);n.removeListener&&this.emit("removeListener",e,r||t)}return this},D.prototype.off=function(e,t){return this.removeListener(e,t)},D.prototype.removeAllListeners=function(e){var t,i;if(!(i=this._events))return this;if(!i.removeListener)return 0===arguments.length?(this._events=new K,this._eventsCount=0):i[e]&&(0==--this._eventsCount?this._events=new K:delete i[e]),this;if(0===arguments.length){for(var n,s=Object.keys(i),o=0;o<s.length;++o)"removeListener"!==(n=s[o])&&this.removeAllListeners(n);return this.removeAllListeners("removeListener"),this._events=new K,this._eventsCount=0,this}if("function"==typeof(t=i[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},D.prototype.listeners=function(e){var t,i=this._events;return i&&(t=i[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),i=0;i<t.length;++i)t[i]=e[i].listener||e[i];return t}(t):[]},D.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):ee.call(e,t)},D.prototype.listenerCount=ee,D.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};const ie="error",ne="speaker",se="talking",oe="activeGroup",re="joinInfo",ae="disConnected",ce={400:"消息不符规范",401:"验证不通过",403:"无权利操作",404:"不存在该用户",415:"错误参数",405:"无法操作",424:"不存在XD群",425:"管理员没有",426:"已存在管理员",427:"以前被拒绝过,或者删除过",428:"超出所配置的下属管理员的数量",429:"群内无用户",430:"管理员的索引号冲突",431:"还有子管理员存在"},de="connected",le="disconnected",he="message",ue=(e,...t)=>{console.log(`%c usc-ptt-devtools %c ${e} %c `,"background:#16aae0 ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#14e04e ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)},ve=(e,...t)=>{console.log(`%c usc-ptt-devtools %c ${e} %c `,"background:#16aae0 ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff","background:#e02514 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff","background:transparent",...t)};class pe extends D{server="";ws=null;constructor(e){super(),this.server=e}connect(){this.ws=new WebSocket(this.server),this.ws.onopen=()=>{ue("websocket connected ",this.server),this.emit(de)},this.ws.onclose=()=>{ue("websocket disconnected ",this.server),this.ws=void 0,this.emit(le)},this.ws.onmessage=({data:e})=>{let t=JSON.parse(e);ue("websocket recive message ",t),this.emit(he,t)}}close(){void 0!==this.ws&&(this.ws.close(),this.ws=void 0)}sendMessage(e){ue("websocket send message ",e),void 0!==e&&void 0!==this.ws&&1===this.ws.readyState&&this.ws.send(JSON.stringify(e))}}class fe extends D{socket=void 0;timer=void 0;waitinggroup=void 0;speaker="";activeGroup="";talking=!1;constructor(e,t=20,i=!0){if(super(),!e)return ve("服务器地址为空!");this.server=e,this.heartTime=t,this.autoReconnect=i}async initClient(){return this.destorySocket(),new Promise(((e,t)=>{if(void 0!==this.server){let i=new pe(this.server);i.on(de,(()=>{this.socket=i,this.timer=setInterval((()=>{this.sendMessage({req:0})}),1e3*this.heartTime),e()})),i.on(le,(()=>{this.socket=void 0,this.autoReconnect?setTimeout((()=>{this.initClient()}),3e3):this.emit(ae),t("socket connecion failed")})),i.on(he,(e=>this.onReviceMessage(e))),i.connect()}else t("server is empty")}))}onReviceMessage(e){ue(e);const{info:t,msg:i,res:n}=e;if(10===t&&(this.speaker=`${i.speaker}`,ue(`${this.speaker}正在说话`),this.emit(ne,this.speaker)),11===t&&(200===n.code?(this.activeGroup=this.waitinggroup,ue(`加入组${this.activeGroup}`)):(this.activeGroup="",ve(ce[n.code]),this.emit(ie,ce[n.code])),this.emit(oe,this.activeGroup)),13===t&&(200===n.code?this.talking=!0:this.talking=!1,ue(`talking:${this.talking}`),this.emit(se,this.talking)),14===t&&(this.speaker="",ue(`${this.speaker}停止说话`),this.emit(ne,this.speaker)),15===t){const e={user:i.number,group:i.g_num,code:i.status};ue(`${e}进入组`),this.emit(re,e)}}joinGroup(e,t,i,n){this.socket&&(this.waitinggroup=e,this.sendMessage({req:1,group:e,user:t,ptts:i,port:n}))}quitGroup(){this.socket&&(this.activeGroup="",this.emit(oe,this.activeGroup),this.sendMessage({req:2}))}talk(){this.socket&&this.sendMessage({req:3})}stop(){this.talking=!1,ue(`talking:${this.talking}`),this.emit(se,this.talking),this.socket&&this.sendMessage({req:4})}sendMessage(e){if(void 0===this.socket)throw new Error("socket is closed");this.socket.sendMessage(e)}destorySocket(){void 0!==this.socket&&(this.socket.close(),this.socket=void 0)}destory(){this.destorySocket()}}class me extends X{constructor(e){let t=e.expireTime||me.defaultExpireTime;super(e.server,e.localNumber,t,e.heartTime||5),this.enablePtt=e.enablePtt,this.pttServer=e.pttServer}initPttClient(){return t(this,void 0,void 0,(function*(){if(this.enablePtt){if(!this.pttServer)return Promise.reject("pttServer不存在");{const e=yield(async(e,t=20,i=!0)=>{let n=new fe(e,t,i);return await n.initClient(),n})(this.pttServer);if(!e)return Promise.reject("ptt客户端初始化失败!");e.on(oe,(e=>{this.emit(oe,e)})),e.on(ne,(e=>{this.emit(ne,e)})),e.on(se,(e=>{this.emit(se,e)})),e.on(re,(e=>{this.emit(re,e)})),e.on(ae,(e=>{this.emit(ae,e)})),this.pttClient=e}}}))}joinGroup(e,t,i,n){var s;null===(s=this.pttClient)||void 0===s||s.joinGroup(e,t,i,n)}quitGroup(){var e;null===(e=this.pttClient)||void 0===e||e.quitGroup()}talk(){var e;null===(e=this.pttClient)||void 0===e||e.talk()}stop(){var e;null===(e=this.pttClient)||void 0===e||e.stop()}onConnected(){}onDisConnected(){super.onDisConnected()}}me.defaultExpireTime=60;class ge extends L{constructor(t,i,n,s,o){super(t,i,n),this.callType=e.RtcCalledType.unknown,this.inviteSeq=o,this.inviteOffer=s;const r=s.split("\r\n"),a=r.find((e=>e.startsWith("m=audio"))),c=r.find((e=>e.startsWith("m=video")));this.callType=a&&c?e.RtcCalledType.audioVideo:a?e.RtcCalledType.audio:c?e.RtcCalledType.video:e.RtcCalledType.unknown}}class ke extends me{constructor(){super(...arguments),this.allSession=[]}createSession(e){let t=new I(this,((e=21)=>crypto.getRandomValues(new Uint8Array(e)).reduce(((e,t)=>e+((t&=63)<36?t.toString(36):t<62?(t-26).toString(36).toUpperCase():t>62?"-":"_")),""))(),e);return this.allSession.push(t),t}processMessage(t){var i;let{req:n}=t;if(n===o.MESSAGE_TYPE_INVITE){let i=new ge(this,t.cid,t.from,t.sdp,t.seq);this.allSession.push(i),this.emit(e.RtcClientEvent.invite,i)}else if(n===o.MESSAGE_TYPE_REINVITE){let i=this.allSession.find((e=>e.sessionId==t.cid));void 0!==i&&(i.inviteSeq=t.seq,i.inviteOffer=t.sdp,i.acceptCall(),i.emit(e.RtcSessionEvent.reInvite,i))}else if(n===o.MESSAGE_TYPE_ICE)null===(i=this.allSession.find((e=>e.sessionId===t.cid)))||void 0===i||i.reciveIceRequest(t);else if(n===o.MESSAGE_TYPE_BYE){let i=this.allSession.find((e=>e.sessionId===t.cid));void 0!==i&&(i.emit(e.RtcSessionEvent.bye),i.destory(!1))}n==r.FPRE&&this.emit(e.PTTEvent.FPREInfo,t),n==r.IMS&&this.emit(e.PTTEvent.IMSInfo,t)}destory(){void 0!==this.allSession&&this.allSession.forEach((e=>{e.destory()})),super.destory()}removeSession(e){void 0!==this.allSession&&(this.allSession=this.allSession.filter((t=>t.sessionId!==e.sessionId)))}onDisConnected(){super.onDisConnected(),this.destory()}}e.RtcClient=ke,e.authDevice=()=>t(void 0,void 0,void 0,(function*(){yield navigator.mediaDevices.getUserMedia({audio:!0,video:!0})})),e.createRtcClient=i=>t(void 0,void 0,void 0,(function*(){let t=new ke(i);yield t.reConnect(),yield t.initPttClient().catch((e=>{console.error(e)})),x.random=String(Date.now());let n=yield t.register();return n.status===e.RtcInviteStatus.ok?t:(t.destory(),Promise.reject(n))})),e.disableLog=()=>{d=!1},e.enableLog=()=>{d=!0},e.releaseStreamFromContainer=e=>{void 0!==e&&(e.pause(),e.removeAttribute("src"),e.removeAttribute("srcObject"))},e.renderStreamToContainer=a,Object.defineProperty(e,"__esModule",{value:!0})}));
|
|
2
2
|
//# sourceMappingURL=index.js.map
|