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.

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