let isFirefox="undefined"!=typeof InstallTrigger,isChrome=!!window.chrome&&!!window.chrome,currentSession=null;function ConferencePhone(e,t,i,o,n,s,r=!0,a=!0){EasyEvent.call(this),this.id=e,this.name=t,this.password=i,this.host=o,this.StunServer=n,this.register=s,this.audio=r,this.video=a,this._locals=new Streams,this._locals.bubble("streamAdded",this),this._locals.bubble("streamRemoved",this),this._remotes=new Streams,this._remotes.bubble("streamAdded",this),this._remotes.bubble("streamRemoved",this)}function isUnifiedPlanDefault(){if(!("currentDirection"in RTCRtpTransceiver.prototype))return!1;const e=new RTCPeerConnection;let t=!1;try{e.addTransceiver("audio"),t=!0}catch(e){}return e.close(),t}function mute(e,t){function i(e,t){if(e)for(let i=0;i<e.length;++i)e[i].enabled==t&&(e[i].enabled=!t)}void 0!==(t=t||{audio:!0,video:!0}).audio&&i(e.getAudioTracks(),t.audio),void 0!==t.video&&i(e.getVideoTracks(),t.video)}function unmute(e,t){mute(e,t||{audio:!1,video:!1})}function Streams(){EasyEvent.call(this),this._streams=[]}function EasyEvent(){this._events={}}ConferencePhone.prototype=Object.create(EasyEvent.prototype),ConferencePhone.prototype.constructor=ConferencePhone,ConferencePhone.prototype.connect=function(){if(this._ua)return void this._ua.start();let e=this,t=new JsSIP.WebSocketInterface("wss://"+this.host+":8089/ws"),i="sip:"+this.id+"@"+this.host,o={sockets:[t],uri:i,contact_uri:i,username:this.name?this.name:this.id,password:this.password,register:this.register,register_expires:300,sessionDescriptionHandlerFactoryOptions:{peerConnectionOptions:{alwaysAcquireMediaFirst:!0,iceCheckingTimeout:500,rtcConfiguration:{iceServers:[{urls:"stun:"+this.StunServer}]}}}};function n(t,i){t.on(i,(function(t){e.raise(i,t)}))}this._unified=isUnifiedPlanDefault(),this._ua=new JsSIP.UA(o),n(this._ua,"connected"),n(this._ua,"disconnected"),n(this._ua,"registered"),n(this._ua,"unregistered"),n(this._ua,"registrationFailed"),this._ua.on("newRTCSession",(function(t){currentSession=t.session;let i=t.session;i.interop=new SdpInterop.InteropChrome,console.log("new session - "+i.direction+" - "+i),i.on("confirmed",(function(){let t=i.connection.getLocalStreams();for(let i=0;i<t.length;++i)console.log("confirmed: adding local stream "+t[i].id),t[i].local=!0,e._locals.add(t[i])})),i.on("sdp",(function(t){if(isFirefox&&"remote"===t.originator)t.sdp=t.sdp.replace(/actpass/g,"active");else if(isChrome&&!e._unified){let e=new RTCSessionDescription({type:t.type,sdp:t.sdp});"local"===t.originator?converted=i.interop.toUnifiedPlan(e):converted=i.interop.toPlanB(e),t.sdp=converted.sdp}})),n(i,"muted"),n(i,"unmuted"),n(i,"failed"),n(i,"ended"),i.connection.ontrack=function(t){console.log("ontrack: "+t.track.kind+" - "+t.track.id+" stream "+t.streams[0].id),$("#video-view"+t.streams[0].id).show(),"video"==t.track.kind&&(t.track.enabled=!1);for(let i=0;i<t.streams.length;++i)t.streams[i].local=!1,e._remotes.add(t.streams[i]);t.track.onended=function(){$("#video-view"+t.streams[0].id).hide()}},i.connection.onremovestream=function(t){console.log("onremovestream: "+t.stream.id),e._remotes.remove(t.stream)},"remote"===t.originator&&e.raise("incoming",t.request.ruri.toAor())})),this._ua.start()},ConferencePhone.prototype.disconnect=function(){this._locals.removeAll(),this._remotes.removeAll(),this._ua&&this._ua.stop()},ConferencePhone.prototype.answer=function(){if(!this._ua)return;let e={mediaConstraints:{audio:this.audio,video:this.video}};this._rtc.answer(e)},ConferencePhone.prototype.call=function(e){if(!this._ua||!e)return;let t={mediaConstraints:{audio:this.audio,video:this.video}};e.startsWith("sip:")?this._rtc=this._ua.call(e,t):this._rtc=this._ua.call("sip:"+e+"@"+this.host,t)},ConferencePhone.prototype.terminate=function(){this._locals.removeAll(),this._remotes.removeAll(),this._ua&&this._rtc.terminate()},ConferencePhone.prototype.ShareScreen=function(){var t=new MediaStream,i=currentSession.connection;navigator.mediaDevices.getDisplayMedia({video:!0,audio:!1}).then((function(e){var o=e.getVideoTracks()[0];i.getSenders().forEach((function(e){e.track&&"video"==e.track.kind&&(e.replaceTrack(o),t.addTrack(o))}));var n=$('[id*="locVideo"]').get(0);n.autoplay=!0,n.srcObject=t;var s=$('[id*="video-view"]').get(0);s.append(n),$('[id*="new-media-view"]').get(0).append(s)})).catch((function(){console.error(e)}))},ConferencePhone.prototype.ShareVideo=function(){var t=new MediaStream,i=currentSession.connection;navigator.mediaDevices.getUserMedia({video:!0,audio:!0}).then((function(e){var o=e.getVideoTracks()[0];i.getSenders().forEach((function(e){e.track&&"video"==e.track.kind&&(e.replaceTrack(o),t.addTrack(o))}));var n=$('[id*="locVideo"]').get(0);n.autoplay=!0,n.srcObject=t;var s=$('[id*="video-view"]').get(0);s.append(n),$('[id*="new-media-view"]').get(0).append(s)})).catch((function(){console.error(e)}))},ConferencePhone.prototype.dtmfSend=function(e){$("#dialText").val(e),currentSession.connection.getSenders()[0].dtmf.insertDTMF(e)},Streams.prototype=Object.create(EasyEvent.prototype),Streams.prototype.constructor=Streams,Streams.prototype.add=function(e){-1==this._streams.indexOf(e)&&(this._streams.push(e),console.log("Streams: added "+e.id),this.raise("streamAdded",e))},Streams.prototype.remove=function(e){let t="number"==typeof e?e:this._streams.indexOf(e);if(-1==t)return;let i=this._streams.splice(t,1);for(let e=0;e<i.length;++e)console.log("Streams: removed "+i[e].id),this.raise("streamRemoved",i[e])},Streams.prototype.removeAll=function(){for(let e=this._streams.length-1;e>=0;--e)this.remove(e)},EasyEvent.prototype.handle=function(e,t){e in this._events?this._events[e].push(t):this._events[e]=[t]},EasyEvent.prototype.raise=function(e){if(e in this._events)for(let t=0;t<this._events[e].length;++t)this._events[e][t].apply(this,Array.prototype.slice.call(arguments,1))},EasyEvent.prototype.bubble=function(e,t){this.handle(e,(function(i){t.raise(e,i)}))},EasyEvent.prototype.raiseForEach=function(e,t){if(e in this._events)for(let i=0;i<t.length;++i)this.raise(e,t[i],i)};