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

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