-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportsip-web-sdk.js
1 lines (1 loc) · 25.8 KB
/
portsip-web-sdk.js
1
var e,s,t,i,n,o,r,a,l=this&&this.__classPrivateFieldSet||function(e,s,t,i,n){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof s?e!==s||!n:!s.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?n.call(e,t):n?n.value=t:s.set(e,t),t},d=this&&this.__classPrivateFieldGet||function(e,s,t,i){if("a"===t&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?e!==s||!i:!s.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===t?i:"a"===t?i.call(e):i?i.value:s.get(e)};import{Inviter as c,Invitation as u,Session as h,SessionState as v,Subscriber as m,UserAgent as p,Registerer as f,Messager as g}from"../sdk/sipjs";import{addScreenMarkModifier as y,inactiveScreenModifer as w,inactiveVideoModifer as S,sendScreenModifer as D}from"./modifiers";export default class P{constructor(i,n){if(e.add(this),this.videoMaxBitrate=1024e3,this.videoResolusion={width:{max:1920},height:{max:1080}},s.set(this,""),t.set(this,null),Array.isArray(n.remoteAudioID)&&Array.isArray(n.remoteVideoID)){const e=n.remoteAudioID.length,s=n.remoteVideoID.length;if(e!==s)throw Error("An audio tag must matches a video tag");if(s>16)throw Error("The maximum number of media elements passed in is 16")}this.event=i,this.sessions=new Map,this.subscribers=new Map,this.userAgent=null,this.registerer=null,this.UADelegate={onConnect:()=>{this.event.onConnect(),this.registerServer().then((()=>{this.event.onRegisterSuccess()})).catch((e=>{l(this,s,"","f"),this.event.onRegisterFailure(e)}))},onDisconnect:e=>{this.event.onDisconnect((null==e?void 0:e.message)||"user agent disconnect"),this.sessions.size&&this.sessions.clear(),this.registerer&&(this.registerer.unregister(),this.registerer=null),this.userAgent&&this.userAgent.reconnect()},onInvite:e=>{var s,t;const i=null!==(s=e.remoteIdentity.uri.user)&&void 0!==s?s:"",n=e.remoteIdentity.displayName,o="host=true"===e.request.getHeader("X-Host-Conference");e.delegate=this.sessionDelegate;if(this.sessions.get(i))throw new Error("session is existed");const r=null!==(t=e.body)&&void 0!==t?t:"";let a=!1;const l=Boolean(e.request.getHeader("Alert-Info")||e.request.getHeader("Call-Info"));/\r\nm=video /.test(r)?(this.initVideoCall(i),a=!0):this.initAudioCall(i);const d=this.sessions.get(i);if(!d)throw new Error("session not exist");d.session=e,this.event.onInviteIncoming(i,n,o,!0,a,l),e.stateChange.addListener((s=>{var t,n,r;switch(s){case v.Initial:case v.Establishing:break;case v.Established:{try{this.setupRemoteMedia(i),this.setupLocalMedia(i)}catch(e){}const s=null!==(r=null===(n=null===(t=e.dialog)||void 0===t?void 0:t.answer)||void 0===n?void 0:n.content)&&void 0!==r?r:"";s.split(/^m=/gm).some((e=>{if("video"===e.substring(0,5))return e.includes("a=inactive")}))&&this.initAudioCall(i),this.event.onInviteConnected(i,Boolean(d.constraints.video),o,"recieve"),this.setSessionMediaProperty(i,s);break}case v.Terminating:break;case v.Terminated:this.cleanupMedia(i).catch((e=>{})),e.isCanceled&&this.event.onInviteClosed(i),this.sessions.delete(i);break;default:throw new Error("Unknown session state.")}}))},onMessage:e=>{var s,t;e.accept();const i=null!==(s=e.request.getHeader("Content-Type"))&&void 0!==s?s:"",n=null!==(t=e.request.from.uri.user)&&void 0!==t?t:"";this.event.onRecvOutOfDialogMessage(n,e.request.body,i)},onRefer:e=>{e.accept().then((()=>{e.makeInviter().invite()}))}},this.sessionDelegate={onInfo:e=>{var s;e.accept();const t=null!==(s=e.request.from.uri.user)&&void 0!==s?s:"";this.event.onRecvInfo(t,e.request.body),e.request.body.includes("Signal=")&&this.event.onRecvDtmfTone(t,e.request.body[7])},onInvite:e=>{const s=e.from.uri.user;if(s){const t=this.sessions.get(s);if(!t)throw new Error("incoming in dialog INVITE request but session not exist");const i=JSON.parse(JSON.stringify(null==t?void 0:t.mediaProperty));if(!this.setSessionMediaProperty(s,e.body))return;try{this.setupRemoteMedia(s)}catch(e){}const n="sendrecv"===(null==t?void 0:t.mediaProperty.audio)||"recvonly"===(null==t?void 0:t.mediaProperty.audio),o="sendrecv"===(null==t?void 0:t.mediaProperty.video)||"recvonly"===(null==t?void 0:t.mediaProperty.video),r="sendonly"===(null==t?void 0:t.mediaProperty.screen);this.event.onInviteUpdated(s,n,o,r),"sendonly"!==(null==t?void 0:t.mediaProperty.audio)&&"inactive"!==(null==t?void 0:t.mediaProperty.audio)||this.event.onRemoteHold(s),("sendonly"!==i.audio&&"inactive"!==(null==t?void 0:t.mediaProperty.audio)||"sendrecv"!==(null==t?void 0:t.mediaProperty.audio)&&"recvonly"!==(null==t?void 0:t.mediaProperty.audio))&&("inactive"!==i.audio||"recvonly"!==(null==t?void 0:t.mediaProperty.audio))||this.event.onRemoteUnHold(s)}},onBye:e=>{const s=e.request.from.uri.user;s&&this.event.onInviteClosed(s)},onMessage:e=>{var s,t;e.accept();const i=null!==(s=e.request.getHeader("Content-Type"))&&void 0!==s?s:"",n=null!==(t=e.request.from.uri.user)&&void 0!==t?t:"";this.event.onRecvMessage(n,e.request.body,i)}},this.remoteAudioID="string"==typeof n.remoteAudioID?n.remoteAudioID:n.remoteAudioID.map((e=>({id:e,isPlaying:!1,ext:""}))),this.remoteVideoID="string"==typeof n.remoteVideoID?n.remoteVideoID:n.remoteVideoID?n.remoteVideoID.map((e=>({id:e,isPlaying:!1,ext:""}))):n.remoteVideoID,this.remoteScreenID=n.remoteScreenID,this.localVideoID=n.localVideoID}async createUserAgent(e,t,i,n,o="",r=""){try{const a={authorizationUsername:e,authorizationPassword:t,transportOptions:{server:n},uri:p.makeURI(`sip:${e}@${i}`),delegate:this.UADelegate,logLevel:"error",displayName:o,userAgentString:r,sessionDescriptionHandlerFactoryOptions:{peerConnectionConfiguration:{bundlePolicy:"balanced",certificates:void 0,iceCandidatePoolSize:0,iceServers:[],iceTransportPolicy:"all",peerIdentity:void 0,rtcpMuxPolicy:"require"}}};this.userAgent=new p(a),await this.userAgent.start(),l(this,s,e,"f")}catch(e){throw new Error("created agent fail"+e)}}registerServer(){return new Promise(((e,s)=>{if(!this.userAgent)return s("user ugent not exist");this.registerer=new f(this.userAgent),this.registerer.register({requestDelegate:{onAccept:()=>{e("accpet")},onReject:e=>{s(e.message.reasonPhrase)}}}).catch((()=>s("send fail")))}))}refreshRegistration(){return new Promise(((e,s)=>{if(!this.registerer)return s("registerer not exist, create first");this.registerer.register({requestDelegate:{onAccept:()=>{e("accpet")},onReject:e=>{s(e.message.reasonPhrase)}}}).catch((()=>s("send fail")))}))}unRegisterServer(){return!!this.userAgent&&(this.userAgent.stop(),l(this,s,"","f"),!0)}setSessionMediaProperty(e,s){const t=this.sessions.get(e);if(!t)return;const i=JSON.parse(JSON.stringify(t.mediaProperty));return s.split("m=").map((e=>{const s=e.match(/a=(sendrecv|sendonly|recvonly|inactive)/);s&&(e.startsWith("audio")?t.mediaProperty.audio=s[1]:e.includes("stream_screen")?t.mediaProperty.screen=s[1]:t.mediaProperty.video=s[1])})),JSON.stringify(i)!==JSON.stringify(t.mediaProperty)}initAudioCall(e){const s=this.sessions.get(e);s?s.constraints.video=!1:this.sessions.set(e,{session:null,constraints:{video:!1,audio:!0},mediaProperty:{audio:"",video:"",screen:""}})}initVideoCall(e){const s=this.sessions.get(e);s?s.constraints.video=this.videoResolusion:this.sessions.set(e,{session:null,constraints:{video:this.videoResolusion,audio:!0},mediaProperty:{audio:"",video:"",screen:""}})}setVideoBitrate(e){this.videoMaxBitrate=e}setVideoResolution(e,s){this.videoResolusion={width:{max:e},height:{max:s}}}setAudioBitrate(e){}call(t,i=!0,n){const r=p.makeURI(t);if(!r)return Promise.reject("not valid URI");const a=t.split(":")[1].split("@")[0];n?this.initVideoCall(a):this.initAudioCall(a);const l=this.sessions.get(a);if(!l)return Promise.reject("session initial fail");let u;const h={earlyMedia:!0,sessionDescriptionHandlerOptions:{constraints:l.constraints},sessionDescriptionHandlerModifiersReInvite:[e=>{var t,i;const n=null===(t=e.sdp)||void 0===t?void 0:t.split("m=").find((e=>e.includes("stream_screen screen_label")));if("offer"===e.type&&(u=null==n?void 0:n.includes("sendonly"),(null==n?void 0:n.includes("sendonly"))?this.event.onRecieveShare(a,d(this,s,"f")):(null==n?void 0:n.includes("inactive"))&&this.switchScreenShareRenderInRemote(!1,a)),"answer"===e.type&&u){const s=y(null!==(i=e.sdp)&&void 0!==i?i:"");e.sdp=s}return Promise.resolve(e)}]};if(!this.userAgent)return Promise.reject();const m=new c(this.userAgent,r,h);m.delegate=this.sessionDelegate,l.session=m,m.stateChange.addListener((e=>{var s,t,i;switch(e){case v.Initial:case v.Establishing:break;case v.Established:(null!==(i=null===(t=null===(s=m.dialog)||void 0===s?void 0:s.answer)||void 0===t?void 0:t.content)&&void 0!==i?i:"").split(/^m=/gm).some((e=>{if("video"===e.substring(0,5))return e.includes("a=inactive")}))&&this.initAudioCall(a);try{this.setupRemoteMedia(a),this.setupLocalMedia(a)}catch(e){}break;case v.Terminating:break;case v.Terminated:this.cleanupMedia(a).catch((e=>{})),this.sessions.delete(a);break;default:throw new Error("unkonw session state")}}));let f=!0;const g={sessionDescriptionHandlerOptions:{constraints:l.constraints},requestDelegate:{onAccept:e=>{const s="host=true"===e.message.getHeader("X-Host-Conference");this.setSessionMediaProperty(a,e.message.body),this.event.onInviteAnswered(a),this.event.onInviteConnected(a,Boolean(l.constraints.video),s,"send")},onTrying:e=>{const s="host=true"===e.message.getHeader("X-Host-Conference");this.event.onInviteTrying(a,Boolean(l.constraints.video),s)},onProgress:s=>{const t="host=true"===s.message.getHeader("X-Host-Conference");if(183===s.message.statusCode){if(this.event.onInviteSessionProgress(a,Boolean(l.constraints.video),t),!f)return;const s=(()=>{const s=d(this,e,"a",o),t="string"==typeof s?s:null==s?void 0:s.id;let i=null;try{i=document.querySelector(`#${t}`)}catch(e){}return i})();if(null===s)throw new Error("cann't found the remote audio element");const i=()=>{var e,s;const t=new MediaStream;return null===(s=(null===(e=null==l?void 0:l.session)||void 0===e?void 0:e.sessionDescriptionHandler).peerConnection)||void 0===s||s.getReceivers().forEach((e=>{const s=e.track;s&&t.addTrack(s)})),t};this.setElementSrc(s,i(),a),f=!1}180===s.message.statusCode&&this.event.onInviteRinging(a,Boolean(l.constraints.video),t)},onReject:e=>{this.event.onInviteFailure(a,e.message.reasonPhrase||"call be rejected")}},withoutSdp:!i};return m.invite(g)}async answerCall(e,t){const i=this.sessions.get(e);if(!((null==i?void 0:i.session)instanceof u))return Promise.reject(new Error("Session not instance of Invitation"));let n;t?this.initVideoCall(e):this.initAudioCall(e);try{n=await navigator.mediaDevices.getUserMedia(i.constraints)}catch(s){s.message.includes("video")&&this.initAudioCall(e)}finally{null==n||n.getTracks().forEach((e=>e.stop()))}const o={sessionDescriptionHandlerOptions:{constraints:i.constraints},sessionDescriptionHandlerModifiers:[e=>{var s;if(!i.constraints.video){const t=S(null!==(s=e.sdp)&&void 0!==s?s:"");e.sdp=t}return Promise.resolve(e)}]};let r;return i.session.sessionDescriptionHandlerModifiersReInvite=[t=>{var i,n;const o=null===(i=t.sdp)||void 0===i?void 0:i.split("m=").find((e=>e.includes("stream_screen screen_label")));if("offer"===t.type&&(r=null==o?void 0:o.includes("sendonly"),(null==o?void 0:o.includes("sendonly"))?this.event.onRecieveShare(e,d(this,s,"f")):(null==o?void 0:o.includes("inactive"))&&this.switchScreenShareRenderInRemote(!1,e)),"answer"===t.type&&r){const e=y(null!==(n=t.sdp)&&void 0!==n?n:"");t.sdp=e}return Promise.resolve(t)}],i.session.accept(o)}hangUp(e){const s=this.sessions.get(e);if(!s||null===s.session)return Promise.reject("session not exist");switch(s.session.state){case v.Initial:case v.Establishing:return s.session instanceof c?s.session.cancel():s.session instanceof u?s.session.reject({statusCode:486}):Promise.reject("unkonw call direction");case v.Established:return s.session.bye();case v.Terminating:return Promise.reject("session is going over");case v.Terminated:return Promise.reject("session is over");default:return Promise.reject("unknow session state")}}mute(e,s){var t,i,n;const o=this.sessions.get(e);try{if(!o||null===o.session)throw new Error("session not exist");null===(n=null===(i=null===(t=null==o?void 0:o.session)||void 0===t?void 0:t.sessionDescriptionHandler)||void 0===i?void 0:i.peerConnection)||void 0===n||n.getSenders().forEach((e=>{var t;"audio"===(null===(t=null==e?void 0:e.track)||void 0===t?void 0:t.kind)&&(e.track.enabled=!s)}))}catch(e){return Promise.reject(new Error(e))}return Promise.resolve()}sendVideo(e,s){var t,i,n;const o=this.sessions.get(e);null===(n=null===(i=null===(t=null==o?void 0:o.session)||void 0===t?void 0:t.sessionDescriptionHandler)||void 0===i?void 0:i.peerConnection)||void 0===n||n.getSenders().forEach((e=>{var t;"video"===(null===(t=null==e?void 0:e.track)||void 0===t?void 0:t.kind)&&(e.track.enabled=s)}))}hold(e){return new Promise(((s,t)=>{var i,n;const o=this.sessions.get(e);if(!o||null===o.session)return t("session not exist");null===(n=null===(i=null==o?void 0:o.session.sessionDescriptionHandler)||void 0===i?void 0:i.peerConnection)||void 0===n||n.getSenders().forEach((e=>{e.track&&(e.track.enabled=!1)})),o.session.invite({sessionDescriptionHandlerOptions:{hold:!0},requestDelegate:{onAccept:e=>{const t=e.message.to.uri.user;t&&this.setSessionMediaProperty(t,e.message.body),s("hold accept")},onReject:()=>{t("hold reject")}}})}))}unhold(e){return new Promise(((s,t)=>{var i,n;const o=this.sessions.get(e);return o&&null!==o.session?(null===(n=null===(i=null==o?void 0:o.session.sessionDescriptionHandler)||void 0===i?void 0:i.peerConnection)||void 0===n||n.getSenders().forEach((e=>{e.track&&(e.track.enabled=!0)})),o.session.invite({sessionDescriptionHandlerOptions:{hold:!1},requestDelegate:{onAccept:e=>{s("unhold accept");const t=e.message.to.uri.user;t&&this.setSessionMediaProperty(t,e.message.body)},onReject:()=>{t("unhold reject")}}})):t("session not exist")}))}sendDtmf(e,s){const t=this.sessions.get(e);if(!t||null===t.session)return Promise.reject("session not exist");const i={requestOptions:{body:{contentDisposition:"render",contentType:"application/dtmf-relay",content:"Signal="+s+"\r\nDuration=2000"}}};return t.session.info(i)}refer(e,s){const t=this.sessions.get(e);if(!t||null===t.session)throw new Error("session not exist");const i=p.makeURI(s);return i?t.session.refer(i,{onNotify:s=>{var t;const i=null!==(t=s.request.from.uri.user)&&void 0!==t?t:"";s.request.body.includes("200")?(this.hangUp(e),this.event.onACTVTransferSuccess(i)):s.request.body.includes("100")?this.event.onTransferTrying(i):s.request.body.includes("180")||s.request.body.includes("183")?this.event.onTransferRinging(i):this.event.onACTVTransferFailure(i)}}):Promise.reject("refer target URI isn't valid URI.")}attendedRefer(e,s){const t=this.sessions.get(e);if(!t||null===t.session)return Promise.reject("session not exist");const i=this.sessions.get(s);return(null==i?void 0:i.session)instanceof h?t.session.refer(i.session,{onNotify:s=>{var t;const i=null!==(t=s.request.from.uri.user)&&void 0!==t?t:"";s.request.body.includes("200")?(this.hangUp(e),this.event.onACTVTransferSuccess(i)):s.request.body.includes("100")?this.event.onTransferTrying(i):s.request.body.includes("180")||s.request.body.includes("183")?this.event.onTransferRinging(i):this.event.onACTVTransferFailure(i)}}):Promise.reject("refer target not a Session.")}updateCall(e,s,t){var i,n,o,r;const a=this.sessions.get(e);if(!a)return Promise.reject("session not exist");if(null===(o=null===(n=null===(i=a.session)||void 0===i?void 0:i.sessionDescriptionHandler)||void 0===n?void 0:n.peerConnection)||void 0===o||o.getSenders().forEach((e=>{var i,n;"audio"===(null===(i=e.track)||void 0===i?void 0:i.kind)&&(e.track.enabled=s),"video"===(null===(n=e.track)||void 0===n?void 0:n.kind)&&(e.track.enabled=t)})),t){const s=a.constraints.video;this.initVideoCall(e),""===a.mediaProperty.video&&(null===(r=a.session)||void 0===r||r.invite({sessionDescriptionHandlerOptions:{constraints:a.constraints},requestDelegate:{onAccept:s=>{this.setSessionMediaProperty(e,s.message.body);try{this.setupRemoteMedia(e)}catch(e){}},onReject:e=>{a.constraints.video=s}}}))}}async getShareableArea(e){const s=this.sessions.get(e);if(!s||null===s.session)throw new Error("session no exist");try{const e=await navigator.mediaDevices.getDisplayMedia({video:!0,audio:!1});return l(this,t,e.getVideoTracks()[0],"f"),Promise.resolve()}catch(e){return Promise.reject(new Error("screen share error:"+e))}}async startShareScreen(e){const s=this.sessions.get(e);if(!s||null===s.session)throw new Error("session no exist");if(!d(this,t,"f")||"ended"===d(this,t,"f").readyState)throw new Error("local screen share source can't be used");const i=t=>new Promise(((i,n)=>{var o,r,a,l,d,c,u,h;const v=null===(r=(null===(o=s.session)||void 0===o?void 0:o.sessionDescriptionHandler).peerConnection)||void 0===r?void 0:r.getSenders(),m=null==v?void 0:v.find((function(e){var s;return(null===(s=e.track)||void 0===s?void 0:s.kind)===(null==t?void 0:t.kind)})),p=null==v?void 0:v.find((function(e){return null===e.track}));p?p.replaceTrack(t).then((()=>{var t;null===(t=s.session)||void 0===t||t.invite({sessionDescriptionHandlerModifiers:[e=>{var s;let t=y(null!==(s=e.sdp)&&void 0!==s?s:"");return t=D(t),e.sdp=t,Promise.resolve(e)}],requestDelegate:{onAccept:s=>{this.setSessionMediaProperty(e,s.message.body),this.event.onShareAccept(s.message.from.uri.user,s.message.to.uri.user),i()},onReject:e=>{n(e.message.reasonPhrase)}}})})).catch((e=>{n(new Error("replace track error: "+e))})):m?(null===(l=(null===(a=s.session)||void 0===a?void 0:a.sessionDescriptionHandler).peerConnection)||void 0===l||l.addTrack(t),null===(d=s.session)||void 0===d||d.invite({sessionDescriptionHandlerModifiers:[e=>{var s;let t=y(null!==(s=e.sdp)&&void 0!==s?s:"");return e.sdp=t,Promise.resolve(e)}],requestDelegate:{onAccept:s=>{this.setSessionMediaProperty(e,s.message.body),this.event.onShareAccept(s.message.from.uri.user,s.message.to.uri.user),i()},onReject:e=>{n(e.message.reasonPhrase)}}})):(null===(u=(null===(c=s.session)||void 0===c?void 0:c.sessionDescriptionHandler).peerConnection)||void 0===u||u.addTrack(t),null===(h=s.session)||void 0===h||h.invite({sessionDescriptionHandlerModifiers:[e=>{var s;let t=y(null!==(s=e.sdp)&&void 0!==s?s:"");return e.sdp=t,Promise.resolve(e)}],requestDelegate:{onAccept:s=>{this.setSessionMediaProperty(e,s.message.body),this.event.onShareAccept(s.message.from.uri.user,s.message.to.uri.user),i()},onReject:e=>{n(e.message.reasonPhrase)}}}))}));d(this,t,"f").addEventListener("ended",(()=>{this.endShareScreen(e)}));try{return await i(d(this,t,"f")),!0}catch(e){return!1}}endShareScreen(e){var t;try{const i=this.sessions.get(e);if(!i||null===i.session)throw new Error("session not find");const n=i.session.sessionDescriptionHandler.peerConnection,o=null==n?void 0:n.getSenders(),r=null==o?void 0:o.find((e=>e.track&&e.track.label.match(/(screen|window|web-contents-media-stream)/)));return r&&(null===(t=r.track)||void 0===t||t.stop(),null==n||n.removeTrack(r),i.session.invite({sessionDescriptionHandlerModifiers:[t=>{var i,n;const o=null===(i=t.sdp)||void 0===i?void 0:i.split("m=").reverse().find((e=>e.includes("stream_screen")));if("offer"===t.type){(null==o?void 0:o.includes("sendonly"))?this.event.onRecieveShare(e,d(this,s,"f")):(null==o?void 0:o.includes("inactive"))&&this.switchScreenShareRenderInRemote(!1,e);const i=w(null!==(n=t.sdp)&&void 0!==n?n:"");t.sdp=i}return Promise.resolve(t)}],requestDelegate:{onAccept:s=>{this.setSessionMediaProperty(e,s.message.body),this.event.onShareStop(s.message.from.uri.user,s.message.to.uri.user)}}})),!0}catch(e){return!1}}sendOutOfDialogMessage(e,s,t){if(!this.userAgent)return Promise.reject();const i=p.makeURI(s);if(!i)return Promise.reject("not valid targetURI");let n;t&&(n="application/json");return new g(this.userAgent,i,e,n).message()}sendMessage(e,s,t){const i=s.split(":")[1].split("@")[0],n=this.sessions.get(i);if(!n||null===n.session)return Promise.reject("session not exist");const o={contentDisposition:"session",content:e,contentType:t?"application/x-conference-control+json":"application/json"};return n.session.message({requestOptions:{body:o}})}sendSubscription(e){if(!this.userAgent)return Promise.reject();const s=p.makeURI(e),t=new m(this.userAgent,s,"presence");return t.delegate={onNotify:s=>{s.accept();const t=e.split(":")[1].split("@")[0];this.event.onRecvNotifyOfSubscription(t,s.request.body)}},this.subscribers.set(e,t),t.subscribe()}terminateSubscription(e){const s=this.subscribers.get(e);return s?s.unsubscribe():Promise.reject("subscriber not exist")}cleanupMedia(s){let t=null;const o=d(this,e,"m",n).call(this,s);try{t=document.querySelector(`#${o}`)}catch(e){}if(null===t)return Promise.reject("cann't found the remote audio element");t.pause(),this.setElementSrc(t,null,s);let r=null;const a=d(this,e,"m",i).call(this,s);try{r=document.querySelector(`#${a}`)}catch(e){}if(null===r)return Promise.reject("cann't found the remote video element");r.pause(),this.setElementSrc(r,null,s);let l=null;try{l=document.querySelector(`#${this.localVideoID}`)}catch(e){}return null===l?Promise.reject("cann't found the local video element"):(l.pause(),this.setElementSrc(l,null,s),Promise.resolve())}setupLocalMedia(e){var s,t;const i=this.sessions.get(e);if(!i||null===i.session)throw new Error("session not exist");let n=null;try{n=document.querySelector(`#${this.localVideoID}`)}catch(e){}const o=i.session.sessionDescriptionHandler.localMediaStream;if(null===n)throw new Error("cann't found the local media element");null===(t=null===(s=null==i?void 0:i.session.sessionDescriptionHandler)||void 0===s?void 0:s.peerConnection)||void 0===t||t.getSenders().forEach((e=>{if(e.track&&"video"===e.track.kind){const s=e.getParameters();s.encodings.forEach((e=>e.maxBitrate=this.videoMaxBitrate)),e.setParameters(s)}})),this.setElementSrc(n,o,e)}setupRemoteMedia(t){const i=this.sessions.get(t);if(!i||null===i.session)throw new Error("session not exist");const n=i.session.sessionDescriptionHandler.remoteMediaStream;if(!n.active)return;const a=i.session.sessionDescriptionHandler.remoteScreenMediaStream,l=d(this,e,"a",o),c="string"==typeof l?l:null==l?void 0:l.id;let u=null;try{u=document.querySelector(`#${c}`)}catch(e){}if(null===u)throw new Error("cann't found the remote audio element");const h=new MediaStream,v=n.getAudioTracks()[0];h.addTrack(v),this.setElementSrc(u,h,t);const m=d(this,e,"a",r),p="string"==typeof m?m:null==m?void 0:m.id;let f=null;try{f=document.querySelector(`#${p}`)}catch(e){}if(null===f)throw new Error("cann't found the remote media element");const g=new MediaStream,y=n.getVideoTracks()[0];y?(g.addTrack(y),this.setElementSrc(f,g,t)):this.setElementSrc(f,null,t);let w=null;try{w=document.querySelector("#remoteScreen")}catch(e){}if(null===w)throw new Error("cann't find the remote screen element");w.pause();const S=new MediaStream,D=a.getVideoTracks()[0];if(D){if("sendonly"!==i.mediaProperty.screen)return void this.event.onRecieveStopShare(t,d(this,s,"f"));S.addTrack(D),this.setElementSrc(w,g,t),this.setElementSrc(f,S,t)}else this.setElementSrc(w,null,t)}playLocalVideoInRemoteElement(e){var s,t;if("string"!=typeof this.remoteVideoID)throw new Error("remote video is array, cann't go on");const i=this.sessions.get(e);if(!i||null===i.session)throw new Error("session not exist");let n=null;try{n=document.querySelector(`#${this.remoteVideoID}`)}catch(e){}if(null===n)throw new Error("cann't found the remote media element");const o=new MediaStream;i.constraints.video&&(null===(t=null===(s=i.session.sessionDescriptionHandler)||void 0===s?void 0:s.peerConnection)||void 0===t||t.getSenders().forEach((e=>{e.track&&o.addTrack(e.track)}))),this.setElementSrc(n,o,e)}switchScreenShareRenderInRemote(e,s){var t,i;if("string"!=typeof this.remoteVideoID)throw new Error("remote video is array, cann't go on");const n=this.sessions.get(s);if(!n||null===n.session)throw new Error("session not exist");let o=null,r=null;try{o=document.querySelector(`#${this.remoteVideoID}`),r=document.querySelector(`#${this.remoteScreenID}`)}catch(e){}if(null===o||null===r)throw new Error("remote video or remote screen element not found");const a=(null===(t=null==n?void 0:n.session)||void 0===t?void 0:t.sessionDescriptionHandler).remoteMediaStream,l=new MediaStream,d=a.getVideoTracks();d[0]&&l.addTrack(d[0]);const c=(null===(i=null==n?void 0:n.session)||void 0===i?void 0:i.sessionDescriptionHandler).remoteScreenMediaStream;e?(this.setElementSrc(o,c,s),this.setElementSrc(r,l,s)):(this.setElementSrc(o,l,s),this.setElementSrc(r,c,s))}setElementSrc(s,t,i){if(null===t&&d(this,e,"m",a).call(this,s.id,i,!1),"srcObject"in s)s.srcObject=t,s.load(),s.play().then((()=>{d(this,e,"m",a).call(this,s.id,i,!0)})).catch((t=>{d(this,e,"m",a).call(this,s.id,i,!1)}));else if(null===t)s.removeAttribute("src");else{const n=URL.createObjectURL(t);s.src=n,s.onload=function(){URL.revokeObjectURL(n)},s.load(),s.play().then((()=>{d(this,e,"m",a).call(this,s.id,i,!0)})).catch((t=>{d(this,e,"m",a).call(this,s.id,i,!1)}))}}}s=new WeakMap,t=new WeakMap,e=new WeakSet,i=function(e){var s,t;return void 0===this.remoteVideoID?void 0:"string"==typeof this.remoteVideoID?this.remoteVideoID:null===(t=null===(s=this.remoteVideoID)||void 0===s?void 0:s.find((s=>s.ext===e)))||void 0===t?void 0:t.id},n=function(e){var s,t;return void 0===this.remoteAudioID?void 0:"string"==typeof this.remoteAudioID?this.remoteAudioID:null===(t=null===(s=this.remoteAudioID)||void 0===s?void 0:s.find((s=>s.ext===e)))||void 0===t?void 0:t.id},o=function(){if("string"==typeof this.remoteAudioID)return this.remoteAudioID;return this.remoteAudioID.find((e=>!1===e.isPlaying))},r=function(){var e;if("string"==typeof this.remoteVideoID)return this.remoteVideoID;return null===(e=this.remoteVideoID)||void 0===e?void 0:e.find((e=>!1===e.isPlaying))},a=function(e,s,t){if(Array.isArray(this.remoteAudioID)&&Array.isArray(this.remoteVideoID)){let i;if(t){if(i=this.remoteAudioID.concat(this.remoteVideoID).find((s=>s.id===e)),!i)return;i.isPlaying=!0,i.ext=s}else{if(i=this.remoteAudioID.concat(this.remoteVideoID).find((s=>s.id===e)),!i)return;i.isPlaying=!1,i.ext=""}}};