Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

initial commit

Philipp Hancke62530ef1235e
diff --git a/LICENSE b/LICENSE
index c6451cb67a..b6aa7952da 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,7 @@
The MIT License (MIT)
Copyright (c) 2013 ESTOS GmbH
+Copyright (c) 2013 BlueJimp SARL
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
diff --git a/README.md b/README.md
index a1676cb39b..a46d0205ff 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
-meet
+meet - a colibri.js sample application
====
+A WebRTC-powered multi-user videochat. For a live demo, check out either https://meet.estos.de/ or https://meet.jit.si/.
-colibri.js sample application
+Built using [colibri.js](https://github.com/ESTOS/colibri.js) and [strophe.jingle](https://github.com/ESTOS/strophe.jingle), powered by the [jitsi-videobridge](https://github.com/jitsi/jitsi-videobridge) and [prosody](http://prosody.im/).
diff --git a/app.js b/app.js
new file mode 100644
index 0000000000..d4961487d4
--- /dev/null
+++ b/app.js
@@ -0,0 +1,427 @@
+/* jshint -W117 */
+/* application specific logic */
+var connection = null;
+var focus = null;
+var RTC;
+var RTCPeerConnection = null;
+var nickname = null;
+var sharedKey = '';
+var roomUrl = null;
+
+function init() {
+ RTC = setupRTC();
+ if (RTC === null) {
+ window.location.href = '/webrtcrequired.html';
+ return;
+ } else if (RTC.browser != 'chrome') {
+ window.location.href = '/chromeonly.html';
+ return;
+ }
+ RTCPeerconnection = RTC.peerconnection;
+
+ connection = new Strophe.Connection(document.getElementById('boshURL').value || config.bosh || '/http-bind');
+ /*
+ connection.rawInput = function (data) { console.log('RECV: ' + data); };
+ connection.rawOutput = function (data) { console.log('SEND: ' + data); };
+ */
+ connection.jingle.pc_constraints = RTC.pc_constraints;
+
+ var jid = document.getElementById('jid').value || config.hosts.domain || window.location.hostname;
+
+ connection.connect(jid, document.getElementById('password').value, function (status) {
+ if (status == Strophe.Status.CONNECTED) {
+ console.log('connected');
+ getUserMediaWithConstraints(['audio', 'video'], '360');
+ document.getElementById('connect').disabled = true;
+ } else {
+ console.log('status', status);
+ }
+ });
+}
+
+function doJoin() {
+ var roomnode = null;
+ var path = window.location.pathname;
+ var roomjid;
+ if (path.length > 1) {
+ roomnode = path.substr(1).toLowerCase();
+ } else {
+ roomnode = Math.random().toString(36).substr(2, 20);
+ window.history.pushState('VideoChat', 'Room: ' + roomnode, window.location.pathname + roomnode);
+ }
+ roomjid = roomnode + '@' + config.hosts.muc;
+
+ if (config.useNicks) {
+ var nick = window.prompt('Your nickname (optional)');
+ if (nick) {
+ roomjid += '/' + nick;
+ } else {
+ roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
+ }
+ } else {
+ roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
+ }
+ connection.emuc.doJoin(roomjid);
+}
+
+$(document).bind('mediaready.jingle', function (event, stream) {
+ connection.jingle.localStream = stream;
+ RTC.attachMediaStream($('#localVideo'), stream);
+ document.getElementById('localVideo').muted = true;
+ document.getElementById('localVideo').autoplay = true;
+ document.getElementById('localVideo').volume = 0;
+
+ document.getElementById('largeVideo').volume = 0;
+ document.getElementById('largeVideo').src = document.getElementById('localVideo').src;
+ doJoin();
+});
+
+$(document).bind('mediafailure.jingle', function () {
+ // FIXME
+});
+
+$(document).bind('remotestreamadded.jingle', function (event, data, sid) {
+ function waitForRemoteVideo(selector, sid) {
+ var sess = connection.jingle.sessions[sid];
+ videoTracks = data.stream.getVideoTracks();
+ if (videoTracks.length === 0 || selector[0].currentTime > 0) {
+ RTC.attachMediaStream(selector, data.stream); // FIXME: why do i have to do this for FF?
+ $(document).trigger('callactive.jingle', [selector, sid]);
+ console.log('waitForremotevideo', sess.peerconnection.iceConnectionState, sess.peerconnection.signalingState);
+ } else {
+ setTimeout(function () { waitForRemoteVideo(selector, sid); }, 100);
+ }
+ }
+ var sess = connection.jingle.sessions[sid];
+ var vid = document.createElement('video');
+ var id = 'remoteVideo_' + sid + '_' + data.stream.id;
+ vid.id = id;
+ vid.autoplay = true;
+ vid.oncontextmenu = function () { return false; };
+ var remotes = document.getElementById('remoteVideos');
+ remotes.appendChild(vid);
+ var sel = $('#' + id);
+ sel.hide();
+ RTC.attachMediaStream(sel, data.stream);
+ waitForRemoteVideo(sel, sid);
+ data.stream.onended = function () {
+ console.log('stream ended', this.id);
+ var src = $('#' + id).attr('src');
+ $('#' + id).remove();
+ if (src === $('#largeVideo').attr('src')) {
+ // this is currently displayed as large
+ // pick the last visible video in the row
+ // if nobody else is left, this picks the local video
+ var pick = $('#remoteVideos :visible:last').get(0);
+ // mute if localvideo
+ document.getElementById('largeVideo').volume = pick.volume;
+ document.getElementById('largeVideo').src = pick.src;
+ }
+ resizeThumbnails();
+ };
+ sel.click(
+ function () {
+ console.log('hover in', $(this).attr('src'));
+ var newSrc = $(this).attr('src');
+ if ($('#largeVideo').attr('src') != newSrc) {
+ document.getElementById('largeVideo').volume = 1;
+ $('#largeVideo').fadeOut(300, function () {
+ $(this).attr('src', newSrc);
+ $(this).fadeIn(300);
+ });
+ }
+ }
+ );
+});
+
+$(document).bind('callincoming.jingle', function (event, sid) {
+ var sess = connection.jingle.sessions[sid];
+ // TODO: check affiliation and/or role
+ console.log('emuc data for', sess.peerjid, connection.emuc.members[sess.peerjid]);
+ sess.sendAnswer();
+ sess.accept();
+});
+
+$(document).bind('callactive.jingle', function (event, videoelem, sid) {
+ console.log('call active');
+ if (videoelem.attr('id').indexOf('mixedmslabel') == -1) {
+ // ignore mixedmslabela0 and v0
+ videoelem.show();
+ resizeThumbnails();
+
+ document.getElementById('largeVideo').volume = 1;
+ $('#largeVideo').attr('src', videoelem.attr('src'));
+ }
+});
+
+$(document).bind('callterminated.jingle', function (event, sid, reason) {
+ // FIXME
+});
+
+
+$(document).bind('joined.muc', function (event, jid, info) {
+ console.log('onJoinComplete', info);
+ updateRoomUrl(window.location.href);
+ if (Object.keys(connection.emuc.members).length < 1) {
+ focus = new ColibriFocus(connection, config.hosts.bridge);
+ return;
+ }
+});
+
+$(document).bind('entered.muc', function (event, jid, info) {
+ console.log('entered', jid, info);
+ console.log(focus);
+ if (focus !== null) {
+ // FIXME: this should prepare the video
+ if (focus.confid === null) {
+ console.log('make new conference with', jid);
+ focus.makeConference(Object.keys(connection.emuc.members));
+ } else {
+ console.log('invite', jid, 'into conference');
+ focus.addNewParticipant(jid);
+ }
+ }
+});
+
+$(document).bind('left.muc', function (event, jid) {
+ console.log('left', jid);
+ connection.jingle.terminateByJid(jid);
+ // FIXME: this should actually hide the video already for a nicer UX
+
+ if (Object.keys(connection.emuc.members).length === 0) {
+ console.log('everyone left');
+ if (focus !== null) {
+ // FIXME: closing the connection is a hack to avoid some
+ // problemswith reinit
+ if (focus.peerconnection !== null) {
+ focus.peerconnection.close();
+ }
+ focus = new ColibriFocus(connection, config.hosts.bridge);
+ }
+ }
+});
+
+function toggleVideo() {
+ if (!(connection && connection.jingle.localStream)) return;
+ for (var idx = 0; idx < connection.jingle.localStream.getVideoTracks().length; idx++) {
+ connection.jingle.localStream.getVideoTracks()[idx].enabled = !connection.jingle.localStream.getVideoTracks()[idx].enabled;
+ }
+}
+
+function toggleAudio() {
+ if (!(connection && connection.jingle.localStream)) return;
+ for (var idx = 0; idx < connection.jingle.localStream.getAudioTracks().length; idx++) {
+ connection.jingle.localStream.getAudioTracks()[idx].enabled = !connection.jingle.localStream.getAudioTracks()[idx].enabled;
+ }
+}
+
+function resizeLarge() {
+ var availableHeight = window.innerHeight;
+ var chatspaceWidth = $('#chatspace').width();
+
+ var numvids = $('#remoteVideos>video:visible').length;
+ if (numvids < 5)
+ availableHeight -= 100; // min thumbnail height for up to 4 videos
+ else
+ availableHeight -= 50; // min thumbnail height for more than 5 videos
+
+ availableHeight -= 79; // padding + link ontop
+ var availableWidth = window.innerWidth - chatspaceWidth;
+ var aspectRatio = 16.0 / 9.0;
+ if (availableHeight < availableWidth / aspectRatio) {
+ availableWidth = Math.floor(availableHeight * aspectRatio);
+ }
+ if (availableWidth < 0 || availableHeight < 0) return;
+ $('#largeVideo').width(availableWidth);
+ $('#largeVideo').height(availableWidth / aspectRatio);
+ resizeThumbnails();
+}
+
+function resizeThumbnails() {
+ // Calculate the available height, which is the inner window height minus 39px for the header
+ // minus 4px for the delimiter lines on the top and bottom of the large video,
+ // minus the 36px space inside the remoteVideos container used for highlighting shadow.
+ var availableHeight = window.innerHeight - $('#largeVideo').height() - 79;
+ var numvids = $('#remoteVideos>video:visible').length;
+ // Remove the 1px borders arround videos.
+ var availableWinWidth = $('#remoteVideos').width() - 2 * numvids;
+ var availableWidth = availableWinWidth / numvids;
+ var aspectRatio = 16.0 / 9.0;
+ var maxHeight = Math.min(160, availableHeight);
+ availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
+ if (availableHeight < availableWidth / aspectRatio) {
+ availableWidth = Math.floor(availableHeight * aspectRatio);
+ }
+ // size videos so that while keeping AR and max height, we have a nice fit
+ $('#remoteVideos').height(availableHeight + 36); // add the 2*18px border used for highlighting shadow.
+ $('#remoteVideos>video:visible').width(availableWidth);
+ $('#remoteVideos>video:visible').height(availableHeight);
+}
+
+$(document).ready(function () {
+ $('#nickinput').keydown(function(event) {
+ if (event.keyCode == 13) {
+ event.preventDefault();
+ var val = this.value;
+ this.value = '';
+ if (!nickname) {
+ nickname = val;
+ $('#nickname').css({visibility:"hidden"});
+ $('#chatconversation').css({visibility:'visible'});
+ $('#usermsg').css({visibility:'visible'});
+ $('#usermsg').focus();
+ return;
+ }
+ }
+ });
+
+ $('#usermsg').keydown(function(event) {
+ if (event.keyCode == 13) {
+ event.preventDefault();
+ var message = this.value;
+ $('#usermsg').val('').trigger('autosize.resize');
+ this.focus();
+ connection.emuc.sendMessage(message, nickname);
+ }
+ });
+
+ $('#usermsg').autosize();
+
+ resizeLarge();
+ $(window).resize(function () {
+ resizeLarge();
+ });
+ if (!$('#settings').is(':visible')) {
+ console.log('init');
+ init();
+ } else {
+ loginInfo.onsubmit = function (e) {
+ if (e.preventDefault) e.preventDefault();
+ $('#settings').hide();
+ init();
+ };
+ }
+});
+
+$(window).bind('beforeunload', function () {
+ if (connection && connection.connected) {
+ // ensure signout
+ $.ajax({
+ type: 'POST',
+ url: config.bosh,
+ async: false,
+ cache: false,
+ contentType: 'application/xml',
+ data: "<body rid='" + (connection.rid || connection._proto.rid) + "' xmlns='http://jabber.org/protocol/httpbind' sid='" + (connection.sid || connection._proto.sid) + "' type='terminate'><presence xmlns='jabber:client' type='unavailable'/></body>",
+ success: function (data) {
+ console.log('signed out');
+ console.log(data);
+ },
+ error: function (XMLHttpRequest, textStatus, errorThrown) {
+ console.log('signout error', textStatus + ' (' + errorThrown + ')');
+ }
+ });
+ }
+});
+
+function updateChatConversation(nick, message)
+{
+ var divClassName = '';
+ if (nickname == nick)
+ divClassName = "localuser";
+ else
+ divClassName = "remoteuser";
+
+ $('#chatconversation').append('<div class="' + divClassName + '"><b>' + nick + ': </b>' + message + '</div>');
+ $('#chatconversation').animate({ scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
+}
+
+function buttonClick(id, classname) {
+ $(id).toggleClass(classname); // add the class to the clicked element
+}
+
+function openLockDialog() {
+ if (sharedKey)
+ $.prompt("Are you sure you would like to remove your secret key?",
+ {
+ title: "Remove secrect key",
+ persistent: false,
+ buttons: { "Remove": true, "Cancel": false},
+ defaultButton: 1,
+ submit: function(e,v,m,f){
+ if(v)
+ {
+ sharedKey = '';
+ lockRoom();
+ }
+ }
+ });
+ else
+ $.prompt('<h2>Set a secrect key to lock your room</h2>' +
+ '<input id="lockKey" type="text" placeholder="your shared key" autofocus>',
+ {
+ persistent: false,
+ buttons: { "Save": true , "Cancel": false},
+ defaultButton: 1,
+ loaded: function(event) {
+ document.getElementById('lockKey').focus();
+ },
+ submit: function(e,v,m,f){
+ if(v)
+ {
+ var lockKey = document.getElementById('lockKey');
+
+ if (lockKey.value != null)
+ {
+ sharedKey = lockKey.value;
+ lockRoom(true);
+ }
+ }
+ }
+ });
+}
+
+function openLinkDialog() {
+ $.prompt('<input id="inviteLinkRef" type="text" value="' + roomUrl + '" onclick="this.select();">',
+ {
+ title: "Share this link with everyone you want to invite",
+ persistent: false,
+ buttons: { "Cancel": false},
+ loaded: function(event) {
+ document.getElementById('inviteLinkRef').select();
+ }
+ });
+}
+
+function lockRoom(lock) {
+ connection.emuc.lockRoom(sharedKey);
+
+ buttonClick("#lockIcon", "fa fa-unlock fa-lg fa fa-lock fa-lg");
+}
+
+function openChat() {
+ var chatspace = $('#chatspace');
+ var videospace = $('#videospace');
+ var chatspaceWidth = chatspace.width();
+
+ if (chatspace.css("opacity") == 1) {
+ chatspace.animate({opacity: 0}, "fast");
+ chatspace.animate({width: 0}, "slow");
+ videospace.animate({right: 0, width:"100%"}, "slow");
+ }
+ else {
+ chatspace.animate({width:"20%"}, "slow");
+ chatspace.animate({opacity: 1}, "slow");
+ videospace.animate({right:chatspaceWidth, width:"80%"}, "slow");
+ }
+
+ // Request the focus in the nickname field or the chat input field.
+ if ($('#nickinput').is(':visible'))
+ $('#nickinput').focus();
+ else
+ $('#usermsg').focus();
+}
+
+function updateRoomUrl(newRoomUrl) {
+ roomUrl = newRoomUrl;
+}
diff --git a/chromeonly.html b/chromeonly.html
new file mode 100644
index 0000000000..268bbc2cea
--- /dev/null
+++ b/chromeonly.html
@@ -0,0 +1 @@
+Sorry, this currently only works with chrome because it uses "Plan B".
diff --git a/config.js b/config.js
new file mode 100644
index 0000000000..5d8363c49f
--- /dev/null
+++ b/config.js
@@ -0,0 +1,9 @@
+var config = {
+ hosts: {
+ domain: 'your.domain.example',
+ muc: 'conference.your.domain.example', // FIXME: use XEP-0030
+ bridge: 'jitsi-videobridge.your.domain.example' // FIXME: use XEP-0030
+ },
+ useNicks: false,
+ bosh: '/http-bind' // FIXME: use xep-0156 for that
+};
diff --git a/css/jquery-impromptu.css b/css/jquery-impromptu.css
new file mode 100644
index 0000000000..dd5545b11a
--- /dev/null
+++ b/css/jquery-impromptu.css
@@ -0,0 +1,125 @@
+/*
+------------------------------
+ Impromptu
+------------------------------
+*/
+.jqifade{
+ position: absolute;
+ background-color: #000;
+}
+div.jqi{
+ width: 400px;
+ font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
+ position: absolute;
+ background-color: #ffffff;
+ font-size: 11px;
+ text-align: left;
+ border: solid 1px #eeeeee;
+ border-radius: 6px;
+ -moz-border-radius: 6px;
+ -webkit-border-radius: 6px;
+ padding: 7px;
+}
+div.jqi .jqicontainer{
+}
+div.jqi .jqiclose{
+ position: absolute;
+ top: 4px; right: -2px;
+ width: 18px;
+ cursor: default;
+ color: #bbbbbb;
+ font-weight: bold;
+}
+div.jqi .jqistate{
+ background-color: #fff;
+}
+div.jqi .jqititle{
+ padding: 5px 10px;
+ font-size: 16px;
+ line-height: 20px;
+ border-bottom: solid 1px #eeeeee;
+}
+div.jqi .jqimessage{
+ padding: 10px;
+ line-height: 20px;
+ color: #444444;
+}
+div.jqi .jqibuttons{
+ text-align: right;
+ margin: 0 -7px -7px -7px;
+ border-top: solid 1px #e4e4e4;
+ background-color: #f4f4f4;
+ border-radius: 0 0 6px 6px;
+ -moz-border-radius: 0 0 6px 6px;
+ -webkit-border-radius: 0 0 6px 6px;
+}
+div.jqi .jqibuttons button{
+ margin: 0;
+ padding: 5px 20px;
+ background-color: transparent;
+ font-weight: normal;
+ border: none;
+ border-left: solid 1px #e4e4e4;
+ color: #777;
+ font-weight: bold;
+ font-size: 12px;
+}
+div.jqi .jqibuttons button.jqidefaultbutton{
+ color: #489afe;
+}
+div.jqi .jqibuttons button:hover,
+div.jqi .jqibuttons button:focus{
+ color: #287ade;
+ outline: none;
+}
+.jqiwarning .jqi .jqibuttons{
+ background-color: #b95656;
+}
+
+/* sub states */
+div.jqi .jqiparentstate::after{
+ background-color: #777;
+ opacity: 0.6;
+ filter: alpha(opacity=60);
+ content: '';
+ position: absolute;
+ top:0;left:0;bottom:0;right:0;
+ border-radius: 6px;
+ -moz-border-radius: 6px;
+ -webkit-border-radius: 6px;
+}
+div.jqi .jqisubstate{
+ position: absolute;
+ top:0;
+ left: 20%;
+ width: 60%;
+ padding: 7px;
+ border: solid 1px #eeeeee;
+ border-top: none;
+ border-radius: 0 0 6px 6px;
+ -moz-border-radius: 0 0 6px 6px;
+ -webkit-border-radius: 0 0 6px 6px;
+}
+div.jqi .jqisubstate .jqibuttons button{
+ padding: 10px 18px;
+}
+
+/* arrows for tooltips/tours */
+.jqi .jqiarrow{ position: absolute; height: 0; width:0; line-height: 0; font-size: 0; border: solid 10px transparent;}
+
+.jqi .jqiarrowtl{ left: 10px; top: -20px; border-bottom-color: #ffffff; }
+.jqi .jqiarrowtc{ left: 50%; top: -20px; border-bottom-color: #ffffff; margin-left: -10px; }
+.jqi .jqiarrowtr{ right: 10px; top: -20px; border-bottom-color: #ffffff; }
+
+.jqi .jqiarrowbl{ left: 10px; bottom: -20px; border-top-color: #ffffff; }
+.jqi .jqiarrowbc{ left: 50%; bottom: -20px; border-top-color: #ffffff; margin-left: -10px; }
+.jqi .jqiarrowbr{ right: 10px; bottom: -20px; border-top-color: #ffffff; }
+
+.jqi .jqiarrowlt{ left: -20px; top: 10px; border-right-color: #ffffff; }
+.jqi .jqiarrowlm{ left: -20px; top: 50%; border-right-color: #ffffff; margin-top: -10px; }
+.jqi .jqiarrowlb{ left: -20px; bottom: 10px; border-right-color: #ffffff; }
+
+.jqi .jqiarrowrt{ right: -20px; top: 10px; border-left-color: #ffffff; }
+.jqi .jqiarrowrm{ right: -20px; top: 50%; border-left-color: #ffffff; margin-top: -10px; }
+.jqi .jqiarrowrb{ right: -20px; bottom: 10px; border-left-color: #ffffff; }
+
diff --git a/css/main.css b/css/main.css
new file mode 100644
index 0000000000..bac3ec7248
--- /dev/null
+++ b/css/main.css
@@ -0,0 +1,318 @@
+html, body{
+ margin:0px;
+ height:100%;
+ color: #424242;
+ font-family:'YanoneKaffeesatzLight',Verdana,Tahoma,Arial;
+ font-weight: 400;
+ background: #e9e9e9;
+}
+
+
+#videospace {
+ display: block;
+ position: absolute;
+ top: 39px;
+ left: 0px;
+ right: 0px;
+ float: left;
+}
+
+#largeVideo {
+ display:block;
+ position:relative;
+ width:1280px;
+ height:720px;
+ margin-left:auto;
+ margin-right:auto;
+ z-index: 0;
+}
+
+#remoteVideos {
+ display:block;
+ position:relative;
+ text-align:center;
+ height:196px;
+ width:auto;
+ overflow: hidden;
+ border:1px solid transparent;
+ font-size:0;
+ z-index: 2;
+}
+
+#remoteVideos video {
+ position:relative;
+ top:18px;
+ height:160px;
+ width:auto;
+ z-index:0;
+ border:1px solid #FFFFFF;
+}
+
+#remoteVideos video:hover {
+ cursor: pointer;
+ cursor: hand;
+ transform:scale(1.08, 1.08);
+ -webkit-transform:scale(1.08, 1.08);
+ transition-duration: 0.5s;
+ -webkit-transition-duration: 0.5s;
+ background-color: #FFFFFF;
+ -webkit-animation-name: greyPulse;
+ -webkit-animation-duration: 2s;
+ -webkit-animation-iteration-count: 1;
+ -webkit-box-shadow: 0 0 18px #515151;
+ border:1px solid #FFFFFF;
+ z-index: 10;
+}
+
+#chatspace {
+ display:block;
+ position:absolute;
+ float: right;
+ top: 40px;
+ bottom: 0px;
+ right: 0px;
+ width:0;
+ opacity: 0;
+ overflow: hidden;
+ background-color:#f6f6f6;
+ border-left:1px solid #424242;
+}
+
+#chatconversation {
+ display:block;
+ position:relative;
+ top: -120px;
+ float:top;
+ text-align:left;
+ line-height:20px;
+ font-size:14px;
+ padding:5px;
+ height:90%;
+ overflow:scroll;
+ visibility:hidden;
+}
+
+div.localuser {
+ color: #087dba;
+}
+
+div.remoteuser {
+ color: #424242;
+}
+
+#usermsg {
+ position:absolute;
+ bottom: 5px;
+ left: 5px;
+ right: 5px;
+ width: 95%;
+ height: 40px;
+ z-index: 5;
+ visibility:hidden;
+ max-height:150px;
+}
+
+#nickname {
+ position:relative;
+ text-align:center;
+ color: #9d9d9d;
+ font-size: 18;
+ top: 100px;
+ left: 5px;
+ right: 5px;
+ width: 95%;
+}
+
+#nickinput {
+ margin-top: 20px;
+ font-size: 14;
+}
+
+div#spacer {
+ height:5px;
+}
+
+#settings {
+ display:none;
+}
+
+#nowebrtc {
+ display:none;
+}
+
+div#header{
+ display:block;
+ position:relative;
+ width:100%;
+ height:39px;
+ z-index: 1;
+ text-align:center;
+ background-color:#087dba;
+}
+
+div#left {
+ display:block;
+ position: absolute;
+ left: 0px;
+ top: 0px;
+ width: 100px;
+ height: 39px;
+ background-image:url(../images/left1.png);
+ background-repeat:no-repeat;
+ margin: 0;
+ padding: 0;
+}
+
+div#leftlogo {
+ position:absolute;
+ top: 5px;
+ left: 15px;
+ background-image:url(../images/jitsilogo.png);
+ background-repeat:no-repeat;
+ height: 31px;
+ width: 68px;
+ z-index:1;
+}
+
+div#link {
+ display:block;
+ position:relative;
+ height:39px;
+ width:auto;
+ overflow: hidden;
+ z-index:0;
+}
+
+.button {
+ display: inline-block;
+ position: relative;
+ color: #FFFFFF;
+ top: 0;
+ padding: 10px 0px;
+ height: 19px;
+ width: 39px;
+ cursor: pointer;
+ font-size: 19px;
+ text-align: center;
+ text-shadow: 0px 1px 0px rgba(255,255,255,.3), 0px -1px 0px rgba(0,0,0,.7);
+}
+
+a.button:hover {
+ top: 0;
+ cursor: pointer;
+ background: rgba(0, 0, 0, 0.3);
+ border-radius: 5px;
+ background-clip: padding-box;
+ -webkit-border-radius: 5px;
+ -webkit-background-clip: padding-box;
+}
+
+.no-fa-video-camera, .fa-microphone-slash {
+ color: #636363;
+}
+
+.fade_line {
+ height: 1px;
+ background: black;
+ background: -webkit-gradient(linear, 0 0, 100% 0, from(#e9e9e9), to(#e9e9e9), color-stop(50%, black));
+}
+
+.header_button_separator {
+ display: inline-block;
+ position:relative;
+ top: 7;
+ width: 1px;
+ height: 25px;
+ background: white;
+ background: -webkit-gradient(linear, 0 0, 0 100%, from(#087dba), to(#087dba), color-stop(50%, white));
+}
+
+div#right {
+ display:block;
+ position:absolute;
+ right: 0px;
+ top: 0px;
+ background-image:url(../images/right1.png);
+ background-repeat:no-repeat;
+ margin:0;
+ padding:0;
+ width:100px;
+ height:39px;
+}
+div#rightlogo {
+ position:absolute;
+ top: 6px;
+ right: 15px;
+ background-image:url(../images/estoslogo.png);
+ background-repeat:no-repeat;
+ height: 25px;
+ width: 62px;
+ z-index:1;
+}
+
+input, textarea {
+ border: 0px none;
+ display: inline-block;
+ font-size: 14px;
+ padding: 5px;
+ background: #f3f3f3;
+ border-radius: 3px;
+ font-weight: 100;
+ line-height: 20px;
+ height: 40px;
+ color: #333;
+ font-weight: bold;
+ text-align: left;
+ border:1px solid #ACD8F0;
+ outline: none; /* removes the default outline */
+ resize: none; /* prevents the user-resizing, adjust to taste */
+}
+
+input, textarea:focus {
+ box-shadow: inset 0 0 3px 2px #ACD8F0; /* provides a more style-able
+ replacement to the outline */
+}
+
+textarea {
+ overflow: hidden;
+ word-wrap: break-word;
+ resize: horizontal;
+}
+
+button.no-icon {
+ padding: 0 1em;
+}
+
+button {
+ border: none;
+ height: 35px;
+ padding: 0 1em 0 2em;
+ position: relative;
+ border-radius: 3px;
+ font-weight: bold;
+ color: #fff;
+ line-height: 35px;
+ background: #2c8ad2;
+}
+
+button, input, select, textarea {
+ font-size: 100%;
+ margin: 0;
+ vertical-align: baseline;
+}
+
+button, input[type="button"], input[type="reset"], input[type="submit"] {
+ cursor: pointer;
+ -webkit-appearance: button;
+}
+
+form {
+ display: block;
+}
+
+/* Animated text area. */
+.animated {
+ -webkit-transition: height 0.2s;
+ -moz-transition: height 0.2s;
+ transition: height 0.2s;
+}
diff --git a/css/modaldialog.css b/css/modaldialog.css
new file mode 100644
index 0000000000..a1e8f854b5
--- /dev/null
+++ b/css/modaldialog.css
@@ -0,0 +1,22 @@
+.jqistates h2 {
+ padding-bottom: 10px;
+ border-bottom: 1px solid #eee;
+ font-size: 18px;
+ line-height: 25px;
+ text-align: center;
+ color: #424242;
+}
+
+.jqistates input {
+ width: 100%;
+ margin: 20px 0;
+}
+
+.jqibuttons button {
+ margin-right: 5px;
+ float:right;
+}
+
+button.jqidefaultbutton #inviteLinkRef {
+ color: #2c8ad2;
+}
\ No newline at end of file
diff --git a/images/estoslogo.png b/images/estoslogo.png
new file mode 100644
index 0000000000..085435f173
Binary files /dev/null and b/images/estoslogo.png differ
diff --git a/images/jitsilogo.png b/images/jitsilogo.png
new file mode 100644
index 0000000000..51a385452f
Binary files /dev/null and b/images/jitsilogo.png differ
diff --git a/index.html b/index.html
new file mode 100644
index 0000000000..0c5f2dc7cd
--- /dev/null
+++ b/index.html
@@ -0,0 +1,68 @@
+<html>
+ <head>
+ <title>WebRTC, meet the Jitsi Videobridge</title>
+ <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
+ <script src="libs/strophejingle.bundle.js"></script><!-- strophe.jingle bundle -->
+ <script src="libs/colibri.js"></script><!-- colibri focus implementation -->
+ <script src="muc.js"></script><!-- simple MUC library -->
+ <script src="app.js"></script><!-- application logic -->
+ <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
+ <link rel="stylesheet" type="text/css" media="screen" href="css/main.css" />
+ <link rel="stylesheet" href="css/jquery-impromptu.css">
+ <link rel="stylesheet" href="css/modaldialog.css">
+ <script src="libs/jquery-impromptu.js"></script>
+ <script src="libs/jquery.autosize.js"></script>
+ <script src="config.js"></script><!-- adapt to your needs, i.e. set hosts and bosh path -->
+ </head>
+ <body>
+ <div id="header">
+ <a href="http://jitsi.org" target="_blank"><div id="leftlogo"></div></a>
+ <a href="http://www.estos.com/" target="_blank"><div id="rightlogo"></div></a>
+ <div id="link">
+ <a class="button" onclick='buttonClick("#mute", "fa fa-microphone fa-lg fa fa-microphone-slash fa-lg");toggleAudio();'><i id="mute" title="Mute / unmute" class="fa fa-microphone fa-lg"></i></a>
+ <div class="header_button_separator"></div>
+ <a class="button" onclick='buttonClick("#video", "fa fa-video-camera fa-lg fa fa-video-camera no-fa-video-camera fa-lg");toggleVideo();'><i id="video" title="Start / stop camera" class="fa fa-video-camera fa-lg"></i></a>
+ <div class="header_button_separator"></div>
+ <a class="button" onclick="openLockDialog();"><i id="lockIcon" title="Lock/unlock room" class="fa fa-unlock fa-lg"></i></a>
+ <div class="header_button_separator"></div>
+ <a class="button" onclick="openLinkDialog();"><i title="Invite others" class="fa fa-link fa-lg"></i></a>
+ <div class="header_button_separator"></div>
+ <a class="button" onclick='openChat();'><i id="chat" title="Open chat" class="fa fa-comments fa-lg"></i></a>
+ <!--i class='fa fa-external-link'>&nbsp;</i>Others can join you by just going to <span id='roomurl'></span-->
+ </div>
+ </div>
+ <div id="settings">
+ <h1>Connection Settings</h1>
+ <form id="loginInfo">
+ <label>JID: <input id="jid" type="text" name="jid" placeholder="[email protected]"/></label>
+ <label>Password: <input id="password" type="password" name="password" placeholder="secret"/></label>
+ <label>BOSH URL: <input id="boshURL" type="text" name="boshURL" placeholder="/http-bind"/></label>
+ <input id="connect" type="submit" value="Connect" />
+ </form>
+ </div>
+
+ <div id="videospace">
+ <div class="fade_line"></div>
+ <video id="largeVideo" autoplay oncontextmenu="return false;"></video>
+ <div class="fade_line"></div>
+ <div id="remoteVideos">
+ <video id="localVideo" autoplay oncontextmenu="return false;" muted/>
+ </div>
+ </div>
+ <div id="chatspace">
+ <div id="nickname">
+ Enter a nickname in the box below
+ <form>
+ <input type='text' id="nickinput" placeholder='Choose a nickname' autofocus>
+ </form>
+ </div>
+
+ <!--div><i class="fa fa-comments">&nbsp;</i><span class='nick'></span>:&nbsp;<span class='chattext'></span></div-->
+ <div id="chatconversation"></div>
+ <textarea id="usermsg" class= "animated" placeholder='Enter text...' autofocus></textarea>
+ </div>
+
+ <script>
+ </script>
+ </body>
+</html>
diff --git a/libs/colibri.js b/libs/colibri.js
new file mode 100644
index 0000000000..0d14ddf128
--- /dev/null
+++ b/libs/colibri.js
@@ -0,0 +1,814 @@
+/* colibri.js -- a COLIBRI focus
+ * The colibri spec has been submitted to the XMPP Standards Foundation
+ * for publications as a XMPP extensions:
+ * http://xmpp.org/extensions/inbox/colibri.html
+ *
+ * colibri.js is a participating focus, i.e. the focus participates
+ * in the conference. The conference itself can be ad-hoc, through a
+ * MUC, through PubSub, etc.
+ *
+ * colibri.js relies heavily on the strophe.jingle library available
+ * from https://github.com/ESTOS/strophe.jingle
+ * and interoperates with the Jitsi videobridge available from
+ * https://jitsi.org/Projects/JitsiVideobridge
+ */
+/*
+Copyright (c) 2013 ESTOS GmbH
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+/* jshint -W117 */
+function ColibriFocus(connection, bridgejid) {
+ this.connection = connection;
+ this.bridgejid = bridgejid;
+ this.peers = [];
+ this.confid = null;
+
+ this.peerconnection = null;
+
+ this.sid = Math.random().toString(36).substr(2, 12);
+ this.connection.jingle.sessions[this.sid] = this;
+ this.mychannel = [];
+ this.channels = [];
+ this.remotessrc = {};
+
+ // ssrc lines to be added on next update
+ this.addssrc = [];
+ // ssrc lines to be removed on next update
+ this.removessrc = [];
+
+ // silly wait flag
+ this.wait = true;
+}
+
+// creates a conferences with an initial set of peers
+ColibriFocus.prototype.makeConference = function (peers) {
+ var ob = this;
+ if (this.confid !== null) {
+ console.error('makeConference called twice? Ignoring...');
+ // FIXME: just invite peers?
+ return;
+ }
+ this.confid = 0; // !null
+ this.peers = [];
+ peers.forEach(function (peer) {
+ ob.peers.push(peer);
+ ob.channels.push([]);
+ });
+
+ this.peerconnection = new RTC.peerconnection(this.connection.jingle.ice_config, this.connection.jingle.pc_constraints);
+ this.peerconnection.addStream(this.connection.jingle.localStream);
+ this.peerconnection.oniceconnectionstatechange = function (event) {
+ console.warn('ice connection state changed to', ob.peerconnection.iceConnectionState);
+ /*
+ if (ob.peerconnection.signalingState == 'stable' && ob.peerconnection.iceConnectionState == 'connected') {
+ console.log('adding new remote SSRCs from iceconnectionstatechange');
+ window.setTimeout(function() { ob.modifySources(); }, 1000);
+ }
+ */
+ };
+ this.peerconnection.onsignalingstatechange = function (event) {
+ console.warn(ob.peerconnection.signalingState);
+ /*
+ if (ob.peerconnection.signalingState == 'stable' && ob.peerconnection.iceConnectionState == 'connected') {
+ console.log('adding new remote SSRCs from signalingstatechange');
+ window.setTimeout(function() { ob.modifySources(); }, 1000);
+ }
+ */
+ };
+ this.peerconnection.onaddstream = function (event) {
+ ob.remoteStream = event.stream;
+ $(document).trigger('remotestreamadded.jingle', [event, ob.sid]);
+ };
+ this.peerconnection.onicecandidate = function (event) {
+ ob.sendIceCandidate(event.candidate);
+ };
+ this.peerconnection.createOffer(
+ function (offer) {
+ ob.peerconnection.setLocalDescription(
+ offer,
+ function () {
+ // success
+ // FIXME: could call _makeConference here and trickle candidates later
+ },
+ function (error) {
+ console.log('setLocalDescription failed', error);
+ }
+ );
+ },
+ function (error) {
+ console.warn(error);
+ }
+ );
+ this.peerconnection.onicecandidate = function (event) {
+ console.log('candidate', event.candidate);
+ if (!event.candidate) {
+ console.log('end of candidates');
+ ob._makeConference();
+ return;
+ }
+ };
+};
+
+ColibriFocus.prototype._makeConference = function () {
+ var ob = this;
+ var elem = $iq({to: this.bridgejid, type: 'get'});
+ elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri'});
+
+ var localSDP = new SDP(this.peerconnection.localDescription.sdp);
+ var contents = SDPUtil.find_lines(localSDP.raw, 'a=mid:').map(SDPUtil.parse_mid);
+ localSDP.media.forEach(function (media, channel) {
+ var name = SDPUtil.parse_mid(SDPUtil.find_line(media, 'a=mid:'));
+ elem.c('content', {name: name});
+ elem.c('channel', {initiator: 'false', expire: '15'});
+
+ // FIXME: should reuse code from .toJingle
+ var mline = SDPUtil.parse_mline(media.split('\r\n')[0]);
+ for (var j = 0; j < mline.fmt.length; j++) {
+ var rtpmap = SDPUtil.find_line(media, 'a=rtpmap:' + mline.fmt[j]);
+ elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
+ elem.up();
+ }
+
+ // FIXME: should reuse code from .toJingle
+ elem.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
+ var tmp = SDPUtil.iceparams(media, localSDP.session);
+ if (tmp) {
+ elem.attrs(tmp);
+ var fingerprints = SDPUtil.find_lines(media, 'a=fingerprint:', localSDP.session);
+ fingerprints.forEach(function (line) {
+ tmp = SDPUtil.parse_fingerprint(line);
+ //tmp.xmlns = 'urn:xmpp:tmp:jingle:apps:dtls:0';
+ tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
+ elem.c('fingerprint').t(tmp.fingerprint);
+ delete tmp.fingerprint;
+ line = SDPUtil.find_line(media, 'a=setup:', ob.session);
+ if (line) {
+ tmp.setup = line.substr(8);
+ }
+ elem.attrs(tmp);
+ elem.up();
+ });
+ // XEP-0176
+ if (SDPUtil.find_line(media, 'a=candidate:', localSDP.session)) { // add any a=candidate lines
+ lines = SDPUtil.find_lines(media, 'a=candidate:', localSDP.session);
+ for (j = 0; j < lines.length; j++) {
+ tmp = SDPUtil.candidateToJingle(lines[j]);
+ elem.c('candidate', tmp).up();
+ }
+ }
+ elem.up(); // end of transport
+ }
+ elem.up(); // end of channel
+ for (j = 0; j < ob.peers.length; j++) {
+ elem.c('channel', {initiator: 'true', expire:'15' }).up();
+ }
+ elem.up(); // end of content
+ });
+
+ this.connection.sendIQ(elem,
+ function (result) {
+ ob.createdConference(result);
+ },
+ function (error) {
+ console.warn(error);
+ }
+ );
+};
+
+// callback when a conference was created
+ColibriFocus.prototype.createdConference = function (result) {
+ console.log('created a conference on the bridge');
+ var tmp;
+
+ this.confid = $(result).find('>conference').attr('id');
+ var remotecontents = $(result).find('>conference>content').get();
+ for (var i = 0; i < remotecontents.length; i++) {
+ tmp = $(remotecontents[i]).find('>channel').get();
+ this.mychannel.push($(tmp.shift()));
+ for (j = 0; j < tmp.length; j++) {
+ if (this.channels[j] === undefined) {
+ this.channels[j] = [];
+ }
+ this.channels[j].push(tmp[j]);
+ }
+ }
+ console.log('remote channels', this.channels);
+
+ // establish our channel with the bridge
+ // static answer taken from chrome M31, should be replaced by a
+ // dynamic one that is based on our offer FIXME
+ var bridgeSDP = new SDP('v=0\r\no=- 5151055458874951233 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\nm=audio 1 RTP/SAVPF 111 103 104 0 8 106 105 13 126\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=mid:audio\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=sendrecv\r\na=rtpmap:111 opus/48000/2\r\na=fmtp:111 minptime=10\r\na=rtpmap:103 ISAC/16000\r\na=rtpmap:104 ISAC/32000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:106 CN/32000\r\na=rtpmap:105 CN/16000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:126 telephone-event/8000\r\na=maxptime:60\r\nm=video 1 RTP/SAVPF 100 116 117\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=mid:video\r\na=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\na=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=sendrecv\r\na=rtpmap:100 VP8/90000\r\na=rtcp-fb:100 ccm fir\r\na=rtcp-fb:100 nack\r\na=rtcp-fb:100 goog-remb\r\na=rtpmap:116 red/90000\r\na=rtpmap:117 ulpfec/90000\r\n');
+ // get the mixed ssrc
+ for (var channel = 0; channel < remotecontents.length; channel++) {
+ tmp = $(this.mychannel[channel]).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
+ // FIXME: check rtp-level-relay-type
+ if (tmp.length) {
+ bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
+ bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
+ bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabela0 mixedlabela0' + '\r\n';
+ bridgeSDP.media[channel] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabela0' + '\r\n';
+ } else {
+ // make chrome happy... '3735928559' == 0xDEADBEEF
+ bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
+ bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
+ bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabelv0 mixedlabelv0' + '\r\n';
+ bridgeSDP.media[channel] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabelv0' + '\r\n';
+ }
+
+ // FIXME: should take code from .fromJingle
+ tmp = $(this.mychannel[channel]).find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
+ if (tmp.length) {
+ bridgeSDP.media[channel] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
+ bridgeSDP.media[channel] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
+ tmp.find('>candidate').each(function () {
+ bridgeSDP.media[channel] += SDPUtil.candidateFromJingle(this);
+ });
+ tmp = tmp.find('>fingerprint');
+ if (tmp.length) {
+ bridgeSDP.media[channel] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
+ if (tmp.attr('setup')) {
+ bridgeSDP.media[channel] += 'a=setup:' + tmp.attr('setup') + '\r\n';
+ }
+ }
+ }
+ }
+ bridgeSDP.raw = bridgeSDP.session + bridgeSDP.media.join('');
+
+ var ob = this;
+ this.peerconnection.setRemoteDescription(
+ new RTCSessionDescription({type: 'answer', sdp: bridgeSDP.raw}),
+ function () {
+ console.log('setRemoteDescription success');
+ // remote channels == remotecontents length - 1!
+ for (var i = 0; i < remotecontents.length - 1; i++) {
+ ob.initiate(ob.peers[i], true);
+ }
+ },
+ function (error) {
+ console.log('setRemoteDescription failed');
+ }
+ );
+
+};
+
+// send a session-initiate to a new participant
+ColibriFocus.prototype.initiate = function (peer, isInitiator) {
+ var participant = this.peers.indexOf(peer);
+ console.log('tell', peer, participant);
+ var sdp;
+ if (this.peerconnection != null && this.peerconnection.signalingState == 'stable') {
+ sdp = new SDP(this.peerconnection.remoteDescription.sdp);
+ var localSDP = new SDP(this.peerconnection.localDescription.sdp);
+ // throw away stuff we don't want
+ // not needed with static offer
+ sdp.removeSessionLines('a=group:');
+ sdp.removeSessionLines('a=msid-semantic:'); // FIXME: not mapped over jingle anyway...
+ for (var i = 0; i < sdp.media.length; i++) {
+ sdp.removeMediaLines(i, 'a=rtcp-mux');
+ sdp.removeMediaLines(i, 'a=ssrc:');
+ sdp.removeMediaLines(i, 'a=crypto:');
+ sdp.removeMediaLines(i, 'a=candidate:');
+ sdp.removeMediaLines(i, 'a=ice-options:google-ice');
+ sdp.removeMediaLines(i, 'a=ice-ufrag:');
+ sdp.removeMediaLines(i, 'a=ice-pwd:');
+ sdp.removeMediaLines(i, 'a=fingerprint:');
+ sdp.removeMediaLines(i, 'a=setup:');
+
+ // re-add all remote a=ssrcs
+ for (var jid in this.remotessrc) {
+ if (jid == peer) continue;
+ sdp.media[i] += this.remotessrc[jid][i];
+ }
+ // and local a=ssrc lines
+ sdp.media[i] += SDPUtil.find_lines(localSDP.media[i], 'a=ssrc').join('\r\n') + '\r\n';
+ }
+ sdp.raw = sdp.session + sdp.media.join('');
+ } else {
+ console.error('can not initiate a new session without a stable peerconnection');
+ return;
+ }
+
+ // add stuff we got from the bridge
+ for (var j = 0; j < sdp.media.length; j++) {
+ var chan = $(this.channels[participant][j]);
+ console.log('channel id', chan.attr('id'));
+
+ tmp = chan.find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
+ if (tmp.length) {
+ sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'cname:mixed' + '\r\n';
+ sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'label:mixedlabela0' + '\r\n';
+ sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'msid:mixedmslabela0 mixedlabela0' + '\r\n';
+ sdp.media[j] += 'a=ssrc:' + tmp.attr('ssrc') + ' ' + 'mslabel:mixedmslabela0' + '\r\n';
+ } else {
+ // make chrome happy... '3735928559' == 0xDEADBEEF
+ sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'cname:mixed' + '\r\n';
+ sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'label:mixedlabelv0' + '\r\n';
+ sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'msid:mixedmslabelv0 mixedlabelv0' + '\r\n';
+ sdp.media[j] += 'a=ssrc:' + '3735928559' + ' ' + 'mslabel:mixedmslabelv0' + '\r\n';
+ }
+
+ tmp = chan.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
+ if (tmp.length) {
+ if (tmp.attr('ufrag'))
+ sdp.media[j] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
+ if (tmp.attr('pwd'))
+ sdp.media[j] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
+ // and the candidates...
+ tmp.find('>candidate').each(function () {
+ sdp.media[j] += SDPUtil.candidateFromJingle(this);
+ });
+ tmp = tmp.find('>fingerprint');
+ if (tmp.length) {
+ sdp.media[j] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
+ /*
+ if (tmp.attr('direction')) {
+ sdp.media[j] += 'a=setup:' + tmp.attr('direction') + '\r\n';
+ }
+ */
+ sdp.media[j] += 'a=setup:actpass\r\n';
+ }
+ }
+ }
+ // make a new colibri session and configure it
+ // FIXME: is it correct to use this.connection.jid when used in a MUC?
+ var sess = new ColibriSession(this.connection.jid,
+ Math.random().toString(36).substr(2, 12), // random string
+ this.connection);
+ sess.initiate(peer);
+ sess.colibri = this;
+ sess.localStream = this.connection.jingle.localStream;
+ sess.media_constraints = this.connection.jingle.media_constraints;
+ sess.pc_constraints = this.connection.jingle.pc_constraints;
+ sess.ice_config = this.connection.ice_config;
+
+ this.connection.jingle.sessions[sess.sid] = sess;
+ this.connection.jingle.jid2session[sess.peerjid] = sess;
+
+ // send a session-initiate
+ var init = $iq({to: peer, type: 'set'})
+ .c('jingle',
+ {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-initiate',
+ initiator: sess.me,
+ sid: sess.sid
+ }
+ );
+ sdp.toJingle(init, 'initiator');
+ this.connection.sendIQ(init,
+ function (res) {
+ console.log('got result');
+ },
+ function (err) {
+ console.log('got error');
+ }
+ );
+};
+
+// pull in a new participant into the conference
+ColibriFocus.prototype.addNewParticipant = function (peer) {
+ var ob = this;
+ if (this.confid === 0) {
+ // bad state
+ console.log('confid does not exist yet, postponing', peer);
+ window.setTimeout(function () {
+ ob.addNewParticipant(peer);
+ }, 250);
+ return;
+ }
+ var index = this.channels.length;
+ this.channels.push([]);
+ this.peers.push(peer);
+
+ var elem = $iq({to: this.bridgejid, type: 'get'});
+ elem.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
+ var localSDP = new SDP(this.peerconnection.localDescription.sdp);
+ var contents = SDPUtil.find_lines(localSDP.raw, 'a=mid:').map(SDPUtil.parse_mid);
+ contents.forEach(function (name) {
+ elem.c('content', {name: name});
+ elem.c('channel', {initiator: 'true', expire:'15'});
+ elem.up(); // end of channel
+ elem.up(); // end of content
+ });
+
+ this.connection.sendIQ(elem,
+ function (result) {
+ var contents = $(result).find('>conference>content').get();
+ for (var i = 0; i < contents.length; i++) {
+ tmp = $(contents[i]).find('>channel').get();
+ ob.channels[index][i] = tmp[0];
+ }
+ ob.initiate(peer, true);
+ },
+ function (error) {
+ console.warn(error);
+ }
+ );
+};
+
+// update the channel description (payload-types + dtls fp) for a participant
+ColibriFocus.prototype.updateChannel = function (remoteSDP, participant) {
+ console.log('change allocation for', this.confid);
+ var change = $iq({to: this.bridgejid, type: 'set'});
+ change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
+ for (channel = 0; channel < this.channels[participant].length; channel++) {
+ change.c('content', {name: channel === 0 ? 'audio' : 'video'});
+ change.c('channel', {id: $(this.channels[participant][channel]).attr('id')});
+
+ var rtpmap = SDPUtil.find_lines(remoteSDP.media[channel], 'a=rtpmap:');
+ rtpmap.forEach(function (val) {
+ // TODO: too much copy-paste
+ var rtpmap = SDPUtil.parse_rtpmap(val);
+ change.c('payload-type', rtpmap);
+ //
+ // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
+ /*
+ if (SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id)) {
+ tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(remoteSDP.media[channel], 'a=fmtp:' + rtpmap.id));
+ for (var k = 0; k < tmp.length; k++) {
+ change.c('parameter', tmp[k]).up();
+ }
+ }
+ */
+ change.up();
+ });
+
+ // now add transport
+ change.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
+ var fingerprints = SDPUtil.find_lines(remoteSDP.media[channel], 'a=fingerprint:', remoteSDP.session);
+ fingerprints.forEach(function (line) {
+ tmp = SDPUtil.parse_fingerprint(line);
+ tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
+ change.c('fingerprint').t(tmp.fingerprint);
+ delete tmp.fingerprint;
+ line = SDPUtil.find_line(remoteSDP.media[channel], 'a=setup:', remoteSDP.session);
+ if (line) {
+ tmp.setup = line.substr(8);
+ }
+ change.attrs(tmp);
+ change.up();
+ });
+ var candidates = SDPUtil.find_lines(remoteSDP.media[channel], 'a=candidate:', remoteSDP.session);
+ candidates.forEach(function (line) {
+ var tmp = SDPUtil.candidateToJingle(line);
+ change.c('candidate', tmp).up();
+ });
+ tmp = SDPUtil.iceparams(remoteSDP.media[channel], remoteSDP.session);
+ if (tmp) {
+ change.attrs(tmp);
+
+ }
+ change.up(); // end of transport
+ change.up(); // end of channel
+ change.up(); // end of content
+ }
+ this.connection.sendIQ(change,
+ function (res) {
+ console.log('got result');
+ },
+ function (err) {
+ console.log('got error');
+ }
+ );
+};
+
+// tell everyone about a new participants a=ssrc lines (isadd is true)
+// or a leaving participants a=ssrc lines
+// FIXME: should not take an SDP, but rather the a=ssrc lines and probably a=mid
+ColibriFocus.prototype.sendSSRCUpdate = function (sdp, exclude, isadd) {
+ var ob = this;
+ this.peers.forEach(function (peerjid) {
+ if (peerjid == exclude) return;
+ console.log('tell', peerjid, 'about ' + (isadd ? 'new' : 'removed') + ' ssrcs from', exclude);
+ if (!ob.remotessrc[peerjid]) {
+ // FIXME: this should only send to participants that are stable, i.e. who have sent a session-accept
+ // possibly, this.remoteSSRC[session.peerjid] does not exist yet
+ console.warn('do we really want to bother', peerjid, 'with updates yet?');
+ }
+ var channel;
+ var peersess = ob.connection.jingle.jid2session[peerjid];
+ var modify = $iq({to: peerjid, type: 'set'})
+ .c('jingle', {
+ xmlns: 'urn:xmpp:jingle:1',
+ action: isadd ? 'addsource' : 'removesource',
+ initiator: peersess.initiator,
+ sid: peersess.sid
+ }
+ );
+ for (channel = 0; channel < sdp.media.length; channel++) {
+ tmp = SDPUtil.find_lines(sdp.media[channel], 'a=ssrc:');
+ modify.c('content', {name: SDPUtil.parse_mid(SDPUtil.find_line(sdp.media[channel], 'a=mid:'))});
+ modify.c('source', { xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
+ // FIXME: not completly sure this operates on blocks and / or handles different ssrcs correctly
+ tmp.forEach(function (line) {
+ var idx = line.indexOf(' ');
+ var linessrc = line.substr(0, idx).substr(7);
+ modify.attrs({ssrc: linessrc});
+
+ var kv = line.substr(idx + 1);
+ modify.c('parameter');
+ if (kv.indexOf(':') == -1) {
+ modify.attrs({ name: kv });
+ } else {
+ modify.attrs({ name: kv.split(':', 2)[0] });
+ modify.attrs({ value: kv.split(':', 2)[1] });
+ }
+ modify.up();
+ });
+ modify.up(); // end of source
+ modify.up(); // end of content
+ }
+ ob.connection.sendIQ(modify,
+ function (res) {
+ console.warn('got modify result');
+ },
+ function (err) {
+ console.warn('got modify error');
+ }
+ );
+ });
+};
+
+ColibriFocus.prototype.setRemoteDescription = function (session, elem, desctype) {
+ var participant = this.peers.indexOf(session.peerjid);
+ console.log('Colibri.setRemoteDescription from', session.peerjid, participant);
+ var ob = this;
+ var remoteSDP = new SDP('');
+ var tmp;
+ var channel;
+ remoteSDP.fromJingle(elem);
+
+ // ACT 1: change allocation on bridge
+ this.updateChannel(remoteSDP, participant);
+
+ // ACT 2: tell anyone else about the new SSRCs
+ this.sendSSRCUpdate(remoteSDP, session.peerjid, true);
+
+ // ACT 3: note the SSRCs
+ this.remotessrc[session.peerjid] = [];
+ for (channel = 0; channel < this.channels[participant].length; channel++) {
+ this.remotessrc[session.peerjid][channel] = SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').join('\r\n') + '\r\n';
+ }
+
+ // ACT 4: add new a=ssrc lines to local remotedescription
+ for (channel = 0; channel < this.channels[participant].length; channel++) {
+ if (!this.addssrc[channel]) this.addssrc[channel] = '';
+ this.addssrc[channel] += SDPUtil.find_lines(remoteSDP.media[channel], 'a=ssrc:').join('\r\n') + '\r\n';
+ }
+ this.modifySources();
+};
+
+// relay ice candidates to bridge using trickle
+ColibriFocus.prototype.addIceCandidate = function (session, elem) {
+ var ob = this;
+ var participant = this.peers.indexOf(session.peerjid);
+ console.log('change transport allocation for', this.confid, session.peerjid, participant);
+ var change = $iq({to: this.bridgejid, type: 'set'});
+ change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
+ $(elem).each(function () {
+ var name = $(this).attr('name');
+ var channel = name == 'audio' ? 0 : 1; // FIXME: search mlineindex in localdesc
+
+ change.c('content', {name: name});
+ change.c('channel', {id: $(ob.channels[participant][channel]).attr('id')});
+ $(this).find('>transport').each(function () {
+ change.c('transport', {
+ ufrag: $(this).attr('ufrag'),
+ pwd: $(this).attr('pwd'),
+ xmlns: $(this).attr('xmlns')
+ });
+
+ $(this).find('>candidate').each(function () {
+ /* not yet
+ if (this.getAttribute('protocol') == 'tcp' && this.getAttribute('port') == 0) {
+ // chrome generates TCP candidates with port 0
+ return;
+ }
+ */
+ var line = SDPUtil.candidateFromJingle(this);
+ change.c('candidate', SDPUtil.candidateToJingle(line)).up();
+ });
+ change.up(); // end of transport
+ });
+ change.up(); // end of channel
+ change.up(); // end of content
+ });
+ // FIXME: need to check if there is at least one candidate when filtering TCP ones
+ this.connection.sendIQ(change,
+ function (res) {
+ console.log('got result');
+ },
+ function (err) {
+ console.warn('got error');
+ }
+ );
+};
+
+// send our own candidate to the bridge
+ColibriFocus.prototype.sendIceCandidate = function (candidate) {
+ //console.log('candidate', candidate);
+ if (!candidate) {
+ console.log('end of candidates');
+ return;
+ }
+ var mycands = $iq({to: this.bridgejid, type: 'set'});
+ mycands.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
+ mycands.c('content', {name: candidate.sdpMid });
+ mycands.c('channel', {id: $(this.mychannel[candidate.sdpMLineIndex]).attr('id')});
+ mycands.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
+ tmp = SDPUtil.candidateToJingle(candidate.candidate);
+ mycands.c('candidate', tmp).up();
+ this.connection.sendIQ(mycands,
+ function (res) {
+ console.log('got result');
+ },
+ function (err) {
+ console.warn('got error');
+ }
+ );
+};
+
+ColibriFocus.prototype.terminate = function (session, reason) {
+ console.log('remote session terminated from', session.peerjid);
+ var participant = this.peers.indexOf(session.peerjid);
+ if (!this.remotessrc[session.peerjid] || participant == -1) {
+ return;
+ }
+ console.log('remote ssrcs:', this.remotessrc[session.peerjid]);
+ var ssrcs = this.remotessrc[session.peerjid];
+ for (var i = 0; i < ssrcs.length; i++) {
+ if (!this.removessrc[i]) this.removessrc[i] = '';
+ this.removessrc[i] += ssrcs[i];
+ }
+ // remove from this.peers
+ this.peers.splice(participant, 1);
+ // expire channel on bridge
+ var change = $iq({to: this.bridgejid, type: 'set'});
+ change.c('conference', {xmlns: 'http://jitsi.org/protocol/colibri', id: this.confid});
+ for (var channel = 0; channel < this.channels[participant].length; channel++) {
+ change.c('content', {name: channel === 0 ? 'audio' : 'video'});
+ change.c('channel', {id: $(this.channels[participant][channel]).attr('id'), expire: '0'});
+ change.up(); // end of channel
+ change.up(); // end of content
+ }
+ this.connection.sendIQ(change,
+ function (res) {
+ console.log('got result');
+ },
+ function (err) {
+ console.log('got error');
+ }
+ );
+ // and remove from channels
+ this.channels.splice(participant, 1);
+
+ // tell everyone about the ssrcs to be removed
+ var sdp = new SDP('');
+ var localSDP = new SDP(this.peerconnection.localDescription.sdp);
+ var contents = SDPUtil.find_lines(localSDP.raw, 'a=mid:').map(SDPUtil.parse_mid);
+ for (var j = 0; j < ssrcs.length; j++) {
+ sdp.media[j] = 'a=mid:' + contents[j] + '\r\n';
+ sdp.media[j] += ssrcs[j];
+ this.removessrc[j] += ssrcs[j];
+ }
+ this.sendSSRCUpdate(sdp, session.peerjid, false);
+
+ delete this.remotessrc[session.peerjid];
+ this.modifySources();
+};
+
+ColibriFocus.prototype.modifySources = function () {
+ var ob = this;
+ if (!(this.addssrc.length || this.removessrc.length)) return;
+ if (this.peerconnection.signalingState == 'closed') return;
+
+ // FIXME: this is a big hack
+ // https://code.google.com/p/webrtc/issues/detail?id=2688
+ if (!(this.peerconnection.signalingState == 'stable' && this.peerconnection.iceConnectionState == 'connected')) {
+ console.warn('modifySources not yet', this.peerconnection.signalingState, this.peerconnection.iceConnectionState);
+ window.setTimeout(function () { ob.modifySources(); }, 250);
+ this.wait = true;
+ return;
+ }
+ if (this.wait) {
+ window.setTimeout(function () { ob.modifySources(); }, 2500);
+ this.wait = false;
+ return;
+ }
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
+
+ // add sources
+ this.addssrc.forEach(function (lines, idx) {
+ sdp.media[idx] += lines;
+ });
+ this.addssrc = [];
+
+ // remove sources
+ this.removessrc.forEach(function (lines, idx) {
+ lines = lines.split('\r\n');
+ lines.pop(); // remove empty last element;
+ lines.forEach(function (line) {
+ sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
+ });
+ });
+ this.removessrc = [];
+
+ sdp.raw = sdp.session + sdp.media.join('');
+ this.peerconnection.setRemoteDescription(
+ new RTCSessionDescription({type: 'offer', sdp: sdp.raw }),
+ function () {
+ console.log('setModifiedRemoteDescription ok');
+ ob.peerconnection.createAnswer(
+ function (modifiedAnswer) {
+ console.log('modifiedAnswer created');
+ // FIXME: pushing down an answer while ice connection state
+ // is still checking is bad...
+ console.log(ob.peerconnection.iceConnectionState);
+ ob.peerconnection.setLocalDescription(modifiedAnswer,
+ function () {
+ console.log('setModifiedLocalDescription ok');
+ },
+ function (error) {
+ console.log('setModifiedLocalDescription failed');
+ }
+ );
+ },
+ function (error) {
+ console.log('createModifiedAnswer failed');
+ }
+ );
+ },
+ function (error) {
+ console.log('setModifiedRemoteDescription failed');
+ }
+ );
+};
+
+
+// A colibri session is similar to a jingle session, it just implements some things differently
+// FIXME: inherit jinglesession, see https://github.com/legastero/Jingle-RTCPeerConnection/blob/master/index.js
+function ColibriSession(me, sid, connection) {
+ this.me = me;
+ this.sid = sid;
+ this.connection = connection;
+ //this.peerconnection = null;
+ //this.mychannel = null;
+ //this.channels = null;
+ this.peerjid = null;
+
+ this.colibri = null;
+}
+
+// implementation of JingleSession interface
+ColibriSession.prototype.initiate = function (peerjid, isInitiator) {
+ this.peerjid = peerjid;
+};
+
+ColibriSession.prototype.sendOffer = function (offer) {
+ console.log('ColibriSession.sendOffer');
+};
+
+
+ColibriSession.prototype.accept = function () {
+ console.log('ColibriSession.accept');
+};
+
+ColibriSession.prototype.terminate = function (reason) {
+ this.colibri.terminate(this, reason);
+};
+
+ColibriSession.prototype.active = function () {
+ console.log('ColibriSession.active');
+};
+
+ColibriSession.prototype.setRemoteDescription = function (elem, desctype) {
+ this.colibri.setRemoteDescription(this, elem, desctype);
+};
+
+ColibriSession.prototype.addIceCandidate = function (elem) {
+ this.colibri.addIceCandidate(this, elem);
+};
+
+ColibriSession.prototype.sendAnswer = function (sdp, provisional) {
+ console.log('ColibriSession.sendAnswer');
+};
+
+ColibriSession.prototype.sendTerminate = function (reason, text) {
+ console.log('ColibriSession.sendTerminate');
+};
diff --git a/libs/jquery-impromptu.js b/libs/jquery-impromptu.js
new file mode 100644
index 0000000000..6cc82bd16d
--- /dev/null
+++ b/libs/jquery-impromptu.js
@@ -0,0 +1,666 @@
+/*! jQuery-Impromptu - v5.1.1
+* http://trentrichardson.com/Impromptu
+* Copyright (c) 2013 Trent Richardson; Licensed MIT */
+(function($) {
+ "use strict";
+
+ /**
+ * setDefaults - Sets the default options
+ * @param message String/Object - String of html or Object of states
+ * @param options Object - Options to set the prompt
+ * @return jQuery - container with overlay and prompt
+ */
+ $.prompt = function(message, options) {
+ // only for backwards compat, to be removed in future version
+ if(options !== undefined && options.classes !== undefined && typeof options.classes === 'string'){
+ options = { box: options.classes };
+ }
+
+ $.prompt.options = $.extend({},$.prompt.defaults,options);
+ $.prompt.currentPrefix = $.prompt.options.prefix;
+
+ // Be sure any previous timeouts are destroyed
+ if($.prompt.timeout){
+ clearTimeout($.prompt.timeout);
+ }
+ $.prompt.timeout = false;
+
+ var opts = $.prompt.options,
+ $body = $(document.body),
+ $window = $(window);
+
+ //build the box and fade
+ var msgbox = '<div class="'+ $.prompt.options.prefix +'box '+ opts.classes.box +'">';
+ if(opts.useiframe && ($('object, applet').length > 0)) {
+ msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></iframe>';
+ } else {
+ msgbox +='<div class="'+ opts.prefix +'fade '+ opts.classes.fade +'"></div>';
+ }
+ msgbox += '<div class="'+ opts.prefix +' '+ opts.classes.prompt +'">'+
+ '<form action="javascript:false;" onsubmit="return false;" class="'+ opts.prefix +'form">'+
+ '<div class="'+ opts.prefix +'close '+ opts.classes.close +'">'+ opts.closeText +'</div>'+
+ '<div class="'+ opts.prefix +'states"></div>'+
+ '</form>'+
+ '</div>'+
+ '</div>';
+
+ $.prompt.jqib = $(msgbox).appendTo($body);
+ $.prompt.jqi = $.prompt.jqib.children('.'+ opts.prefix);//.data('jqi',opts);
+ $.prompt.jqif = $.prompt.jqib.children('.'+ opts.prefix +'fade');
+
+ //if a string was passed, convert to a single state
+ if(message.constructor === String){
+ message = {
+ state0: {
+ title: opts.title,
+ html: message,
+ buttons: opts.buttons,
+ position: opts.position,
+ focus: opts.focus,
+ submit: opts.submit
+ }
+ };
+ }
+
+ //build the states
+ $.prompt.options.states = {};
+ var k,v;
+ for(k in message){
+ v = $.extend({},$.prompt.defaults.state,{name:k},message[k]);
+ $.prompt.addState(v.name, v);
+
+ if($.prompt.currentStateName === ''){
+ $.prompt.currentStateName = v.name;
+ }
+ }
+
+ // Go ahead and transition to the first state. It won't be visible just yet though until we show the prompt
+ var $firstState = $.prompt.jqi.find('.'+ opts.prefix +'states .'+ opts.prefix +'state').eq(0);
+ $.prompt.goToState($firstState.data('jqi-name'));
+
+ //Events
+ $.prompt.jqi.on('click', '.'+ opts.prefix +'buttons button', function(e){
+ var $t = $(this),
+ $state = $t.parents('.'+ opts.prefix +'state'),
+ stateobj = $.prompt.options.states[$state.data('jqi-name')],
+ msg = $state.children('.'+ opts.prefix +'message'),
+ clicked = stateobj.buttons[$t.text()] || stateobj.buttons[$t.html()],
+ forminputs = {};
+
+ // if for some reason we couldn't get the value
+ if(clicked === undefined){
+ for(var i in stateobj.buttons){
+ if(stateobj.buttons[i].title === $t.text() || stateobj.buttons[i].title === $t.html()){
+ clicked = stateobj.buttons[i].value;
+ }
+ }
+ }
+
+ //collect all form element values from all states.
+ $.each($.prompt.jqi.children('form').serializeArray(),function(i,obj){
+ if (forminputs[obj.name] === undefined) {
+ forminputs[obj.name] = obj.value;
+ } else if (typeof forminputs[obj.name] === Array || typeof forminputs[obj.name] === 'object') {
+ forminputs[obj.name].push(obj.value);
+ } else {
+ forminputs[obj.name] = [forminputs[obj.name],obj.value];
+ }
+ });
+
+ // trigger an event
+ var promptsubmite = new $.Event('impromptu:submit');
+ promptsubmite.stateName = stateobj.name;
+ promptsubmite.state = $state;
+ $state.trigger(promptsubmite, [clicked, msg, forminputs]);
+
+ if(!promptsubmite.isDefaultPrevented()){
+ $.prompt.close(true, clicked,msg,forminputs);
+ }
+ });
+
+ // if the fade is clicked blink the prompt
+ var fadeClicked = function(){
+ if(opts.persistent){
+ var offset = (opts.top.toString().indexOf('%') >= 0? ($window.height()*(parseInt(opts.top,10)/100)) : parseInt(opts.top,10)),
+ top = parseInt($.prompt.jqi.css('top').replace('px',''),10) - offset;
+
+ //$window.scrollTop(top);
+ $('html,body').animate({ scrollTop: top }, 'fast', function(){
+ var i = 0;
+ $.prompt.jqib.addClass(opts.prefix +'warning');
+ var intervalid = setInterval(function(){
+ $.prompt.jqib.toggleClass(opts.prefix +'warning');
+ if(i++ > 1){
+ clearInterval(intervalid);
+ $.prompt.jqib.removeClass(opts.prefix +'warning');
+ }
+ }, 100);
+ });
+ }
+ else {
+ $.prompt.close(true);
+ }
+ };
+
+ // listen for esc or tab keys
+ var keyPressEventHandler = function(e){
+ var key = (window.event) ? event.keyCode : e.keyCode;
+
+ //escape key closes
+ if(key===27) {
+ fadeClicked();
+ }
+
+ //constrain tabs, tabs should iterate through the state and not leave
+ if (key === 9){
+ var $inputels = $('input,select,textarea,button',$.prompt.getCurrentState());
+ var fwd = !e.shiftKey && e.target === $inputels[$inputels.length-1];
+ var back = e.shiftKey && e.target === $inputels[0];
+ if (fwd || back) {
+ setTimeout(function(){
+ if (!$inputels){
+ return;
+ }
+ var el = $inputels[back===true ? $inputels.length-1 : 0];
+
+ if (el){
+ el.focus();
+ }
+ },10);
+ return false;
+ }
+ }
+ };
+
+ $.prompt.position();
+ $.prompt.style();
+
+ $.prompt.jqif.click(fadeClicked);
+ $window.resize({animate:false}, $.prompt.position);
+ $.prompt.jqi.find('.'+ opts.prefix +'close').click($.prompt.close);
+ $.prompt.jqib.on("keydown",keyPressEventHandler)
+ .on('impromptu:loaded', opts.loaded)
+ .on('impromptu:close', opts.close)
+ .on('impromptu:statechanging', opts.statechanging)
+ .on('impromptu:statechanged', opts.statechanged);
+
+ // Show it
+ $.prompt.jqif[opts.show](opts.overlayspeed);
+ $.prompt.jqi[opts.show](opts.promptspeed, function(){
+ $.prompt.jqib.trigger('impromptu:loaded');
+ });
+
+ // Timeout
+ if(opts.timeout > 0){
+ $.prompt.timeout = setTimeout(function(){ $.prompt.close(true); },opts.timeout);
+ }
+
+ return $.prompt.jqib;
+ };
+
+ $.prompt.defaults = {
+ prefix:'jqi',
+ classes: {
+ box: '',
+ fade: '',
+ prompt: '',
+ close: '',
+ title: '',
+ message: '',
+ buttons: '',
+ button: '',
+ defaultButton: ''
+ },
+ title: '',
+ closeText: '&times;',
+ buttons: {
+ Ok: true
+ },
+ loaded: function(e){},
+ submit: function(e,v,m,f){},
+ close: function(e,v,m,f){},
+ statechanging: function(e, from, to){},
+ statechanged: function(e, to){},
+ opacity: 0.6,
+ zIndex: 999,
+ overlayspeed: 'slow',
+ promptspeed: 'fast',
+ show: 'fadeIn',
+ focus: 0,
+ defaultButton: 0,
+ useiframe: false,
+ top: '15%',
+ position: {
+ container: null,
+ x: null,
+ y: null,
+ arrow: null,
+ width: null
+ },
+ persistent: true,
+ timeout: 0,
+ states: {},
+ state: {
+ name: null,
+ title: '',
+ html: '',
+ buttons: {
+ Ok: true
+ },
+ focus: 0,
+ defaultButton: 0,
+ position: {
+ container: null,
+ x: null,
+ y: null,
+ arrow: null,
+ width: null
+ },
+ submit: function(e,v,m,f){
+ return true;
+ }
+ }
+ };
+
+ /**
+ * currentPrefix String - At any time this show be the prefix
+ * of the current prompt ex: "jqi"
+ */
+ $.prompt.currentPrefix = $.prompt.defaults.prefix;
+
+ /**
+ * currentStateName String - At any time this is the current state
+ * of the current prompt ex: "state0"
+ */
+ $.prompt.currentStateName = "";
+
+ /**
+ * setDefaults - Sets the default options
+ * @param o Object - Options to set as defaults
+ * @return void
+ */
+ $.prompt.setDefaults = function(o) {
+ $.prompt.defaults = $.extend({}, $.prompt.defaults, o);
+ };
+
+ /**
+ * setStateDefaults - Sets the default options for a state
+ * @param o Object - Options to set as defaults
+ * @return void
+ */
+ $.prompt.setStateDefaults = function(o) {
+ $.prompt.defaults.state = $.extend({}, $.prompt.defaults.state, o);
+ };
+
+ /**
+ * position - Repositions the prompt (Used internally)
+ * @return void
+ */
+ $.prompt.position = function(e){
+ var restoreFx = $.fx.off,
+ $state = $.prompt.getCurrentState(),
+ stateObj = $.prompt.options.states[$state.data('jqi-name')],
+ pos = stateObj? stateObj.position : undefined,
+ $window = $(window),
+ bodyHeight = document.body.scrollHeight, //$(document.body).outerHeight(true),
+ windowHeight = $(window).height(),
+ documentHeight = $(document).height(),
+ height = bodyHeight > windowHeight ? bodyHeight : windowHeight,
+ top = parseInt($window.scrollTop(),10) + ($.prompt.options.top.toString().indexOf('%') >= 0?
+ (windowHeight*(parseInt($.prompt.options.top,10)/100)) : parseInt($.prompt.options.top,10));
+
+ // when resizing the window turn off animation
+ if(e !== undefined && e.data.animate === false){
+ $.fx.off = true;
+ }
+
+ $.prompt.jqib.css({
+ position: "absolute",
+ height: height,
+ width: "100%",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0
+ });
+ $.prompt.jqif.css({
+ position: "fixed",
+ height: height,
+ width: "100%",
+ top: 0,
+ left: 0,
+ right: 0,
+ bottom: 0
+ });
+
+ // tour positioning
+ if(pos && pos.container){
+ var offset = $(pos.container).offset();
+
+ if($.isPlainObject(offset) && offset.top !== undefined){
+ $.prompt.jqi.css({
+ position: "absolute"
+ });
+ $.prompt.jqi.animate({
+ top: offset.top + pos.y,
+ left: offset.left + pos.x,
+ marginLeft: 0,
+ width: (pos.width !== undefined)? pos.width : null
+ });
+ top = (offset.top + pos.y) - ($.prompt.options.top.toString().indexOf('%') >= 0? (windowHeight*(parseInt($.prompt.options.top,10)/100)) : parseInt($.prompt.options.top,10));
+ $('html,body').animate({ scrollTop: top }, 'slow', 'swing', function(){});
+ }
+ }
+ // custom state width animation
+ else if(pos && pos.width){
+ $.prompt.jqi.css({
+ position: "absolute",
+ left: '50%'
+ });
+ $.prompt.jqi.animate({
+ top: pos.y || top,
+ left: pos.x || '50%',
+ marginLeft: ((pos.width/2)*-1),
+ width: pos.width
+ });
+ }
+ // standard prompt positioning
+ else{
+ $.prompt.jqi.css({
+ position: "absolute",
+ top: top,
+ left: '50%',//$window.width()/2,
+ marginLeft: (($.prompt.jqi.outerWidth(false)/2)*-1)
+ });
+ }
+
+ // restore fx settings
+ if(e !== undefined && e.data.animate === false){
+ $.fx.off = restoreFx;
+ }
+ };
+
+ /**
+ * style - Restyles the prompt (Used internally)
+ * @return void
+ */
+ $.prompt.style = function(){
+ $.prompt.jqif.css({
+ zIndex: $.prompt.options.zIndex,
+ display: "none",
+ opacity: $.prompt.options.opacity
+ });
+ $.prompt.jqi.css({
+ zIndex: $.prompt.options.zIndex+1,
+ display: "none"
+ });
+ $.prompt.jqib.css({
+ zIndex: $.prompt.options.zIndex
+ });
+ };
+
+ /**
+ * get - Get the prompt
+ * @return jQuery - the prompt
+ */
+ $.prompt.get = function(state) {
+ return $('.'+ $.prompt.currentPrefix);
+ };
+
+ /**
+ * addState - Injects a state into the prompt
+ * @param statename String - Name of the state
+ * @param stateobj Object - options for the state
+ * @param afterState String - selector of the state to insert after
+ * @return jQuery - the newly created state
+ */
+ $.prompt.addState = function(statename, stateobj, afterState) {
+ var state = "",
+ $state = null,
+ arrow = "",
+ title = "",
+ opts = $.prompt.options,
+ $jqistates = $('.'+ $.prompt.currentPrefix +'states'),
+ defbtn,k,v,i=0;
+
+ stateobj = $.extend({},$.prompt.defaults.state, {name:statename}, stateobj);
+
+ if(stateobj.position.arrow !== null){
+ arrow = '<div class="'+ opts.prefix + 'arrow '+ opts.prefix + 'arrow'+ stateobj.position.arrow +'"></div>';
+ }
+ if(stateobj.title && stateobj.title !== ''){
+ title = '<div class="lead '+ opts.prefix + 'title '+ opts.classes.title +'">'+ stateobj.title +'</div>';
+ }
+ state += '<div id="'+ opts.prefix +'state_'+ statename +'" class="'+ opts.prefix + 'state" data-jqi-name="'+ statename +'" style="display:none;">'+
+ arrow + title +
+ '<div class="'+ opts.prefix +'message '+ opts.classes.message +'">' + stateobj.html +'</div>'+
+ '<div class="'+ opts.prefix +'buttons '+ opts.classes.buttons +'"'+ ($.isEmptyObject(stateobj.buttons)? 'style="display:none;"':'') +'>';
+
+ for(k in stateobj.buttons){
+ v = stateobj.buttons[k],
+ defbtn = stateobj.focus === i || (isNaN(stateobj.focus) && stateobj.defaultButton === i) ? ($.prompt.currentPrefix + 'defaultbutton ' + opts.classes.defaultButton) : '';
+
+ if(typeof v === 'object'){
+ state += '<button class="'+ opts.classes.button +' '+ defbtn;
+
+ if(typeof v.classes !== "undefined"){
+ state += ' '+ ($.isArray(v.classes)? v.classes.join(' ') : v.classes) + ' ';
+ }
+
+ state += '" name="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + opts.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
+
+ } else {
+ state += '<button class="'+ opts.classes.button +' '+ defbtn +'" name="' + opts.prefix + '_' + statename + '_button' + k + '" id="' + opts.prefix + '_' + statename + '_button' + k + '" value="' + v + '">' + k + '</button>';
+
+ }
+ i++;
+ }
+ state += '</div></div>';
+
+ $state = $(state);
+
+ $state.on('impromptu:submit', stateobj.submit);
+
+ if(afterState !== undefined){
+ $jqistates.find('#'+ $.prompt.currentPrefix +'state_'+ afterState).after($state);
+ }
+ else{
+ $jqistates.append($state);
+ }
+
+ $.prompt.options.states[statename] = stateobj;
+
+ return $state;
+ };
+
+ /**
+ * removeState - Removes a state from the promt
+ * @param state String - Name of the state
+ * @return Boolean - returns true on success, false on failure
+ */
+ $.prompt.removeState = function(state) {
+ var $state = $.prompt.getState(state),
+ rm = function(){ $state.remove(); };
+
+ if($state.length === 0){
+ return false;
+ }
+
+ // transition away from it before deleting
+ if($state.is(':visible')){
+ if($state.next().length > 0){
+ $.prompt.nextState(rm);
+ }
+ else{
+ $.prompt.prevState(rm);
+ }
+ }
+ else{
+ $state.slideUp('slow', rm);
+ }
+
+ return true;
+ };
+
+ /**
+ * getState - Get the state by its name
+ * @param state String - Name of the state
+ * @return jQuery - the state
+ */
+ $.prompt.getState = function(state) {
+ return $('#'+ $.prompt.currentPrefix +'state_'+ state);
+ };
+ $.prompt.getStateContent = function(state) {
+ return $.prompt.getState(state);
+ };
+
+ /**
+ * getCurrentState - Get the current visible state
+ * @return jQuery - the current visible state
+ */
+ $.prompt.getCurrentState = function() {
+ return $.prompt.getState($.prompt.getCurrentStateName());
+ };
+
+ /**
+ * getCurrentStateName - Get the name of the current visible state
+ * @return String - the current visible state's name
+ */
+ $.prompt.getCurrentStateName = function() {
+ return $.prompt.currentStateName;
+ };
+
+ /**
+ * goToState - Goto the specified state
+ * @param state String - name of the state to transition to
+ * @param subState Boolean - true to be a sub state within the currently open state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ $.prompt.goToState = function(state, subState, callback) {
+ var $jqi = $.prompt.get(),
+ jqiopts = $.prompt.options,
+ $state = $.prompt.getState(state),
+ stateobj = jqiopts.states[$state.data('jqi-name')],
+ promptstatechanginge = new $.Event('impromptu:statechanging');
+
+ // subState can be ommitted
+ if(typeof subState === 'function'){
+ callback = subState;
+ subState = false;
+ }
+
+ $.prompt.jqib.trigger(promptstatechanginge, [$.prompt.getCurrentStateName(), state]);
+
+ if(!promptstatechanginge.isDefaultPrevented() && $state.length > 0){
+ $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'parentstate').removeClass($.prompt.currentPrefix +'parentstate');
+
+ if(subState){ // hide any open substates
+ // get rid of any substates
+ $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'substate').not($state)
+ .slideUp(jqiopts.promptspeed)
+ .removeClass('.'+ $.prompt.currentPrefix +'substate')
+ .find('.'+ $.prompt.currentPrefix +'arrow').hide();
+
+ // add parent state class so it can be visible, but blocked
+ $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'state:visible').addClass($.prompt.currentPrefix +'parentstate');
+
+ // add substate class so we know it will be smaller
+ $state.addClass($.prompt.currentPrefix +'substate');
+ }
+ else{ // hide any open states
+ $.prompt.jqi.find('.'+ $.prompt.currentPrefix +'state').not($state)
+ .slideUp(jqiopts.promptspeed)
+ .find('.'+ $.prompt.currentPrefix +'arrow').hide();
+ }
+ $.prompt.currentStateName = stateobj.name;
+
+ $state.slideDown(jqiopts.promptspeed,function(){
+ var $t = $(this);
+
+ // if focus is a selector, find it, else its button index
+ if(typeof(stateobj.focus) === 'string'){
+ $t.find(stateobj.focus).eq(0).focus();
+ }
+ else{
+ $t.find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
+ }
+
+ $t.find('.'+ $.prompt.currentPrefix +'arrow').show(jqiopts.promptspeed);
+
+ if (typeof callback === 'function'){
+ $.prompt.jqib.on('impromptu:statechanged', callback);
+ }
+ $.prompt.jqib.trigger('impromptu:statechanged', [state]);
+ if (typeof callback === 'function'){
+ $.prompt.jqib.off('impromptu:statechanged', callback);
+ }
+ });
+ if(!subState){
+ $.prompt.position();
+ }
+ }
+ return $state;
+ };
+
+ /**
+ * nextState - Transition to the next state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ $.prompt.nextState = function(callback) {
+ var $next = $('#'+ $.prompt.currentPrefix +'state_'+ $.prompt.getCurrentStateName()).next();
+ return $.prompt.goToState( $next.attr('id').replace($.prompt.currentPrefix +'state_',''), callback );
+ };
+
+ /**
+ * prevState - Transition to the previous state
+ * @param callback Function - called when the transition is complete
+ * @return jQuery - the newly active state
+ */
+ $.prompt.prevState = function(callback) {
+ var $prev = $('#'+ $.prompt.currentPrefix +'state_'+ $.prompt.getCurrentStateName()).prev();
+ $.prompt.goToState( $prev.attr('id').replace($.prompt.currentPrefix +'state_',''), callback );
+ };
+
+ /**
+ * close - Closes the prompt
+ * @param callback Function - called when the transition is complete
+ * @param clicked String - value of the button clicked (only used internally)
+ * @param msg jQuery - The state message body (only used internally)
+ * @param forvals Object - key/value pairs of all form field names and values (only used internally)
+ * @return jQuery - the newly active state
+ */
+ $.prompt.close = function(callCallback, clicked, msg, formvals){
+ if($.prompt.timeout){
+ clearTimeout($.prompt.timeout);
+ $.prompt.timeout = false;
+ }
+
+ $.prompt.jqib.fadeOut('fast',function(){
+
+ if(callCallback) {
+ $.prompt.jqib.trigger('impromptu:close', [clicked,msg,formvals]);
+ }
+ $.prompt.jqib.remove();
+
+ $(window).off('resize',$.prompt.position);
+ });
+ };
+
+ /**
+ * Enable using $('.selector').prompt({});
+ * This will grab the html within the prompt as the prompt message
+ */
+ $.fn.prompt = function(options){
+ if(options === undefined){
+ options = {};
+ }
+ if(options.withDataAndEvents === undefined){
+ options.withDataAndEvents = false;
+ }
+
+ $.prompt($(this).clone(options.withDataAndEvents).html(),options);
+ };
+
+})(jQuery);
diff --git a/libs/jquery.autosize.js b/libs/jquery.autosize.js
new file mode 100755
index 0000000000..0f296d4b2d
--- /dev/null
+++ b/libs/jquery.autosize.js
@@ -0,0 +1,250 @@
+/*!
+ Autosize v1.18.1 - 2013-11-05
+ Automatically adjust textarea height based on user input.
+ (c) 2013 Jack Moore - http://www.jacklmoore.com/autosize
+ license: http://www.opensource.org/licenses/mit-license.php
+*/
+(function ($) {
+ var
+ defaults = {
+ className: 'autosizejs',
+ append: '',
+ callback: false,
+ resizeDelay: 10
+ },
+
+ // border:0 is unnecessary, but avoids a bug in Firefox on OSX
+ copy = '<textarea tabindex="-1" style="position:absolute; top:-999px; left:0; right:auto; bottom:auto; border:0; padding: 0; -moz-box-sizing:content-box; -webkit-box-sizing:content-box; box-sizing:content-box; word-wrap:break-word; height:0 !important; min-height:0 !important; overflow:hidden; transition:none; -webkit-transition:none; -moz-transition:none;"/>',
+
+ // line-height is conditionally included because IE7/IE8/old Opera do not return the correct value.
+ typographyStyles = [
+ 'fontFamily',
+ 'fontSize',
+ 'fontWeight',
+ 'fontStyle',
+ 'letterSpacing',
+ 'textTransform',
+ 'wordSpacing',
+ 'textIndent'
+ ],
+
+ // to keep track which textarea is being mirrored when adjust() is called.
+ mirrored,
+
+ // the mirror element, which is used to calculate what size the mirrored element should be.
+ mirror = $(copy).data('autosize', true)[0];
+
+ // test that line-height can be accurately copied.
+ mirror.style.lineHeight = '99px';
+ if ($(mirror).css('lineHeight') === '99px') {
+ typographyStyles.push('lineHeight');
+ }
+ mirror.style.lineHeight = '';
+
+ $.fn.autosize = function (options) {
+ if (!this.length) {
+ return this;
+ }
+
+ options = $.extend({}, defaults, options || {});
+
+ if (mirror.parentNode !== document.body) {
+ $(document.body).append(mirror);
+ }
+
+ return this.each(function () {
+ var
+ ta = this,
+ $ta = $(ta),
+ maxHeight,
+ minHeight,
+ boxOffset = 0,
+ callback = $.isFunction(options.callback),
+ originalStyles = {
+ height: ta.style.height,
+ overflow: ta.style.overflow,
+ overflowY: ta.style.overflowY,
+ wordWrap: ta.style.wordWrap,
+ resize: ta.style.resize
+ },
+ timeout,
+ width = $ta.width();
+
+ if ($ta.data('autosize')) {
+ // exit if autosize has already been applied, or if the textarea is the mirror element.
+ return;
+ }
+ $ta.data('autosize', true);
+
+ if ($ta.css('box-sizing') === 'border-box' || $ta.css('-moz-box-sizing') === 'border-box' || $ta.css('-webkit-box-sizing') === 'border-box'){
+ boxOffset = $ta.outerHeight() - $ta.height();
+ }
+
+ // IE8 and lower return 'auto', which parses to NaN, if no min-height is set.
+ minHeight = Math.max(parseInt($ta.css('minHeight'), 10) - boxOffset || 0, $ta.height());
+
+ $ta.css({
+ overflow: 'hidden',
+ overflowY: 'hidden',
+ wordWrap: 'break-word', // horizontal overflow is hidden, so break-word is necessary for handling words longer than the textarea width
+ resize: ($ta.css('resize') === 'none' || $ta.css('resize') === 'vertical') ? 'none' : 'horizontal'
+ });
+
+ // The mirror width must exactly match the textarea width, so using getBoundingClientRect because it doesn't round the sub-pixel value.
+ function setWidth() {
+ var style, width;
+
+ if ('getComputedStyle' in window) {
+ style = window.getComputedStyle(ta, null);
+ width = ta.getBoundingClientRect().width;
+
+ $.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){
+ width -= parseInt(style[val],10);
+ });
+
+ mirror.style.width = width + 'px';
+ }
+ else {
+ // window.getComputedStyle, getBoundingClientRect returning a width are unsupported and unneeded in IE8 and lower.
+ mirror.style.width = Math.max($ta.width(), 0) + 'px';
+ }
+ }
+
+ function initMirror() {
+ var styles = {};
+
+ mirrored = ta;
+ mirror.className = options.className;
+ maxHeight = parseInt($ta.css('maxHeight'), 10);
+
+ // mirror is a duplicate textarea located off-screen that
+ // is automatically updated to contain the same text as the
+ // original textarea. mirror always has a height of 0.
+ // This gives a cross-browser supported way getting the actual
+ // height of the text, through the scrollTop property.
+ $.each(typographyStyles, function(i,val){
+ styles[val] = $ta.css(val);
+ });
+ $(mirror).css(styles);
+
+ setWidth();
+
+ // Chrome-specific fix:
+ // When the textarea y-overflow is hidden, Chrome doesn't reflow the text to account for the space
+ // made available by removing the scrollbar. This workaround triggers the reflow for Chrome.
+ if (window.chrome) {
+ var width = ta.style.width;
+ ta.style.width = '0px';
+ var ignore = ta.offsetWidth;
+ ta.style.width = width;
+ }
+ }
+
+ // Using mainly bare JS in this function because it is going
+ // to fire very often while typing, and needs to very efficient.
+ function adjust() {
+ var height, original;
+
+ if (mirrored !== ta) {
+ initMirror();
+ } else {
+ setWidth();
+ }
+
+ mirror.value = ta.value + options.append;
+ mirror.style.overflowY = ta.style.overflowY;
+ original = parseInt(ta.style.height,10);
+
+ // Setting scrollTop to zero is needed in IE8 and lower for the next step to be accurately applied
+ mirror.scrollTop = 0;
+
+ mirror.scrollTop = 9e4;
+
+ // Using scrollTop rather than scrollHeight because scrollHeight is non-standard and includes padding.
+ height = mirror.scrollTop;
+
+ if (maxHeight && height > maxHeight) {
+ ta.style.overflowY = 'scroll';
+ height = maxHeight;
+ } else {
+ ta.style.overflowY = 'hidden';
+ if (height < minHeight) {
+ height = minHeight;
+ }
+ }
+
+ height += boxOffset;
+
+ if (original !== height) {
+ ta.style.height = height + 'px';
+ if (callback) {
+ options.callback.call(ta,ta);
+ }
+ }
+ }
+
+ function resize () {
+ clearTimeout(timeout);
+ timeout = setTimeout(function(){
+ var newWidth = $ta.width();
+
+ if (newWidth !== width) {
+ width = newWidth;
+ adjust();
+ }
+ }, parseInt(options.resizeDelay,10));
+ }
+
+ if ('onpropertychange' in ta) {
+ if ('oninput' in ta) {
+ // Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
+ // so binding to onkeyup to catch most of those occasions. There is no way that I
+ // know of to detect something like 'cut' in IE9.
+ $ta.on('input.autosize keyup.autosize', adjust);
+ } else {
+ // IE7 / IE8
+ $ta.on('propertychange.autosize', function(){
+ if(event.propertyName === 'value'){
+ adjust();
+ }
+ });
+ }
+ } else {
+ // Modern Browsers
+ $ta.on('input.autosize', adjust);
+ }
+
+ // Set options.resizeDelay to false if using fixed-width textarea elements.
+ // Uses a timeout and width check to reduce the amount of times adjust needs to be called after window resize.
+
+ if (options.resizeDelay !== false) {
+ $(window).on('resize.autosize', resize);
+ }
+
+ // Event for manual triggering if needed.
+ // Should only be needed when the value of the textarea is changed through JavaScript rather than user input.
+ $ta.on('autosize.resize', adjust);
+
+ // Event for manual triggering that also forces the styles to update as well.
+ // Should only be needed if one of typography styles of the textarea change, and the textarea is already the target of the adjust method.
+ $ta.on('autosize.resizeIncludeStyle', function() {
+ mirrored = null;
+ adjust();
+ });
+
+ $ta.on('autosize.destroy', function(){
+ mirrored = null;
+ clearTimeout(timeout);
+ $(window).off('resize', resize);
+ $ta
+ .off('autosize')
+ .off('.autosize')
+ .css(originalStyles)
+ .removeData('autosize');
+ });
+
+ // Call adjust in case the textarea already contains text.
+ adjust();
+ });
+ };
+}(window.jQuery || window.$)); // jQuery or jQuery-like library, such as Zepto
\ No newline at end of file
diff --git a/libs/strophejingle.bundle.js b/libs/strophejingle.bundle.js
new file mode 100644
index 0000000000..a4ce80e7c3
--- /dev/null
+++ b/libs/strophejingle.bundle.js
@@ -0,0 +1,2071 @@
+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 */
+// mozilla chrome compat layer -- very similar to adapter.js
+function setupRTC() {
+ var RTC = null;
+ if (navigator.mozGetUserMedia) {
+ console.log('This appears to be Firefox');
+ var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
+ if (version >= 22) {
+ RTC = {
+ peerconnection: mozRTCPeerConnection,
+ browser: 'firefox',
+ getUserMedia: navigator.mozGetUserMedia.bind(navigator),
+ attachMediaStream: function (element, stream) {
+ element[0].mozSrcObject = stream;
+ element[0].play();
+ },
+ pc_constraints: {}
+ };
+ if (!MediaStream.prototype.getVideoTracks)
+ MediaStream.prototype.getVideoTracks = function () { return []; };
+ if (!MediaStream.prototype.getAudioTracks)
+ MediaStream.prototype.getAudioTracks = function () { return []; };
+ RTCSessionDescription = mozRTCSessionDescription;
+ RTCIceCandidate = mozRTCIceCandidate;
+ }
+ } else if (navigator.webkitGetUserMedia) {
+ console.log('This appears to be Chrome');
+ RTC = {
+ peerconnection: webkitRTCPeerConnection,
+ browser: 'chrome',
+ getUserMedia: navigator.webkitGetUserMedia.bind(navigator),
+ attachMediaStream: function (element, stream) {
+ element.attr('src', webkitURL.createObjectURL(stream));
+ },
+// pc_constraints: {} // FIVE-182
+ pc_constraints: {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]} // enable dtls support in canary
+ };
+ if (navigator.userAgent.indexOf('Android') != -1) {
+ RTC.pc_constraints = {}; // disable DTLS on Android
+ }
+ if (!webkitMediaStream.prototype.getVideoTracks) {
+ webkitMediaStream.prototype.getVideoTracks = function () {
+ return this.videoTracks;
+ };
+ }
+ if (!webkitMediaStream.prototype.getAudioTracks) {
+ webkitMediaStream.prototype.getAudioTracks = function () {
+ return this.audioTracks;
+ };
+ }
+ }
+ if (RTC === null) {
+ try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
+ }
+ return RTC;
+}
+
+function getUserMediaWithConstraints(um, resolution, bandwidth, fps) {
+ var constraints = {audio: false, video: false};
+
+ if (um.indexOf('video') >= 0) {
+ constraints.video = {mandatory: {}};// same behaviour as true
+ }
+ if (um.indexOf('audio') >= 0) {
+ constraints.audio = {};// same behaviour as true
+ }
+ if (um.indexOf('screen') >= 0) {
+ constraints.video = {
+ "mandatory": {
+ "chromeMediaSource": "screen"
+ }
+ };
+ }
+
+ if (resolution && !constraints.video) {
+ constraints.video = {mandatory: {}};// same behaviour as true
+ }
+ // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
+ switch (resolution) {
+ // 16:9 first
+ case '1080':
+ case 'fullhd':
+ constraints.video.mandatory.minWidth = 1920;
+ constraints.video.mandatory.minHeight = 1080;
+ constraints.video.mandatory.minAspectRatio = 1.77;
+ break;
+ case '720':
+ case 'hd':
+ constraints.video.mandatory.minWidth = 1280;
+ constraints.video.mandatory.minHeight = 720;
+ constraints.video.mandatory.minAspectRatio = 1.77;
+ break;
+ case '360':
+ constraints.video.mandatory.minWidth = 640;
+ constraints.video.mandatory.minHeight = 360;
+ constraints.video.mandatory.minAspectRatio = 1.77;
+ break;
+ case '180':
+ constraints.video.mandatory.minWidth = 320;
+ constraints.video.mandatory.minHeight = 180;
+ constraints.video.mandatory.minAspectRatio = 1.77;
+ break;
+ // 4:3
+ case '960':
+ constraints.video.mandatory.minWidth = 960;
+ constraints.video.mandatory.minHeight = 720;
+ break;
+ case '640':
+ case 'vga':
+ constraints.video.mandatory.minWidth = 640;
+ constraints.video.mandatory.minHeight = 480;
+ break;
+ case '320':
+ constraints.video.mandatory.minWidth = 320;
+ constraints.video.mandatory.minHeight = 240;
+ break;
+ default:
+ if (navigator.userAgent.indexOf('Android') != -1) {
+ constraints.video.mandatory.minWidth = 320;
+ constraints.video.mandatory.minHeight = 240;
+ constraints.video.mandatory.maxFrameRate = 15;
+ }
+ break;
+ }
+
+ if (bandwidth) { // doesn't work currently, see webrtc issue 1846
+ if (!constraints.video) constraints.video = {mandatory: {}};//same behaviour as true
+ constraints.video.optional = [{bandwidth: bandwidth}];
+ }
+ if (fps) { // for some cameras it might be necessary to request 30fps
+ // so they choose 30fps mjpg over 10fps yuy2
+ if (!constraints.video) constraints.video = {mandatory: {}};// same behaviour as tru;
+ constraints.video.mandatory.minFrameRate = fps;
+ }
+
+ try {
+ RTC.getUserMedia(constraints,
+ function (stream) {
+ console.log('onUserMediaSuccess');
+ $(document).trigger('mediaready.jingle', [stream]);
+ },
+ function (error) {
+ console.warn('Failed to get access to local media. Error ', error);
+ $(document).trigger('mediafailure.jingle');
+ });
+ } catch (e) {
+ console.error('GUM failed: ', e);
+ $(document).trigger('mediafailure.jingle');
+ }
+}
+/* jshint -W117 */
+Strophe.addConnectionPlugin('jingle', {
+ connection: null,
+ sessions: {},
+ jid2session: {},
+ ice_config: {iceServers: []},
+ pc_constraints: {},
+ media_constraints: {
+ mandatory: {
+ 'OfferToReceiveAudio': true,
+ 'OfferToReceiveVideo': true
+ }
+ // MozDontOfferDataChannel: true when this is firefox
+ },
+ localStream: null,
+
+ init: function (conn) {
+ this.connection = conn;
+ if (this.connection.disco) {
+ // http://xmpp.org/extensions/xep-0167.html#support
+ // http://xmpp.org/extensions/xep-0176.html#support
+ this.connection.disco.addFeature('urn:xmpp:jingle:1');
+ this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:1');
+ this.connection.disco.addFeature('urn:xmpp:jingle:transports:ice-udp:1');
+ this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:audio');
+ this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:video');
+
+
+ // this is dealt with by SDP O/A so we don't need to annouce this
+ //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtcp-fb:0'); // XEP-0293
+ //this.connection.disco.addFeature('urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'); // XEP-0294
+ this.connection.disco.addFeature('urn:ietf:rfc:5761'); // rtcp-mux
+ //this.connection.disco.addFeature('urn:ietf:rfc:5888'); // a=group, e.g. bundle
+ //this.connection.disco.addFeature('urn:ietf:rfc:5576'); // a=ssrc
+ }
+ this.connection.addHandler(this.onJingle.bind(this), 'urn:xmpp:jingle:1', 'iq', 'set', null, null);
+ },
+ onJingle: function (iq) {
+ var sid = $(iq).find('jingle').attr('sid');
+ var action = $(iq).find('jingle').attr('action');
+ // send ack first
+ var ack = $iq({type: 'result',
+ to: iq.getAttribute('from'),
+ id: iq.getAttribute('id')
+ });
+ console.log('on jingle ' + action);
+ var sess = this.sessions[sid];
+ if ('session-initiate' != action) {
+ if (sess === null) {
+ ack.type = 'error';
+ ack.c('error', {type: 'cancel'})
+ .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
+ .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
+ this.connection.send(ack);
+ return true;
+ }
+ // compare from to sess.peerjid (bare jid comparison for later compat with message-mode)
+ // local jid is not checked
+ if (Strophe.getBareJidFromJid(iq.getAttribute('from')) != Strophe.getBareJidFromJid(sess.peerjid)) {
+ console.warn('jid mismatch for session id', sid, iq.getAttribute('from'), sess.peerjid);
+ ack.type = 'error';
+ ack.c('error', {type: 'cancel'})
+ .c('item-not-found', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up()
+ .c('unknown-session', {xmlns: 'urn:xmpp:jingle:errors:1'});
+ this.connection.send(ack);
+ return true;
+ }
+ } else if (sess !== undefined) {
+ // existing session with same session id
+ // this might be out-of-order if the sess.peerjid is the same as from
+ ack.type = 'error';
+ ack.c('error', {type: 'cancel'})
+ .c('service-unavailable', {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}).up();
+ console.warn('duplicate session id', sid);
+ this.connection.send(ack);
+ return true;
+ }
+ // FIXME: check for a defined action
+ this.connection.send(ack);
+ // see http://xmpp.org/extensions/xep-0166.html#concepts-session
+ switch (action) {
+ case 'session-initiate':
+ sess = new JingleSession($(iq).attr('to'), $(iq).find('jingle').attr('sid'), this.connection);
+ // configure session
+ if (this.localStream) {
+ sess.localStreams.push(this.localStream);
+ }
+ sess.media_constraints = this.media_constraints;
+ sess.pc_constraints = this.pc_constraints;
+ sess.ice_config = this.ice_config;
+
+ sess.initiate($(iq).attr('from'), false);
+ // FIXME: setRemoteDescription should only be done when this call is to be accepted
+ sess.setRemoteDescription($(iq).find('>jingle'), 'offer');
+
+ this.sessions[sess.sid] = sess;
+ this.jid2session[sess.peerjid] = sess;
+
+ // the callback should either
+ // .sendAnswer and .accept
+ // or .sendTerminate -- not necessarily synchronus
+ $(document).trigger('callincoming.jingle', [sess.sid]);
+ break;
+ case 'session-accept':
+ sess.setRemoteDescription($(iq).find('>jingle'), 'answer');
+ sess.accept();
+ break;
+ case 'session-terminate':
+ console.log('terminating...');
+ sess.terminate();
+ this.terminate(sess.sid);
+ if ($(iq).find('>jingle>reason').length) {
+ $(document).trigger('callterminated.jingle', [
+ sess.sid,
+ $(iq).find('>jingle>reason>:first')[0].tagName,
+ $(iq).find('>jingle>reason>text').text()
+ ]);
+ } else {
+ $(document).trigger('callterminated.jingle', [sess.sid]);
+ }
+ break;
+ case 'transport-info':
+ sess.addIceCandidate($(iq).find('>jingle>content'));
+ break;
+ case 'session-info':
+ var affected;
+ if ($(iq).find('>jingle>ringing[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
+ $(document).trigger('ringing.jingle', [sess.sid]);
+ } else if ($(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
+ affected = $(iq).find('>jingle>mute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
+ $(document).trigger('mute.jingle', [sess.sid, affected]);
+ } else if ($(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').length) {
+ affected = $(iq).find('>jingle>unmute[xmlns="urn:xmpp:jingle:apps:rtp:info:1"]').attr('name');
+ $(document).trigger('unmute.jingle', [sess.sid, affected]);
+ }
+ break;
+ case 'addsource': // FIXME: proprietary
+ sess.addSource($(iq).find('>jingle>content'));
+ break;
+ case 'removesource': // FIXME: proprietary
+ sess.removeSource($(iq).find('>jingle>content'));
+ break;
+ default:
+ console.warn('jingle action not implemented', action);
+ break;
+ }
+ return true;
+ },
+ initiate: function (peerjid, myjid) { // initiate a new jinglesession to peerjid
+ var sess = new JingleSession(myjid,
+ Math.random().toString(36).substr(2, 12), // random string
+ this.connection);
+ // configure session
+ if (this.localStream) {
+ sess.localStreams.push(this.localStream);
+ }
+ sess.media_constraints = this.media_constraints;
+ sess.pc_constraints = this.pc_constraints;
+ sess.ice_config = this.ice_config;
+
+ sess.initiate(peerjid, true);
+ this.sessions[sess.sid] = sess;
+ this.jid2session[sess.peerjid] = sess;
+ sess.sendOffer();
+ return sess;
+ },
+ terminate: function (sid, reason, text) { // terminate by sessionid (or all sessions)
+ if (sid === null || sid === undefined) {
+ for (sid in this.sessions) {
+ if (this.sessions[sid].state != 'ended') {
+ this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
+ this.sessions[sid].terminate();
+ }
+ delete this.jid2session[this.sessions[sid].peerjid];
+ delete this.sessions[sid];
+ }
+ } else if (this.sessions.hasOwnProperty(sid)) {
+ if (this.sessions[sid].state != 'ended') {
+ this.sessions[sid].sendTerminate(reason || (!this.sessions[sid].active()) ? 'cancel' : null, text);
+ this.sessions[sid].terminate();
+ }
+ delete this.jid2session[this.sessions[sid].peerjid];
+ delete this.sessions[sid];
+ }
+ },
+ terminateByJid: function (jid) {
+ if (this.jid2session.hasOwnProperty(jid)) {
+ var sess = this.jid2session[jid];
+ if (sess) {
+ sess.terminate();
+ console.log('peer went away silently', jid);
+ delete this.sessions[sess.sid];
+ delete this.jid2session[jid];
+ $(document).trigger('callterminated.jingle', [sess.sid, 'gone']);
+ }
+ }
+ },
+ getStunAndTurnCredentials: function () {
+ // get stun and turn configuration from server via xep-0215
+ // uses time-limited credentials as described in
+ // http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00
+ //
+ // see https://code.google.com/p/prosody-modules/source/browse/mod_turncredentials/mod_turncredentials.lua
+ // for a prosody module which implements this
+ //
+ // currently, this doesn't work with updateIce and therefore credentials with a long
+ // validity have to be fetched before creating the peerconnection
+ // TODO: implement refresh via updateIce as described in
+ // https://code.google.com/p/webrtc/issues/detail?id=1650
+ this.connection.sendIQ(
+ $iq({type: 'get', to: this.connection.domain})
+ .c('services', {xmlns: 'urn:xmpp:extdisco:1'}).c('service', {host: 'turn.' + this.connection.domain}),
+ function (res) {
+ var iceservers = [];
+ $(res).find('>services>service').each(function (idx, el) {
+ el = $(el);
+ var dict = {};
+ switch (el.attr('type')) {
+ case 'stun':
+ dict.url = 'stun:' + el.attr('host');
+ if (el.attr('port')) {
+ dict.url += ':' + el.attr('port');
+ }
+ iceservers.push(dict);
+ break;
+ case 'turn':
+ dict.url = 'turn:';
+ if (el.attr('username')) { // https://code.google.com/p/webrtc/issues/detail?id=1508
+ if (navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) && parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10) < 28) {
+ dict.url += el.attr('username') + '@';
+ } else {
+ dict.username = el.attr('username'); // only works in M28
+ }
+ }
+ dict.url += el.attr('host');
+ if (el.attr('port') && el.attr('port') != '3478') {
+ dict.url += ':' + el.attr('port');
+ }
+ if (el.attr('transport') && el.attr('transport') != 'udp') {
+ dict.url += '?transport=' + el.attr('transport');
+ }
+ if (el.attr('password')) {
+ dict.credential = el.attr('password');
+ }
+ iceservers.push(dict);
+ break;
+ }
+ });
+ this.ice_config.iceServers = iceservers;
+ },
+ function (err) {
+ console.warn('getting turn credentials failed', err);
+ console.warn('is mod_turncredentials or similar installed?');
+ }
+ );
+ // implement push?
+ }
+});
+/* jshint -W117 */
+// SDP STUFF
+function SDP(sdp) {
+ this.media = sdp.split('\r\nm=');
+ for (var i = 1; i < this.media.length; i++) {
+ this.media[i] = 'm=' + this.media[i];
+ if (i != this.media.length - 1) {
+ this.media[i] += '\r\n';
+ }
+ }
+ this.session = this.media.shift() + '\r\n';
+ this.raw = this.session + this.media.join('');
+}
+
+// remove iSAC and CN from SDP
+SDP.prototype.mangle = function () {
+ var i, j, mline, lines, rtpmap, newdesc;
+ for (i = 0; i < this.media.length; i++) {
+ lines = this.media[i].split('\r\n');
+ lines.pop(); // remove empty last element
+ mline = SDPUtil.parse_mline(lines.shift());
+ if (mline.media != 'audio')
+ continue;
+ newdesc = '';
+ mline.fmt.length = 0;
+ for (j = 0; j < lines.length; j++) {
+ if (lines[j].substr(0, 9) == 'a=rtpmap:') {
+ rtpmap = SDPUtil.parse_rtpmap(lines[j]);
+ if (rtpmap.name == 'CN' || rtpmap.name == 'ISAC')
+ continue;
+ mline.fmt.push(rtpmap.id);
+ newdesc += lines[j] + '\r\n';
+ } else {
+ newdesc += lines[j] + '\r\n';
+ }
+ }
+ this.media[i] = SDPUtil.build_mline(mline) + '\r\n';
+ this.media[i] += newdesc;
+ }
+ this.raw = this.session + this.media.join('');
+};
+
+// remove lines matching prefix from session section
+SDP.prototype.removeSessionLines = function(prefix) {
+ var ob = this;
+ var lines = SDPUtil.find_lines(this.session, prefix);
+ lines.forEach(function(line) {
+ ob.session = ob.session.replace(line + '\r\n', '');
+ });
+ this.raw = this.session + this.media.join('');
+ return lines;
+}
+// remove lines matching prefix from a media section specified by mediaindex
+// TODO: non-numeric mediaindex could match mid
+SDP.prototype.removeMediaLines = function(mediaindex, prefix) {
+ var ob = this;
+ var lines = SDPUtil.find_lines(this.media[mediaindex], prefix);
+ lines.forEach(function(line) {
+ ob.media[mediaindex] = ob.media[mediaindex].replace(line + '\r\n', '');
+ });
+ this.raw = this.session + this.media.join('');
+ return lines;
+}
+
+// add content's to a jingle element
+SDP.prototype.toJingle = function (elem, thecreator) {
+ var i, j, k, mline, ssrc, rtpmap, tmp, line, lines;
+ var ob = this;
+ // new bundle plan
+ if (SDPUtil.find_line(this.session, 'a=group:')) {
+ lines = SDPUtil.find_lines(this.session, 'a=group:');
+ for (i = 0; i < lines.length; i++) {
+ tmp = lines[i].split(' ');
+ var semantics = tmp.shift().substr(8);
+ // new plan
+ elem.c('group', {xmlns: 'urn:xmpp:jingle:apps:grouping:0', type: semantics, semantics:semantics});
+ for (j = 0; j < tmp.length; j++) {
+ elem.c('content', {name: tmp[j]}).up();
+ }
+ elem.up();
+
+ // temporary plan, to be removed
+ elem.c('group', {xmlns: 'urn:ietf:rfc:5888', type: semantics});
+ for (j = 0; j < tmp.length; j++) {
+ elem.c('content', {name: tmp[j]}).up();
+ }
+ elem.up();
+ }
+ }
+ // old bundle plan, to be removed
+ var bundle = [];
+ if (SDPUtil.find_line(this.session, 'a=group:BUNDLE')) {
+ bundle = SDPUtil.find_line(this.session, 'a=group:BUNDLE ').split(' ');
+ bundle.shift();
+ }
+ for (i = 0; i < this.media.length; i++) {
+ mline = SDPUtil.parse_mline(this.media[i].split('\r\n')[0]);
+ if (!(mline.media == 'audio' || mline.media == 'video')) {
+ continue;
+ }
+ if (SDPUtil.find_line(this.media[i], 'a=ssrc:')) {
+ ssrc = SDPUtil.find_line(this.media[i], 'a=ssrc:').substring(7).split(' ')[0]; // take the first
+ } else {
+ ssrc = false;
+ }
+
+ elem.c('content', {creator: thecreator, name: mline.media});
+ if (SDPUtil.find_line(this.media[i], 'a=mid:')) {
+ // prefer identifier from a=mid if present
+ var mid = SDPUtil.parse_mid(SDPUtil.find_line(this.media[i], 'a=mid:'));
+ elem.attrs({ name: mid });
+
+ // old BUNDLE plan, to be removed
+ if (bundle.indexOf(mid) != -1) {
+ elem.c('bundle', {xmlns: 'http://estos.de/ns/bundle'}).up();
+ bundle.splice(bundle.indexOf(mid), 1);
+ }
+ }
+ if (SDPUtil.find_line(this.media[i], 'a=rtpmap:').length) {
+ elem.c('description',
+ {xmlns: 'urn:xmpp:jingle:apps:rtp:1',
+ media: mline.media });
+ if (ssrc) {
+ elem.attrs({ssrc: ssrc});
+ }
+ for (j = 0; j < mline.fmt.length; j++) {
+ rtpmap = SDPUtil.find_line(this.media[i], 'a=rtpmap:' + mline.fmt[j]);
+ elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
+ // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
+ if (SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j])) {
+ tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j]));
+ for (k = 0; k < tmp.length; k++) {
+ elem.c('parameter', tmp[k]).up();
+ }
+ }
+ this.RtcpFbToJingle(this.media[i], elem, mline.fmt[j]); // XEP-0293 -- map a=rtcp-fb
+
+ elem.up();
+ }
+ if (SDPUtil.find_line(this.media[i], 'a=crypto:', this.session)) {
+ elem.c('encryption', {required: 1});
+ var crypto = SDPUtil.find_lines(this.media[i], 'a=crypto:', this.session);
+ crypto.forEach(function(line) {
+ elem.c('crypto', SDPUtil.parse_crypto(line)).up();
+ });
+ elem.up(); // end of encryption
+ }
+
+ if (ssrc) {
+ // new style mapping
+ elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
+ // FIXME: group by ssrc and support multiple different ssrcs
+ var ssrclines = SDPUtil.find_lines(this.media[i], 'a=ssrc:');
+ ssrclines.forEach(function(line) {
+ idx = line.indexOf(' ');
+ var linessrc = line.substr(0, idx).substr(7);
+ if (linessrc != ssrc) {
+ elem.up();
+ ssrc = linessrc;
+ elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
+ }
+ var kv = line.substr(idx + 1);
+ elem.c('parameter');
+ if (kv.indexOf(':') == -1) {
+ elem.attrs({ name: kv });
+ } else {
+ elem.attrs({ name: kv.split(':', 2)[0] });
+ elem.attrs({ value: kv.split(':', 2)[1] });
+ }
+ elem.up();
+ });
+ elem.up();
+
+ // old proprietary mapping, to be removed at some point
+ tmp = SDPUtil.parse_ssrc(this.media[i]);
+ tmp.xmlns = 'http://estos.de/ns/ssrc';
+ tmp.ssrc = ssrc;
+ elem.c('ssrc', tmp).up(); // ssrc is part of description
+ }
+
+ if (SDPUtil.find_line(this.media[i], 'a=rtcp-mux')) {
+ elem.c('rtcp-mux').up();
+ }
+
+ // XEP-0293 -- map a=rtcp-fb:*
+ this.RtcpFbToJingle(this.media[i], elem, '*');
+
+ // XEP-0294
+ if (SDPUtil.find_line(this.media[i], 'a=extmap:')) {
+ lines = SDPUtil.find_lines(this.media[i], 'a=extmap:');
+ for (j = 0; j < lines.length; j++) {
+ tmp = SDPUtil.parse_extmap(lines[j]);
+ elem.c('rtp-hdrext', { xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
+ uri: tmp.uri,
+ id: tmp.value });
+ if (tmp.hasOwnProperty('direction')) {
+ switch (tmp.direction) {
+ case 'sendonly':
+ elem.attrs({senders: 'responder'});
+ break;
+ case 'recvonly':
+ elem.attrs({senders: 'initiator'});
+ break;
+ case 'sendrecv':
+ elem.attrs({senders: 'both'});
+ break;
+ case 'inactive':
+ elem.attrs({senders: 'none'});
+ break;
+ }
+ }
+ // TODO: handle params
+ elem.up();
+ }
+ }
+ elem.up(); // end of description
+ }
+
+ elem.c('transport', {xmlns: 'urn:xmpp:jingle:transports:ice-udp:1'});
+ // XEP-0320
+ var fingerprints = SDPUtil.find_lines(this.media[i], 'a=fingerprint:', this.session);
+ fingerprints.forEach(function(line) {
+ tmp = SDPUtil.parse_fingerprint(line);
+ tmp.xmlns = 'urn:xmpp:tmp:jingle:apps:dtls:0';
+ // tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0'; -- FIXME: update receivers first
+ elem.c('fingerprint').t(tmp.fingerprint);
+ delete tmp.fingerprint;
+ line = SDPUtil.find_line(ob.media[i], 'a=setup:', ob.session);
+ if (line) {
+ tmp.setup = line.substr(8);
+ }
+ elem.attrs(tmp);
+ elem.up();
+ });
+ tmp = SDPUtil.iceparams(this.media[i], this.session);
+ if (tmp) {
+ elem.attrs(tmp);
+ // XEP-0176
+ if (SDPUtil.find_line(this.media[i], 'a=candidate:', this.session)) { // add any a=candidate lines
+ lines = SDPUtil.find_lines(this.media[i], 'a=candidate:', this.session);
+ for (j = 0; j < lines.length; j++) {
+ tmp = SDPUtil.candidateToJingle(lines[j]);
+ elem.c('candidate', tmp).up();
+ }
+ }
+ elem.up(); // end of transport
+ }
+
+ if (SDPUtil.find_line(this.media[i], 'a=sendrecv', this.session)) {
+ elem.attrs({senders: 'both'});
+ } else if (SDPUtil.find_line(this.media[i], 'a=sendonly', this.session)) {
+ elem.attrs({senders: 'initiator'});
+ } else if (SDPUtil.find_line(this.media[i], 'a=recvonly', this.session)) {
+ elem.attrs({senders: 'responder'});
+ } else if (SDPUtil.find_line(this.media[i], 'a=inactive', this.session)) {
+ elem.attrs({senders: 'none'});
+ }
+ if (mline.port == '0') {
+ // estos hack to reject an m-line
+ elem.attrs({senders: 'rejected'});
+ }
+ elem.up(); // end of content
+ }
+ elem.up();
+ return elem;
+};
+
+SDP.prototype.RtcpFbToJingle = function (sdp, elem, payloadtype) { // XEP-0293
+ var lines = SDPUtil.find_lines(sdp, 'a=rtcp-fb:' + payloadtype);
+ for (var i = 0; i < lines.length; i++) {
+ var tmp = SDPUtil.parse_rtcpfb(lines[i]);
+ if (tmp.type == 'trr-int') {
+ elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
+ elem.up();
+ } else {
+ elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
+ if (tmp.params.length > 0) {
+ elem.attrs({'subtype': tmp.params[0]});
+ }
+ elem.up();
+ }
+ }
+};
+
+SDP.prototype.RtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
+ var media = '';
+ var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
+ if (tmp.length) {
+ media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
+ if (tmp.attr('value')) {
+ media += tmp.attr('value');
+ } else {
+ media += '0';
+ }
+ media += '\r\n';
+ }
+ tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
+ tmp.each(function () {
+ media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
+ if ($(this).attr('subtype')) {
+ media += ' ' + $(this).attr('subtype');
+ }
+ media += '\r\n';
+ });
+ return media;
+};
+
+// construct an SDP from a jingle stanza
+SDP.prototype.fromJingle = function (jingle) {
+ var obj = this;
+ this.raw = 'v=0\r\n' +
+ 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
+ 's=-\r\n' +
+ 't=0 0\r\n';
+ // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
+ if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
+ try {
+ $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
+ var contents = $(group).find('>content').map(function (idx, content) {
+ return $(content).attr('name');
+ }).get();
+ if (contents.length > 0) {
+ obj.raw += 'a=group:' + ($(group).attr('semantics') || $(group).attr('type')) + ' ' + contents.join(' ') + '\r\n';
+ }
+ });
+ } catch (e) { console.error(e.toString()); }
+ } else if ($(jingle).find('>group[xmlns="urn:ietf:rfc:5888"]').length) {
+ // temporary namespace, not to be used. to be removed soon.
+ $(jingle).find('>group[xmlns="urn:ietf:rfc:5888"]').each(function (idx, group) {
+ var contents = $(group).find('>content').map(function (idx, content) {
+ return $(content).attr('name');
+ }).get();
+ if ($(group).attr('type') !== null && contents.length > 0) {
+ obj.raw += 'a=group:' + $(group).attr('type') + ' ' + contents.join(' ') + '\r\n';
+ }
+ });
+ } else {
+ // for backward compability, to be removed soon
+ // assume all contents are in the same bundle group, can be improved upon later
+ var bundle = $(jingle).find('>content').filter(function (idx, content) {
+ //elem.c('bundle', {xmlns:'http://estos.de/ns/bundle'});
+ return $(content).find('>bundle').length > 0;
+ }).map(function (idx, content) {
+ return $(content).attr('name');
+ }).get();
+ if (bundle.length) {
+ this.raw += 'a=group:BUNDLE ' + bundle.join(' ') + '\r\n';
+ }
+ }
+
+ this.session = this.raw;
+ jingle.find('>content').each(function () {
+ var m = obj.jingle2media($(this));
+ obj.media.push(m);
+ });
+
+ // reconstruct msid-semantic -- apparently not necessary
+ /*
+ var msid = SDPUtil.parse_ssrc(this.raw);
+ if (msid.hasOwnProperty('mslabel')) {
+ this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
+ }
+ */
+
+ this.raw = this.session + this.media.join('');
+};
+
+// translate a jingle content element into an an SDP media part
+SDP.prototype.jingle2media = function (content) {
+ var media = '',
+ desc = content.find('description'),
+ ssrc = desc.attr('ssrc'),
+ self = this,
+ tmp;
+
+ tmp = { media: desc.attr('media') };
+ tmp.port = '1';
+ if (content.attr('senders') == 'rejected') {
+ // estos hack to reject an m-line.
+ tmp.port = '0';
+ }
+ if (content.find('>transport>fingerprint').length || desc.find('encryption').length) {
+ tmp.proto = 'RTP/SAVPF';
+ } else {
+ tmp.proto = 'RTP/AVPF';
+ }
+ tmp.fmt = desc.find('payload-type').map(function () { return $(this).attr('id'); }).get();
+ media += SDPUtil.build_mline(tmp) + '\r\n';
+ media += 'c=IN IP4 0.0.0.0\r\n';
+ media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
+ tmp = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
+ if (tmp.length) {
+ if (tmp.attr('ufrag')) {
+ media += SDPUtil.build_iceufrag(tmp.attr('ufrag')) + '\r\n';
+ }
+ if (tmp.attr('pwd')) {
+ media += SDPUtil.build_icepwd(tmp.attr('pwd')) + '\r\n';
+ }
+ tmp.find('>fingerprint').each(function () {
+ // FIXME: check namespace at some point
+ media += 'a=fingerprint:' + $(this).attr('hash');
+ media += ' ' + $(this).text();
+ media += '\r\n';
+ if ($(this).attr('setup')) {
+ media += 'a=setup:' + $(this).attr('setup') + '\r\n';
+ }
+ });
+ }
+ switch (content.attr('senders')) {
+ case 'initiator':
+ media += 'a=sendonly\r\n';
+ break;
+ case 'responder':
+ media += 'a=recvonly\r\n';
+ break;
+ case 'none':
+ media += 'a=inactive\r\n';
+ break;
+ case 'both':
+ media += 'a=sendrecv\r\n';
+ break;
+ }
+ media += 'a=mid:' + content.attr('name') + '\r\n';
+
+ // <description><rtcp-mux/></description>
+ // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
+ // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
+ if (desc.find('rtcp-mux').length) {
+ media += 'a=rtcp-mux\r\n';
+ }
+
+ if (desc.find('encryption').length) {
+ desc.find('encryption>crypto').each(function () {
+ media += 'a=crypto:' + $(this).attr('tag');
+ media += ' ' + $(this).attr('crypto-suite');
+ media += ' ' + $(this).attr('key-params');
+ if ($(this).attr('session-params')) {
+ media += ' ' + $(this).attr('session-params');
+ }
+ media += '\r\n';
+ });
+ }
+ desc.find('payload-type').each(function () {
+ media += SDPUtil.build_rtpmap(this) + '\r\n';
+ if ($(this).find('>parameter').length) {
+ media += 'a=fmtp:' + $(this).attr('id') + ' ';
+ media += $(this).find('parameter').map(function () { return ($(this).attr('name') ? ($(this).attr('name') + '=') : '') + $(this).attr('value'); }).get().join(';');
+ media += '\r\n';
+ }
+ // xep-0293
+ media += self.RtcpFbFromJingle($(this), $(this).attr('id'));
+ });
+
+ // xep-0293
+ media += self.RtcpFbFromJingle(desc, '*');
+
+ // xep-0294
+ tmp = desc.find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
+ tmp.each(function () {
+ media += 'a=extmap:' + $(this).attr('id') + ' ' + $(this).attr('uri') + '\r\n';
+ });
+
+ content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]>candidate').each(function () {
+ media += SDPUtil.candidateFromJingle(this);
+ });
+
+ tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
+ tmp.each(function () {
+ var ssrc = $(this).attr('ssrc');
+ $(this).find('>parameter').each(function () {
+ media += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
+ if ($(this).attr('value') && $(this).attr('value').length)
+ media += ':' + $(this).attr('value');
+ media += '\r\n';
+ });
+ });
+
+ if (tmp.length === 0) {
+ // fallback to proprietary mapping of a=ssrc lines
+ tmp = content.find('description>ssrc[xmlns="http://estos.de/ns/ssrc"]');
+ if (tmp.length) {
+ media += 'a=ssrc:' + ssrc + ' cname:' + tmp.attr('cname') + '\r\n';
+ media += 'a=ssrc:' + ssrc + ' msid:' + tmp.attr('msid') + '\r\n';
+ media += 'a=ssrc:' + ssrc + ' mslabel:' + tmp.attr('mslabel') + '\r\n';
+ media += 'a=ssrc:' + ssrc + ' label:' + tmp.attr('label') + '\r\n';
+ }
+ }
+ return media;
+};
+
+SDPUtil = {
+ iceparams: function (mediadesc, sessiondesc) {
+ var data = null;
+ if (SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc) &&
+ SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc)) {
+ data = {
+ ufrag: SDPUtil.parse_iceufrag(SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc)),
+ pwd: SDPUtil.parse_icepwd(SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc))
+ };
+ }
+ return data;
+ },
+ parse_iceufrag: function (line) {
+ return line.substring(12);
+ },
+ build_iceufrag: function (frag) {
+ return 'a=ice-ufrag:' + frag;
+ },
+ parse_icepwd: function (line) {
+ return line.substring(10);
+ },
+ build_icepwd: function (pwd) {
+ return 'a=ice-pwd:' + pwd;
+ },
+ parse_mid: function (line) {
+ return line.substring(6);
+ },
+ parse_mline: function (line) {
+ var parts = line.substring(2).split(' '),
+ data = {};
+ data.media = parts.shift();
+ data.port = parts.shift();
+ data.proto = parts.shift();
+ if (parts[parts.length - 1] === '') { // trailing whitespace
+ parts.pop();
+ }
+ data.fmt = parts;
+ return data;
+ },
+ build_mline: function (mline) {
+ return 'm=' + mline.media + ' ' + mline.port + ' ' + mline.proto + ' ' + mline.fmt.join(' ');
+ },
+ parse_rtpmap: function (line) {
+ var parts = line.substring(9).split(' '),
+ data = {};
+ data.id = parts.shift();
+ parts = parts[0].split('/');
+ data.name = parts.shift();
+ data.clockrate = parts.shift();
+ data.channels = parts.length ? parts.shift() : '1';
+ return data;
+ },
+ build_rtpmap: function (el) {
+ var line = 'a=rtpmap:' + el.getAttribute('id') + ' ' + el.getAttribute('name') + '/' + el.getAttribute('clockrate');
+ if (el.getAttribute('channels') && el.getAttribute('channels') != '1') {
+ line += '/' + el.getAttribute('channels');
+ }
+ return line;
+ },
+ parse_crypto: function (line) {
+ var parts = line.substring(9).split(' '),
+ data = {};
+ data.tag = parts.shift();
+ data['crypto-suite'] = parts.shift();
+ data['key-params'] = parts.shift();
+ if (parts.length) {
+ data['session-params'] = parts.join(' ');
+ }
+ return data;
+ },
+ parse_fingerprint: function (line) { // RFC 4572
+ var parts = line.substring(14).split(' '),
+ data = {};
+ data.hash = parts.shift();
+ data.fingerprint = parts.shift();
+ // TODO assert that fingerprint satisfies 2UHEX *(":" 2UHEX) ?
+ return data;
+ },
+ parse_fmtp: function (line) {
+ var parts = line.split(' '),
+ i, key, value,
+ data = [];
+ parts.shift();
+ parts = parts.join(' ').split(';');
+ for (i = 0; i < parts.length; i++) {
+ key = parts[i].split('=')[0];
+ while (key.length && key[0] == ' ') {
+ key = key.substring(1);
+ }
+ value = parts[i].split('=')[1];
+ if (key && value) {
+ data.push({name: key, value: value});
+ } else if (key) {
+ // rfc 4733 (DTMF) style stuff
+ data.push({name: '', value: key});
+ }
+ }
+ return data;
+ },
+ parse_icecandidate: function (line) {
+ var candidate = {},
+ elems = line.split(' ');
+ candidate.foundation = elems[0].substring(12);
+ candidate.component = elems[1];
+ candidate.protocol = elems[2].toLowerCase();
+ candidate.priority = elems[3];
+ candidate.ip = elems[4];
+ candidate.port = elems[5];
+ // elems[6] => "typ"
+ candidate.type = elems[7];
+ candidate.generation = 0; // default value, may be overwritten below
+ for (var i = 8; i < elems.length; i += 2) {
+ switch (elems[i]) {
+ case 'raddr':
+ candidate['rel-addr'] = elems[i + 1];
+ break;
+ case 'rport':
+ candidate['rel-port'] = elems[i + 1];
+ break;
+ case 'generation':
+ candidate.generation = elems[i + 1];
+ break;
+ default: // TODO
+ console.log('parse_icecandidate not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
+ }
+ }
+ candidate.network = '1';
+ candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
+ return candidate;
+ },
+ build_icecandidate: function (cand) {
+ var line = ['a=candidate:' + cand.foundation, cand.component, cand.protocol, cand.priority, cand.ip, cand.port, 'typ', cand.type].join(' ');
+ line += ' ';
+ switch (cand.type) {
+ case 'srflx':
+ case 'prflx':
+ case 'relay':
+ if (cand.hasOwnAttribute('rel-addr') && cand.hasOwnAttribute('rel-port')) {
+ line += 'raddr';
+ line += ' ';
+ line += cand['rel-addr'];
+ line += ' ';
+ line += 'rport';
+ line += ' ';
+ line += cand['rel-port'];
+ line += ' ';
+ }
+ break;
+ }
+ line += 'generation';
+ line += ' ';
+ line += cand.hasOwnAttribute('generation') ? cand.generation : '0';
+ return line;
+ },
+ parse_ssrc: function (desc) {
+ // proprietary mapping of a=ssrc lines
+ // TODO: see "Jingle RTP Source Description" by Juberti and P. Thatcher on google docs
+ // and parse according to that
+ var lines = desc.split('\r\n'),
+ data = {};
+ for (var i = 0; i < lines.length; i++) {
+ if (lines[i].substring(0, 7) == 'a=ssrc:') {
+ var idx = lines[i].indexOf(' ');
+ data[lines[i].substr(idx + 1).split(':', 2)[0]] = lines[i].substr(idx + 1).split(':', 2)[1];
+ }
+ }
+ return data;
+ },
+ parse_rtcpfb: function (line) {
+ var parts = line.substr(10).split(' ');
+ var data = {};
+ data.pt = parts.shift();
+ data.type = parts.shift();
+ data.params = parts;
+ return data;
+ },
+ parse_extmap: function (line) {
+ var parts = line.substr(9).split(' ');
+ var data = {};
+ data.value = parts.shift();
+ if (data.value.indexOf('/') != -1) {
+ data.direction = data.value.substr(data.value.indexOf('/') + 1);
+ data.value = data.value.substr(0, data.value.indexOf('/'));
+ } else {
+ data.direction = 'both';
+ }
+ data.uri = parts.shift();
+ data.params = parts;
+ return data;
+ },
+ find_line: function (haystack, needle, sessionpart) {
+ var lines = haystack.split('\r\n');
+ for (var i = 0; i < lines.length; i++) {
+ if (lines[i].substring(0, needle.length) == needle) {
+ return lines[i];
+ }
+ }
+ if (!sessionpart) {
+ return false;
+ }
+ // search session part
+ lines = sessionpart.split('\r\n');
+ for (var j = 0; j < lines.length; j++) {
+ if (lines[j].substring(0, needle.length) == needle) {
+ return lines[j];
+ }
+ }
+ return false;
+ },
+ find_lines: function (haystack, needle, sessionpart) {
+ var lines = haystack.split('\r\n'),
+ needles = [];
+ for (var i = 0; i < lines.length; i++) {
+ if (lines[i].substring(0, needle.length) == needle)
+ needles.push(lines[i]);
+ }
+ if (needles.length || !sessionpart) {
+ return needles;
+ }
+ // search session part
+ lines = sessionpart.split('\r\n');
+ for (var j = 0; j < lines.length; j++) {
+ if (lines[j].substring(0, needle.length) == needle) {
+ needles.push(lines[j]);
+ }
+ }
+ return needles;
+ },
+ candidateToJingle: function (line) {
+ // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
+ // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
+ if (line.substring(0, 12) != 'a=candidate:') {
+ console.log('parseCandidate called with a line that is not a candidate line');
+ console.log(line);
+ return null;
+ }
+ if (line.substring(line.length - 2) == '\r\n') // chomp it
+ line = line.substring(0, line.length - 2);
+ var candidate = {},
+ elems = line.split(' '),
+ i;
+ if (elems[6] != 'typ') {
+ console.log('did not find typ in the right place');
+ console.log(line);
+ return null;
+ }
+ candidate.foundation = elems[0].substring(12);
+ candidate.component = elems[1];
+ candidate.protocol = elems[2].toLowerCase();
+ candidate.priority = elems[3];
+ candidate.ip = elems[4];
+ candidate.port = elems[5];
+ // elems[6] => "typ"
+ candidate.type = elems[7];
+ for (i = 8; i < elems.length; i += 2) {
+ switch (elems[i]) {
+ case 'raddr':
+ candidate['rel-addr'] = elems[i + 1];
+ break;
+ case 'rport':
+ candidate['rel-port'] = elems[i + 1];
+ break;
+ case 'generation':
+ candidate.generation = elems[i + 1];
+ break;
+ default: // TODO
+ console.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
+ }
+ }
+ candidate.network = '1';
+ candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
+ return candidate;
+ },
+ candidateFromJingle: function (cand) {
+ var line = 'a=candidate:';
+ line += cand.getAttribute('foundation');
+ line += ' ';
+ line += cand.getAttribute('component');
+ line += ' ';
+ line += cand.getAttribute('protocol'); //.toUpperCase(); // chrome M23 doesn't like this
+ line += ' ';
+ line += cand.getAttribute('priority');
+ line += ' ';
+ line += cand.getAttribute('ip');
+ line += ' ';
+ line += cand.getAttribute('port');
+ line += ' ';
+ line += 'typ';
+ line += ' ' + cand.getAttribute('type');
+ line += ' ';
+ switch (cand.getAttribute('type')) {
+ case 'srflx':
+ case 'prflx':
+ case 'relay':
+ if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
+ line += 'raddr';
+ line += ' ';
+ line += cand.getAttribute('rel-addr');
+ line += ' ';
+ line += 'rport';
+ line += ' ';
+ line += cand.getAttribute('rel-port');
+ line += ' ';
+ }
+ break;
+ }
+ line += 'generation';
+ line += ' ';
+ line += cand.getAttribute('generation') || '0';
+ return line + '\r\n';
+ }
+};
+/* jshint -W117 */
+// Jingle stuff
+function JingleSession(me, sid, connection) {
+ this.me = me;
+ this.sid = sid;
+ this.connection = connection;
+ this.initiator = null;
+ this.responder = null;
+ this.isInitiator = null;
+ this.peerjid = null;
+ this.state = null;
+ this.peerconnection = null;
+ this.remoteStream = null;
+ this.localSDP = null;
+ this.remoteSDP = null;
+ this.localStreams = [];
+ this.relayedStreams = [];
+ this.remoteStreams = [];
+ this.startTime = null;
+ this.stopTime = null;
+ this.media_constraints = null;
+ this.pc_constraints = null;
+ this.ice_config = {},
+ this.drip_container = [];
+
+ this.usetrickle = true;
+ this.usepranswer = false; // early transport warmup -- mind you, this might fail. depends on webrtc issue 1718
+ this.usedrip = false; // dripping is sending trickle candidates not one-by-one
+
+ this.hadstuncandidate = false;
+ this.hadturncandidate = false;
+ this.lasticecandidate = false;
+
+ this.statsinterval = null;
+
+ this.reason = null;
+
+ this.addssrc = [];
+ this.removessrc = [];
+
+ this.wait = true;
+}
+
+JingleSession.prototype.initiate = function (peerjid, isInitiator) {
+ var obj = this;
+ if (this.state !== null) {
+ console.error('attempt to initiate on session ' + this.sid +
+ 'in state ' + this.state);
+ return;
+ }
+ this.isInitiator = isInitiator;
+ this.state = 'pending';
+ this.initiator = isInitiator ? this.me : peerjid;
+ this.responder = !isInitiator ? this.me : peerjid;
+ this.peerjid = peerjid;
+ console.log('create PeerConnection ' + JSON.stringify(this.ice_config));
+ try {
+ this.peerconnection = new RTCPeerconnection(this.ice_config,
+ this.pc_constraints);
+ console.log('Created RTCPeerConnnection');
+ } catch (e) {
+ console.error('Failed to create PeerConnection, exception: ',
+ e.message);
+ console.error(e);
+ return;
+ }
+ this.hadstuncandidate = false;
+ this.hadturncandidate = false;
+ this.lasticecandidate = false;
+ this.peerconnection.onicecandidate = function (event) {
+ obj.sendIceCandidate(event.candidate);
+ };
+ this.peerconnection.onaddstream = function (event) {
+ obj.remoteStream = event.stream;
+ obj.remoteStreams.push(event.stream);
+ $(document).trigger('remotestreamadded.jingle', [event, obj.sid]);
+ };
+ this.peerconnection.onremovestream = function (event) {
+ obj.remoteStream = null;
+ // FIXME: remove from this.remoteStreams
+ $(document).trigger('remotestreamremoved.jingle', [event, obj.sid]);
+ };
+ this.peerconnection.onsignalingstatechange = function (event) {
+ if (!(obj && obj.peerconnection)) return;
+ console.log('signallingstate ', obj.peerconnection.signalingState, event);
+ };
+ this.peerconnection.oniceconnectionstatechange = function (event) {
+ if (!(obj && obj.peerconnection)) return;
+ console.log('iceconnectionstatechange', obj.peerconnection.iceConnectionState, event);
+ switch (obj.peerconnection.iceConnectionState) {
+ case 'connected':
+ this.startTime = new Date();
+ break;
+ case 'disconnected':
+ this.stopTime = new Date();
+ break;
+ }
+ $(document).trigger('iceconnectionstatechange.jingle', [obj.sid, obj]);
+ };
+ // add any local and relayed stream
+ this.localStreams.forEach(function(stream) {
+ obj.peerconnection.addStream(stream);
+ });
+ this.relayedStreams.forEach(function(stream) {
+ obj.peerconnection.addStream(stream);
+ });
+};
+
+JingleSession.prototype.accept = function () {
+ var ob = this;
+ this.state = 'active';
+
+ var pranswer = this.peerconnection.localDescription;
+ if (!pranswer || pranswer.type != 'pranswer') {
+ return;
+ }
+ console.log('going from pranswer to answer');
+ if (this.usetrickle) {
+ // remove candidates already sent from session-accept
+ var lines = SDPUtil.find_lines(pranswer.sdp, 'a=candidate:');
+ for (var i = 0; i < lines.length; i++) {
+ pranswer.sdp = pranswer.sdp.replace(lines[i] + '\r\n', '');
+ }
+ }
+ while (SDPUtil.find_line(pranswer.sdp, 'a=inactive')) {
+ // FIXME: change any inactive to sendrecv or whatever they were originally
+ pranswer.sdp = pranswer.sdp.replace('a=inactive', 'a=sendrecv');
+ }
+ var prsdp = new SDP(pranswer.sdp);
+ var accept = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-accept',
+ initiator: this.initiator,
+ responder: this.responder,
+ sid: this.sid });
+ prsdp.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
+ this.connection.sendIQ(accept,
+ function () {
+ var ack = {};
+ ack.source = 'answer';
+ $(document).trigger('ack.jingle', [ob.sid, ack]);
+ },
+ function (stanza) {
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ error.source = 'answer';
+ $(document).trigger('error.jingle', [ob.sid, error]);
+ },
+ 10000);
+
+ var sdp = this.peerconnection.localDescription.sdp;
+ while (SDPUtil.find_line(sdp, 'a=inactive')) {
+ // FIXME: change any inactive to sendrecv or whatever they were originally
+ sdp = sdp.replace('a=inactive', 'a=sendrecv');
+ }
+ this.peerconnection.setLocalDescription(new RTCSessionDescription({type: 'answer', sdp: sdp}),
+ function () {
+ console.log('setLocalDescription success');
+ },
+ function (e) {
+ console.error('setLocalDescription failed', e);
+ }
+ );
+};
+
+JingleSession.prototype.terminate = function (reason) {
+ this.state = 'ended';
+ this.reason = reason;
+ this.peerconnection.close();
+ if (this.statsinterval !== null) {
+ window.clearInterval(this.statsinterval);
+ this.statsinterval = null;
+ }
+};
+
+JingleSession.prototype.active = function () {
+ return this.state == 'active';
+};
+
+JingleSession.prototype.sendIceCandidate = function (candidate) {
+ var ob = this;
+ if (candidate && !this.lasticecandidate) {
+ var ice = SDPUtil.iceparams(this.localSDP.media[candidate.sdpMLineIndex], this.localSDP.session),
+ jcand = SDPUtil.candidateToJingle(candidate.candidate);
+ if (!(ice && jcand)) {
+ console.error('failed to get ice && jcand');
+ return;
+ }
+ ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
+
+ if (jcand.type === 'srflx') {
+ this.hadstuncandidate = true;
+ } else if (jcand.type === 'relay') {
+ this.hadturncandidate = true;
+ }
+ console.log(event.candidate, jcand);
+
+ if (this.usetrickle) {
+ if (this.usedrip) {
+ if (this.drip_container.length === 0) {
+ // start 10ms callout
+ window.setTimeout(function () {
+ if (ob.drip_container.length === 0) return;
+ var allcands = ob.drip_container;
+ ob.drip_container = [];
+ var cand = $iq({to: ob.peerjid, type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'transport-info',
+ initiator: ob.initiator,
+ sid: ob.sid});
+ for (var mid = 0; mid < ob.localSDP.media.length; mid++) {
+ var cands = allcands.filter(function (el) { return el.sdpMLineIndex == mid; });
+ if (cands.length > 0) {
+ var ice = SDPUtil.iceparams(ob.localSDP.media[mid], ob.localSDP.session);
+ ice.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
+ cand.c('content', {creator: ob.initiator == ob.me ? 'initiator' : 'responder',
+ name: cands[0].sdpMid
+ }).c('transport', ice);
+ for (var i = 0; i < cands.length; i++) {
+ cand.c('candidate', SDPUtil.candidateToJingle(cands[i].candidate)).up();
+ }
+ // add fingerprint
+ if (SDPUtil.find_line(ob.localSDP.media[mid], 'a=fingerprint:', ob.localSDP.session)) {
+ var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(ob.localSDP.media[mid], 'a=fingerprint:', ob.localSDP.session));
+ tmp.required = true;
+ cand.c('fingerprint').t(tmp.fingerprint);
+ delete tmp.fingerprint;
+ cand.attrs(tmp);
+ cand.up();
+ }
+ cand.up(); // transport
+ cand.up(); // content
+ }
+ }
+ // might merge last-candidate notification into this, but it is called alot later. See webrtc issue #2340
+ //console.log('was this the last candidate', ob.lasticecandidate);
+ ob.connection.sendIQ(cand,
+ function () {
+ var ack = {};
+ ack.source = 'transportinfo';
+ $(document).trigger('ack.jingle', [ob.sid, ack]);
+ },
+ function (stanza) {
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ error.source = 'transportinfo';
+ $(document).trigger('error.jingle', [ob.sid, error]);
+ },
+ 10000);
+ }, 10);
+ }
+ this.drip_container.push(event.candidate);
+ return;
+ }
+ // map to transport-info
+ var cand = $iq({to: this.peerjid, type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'transport-info',
+ initiator: this.initiator,
+ sid: this.sid})
+ .c('content', {creator: this.initiator == this.me ? 'initiator' : 'responder',
+ name: candidate.sdpMid
+ })
+ .c('transport', ice)
+ .c('candidate', jcand);
+ cand.up();
+ // add fingerprint
+ if (SDPUtil.find_line(this.localSDP.media[candidate.sdpMLineIndex], 'a=fingerprint:', this.localSDP.session)) {
+ var tmp = SDPUtil.parse_fingerprint(SDPUtil.find_line(this.localSDP.media[candidate.sdpMLineIndex], 'a=fingerprint:', this.localSDP.session));
+ tmp.required = true;
+ cand.c('fingerprint').t(tmp.fingerprint);
+ delete tmp.fingerprint;
+ cand.attrs(tmp);
+ cand.up();
+ }
+ this.connection.sendIQ(cand,
+ function () {
+ var ack = {};
+ ack.source = 'transportinfo';
+ $(document).trigger('ack.jingle', [ob.sid, ack]);
+ },
+ function (stanza) {
+ console.error('transport info error');
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ error.source = 'transportinfo';
+ $(document).trigger('error.jingle', [ob.sid, error]);
+ },
+ 10000);
+ }
+ } else {
+ console.log('sendIceCandidate: last candidate.');
+ if (!this.usetrickle) {
+ console.log('should send full offer now...');
+ var init = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: this.peerconnection.localDescription.type == 'offer' ? 'session-initiate' : 'session-accept',
+ initiator: this.initiator,
+ sid: this.sid});
+ this.localSDP = new SDP(this.peerconnection.localDescription.sdp);
+ this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
+ this.connection.sendIQ(init,
+ function () {
+ console.log('session initiate ack');
+ var ack = {};
+ ack.source = 'offer';
+ $(document).trigger('ack.jingle', [ob.sid, ack]);
+ },
+ function (stanza) {
+ ob.state = 'error';
+ ob.peerconnection.close();
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ error.source = 'offer';
+ $(document).trigger('error.jingle', [ob.sid, error]);
+ },
+ 10000);
+ }
+ this.lasticecandidate = true;
+ console.log('Have we encountered any srflx candidates? ' + this.hadstuncandidate);
+ console.log('Have we encountered any relay candidates? ' + this.hadturncandidate);
+
+ if (!(this.hadstuncandidate || this.hadturncandidate) && this.peerconnection.signalingState != 'closed') {
+ $(document).trigger('nostuncandidates.jingle', [this.sid]);
+ }
+ }
+};
+
+JingleSession.prototype.sendOffer = function () {
+ console.log('sendOffer...');
+ var ob = this;
+ this.peerconnection.createOffer(function (sdp) {
+ ob.createdOffer(sdp);
+ },
+ function (e) {
+ console.error('createOffer failed', e);
+ },
+ this.media_constraints
+ );
+};
+
+JingleSession.prototype.createdOffer = function (sdp) {
+ console.log('createdOffer', sdp);
+ var ob = this;
+ this.localSDP = new SDP(sdp.sdp);
+ //this.localSDP.mangle();
+ if (this.usetrickle) {
+ var init = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-initiate',
+ initiator: this.initiator,
+ sid: this.sid});
+ this.localSDP.toJingle(init, this.initiator == this.me ? 'initiator' : 'responder');
+ this.connection.sendIQ(init,
+ function () {
+ var ack = {};
+ ack.source = 'offer';
+ $(document).trigger('ack.jingle', [ob.sid, ack]);
+ },
+ function (stanza) {
+ ob.state = 'error';
+ ob.peerconnection.close();
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ error.source = 'offer';
+ $(document).trigger('error.jingle', [ob.sid, error]);
+ },
+ 10000);
+ }
+ sdp.sdp = this.localSDP.raw;
+ this.peerconnection.setLocalDescription(sdp, function () {
+ console.log('setLocalDescription success');
+ },
+ function (e) {
+ console.error('setLocalDescription failed', e);
+ }
+ );
+ var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
+ for (var i = 0; i < cands.length; i++) {
+ var cand = SDPUtil.parse_icecandidate(cands[i]);
+ if (cand.type == 'srflx') {
+ this.hadstuncandidate = true;
+ } else if (cand.type == 'relay') {
+ this.hadturncandidate = true;
+ }
+ }
+};
+
+JingleSession.prototype.setRemoteDescription = function (elem, desctype) {
+ console.log('setting remote description... ', desctype);
+ this.remoteSDP = new SDP('');
+ this.remoteSDP.fromJingle(elem);
+ if (this.peerconnection.remoteDescription !== null) {
+ console.log('setRemoteDescription when remote description is not null, should be pranswer', this.peerconnection.remoteDescription);
+ if (this.peerconnection.remoteDescription.type == 'pranswer') {
+ var pranswer = new SDP(this.peerconnection.remoteDescription.sdp);
+ for (var i = 0; i < pranswer.media.length; i++) {
+ // make sure we have ice ufrag and pwd
+ if (!SDPUtil.find_line(this.remoteSDP.media[i], 'a=ice-ufrag:', this.remoteSDP.session)) {
+ if (SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session)) {
+ this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-ufrag:', pranswer.session) + '\r\n';
+ } else {
+ console.warn('no ice ufrag?');
+ }
+ if (SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session)) {
+ this.remoteSDP.media[i] += SDPUtil.find_line(pranswer.media[i], 'a=ice-pwd:', pranswer.session) + '\r\n';
+ } else {
+ console.warn('no ice pwd?');
+ }
+ }
+ // copy over candidates
+ var lines = SDPUtil.find_lines(pranswer.media[i], 'a=candidate:');
+ for (var j = 0; j < lines.length; j++) {
+ this.remoteSDP.media[i] += lines[j] + '\r\n';
+ }
+ }
+ this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
+ }
+ }
+ var remotedesc = new RTCSessionDescription({type: desctype, sdp: this.remoteSDP.raw});
+
+ this.peerconnection.setRemoteDescription(remotedesc,
+ function () {
+ console.log('setRemoteDescription success');
+ },
+ function (e) {
+ console.error('setRemoteDescription error', e);
+ }
+ );
+};
+
+JingleSession.prototype.addIceCandidate = function (elem) {
+ var obj = this;
+ if (this.peerconnection.signalingState == 'closed') {
+ return;
+ }
+ if (!this.peerconnection.remoteDescription && this.peerconnection.signalingState == 'have-local-offer') {
+ console.log('trickle ice candidate arriving before session accept...');
+ // create a PRANSWER for setRemoteDescription
+ if (!this.remoteSDP) {
+ var cobbled = 'v=0\r\n' +
+ 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
+ 's=-\r\n' +
+ 't=0 0\r\n';
+ // first, take some things from the local description
+ for (var i = 0; i < this.localSDP.media.length; i++) {
+ cobbled += SDPUtil.find_line(this.localSDP.media[i], 'm=') + '\r\n';
+ cobbled += SDPUtil.find_lines(this.localSDP.media[i], 'a=rtpmap:').join('\r\n') + '\r\n';
+ if (SDPUtil.find_line(this.localSDP.media[i], 'a=mid:')) {
+ cobbled += SDPUtil.find_line(this.localSDP.media[i], 'a=mid:') + '\r\n';
+ }
+ cobbled += 'a=inactive\r\n';
+ }
+ this.remoteSDP = new SDP(cobbled);
+ }
+ // then add things like ice and dtls from remote candidate
+ elem.each(function () {
+ for (var i = 0; i < obj.remoteSDP.media.length; i++) {
+ if (SDPUtil.find_line(obj.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
+ obj.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
+ if (!SDPUtil.find_line(obj.remoteSDP.media[i], 'a=ice-ufrag:')) {
+ var tmp = $(this).find('transport');
+ obj.remoteSDP.media[i] += 'a=ice-ufrag:' + tmp.attr('ufrag') + '\r\n';
+ obj.remoteSDP.media[i] += 'a=ice-pwd:' + tmp.attr('pwd') + '\r\n';
+ tmp = $(this).find('transport>fingerprint');
+ if (tmp.length) {
+ obj.remoteSDP.media[i] += 'a=fingerprint:' + tmp.attr('hash') + ' ' + tmp.text() + '\r\n';
+ } else {
+ console.log('no dtls fingerprint (webrtc issue #1718?)');
+ obj.remoteSDP.media[i] += 'a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:BAADBAADBAADBAADBAADBAADBAADBAADBAADBAAD\r\n';
+ }
+ break;
+ }
+ }
+ }
+ });
+ this.remoteSDP.raw = this.remoteSDP.session + this.remoteSDP.media.join('');
+
+ // we need a complete SDP with ice-ufrag/ice-pwd in all parts
+ // this makes the assumption that the PRANSWER is constructed such that the ice-ufrag is in all mediaparts
+ // but it could be in the session part as well. since the code above constructs this sdp this can't happen however
+ var iscomplete = this.remoteSDP.media.filter(function (mediapart) {
+ return SDPUtil.find_line(mediapart, 'a=ice-ufrag:');
+ }).length == this.remoteSDP.media.length;
+
+ if (iscomplete) {
+ console.log('setting pranswer');
+ try {
+ this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'pranswer', sdp: this.remoteSDP.raw }));
+ } catch (e) {
+ console.error('setting pranswer failed', e);
+ }
+ } else {
+ console.log('not yet setting pranswer');
+ }
+ }
+ // operate on each content element
+ elem.each(function () {
+ // would love to deactivate this, but firefox still requires it
+ var idx = -1;
+ var i;
+ for (i = 0; i < obj.remoteSDP.media.length; i++) {
+ if (SDPUtil.find_line(obj.remoteSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
+ obj.remoteSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
+ idx = i;
+ break;
+ }
+ }
+ if (idx == -1) { // fall back to localdescription
+ for (i = 0; i < obj.localSDP.media.length; i++) {
+ if (SDPUtil.find_line(obj.localSDP.media[i], 'a=mid:' + $(this).attr('name')) ||
+ obj.localSDP.media[i].indexOf('m=' + $(this).attr('name')) === 0) {
+ idx = i;
+ break;
+ }
+ }
+ }
+ var name = $(this).attr('name');
+ // TODO: check ice-pwd and ice-ufrag?
+ $(this).find('transport>candidate').each(function () {
+ var line, candidate;
+ line = SDPUtil.candidateFromJingle(this);
+ candidate = new RTCIceCandidate({sdpMLineIndex: idx,
+ sdpMid: name,
+ candidate: line});
+ try {
+ obj.peerconnection.addIceCandidate(candidate);
+ } catch (e) {
+ console.error('addIceCandidate failed', e.toString(), line);
+ }
+ });
+ });
+};
+
+JingleSession.prototype.sendAnswer = function (provisional) {
+ console.log('createAnswer', provisional);
+ var ob = this;
+ this.peerconnection.createAnswer(
+ function (sdp) {
+ ob.createdAnswer(sdp, provisional);
+ },
+ function (e) {
+ console.error('createAnswer failed', e);
+ },
+ this.media_constraints
+ );
+};
+
+JingleSession.prototype.createdAnswer = function (sdp, provisional) {
+ console.log('createAnswer callback');
+ console.log(sdp);
+ var ob = this;
+ this.localSDP = new SDP(sdp.sdp);
+ //this.localSDP.mangle();
+ this.usepranswer = provisional === true;
+ if (this.usetrickle) {
+ if (!this.usepranswer) {
+ var accept = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-accept',
+ initiator: this.initiator,
+ responder: this.responder,
+ sid: this.sid });
+ this.localSDP.toJingle(accept, this.initiator == this.me ? 'initiator' : 'responder');
+ this.connection.sendIQ(accept,
+ function () {
+ var ack = {};
+ ack.source = 'answer';
+ $(document).trigger('ack.jingle', [ob.sid, ack]);
+ },
+ function (stanza) {
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ error.source = 'answer';
+ $(document).trigger('error.jingle', [ob.sid, error]);
+ },
+ 10000);
+ } else {
+ sdp.type = 'pranswer';
+ for (var i = 0; i < this.localSDP.media.length; i++) {
+ this.localSDP.media[i] = this.localSDP.media[i].replace('a=sendrecv\r\n', 'a=inactive\r\n');
+ }
+ this.localSDP.raw = this.localSDP.session + '\r\n' + this.localSDP.media.join('');
+ }
+ }
+ sdp.sdp = this.localSDP.raw;
+ this.peerconnection.setLocalDescription(sdp,
+ function () {
+ console.log('setLocalDescription success');
+ },
+ function (e) {
+ console.error('setLocalDescription failed', e);
+ }
+ );
+ var cands = SDPUtil.find_lines(this.localSDP.raw, 'a=candidate:');
+ for (var j = 0; j < cands.length; j++) {
+ var cand = SDPUtil.parse_icecandidate(cands[j]);
+ if (cand.type == 'srflx') {
+ this.hadstuncandidate = true;
+ } else if (cand.type == 'relay') {
+ this.hadturncandidate = true;
+ }
+ }
+};
+
+JingleSession.prototype.sendTerminate = function (reason, text) {
+ var obj = this,
+ term = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-terminate',
+ initiator: this.initiator,
+ sid: this.sid})
+ .c('reason')
+ .c(reason || 'success');
+
+ if (text) {
+ term.up().c('text').t(text);
+ }
+
+ this.connection.sendIQ(term,
+ function () {
+ obj.peerconnection.close();
+ obj.peerconnection = null;
+ obj.terminate();
+ var ack = {};
+ ack.source = 'terminate';
+ $(document).trigger('ack.jingle', [obj.sid, ack]);
+ },
+ function (stanza) {
+ var error = ($(stanza).find('error').length) ? {
+ code: $(stanza).find('error').attr('code'),
+ reason: $(stanza).find('error :first')[0].tagName,
+ }:{};
+ $(document).trigger('ack.jingle', [obj.sid, error]);
+ },
+ 10000);
+ if (this.statsinterval !== null) {
+ window.clearInterval(this.statsinterval);
+ this.statsinterval = null;
+ }
+};
+
+
+JingleSession.prototype.addSource = function (elem) {
+ console.log('addssrc', new Date().getTime());
+ console.log('ice', this.peerconnection.iceConnectionState);
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
+
+ var ob = this;
+ $(elem).each(function (idx, content) {
+ var name = $(content).attr('name');
+ var lines = '';
+ tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
+ tmp.each(function () {
+ var ssrc = $(this).attr('ssrc');
+ $(this).find('>parameter').each(function () {
+ lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
+ if ($(this).attr('value') && $(this).attr('value').length)
+ lines += ':' + $(this).attr('value');
+ lines += '\r\n';
+ });
+ });
+ console.log(name, lines);
+ sdp.media.forEach(function(media, idx) {
+ if (!SDPUtil.find_line(media, 'a=mid:' + name))
+ return;
+ sdp.media[idx] += lines;
+ if (!ob.addssrc[idx]) ob.addssrc[idx] = '';
+ ob.addssrc[idx] += lines;
+ });
+ sdp.raw = sdp.session + sdp.media.join('');
+ });
+ this.modifySources();
+};
+
+JingleSession.prototype.removeSource = function (elem) {
+ console.log('removessrc', new Date().getTime());
+ console.log('ice', this.peerconnection.iceConnectionState);
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
+
+ var ob = this;
+ $(elem).each(function (idx, content) {
+ var name = $(content).attr('name');
+ var lines = '';
+ tmp = $(content).find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
+ tmp.each(function () {
+ var ssrc = $(this).attr('ssrc');
+ $(this).find('>parameter').each(function () {
+ lines += 'a=ssrc:' + ssrc + ' ' + $(this).attr('name');
+ if ($(this).attr('value') && $(this).attr('value').length)
+ lines += ':' + $(this).attr('value');
+ lines += '\r\n';
+ });
+ });
+ console.log(name, lines);
+ sdp.media.forEach(function(media, idx) {
+ if (!SDPUtil.find_line(media, 'a=mid:' + name))
+ return;
+ sdp.media[idx] += lines;
+ if (!ob.addssrc[idx]) ob.removessrc[idx] = '';
+ ob.removessrc[idx] += lines;
+ });
+ sdp.raw = sdp.session + sdp.media.join('');
+ });
+ console.log(this.removessrc);
+ this.modifySources();
+};
+
+
+
+JingleSession.prototype.modifySources = function() {
+ var ob = this;
+ if (!(this.addssrc.length || this.removessrc.length)) return;
+ if (this.peerconnection.signalingState == 'closed') return;
+ if (!(this.peerconnection.signalingState == 'stable' && this.peerconnection.iceConnectionState == 'connected')) {
+ console.warn('modifySources not yet', this.peerconnection.signalingState, this.peerconnection.iceConnectionState);
+ this.wait = true;
+ window.setTimeout(function() { ob.modifySources(); }, 250);
+ return;
+ }
+ if (this.wait) {
+ window.setTimeout(function() { ob.modifySources(); }, 2500);
+ this.wait = false;
+ return;
+ }
+
+ console.log('ice', this.peerconnection.iceConnectionState);
+ var sdp = new SDP(this.peerconnection.remoteDescription.sdp);
+
+ // add sources
+ this.addssrc.forEach(function(lines, idx) {
+ sdp.media[idx] += lines;
+ });
+ this.addssrc = [];
+
+ // remove sources
+ this.removessrc.forEach(function(lines, idx) {
+ lines = lines.split('\r\n');
+ lines.pop(); // remove empty last element;
+ lines.forEach(function(line) {
+ sdp.media[idx] = sdp.media[idx].replace(line + '\r\n', '');
+ });
+ });
+ this.removessrc = [];
+
+ sdp.raw = sdp.session + sdp.media.join('');
+ this.peerconnection.setRemoteDescription(new RTCSessionDescription({type: 'offer', sdp: sdp.raw}),
+ function() {
+ console.log('modify ok');
+ ob.peerconnection.createAnswer(
+ function(modifiedAnswer) {
+ console.log('modified answer...');
+ ob.peerconnection.setLocalDescription(modifiedAnswer,
+ function() {
+ console.log('modified setLocalDescription ok');
+ },
+ function(error) {
+ console.log('modified setLocalDescription failed');
+ }
+ );
+ },
+ function(error) {
+ console.log('modified answer failed');
+ }
+ );
+ },
+ function(error) {
+ console.log('modify failed');
+ }
+ );
+};
+
+JingleSession.prototype.sendMute = function (muted, content) {
+ var info = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-info',
+ initiator: this.initiator,
+ sid: this.sid });
+ info.c(muted ? 'mute' : 'unmute', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
+ info.attrs({'creator': this.me == this.initiator ? 'creator' : 'responder'});
+ if (content) {
+ info.attrs({'name': content});
+ }
+ this.connection.send(info);
+};
+
+JingleSession.prototype.sendRinging = function () {
+ var info = $iq({to: this.peerjid,
+ type: 'set'})
+ .c('jingle', {xmlns: 'urn:xmpp:jingle:1',
+ action: 'session-info',
+ initiator: this.initiator,
+ sid: this.sid });
+ info.c('ringing', {xmlns: 'urn:xmpp:jingle:apps:rtp:info:1'});
+ this.connection.send(info);
+};
+
+JingleSession.prototype.getStats = function (interval) {
+ var ob = this;
+ var recv = {audio: 0, video: 0};
+ var lost = {audio: 0, video: 0};
+ var lastrecv = {audio: 0, video: 0};
+ var lastlost = {audio: 0, video: 0};
+ var loss = {audio: 0, video: 0};
+ var delta = {audio: 0, video: 0};
+ this.statsinterval = window.setInterval(function () {
+ if (ob && ob.peerconnection && ob.peerconnection.getStats) {
+ ob.peerconnection.getStats(function (stats) {
+ var results = stats.result();
+ // TODO: there are so much statistics you can get from this..
+ for (var i = 0; i < results.length; ++i) {
+ if (results[i].type == 'ssrc') {
+ var packetsrecv = results[i].stat('packetsReceived');
+ var packetslost = results[i].stat('packetsLost');
+ if (packetsrecv && packetslost) {
+ packetsrecv = parseInt(packetsrecv, 10);
+ packetslost = parseInt(packetslost, 10);
+
+ if (results[i].stat('googFrameRateReceived')) {
+ lastlost.video = lost.video;
+ lastrecv.video = recv.video;
+ recv.video = packetsrecv;
+ lost.video = packetslost;
+ } else {
+ lastlost.audio = lost.audio;
+ lastrecv.audio = recv.audio;
+ recv.audio = packetsrecv;
+ lost.audio = packetslost;
+ }
+ }
+ }
+ }
+ delta.audio = recv.audio - lastrecv.audio;
+ delta.video = recv.video - lastrecv.video;
+ loss.audio = (delta.audio > 0) ? Math.ceil(100 * (lost.audio - lastlost.audio) / delta.audio) : 0;
+ loss.video = (delta.video > 0) ? Math.ceil(100 * (lost.video - lastlost.video) / delta.video) : 0;
+ $(document).trigger('packetloss.jingle', [ob.sid, loss]);
+ });
+ }
+ }, interval || 3000);
+ return this.statsinterval;
+};
+
diff --git a/muc.js b/muc.js
new file mode 100644
index 0000000000..4e26763637
--- /dev/null
+++ b/muc.js
@@ -0,0 +1,139 @@
+/* jshint -W117 */
+/* a simple MUC connection plugin
+ * can only handle a single MUC room
+ */
+Strophe.addConnectionPlugin('emuc', {
+ connection: null,
+ roomjid: null,
+ myroomjid: null,
+ members: {},
+ isOwner: false,
+ init: function (conn) {
+ this.connection = conn;
+ },
+ doJoin: function (jid, password) {
+ this.myroomjid = jid;
+ if (!this.roomjid) {
+ this.roomjid = Strophe.getBareJidFromJid(jid);
+ // add handlers (just once)
+ this.connection.addHandler(this.onPresence.bind(this), null, 'presence', null, null, this.roomjid, {matchBare: true});
+ this.connection.addHandler(this.onPresenceUnavailable.bind(this), null, 'presence', 'unavailable', null, this.roomjid, {matchBare: true});
+ this.connection.addHandler(this.onPresenceError.bind(this), null, 'presence', 'error', null, this.roomjid, {matchBare: true});
+ this.connection.addHandler(this.onMessage.bind(this), null, 'message', null, null, this.roomjid, {matchBare: true});
+ }
+
+ var join = $pres({to: this.myroomjid }).c('x', {xmlns: 'http://jabber.org/protocol/muc'});
+ if (password !== null) {
+ join.c('password').t(password);
+ }
+ this.connection.send(join);
+ },
+ onPresence: function (pres) {
+ var from = pres.getAttribute('from');
+ var type = pres.getAttribute('type');
+ if (type != null) {
+ return true;
+ }
+ if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="201"]').length) {
+ // http://xmpp.org/extensions/xep-0045.html#createroom-instant
+ this.isOwner = true;
+ var create = $iq({type: 'set', to: this.roomjid})
+ .c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'})
+ .c('x', {xmlns: 'jabber:x:data', type: 'submit'});
+ this.connection.send(create); // fire away
+ }
+
+ var member = {};
+ member.show = $(pres).find('>show').text();
+ member.status = $(pres).find('>status').text();
+ var tmp = $(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item');
+ member.affilication = tmp.attr('affiliation');
+ member.role = tmp.attr('role');
+ if (from == this.myroomjid) {
+ $(document).trigger('joined.muc', [from, member]);
+ } else if (this.members[from] === undefined) {
+ // new participant
+ this.members[from] = member;
+ $(document).trigger('entered.muc', [from, member]);
+ } else {
+ console.log('presence change from', from);
+ }
+ return true;
+ },
+ onPresenceUnavailable: function (pres) {
+ var from = pres.getAttribute('from');
+ delete this.members[from];
+ $(document).trigger('left.muc', [from]);
+ return true;
+ },
+ onPresenceError: function (pres) {
+ var from = pres.getAttribute('from');
+ if ($(pres).find('>error[type="auth"]>not-authorized[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
+ $(document).trigger('passwordrequired.muc', [from]);
+
+ // FIXME: remove once moved to passwordrequired which should reuse dojoin
+ var ob = this;
+ window.setTimeout(function () {
+ var given = window.prompt('Password required');
+ if (given != null) {
+ // FIXME: reuse doJoin?
+ ob.connection.send($pres({to: ob.myroomjid }).c('x', {xmlns: 'http://jabber.org/protocol/muc'}).c('password').t(given));
+ } else {
+ // user aborted
+ }
+ }, 50);
+ } else {
+ console.warn('onPresError ', pres);
+ }
+ return true;
+ },
+ sendMessage: function (body, nickname) {
+ var msg = $msg({to: this.roomjid, type: 'groupchat'});
+ msg.c('body', body).up();
+ if (nickname) {
+ msg.c('nick', {xmlns: 'http://jabber.org/protocol/nick'}).t(nickname).up().up();
+ }
+ this.connection.send(msg);
+ },
+ onMessage: function (msg) {
+ var txt = $(msg).find('>body').text();
+ // TODO: <subject/>
+ // FIXME: this is a hack. but jingle on muc makes nickchanges hard
+ var nick = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]').text() || Strophe.getResourceFromJid(msg.getAttribute('from'));
+ if (txt) {
+ console.log('chat', nick, txt);
+
+ updateChatConversation(nick, txt);
+ }
+ return true;
+ },
+ lockRoom: function (key) {
+ //http://xmpp.org/extensions/xep-0045.html#roomconfig
+ var ob = this;
+ this.connection.sendIQ($iq({to: this.roomjid, type: 'get'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'}),
+ function (res) {
+ if ($(res).find('>query>x[xmlns="jabber:x:data"]>field[var="muc#roomconfig_roomsecret"]').length) {
+ var formsubmit = $iq({to: ob.roomjid, type: 'set'}).c('query', {xmlns: 'http://jabber.org/protocol/muc#owner'});
+ formsubmit.c('x', {xmlns: 'jabber:x:data', type: 'submit'});
+ formsubmit.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up();
+ formsubmit.c('field', {'var': 'muc#roomconfig_roomsecret'}).c('value').t(key).up().up();
+ // FIXME: is muc#roomconfig_passwordprotectedroom required?
+ this.connection.sendIQ(formsubmit,
+ function (res) {
+ console.log('set room password');
+ },
+ function (err) {
+ console.warn('setting password failed', err);
+ }
+ );
+ } else {
+ console.warn('room passwords not supported');
+ }
+ },
+ function (err) {
+ console.warn('setting password failed', err);
+ }
+ );
+ }
+});
+
diff --git a/webrtcrequired.html b/webrtcrequired.html
new file mode 100644
index 0000000000..2b7faf7fb6
--- /dev/null
+++ b/webrtcrequired.html
@@ -0,0 +1 @@
+Sorry, webrtc is required for this and your browser does not seem to support it.