From 2038223dc254cf2eba3f8d244ea2d4010b3b6427 Mon Sep 17 00:00:00 2001 From: Henry Rapley Date: Tue, 20 Aug 2013 11:55:11 +0100 Subject: [PATCH] added more event callbacks. made peer.persist method. added description types to callbacks. #78. --- dist/wsc.dAmn.js | 98 ++++++++++++++++++++++++++-------- dist/wsc.dAmn.min.js | 22 ++++---- src/dAmn/peer/call.js | 8 +-- src/dAmn/peer/connection.js | 76 +++++++++++++++++++++----- src/dAmn/peer/signalchannel.js | 10 ++-- src/dAmn/peer/signalhandler.js | 4 +- 6 files changed, 165 insertions(+), 53 deletions(-) diff --git a/dist/wsc.dAmn.js b/dist/wsc.dAmn.js index 6d30537..ec191ae 100644 --- a/dist/wsc.dAmn.js +++ b/dist/wsc.dAmn.js @@ -12946,7 +12946,7 @@ wsc.dAmn.BDS.Peer.chan.calls = []; * @param pns {String} Peer namespace the call is associated with * @since 0.0.0 */ -wsc.dAmn.BDS.Peer.Call = function( client, bds, pns, user, application ) { +wsc.dAmn.BDS.Peer.Call = function( client, bds, pns, user, application, constraints, stream ) { this.client = client; @@ -12957,8 +12957,10 @@ wsc.dAmn.BDS.Peer.Call = function( client, bds, pns, user, application ) { this.app = application; this.title = ''; this.pc = ''; - + this.localstream = stream; + this.constraints = constraints; this.peers = {}; + this.group = user.substr(0, 5) == 'chat:'; var boom = this.pns.split(':'); @@ -13015,7 +13017,7 @@ wsc.dAmn.BDS.Peer.Call.prototype.new_peer = function( user, offer ) { } */ - var peer = wsc.dAmn.BDS.peer_connection( this, user, offer ); + var peer = wsc.dAmn.BDS.peer_connection( this, user, offer, this.constraints, this.localstream ); this.peers[user] = peer; @@ -13039,12 +13041,12 @@ wsc.dAmn.BDS.Peer.Call.prototype.peer = function( peer ) { * * Make a peer connection. */ -wsc.dAmn.BDS.peer_connection = function( call, user, remote ) { +wsc.dAmn.BDS.peer_connection = function( call, user, remote, constraints ) { if( !wsc.dAmn.BDS.Peer.RTC.PeerConnection ) return null; - return new wsc.dAmn.BDS.Peer.Connection( call, user, remote ); + return new wsc.dAmn.BDS.Peer.Connection( call, user, remote, constraints ); }; @@ -13060,11 +13062,11 @@ wsc.dAmn.BDS.peer_connection = function( call, user, remote ) { * @param [remote_offer=null] {String} Descriptor for a remote offer. * @since 0.0.0 */ -wsc.dAmn.BDS.Peer.Connection = function( call, user, remote_offer ) { +wsc.dAmn.BDS.Peer.Connection = function( call, user, remote_offer, constraints, stream ) { this.call = call; this.user = user; - this.pc = new wsc.dAmn.BDS.Peer.RTC.PeerConnection( wsc.dAmn.BDS.Peer._options ); + this.pc = new wsc.dAmn.BDS.Peer.RTC.PeerConnection( wsc.dAmn.BDS.Peer._options, constraints ); this.offer = ''; this.remote_offer = remote_offer || null; this.responding = this.remote_offer != null; @@ -13077,6 +13079,9 @@ wsc.dAmn.BDS.Peer.Connection = function( call, user, remote_offer ) { if( this.remote_offer ) this.set_remote_description( this.remote_offer ); + + if( stream ) + this.set_local_stream( stream ); }; @@ -13099,6 +13104,11 @@ wsc.dAmn.BDS.Peer.Connection.prototype.bindings = function( ) { pc.set_remote_stream( event ); }; + // Negotiation is needed! + this.pc.onnegotiationneeded = function( event ) { + pc.onnegotiationneeded( event ); + }; + // Stub event handler var stub = function() {}; this.onready = stub; @@ -13108,6 +13118,7 @@ wsc.dAmn.BDS.Peer.Connection.prototype.bindings = function( ) { this.onicecompleted = stub; this.onremotestream = stub; this.onlocalstream = stub; + this.onnegotiationneeded = stub; }; @@ -13265,7 +13276,7 @@ wsc.dAmn.BDS.Peer.Connection.prototype.offer_created = function( description ) { this.offer = description; var pc = this; - this.pc.setLocalDescription( this.offer , function( ) { pc.local_description_set(); }, this.onerror ); + this.pc.setLocalDescription( this.offer , function( ) { pc.local_description_set( 0 ); }, this.onerror ); }; @@ -13273,43 +13284,46 @@ wsc.dAmn.BDS.Peer.Connection.prototype.offer_created = function( description ) { * Set the descriptor for the remote connection. * @method set_remote_description * @param description {String} Descriptor for the remote connection + * @param type {Integer} Offer (0) or answer (1) */ -wsc.dAmn.BDS.Peer.Connection.prototype.set_remote_description = function( description ) { +wsc.dAmn.BDS.Peer.Connection.prototype.set_remote_description = function( description, type ) { this.remote_offer = description; this.responding = this.pc.signalingState == 'stable'; var pc = this; - this.pc.setRemoteDescription( this.remote_offer , function( ) { pc.remote_description_set(); }, this.onerror ); + this.pc.setRemoteDescription( this.remote_offer , function( ) { pc.remote_description_set( type ); }, this.onerror ); }; /** * A local description as been set. Handle it! * @method local_description_set + * @param type {Integer} Offer (0) or answer (1) */ -wsc.dAmn.BDS.Peer.Connection.prototype.local_description_set = function( ) { +wsc.dAmn.BDS.Peer.Connection.prototype.local_description_set = function( type ) { if( this.responding ) { this.connected = true; this.responding = false; } - this.onlocaldescription(); + this.onlocaldescription( type ); }; /** * A local description as been set. Handle it! * @method remote_description_set + * @param type {Integer} Offer (0) or answer (1) */ -wsc.dAmn.BDS.Peer.Connection.prototype.remote_description_set = function( ) { +wsc.dAmn.BDS.Peer.Connection.prototype.remote_description_set = function( type ) { if( !this.responding ) { this.connected = true; } - this.onremotedescription(); + this.onremotedescription( type ); }; @@ -13340,7 +13354,7 @@ wsc.dAmn.BDS.Peer.Connection.prototype.answer_created = function( answer ) { var pc = this; this.pc.setLocalDescription( this.offer, - function( ) { pc.local_description_set(); }, + function( ) { pc.local_description_set( 1 ); }, function( err ) { pc.onerror( err ); } ); @@ -13371,6 +13385,46 @@ wsc.dAmn.BDS.Peer.Connection.prototype.set_local_stream = function( stream ) { this.stream = stream; this.onlocalstream(); +}; + + +/** + * Keep responding to offers and answers as though your life depends on it. + * + * @method persist + */ +wsc.dAmn.BDS.Peer.Connection.prototype.persist = function( ) { + + var peer = this; + var call = this.call; + + // Send an answer to an offer. + peer.onremotedescription = function( type ) { + if( type != 0 ) + return; + + peer.create_answer(); + }; + + // Send our offers and answers + peer.onlocaldescription = function( type ) { + switch( type ) { + case 0: + call.signal.offer( peer ); + break; + case 1: + call.signal.answer( peer ); + break; + default: + break; + } + }; + + // Make it happen. + peer.onnegotiationneeded = function( ) { + peer.create_offer(); + }; + };/** * This object represents a signalling channel used for transmitting connection data * between two peers. @@ -13378,17 +13432,19 @@ wsc.dAmn.BDS.Peer.Connection.prototype.set_local_stream = function( stream ) { * @class wsc.dAmn.BDS.Peer.SignalChannel * @constructor * @param client {Object} An instance of wsc. - * @param this.bds {String} dAmn channel used for this.bds commands - * @param this.pns {String} Peer namespace associated with the signals + * @param bds {String} dAmn channel used for this.bds commands + * @param pns {String} Peer namespace associated with the signals + * @param application {String} Application name for the thingy * @since 0.0.0 */ -wsc.dAmn.BDS.Peer.SignalChannel = function( client, bds, pns ) { +wsc.dAmn.BDS.Peer.SignalChannel = function( client, bds, pns, application ) { this.user = client.settings.username; this.nse = ns ? ',' + ns : ''; this.bds = bds; this.pns = pns; this.ns = ns; + this.app = application; this.client = client; }; @@ -13426,7 +13482,7 @@ wsc.dAmn.BDS.Peer.SignalChannel.prototype.command = function( ) { */ wsc.dAmn.BDS.Peer.SignalChannel.prototype.request = function( app ) { - this.command( 'REQUEST', this.user, app || 'webcam' ); + this.command( 'REQUEST', this.user, app || this.app ); }; @@ -13721,7 +13777,7 @@ wsc.dAmn.BDS.Peer.SignalHandler.prototype.offer = function( event, client ) { offer: offer } ); - peer.set_remote_description( offer ); + peer.set_remote_description( offer, 0 ); }; @@ -13762,7 +13818,7 @@ wsc.dAmn.BDS.Peer.SignalHandler.prototype.answer = function( event, client ) { answer: answer } ); - peer.set_remote_description( answer ); + peer.set_remote_description( answer, 1 ); }; diff --git a/dist/wsc.dAmn.min.js b/dist/wsc.dAmn.min.js index 47c2692..68620f2 100644 --- a/dist/wsc.dAmn.min.js +++ b/dist/wsc.dAmn.min.js @@ -160,14 +160,14 @@ z=a.find("img.larger")}z.css("display","inline");n=true;p.removeClass("plus");p. },function(a){t=a[1];return''+(t||"[link]")+""}],"&acro\t":[1,''],"&/acro\t":[0,""],"&abbr\t":[1,''],"&/abbr\t":[0,""],"&img\t":[3,'{1}'],"&iframe\t":[3,'"],"&a\t":[2,''],"&/a\t":[0,""],"&br\t":[0,"
"],"&bcode\t":[0,"","
"],"&/bcode\t":[0,"","
"],"&avatar\t":[2,":icon{0}:",function(a){return wsc.dAmn.avatar.link(a[0],a[1])}],"&emote\t":[5,"{0}",'{0}'],"&dev\t":[2,":dev{1}:",'{0}{1}',"{0}\x1b[36m{1}\x1b[39m"],"&thumb\t":[6,":thumb{0}:",wsc.dAmn.Emotes.Tablumps],EOF:[0,"",null,"\x1b[m"]}};wsc.dAmn.TablumpParser.prototype.parse=function(b,a){b=replaceAll(b,"<","<"); b=replaceAll(b,">",">");return new wsc.dAmn.TablumpString(b,this)};wsc.dAmn.TablumpParser.prototype.tokenise=function(j){if(!j){return[]}var a=wsc.dAmn.PARSE.RAW;var m=[];var b="";var g=0;var l=[];var f="";var n="";var k="";for(var h=0;h0){m.push(["raw",f]);f=""}l=[b,[]];g=this.lumps[b][0];m.push(l);b="";if(g>0){a=wsc.dAmn.PARSE.ARG}else{a=wsc.dAmn.PARSE.RAW}}}break;case wsc.dAmn.PARSE.ARG:if(k=="\t"){g--;if(b=="&"){a=wsc.dAmn.PARSE.RAW;b="";break}l[1].push(b);b="";if(g==0){a=wsc.dAmn.PARSE.RAW}break}b+=k;break}}if(f.length>0||b.length>0){m.push(["raw",f+b])}return m};wsc.dAmn.TablumpParser.prototype.render=function(a,g){if(!g){return""}if(!g.hasOwnProperty("tokens")){return""}a=a+1;var h=""; var f=[];for(var b in g.tokens){if(!g.tokens.hasOwnProperty(b)){continue}f=g.tokens[b];if(f[0]=="raw"){h+=f[1];continue}h+=this.renderOne(a,f[0],f[1])}return h+this.renderOne(a,"EOF","")};wsc.dAmn.TablumpParser.prototype.renderOne=function(b,a,h){var f=this.lumps[a];if(f===undefined){return"&"+a+"\t"+h.join("\t")}var g=f[b]||f[1];if(typeof(g)=="string"){return String.format(g,h)}else{return g.call(this,h)}};wsc.dAmn.BDS.Peer=function(a,g,b){var f=function(){var h=new wsc.dAmn.BDS.Peer.SignalHandler(a)};b.bds.peer={handler:null,calls:{},call:function(h){h=h.toLowerCase();for(var j in b.bds.peer.calls){if(!b.bds.peer.calls.hasOwnProperty(j)){continue}if(j.toLowerCase()==h){return b.bds.peer.calls[j]}}return null},open:function(k,l,h,j){b.bds.peer.calls[l]=new wsc.dAmn.BDS.Peer.Call(a,k,l,h,j);return b.bds.peer.calls[l]},request:function(){},};f()};wsc.dAmn.BDS.Peer.bots=["botdom","damnphone"];wsc.dAmn.BDS.Peer.peer_options={iceServers:[{url:"stun:stun.l.google.com:19302"}]};wsc.dAmn.BDS.Peer.phone=null; -wsc.dAmn.BDS.Peer.signal=null;wsc.dAmn.BDS.Peer.local={};wsc.dAmn.BDS.Peer.local.stream=null;wsc.dAmn.BDS.Peer.local.url=null;wsc.dAmn.BDS.Peer.remote={};wsc.dAmn.BDS.Peer.remote._empty={video:null,audio:null,conn:null};wsc.dAmn.BDS.Peer.chan={};wsc.dAmn.BDS.Peer.chan.group=false;wsc.dAmn.BDS.Peer.chan.calls=[];wsc.dAmn.BDS.Peer.Call=function(f,a,l,g,j){this.client=f;this.bds=a;this.ns="";this.pns=l;this.user=g;this.app=j;this.title="";this.pc="";this.peers={};this.group=g.substr(0,5)=="chat:";var b=this.pns.split(":");var k=b.shift();if(k=="chat"){this.ns="chat:"+b.shift()}else{if(k=="pchat"){this.ns="pchat:"+b.shift()+":"+b.shift()}}if(b[0]=="pc"){b.shift();this.pc=b.shift()}this.title=b.join(":");this.group=wsc.dAmn.BDS.Peer.bots.indexOf(this.ns.substr(1))!=-1;this.signal=new wsc.dAmn.BDS.Peer.SignalChannel(f,a,l)};wsc.dAmn.BDS.Peer.Call.prototype.close=function(){for(var a in this.peers){if(!this.peers.hasOwnProperty(a)){continue}this.peers[a].conn.close()}};wsc.dAmn.BDS.Peer.Call.prototype.new_peer=function(a,b){var f=wsc.dAmn.BDS.peer_connection(this,a,b); -this.peers[a]=f;return f};wsc.dAmn.BDS.Peer.Call.prototype.peer=function(a){return this.peers[a]||null};wsc.dAmn.BDS.peer_connection=function(b,a,f){if(!wsc.dAmn.BDS.Peer.RTC.PeerConnection){return null}return new wsc.dAmn.BDS.Peer.Connection(b,a,f)};wsc.dAmn.BDS.Peer.Connection=function(b,a,f){this.call=b;this.user=a;this.pc=new wsc.dAmn.BDS.Peer.RTC.PeerConnection(wsc.dAmn.BDS.Peer._options);this.offer="";this.remote_offer=f||null;this.responding=this.remote_offer!=null;this.streamed=false;this.remote_stream=null;this.stream=null;this.connected=false;this.bindings();if(this.remote_offer){this.set_remote_description(this.remote_offer)}};wsc.dAmn.BDS.Peer.Connection.prototype.bindings=function(){var b=this;var a=this.user;this.pc.onicecandidate=function(g){b.call.signal.candidate(b,g.candidate)};this.pc.onaddstream=function(g){b.set_remote_stream(g)};var f=function(){};this.onready=f;this.onopen=f;this.onremotedescription=f;this.onlocaldescription=f;this.onicecompleted=f;this.onremotestream=f; -this.onlocalstream=f};wsc.dAmn.BDS.Peer.Connection.prototype.ready=function(a,g){var f=this;var b=this.onready;this.remote_offer=g||this.remote_offer;this.responding=this.remote_offer!=null;this.create_offer()};wsc.dAmn.BDS.Peer.Connection.prototype.open=function(a,f){if(!this.offer){return}var b=this;var g=this.onopen;this.remote_offer=f||this.remote_offer;this.onopen=function(){(a||g)();b.onopen=g};if(!this.remote_offer){return}this.set_remote_description(this.remote_offer)};wsc.dAmn.BDS.Peer.Connection.prototype.close=function(){this.pc.close()};wsc.dAmn.BDS.Peer.Connection.prototype.onerror=function(a){console.log(">> Got an error:",'"',a.message,'"',a)};wsc.dAmn.BDS.Peer.Connection.prototype.candidate=function(a){this.pc.addIceCandidate(a)};wsc.dAmn.BDS.Peer.Connection.prototype.ice_completed=function(){this.onicecompleted()};wsc.dAmn.BDS.Peer.Connection.prototype.create_offer=function(){var a=this;this.pc.createOffer(function(b){a.offer_created(b)},function(b){a.onerror(b)})};wsc.dAmn.BDS.Peer.Connection.prototype.offer_created=function(b){this.offer=b; -var a=this;this.pc.setLocalDescription(this.offer,function(){a.local_description_set()},this.onerror)};wsc.dAmn.BDS.Peer.Connection.prototype.set_remote_description=function(b){this.remote_offer=b;this.responding=this.pc.signalingState=="stable";var a=this;this.pc.setRemoteDescription(this.remote_offer,function(){a.remote_description_set()},this.onerror)};wsc.dAmn.BDS.Peer.Connection.prototype.local_description_set=function(){if(this.responding){this.connected=true;this.responding=false}this.onlocaldescription()};wsc.dAmn.BDS.Peer.Connection.prototype.remote_description_set=function(){if(!this.responding){this.connected=true}this.onremotedescription()};wsc.dAmn.BDS.Peer.Connection.prototype.create_answer=function(){var a=this;this.responding=true;this.pc.createAnswer(function(b){a.answer_created(b)},function(b){a.onerror(b)})};wsc.dAmn.BDS.Peer.Connection.prototype.answer_created=function(b){this.offer=b;var a=this;this.pc.setLocalDescription(this.offer,function(){a.local_description_set() -},function(f){a.onerror(f)})};wsc.dAmn.BDS.Peer.Connection.prototype.set_remote_stream=function(a){this.remote_stream=a.stream;this.onremotestream()};wsc.dAmn.BDS.Peer.Connection.prototype.set_local_stream=function(a){this.pc.addStream(a);this.stream=a;this.onlocalstream()};wsc.dAmn.BDS.Peer.SignalChannel=function(b,a,f){this.user=b.settings.username;this.nse=ns?","+ns:"";this.bds=a;this.pns=f;this.ns=ns;this.client=b};wsc.dAmn.BDS.Peer.SignalChannel.prototype.command=function(){var b=Array.prototype.slice.call(arguments);var g=b.shift();var a=this.pns;for(var f=0;f> Got an error:",'"',a.message,'"',a)};wsc.dAmn.BDS.Peer.Connection.prototype.candidate=function(a){this.pc.addIceCandidate(a)};wsc.dAmn.BDS.Peer.Connection.prototype.ice_completed=function(){this.onicecompleted() +};wsc.dAmn.BDS.Peer.Connection.prototype.create_offer=function(){var a=this;this.pc.createOffer(function(b){a.offer_created(b)},function(b){a.onerror(b)})};wsc.dAmn.BDS.Peer.Connection.prototype.offer_created=function(b){this.offer=b;var a=this;this.pc.setLocalDescription(this.offer,function(){a.local_description_set(0)},this.onerror)};wsc.dAmn.BDS.Peer.Connection.prototype.set_remote_description=function(f,b){this.remote_offer=f;this.responding=this.pc.signalingState=="stable";var a=this;this.pc.setRemoteDescription(this.remote_offer,function(){a.remote_description_set(b)},this.onerror)};wsc.dAmn.BDS.Peer.Connection.prototype.local_description_set=function(a){if(this.responding){this.connected=true;this.responding=false}this.onlocaldescription(a)};wsc.dAmn.BDS.Peer.Connection.prototype.remote_description_set=function(a){if(!this.responding){this.connected=true}this.onremotedescription(a)};wsc.dAmn.BDS.Peer.Connection.prototype.create_answer=function(){var a=this;this.responding=true;this.pc.createAnswer(function(b){a.answer_created(b) +},function(b){a.onerror(b)})};wsc.dAmn.BDS.Peer.Connection.prototype.answer_created=function(b){this.offer=b;var a=this;this.pc.setLocalDescription(this.offer,function(){a.local_description_set(1)},function(f){a.onerror(f)})};wsc.dAmn.BDS.Peer.Connection.prototype.set_remote_stream=function(a){this.remote_stream=a.stream;this.onremotestream()};wsc.dAmn.BDS.Peer.Connection.prototype.set_local_stream=function(a){this.pc.addStream(a);this.stream=a;this.onlocalstream()};wsc.dAmn.BDS.Peer.Connection.prototype.persist=function(){var b=this;var a=this.call;b.onremotedescription=function(f){if(f!=0){return}b.create_answer()};b.onlocaldescription=function(f){switch(f){case 0:a.signal.offer(b);break;case 1:a.signal.answer(b);break;default:break}};b.onnegotiationneeded=function(){b.create_offer()}};wsc.dAmn.BDS.Peer.SignalChannel=function(b,a,g,f){this.user=b.settings.username;this.nse=ns?","+ns:"";this.bds=a;this.pns=g;this.ns=ns;this.app=f;this.client=b};wsc.dAmn.BDS.Peer.SignalChannel.prototype.command=function(){var b=Array.prototype.slice.call(arguments); +var g=b.shift();var a=this.pns;for(var f=0;f