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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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);
  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. };
  336. /**
  337. * Calculates the thumbnail size.
  338. *
  339. * @param videoSpaceWidth the width of the video space
  340. */
  341. my.calculateThumbnailSize = function (videoSpaceWidth) {
  342. // Calculate the available height, which is the inner window height minus
  343. // 39px for the header minus 2px for the delimiter lines on the top and
  344. // bottom of the large video, minus the 36px space inside the remoteVideos
  345. // container used for highlighting shadow.
  346. var availableHeight = 100;
  347. var numvids = $('#remoteVideos>span:visible').length;
  348. if (localLastNCount && localLastNCount > 0) {
  349. numvids = Math.min(localLastNCount + 1, numvids);
  350. }
  351. // Remove the 3px borders arround videos and border around the remote
  352. // videos area and the 4 pixels between the local video and the others
  353. //TODO: Find out where the 4 pixels come from and remove them
  354. var availableWinWidth = videoSpaceWidth - 2 * 3 * numvids - 70 - 4;
  355. var availableWidth = availableWinWidth / numvids;
  356. var aspectRatio = 16.0 / 9.0;
  357. var maxHeight = Math.min(160, availableHeight);
  358. availableHeight = Math.min(maxHeight, availableWidth / aspectRatio);
  359. if (availableHeight < availableWidth / aspectRatio) {
  360. availableWidth = Math.floor(availableHeight * aspectRatio);
  361. }
  362. return [availableWidth, availableHeight];
  363. };
  364. /**
  365. * Returns the current dominant speaker resource jid.
  366. */
  367. my.getDominantSpeakerResourceJid = function () {
  368. return currentDominantSpeaker;
  369. };
  370. /**
  371. * Returns the corresponding resource jid to the given peer container
  372. * DOM element.
  373. *
  374. * @return the corresponding resource jid to the given peer container
  375. * DOM element
  376. */
  377. my.getPeerContainerResourceJid = function (containerElement) {
  378. var i = containerElement.id.indexOf('participant_');
  379. if (i >= 0)
  380. return containerElement.id.substring(i + 12);
  381. };
  382. /**
  383. * On contact list item clicked.
  384. */
  385. $(ContactList).bind('contactclicked', function(event, jid) {
  386. if (!jid) {
  387. return;
  388. }
  389. var resource = Strophe.getResourceFromJid(jid);
  390. var videoContainer = $("#participant_" + resource);
  391. if (videoContainer.length > 0) {
  392. var videoThumb = $('video', videoContainer).get(0);
  393. // It is not always the case that a videoThumb exists (if there is
  394. // no actual video).
  395. if (videoThumb) {
  396. if (videoThumb.src && videoThumb.src != '') {
  397. // We have a video src, great! Let's update the large video
  398. // now.
  399. VideoLayout.handleVideoThumbClicked(
  400. false,
  401. Strophe.getResourceFromJid(jid));
  402. } else {
  403. // If we don't have a video src for jid, there's absolutely
  404. // no point in calling handleVideoThumbClicked; Quite
  405. // simply, it won't work because it needs an src to attach
  406. // to the large video.
  407. //
  408. // Instead, we trigger the pinned endpoint changed event to
  409. // let the bridge adjust its lastN set for myjid and store
  410. // the pinned user in the lastNPickupJid variable to be
  411. // picked up later by the lastN changed event handler.
  412. lastNPickupJid = jid;
  413. eventEmitter.emit(UIEvents.PINNED_ENDPOINT,
  414. Strophe.getResourceFromJid(jid));
  415. }
  416. } else if (jid == APP.xmpp.myJid()) {
  417. $("#localVideoContainer").click();
  418. }
  419. }
  420. });
  421. /**
  422. * On audio muted event.
  423. */
  424. my.onAudioMute = function (jid, isMuted) {
  425. var resourceJid = Strophe.getResourceFromJid(jid);
  426. if (resourceJid === APP.xmpp.myResource()) {
  427. localVideoThumbnail.showAudioIndicator(isMuted);
  428. } else {
  429. VideoLayout.ensurePeerContainerExists(jid);
  430. remoteVideos[resourceJid].showAudioIndicator(isMuted);
  431. if (APP.xmpp.isModerator()) {
  432. remoteVideos[resourceJid].updateRemoteVideoMenu(isMuted);
  433. }
  434. }
  435. };
  436. /**
  437. * On video muted event.
  438. */
  439. my.onVideoMute = function (jid, value) {
  440. if(jid !== APP.xmpp.myJid() && !APP.RTC.muteRemoteVideoStream(jid, value))
  441. return;
  442. if (jid === APP.xmpp.myJid()) {
  443. localVideoThumbnail.showVideoIndicator(value);
  444. } else {
  445. VideoLayout.ensurePeerContainerExists(jid);
  446. remoteVideos[Strophe.getResourceFromJid(jid)].showVideoIndicator(value);
  447. }
  448. };
  449. /**
  450. * Display name changed.
  451. */
  452. my.onDisplayNameChanged =
  453. function (jid, displayName, status) {
  454. if (jid === 'localVideoContainer'
  455. || jid === APP.xmpp.myJid()) {
  456. localVideoThumbnail.setDisplayName(displayName);
  457. } else {
  458. VideoLayout.ensurePeerContainerExists(jid);
  459. remoteVideos[Strophe.getResourceFromJid(jid)].setDisplayName(
  460. displayName,
  461. status);
  462. }
  463. };
  464. /**
  465. * On dominant speaker changed event.
  466. */
  467. my.onDominantSpeakerChanged = function (resourceJid) {
  468. // We ignore local user events.
  469. if (resourceJid === APP.xmpp.myResource())
  470. return;
  471. var members = APP.xmpp.getMembers();
  472. // Update the current dominant speaker.
  473. if (resourceJid !== currentDominantSpeaker) {
  474. var currentJID = APP.xmpp.findJidFromResource(currentDominantSpeaker);
  475. var newJID = APP.xmpp.findJidFromResource(resourceJid);
  476. if(currentDominantSpeaker && (!members || !members[currentJID] ||
  477. !members[currentJID].displayName)) {
  478. remoteVideos[resourceJid].setDisplayName(null);
  479. }
  480. if(resourceJid && (!members || !members[newJID] ||
  481. !members[newJID].displayName)) {
  482. remoteVideos[resourceJid].setDisplayName(null,
  483. interfaceConfig.DEFAULT_DOMINANT_SPEAKER_DISPLAY_NAME);
  484. }
  485. currentDominantSpeaker = resourceJid;
  486. } else {
  487. return;
  488. }
  489. // Obtain container for new dominant speaker.
  490. var container = document.getElementById(
  491. 'participant_' + 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 (container && !focusedVideoResourceJid)
  495. {
  496. var video = container.getElementsByTagName("video");
  497. // Update the large video if the video source is already available,
  498. // otherwise wait for the "videoactive.jingle" event.
  499. if (video.length && video[0].currentTime > 0) {
  500. LargeVideo.updateLargeVideo(resourceJid);
  501. }
  502. }
  503. };
  504. /**
  505. * On last N change event.
  506. *
  507. * @param lastNEndpoints the list of last N endpoints
  508. * @param endpointsEnteringLastN the list currently entering last N
  509. * endpoints
  510. */
  511. my.onLastNEndpointsChanged = function ( lastNEndpoints,
  512. endpointsEnteringLastN,
  513. stream) {
  514. if (lastNCount !== lastNEndpoints.length)
  515. lastNCount = lastNEndpoints.length;
  516. lastNEndpointsCache = lastNEndpoints;
  517. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  518. //
  519. // If LastN drops to, say, 2, because of adaptivity, then E should see
  520. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  521. // so E sees them. C is only in E's local LastN set.
  522. //
  523. // If F starts talking and LastN = 3, then E should see thumbnails for
  524. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  525. // enters E's local LastN ejecting C.
  526. // Increase the local LastN set size, if necessary.
  527. if (lastNCount > localLastNCount) {
  528. localLastNCount = lastNCount;
  529. }
  530. // Update the local LastN set preserving the order in which the
  531. // endpoints appeared in the LastN/local LastN set.
  532. var nextLocalLastNSet = lastNEndpoints.slice(0);
  533. for (var i = 0; i < localLastNSet.length; i++) {
  534. if (nextLocalLastNSet.length >= localLastNCount) {
  535. break;
  536. }
  537. var resourceJid = localLastNSet[i];
  538. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  539. nextLocalLastNSet.push(resourceJid);
  540. }
  541. }
  542. localLastNSet = nextLocalLastNSet;
  543. var updateLargeVideo = false;
  544. // Handle LastN/local LastN changes.
  545. $('#remoteVideos>span').each(function( index, element ) {
  546. var resourceJid = VideoLayout.getPeerContainerResourceJid(element);
  547. var isReceived = true;
  548. if (resourceJid
  549. && lastNEndpoints.indexOf(resourceJid) < 0
  550. && localLastNSet.indexOf(resourceJid) < 0) {
  551. console.log("Remove from last N", resourceJid);
  552. remoteVideos[resourceJid].showPeerContainer('hide');
  553. isReceived = false;
  554. } else if (resourceJid
  555. && $('#participant_' + resourceJid).is(':visible')
  556. && lastNEndpoints.indexOf(resourceJid) < 0
  557. && localLastNSet.indexOf(resourceJid) >= 0) {
  558. remoteVideos[resourceJid].showPeerContainer('avatar');
  559. isReceived = false;
  560. }
  561. if (!isReceived) {
  562. // resourceJid has dropped out of the server side lastN set, so
  563. // it is no longer being received. If resourceJid was being
  564. // displayed in the large video we have to switch to another
  565. // user.
  566. if (!updateLargeVideo && resourceJid === LargeVideo.getResourceJid()) {
  567. updateLargeVideo = true;
  568. }
  569. }
  570. });
  571. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  572. endpointsEnteringLastN = lastNEndpoints;
  573. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  574. endpointsEnteringLastN.forEach(function (resourceJid) {
  575. var isVisible = $('#participant_' + resourceJid).is(':visible');
  576. remoteVideos[resourceJid].showPeerContainer('show');
  577. if (!isVisible) {
  578. console.log("Add to last N", resourceJid);
  579. var jid = APP.xmpp.findJidFromResource(resourceJid);
  580. var mediaStream = APP.RTC.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  581. var sel = $('#participant_' + resourceJid + '>video');
  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();
  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;