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.

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. /**
  374. * On contact list item clicked.
  375. */
  376. $(ContactList).bind('contactclicked', function(event, jid) {
  377. if (!jid) {
  378. return;
  379. }
  380. var resource = Strophe.getResourceFromJid(jid);
  381. var videoContainer = $("#participant_" + resource);
  382. if (videoContainer.length > 0) {
  383. var videoThumb
  384. = $(RTC.getVideoElementName(), videoContainer).get(0);
  385. // It is not always the case that a videoThumb exists (if there is
  386. // no actual video).
  387. if (videoThumb) {
  388. if (videoThumb.src && videoThumb.src != '') {
  389. // We have a video src, great! Let's update the large video
  390. // now.
  391. VideoLayout.handleVideoThumbClicked(
  392. false,
  393. Strophe.getResourceFromJid(jid));
  394. } else {
  395. // If we don't have a video src for jid, there's absolutely
  396. // no point in calling handleVideoThumbClicked; Quite
  397. // simply, it won't work because it needs an src to attach
  398. // to the large video.
  399. //
  400. // Instead, we trigger the pinned endpoint changed event to
  401. // let the bridge adjust its lastN set for myjid and store
  402. // the pinned user in the lastNPickupJid variable to be
  403. // picked up later by the lastN changed event handler.
  404. lastNPickupJid = jid;
  405. eventEmitter.emit(UIEvents.PINNED_ENDPOINT,
  406. Strophe.getResourceFromJid(jid));
  407. }
  408. } else if (jid == APP.xmpp.myJid()) {
  409. $("#localVideoContainer").click();
  410. }
  411. }
  412. });
  413. /**
  414. * On audio muted event.
  415. */
  416. my.onAudioMute = function (jid, isMuted) {
  417. var resourceJid = Strophe.getResourceFromJid(jid);
  418. if (resourceJid === APP.xmpp.myResource()) {
  419. localVideoThumbnail.showAudioIndicator(isMuted);
  420. } else {
  421. VideoLayout.ensurePeerContainerExists(jid);
  422. remoteVideos[resourceJid].showAudioIndicator(isMuted);
  423. if (APP.xmpp.isModerator()) {
  424. remoteVideos[resourceJid].updateRemoteVideoMenu(isMuted);
  425. }
  426. }
  427. };
  428. /**
  429. * On video muted event.
  430. */
  431. my.onVideoMute = function (jid, value) {
  432. if(jid !== APP.xmpp.myJid() && !APP.RTC.muteRemoteVideoStream(jid, value))
  433. return;
  434. if (jid === APP.xmpp.myJid()) {
  435. localVideoThumbnail.showVideoIndicator(value);
  436. } else {
  437. VideoLayout.ensurePeerContainerExists(jid);
  438. remoteVideos[Strophe.getResourceFromJid(jid)].showVideoIndicator(value);
  439. var el = $('#participant_' + Strophe.getResourceFromJid(jid)
  440. + '>' + APP.RTC.getVideoElementName());
  441. if (!value)
  442. el.show();
  443. else
  444. el.hide();
  445. }
  446. };
  447. /**
  448. * Display name changed.
  449. */
  450. my.onDisplayNameChanged =
  451. function (jid, displayName, status) {
  452. if (jid === 'localVideoContainer'
  453. || jid === APP.xmpp.myJid()) {
  454. localVideoThumbnail.setDisplayName(displayName);
  455. } else {
  456. VideoLayout.ensurePeerContainerExists(jid);
  457. remoteVideos[Strophe.getResourceFromJid(jid)].setDisplayName(
  458. displayName,
  459. status);
  460. }
  461. };
  462. /**
  463. * On dominant speaker changed event.
  464. */
  465. my.onDominantSpeakerChanged = function (resourceJid) {
  466. // We ignore local user events.
  467. if (resourceJid === APP.xmpp.myResource())
  468. return;
  469. var members = APP.xmpp.getMembers();
  470. // Update the current dominant speaker.
  471. if (resourceJid !== currentDominantSpeaker) {
  472. var currentJID = APP.xmpp.findJidFromResource(currentDominantSpeaker);
  473. var newJID = APP.xmpp.findJidFromResource(resourceJid);
  474. if(currentDominantSpeaker && (!members || !members[currentJID] ||
  475. !members[currentJID].displayName)) {
  476. remoteVideos[resourceJid].setDisplayName(null);
  477. }
  478. if(resourceJid && (!members || !members[newJID] ||
  479. !members[newJID].displayName)) {
  480. remoteVideos[resourceJid].setDisplayName(null,
  481. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  482. }
  483. currentDominantSpeaker = resourceJid;
  484. } else {
  485. return;
  486. }
  487. // Obtain container for new dominant speaker.
  488. var container = document.getElementById(
  489. 'participant_' + resourceJid);
  490. // Local video will not have container found, but that's ok
  491. // since we don't want to switch to local video.
  492. if (container && !focusedVideoResourceJid)
  493. {
  494. var video
  495. = container.getElementsByTagName(RTC.getVideoElementName());
  496. // Update the large video if the video source is already available,
  497. // otherwise wait for the "videoactive.jingle" event.
  498. if (video.length && video[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 = $('#participant_' + resourceJid +
  581. '>' + RTC.getVideoElementName());
  582. APP.RTC.attachMediaStream(sel, mediaStream.stream);
  583. if (lastNPickupJid == mediaStream.peerjid) {
  584. // Clean up the lastN pickup jid.
  585. lastNPickupJid = null;
  586. // Don't fire the events again, they've already
  587. // been fired in the contact list click handler.
  588. VideoLayout.handleVideoThumbClicked(
  589. false,
  590. Strophe.getResourceFromJid(mediaStream.peerjid));
  591. updateLargeVideo = false;
  592. }
  593. remoteVideos[resourceJid].waitForRemoteVideo(sel, mediaStream.ssrc, mediaStream.stream);
  594. }
  595. });
  596. }
  597. // The endpoint that was being shown in the large video has dropped out
  598. // of the lastN set and there was no lastN pickup jid. We need to update
  599. // the large video now.
  600. if (updateLargeVideo) {
  601. var resource, container, src;
  602. var myResource
  603. = APP.xmpp.myResource();
  604. // Find out which endpoint to show in the large video.
  605. for (var i = 0; i < lastNEndpoints.length; i++) {
  606. resource = lastNEndpoints[i];
  607. if (!resource || resource === myResource)
  608. continue;
  609. // videoSrcToSsrc needs to be update for this call to succeed.
  610. LargeVideo.updateLargeVideo(resource);
  611. break;
  612. }
  613. }
  614. };
  615. /**
  616. * Updates local stats
  617. * @param percent
  618. * @param object
  619. */
  620. my.updateLocalConnectionStats = function (percent, object) {
  621. var resolution = null;
  622. if(object.resolution !== null)
  623. {
  624. resolution = object.resolution;
  625. object.resolution = resolution[APP.xmpp.myJid()];
  626. delete resolution[APP.xmpp.myJid()];
  627. }
  628. localVideoThumbnail.updateStatsIndicator(percent, object);
  629. for(var jid in resolution)
  630. {
  631. if(resolution[jid] === null)
  632. continue;
  633. var resourceJid = Strophe.getResourceFromJid(jid);
  634. if(remoteVideos[resourceJid] && remoteVideos[resourceJid].connectionIndicator)
  635. {
  636. remoteVideos[resourceJid].connectionIndicator.updateResolution(resolution[jid]);
  637. }
  638. }
  639. };
  640. /**
  641. * Updates remote stats.
  642. * @param jid the jid associated with the stats
  643. * @param percent the connection quality percent
  644. * @param object the stats data
  645. */
  646. my.updateConnectionStats = function (jid, percent, object) {
  647. var resourceJid = Strophe.getResourceFromJid(jid);
  648. if(remoteVideos[resourceJid])
  649. remoteVideos[resourceJid].updateStatsIndicator(percent, object);
  650. };
  651. /**
  652. * Removes the connection
  653. * @param jid
  654. */
  655. my.removeConnectionIndicator = function (jid) {
  656. remoteVideos[Strophe.getResourceFromJid(jid)].removeConnectionIndicator();
  657. };
  658. /**
  659. * Hides the connection indicator
  660. * @param jid
  661. */
  662. my.hideConnectionIndicator = function (jid) {
  663. remoteVideos[Strophe.getResourceFromJid(jid)].hideConnectionIndicator();
  664. };
  665. /**
  666. * Hides all the indicators
  667. */
  668. my.onStatsStop = function () {
  669. for(var video in remoteVideos)
  670. {
  671. remoteVideos[video].hideIndicator();
  672. }
  673. localVideoThumbnail.hideIndicator();
  674. };
  675. my.participantLeft = function (jid) {
  676. // Unlock large video
  677. var resourceJid = Strophe.getResourceFromJid(jid);
  678. if (focusedVideoResourceJid === resourceJid)
  679. {
  680. console.info("Focused video owner has left the conference");
  681. focusedVideoResourceJid = null;
  682. }
  683. };
  684. my.onVideoTypeChanged = function (jid) {
  685. LargeVideo.onVideoTypeChanged(jid);
  686. };
  687. my.showMore = function (jid) {
  688. if(APP.xmpp.myJid = jid)
  689. {
  690. localVideoThumbnail.connectionIndicator.showMore();
  691. }
  692. else
  693. {
  694. remoteVideos[Strophe.getResourceFromJid(jid)].connectionIndicator.showMore();
  695. }
  696. };
  697. my.addPreziContainer = function (id) {
  698. return RemoteVideo.createContainer(id);
  699. };
  700. my.setLargeVideoVisible = function (isVisible) {
  701. LargeVideo.setLargeVideoVisible(isVisible);
  702. if(!isVisible && focusedVideoResourceJid)
  703. {
  704. var smallVideo = VideoLayout.getSmallVideo(focusedVideoResourceJid);
  705. if(smallVideo)
  706. smallVideo.focus(false);
  707. smallVideo.showAvatar();
  708. focusedVideoResourceJid = null;
  709. }
  710. };
  711. /**
  712. * Resizes the video area
  713. * @param completeFunction a function to be called when the video space is resized
  714. */
  715. my.resizeVideoArea = function(isVisible, completeFunction) {
  716. LargeVideo.resizeVideoAreaAnimated(isVisible, completeFunction);
  717. VideoLayout.resizeThumbnails(true);
  718. };
  719. my.getSmallVideo = function (resourceJid) {
  720. if(resourceJid == APP.xmpp.myResource())
  721. {
  722. return localVideoThumbnail;
  723. }
  724. else
  725. {
  726. if(!remoteVideos[resourceJid])
  727. return null;
  728. return remoteVideos[resourceJid];
  729. }
  730. };
  731. my.userAvatarChanged = function(resourceJid, thumbUrl)
  732. {
  733. var smallVideo = VideoLayout.getSmallVideo(resourceJid);
  734. if(smallVideo)
  735. smallVideo.avatarChanged(thumbUrl);
  736. LargeVideo.updateAvatar(resourceJid, thumbUrl);
  737. };
  738. return my;
  739. }(VideoLayout || {}));
  740. module.exports = VideoLayout;