/*! peerjs.min.js build:0.3.9, production. Copyright(c) 2013 Michelle Bu */(function(e){function n(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(){this.bufferBuilder=new n}function s(e){var t=e.charCodeAt(0);return t<=2047?"00":t<=65535?"000":t<=2097151?"0000":t<=67108863?"00000":"000000"}function o(e){return e.length>600?(new Blob([e])).size:e.replace(/[^\u0000-\u007F]/g,s).length}function u(){this._events={}}function f(e,t){if(!(this instanceof f))return new f(e);this._dc=e,h.debug=t,this._outgoing={},this._incoming={},this._received={},this._window=1e3,this._mtu=500,this._interval=0,this._count=0,this._queue=[],this._setupDC()}function p(e,t){if(!(this instanceof p))return new p(e,t);u.call(this),e&&e.constructor==Object?(t=e,e=undefined):e&&(e=e.toString()),t=h.extend({debug:0,host:h.CLOUD_HOST,port:h.CLOUD_PORT,key:"peerjs",path:"/",token:h.randomToken(),config:h.defaultConfig},t),this.options=t,t.host==="/"&&(t.host=window.location.hostname),t.path[0]!=="/"&&(t.path="/"+t.path),t.path[t.path.length-1]!=="/"&&(t.path+="/"),t.secure===undefined&&t.host!==h.CLOUD_HOST&&(t.secure=h.isSecure()),t.logFunction&&h.setLogFunction(t.logFunction),h.setLogLevel(t.debug);if(!h.supports.audioVideo&&!h.supports.data){this._delayedAbort("browser-incompatible","The current browser does not support WebRTC");return}if(!h.validateId(e)){this._delayedAbort("invalid-id",'ID "'+e+'" is invalid');return}if(!h.validateKey(t.key)){this._delayedAbort("invalid-key",'API KEY "'+t.key+'" is invalid');return}if(t.secure&&t.host==="0.peerjs.com"){this._delayedAbort("ssl-unavailable","The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS.");return}this.destroyed=!1,this.disconnected=!1,this.open=!1,this.connections={},this._lostMessages={},this._initializeServerConnection(),e?this._initialize(e):this._retrieveId()}function d(e,t,n){if(!(this instanceof d))return new d(e,t,n);u.call(this),this.options=h.extend({serialization:"binary",reliable:!1},n),this.open=!1,this.type="data",this.peer=e,this.provider=t,this.id=this.options.connectionId||d._idPrefix+h.randomToken(),this.label=this.options.label||this.id,this.metadata=this.options.metadata,this.serialization=this.options.serialization,this.reliable=this.options.reliable,this._buffer=[],this._buffering=!1,this.bufferSize=0,this._chunkedData={},this.options._payload&&(this._peerBrowser=this.options._payload.browser),m.startConnection(this,this.options._payload||{originator:!0})}function v(e,t,n){if(!(this instanceof v))return new v(e,t,n);u.call(this),this.options=h.extend({},n),this.open=!1,this.type="media",this.peer=e,this.provider=t,this.metadata=this.options.metadata,this.localStream=this.options._stream,this.id=this.options.connectionId||v._idPrefix+h.randomToken(),this.localStream&&m.startConnection(this,{_stream:this.localStream,originator:!0})}function g(e,t,n,r,i){if(!(this instanceof g))return new g(e,t,n,r,i);u.call(this),this.disconnected=!1,this._queue=[];var s=e?"https://":"http://",o=e?"wss://":"ws://";this._httpUrl=s+t+":"+n+r+i,this._wsUrl=o+t+":"+n+r+"peerjs?key="+i}var t={};t.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),t.useArrayBufferView=!t.useBlobBuilder&&function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),e.binaryFeatures=t,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,n.prototype.append=function(e){typeof e=="number"?this._pieces.push(e):(this.flush(),this._parts.push(e))},n.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);t.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},n.prototype.getBuffer=function(){this.flush();if(t.useBlobBuilder){var e=new BlobBuilder;for(var n=0,r=this._parts.length;n>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},r.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},r.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},i.prototype.getBuffer=function(){return this.bufferBuilder.getBuffer()},i.prototype.pack=function(e){var n=typeof e;if(n=="string")this.pack_string(e);else if(n=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(n=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(n=="undefined")this.bufferBuilder.append(192);else{if(n!="object")throw new Error('Type "'+n+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var r=e.constructor;if(r==Array)this.pack_array(e);else if(r==Blob||r==File)this.pack_bin(e);else if(r==ArrayBuffer)t.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)t.useArrayBufferView?this.pack_bin(new Uint8Array(e.buffer)):this.pack_bin(e.buffer);else if(r==Object)this.pack_object(e);else if(r==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+r.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}this.bufferBuilder.flush()},i.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_string=function(e){var t=o(e);if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},i.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n>8),this.bufferBuilder.append(e&255)},i.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},i.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},i.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},i.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},i.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)};var a=Array.isArray;u.prototype.addListener=function(e,t,n,r){if("function"!=typeof t)throw new Error("addListener only takes instances of Function");return this.emit("newListener",e,typeof t.listener=="function"?t.listener:t),this._events[e]?a(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t,this},u.prototype.on=u.prototype.addListener,u.prototype.once=function(e,t,n){function i(){r.removeListener(e,i),t.apply(this,arguments)}if("function"!=typeof t)throw new Error(".once only takes instances of Function");var r=this;return i.listener=t,r.on(e,i),this},u.prototype.removeListener=function(e,t,n){if("function"!=typeof t)throw new Error("removeListener only takes instances of Function");if(!this._events[e])return this;var r=this._events[e];if(a(r)){var i=-1;for(var s=0,o=r.length;s=i.chunks.length?(h.log("Time: ",new Date-i.timer),delete this._outgoing[t]):this._processAcks()}break;case"chunk":i=n;if(!i){var u=this._received[t];if(u===!0)break;i={ack:["ack",t,0],chunks:[]},this._incoming[t]=i}var a=e[2],f=e[3];i.chunks[a]=new Uint8Array(f),a===i.ack[2]&&this._calculateNextAck(t),this._ack(t);break;default:this._handleSend(e)}},f.prototype._chunk=function(e){var t=[],n=e.size,r=0;while(r=n.length&&r.push(["end",e,n.length]),r._multiple=!0,this._handleSend(r)},f.prototype._complete=function(e){h.log("Completed called for",e);var t=this,n=this._incoming[e].chunks,r=new Blob(n);h.blobToArrayBuffer(r,function(e){t.onmessage(h.unpack(e))}),delete this._incoming[e]},f.higherBandwidthSDP=function(e){var t=navigator.appVersion.match(/Chrome\/(.*?) /);if(t){t=parseInt(t[1].split(".").shift());if(t<31){var n=e.split("b=AS:30"),r="b=AS:102400";if(n.length>1)return n[0]+r+n[1]}}return e},f.prototype.onmessage=function(e){},e.Reliable=f,e.RTCSessionDescription=window.RTCSessionDescription||window.mozRTCSessionDescription,e.RTCPeerConnection=window.RTCPeerConnection||window.mozRTCPeerConnection||window.webkitRTCPeerConnection,e.RTCIceCandidate=window.RTCIceCandidate||window.mozRTCIceCandidate;var l={iceServers:[{url:"stun:stun.l.google.com:19302"}]},c=1,h={noop:function(){},CLOUD_HOST:"0.peerjs.com",CLOUD_PORT:9e3,chunkedBrowsers:{Chrome:1},chunkedMTU:16300,logLevel:0,setLogLevel:function(e){var t=parseInt(e,10);isNaN(parseInt(e,10))?h.logLevel=e?3:0:h.logLevel=t,h.log=h.warn=h.error=h.noop,h.logLevel>0&&(h.error=h._printWith("ERROR")),h.logLevel>1&&(h.warn=h._printWith("WARNING")),h.logLevel>2&&(h.log=h._print)},setLogFunction:function(e){e.constructor!==Function?h.warn("The log function you passed in is not a function. Defaulting to regular logs."):h._print=e},_printWith:function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e),h._print.apply(h,t)}},_print:function(){var e=!1,t=Array.prototype.slice.call(arguments);t.unshift("PeerJS: ");for(var n=0,r=t.length;nh.chunkedMTU){this._sendChunks(r);return}h.supports.sctp?h.supports.binaryBlob?this._bufferedSend(r):h.blobToArrayBuffer(r,function(e){n._bufferedSend(e)}):h.blobToBinaryString(r,function(e){n._bufferedSend(e)})}else this._bufferedSend(e)},d.prototype._bufferedSend=function(e){if(this._buffering||!this._trySend(e))this._buffer.push(e),this.bufferSize=this._buffer.length},d.prototype._trySend=function(e){try{this._dc.send(e)}catch(t){this._buffering=!0;var n=this;return setTimeout(function(){n._buffering=!1,n._tryBuffer()},100),!1}return!0},d.prototype._tryBuffer=function(){if(this._buffer.length===0)return;var e=this._buffer[0];this._trySend(e)&&(this._buffer.shift(),this.bufferSize=this._buffer.length,this._tryBuffer())},d.prototype._sendChunks=function(e){var t=h.chunk(e);for(var n=0,r=t.length;n2&&this.status===200&&this.responseText?t._handleStream(this):this.status!==200&&(clearTimeout(t._timeout),t.emit("disconnected"))},this._http.send(null),this._setHTTPTimeout()}catch(n){h.log("XMLHttpRequest not available; defaulting to WebSockets")}},g.prototype._handleStream=function(e){var t=e.responseText.split("\n");if(e._buffer)while(e._buffer.length>0){var n=e._buffer.shift(),r=t[n];try{r=JSON.parse(r)}catch(i){e._buffer.shift(n);break}this.emit("message",r)}var s=t[e._index];if(s){e._index+=1;if(e._index===t.length)e._buffer||(e._buffer=[]),e._buffer.push(e._index-1);else{try{s=JSON.parse(s)}catch(i){h.log("Invalid server message",s);return}this.emit("message",s)}}},g.prototype._setHTTPTimeout=function(){var e=this;this._timeout=setTimeout(function(){var t=e._http;e._wsOpen()?t.abort():(e._startXhrStream(t._streamIndex+1),e._http.old=t)},25e3)},g.prototype._wsOpen=function(){return this._socket&&this._socket.readyState==1},g.prototype._sendQueuedMessages=function(){for(var e=0,t=this._queue.length;e