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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* jshint -W117 */
  2. /* application specific logic */
  3. var connection = null;
  4. var focus = null;
  5. var RTC;
  6. var RTCPeerConnection = null;
  7. var nickname = null;
  8. var sharedKey = '';
  9. var roomUrl = null;
  10. var ssrc2jid = {};
  11. var localVideoSrc = null;
  12. /* window.onbeforeunload = closePageWarning; */
  13. function init() {
  14. RTC = setupRTC();
  15. if (RTC === null) {
  16. window.location.href = 'webrtcrequired.html';
  17. return;
  18. } else if (RTC.browser != 'chrome') {
  19. window.location.href = 'chromeonly.html';
  20. return;
  21. }
  22. RTCPeerconnection = TraceablePeerConnection;
  23. connection = new Strophe.Connection(document.getElementById('boshURL').value || config.bosh || '/http-bind');
  24. if (connection.disco) {
  25. // for chrome, add multistream cap
  26. }
  27. connection.jingle.pc_constraints = RTC.pc_constraints;
  28. var jid = document.getElementById('jid').value || config.hosts.domain || window.location.hostname;
  29. connection.connect(jid, document.getElementById('password').value, function (status) {
  30. if (status == Strophe.Status.CONNECTED) {
  31. console.log('connected');
  32. if (RTC.browser == 'firefox') {
  33. getUserMediaWithConstraints(['audio']);
  34. } else {
  35. getUserMediaWithConstraints(['audio', 'video'], '360');
  36. }
  37. document.getElementById('connect').disabled = true;
  38. } else {
  39. console.log('status', status);
  40. }
  41. });
  42. }
  43. function doJoin() {
  44. var roomnode = null;
  45. var path = window.location.pathname;
  46. var roomjid;
  47. // determinde the room node from the url
  48. // TODO: just the roomnode or the whole bare jid?
  49. if (config.getroomnode && typeof config.getroomnode === 'function') {
  50. // custom function might be responsible for doing the pushstate
  51. roomnode = config.getroomnode(path);
  52. } else {
  53. /* fall back to default strategy
  54. * this is making assumptions about how the URL->room mapping happens.
  55. * It currently assumes deployment at root, with a rewrite like the
  56. * following one (for nginx):
  57. location ~ ^/([a-zA-Z0-9]+)$ {
  58. rewrite ^/(.*)$ / break;
  59. }
  60. */
  61. if (path.length > 1) {
  62. roomnode = path.substr(1).toLowerCase();
  63. } else {
  64. roomnode = Math.random().toString(36).substr(2, 20);
  65. window.history.pushState('VideoChat', 'Room: ' + roomnode, window.location.pathname + roomnode);
  66. }
  67. }
  68. roomjid = roomnode + '@' + config.hosts.muc;
  69. if (config.useNicks) {
  70. var nick = window.prompt('Your nickname (optional)');
  71. if (nick) {
  72. roomjid += '/' + nick;
  73. } else {
  74. roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
  75. }
  76. } else {
  77. roomjid += '/' + Strophe.getNodeFromJid(connection.jid).substr(0,8);
  78. }
  79. connection.emuc.doJoin(roomjid);
  80. }
  81. $(document).bind('mediaready.jingle', function (event, stream) {
  82. connection.jingle.localStream = stream;
  83. RTC.attachMediaStream($('#localVideo'), stream);
  84. document.getElementById('localVideo').muted = true;
  85. document.getElementById('localVideo').autoplay = true;
  86. document.getElementById('localVideo').volume = 0;
  87. localVideoSrc = document.getElementById('localVideo').src;
  88. updateLargeVideo(localVideoSrc, true, 0);
  89. $('#localVideo').click(function () {
  90. updateLargeVideo($(this).attr('src'), true, 1);
  91. });
  92. doJoin();
  93. });
  94. $(document).bind('mediafailure.jingle', function () {
  95. // FIXME
  96. });
  97. $(document).bind('remotestreamadded.jingle', function (event, data, sid) {
  98. function waitForRemoteVideo(selector, sid) {
  99. var sess = connection.jingle.sessions[sid];
  100. videoTracks = data.stream.getVideoTracks();
  101. if (videoTracks.length === 0 || selector[0].currentTime > 0) {
  102. RTC.attachMediaStream(selector, data.stream); // FIXME: why do i have to do this for FF?
  103. $(document).trigger('callactive.jingle', [selector, sid]);
  104. console.log('waitForremotevideo', sess.peerconnection.iceConnectionState, sess.peerconnection.signalingState);
  105. } else {
  106. setTimeout(function () { waitForRemoteVideo(selector, sid); }, 100);
  107. }
  108. }
  109. var sess = connection.jingle.sessions[sid];
  110. // look up an associated JID for a stream id
  111. if (data.stream.id.indexOf('mixedmslabel') == -1) {
  112. var ssrclines = SDPUtil.find_lines(sess.peerconnection.remoteDescription.sdp, 'a=ssrc');
  113. ssrclines = ssrclines.filter(function (line) {
  114. return line.indexOf('mslabel:' + data.stream.label) != -1;
  115. });
  116. if (ssrclines.length) {
  117. thessrc = ssrclines[0].substring(7).split(' ')[0];
  118. // ok to overwrite the one from focus? might save work in colibri.js
  119. console.log('associated jid', ssrc2jid[thessrc], data.peerjid);
  120. if (ssrc2jid[thessrc]) {
  121. data.peerjid = ssrc2jid[thessrc];
  122. }
  123. }
  124. }
  125. var container;
  126. var remotes = document.getElementById('remoteVideos');
  127. if (data.peerjid) {
  128. container = document.getElementById('participant_' + Strophe.getResourceFromJid(data.peerjid));
  129. if (!container) {
  130. console.warn('no container for', data.peerjid);
  131. // create for now...
  132. // FIXME: should be removed
  133. container = document.createElement('span');
  134. container.id = 'participant_' + Strophe.getResourceFromJid(data.peerjid);
  135. container.className = 'videocontainer';
  136. remotes.appendChild(container);
  137. } else {
  138. //console.log('found container for', data.peerjid);
  139. }
  140. } else {
  141. if (data.stream.id != 'mixedmslabel') {
  142. console.warn('can not associate stream', data.stream.id, 'with a participant');
  143. }
  144. // FIXME: for the mixed ms we dont need a video -- currently
  145. container = document.createElement('span');
  146. container.className = 'videocontainer';
  147. remotes.appendChild(container);
  148. }
  149. var vid = document.createElement('video');
  150. var id = 'remoteVideo_' + sid + '_' + data.stream.id;
  151. vid.id = id;
  152. vid.autoplay = true;
  153. vid.oncontextmenu = function () { return false; };
  154. container.appendChild(vid);
  155. // TODO: make mixedstream display:none via css?
  156. if (id.indexOf('mixedmslabel') != -1) {
  157. container.id = 'mixedstream';
  158. $(container).hide();
  159. }
  160. var sel = $('#' + id);
  161. sel.hide();
  162. RTC.attachMediaStream(sel, data.stream);
  163. waitForRemoteVideo(sel, sid);
  164. data.stream.onended = function () {
  165. console.log('stream ended', this.id);
  166. var src = $('#' + id).attr('src');
  167. if (src === $('#largeVideo').attr('src')) {
  168. // this is currently displayed as large
  169. // pick the last visible video in the row
  170. // if nobody else is left, this picks the local video
  171. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video').get(0);
  172. // mute if localvideo
  173. var isLocalVideo = false;
  174. if (pick.src === localVideoSrc)
  175. isLocalVideo = true;
  176. updateLargeVideo(pick.src, isLocalVideo, pick.volume);
  177. }
  178. $('#' + id).parent().remove();
  179. resizeThumbnails();
  180. };
  181. sel.click(
  182. function () {
  183. updateLargeVideo($(this).attr('src'), false, 1);
  184. }
  185. );
  186. });
  187. $(document).bind('callincoming.jingle', function (event, sid) {
  188. var sess = connection.jingle.sessions[sid];
  189. // TODO: check affiliation and/or role
  190. console.log('emuc data for', sess.peerjid, connection.emuc.members[sess.peerjid]);
  191. sess.sendAnswer();
  192. sess.accept();
  193. });
  194. $(document).bind('callactive.jingle', function (event, videoelem, sid) {
  195. if (videoelem.attr('id').indexOf('mixedmslabel') == -1) {
  196. // ignore mixedmslabela0 and v0
  197. videoelem.show();
  198. resizeThumbnails();
  199. updateLargeVideo(videoelem.attr('src'), false, 1);
  200. showFocusIndicator();
  201. }
  202. });
  203. $(document).bind('callterminated.jingle', function (event, sid, reason) {
  204. // FIXME
  205. });
  206. $(document).bind('setLocalDescription.jingle', function (event, sid) {
  207. // put our ssrcs into presence so other clients can identify our stream
  208. var sess = connection.jingle.sessions[sid];
  209. var newssrcs = {};
  210. var localSDP = new SDP(sess.peerconnection.localDescription.sdp);
  211. localSDP.media.forEach(function (media) {
  212. var type = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  213. var ssrc = SDPUtil.find_line(media, 'a=ssrc:').substring(7).split(' ')[0];
  214. // assumes a single local ssrc
  215. newssrcs[type] = ssrc;
  216. });
  217. console.log('new ssrcs', newssrcs);
  218. // just blast off presence for everything -- TODO: optimize
  219. var pres = $pres({to: connection.emuc.myroomjid });
  220. pres.c('x', {xmlns: 'http://jabber.org/protocol/muc'}).up();
  221. pres.c('media', {xmlns: 'http://estos.de/ns/mjs'});
  222. Object.keys(newssrcs).forEach(function (mtype) {
  223. pres.c('source', {type: mtype, ssrc: newssrcs[mtype]}).up();
  224. });
  225. pres.up();
  226. connection.send(pres);
  227. });
  228. $(document).bind('joined.muc', function (event, jid, info) {
  229. updateRoomUrl(window.location.href);
  230. document.getElementById('localNick').appendChild(
  231. document.createTextNode(Strophe.getResourceFromJid(jid) + ' (you)')
  232. );
  233. // Once we've joined the muc show the toolbar
  234. showToolbar();
  235. if (Object.keys(connection.emuc.members).length < 1) {
  236. focus = new ColibriFocus(connection, config.hosts.bridge);
  237. }
  238. });
  239. $(document).bind('entered.muc', function (event, jid, info, pres) {
  240. console.log('entered', jid, info);
  241. console.log(focus);
  242. var container = document.createElement('span');
  243. container.id = 'participant_' + Strophe.getResourceFromJid(jid);
  244. container.className = 'videocontainer';
  245. var remotes = document.getElementById('remoteVideos');
  246. remotes.appendChild(container);
  247. var nickfield = document.createElement('span');
  248. nickfield.appendChild(document.createTextNode(Strophe.getResourceFromJid(jid)));
  249. container.appendChild(nickfield);
  250. resizeThumbnails();
  251. if (focus !== null) {
  252. // FIXME: this should prepare the video
  253. if (focus.confid === null) {
  254. console.log('make new conference with', jid);
  255. focus.makeConference(Object.keys(connection.emuc.members));
  256. } else {
  257. console.log('invite', jid, 'into conference');
  258. focus.addNewParticipant(jid);
  259. }
  260. }
  261. else if (sharedKey) {
  262. updateLockButton();
  263. }
  264. $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) {
  265. //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc'));
  266. ssrc2jid[ssrc.getAttribute('ssrc')] = jid;
  267. });
  268. });
  269. $(document).bind('left.muc', function (event, jid) {
  270. console.log('left', jid);
  271. connection.jingle.terminateByJid(jid);
  272. var container = document.getElementById('participant_' + Strophe.getResourceFromJid(jid));
  273. if (container) {
  274. // hide here, wait for video to close before removing
  275. $(container).hide();
  276. resizeThumbnails();
  277. }
  278. if (Object.keys(connection.emuc.members).length === 0) {
  279. console.log('everyone left');
  280. if (focus !== null) {
  281. // FIXME: closing the connection is a hack to avoid some
  282. // problemswith reinit
  283. if (focus.peerconnection !== null) {
  284. focus.peerconnection.close();
  285. }
  286. focus = new ColibriFocus(connection, config.hosts.bridge);
  287. }
  288. }
  289. });
  290. $(document).bind('presence.muc', function (event, jid, info, pres) {
  291. $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) {
  292. //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc'));
  293. ssrc2jid[ssrc.getAttribute('ssrc')] = jid;
  294. });
  295. });
  296. $(document).bind('passwordrequired.muc', function (event, jid) {
  297. console.log('on password required', jid);
  298. $.prompt('<h2>Password required</h2>' +
  299. '<input id="lockKey" type="text" placeholder="shared key" autofocus>',
  300. {
  301. persistent: true,
  302. buttons: { "Ok": true , "Cancel": false},
  303. defaultButton: 1,
  304. loaded: function(event) {
  305. document.getElementById('lockKey').focus();
  306. },
  307. submit: function(e,v,m,f){
  308. if(v)
  309. {
  310. var lockKey = document.getElementById('lockKey');
  311. if (lockKey.value != null)
  312. {
  313. setSharedKey(lockKey);
  314. connection.emuc.doJoin(jid, lockKey.value);
  315. }
  316. }
  317. }
  318. });
  319. });
  320. /**
  321. * Updates the large video with the given new video source.
  322. */
  323. function updateLargeVideo(newSrc, localVideo, vol) {
  324. console.log('hover in', newSrc);
  325. if ($('#largeVideo').attr('src') != newSrc) {
  326. document.getElementById('largeVideo').volume = vol;
  327. $('#largeVideo').fadeOut(300, function () {
  328. $(this).attr('src', newSrc);
  329. var videoTransform = document.getElementById('largeVideo').style.webkitTransform;
  330. if (localVideo && videoTransform != 'scaleX(-1)') {
  331. document.getElementById('largeVideo').style.webkitTransform = "scaleX(-1)";
  332. }
  333. else if (!localVideo && videoTransform == 'scaleX(-1)') {
  334. document.getElementById('largeVideo').style.webkitTransform = "none";
  335. }
  336. $(this).fadeIn(300);
  337. });
  338. }
  339. }
  340. function toggleVideo() {
  341. if (!(connection && connection.jingle.localStream)) return;
  342. for (var idx = 0; idx < connection.jingle.localStream.getVideoTracks().length; idx++) {
  343. connection.jingle.localStream.getVideoTracks()[idx].enabled = !connection.jingle.localStream.getVideoTracks()[idx].enabled;
  344. }
  345. }
  346. function toggleAudio() {
  347. if (!(connection && connection.jingle.localStream)) return;
  348. for (var idx = 0; idx < connection.jingle.localStream.getAudioTracks().length; idx++) {
  349. connection.jingle.localStream.getAudioTracks()[idx].enabled = !connection.jingle.localStream.getAudioTracks()[idx].enabled;
  350. }
  351. }
  352. function resizeLarge() {
  353. var availableHeight = window.innerHeight;
  354. var chatspaceWidth = $('#chatspace').width();
  355. var numvids = $('#remoteVideos>video:visible').length;
  356. if (numvids < 5)
  357. availableHeight -= 100; // min thumbnail height for up to 4 videos
  358. else
  359. availableHeight -= 50; // min thumbnail height for more than 5 videos
  360. availableHeight -= 79; // padding + link ontop
  361. var availableWidth = window.innerWidth - chatspaceWidth;
  362. var aspectRatio = 16.0 / 9.0;
  363. if (availableHeight < availableWidth / aspectRatio) {
  364. availableWidth = Math.floor(availableHeight * aspectRatio);
  365. }
  366. if (availableWidth < 0 || availableHeight < 0) return;
  367. $('#largeVideo').parent().width(availableWidth);
  368. $('#largeVideo').parent().height(availableWidth / aspectRatio);
  369. resizeThumbnails();
  370. }
  371. function resizeThumbnails() {
  372. // Calculate the available height, which is the inner window height minus 39px for the header
  373. // minus 4px for the delimiter lines on the top and bottom of the large video,
  374. // minus the 36px space inside the remoteVideos container used for highlighting shadow.
  375. var availableHeight = window.innerHeight - $('#largeVideo').height() - 79;
  376. var numvids = $('#remoteVideos>span:visible').length;
  377. // Remove the 1px borders arround videos.
  378. var availableWinWidth = $('#remoteVideos').width() - 2 * numvids;
  379. var availableWidth = availableWinWidth / numvids;
  380. var aspectRatio = 16.0 / 9.0;
  381. var maxHeight = Math.min(160, availableHeight);
  382. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  383. if (availableHeight < availableWidth / aspectRatio) {
  384. availableWidth = Math.floor(availableHeight * aspectRatio);
  385. }
  386. // size videos so that while keeping AR and max height, we have a nice fit
  387. $('#remoteVideos').height(availableHeight+26); // add the 2*18px-padding-top border used for highlighting shadow.
  388. $('#remoteVideos>span').width(availableWidth);
  389. $('#remoteVideos>span').height(availableHeight);
  390. }
  391. $(document).ready(function () {
  392. $('#nickinput').keydown(function(event) {
  393. if (event.keyCode == 13) {
  394. event.preventDefault();
  395. var val = this.value;
  396. this.value = '';
  397. if (!nickname) {
  398. nickname = val;
  399. $('#nickname').css({visibility:"hidden"});
  400. $('#chatconversation').css({visibility:'visible'});
  401. $('#usermsg').css({visibility:'visible'});
  402. $('#usermsg').focus();
  403. return;
  404. }
  405. }
  406. });
  407. $('#usermsg').keydown(function(event) {
  408. if (event.keyCode == 13) {
  409. event.preventDefault();
  410. var message = this.value;
  411. $('#usermsg').val('').trigger('autosize.resize');
  412. this.focus();
  413. connection.emuc.sendMessage(message, nickname);
  414. }
  415. });
  416. $('#usermsg').autosize();
  417. resizeLarge();
  418. $(window).resize(function () {
  419. resizeLarge();
  420. });
  421. if (!$('#settings').is(':visible')) {
  422. console.log('init');
  423. init();
  424. } else {
  425. loginInfo.onsubmit = function (e) {
  426. if (e.preventDefault) e.preventDefault();
  427. $('#settings').hide();
  428. init();
  429. };
  430. }
  431. });
  432. $(window).bind('beforeunload', function () {
  433. if (connection && connection.connected) {
  434. // ensure signout
  435. $.ajax({
  436. type: 'POST',
  437. url: config.bosh,
  438. async: false,
  439. cache: false,
  440. contentType: 'application/xml',
  441. 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>",
  442. success: function (data) {
  443. console.log('signed out');
  444. console.log(data);
  445. },
  446. error: function (XMLHttpRequest, textStatus, errorThrown) {
  447. console.log('signout error', textStatus + ' (' + errorThrown + ')');
  448. }
  449. });
  450. }
  451. });
  452. function dump(elem, filename){
  453. elem = elem.parentNode;
  454. elem.download = filename || 'meetlog.json';
  455. elem.href = 'data:application/json;charset=utf-8,\n';
  456. var data = {};
  457. if (connection.jingle) {
  458. Object.keys(connection.jingle.sessions).forEach(function (sid) {
  459. var session = connection.jingle.sessions[sid];
  460. if (session.peerconnection && session.peerconnection.updateLog) {
  461. // FIXME: should probably be a .dump call
  462. /* well, if I need to modify the output format anyway...
  463. var stats = JSON.parse(JSON.stringify(session.peerconnection.stats));
  464. Object.keys(stats).forEach(function (name) {
  465. stats[name].values = JSON.stringify(stats[name].values);
  466. });
  467. */
  468. data["jingle_" + session.sid] = {
  469. updateLog: session.peerconnection.updateLog,
  470. stats: session.peerconnection.stats,
  471. url: window.location.href}
  472. ;
  473. }
  474. });
  475. }
  476. metadata = {};
  477. metadata.time = new Date();
  478. metadata.url = window.location.href;
  479. metadata.ua = navigator.userAgent;
  480. if (connection.logger) {
  481. metadata.xmpp = connection.logger.log;
  482. }
  483. data.metadata = metadata;
  484. elem.href += encodeURIComponent(JSON.stringify(data, null, ' '));
  485. return false;
  486. }
  487. /*
  488. * Appends the given message to the chat conversation.
  489. */
  490. function updateChatConversation(nick, message)
  491. {
  492. var divClassName = '';
  493. if (nickname == nick)
  494. divClassName = "localuser";
  495. else
  496. divClassName = "remoteuser";
  497. $('#chatconversation').append('<div class="' + divClassName + '"><b>' + nick + ': </b>' + message + '</div>');
  498. $('#chatconversation').animate({ scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  499. }
  500. /*
  501. * Changes the style class of the element given by id.
  502. */
  503. function buttonClick(id, classname) {
  504. $(id).toggleClass(classname); // add the class to the clicked element
  505. }
  506. /*
  507. * Opens the lock room dialog.
  508. */
  509. function openLockDialog() {
  510. // Only the focus is able to set a shared key.
  511. if (focus == null) {
  512. if (sharedKey)
  513. $.prompt("This conversation is currently protected by a shared secret key.",
  514. {
  515. title: "Secrect key",
  516. persistent: false
  517. });
  518. else
  519. $.prompt("This conversation isn't currently protected by a secret key. Only the owner of the conference could set a shared key.",
  520. {
  521. title: "Secrect key",
  522. persistent: false
  523. });
  524. }
  525. else {
  526. if (sharedKey)
  527. $.prompt("Are you sure you would like to remove your secret key?",
  528. {
  529. title: "Remove secrect key",
  530. persistent: false,
  531. buttons: { "Remove": true, "Cancel": false},
  532. defaultButton: 1,
  533. submit: function(e,v,m,f){
  534. if(v)
  535. {
  536. setSharedKey('');
  537. lockRoom(false);
  538. }
  539. }
  540. });
  541. else
  542. $.prompt('<h2>Set a secrect key to lock your room</h2>' +
  543. '<input id="lockKey" type="text" placeholder="your shared key" autofocus>',
  544. {
  545. persistent: false,
  546. buttons: { "Save": true , "Cancel": false},
  547. defaultButton: 1,
  548. loaded: function(event) {
  549. document.getElementById('lockKey').focus();
  550. },
  551. submit: function(e,v,m,f){
  552. if(v)
  553. {
  554. var lockKey = document.getElementById('lockKey');
  555. if (lockKey.value)
  556. {
  557. setSharedKey(lockKey.value);
  558. lockRoom(true);
  559. }
  560. }
  561. }
  562. });
  563. }
  564. }
  565. /*
  566. * Opens the invite link dialog.
  567. */
  568. function openLinkDialog() {
  569. $.prompt('<input id="inviteLinkRef" type="text" value="' + roomUrl + '" onclick="this.select();">',
  570. {
  571. title: "Share this link with everyone you want to invite",
  572. persistent: false,
  573. buttons: { "Cancel": false},
  574. loaded: function(event) {
  575. document.getElementById('inviteLinkRef').select();
  576. }
  577. });
  578. }
  579. /*
  580. * Locks / unlocks the room.
  581. */
  582. function lockRoom(lock) {
  583. console.log("LOCK", sharedKey);
  584. if (lock)
  585. connection.emuc.lockRoom(sharedKey);
  586. else
  587. connection.emuc.lockRoom('');
  588. updateLockButton();
  589. }
  590. /*
  591. * Sets the shared key.
  592. */
  593. function setSharedKey(sKey) {
  594. sharedKey = sKey;
  595. }
  596. /*
  597. * Updates the lock button state.
  598. */
  599. function updateLockButton() {
  600. buttonClick("#lockIcon", "fa fa-unlock fa-lg fa fa-lock fa-lg");
  601. }
  602. /*
  603. * Opens / closes the chat area.
  604. */
  605. function openChat() {
  606. var chatspace = $('#chatspace');
  607. var videospace = $('#videospace');
  608. var chatspaceWidth = chatspace.width();
  609. if (chatspace.css("opacity") == 1) {
  610. chatspace.animate({opacity: 0}, "fast");
  611. chatspace.animate({width: 0}, "slow");
  612. videospace.animate({right: 0, width:"100%"}, "slow");
  613. }
  614. else {
  615. chatspace.animate({width:"20%"}, "slow");
  616. chatspace.animate({opacity: 1}, "slow");
  617. videospace.animate({right:chatspaceWidth, width:"80%"}, "slow");
  618. }
  619. // Request the focus in the nickname field or the chat input field.
  620. if ($('#nickinput').is(':visible'))
  621. $('#nickinput').focus();
  622. else
  623. $('#usermsg').focus();
  624. }
  625. /*
  626. * Shows the call main toolbar.
  627. */
  628. function showToolbar() {
  629. $('#toolbar').css({visibility:"visible"});
  630. }
  631. /*
  632. * Updates the room invite url.
  633. */
  634. function updateRoomUrl(newRoomUrl) {
  635. roomUrl = newRoomUrl;
  636. }
  637. /*
  638. * Warning to the user that the conference window is about to be closed.
  639. */
  640. function closePageWarning() {
  641. if (focus != null)
  642. return "You are the owner of this conference call and you are about to end it.";
  643. else
  644. return "You are about to leave this conversation.";
  645. }
  646. /*
  647. * Shows a visual indicator for the focus of the conference.
  648. * Currently if we're not the owner of the conference we obtain the focus
  649. * from the connection.jingle.sessions.
  650. */
  651. function showFocusIndicator() {
  652. if (focus != null) {
  653. var localVideoToolbar = document.getElementById('localVideoToolbar');
  654. if (localVideoToolbar.childNodes.length === 0)
  655. {
  656. createFocusIndicatorElement(localVideoToolbar);
  657. }
  658. }
  659. else if (Object.keys(connection.jingle.sessions).length > 0) {
  660. // If we're only a participant the focus will be the only session we have.
  661. var session = connection.jingle.sessions[Object.keys(connection.jingle.sessions)[0]];
  662. var focusId = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  663. var focusContainer = document.getElementById(focusId);
  664. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  665. if (!indicatorSpan || indicatorSpan.length == 0) {
  666. indicatorSpan = document.createElement('span');
  667. indicatorSpan.className = 'focusindicator';
  668. focusContainer.appendChild(indicatorSpan);
  669. createFocusIndicatorElement(indicatorSpan);
  670. }
  671. }
  672. }
  673. /*
  674. * Creates the element indicating the focus of the conference.
  675. */
  676. function createFocusIndicatorElement(parentElement) {
  677. var focusIndicator = document.createElement('i');
  678. focusIndicator.className = 'fa fa-star';
  679. focusIndicator.title = "The owner of this conference"
  680. parentElement.appendChild(focusIndicator);
  681. }