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

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