/*
 InUC Web Client - sipapi.js v3.0.1 by NEC Platforms,Ltd.
*/
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.arrayIteratorImpl=function(c){var l=0;return function(){return l<c.length?{done:!1,value:c[l++]}:{done:!0}}};$jscomp.arrayIterator=function(c){return{next:$jscomp.arrayIteratorImpl(c)}};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;
$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(c,l,g){c!=Array.prototype&&c!=Object.prototype&&(c[l]=g.value)};$jscomp.getGlobal=function(c){c=["object"==typeof window&&window,"object"==typeof self&&self,"object"==typeof global&&global,c];for(var l=0;l<c.length;++l){var g=c[l];if(g&&g.Math==Math)return g}return globalThis};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";
$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.SymbolClass=function(c,l){this.$jscomp$symbol$id_=c;$jscomp.defineProperty(this,"description",{configurable:!0,writable:!0,value:l})};$jscomp.SymbolClass.prototype.toString=function(){return this.$jscomp$symbol$id_};
$jscomp.Symbol=function(){function c(g){if(this instanceof c)throw new TypeError("Symbol is not a constructor");return new $jscomp.SymbolClass($jscomp.SYMBOL_PREFIX+(g||"")+"_"+l++,g)}var l=0;return c}();
$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var c=$jscomp.global.Symbol.iterator;c||(c=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("Symbol.iterator"));"function"!=typeof Array.prototype[c]&&$jscomp.defineProperty(Array.prototype,c,{configurable:!0,writable:!0,value:function(){return $jscomp.iteratorPrototype($jscomp.arrayIteratorImpl(this))}});$jscomp.initSymbolIterator=function(){}};
$jscomp.initSymbolAsyncIterator=function(){$jscomp.initSymbol();var c=$jscomp.global.Symbol.asyncIterator;c||(c=$jscomp.global.Symbol.asyncIterator=$jscomp.global.Symbol("Symbol.asyncIterator"));$jscomp.initSymbolAsyncIterator=function(){}};$jscomp.iteratorPrototype=function(c){$jscomp.initSymbolIterator();c={next:c};c[$jscomp.global.Symbol.iterator]=function(){return this};return c};
$jscomp.iteratorFromArray=function(c,l){$jscomp.initSymbolIterator();c instanceof String&&(c+="");var g=0,h={next:function(){if(g<c.length){var f=g++;return{value:l(f,c[f]),done:!1}}h.next=function(){return{done:!0,value:void 0}};return h.next()}};h[Symbol.iterator]=function(){return h};return h};
$jscomp.polyfill=function(c,l,g,h){if(l){g=$jscomp.global;c=c.split(".");for(h=0;h<c.length-1;h++){var f=c[h];f in g||(g[f]={});g=g[f]}c=c[c.length-1];h=g[c];l=l(h);l!=h&&null!=l&&$jscomp.defineProperty(g,c,{configurable:!0,writable:!0,value:l})}};$jscomp.polyfill("Array.prototype.keys",function(c){return c?c:function(){return $jscomp.iteratorFromArray(this,function(c){return c})}},"es6","es3");
var wplog=0,wpl={log:function(c){0<wplog&&console.log(c)},dir:function(c){1<wplog&&console.dir(c)},warn:function(c){console.warn(c)},error:function(c){console.error(c)}},sipUaStream=null,sipUaObj=function(c,l,g){var h={},f={},e=this;this.bind=function(a,b){h[a]=h[a]||[];h[a].push(b);return this};this.unbind=function(){for(var a in h)h[a]=function(){}};this.dispatch=function(a,b){a=h[a];if("undefined"!=typeof a)for(var d=0;d<a.length;d++)a[d](b)};this.addSession=function(a,b){wpl.dir(b);f[a]?wpl.log("This session has already exist."):
(b.on("progress",function(d){wpl.log("on progress");wpl.dir(d);d={info:"progress",cid:e.searchSession(this),code:d.response?d.response.status_code:""};e.dispatch("session",d)}),b.on("accepted",function(d){var a=this.connection.getTransceivers(),b;for(b in a)a[b].sender.track&&(a[b].sender.track.enabled=!0);var c=-1;a=this.connection.getTransceivers();if(d.response){if(-1!=e.checkVideoSdp(d.response.body))for(b in a)a[b].stopped||a[b].receiver.track&&"video"==a[b].receiver.track.kind&&(c=e.isActiveTrack(a[b].receiver.track)?
1:0);if(-1==c)for(b in a)!a[b].stopped&&a[b].receiver.track&&"video"==a[b].receiver.track.kind&&(a[b].direction="inactive")}wpl.log("on accepted");wpl.dir(d)}),b.on("confirmed",function(a){wpl.log("on confirmed");wpl.dir(a);a={info:"confirmed",cid:e.searchSession(this)};e.dispatch("session",a)}),b.on("ended",function(a){wpl.log("on ended");wpl.dir(a);var d=e.searchSession(this);e.dispatch("session",{info:"ended",cid:d,cause:a.cause});e.removeSession(d)}),b.on("failed",function(a){wpl.log("on failed");
wpl.dir(a);var d=e.searchSession(this);e.dispatch("session",{info:"failed",cid:d,cause:a.cause,code:a.message?a.message.status_code:""});e.removeSession(d)}),b.on("refer",function(a){wpl.dir(a);var d={mediaStream:sipUaStream,sessionTimersExpires:180};wpl.dir(d);a.accept(function(a){var d=a.id;e.addSession(d,a);a={info:"ringback",cid:d,caller:a.remote_identity.uri.user,v_flg:sipUaStream.getVideoTracks().length?!0:!1};e.dispatch("session",a)},d)}),b.on("replaces",function(a){wpl.dir(a);a.accept(function(d){var b=
d.id;e.addSession(b,d);d={info:"replaces",cid:b,caller:d.remote_identity.uri.user,v_flg:1==e.checkVideoSdp(a.request.body)&&sipUaStream.getVideoTracks().length?!0:!1};e.dispatch("session",d)})}),b.on("reinvite",function(a){wpl.log("on re-INVITE");wpl.dir(a);var d=-1,b=this.connection.getTransceivers();if(a.request){var c=e.checkHoldSdp(a.request.body)?1:0;if(-1!=e.checkVideoSdp(a.request.body))for(var f in b)b[f].stopped||b[f].receiver.track&&"video"==b[f].receiver.track.kind&&(d=e.isActiveTrack(b[f].receiver.track)?
1:0);if(1==c&&-1==d)for(f in b)!b[f].stopped&&b[f].receiver.track&&"video"==b[f].receiver.track.kind&&(b[f].direction="inactive")}a={info:"update",cid:e.searchSession(this)};e.dispatch("session",a)}),b.on("update",function(a){wpl.log("on UPDATE");wpl.dir(a);a={info:"update",cid:e.searchSession(this)};e.dispatch("session",a)}),b.on("muted",function(a){wpl.log("on muted");wpl.dir(a);a={info:"muted",cid:e.searchSession(this)};e.dispatch("session",a)}),b.on("unmuted",function(a){wpl.log("on unmuted");
wpl.dir(a);a={info:"unmuted",cid:e.searchSession(this)};e.dispatch("session",a)}),b.on("hold",function(a){wpl.log("on hold");wpl.dir(a);"remote"==a.originator&&(a={info:"hold",cid:e.searchSession(this)},e.dispatch("session",a))}),b.on("unhold",function(a){wpl.log("on unhold");wpl.dir(a);if("remote"==a.originator){var b=e.searchSession(this),d=f[b].connection.getTransceivers();void 0==a.wait_time&&(a.wait_time=0);if(null==d[0].currentDirection&&5E3>a.wait_time)setTimeout(function(a,b){b.wait_time+=
100;a.emit("unhold",b)},100,this,a);else{var c=-1;if(a.request&&-1!=e.checkVideoSdp(a.request.body))for(var g in d)d[g].stopped||d[g].receiver.track&&"video"==d[g].receiver.track.kind&&(c=e.isActiveTrack(d[g].receiver.track)?1:0);if(-1==c)for(g in d)!d[g].stopped&&d[g].receiver.track&&"video"==d[g].receiver.track.kind&&(d[g].direction="inactive");e.dispatch("session",{info:"unhold",cid:b,v_flg:c})}}}),f[a]=b)};this.removeSession=function(a){delete f[a];if(0==Object.keys(f).length&&sipUaStream){a=
sipUaStream.getVideoTracks();for(var b in a)a[b].stop();a=sipUaStream.getAudioTracks();for(b in a)a[b].stop();sipUaStream=null}};this.searchSession=function(a){for(var b in f)if(f[b]===a)return b;return null};this.checkHoldSdp=function(a){a=a.split("m=");for(var b in a)if(0===a[b].indexOf("audio 0"))break;else if(0===a[b].indexOf("audio")){if(-1!==a[b].indexOf("a=sendonly"))return!0;break}return!1};this.checkVideoSdp=function(a){a=a.split("m=");for(var b in a)if(0===a[b].indexOf("video 0"))break;
else if(0===a[b].indexOf("video")){if(-1!==a[b].indexOf("a=recvonly"))return 0;if(-1!==a[b].indexOf("a=inactive"))break;else return 1}return-1};this.isActiveTrack=function(a){return a&&0!=a.enabled?!0:!1};this.adjustDial=function(a){a=a.replace(/#/g,"%23");return a=a.replace(/\*/g,"%2a")};this.register=function(a,b){""!==c&&"0"!==l&&""!==g||e.dispatch("error",{method:"register",info:"Input Error"});var d=new JsSIP.WebSocketInterface("wss://"+c+":"+l+"/uc/ws");this.ua=new JsSIP.UA({sockets:[d],uri:"sip:"+
a+"@"+g,password:b,contact_uri:"sip:"+a+"@"+g+";transport=ws"});this.ua.on("connected",function(a){wpl.log("[SIPAPI] UA.connected");wpl.dir(a);d=a.socket.socket;e.dispatch("connection",{info:"connected"})});this.ua.on("disconnected",function(a){wpl.log("[SIPAPI] UA.disconnected");wpl.dir(a);a&&d===a.socket&&e.dispatch("connection",{info:"disconnected"})});this.ua.on("registered",function(a){wpl.log("[SIPAPI] UA.registered");wpl.dir(a);e.dispatch("registration",{info:"registered"})});this.ua.on("unregistered",
function(a){wpl.log("[SIPAPI] UA.unregistered");wpl.dir(a);a.cause?(a={info:"failed",code:a.response?a.response.status_code:"",cause:a.cause},e.dispatch("registration",a)):(a={info:"unregisterd"},e.dispatch("register",a))});this.ua.on("registrationFailed",function(a){wpl.log("[SIPAPI] UA.registrationFailed");wpl.dir(a);e.dispatch("registration",{info:"failed",code:a.response?a.response.status_code:null,cause:a.cause})});this.ua.on("newRTCSession",function(a){wpl.log("[SIPAPI] UA.newRTCSession");var b=
a.session,d=a.originator,c=b.id,f=b.remote_identity.uri.user;e.addSession(c,b);a="remote"===d?{info:"incoming",cid:c,caller:f,v_flg:1==e.checkVideoSdp(a.request.body)}:{info:"ringback",cid:c,caller:f};e.dispatch("session",a)});this.ua.set("display_name",a);this.ua.start()};this.destroy=function(){this.unbind();this.ua&&this.ua.stop()};this.call=function(a,b){if(!a){var d={method:"call",info:"Input Error"};e.dispatch("error",d)}this.ua||(d={method:"call",info:"Not Registered."},e.dispatch("error",
d));a="sip:"+a+"@"+g;if(b){if(sipUaStream){d=sipUaStream.getTracks();for(var c in d)d[c].stop()}sipUaStream=b}return this.ua.call(a,{mediaStream:sipUaStream,sessionTimersExpires:180})?!0:(wpl.log("Failed to estblished a call session"),!1)};this.answer=function(a,b){this.ua||e.dispatch("error",{method:"call",info:"Not Registered."});if(b){if(sipUaStream){var d=sipUaStream.getTracks();for(var c in d)d[c].stop()}sipUaStream=b}f[a].answer({mediaStream:sipUaStream,sessionTimersExpires:180})};this.end=
function(a){f[a].terminate({});e.removeSession(a)};this.hold=function(a,b){f[a].hold({useUpdate:!1},b)};this.unhold=function(a,b){var d=f[a].connection.getTransceivers(),c;for(c in d)if("audio"==d[c].sender.track.kind){d[c].direction="sendrecv";break}f[a].unhold({useUpdate:!1},b)};this.dtmf=function(a,b){f[a].sendDTMF(b)};this.refer=function(a,b,d,c){wpl.dir(f[a]);wpl.dir(f[b]);var l={};c={extraHeaders:["Referred-By: <sip:"+e.adjustDial(c)+"@"+g+">"],eventHandlers:l};f[b]&&(l.requestSucceeded=function(c){e.dispatch("session",
{info:"refer",ref_cid:a,rep_cid:b})},c.replaces=f[b]);f[a].refer(d,c)};this.update=function(a,b,c,g){f[a]&&(e.changeStream(a,b,c),f[a].renegotiate({},g))};this.getStream=function(a){if(f[a]){var b=null,c=null,g=null,l=null,h,n=null,p=null,k=f[a].connection.getTransceivers(),m;for(m in k)if(!k[m].stopped){switch(k[m].currentDirection?k[m].currentDirection:k[m].direction){case "sendrecv":var q=h=!0;break;case "sendonly":h=!0;q=!1;break;case "recvonly":h=!1;q=!0;break;default:q=h=!1}h&&e.isActiveTrack(k[m].sender.track)&&
("audio"==k[m].sender.track.kind?b=k[m].sender.track:"video"==k[m].sender.track.kind&&(c=k[m].sender.track));q&&e.isActiveTrack(k[m].receiver.track)&&("audio"==k[m].receiver.track.kind?g=k[m].receiver.track:"video"==k[m].receiver.track.kind&&(l=k[m].receiver.track))}if(b||c)n=new MediaStream,b&&n.addTrack(b),c&&n.addTrack(c);if(g||l)p=new MediaStream,g&&p.addTrack(g),l&&p.addTrack(l);e.dispatch("modStream",{cid:a,local:n,remote:p})}};this.changeStream=function(a,b,c){if(f[a]){var d=null,e=null,g=
0,l=0;if(c.audio&&(b&&0<b.getAudioTracks().length?(g=1,d=b.getAudioTracks()[0]):g=2,sipUaStream))if(d){var h=sipUaStream.getAudioTracks();for(var k in h)h[k].id!==d.id&&h[k].stop(),sipUaStream.removeTrack(h[k]);sipUaStream.addTrack(d)}else for(k in h=sipUaStream.getAudioTracks(),h)h[k].stop(),sipUaStream.removeTrack(h[k]);if(c.video&&(b&&0<b.getVideoTracks().length?(l=1,e=b.getVideoTracks()[0]):l=2,sipUaStream))if(e){h=sipUaStream.getVideoTracks();for(k in h)h[k].id!==e.id&&h[k].stop(),sipUaStream.removeTrack(h[k]);
sipUaStream.addTrack(e)}else for(k in h=sipUaStream.getVideoTracks(),h)h[k].stop(),sipUaStream.removeTrack(h[k]);var m=f[a].connection.getTransceivers();for(k in m)if(m[k].sender&&m[k].sender.track){if("audio"==m[k].sender.track.kind)b=d,h=g;else if("video"==m[k].sender.track.kind)b=e,h=l;else continue;c=m[k].sender.track.kind;if(2==h){m[k].sender.track.enabled=!1;switch(m[k].direction){case "sendrecv":m[k].direction="recvonly";break;case "sendonly":m[k].direction="inactive"}"audio"==c?g=0:"video"==
c&&(l=0)}else if(1==h){m[k].sender.replaceTrack(b);m[k].sender.track.enabled=!0;switch(m[k].direction){case "recvonly":m[k].direction="sendrecv";break;case "inactive":m[k].direction="sendonly"}"audio"==c?g=0:"video"==c&&(l=0)}}1==g&&f[a].connection.addTrack(d);1==l&&f[a].connection.addTrack(e);f[a]._localMediaStream=sipUaStream}};this.mute=function(a,b,c){f[a]&&f[a].mute({audio:b,video:c})};this.unmute=function(a,b,c){f[a]&&f[a].unmute({audio:b,video:c})};this.getMuteStatus=function(a){return f[a]?
f[a].isMuted():{audio:!1,video:!1}};this.getSessions=function(){var a=[],b;for(b in f)a.push(f[b]);return a}};
