-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpalava.min.js
24 lines (19 loc) · 28.5 KB
/
palava.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
palava v2.2.1 | LGPL | https://github.com/palavatv/palava-client
Copyright (C) 2014-2020 palava e. V. [email protected]
Copyright (C) 2013 Jan Lelis [email protected]
Copyright (C) 2013 Marius Melzer [email protected]
Copyright (C) 2013 Stephan Thamm [email protected]
Copyright (C) 2013 Kilian Ulbrich [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
(function(){}).call(this),function(){this.palava={browser:{}}}.call(this),function(){"object"==typeof module&&"object"==typeof module.exports&&(module.exports=this.palava),"object"!=typeof EventEmitter&&"function"==typeof require?this.EventEmitter=require("wolfy87-eventemitter"):this.EventEmitter=EventEmitter,"object"!=typeof adapter&&"function"==typeof require?this.adapter=require("webrtc-adapter/out/adapter_no_edge"):this.adapter=adapter}.call(this),function(){var t,r;r=this.palava,t=this.adapter,r.browser.isMozilla=function(){return"firefox"===t.browserDetails.browser},r.browser.isChrome=function(){return"chrome"===t.browserDetails.browser},r.browser.getUserAgent=function(){return t.browserDetails.browser},r.browser.getUserAgentVersion=function(){return t.browserDetails.version},r.browser.checkForWebrtcError=function(){try{new window.RTCPeerConnection({iceServers:[]})}catch(t){return t}return!(window.RTCPeerConnection&&window.RTCIceCandidate&&window.RTCSessionDescription&&navigator.mediaDevices&&navigator.mediaDevices.getUserMedia)},r.browser.getConstraints=function(){return{optional:[],mandatory:{OfferToReceiveAudio:!0,OfferToReceiveVideo:!0}}},r.browser.getPeerConnectionOptions=function(){return r.browser.isChrome()?{optional:[{DtlsSrtpKeyAgreement:!0}]}:{}},r.browser.registerFullscreen=function(t,e){return console.log("DEPRECATED: palava.browser.registerFullscreen will be removed from the palava library in early 2021"),t.requestFullscreen?t.addEventListener(e,function(){return this.requestFullscreen()}):t.mozRequestFullScreen?t.addEventListener(e,function(){return this.mozRequestFullScreen()}):t.webkitRequestFullscreen?t.addEventListener(e,function(){return this.webkitRequestFullscreen()}):void 0},r.browser.attachMediaStream=function(t,e){return e?t.srcObject=e:(t.pause(),t.srcObject=null)},r.browser.attachPeer=function(t,e){var n;return n=function(){return r.browser.attachMediaStream(t,e.getStream()),e.isLocal()&&t.setAttribute("muted",!0),t.play()},e.getStream()?n():e.on("stream_ready",function(){return n()})}}.call(this),function(){var n=function(t,e){return function(){return t.apply(e,arguments)}},r=function(t,e){function n(){this.constructor=t}for(var r in e)i.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},i={}.hasOwnProperty;this.palava.Gum=function(t){function e(t){this.releaseStream=n(this.releaseStream,this),this.getStream=n(this.getStream,this),this.requestStream=n(this.requestStream,this),this.changeConfig=n(this.changeConfig,this),this.config=t||{video:!0,audio:!0},this.stream=null}return r(e,t),e.prototype.changeConfig=function(t){return this.config=t,this.releaseStream(),this.requestStream()},e.prototype.requestStream=function(){return navigator.mediaDevices.getUserMedia(this.config).then((n=this,function(t){return n.stream=t,n.emit("stream_ready",t)}))["catch"]((e=this,function(t){return e.emit("stream_error",t)}));var e,n},e.prototype.getStream=function(){return this.stream},e.prototype.releaseStream=function(){return!!this.stream&&(this.stream.getAudioTracks().forEach(function(t){return t.stop()}),this.stream.getVideoTracks().forEach(function(t){return t.stop()}),this.stream=null,this.emit("stream_released",this),!0)},e}(this.EventEmitter)}.call(this),function(){var e,n=function(t,e){return function(){return t.apply(e,arguments)}};(e=this.palava).Identity=function(){function t(t){this.getStatus=n(this.getStatus,this),this.getName=n(this.getName,this),this.userMediaConfig=t.userMediaConfig,this.status=t.status||{},this.status.name=t.name}return t.prototype.newUserMedia=function(){return new e.Gum(this.userMediaConfig)},t.prototype.getName=function(){return this.name},t.prototype.getStatus=function(){return this.status},t}()}.call(this),function(){var r,i=function(t,e){return function(){return t.apply(e,arguments)}},n=function(t,e){function n(){this.constructor=t}for(var r in e)o.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},o={}.hasOwnProperty;(r=this.palava).Peer=function(t){function e(t,e){var n;this.isRemote=i(this.isRemote,this),this.isLocal=i(this.isLocal,this),this.isReady=i(this.isReady,this),this.isMuted=i(this.isMuted,this),this.getError=i(this.getError,this),this.hasError=i(this.hasError,this),this.hasVideo=i(this.hasVideo,this),this.transmitsVideo=i(this.transmitsVideo,this),this.hasAudio=i(this.hasAudio,this),this.transmitsAudio=i(this.transmitsAudio,this),this.id=t,this.status=e||{},(n=this.status).user_agent||(n.user_agent=r.browser.getUserAgent()),this.joinTime=(new Date).getTime(),this.ready=!1,this.error=null}return n(e,t),e.prototype.transmitsAudio=function(){var t,e,n;return!!(null!=(t=this.getStream())&&null!=(e=t.getAudioTracks())&&null!=(n=e[0])?n.enabled:void 0)},e.prototype.hasAudio=function(){var t,e;return!!(null!=(t=this.getStream())&&null!=(e=t.getAudioTracks())?e[0]:void 0)},e.prototype.transmitsVideo=function(){var t,e,n;return!!(null!=(t=this.getStream())&&null!=(e=t.getVideoTracks())&&null!=(n=e[0])?n.enabled:void 0)},e.prototype.hasVideo=function(){var t,e;return!!(null!=(t=this.getStream())&&null!=(e=t.getVideoTracks())?e[0]:void 0)},e.prototype.hasError=function(){return!!this.error},e.prototype.getError=function(){return this.error},e.prototype.isMuted=function(){return!!this.muted},e.prototype.isReady=function(){return!!this.ready},e.prototype.isLocal=function(){return!!this.local},e.prototype.isRemote=function(){return!this.local},e}(this.EventEmitter)}.call(this),function(){var i,o=function(t,e){return function(){return t.apply(e,arguments)}},e=function(t,e){function n(){this.constructor=t}for(var r in e)s.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},s={}.hasOwnProperty;(i=this.palava).LocalPeer=function(t){function r(t,e,n){this.leave=o(this.leave,this),this.enableVideo=o(this.enableVideo,this),this.disableVideo=o(this.disableVideo,this),this.enableAudio=o(this.enableAudio,this),this.disableAudio=o(this.disableAudio,this),this.updateStatus=o(this.updateStatus,this),this.getStream=o(this.getStream,this),this.setupRoom=o(this.setupRoom,this),this.setupUserMedia=o(this.setupUserMedia,this),this.muted=!0,this.local=!0,r.__super__.constructor.call(this,t,e),this.room=n,this.userMedia=n.userMedia,this.setupRoom(),this.setupUserMedia()}return e(r,t),r.prototype.setupUserMedia=function(){var t,e,n;if(this.userMedia.on("stream_released",(t=this,function(){return t.ready=!1,t.emit("stream_removed")})),this.userMedia.on("stream_ready",(e=this,function(t){return e.ready=!0,e.emit("stream_ready",t)})),this.userMedia.on("stream_error",(n=this,function(t){return n.emit("stream_error",t)})),this.getStream())return this.ready=!0,this.emit("stream_ready")},r.prototype.setupRoom=function(){var t,e,n;return this.room.peers[this.id]=this.room.localPeer=this,this.on("update",(t=this,function(){return t.room.emit("peer_update",t)})),this.on("stream_ready",(e=this,function(){return e.room.emit("peer_stream_ready",e)})),this.on("stream_removed",(n=this,function(){return n.room.emit("peer_stream_removed",n)}))},r.prototype.getStream=function(){return this.userMedia.getStream()},r.prototype.updateStatus=function(t){var e,n;if(!(t&&t instanceof Object&&0!==Object.keys(t).length))return t;for(n in t)this.status[n]=t[n];return(e=this.status).user_agent||(e.user_agent=i.browser.getUserAgent()),this.room.channel.send({event:"update_status",status:this.status}),this.status},r.prototype.disableAudio=function(){var t,e,n,r,i;if(this.ready){for(r=[],t=0,e=(n=this.getStream().getAudioTracks()).length;t<e;t++)i=n[t],r.push(i.enabled=!1);return r}},r.prototype.enableAudio=function(){var t,e,n,r,i;if(this.ready){for(r=[],t=0,e=(n=this.getStream().getAudioTracks()).length;t<e;t++)i=n[t],r.push(i.enabled=!0);return r}},r.prototype.disableVideo=function(){var t,e,n,r,i;if(this.ready){for(r=[],t=0,e=(n=this.getStream().getVideoTracks()).length;t<e;t++)i=n[t],r.push(i.enabled=!1);return r}},r.prototype.enableVideo=function(){var t,e,n,r,i;if(this.ready){for(r=[],t=0,e=(n=this.getStream().getVideoTracks()).length;t<e;t++)i=n[t],r.push(i.enabled=!0);return r}},r.prototype.leave=function(){return this.ready=!1,this.emit("left")},r}(i.Peer)}.call(this),function(){var n=function(t,e){return function(){return t.apply(e,arguments)}};this.palava.Distributor=function(){function t(t,e){null==e&&(e=null),this.send=n(this.send,this),this.on=n(this.on,this),this.channel=t,this.peerId=e}return t.prototype.on=function(e,n){return this.channel.on("message",(r=this,function(t){if(r.peerId){if(t.sender_id===r.peerId&&e===t.event)return n(t)}else if(!t.sender_id&&e===t.event)return n(t)}));var r},t.prototype.send=function(t){var e;return e=this.peerId?{event:"send_to_peer",peer_id:this.peerId,data:t}:t,this.channel.send(e)},t}()}.call(this),function(){var n=function(t,e){function n(){this.constructor=t}for(var r in e)i.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},i={}.hasOwnProperty;this.palava.DataChannel=function(t){function e(t){var e,n,r;this.channel=t,this.channel.onmessage=(e=this,function(t){return e.emit("message",t.data)}),this.channel.onclose=(n=this,function(){return n.emit("close")}),this.channel.onerror=(r=this,function(t){return r.emit("error",t)}),this.sendBuffer=[]}return n(e,t),e.prototype.MAX_BUFFER=1048576,e.prototype.send=function(t,e){if(this.sendBuffer.push([t,e]),1===this.sendBuffer.length)return this.actualSend()},e.prototype.actualSend=function(){var t,e,n,r;if("open"===this.channel.readyState)for(;this.sendBuffer.length;){if(this.channel.bufferedAmount>this.MAX_BUFFER)return void setTimeout(this.actualSend.bind(this),1);e=(r=this.sendBuffer[0])[0],t=r[1];try{this.channel.send(e)}catch(i){return n=i,void setTimeout(this.actualSend.bind(this),1)}try{"function"==typeof t&&t()}catch(i){n=i,console.log("Exception in write callback:",n)}this.sendBuffer.shift()}else console.log("Not sending when not open!")},e}(this.EventEmitter)}.call(this),function(){var c,s=function(t,e){return function(){return t.apply(e,arguments)}},e=function(t,e){function n(){this.constructor=t}for(var r in e)i.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},i={}.hasOwnProperty;(c=this.palava).RemotePeer=function(t){function o(t,e,n,r,i){this.closePeerConnection=s(this.closePeerConnection,this),this.oaError=s(this.oaError,this),this.sdpSender=s(this.sdpSender,this),this.sendMessage=s(this.sendMessage,this),this.sendAnswer=s(this.sendAnswer,this),this.sendOffer=s(this.sendOffer,this),this.setupRoom=s(this.setupRoom,this),this.setupDistributor=s(this.setupDistributor,this),this.setupPeerConnection=s(this.setupPeerConnection,this),this.generateIceOptions=s(this.generateIceOptions,this),this.toggleMute=s(this.toggleMute,this),this.getStream=s(this.getStream,this),this.muted=!1,this.local=!1,o.__super__.constructor.call(this,t,e),this.room=n,this.remoteStream=null,this.turnCredentials=i,this.dataChannels={},this.setupRoom(),this.setupPeerConnection(r),this.setupDistributor(),r&&this.sendOffer()}return e(o,t),o.prototype.getStream=function(){return this.remoteStream},o.prototype.toggleMute=function(){return this.muted=!this.muted},o.prototype.generateIceOptions=function(){var t;return t=[],this.room.options.stun&&t.push({urls:[this.room.options.stun]}),this.room.options.turnUrls&&this.turnCredentials&&t.push({urls:this.room.options.turnUrls,username:this.turnCredentials.user,credential:this.turnCredentials.password}),{iceServers:t}},o.prototype.setupPeerConnection=function(t){var e,n,r,i,o,s,a,u,h;if(this.peerConnection=new RTCPeerConnection(this.generateIceOptions(),c.browser.getPeerConnectionOptions()),this.peerConnection.onicecandidate=(o=this,function(t){if(t.candidate)return o.distributor.send({event:"ice_candidate",sdpmlineindex:t.candidate.sdpMLineIndex,sdpmid:t.candidate.sdpMid,candidate:t.candidate.candidate})}),this.peerConnection.ontrack=(s=this,function(t){return s.remoteStream=t.streams[0],s.ready=!0,s.emit("stream_ready")}),this.peerConnection.onremovestream=(a=this,function(){return a.remoteStream=null,a.ready=!1,a.emit("stream_removed")}),this.peerConnection.oniceconnectionstatechange=(u=this,function(t){switch(t.target.iceConnectionState){case"connecting":return u.error=null,u.emit("connection_pending");case"connected":return u.error=null,u.emit("connection_established");case"failed":return u.error="connection_failed",u.emit("connection_failed");case"disconnected":return u.error="connection_disconnected",u.emit("connection_disconnected");case"closed":return u.error="connection_closed",u.emit("connection_closed")}}),this.room.localPeer.getStream()&&this.peerConnection.addStream(this.room.localPeer.getStream()),null!=this.room.options.dataChannels)if(h=this,i=function(t){var e,n;return e=t.label,n=new c.DataChannel(t),h.dataChannels[e]=n,h.emit("channel_ready",e,n)},t)for(e in r=this.room.options.dataChannels)n=r[e],this.peerConnection.createDataChannel(e,n).onopen=function(){return i(this)};else this.peerConnection.ondatachannel=function(t){return i(t.channel)};return this.peerConnection},o.prototype.setupDistributor=function(){var t,n,e,r,i,o;return this.distributor=new c.Distributor(this.room.channel,this.id),this.distributor.on("peer_left",(t=this,function(){return t.ready&&(t.remoteStream=null,t.emit("stream_removed"),t.ready=!1),t.peerConnection.close(),t.emit("left")})),this.distributor.on("ice_candidate",(n=this,function(t){var e;if(""!==t.candidate)return e=new RTCIceCandidate({candidate:t.candidate,sdpMLineIndex:t.sdpmlineindex,sdpMid:t.sdpmid}),n.room.options.filterIceCandidateTypes.includes(e.type)?void 0:n.peerConnection.addIceCandidate(e)})),this.distributor.on("offer",(e=this,function(t){return e.peerConnection.setRemoteDescription(new RTCSessionDescription(t.sdp)),e.emit("offer"),e.sendAnswer()})),this.distributor.on("answer",(r=this,function(t){return r.peerConnection.setRemoteDescription(new RTCSessionDescription(t.sdp)),r.emit("answer")})),this.distributor.on("peer_updated_status",(i=this,function(t){return i.status=t.status,i.emit("update")})),this.distributor.on("message",(o=this,function(t){return o.emit("message",t.data)})),this.distributor},o.prototype.setupRoom=function(){var t,e,n,r,i,o,s,a,u,h,c,p,d;return(this.room.peers[this.id]=this).on("left",(t=this,function(){return delete t.room.peers[t.id],t.room.emit("peer_left",t)})),this.on("offer",(e=this,function(){return e.room.emit("peer_offer",e)})),this.on("answer",(n=this,function(){return n.room.emit("peer_answer",n)})),this.on("update",(r=this,function(){return r.room.emit("peer_update",r)})),this.on("stream_ready",(i=this,function(){return i.room.emit("peer_stream_ready",i)})),this.on("stream_removed",(o=this,function(){return o.room.emit("peer_stream_removed",o)})),this.on("connection_pending",(s=this,function(){return s.room.emit("peer_connection_pending",s)})),this.on("connection_established",(a=this,function(){return a.room.emit("peer_connection_established",a)})),this.on("connection_failed",(u=this,function(){return u.room.emit("peer_connection_failed",u)})),this.on("connection_disconnected",(h=this,function(){return h.room.emit("peer_connection_disconnected",h)})),this.on("connection_closed",(c=this,function(){return c.room.emit("peer_connection_closed",c)})),this.on("oaerror",(p=this,function(t){return p.room.emit("peer_oaerror",p,t)})),this.on("channel_ready",(d=this,function(t,e){return d.room.emit("peer_channel_ready",d,t,e)}))},o.prototype.sendOffer=function(){return this.peerConnection.createOffer(this.sdpSender("offer"),this.oaError,c.browser.getConstraints())},o.prototype.sendAnswer=function(){return this.peerConnection.createAnswer(this.sdpSender("answer"),this.oaError,c.browser.getConstraints())},o.prototype.sendMessage=function(t){return this.distributor.send({event:"message",data:t})},o.prototype.sdpSender=function(e){return n=this,function(t){return n.peerConnection.setLocalDescription(t),n.distributor.send({event:e,sdp:t})};var n},o.prototype.oaError=function(t){return this.emit("oaerror",t)},o.prototype.closePeerConnection=function(){var t;return null!=(t=this.peerConnection)&&t.close(),this.peerConnection=null},o}(c.Peer)}.call(this),function(){var u,i=function(t,e){return function(){return t.apply(e,arguments)}},n=function(t,e){function n(){this.constructor=t}for(var r in e)o.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},o={}.hasOwnProperty;(u=this.palava).Room=function(t){function e(t,e,n,r){null==r&&(r={}),this.getAllPeers=i(this.getAllPeers,this),this.getRemotePeers=i(this.getRemotePeers,this),this.getLocalPeer=i(this.getLocalPeer,this),this.getPeerById=i(this.getPeerById,this),this.destroy=i(this.destroy,this),this.leave=i(this.leave,this),this.join=i(this.join,this),this.setupDistributor=i(this.setupDistributor,this),this.setupOptions=i(this.setupOptions,this),this.setupUserMedia=i(this.setupUserMedia,this),this.id=t,this.userMedia=n,this.channel=e,this.peers={},this.options=r,this.setupUserMedia(),this.setupDistributor(),this.setupOptions()}return n(e,t),e.prototype.setupUserMedia=function(){var e,n,t;return this.userMedia.on("stream_ready",(e=this,function(t){return e.emit("local_stream_ready",t)})),this.userMedia.on("stream_error",(n=this,function(t){return n.emit("local_stream_error",t)})),this.userMedia.on("stream_released",(t=this,function(){return t.emit("local_stream_removed")}))},e.prototype.setupOptions=function(){var t,e,n;return(t=this.options).joinTimeout||(t.joinTimeout=1e3),(e=this.options).ownStatus||(e.ownStatus={}),(n=this.options).filterIceCandidateTypes||(n.filterIceCandidateTypes=[])},e.prototype.setupDistributor=function(){var a,r,e,n;return this.distributor=new u.Distributor(this.channel),this.distributor.on("joined_room",(a=this,function(t){var e,n,r,i,o,s;for(clearTimeout(a.joinCheckTimeout),s=t.turn_user?{user:t.turn_user,password:t.turn_password}:null,new u.LocalPeer(t.own_id,a.options.ownStatus,a),e=0,n=(o=t.peers).length;e<n;e++)i=o[e],r=!u.browser.isChrome(),new u.RemotePeer(i.peer_id,i.status,a,r,s);return a.emit("joined")})),this.distributor.on("new_peer",(r=this,function(t){var e,n;return n="chrome"===t.status.user_agent,e=new u.RemotePeer(t.peer_id,t.status,r,n),r.emit("peer_joined",e)})),this.distributor.on("error",(e=this,function(t){return e.emit("signaling_error","server",t.description)})),this.distributor.on("shutdown",(n=this,function(t){return n.emit("signaling_shutdown",t.seconds)}))},e.prototype.join=function(t){var e,n,r,i,o;for(null==t&&(t={}),this.joinCheckTimeout=setTimeout((o=this,function(){return o.emit("join_error")}),this.options.joinTimeout),n=0,i=t.length;n<i;n++)r=t[n],this.options.ownStatus[r]=t[r];return(e=this.options.ownStatus).user_agent||(e.user_agent=u.browser.getUserAgent()),this.distributor.send({event:"join_room",room_id:this.id,status:this.options.ownStatus})},e.prototype.leave=function(){return this.channel&&this.distributor.send({event:"leave_room"}),this.emit("left")},e.prototype.destroy=function(){return this.getRemotePeers().forEach(function(t){return t.closePeerConnection()}),clearTimeout(this.joinCheckTimeout)},e.prototype.getPeerById=function(t){return this.peers[t]},e.prototype.getLocalPeer=function(){return this.localPeer},e.prototype.getRemotePeers=function(){return this.getAllPeers(!1)},e.prototype.getAllPeers=function(t){var e,n,r,i;for(e in null==t&&(t=!0),r=[],i=this.peers)n=i[e],!t&&n.local||r.push(n);return r},e}(this.EventEmitter)}.call(this),function(){var n=function(t,e){return function(){return t.apply(e,arguments)}},r=function(t,e){function n(){this.constructor=t}for(var r in e)i.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},i={}.hasOwnProperty;this.palava.WebSocketChannel=function(t){function e(t,e){null==e&&(e=2),this.close=n(this.close,this),this.send=n(this.send,this),this.startClientPings=n(this.startClientPings,this),this.setupWebsocket=n(this.setupWebsocket,this),this.sendDeliverOnConnectMessages=n(this.sendDeliverOnConnectMessages,this),this.isConnected=n(this.isConnected,this),this.address=t,this.retries=e,this.messagesToDeliverOnConnect=[],this.setupWebsocket(),this.startClientPings()}return r(e,t),e.prototype.isConnected=function(){var t;return 1===(null!=(t=this.socket)?t.readyState:void 0)},e.prototype.sendDeliverOnConnectMessages=function(){var t,e,n,r;for(t=0,e=(r=this.messagesToDeliverOnConnect).length;t<e;t++)n=r[t],this.socket.send(n);return this.messagesToDeliverOnConnect=[]},e.prototype.setupWebsocket=function(){var e,i,n,t;return this.socket=new WebSocket(this.address),this.socket.onopen=(e=this,function(t){return e.retries=0,e.sendDeliverOnConnectMessages(),e.emit("open",t)}),this.socket.onmessage=(i=this,function(t){var e,n;try{n=JSON.parse(t.data)}catch(r){return e=r,void i.emit("error","invalid_format",{error:e,data:t.data})}return"pong"===n.event?i.outstandingPongs=0:i.emit("message",n)}),this.socket.onerror=(n=this,function(t){return clearInterval(n.pingInterval),0<n.retries?(n.retries-=1,n.setupWebsocket(),n.startClientPings()):n.emit("error","socket",t)}),this.socket.onclose=(t=this,function(){return clearInterval(t.pingInterval),t.emit("close")})},e.prototype.startClientPings=function(){return this.outstandingPongs=0,this.pingInterval=setInterval((t=this,function(){return 6<=t.outstandingPongs&&(clearInterval(t.pingInterval),t.socket.close(),t.emit("error","missing_pongs")),t.socket.send(JSON.stringify({event:"ping"})),t.outstandingPongs+=1}),5e3);var t},e.prototype.send=function(t){return 1===this.socket.readyState?(0!==this.messagesToDeliverOnConnect.length&&this.sendDeliverOnConnectMessages(),this.socket.send(JSON.stringify(t))):1<this.socket.readyState?this.emit("not_reachable"):this.messagesToDeliverOnConnect.push(JSON.stringify(t))},e.prototype.close=function(){return this.socket.close()},e}(this.EventEmitter)}.call(this),function(){var C,n=function(t,e){return function(){return t.apply(e,arguments)}},r=function(t,e){function n(){this.constructor=t}for(var r in e)i.call(e,r)&&(t[r]=e[r]);return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},i={}.hasOwnProperty;(C=this.palava).Session=function(t){function e(t){this.destroy=n(this.destroy,this),this.createRoom=n(this.createRoom,this),this.createChannel=n(this.createChannel,this),this.getRoom=n(this.getRoom,this),this.getUserMedia=n(this.getUserMedia,this),this.getChannel=n(this.getChannel,this),this.checkRequirements=n(this.checkRequirements,this),this.assignOptions=n(this.assignOptions,this),this.tearDown=n(this.tearDown,this),this.reconnect=n(this.reconnect,this),this.connect=n(this.connect,this),this.roomOptions={},this.assignOptions(t)}return r(e,t),e.prototype.connect=function(t){var e;if(this.assignOptions(t),this.checkRequirements())return this.createChannel(),this.createRoom(),this.userMedia.stream?this.room.join():this.userMedia.requestStream().then((e=this,function(){return e.room.join()}))},e.prototype.reconnect=function(){return this.emit("session_reconnect"),this.tearDown(),this.createChannel(),this.createRoom(),this.room.join()},e.prototype.tearDown=function(t){var e,n,r,i,o,s;if(null==t&&(t=!1),null!=(e=this.room)&&e.removeAllListeners(),null!=(n=this.channel)&&n.removeAllListeners(),(null!=(r=this.channel)?r.isConnected():void 0)&&null!=(i=this.room)&&i.leave(),null!=(o=this.channel)&&o.close(),(this.channel=null)!=(s=this.room)&&s.destroy(),this.room=null,t&&this.userMedia)return this.userMedia.releaseStream()},e.prototype.assignOptions=function(t){if(t.roomId&&(this.roomId=t.roomId),t.webSocketAddress&&(this.webSocketAddress=t.webSocketAddress),t.identity&&(this.userMedia=t.identity.newUserMedia(),this.roomOptions.ownStatus=t.identity.getStatus()),t.userMediaConfig&&(this.userMedia=new C.Gum(t.userMediaConfig)),t.dataChannels&&(this.roomOptions.dataChannels=t.dataChannels),t.stun&&(this.roomOptions.stun=t.stun),t.turnUrls&&(this.roomOptions.turnUrls=t.turnUrls),t.joinTimeout&&(this.roomOptions.joinTimeout=t.joinTimeout),t.filterIceCandidateTypes)return this.roomOptions.filterIceCandidateTypes=t.filterIceCandidateTypes},e.prototype.checkRequirements=function(){var t;return this.webSocketAddress?this.userMedia?this.roomId?this.roomOptions.stun?this.roomOptions.turnUrls&&!Array.isArray(this.roomOptions.turnUrls)?(this.emit("argument_error","turnUrls must be an array"),!1):navigator.onLine?!(t=C.browser.checkForWebrtcError())||(this.emit("webrtc_no_support","WebRTC is not supported by your browser",t),!1):(this.emit("signaling_not_reachable"),!1):(this.emit("argument_error","no stun server given"),!1):(this.emit("argument_error","no room id given"),!1):(this.emit("argument_error","no user media given"),!1):(this.emit("argument_error","no web socket address given"),!1)},e.prototype.getChannel=function(){return this.channel},e.prototype.getUserMedia=function(){return this.userMedia},e.prototype.getRoom=function(){return this.room},e.prototype.createChannel=function(){var t,n,e,r;return this.channel=new C.WebSocketChannel(this.webSocketAddress),this.channel.on("open",(t=this,function(){return t.emit("signaling_open")})),this.channel.on("error",(n=this,function(t,e){return n.emit("signaling_error",t,e)})),this.channel.on("close",(e=this,function(t){return e.emit("signaling_close",t)})),this.channel.on("not_reachable",(r=this,function(){return r.emit("signaling_not_reachable")}))},e.prototype.createRoom=function(){var e,n,t,r,i,o,s,a,u,h,c,p,d,l,m,f,_,g,y,v,b,w;return this.room=new C.Room(this.roomId,this.channel,this.userMedia,this.roomOptions),this.room.on("local_stream_ready",(e=this,function(t){return e.emit("local_stream_ready",t)})),this.room.on("local_stream_error",(n=this,function(t){return n.emit("local_stream_error",t)})),this.room.on("local_stream_removed",(t=this,function(){return t.emit("local_stream_removed")})),this.room.on("join_error",(r=this,function(){return r.tearDown(!0),r.emit("room_join_error",r.room)})),this.room.on("full",(i=this,function(){return i.emit("room_full",i.room)})),this.room.on("joined",(o=this,function(){return o.emit("room_joined",o.room)})),this.room.on("left",(s=this,function(){return s.emit("room_left",s.room)})),this.room.on("peer_joined",(a=this,function(t){return a.emit("peer_joined",t)})),this.room.on("peer_offer",(u=this,function(t){return u.emit("peer_offer",t)})),this.room.on("peer_answer",(h=this,function(t){return h.emit("peer_answer",t)})),this.room.on("peer_update",(c=this,function(t){return c.emit("peer_update",t)})),this.room.on("peer_stream_ready",(p=this,function(t){return p.emit("peer_stream_ready",t)})),this.room.on("peer_stream_removed",(d=this,function(t){return d.emit("peer_stream_removed",t)})),this.room.on("peer_connection_pending",(l=this,function(t){return l.emit("peer_connection_pending",t)})),this.room.on("peer_connection_established",(m=this,function(t){return m.emit("peer_connection_established",t)})),this.room.on("peer_connection_failed",(f=this,function(t){return f.emit("peer_connection_failed",t)})),this.room.on("peer_connection_disconnected",(_=this,function(t){return _.emit("peer_connection_disconnected",t)})),this.room.on("peer_connection_closed",(g=this,function(t){return g.emit("peer_connection_closed",t)})),this.room.on("peer_left",(y=this,function(t){return y.emit("peer_left",t)})),this.room.on("peer_channel_ready",(v=this,function(t,e,n){return v.emit("peer_channel_ready",t,e,n)})),this.room.on("signaling_shutdown",(b=this,function(t){return b.emit("signaling_shutdown",t)})),this.room.on("signaling_error",(w=this,function(t,e){return w.emit("signaling_error",t,e)})),!0},e.prototype.destroy=function(){return this.emit("session_before_destroy"),this.tearDown(!0),this.emit("session_after_destroy")},e}(this.EventEmitter)}.call(this),function(){var t;(t=this.palava).PROTOCOL_NAME="palava",t.PROTOCOL_VERSION="1.0.0",t.LIB_VERSION="2.2.1",t.LIB_COMMIT="v2.2.0-1-g84e26da095-dirty",t.protocol_identifier=function(){return t.PROTOCOL_NAME="palava.1.0"}}.call(this);