/* jshint -W117 */ /* application specific logic */ var connection = null; var focus = null; var RTC; var RTCPeerConnection = null; var nickname = null; var sharedKey = ''; var roomUrl = null; var ssrc2jid = {}; /* window.onbeforeunload = closePageWarning; */ 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 = TraceablePeerConnection; connection = new Strophe.Connection(document.getElementById('boshURL').value || config.bosh || '/http-bind'); if (connection.disco) { // for chrome, add multistream cap } 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'); if (RTC.browser == 'firefox') { getUserMediaWithConstraints(['audio']); } else { 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; // determinde the room node from the url // TODO: just the roomnode or the whole bare jid? if (config.getroomnode && typeof config.getroomnode === 'function') { // custom function might be responsible for doing the pushstate roomnode = config.getroomnode(path); } else { /* fall back to default strategy * this is making assumptions about how the URL->room mapping happens. * It currently assumes deployment at root, with a rewrite like the * following one (for nginx): location ~ ^/([a-zA-Z0-9]+)$ { rewrite ^/(.*)$ / break; } */ 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).substr(0,8); } 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; updateLargeVideo(document.getElementById('localVideo').src, true); $('#localVideo').click(function () { updateLargeVideo($(this).attr('src'), true); }); 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]; // look up an associated JID for a stream id if (data.stream.id.indexOf('mixedmslabel') == -1) { var ssrclines = SDPUtil.find_lines(sess.peerconnection.remoteDescription.sdp, 'a=ssrc'); ssrclines = ssrclines.filter(function (line) { return line.indexOf('mslabel:' + data.stream.label) != -1; }); if (ssrclines.length) { thessrc = ssrclines[0].substring(7).split(' ')[0]; // ok to overwrite the one from focus? might save work in colibri.js console.log('associated jid', ssrc2jid[thessrc], data.peerjid); if (ssrc2jid[thessrc]) { data.peerjid = ssrc2jid[thessrc]; } } } var container; var remotes = document.getElementById('remoteVideos'); if (data.peerjid) { container = document.getElementById('participant_' + Strophe.getResourceFromJid(data.peerjid)); if (!container) { console.warn('no container for', data.peerjid); // create for now... // FIXME: should be removed container = document.createElement('span'); container.id = 'participant_' + Strophe.getResourceFromJid(data.peerjid); container.className = 'videocontainer'; remotes.appendChild(container); } else { //console.log('found container for', data.peerjid); } } else { if (data.stream.id != 'mixedmslabel') { console.warn('can not associate stream', data.stream.id, 'with a participant'); } // FIXME: for the mixed ms we dont need a video -- currently container = document.createElement('span'); container.className = 'videocontainer'; remotes.appendChild(container); } var vid = document.createElement('video'); var id = 'remoteVideo_' + sid + '_' + data.stream.id; vid.id = id; vid.autoplay = true; vid.oncontextmenu = function () { return false; }; container.appendChild(vid); // TODO: make mixedstream display:none via css? if (id.indexOf('mixedmslabel') != -1) { container.id = 'mixedstream'; $(container).hide(); } 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'); 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>span[id!="mixedstream"]:visible:last>video').get(0); // mute if localvideo document.getElementById('largeVideo').volume = pick.volume; document.getElementById('largeVideo').src = pick.src; } $('#' + id).parent().remove(); resizeThumbnails(); }; sel.click( function () { updateLargeVideo($(this).attr('src'), false); } ); }); $(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) { 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('setLocalDescription.jingle', function (event, sid) { // put our ssrcs into presence so other clients can identify our stream var sess = connection.jingle.sessions[sid]; var newssrcs = {}; var localSDP = new SDP(sess.peerconnection.localDescription.sdp); localSDP.media.forEach(function (media) { var type = SDPUtil.parse_mline(media.split('\r\n')[0]).media; var ssrc = SDPUtil.find_line(media, 'a=ssrc:').substring(7).split(' ')[0]; // assumes a single local ssrc newssrcs[type] = ssrc; }); console.log('new ssrcs', newssrcs); // just blast off presence for everything -- TODO: optimize var pres = $pres({to: connection.emuc.myroomjid }); pres.c('x', {xmlns: 'http://jabber.org/protocol/muc'}).up(); pres.c('media', {xmlns: 'http://estos.de/ns/mjs'}); Object.keys(newssrcs).forEach(function (mtype) { pres.c('source', {type: mtype, ssrc: newssrcs[mtype]}).up(); }); pres.up(); connection.send(pres); }); $(document).bind('joined.muc', function (event, jid, info) { updateRoomUrl(window.location.href); document.getElementById('localNick').appendChild( document.createTextNode(Strophe.getResourceFromJid(jid) + ' (you)') ); // Once we've joined the muc show the toolbar showToolbar(); if (Object.keys(connection.emuc.members).length < 1) { focus = new ColibriFocus(connection, config.hosts.bridge); } }); $(document).bind('entered.muc', function (event, jid, info, pres) { console.log('entered', jid, info); console.log(focus); var container = document.createElement('span'); container.id = 'participant_' + Strophe.getResourceFromJid(jid); container.className = 'videocontainer'; var remotes = document.getElementById('remoteVideos'); remotes.appendChild(container); var nickfield = document.createElement('span'); nickfield.appendChild(document.createTextNode(Strophe.getResourceFromJid(jid))); container.appendChild(nickfield); resizeThumbnails(); 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); } } else if (sharedKey) { updateLockButton(); } $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) { //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc')); ssrc2jid[ssrc.getAttribute('ssrc')] = jid; }); }); $(document).bind('left.muc', function (event, jid) { console.log('left', jid); connection.jingle.terminateByJid(jid); var container = document.getElementById('participant_' + Strophe.getResourceFromJid(jid)); if (container) { // hide here, wait for video to close before removing $(container).hide(); resizeThumbnails(); } 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); } } }); $(document).bind('presence.muc', function (event, jid, info, pres) { $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) { //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc')); ssrc2jid[ssrc.getAttribute('ssrc')] = jid; }); }); $(document).bind('passwordrequired.muc', function (event, jid) { console.log('on password required', jid); $.prompt('