You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophejingle.bundle.js 150KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. var Base64=(function(){var keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";var obj={encode:function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;do{chr1=input.charCodeAt(i++);chr2=input.charCodeAt(i++);chr3=input.charCodeAt(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64}else{if(isNaN(chr3)){enc4=64}}output=output+keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4)}while(i<input.length);return output},decode:function(input){var output="";var chr1,chr2,chr3;var enc1,enc2,enc3,enc4;var i=0;input=input.replace(/[^A-Za-z0-9\+\/\=]/g,"");do{enc1=keyStr.indexOf(input.charAt(i++));enc2=keyStr.indexOf(input.charAt(i++));enc3=keyStr.indexOf(input.charAt(i++));enc4=keyStr.indexOf(input.charAt(i++));chr1=(enc1<<2)|(enc2>>4);chr2=((enc2&15)<<4)|(enc3>>2);chr3=((enc3&3)<<6)|enc4;output=output+String.fromCharCode(chr1);if(enc3!=64){output=output+String.fromCharCode(chr2)}if(enc4!=64){output=output+String.fromCharCode(chr3)}}while(i<input.length);return output}};return obj})();var hexcase=0;var b64pad="=";var chrsz=8;function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length*chrsz))}function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length*chrsz))}function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length*chrsz))}function hex_hmac_sha1(key,data){return binb2hex(core_hmac_sha1(key,data))}function b64_hmac_sha1(key,data){return binb2b64(core_hmac_sha1(key,data))}function str_hmac_sha1(key,data){return binb2str(core_hmac_sha1(key,data))}function sha1_vm_test(){return hex_sha1("abc")=="a9993e364706816aba3e25717850c26c9cd0d89d"}function core_sha1(x,len){x[len>>5]|=128<<(24-len%32);x[((len+64>>9)<<4)+15]=len;var w=new Array(80);var a=1732584193;var b=-271733879;var c=-1732584194;var d=271733878;var e=-1009589776;var i,j,t,olda,oldb,oldc,oldd,olde;for(i=0;i<x.length;i+=16){olda=a;oldb=b;oldc=c;oldd=d;olde=e;for(j=0;j<80;j++){if(j<16){w[j]=x[i+j]}else{w[j]=rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1)}t=safe_add(safe_add(rol(a,5),sha1_ft(j,b,c,d)),safe_add(safe_add(e,w[j]),sha1_kt(j)));e=d;d=c;c=rol(b,30);b=a;a=t}a=safe_add(a,olda);b=safe_add(b,oldb);c=safe_add(c,oldc);d=safe_add(d,oldd);e=safe_add(e,olde)}return[a,b,c,d,e]}function sha1_ft(t,b,c,d){if(t<20){return(b&c)|((~b)&d)}if(t<40){return b^c^d}if(t<60){return(b&c)|(b&d)|(c&d)}return b^c^d}function sha1_kt(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514}function core_hmac_sha1(key,data){var bkey=str2binb(key);if(bkey.length>16){bkey=core_sha1(bkey,key.length*chrsz)}var ipad=new Array(16),opad=new Array(16);for(var i=0;i<16;i++){ipad[i]=bkey[i]^909522486;opad[i]=bkey[i]^1549556828}var hash=core_sha1(ipad.concat(str2binb(data)),512+data.length*chrsz);return core_sha1(opad.concat(hash),512+160)}function safe_add(x,y){var lsw=(x&65535)+(y&65535);var msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&65535)}function rol(num,cnt){return(num<<cnt)|(num>>>(32-cnt))}function str2binb(str){var bin=[];var mask=(1<<chrsz)-1;for(var i=0;i<str.length*chrsz;i+=chrsz){bin[i>>5]|=(str.charCodeAt(i/chrsz)&mask)<<(32-chrsz-i%32)}return bin}function binb2str(bin){var str="";var mask=(1<<chrsz)-1;for(var i=0;i<bin.length*32;i+=chrsz){str+=String.fromCharCode((bin[i>>5]>>>(32-chrsz-i%32))&mask)}return str}function binb2hex(binarray){var hex_tab=hexcase?"0123456789ABCDEF":"0123456789abcdef";var str="";for(var i=0;i<binarray.length*4;i++){str+=hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8+4))&15)+hex_tab.charAt((binarray[i>>2]>>((3-i%4)*8))&15)}return str}function binb2b64(binarray){var tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var str="";var triplet,j;for(var i=0;i<binarray.length*4;i+=3){triplet=(((binarray[i>>2]>>8*(3-i%4))&255)<<16)|(((binarray[i+1>>2]>>8*(3-(i+1)%4))&255)<<8)|((binarray[i+2>>2]>>8*(3-(i+2)%4))&255);for(j=0;j<4;j++){if(i*8+j*6>binarray.length*32){str+=b64pad}else{str+=tab.charAt((triplet>>6*(3-j))&63)}}}return str}var MD5=(function(){var hexcase=0;var b64pad="";var chrsz=8;var safe_add=function(x,y){var lsw=(x&65535)+(y&65535);var msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&65535)};var bit_rol=function(num,cnt){return(num<<cnt)|(num>>>(32-cnt))};var str2binl=function(str){var bin=[];var mask=(1<<chrsz)-1;for(var i=0;i<str.length*chrsz;i+=chrsz){bin[i>>5]|=(str.charCodeAt(i/chrsz)&mask)<<(i%32)}return bin};var binl2str=function(bin){var str="";var mask=(1<<chrsz)-1;for(var i=0;i<bin.length*32;i+=chrsz){str+=String.fromCharCode((bin[i>>5]>>>(i%32))&mask)}return str};var binl2hex=function(binarray){var hex_tab=hexcase?"0123456789ABCDEF":"0123456789abcdef";var str="";for(var i=0;i<binarray.length*4;i++){str+=hex_tab.charAt((binarray[i>>2]>>((i%4)*8+4))&15)+hex_tab.charAt((binarray[i>>2]>>((i%4)*8))&15)}return str};var binl2b64=function(binarray){var tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var str="";var triplet,j;for(var i=0;i<binarray.length*4;i+=3){triplet=(((binarray[i>>2]>>8*(i%4))&255)<<16)|(((binarray[i+1>>2]>>8*((i+1)%4))&255)<<8)|((binarray[i+2>>2]>>8*((i+2)%4))&255);for(j=0;j<4;j++){if(i*8+j*6>binarray.length*32){str+=b64pad}else{str+=tab.charAt((triplet>>6*(3-j))&63)}}}return str};var md5_cmn=function(q,a,b,x,s,t){return safe_add(bit_rol(safe_add(safe_add(a,q),safe_add(x,t)),s),b)};var md5_ff=function(a,b,c,d,x,s,t){return md5_cmn((b&c)|((~b)&d),a,b,x,s,t)};var md5_gg=function(a,b,c,d,x,s,t){return md5_cmn((b&d)|(c&(~d)),a,b,x,s,t)};var md5_hh=function(a,b,c,d,x,s,t){return md5_cmn(b^c^d,a,b,x,s,t)};var md5_ii=function(a,b,c,d,x,s,t){return md5_cmn(c^(b|(~d)),a,b,x,s,t)};var core_md5=function(x,len){x[len>>5]|=128<<((len)%32);x[(((len+64)>>>9)<<4)+14]=len;var a=1732584193;var b=-271733879;var c=-1732584194;var d=271733878;var olda,oldb,oldc,oldd;for(var i=0;i<x.length;i+=16){olda=a;oldb=b;oldc=c;oldd=d;a=md5_ff(a,b,c,d,x[i+0],7,-680876936);d=md5_ff(d,a,b,c,x[i+1],12,-389564586);c=md5_ff(c,d,a,b,x[i+2],17,606105819);b=md5_ff(b,c,d,a,x[i+3],22,-1044525330);a=md5_ff(a,b,c,d,x[i+4],7,-176418897);d=md5_ff(d,a,b,c,x[i+5],12,1200080426);c=md5_ff(c,d,a,b,x[i+6],17,-1473231341);b=md5_ff(b,c,d,a,x[i+7],22,-45705983);a=md5_ff(a,b,c,d,x[i+8],7,1770035416);d=md5_ff(d,a,b,c,x[i+9],12,-1958414417);c=md5_ff(c,d,a,b,x[i+10],17,-42063);b=md5_ff(b,c,d,a,x[i+11],22,-1990404162);a=md5_ff(a,b,c,d,x[i+12],7,1804603682);d=md5_ff(d,a,b,c,x[i+13],12,-40341101);c=md5_ff(c,d,a,b,x[i+14],17,-1502002290);b=md5_ff(b,c,d,a,x[i+15],22,1236535329);a=md5_gg(a,b,c,d,x[i+1],5,-165796510);d=md5_gg(d,a,b,c,x[i+6],9,-1069501632);c=md5_gg(c,d,a,b,x[i+11],14,643717713);b=md5_gg(b,c,d,a,x[i+0],20,-373897302);a=md5_gg(a,b,c,d,x[i+5],5,-701558691);d=md5_gg(d,a,b,c,x[i+10],9,38016083);c=md5_gg(c,d,a,b,x[i+15],14,-660478335);b=md5_gg(b,c,d,a,x[i+4],20,-405537848);a=md5_gg(a,b,c,d,x[i+9],5,568446438);d=md5_gg(d,a,b,c,x[i+14],9,-1019803690);c=md5_gg(c,d,a,b,x[i+3],14,-187363961);b=md5_gg(b,c,d,a,x[i+8],20,1163531501);a=md5_gg(a,b,c,d,x[i+13],5,-1444681467);d=md5_gg(d,a,b,c,x[i+2],9,-51403784);c=md5_gg(c,d,a,b,x[i+7],14,1735328473);b=md5_gg(b,c,d,a,x[i+12],20,-1926607734);a=md5_hh(a,b,c,d,x[i+5],4,-378558);d=md5_hh(d,a,b,c,x[i+8],11,-2022574463);c=md5_hh(c,d,a,b,x[i+11],16,1839030562);b=md5_hh(b,c,d,a,x[i+14],23,-35309556);a=md5_hh(a,b,c,d,x[i+1],4,-1530992060);d=md5_hh(d,a,b,c,x[i+4],11,1272893353);c=md5_hh(c,d,a,b,x[i+7],16,-155497632);b=md5_hh(b,c,d,a,x[i+10],23,-1094730640);a=md5_hh(a,b,c,d,x[i+13],4,681279174);d=md5_hh(d,a,b,c,x[i+0],11,-358537222);c=md5_hh(c,d,a,b,x[i+3],16,-722521979);b=md5_hh(b,c,d,a,x[i+6],23,76029189);a=md5_hh(a,b,c,d,x[i+9],4,-640364487);d=md5_hh(d,a,b,c,x[i+12],11,-421815835);c=md5_hh(c,d,a,b,x[i+15],16,530742520);b=md5_hh(b,c,d,a,x[i+2],23,-995338651);a=md5_ii(a,b,c,d,x[i+0],6,-198630844);d=md5_ii(d,a,b,c,x[i+7],10,1126891415);c=md5_ii(c,d,a,b,x[i+14],15,-1416354905);b=md5_ii(b,c,d,a,x[i+5],21,-57434055);a=md5_ii(a,b,c,d,x[i+12],6,1700485571);d=md5_ii(d,a,b,c,x[i+3],10,-1894986606);c=md5_ii(c,d,a,b,x[i+10],15,-1051523);b=md5_ii(b,c,d,a,x[i+1],21,-2054922799);a=md5_ii(a,b,c,d,x[i+8],6,1873313359);d=md5_ii(d,a,b,c,x[i+15],10,-30611744);c=md5_ii(c,d,a,b,x[i+6],15,-1560198380);b=md5_ii(b,c,d,a,x[i+13],21,1309151649);a=md5_ii(a,b,c,d,x[i+4],6,-145523070);d=md5_ii(d,a,b,c,x[i+11],10,-1120210379);c=md5_ii(c,d,a,b,x[i+2],15,718787259);b=md5_ii(b,c,d,a,x[i+9],21,-343485551);a=safe_add(a,olda);b=safe_add(b,oldb);c=safe_add(c,oldc);d=safe_add(d,oldd)}return[a,b,c,d]};var core_hmac_md5=function(key,data){var bkey=str2binl(key);if(bkey.length>16){bkey=core_md5(bkey,key.length*chrsz)}var ipad=new Array(16),opad=new Array(16);for(var i=0;i<16;i++){ipad[i]=bkey[i]^909522486;opad[i]=bkey[i]^1549556828}var hash=core_md5(ipad.concat(str2binl(data)),512+data.length*chrsz);return core_md5(opad.concat(hash),512+128)};var obj={hexdigest:function(s){return binl2hex(core_md5(str2binl(s),s.length*chrsz))},b64digest:function(s){return binl2b64(core_md5(str2binl(s),s.length*chrsz))},hash:function(s){return binl2str(core_md5(str2binl(s),s.length*chrsz))},hmac_hexdigest:function(key,data){return binl2hex(core_hmac_md5(key,data))},hmac_b64digest:function(key,data){return binl2b64(core_hmac_md5(key,data))},hmac_hash:function(key,data){return binl2str(core_hmac_md5(key,data))},test:function(){return MD5.hexdigest("abc")==="900150983cd24fb0d6963f7d28e17f72"}};return obj})();if(!Function.prototype.bind){Function.prototype.bind=function(obj){var func=this;var _slice=Array.prototype.slice;var _concat=Array.prototype.concat;var _args=_slice.call(arguments,1);return function(){return func.apply(obj?obj:this,_concat.call(_args,_slice.call(arguments,0)))}}}if(!Array.prototype.indexOf){Array.prototype.indexOf=function(elt){var len=this.length;var from=Number(arguments[1])||0;from=(from<0)?Math.ceil(from):Math.floor(from);if(from<0){from+=len}for(;from<len;from++){if(from in this&&this[from]===elt){return from}}return -1}}(function(callback){var Strophe;function $build(name,attrs){return new Strophe.Builder(name,attrs)}function $msg(attrs){return new Strophe.Builder("message",attrs)}function $iq(attrs){return new Strophe.Builder("iq",attrs)}function $pres(attrs){return new Strophe.Builder("presence",attrs)}Strophe={VERSION:"1.1.0",NS:{HTTPBIND:"http://jabber.org/protocol/httpbind",BOSH:"urn:xmpp:xbosh",CLIENT:"jabber:client",AUTH:"jabber:iq:auth",ROSTER:"jabber:iq:roster",PROFILE:"jabber:iq:profile",DISCO_INFO:"http://jabber.org/protocol/disco#info",DISCO_ITEMS:"http://jabber.org/protocol/disco#items",MUC:"http://jabber.org/protocol/muc",SASL:"urn:ietf:params:xml:ns:xmpp-sasl",STREAM:"http://etherx.jabber.org/streams",BIND:"urn:ietf:params:xml:ns:xmpp-bind",SESSION:"urn:ietf:params:xml:ns:xmpp-session",VERSION:"jabber:iq:version",STANZAS:"urn:ietf:params:xml:ns:xmpp-stanzas",XHTML_IM:"http://jabber.org/protocol/xhtml-im",XHTML:"http://www.w3.org/1999/xhtml"},XHTML:{tags:["a","blockquote","br","cite","em","img","li","ol","p","span","strong","ul","body"],attributes:{a:["href"],blockquote:["style"],br:[],cite:["style"],em:[],img:["src","alt","style","height","width"],li:["style"],ol:["style"],p:["style"],span:["style"],strong:[],ul:["style"],body:[]},css:["background-color","color","font-family","font-size","font-style","font-weight","margin-left","margin-right","text-align","text-decoration"],validTag:function(tag){for(var i=0;i<Strophe.XHTML.tags.length;i++){if(tag==Strophe.XHTML.tags[i]){return true}}return false},validAttribute:function(tag,attribute){if(typeof Strophe.XHTML.attributes[tag]!=="undefined"&&Strophe.XHTML.attributes[tag].length>0){for(var i=0;i<Strophe.XHTML.attributes[tag].length;i++){if(attribute==Strophe.XHTML.attributes[tag][i]){return true}}}return false},validCSS:function(style){for(var i=0;i<Strophe.XHTML.css.length;i++){if(style==Strophe.XHTML.css[i]){return true}}return false}},Status:{ERROR:0,CONNECTING:1,CONNFAIL:2,AUTHENTICATING:3,AUTHFAIL:4,CONNECTED:5,DISCONNECTED:6,DISCONNECTING:7,ATTACHED:8},LogLevel:{DEBUG:0,INFO:1,WARN:2,ERROR:3,FATAL:4},ElementType:{NORMAL:1,TEXT:3,CDATA:4,FRAGMENT:11},TIMEOUT:1.1,SECONDARY_TIMEOUT:0.1,addNamespace:function(name,value){Strophe.NS[name]=value},forEachChild:function(elem,elemName,func){var i,childNode;for(i=0;i<elem.childNodes.length;i++){childNode=elem.childNodes[i];if(childNode.nodeType==Strophe.ElementType.NORMAL&&(!elemName||this.isTagEqual(childNode,elemName))){func(childNode)}}},isTagEqual:function(el,name){return el.tagName.toLowerCase()==name.toLowerCase()},_xmlGenerator:null,_makeGenerator:function(){var doc;if(document.implementation.createDocument===undefined||document.implementation.createDocument&&document.documentMode&&document.documentMode<10){doc=this._getIEXmlDom();doc.appendChild(doc.createElement("strophe"))}else{doc=document.implementation.createDocument("jabber:client","strophe",null)}return doc},xmlGenerator:function(){if(!Strophe._xmlGenerator){Strophe._xmlGenerator=Strophe._makeGenerator()}return Strophe._xmlGenerator},_getIEXmlDom:function(){var doc=null;var docStrings=["Msxml2.DOMDocument.6.0","Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument","MSXML.DOMDocument","Microsoft.XMLDOM"];for(var d=0;d<docStrings.length;d++){if(doc===null){try{doc=new ActiveXObject(docStrings[d])}catch(e){doc=null}}else{break}}return doc},xmlElement:function(name){if(!name){return null}var node=Strophe.xmlGenerator().createElement(name);var a,i,k;for(a=1;a<arguments.length;a++){if(!arguments[a]){continue}if(typeof(arguments[a])=="string"||typeof(arguments[a])=="number"){node.appendChild(Strophe.xmlTextNode(arguments[a]))}else{if(typeof(arguments[a])=="object"&&typeof(arguments[a].sort)=="function"){for(i=0;i<arguments[a].length;i++){if(typeof(arguments[a][i])=="object"&&typeof(arguments[a][i].sort)=="function"){node.setAttribute(arguments[a][i][0],arguments[a][i][1])}}}else{if(typeof(arguments[a])=="object"){for(k in arguments[a]){if(arguments[a].hasOwnProperty(k)){node.setAttribute(k,arguments[a][k])}}}}}}return node},xmlescape:function(text){text=text.replace(/\&/g,"&amp;");text=text.replace(/</g,"&lt;");text=text.replace(/>/g,"&gt;");text=text.replace(/'/g,"&apos;");text=text.replace(/"/g,"&quot;");return text},xmlTextNode:function(text){return Strophe.xmlGenerator().createTextNode(text)},xmlHtmlNode:function(html){if(window.DOMParser){parser=new DOMParser();node=parser.parseFromString(html,"text/xml")}else{node=new ActiveXObject("Microsoft.XMLDOM");node.async="false";node.loadXML(html)}return node},getText:function(elem){if(!elem){return null}var str="";if(elem.childNodes.length===0&&elem.nodeType==Strophe.ElementType.TEXT){str+=elem.nodeValue}for(var i=0;i<elem.childNodes.length;i++){if(elem.childNodes[i].nodeType==Strophe.ElementType.TEXT){str+=elem.childNodes[i].nodeValue}}return Strophe.xmlescape(str)},copyElement:function(elem){var i,el;if(elem.nodeType==Strophe.ElementType.NORMAL){el=Strophe.xmlElement(elem.tagName);for(i=0;i<elem.attributes.length;i++){el.setAttribute(elem.attributes[i].nodeName.toLowerCase(),elem.attributes[i].value)}for(i=0;i<elem.childNodes.length;i++){el.appendChild(Strophe.copyElement(elem.childNodes[i]))}}else{if(elem.nodeType==Strophe.ElementType.TEXT){el=Strophe.xmlGenerator().createTextNode(elem.nodeValue)}}return el},createHtml:function(elem){var i,el,j,tag,attribute,value,css,cssAttrs,attr,cssName,cssValue,children,child;if(elem.nodeType==Strophe.ElementType.NORMAL){tag=elem.nodeName.toLowerCase();if(Strophe.XHTML.validTag(tag)){try{el=Strophe.xmlElement(tag);for(i=0;i<Strophe.XHTML.attributes[tag].length;i++){attribute=Strophe.XHTML.attributes[tag][i];value=elem.getAttribute(attribute);if(typeof value=="undefined"||value===null||value===""||value===false||value===0){continue}if(attribute=="style"&&typeof value=="object"){if(typeof value.cssText!="undefined"){value=value.cssText}}if(attribute=="style"){css=[];cssAttrs=value.split(";");for(j=0;j<cssAttrs.length;j++){attr=cssAttrs[j].split(":");cssName=attr[0].replace(/^\s*/,"").replace(/\s*$/,"").toLowerCase();if(Strophe.XHTML.validCSS(cssName)){cssValue=attr[1].replace(/^\s*/,"").replace(/\s*$/,"");css.push(cssName+": "+cssValue)}}if(css.length>0){value=css.join("; ");el.setAttribute(attribute,value)}}else{el.setAttribute(attribute,value)}}for(i=0;i<elem.childNodes.length;i++){el.appendChild(Strophe.createHtml(elem.childNodes[i]))}}catch(e){el=Strophe.xmlTextNode("")}}else{el=Strophe.xmlGenerator().createDocumentFragment();for(i=0;i<elem.childNodes.length;i++){el.appendChild(Strophe.createHtml(elem.childNodes[i]))}}}else{if(elem.nodeType==Strophe.ElementType.FRAGMENT){el=Strophe.xmlGenerator().createDocumentFragment();for(i=0;i<elem.childNodes.length;i++){el.appendChild(Strophe.createHtml(elem.childNodes[i]))}}else{if(elem.nodeType==Strophe.ElementType.TEXT){el=Strophe.xmlTextNode(elem.nodeValue)}}}return el},escapeNode:function(node){return node.replace(/^\s+|\s+$/g,"").replace(/\\/g,"\\5c").replace(/ /g,"\\20").replace(/\"/g,"\\22").replace(/\&/g,"\\26").replace(/\'/g,"\\27").replace(/\//g,"\\2f").replace(/:/g,"\\3a").replace(/</g,"\\3c").replace(/>/g,"\\3e").replace(/@/g,"\\40")},unescapeNode:function(node){return node.replace(/\\20/g," ").replace(/\\22/g,'"').replace(/\\26/g,"&").replace(/\\27/g,"'").replace(/\\2f/g,"/").replace(/\\3a/g,":").replace(/\\3c/g,"<").replace(/\\3e/g,">").replace(/\\40/g,"@").replace(/\\5c/g,"\\")},getNodeFromJid:function(jid){if(jid.indexOf("@")<0){return null}return jid.split("@")[0]},getDomainFromJid:function(jid){var bare=Strophe.getBareJidFromJid(jid);if(bare.indexOf("@")<0){return bare}else{var parts=bare.split("@");parts.splice(0,1);return parts.join("@")}},getResourceFromJid:function(jid){var s=jid.split("/");if(s.length<2){return null}s.splice(0,1);return s.join("/")},getBareJidFromJid:function(jid){return jid?jid.split("/")[0]:null},log:function(level,msg){return},debug:function(msg){this.log(this.LogLevel.DEBUG,msg)},info:function(msg){this.log(this.LogLevel.INFO,msg)},warn:function(msg){this.log(this.LogLevel.WARN,msg)},error:function(msg){this.log(this.LogLevel.ERROR,msg)},fatal:function(msg){this.log(this.LogLevel.FATAL,msg)},serialize:function(elem){var result;if(!elem){return null}if(typeof(elem.tree)==="function"){elem=elem.tree()}var nodeName=elem.nodeName;var i,child;if(elem.getAttribute("_realname")){nodeName=elem.getAttribute("_realname")}result="<"+nodeName;for(i=0;i<elem.attributes.length;i++){if(elem.attributes[i].nodeName!="_realname"){result+=" "+elem.attributes[i].nodeName.toLowerCase()+"='"+elem.attributes[i].value.replace(/&/g,"&amp;").replace(/\'/g,"&apos;").replace(/>/g,"&gt;").replace(/</g,"&lt;")+"'"}}if(elem.childNodes.length>0){result+=">";for(i=0;i<elem.childNodes.length;i++){child=elem.childNodes[i];switch(child.nodeType){case Strophe.ElementType.NORMAL:result+=Strophe.serialize(child);break;case Strophe.ElementType.TEXT:result+=Strophe.xmlescape(child.nodeValue);break;case Strophe.ElementType.CDATA:result+="<![CDATA["+child.nodeValue+"]]>"}}result+="</"+nodeName+">"}else{result+="/>"}return result},_requestId:0,_connectionPlugins:{},addConnectionPlugin:function(name,ptype){Strophe._connectionPlugins[name]=ptype}};Strophe.Builder=function(name,attrs){if(name=="presence"||name=="message"||name=="iq"){if(attrs&&!attrs.xmlns){attrs.xmlns=Strophe.NS.CLIENT}else{if(!attrs){attrs={xmlns:Strophe.NS.CLIENT}}}}this.nodeTree=Strophe.xmlElement(name,attrs);this.node=this.nodeTree};Strophe.Builder.prototype={tree:function(){return this.nodeTree},toString:function(){return Strophe.serialize(this.nodeTree)},up:function(){this.node=this.node.parentNode;return this},attrs:function(moreattrs){for(var k in moreattrs){if(moreattrs.hasOwnProperty(k)){this.node.setAttribute(k,moreattrs[k])}}return this},c:function(name,attrs,text){var child=Strophe.xmlElement(name,attrs,text);this.node.appendChild(child);if(!text){this.node=child}return this},cnode:function(elem){var impNode;var xmlGen=Strophe.xmlGenerator();try{impNode=(xmlGen.importNode!==undefined)}catch(e){impNode=false}var newElem=impNode?xmlGen.importNode(elem,true):Strophe.copyElement(elem);this.node.appendChild(newElem);this.node=newElem;return this},t:function(text){var child=Strophe.xmlTextNode(text);this.node.appendChild(child);return this},h:function(html){var fragment=document.createElement("body");fragment.innerHTML=html;var xhtml=Strophe.createHtml(fragment);while(xhtml.childNodes.length>0){this.node.appendChild(xhtml.childNodes[0])}return this}};Strophe.Handler=function(handler,ns,name,type,id,from,options){this.handler=handler;this.ns=ns;this.name=name;this.type=type;this.id=id;this.options=options||{matchBare:false};if(!this.options.matchBare){this.options.matchBare=false}if(this.options.matchBare){this.from=from?Strophe.getBareJidFromJid(from):null}else{this.from=from}this.user=true};Strophe.Handler.prototype={isMatch:function(elem){var nsMatch;var from=null;if(this.options.matchBare){from=Strophe.getBareJidFromJid(elem.getAttribute("from"))}else{from=elem.getAttribute("from")}nsMatch=false;if(!this.ns){nsMatch=true}else{var that=this;Strophe.forEachChild(elem,null,function(elem){if(elem.getAttribute("xmlns")==that.ns){nsMatch=true}});nsMatch=nsMatch||elem.getAttribute("xmlns")==this.ns}if(nsMatch&&(!this.name||Strophe.isTagEqual(elem,this.name))&&(!this.type||elem.getAttribute("type")==this.type)&&(!this.id||elem.getAttribute("id")==this.id)&&(!this.from||from==this.from)){return true}return false},run:function(elem){var result=null;try{result=this.handler(elem)}catch(e){if(e.sourceURL){Strophe.fatal("error: "+this.handler+" "+e.sourceURL+":"+e.line+" - "+e.name+": "+e.message)}else{if(e.fileName){if(typeof(console)!="undefined"){console.trace();console.error(this.handler," - error - ",e,e.message)}Strophe.fatal("error: "+this.handler+" "+e.fileName+":"+e.lineNumber+" - "+e.name+": "+e.message)}else{Strophe.fatal("error: "+e.message+"\n"+e.stack)}}throw e}return result},toString:function(){return"{Handler: "+this.handler+"("+this.name+","+this.id+","+this.ns+")}"}};Strophe.TimedHandler=function(period,handler){this.period=period;this.handler=handler;this.lastCalled=new Date().getTime();this.user=true};Strophe.TimedHandler.prototype={run:function(){this.lastCalled=new Date().getTime();return this.handler()},reset:function(){this.lastCalled=new Date().getTime()},toString:function(){return"{TimedHandler: "+this.handler+"("+this.period+")}"}};Strophe.Connection=function(service,options){this.service=service;this._options=options||{};var proto=this._options.protocol||"";if(service.indexOf("ws:")===0||service.indexOf("wss:")===0||proto.indexOf("ws")===0){this._proto=new Strophe.Websocket(this)}else{this._proto=new Strophe.Bosh(this)}this.jid="";this.domain=null;this.features=null;this._sasl_data={};this.do_session=false;this.do_bind=false;this.timedHandlers=[];this.handlers=[];this.removeTimeds=[];this.removeHandlers=[];this.addTimeds=[];this.addHandlers=[];this._authentication={};this._idleTimeout=null;this._disconnectTimeout=null;this.do_authentication=true;this.authenticated=false;this.disconnecting=false;this.connected=false;this.errors=0;this.paused=false;this._data=[];this._uniqueId=0;this._sasl_success_handler=null;this._sasl_failure_handler=null;this._sasl_challenge_handler=null;this.maxRetries=5;this._idleTimeout=setTimeout(this._onIdle.bind(this),100);for(var k in Strophe._connectionPlugins){if(Strophe._connectionPlugins.hasOwnProperty(k)){var ptype=Strophe._connectionPlugins[k];var F=function(){};F.prototype=ptype;this[k]=new F();this[k].init(this)}}};Strophe.Connection.prototype={reset:function(){this._proto._reset();this.do_session=false;this.do_bind=false;this.timedHandlers=[];this.handlers=[];this.removeTimeds=[];this.removeHandlers=[];this.addTimeds=[];this.addHandlers=[];this._authentication={};this.authenticated=false;this.disconnecting=false;this.connected=false;this.errors=0;this._requests=[];this._uniqueId=0},pause:function(){this.paused=true},resume:function(){this.paused=false},getUniqueId:function(suffix){if(typeof(suffix)=="string"||typeof(suffix)=="number"){return ++this._uniqueId+":"+suffix}else{return ++this._uniqueId+""}},connect:function(jid,pass,callback,wait,hold,route){this.jid=jid;this.authzid=Strophe.getBareJidFromJid(this.jid);this.authcid=Strophe.getNodeFromJid(this.jid);this.pass=pass;this.servtype="xmpp";this.connect_callback=callback;this.disconnecting=false;this.connected=false;this.authenticated=false;this.errors=0;this.domain=this.domain||Strophe.getDomainFromJid(this.jid);this._changeConnectStatus(Strophe.Status.CONNECTING,null);this._proto._connect(wait,hold,route)},attach:function(jid,sid,rid,callback,wait,hold,wind){this._proto.attach(jid,sid,rid,callback,wait,hold,wind)},xmlInput:function(elem){return},xmlOutput:function(elem){return},rawInput:function(data){return},rawOutput:function(data){return},send:function(elem){if(elem===null){return}if(typeof(elem.sort)==="function"){for(var i=0;i<elem.length;i++){this._queueData(elem[i])}}else{if(typeof(elem.tree)==="function"){this._queueData(elem.tree())}else{this._queueData(elem)}}this._proto._send()},flush:function(){clearTimeout(this._idleTimeout);this._onIdle()},sendIQ:function(elem,callback,errback,timeout){var timeoutHandler=null;var that=this;if(typeof(elem.tree)==="function"){elem=elem.tree()}var id=elem.getAttribute("id");if(!id){id=this.getUniqueId("sendIQ");elem.setAttribute("id",id)}var handler=this.addHandler(function(stanza){if(timeoutHandler){that.deleteTimedHandler(timeoutHandler)}var iqtype=stanza.getAttribute("type");if(iqtype=="result"){if(callback){callback(stanza)}}else{if(iqtype=="error"){if(errback){errback(stanza)}}else{throw {name:"StropheError",message:"Got bad IQ type of "+iqtype}}}},null,"iq",null,id);if(timeout){timeoutHandler=this.addTimedHandler(timeout,function(){that.deleteHandler(handler);if(errback){errback(null)}return false})}this.send(elem);return id},_queueData:function(element){if(element===null||!element.tagName||!element.childNodes){throw {name:"StropheError",message:"Cannot queue non-DOMElement."}}this._data.push(element)},_sendRestart:function(){this._data.push("restart");this._proto._sendRestart();this._idleTimeout=setTimeout(this._onIdle.bind(this),100)},addTimedHandler:function(period,handler){var thand=new Strophe.TimedHandler(period,handler);this.addTimeds.push(thand);return thand},deleteTimedHandler:function(handRef){this.removeTimeds.push(handRef)},addHandler:function(handler,ns,name,type,id,from,options){var hand=new Strophe.Handler(handler,ns,name,type,id,from,options);this.addHandlers.push(hand);return hand},deleteHandler:function(handRef){this.removeHandlers.push(handRef)},disconnect:function(reason){this._changeConnectStatus(Strophe.Status.DISCONNECTING,reason);Strophe.info("Disconnect was called because: "+reason);if(this.connected){var pres=false;this.disconnecting=true;if(this.authenticated){pres=$pres({xmlns:Strophe.NS.CLIENT,type:"unavailable"})}this._disconnectTimeout=this._addSysTimedHandler(3000,this._onDisconnectTimeout.bind(this));this._proto._disconnect(pres)}},_changeConnectStatus:function(status,condition){for(var k in Strophe._connectionPlugins){if(Strophe._connectionPlugins.hasOwnProperty(k)){var plugin=this[k];if(plugin.statusChanged){try{plugin.statusChanged(status,condition)}catch(err){Strophe.error(""+k+" plugin caused an exception changing status: "+err)}}}}if(this.connect_callback){try{this.connect_callback(status,condition)}catch(e){Strophe.error("User connection callback caused an exception: "+e)}}},_doDisconnect:function(){if(this._disconnectTimeout!==null){this.deleteTimedHandler(this._disconnectTimeout);this._disconnectTimeout=null}Strophe.info("_doDisconnect was called");this._proto._doDisconnect();this.authenticated=false;this.disconnecting=false;this.handlers=[];this.timedHandlers=[];this.removeTimeds=[];this.removeHandlers=[];this.addTimeds=[];this.addHandlers=[];this._changeConnectStatus(Strophe.Status.DISCONNECTED,null);this.connected=false},_dataRecv:function(req,raw){Strophe.info("_dataRecv called");var elem=this._proto._reqToData(req);if(elem===null){return}if(this.xmlInput!==Strophe.Connection.prototype.xmlInput){if(elem.nodeName===this._proto.strip&&elem.childNodes.length){this.xmlInput(elem.childNodes[0])}else{this.xmlInput(elem)}}if(this.rawInput!==Strophe.Connection.prototype.rawInput){if(raw){this.rawInput(raw)}else{this.rawInput(Strophe.serialize(elem))}}var i,hand;while(this.removeHandlers.length>0){hand=this.removeHandlers.pop();i=this.handlers.indexOf(hand);if(i>=0){this.handlers.splice(i,1)}}while(this.addHandlers.length>0){this.handlers.push(this.addHandlers.pop())}if(this.disconnecting&&this._proto._emptyQueue()){this._doDisconnect();return}var typ=elem.getAttribute("type");var cond,conflict;if(typ!==null&&typ=="terminate"){if(this.disconnecting){return}cond=elem.getAttribute("condition");conflict=elem.getElementsByTagName("conflict");if(cond!==null){if(cond=="remote-stream-error"&&conflict.length>0){cond="conflict"}this._changeConnectStatus(Strophe.Status.CONNFAIL,cond)}else{this._changeConnectStatus(Strophe.Status.CONNFAIL,"unknown")}this.disconnect("unknown stream-error");return}var that=this;Strophe.forEachChild(elem,null,function(child){var i,newList;newList=that.handlers;that.handlers=[];for(i=0;i<newList.length;i++){var hand=newList[i];try{if(hand.isMatch(child)&&(that.authenticated||!hand.user)){if(hand.run(child)){that.handlers.push(hand)}}else{that.handlers.push(hand)}}catch(e){Strophe.warn("Removing Strophe handlers due to uncaught exception: "+e.message)}}})},mechanisms:{},_connect_cb:function(req,_callback,raw){Strophe.info("_connect_cb was called");this.connected=true;var bodyWrap=this._proto._reqToData(req);if(!bodyWrap){return}if(this.xmlInput!==Strophe.Connection.prototype.xmlInput){if(bodyWrap.nodeName===this._proto.strip&&bodyWrap.childNodes.length){this.xmlInput(bodyWrap.childNodes[0])}else{this.xmlInput(bodyWrap)}}if(this.rawInput!==Strophe.Connection.prototype.rawInput){if(raw){this.rawInput(raw)}else{this.rawInput(Strophe.serialize(bodyWrap))}}var conncheck=this._proto._connect_cb(bodyWrap);if(conncheck===Strophe.Status.CONNFAIL){return}this._authentication.sasl_scram_sha1=false;this._authentication.sasl_plain=false;this._authentication.sasl_digest_md5=false;this._authentication.sasl_anonymous=false;this._authentication.legacy_auth=false;var hasFeatures=bodyWrap.getElementsByTagName("stream:features").length>0;if(!hasFeatures){hasFeatures=bodyWrap.getElementsByTagName("features").length>0}var mechanisms=bodyWrap.getElementsByTagName("mechanism");var matched=[];var i,mech,auth_str,hashed_auth_str,found_authentication=false;if(!hasFeatures){this._proto._no_auth_received(_callback);return}if(mechanisms.length>0){for(i=0;i<mechanisms.length;i++){mech=Strophe.getText(mechanisms[i]);if(this.mechanisms[mech]){matched.push(this.mechanisms[mech])}}}this._authentication.legacy_auth=bodyWrap.getElementsByTagName("auth").length>0;found_authentication=this._authentication.legacy_auth||matched.length>0;if(!found_authentication){this._proto._no_auth_received(_callback);return}if(this.do_authentication!==false){this.authenticate(matched)}},authenticate:function(matched){var i;for(i=0;i<matched.length-1;++i){var higher=i;for(var j=i+1;j<matched.length;++j){if(matched[j].prototype.priority>matched[higher].prototype.priority){higher=j}}if(higher!=i){var swap=matched[i];matched[i]=matched[higher];matched[higher]=swap}}var mechanism_found=false;for(i=0;i<matched.length;++i){if(!matched[i].test(this)){continue}this._sasl_success_handler=this._addSysHandler(this._sasl_success_cb.bind(this),null,"success",null,null);this._sasl_failure_handler=this._addSysHandler(this._sasl_failure_cb.bind(this),null,"failure",null,null);this._sasl_challenge_handler=this._addSysHandler(this._sasl_challenge_cb.bind(this),null,"challenge",null,null);this._sasl_mechanism=new matched[i]();this._sasl_mechanism.onStart(this);var request_auth_exchange=$build("auth",{xmlns:Strophe.NS.SASL,mechanism:this._sasl_mechanism.name});if(this._sasl_mechanism.isClientFirst){var response=this._sasl_mechanism.onChallenge(this,null);request_auth_exchange.t(Base64.encode(response))}this.send(request_auth_exchange.tree());mechanism_found=true;break}if(!mechanism_found){if(Strophe.getNodeFromJid(this.jid)===null){this._changeConnectStatus(Strophe.Status.CONNFAIL,"x-strophe-bad-non-anon-jid");this.disconnect("x-strophe-bad-non-anon-jid")}else{this._changeConnectStatus(Strophe.Status.AUTHENTICATING,null);this._addSysHandler(this._auth1_cb.bind(this),null,null,null,"_auth_1");this.send($iq({type:"get",to:this.domain,id:"_auth_1"}).c("query",{xmlns:Strophe.NS.AUTH}).c("username",{}).t(Strophe.getNodeFromJid(this.jid)).tree())}}},_sasl_challenge_cb:function(elem){var challenge=Base64.decode(Strophe.getText(elem));var response=this._sasl_mechanism.onChallenge(this,challenge);var stanza=$build("response",{xmlns:Strophe.NS.SASL});if(response!==""){stanza.t(Base64.encode(response))}this.send(stanza.tree());return true},_auth1_cb:function(elem){var iq=$iq({type:"set",id:"_auth_2"}).c("query",{xmlns:Strophe.NS.AUTH}).c("username",{}).t(Strophe.getNodeFromJid(this.jid)).up().c("password").t(this.pass);if(!Strophe.getResourceFromJid(this.jid)){this.jid=Strophe.getBareJidFromJid(this.jid)+"/strophe"}iq.up().c("resource",{}).t(Strophe.getResourceFromJid(this.jid));this._addSysHandler(this._auth2_cb.bind(this),null,null,null,"_auth_2");this.send(iq.tree());return false},_sasl_success_cb:function(elem){if(this._sasl_data["server-signature"]){var serverSignature;var success=Base64.decode(Strophe.getText(elem));var attribMatch=/([a-z]+)=([^,]+)(,|$)/;matches=success.match(attribMatch);if(matches[1]=="v"){serverSignature=matches[2]}if(serverSignature!=this._sasl_data["server-signature"]){this.deleteHandler(this._sasl_failure_handler);this._sasl_failure_handler=null;if(this._sasl_challenge_handler){this.deleteHandler(this._sasl_challenge_handler);this._sasl_challenge_handler=null}this._sasl_data={};return this._sasl_failure_cb(null)}}Strophe.info("SASL authentication succeeded.");if(this._sasl_mechanism){this._sasl_mechanism.onSuccess()}this.deleteHandler(this._sasl_failure_handler);this._sasl_failure_handler=null;if(this._sasl_challenge_handler){this.deleteHandler(this._sasl_challenge_handler);this._sasl_challenge_handler=null}this._addSysHandler(this._sasl_auth1_cb.bind(this),null,"stream:features",null,null);this._sendRestart();return false},_sasl_auth1_cb:function(elem){this.features=elem;var i,child;for(i=0;i<elem.childNodes.length;i++){child=elem.childNodes[i];if(child.nodeName=="bind"){this.do_bind=true}if(child.nodeName=="session"){this.do_session=true}}if(!this.do_bind){this._changeConnectStatus(Strophe.Status.AUTHFAIL,null);return false}else{this._addSysHandler(this._sasl_bind_cb.bind(this),null,null,null,"_bind_auth_2");var resource=Strophe.getResourceFromJid(this.jid);if(resource){this.send($iq({type:"set",id:"_bind_auth_2"}).c("bind",{xmlns:Strophe.NS.BIND}).c("resource",{}).t(resource).tree())}else{this.send($iq({type:"set",id:"_bind_auth_2"}).c("bind",{xmlns:Strophe.NS.BIND}).tree())}}return false},_sasl_bind_cb:function(elem){if(elem.getAttribute("type")=="error"){Strophe.info("SASL binding failed.");var conflict=elem.getElementsByTagName("conflict"),condition;if(conflict.length>0){condition="conflict"}this._changeConnectStatus(Strophe.Status.AUTHFAIL,condition);return false}var bind=elem.getElementsByTagName("bind");var jidNode;if(bind.length>0){jidNode=bind[0].getElementsByTagName("jid");if(jidNode.length>0){this.jid=Strophe.getText(jidNode[0]);if(this.do_session){this._addSysHandler(this._sasl_session_cb.bind(this),null,null,null,"_session_auth_2");this.send($iq({type:"set",id:"_session_auth_2"}).c("session",{xmlns:Strophe.NS.SESSION}).tree())}else{this.authenticated=true;this._changeConnectStatus(Strophe.Status.CONNECTED,null)}}}else{Strophe.info("SASL binding failed.");this._changeConnectStatus(Strophe.Status.AUTHFAIL,null);return false}},_sasl_session_cb:function(elem){if(elem.getAttribute("type")=="result"){this.authenticated=true;this._changeConnectStatus(Strophe.Status.CONNECTED,null)}else{if(elem.getAttribute("type")=="error"){Strophe.info("Session creation failed.");this._changeConnectStatus(Strophe.Status.AUTHFAIL,null);return false}}return false},_sasl_failure_cb:function(elem){if(this._sasl_success_handler){this.deleteHandler(this._sasl_success_handler);this._sasl_success_handler=null}if(this._sasl_challenge_handler){this.deleteHandler(this._sasl_challenge_handler);this._sasl_challenge_handler=null}if(this._sasl_mechanism){this._sasl_mechanism.onFailure()}this._changeConnectStatus(Strophe.Status.AUTHFAIL,null);return false},_auth2_cb:function(elem){if(elem.getAttribute("type")=="result"){this.authenticated=true;this._changeConnectStatus(Strophe.Status.CONNECTED,null)}else{if(elem.getAttribute("type")=="error"){this._changeConnectStatus(Strophe.Status.AUTHFAIL,null);this.disconnect("authentication failed")}}return false},_addSysTimedHandler:function(period,handler){var thand=new Strophe.TimedHandler(period,handler);thand.user=false;this.addTimeds.push(thand);return thand},_addSysHandler:function(handler,ns,name,type,id){var hand=new Strophe.Handler(handler,ns,name,type,id);hand.user=false;this.addHandlers.push(hand);return hand},_onDisconnectTimeout:function(){Strophe.info("_onDisconnectTimeout was called");this._proto._onDisconnectTimeout();this._doDisconnect();return false},_onIdle:function(){var i,thand,since,newList;while(this.addTimeds.length>0){this.timedHandlers.push(this.addTimeds.pop())}while(this.removeTimeds.length>0){thand=this.removeTimeds.pop();i=this.timedHandlers.indexOf(thand);if(i>=0){this.timedHandlers.splice(i,1)}}var now=new Date().getTime();newList=[];for(i=0;i<this.timedHandlers.length;i++){thand=this.timedHandlers[i];if(this.authenticated||!thand.user){since=thand.lastCalled+thand.period;if(since-now<=0){if(thand.run()){newList.push(thand)}}else{newList.push(thand)}}}this.timedHandlers=newList;var body,time_elapsed;clearTimeout(this._idleTimeout);this._proto._onIdle();if(this.connected){this._idleTimeout=setTimeout(this._onIdle.bind(this),100)}}};if(callback){callback(Strophe,$build,$msg,$iq,$pres)}Strophe.SASLMechanism=function(name,isClientFirst,priority){this.name=name;this.isClientFirst=isClientFirst;this.priority=priority};Strophe.SASLMechanism.prototype={_sasl_data:{},test:function(connection){return true},onStart:function(connection){this._connection=connection},onChallenge:function(connection,challenge){throw new Error("You should implement challenge handling!")},onFailure:function(){this._connection=null},onSuccess:function(){this._connection=null}};Strophe.SASLAnonymous=function(){};Strophe.SASLAnonymous.prototype=new Strophe.SASLMechanism("ANONYMOUS",false,10);Strophe.SASLAnonymous.test=function(connection){return connection.authcid===null};Strophe.Connection.prototype.mechanisms[Strophe.SASLAnonymous.prototype.name]=Strophe.SASLAnonymous;Strophe.SASLPlain=function(){};Strophe.SASLPlain.prototype=new Strophe.SASLMechanism("PLAIN",true,20);Strophe.SASLPlain.test=function(connection){return connection.authcid!==null};Strophe.SASLPlain.prototype.onChallenge=function(connection,challenge){var auth_str=connection.authzid;auth_str=auth_str+"\u0000";auth_str=auth_str+connection.authcid;auth_str=auth_str+"\u0000";auth_str=auth_str+connection.pass;return auth_str};Strophe.Connection.prototype.mechanisms[Strophe.SASLPlain.prototype.name]=Strophe.SASLPlain;Strophe.SASLSHA1=function(){};Strophe.SASLSHA1.prototype=new Strophe.SASLMechanism("SCRAM-SHA-1",true,40);Strophe.SASLSHA1.test=function(connection){return connection.authcid!==null};Strophe.SASLSHA1.prototype.onChallenge=function(connection,challenge,test_cnonce){var cnonce=test_cnonce||MD5.hexdigest(Math.random()*1234567890);var auth_str="n="+connection.authcid;auth_str+=",r=";auth_str+=cnonce;this._sasl_data.cnonce=cnonce;this._sasl_data["client-first-message-bare"]=auth_str;auth_str="n,,"+auth_str;this.onChallenge=function(connection,challenge){var nonce,salt,iter,Hi,U,U_old;var clientKey,serverKey,clientSignature;var responseText="c=biws,";var authMessage=this._sasl_data["client-first-message-bare"]+","+challenge+",";var cnonce=this._sasl_data.cnonce;var attribMatch=/([a-z]+)=([^,]+)(,|$)/;while(challenge.match(attribMatch)){matches=challenge.match(attribMatch);challenge=challenge.replace(matches[0],"");switch(matches[1]){case"r":nonce=matches[2];break;case"s":salt=matches[2];break;case"i":iter=matches[2];break}}if(nonce.substr(0,cnonce.length)!==cnonce){this._sasl_data={};return connection._sasl_failure_cb()}responseText+="r="+nonce;authMessage+=responseText;salt=Base64.decode(salt);salt+="\x00\x00\x00\x01";Hi=U_old=core_hmac_sha1(connection.pass,salt);for(i=1;i<iter;i++){U=core_hmac_sha1(connection.pass,binb2str(U_old));for(k=0;k<5;k++){Hi[k]^=U[k]}U_old=U}Hi=binb2str(Hi);clientKey=core_hmac_sha1(Hi,"Client Key");serverKey=str_hmac_sha1(Hi,"Server Key");clientSignature=core_hmac_sha1(str_sha1(binb2str(clientKey)),authMessage);connection._sasl_data["server-signature"]=b64_hmac_sha1(serverKey,authMessage);for(k=0;k<5;k++){clientKey[k]^=clientSignature[k]}responseText+=",p="+Base64.encode(binb2str(clientKey));return responseText}.bind(this);return auth_str};Strophe.Connection.prototype.mechanisms[Strophe.SASLSHA1.prototype.name]=Strophe.SASLSHA1;Strophe.SASLMD5=function(){};Strophe.SASLMD5.prototype=new Strophe.SASLMechanism("DIGEST-MD5",false,30);Strophe.SASLMD5.test=function(connection){return connection.authcid!==null};Strophe.SASLMD5.prototype._quote=function(str){return'"'+str.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+'"'};Strophe.SASLMD5.prototype.onChallenge=function(connection,challenge,test_cnonce){var attribMatch=/([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;var cnonce=test_cnonce||MD5.hexdigest(""+(Math.random()*1234567890));var realm="";var host=null;var nonce="";var qop="";var matches;while(challenge.match(attribMatch)){matches=challenge.match(attribMatch);challenge=challenge.replace(matches[0],"");matches[2]=matches[2].replace(/^"(.+)"$/,"$1");switch(matches[1]){case"realm":realm=matches[2];break;case"nonce":nonce=matches[2];break;case"qop":qop=matches[2];break;case"host":host=matches[2];break}}var digest_uri=connection.servtype+"/"+connection.domain;if(host!==null){digest_uri=digest_uri+"/"+host}var A1=MD5.hash(connection.authcid+":"+realm+":"+this._connection.pass)+":"+nonce+":"+cnonce;var A2="AUTHENTICATE:"+digest_uri;var responseText="";responseText+="charset=utf-8,";responseText+="username="+this._quote(connection.authcid)+",";responseText+="realm="+this._quote(realm)+",";responseText+="nonce="+this._quote(nonce)+",";responseText+="nc=00000001,";responseText+="cnonce="+this._quote(cnonce)+",";responseText+="digest-uri="+this._quote(digest_uri)+",";responseText+="response="+MD5.hexdigest(MD5.hexdigest(A1)+":"+nonce+":00000001:"+cnonce+":auth:"+MD5.hexdigest(A2))+",";responseText+="qop=auth";this.onChallenge=function(connection,challenge){return""}.bind(this);return responseText};Strophe.Connection.prototype.mechanisms[Strophe.SASLMD5.prototype.name]=Strophe.SASLMD5})(function(){window.Strophe=arguments[0];window.$build=arguments[1];window.$msg=arguments[2];window.$iq=arguments[3];window.$pres=arguments[4]});Strophe.Request=function(elem,func,rid,sends){this.id=++Strophe._requestId;this.xmlData=elem;this.data=Strophe.serialize(elem);this.origFunc=func;this.func=func;this.rid=rid;this.date=NaN;this.sends=sends||0;this.abort=false;this.dead=null;this.age=function(){if(!this.date){return 0}var now=new Date();return(now-this.date)/1000};this.timeDead=function(){if(!this.dead){return 0}var now=new Date();return(now-this.dead)/1000};this.xhr=this._newXHR()};Strophe.Request.prototype={getResponse:function(){var node=null;if(this.xhr.responseXML&&this.xhr.responseXML.documentElement){node=this.xhr.responseXML.documentElement;if(node.tagName=="parsererror"){Strophe.error("invalid response received");Strophe.error("responseText: "+this.xhr.responseText);Strophe.error("responseXML: "+Strophe.serialize(this.xhr.responseXML));throw"parsererror"}}else{if(this.xhr.responseText){Strophe.error("invalid response received");Strophe.error("responseText: "+this.xhr.responseText);Strophe.error("responseXML: "+Strophe.serialize(this.xhr.responseXML))}}return node},_newXHR:function(){var xhr=null;if(window.XMLHttpRequest){xhr=new XMLHttpRequest();if(xhr.overrideMimeType){xhr.overrideMimeType("text/xml")}}else{if(window.ActiveXObject){xhr=new ActiveXObject("Microsoft.XMLHTTP")}}xhr.onreadystatechange=this.func.bind(null,this);return xhr}};Strophe.Bosh=function(connection){this._conn=connection;this.rid=Math.floor(Math.random()*4294967295);this.sid=null;this.hold=1;this.wait=60;this.window=5;this._requests=[]};Strophe.Bosh.prototype={strip:null,_buildBody:function(){var bodyWrap=$build("body",{rid:this.rid++,xmlns:Strophe.NS.HTTPBIND});if(this.sid!==null){bodyWrap.attrs({sid:this.sid})}return bodyWrap},_reset:function(){this.rid=Math.floor(Math.random()*4294967295);this.sid=null},_connect:function(wait,hold,route){this.wait=wait||this.wait;this.hold=hold||this.hold;var body=this._buildBody().attrs({to:this._conn.domain,"xml:lang":"en",wait:this.wait,hold:this.hold,content:"text/xml; charset=utf-8",ver:"1.6","xmpp:version":"1.0","xmlns:xmpp":Strophe.NS.BOSH});if(route){body.attrs({route:route})}var _connect_cb=this._conn._connect_cb;this._requests.push(new Strophe.Request(body.tree(),this._onRequestStateChange.bind(this,_connect_cb.bind(this._conn)),body.tree().getAttribute("rid")));this._throttledRequestHandler()},_attach:function(jid,sid,rid,callback,wait,hold,wind){this._conn.jid=jid;this.sid=sid;this.rid=rid;this._conn.connect_callback=callback;this._conn.domain=Strophe.getDomainFromJid(this._conn.jid);this._conn.authenticated=true;this._conn.connected=true;this.wait=wait||this.wait;this.hold=hold||this.hold;this.window=wind||this.window;this._conn._changeConnectStatus(Strophe.Status.ATTACHED,null)},_connect_cb:function(bodyWrap){var typ=bodyWrap.getAttribute("type");var cond,conflict;if(typ!==null&&typ=="terminate"){Strophe.error("BOSH-Connection failed: "+cond);cond=bodyWrap.getAttribute("condition");conflict=bodyWrap.getElementsByTagName("conflict");if(cond!==null){if(cond=="remote-stream-error"&&conflict.length>0){cond="conflict"}this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,cond)}else{this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,"unknown")}this._conn._doDisconnect();return Strophe.Status.CONNFAIL}if(!this.sid){this.sid=bodyWrap.getAttribute("sid")}var wind=bodyWrap.getAttribute("requests");if(wind){this.window=parseInt(wind,10)}var hold=bodyWrap.getAttribute("hold");if(hold){this.hold=parseInt(hold,10)}var wait=bodyWrap.getAttribute("wait");if(wait){this.wait=parseInt(wait,10)}},_disconnect:function(pres){this._sendTerminate(pres)},_doDisconnect:function(){this.sid=null;this.rid=Math.floor(Math.random()*4294967295)},_emptyQueue:function(){return this._requests.length===0},_hitError:function(reqStatus){this.errors++;Strophe.warn("request errored, status: "+reqStatus+", number of errors: "+this.errors);if(this.errors>4){this._onDisconnectTimeout()}},_no_auth_received:function(_callback){if(_callback){_callback=_callback.bind(this._conn)}else{_callback=this._conn._connect_cb.bind(this._conn)}var body=this._buildBody();this._requests.push(new Strophe.Request(body.tree(),this._onRequestStateChange.bind(this,_callback.bind(this._conn)),body.tree().getAttribute("rid")));this._throttledRequestHandler()},_onDisconnectTimeout:function(){var req;while(this._requests.length>0){req=this._requests.pop();req.abort=true;req.xhr.abort();req.xhr.onreadystatechange=function(){}}},_onIdle:function(){var data=this._conn._data;if(this._conn.authenticated&&this._requests.length===0&&data.length===0&&!this._conn.disconnecting){Strophe.info("no requests during idle cycle, sending blank request");data.push(null)}if(this._requests.length<2&&data.length>0&&!this._conn.paused){body=this._buildBody();for(i=0;i<data.length;i++){if(data[i]!==null){if(data[i]==="restart"){body.attrs({to:this._conn.domain,"xml:lang":"en","xmpp:restart":"true","xmlns:xmpp":Strophe.NS.BOSH})}else{body.cnode(data[i]).up()}}}delete this._conn._data;this._conn._data=[];this._requests.push(new Strophe.Request(body.tree(),this._onRequestStateChange.bind(this,this._conn._dataRecv.bind(this._conn)),body.tree().getAttribute("rid")));this._processRequest(this._requests.length-1)}if(this._requests.length>0){time_elapsed=this._requests[0].age();if(this._requests[0].dead!==null){if(this._requests[0].timeDead()>Math.floor(Strophe.SECONDARY_TIMEOUT*this.wait)){this._throttledRequestHandler()}}if(time_elapsed>Math.floor(Strophe.TIMEOUT*this.wait)){Strophe.warn("Request "+this._requests[0].id+" timed out, over "+Math.floor(Strophe.TIMEOUT*this.wait)+" seconds since last activity");this._throttledRequestHandler()}}},_onRequestStateChange:function(func,req){Strophe.debug("request id "+req.id+"."+req.sends+" state changed to "+req.xhr.readyState);if(req.abort){req.abort=false;return}var reqStatus;if(req.xhr.readyState==4){reqStatus=0;try{reqStatus=req.xhr.status}catch(e){}if(typeof(reqStatus)=="undefined"){reqStatus=0}if(this.disconnecting){if(reqStatus>=400){this._hitError(reqStatus);return}}var reqIs0=(this._requests[0]==req);var reqIs1=(this._requests[1]==req);if((reqStatus>0&&reqStatus<500)||req.sends>5){this._removeRequest(req);Strophe.debug("request id "+req.id+" should now be removed")}if(reqStatus==200){if(reqIs1||(reqIs0&&this._requests.length>0&&this._requests[0].age()>Math.floor(Strophe.SECONDARY_TIMEOUT*this.wait))){this._restartRequest(0)}Strophe.debug("request id "+req.id+"."+req.sends+" got 200");func(req);this.errors=0}else{Strophe.error("request id "+req.id+"."+req.sends+" error "+reqStatus+" happened");if(reqStatus===0||(reqStatus>=400&&reqStatus<600)||reqStatus>=12000){this._hitError(reqStatus);if(reqStatus>=400&&reqStatus<500){this._conn._changeConnectStatus(Strophe.Status.DISCONNECTING,null);this._conn._doDisconnect()}}}if(!((reqStatus>0&&reqStatus<500)||req.sends>5)){this._throttledRequestHandler()}}},_processRequest:function(i){var self=this;var req=this._requests[i];var reqStatus=-1;try{if(req.xhr.readyState==4){reqStatus=req.xhr.status}}catch(e){Strophe.error("caught an error in _requests["+i+"], reqStatus: "+reqStatus)}if(typeof(reqStatus)=="undefined"){reqStatus=-1}if(req.sends>this.maxRetries){this._onDisconnectTimeout();return}var time_elapsed=req.age();var primaryTimeout=(!isNaN(time_elapsed)&&time_elapsed>Math.floor(Strophe.TIMEOUT*this.wait));var secondaryTimeout=(req.dead!==null&&req.timeDead()>Math.floor(Strophe.SECONDARY_TIMEOUT*this.wait));var requestCompletedWithServerError=(req.xhr.readyState==4&&(reqStatus<1||reqStatus>=500));if(primaryTimeout||secondaryTimeout||requestCompletedWithServerError){if(secondaryTimeout){Strophe.error("Request "+this._requests[i].id+" timed out (secondary), restarting")}req.abort=true;req.xhr.abort();req.xhr.onreadystatechange=function(){};this._requests[i]=new Strophe.Request(req.xmlData,req.origFunc,req.rid,req.sends);req=this._requests[i]}if(req.xhr.readyState===0){Strophe.debug("request id "+req.id+"."+req.sends+" posting");try{req.xhr.open("POST",this._conn.service,true)}catch(e2){Strophe.error("XHR open failed.");if(!this._conn.connected){this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,"bad-service")}this._conn.disconnect();return}var sendFunc=function(){req.date=new Date();if(self._conn._options.customHeaders){var headers=self._conn._options.customHeaders;for(var header in headers){if(headers.hasOwnProperty(header)){req.xhr.setRequestHeader(header,headers[header])}}}req.xhr.send(req.data)};if(req.sends>1){var backoff=Math.min(Math.floor(Strophe.TIMEOUT*this.wait),Math.pow(req.sends,3))*1000;setTimeout(sendFunc,backoff)}else{sendFunc()}req.sends++;if(this._conn.xmlOutput!==Strophe.Connection.prototype.xmlOutput){if(req.xmlData.nodeName===this.strip&&req.xmlData.childNodes.length){this._conn.xmlOutput(req.xmlData.childNodes[0])}else{this._conn.xmlOutput(req.xmlData)}}if(this._conn.rawOutput!==Strophe.Connection.prototype.rawOutput){this._conn.rawOutput(req.data)}}else{Strophe.debug("_processRequest: "+(i===0?"first":"second")+" request has readyState of "+req.xhr.readyState)}},_removeRequest:function(req){Strophe.debug("removing request");var i;for(i=this._requests.length-1;i>=0;i--){if(req==this._requests[i]){this._requests.splice(i,1)}}req.xhr.onreadystatechange=function(){};this._throttledRequestHandler()},_restartRequest:function(i){var req=this._requests[i];if(req.dead===null){req.dead=new Date()}this._processRequest(i)},_reqToData:function(req){try{return req.getResponse()}catch(e){if(e!="parsererror"){throw e}this._conn.disconnect("strophe-parsererror")}},_sendTerminate:function(pres){Strophe.info("_sendTerminate was called");var body=this._buildBody().attrs({type:"terminate"});if(pres){body.cnode(pres.tree())}var req=new Strophe.Request(body.tree(),this._onRequestStateChange.bind(this,this._conn._dataRecv.bind(this._conn)),body.tree().getAttribute("rid"));this._requests.push(req);this._throttledRequestHandler()},_send:function(){clearTimeout(this._conn._idleTimeout);this._throttledRequestHandler();this._conn._idleTimeout=setTimeout(this._conn._onIdle.bind(this._conn),100)},_sendRestart:function(){this._throttledRequestHandler();clearTimeout(this._conn._idleTimeout)},_throttledRequestHandler:function(){if(!this._requests){Strophe.debug("_throttledRequestHandler called with undefined requests")}else{Strophe.debug("_throttledRequestHandler called with "+this._requests.length+" requests")}if(!this._requests||this._requests.length===0){return}if(this._requests.length>0){this._processRequest(0)}if(this._requests.length>1&&Math.abs(this._requests[0].rid-this._requests[1].rid)<this.window){this._processRequest(1)}}};Strophe.Websocket=function(connection){this._conn=connection;this.strip="stream:stream";var service=connection.service;if(service.indexOf("ws:")!==0&&service.indexOf("wss:")!==0){var new_service="";if(connection._options.protocol==="ws"&&window.location.protocol!=="https:"){new_service+="ws"}else{new_service+="wss"}new_service+="://"+window.location.host;if(service.indexOf("/")!==0){new_service+=window.location.pathname+service}else{new_service+=service}connection.service=new_service}};Strophe.Websocket.prototype={_buildStream:function(){return $build("stream:stream",{to:this._conn.domain,xmlns:Strophe.NS.CLIENT,"xmlns:stream":Strophe.NS.STREAM,version:"1.0"})},_check_streamerror:function(bodyWrap,connectstatus){var errors=bodyWrap.getElementsByTagName("stream:error");if(errors.length===0){return false}var error=errors[0];var condition="";var text="";ns="urn:ietf:params:xml:ns:xmpp-streams";for(i=0;i<error.childNodes.length;i++){e=error.childNodes[i];if(e.getAttribute("xmlns")!==ns){break}if(e.nodeName==="text"){text=e.textContent}else{condition=e.nodeName}}errorString="WebSocket stream error: ";if(condition){errorString+=condition}else{errorString+="unknown"}if(text){errorString+=" - "+condition}Strophe.error(errorString);this._conn._changeConnectStatus(connectstatus,condition);this._conn._doDisconnect();return true},_reset:function(){return},_connect:function(){this._closeSocket();this.socket=new WebSocket(this._conn.service,"xmpp");this.socket.onopen=this._onOpen.bind(this);this.socket.onerror=this._onError.bind(this);this.socket.onclose=this._onClose.bind(this);this.socket.onmessage=this._connect_cb_wrapper.bind(this)},_connect_cb:function(bodyWrap){var error=this._check_streamerror(bodyWrap,Strophe.Status.CONNFAIL);if(error){return Strophe.Status.CONNFAIL}},_handleStreamStart:function(message){var error=false;var ns=message.getAttribute("xmlns");if(typeof ns!=="string"){error="Missing xmlns in stream:stream"}else{if(ns!==Strophe.NS.CLIENT){error="Wrong xmlns in stream:stream: "+ns}}var ns_stream=message.namespaceURI;if(typeof ns_stream!=="string"){error="Missing xmlns:stream in stream:stream"}else{if(ns_stream!==Strophe.NS.STREAM){error="Wrong xmlns:stream in stream:stream: "+ns_stream}}var ver=message.getAttribute("version");if(typeof ver!=="string"){error="Missing version in stream:stream"}else{if(ver!=="1.0"){error="Wrong version in stream:stream: "+ver}}if(error){this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,error);this._conn._doDisconnect();return false}return true},_connect_cb_wrapper:function(message){if(message.data.indexOf("<stream:stream ")===0||message.data.indexOf("<?xml")===0){var data=message.data.replace(/^(<\?.*?\?>\s*)*/,"");if(data===""){return}data=message.data.replace(/<stream:stream (.*[^\/])>/,"<stream:stream $1/>");var streamStart=new DOMParser().parseFromString(data,"text/xml").documentElement;this._conn.xmlInput(streamStart);this._conn.rawInput(message.data);if(this._handleStreamStart(streamStart)){this._connect_cb(streamStart);this.streamStart=message.data.replace(/^<stream:(.*)\/>$/,"<stream:$1>")}}else{if(message.data==="</stream:stream>"){this._conn.rawInput(message.data);this._conn.xmlInput(document.createElement("stream:stream"));this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,error);this._conn._doDisconnect();return}else{var string=this._streamWrap(message.data);var elem=new DOMParser().parseFromString(string,"text/xml").documentElement;this.socket.onmessage=this._onMessage.bind(this);this._conn._connect_cb(elem,null,message.data)}}},_disconnect:function(pres){if(this.socket.readyState!==WebSocket.CLOSED){if(pres){this._conn.send(pres)}var close="</stream:stream>";this._conn.xmlOutput(document.createElement("stream:stream"));this._conn.rawOutput(close);try{this.socket.send(close)}catch(e){Strophe.info("Couldn't send closing stream tag.")}}this._conn._doDisconnect()},_doDisconnect:function(){Strophe.info("WebSockets _doDisconnect was called");this._closeSocket()},_streamWrap:function(stanza){return this.streamStart+stanza+"</stream:stream>"},_closeSocket:function(){if(this.socket){try{this.socket.close()}catch(e){}}this.socket=null},_emptyQueue:function(){return true},_onClose:function(event){if(this._conn.connected&&!this._conn.disconnecting){Strophe.error("Websocket closed unexcectedly");this._conn._doDisconnect()}else{Strophe.info("Websocket closed")}},_no_auth_received:function(_callback){Strophe.error("Server did not send any auth methods");this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,"Server did not send any auth methods");if(_callback){_callback=_callback.bind(this._conn);_callback()}this._conn._doDisconnect()},_onDisconnectTimeout:function(){},_onError:function(error){Strophe.error("Websocket error "+error);this._conn._changeConnectStatus(Strophe.Status.CONNFAIL,"The WebSocket connection could not be established was disconnected.");this._disconnect()},_onIdle:function(){var data=this._conn._data;if(data.length>0&&!this._conn.paused){for(i=0;i<data.length;i++){if(data[i]!==null){var stanza,rawStanza;if(data[i]==="restart"){stanza=this._buildStream();rawStanza=this._removeClosingTag(stanza);stanza=stanza.tree()}else{stanza=data[i];rawStanza=Strophe.serialize(stanza)}this._conn.xmlOutput(stanza);this._conn.rawOutput(rawStanza);this.socket.send(rawStanza)}}this._conn._data=[]}},_onMessage:function(message){var elem;if(message.data==="</stream:stream>"){var close="</stream:stream>";this._conn.rawInput(close);this._conn.xmlInput(document.createElement("stream:stream"));if(!this._conn.disconnecting){this._conn._doDisconnect()}return}else{if(message.data.search("<stream:stream ")===0){data=message.data.replace(/<stream:stream (.*[^\/])>/,"<stream:stream $1/>");elem=new DOMParser().parseFromString(data,"text/xml").documentElement;if(!this._handleStreamStart(elem)){return}}else{data=this._streamWrap(message.data);elem=new DOMParser().parseFromString(data,"text/xml").documentElement}}if(this._check_streamerror(elem,Strophe.Status.ERROR)){return}if(this._conn.disconnecting&&elem.firstChild.nodeName==="presence"&&elem.firstChild.getAttribute("type")==="unavailable"){this._conn.xmlInput(elem);this._conn.rawInput(Strophe.serialize(elem));return}this._conn._dataRecv(elem,message.data)},_onOpen:function(){Strophe.info("Websocket open");var start=this._buildStream();this._conn.xmlOutput(start.tree());var startString=this._removeClosingTag(start);this._conn.rawOutput(startString);this.socket.send(startString)},_removeClosingTag:function(elem){var string=Strophe.serialize(elem);string=string.replace(/<(stream:stream .*[^\/])\/>$/,"<$1>");return string},_reqToData:function(stanza){return stanza},_send:function(){this._conn.flush()},_sendRestart:function(){clearTimeout(this._conn._idleTimeout);this._conn._onIdle.bind(this._conn)()}};Strophe.addConnectionPlugin("disco",{_connection:null,_identities:[],_features:[],_items:[],init:function(a){this._connection=a;this._identities=[];this._features=[];this._items=[];a.addHandler(this._onDiscoInfo.bind(this),Strophe.NS.DISCO_INFO,"iq","get",null,null);a.addHandler(this._onDiscoItems.bind(this),Strophe.NS.DISCO_ITEMS,"iq","get",null,null)},addIdentity:function(d,c,a,e){for(var b=0;b<this._identities.length;b++){if(this._identities[b].category==d&&this._identities[b].type==c&&this._identities[b].name==a&&this._identities[b].lang==e){return false}}this._identities.push({category:d,type:c,name:a,lang:e});return true},addFeature:function(b){for(var a=0;a<this._features.length;a++){if(this._features[a]==b){return false}}this._features.push(b);return true},removeFeature:function(b){for(var a=0;a<this._features.length;a++){if(this._features[a]===b){this._features.splice(a,1);return true}}return false},addItem:function(b,a,c,d){if(c&&!d){return false}this._items.push({jid:b,name:a,node:c,call_back:d});return true},info:function(c,d,g,b,e){var a={xmlns:Strophe.NS.DISCO_INFO};if(d){a.node=d}var f=$iq({from:this._connection.jid,to:c,type:"get"}).c("query",a);this._connection.sendIQ(f,g,b,e)},items:function(d,e,g,c,f){var b={xmlns:Strophe.NS.DISCO_ITEMS};if(e){b.node=e}var a=$iq({from:this._connection.jid,to:d,type:"get"}).c("query",b);this._connection.sendIQ(a,g,c,f)},_buildIQResult:function(c,b){var e=c.getAttribute("id");var d=c.getAttribute("from");var a=$iq({type:"result",id:e});if(d!==null){a.attrs({to:d})}return a.c("query",b)},_onDiscoInfo:function(e){var d=e.getElementsByTagName("query")[0].getAttribute("node");var a={xmlns:Strophe.NS.DISCO_INFO};if(d){a.node=d}var c=this._buildIQResult(e,a);for(var b=0;b<this._identities.length;b++){var a={category:this._identities[b].category,type:this._identities[b].type};if(this._identities[b].name){a.name=this._identities[b].name}if(this._identities[b].lang){a["xml:lang"]=this._identities[b].lang}c.c("identity",a).up()}for(var b=0;b<this._features.length;b++){c.c("feature",{"var":this._features[b]}).up()}this._connection.send(c.tree());return true},_onDiscoItems:function(g){var f={xmlns:Strophe.NS.DISCO_ITEMS};var e=g.getElementsByTagName("query")[0].getAttribute("node");if(e){f.node=e;var a=[];for(var c=0;c<this._items.length;c++){if(this._items[c].node==e){a=this._items[c].call_back(g);break}}}else{var a=this._items}var d=this._buildIQResult(g,f);for(var c=0;c<a.length;c++){var b={jid:a[c].jid};if(a[c].name){b.name=a[c].name}if(a[c].node){b.node=a[c].node}d.c("item",b).up()}this._connection.send(d.tree());return true}});/* jshint -W117 */
  2. function TraceablePeerConnection(ice_config, constraints) {
  3. var self = this;
  4. var RTCPeerconnection = navigator.mozGetUserMedia ? mozRTCPeerConnection : webkitRTCPeerConnection;
  5. this.peerconnection = new RTCPeerconnection(ice_config, constraints);
  6. this.updateLog = [];
  7. // override as desired
  8. this.trace = function(what, info) {
  9. //console.warn('WTRACE', what, info);
  10. self.updateLog.push({
  11. time: new Date(),
  12. type: what,
  13. value: info
  14. });
  15. };
  16. this.onicecandidate = null;
  17. this.peerconnection.onicecandidate = function (event) {
  18. self.trace('onicecandidate', event.candidate);
  19. if (self.onicecandidate !== null) {
  20. self.onicecandidate(event);
  21. }
  22. };
  23. this.onaddstream = null;
  24. this.peerconnection.onaddstream = function (event) {
  25. self.trace('onaddstream', event.stream);
  26. if (self.onaddstream !== null) {
  27. self.onaddstream(event);
  28. }
  29. };
  30. this.onremovestream = null;
  31. this.peerconnection.onremovestream = function (event) {
  32. self.trace('onremovestream', event.stream);
  33. if (self.onremovestream !== null) {
  34. self.onremovestream(event);
  35. }
  36. };
  37. this.onsignalingstatechange = null;
  38. this.peerconnection.onsignalingstatechange = function (event) {
  39. self.trace('onsignalingstatechange', event);
  40. if (self.onsignalingstatechange !== null) {
  41. self.onsignalingstatechange(event);
  42. }
  43. };
  44. this.oniceconnectionstatechange = null;
  45. this.peerconnection.oniceconnectionstatechange = function (event) {
  46. self.trace('oniceconnectionstatechange', event);
  47. if (self.oniceconnectionstatechange !== null) {
  48. self.oniceconnectionstatechange(event);
  49. }
  50. }
  51. };
  52. TraceablePeerConnection.prototype.__defineGetter__('signalingState', function() { return this.peerconnection.signalingState; });
  53. TraceablePeerConnection.prototype.__defineGetter__('iceConnectionState', function() { return this.peerconnection.iceConnectionState; });
  54. TraceablePeerConnection.prototype.__defineGetter__('localDescription', function() { return this.peerconnection.localDescription; });
  55. TraceablePeerConnection.prototype.__defineGetter__('remoteDescription', function() { return this.peerconnection.remoteDescription; });
  56. TraceablePeerConnection.prototype.addStream = function (stream) {
  57. this.trace('addStream', stream);
  58. this.peerconnection.addStream(stream);
  59. };
  60. TraceablePeerConnection.prototype.removeStream = function (stream) {
  61. this.trace('removeStream', stream);
  62. this.peerconnection.removeStream(stream);
  63. };
  64. TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) {
  65. var self = this;
  66. this.trace('setLocalDescription', description);
  67. this.peerconnection.setLocalDescription(description,
  68. function () {
  69. self.trace('setLocalDescriptionOnSuccess');
  70. successCallback();
  71. },
  72. function (err) {
  73. self.trace('setLocalDescriptionOnFailure', err);
  74. failureCallback(err);
  75. }
  76. );
  77. };
  78. TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) {
  79. var self = this;
  80. this.trace('setRemoteDescription', description);
  81. this.peerconnection.setRemoteDescription(description,
  82. function () {
  83. self.trace('setRemoteDescriptionOnSuccess');
  84. successCallback();
  85. },
  86. function (err) {
  87. self.trace('setRemoteDescriptionOnFailure', err);
  88. failureCallback(err);
  89. }
  90. );
  91. };
  92. TraceablePeerConnection.prototype.close = function () {
  93. this.trace('stop');
  94. this.peerconnection.close();
  95. };
  96. TraceablePeerConnection.prototype.createOffer = function (successCallback, failureCallback, constraints) {
  97. var self = this;
  98. this.trace('createOffer', constraints);
  99. this.peerconnection.createOffer(
  100. function (sdp) {
  101. self.trace('createOfferOnSuccess', sdp);
  102. successCallback(sdp);
  103. },
  104. function(err) {
  105. self.trace('createOfferOnFailure', err);
  106. failureCallback(err);
  107. },
  108. constraints
  109. );
  110. };
  111. TraceablePeerConnection.prototype.createAnswer = function (successCallback, failureCallback, constraints) {
  112. var self = this;
  113. this.trace('createAnswer', constraints);
  114. this.peerconnection.createAnswer(
  115. function (sdp) {
  116. self.trace('createAnswerOnSuccess', sdp);
  117. successCallback(sdp);
  118. },
  119. function(err) {
  120. self.trace('createAnswerOnFailure', err);
  121. failureCallback(err);
  122. },
  123. constraints
  124. );
  125. };
  126. TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) {
  127. var self = this;
  128. this.trace('addIceCandidate', candidate);
  129. this.peerconnection.addIceCandidate(candidate);
  130. /* maybe later
  131. this.peerconnection.addIceCandidate(candidate,
  132. function () {
  133. self.trace('addIceCandidateOnSuccess');
  134. successCallback();
  135. },
  136. function (err) {
  137. self.trace('addIceCandidateOnFailure', err);
  138. failureCallback(err);
  139. }
  140. );
  141. */
  142. };
  143. TraceablePeerConnection.prototype.getStats = function(callback) {
  144. this.peerconnection.getStats(callback);
  145. };
  146. // mozilla chrome compat layer -- very similar to adapter.js
  147. function setupRTC() {
  148. var RTC = null;
  149. if (navigator.mozGetUserMedia) {
  150. console.log('This appears to be Firefox');
  151. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  152. if (version >= 22) {
  153. RTC = {
  154. peerconnection: mozRTCPeerConnection,
  155. browser: 'firefox',
  156. getUserMedia: navigator.mozGetUserMedia.bind(navigator),
  157. attachMediaStream: function (element, stream) {
  158. element[0].mozSrcObject = stream;
  159. element[0].play();
  160. },
  161. pc_constraints: {}
  162. };
  163. if (!MediaStream.prototype.getVideoTracks)
  164. MediaStream.prototype.getVideoTracks = function () { return []; };
  165. if (!MediaStream.prototype.getAudioTracks)
  166. MediaStream.prototype.getAudioTracks = function () { return []; };
  167. RTCSessionDescription = mozRTCSessionDescription;
  168. RTCIceCandidate = mozRTCIceCandidate;
  169. }
  170. } else if (navigator.webkitGetUserMedia) {
  171. console.log('This appears to be Chrome');
  172. RTC = {
  173. peerconnection: webkitRTCPeerConnection,
  174. browser: 'chrome',
  175. getUserMedia: navigator.webkitGetUserMedia.bind(navigator),
  176. attachMediaStream: function (element, stream) {
  177. element.attr('src', webkitURL.createObjectURL(stream));
  178. },
  179. // DTLS should now be enabled by default but..
  180. pc_constraints: {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]}
  181. };
  182. if (navigator.userAgent.indexOf('Android') != -1) {
  183. RTC.pc_constraints = {}; // disable DTLS on Android
  184. }
  185. if (!webkitMediaStream.prototype.getVideoTracks) {
  186. webkitMediaStream.prototype.getVideoTracks = function () {
  187. return this.videoTracks;
  188. };
  189. }
  190. if (!webkitMediaStream.prototype.getAudioTracks) {
  191. webkitMediaStream.prototype.getAudioTracks = function () {
  192. return this.audioTracks;
  193. };
  194. }
  195. }
  196. if (RTC === null) {
  197. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  198. }
  199. return RTC;
  200. }
  201. function getUserMediaWithConstraints(um, resolution, bandwidth, fps) {
  202. var constraints = {audio: false, video: false};
  203. if (um.indexOf('video') >= 0) {
  204. constraints.video = {mandatory: {}};// same behaviour as true
  205. }
  206. if (um.indexOf('audio') >= 0) {
  207. constraints.audio = {};// same behaviour as true
  208. }
  209. if (um.indexOf('screen') >= 0) {
  210. constraints.video = {
  211. "mandatory": {
  212. "chromeMediaSource": "screen"
  213. }
  214. };
  215. }
  216. if (resolution && !constraints.video) {
  217. constraints.video = {mandatory: {}};// same behaviour as true
  218. }
  219. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  220. switch (resolution) {
  221. // 16:9 first
  222. case '1080':
  223. case 'fullhd':
  224. constraints.video.mandatory.minWidth = 1920;
  225. constraints.video.mandatory.minHeight = 1080;
  226. constraints.video.mandatory.minAspectRatio = 1.77;
  227. break;
  228. case '720':
  229. case 'hd':
  230. constraints.video.mandatory.minWidth = 1280;
  231. constraints.video.mandatory.minHeight = 720;
  232. constraints.video.mandatory.minAspectRatio = 1.77;
  233. break;
  234. case '360':
  235. constraints.video.mandatory.minWidth = 640;
  236. constraints.video.mandatory.minHeight = 360;
  237. constraints.video.mandatory.minAspectRatio = 1.77;
  238. break;
  239. case '180':
  240. constraints.video.mandatory.minWidth = 320;
  241. constraints.video.mandatory.minHeight = 180;
  242. constraints.video.mandatory.minAspectRatio = 1.77;
  243. break;
  244. // 4:3
  245. case '960':
  246. constraints.video.mandatory.minWidth = 960;
  247. constraints.video.mandatory.minHeight = 720;
  248. break;
  249. case '640':
  250. case 'vga':
  251. constraints.video.mandatory.minWidth = 640;
  252. constraints.video.mandatory.minHeight = 480;
  253. break;
  254. case '320':
  255. constraints.video.mandatory.minWidth = 320;
  256. constraints.video.mandatory.minHeight = 240;
  257. break;
  258. default:
  259. if (navigator.userAgent.indexOf('Android') != -1) {
  260. constraints.video.mandatory.minWidth = 320;
  261. constraints.video.mandatory.minHeight = 240;
  262. constraints.video.mandatory.maxFrameRate = 15;
  263. }
  264. break;
  265. }
  266. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  267. if (!constraints.video) constraints.video = {mandatory: {}};//same behaviour as true
  268. constraints.video.optional = [{bandwidth: bandwidth}];
  269. }
  270. if (fps) { // for some cameras it might be necessary to request 30fps
  271. // so they choose 30fps mjpg over 10fps yuy2
  272. if (!constraints.video) constraints.video = {mandatory: {}};// same behaviour as tru;
  273. constraints.video.mandatory.minFrameRate = fps;
  274. }
  275. try {
  276. RTC.getUserMedia(constraints,
  277. function (stream) {
  278. console.log('onUserMediaSuccess');
  279. $(document).trigger('mediaready.jingle', [stream]);
  280. },
  281. function (error) {
  282. console.warn('Failed to get access to local media. Error ', error);
  283. $(document).trigger('mediafailure.jingle');
  284. });
  285. } catch (e) {
  286. console.error('GUM failed: ', e);
  287. $(document).trigger('mediafailure.jingle');
  288. }
  289. }
  290. /* jshint -W117 */
  291. Strophe.addConnectionPlugin('jingle', {
  292. connection: null,
  293. sessions: {},
  294. jid2session: {},
  295. ice_config: {iceServers: []},
  296. pc_constraints: {},
  297. media_constraints: {
  298. mandatory: {
  299. 'OfferToReceiveAudio': true,
  300. 'OfferToReceiveVideo': true
  301. }
  302. // MozDontOfferDataChannel: true when this is firefox
  303. },
  304. localStream: null,
  305. init: function (conn) {
  306. this.connection = conn;
  307. if (this.connection.disco) {
  308. // http://xmpp.org/extensions/xep-0167.html#support
  309. // http://xmpp.org/extensions/xep-0176.html#support
  310. this.connection.disco.addFeature('urn:xmpp:jingle:1');
  311. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
  312. this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
  313. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
  314. this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
  315. // this is dealt with by SDP O/A so we don't need to annouce this
  316. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
  317. //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
  318. this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
  319. //this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
  320. //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
  321. }
  322. this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
  323. },
  324. onJingle: function (iq) {
  325. var sid = $(iq).find('jingle').attr('sid');
  326. var action = $(iq).find('jingle').attr('action');
  327. // send ack first
  328. var ack = $iq({type: 'result',
  329. to: iq.getAttribute('from'),
  330. id: iq.getAttribute('id')
  331. });
  332. console.log('on jingle ' + action);
  333. var sess = this.sessions[sid];
  334. if ('session-initiate' != action) {
  335. if (sess === null) {
  336. ack.type = 'error';
  337. ack.c('error', {type: 'cancel'})
  338. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  339. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  340. this.connection.send(ack);
  341. return true;
  342. }
  343. // compare from to sess.peerjid (bare jid comparison for later compat with message-mode)
  344. // local jid is not checked
  345. if (Strophe.getBareJidFromJid(iq.getAttribute('from')) != Strophe.getBareJidFromJid(sess.peerjid)) {
  346. console.warn('jid mismatch for session id', sid, iq.getAttribute('from'), sess.peerjid);
  347. ack.type = 'error';
  348. ack.c('error', {type: 'cancel'})
  349. .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
  350. .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
  351. this.connection.send(ack);
  352. return true;
  353. }
  354. } else if (sess !== undefined) {
  355. // existing session with same session id
  356. // this might be out-of-order if the sess.peerjid is the same as from
  357. ack.type = 'error';
  358. ack.c('error', {type: 'cancel'})
  359. .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
  360. console.warn('duplicate session id', sid);
  361. this.connection.send(ack);
  362. return true;
  363. }
  364. // FIXME: check for a defined action
  365. this.connection.send(ack);
  366. // see http://xmpp.org/extensions/xep-0166.html#concepts-session
  367. switch (action) {
  368. case 'session-initiate':
  369. sess = new JingleSession($(iq).attr('to'), $(iq).find('jingle').attr('sid'), this.connection);
  370. // configure session
  371. if (this.localStream) {
  372. sess.localStreams.push(this.localStream);
  373. }
  374. sess.media_constraints = this.media_constraints;
  375. sess.pc_constraints = this.pc_constraints;
  376. sess.ice_config = this.ice_config;
  377. sess.initiate($(iq).attr('from'), false);
  378. // FIXME: setRemoteDescription should only be done when this call is to be accepted
  379. sess.setRemoteDescription($(iq).find('>jingle'), 'offer');
  380. this.sessions[sess.sid] = sess;
  381. this.jid2session[sess.peerjid] = sess;
  382. // the callback should either
  383. // .sendAnswer and .accept
  384. // or .sendTerminate -- not necessarily synchronus
  385. $(document).trigger('callincoming.jingle', [sess.sid]);
  386. break;
  387. case 'session-accept':
  388. sess.setRemoteDescription($(iq).find('>jingle'), 'answer');
  389. sess.accept();
  390. break;
  391. case 'session-terminate':
  392. console.log('terminating...');
  393. sess.terminate();
  394. this.terminate(sess.sid);
  395. if ($(iq).find('>jingle>reason').length) {
  396. $(document).trigger('callterminated.jingle', [
  397. sess.sid,
  398. $(iq).find('>jingle>reason>:first')[0].tagName,
  399. $(iq).find('>jingle>reason>text').text()
  400. ]);
  401. } else {
  402. $(document).trigger('callterminated.jingle', [sess.sid]);
  403. }
  404. break;
  405. case 'transport-info':
  406. sess.addIceCandidate($(iq).find('>jingle>content'));
  407. break;
  408. case 'session-info':
  409. var affected;
  410. if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  411. $(document).trigger('ringing.jingle', [sess.sid]);
  412. } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  413. affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  414. $(document).trigger('mute.jingle', [sess.sid, affected]);
  415. } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
  416. affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
  417. $(document).trigger('unmute.jingle', [sess.sid, affected]);
  418. }
  419. break;
  420. case 'addsource': // FIXME: proprietary
  421. sess.addSource($(iq).find('>jingle>content'));
  422. break;
  423. case 'removesource': // FIXME: proprietary
  424. sess.removeSource($(iq).find('>jingle>content'));
  425. break;
  426. default:
  427. console.warn('jingle action not implemented', action);
  428. break;
  429. }
  430. return true;
  431. },
  432. initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
  433. var sess = new JingleSession(myjid || this.connection.jid,
  434. Math.random().toString(36).substr(2, 12), // random string
  435. this.connection);
  436. // configure session
  437. if (this.localStream) {
  438. sess.localStreams.push(this.localStream);
  439. }
  440. sess.media_constraints = this.media_constraints;
  441. sess.pc_constraints = this.pc_constraints;
  442. sess.ice_config = this.ice_config;
  443. sess.initiate(peerjid, true);
  444. this.sessions[sess.sid] = sess;
  445. this.jid2session[sess.peerjid] = sess;
  446. sess.sendOffer();
  447. return sess;
  448. },
  449. terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
  450. if (sid === null || sid === undefined) {
  451. for (sid in this.sessions) {
  452. if (this.sessions[sid].state != 'ended') {
  453. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  454. this.sessions[sid].terminate();
  455. }
  456. delete this.jid2session[this.sessions[sid].peerjid];
  457. delete this.sessions[sid];
  458. }
  459. } else if (this.sessions.hasOwnProperty(sid)) {
  460. if (this.sessions[sid].state != 'ended') {
  461. this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
  462. this.sessions[sid].terminate();
  463. }
  464. delete this.jid2session[this.sessions[sid].peerjid];
  465. delete this.sessions[sid];
  466. }
  467. },
  468. terminateByJid: function (jid) {
  469. if (this.jid2session.hasOwnProperty(jid)) {
  470. var sess = this.jid2session[jid];
  471. if (sess) {
  472. sess.terminate();
  473. console.log('peer went away silently', jid);
  474. delete this.sessions[sess.sid];
  475. delete this.jid2session[jid];
  476. $(document).trigger('callterminated.jingle', [sess.sid, 'gone']);
  477. }
  478. }
  479. },
  480. getStunAndTurnCredentials: function () {
  481. // get stun and turn configuration from server via xep-0215
  482. // uses time-limited credentials as described in
  483. // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
  484. //
  485. // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
  486. // for a prosody module which implements this
  487. //
  488. // currently, this doesn't work with updateIce and therefore credentials with a long
  489. // validity have to be fetched before creating the peerconnection
  490. // TODO: implement refresh via updateIce as described in
  491. // https://code.google.com/p/webrtc/issues/detail?id=1650
  492. this.connection.sendIQ(
  493. $iq({type: 'get', to: this.connection.domain})
  494. .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
  495. function (res) {
  496. var iceservers = [];
  497. $(res).find('>services>service').each(function (idx, el) {
  498. el = $(el);
  499. var dict = {};
  500. switch (el.attr('type')) {
  501. case 'stun':
  502. dict.url = 'stun:' + el.attr('host');
  503. if (el.attr('port')) {
  504. dict.url += ':' + el.attr('port');
  505. }
  506. iceservers.push(dict);
  507. break;
  508. case 'turn':
  509. dict.url = 'turn:';
  510. if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
  511. if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
  512. dict.url += el.attr('username') + '@';
  513. } else {
  514. dict.username = el.attr('username'); // only works in M28
  515. }
  516. }
  517. dict.url += el.attr('host');
  518. if (el.attr('port') && el.attr('port') != '3478') {
  519. dict.url += ':' + el.attr('port');
  520. }
  521. if (el.attr('transport') && el.attr('transport') != 'udp') {
  522. dict.url += '?transport=' + el.attr('transport');
  523. }
  524. if (el.attr('password')) {
  525. dict.credential = el.attr('password');
  526. }
  527. iceservers.push(dict);
  528. break;
  529. }
  530. });
  531. this.ice_config.iceServers = iceservers;
  532. },
  533. function (err) {
  534. console.warn('getting turn credentials failed', err);
  535. console.warn('is mod_turncredentials or similar installed?');
  536. }
  537. );
  538. // implement push?
  539. }
  540. });
  541. /* jshint -W117 */
  542. // SDP STUFF
  543. function SDP(sdp) {
  544. this.media = sdp.split('\r\nm=');
  545. for (var i = 1; i < this.media.length; i++) {
  546. this.media[i] = 'm=' + this.media[i];
  547. if (i != this.media.length - 1) {
  548. this.media[i] += '\r\n';
  549. }
  550. }
  551. this.session = this.media.shift() + '\r\n';
  552. this.raw = this.session + this.media.join('');
  553. }
  554. // remove iSAC and CN from SDP
  555. SDP.prototype.mangle = function () {
  556. var i, j, mline, lines, rtpmap, newdesc;
  557. for (i = 0; i < this.media.length; i++) {
  558. lines = this.media[i].split('\r\n');
  559. lines.pop(); // remove empty last element
  560. mline = SDPUtil.parse_mline(lines.shift());
  561. if (mline.media != 'audio')
  562. continue;
  563. newdesc = '';
  564. mline.fmt.length = 0;
  565. for (j = 0; j < lines.length; j++) {
  566. if (lines[j].substr(0, 9) == 'a=rtpmap:') {
  567. rtpmap = SDPUtil.parse_rtpmap(lines[j]);
  568. if (rtpmap.name == 'CN' || rtpmap.name == 'ISAC')
  569. continue;
  570. mline.fmt.push(rtpmap.id);
  571. newdesc += lines[j] + '\r\n';
  572. } else {
  573. newdesc += lines[j] + '\r\n';
  574. }
  575. }
  576. this.media[i] = SDPUtil.build_mline(mline) + '\r\n';
  577. this.media[i] += newdesc;
  578. }
  579. this.raw = this.session + this.media.join('');
  580. };
  581. // remove lines matching prefix from session section
  582. SDP.prototype.removeSessionLines = function(prefix) {
  583. var ob = this;
  584. var lines = SDPUtil.find_lines(this.session, prefix);
  585. lines.forEach(function(line) {
  586. ob.session = ob.session.replace(line + '\r\n', '');
  587. });
  588. this.raw = this.session + this.media.join('');
  589. return lines;
  590. }
  591. // remove lines matching prefix from a media section specified by mediaindex
  592. // TODO: non-numeric mediaindex could match mid
  593. SDP.prototype.removeMediaLines = function(mediaindex, prefix) {
  594. var ob = this;
  595. var lines = SDPUtil.find_lines(this.media[mediaindex], prefix);
  596. lines.forEach(function(line) {
  597. ob.media[mediaindex] = ob.media[mediaindex].replace(line + '\r\n', '');
  598. });
  599. this.raw = this.session + this.media.join('');
  600. return lines;
  601. }
  602. // add content's to a jingle element
  603. SDP.prototype.toJingle = function (elem, thecreator) {
  604. var i, j, k, mline, ssrc, rtpmap, tmp, line, lines;
  605. var ob = this;
  606. // new bundle plan
  607. if (SDPUtil.find_line(this.session, 'a=group:')) {
  608. lines = SDPUtil.find_lines(this.session, 'a=group:');
  609. for (i = 0; i < lines.length; i++) {
  610. tmp = lines[i].split(' ');
  611. var semantics = tmp.shift().substr(8);
  612. // new plan
  613. elem.c('group', {xmlns: 'urn:xmpp:jingle:apps:grouping:0', type: semantics, semantics:semantics});
  614. for (j = 0; j < tmp.length; j++) {
  615. elem.c('content', {name: tmp[j]}).up();
  616. }
  617. elem.up();
  618. // temporary plan, to be removed
  619. elem.c('group', {xmlns: 'urn:ietf:rfc:5888', type: semantics});
  620. for (j = 0; j < tmp.length; j++) {
  621. elem.c('content', {name: tmp[j]}).up();
  622. }
  623. elem.up();
  624. }
  625. }
  626. // old bundle plan, to be removed
  627. var bundle = [];
  628. if (SDPUtil.find_line(this.session, 'a=group:BUNDLE')) {
  629. bundle = SDPUtil.find_line(this.session, 'a=group:BUNDLE ').split(' ');
  630. bundle.shift();
  631. }
  632. for (i = 0; i < this.media.length; i++) {
  633. mline = SDPUtil.parse_mline(this.media[i].split('\r\n')[0]);
  634. if (!(mline.media == 'audio' || mline.media == 'video')) {
  635. continue;
  636. }
  637. if (SDPUtil.find_line(this.media[i], 'a=ssrc:')) {
  638. ssrc = SDPUtil.find_line(this.media[i], 'a=ssrc:').substring(7).split(' ')[0]; // take the first
  639. } else {
  640. ssrc = false;
  641. }
  642. elem.c('content', {creator: thecreator, name: mline.media});
  643. if (SDPUtil.find_line(this.media[i], 'a=mid:')) {
  644. // prefer identifier from a=mid if present
  645. var mid = SDPUtil.parse_mid(SDPUtil.find_line(this.media[i], 'a=mid:'));
  646. elem.attrs({ name: mid });
  647. // old BUNDLE plan, to be removed
  648. if (bundle.indexOf(mid) != -1) {
  649. elem.c('bundle', {xmlns: 'http://estos.de/ns/bundle'}).up();
  650. bundle.splice(bundle.indexOf(mid), 1);
  651. }
  652. }
  653. if (SDPUtil.find_line(this.media[i], 'a=rtpmap:').length) {
  654. elem.c('description',
  655. {xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  656. media: mline.media });
  657. if (ssrc) {
  658. elem.attrs({ssrc: ssrc});
  659. }
  660. for (j = 0; j < mline.fmt.length; j++) {
  661. rtpmap = SDPUtil.find_line(this.media[i], 'a=rtpmap:' + mline.fmt[j]);
  662. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  663. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
  664. if (SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j])) {
  665. tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j]));
  666. for (k = 0; k < tmp.length; k++) {
  667. elem.c('parameter', tmp[k]).up();
  668. }
  669. }
  670. this.RtcpFbToJingle(i, elem, mline.fmt[j]); // XEP-0293 -- map a=rtcp-fb
  671. elem.up();
  672. }
  673. if (SDPUtil.find_line(this.media[i], 'a=crypto:', this.session)) {
  674. elem.c('encryption', {required: 1});
  675. var crypto = SDPUtil.find_lines(this.media[i], 'a=crypto:', this.session);
  676. crypto.forEach(function(line) {
  677. elem.c('crypto', SDPUtil.parse_crypto(line)).up();
  678. });
  679. elem.up(); // end of encryption
  680. }
  681. if (ssrc) {
  682. // new style mapping
  683. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  684. // FIXME: group by ssrc and support multiple different ssrcs
  685. var ssrclines = SDPUtil.find_lines(this.media[i], 'a=ssrc:');
  686. ssrclines.forEach(function(line) {
  687. idx = line.indexOf(' ');
  688. var linessrc = line.substr(0, idx).substr(7);
  689. if (linessrc != ssrc) {
  690. elem.up();
  691. ssrc = linessrc;
  692. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  693. }
  694. var kv = line.substr(idx + 1);
  695. elem.c('parameter');
  696. if (kv.indexOf(':') == -1) {
  697. elem.attrs({ name: kv });
  698. } else {
  699. elem.attrs({ name: kv.split(':', 2)[0] });
  700. elem.attrs({ value: kv.split(':', 2)[1] });
  701. }
  702. elem.up();
  703. });
  704. elem.up();
  705. // old proprietary mapping, to be removed at some point
  706. tmp = SDPUtil.parse_ssrc(this.media[i]);
  707. tmp.xmlns = 'http://estos.de/ns/ssrc';
  708. tmp.ssrc = ssrc;
  709. elem.c('ssrc', tmp).up(); // ssrc is part of description
  710. }
  711. if (SDPUtil.find_line(this.media[i], 'a=rtcp-mux')) {
  712. elem.c('rtcp-mux').up();
  713. }
  714. // XEP-0293 -- map a=rtcp-fb:*
  715. this.RtcpFbToJingle(i, elem, '*');
  716. // XEP-0294
  717. if (SDPUtil.find_line(this.media[i], 'a=extmap:')) {
  718. lines = SDPUtil.find_lines(this.media[i], 'a=extmap:');
  719. for (j = 0; j < lines.length; j++) {
  720. tmp = SDPUtil.parse_extmap(lines[j]);
  721. elem.c('rtp-hdrext', { xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  722. uri: tmp.uri,
  723. id: tmp.value });
  724. if (tmp.hasOwnProperty('direction')) {
  725. switch (tmp.direction) {
  726. case 'sendonly':
  727. elem.attrs({senders: 'responder'});
  728. break;
  729. case 'recvonly':
  730. elem.attrs({senders: 'initiator'});
  731. break;
  732. case 'sendrecv':
  733. elem.attrs({senders: 'both'});
  734. break;
  735. case 'inactive':
  736. elem.attrs({senders: 'none'});
  737. break;
  738. }
  739. }
  740. // TODO: handle params
  741. elem.up();
  742. }
  743. }
  744. elem.up(); // end of description
  745. }
  746. // map ice-ufrag/pwd, dtls fingerprint, candidates
  747. this.TransportToJingle(i, elem);
  748. if (SDPUtil.find_line(this.media[i], 'a=sendrecv', this.session)) {
  749. elem.attrs({senders: 'both'});
  750. } else if (SDPUtil.find_line(this.media[i], 'a=sendonly', this.session)) {
  751. elem.attrs({senders: 'initiator'});
  752. } else if (SDPUtil.find_line(this.media[i], 'a=recvonly', this.session)) {
  753. elem.attrs({senders: 'responder'});
  754. } else if (SDPUtil.find_line(this.media[i], 'a=inactive', this.session)) {
  755. elem.attrs({senders: 'none'});
  756. }
  757. if (mline.port == '0') {
  758. // estos hack to reject an m-line
  759. elem.attrs({senders: 'rejected'});
  760. }
  761. elem.up(); // end of content
  762. }
  763. elem.up();
  764. return elem;
  765. };
  766. SDP.prototype.TransportToJingle = function (mediaindex, elem) {
  767. var i = mediaindex;
  768. var tmp;
  769. var ob = this;
  770. elem.c('transport');
  771. // XEP-0320
  772. var fingerprints = SDPUtil.find_lines(this.media[mediaindex], 'a=fingerprint:', this.session);
  773. fingerprints.forEach(function(line) {
  774. tmp = SDPUtil.parse_fingerprint(line);
  775. tmp.xmlns = 'urn:xmpp:tmp:jingle:apps:dtls:0';
  776. // tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0'; -- FIXME: update receivers first
  777. elem.c('fingerprint').t(tmp.fingerprint);
  778. delete tmp.fingerprint;
  779. line = SDPUtil.find_line(ob.media[mediaindex], 'a=setup:', ob.session);
  780. if (line) {
  781. tmp.setup = line.substr(8);
  782. }
  783. elem.attrs(tmp);
  784. elem.up(); // end of fingerprint
  785. });
  786. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  787. if (tmp) {
  788. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  789. elem.attrs(tmp);
  790. // XEP-0176
  791. if (SDPUtil.find_line(this.media[mediaindex], 'a=candidate:', this.session)) { // add any a=candidate lines
  792. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=candidate:', this.session);
  793. lines.forEach(function (line) {
  794. elem.c('candidate', SDPUtil.candidateToJingle(line)).up();
  795. });
  796. }
  797. }
  798. elem.up(); // end of transport
  799. }
  800. SDP.prototype.RtcpFbToJingle = function (mediaindex, elem, payloadtype) { // XEP-0293
  801. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=rtcp-fb:' + payloadtype);
  802. lines.forEach(function (line) {
  803. var tmp = SDPUtil.parse_rtcpfb(line);
  804. if (tmp.type == 'trr-int') {
  805. elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
  806. elem.up();
  807. } else {
  808. elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
  809. if (tmp.params.length > 0) {
  810. elem.attrs({'subtype': tmp.params[0]});
  811. }
  812. elem.up();
  813. }
  814. });
  815. };
  816. SDP.prototype.RtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
  817. var media = '';
  818. var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  819. if (tmp.length) {
  820. media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
  821. if (tmp.attr('value')) {
  822. media += tmp.attr('value');
  823. } else {
  824. media += '0';
  825. }
  826. media += '\r\n';
  827. }
  828. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  829. tmp.each(function () {
  830. media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
  831. if ($(this).attr('subtype')) {
  832. media += ' ' + $(this).attr('subtype');
  833. }
  834. media += '\r\n';
  835. });
  836. return media;
  837. };
  838. // construct an SDP from a jingle stanza
  839. SDP.prototype.fromJingle = function (jingle) {
  840. var obj = this;
  841. this.raw = 'v=0\r\n' +
  842. 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
  843. 's=-\r\n' +
  844. 't=0 0\r\n';
  845. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
  846. if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
  847. $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
  848. var contents = $(group).find('>content').map(function (idx, content) {
  849. return content.getAttribute('name');
  850. }).get();
  851. if (contents.length > 0) {
  852. obj.raw += 'a=group:' + (group.getAttribute('semantics') || group.getAttribute('type')) + ' ' + contents.join(' ') + '\r\n';
  853. }
  854. });
  855. } else if ($(jingle).find('>group[xmlns="urn:ietf:rfc:5888"]').length) {
  856. // temporary namespace, not to be used. to be removed soon.
  857. $(jingle).find('>group[xmlns="urn:ietf:rfc:5888"]').each(function (idx, group) {
  858. var contents = $(group).find('>content').map(function (idx, content) {
  859. return content.getAttribute('name');
  860. }).get();
  861. if (group.getAttribute('type') !== null && contents.length > 0) {
  862. obj.raw += 'a=group:' + group.getAttribute('type') + ' ' + contents.join(' ') + '\r\n';
  863. }
  864. });
  865. } else {
  866. // for backward compability, to be removed soon
  867. // assume all contents are in the same bundle group, can be improved upon later
  868. var bundle = $(jingle).find('>content').filter(function (idx, content) {
  869. //elem.c('bundle', {xmlns:'http://estos.de/ns/bundle'});
  870. return $(content).find('>bundle').length > 0;
  871. }).map(function (idx, content) {
  872. return content.getAttribute('name');
  873. }).get();
  874. if (bundle.length) {
  875. this.raw += 'a=group:BUNDLE ' + bundle.join(' ') + '\r\n';
  876. }
  877. }
  878. this.session = this.raw;
  879. jingle.find('>content').each(function () {
  880. var m = obj.jingle2media($(this));
  881. obj.media.push(m);
  882. });
  883. // reconstruct msid-semantic -- apparently not necessary
  884. /*
  885. var msid = SDPUtil.parse_ssrc(this.raw);
  886. if (msid.hasOwnProperty('mslabel')) {
  887. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  888. }
  889. */
  890. this.raw = this.session + this.media.join('');
  891. };
  892. // translate a jingle content element into an an SDP media part
  893. SDP.prototype.jingle2media = function (content) {
  894. var media = '',
  895. desc = content.find('description'),
  896. ssrc = desc.attr('ssrc'),
  897. self = this,
  898. tmp;
  899. tmp = { media: desc.attr('media') };
  900. tmp.port = '1';
  901. if (content.attr('senders') == 'rejected') {
  902. // estos hack to reject an m-line.
  903. tmp.port = '0';
  904. }
  905. if (content.find('>transport>fingerprint').length || desc.find('encryption').length) {
  906. tmp.proto = 'RTP/SAVPF';
  907. } else {
  908. tmp.proto = 'RTP/AVPF';
  909. }
  910. tmp.fmt = desc.find('payload-type').map(function () { return this.getAttribute('id'); }).get();
  911. media += SDPUtil.build_mline(tmp) + '\r\n';
  912. media += 'c=IN IP4 0.0.0.0\r\n';
  913. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  914. tmp = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  915. if (tmp.length) {
  916. if (tmp.attr('ufrag')) {
  917. media += SDPUtil.build_iceufrag(tmp.attr('ufrag')) + '\r\n';
  918. }
  919. if (tmp.attr('pwd')) {
  920. media += SDPUtil.build_icepwd(tmp.attr('pwd')) + '\r\n';
  921. }
  922. tmp.find('>fingerprint').each(function () {
  923. // FIXME: check namespace at some point
  924. media += 'a=fingerprint:' + this.getAttribute('hash');
  925. media += ' ' + $(this).text();
  926. media += '\r\n';
  927. if (this.getAttribute('setup')) {
  928. media += 'a=setup:' + this.getAttribute('setup') + '\r\n';
  929. }
  930. });
  931. }
  932. switch (content.attr('senders')) {
  933. case 'initiator':
  934. media += 'a=sendonly\r\n';
  935. break;
  936. case 'responder':
  937. media += 'a=recvonly\r\n';
  938. break;
  939. case 'none':
  940. media += 'a=inactive\r\n';
  941. break;
  942. case 'both':
  943. media += 'a=sendrecv\r\n';
  944. break;
  945. }
  946. media += 'a=mid:' + content.attr('name') + '\r\n';
  947. // <description><rtcp-mux/></description>
  948. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
  949. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  950. if (desc.find('rtcp-mux').length) {
  951. media += 'a=rtcp-mux\r\n';
  952. }
  953. if (desc.find('encryption').length) {
  954. desc.find('encryption>crypto').each(function () {
  955. media += 'a=crypto:' + this.getAttribute('tag');
  956. media += ' ' + this.getAttribute('crypto-suite');
  957. media += ' ' + this.getAttribute('key-params');
  958. if (this.getAttribute('session-params')) {
  959. media += ' ' + this.getAttribute('session-params');
  960. }
  961. media += '\r\n';
  962. });
  963. }
  964. desc.find('payload-type').each(function () {
  965. media += SDPUtil.build_rtpmap(this) + '\r\n';
  966. if ($(this).find('>parameter').length) {
  967. media += 'a=fmtp:' + this.getAttribute('id') + ' ';
  968. media += $(this).find('parameter').map(function () { return (this.getAttribute('name') ? (this.getAttribute('name') + '=') : '') + this.getAttribute('value'); }).get().join(';');
  969. media += '\r\n';
  970. }
  971. // xep-0293
  972. media += self.RtcpFbFromJingle($(this), this.getAttribute('id'));
  973. });
  974. // xep-0293
  975. media += self.RtcpFbFromJingle(desc, '*');
  976. // xep-0294
  977. tmp = desc.find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  978. tmp.each(function () {
  979. media += 'a=extmap:' + this.getAttribute('id') + ' ' + this.getAttribute('uri') + '\r\n';
  980. });
  981. content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]>candidate').each(function () {
  982. media += SDPUtil.candidateFromJingle(this);
  983. });
  984. tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  985. tmp.each(function () {
  986. var ssrc = this.getAttribute('ssrc');
  987. $(this).find('>parameter').each(function () {
  988. media += 'a=ssrc:' + ssrc + ' ' + this.getAttribute('name');
  989. if (this.getAttribute('value') && this.getAttribute('value').length)
  990. media += ':' + this.getAttribute('value');
  991. media += '\r\n';
  992. });
  993. });
  994. if (tmp.length === 0) {
  995. // fallback to proprietary mapping of a=ssrc lines
  996. tmp = content.find('description>ssrc[xmlns="http://estos.de/ns/ssrc"]');
  997. if (tmp.length) {
  998. media += 'a=ssrc:' + ssrc + ' cname:' + tmp.attr('cname') + '\r\n';
  999. media += 'a=ssrc:' + ssrc + ' msid:' + tmp.attr('msid') + '\r\n';
  1000. media += 'a=ssrc:' + ssrc + ' mslabel:' + tmp.attr('mslabel') + '\r\n';
  1001. media += 'a=ssrc:' + ssrc + ' label:' + tmp.attr('label') + '\r\n';
  1002. }
  1003. }
  1004. return media;
  1005. };
  1006. SDPUtil = {
  1007. iceparams: function (mediadesc, sessiondesc) {
  1008. var data = null;
  1009. if (SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc) &&
  1010. SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc)) {
  1011. data = {
  1012. ufrag: SDPUtil.parse_iceufrag(SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc)),
  1013. pwd: SDPUtil.parse_icepwd(SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc))
  1014. };
  1015. }
  1016. return data;
  1017. },
  1018. parse_iceufrag: function (line) {
  1019. return line.substring(12);
  1020. },
  1021. build_iceufrag: function (frag) {
  1022. return 'a=ice-ufrag:' + frag;
  1023. },
  1024. parse_icepwd: function (line) {
  1025. return line.substring(10);
  1026. },
  1027. build_icepwd: function (pwd) {
  1028. return 'a=ice-pwd:' + pwd;
  1029. },
  1030. parse_mid: function (line) {
  1031. return line.substring(6);
  1032. },
  1033. parse_mline: function (line) {
  1034. var parts = line.substring(2).split(' '),
  1035. data = {};
  1036. data.media = parts.shift();
  1037. data.port = parts.shift();
  1038. data.proto = parts.shift();
  1039. if (parts[parts.length - 1] === '') { // trailing whitespace
  1040. parts.pop();
  1041. }
  1042. data.fmt = parts;
  1043. return data;
  1044. },
  1045. build_mline: function (mline) {
  1046. return 'm=' + mline.media + ' ' + mline.port + ' ' + mline.proto + ' ' + mline.fmt.join(' ');
  1047. },
  1048. parse_rtpmap: function (line) {
  1049. var parts = line.substring(9).split(' '),
  1050. data = {};
  1051. data.id = parts.shift();
  1052. parts = parts[0].split('/');
  1053. data.name = parts.shift();
  1054. data.clockrate = parts.shift();
  1055. data.channels = parts.length ? parts.shift() : '1';
  1056. return data;
  1057. },
  1058. build_rtpmap: function (el) {
  1059. var line = 'a=rtpmap:' + el.getAttribute('id') + ' ' + el.getAttribute('name') + '/' + el.getAttribute('clockrate');
  1060. if (el.getAttribute('channels') && el.getAttribute('channels') != '1') {
  1061. line += '/' + el.getAttribute('channels');
  1062. }
  1063. return line;
  1064. },
  1065. parse_crypto: function (line) {
  1066. var parts = line.substring(9).split(' '),
  1067. data = {};
  1068. data.tag = parts.shift();
  1069. data['crypto-suite'] = parts.shift();
  1070. data['key-params'] = parts.shift();
  1071. if (parts.length) {
  1072. data['session-params'] = parts.join(' ');
  1073. }
  1074. return data;
  1075. },
  1076. parse_fingerprint: function (line) { // RFC 4572
  1077. var parts = line.substring(14).split(' '),
  1078. data = {};
  1079. data.hash = parts.shift();
  1080. data.fingerprint = parts.shift();
  1081. // TODO assert that fingerprint satisfies 2UHEX *(":" 2UHEX) ?
  1082. return data;
  1083. },
  1084. parse_fmtp: function (line) {
  1085. var parts = line.split(' '),
  1086. i, key, value,
  1087. data = [];
  1088. parts.shift();
  1089. parts = parts.join(' ').split(';');
  1090. for (i = 0; i < parts.length; i++) {
  1091. key = parts[i].split('=')[0];
  1092. while (key.length && key[0] == ' ') {
  1093. key = key.substring(1);
  1094. }
  1095. value = parts[i].split('=')[1];
  1096. if (key && value) {
  1097. data.push({name: key, value: value});
  1098. } else if (key) {
  1099. // rfc 4733 (DTMF) style stuff
  1100. data.push({name: '', value: key});
  1101. }
  1102. }
  1103. return data;
  1104. },
  1105. parse_icecandidate: function (line) {
  1106. var candidate = {},
  1107. elems = line.split(' ');
  1108. candidate.foundation = elems[0].substring(12);
  1109. candidate.component = elems[1];
  1110. candidate.protocol = elems[2].toLowerCase();
  1111. candidate.priority = elems[3];
  1112. candidate.ip = elems[4];
  1113. candidate.port = elems[5];
  1114. // elems[6] => "typ"
  1115. candidate.type = elems[7];
  1116. candidate.generation = 0; // default value, may be overwritten below
  1117. for (var i = 8; i < elems.length; i += 2) {
  1118. switch (elems[i]) {
  1119. case 'raddr':
  1120. candidate['rel-addr'] = elems[i + 1];
  1121. break;
  1122. case 'rport':
  1123. candidate['rel-port'] = elems[i + 1];
  1124. break;
  1125. case 'generation':
  1126. candidate.generation = elems[i + 1];
  1127. break;
  1128. default: // TODO
  1129. console.log('parse_icecandidate not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  1130. }
  1131. }
  1132. candidate.network = '1';
  1133. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  1134. return candidate;
  1135. },
  1136. build_icecandidate: function (cand) {
  1137. var line = ['a=candidate:' + cand.foundation, cand.component, cand.protocol, cand.priority, cand.ip, cand.port, 'typ', cand.type].join(' ');
  1138. line += ' ';
  1139. switch (cand.type) {
  1140. case 'srflx':
  1141. case 'prflx':
  1142. case 'relay':
  1143. if (cand.hasOwnAttribute('rel-addr') && cand.hasOwnAttribute('rel-port')) {
  1144. line += 'raddr';
  1145. line += ' ';
  1146. line += cand['rel-addr'];
  1147. line += ' ';
  1148. line += 'rport';
  1149. line += ' ';
  1150. line += cand['rel-port'];
  1151. line += ' ';
  1152. }
  1153. break;
  1154. }
  1155. line += 'generation';
  1156. line += ' ';
  1157. line += cand.hasOwnAttribute('generation') ? cand.generation : '0';
  1158. return line;
  1159. },
  1160. parse_ssrc: function (desc) {
  1161. // proprietary mapping of a=ssrc lines
  1162. // TODO: see "Jingle RTP Source Description" by Juberti and P. Thatcher on google docs
  1163. // and parse according to that
  1164. var lines = desc.split('\r\n'),
  1165. data = {};
  1166. for (var i = 0; i < lines.length; i++) {
  1167. if (lines[i].substring(0, 7) == 'a=ssrc:') {
  1168. var idx = lines[i].indexOf(' ');
  1169. data[lines[i].substr(idx + 1).split(':', 2)[0]] = lines[i].substr(idx + 1).split(':', 2)[1];
  1170. }
  1171. }
  1172. return data;
  1173. },
  1174. parse_rtcpfb: function (line) {
  1175. var parts = line.substr(10).split(' ');
  1176. var data = {};
  1177. data.pt = parts.shift();
  1178. data.type = parts.shift();
  1179. data.params = parts;
  1180. return data;
  1181. },
  1182. parse_extmap: function (line) {
  1183. var parts = line.substr(9).split(' ');
  1184. var data = {};
  1185. data.value = parts.shift();
  1186. if (data.value.indexOf('/') != -1) {
  1187. data.direction = data.value.substr(data.value.indexOf('/') + 1);
  1188. data.value = data.value.substr(0, data.value.indexOf('/'));
  1189. } else {
  1190. data.direction = 'both';
  1191. }
  1192. data.uri = parts.shift();
  1193. data.params = parts;
  1194. return data;
  1195. },
  1196. find_line: function (haystack, needle, sessionpart) {
  1197. var lines = haystack.split('\r\n');
  1198. for (var i = 0; i < lines.length; i++) {
  1199. if (lines[i].substring(0, needle.length) == needle) {
  1200. return lines[i];
  1201. }
  1202. }
  1203. if (!sessionpart) {
  1204. return false;
  1205. }
  1206. // search session part
  1207. lines = sessionpart.split('\r\n');
  1208. for (var j = 0; j < lines.length; j++) {
  1209. if (lines[j].substring(0, needle.length) == needle) {
  1210. return lines[j];
  1211. }
  1212. }
  1213. return false;
  1214. },
  1215. find_lines: function (haystack, needle, sessionpart) {
  1216. var lines = haystack.split('\r\n'),
  1217. needles = [];
  1218. for (var i = 0; i < lines.length; i++) {
  1219. if (lines[i].substring(0, needle.length) == needle)
  1220. needles.push(lines[i]);
  1221. }
  1222. if (needles.length || !sessionpart) {
  1223. return needles;
  1224. }
  1225. // search session part
  1226. lines = sessionpart.split('\r\n');
  1227. for (var j = 0; j < lines.length; j++) {
  1228. if (lines[j].substring(0, needle.length) == needle) {
  1229. needles.push(lines[j]);
  1230. }
  1231. }
  1232. return needles;
  1233. },
  1234. candidateToJingle: function (line) {
  1235. // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
  1236. // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
  1237. if (line.substring(0, 12) != 'a=candidate:') {
  1238. console.log('parseCandidate called with a line that is not a candidate line');
  1239. console.log(line);
  1240. return null;
  1241. }
  1242. if (line.substring(line.length - 2) == '\r\n') // chomp it
  1243. line = line.substring(0, line.length - 2);
  1244. var candidate = {},
  1245. elems = line.split(' '),
  1246. i;
  1247. if (elems[6] != 'typ') {
  1248. console.log('did not find typ in the right place');
  1249. console.log(line);
  1250. return null;
  1251. }
  1252. candidate.foundation = elems[0].substring(12);
  1253. candidate.component = elems[1];
  1254. candidate.protocol = elems[2].toLowerCase();
  1255. candidate.priority = elems[3];
  1256. candidate.ip = elems[4];
  1257. candidate.port = elems[5];
  1258. // elems[6] => "typ"
  1259. candidate.type = elems[7];
  1260. for (i = 8; i < elems.length; i += 2) {
  1261. switch (elems[i]) {
  1262. case 'raddr':
  1263. candidate['rel-addr'] = elems[i + 1];
  1264. break;
  1265. case 'rport':
  1266. candidate['rel-port'] = elems[i + 1];
  1267. break;
  1268. case 'generation':
  1269. candidate.generation = elems[i + 1];
  1270. break;
  1271. default: // TODO
  1272. console.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  1273. }
  1274. }
  1275. candidate.network = '1';
  1276. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  1277. return candidate;
  1278. },
  1279. candidateFromJingle: function (cand) {
  1280. var line = 'a=candidate:';
  1281. line += cand.getAttribute('foundation');
  1282. line += ' ';
  1283. line += cand.getAttribute('component');
  1284. line += ' ';
  1285. line += cand.getAttribute('protocol'); //.toUpperCase(); // chrome M23 doesn't like this
  1286. line += ' ';
  1287. line += cand.getAttribute('priority');
  1288. line += ' ';
  1289. line += cand.getAttribute('ip');
  1290. line += ' ';
  1291. line += cand.getAttribute('port');
  1292. line += ' ';
  1293. line += 'typ';
  1294. line += ' ' + cand.getAttribute('type');
  1295. line += ' ';
  1296. switch (cand.getAttribute('type')) {
  1297. case 'srflx':
  1298. case 'prflx':
  1299. case 'relay':
  1300. if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
  1301. line += 'raddr';
  1302. line += ' ';
  1303. line += cand.getAttribute('rel-addr');
  1304. line += ' ';
  1305. line += 'rport';
  1306. line += ' ';
  1307. line += cand.getAttribute('rel-port');
  1308. line += ' ';
  1309. }
  1310. break;
  1311. }
  1312. line += 'generation';
  1313. line += ' ';
  1314. line += cand.getAttribute('generation') || '0';
  1315. return line + '\r\n';
  1316. }
  1317. };
  1318. /* jshint -W117 */
  1319. // Jingle stuff
  1320. function JingleSession(me, sid, connection) {
  1321. this.me = me;
  1322. this.sid = sid;
  1323. this.connection = connection;
  1324. this.initiator = null;
  1325. this.responder = null;
  1326. this.isInitiator = null;
  1327. this.peerjid = null;
  1328. this.state = null;
  1329. this.peerconnection = null;
  1330. this.remoteStream = null;
  1331. this.localSDP = null;
  1332. this.remoteSDP = null;
  1333. this.localStreams = [];
  1334. this.relayedStreams = [];
  1335. this.remoteStreams = [];
  1336. this.startTime = null;
  1337. this.stopTime = null;
  1338. this.media_constraints = null;
  1339. this.pc_constraints = null;
  1340. this.ice_config = {},
  1341. this.drip_container = [];
  1342. this.usetrickle = true;
  1343. this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
  1344. this.usedrip = false; // dripping is sending trickle candidates not one-by-one
  1345. this.hadstuncandidate = false;
  1346. this.hadturncandidate = false;
  1347. this.lasticecandidate = false;
  1348. this.statsinterval = null;
  1349. this.reason = null;
  1350. this.addssrc = [];
  1351. this.removessrc = [];
  1352. this.wait = true;
  1353. }
  1354. JingleSession.prototype.initiate = function (peerjid, isInitiator) {
  1355. var obj = this;
  1356. if (this.state !== null) {
  1357. console.error('attempt to initiate on session ' + this.sid +
  1358. 'in state ' + this.state);
  1359. return;
  1360. }
  1361. this.isInitiator = isInitiator;
  1362. this.state = 'pending';
  1363. this.initiator = isInitiator ? this.me : peerjid;
  1364. this.responder = !isInitiator ? this.me : peerjid;
  1365. this.peerjid = peerjid;
  1366. console.log('create PeerConnection ' + JSON.stringify(this.ice_config));
  1367. try {
  1368. this.peerconnection = new RTCPeerconnection(this.ice_config,
  1369. this.pc_constraints);
  1370. console.log('Created RTCPeerConnnection');
  1371. } catch (e) {
  1372. console.error('Failed to create PeerConnection, exception: ',
  1373. e.message);
  1374. console.error(e);
  1375. return;
  1376. }
  1377. this.hadstuncandidate = false;
  1378. this.hadturncandidate = false;
  1379. this.lasticecandidate = false;
  1380. this.peerconnection.onicecandidate = function (event) {
  1381. obj.sendIceCandidate(event.candidate);
  1382. };
  1383. this.peerconnection.onaddstream = function (event) {
  1384. obj.remoteStream = event.stream;
  1385. obj.remoteStreams.push(event.stream);
  1386. $(document).trigger('remotestreamadded.jingle', [event, obj.sid]);
  1387. };
  1388. this.peerconnection.onremovestream = function (event) {
  1389. obj.remoteStream = null;
  1390. // FIXME: remove from this.remoteStreams
  1391. $(document).trigger('remotestreamremoved.jingle', [event, obj.sid]);
  1392. };
  1393. this.peerconnection.onsignalingstatechange = function (event) {
  1394. if (!(obj && obj.peerconnection)) return;
  1395. console.log('signallingstate ', obj.peerconnection.signalingState, event);
  1396. };
  1397. this.peerconnection.oniceconnectionstatechange = function (event) {
  1398. if (!(obj && obj.peerconnection)) return;
  1399. console.log('iceconnectionstatechange', obj.peerconnection.iceConnectionState, event);
  1400. switch (obj.peerconnection.iceConnectionState) {
  1401. case 'connected':
  1402. this.startTime = new Date();
  1403. break;
  1404. case 'disconnected':
  1405. this.stopTime = new Date();
  1406. break;
  1407. }
  1408. $(document).trigger('iceconnectionstatechange.jingle', [obj.sid, obj]);
  1409. };
  1410. // add any local and relayed stream
  1411. this.localStreams.forEach(function(stream) {
  1412. obj.peerconnection.addStream(stream);
  1413. });
  1414. this.relayedStreams.forEach(function(stream) {
  1415. obj.peerconnection.addStream(stream);
  1416. });
  1417. };
  1418. JingleSession.prototype.accept = function () {
  1419. var ob = this;
  1420. this.state = 'active';
  1421. var pranswer = this.peerconnection.localDescription;
  1422. if (!pranswer || pranswer.type != 'pranswer') {
  1423. return;
  1424. }
  1425. console.log('going from pranswer to answer');
  1426. if (this.usetrickle) {
  1427. // remove candidates already sent from session-accept
  1428. var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
  1429. for (var i = 0; i < lines.length; i++) {
  1430. pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
  1431. }
  1432. }
  1433. while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
  1434. // FIXME: change any inactive to sendrecv or whatever they were originally
  1435. pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
  1436. }
  1437. var prsdp = new SDP(pranswer.sdp);
  1438. var accept = $iq({to: this.peerjid,
  1439. type: 'set'})
  1440. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1441. action: 'session-accept',
  1442. initiator: this.initiator,
  1443. responder: this.responder,
  1444. sid: this.sid });
  1445. prsdp.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
  1446. this.connection.sendIQ(accept,
  1447. function () {
  1448. var ack = {};
  1449. ack.source = 'answer';
  1450. $(document).trigger('ack.jingle', [ob.sid, ack]);
  1451. },
  1452. function (stanza) {
  1453. var error = ($(stanza).find('error').length) ? {
  1454. code: $(stanza).find('error').attr('code'),
  1455. reason: $(stanza).find('error :first')[0].tagName,
  1456. }:{};
  1457. error.source = 'answer';
  1458. $(document).trigger('error.jingle', [ob.sid, error]);
  1459. },
  1460. 10000);
  1461. var sdp = this.peerconnection.localDescription.sdp;
  1462. while (SDPUtil.find_line(sdp, 'a=inactive')) {
  1463. // FIXME: change any inactive to sendrecv or whatever they were originally
  1464. sdp = sdp.replace('a=inactive', 'a=sendrecv');
  1465. }
  1466. this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
  1467. function () {
  1468. console.log('setLocalDescription success');
  1469. },
  1470. function (e) {
  1471. console.error('setLocalDescription failed', e);
  1472. }
  1473. );
  1474. };
  1475. JingleSession.prototype.terminate = function (reason) {
  1476. this.state = 'ended';
  1477. this.reason = reason;
  1478. this.peerconnection.close();
  1479. if (this.statsinterval !== null) {
  1480. window.clearInterval(this.statsinterval);
  1481. this.statsinterval = null;
  1482. }
  1483. };
  1484. JingleSession.prototype.active = function () {
  1485. return this.state == 'active';
  1486. };
  1487. JingleSession.prototype.sendIceCandidate = function (candidate) {
  1488. var ob = this;
  1489. if (candidate && !this.lasticecandidate) {
  1490. var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session);
  1491. var jcand = SDPUtil.candidateToJingle(candidate.candidate);
  1492. if (!(ice && jcand)) {
  1493. console.error('failed to get ice && jcand');
  1494. return;
  1495. }
  1496. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  1497. if (jcand.type === 'srflx') {
  1498. this.hadstuncandidate = true;
  1499. } else if (jcand.type === 'relay') {
  1500. this.hadturncandidate = true;
  1501. }
  1502. if (this.usetrickle) {
  1503. if (this.usedrip) {
  1504. if (this.drip_container.length === 0) {
  1505. // start 10ms callout
  1506. window.setTimeout(function () {
  1507. if (ob.drip_container.length === 0) return;
  1508. var allcands = ob.drip_container;
  1509. ob.drip_container = [];
  1510. var cand = $iq({to: ob.peerjid, type: 'set'})
  1511. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1512. action: 'transport-info',
  1513. initiator: ob.initiator,
  1514. sid: ob.sid});
  1515. for (var mid = 0; mid < ob.localSDP.media.length; mid++) {
  1516. var cands = allcands.filter(function (el) { return el.sdpMLineIndex == mid; });
  1517. if (cands.length > 0) {
  1518. var ice = SDPUtil.iceparams(ob.localSDP.media[mid], ob.localSDP.session);
  1519. ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  1520. cand.c('content', {creator: ob.initiator == ob.me ? 'initiator' : 'responder',
  1521. name: cands[0].sdpMid
  1522. }).c('transport', ice);
  1523. for (var i = 0; i < cands.length; i++) {
  1524. cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
  1525. }
  1526. // add fingerprint
  1527. if (SDPUtil.find_line(ob.localSDP.media[mid], 'a=fingerprint:', ob.localSDP.session)) {
  1528. var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(ob.localSDP.media[mid], 'a=fingerprint:', ob.localSDP.session));
  1529. tmp.required = true;
  1530. cand.c('fingerprint').t(tmp.fingerprint);
  1531. delete tmp.fingerprint;
  1532. cand.attrs(tmp);
  1533. cand.up();
  1534. }
  1535. cand.up(); // transport
  1536. cand.up(); // content
  1537. }
  1538. }
  1539. // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
  1540. //console.log('was this the last candidate', ob.lasticecandidate);
  1541. ob.connection.sendIQ(cand,
  1542. function () {
  1543. var ack = {};
  1544. ack.source = 'transportinfo';
  1545. $(document).trigger('ack.jingle', [ob.sid, ack]);
  1546. },
  1547. function (stanza) {
  1548. var error = ($(stanza).find('error').length) ? {
  1549. code: $(stanza).find('error').attr('code'),
  1550. reason: $(stanza).find('error :first')[0].tagName,
  1551. }:{};
  1552. error.source = 'transportinfo';
  1553. $(document).trigger('error.jingle', [ob.sid, error]);
  1554. },
  1555. 10000);
  1556. }, 10);
  1557. }
  1558. this.drip_container.push(event.candidate);
  1559. return;
  1560. }
  1561. // map to transport-info
  1562. var cand = $iq({to: this.peerjid, type: 'set'})
  1563. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1564. action: 'transport-info',
  1565. initiator: this.initiator,
  1566. sid: this.sid})
  1567. .c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
  1568. name: candidate.sdpMid
  1569. })
  1570. .c('transport', ice)
  1571. .c('candidate', jcand);
  1572. cand.up();
  1573. // add fingerprint
  1574. if (SDPUtil.find_line(this.localSDP.media[candidate.sdpMLineIndex], 'a=fingerprint:', this.localSDP.session)) {
  1575. var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(this.localSDP.media[candidate.sdpMLineIndex], 'a=fingerprint:', this.localSDP.session));
  1576. tmp.required = true;
  1577. cand.c('fingerprint').t(tmp.fingerprint);
  1578. delete tmp.fingerprint;
  1579. cand.attrs(tmp);
  1580. cand.up();
  1581. }
  1582. this.connection.sendIQ(cand,
  1583. function () {
  1584. var ack = {};
  1585. ack.source = 'transportinfo';
  1586. $(document).trigger('ack.jingle', [ob.sid, ack]);
  1587. },
  1588. function (stanza) {
  1589. console.error('transport info error');
  1590. var error = ($(stanza).find('error').length) ? {
  1591. code: $(stanza).find('error').attr('code'),
  1592. reason: $(stanza).find('error :first')[0].tagName,
  1593. }:{};
  1594. error.source = 'transportinfo';
  1595. $(document).trigger('error.jingle', [ob.sid, error]);
  1596. },
  1597. 10000);
  1598. }
  1599. } else {
  1600. console.log('sendIceCandidate: last candidate.');
  1601. if (!this.usetrickle) {
  1602. console.log('should send full offer now...');
  1603. var init = $iq({to: this.peerjid,
  1604. type: 'set'})
  1605. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1606. action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
  1607. initiator: this.initiator,
  1608. sid: this.sid});
  1609. this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
  1610. this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
  1611. this.connection.sendIQ(init,
  1612. function () {
  1613. console.log('session initiate ack');
  1614. var ack = {};
  1615. ack.source = 'offer';
  1616. $(document).trigger('ack.jingle', [ob.sid, ack]);
  1617. },
  1618. function (stanza) {
  1619. ob.state = 'error';
  1620. ob.peerconnection.close();
  1621. var error = ($(stanza).find('error').length) ? {
  1622. code: $(stanza).find('error').attr('code'),
  1623. reason: $(stanza).find('error :first')[0].tagName,
  1624. }:{};
  1625. error.source = 'offer';
  1626. $(document).trigger('error.jingle', [ob.sid, error]);
  1627. },
  1628. 10000);
  1629. }
  1630. this.lasticecandidate = true;
  1631. console.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
  1632. console.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
  1633. if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
  1634. $(document).trigger('nostuncandidates.jingle', [this.sid]);
  1635. }
  1636. }
  1637. };
  1638. JingleSession.prototype.sendOffer = function () {
  1639. console.log('sendOffer...');
  1640. var ob = this;
  1641. this.peerconnection.createOffer(function (sdp) {
  1642. ob.createdOffer(sdp);
  1643. },
  1644. function (e) {
  1645. console.error('createOffer failed', e);
  1646. },
  1647. this.media_constraints
  1648. );
  1649. };
  1650. JingleSession.prototype.createdOffer = function (sdp) {
  1651. console.log('createdOffer', sdp);
  1652. var ob = this;
  1653. this.localSDP = new SDP(sdp.sdp);
  1654. //this.localSDP.mangle();
  1655. if (this.usetrickle) {
  1656. var init = $iq({to: this.peerjid,
  1657. type: 'set'})
  1658. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1659. action: 'session-initiate',
  1660. initiator: this.initiator,
  1661. sid: this.sid});
  1662. this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
  1663. this.connection.sendIQ(init,
  1664. function () {
  1665. var ack = {};
  1666. ack.source = 'offer';
  1667. $(document).trigger('ack.jingle', [ob.sid, ack]);
  1668. },
  1669. function (stanza) {
  1670. ob.state = 'error';
  1671. ob.peerconnection.close();
  1672. var error = ($(stanza).find('error').length) ? {
  1673. code: $(stanza).find('error').attr('code'),
  1674. reason: $(stanza).find('error :first')[0].tagName,
  1675. }:{};
  1676. error.source = 'offer';
  1677. $(document).trigger('error.jingle', [ob.sid, error]);
  1678. },
  1679. 10000);
  1680. }
  1681. sdp.sdp = this.localSDP.raw;
  1682. this.peerconnection.setLocalDescription(sdp, function () {
  1683. console.log('setLocalDescription success');
  1684. },
  1685. function (e) {
  1686. console.error('setLocalDescription failed', e);
  1687. }
  1688. );
  1689. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  1690. for (var i = 0; i < cands.length; i++) {
  1691. var cand = SDPUtil.parse_icecandidate(cands[i]);
  1692. if (cand.type == 'srflx') {
  1693. this.hadstuncandidate = true;
  1694. } else if (cand.type == 'relay') {
  1695. this.hadturncandidate = true;
  1696. }
  1697. }
  1698. };
  1699. JingleSession.prototype.setRemoteDescription = function (elem, desctype) {
  1700. console.log('setting remote description... ', desctype);
  1701. this.remoteSDP = new SDP('');
  1702. this.remoteSDP.fromJingle(elem);
  1703. if (this.peerconnection.remoteDescription !== null) {
  1704. console.log('setRemoteDescription when remote description is not null, should be pranswer', this.peerconnection.remoteDescription);
  1705. if (this.peerconnection.remoteDescription.type == 'pranswer') {
  1706. var pranswer = new SDP(this.peerconnection.remoteDescription.sdp);
  1707. for (var i = 0; i < pranswer.media.length; i++) {
  1708. // make sure we have ice ufrag and pwd
  1709. if (!SDPUtil.find_line(this.remoteSDP.media[i], 'a=ice-ufrag:', this.remoteSDP.session)) {
  1710. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session)) {
  1711. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session) + '\r\n';
  1712. } else {
  1713. console.warn('no ice ufrag?');
  1714. }
  1715. if (SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session)) {
  1716. this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session) + '\r\n';
  1717. } else {
  1718. console.warn('no ice pwd?');
  1719. }
  1720. }
  1721. // copy over candidates
  1722. var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
  1723. for (var j = 0; j < lines.length; j++) {
  1724. this.remoteSDP.media[i] += lines[j] + '\r\n';
  1725. }
  1726. }
  1727. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  1728. }
  1729. }
  1730. var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
  1731. this.peerconnection.setRemoteDescription(remotedesc,
  1732. function () {
  1733. console.log('setRemoteDescription success');
  1734. },
  1735. function (e) {
  1736. console.error('setRemoteDescription error', e);
  1737. }
  1738. );
  1739. };
  1740. JingleSession.prototype.addIceCandidate = function (elem) {
  1741. var obj = this;
  1742. if (this.peerconnection.signalingState == 'closed') {
  1743. return;
  1744. }
  1745. if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
  1746. console.log('trickle ice candidate arriving before session accept...');
  1747. // create a PRANSWER for setRemoteDescription
  1748. if (!this.remoteSDP) {
  1749. var cobbled = 'v=0\r\n' +
  1750. 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
  1751. 's=-\r\n' +
  1752. 't=0 0\r\n';
  1753. // first, take some things from the local description
  1754. for (var i = 0; i < this.localSDP.media.length; i++) {
  1755. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
  1756. cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
  1757. if (SDPUtil.find_line(this.localSDP.media[i], 'a=mid:')) {
  1758. cobbled += SDPUtil.find_line(this.localSDP.media[i], 'a=mid:') + '\r\n';
  1759. }
  1760. cobbled += 'a=inactive\r\n';
  1761. }
  1762. this.remoteSDP = new SDP(cobbled);
  1763. }
  1764. // then add things like ice and dtls from remote candidate
  1765. elem.each(function () {
  1766. for (var i = 0; i < obj.remoteSDP.media.length; i++) {
  1767. if (SDPUtil.find_line(obj.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  1768. obj.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  1769. if (!SDPUtil.find_line(obj.remoteSDP.media[i], 'a=ice-ufrag:')) {
  1770. var tmp = $(this).find('transport');
  1771. obj.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
  1772. obj.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
  1773. tmp = $(this).find('transport>fingerprint');
  1774. if (tmp.length) {
  1775. obj.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
  1776. } else {
  1777. console.log('no dtls fingerprint (webrtc issue #1718?)');
  1778. obj.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
  1779. }
  1780. break;
  1781. }
  1782. }
  1783. }
  1784. });
  1785. this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
  1786. // we need a complete SDP with ice-ufrag/ice-pwd in all parts
  1787. // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
  1788. // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
  1789. var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
  1790. return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
  1791. }).length == this.remoteSDP.media.length;
  1792. if (iscomplete) {
  1793. console.log('setting pranswer');
  1794. try {
  1795. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }));
  1796. } catch (e) {
  1797. console.error('setting pranswer failed', e);
  1798. }
  1799. } else {
  1800. console.log('not yet setting pranswer');
  1801. }
  1802. }
  1803. // operate on each content element
  1804. elem.each(function () {
  1805. // would love to deactivate this, but firefox still requires it
  1806. var idx = -1;
  1807. var i;
  1808. for (i = 0; i < obj.remoteSDP.media.length; i++) {
  1809. if (SDPUtil.find_line(obj.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  1810. obj.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  1811. idx = i;
  1812. break;
  1813. }
  1814. }
  1815. if (idx == -1) { // fall back to localdescription
  1816. for (i = 0; i < obj.localSDP.media.length; i++) {
  1817. if (SDPUtil.find_line(obj.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
  1818. obj.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
  1819. idx = i;
  1820. break;
  1821. }
  1822. }
  1823. }
  1824. var name = $(this).attr('name');
  1825. // TODO: check ice-pwd and ice-ufrag?
  1826. $(this).find('transport>candidate').each(function () {
  1827. var line, candidate;
  1828. line = SDPUtil.candidateFromJingle(this);
  1829. candidate = new RTCIceCandidate({sdpMLineIndex: idx,
  1830. sdpMid: name,
  1831. candidate: line});
  1832. try {
  1833. obj.peerconnection.addIceCandidate(candidate);
  1834. } catch (e) {
  1835. console.error('addIceCandidate failed', e.toString(), line);
  1836. }
  1837. });
  1838. });
  1839. };
  1840. JingleSession.prototype.sendAnswer = function (provisional) {
  1841. console.log('createAnswer', provisional);
  1842. var ob = this;
  1843. this.peerconnection.createAnswer(
  1844. function (sdp) {
  1845. ob.createdAnswer(sdp, provisional);
  1846. },
  1847. function (e) {
  1848. console.error('createAnswer failed', e);
  1849. },
  1850. this.media_constraints
  1851. );
  1852. };
  1853. JingleSession.prototype.createdAnswer = function (sdp, provisional) {
  1854. console.log('createAnswer callback');
  1855. console.log(sdp);
  1856. var ob = this;
  1857. this.localSDP = new SDP(sdp.sdp);
  1858. //this.localSDP.mangle();
  1859. this.usepranswer = provisional === true;
  1860. if (this.usetrickle) {
  1861. if (!this.usepranswer) {
  1862. var accept = $iq({to: this.peerjid,
  1863. type: 'set'})
  1864. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1865. action: 'session-accept',
  1866. initiator: this.initiator,
  1867. responder: this.responder,
  1868. sid: this.sid });
  1869. this.localSDP.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
  1870. this.connection.sendIQ(accept,
  1871. function () {
  1872. var ack = {};
  1873. ack.source = 'answer';
  1874. $(document).trigger('ack.jingle', [ob.sid, ack]);
  1875. },
  1876. function (stanza) {
  1877. var error = ($(stanza).find('error').length) ? {
  1878. code: $(stanza).find('error').attr('code'),
  1879. reason: $(stanza).find('error :first')[0].tagName,
  1880. }:{};
  1881. error.source = 'answer';
  1882. $(document).trigger('error.jingle', [ob.sid, error]);
  1883. },
  1884. 10000);
  1885. } else {
  1886. sdp.type = 'pranswer';
  1887. for (var i = 0; i < this.localSDP.media.length; i++) {
  1888. this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
  1889. }
  1890. this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
  1891. }
  1892. }
  1893. sdp.sdp = this.localSDP.raw;
  1894. this.peerconnection.setLocalDescription(sdp,
  1895. function () {
  1896. console.log('setLocalDescription success');
  1897. },
  1898. function (e) {
  1899. console.error('setLocalDescription failed', e);
  1900. }
  1901. );
  1902. var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
  1903. for (var j = 0; j < cands.length; j++) {
  1904. var cand = SDPUtil.parse_icecandidate(cands[j]);
  1905. if (cand.type == 'srflx') {
  1906. this.hadstuncandidate = true;
  1907. } else if (cand.type == 'relay') {
  1908. this.hadturncandidate = true;
  1909. }
  1910. }
  1911. };
  1912. JingleSession.prototype.sendTerminate = function (reason, text) {
  1913. var obj = this,
  1914. term = $iq({to: this.peerjid,
  1915. type: 'set'})
  1916. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  1917. action: 'session-terminate',
  1918. initiator: this.initiator,
  1919. sid: this.sid})
  1920. .c('reason')
  1921. .c(reason || 'success');
  1922. if (text) {
  1923. term.up().c('text').t(text);
  1924. }
  1925. this.connection.sendIQ(term,
  1926. function () {
  1927. obj.peerconnection.close();
  1928. obj.peerconnection = null;
  1929. obj.terminate();
  1930. var ack = {};
  1931. ack.source = 'terminate';
  1932. $(document).trigger('ack.jingle', [obj.sid, ack]);
  1933. },
  1934. function (stanza) {
  1935. var error = ($(stanza).find('error').length) ? {
  1936. code: $(stanza).find('error').attr('code'),
  1937. reason: $(stanza).find('error :first')[0].tagName,
  1938. }:{};
  1939. $(document).trigger('ack.jingle', [obj.sid, error]);
  1940. },
  1941. 10000);
  1942. if (this.statsinterval !== null) {
  1943. window.clearInterval(this.statsinterval);
  1944. this.statsinterval = null;
  1945. }
  1946. };
  1947. JingleSession.prototype.addSource = function (elem) {
  1948. console.log('addssrc', new Date().getTime());
  1949. console.log('ice', this.peerconnection.iceConnectionState);
  1950. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  1951. var ob = this;
  1952. $(elem).each(function (idx, content) {
  1953. var name = $(content).attr('name');
  1954. var lines = '';
  1955. tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  1956. tmp.each(function () {
  1957. var ssrc = $(this).attr('ssrc');
  1958. $(this).find('>parameter').each(function () {
  1959. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  1960. if ($(this).attr('value') && $(this).attr('value').length)
  1961. lines += ':' + $(this).attr('value');
  1962. lines += '\r\n';
  1963. });
  1964. });
  1965. console.log(name, lines);
  1966. sdp.media.forEach(function(media, idx) {
  1967. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  1968. return;
  1969. sdp.media[idx] += lines;
  1970. if (!ob.addssrc[idx]) ob.addssrc[idx] = '';
  1971. ob.addssrc[idx] += lines;
  1972. });
  1973. sdp.raw = sdp.session + sdp.media.join('');
  1974. });
  1975. this.modifySources();
  1976. };
  1977. JingleSession.prototype.removeSource = function (elem) {
  1978. console.log('removessrc', new Date().getTime());
  1979. console.log('ice', this.peerconnection.iceConnectionState);
  1980. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  1981. var ob = this;
  1982. $(elem).each(function (idx, content) {
  1983. var name = $(content).attr('name');
  1984. var lines = '';
  1985. tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  1986. tmp.each(function () {
  1987. var ssrc = $(this).attr('ssrc');
  1988. $(this).find('>parameter').each(function () {
  1989. lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
  1990. if ($(this).attr('value') && $(this).attr('value').length)
  1991. lines += ':' + $(this).attr('value');
  1992. lines += '\r\n';
  1993. });
  1994. });
  1995. console.log(name, lines);
  1996. sdp.media.forEach(function(media, idx) {
  1997. if (!SDPUtil.find_line(media, 'a=mid:' + name))
  1998. return;
  1999. sdp.media[idx] += lines;
  2000. if (!ob.addssrc[idx]) ob.removessrc[idx] = '';
  2001. ob.removessrc[idx] += lines;
  2002. });
  2003. sdp.raw = sdp.session + sdp.media.join('');
  2004. });
  2005. console.log(this.removessrc);
  2006. this.modifySources();
  2007. };
  2008. JingleSession.prototype.modifySources = function() {
  2009. var ob = this;
  2010. if (!(this.addssrc.length || this.removessrc.length)) return;
  2011. if (this.peerconnection.signalingState == 'closed') return;
  2012. if (!(this.peerconnection.signalingState == 'stable' && this.peerconnection.iceConnectionState == 'connected')) {
  2013. console.warn('modifySources not yet', this.peerconnection.signalingState, this.peerconnection.iceConnectionState);
  2014. this.wait = true;
  2015. window.setTimeout(function() { ob.modifySources(); }, 250);
  2016. return;
  2017. }
  2018. if (this.wait) {
  2019. window.setTimeout(function() { ob.modifySources(); }, 2500);
  2020. this.wait = false;
  2021. return;
  2022. }
  2023. console.log('ice', this.peerconnection.iceConnectionState);
  2024. var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
  2025. // add sources
  2026. this.addssrc.forEach(function(lines, idx) {
  2027. sdp.media[idx] += lines;
  2028. });
  2029. this.addssrc = [];
  2030. // remove sources
  2031. this.removessrc.forEach(function(lines, idx) {
  2032. lines = lines.split('\r\n');
  2033. lines.pop(); // remove empty last element;
  2034. lines.forEach(function(line) {
  2035. sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
  2036. });
  2037. });
  2038. this.removessrc = [];
  2039. sdp.raw = sdp.session + sdp.media.join('');
  2040. this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
  2041. function() {
  2042. console.log('modify ok');
  2043. ob.peerconnection.createAnswer(
  2044. function(modifiedAnswer) {
  2045. console.log('modified answer...');
  2046. ob.peerconnection.setLocalDescription(modifiedAnswer,
  2047. function() {
  2048. console.log('modified setLocalDescription ok');
  2049. },
  2050. function(error) {
  2051. console.log('modified setLocalDescription failed');
  2052. }
  2053. );
  2054. },
  2055. function(error) {
  2056. console.log('modified answer failed');
  2057. }
  2058. );
  2059. },
  2060. function(error) {
  2061. console.log('modify failed');
  2062. }
  2063. );
  2064. };
  2065. JingleSession.prototype.sendMute = function (muted, content) {
  2066. var info = $iq({to: this.peerjid,
  2067. type: 'set'})
  2068. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  2069. action: 'session-info',
  2070. initiator: this.initiator,
  2071. sid: this.sid });
  2072. info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  2073. info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
  2074. if (content) {
  2075. info.attrs({'name': content});
  2076. }
  2077. this.connection.send(info);
  2078. };
  2079. JingleSession.prototype.sendRinging = function () {
  2080. var info = $iq({to: this.peerjid,
  2081. type: 'set'})
  2082. .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
  2083. action: 'session-info',
  2084. initiator: this.initiator,
  2085. sid: this.sid });
  2086. info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
  2087. this.connection.send(info);
  2088. };
  2089. JingleSession.prototype.getStats = function (interval) {
  2090. var ob = this;
  2091. var recv = {audio: 0, video: 0};
  2092. var lost = {audio: 0, video: 0};
  2093. var lastrecv = {audio: 0, video: 0};
  2094. var lastlost = {audio: 0, video: 0};
  2095. var loss = {audio: 0, video: 0};
  2096. var delta = {audio: 0, video: 0};
  2097. this.statsinterval = window.setInterval(function () {
  2098. if (ob && ob.peerconnection && ob.peerconnection.getStats) {
  2099. ob.peerconnection.getStats(function (stats) {
  2100. var results = stats.result();
  2101. // TODO: there are so much statistics you can get from this..
  2102. for (var i = 0; i < results.length; ++i) {
  2103. if (results[i].type == 'ssrc') {
  2104. var packetsrecv = results[i].stat('packetsReceived');
  2105. var packetslost = results[i].stat('packetsLost');
  2106. if (packetsrecv && packetslost) {
  2107. packetsrecv = parseInt(packetsrecv, 10);
  2108. packetslost = parseInt(packetslost, 10);
  2109. if (results[i].stat('googFrameRateReceived')) {
  2110. lastlost.video = lost.video;
  2111. lastrecv.video = recv.video;
  2112. recv.video = packetsrecv;
  2113. lost.video = packetslost;
  2114. } else {
  2115. lastlost.audio = lost.audio;
  2116. lastrecv.audio = recv.audio;
  2117. recv.audio = packetsrecv;
  2118. lost.audio = packetslost;
  2119. }
  2120. }
  2121. }
  2122. }
  2123. delta.audio = recv.audio - lastrecv.audio;
  2124. delta.video = recv.video - lastrecv.video;
  2125. loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
  2126. loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
  2127. $(document).trigger('packetloss.jingle', [ob.sid, loss]);
  2128. });
  2129. }
  2130. }, interval || 3000);
  2131. return this.statsinterval;
  2132. };