您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. var preziPlayer = null;
  13. /* window.onbeforeunload = closePageWarning; */
  14. function init() {
  15. RTC = setupRTC();
  16. if (RTC === null) {
  17. window.location.href = 'webrtcrequired.html';
  18. return;
  19. } else if (RTC.browser != 'chrome') {
  20. window.location.href = 'chromeonly.html';
  21. return;
  22. }
  23. RTCPeerconnection = TraceablePeerConnection;
  24. connection = new Strophe.Connection(document.getElementById('boshURL').value || config.bosh || '/http-bind');
  25. if (connection.disco) {
  26. // for chrome, add multistream cap
  27. }
  28. connection.jingle.pc_constraints = RTC.pc_constraints;
  29. if (config.useIPv6) {
  30. // https://code.google.com/p/webrtc/issues/detail?id=2828
  31. if (!connection.jingle.pc_constraints.optional) connection.jingle.pc_constraints.optional = [];
  32. connection.jingle.pc_constraints.optional.push({googIPv6: true});
  33. }
  34. var jid = document.getElementById('jid').value || config.hosts.domain || window.location.hostname;
  35. connection.connect(jid, document.getElementById('password').value, function (status) {
  36. if (status == Strophe.Status.CONNECTED) {
  37. console.log('connected');
  38. if (config.useStunTurn) {
  39. connection.jingle.getStunAndTurnCredentials();
  40. }
  41. if (RTC.browser == 'firefox') {
  42. getUserMediaWithConstraints(['audio']);
  43. } else {
  44. getUserMediaWithConstraints(['audio', 'video'], '360');
  45. }
  46. document.getElementById('connect').disabled = true;
  47. } else {
  48. console.log('status', status);
  49. }
  50. });
  51. }
  52. function doJoin() {
  53. var roomnode = null;
  54. var path = window.location.pathname;
  55. var roomjid;
  56. // determinde the room node from the url
  57. // TODO: just the roomnode or the whole bare jid?
  58. if (config.getroomnode && typeof config.getroomnode === 'function') {
  59. // custom function might be responsible for doing the pushstate
  60. roomnode = config.getroomnode(path);
  61. } else {
  62. /* fall back to default strategy
  63. * this is making assumptions about how the URL->room mapping happens.
  64. * It currently assumes deployment at root, with a rewrite like the
  65. * following one (for nginx):
  66. location ~ ^/([a-zA-Z0-9]+)$ {
  67. rewrite ^/(.*)$ / break;
  68. }
  69. */
  70. if (path.length > 1) {
  71. roomnode = path.substr(1).toLowerCase();
  72. } else {
  73. roomnode = Math.random().toString(36).substr(2, 20);
  74. window.history.pushState('VideoChat', 'Room: ' + roomnode, window.location.pathname + roomnode);
  75. }
  76. }
  77. roomjid = roomnode + '@' + config.hosts.muc;
  78. if (config.useNicks) {
  79. var nick = window.prompt('Your nickname (optional)');
  80. if (nick) {
  81. roomjid += '/' + nick;
  82. } else {
  83. roomjid += '/' + Strophe.getNodeFromJid(connection.jid);
  84. }
  85. } else {
  86. roomjid += '/' + Strophe.getNodeFromJid(connection.jid).substr(0,8);
  87. }
  88. connection.emuc.doJoin(roomjid);
  89. }
  90. $(document).bind('mediaready.jingle', function (event, stream) {
  91. connection.jingle.localStream = stream;
  92. RTC.attachMediaStream($('#localVideo'), stream);
  93. document.getElementById('localVideo').muted = true;
  94. document.getElementById('localVideo').autoplay = true;
  95. document.getElementById('localVideo').volume = 0;
  96. localVideoSrc = document.getElementById('localVideo').src;
  97. updateLargeVideo(localVideoSrc, true, 0);
  98. $('#localVideo').click(function () {
  99. updateLargeVideo($(this).attr('src'), true, 1);
  100. });
  101. doJoin();
  102. });
  103. $(document).bind('mediafailure.jingle', function () {
  104. // FIXME
  105. });
  106. $(document).bind('remotestreamadded.jingle', function (event, data, sid) {
  107. function waitForRemoteVideo(selector, sid) {
  108. var sess = connection.jingle.sessions[sid];
  109. videoTracks = data.stream.getVideoTracks();
  110. if (videoTracks.length === 0 || selector[0].currentTime > 0) {
  111. RTC.attachMediaStream(selector, data.stream); // FIXME: why do i have to do this for FF?
  112. $(document).trigger('callactive.jingle', [selector, sid]);
  113. console.log('waitForremotevideo', sess.peerconnection.iceConnectionState, sess.peerconnection.signalingState);
  114. } else {
  115. setTimeout(function () { waitForRemoteVideo(selector, sid); }, 100);
  116. }
  117. }
  118. var sess = connection.jingle.sessions[sid];
  119. // look up an associated JID for a stream id
  120. if (data.stream.id.indexOf('mixedmslabel') == -1) {
  121. var ssrclines = SDPUtil.find_lines(sess.peerconnection.remoteDescription.sdp, 'a=ssrc');
  122. ssrclines = ssrclines.filter(function (line) {
  123. return line.indexOf('mslabel:' + data.stream.label) != -1;
  124. });
  125. if (ssrclines.length) {
  126. thessrc = ssrclines[0].substring(7).split(' ')[0];
  127. // ok to overwrite the one from focus? might save work in colibri.js
  128. console.log('associated jid', ssrc2jid[thessrc], data.peerjid);
  129. if (ssrc2jid[thessrc]) {
  130. data.peerjid = ssrc2jid[thessrc];
  131. }
  132. }
  133. }
  134. var container;
  135. var remotes = document.getElementById('remoteVideos');
  136. if (data.peerjid) {
  137. container = document.getElementById('participant_' + Strophe.getResourceFromJid(data.peerjid));
  138. if (!container) {
  139. console.warn('no container for', data.peerjid);
  140. // create for now...
  141. // FIXME: should be removed
  142. container = addRemoteVideoContainer('participant_' + Strophe.getResourceFromJid(data.peerjid));
  143. } else {
  144. //console.log('found container for', data.peerjid);
  145. }
  146. } else {
  147. if (data.stream.id != 'mixedmslabel') {
  148. console.warn('can not associate stream', data.stream.id, 'with a participant');
  149. }
  150. // FIXME: for the mixed ms we dont need a video -- currently
  151. container = document.createElement('span');
  152. container.className = 'videocontainer';
  153. remotes.appendChild(container);
  154. }
  155. var vid = document.createElement('video');
  156. var id = 'remoteVideo_' + sid + '_' + data.stream.id;
  157. vid.id = id;
  158. vid.autoplay = true;
  159. vid.oncontextmenu = function () { return false; };
  160. container.appendChild(vid);
  161. // TODO: make mixedstream display:none via css?
  162. if (id.indexOf('mixedmslabel') != -1) {
  163. container.id = 'mixedstream';
  164. $(container).hide();
  165. }
  166. var sel = $('#' + id);
  167. sel.hide();
  168. RTC.attachMediaStream(sel, data.stream);
  169. waitForRemoteVideo(sel, sid);
  170. data.stream.onended = function () {
  171. console.log('stream ended', this.id);
  172. var src = $('#' + id).attr('src');
  173. if (src === $('#largeVideo').attr('src')) {
  174. // this is currently displayed as large
  175. // pick the last visible video in the row
  176. // if nobody else is left, this picks the local video
  177. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last>video').get(0);
  178. // mute if localvideo
  179. var isLocalVideo = false;
  180. if (pick) {
  181. if (pick.src === localVideoSrc)
  182. isLocalVideo = true;
  183. updateLargeVideo(pick.src, isLocalVideo, pick.volume);
  184. }
  185. }
  186. $('#' + id).parent().remove();
  187. resizeThumbnails();
  188. };
  189. sel.click(
  190. function () {
  191. updateLargeVideo($(this).attr('src'), false, 1);
  192. }
  193. );
  194. });
  195. $(document).bind('callincoming.jingle', function (event, sid) {
  196. var sess = connection.jingle.sessions[sid];
  197. // TODO: check affiliation and/or role
  198. console.log('emuc data for', sess.peerjid, connection.emuc.members[sess.peerjid]);
  199. sess.usedrip = true; // not-so-naive trickle ice
  200. sess.sendAnswer();
  201. sess.accept();
  202. });
  203. $(document).bind('callactive.jingle', function (event, videoelem, sid) {
  204. if (videoelem.attr('id').indexOf('mixedmslabel') == -1) {
  205. // ignore mixedmslabela0 and v0
  206. videoelem.show();
  207. resizeThumbnails();
  208. updateLargeVideo(videoelem.attr('src'), false, 1);
  209. showFocusIndicator();
  210. }
  211. });
  212. $(document).bind('callterminated.jingle', function (event, sid, reason) {
  213. // FIXME
  214. });
  215. $(document).bind('setLocalDescription.jingle', function (event, sid) {
  216. // put our ssrcs into presence so other clients can identify our stream
  217. var sess = connection.jingle.sessions[sid];
  218. var newssrcs = {};
  219. var localSDP = new SDP(sess.peerconnection.localDescription.sdp);
  220. localSDP.media.forEach(function (media) {
  221. var type = SDPUtil.parse_mline(media.split('\r\n')[0]).media;
  222. var ssrc = SDPUtil.find_line(media, 'a=ssrc:').substring(7).split(' ')[0];
  223. // assumes a single local ssrc
  224. newssrcs[type] = ssrc;
  225. });
  226. console.log('new ssrcs', newssrcs);
  227. var i = 0;
  228. Object.keys(newssrcs).forEach(function (mtype) {
  229. i++;
  230. connection.emuc.addMediaToPresence(i, mtype, newssrcs[mtype]);
  231. });
  232. connection.emuc.sendPresence();
  233. });
  234. $(document).bind('joined.muc', function (event, jid, info) {
  235. updateRoomUrl(window.location.href);
  236. document.getElementById('localNick').appendChild(
  237. document.createTextNode(Strophe.getResourceFromJid(jid) + ' (you)')
  238. );
  239. if (Object.keys(connection.emuc.members).length < 1) {
  240. focus = new ColibriFocus(connection, config.hosts.bridge);
  241. }
  242. // Once we've joined the muc show the toolbar
  243. showToolbar();
  244. });
  245. $(document).bind('entered.muc', function (event, jid, info, pres) {
  246. console.log('entered', jid, info);
  247. console.log(focus);
  248. var container = addRemoteVideoContainer('participant_' + Strophe.getResourceFromJid(jid));
  249. var nickfield = document.createElement('span');
  250. nickfield.appendChild(document.createTextNode(Strophe.getResourceFromJid(jid)));
  251. container.appendChild(nickfield);
  252. resizeThumbnails();
  253. if (focus !== null) {
  254. // FIXME: this should prepare the video
  255. if (focus.confid === null) {
  256. console.log('make new conference with', jid);
  257. focus.makeConference(Object.keys(connection.emuc.members));
  258. } else {
  259. console.log('invite', jid, 'into conference');
  260. focus.addNewParticipant(jid);
  261. }
  262. }
  263. else if (sharedKey) {
  264. updateLockButton();
  265. }
  266. showFocusIndicator();
  267. $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) {
  268. //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc'));
  269. ssrc2jid[ssrc.getAttribute('ssrc')] = jid;
  270. });
  271. });
  272. $(document).bind('left.muc', function (event, jid) {
  273. console.log('left', jid);
  274. connection.jingle.terminateByJid(jid);
  275. var container = document.getElementById('participant_' + Strophe.getResourceFromJid(jid));
  276. if (container) {
  277. // hide here, wait for video to close before removing
  278. $(container).hide();
  279. resizeThumbnails();
  280. }
  281. if (Object.keys(connection.emuc.members).length === 0) {
  282. console.log('everyone left');
  283. if (focus !== null) {
  284. // FIXME: closing the connection is a hack to avoid some
  285. // problemswith reinit
  286. if (focus.peerconnection !== null) {
  287. focus.peerconnection.close();
  288. }
  289. focus = new ColibriFocus(connection, config.hosts.bridge);
  290. }
  291. }
  292. if (connection.emuc.getPrezi(jid)) {
  293. $(document).trigger('presentationremoved.muc', [jid, connection.emuc.getPrezi(jid)]);
  294. }
  295. });
  296. $(document).bind('presence.muc', function (event, jid, info, pres) {
  297. $(pres).find('>media[xmlns="http://estos.de/ns/mjs"]>source').each(function (idx, ssrc) {
  298. //console.log(jid, 'assoc ssrc', ssrc.getAttribute('type'), ssrc.getAttribute('ssrc'));
  299. ssrc2jid[ssrc.getAttribute('ssrc')] = jid;
  300. });
  301. });
  302. $(document).bind('passwordrequired.muc', function (event, jid) {
  303. console.log('on password required', jid);
  304. $.prompt('<h2>Password required</h2>' +
  305. '<input id="lockKey" type="text" placeholder="shared key" autofocus>',
  306. {
  307. persistent: true,
  308. buttons: { "Ok": true , "Cancel": false},
  309. defaultButton: 1,
  310. loaded: function(event) {
  311. document.getElementById('lockKey').focus();
  312. },
  313. submit: function(e,v,m,f){
  314. if(v)
  315. {
  316. var lockKey = document.getElementById('lockKey');
  317. if (lockKey.value != null)
  318. {
  319. setSharedKey(lockKey);
  320. connection.emuc.doJoin(jid, lockKey.value);
  321. }
  322. }
  323. }
  324. });
  325. });
  326. /*
  327. * Presentation has been removed.
  328. */
  329. $(document).bind('presentationremoved.muc', function(event, jid, presUrl) {
  330. console.log('presentation removed', presUrl);
  331. var presId = getPresentationId(presUrl);
  332. setPresentationVisible(false);
  333. $('#participant_' + Strophe.getResourceFromJid(jid) + '_' + presId).remove();
  334. $('#presentation>iframe').remove();
  335. if (preziPlayer != null) {
  336. preziPlayer.destroy();
  337. preziPlayer = null;
  338. }
  339. });
  340. /*
  341. * Presentation has been added.
  342. */
  343. $(document).bind('presentationadded.muc', function (event, jid, presUrl, currentSlide) {
  344. console.log("presentation added", presUrl);
  345. var presId = getPresentationId(presUrl);
  346. var elementId = 'participant_' + Strophe.getResourceFromJid(jid) + '_' + presId;
  347. var container = addRemoteVideoContainer(elementId);
  348. resizeThumbnails();
  349. var controlsEnabled = false;
  350. if (jid === connection.emuc.myroomjid)
  351. controlsEnabled = true;
  352. setPresentationVisible(true);
  353. $('#largeVideoContainer').hover(
  354. function (event) {
  355. if ($('#largeVideo').css('visibility') == 'hidden')
  356. $('#reloadPresentation').css({display:'inline-block'});
  357. },
  358. function (event) {
  359. if ($('#largeVideo').css('visibility') == 'visible')
  360. $('#reloadPresentation').css({display:'none'});
  361. else {
  362. var e = event.toElement || event.relatedTarget;
  363. while(e && e.parentNode && e.parentNode != window) {
  364. if (e.parentNode == this || e == this) {
  365. return false;
  366. }
  367. e = e.parentNode;
  368. }
  369. $('#reloadPresentation').css({display:'none'});
  370. }
  371. });
  372. preziPlayer = new PreziPlayer(
  373. 'presentation',
  374. {preziId: presId,
  375. width: $('#largeVideoContainer').width(),
  376. height: $('#largeVideoContainer').height(),
  377. controls: controlsEnabled,
  378. debug: true
  379. });
  380. $('#presentation>iframe').attr('id', preziPlayer.options.preziId);
  381. // $('#presentation>iframe').load(function (){
  382. // console.log("IFRAME LOADED!!!!!!!!!!!!!!!!");
  383. // });
  384. // $('#presentation>iframe').ready(function (){
  385. // console.log("IFRAME READY!!!!!!!!!!!!!!!!");
  386. // });
  387. preziPlayer.on(PreziPlayer.EVENT_STATUS, function(event) {
  388. console.log("prezi status", event.value);
  389. if (event.value == PreziPlayer.STATUS_CONTENT_READY) {
  390. if (jid != connection.emuc.myroomjid)
  391. preziPlayer.flyToStep(currentSlide);
  392. }
  393. });
  394. preziPlayer.on(PreziPlayer.EVENT_CURRENT_STEP, function(event) {
  395. console.log("event value", event.value);
  396. connection.emuc.addCurrentSlideToPresence(event.value);
  397. connection.emuc.sendPresence();
  398. });
  399. $("#" + elementId).css('background-image','url(../images/avatarprezi.png)');
  400. $("#" + elementId).click(
  401. function () {
  402. setPresentationVisible(true);
  403. }
  404. );
  405. });
  406. /*
  407. * Indicates presentation slide change.
  408. */
  409. $(document).bind('gotoslide.muc', function (event, jid, presUrl, current) {
  410. if (preziPlayer) {
  411. preziPlayer.flyToStep(current);
  412. }
  413. });
  414. /**
  415. * Returns the presentation id from the given url.
  416. */
  417. function getPresentationId (presUrl) {
  418. var presIdTmp = presUrl.substring(presUrl.indexOf("prezi.com/") + 10);
  419. return presIdTmp.substring(0, presIdTmp.indexOf('/'));
  420. }
  421. /*
  422. * Reloads the current presentation.
  423. */
  424. function reloadPresentation() {
  425. var iframe = document.getElementById(preziPlayer.options.preziId);
  426. iframe.src = iframe.src;
  427. }
  428. /*
  429. * Shows/hides a presentation.
  430. */
  431. function setPresentationVisible(visible) {
  432. if (visible) {
  433. $('#largeVideo').fadeOut(300, function () {
  434. $('#largeVideo').css({visibility:'hidden'});
  435. $('#presentation>iframe').fadeIn(300, function() {
  436. $('#presentation>iframe').css({opacity:'1'});
  437. });
  438. });
  439. }
  440. else {
  441. if ($('#presentation>iframe')) {
  442. $('#presentation>iframe').fadeOut(300, function () {
  443. $('#presentation>iframe').css({opacity:'0'});
  444. $('#largeVideo').fadeIn(300, function() {
  445. $('#largeVideo').css({visibility:'visible'});
  446. });
  447. });
  448. }
  449. }
  450. }
  451. /**
  452. * Updates the large video with the given new video source.
  453. */
  454. function updateLargeVideo(newSrc, localVideo, vol) {
  455. console.log('hover in', newSrc);
  456. setPresentationVisible(false);
  457. if ($('#largeVideo').attr('src') != newSrc) {
  458. document.getElementById('largeVideo').volume = vol;
  459. $('#largeVideo').fadeOut(300, function () {
  460. $(this).attr('src', newSrc);
  461. var videoTransform = document.getElementById('largeVideo').style.webkitTransform;
  462. if (localVideo && videoTransform != 'scaleX(-1)') {
  463. document.getElementById('largeVideo').style.webkitTransform = "scaleX(-1)";
  464. }
  465. else if (!localVideo && videoTransform == 'scaleX(-1)') {
  466. document.getElementById('largeVideo').style.webkitTransform = "none";
  467. }
  468. $(this).fadeIn(300);
  469. });
  470. }
  471. }
  472. function toggleVideo() {
  473. if (!(connection && connection.jingle.localStream)) return;
  474. for (var idx = 0; idx < connection.jingle.localStream.getVideoTracks().length; idx++) {
  475. connection.jingle.localStream.getVideoTracks()[idx].enabled = !connection.jingle.localStream.getVideoTracks()[idx].enabled;
  476. }
  477. }
  478. function toggleAudio() {
  479. if (!(connection && connection.jingle.localStream)) return;
  480. for (var idx = 0; idx < connection.jingle.localStream.getAudioTracks().length; idx++) {
  481. connection.jingle.localStream.getAudioTracks()[idx].enabled = !connection.jingle.localStream.getAudioTracks()[idx].enabled;
  482. }
  483. }
  484. function resizeLarge() {
  485. var availableHeight = window.innerHeight;
  486. var chatspaceWidth = $('#chatspace').width();
  487. var numvids = $('#remoteVideos>video:visible').length;
  488. if (numvids < 5)
  489. availableHeight -= 100; // min thumbnail height for up to 4 videos
  490. else
  491. availableHeight -= 50; // min thumbnail height for more than 5 videos
  492. availableHeight -= 79; // padding + link ontop
  493. var availableWidth = window.innerWidth - chatspaceWidth;
  494. var aspectRatio = 16.0 / 9.0;
  495. if (availableHeight < availableWidth / aspectRatio) {
  496. availableWidth = Math.floor(availableHeight * aspectRatio);
  497. }
  498. if (availableWidth < 0 || availableHeight < 0) return;
  499. $('#largeVideo').parent().width(availableWidth);
  500. $('#largeVideo').parent().height(availableWidth / aspectRatio);
  501. $('#presentation>iframe').width(availableWidth);
  502. $('#presentation>iframe').height(availableWidth / aspectRatio);
  503. resizeThumbnails();
  504. }
  505. function resizeThumbnails() {
  506. // Calculate the available height, which is the inner window height minus 39px for the header
  507. // minus 4px for the delimiter lines on the top and bottom of the large video,
  508. // minus the 36px space inside the remoteVideos container used for highlighting shadow.
  509. var availableHeight = window.innerHeight - $('#largeVideo').height() - 79;
  510. var numvids = $('#remoteVideos>span:visible').length;
  511. // Remove the 1px borders arround videos.
  512. var availableWinWidth = $('#remoteVideos').width() - 2 * numvids - 50;
  513. var availableWidth = availableWinWidth / numvids;
  514. var aspectRatio = 16.0 / 9.0;
  515. var maxHeight = Math.min(160, availableHeight);
  516. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  517. if (availableHeight < availableWidth / aspectRatio) {
  518. availableWidth = Math.floor(availableHeight * aspectRatio);
  519. }
  520. // size videos so that while keeping AR and max height, we have a nice fit
  521. $('#remoteVideos').height(availableHeight+26); // add the 2*18px-padding-top border used for highlighting shadow.
  522. $('#remoteVideos>span').width(availableWidth);
  523. $('#remoteVideos>span').height(availableHeight);
  524. }
  525. $(document).ready(function () {
  526. $('#nickinput').keydown(function(event) {
  527. if (event.keyCode == 13) {
  528. event.preventDefault();
  529. var val = this.value;
  530. this.value = '';
  531. if (!nickname) {
  532. nickname = val;
  533. $('#nickname').css({visibility:"hidden"});
  534. $('#chatconversation').css({visibility:'visible'});
  535. $('#usermsg').css({visibility:'visible'});
  536. $('#usermsg').focus();
  537. return;
  538. }
  539. }
  540. });
  541. $('#usermsg').keydown(function(event) {
  542. if (event.keyCode == 13) {
  543. event.preventDefault();
  544. var message = this.value;
  545. $('#usermsg').val('').trigger('autosize.resize');
  546. this.focus();
  547. connection.emuc.sendMessage(message, nickname);
  548. }
  549. });
  550. $('#usermsg').autosize();
  551. // Set the defaults for prompt dialogs.
  552. jQuery.prompt.setDefaults({persistent: false});
  553. resizeLarge();
  554. $(window).resize(function () {
  555. resizeLarge();
  556. });
  557. if (!$('#settings').is(':visible')) {
  558. console.log('init');
  559. init();
  560. } else {
  561. loginInfo.onsubmit = function (e) {
  562. if (e.preventDefault) e.preventDefault();
  563. $('#settings').hide();
  564. init();
  565. };
  566. }
  567. });
  568. $(window).bind('beforeunload', function () {
  569. if (connection && connection.connected) {
  570. // ensure signout
  571. $.ajax({
  572. type: 'POST',
  573. url: config.bosh,
  574. async: false,
  575. cache: false,
  576. contentType: 'application/xml',
  577. 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>",
  578. success: function (data) {
  579. console.log('signed out');
  580. console.log(data);
  581. },
  582. error: function (XMLHttpRequest, textStatus, errorThrown) {
  583. console.log('signout error', textStatus + ' (' + errorThrown + ')');
  584. }
  585. });
  586. }
  587. });
  588. function dump(elem, filename){
  589. elem = elem.parentNode;
  590. elem.download = filename || 'meetlog.json';
  591. elem.href = 'data:application/json;charset=utf-8,\n';
  592. var data = {};
  593. if (connection.jingle) {
  594. Object.keys(connection.jingle.sessions).forEach(function (sid) {
  595. var session = connection.jingle.sessions[sid];
  596. if (session.peerconnection && session.peerconnection.updateLog) {
  597. // FIXME: should probably be a .dump call
  598. data["jingle_" + session.sid] = {
  599. updateLog: session.peerconnection.updateLog,
  600. url: window.location.href}
  601. ;
  602. }
  603. });
  604. }
  605. metadata = {};
  606. metadata.time = new Date();
  607. metadata.url = window.location.href;
  608. metadata.ua = navigator.userAgent;
  609. if (connection.logger) {
  610. metadata.xmpp = connection.logger.log;
  611. }
  612. data.metadata = metadata;
  613. elem.href += encodeURIComponent(JSON.stringify(data, null, ' '));
  614. return false;
  615. }
  616. /*
  617. * Appends the given message to the chat conversation.
  618. */
  619. function updateChatConversation(nick, message)
  620. {
  621. var divClassName = '';
  622. if (nickname == nick)
  623. divClassName = "localuser";
  624. else
  625. divClassName = "remoteuser";
  626. $('#chatconversation').append('<div class="' + divClassName + '"><b>' + nick + ': </b>' + message + '</div>');
  627. $('#chatconversation').animate({ scrollTop: $('#chatconversation')[0].scrollHeight}, 1000);
  628. }
  629. /*
  630. * Changes the style class of the element given by id.
  631. */
  632. function buttonClick(id, classname) {
  633. $(id).toggleClass(classname); // add the class to the clicked element
  634. }
  635. /*
  636. * Opens the lock room dialog.
  637. */
  638. function openLockDialog() {
  639. // Only the focus is able to set a shared key.
  640. if (focus == null) {
  641. if (sharedKey)
  642. $.prompt("This conversation is currently protected by a shared secret key.",
  643. {
  644. title: "Secrect key",
  645. persistent: false
  646. });
  647. else
  648. $.prompt("This conversation isn't currently protected by a secret key. Only the owner of the conference could set a shared key.",
  649. {
  650. title: "Secrect key",
  651. persistent: false
  652. });
  653. }
  654. else {
  655. if (sharedKey)
  656. $.prompt("Are you sure you would like to remove your secret key?",
  657. {
  658. title: "Remove secrect key",
  659. persistent: false,
  660. buttons: { "Remove": true, "Cancel": false},
  661. defaultButton: 1,
  662. submit: function(e,v,m,f){
  663. if(v)
  664. {
  665. setSharedKey('');
  666. lockRoom(false);
  667. }
  668. }
  669. });
  670. else
  671. $.prompt('<h2>Set a secrect key to lock your room</h2>' +
  672. '<input id="lockKey" type="text" placeholder="your shared key" autofocus>',
  673. {
  674. persistent: false,
  675. buttons: { "Save": true , "Cancel": false},
  676. defaultButton: 1,
  677. loaded: function(event) {
  678. document.getElementById('lockKey').focus();
  679. },
  680. submit: function(e,v,m,f){
  681. if(v)
  682. {
  683. var lockKey = document.getElementById('lockKey');
  684. if (lockKey.value)
  685. {
  686. setSharedKey(lockKey.value);
  687. lockRoom(true);
  688. }
  689. }
  690. }
  691. });
  692. }
  693. }
  694. /*
  695. * Opens the invite link dialog.
  696. */
  697. function openLinkDialog() {
  698. $.prompt('<input id="inviteLinkRef" type="text" value="' + roomUrl + '" onclick="this.select();">',
  699. {
  700. title: "Share this link with everyone you want to invite",
  701. persistent: false,
  702. buttons: { "Cancel": false},
  703. loaded: function(event) {
  704. document.getElementById('inviteLinkRef').select();
  705. }
  706. });
  707. }
  708. /*
  709. * Opens the settings dialog.
  710. */
  711. function openSettingsDialog() {
  712. $.prompt('<h2>Configure your conference</h2>' +
  713. '<input type="checkbox" id="initMuted"> Participants join muted<br/>' +
  714. '<input type="checkbox" id="requireNicknames"> Require nicknames<br/><br/>' +
  715. 'Set a secrect key to lock your room: <input id="lockKey" type="text" placeholder="your shared key" autofocus>',
  716. {
  717. persistent: false,
  718. buttons: { "Save": true , "Cancel": false},
  719. defaultButton: 1,
  720. loaded: function(event) {
  721. document.getElementById('lockKey').focus();
  722. },
  723. submit: function(e,v,m,f){
  724. if(v)
  725. {
  726. if ($('#initMuted').is(":checked"))
  727. {
  728. // it is checked
  729. }
  730. if ($('#requireNicknames').is(":checked"))
  731. {
  732. // it is checked
  733. }
  734. /*
  735. var lockKey = document.getElementById('lockKey');
  736. if (lockKey.value)
  737. {
  738. setSharedKey(lockKey.value);
  739. lockRoom(true);
  740. }
  741. */
  742. }
  743. }
  744. });
  745. }
  746. /*
  747. * Opens the Prezi dialog, from which the user could choose a presentation to load.
  748. */
  749. function openPreziDialog() {
  750. var myprezi = connection.emuc.getPrezi(connection.emuc.myroomjid);
  751. if (myprezi) {
  752. $.prompt("Are you sure you would like to remove your Prezi?",
  753. {
  754. title: "Remove Prezi",
  755. buttons: { "Remove": true, "Cancel": false},
  756. defaultButton: 1,
  757. submit: function(e,v,m,f){
  758. if(v)
  759. {
  760. connection.emuc.removePreziFromPresence();
  761. connection.emuc.sendPresence();
  762. }
  763. }
  764. });
  765. }
  766. else if (preziPlayer != null) {
  767. $.prompt("Another participant is already sharing a Prezi. This conference allows only one Prezi at a time.",
  768. {
  769. title: "Share a Prezi",
  770. buttons: { "Ok": true},
  771. defaultButton: 0,
  772. submit: function(e,v,m,f){
  773. $.prompt.close();
  774. }
  775. });
  776. }
  777. else {
  778. var openPreziState = {
  779. state0: {
  780. html: '<h2>Share a Prezi</h2>' +
  781. '<input id="preziUrl" type="text" placeholder="e.g. http://prezi.com/wz7vhjycl7e6/my-prezi" autofocus>',
  782. persistent: false,
  783. buttons: { "Share": true , "Cancel": false},
  784. defaultButton: 1,
  785. submit: function(e,v,m,f){
  786. e.preventDefault();
  787. if(v)
  788. {
  789. var preziUrl = document.getElementById('preziUrl');
  790. if (preziUrl.value)
  791. {
  792. if (preziUrl.value.indexOf('http://prezi.com/') != 0
  793. && preziUrl.value.indexOf('https://prezi.com/') != 0)
  794. {
  795. $.prompt.goToState('state1');
  796. return false;
  797. }
  798. else {
  799. var presIdTmp = preziUrl.value.substring(preziUrl.value.indexOf("prezi.com/") + 10);
  800. if (presIdTmp.indexOf('/') < 2) {
  801. $.prompt.goToState('state1');
  802. return false;
  803. }
  804. else {
  805. connection.emuc.addPreziToPresence(preziUrl.value, 0);
  806. connection.emuc.sendPresence();
  807. $.prompt.close();
  808. }
  809. }
  810. }
  811. }
  812. else
  813. $.prompt.close();
  814. }
  815. },
  816. state1: {
  817. html: '<h2>Share a Prezi</h2>' +
  818. 'Please provide a correct prezi link.',
  819. persistent: false,
  820. buttons: { "Back": true, "Cancel": false },
  821. defaultButton: 1,
  822. submit:function(e,v,m,f) {
  823. e.preventDefault();
  824. if(v==0)
  825. $.prompt.close();
  826. else
  827. $.prompt.goToState('state0');
  828. }
  829. }
  830. };
  831. var myPrompt = jQuery.prompt(openPreziState);
  832. myPrompt.on('impromptu:loaded', function(e) {
  833. document.getElementById('preziUrl').focus();
  834. });
  835. myPrompt.on('impromptu:statechanged', function(e) {
  836. document.getElementById('preziUrl').focus();
  837. });
  838. }
  839. }
  840. /*
  841. * Locks / unlocks the room.
  842. */
  843. function lockRoom(lock) {
  844. if (lock)
  845. connection.emuc.lockRoom(sharedKey);
  846. else
  847. connection.emuc.lockRoom('');
  848. updateLockButton();
  849. }
  850. /*
  851. * Sets the shared key.
  852. */
  853. function setSharedKey(sKey) {
  854. sharedKey = sKey;
  855. }
  856. /*
  857. * Updates the lock button state.
  858. */
  859. function updateLockButton() {
  860. buttonClick("#lockIcon", "fa fa-unlock fa-lg fa fa-lock fa-lg");
  861. }
  862. /*
  863. * Opens / closes the chat area.
  864. */
  865. function openChat() {
  866. var chatspace = $('#chatspace');
  867. var videospace = $('#videospace');
  868. var chatspaceWidth = chatspace.width();
  869. if (chatspace.css("opacity") == 1) {
  870. chatspace.animate({opacity: 0}, "fast");
  871. chatspace.animate({width: 0}, "slow");
  872. videospace.animate({right: 0, width:"100%"}, "slow");
  873. }
  874. else {
  875. chatspace.animate({width:"20%"}, "slow");
  876. chatspace.animate({opacity: 1}, "slow");
  877. videospace.animate({right:chatspaceWidth, width:"80%"}, "slow");
  878. }
  879. // Request the focus in the nickname field or the chat input field.
  880. if ($('#nickinput').is(':visible'))
  881. $('#nickinput').focus();
  882. else
  883. $('#usermsg').focus();
  884. }
  885. /*
  886. * Shows the call main toolbar.
  887. */
  888. function showToolbar() {
  889. $('#toolbar').css({visibility:"visible"});
  890. if (focus != null)
  891. {
  892. // TODO: Enable settings functionality. Need to uncomment the settings button in index.html.
  893. // $('#settingsButton').css({visibility:"visible"});
  894. }
  895. }
  896. /*
  897. * Updates the room invite url.
  898. */
  899. function updateRoomUrl(newRoomUrl) {
  900. roomUrl = newRoomUrl;
  901. }
  902. /*
  903. * Warning to the user that the conference window is about to be closed.
  904. */
  905. function closePageWarning() {
  906. if (focus != null)
  907. return "You are the owner of this conference call and you are about to end it.";
  908. else
  909. return "You are about to leave this conversation.";
  910. }
  911. /*
  912. * Shows a visual indicator for the focus of the conference.
  913. * Currently if we're not the owner of the conference we obtain the focus
  914. * from the connection.jingle.sessions.
  915. */
  916. function showFocusIndicator() {
  917. if (focus != null) {
  918. var localVideoToolbar = document.getElementById('localVideoToolbar');
  919. if (localVideoToolbar.childNodes.length === 0)
  920. {
  921. createFocusIndicatorElement(localVideoToolbar);
  922. }
  923. }
  924. else if (Object.keys(connection.jingle.sessions).length > 0) {
  925. // If we're only a participant the focus will be the only session we have.
  926. var session = connection.jingle.sessions[Object.keys(connection.jingle.sessions)[0]];
  927. var focusId = 'participant_' + Strophe.getResourceFromJid(session.peerjid);
  928. var focusContainer = document.getElementById(focusId);
  929. var indicatorSpan = $('#' + focusId + ' .focusindicator');
  930. if (!indicatorSpan || indicatorSpan.length == 0) {
  931. indicatorSpan = document.createElement('span');
  932. indicatorSpan.className = 'focusindicator';
  933. focusContainer.appendChild(indicatorSpan);
  934. createFocusIndicatorElement(indicatorSpan);
  935. }
  936. }
  937. }
  938. function addRemoteVideoContainer(id) {
  939. var container = document.createElement('span');
  940. container.id = id;
  941. container.className = 'videocontainer';
  942. var remotes = document.getElementById('remoteVideos');
  943. remotes.appendChild(container);
  944. return container;
  945. }
  946. /*
  947. * Creates the element indicating the focus of the conference.
  948. */
  949. function createFocusIndicatorElement(parentElement) {
  950. var focusIndicator = document.createElement('i');
  951. focusIndicator.className = 'fa fa-star';
  952. focusIndicator.title = "The owner of this conference"
  953. parentElement.appendChild(focusIndicator);
  954. }