Current Path : /var/www/axolotl/data/www/arhangelsk.axolotls.ru/bitrix/js/im/call/ |
Current File : /var/www/axolotl/data/www/arhangelsk.axolotls.ru/bitrix/js/im/call/voximplant_call.min.js |
(function(){BX.namespace("BX.Call");var e={invite:"im.call.invite",cancel:"im.call.cancel",answer:"im.call.answer",decline:"im.call.decline",hangup:"im.call.hangup",ping:"im.call.ping"};var t={ping:"Call::ping",hangup:"Call::hangup",userInviteTimeout:"Call::userInviteTimeout"};var i={voiceStarted:"Call::voiceStarted",voiceStopped:"Call::voiceStopped",microphoneState:"Call::microphoneState"};var n={onCallConference:"VoximplantCall::onCallConference"};var a=5e3;var l=25e3;var s=5500;var o=15e3;if(window["BXDesktopSystem"]){navigator["getDisplayMedia"]=function(){var e={audio:false,video:{mandatory:{chromeMediaSource:"desktop",maxWidth:screen.width>1920?screen.width:1920,maxHeight:screen.height>1080?screen.height:1080},optional:[{googTemporalLayeredScreencast:true}]}};return navigator.mediaDevices.getUserMedia(e)}}BX.Call.VoximplantCall=function(e){BX.Call.VoximplantCall.superclass.constructor.apply(this,arguments);if(!window.VoxImplant){throw new Error("Voximplant SDK is not found")}this.voximplantCall=null;this.signaling=new BX.Call.VoximplantCall.Signaling({call:this});this.peers={};this.joinedElsewhere=false;this.screenShared=false;this.localVideoShown=false;this.clientEventsBound=false;this.deviceList=[];this.__onLocalDevicesUpdatedHandler=this.__onLocalDevicesUpdated.bind(this);this.__onLocalMediaRendererAddedHandler=this.__onLocalMediaRendererAdded.bind(this);this.__onBeforeLocalMediaRendererRemovedHandler=this.__onBeforeLocalMediaRendererRemoved.bind(this);this.__onCallDisconnectedHandler=this.__onCallDisconnected.bind(this);this.__onCallMessageReceivedHandler=this.__onCallMessageReceived.bind(this);this.__onCallEndpointAddedHandler=this.__onCallEndpointAdded.bind(this);this.initPeers();this.pingUsersInterval=setInterval(this.pingUsers.bind(this),a);this.pingBackendInterval=setInterval(this.pingBackend.bind(this),l);this.lastPingReceivedTimeout=null;this.lastSelfPingReceivedTimeout=null;this.reinviteTimeout=null};BX.extend(BX.Call.VoximplantCall,BX.Call.AbstractCall);BX.Call.VoximplantCall.prototype.initPeers=function(){this.users.forEach(function(e){e=Number(e);this.peers[e]=this.createPeer(e)},this)};BX.Call.VoximplantCall.prototype.reinitPeers=function(){for(var e in this.peers){if(this.peers.hasOwnProperty(e)&&this.peers[e]){this.peers[e].destroy();this.peers[e]=null}}this.initPeers()};BX.Call.VoximplantCall.prototype.pingUsers=function(){if(this.ready){var e=this.users.concat(this.userId);this.signaling.sendPingToUsers({userId:e},true)}};BX.Call.VoximplantCall.prototype.pingBackend=function(){if(this.ready){this.signaling.sendPingToBackend()}};BX.Call.VoximplantCall.prototype.createPeer=function(e){return new BX.Call.VoximplantCall.Peer({call:this,userId:e,ready:e==this.initiatorId,onStreamReceived:function(e){this.runCallback(BX.Call.Event.onStreamReceived,e)}.bind(this),onStreamRemoved:function(e){this.runCallback(BX.Call.Event.onStreamRemoved,e)}.bind(this),onStateChanged:this.__onPeerStateChanged.bind(this),onInviteTimeout:this.__onPeerInviteTimeout.bind(this)})};BX.Call.VoximplantCall.prototype.getUsers=function(){var e={};for(var t in this.peers){e[t]=this.peers[t].calculatedState}return e};BX.Call.VoximplantCall.prototype.getClient=function(){return new Promise(function(e,t){BX.Voximplant.getClient().then(function(t){t.enableSilentLogging();t.setLoggerCallback(function(e){this.log(e.label+": "+e.message)}.bind(this));this.bindClientEvents();e(t)}.bind(this)).catch(t)}.bind(this))};BX.Call.VoximplantCall.prototype.bindClientEvents=function(){var e=VoxImplant.Hardware.StreamManager.get();if(!this.clientEventsBound){e.on(VoxImplant.Hardware.HardwareEvents.DevicesUpdated,this.__onLocalDevicesUpdatedHandler);e.on(VoxImplant.Hardware.HardwareEvents.MediaRendererAdded,this.__onLocalMediaRendererAddedHandler);e.on(VoxImplant.Hardware.HardwareEvents.MediaRendererUpdated,this.__onLocalMediaRendererAddedHandler);e.on(VoxImplant.Hardware.HardwareEvents.BeforeMediaRendererRemoved,this.__onBeforeLocalMediaRendererRemovedHandler);this.clientEventsBound=true}};BX.Call.VoximplantCall.prototype.removeClientEvents=function(){var e=VoxImplant.Hardware.StreamManager.get();e.off(VoxImplant.Hardware.HardwareEvents.DevicesUpdated,this.__onLocalDevicesUpdatedHandler);e.off(VoxImplant.Hardware.HardwareEvents.MediaRendererAdded,this.__onLocalMediaRendererAddedHandler);e.off(VoxImplant.Hardware.HardwareEvents.BeforeMediaRendererRemoved,this.__onBeforeLocalMediaRendererRemovedHandler);this.clientEventsBound=false};BX.Call.VoximplantCall.prototype.setMuted=function(e){if(this.muted==e){return}this.muted=e;if(this.voximplantCall){if(this.muted){this.voximplantCall.muteMicrophone()}else{this.voximplantCall.unmuteMicrophone()}this.signaling.sendMicrophoneState(!this.muted)}};BX.Call.VoximplantCall.prototype.setVideoEnabled=function(e){e=e===true;if(this.videoEnabled==e){return}this.videoEnabled=e;if(this.voximplantCall){if(e){this._showLocalVideo()}else{if(this.localVideoShown){VoxImplant.Hardware.StreamManager.get().hideLocalVideo().then(function(){this.localVideoShown=false;this.runCallback(BX.Call.Event.onLocalMediaReceived,{tag:"main",stream:new MediaStream})}.bind(this))}}this.voximplantCall.sendVideo(this.videoEnabled)}};BX.Call.VoximplantCall.prototype.setCameraId=function(e){this.cameraId=e;var t={cameraId:this.cameraId};if(this.videoResolution){t.frameHeight=this.videoResolution.height;t.frameWidth=this.videoResolution.width}VoxImplant.Hardware.CameraManager.get().getInputDevices().then(function(){if(this.voximplantCall){VoxImplant.Hardware.CameraManager.get().setCallVideoSettings(this.voximplantCall,t)}VoxImplant.Hardware.CameraManager.get().setDefaultVideoSettings(t)}.bind(this))};BX.Call.VoximplantCall.prototype.setMicrophoneId=function(e){if(this.microphoneId==e){return}this.microphoneId=e;if(this.voximplantCall){VoxImplant.Hardware.AudioDeviceManager.get().getInputDevices().then(function(){VoxImplant.Hardware.AudioDeviceManager.get().setCallAudioSettings(this.voximplantCall,{inputId:this.microphoneId})}.bind(this))}};BX.Call.VoximplantCall.prototype.getCurrentMicrophoneId=function(){if(this.voximplantCall.peerConnection.impl.getTransceivers){var e=this.voximplantCall.peerConnection.impl.getTransceivers();if(e.length>0){var t=e[0].sender.track;var i=t.getSettings();return i.deviceId}}return this.microphoneId};BX.Call.VoximplantCall.prototype.useHdVideo=function(e){this.videoHd=e===true;if(this.voximplantCall){var t={cameraId:this.cameraId,videoQuality:this.videoHd?VoxImplant.Hardware.VideoQuality.VIDEO_SIZE_HD:VoxImplant.Hardware.VideoQuality.VIDEO_SIZE_nHD};VoxImplant.Hardware.CameraManager.get().setCallVideoSettings(this.voximplantCall,t)}};BX.Call.VoximplantCall.prototype.setVideoQuality=function(e){if(this.videoQuality==e){return}this.videoQuality=e;this._applyCurrentVideoQuality()};BX.Call.VoximplantCall.prototype._setMaxBitrate=function(e){if(this.voximplantCall){var t=this.voximplantCall.peerConnection.getTransceivers();t.forEach(function(t){if(t.sender&&t.sender.track&&t.sender.track.kind==="video"&&!t.stoped&&t.currentDirection.indexOf("send")!==-1){var i=t.sender;var n=i.getParameters();if(!n.encodings){n.encodings=[{}]}if(e===0){delete n.encodings[0].maxBitrate}else{n.encodings[0].maxBitrate=e*1e3}i.setParameters(n)}},this)}};BX.Call.VoximplantCall.prototype._applyCurrentVideoQuality=function(){if(!this.voximplantCall){return}if("RTCRtpSender"in window&&"setParameters"in window.RTCRtpSender.prototype){this._setMaxBitrate(BX.Call.Util.getMaxBitrate(this.videoQuality))}else{this._useVideoResolution(BX.Call.Util.getMaxResolution(this.videoQuality,this.useHdVideo()))}};BX.Call.VoximplantCall.prototype._useVideoResolution=function(e){this.videoResolution=e;if(this.voximplantCall){var t={cameraId:this.cameraId,frameHeight:this.videoResolution.height,frameWidth:this.videoResolution.width};VoxImplant.Hardware.CameraManager.get().setCallVideoSettings(this.voximplantCall,t)}};BX.Call.VoximplantCall.prototype._showLocalVideo=function(){return new Promise(function(e,t){VoxImplant.Hardware.StreamManager.get().showLocalVideo().then(function(){this.localVideoShown=true;e()}.bind(this),function(){this.localVideoShown=true;e()}.bind(this))}.bind(this))};BX.Call.VoximplantCall.prototype._hideLocalVideo=function(){return new Promise(function(e,t){VoxImplant.Hardware.StreamManager.get().hideLocalVideo().then(function(){this.localVideoShown=false;e()}.bind(this),function(){this.localVideoShown=false;e()}.bind(this))})};BX.Call.VoximplantCall.prototype.startScreenSharing=function(){if(!this.voximplantCall){return}var e=!this.videoEnabled;var t=this.videoEnabled;this.voximplantCall.shareScreen(e,t).then(function(){this.log("Screen shared");this.screenShared=true}.bind(this))};BX.Call.VoximplantCall.prototype.stopScreenSharing=function(){if(!this.voximplantCall){return}this.voximplantCall.stopSharingScreen().then(function(){this.log("Screen is no longer shared");this.screenShared=false}.bind(this))};BX.Call.VoximplantCall.prototype.isScreenSharingStarted=function(){return this.screenShared};BX.Call.VoximplantCall.prototype.inviteUsers=function(e){var t=this;this.ready=true;if(!BX.type.isPlainObject(e)){e={}}var i=BX.type.isArray(e.users)?e.users:this.users;this.attachToConference().then(function(){t.signaling.sendPingToUsers({userId:i});return t.signaling.inviteUsers({userIds:i,video:t.videoEnabled?"Y":"N"})}).then(function(e){t.runCallback(BX.Call.Event.onJoin,{local:true});for(var n=0;n<i.length;n++){var a=parseInt(i[n],10);if(!t.users.includes(a)){t.users.push(a)}if(!t.peers[a]){t.peers[a]=t.createPeer(a);t.runCallback(BX.Call.Event.onUserInvited,{userId:a})}t.peers[a].onInvited();t.scheduleRepeatInvite()}}).catch(t.onFatalError.bind(t))};BX.Call.VoximplantCall.prototype.scheduleRepeatInvite=function(){clearTimeout(this.reinviteTimeout);this.reinviteTimeout=setTimeout(this.repeatInviteUsers.bind(this),s)};BX.Call.VoximplantCall.prototype.repeatInviteUsers=function(){clearTimeout(this.reinviteTimeout);if(!this.ready){return}var e=[];for(var t in this.peers){if(this.peers.hasOwnProperty(t)&&this.peers[t].calculatedState===BX.Call.UserState.Calling){e.push(t)}}if(e.length===0){return}this.signaling.inviteUsers({userIds:e,video:this.videoEnabled?"Y":"N"}).then(function(){this.scheduleRepeatInvite()}.bind(this))};BX.Call.VoximplantCall.prototype.answer=function(e){this.ready=true;if(!BX.type.isPlainObject(e)){e={}}this.videoEnabled=e.useVideo==true;this.signaling.sendAnswer();this.attachToConference().then(function(){this.log("Attached to conference");this.runCallback(BX.Call.Event.onJoin,{local:true})}.bind(this)).catch(this.onFatalError.bind(this))};BX.Call.VoximplantCall.prototype.decline=function(t){this.ready=false;var i={callId:this.id,callInstanceId:this.instanceId};if(t){i.code=t}BX.CallEngine.getRestClient().callMethod(e.decline,i)};BX.Call.VoximplantCall.prototype.hangup=function(e,t){if(!this.ready){var i=new Error("Hangup in wrong state");this.log(i);return}var n=new Error;n.name="Call stack:";this.log("Hangup received \n"+n.stack);var a={};this.ready=false;if(typeof e!="undefined"){a.code=e}if(typeof t!="undefined"){a.reason=t}this.runCallback(BX.Call.Event.onLeave,{local:true});a.userId=this.users;this.signaling.sendHangup(a);this.muted=false;this.reinitPeers();if(this.voximplantCall){this.voximplantCall._replaceVideoSharing=false;try{this.voximplantCall.hangup()}catch(e){console.error(e)}}this.screenShared=false;this._hideLocalVideo()};BX.Call.VoximplantCall.prototype.attachToConference=function(){var e=this;var t={};if(this.cameraId){t.cameraId=this.cameraId}t.videoQuality=this.videoHd?VoxImplant.Hardware.VideoQuality.VIDEO_SIZE_HD:VoxImplant.Hardware.VideoQuality.VIDEO_SIZE_nHD;VoxImplant.Hardware.CameraManager.get().setDefaultVideoSettings(t);if(this.microphoneId){VoxImplant.Hardware.AudioDeviceManager.get().setDefaultAudioSettings({inputId:this.microphoneId})}return new Promise(function(t,i){if(e.voximplantCall&&e.voximplantCall.state()==="CONNECTED"){return t()}e.getClient().then(function(n){if(e.videoEnabled){e._showLocalVideo()}try{e.voximplantCall=n.callConference({number:"bx_conf_"+e.id,video:{sendVideo:e.videoEnabled,receiveVideo:true},customData:{}})}catch(e){console.error(e);return i(e)}if(!e.voximplantCall){e.log("Error: could not create voximplant call");return i({code:"VOX_NO_CALL"})}e.runCallback(BX.Call.VoximplantCall.Event.onCallConference,{call:e});e.bindCallEvents();var a=function(){e.log("Call connected");e.voximplantCall.removeEventListener(VoxImplant.CallEvents.Connected,a);e.voximplantCall.removeEventListener(VoxImplant.CallEvents.Failed,l);e.voximplantCall.addEventListener(VoxImplant.CallEvents.Failed,e.__onCallDisconnectedHandler);if(e.deviceList.length===0){navigator.mediaDevices.enumerateDevices().then(function(t){e.deviceList=t;e.runCallback(BX.Call.Event.onDeviceListUpdated,{deviceList:e.deviceList})})}else{e.runCallback(BX.Call.Event.onDeviceListUpdated,{deviceList:e.deviceList})}if(e.muted){e.voximplantCall.muteMicrophone()}e.signaling.sendMicrophoneState(!e.muted);setTimeout(function(){e._applyCurrentVideoQuality()},1e3);t()};var l=function(t){e.log("Could not attach to conference",t);e.voximplantCall.removeEventListener(VoxImplant.CallEvents.Connected,a);e.voximplantCall.removeEventListener(VoxImplant.CallEvents.Failed,l);var n=VoxImplant.getInstance();n.enableSilentLogging(false);n.setLoggerCallback(null);i(t)};e.voximplantCall.addEventListener(VoxImplant.CallEvents.Connected,a);e.voximplantCall.addEventListener(VoxImplant.CallEvents.Failed,l)}).catch(e.onFatalError.bind(e))})};BX.Call.VoximplantCall.prototype.bindCallEvents=function(){this.voximplantCall.addEventListener(VoxImplant.CallEvents.Disconnected,this.__onCallDisconnectedHandler);this.voximplantCall.addEventListener(VoxImplant.CallEvents.MessageReceived,this.__onCallMessageReceivedHandler);this.voximplantCall.addEventListener(VoxImplant.CallEvents.EndpointAdded,this.__onCallEndpointAddedHandler)};BX.Call.VoximplantCall.prototype.removeCallEvents=function(){if(this.voximplantCall){this.voximplantCall.removeEventListener(VoxImplant.CallEvents.Disconnected,this.__onCallDisconnectedHandler);this.voximplantCall.removeEventListener(VoxImplant.CallEvents.MessageReceived,this.__onCallMessageReceivedHandler);this.voximplantCall.removeEventListener(VoxImplant.CallEvents.EndpointAdded,this.__onCallEndpointAddedHandler)}};BX.Call.VoximplantCall.prototype.addInvitedUsers=function(e){for(var t=0;t<e.length;t++){var i=Number(e[t]);if(i==this.userId){continue}if(this.peers[i]){if(this.peers[i].calculatedState===BX.Call.UserState.Failed||this.peers[i].calculatedState===BX.Call.UserState.Idle){this.peers[i].onInvited()}}else{this.peers[i]=this.createPeer(i);this.runCallback(BX.Call.Event.onUserInvited,{userId:i});this.peers[i].onInvited()}if(!this.users.includes(i)){this.users.push(i)}}};BX.Call.VoximplantCall.prototype.isAnyoneParticipating=function(){for(var e in this.peers){if(this.peers[e].isParticipating()){return true}}return false};BX.Call.VoximplantCall.prototype.__onPeerStateChanged=function(e){this.runCallback(BX.Call.Event.onUserStateChanged,e);if(!this.ready){return}if(e.state==BX.Call.UserState.Failed||e.state==BX.Call.UserState.Unavailable||e.state==BX.Call.UserState.Declined||e.state==BX.Call.UserState.Idle){if(!this.isAnyoneParticipating()){this.hangup()}}};BX.Call.VoximplantCall.prototype.__onPeerInviteTimeout=function(e){if(!this.ready){return}this.signaling.sendUserInviteTimeout({userId:this.users,failedUserId:e.userId})};BX.Call.VoximplantCall.prototype.__onPullEvent=function(e,t,i){var n={"Call::answer":this.__onPullEventAnswer.bind(this),"Call::hangup":this.__onPullEventHangup.bind(this),"Call::usersInvited":this.__onPullEventUsersInvited.bind(this),"Call::userInviteTimeout":this.__onPullEventUserInviteTimeout.bind(this),"Call::ping":this.__onPullEventPing.bind(this),"Call::finish":this.__onPullEventFinish.bind(this)};if(n[e]){n[e].call(this,t)}};BX.Call.VoximplantCall.prototype.__onPullEventAnswer=function(e){var t=Number(e.senderId);if(t==this.userId){return this.__onPullEventAnswerSelf(e)}if(!this.peers[t]){this.peers[t]=this.createPeer(t);this.runCallback(BX.Call.Event.onUserInvited,{userId:t})}if(!this.users.includes(t)){this.users.push(t)}this.peers[t].setReady(true)};BX.Call.VoximplantCall.prototype.__onPullEventAnswerSelf=function(e){if(e.callInstanceId===this.instanceId){return}this.joinedElsewhere=true;this.runCallback(BX.Call.Event.onJoin,{local:false})};BX.Call.VoximplantCall.prototype.__onPullEventHangup=function(e){var t=e.senderId;if(this.userId==t&&this.instanceId!=e.callInstanceId){this.runCallback(BX.Call.Event.onLeave,{local:false});return}if(!this.peers[t])return;this.peers[t].setReady(false);if(e.code==603){this.peers[t].setDeclined(true)}else if(e.code==486){this.peers[t].setBusy(true);console.error("user "+t+" is busy")}if(this.ready&&!this.isAnyoneParticipating()){this.hangup()}};BX.Call.VoximplantCall.prototype.__onPullEventUsersInvited=function(e){this.log("__onPullEventUsersInvited",e);var t=e.users;this.addInvitedUsers(t)};BX.Call.VoximplantCall.prototype.__onPullEventUserInviteTimeout=function(e){this.log("__onPullEventUserInviteTimeout",e);var t=e.failedUserId;if(this.peers[t]){this.peers[t].onInviteTimeout(false)}};BX.Call.VoximplantCall.prototype.__onPullEventPing=function(e){if(e.callInstanceId==this.instanceId){return}if(e.senderId==this.userId){if(!this.joinedElsewhere){this.runCallback(BX.Call.Event.onJoin,{local:false});this.joinedElsewhere=true}clearTimeout(this.lastSelfPingReceivedTimeout);this.lastSelfPingReceivedTimeout=setTimeout(this.__onNoSelfPingsReceived.bind(this),a*2.1)}clearTimeout(this.lastPingReceivedTimeout);this.lastPingReceivedTimeout=setTimeout(this.__onNoPingsReceived.bind(this),a*2.1)};BX.Call.VoximplantCall.prototype.__onNoPingsReceived=function(){if(!this.ready){this.destroy()}};BX.Call.VoximplantCall.prototype.__onNoSelfPingsReceived=function(){this.runCallback(BX.Call.Event.onLeave,{local:false});this.joinedElsewhere=false};BX.Call.VoximplantCall.prototype.__onPullEventFinish=function(e){this.destroy()};BX.Call.VoximplantCall.prototype.__onLocalDevicesUpdated=function(e){this.log("__onLocalDevicesUpdated",e)};BX.Call.VoximplantCall.prototype.__onLocalMediaRendererAdded=function(e){var t=e.renderer;var i=t.stream.getVideoTracks().length>0?t.stream.getVideoTracks()[0].label:"";this.log("__onLocalMediaRendererAdded",t.kind,i);if(t.kind==="video"){if(i.match(/^screen|window|tab/i)){var n="screen"}else{n="main"}this.runCallback(BX.Call.Event.onLocalMediaReceived,{tag:n,stream:t.stream})}else if(t.kind==="sharing"){this.runCallback(BX.Call.Event.onLocalMediaReceived,{tag:"screen",stream:t.stream})}};BX.Call.VoximplantCall.prototype.__onBeforeLocalMediaRendererRemoved=function(e){var t=e.renderer;this.log("__onBeforeLocalMediaRendererRemoved",t.kind);if(t.kind==="sharing"&&!this.videoEnabled){this.runCallback(BX.Call.Event.onLocalMediaReceived,{tag:"main",stream:new MediaStream})}};BX.Call.VoximplantCall.prototype.__onCallDisconnected=function(e){this.log("__onCallDisconnected",e);this.ready=false;this.muted=false;this.reinitPeers();this._hideLocalVideo();this.removeCallEvents();this.voximplantCall=null;var t=VoxImplant.getInstance();t.enableSilentLogging(false);t.setLoggerCallback(null);this.runCallback(BX.Call.Event.onLeave,{local:true})};BX.Call.VoximplantCall.prototype.onFatalError=function(e){if(e&&e.call){delete e.call}this.log("onFatalError",e);this.ready=false;this.muted=false;this.reinitPeers();this._hideLocalVideo().then(function(){if(this.voximplantCall){this.removeCallEvents();try{this.voximplantCall.hangup({"X-Reason":"Fatal error","X-Error":typeof e==="string"?e:e.code||e.name})}catch(e){}this.voximplantCall=null}var t=VoxImplant.getInstance();t.enableSilentLogging(false);t.setLoggerCallback(null);if(typeof e==="string"){this.runCallback(BX.Call.Event.onCallFailure,{name:e})}else if(e.name){this.runCallback(BX.Call.Event.onCallFailure,e)}}.bind(this))};BX.Call.VoximplantCall.prototype.__onCallEndpointAdded=function(e){var t=e.endpoint;var i=t.userName;this.log("__onCallEndpointAdded ("+i+")",e.endpoint);if(BX.type.isNotEmptyString(i)&&i.substr(0,4)=="user"){var n=parseInt(i.substr(4));if(this.peers[n]){this.peers[n].setEndpoint(t)}}else{t.addEventListener(VoxImplant.EndpointEvents.InfoUpdated,function(e){var t=e.endpoint;var i=t.userName;this.log("VoxImplant.EndpointEvents.InfoUpdated ("+i+")",e.endpoint);if(BX.type.isNotEmptyString(i)&&i.substr(0,4)=="user"){var n=parseInt(i.substr(4));if(this.peers[n]){this.peers[n].setEndpoint(t)}}}.bind(this));this.log("Unknown endpoint "+i)}this.setVideoQuality(BX.CallEngine.getAllowedVideoQuality(this.voximplantCall.getEndpoints().length))};BX.Call.VoximplantCall.prototype.__onCallMessageReceived=function(e){var t;try{t=JSON.parse(e.text)}catch(e){this.log("Could not parse scenario message.",e);return}var n=t.eventName;if(n===i.voiceStarted){this.runCallback(BX.Call.Event.onUserVoiceStarted,{userId:t.senderId})}else if(n===i.voiceStopped){this.runCallback(BX.Call.Event.onUserVoiceStopped,{userId:t.senderId})}else if(n===i.microphoneState){this.runCallback(BX.Call.Event.onUserMicrophoneState,{userId:t.senderId,microphoneState:t.microphoneState==="Y"})}else{this.log("Unknown scenario event "+n)}};BX.Call.VoximplantCall.prototype.destroy=function(){this.ready=false;this._hideLocalVideo();if(this.voximplantCall){this.removeCallEvents();if(this.voximplantCall.state()!="ENDED"){this.voximplantCall.hangup()}this.voximplantCall=null}for(var e in this.peers){if(this.peers.hasOwnProperty(e)&&this.peers[e]){this.peers[e].destroy()}}this.removeClientEvents();clearTimeout(this.lastPingReceivedTimeout);clearTimeout(this.lastSelfPingReceivedTimeout);clearInterval(this.pingUsersInterval);clearInterval(this.pingBackendInterval);this.runCallback(BX.Call.Event.onDestroy)};BX.Call.VoximplantCall.Signaling=function(e){this.call=e.call};BX.Call.VoximplantCall.Signaling.prototype.inviteUsers=function(t){return this.__runRestAction(e.invite,t)};BX.Call.VoximplantCall.Signaling.prototype.sendAnswer=function(t){return this.__runRestAction(e.answer,t)};BX.Call.VoximplantCall.Signaling.prototype.sendCancel=function(t){return this.__runRestAction(e.cancel,t)};BX.Call.VoximplantCall.Signaling.prototype.sendHangup=function(i){if(BX.PULL.isPublishingEnabled()){this.__sendPullEvent(t.hangup,i);i.retransmit=false;this.__runRestAction(e.hangup,i)}else{i.retransmit=true;this.__runRestAction(e.hangup,i)}};BX.Call.VoximplantCall.Signaling.prototype.sendVoiceStarted=function(e){return this.__sendMessage(i.voiceStarted,e)};BX.Call.VoximplantCall.Signaling.prototype.sendVoiceStopped=function(e){return this.__sendMessage(i.voiceStopped,e)};BX.Call.VoximplantCall.Signaling.prototype.sendMicrophoneState=function(e){return this.__sendMessage(i.microphoneState,{microphoneState:e?"Y":"N"})};BX.Call.VoximplantCall.Signaling.prototype.sendPingToUsers=function(e){if(BX.PULL.isPublishingEnabled()){this.__sendPullEvent(t.ping,e,0)}};BX.Call.VoximplantCall.Signaling.prototype.sendPingToBackend=function(){this.__runRestAction(e.ping,{retransmit:false})};BX.Call.VoximplantCall.Signaling.prototype.sendUserInviteTimeout=function(e){if(BX.PULL.isPublishingEnabled()){this.__sendPullEvent(t.userInviteTimeout,e,0)}};BX.Call.VoximplantCall.Signaling.prototype.__sendPullEvent=function(e,t,i){i=i||5;if(!t.userId){throw new Error("userId is not found in data")}if(!BX.type.isArray(t.userId)){t.userId=[t.userId]}t.callInstanceId=this.call.instanceId;t.senderId=this.call.userId;t.callId=this.call.id;t.requestId=BX.Call.Engine.getInstance().getUuidv4();this.call.log("Sending p2p signaling event "+e+"; "+JSON.stringify(t));BX.PULL.sendMessage(t.userId,"im",e,t,i)};BX.Call.VoximplantCall.Signaling.prototype.__sendMessage=function(e,t){if(!this.call.voximplantCall){return}if(!BX.type.isPlainObject(t)){t={}}t.eventName=e;t.requestId=BX.Call.Engine.getInstance().getUuidv4();this.call.voximplantCall.sendMessage(JSON.stringify(t))};BX.Call.VoximplantCall.Signaling.prototype.__runRestAction=function(e,t){if(!BX.type.isPlainObject(t)){t={}}t.callId=this.call.id;t.callInstanceId=this.call.instanceId;t.requestId=BX.Call.Engine.getInstance().getUuidv4();return BX.CallEngine.getRestClient().callMethod(e,t)};BX.Call.VoximplantCall.Peer=function(e){this.userId=e.userId;this.call=e.call;this.ready=!!e.ready;this.calling=false;this.declined=false;this.busy=false;this.inviteTimeout=false;this.endpoint=null;this.stream=null;this.tracks={audio:null,video:null,sharing:null};this.callingTimeout=0;this.connectionRestoreTimeout=0;this.callbacks={onStateChanged:BX.type.isFunction(e.onStateChanged)?e.onStateChanged:BX.DoNothing,onInviteTimeout:BX.type.isFunction(e.onInviteTimeout)?e.onInviteTimeout:BX.DoNothing,onStreamReceived:BX.type.isFunction(e.onStreamReceived)?e.onStreamReceived:BX.DoNothing,onStreamRemoved:BX.type.isFunction(e.onStreamRemoved)?e.onStreamRemoved:BX.DoNothing};this.__onEndpointRemoteMediaAddedHandler=this.__onEndpointRemoteMediaAdded.bind(this);this.__onEndpointRemoteMediaRemovedHandler=this.__onEndpointRemoteMediaRemoved.bind(this);this.__onEndpointRemovedHandler=this.__onEndpointRemoved.bind(this);this.calculatedState=this.calculateState()};BX.Call.VoximplantCall.Peer.prototype={setReady:function(e){e=!!e;if(this.ready==e){return}this.ready=e;if(this.calling){clearTimeout(this.callingTimeout);this.calling=false;this.inviteTimeout=false}if(this.ready){this.declined=false;this.busy=false}else{clearTimeout(this.connectionRestoreTimeout)}this.updateCalculatedState()},setDeclined:function(e){this.declined=e;if(this.calling){clearTimeout(this.callingTimeout);this.calling=false}if(this.declined){this.ready=false;this.busy=false}clearTimeout(this.connectionRestoreTimeout);this.updateCalculatedState()},setBusy:function(e){this.busy=e;if(this.calling){clearTimeout(this.callingTimeout);this.calling=false}if(this.busy){this.ready=false;this.declined=false}clearTimeout(this.connectionRestoreTimeout);this.updateCalculatedState()},setEndpoint:function(e){this.log("Adding endpoint with "+e.mediaRenderers.length+" media renderers");this.setReady(true);this.inviteTimeout=false;this.declined=false;clearTimeout(this.connectionRestoreTimeout);if(this.endpoint){this.removeEndpointEventHandlers();this.endpoint=null}this.endpoint=e;for(var t=0;t<this.endpoint.mediaRenderers.length;t++){this.addMediaRenderer(this.endpoint.mediaRenderers[t]);if(this.endpoint.mediaRenderers[t].element){BX.remove(this.endpoint.mediaRenderers[t].element)}}this.bindEndpointEventHandlers()},addMediaRenderer:function(e){this.log("Adding media renderer");if(!this.stream){this.stream=new MediaStream}e.stream.getTracks().forEach(function(t){if(t.kind=="audio"){this.tracks.audio=t}else if(t.kind=="video"){if(e.kind=="sharing"){this.tracks.sharing=t}else{this.tracks.video=t}}else{this.log("Unknown track kind "+t.kind)}},this);this.updateMediaStream();this.updateCalculatedState()},updateMediaStream:function(){if(!this.stream){this.stream=new MediaStream}this.stream.getTracks().forEach(function(e){if(!this.hasTrack(e)){this.stream.removeTrack(e)}},this);if(this.tracks.audio&&!this.stream.getTrackById(this.tracks.audio.id)){this.stream.addTrack(this.tracks.audio)}if(this.tracks.sharing){if(this.tracks.video&&this.stream.getTrackById(this.tracks.video.id)){this.stream.removeTrack(this.tracks.video)}if(!this.stream.getTrackById(this.tracks.sharing.id)){this.stream.addTrack(this.tracks.sharing)}}else{if(this.tracks.video&&!this.stream.getTrackById(this.tracks.video.id)){this.stream.addTrack(this.tracks.video)}}this.callbacks.onStreamReceived({userId:this.userId,stream:this.stream})},hasTrack:function(e){for(var t in this.tracks){if(!this.tracks.hasOwnProperty(t)){continue}if(this.tracks.kind&&this.tracks.kind.id==e.id){return true}}return false},removeTrack:function(e){for(var t in this.tracks){if(!this.tracks.hasOwnProperty(t)){continue}var i=this.tracks[t]?this.tracks[t].id:"";if(i==e.id){this.tracks[t]=null}}},bindEndpointEventHandlers:function(){this.endpoint.addEventListener(VoxImplant.EndpointEvents.RemoteMediaAdded,this.__onEndpointRemoteMediaAddedHandler);this.endpoint.addEventListener(VoxImplant.EndpointEvents.RemoteMediaRemoved,this.__onEndpointRemoteMediaRemovedHandler);this.endpoint.addEventListener(VoxImplant.EndpointEvents.Removed,this.__onEndpointRemovedHandler)},removeEndpointEventHandlers:function(){this.endpoint.removeEventListener(VoxImplant.EndpointEvents.RemoteMediaAdded,this.__onEndpointRemoteMediaAddedHandler);this.endpoint.removeEventListener(VoxImplant.EndpointEvents.RemoteMediaRemoved,this.__onEndpointRemoteMediaRemovedHandler);this.endpoint.removeEventListener(VoxImplant.EndpointEvents.Removed,this.__onEndpointRemovedHandler)},calculateState:function(){if(this.stream)return BX.Call.UserState.Connected;if(this.endpoint)return BX.Call.UserState.Connecting;if(this.calling)return BX.Call.UserState.Calling;if(this.inviteTimeout)return BX.Call.UserState.Unavailable;if(this.declined)return BX.Call.UserState.Declined;if(this.busy)return BX.Call.UserState.Busy;if(this.ready)return BX.Call.UserState.Ready;return BX.Call.UserState.Idle},updateCalculatedState:function(){var e=this.calculateState();if(this.calculatedState!=e){this.callbacks.onStateChanged({userId:this.userId,state:e,previousState:this.calculatedState});this.calculatedState=e}},isParticipating:function(){return(this.calling||this.ready||this.endpoint)&&!this.declined},waitForConnectionRestore:function(){clearTimeout(this.connectionRestoreTimeout);this.connectionRestoreTimeout=setTimeout(this.onConnectionRestoreTimeout.bind(this),o)},onInvited:function(){this.ready=false;this.inviteTimeout=false;this.declined=false;this.calling=true;clearTimeout(this.connectionRestoreTimeout);if(this.callingTimeout){clearTimeout(this.callingTimeout)}this.callingTimeout=setTimeout(function(){this.onInviteTimeout(true)}.bind(this),3e4);this.updateCalculatedState()},onInviteTimeout:function(e){clearTimeout(this.callingTimeout);if(!this.calling){return}this.calling=false;this.inviteTimeout=true;if(e){this.callbacks.onInviteTimeout({userId:this.userId})}this.updateCalculatedState()},onConnectionRestoreTimeout:function(){if(this.endpoint||!this.ready){return}this.log("Done waiting for connection restoration");this.setReady(false)},__onEndpointRemoteMediaAdded:function(e){this.log("VoxImplant.EndpointEvents.RemoteMediaAdded",e);if(e.mediaRenderer.element){e.mediaRenderer.element.volume=0;e.mediaRenderer.element.srcObject=null}this.addMediaRenderer(e.mediaRenderer)},__onEndpointRemoteMediaRemoved:function(e){this.log("VoxImplant.EndpointEvents.RemoteMediaRemoved, track id: "+e.mediaRenderer.stream.getTracks()[0].id,e);e.mediaRenderer.stream.getTracks().forEach(function(e){this.removeTrack(e)},this);if(this.stream){this.updateMediaStream()}this.updateCalculatedState()},__onEndpointRemoved:function(e){this.log("VoxImplant.EndpointEvents.Removed",e);if(this.endpoint){this.removeEndpointEventHandlers();this.endpoint=null}if(this.stream){this.stream=null}for(var t in this.tracks){if(this.tracks.hasOwnProperty(t)){this.tracks[t]=null}}if(this.ready){this.waitForConnectionRestore()}this.updateCalculatedState()},log:function(){this.call.log.apply(this.call,arguments)},destroy:function(){if(this.stream){this.stream.getTracks().forEach(function(e){e.stop()});this.stream=null}if(this.endpoint){this.removeEndpointEventHandlers();this.endpoint=null}for(var e in this.tracks){if(this.tracks.hasOwnProperty(e)){if(this.tracks[e]&&this.tracks[e].stop){this.tracks[e].stop()}this.tracks[e]=null}}this.callbacks["onStateChanged"]=BX.DoNothing;this.callbacks["onStreamReceived"]=BX.DoNothing;this.callbacks["onStreamRemoved"]=BX.DoNothing;clearTimeout(this.callingTimeout);clearTimeout(this.connectionRestoreTimeout);this.callingTimeout=null;this.connectionRestoreTimeout=null}};BX.Call.VoximplantCall.Event=n})(); //# sourceMappingURL=voximplant_call.map.js