appeerjs
Version:
A basic encapsulation of Native WebRTC, this would offer an easy to use and understand API for beginners out there
1 lines • 7 kB
JavaScript
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){b.exports.RTCSessionDescription=window.RTCSessionDescription||window.mozRTCSessionDescription,b.exports.RTCPeerConnection=window.RTCPeerConnection||window.mozRTCPeerConnection||window.webkitRTCPeerConnection,b.exports.RTCIceCandidate=window.RTCIceCandidate||window.mozRTCIceCandidate},{}],2:[function(a,b,c){"use strict";function d(a,b){if(!(this instanceof d))return new d(a,b);var c={iceServers:[{url:"stun:stun.1.google.com:19302"}]};this.id=a||"",this.options=b||{},this.socket=null,this.options.config=b.config||c,this.connections={},this.rooms={},this.pendingCalls=[],this._initSocketIo(b.host+":"+b.port),g.setLogLevel(this.options.debug)}var e=a("./eventdispatcher"),f=a("./mediaconnection"),g=a("./logger");Object.assign(d.prototype,e.prototype),d.prototype._initSocketIo=function(a,b){var c=this;b=b||{},b.secure=!0,b.query="customId="+this.id,this.socket=io.connect(a,b),this.socket.on("message",function(a){c._handleMessages(a)}),this.socket.on("error",function(a){c.emit("error",{error:a})}),this.on("on-stream-added",function(a){var b=a.data.stream,d=c.pendingCalls.length;c.stream=b;for(var e=0;e<d;e++){var f=c.pendingCalls[e],g=this.connections[f];g&&!function(a){a.localStream=b,a._makeOffer(g.pc)}(g)}})},d.prototype.call=function(a,b,c){var c=c||{};this.stream=b,c.originator=!0,c._stream=b,this.rooms[a]?this.socket.send({type:"call",from:this.id,to:a}):this.connections[a]=new f(a,this,c)},d.prototype._handleMessages=function(a){var b=a.id,c=a.payload,d=a.type;switch(d){case"connect":this.emit("connection",{id:a.appeerId});break;case"answer":this.connections[b].handleSdp(c);break;case"candidate":this.connections[b].handleCandidate(c,b);break;case"offer":g.log("Setting remote description on offer from",b);var e=new f(b,this,{originator:!1,_payload:a.payload});this.connections[b]=e,this.emit("call",{call:e});break;case"incoming-call":var h=a.room;g.log("Incoming call from",b,"in room",h),this.rooms[h][b]=b.toString(),this.socket.send({type:"answer-call",to:h});break;case"incoming-member":g.log("Incoming member",b);var h=a.room,e=new f(b,this,{originator:!0,_stream:this.stream});this.connections[b]=e,this.rooms[h][b]=b.toString(),this.stream||(g.log("Stream not yet available, adding pending call from",b),this.pendingCalls.push(b));break;case"close":this.emit("close",{id:b});break;default:var i=a.error;g.error("Invalid message with type",d,"from",b),g.error("AppeerJS Error:",i)}},d.prototype.join=function(a,b){if(a){var c=this;this.room=a,this.socket.send({room:a,type:"join"},function(a){g.log("Successfully joined room:",a),c.rooms[a]={},"function"==typeof b&&b()})}},d.prototype.close=function(a){if(a){var b=this.connections[a],c=this.rooms[a];if(b)this.connections[a].close();else if(c)for(var d in c)c.hasOwnProperty(d)&&(this.connections[d].close(a),delete this.connections[d])}},b.exports=d},{"./eventdispatcher":3,"./logger":5,"./mediaconnection":6}],3:[function(a,b,c){"use strict";function d(){}Object.assign(d.prototype,{addEventListener:function(a,b){void 0===this._listeners&&(this._listeners={});var c=this._listeners;void 0===c[a]&&(c[a]=[]),c[a].indexOf(b)===-1&&c[a].push(b)},hasEventListener:function(a,b){if(void 0===this._listeners)return!1;var c=this._listeners;return void 0!==c[a]&&c[a].indexOf(b)!==-1},removeEventListener:function(a,b){if(void 0!==this._listeners){var c=this._listeners,d=c[a];if(void 0!==d){var e=d.indexOf(b);e!==-1&&d.splice(e,1)}}},dispatchEvent:function(a){if(void 0!==this._listeners){var b=this._listeners,c=b[a.type];if(void 0!==c){a.target=this;var d=0,e=[],f=c.length;for(d=0;d<f;d++)e[d]=c[d];for(d=0;d<f;d++)e[d].call(this,a)}}},on:function(a,b){this.addEventListener(a,b)},emit:function(a,b){var c={type:a,data:b};this.dispatchEvent(c)}}),b.exports=d},{}],4:[function(a,b,c){window.Appeer=a("./appeer"),window.RTCPeerConnection=a("./adapter").RTCPeerConnection,window.RTCSessionDescription=a("./adapter").RTCSessionDescription,window.RTCIceCandidate=a("./adapter").RTCIceCandidate},{"./adapter":1,"./appeer":2}],5:[function(a,b,c){"use strict";function d(){function a(a){d=a}function b(){d&&console.log.apply(console,arguments)}function c(){d&&console.error.apply(console,arguments)}var d=!1;return{setLogLevel:a,log:b,error:c}}b.exports=d()},{}],6:[function(a,b,c){"use strict";function d(a,b,c){return this instanceof d?(this.options=c||{},this.peerId=a,this.appeer=b,this.socket=b.socket,this.localStream=c._stream,this._startConnection(),this.pc.onaddstream=this._handleAddStream.bind(b),void(this.pc.onicecandidate=this._handleIceCandidate.bind(this))):new d(a,b,c)}var e=a("./logger");d.prototype._startConnection=function(){this.pc=new RTCPeerConnection(this.appeer.options.config,{optional:[{DtlsSrtpKeyAgreement:!0}]}),this.options.originator&&this.localStream?this._makeOffer(this.pc):this.options.originator===!1&&this.handleSdp(this.options._payload)},d.prototype._makeOffer=function(a){var b=this;e.log("Creating offer to",b.peerId),a.addStream(this.localStream),a.createOffer(function(c){e.log("Setting local description on offer"),a.setLocalDescription(c,function(){b.socket.send({type:"offer",payload:c,to:b.peerId})},function(a){e.error("Error setting local description on offer",a)})},function(a){e.error("Error on creating offer",a)})},d.prototype.handleSdp=function(a){e.log("Setting remote description on answer from",this.peerId),this.pc.setRemoteDescription(new RTCSessionDescription(a))},d.prototype._handleAddStream=function(a){e.log("Incoming remote media stream",a),this.emit("stream",{stream:a.stream})},d.prototype._handleIceCandidate=function(a){e.log("On Ice Candidate:",this.peerId),a.candidate&&this.socket.emit("message",{type:"candidate",payload:a.candidate,to:this.peerId})},d.prototype.answer=function(a){this.pc.addStream(a),this._handleOffer(this.peerId),this.appeer.emit("on-stream-added",{stream:a})},d.prototype._handleOffer=function(a){var b=this.pc,c=this.socket;e.log("Creating answer to",a),b.createAnswer(function(d){e.log("Setting local description on answer"),b.setLocalDescription(d,function(){c.emit("message",{type:"answer",payload:d,to:a})},function(a){e.error("Error setting local description on answer",a)})},function(a){e.error("Error on create answer from offer",a)})},d.prototype.handleCandidate=function(a){e.log("Adding ice candidate from",this.peerId),this.pc.addIceCandidate(new RTCIceCandidate(a))},d.prototype.close=function(a){var b=this.pc;b&&"closed"!==b.signalingState&&(b.close(),b=null,this.socket.send({type:"close",to:this.peerId,room:a}))},b.exports=d},{"./logger":5}]},{},[4]);