12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514 |
- /*! peerjs.js build:0.0.1, development. Copyright(c) 2013 Michelle Bu <michelle@michellebu.com> */
- (function(exports){
- var binaryFeatures = {};
- binaryFeatures.useBlobBuilder = (function(){
- try {
- new Blob([]);
- return false;
- } catch (e) {
- return true;
- }
- })();
- binaryFeatures.useArrayBufferView = !binaryFeatures.useBlobBuilder && (function(){
- try {
- return (new Blob([new Uint8Array([])])).size === 0;
- } catch (e) {
- return true;
- }
- })();
- binaryFeatures.supportsBinaryWebsockets = (function(){
- try {
- var wstest = new WebSocket('ws://null');
- wstest.onerror = function(){};
- if (typeof(wstest.binaryType) !== "undefined") {
- return true;
- } else {
- return false;
- }
- wstest.close();
- wstest = null;
- } catch (e) {
- return false;
- }
- })();
- exports.binaryFeatures = binaryFeatures;
- exports.BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder;
- function BufferBuilder(){
- this._pieces = [];
- this._parts = [];
- }
- BufferBuilder.prototype.append = function(data) {
- if(typeof data === 'number') {
- this._pieces.push(data);
- } else {
- this._flush();
- this._parts.push(data);
- }
- };
- BufferBuilder.prototype._flush = function() {
- if (this._pieces.length > 0) {
- var buf = new Uint8Array(this._pieces);
- if(!binaryFeatures.useArrayBufferView) {
- buf = buf.buffer;
- }
- this._parts.push(buf);
- this._pieces = [];
- }
- };
- BufferBuilder.prototype.getBuffer = function() {
- this._flush();
- if(binaryFeatures.useBlobBuilder) {
- var builder = new BlobBuilder();
- for(var i = 0, ii = this._parts.length; i < ii; i++) {
- builder.append(this._parts[i]);
- }
- return builder.getBlob();
- } else {
- return new Blob(this._parts);
- }
- };
- exports.BinaryPack = {
- unpack: function(data){
- var unpacker = new Unpacker(data);
- return unpacker.unpack();
- },
- pack: function(data){
- var packer = new Packer();
- var buffer = packer.pack(data);
- return buffer;
- }
- };
- function Unpacker (data){
- // Data is ArrayBuffer
- this.index = 0;
- this.dataBuffer = data;
- this.dataView = new Uint8Array(this.dataBuffer);
- this.length = this.dataBuffer.byteLength;
- }
- Unpacker.prototype.unpack = function(){
- var type = this.unpack_uint8();
- if (type < 0x80){
- var positive_fixnum = type;
- return positive_fixnum;
- } else if ((type ^ 0xe0) < 0x20){
- var negative_fixnum = (type ^ 0xe0) - 0x20;
- return negative_fixnum;
- }
- var size;
- if ((size = type ^ 0xa0) <= 0x0f){
- return this.unpack_raw(size);
- } else if ((size = type ^ 0xb0) <= 0x0f){
- return this.unpack_string(size);
- } else if ((size = type ^ 0x90) <= 0x0f){
- return this.unpack_array(size);
- } else if ((size = type ^ 0x80) <= 0x0f){
- return this.unpack_map(size);
- }
- switch(type){
- case 0xc0:
- return null;
- case 0xc1:
- return undefined;
- case 0xc2:
- return false;
- case 0xc3:
- return true;
- case 0xca:
- return this.unpack_float();
- case 0xcb:
- return this.unpack_double();
- case 0xcc:
- return this.unpack_uint8();
- case 0xcd:
- return this.unpack_uint16();
- case 0xce:
- return this.unpack_uint32();
- case 0xcf:
- return this.unpack_uint64();
- case 0xd0:
- return this.unpack_int8();
- case 0xd1:
- return this.unpack_int16();
- case 0xd2:
- return this.unpack_int32();
- case 0xd3:
- return this.unpack_int64();
- case 0xd4:
- return undefined;
- case 0xd5:
- return undefined;
- case 0xd6:
- return undefined;
- case 0xd7:
- return undefined;
- case 0xd8:
- size = this.unpack_uint16();
- return this.unpack_string(size);
- case 0xd9:
- size = this.unpack_uint32();
- return this.unpack_string(size);
- case 0xda:
- size = this.unpack_uint16();
- return this.unpack_raw(size);
- case 0xdb:
- size = this.unpack_uint32();
- return this.unpack_raw(size);
- case 0xdc:
- size = this.unpack_uint16();
- return this.unpack_array(size);
- case 0xdd:
- size = this.unpack_uint32();
- return this.unpack_array(size);
- case 0xde:
- size = this.unpack_uint16();
- return this.unpack_map(size);
- case 0xdf:
- size = this.unpack_uint32();
- return this.unpack_map(size);
- }
- }
- Unpacker.prototype.unpack_uint8 = function(){
- var byte = this.dataView[this.index] & 0xff;
- this.index++;
- return byte;
- };
- Unpacker.prototype.unpack_uint16 = function(){
- var bytes = this.read(2);
- var uint16 =
- ((bytes[0] & 0xff) * 256) + (bytes[1] & 0xff);
- this.index += 2;
- return uint16;
- }
- Unpacker.prototype.unpack_uint32 = function(){
- var bytes = this.read(4);
- var uint32 =
- ((bytes[0] * 256 +
- bytes[1]) * 256 +
- bytes[2]) * 256 +
- bytes[3];
- this.index += 4;
- return uint32;
- }
- Unpacker.prototype.unpack_uint64 = function(){
- var bytes = this.read(8);
- var uint64 =
- ((((((bytes[0] * 256 +
- bytes[1]) * 256 +
- bytes[2]) * 256 +
- bytes[3]) * 256 +
- bytes[4]) * 256 +
- bytes[5]) * 256 +
- bytes[6]) * 256 +
- bytes[7];
- this.index += 8;
- return uint64;
- }
- Unpacker.prototype.unpack_int8 = function(){
- var uint8 = this.unpack_uint8();
- return (uint8 < 0x80 ) ? uint8 : uint8 - (1 << 8);
- };
- Unpacker.prototype.unpack_int16 = function(){
- var uint16 = this.unpack_uint16();
- return (uint16 < 0x8000 ) ? uint16 : uint16 - (1 << 16);
- }
- Unpacker.prototype.unpack_int32 = function(){
- var uint32 = this.unpack_uint32();
- return (uint32 < Math.pow(2, 31) ) ? uint32 :
- uint32 - Math.pow(2, 32);
- }
- Unpacker.prototype.unpack_int64 = function(){
- var uint64 = this.unpack_uint64();
- return (uint64 < Math.pow(2, 63) ) ? uint64 :
- uint64 - Math.pow(2, 64);
- }
- Unpacker.prototype.unpack_raw = function(size){
- if ( this.length < this.index + size){
- throw new Error('BinaryPackFailure: index is out of range'
- + ' ' + this.index + ' ' + size + ' ' + this.length);
- }
- var buf = this.dataBuffer.slice(this.index, this.index + size);
- this.index += size;
-
- //buf = util.bufferToString(buf);
-
- return buf;
- }
- Unpacker.prototype.unpack_string = function(size){
- var bytes = this.read(size);
- var i = 0, str = '', c, code;
- while(i < size){
- c = bytes[i];
- if ( c < 128){
- str += String.fromCharCode(c);
- i++;
- } else if ((c ^ 0xc0) < 32){
- code = ((c ^ 0xc0) << 6) | (bytes[i+1] & 63);
- str += String.fromCharCode(code);
- i += 2;
- } else {
- code = ((c & 15) << 12) | ((bytes[i+1] & 63) << 6) |
- (bytes[i+2] & 63);
- str += String.fromCharCode(code);
- i += 3;
- }
- }
- this.index += size;
- return str;
- }
- Unpacker.prototype.unpack_array = function(size){
- var objects = new Array(size);
- for(var i = 0; i < size ; i++){
- objects[i] = this.unpack();
- }
- return objects;
- }
- Unpacker.prototype.unpack_map = function(size){
- var map = {};
- for(var i = 0; i < size ; i++){
- var key = this.unpack();
- var value = this.unpack();
- map[key] = value;
- }
- return map;
- }
- Unpacker.prototype.unpack_float = function(){
- var uint32 = this.unpack_uint32();
- var sign = uint32 >> 31;
- var exp = ((uint32 >> 23) & 0xff) - 127;
- var fraction = ( uint32 & 0x7fffff ) | 0x800000;
- return (sign == 0 ? 1 : -1) *
- fraction * Math.pow(2, exp - 23);
- }
- Unpacker.prototype.unpack_double = function(){
- var h32 = this.unpack_uint32();
- var l32 = this.unpack_uint32();
- var sign = h32 >> 31;
- var exp = ((h32 >> 20) & 0x7ff) - 1023;
- var hfrac = ( h32 & 0xfffff ) | 0x100000;
- var frac = hfrac * Math.pow(2, exp - 20) +
- l32 * Math.pow(2, exp - 52);
- return (sign == 0 ? 1 : -1) * frac;
- }
- Unpacker.prototype.read = function(length){
- var j = this.index;
- if (j + length <= this.length) {
- return this.dataView.subarray(j, j + length);
- } else {
- throw new Error('BinaryPackFailure: read index out of range');
- }
- }
-
- function Packer (){
- this.bufferBuilder = new BufferBuilder();
- }
- Packer.prototype.pack = function(value){
- var type = typeof(value);
- if (type == 'string'){
- this.pack_string(value);
- } else if (type == 'number'){
- if (Math.floor(value) === value){
- this.pack_integer(value);
- } else{
- this.pack_double(value);
- }
- } else if (type == 'boolean'){
- if (value === true){
- this.bufferBuilder.append(0xc3);
- } else if (value === false){
- this.bufferBuilder.append(0xc2);
- }
- } else if (type == 'undefined'){
- this.bufferBuilder.append(0xc0);
- } else if (type == 'object'){
- if (value === null){
- this.bufferBuilder.append(0xc0);
- } else {
- var constructor = value.constructor;
- if (constructor == Array){
- this.pack_array(value);
- } else if (constructor == Blob || constructor == File) {
- this.pack_bin(value);
- } else if (constructor == ArrayBuffer) {
- if(binaryFeatures.useArrayBufferView) {
- this.pack_bin(new Uint8Array(value));
- } else {
- this.pack_bin(value);
- }
- } else if ('BYTES_PER_ELEMENT' in value){
- if(binaryFeatures.useArrayBufferView) {
- this.pack_bin(value);
- } else {
- this.pack_bin(value.buffer);
- }
- } else if (constructor == Object){
- this.pack_object(value);
- } else if (constructor == Date){
- this.pack_string(value.toString());
- } else if (typeof value.toBinaryPack == 'function'){
- this.bufferBuilder.append(value.toBinaryPack());
- } else {
- throw new Error('Type "' + constructor.toString() + '" not yet supported');
- }
- }
- } else {
- throw new Error('Type "' + type + '" not yet supported');
- }
- return this.bufferBuilder.getBuffer();
- }
- Packer.prototype.pack_bin = function(blob){
- var length = blob.length || blob.byteLength || blob.size;
- if (length <= 0x0f){
- this.pack_uint8(0xa0 + length);
- } else if (length <= 0xffff){
- this.bufferBuilder.append(0xda) ;
- this.pack_uint16(length);
- } else if (length <= 0xffffffff){
- this.bufferBuilder.append(0xdb);
- this.pack_uint32(length);
- } else{
- throw new Error('Invalid length');
- return;
- }
- this.bufferBuilder.append(blob);
- }
- Packer.prototype.pack_string = function(str){
- var length = str.length;
- if (length <= 0x0f){
- this.pack_uint8(0xb0 + length);
- } else if (length <= 0xffff){
- this.bufferBuilder.append(0xd8) ;
- this.pack_uint16(length);
- } else if (length <= 0xffffffff){
- this.bufferBuilder.append(0xd9);
- this.pack_uint32(length);
- } else{
- throw new Error('Invalid length');
- return;
- }
- this.bufferBuilder.append(str);
- }
- Packer.prototype.pack_array = function(ary){
- var length = ary.length;
- if (length <= 0x0f){
- this.pack_uint8(0x90 + length);
- } else if (length <= 0xffff){
- this.bufferBuilder.append(0xdc)
- this.pack_uint16(length);
- } else if (length <= 0xffffffff){
- this.bufferBuilder.append(0xdd);
- this.pack_uint32(length);
- } else{
- throw new Error('Invalid length');
- }
- for(var i = 0; i < length ; i++){
- this.pack(ary[i]);
- }
- }
- Packer.prototype.pack_integer = function(num){
- if ( -0x20 <= num && num <= 0x7f){
- this.bufferBuilder.append(num & 0xff);
- } else if (0x00 <= num && num <= 0xff){
- this.bufferBuilder.append(0xcc);
- this.pack_uint8(num);
- } else if (-0x80 <= num && num <= 0x7f){
- this.bufferBuilder.append(0xd0);
- this.pack_int8(num);
- } else if ( 0x0000 <= num && num <= 0xffff){
- this.bufferBuilder.append(0xcd);
- this.pack_uint16(num);
- } else if (-0x8000 <= num && num <= 0x7fff){
- this.bufferBuilder.append(0xd1);
- this.pack_int16(num);
- } else if ( 0x00000000 <= num && num <= 0xffffffff){
- this.bufferBuilder.append(0xce);
- this.pack_uint32(num);
- } else if (-0x80000000 <= num && num <= 0x7fffffff){
- this.bufferBuilder.append(0xd2);
- this.pack_int32(num);
- } else if (-0x8000000000000000 <= num && num <= 0x7FFFFFFFFFFFFFFF){
- this.bufferBuilder.append(0xd3);
- this.pack_int64(num);
- } else if (0x0000000000000000 <= num && num <= 0xFFFFFFFFFFFFFFFF){
- this.bufferBuilder.append(0xcf);
- this.pack_uint64(num);
- } else{
- throw new Error('Invalid integer');
- }
- }
- Packer.prototype.pack_double = function(num){
- var sign = 0;
- if (num < 0){
- sign = 1;
- num = -num;
- }
- var exp = Math.floor(Math.log(num) / Math.LN2);
- var frac0 = num / Math.pow(2, exp) - 1;
- var frac1 = Math.floor(frac0 * Math.pow(2, 52));
- var b32 = Math.pow(2, 32);
- var h32 = (sign << 31) | ((exp+1023) << 20) |
- (frac1 / b32) & 0x0fffff;
- var l32 = frac1 % b32;
- this.bufferBuilder.append(0xcb);
- this.pack_int32(h32);
- this.pack_int32(l32);
- }
- Packer.prototype.pack_object = function(obj){
- var keys = Object.keys(obj);
- var length = keys.length;
- if (length <= 0x0f){
- this.pack_uint8(0x80 + length);
- } else if (length <= 0xffff){
- this.bufferBuilder.append(0xde);
- this.pack_uint16(length);
- } else if (length <= 0xffffffff){
- this.bufferBuilder.append(0xdf);
- this.pack_uint32(length);
- } else{
- throw new Error('Invalid length');
- }
- for(var prop in obj){
- if (obj.hasOwnProperty(prop)){
- this.pack(prop);
- this.pack(obj[prop]);
- }
- }
- }
- Packer.prototype.pack_uint8 = function(num){
- this.bufferBuilder.append(num);
- }
- Packer.prototype.pack_uint16 = function(num){
- this.bufferBuilder.append(num >> 8);
- this.bufferBuilder.append(num & 0xff);
- }
- Packer.prototype.pack_uint32 = function(num){
- var n = num & 0xffffffff;
- this.bufferBuilder.append((n & 0xff000000) >>> 24);
- this.bufferBuilder.append((n & 0x00ff0000) >>> 16);
- this.bufferBuilder.append((n & 0x0000ff00) >>> 8);
- this.bufferBuilder.append((n & 0x000000ff));
- }
- Packer.prototype.pack_uint64 = function(num){
- var high = num / Math.pow(2, 32);
- var low = num % Math.pow(2, 32);
- this.bufferBuilder.append((high & 0xff000000) >>> 24);
- this.bufferBuilder.append((high & 0x00ff0000) >>> 16);
- this.bufferBuilder.append((high & 0x0000ff00) >>> 8);
- this.bufferBuilder.append((high & 0x000000ff));
- this.bufferBuilder.append((low & 0xff000000) >>> 24);
- this.bufferBuilder.append((low & 0x00ff0000) >>> 16);
- this.bufferBuilder.append((low & 0x0000ff00) >>> 8);
- this.bufferBuilder.append((low & 0x000000ff));
- }
- Packer.prototype.pack_int8 = function(num){
- this.bufferBuilder.append(num & 0xff);
- }
- Packer.prototype.pack_int16 = function(num){
- this.bufferBuilder.append((num & 0xff00) >> 8);
- this.bufferBuilder.append(num & 0xff);
- }
- Packer.prototype.pack_int32 = function(num){
- this.bufferBuilder.append((num >>> 24) & 0xff);
- this.bufferBuilder.append((num & 0x00ff0000) >>> 16);
- this.bufferBuilder.append((num & 0x0000ff00) >>> 8);
- this.bufferBuilder.append((num & 0x000000ff));
- }
- Packer.prototype.pack_int64 = function(num){
- var high = Math.floor(num / Math.pow(2, 32));
- var low = num % Math.pow(2, 32);
- this.bufferBuilder.append((high & 0xff000000) >>> 24);
- this.bufferBuilder.append((high & 0x00ff0000) >>> 16);
- this.bufferBuilder.append((high & 0x0000ff00) >>> 8);
- this.bufferBuilder.append((high & 0x000000ff));
- this.bufferBuilder.append((low & 0xff000000) >>> 24);
- this.bufferBuilder.append((low & 0x00ff0000) >>> 16);
- this.bufferBuilder.append((low & 0x0000ff00) >>> 8);
- this.bufferBuilder.append((low & 0x000000ff));
- }
- /**
- * Light EventEmitter. Ported from Node.js/events.js
- * Eric Zhang
- */
- /**
- * EventEmitter class
- * Creates an object with event registering and firing methods
- */
- function EventEmitter() {
- // Initialise required storage variables
- this._events = {};
- }
- var isArray = Array.isArray;
- EventEmitter.prototype.addListener = function(type, listener, scope, once) {
- if ('function' !== typeof listener) {
- throw new Error('addListener only takes instances of Function');
- }
-
- // To avoid recursion in the case that type == "newListeners"! Before
- // adding it to the listeners, first emit "newListeners".
- this.emit('newListener', type, typeof listener.listener === 'function' ?
- listener.listener : listener);
-
- if (!this._events[type]) {
- // Optimize the case of one listener. Don't need the extra array object.
- this._events[type] = listener;
- } else if (isArray(this._events[type])) {
- // If we've already got an array, just append.
- this._events[type].push(listener);
- } else {
- // Adding the second element, need to change to array.
- this._events[type] = [this._events[type], listener];
- }
-
- };
- EventEmitter.prototype.on = EventEmitter.prototype.addListener;
- EventEmitter.prototype.once = function(type, listener, scope) {
- if ('function' !== typeof listener) {
- throw new Error('.once only takes instances of Function');
- }
- var self = this;
- function g() {
- self.removeListener(type, g);
- listener.apply(this, arguments);
- };
- g.listener = listener;
- self.on(type, g);
- return this;
- };
- EventEmitter.prototype.removeListener = function(type, listener, scope) {
- if ('function' !== typeof listener) {
- throw new Error('removeListener only takes instances of Function');
- }
- // does not use listeners(), so no side effect of creating _events[type]
- if (!this._events[type]) return this;
- var list = this._events[type];
- if (isArray(list)) {
- var position = -1;
- for (var i = 0, length = list.length; i < length; i++) {
- if (list[i] === listener ||
- (list[i].listener && list[i].listener === listener))
- {
- position = i;
- break;
- }
- }
- if (position < 0) return this;
- list.splice(position, 1);
- if (list.length == 0)
- delete this._events[type];
- } else if (list === listener ||
- (list.listener && list.listener === listener))
- {
- delete this._events[type];
- }
- return this;
- };
- EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
- EventEmitter.prototype.removeAllListeners = function(type) {
- if (arguments.length === 0) {
- this._events = {};
- return this;
- }
- // does not use listeners(), so no side effect of creating _events[type]
- if (type && this._events && this._events[type]) this._events[type] = null;
- return this;
- };
- EventEmitter.prototype.listeners = function(type) {
- if (!this._events[type]) this._events[type] = [];
- if (!isArray(this._events[type])) {
- this._events[type] = [this._events[type]];
- }
- return this._events[type];
- };
- EventEmitter.prototype.emit = function(type) {
- var type = arguments[0];
- var handler = this._events[type];
- if (!handler) return false;
- if (typeof handler == 'function') {
- switch (arguments.length) {
- // fast cases
- case 1:
- handler.call(this);
- break;
- case 2:
- handler.call(this, arguments[1]);
- break;
- case 3:
- handler.call(this, arguments[1], arguments[2]);
- break;
- // slower
- default:
- var l = arguments.length;
- var args = new Array(l - 1);
- for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
- handler.apply(this, args);
- }
- return true;
- } else if (isArray(handler)) {
- var l = arguments.length;
- var args = new Array(l - 1);
- for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
- var listeners = handler.slice();
- for (var i = 0, l = listeners.length; i < l; i++) {
- listeners[i].apply(this, args);
- }
- return true;
- } else {
- return false;
- }
- };
- var util = {
-
- debug: false,
- browserisms: '',
-
- inherits: function(ctor, superCtor) {
- ctor.super_ = superCtor;
- ctor.prototype = Object.create(superCtor.prototype, {
- constructor: {
- value: ctor,
- enumerable: false,
- writable: true,
- configurable: true
- }
- });
- },
- extend: function(dest, source) {
- for(var key in source) {
- if(source.hasOwnProperty(key)) {
- dest[key] = source[key];
- }
- }
- return dest;
- },
- pack: BinaryPack.pack,
- unpack: BinaryPack.unpack,
- randomPort: function() {
- return Math.round(Math.random() * 60535) + 5000;
- },
-
- log: function () {
- if (util.debug) {
- var copy = [];
- for (var i = 0; i < arguments.length; i++) {
- copy[i] = arguments[i];
- }
- copy.unshift('PeerJS: ');
- console.log.apply(console, copy);
- }
- },
- setZeroTimeout: (function(global) {
- var timeouts = [];
- var messageName = 'zero-timeout-message';
- // Like setTimeout, but only takes a function argument. There's
- // no time argument (always zero) and no arguments (you have to
- // use a closure).
- function setZeroTimeoutPostMessage(fn) {
- timeouts.push(fn);
- global.postMessage(messageName, '*');
- }
- function handleMessage(event) {
- if (event.source == global && event.data == messageName) {
- if (event.stopPropagation) {
- event.stopPropagation();
- }
- if (timeouts.length) {
- timeouts.shift()();
- }
- }
- }
- if (global.addEventListener) {
- global.addEventListener('message', handleMessage, true);
- } else if (global.attachEvent) {
- global.attachEvent('onmessage', handleMessage);
- }
- return setZeroTimeoutPostMessage;
- }(this)),
-
- blobToArrayBuffer: function(blob, cb){
- var fr = new FileReader();
- fr.onload = function(evt) {
- cb(evt.target.result);
- };
- fr.readAsArrayBuffer(blob);
- },
- blobToBinaryString: function(blob, cb){
- var fr = new FileReader();
- fr.onload = function(evt) {
- cb(evt.target.result);
- };
- fr.readAsBinaryString(blob);
- },
- binaryStringToArrayBuffer: function(binary) {
- var byteArray = new Uint8Array(binary.length);
- for (var i = 0; i < binary.length; i++) {
- byteArray[i] = binary.charCodeAt(i) & 0xff;
- }
- return byteArray.buffer;
- }
- };
- var RTCPeerConnection = null;
- var getUserMedia = null;
- var attachMediaStream = null;
- if (navigator.mozGetUserMedia) {
- util.browserisms = 'Firefox'
- RTCPeerConnection = mozRTCPeerConnection;
- getUserMedia = navigator.mozGetUserMedia.bind(navigator);
- } else if (navigator.webkitGetUserMedia) {
- util.browserisms = 'Webkit'
- RTCPeerConnection = webkitRTCPeerConnection;
- getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
- }
- exports.RTCPeerConnection = RTCPeerConnection;
- exports.getUserMedia = getUserMedia;
- function Peer(options) {
- if (!(this instanceof Peer)) return new Peer(options);
- EventEmitter.call(this);
- options = util.extend({
- debug: false,
- host: 'localhost',
- protocol: 'http',
- config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
- port: 80
- }, options);
- this.options = options;
- util.debug = options.debug;
- this._server = options.host + ':' + options.port;
- this._httpUrl = options.protocol + '://' + this._server;
- this._config = options.config;
- // Ensure alphanumeric_-
- if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id))
- throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
- this._id = options.id;
- // Not used unless using cloud server.
- this._apikey = options.apikey;
- // Check in with the server with ID or get an ID.
- this._checkIn();
- // Connections for this peer.
- this.connections = {};
- // Queued connections to make.
- this._queued = [];
- // Make sure connections are cleaned up.
- window.onbeforeunload = this._cleanup;
- };
- util.inherits(Peer, EventEmitter);
- /** Check in with ID or get one from server. */
- Peer.prototype._checkIn = function() {
- // If no ID provided, get a unique ID from server.
- var self = this;
- if (!this._id) {
- try {
- var http = new XMLHttpRequest();
- // If there's no ID we need to wait for one before trying to init socket.
- http.open('get', this._httpUrl + '/id', true);
- http.onreadystatechange = function() {
- if (!self._id && http.readyState > 2 && !!http.responseText) {
- try {
- var response = JSON.parse(http.responseText.split('\n').shift());
- if (!!response.id) {
- self._id = response.id;
- self._socketInit();
- self.emit('ready', self._id);
- self._processQueue();
- }
- } catch (e) {
- self._socketInit();
- }
- }
- self._handleStream(http, true);
- };
- http.send(null);
- } catch(e) {
- util.log('XMLHttpRequest not available; defaulting to WebSockets');
- this._socketInit();
- }
- } else {
- this._startXhrStream();
- this._socketInit();
- }
- // TODO: may need to setInterval in case handleStream is not being called
- // enough.
- };
- Peer.prototype._startXhrStream = function() {
- try {
- var http = new XMLHttpRequest();
- var self = this;
- http.open('post', this._httpUrl + '/id', true);
- http.setRequestHeader('Content-Type', 'application/json');
- http.onreadystatechange = function() {
- self._handleStream(http);
- };
- http.send(JSON.stringify({ id: this._id }));
- // TODO: may need to setInterval in case handleStream is not being called
- // enough.
- } catch(e) {
- util.log('XMLHttpRequest not available; defaulting to WebSockets');
- }
- };
- /** Handles onreadystatechange response as a stream. */
- Peer.prototype._handleStream = function(http, pad) {
- // 3 and 4 are loading/done state. All others are not relevant.
- if (http.readyState < 3) {
- return;
- } else if (http.readyState == 3 && http.status != 200) {
- return;
- } else if (http.readyState == 4 && http.status != 200) {
- // Clear setInterval here if using it.
- }
- if (this._index === undefined)
- this._index = pad ? 2 : 1;
- if (http.responseText === null)
- return;
- // TODO: handle
- var message = http.responseText.split('\n')[this._index];
- if (!!message) {
- this._index += 1;
- this._handleServerMessage(message);
- }
- if (http.readyState == 4 && !this._socketOpen)
- this._startXhrStream();
- };
- /** Start up websocket communications. */
- Peer.prototype._socketInit = function() {
- if (!!this._socket)
- return;
- this._socket = new WebSocket('ws://' + this._server + '/ws?id=' + this._id);
- var self = this;
- this._socket.onmessage = function(event) {
- self._handleServerMessage(event.data);
- };
- // Take care of the queue of connections if necessary and make sure Peer knows
- // socket is open.
- this._socket.onopen = function() {
- util.log('Socket open');
- self._socketOpen = true;
- var ids = Object.keys(self.connections)
- for (var i = 0, ii = ids.length; i < ii; i += 1) {
- self.connections[ids[i]].setSocketOpen();
- }
- if (self._id)
- self._processQueue();
- };
- };
- Peer.prototype._handleServerMessage = function(message) {
- var message;
- try {
- message = JSON.parse(message);
- } catch(e) {
- util.log('message unrecognizable:', message);
- return;
- }
- var peer = message.src;
- var connection = this.connections[peer];
- switch (message.type) {
- case 'ID':
- if (!this._id) {
- // If we're just now getting an ID then we may have a queue.
- this._id = message.id;
- this.emit('ready', this._id);
- this._processQueue();
- }
- break;
- case 'ERROR':
- //throw new Error(message.msg);
- this.emit('error', message.msg);
- util.log(message.msg);
- break;
- case 'OFFER':
- var options = {
- metadata: message.metadata,
- sdp: message.sdp,
- socketOpen: this._socketOpen,
- config: this._config
- };
- var self = this;
- var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, function(err, connection) {
- if (!err) {
- self.emit('connection', connection, message.metadata);
- }
- }, options);
- this._attachConnectionListeners(connection);
- this.connections[peer] = connection;
- break;
- case 'PEER_READY':
- //if (connection) connection.processQueuedIce();
- break;
- case 'ANSWER':
- if (connection) connection.handleSDP(message);
- break;
- case 'CANDIDATE':
- if (connection) connection.handleCandidate(message);
- break;
- case 'LEAVE':
- if (connection) connection.handleLeave();
- break;
- case 'PORT':
- if (util.browserisms === 'Firefox') {
- connection.handlePort(message);
- break;
- }
- case 'DEFAULT':
- util.log('PEER: unrecognized message ', message.type);
- break;
- }
- };
- /** Process queued calls to connect. */
- Peer.prototype._processQueue = function() {
- while (this._queued.length > 0) {
- var cdata = this._queued.pop();
- this.connect.apply(this, cdata);
- }
- };
- Peer.prototype._cleanup = function() {
- this._socket.send(JSON.stringify({ type: 'LEAVE', src: this._id }));
- for (var peer in this.connections) {
- if (this.connections.hasOwnProperty(peer)) {
- this.connections[peer].close();
- }
- }
- };
- /** Listeners for DataConnection events. */
- Peer.prototype._attachConnectionListeners = function(connection) {
- var self = this;
- connection.on('close', function(peer) {
- if (self.connections[peer]) delete self.connections[peer];
- });
- };
- /** Exposed connect function for users. Will try to connect later if user
- * is waiting for an ID. */
- Peer.prototype.connect = function(peer, metadata, cb) {
- if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
- if (!this._id) {
- this._queued.push(Array.prototype.slice.apply(arguments));
- return;
- }
- var options = {
- metadata: metadata,
- socketOpen: this._socketOpen,
- config: this._config
- };
- var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
- this._attachConnectionListeners(connection);
- this.connections[peer] = connection;
- };
- Peer.prototype.destroy = function() {
- this._cleanup();
- };
- exports.Peer = Peer;
- function DataConnection(id, peer, socket, httpUrl, cb, options) {
- if (!(this instanceof DataConnection)) return new DataConnection(options);
- EventEmitter.call(this);
- options = util.extend({
- config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
- socketOpen: false
- }, options);
- this.options = options;
-
- this._id = id;
- this._peer = peer;
- this._originator = (options.sdp === undefined);
- this._cb = cb;
- this._httpUrl = httpUrl;
- //this._peerReady = true;
- this.metadata = options.metadata;
- this._socketOpen = options.socketOpen;
- this._config = options.config;
- // Set up socket handlers.
- this._socket = socket;
- // Firefoxism: connectDataConnection ports.
- if (util.browserisms === 'Firefox') {
- this._firefoxPortSetup();
- }
- //
-
- // Set up PeerConnection.
- this._startPeerConnection();
-
- // Listen for ICE candidates
- //this._queuedIce = [];
- this._setupIce();
-
- // Listen for negotiation needed
- // ** Chrome only.
- if (util.browserisms === 'Webkit') {
- this._setupOffer();
- }
-
- // Listen or create a data channel
- this._setupDataChannel();
-
- var self = this;
- if (options.sdp) {
- this.handleSDP({ type: 'OFFER', sdp: options.sdp });
- if (util.browserisms !== 'Firefox') {
- this._makeAnswer();
- }
- }
-
- if (util.browserisms === 'Firefox') {
- this._firefoxAdditional();
- }
- };
- util.inherits(DataConnection, EventEmitter);
- DataConnection.prototype._setupOffer = function() {
- var self = this;
- util.log('Listening for `negotiationneeded`');
- this._pc.onnegotiationneeded = function() {
- util.log('`negotiationneeded` triggered');
- self._makeOffer();
- };
- }
- DataConnection.prototype._setupDataChannel = function() {
- var self = this;
- if (this._originator) {
- util.log('Creating data channel');
- this._dc = this._pc.createDataChannel(this._peer, { reliable: false });
- this._configureDataChannel();
- } else {
- util.log('Listening for data channel');
- this._pc.ondatachannel = function(evt) {
- util.log('Received data channel');
- self._dc = evt.channel;
- self._configureDataChannel();
- };
- }
- };
- /** Starts a PeerConnection and sets up handlers. */
- DataConnection.prototype._startPeerConnection = function() {
- util.log('Creating RTCPeerConnection: ', this._config);
- this._pc = new RTCPeerConnection(this._config, { optional:[ { RtpDataChannels: true } ]});
- };
- /** Takes care of ice handlers. */
- DataConnection.prototype._setupIce = function() {
- util.log('Listening for ICE candidates');
- var self = this;
- this._pc.onicecandidate = function(evt) {
- if (evt.candidate) {
- util.log('Received ICE candidates');
- self._handleBroker('ice', JSON.stringify({
- type: 'CANDIDATE',
- candidate: evt.candidate,
- dst: self._peer,
- src: self._id
- }));
- /*var data = JSON.stringify({
- type: 'CANDIDATE',
- candidate: evt.candidate,
- dst: self._peer,
- src: self._id
- });
- if (self._peerReady) {
- self._handleBroker('ice', data);
- } else {
- self._queuedIce.push(data);
- }*/
- }
- };
- };
- DataConnection.prototype._handleBroker = function(type, data) {
- if (this._socketOpen) {
- this._socket.send(data);
- } else {
- var self = this;
- var http = new XMLHttpRequest();
- http.open('post', this._httpUrl + '/' + type, true);
- http.setRequestHeader('Content-Type', 'application/json');
- http.onload = function() {
- // If destination peer is not available...
- if (http.responseText != 'OK') {
- util.log('Destination peer not available. Connection closing...');
- self.close();
- }/* else {
- if (type == 'offer') {
- self.processQueuedIce();
- }
- }*/
- }
- http.send(data);
- }
- };
- /*
- DataConnection.prototype.processQueuedIce = function() {
- this._peerReady = true;
- console.log('processing ice');
- while (this._queuedIce.length > 0) {
- var data = this._queuedIce.shift();
- this._handleBroker('ice', data);
- }
- }
- */
- // Awaiting update in Firefox spec ***
- /** Sets up DataChannel handlers.
- DataConnection.prototype._setupDataChannel = function() {
- var self = this;
- if (this._originator) {
-
- if (util.browserisms === 'Webkit') {
- // TODO: figure out the right thing to do with this.
- this._pc.onstatechange = function() {
- util.log('State Change: ', self._pc.readyState);
- }
-
- } else {
- this._pc.onconnection = function() {
- util.log('ORIGINATOR: onconnection triggered');
- self._startDataChannel();
- };
- }
- } else {
-
- this._pc.onconnection = function() {
- util.log('SINK: onconnection triggered');
- };
- }
- this._pc.onclosedconnection = function() {
- // Remove socket handlers perhaps.
- self.emit('close', self._peer);
- };
- };
- */
- DataConnection.prototype._firefoxPortSetup = function() {
- if (!DataConnection.usedPorts) {
- DataConnection.usedPorts = [];
- }
- this.localPort = util.randomPort();
- while (DataConnection.usedPorts.indexOf(this.localPort) != -1) {
- this.localPort = util.randomPort();
- }
- this.remotePort = util.randomPort();
- while (this.remotePort === this.localPort ||
- DataConnection.usedPorts.indexOf(this.localPort) != -1) {
- this.remotePort = util.randomPort();
- }
- DataConnection.usedPorts.push(this.remotePort);
- DataConnection.usedPorts.push(this.localPort);
- }
- DataConnection.prototype._configureDataChannel = function() {
- var self = this;
-
- if (util.browserisms === 'Firefox') {
- this._dc.binaryType = 'blob';
- }
- this._dc.onopen = function() {
- util.log('Data channel connection success');
- self._cb(null, self);
- };
- this._dc.onmessage = function(e) {
- self._handleDataMessage(e);
- };
- };
- /** Decide whether to handle Firefoxisms. */
- DataConnection.prototype._firefoxAdditional = function() {
- var self = this;
- getUserMedia({ audio: true, fake: true }, function(s) {
- self._pc.addStream(s);
- if (self._originator) {
- self._makeOffer();
- } else {
- self._makeAnswer();
- }
- }, function(err) { util.log('Could not getUserMedia'); });
- }
- DataConnection.prototype._makeOffer = function() {
- var self = this;
- this._pc.createOffer(function(offer) {
- util.log('Created offer');
- self._pc.setLocalDescription(offer, function() {
- util.log('Set localDescription to offer');
- //self._peerReady = false;
- self._handleBroker('offer', JSON.stringify({
- type: 'OFFER',
- sdp: offer,
- dst: self._peer,
- src: self._id,
- metadata: self.metadata
- }));
- }, function(err) {
- self._cb('Failed to setLocalDescription');
- util.log('Failed to setLocalDescription, ', err);
- });
- });
- };
- /** Create an answer for PC. */
- DataConnection.prototype._makeAnswer = function() {
- var self = this;
- this._pc.createAnswer(function(answer) {
- util.log('Created answer');
- self._pc.setLocalDescription(answer, function() {
- util.log('Set localDescription to answer');
- self._handleBroker('answer', JSON.stringify({
- type: 'ANSWER',
- src: self._id,
- sdp: answer,
- dst: self._peer
- }));
- }, function(err) {
- self._cb('Failed to setLocalDescription');
- util.log('Failed to setLocalDescription, ', err)
- });
- }, function(err) {
- self._cb('Failed to create answer');
- util.log('Failed to create answer, ', err)
- });
- };
- DataConnection.prototype._cleanup = function() {
- if (!!this._pc && this._pc.readyState != 'closed') {
- this._pc.close();
- this._pc = null;
- }
- if (!!this._dc && this._dc.readyState != 'closed') {
- this._dc.close();
- this._dc = null;
- }
- this.emit('close', this._peer);
- };
- // Handles a DataChannel message.
- DataConnection.prototype._handleDataMessage = function(e) {
- var self = this;
- if (e.data.constructor === Blob) {
- util.blobToArrayBuffer(e.data, function(ab) {
- var data = BinaryPack.unpack(ab);
- self.emit('data', data);
- });
- } else if (e.data.constructor === ArrayBuffer) {
- var data = BinaryPack.unpack(e.data);
- self.emit('data', data);
- } else if (e.data.constructor === String) {
- var ab = util.binaryStringToArrayBuffer(e.data);
- var data = BinaryPack.unpack(ab);
- self.emit('data', data);
- }
- };
- /**
- * Exposed functionality for users.
- */
- /** Allows user to close connection. */
- DataConnection.prototype.close = function() {
- // TODO: how to emit close to the Peer? Also, how to handle Websocket closing
- // gracefully?
- this._cleanup();
- var self = this;
- this._handleBroker('leave', JSON.stringify({
- type: 'LEAVE',
- dst: self._peer,
- src: self._id,
- }));
- this.emit('close', this._peer);
- };
- /** Allows user to send data. */
- DataConnection.prototype.send = function(data) {
- var self = this;
- var blob = BinaryPack.pack(data);
- if (util.browserisms === 'Webkit') {
- util.blobToBinaryString(blob, function(str){
- self._dc.send(str);
- });
- } else {
- this._dc.send(blob);
- }
- };
- /**
- * Exposed functions for Peer.
- */
- DataConnection.prototype.setSocketOpen = function() {
- this._socketOpen = true;
- };
- DataConnection.prototype.handleSDP = function(message) {
- var sdp = message.sdp;
- if (util.browserisms != 'Firefox') {
- sdp = new RTCSessionDescription(sdp);
- }
- var self = this;
- this._pc.setRemoteDescription(sdp, function() {
- util.log('Set remoteDescription: ' + message.type);
- // Firefoxism
- if (message.type === 'ANSWER' && util.browserisms === 'Firefox') {
- self._pc.connectDataConnection(self.localPort, self.remotePort);
- self._handleBroker('port', JSON.stringify({
- type: 'PORT',
- dst: self._peer,
- src: self._id,
- remote: self.localPort,
- local: self.remotePort
- }));
- }
- }, function(err) {
- this._cb('Failed to setRemoteDescription');
- util.log('Failed to setRemoteDescription, ', err);
- });
- };
- DataConnection.prototype.handleCandidate = function(message) {
- var candidate = new RTCIceCandidate(message.candidate);
- this._pc.addIceCandidate(candidate);
- util.log('Added ice candidate');
- };
- DataConnection.prototype.handleLeave = function() {
- util.log('Peer ' + this._peer + ' disconnected');
- this._cleanup();
- };
- DataConnection.prototype.handlePort = function(message) {
- if (!DataConnection.usedPorts) {
- DataConnection.usedPorts = [];
- }
- DataConnection.usedPorts.push(message.local);
- DataConnection.usedPorts.push(message.remote);
- this._pc.connectDataConnection(message.local, message.remote);
- };
- })(this);
|