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.

VideoLayout.js 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /* global config, APP, $, Strophe, require, interfaceConfig */
  2. /* jshint -W101 */
  3. var AudioLevels = require("../audio_levels/AudioLevels");
  4. var ContactList = require("../side_pannels/contactlist/ContactList");
  5. var MediaStreamType = require("../../../service/RTC/MediaStreamTypes");
  6. var UIEvents = require("../../../service/UI/UIEvents");
  7. var UIUtil = require("../util/UIUtil");
  8. var RTC = require("../../RTC/RTC");
  9. var RTCBrowserType = require('../../RTC/RTCBrowserType');
  10. var RemoteVideo = require("./RemoteVideo");
  11. var LargeVideo = require("./LargeVideo");
  12. var LocalVideo = require("./LocalVideo");
  13. var remoteVideos = {};
  14. var remoteVideoTypes = {};
  15. var localVideoThumbnail = null;
  16. var currentDominantSpeaker = null;
  17. var lastNCount = config.channelLastN;
  18. var localLastNCount = config.channelLastN;
  19. var localLastNSet = [];
  20. var lastNEndpointsCache = [];
  21. var lastNPickupJid = null;
  22. var eventEmitter = null;
  23. /**
  24. * Currently focused video jid
  25. * @type {String}
  26. */
  27. var focusedVideoResourceJid = null;
  28. var VideoLayout = (function (my) {
  29. my.init = function (emitter) {
  30. eventEmitter = emitter;
  31. localVideoThumbnail = new LocalVideo(VideoLayout);
  32. if (interfaceConfig.filmStripOnly) {
  33. LargeVideo.disable();
  34. } else {
  35. LargeVideo.init(VideoLayout, emitter);
  36. }
  37. VideoLayout.resizeLargeVideoContainer();
  38. };
  39. my.isInLastN = function(resource) {
  40. return lastNCount < 0 || // lastN is disabled
  41. // lastNEndpoints cache not built yet
  42. (lastNCount > 0 && !lastNEndpointsCache.length) ||
  43. (lastNEndpointsCache &&
  44. lastNEndpointsCache.indexOf(resource) !== -1);
  45. };
  46. my.changeLocalAudio = function(stream, isMuted) {
  47. if (isMuted)
  48. APP.UI.setAudioMuted(true, true);
  49. APP.RTC.attachMediaStream($('#localAudio'), stream.getOriginalStream());
  50. var localAudio = document.getElementById('localAudio');
  51. // Writing volume not allowed in IE
  52. if (!RTCBrowserType.isIExplorer()) {
  53. localAudio.autoplay = true;
  54. localAudio.volume = 0;
  55. }
  56. // Now when Temasys plugin is converting also <audio> elements to
  57. // plugin's <object>s, in current layout it will capture click events
  58. // before it reaches the local video object. We hide it here in order
  59. // to prevent that.
  60. if (RTCBrowserType.isIExplorer()) {
  61. // The issue is not present on Safari. Also if we hide it in Safari
  62. // then the local audio track will have 'enabled' flag set to false
  63. // which will result in audio mute issues
  64. $('#localAudio').hide();
  65. }
  66. };
  67. my.changeLocalVideo = function(stream, isMuted) {
  68. // Set default display name.
  69. localVideoThumbnail.setDisplayName();
  70. localVideoThumbnail.createConnectionIndicator();
  71. this.onVideoTypeChanged(APP.xmpp.myResource(), stream.videoType);
  72. AudioLevels.updateAudioLevelCanvas(null, VideoLayout);
  73. localVideoThumbnail.changeVideo(stream, isMuted);
  74. /* force update if we're currently being displayed */
  75. if (LargeVideo.isCurrentlyOnLarge(APP.xmpp.myResource())) {
  76. LargeVideo.updateLargeVideo(APP.xmpp.myResource(), true);
  77. }
  78. };
  79. my.mucJoined = function () {
  80. var myResourceJid = APP.xmpp.myResource();
  81. localVideoThumbnail.joined(APP.xmpp.myJid());
  82. if (!LargeVideo.getResourceJid())
  83. LargeVideo.updateLargeVideo(myResourceJid, true);
  84. };
  85. /**
  86. * Adds or removes icons for not available camera and microphone.
  87. * @param resourceJid the jid of user
  88. * @param devices available devices
  89. */
  90. my.setDeviceAvailabilityIcons = function (resourceJid, devices) {
  91. if(!devices)
  92. return;
  93. if(!resourceJid) {
  94. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  95. } else {
  96. if(remoteVideos[resourceJid])
  97. remoteVideos[resourceJid].setDeviceAvailabilityIcons(devices);
  98. }
  99. };
  100. /**
  101. * Checks if removed video is currently displayed and tries to display
  102. * another one instead.
  103. */
  104. my.updateRemovedVideo = function(resourceJid) {
  105. var newResourceJid;
  106. if (resourceJid === LargeVideo.getResourceJid()) {
  107. // We'll show user's avatar if he is the dominant speaker or if
  108. // his video thumbnail is pinned
  109. if (remoteVideos[resourceJid] &&
  110. resourceJid === focusedVideoResourceJid ||
  111. resourceJid === currentDominantSpeaker) {
  112. newResourceJid = resourceJid;
  113. } else {
  114. // Otherwise select last visible video
  115. newResourceJid = this.electLastVisibleVideo();
  116. }
  117. LargeVideo.updateLargeVideo(newResourceJid);
  118. }
  119. };
  120. my.electLastVisibleVideo = function () {
  121. // pick the last visible video in the row
  122. // if nobody else is left, this picks the local video
  123. var jid;
  124. var pick = $('#remoteVideos>span[id!="mixedstream"]:visible:last');
  125. if (pick.length) {
  126. jid = VideoLayout.getPeerContainerResourceJid(pick[0]);
  127. if (!remoteVideos[jid]) {
  128. // The RemoteVideo was removed (but the DOM elements may still
  129. // exist).
  130. jid = null;
  131. }
  132. }
  133. if (!jid) {
  134. console.info("Last visible video no longer exists");
  135. pick = $('#remoteVideos>span[id!="mixedstream"]');
  136. if (pick.length) {
  137. jid = VideoLayout.getPeerContainerResourceJid(pick[0]);
  138. if (!remoteVideos[jid]) {
  139. // The RemoteVideo was removed (but the DOM elements may
  140. // still exist).
  141. jid = null;
  142. }
  143. }
  144. }
  145. if (!jid) {
  146. // Go with local video
  147. console.info("Fallback to local video...");
  148. jid = APP.xmpp.myResource();
  149. }
  150. console.info("electLastVisibleVideo: " + jid);
  151. return jid;
  152. };
  153. my.onRemoteStreamAdded = function (stream) {
  154. if (stream.peerjid) {
  155. VideoLayout.ensurePeerContainerExists(stream.peerjid);
  156. var resourceJid = Strophe.getResourceFromJid(stream.peerjid);
  157. remoteVideos[resourceJid].addRemoteStreamElement(stream);
  158. }
  159. };
  160. my.getLargeVideoResource = function () {
  161. return LargeVideo.getResourceJid();
  162. };
  163. /**
  164. * Called when large video update is finished
  165. * @param currentSmallVideo small video currently displayed on large video
  166. */
  167. my.largeVideoUpdated = function (currentSmallVideo) {
  168. // Makes sure that dominant speaker UI
  169. // is enabled only on current small video
  170. localVideoThumbnail.enableDominantSpeaker(
  171. localVideoThumbnail === currentSmallVideo);
  172. Object.keys(remoteVideos).forEach(
  173. function (resourceJid) {
  174. var remoteVideo = remoteVideos[resourceJid];
  175. if (remoteVideo) {
  176. remoteVideo.enableDominantSpeaker(
  177. remoteVideo === currentSmallVideo);
  178. }
  179. }
  180. );
  181. };
  182. my.handleVideoThumbClicked = function(noPinnedEndpointChangedEvent,
  183. resourceJid) {
  184. if(focusedVideoResourceJid) {
  185. var oldSmallVideo
  186. = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  187. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  188. oldSmallVideo.focus(false);
  189. }
  190. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  191. // Unlock current focused.
  192. if (focusedVideoResourceJid === resourceJid)
  193. {
  194. focusedVideoResourceJid = null;
  195. // Enable the currently set dominant speaker.
  196. if (currentDominantSpeaker) {
  197. if(smallVideo && smallVideo.hasVideo()) {
  198. LargeVideo.updateLargeVideo(currentDominantSpeaker);
  199. }
  200. }
  201. if (!noPinnedEndpointChangedEvent) {
  202. eventEmitter.emit(UIEvents.PINNED_ENDPOINT);
  203. }
  204. return;
  205. }
  206. // Lock new video
  207. focusedVideoResourceJid = resourceJid;
  208. // Update focused/pinned interface.
  209. if (resourceJid) {
  210. if (smallVideo && !interfaceConfig.filmStripOnly)
  211. smallVideo.focus(true);
  212. if (!noPinnedEndpointChangedEvent) {
  213. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, resourceJid);
  214. }
  215. }
  216. LargeVideo.setState("video");
  217. LargeVideo.updateLargeVideo(resourceJid);
  218. // Writing volume not allowed in IE
  219. if (!RTCBrowserType.isIExplorer()) {
  220. $('audio').each(function (idx, el) {
  221. el.volume = 0;
  222. el.volume = 1;
  223. });
  224. }
  225. };
  226. /**
  227. * Checks if container for participant identified by given peerJid exists
  228. * in the document and creates it eventually.
  229. *
  230. * @param peerJid peer Jid to check.
  231. *
  232. * @return Returns <tt>true</tt> if the peer container exists,
  233. * <tt>false</tt> - otherwise
  234. */
  235. my.ensurePeerContainerExists = function(peerJid) {
  236. ContactList.ensureAddContact(peerJid);
  237. var resourceJid = Strophe.getResourceFromJid(peerJid);
  238. if (!remoteVideos[resourceJid]) {
  239. var remoteVideo = new RemoteVideo(peerJid, VideoLayout);
  240. remoteVideos[resourceJid] = remoteVideo;
  241. var videoType = remoteVideoTypes[resourceJid];
  242. if (videoType) {
  243. remoteVideo.setVideoType(videoType);
  244. }
  245. // In case this is not currently in the last n we don't show it.
  246. if (localLastNCount &&
  247. localLastNCount > 0 &&
  248. $('#remoteVideos>span').length >= localLastNCount + 2) {
  249. remoteVideo.showPeerContainer('hide');
  250. }
  251. else
  252. VideoLayout.resizeThumbnails();
  253. }
  254. };
  255. my.inputDisplayNameHandler = function (name) {
  256. localVideoThumbnail.inputDisplayNameHandler(name);
  257. };
  258. my.videoactive = function (videoelem, resourceJid) {
  259. console.info(resourceJid + " video is now active");
  260. videoelem.show();
  261. VideoLayout.resizeThumbnails();
  262. // Update the large video to the last added video only if there's no
  263. // current dominant, focused speaker or prezi playing or update it to
  264. // the current dominant speaker.
  265. if ((!focusedVideoResourceJid &&
  266. !currentDominantSpeaker &&
  267. !require("../prezi/Prezi").isPresentationVisible()) ||
  268. focusedVideoResourceJid === resourceJid ||
  269. (resourceJid &&
  270. currentDominantSpeaker === resourceJid)) {
  271. LargeVideo.updateLargeVideo(resourceJid, true);
  272. }
  273. };
  274. /**
  275. * Shows the presence status message for the given video.
  276. */
  277. my.setPresenceStatus = function (resourceJid, statusMsg) {
  278. remoteVideos[resourceJid].setPresenceStatus(statusMsg);
  279. };
  280. /**
  281. * Shows a visual indicator for the moderator of the conference.
  282. */
  283. my.showModeratorIndicator = function () {
  284. var isModerator = APP.xmpp.isModerator();
  285. if (isModerator) {
  286. localVideoThumbnail.createModeratorIndicatorElement();
  287. }
  288. var members = APP.xmpp.getMembers();
  289. Object.keys(members).forEach(function (jid) {
  290. var resourceJid = Strophe.getResourceFromJid(jid);
  291. var member = members[jid];
  292. if (member.isFocus) {
  293. // Skip server side focus
  294. return;
  295. }
  296. if (member.role === 'moderator') {
  297. remoteVideos[resourceJid].removeRemoteVideoMenu();
  298. remoteVideos[resourceJid].createModeratorIndicatorElement();
  299. } else if (isModerator) {
  300. // We are moderator, but user is not - add menu
  301. if ($('#remote_popupmenu_' + resourceJid).length <= 0) {
  302. remoteVideos[resourceJid].addRemoteVideoMenu();
  303. }
  304. }
  305. });
  306. };
  307. /*
  308. * Shows or hides the audio muted indicator over the local thumbnail video.
  309. * @param {boolean} isMuted
  310. */
  311. my.showLocalAudioIndicator = function(isMuted) {
  312. localVideoThumbnail.showAudioIndicator(isMuted);
  313. };
  314. /**
  315. * Resizes the large video container.
  316. */
  317. my.resizeLargeVideoContainer = function () {
  318. if(LargeVideo.isEnabled()) {
  319. LargeVideo.resize();
  320. } else {
  321. VideoLayout.resizeVideoSpace();
  322. }
  323. VideoLayout.resizeThumbnails();
  324. LargeVideo.position();
  325. };
  326. /**
  327. * Resizes thumbnails.
  328. */
  329. my.resizeThumbnails = function(animate) {
  330. var videoSpaceWidth = $('#remoteVideos').width();
  331. var thumbnailSize = VideoLayout.calculateThumbnailSize(videoSpaceWidth);
  332. var width = thumbnailSize[0];
  333. var height = thumbnailSize[1];
  334. $('.userAvatar').css('left', (width - height) / 2);
  335. if(animate) {
  336. $('#remoteVideos').animate({
  337. // adds 2 px because of small video 1px border
  338. height: height + 2
  339. },
  340. {
  341. queue: false,
  342. duration: 500
  343. });
  344. $('#remoteVideos>span').animate({
  345. height: height,
  346. width: width
  347. },
  348. {
  349. queue: false,
  350. duration: 500,
  351. complete: function () {
  352. $(document).trigger(
  353. "remotevideo.resized",
  354. [width,
  355. height]);
  356. }
  357. });
  358. } else {
  359. // size videos so that while keeping AR and max height, we have a
  360. // nice fit
  361. // adds 2 px because of small video 1px border
  362. $('#remoteVideos').height(height + 2);
  363. $('#remoteVideos>span').width(width);
  364. $('#remoteVideos>span').height(height);
  365. $(document).trigger("remotevideo.resized", [width, height]);
  366. }
  367. };
  368. /**
  369. * Calculates the thumbnail size.
  370. *
  371. * @param videoSpaceWidth the width of the video space
  372. */
  373. my.calculateThumbnailSize = function (videoSpaceWidth) {
  374. // Calculate the available height, which is the inner window height
  375. // minus 39px for the header minus 2px for the delimiter lines on the
  376. // top and bottom of the large video, minus the 36px space inside the
  377. // remoteVideos container used for highlighting shadow.
  378. var availableHeight = 100;
  379. var numvids = $('#remoteVideos>span:visible').length;
  380. if (localLastNCount && localLastNCount > 0) {
  381. numvids = Math.min(localLastNCount + 1, numvids);
  382. }
  383. // Remove the 3px borders arround videos and border around the remote
  384. // videos area and the 4 pixels between the local video and the others
  385. //TODO: Find out where the 4 pixels come from and remove them
  386. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  387. var availableWidth = availableWinWidth / numvids;
  388. var aspectRatio = 16.0 / 9.0;
  389. var maxHeight = Math.min(160, availableHeight);
  390. availableHeight
  391. = Math.min( maxHeight,
  392. availableWidth / aspectRatio,
  393. window.innerHeight - 18);
  394. if (availableHeight < availableWidth / aspectRatio) {
  395. availableWidth = Math.floor(availableHeight * aspectRatio);
  396. }
  397. return [availableWidth, availableHeight];
  398. };
  399. /**
  400. * Returns the corresponding resource jid to the given peer container
  401. * DOM element.
  402. *
  403. * @return the corresponding resource jid to the given peer container
  404. * DOM element
  405. */
  406. my.getPeerContainerResourceJid = function (containerElement) {
  407. if (localVideoThumbnail.container === containerElement) {
  408. return localVideoThumbnail.getResourceJid();
  409. }
  410. var i = containerElement.id.indexOf('participant_');
  411. if (i >= 0)
  412. return containerElement.id.substring(i + 12);
  413. };
  414. /**
  415. * On contact list item clicked.
  416. */
  417. $(ContactList).bind('contactclicked', function(event, jid) {
  418. if (!jid) {
  419. return;
  420. }
  421. if (jid === APP.xmpp.myJid()) {
  422. $("#localVideoContainer").click();
  423. return;
  424. }
  425. var resource = Strophe.getResourceFromJid(jid);
  426. var remoteVideo = remoteVideos[resource];
  427. if (remoteVideo && remoteVideo.selectVideoElement().length) {
  428. var videoThumb = remoteVideo.selectVideoElement()[0];
  429. // It is not always the case that a videoThumb exists (if there is
  430. // no actual video).
  431. if (RTC.getVideoSrc(videoThumb)) {
  432. // We have a video src, great! Let's update the large video
  433. // now.
  434. VideoLayout.handleVideoThumbClicked(
  435. false,
  436. Strophe.getResourceFromJid(jid));
  437. } else {
  438. // If we don't have a video src for jid, there's absolutely
  439. // no point in calling handleVideoThumbClicked; Quite
  440. // simply, it won't work because it needs an src to attach
  441. // to the large video.
  442. //
  443. // Instead, we trigger the pinned endpoint changed event to
  444. // let the bridge adjust its lastN set for myjid and store
  445. // the pinned user in the lastNPickupJid variable to be
  446. // picked up later by the lastN changed event handler.
  447. lastNPickupJid = jid;
  448. eventEmitter.emit(UIEvents.PINNED_ENDPOINT,
  449. Strophe.getResourceFromJid(jid));
  450. }
  451. }
  452. });
  453. /**
  454. * On audio muted event.
  455. */
  456. my.onAudioMute = function (jid, isMuted) {
  457. var resourceJid = Strophe.getResourceFromJid(jid);
  458. if (resourceJid === APP.xmpp.myResource()) {
  459. localVideoThumbnail.showAudioIndicator(isMuted);
  460. } else {
  461. VideoLayout.ensurePeerContainerExists(jid);
  462. remoteVideos[resourceJid].showAudioIndicator(isMuted);
  463. if (APP.xmpp.isModerator()) {
  464. remoteVideos[resourceJid].updateRemoteVideoMenu(isMuted);
  465. }
  466. }
  467. };
  468. /**
  469. * On video muted event.
  470. */
  471. my.onVideoMute = function (jid, value) {
  472. if (jid !== APP.xmpp.myJid() &&
  473. !APP.RTC.muteRemoteVideoStream(jid, value))
  474. return;
  475. if (jid === APP.xmpp.myJid()) {
  476. localVideoThumbnail.showVideoIndicator(value);
  477. } else {
  478. var resource = Strophe.getResourceFromJid(jid);
  479. VideoLayout.ensurePeerContainerExists(jid);
  480. var remoteVideo = remoteVideos[resource];
  481. remoteVideo.showVideoIndicator(value);
  482. var el = remoteVideo.selectVideoElement();
  483. if (!value)
  484. el.show();
  485. else
  486. el.hide();
  487. }
  488. };
  489. /**
  490. * Display name changed.
  491. */
  492. my.onDisplayNameChanged =
  493. function (jid, displayName, status) {
  494. if (jid === 'localVideoContainer' ||
  495. jid === APP.xmpp.myJid()) {
  496. localVideoThumbnail.setDisplayName(displayName);
  497. } else {
  498. VideoLayout.ensurePeerContainerExists(jid);
  499. remoteVideos[Strophe.getResourceFromJid(jid)].setDisplayName(
  500. displayName,
  501. status);
  502. }
  503. };
  504. /**
  505. * On dominant speaker changed event.
  506. */
  507. my.onDominantSpeakerChanged = function (resourceJid) {
  508. // We ignore local user events.
  509. if (resourceJid === APP.xmpp.myResource())
  510. return;
  511. var remoteVideo = remoteVideos[resourceJid];
  512. var members = APP.xmpp.getMembers();
  513. // Update the current dominant speaker.
  514. if (resourceJid !== currentDominantSpeaker) {
  515. var currentJID = APP.xmpp.findJidFromResource(currentDominantSpeaker);
  516. var newJID = APP.xmpp.findJidFromResource(resourceJid);
  517. if (currentDominantSpeaker && (!members || !members[currentJID] ||
  518. !members[currentJID].displayName) && remoteVideo) {
  519. remoteVideo.setDisplayName(null);
  520. }
  521. if (resourceJid && (!members || !members[newJID] ||
  522. !members[newJID].displayName) && remoteVideo) {
  523. remoteVideo.setDisplayName(null,
  524. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  525. }
  526. currentDominantSpeaker = resourceJid;
  527. } else {
  528. return;
  529. }
  530. if (!remoteVideo)
  531. return;
  532. // Obtain container for new dominant speaker.
  533. var videoSel = remoteVideo.selectVideoElement();
  534. // Local video will not have container found, but that's ok
  535. // since we don't want to switch to local video.
  536. if (!focusedVideoResourceJid && videoSel.length) {
  537. // Update the large video if the video source is already available,
  538. // otherwise wait for the "videoactive.jingle" event.
  539. if (videoSel[0].currentTime > 0) {
  540. LargeVideo.updateLargeVideo(resourceJid);
  541. }
  542. }
  543. };
  544. /**
  545. * On last N change event.
  546. *
  547. * @param lastNEndpoints the list of last N endpoints
  548. * @param endpointsEnteringLastN the list currently entering last N
  549. * endpoints
  550. */
  551. my.onLastNEndpointsChanged = function (lastNEndpoints,
  552. endpointsEnteringLastN,
  553. stream) {
  554. if (lastNCount !== lastNEndpoints.length)
  555. lastNCount = lastNEndpoints.length;
  556. lastNEndpointsCache = lastNEndpoints;
  557. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  558. //
  559. // If LastN drops to, say, 2, because of adaptivity, then E should see
  560. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  561. // so E sees them. C is only in E's local LastN set.
  562. //
  563. // If F starts talking and LastN = 3, then E should see thumbnails for
  564. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  565. // enters E's local LastN ejecting C.
  566. // Increase the local LastN set size, if necessary.
  567. if (lastNCount > localLastNCount) {
  568. localLastNCount = lastNCount;
  569. }
  570. // Update the local LastN set preserving the order in which the
  571. // endpoints appeared in the LastN/local LastN set.
  572. var nextLocalLastNSet = lastNEndpoints.slice(0);
  573. for (var i = 0; i < localLastNSet.length; i++) {
  574. if (nextLocalLastNSet.length >= localLastNCount) {
  575. break;
  576. }
  577. var resourceJid = localLastNSet[i];
  578. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  579. nextLocalLastNSet.push(resourceJid);
  580. }
  581. }
  582. localLastNSet = nextLocalLastNSet;
  583. var updateLargeVideo = false;
  584. // Handle LastN/local LastN changes.
  585. $('#remoteVideos>span').each(function( index, element ) {
  586. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  587. var isReceived = true;
  588. if (resourceJid &&
  589. lastNEndpoints.indexOf(resourceJid) < 0 &&
  590. localLastNSet.indexOf(resourceJid) < 0) {
  591. console.log("Remove from last N", resourceJid);
  592. if (remoteVideos[resourceJid])
  593. remoteVideos[resourceJid].showPeerContainer('hide');
  594. else if (APP.xmpp.myResource() !== resourceJid)
  595. console.error("No remote video for: " + resourceJid);
  596. isReceived = false;
  597. } else if (resourceJid &&
  598. $('#participant_' + resourceJid).is(':visible') &&
  599. lastNEndpoints.indexOf(resourceJid) < 0 &&
  600. localLastNSet.indexOf(resourceJid) >= 0) {
  601. if (remoteVideos[resourceJid])
  602. remoteVideos[resourceJid].showPeerContainer('avatar');
  603. else if (APP.xmpp.myResource() !== resourceJid)
  604. console.error("No remote video for: " + resourceJid);
  605. isReceived = false;
  606. }
  607. if (!isReceived) {
  608. // resourceJid has dropped out of the server side lastN set, so
  609. // it is no longer being received. If resourceJid was being
  610. // displayed in the large video we have to switch to another
  611. // user.
  612. if (!updateLargeVideo &&
  613. resourceJid === LargeVideo.getResourceJid()) {
  614. updateLargeVideo = true;
  615. }
  616. }
  617. });
  618. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  619. endpointsEnteringLastN = lastNEndpoints;
  620. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  621. endpointsEnteringLastN.forEach(function (resourceJid) {
  622. var isVisible = $('#participant_' + resourceJid).is(':visible');
  623. var remoteVideo = remoteVideos[resourceJid];
  624. remoteVideo.showPeerContainer('show');
  625. if (!isVisible) {
  626. console.log("Add to last N", resourceJid);
  627. var jid = APP.xmpp.findJidFromResource(resourceJid);
  628. var mediaStream =
  629. APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  630. var sel = remoteVideo.selectVideoElement();
  631. APP.RTC.attachMediaStream(sel, mediaStream.stream);
  632. if (lastNPickupJid == mediaStream.peerjid) {
  633. // Clean up the lastN pickup jid.
  634. lastNPickupJid = null;
  635. // Don't fire the events again, they've already
  636. // been fired in the contact list click handler.
  637. VideoLayout.handleVideoThumbClicked(
  638. false,
  639. Strophe.getResourceFromJid(mediaStream.peerjid));
  640. updateLargeVideo = false;
  641. }
  642. remoteVideos[resourceJid].
  643. waitForPlayback(sel, mediaStream);
  644. }
  645. });
  646. }
  647. // The endpoint that was being shown in the large video has dropped out
  648. // of the lastN set and there was no lastN pickup jid. We need to update
  649. // the large video now.
  650. if (updateLargeVideo) {
  651. var resource;
  652. var myResource
  653. = APP.xmpp.myResource();
  654. // Find out which endpoint to show in the large video.
  655. for (i = 0; i < lastNEndpoints.length; i++) {
  656. resource = lastNEndpoints[i];
  657. if (!resource || resource === myResource)
  658. continue;
  659. // videoSrcToSsrc needs to be update for this call to succeed.
  660. LargeVideo.updateLargeVideo(resource);
  661. break;
  662. }
  663. }
  664. };
  665. /**
  666. * Updates local stats
  667. * @param percent
  668. * @param object
  669. */
  670. my.updateLocalConnectionStats = function (percent, object) {
  671. var resolution = null;
  672. if (object.resolution !== null) {
  673. resolution = object.resolution;
  674. object.resolution = resolution[APP.xmpp.myJid()];
  675. delete resolution[APP.xmpp.myJid()];
  676. }
  677. localVideoThumbnail.updateStatsIndicator(percent, object);
  678. for (var jid in resolution) {
  679. if (resolution[jid] === null)
  680. continue;
  681. var resourceJid = Strophe.getResourceFromJid(jid);
  682. if (remoteVideos[resourceJid] &&
  683. remoteVideos[resourceJid].connectionIndicator) {
  684. remoteVideos[resourceJid].connectionIndicator.
  685. updateResolution(resolution[jid]);
  686. }
  687. }
  688. };
  689. /**
  690. * Updates remote stats.
  691. * @param jid the jid associated with the stats
  692. * @param percent the connection quality percent
  693. * @param object the stats data
  694. */
  695. my.updateConnectionStats = function (jid, percent, object) {
  696. var resourceJid = Strophe.getResourceFromJid(jid);
  697. if (remoteVideos[resourceJid])
  698. remoteVideos[resourceJid].updateStatsIndicator(percent, object);
  699. };
  700. /**
  701. * Hides the connection indicator
  702. * @param jid
  703. */
  704. my.hideConnectionIndicator = function (jid) {
  705. remoteVideos[Strophe.getResourceFromJid(jid)].hideConnectionIndicator();
  706. };
  707. /**
  708. * Hides all the indicators
  709. */
  710. my.onStatsStop = function () {
  711. for(var video in remoteVideos) {
  712. remoteVideos[video].hideIndicator();
  713. }
  714. localVideoThumbnail.hideIndicator();
  715. };
  716. my.participantLeft = function (jid) {
  717. // Unlock large video
  718. var resourceJid = Strophe.getResourceFromJid(jid);
  719. if (focusedVideoResourceJid === resourceJid) {
  720. console.info("Focused video owner has left the conference");
  721. focusedVideoResourceJid = null;
  722. }
  723. if (currentDominantSpeaker === resourceJid) {
  724. console.info("Dominant speaker has left the conference");
  725. currentDominantSpeaker = null;
  726. }
  727. var remoteVideo = remoteVideos[resourceJid];
  728. if (remoteVideo) {
  729. // Remove remote video
  730. console.info("Removing remote video: " + resourceJid);
  731. delete remoteVideos[resourceJid];
  732. remoteVideo.remove();
  733. } else {
  734. console.warn("No remote video for " + resourceJid);
  735. }
  736. VideoLayout.resizeThumbnails();
  737. };
  738. my.onVideoTypeChanged = function (resourceJid, newVideoType) {
  739. if (remoteVideoTypes[resourceJid] === newVideoType) {
  740. return;
  741. }
  742. console.info("Peer video type changed: ", resourceJid, newVideoType);
  743. remoteVideoTypes[resourceJid] = newVideoType;
  744. var smallVideo;
  745. if (resourceJid === APP.xmpp.myResource()) {
  746. if (!localVideoThumbnail) {
  747. console.warn("Local video not ready yet");
  748. return;
  749. }
  750. smallVideo = localVideoThumbnail;
  751. } else if (remoteVideos[resourceJid]) {
  752. smallVideo = remoteVideos[resourceJid];
  753. } else {
  754. return;
  755. }
  756. smallVideo.setVideoType(newVideoType);
  757. LargeVideo.onVideoTypeChanged(resourceJid, newVideoType);
  758. };
  759. /**
  760. * Updates the video size and position when the film strip is toggled.
  761. *
  762. * @param isToggled indicates if the film strip is toggled or not. True
  763. * would mean that the film strip is hidden, false would mean it's shown
  764. */
  765. my.onFilmStripToggled = function(isToggled) {
  766. LargeVideo.updateVideoSizeAndPosition();
  767. LargeVideo.position(null, null, null, null, true);
  768. };
  769. my.showMore = function (jid) {
  770. if (jid === 'local') {
  771. localVideoThumbnail.connectionIndicator.showMore();
  772. } else {
  773. var remoteVideo = remoteVideos[Strophe.getResourceFromJid(jid)];
  774. if (remoteVideo) {
  775. remoteVideo.connectionIndicator.showMore();
  776. } else {
  777. console.info("Error - no remote video for jid: " + jid);
  778. }
  779. }
  780. };
  781. my.addPreziContainer = function (id) {
  782. var container = RemoteVideo.createContainer(id);
  783. VideoLayout.resizeThumbnails();
  784. return container;
  785. };
  786. my.setLargeVideoVisible = function (isVisible) {
  787. LargeVideo.setLargeVideoVisible(isVisible);
  788. if(!isVisible && focusedVideoResourceJid) {
  789. var smallVideo = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  790. if(smallVideo) {
  791. smallVideo.focus(false);
  792. smallVideo.showAvatar();
  793. }
  794. focusedVideoResourceJid = null;
  795. }
  796. };
  797. /**
  798. * Resizes the video area.
  799. *
  800. * @param isSideBarVisible indicates if the side bar is currently visible
  801. * @param callback a function to be called when the video space is
  802. * resized.
  803. */
  804. my.resizeVideoArea = function(isSideBarVisible, callback) {
  805. LargeVideo.resizeVideoAreaAnimated(isSideBarVisible, callback);
  806. VideoLayout.resizeThumbnails(true);
  807. };
  808. /**
  809. * Resizes the #videospace html element
  810. * @param animate boolean property that indicates whether the resize should
  811. * be animated or not.
  812. * @param isChatVisible boolean property that indicates whether the chat
  813. * area is displayed or not.
  814. * If that parameter is null the method will check the chat panel
  815. * visibility.
  816. * @param completeFunction a function to be called when the video space
  817. * is resized.
  818. */
  819. my.resizeVideoSpace = function (animate, isChatVisible, completeFunction) {
  820. var availableHeight = window.innerHeight;
  821. var availableWidth = UIUtil.getAvailableVideoWidth(isChatVisible);
  822. if (availableWidth < 0 || availableHeight < 0) return;
  823. if(animate) {
  824. $('#videospace').animate({
  825. right: window.innerWidth - availableWidth,
  826. width: availableWidth,
  827. height: availableHeight
  828. },
  829. {
  830. queue: false,
  831. duration: 500,
  832. complete: completeFunction
  833. });
  834. } else {
  835. $('#videospace').width(availableWidth);
  836. $('#videospace').height(availableHeight);
  837. }
  838. };
  839. my.getSmallVideo = function (resourceJid) {
  840. if(resourceJid == APP.xmpp.myResource()) {
  841. return localVideoThumbnail;
  842. } else {
  843. if(!remoteVideos[resourceJid])
  844. return null;
  845. return remoteVideos[resourceJid];
  846. }
  847. };
  848. my.userAvatarChanged = function(resourceJid, thumbUrl) {
  849. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  850. if(smallVideo)
  851. smallVideo.avatarChanged(thumbUrl);
  852. else
  853. console.warn(
  854. "Missed avatar update - no small video yet for " + resourceJid);
  855. LargeVideo.updateAvatar(resourceJid, thumbUrl);
  856. };
  857. my.createEtherpadIframe = function(src, onloadHandler)
  858. {
  859. return LargeVideo.createEtherpadIframe(src, onloadHandler);
  860. };
  861. my.setLargeVideoState = function (state) {
  862. LargeVideo.setState(state);
  863. };
  864. my.getLargeVideoState = function () {
  865. return LargeVideo.getState();
  866. };
  867. my.setLargeVideoHover = function (inHandler, outHandler) {
  868. LargeVideo.setHover(inHandler, outHandler);
  869. };
  870. /**
  871. * Indicates that the video has been interrupted.
  872. */
  873. my.onVideoInterrupted = function () {
  874. LargeVideo.enableVideoProblemFilter(true);
  875. var reconnectingKey = "connection.RECONNECTING";
  876. $('#videoConnectionMessage').attr("data-i18n", reconnectingKey);
  877. $('#videoConnectionMessage')
  878. .text(APP.translation.translateString(reconnectingKey));
  879. $('#videoConnectionMessage').css({display: "block"});
  880. };
  881. /**
  882. * Indicates that the video has been restored.
  883. */
  884. my.onVideoRestored = function () {
  885. LargeVideo.enableVideoProblemFilter(false);
  886. $('#videoConnectionMessage').css({display: "none"});
  887. };
  888. return my;
  889. }(VideoLayout || {}));
  890. module.exports = VideoLayout;