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

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