-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.peer.min.js
2 lines (2 loc) · 8.35 KB
/
jquery.peer.min.js
1
2
/*! jquery.peer 0.0.0 (2015-02-10). @michelle */
!function(a){function b(b,c){this.options=c,this.room=c.room,this.id=c.id,this.type=c.type,this.internalId=c.type+"-"+(this.room?this.room+"-":"")+this.id,this.isMedia="media"===c.type,this.$el=a("<div>"),this.$el.addClass("peer-container"),this.$parent=b,b.append(this.$el),this._constructCommonElements(),this.isMedia&&(this._createStream(),this._constructMediaElements()),this._setupHandlers(),this._createPeer()}navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia;var c={};b.prototype._setupHandlers=function(){var b=this;this.$el.on("click",".peer-buddy",function(){var c=a(this).text();b.isMedia?b.call(c):b.connect(c)}),this.$el.on("click",".peer-end-call",function(){b.isMedia?b.endCall():b.endConnection()}),this.$el.on("click",".peer-answer-call",function(){b.isMedia&&b.answerCall()}),this.isMedia||this.$parent.on("submit",function(c){c.preventDefault(),b.$parent.find("input").each(function(){var c=a(this).val();c&&b.connection&&b.connection.send(c)})})},b.prototype._createPeer=function(){this.peer=new Peer(this.internalId+(this.idOffset?this.idOffset:""),{key:"lwjd5qra8257b9",debug:!0,config:{iceServers:[{url:"stun:stun.l.google.com:19302"},{url:"turn:[email protected]:80",credential:"homeo"}]}});var a=this;this.peer.on("error",function(b){a._handleError(b)}),this.peer.on("open",function(b){a.internalId=b,a.id=b.split("-").pop(),a.ready=!0,a._refreshBuddyList(),c[a.internalId]=1,"undefined"!=typeof a.pendingCall&&a._startCall(a.pendingCall)}),this.isMedia?this.peer.on("call",function(b){a.peerId=b.peer.split("-").pop(),a._maybeAnswerCall(b)}):this.peer.on("connect",function(b){a.connection=b,a._setupDataHandlers(),a.peerId=remoteCall.peer.split("-").pop()})},b.prototype._maybeAnswerCall=function(a){this.tempRemoteCall=a,this.inUse?this.options.manualCalls?(this._showNotification(a.peer+" is calling."),this.$answerCall.removeClass("peer-hidden-element")):this.options.chatroulette?(this.endCall(),this.answerCall()):this.tempRemoteCall=void 0:this.options.manualCalls?(this._showNotification(a.peer+" is calling."),this.$answerCall.removeClass("peer-hidden-element"),this.$parent.trigger("peer.call",a.peer)):this.answerCall()},b.prototype.answerCall=function(){this.call=this.tempRemoteCall,this._setupCallHandlers(),this.call.answer(this.stream),this.tempRemoteCall=void 0,this.$answerCall.addClass("peer-hidden-element")},b.prototype._createStream=function(){var a=this;navigator.getUserMedia({audio:!this.options.disableAudio,video:!this.options.disableVideo},function(b){a.stream=b,a.options.hideOwnVideo||a.$localVideo.prop("src",URL.createObjectURL(b)),"undefined"!=typeof a.pendingCall&&a._startCall(a.pendingCall)},function(a){console.log("Error starting stream:",a)})},b.prototype._handleError=function(a){if(-1!==["invalid-id","browser-incompatible"].indexOf(a.type))throw a;if("unavailable-id"!==a.type){if(a.type){var b="Oops. Something went wrong internally! :(: "+a;throw this._showNotification(b,"error"),new Error(b)}throw a}this.idOffset=this.idOffset||0,this.idOffset+=1,this._createPeer()},b.prototype._startDataConnection=function(a){(this.inUse||"undefined"!=typeof this.pendingConnection)&&this._cancelConnection();if(!this.isMedia){if(this.inUse=!0,!this.ready||!this.stream)return void(this.pendingConnection=a);this.pendingConnection=void 0,this.connection=this.peer.connect(a),this._setupConnectionHandlers(),setTimeout(this._cancelConnection.bind(this),this.options.timeout||3e4)}},b.prototype._cancelConnection=function(){this.inUse=!1,this.pendingConnection=void 0,this.connection&&(this.connection.close(),this._showNotification("Ended connection with "+this.peerId+"."),this.connection=void 0)},b.prototype._startCall=function(a){(this.inUse||"undefined"!=typeof this.pendingCall)&&this._cancelCall();if(this.isMedia){if(this.inUse=!0,!this.ready||!this.stream)return void(this.pendingCall=a);this.pendingCall=void 0,this.call=this.peer.call(a,this.stream),this._setupCallHandlers(),setTimeout(this._cancelCall.bind(this),this.options.timeout||3e4)}},b.prototype._setupDataHandlers=function(){var a=this;this.connection.on("data",function(b){a.$parent.trigger("peer.data",b)})},b.prototype._setupCallHandlers=function(){var a=this;this.call.on("stream",function(b){a._showNotification("Connected to "+a.peerId+"!"),a.$endCall.removeClass("peer-hidden-element"),a.$remoteVideo.prop("src",URL.createObjectURL(b))}),this.call.on("error",function(b){throw a._showNotification(b,"error"),b}),this.call.on("close",function(){a.$endCall.addClass("peer-hidden-element"),a._showNotification("Call with "+a.peerId+" ended.")})},b.prototype._cancelCall=function(){this.inUse=!1,this.pendingCall=void 0,this.call&&(this.call.close(),this._showNotification("Ended call with "+this.peerId+"."),this.call=void 0)},b.prototype._checkPeerType=function(a){if(a&&this.type!==a)throw new Error("Whoops! This element supports "+this.type+" but you are trying to make a "+a+" connection.")},b.prototype.connect=function(a){this._checkPeerType("data"),this._startDataConnection(this._generateIdentifier(a))},b.prototype.call=function(a){this._checkPeerType("media"),this._startCall(this._generateIdentifier(a))},b.prototype._generateIdentifier=function(a){return this.peerId=a,this.room&&(a=this.room+"-"+a),this.type+"-"+a},b.prototype.endCall=function(){this._cancelCall()},b.prototype.availablePeers=function(a){if(!this.peer)return void a([]);try{var b=this;this.peer.listAllPeers(function(d){for(var e=[],f=0,g=d.length;g>f;f+=1){var h=d[f];c[h]||0!==h.indexOf(b.type+"-")||b.room&&h.indexOf(b.room)!==b.type.length+1||e.push(d[f].split("-").pop())}a(e)})}catch(d){a([])}},b.prototype._constructCommonElements=function(){this.options.hideAllControls||(this.$buddyList=a("<div>"),this.$buddyList.addClass("peer-buddy-list"),this.$el.append(this.$buddyList),this.$controls=a("<div>"),this.$controls.addClass("peer-controls"),this.$notifications=a("<div>"),this._resetNotification(),this.$controls.append(this.$notifications))},b.prototype._constructMediaElements=function(){this.options.hideOwnVideo||(this.$localVideo=a("<video autoplay>"),this.$localVideo.addClass("peer-video-local"),this.$localVideo.attr("muted","true"),this.$el.append(this.$localVideo)),this.$remoteVideo=a("<video autoplay>"),this.$remoteVideo.addClass("peer-video-remote"),this.$el.prepend(this.$remoteVideo),this.options.hideAllControls||(this.$endCall=a("<a>"),this.$endCall.addClass("peer-end-call peer-hidden-element"),this.$endCall.text(this.options.endCallText||"End call"),this.$controls.append(this.$endCall),this.$answerCall=a("<a>"),this.$answerCall.addClass("peer-answer-call peer-hidden-element"),this.$answerCall.text(this.options.answerCallText||"Answer call"),this.$controls.append(this.$answerCall),this.$el.append(this.$controls))},b.prototype._refreshBuddyList=function(){if(!this.options.hideAllControls){var b=this;this.availablePeers(function(c){b.$buddyList.empty(),c.forEach(function(c){var d=a("<div>");d.attr("class","peer-buddy"),d.text(c),b.$buddyList.append(d)}),setTimeout(b._refreshBuddyList.bind(b),3e4)})}},b.prototype._showNotification=function(a,b){this.options.hideAllControls||(this.$notifications.text(a),this.$notifications.removeClass("peer-hidden"),b&&this.$notifications.addClass("peer-"+b))},b.prototype._resetNotification=function(){this.options.hideAllControls||(this.$notifications.attr("class","peer-notification peer-hidden"),this.$notifications.text(""))},a.fn.peer=function(c){if(!window.Peer)throw new Error('You need to import PeerJS in order to use jquery.peer. Try putting this in your HTML: `<script type="text/javascript" src="http://cdn.peerjs.com/latest/peer.js"></script>`');if(1!==this.length)throw new Error("You may select exactly one element. Currently "+this.length+" element(s) are selected.");var d=a.data(this,"connection");if("string"==typeof c){var e=c,f=Array.prototype.slice.call(arguments,1);if(!d)throw new Error("You first have to initialize jquery.peer by calling $(element).peer([options]) on this element.");if("function"!=typeof d[e]||"_"===e[0]||"$"===e[0])throw new Error("Method "+e+" not found.");d[e].apply(d,f)}else{if(d)throw new Error("This element has already been initialized.");if(c.room&&-1!==c.room.indexOf("-"))throw new Error("ID must be alphanumeric.");if(-1!==c.id.indexOf("-"))throw new Error("ID must be alphanumeric.");c.type=a(this).is("form")?"data":"media";var g=new b(this,c);a.data(this,"connection",g)}return this}}(jQuery);