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 36KB

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