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

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